feat(agent-kit): per-variant output instructions for discriminated oneOf #505

Merged
xiaomo merged 1 commits from feat/502-oneOf-output-instruction into main 2026-05-25 06:57:14 +00:00
Owner

What

buildOutputFormatInstruction now detects discriminated union schemas (oneOf with a shared const/$status property) and renders separate YAML example blocks per variant.

Why

Previously, oneOf schemas were flattened into a single merged field list. Agents couldn't tell which fields belong to which outcome — e.g. planner's plan field appeared even for the insufficient_info variant.

Changes

  • build-output-format-instruction.ts — added detectDiscriminant(), buildVariantBlock(); buildOutputFormatInstruction() now renders per-variant when discriminant detected, falls back to flat merge otherwise
  • build-output-format-instruction.test.ts — 2 new tests for discriminated oneOf (const + single-enum), updated existing oneOf test name

Example output (discriminated)

Choose ONE of the following variants based on your outcome:

### When \`$status: ready\`
---
$status: ready
plan: <string>
---

### When \`$status: insufficient_info\`
---
$status: insufficient_info
---

Ref

Fixes #502

## What `buildOutputFormatInstruction` now detects discriminated union schemas (oneOf with a shared const/$status property) and renders **separate YAML example blocks per variant**. ## Why Previously, oneOf schemas were flattened into a single merged field list. Agents couldn't tell which fields belong to which outcome — e.g. planner's `plan` field appeared even for the `insufficient_info` variant. ## Changes - `build-output-format-instruction.ts` — added `detectDiscriminant()`, `buildVariantBlock()`; `buildOutputFormatInstruction()` now renders per-variant when discriminant detected, falls back to flat merge otherwise - `build-output-format-instruction.test.ts` — 2 new tests for discriminated oneOf (const + single-enum), updated existing oneOf test name ## Example output (discriminated) ``` Choose ONE of the following variants based on your outcome: ### When \`$status: ready\` --- $status: ready plan: <string> --- ### When \`$status: insufficient_info\` --- $status: insufficient_info --- ``` ## Ref Fixes #502
xiaoju added 1 commit 2026-05-25 06:55:34 +00:00
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
xiaomo approved these changes 2026-05-25 06:57:09 +00:00
xiaomo left a comment
Owner

Code Review — 小墨 🖊️

APPROVED

detectDiscriminant() 实现正确 — 遍历所有 variant 找 const/single-enum 交集,优雅地兼容 { const: "ready" }{ enum: ["approved"] } 两种写法。

渲染逻辑分层清晰:

  • 有 discriminant → per-variant 渲染,每个 variant 独立的 YAML 示例 + 字段列表
  • 无 discriminant → 原有 flat merge 逻辑不变

测试覆盖到位:const 风格、single-enum 风格、无 discriminant 回退,以及 variant 间字段隔离验证。

RFC #499 三个 Phase 完成得很漂亮 👏

## Code Review — 小墨 🖊️ APPROVED ✅ `detectDiscriminant()` 实现正确 — 遍历所有 variant 找 const/single-enum 交集,优雅地兼容 `{ const: "ready" }` 和 `{ enum: ["approved"] }` 两种写法。 渲染逻辑分层清晰: - 有 discriminant → per-variant 渲染,每个 variant 独立的 YAML 示例 + 字段列表 - 无 discriminant → 原有 flat merge 逻辑不变 测试覆盖到位:const 风格、single-enum 风格、无 discriminant 回退,以及 variant 间字段隔离验证。 RFC #499 三个 Phase 完成得很漂亮 👏
xiaomo merged commit dfb6fda06d into main 2026-05-25 06:57:14 +00:00
Sign in to join this conversation.
No Reviewers
No Label
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: uncaged/workflow#505