This repository has been archived on 2026-09-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
BPOS-MVP/spike/mastra-reopen/src/probe-03-loop-snapshot-detail.ts
T
ChaiHaoandClaude Fable 5 f96eff906a 工具总线 + Agent 开环:声明式工具、人类工具与 LLM 五件系统内化(Mastra tool 形状)
用户定:Agent 调用工具一律**声明式**;人类工具与 LLM 工具要系统内化;
工具总线基于 Mastra tool 设计。

**声明面**(`toolbox/internal/domain/tool-bus.ts`,纯逻辑零框架):
`compileToolBus(声明 × 目录) → 一张清单` —— 编译期定死这一单能调什么,
清单之外结构上调不到(白名单在缝里再把一次关);缺件/停用如实报不静默。
三路同一形状:
  · `function` 功能工具:按活动白名单查目录,带出效应(read/write)
  · `human` **人类工具**(系统内化、零声明即得):调人 = 发工单 + 立监听听人 ——
    这是 Harness 给的能力,不进目录、不需白名单,每个活动天然可以调人
  · `llm` **LLM 五件**(系统内化):extract/classify/summarize/judge/rewrite,
    各有入参出参契约、经模型网关一缝;**仅当活动声明了模型用途名才上总线**
    (没声明却会问模型 = 越权,用量也无从归因);提示词归系统,活动改不了

**装配面**(`app/tool-bus.ts`,Mastra `createTool`):清单 → 可调用工具,
JSON Schema 现译 zod(模型照它填参,填错当场被拦);三路各穿各的缝:
写类工具**过程中一律不落地**,只登记动作意向(骗 Agent 说已执行是最坏选项);
调人把卡号记进 trace,引擎据此把这一拍挂起。

**Agent 开环**(`app/mastra-agent-runner.ts`,M5-2 第二实现,用户选的路线):
Mastra `Agent` + 工具总线,拍内自主连环调工具,拍末交回结构化结论。
放开的是工具循环不是规矩:写类不落地、终局唯放行、调人即挂起(新增
`AgentAction.asked` 型 —— 卡已由总线发出,引擎只收尾,不重复发卡)、
用量按 usage 计入预算。`JCAI_AGENT_OPEN_LOOP=1` 开启,缺省仍是单拍版。
`ModelGateway.resolve` 新增:名 → 四元(密钥当场解出,只给 app 层装配用)。

代价如实记 debt M5-2:开环下模型**逐轮**调用不经录制回放(tool-calling 往返
`generate` 的 text-in/text-out 承载不了)—— 凭证隔离与工具调用一缝完整。

测试:tool-bus 六例(白名单/缺件/停用/人类工具恒在/LLM 按声明/五件契约);
四闸(typecheck/lint/depcruise/test)全过;spike 目录排除出 lint(验证性原型)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeDBx2PHLavbURXB8LJJ7m
2026-07-28 23:46:12 +08:00

58 lines
2.8 KiB
TypeScript

/**
* 精确定位:dowhile 每一轮的 step 记录,在快照里是「追加」还是「覆盖/重置」?
* 运行:SPIKE_NO_AUTORUN=1 tsx src/probe-03-loop-snapshot-detail.ts
*/
import { DecisionScript, Ledger, banner } from './ledger.js';
import { mastraA, storeA, _internals } from './route-a-dowhile.js';
import { resetTrace } from './trace.js';
_internals.setLedger(new Ledger());
_internals.setScript(new DecisionScript([]));
resetTrace();
const wf = mastraA.getWorkflow('routeA');
const snapOf = async (workflowName: string, runId: string) =>
(await (storeA as any).stores.workflows.loadWorkflowSnapshot({ workflowName, runId })) as
| Record<string, any>
| null;
const run = await wf.createRun();
const inner = async () => await snapOf('round-body', run.runId);
const dumpInner = async (tag: string) => {
const s = await inner();
console.log(`\n[${tag}] 内层快照 context 的键 = ${JSON.stringify(Object.keys(s?.context ?? {}))}`);
for (const k of ['A4', 'A5', 'A6-sub']) {
const rec = s?.context?.[k];
if (!rec) continue;
console.log(
` ${k}: status=${rec.status} suspendPayload.taskId=${rec.suspendPayload?.taskId} output.price=${rec.output?.price} output.verdict=${rec.output?.verdict}`,
);
}
};
banner('第 1 轮');
await run.start({ inputData: { quoteId: 'Q-detail' } });
await dumpInner('第1轮 A4 挂起');
await run.resume({ step: ['round-body', 'A4'], resumeData: { price: 100 } });
await dumpInner('第1轮 A4 已推进,A5 挂起');
await run.resume({ step: ['round-body', 'A5'], resumeData: { verdict: 'reject' } });
banner('第 2 轮(E10 回边重开 A4)');
await dumpInner('第2轮 A4 挂起');
console.log('\n→ 第 1 轮的 A5(verdict=reject)记录还在内层快照里吗?',
(await inner())?.context?.['A5'] ? '在' : '不在(嵌套快照按轮重置了)');
console.log('→ 第 1 轮的 A4(taskId=T-A4-001, price=100)记录还在吗?');
console.log(' 当前 A4 记录全文 =', JSON.stringify((await inner())?.context?.['A4'], null, 2));
banner('跑完这一单,看终态');
await run.resume({ step: ['round-body', 'A4'], resumeData: { price: 90 } });
await run.resume({ step: ['round-body', 'A5'], resumeData: { verdict: 'pass' } });
const fin = await run.resume({ step: ['round-body', 'A6-sub', 'A6'], resumeData: { outcome: 'won' } });
console.log('最终 status =', fin.status, JSON.stringify((fin as any).result));
await dumpInner('终态');
const outer = await snapOf('route-a', run.runId);
console.log('\n外层 context["round-body"].metadata =', JSON.stringify(outer?.context?.['round-body']?.metadata));
console.log('外层 context 键 =', JSON.stringify(Object.keys(outer?.context ?? {})));
console.log('\n结论素材:整个 run 只有一份「A4 的记录」,内容是最后一轮的;第 1 轮的 A4/A5 记录不可回溯。');
_internals.getLedger().dump('probe-03(我们自己记的台账则保留了每一轮)');