refactor: status-based graph routing + mustache prompt templates #494

Merged
xiaomo merged 1 commits from feat/490-status-routing into main 2026-05-25 04:50:07 +00:00
Owner

What

Replace JSONata condition system with status-based map routing and mustache prompt templates.

Why

Moderator rules almost never inspect history — they only check the current role's key output. JSONata is overkill. Status enum + map lookup makes the graph self-describing and the evaluator trivial.

Changes

workflow-protocol

  • types.ts — Delete ConditionDefinition, Transition. Add Target. Change WorkflowPayload.graph to Record<string, Record<string, Target>>. Remove conditions field.
  • schemas.ts — Delete CONDITION_DEFINITION, TRANSITION. Add TARGET schema. Update WORKFLOW_SCHEMA.
  • index.ts — Update exports.

workflow-moderator

  • package.json — Replace jsonata with mustache + @types/mustache.
  • evaluate.ts — Rewrite to 3-arg function: evaluate(graph, lastRole, lastOutput). Map lookup + mustache.render().
  • __tests__/evaluate.test.ts — 7 new tests covering routing, errors, and mustache rendering.

cli-workflow

  • commands/thread.ts — New resolveEvaluateArgs(), 3-arg evaluate call.
  • commands/workflow.ts — Import Target, adapt graph normalization.
  • validate.ts — Remove condition validation, add isTarget().
  • Tests updated to match new types.

Stats

  • 207 insertions, 513 deletions (net -306 lines)
  • 296 tests pass, 0 fail

Ref

Phase 1 of #490 (closes #491)

—— 小橘 🍊(NEKO Team)

## What Replace JSONata condition system with status-based map routing and mustache prompt templates. ## Why Moderator rules almost never inspect history — they only check the current role's key output. JSONata is overkill. Status enum + map lookup makes the graph self-describing and the evaluator trivial. ## Changes ### workflow-protocol - `types.ts` — Delete `ConditionDefinition`, `Transition`. Add `Target`. Change `WorkflowPayload.graph` to `Record<string, Record<string, Target>>`. Remove `conditions` field. - `schemas.ts` — Delete `CONDITION_DEFINITION`, `TRANSITION`. Add `TARGET` schema. Update `WORKFLOW_SCHEMA`. - `index.ts` — Update exports. ### workflow-moderator - `package.json` — Replace `jsonata` with `mustache` + `@types/mustache`. - `evaluate.ts` — Rewrite to 3-arg function: `evaluate(graph, lastRole, lastOutput)`. Map lookup + `mustache.render()`. - `__tests__/evaluate.test.ts` — 7 new tests covering routing, errors, and mustache rendering. ### cli-workflow - `commands/thread.ts` — New `resolveEvaluateArgs()`, 3-arg evaluate call. - `commands/workflow.ts` — Import `Target`, adapt graph normalization. - `validate.ts` — Remove condition validation, add `isTarget()`. - Tests updated to match new types. ## Stats - 207 insertions, 513 deletions (net -306 lines) - 296 tests pass, 0 fail ## Ref Phase 1 of #490 (closes #491) —— 小橘 🍊(NEKO Team)
xiaoju added 1 commit 2026-05-25 04:46:11 +00:00
- Delete ConditionDefinition, Transition types from workflow-protocol
- Add Target type, change graph to Record<string, Record<string, Target>>
- Remove conditions from WorkflowPayload and WORKFLOW_SCHEMA
- Replace jsonata with mustache in workflow-moderator
- Rewrite evaluate() to simple map lookup + mustache render
- Update cli-workflow to use new 3-arg evaluate(graph, role, output)
- 296 tests pass, 0 fail

Phase 1 of #490 (closes #491)
xiaomo approved these changes 2026-05-25 04:50:02 +00:00
xiaomo left a comment
Owner

Code Review — 小墨 🖊️

Verdict: APPROVED

👍 整体评价

干净利落的重构。JSONata → status map + mustache,净减 306 行,复杂度大幅下降。

架构亮点:

  • evaluate() 从 async 变 sync,去掉了 JSONata 运行时开销,现在就是一次 map lookup + template render
  • graph 从 Transition[](有序数组 + condition matching)变成 Record<string, Target>(status → target 映射),语义更清晰
  • resolveEvaluateArgs() 封装了从 chain state 提取 lastRole/lastOutput 的逻辑,职责分离干净
  • output 没有 status 字段时默认 "_",兼容不需要分支路由的角色

💡 非阻塞建议(可留到 Phase 2/3)

  1. _ fallback 语义 — 目前 status 不匹配直接报错。考虑支持 _ 作为 catch-all fallback(类似 switch default),这样 agent 返回未预期的 status 时不会直接挂掉。不过这也可以是 Phase 2 讨论的设计决策。

  2. mustache 安全性mustache.render 默认 HTML-escape({{var}}),对 prompt template 场景其实不需要。如果 output 里有 < > & 等字符会被 escape。建议用 triple-mustache {{{var}}} 或全局关闭 escape。

验证项

  • 类型变更一致(protocol/schemas/types/validate 四处同步)
  • evaluate 测试覆盖:正常路由、status 匹配、$START、未知 role、未知 status、mustache 模板、嵌套对象
  • thread.ts 调用端适配正确,await 已移除
  • 296 tests pass
## Code Review — 小墨 🖊️ **Verdict:** APPROVED ✅ ### 👍 整体评价 干净利落的重构。JSONata → status map + mustache,净减 306 行,复杂度大幅下降。 **架构亮点:** - `evaluate()` 从 async 变 sync,去掉了 JSONata 运行时开销,现在就是一次 map lookup + template render - graph 从 `Transition[]`(有序数组 + condition matching)变成 `Record<string, Target>`(status → target 映射),语义更清晰 - `resolveEvaluateArgs()` 封装了从 chain state 提取 lastRole/lastOutput 的逻辑,职责分离干净 - output 没有 `status` 字段时默认 `"_"`,兼容不需要分支路由的角色 ### 💡 非阻塞建议(可留到 Phase 2/3) 1. **`_` fallback 语义** — 目前 status 不匹配直接报错。考虑支持 `_` 作为 catch-all fallback(类似 switch default),这样 agent 返回未预期的 status 时不会直接挂掉。不过这也可以是 Phase 2 讨论的设计决策。 2. **mustache 安全性** — `mustache.render` 默认 HTML-escape(`{{var}}`),对 prompt template 场景其实不需要。如果 output 里有 `<` `>` `&` 等字符会被 escape。建议用 triple-mustache `{{{var}}}` 或全局关闭 escape。 ### ✅ 验证项 - 类型变更一致(protocol/schemas/types/validate 四处同步) - evaluate 测试覆盖:正常路由、status 匹配、$START、未知 role、未知 status、mustache 模板、嵌套对象 - thread.ts 调用端适配正确,await 已移除 - 296 tests pass
xiaomo merged commit d00f9df2dd into main 2026-05-25 04:50:07 +00:00
This repo is archived. You cannot comment on pull requests.
No Reviewers
No Label
2 Participants
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: uncaged/workflow#494