Compare commits

..

3 Commits

Author SHA1 Message Date
xiaoju dfb6fda06d feat(agent-kit): render per-variant output instructions for discriminated oneOf
buildOutputFormatInstruction now detects discriminated union schemas
(oneOf with shared const/ property) and renders separate YAML
example blocks per variant, so agents see exactly which fields belong
to which outcome instead of a flat merge.

Non-discriminated oneOf/anyOf schemas fall back to the existing flat
merge behavior.

Refs #502
2026-05-25 06:54:38 +00:00
xiaoju 827ff13c4a refactor: discriminated union frontmatter for solve-issue workflow
- planner: oneOf ready (plan, repoPath) | insufficient_info
- developer: single exit, plain object (branch, worktree), no $status
- reviewer: oneOf approved (branch, worktree) | rejected (comments)
- tester: oneOf passed (branch, worktree) | fix_code (report) | fix_spec (report)
- committer: oneOf committed (prUrl) | hook_failed (error)
- Edge prompts now use mustache templates with variant-specific fields
- Developer simplified from 2 exits to single exit (unit routing)

Phase 2 of #499 (closes #501)
2026-05-25 06:34:56 +00:00
xiaoju 7a19ceca89 refactor: rename status to $status, default to _ when absent
- evaluate() reads $status instead of status, defaults to _ when missing
- Update all YAML examples and .workflows to use $status
- Update cli-workflow resolveEvaluateArgs to use $status
- 10 moderator tests pass including new default _ test
- Single-exit roles no longer need to declare status field

