diff --git a/docs/job-trigger-context.md b/docs/job-trigger-context.md index 3743c10c..deccbb31 100644 --- a/docs/job-trigger-context.md +++ b/docs/job-trigger-context.md @@ -28,3 +28,5 @@ Manual invocation accepts an optional display `name`, `prompt`, JSON object `dat Run inspection returns `context` as the complete persisted object and `invocation.trigger_id` as the trigger that fired. Parameter-era stored Jobs are migrated: old context and parameters are retained beneath `legacy_context` and `legacy_parameters` in the corresponding trigger context. They are not used as global template variables. Every webhook trigger may also define `handlerCode` containing one synchronous `function handler(webhook)` declaration. Factorize runs it only after provider authentication and trigger scoping, inside an isolated Worker with no bindings or outbound network access. Returning `false` rejects the delivery, `true` retains the default value above, and a JSON-compatible object replaces that value beneath the stable trigger slug. Timeouts, exceptions, and invalid return values are recorded in webhook activity and never create a run. REST `POST /api/v1/job-handlers/test` and MCP `test_job_webhook_handler` exercise the same isolation boundary without creating an invocation. + +Prompt and run-name templates render plain text: double-brace substitutions preserve URLs, quotes, Markdown, and code without HTML entity escaping. Triple-brace substitutions remain supported. Literal entity text in job context is preserved, not decoded. The web UI escapes rendered values when displaying them. This applies to new runs; existing persisted prompts are unchanged. diff --git a/packages/app/src/job-domain.ts b/packages/app/src/job-domain.ts index 4cd8f260..cd536c6b 100644 --- a/packages/app/src/job-domain.ts +++ b/packages/app/src/job-domain.ts @@ -1,4 +1,5 @@ import Mustache from "mustache"; +import { renderTextTemplate } from "./text-template"; export type TriggerKind = "manual" | "schedule" | "webhook" | "jobLifecycle"; export type InvocationSource = "manual" | TriggerKind; @@ -88,7 +89,7 @@ export class InvocationError extends Error { export function renderJobPrompt(job: Pick, context: Record): string { try { Mustache.parse(job.promptTemplate); - return Mustache.render(job.promptTemplate, context); + return renderTextTemplate(job.promptTemplate, context); } catch (error) { if (error instanceof InvocationError) throw error; throw new InvocationError("invalid_template", "The job prompt is not valid Mustache."); @@ -99,7 +100,7 @@ export function renderRunName(template: string, context: Record if (!template.trim()) return fallback; try { Mustache.parse(template); - const name = Mustache.render(template, context).trim(); + const name = renderTextTemplate(template, context).trim(); return name || fallback; } catch { return fallback; diff --git a/packages/app/src/linear-source.ts b/packages/app/src/linear-source.ts index 0f8abb99..4af389cf 100644 --- a/packages/app/src/linear-source.ts +++ b/packages/app/src/linear-source.ts @@ -1,4 +1,4 @@ -import Mustache from "mustache"; +import { renderTextTemplate } from "./text-template"; import type { WorkItem } from "./types"; const object = (value: unknown): Record => value !== null && typeof value === "object" && !Array.isArray(value) ? value as Record : {}; @@ -59,7 +59,7 @@ export function renderContextTemplate(template: string, payload: Record ({ name, last: index === labels.length - 1 })), }; - return Mustache.render(template || DEFAULT_CONTEXT_TEMPLATE, { ...payload, ticket: normalizedTicket, flow: { name: flowName } }); + return renderTextTemplate(template || DEFAULT_CONTEXT_TEMPLATE, { ...payload, ticket: normalizedTicket, flow: { name: flowName } }); } export const linearTicketPrompt = (payload: Record, flowName: string) => renderContextTemplate(DEFAULT_CONTEXT_TEMPLATE, payload, flowName); diff --git a/packages/app/src/text-template.ts b/packages/app/src/text-template.ts new file mode 100644 index 00000000..aaf5d79e --- /dev/null +++ b/packages/app/src/text-template.ts @@ -0,0 +1,6 @@ +import Mustache from "mustache"; + +/** Agent input and display names are plain text; HTML escaping belongs at the UI boundary. */ +export function renderTextTemplate(template: string, context: Record): string { + return Mustache.render(template, context, undefined, { escape: (value: string) => value }); +} diff --git a/packages/app/test/job-domain.test.ts b/packages/app/test/job-domain.test.ts index d8268840..9b2cf5f9 100644 --- a/packages/app/test/job-domain.test.ts +++ b/packages/app/test/job-domain.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import Mustache from "mustache"; import { InvocationService, renderJobPrompt, renderRunName, type Invocation, type Job, type JobRepository, type JobRun } from "../src/job-domain"; const job = (overrides: Partial = {}): Job => ({ @@ -20,6 +21,27 @@ class MemoryRepository implements JobRepository { } describe("job invocation domain", () => { + it("preserves plain text without decoding literal entities or changing HTML renderers", () => { + const value = 'https://linear.app/generation/issue/GEN-2108/run-page?a=1&b=2\n{"title":"Fix it"} `code` = & /'; + const context = { "trigger-1": { prompt: value }, "trigger-2": false }; + for (const substitution of ["{{trigger-1.prompt}}", "{{{trigger-1.prompt}}}", "{{&trigger-1.prompt}}"]) { + expect(renderJobPrompt({ promptTemplate: "{{#trigger-1}}\n" + substitution + "\n{{/trigger-1}}{{^trigger-2}} done{{/trigger-2}}" }, context)) + .toBe("\n" + value + "\n done"); + } + expect(renderRunName("Review {{trigger-1.prompt}}", context, "fallback")).toBe("Review " + value); + expect(Mustache.render("{{value}}", { value: "