Phase 1 of #499 (closes #500)
2026-05-25 06:22:53 +00:00
10 changed files with 269 additions and 82 deletions
+59 -45
View File
@@ -22,16 +22,17 @@ roles:
After producing the test spec:
1. Store it via `uwf cas put-text "<markdown content>"` and capture the returned hash
2. Put the hash in frontmatter.plan (required when status=ready)
output: "Output a brief summary of the test spec. Frontmatter must include: status (ready or insufficient_info) and plan (CAS hash of the test spec, required when status=ready)."
output: "Output a brief summary of the test spec. Set $status to ready (with plan hash and repoPath) or insufficient_info."
frontmatter:
type: object
properties:
status:
type: string
enum: [ready, insufficient_info]
plan:
type: string
required: [status]
oneOf:
- properties:
$status: { const: "ready" }
plan: { type: string }
repoPath: { type: string }
required: [$status, plan, repoPath]
- properties:
$status: { const: "insufficient_info" }
required: [$status]
developer:
description: "TDD implementation per test spec"
goal: "You are a developer agent. You implement code changes following TDD — write tests first, then implementation."
@@ -58,14 +59,13 @@ roles:
8. Implement the code to make tests pass
9. Ensure `bun run build` passes with no errors
10. Run `bun test` to verify all tests pass
output: "List all files changed and provide a summary. Frontmatter must include: status (done or failed)."
output: "List all files changed and provide a summary. Include branch name and worktree path in frontmatter."
frontmatter:
type: object
properties:
status:
type: string
enum: [done, failed]
required: [status]
branch: { type: string }
worktree: { type: string }
required: [branch, worktree]
reviewer:
description: "Code standards compliance check"
goal: "You are a code reviewer. You verify code standards compliance — NOT functionality (that's the tester's job)."
@@ -95,14 +95,18 @@ roles:
Only review standards compliance. Do NOT test functionality.
If rejecting, you MUST explain the specific reason in your output.
output: "Explain your decision with specific file/line references. Frontmatter must include: status (approved or rejected)."
output: "Explain your decision with specific file/line references. Set $status to approved (with branch/worktree) or rejected (with comments)."
frontmatter:
type: object
properties:
status:
type: string
enum: [approved, rejected]
required: [status]
oneOf:
- properties:
$status: { const: "approved" }
branch: { type: string }
worktree: { type: string }
required: [$status, branch, worktree]
- properties:
$status: { const: "rejected" }
comments: { type: string }
required: [$status, comments]
tester:
description: "Functional correctness verification"
goal: "You are a tester agent. You verify that the implementation correctly satisfies every scenario in the test spec."
@@ -118,14 +122,22 @@ roles:
- passed: all scenarios verified, tests pass
- fix_code: tests fail or implementation doesn't match spec → send back to developer
- fix_spec: the spec itself is wrong or incomplete → send back to planner
output: "Report test results per scenario. Frontmatter must include: status (passed, fix_code, or fix_spec)."
output: "Report test results per scenario. Set $status to passed (with branch/worktree), fix_code (with report), or fix_spec (with report)."
frontmatter:
type: object
properties:
status:
type: string
enum: [passed, fix_code, fix_spec]
required: [status]
oneOf:
- properties:
$status: { const: "passed" }
branch: { type: string }
worktree: { type: string }
required: [$status, branch, worktree]
- properties:
$status: { const: "fix_code" }
report: { type: string }
required: [$status, report]
- properties:
$status: { const: "fix_spec" }
report: { type: string }
required: [$status, report]
committer:
description: "Commits and creates PR"
goal: "You are a committer agent. You create a clean commit and push a PR linking the original issue."
@@ -146,30 +158,32 @@ roles:
5. After PR creation, clean up the worktree:
- `cd ~/repos/workflow`
- `git worktree remove ~/repos/workflow-worktrees/fix/<issue-number>-<slug>`
output: "Include PR URL on success or error log on failure. Frontmatter must include: status (committed or hook_failed)."
output: "Include PR URL on success or error log on failure. Set $status to committed (with prUrl) or hook_failed (with error)."
frontmatter:
type: object
properties:
status:
type: string
enum: [committed, hook_failed]
required: [status]
oneOf:
- properties:
$status: { const: "committed" }
prUrl: { type: string }
required: [$status, prUrl]
- properties:
$status: { const: "hook_failed" }
error: { type: string }
required: [$status, error]
graph:
$START:
_: { role: "planner", prompt: "Analyze the issue and produce an implementation plan." }
planner:
insufficient_info: { role: "$END", prompt: "Insufficient information to proceed; end the workflow." }
ready: { role: "developer", prompt: "Implement the plan from the planner." }
ready: { role: "developer", prompt: "Implement the TDD test spec (CAS hash: {{{plan}}}) in repo {{{repoPath}}}." }
developer:
failed: { role: "$END", prompt: "Development failed; end the workflow." }
done: { role: "reviewer", prompt: "Send the implementation to the reviewer." }
_: { role: "reviewer", prompt: "Review branch {{{branch}}} at {{{worktree}}} for code standards compliance." }
reviewer:
rejected: { role: "developer", prompt: "Reviewer rejected the implementation; fix the issues." }
approved: { role: "tester", prompt: "Review passed; run tests on the implementation." }
rejected: { role: "developer", prompt: "Reviewer rejected: {{{comments}}}. Fix the issues." }
approved: { role: "tester", prompt: "Review passed. Run tests on branch {{{branch}}} at {{{worktree}}}." }
tester:
fix_code: { role: "developer", prompt: "Tests found code issues; return to developer." }
fix_spec: { role: "planner", prompt: "Tests found spec issues; return to planner." }
passed: { role: "committer", prompt: "Tests passed; commit and push the changes." }
fix_code: { role: "developer", prompt: "Tests found code issues: {{{report}}}. Fix and re-submit." }
fix_spec: { role: "planner", prompt: "Tests found spec issues: {{{report}}}. Revise the test spec." }
passed: { role: "committer", prompt: "All tests passed. Commit and push branch {{{branch}}} from {{{worktree}}}." }
committer:
hook_failed: { role: "developer", prompt: "Push hook failed; return to developer to fix." }
committed: { role: "$END", prompt: "Commit succeeded; complete the workflow." }
hook_failed: { role: "developer", prompt: "Push hook failed: {{{error}}}. Fix and re-submit." }
committed: { role: "$END", prompt: "PR created: {{{prUrl}}}. Workflow complete." }
+2 -2
View File
@@ -22,7 +22,7 @@ roles:
frontmatter:
type: object
properties:
status:
$status:
enum: ["_"]
thesis:
type: string
@@ -32,7 +32,7 @@ roles:
type: string
caveats:
type: string
required: [status, thesis, keyPoints]
required: [$status, thesis, keyPoints]
graph:
$START:
_: { role: "analyst", prompt: "Analyze the topic in the task and produce a structured summary with key points." }
+4 -4
View File
@@ -21,11 +21,11 @@ roles:
frontmatter:
type: object
properties:
status:
$status:
enum: ["continue", "conceded"]
argument:
type: string
required: [status, argument]
required: [$status, argument]
for:
description: "Argues for the proposition"
goal: |
@@ -46,11 +46,11 @@ roles:
frontmatter:
type: object
properties:
status:
$status:
enum: ["continue", "conceded"]
argument:
type: string
required: [status, argument]
required: [$status, argument]
graph:
$START:
_: { role: "against", prompt: "Present your opening argument against the proposition." }
+6 -6
View File
@@ -27,13 +27,13 @@ roles:
frontmatter:
type: object
properties:
status:
$status:
enum: ["_"]
repoPath:
type: string
plan:
type: string
required: [status, repoPath, plan]
required: [$status, repoPath, plan]
developer:
description: "Implements code changes"
goal: "You are a developer agent. You implement code changes according to plans."
@@ -52,7 +52,7 @@ roles:
frontmatter:
type: object
properties:
status:
$status:
enum: ["_"]
filesChanged:
type: array
@@ -60,7 +60,7 @@ roles:
type: string
summary:
type: string
required: [status, filesChanged, summary]
required: [$status, filesChanged, summary]
reviewer:
description: "Reviews code changes"
goal: "You are a code reviewer. You review implementations for correctness and quality."
@@ -75,11 +75,11 @@ roles:
frontmatter:
type: object
properties:
status:
$status:
enum: ["approved", "rejected"]
comments:
type: string
required: [status, comments]
required: [$status, comments]
graph:
$START:
_: { role: "planner", prompt: "Analyze the issue described in the task and produce a detailed implementation plan." }
@@ -81,17 +81,18 @@ describe("solve-issue workflow: tea pr create worktree fix", () => {
expect(workflow.roles.committer?.frontmatter).toBeDefined();
});
test("committer frontmatter schema should require status field", async () => {
test("committer frontmatter schema should be oneOf with $status discriminant", async () => {
const yamlContent = await readFile(workflowPath, "utf-8");
// Parse as any to access the raw YAML structure (frontmatter is inline JSON Schema in YAML)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const workflow = parse(yamlContent) as any;
const frontmatter = workflow.roles.committer?.frontmatter;
expect(frontmatter).toBeDefined();
expect(frontmatter?.type).toBe("object");
expect(frontmatter?.properties?.status).toBeDefined();
expect(frontmatter?.properties?.status?.enum).toContain("committed");
expect(frontmatter?.required).toContain("status");
expect(frontmatter?.oneOf).toBeDefined();
const committedVariant = frontmatter.oneOf.find(
(v: any) => v.properties?.["$status"]?.const === "committed",
);
expect(committedVariant).toBeDefined();
expect(committedVariant.required).toContain("$status");
});
});
+5 -4
View File
@@ -669,14 +669,16 @@ function formatThreadReadMarkdown(options: {
return parts.join("\n\n---\n\n");
}
type EvaluateLastOutput = Record<string, unknown> & { status: string };
type EvaluateLastOutput = Record<string, unknown>;
const STATUS_KEY = "$status";
function resolveEvaluateArgs(
uwf: UwfStore,
chain: ChainState,
): { lastRole: string; lastOutput: EvaluateLastOutput } {
if (chain.headIsStart) {
return { lastRole: START_ROLE, lastOutput: { status: "_" } };
return { lastRole: START_ROLE, lastOutput: { [STATUS_KEY]: "_" } };
}
const lastStep = chain.stepsNewestFirst[0];
@@ -689,11 +691,10 @@ function resolveEvaluateArgs(
typeof raw === "object" && raw !== null && !Array.isArray(raw)
? (raw as Record<string, unknown>)
: {};
const status = typeof base.status === "string" ? base.status : "_";
return {
lastRole: lastStep.role,
lastOutput: { ...base, status },
lastOutput: base,
};
}
@@ -87,7 +87,7 @@ describe("buildOutputFormatInstruction", () => {
expect(result).toContain("beta: <number>");
});
test("lists union of fields from a oneOf schema", () => {
test("lists union of fields from a oneOf schema (no discriminant — flat merge)", () => {
const schema = {
oneOf: [
{
@@ -101,12 +101,71 @@ describe("buildOutputFormatInstruction", () => {
],
};
const result = buildOutputFormatInstruction(schema);
// No discriminant detected → falls back to flat merge
expect(result).toContain("`foo`");
expect(result).toContain("`bar`");
expect(result).toContain("foo: <string>");
expect(result).toContain("bar: true # true | false");
});
test("renders per-variant instructions for discriminated oneOf", () => {
const schema = {
oneOf: [
{
type: "object",
properties: {
$status: { const: "ready" },
plan: { type: "string" },
},
required: ["$status", "plan"],
},
{
type: "object",
properties: {
$status: { const: "insufficient_info" },
},
required: ["$status"],
},
],
};
const result = buildOutputFormatInstruction(schema);
expect(result).toContain("Choose ONE of the following variants");
expect(result).toContain("When `$status: ready`");
expect(result).toContain("When `$status: insufficient_info`");
expect(result).toContain("plan: <string>");
// The insufficient_info variant should NOT mention plan
const insufficientBlock = result.split("When `$status: insufficient_info`")[1];
expect(insufficientBlock).not.toContain("plan:");
});
test("renders per-variant for single-enum discriminant", () => {
const schema = {
oneOf: [
{
type: "object",
properties: {
$status: { type: "string", enum: ["approved"] },
branch: { type: "string" },
},
required: ["$status"],
},
{
type: "object",
properties: {
$status: { type: "string", enum: ["rejected"] },
comments: { type: "string" },
},
required: ["$status"],
},
],
};
const result = buildOutputFormatInstruction(schema);
expect(result).toContain("When `$status: approved`");
expect(result).toContain("When `$status: rejected`");
expect(result).toContain("branch: <string>");
expect(result).toContain("comments: <string>");
});
test("falls back gracefully for a non-object schema with no properties", () => {
const result = buildOutputFormatInstruction({ type: "string" });
expect(result).toContain("schema fields will be extracted automatically");
@@ -166,14 +166,109 @@ function buildFieldList(properties: SchemaProperty[]): string {
.join("\n");
}
/**
* Detect the discriminant property name from a oneOf schema.
* Returns the property name if all variants share a const/single-enum string property, else null.
*/
function detectDiscriminant(variants: JSONSchema[]): string | null {
// Find property names that appear in ALL variants with const or single-enum
const candidateNames = new Set<string>();
for (const variant of variants) {
const props = variant.properties as Record<string, JSONSchema> | null | undefined;
if (typeof props !== "object" || props === null) return null;
for (const [name, propSchema] of Object.entries(props)) {
const isConst =
propSchema.const !== undefined ||
(Array.isArray(propSchema.enum) && propSchema.enum.length === 1);
if (isConst) candidateNames.add(name);
}
}
// Check which candidate appears in ALL variants
for (const name of candidateNames) {
const allHaveIt = variants.every((v) => {
const props = v.properties as Record<string, JSONSchema> | null | undefined;
if (typeof props !== "object" || props === null) return false;
const propSchema = props[name];
if (!propSchema) return false;
return (
propSchema.const !== undefined ||
(Array.isArray(propSchema.enum) && propSchema.enum.length === 1)
);
});
if (allHaveIt) return name;
}
return null;
}
function getConstValue(propSchema: JSONSchema): string {
if (propSchema.const !== undefined) return String(propSchema.const);
if (Array.isArray(propSchema.enum) && propSchema.enum.length === 1)
return String(propSchema.enum[0]);
return "<unknown>";
}
function buildVariantBlock(variant: JSONSchema, discriminant: string): string {
const props = extractSchemaProperties(variant);
const value = getConstValue(
((variant.properties as Record<string, JSONSchema>) ?? {})[discriminant] ?? {},
);
const yamlExample = buildYamlExampleBlock(props);
const fieldList = buildFieldList(props);
return `### When \`${discriminant}: ${value}\`
\`\`\`
${yamlExample}
\`\`\`
Fields:
${fieldList}`;
}
/**
* Build a concise output format instruction block for an agent role.
*
* The instruction describes the expected frontmatter markdown format and lists
* the meta fields derived from the JSON Schema. It is prepended to the agent's
* system prompt so the deliverable format is the first thing the agent sees.
* For discriminated union schemas (oneOf with a shared const/$status field),
* renders per-variant instructions so the agent knows exactly which fields
* belong to which outcome.
*
* For flat object schemas, renders a single YAML example block.
*/
export function buildOutputFormatInstruction(schema: JSONSchema): string {
// Check for discriminated union (oneOf with shared discriminant)
const unionKey = Array.isArray(schema.oneOf)
? "oneOf"
: Array.isArray(schema.anyOf)
? "anyOf"
: null;
if (unionKey !== null) {
const variants = schema[unionKey] as JSONSchema[];
const discriminant = detectDiscriminant(variants);
if (discriminant !== null && variants.length > 1) {
const variantBlocks = variants.map((v) => buildVariantBlock(v, discriminant)).join("\n\n");
return `## Deliverable Format
Your response MUST begin with a YAML frontmatter block followed by your markdown work.
Choose ONE of the following variants based on your outcome:
${variantBlocks}
The frontmatter is the **primary deliverable** — the engine reads it directly.
Output ONLY the fields listed for your chosen variant. Do not add extra fields that are not specified in the schema.
Focus exclusively on YOUR role's deliverable. Do not perform actions outside your role's scope.`;
}
}
// Flat object schema fallback
const properties = extractSchemaProperties(schema);
const yamlExample = buildYamlExampleBlock(properties);
const fieldList = buildFieldList(properties);
@@ -21,7 +21,7 @@ const solveIssueGraph: WorkflowPayload["graph"] = {
describe("evaluate", () => {
test("$START → first role (unit status _)", () => {
const result = evaluate(solveIssueGraph, "$START", { status: "_" });
const result = evaluate(solveIssueGraph, "$START", { $status: "_" });
expect(result).toEqual({
ok: true,
value: { role: "planner", prompt: "Start planning from the issue in the task." },
@@ -30,7 +30,7 @@ describe("evaluate", () => {
test("status-based routing (reviewer rejected → developer)", () => {
const result = evaluate(solveIssueGraph, "reviewer", {
status: "rejected",
$status: "rejected",
comments: "missing tests",
});
expect(result).toEqual({
@@ -40,7 +40,7 @@ describe("evaluate", () => {
});
test("status-based routing (reviewer approved → $END)", () => {
const result = evaluate(solveIssueGraph, "reviewer", { status: "approved" });
const result = evaluate(solveIssueGraph, "reviewer", { $status: "approved" });
expect(result).toEqual({
ok: true,
value: { role: "$END", prompt: "Done." },
@@ -48,7 +48,7 @@ describe("evaluate", () => {
});
test("missing role in graph → error", () => {
const result = evaluate(solveIssueGraph, "unknown-role", { status: "_" });
const result = evaluate(solveIssueGraph, "unknown-role", { $status: "_" });
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.message).toBe('no transitions defined for role "unknown-role"');
@@ -56,7 +56,7 @@ describe("evaluate", () => {
});
test("missing status in graph → error", () => {
const result = evaluate(solveIssueGraph, "reviewer", { status: "pending" });
const result = evaluate(solveIssueGraph, "reviewer", { $status: "pending" });
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.message).toBe('no transition for role "reviewer" with status "pending"');
@@ -65,7 +65,7 @@ describe("evaluate", () => {
test("mustache template rendering with simple fields", () => {
const result = evaluate(solveIssueGraph, "planner", {
status: "_",
$status: "_",
plan: "Add auth middleware",
});
expect(result).toEqual({
@@ -76,7 +76,7 @@ describe("evaluate", () => {
test("mustache does not HTML-escape prompt content", () => {
const result = evaluate(solveIssueGraph, "reviewer", {
status: "rejected",
$status: "rejected",
comments: 'use <T> & "Result<T, E>" types',
});
expect(result).toEqual({
@@ -92,7 +92,7 @@ describe("evaluate", () => {
},
};
const result = evaluate(graph, "reviewer", {
status: "_",
$status: "_",
comments: "<script>alert(1)</script>",
});
expect(result).toEqual({
@@ -101,6 +101,16 @@ describe("evaluate", () => {
});
});
test("missing $status defaults to _ (unit routing)", () => {
const result = evaluate(solveIssueGraph, "planner", {
plan: "Add auth middleware",
});
expect(result).toEqual({
ok: true,
value: { role: "developer", prompt: "Implement the plan: Add auth middleware" },
});
});
test("mustache template with nested object paths", () => {
const graph: Record<string, Record<string, Target>> = {
reviewer: {
@@ -111,7 +121,7 @@ describe("evaluate", () => {
},
};
const result = evaluate(graph, "reviewer", {
status: "_",
$status: "_",
review: { comments: "refactor the handler" },
});
expect(result).toEqual({
+9 -2
View File
@@ -9,14 +9,21 @@ mustache.escape = (text: string) => text;
const START_ROLE = "$START";
const UNIT_STATUS = "_";
type LastOutput = Record<string, unknown> & { status: string };
type LastOutput = Record<string, unknown>;
const STATUS_KEY = "$status";
export function evaluate(
graph: Record<string, Record<string, Target>>,
lastRole: string,
lastOutput: LastOutput,
): Result<EvaluateResult, Error> {
const status = lastRole === START_ROLE ? UNIT_STATUS : lastOutput.status;
const status =
lastRole === START_ROLE
? UNIT_STATUS
: typeof lastOutput[STATUS_KEY] === "string"
? (lastOutput[STATUS_KEY] as string)
: UNIT_STATUS;
const roleTargets = graph[lastRole];
if (roleTargets === undefined) {