dsh-lean-router 0.0.0-stage → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE.md +23 -0
  3. package/README.en.md +73 -0
  4. package/README.md +72 -2
  5. package/cordis.patch.yml +870 -0
  6. package/docs/adr/0001-token-ledger-durable-usage.md +11 -0
  7. package/docs/agents/domain.md +38 -0
  8. package/docs/agents/issue-tracker.md +44 -0
  9. package/docs/agents/release.md +24 -0
  10. package/docs/agents/triage-labels.md +13 -0
  11. package/docs/agents/verification.md +32 -0
  12. package/docs/architecture.en.md +67 -0
  13. package/docs/architecture.md +66 -0
  14. package/docs/changelog.en.md +279 -0
  15. package/docs/changelog.md +282 -0
  16. package/docs/configuration.en.md +57 -0
  17. package/docs/configuration.md +57 -0
  18. package/docs/install.en.md +60 -0
  19. package/docs/install.md +60 -0
  20. package/docs/quickstart.en.md +75 -0
  21. package/docs/quickstart.md +75 -0
  22. package/docs/release-checklist.en.md +82 -0
  23. package/docs/release-checklist.md +131 -0
  24. package/docs//345/244/215/346/240/270-/345/205/263/346/217/222/344/273/266/346/270/205/345/215/225.md +108 -0
  25. package/docs//345/267/245/345/205/267/351/201/256/350/224/275-/346/216/245/347/272/277/350/257/264/346/230/216.md +140 -0
  26. package/docs//350/267/257/347/224/261-/350/256/276/350/256/241/350/257/264/346/230/216.md +104 -0
  27. package/lib/bands.js +149 -0
  28. package/lib/index.js +12 -0
  29. package/lib/lean/compaction.js +119 -0
  30. package/lib/lean/delivery-gate.js +312 -0
  31. package/lib/lean/disabled.js +61 -0
  32. package/lib/lean/history-budget.js +301 -0
  33. package/lib/lean/history-downgrade.js +345 -0
  34. package/lib/lean/index.js +19 -0
  35. package/lib/lean/output-slim.js +271 -0
  36. package/lib/lean/persona-text.js +111 -0
  37. package/lib/lean/plan-mode.js +31 -0
  38. package/lib/lean/pruner.js +75 -0
  39. package/lib/lean/tool-concurrency.js +319 -0
  40. package/lib/lean/usage-ledger.js +442 -0
  41. package/lib/router.js +287 -0
  42. package/lib/token-ledger/index.js +147 -0
  43. package/lib/token-ledger/live.js +193 -0
  44. package/lib/toolface/desc-slim.js +140 -0
  45. package/lib/toolface/discovery.js +297 -0
  46. package/lib/toolface/executor.js +333 -0
  47. package/lib/toolface/index.js +250 -0
  48. package/lib/toolface/pool.js +160 -0
  49. package/lib/toolface/retention.js +187 -0
  50. package/lib/toolface/rules.js +435 -0
  51. package/lib/toolface/scope.js +188 -0
  52. package/lib/toolface/transport.js +335 -0
  53. package/package.json +65 -4
  54. package/presets/base/frag-lean.patch.yml +459 -0
  55. package/presets/base/frag-preset.patch.yml +274 -0
  56. package/presets/base/frag-router.patch.yml +17 -0
  57. package/presets/base/frag-toolface.patch.yml +73 -0
  58. package/presets/base/lean-router.patch.yml +44 -0
  59. package/scripts/build-bundle.mjs +45 -0
  60. package/scripts/doctor.mjs +35 -0
  61. package/scripts/install-profile.mjs +113 -0
  62. package/scripts/install.mjs +20 -0
  63. package/scripts/integrate-preset.mjs +107 -0
  64. package/scripts/release.mjs +146 -0
  65. package/scripts/verify.mjs +8 -0
  66. package/test/build.test.mjs +7 -0
  67. package/test/history-budget.test.mjs +210 -0
  68. package/test/history-downgrade.test.mjs +379 -0
  69. package/test/integration-preset.test.mjs +59 -0
  70. package/test/lean-compaction.test.mjs +47 -0
  71. package/test/lean-concurrency.test.mjs +117 -0
  72. package/test/lean-delivery-gate.test.mjs +75 -0
  73. package/test/lean-frag.test.mjs +132 -0
  74. package/test/lean-modules.test.mjs +112 -0
  75. package/test/lean-pruner.test.mjs +42 -0
  76. package/test/output-slim.test.mjs +212 -0
  77. package/test/package.test.mjs +20 -0
  78. package/test/patch-yaml.test.mjs +45 -0
  79. package/test/plugin-entry.test.mjs +26 -0
  80. package/test/router-bands.test.mjs +98 -0
  81. package/test/router.test.mjs +215 -0
  82. package/test/token-ledger-live.test.mjs +351 -0
  83. package/test/token-ledger.test.mjs +264 -0
  84. package/test/toolface-desc-slim.test.mjs +255 -0
  85. package/test/toolface-executor.test.mjs +302 -0
  86. package/test/toolface-mask.test.mjs +258 -0
  87. package/test/toolface-rules.test.mjs +177 -0
  88. package/test/toolface-runtime.test.mjs +466 -0
  89. package/test/toolface-wiring.test.mjs +272 -0
  90. package/test/usage-ledger-inject.test.mjs +50 -0
  91. package/test/usage-ledger.test.mjs +399 -0
@@ -0,0 +1,26 @@
1
+ // dsh-lean-router 2.0.0 —— 包入口(lib/index.js)的 cordis 契约回归。
2
+ // 2.0.0 起技能子系统下线,入口只剩一个合法插件形状;保留本测试是因为历史上两次真机事故都出在这里:
3
+ // 1.0.6: inject 写成 { required: [], optional: [...] } → 被当成两个服务名,宿主行永远 PENDING。
4
+ // 1.0.2: apply 返回普通对象 → cordis 抛 Invalid effect,整条 preset 不注册。
5
+ import test from "node:test";
6
+ import assert from "node:assert/strict";
7
+
8
+ import * as entry from "../lib/index.js";
9
+
10
+ test("包可被导入,且有 name / inject / apply", () => {
11
+ assert.equal(typeof entry.name, "string");
12
+ assert.ok(Array.isArray(entry.inject), "inject 必须是服务名数组或服务名映射,不能是 { required, optional }");
13
+ assert.equal(typeof entry.apply, "function");
14
+ });
15
+
16
+ test("inject 里不出现 required / optional 这类伪服务名", () => {
17
+ const keys = Array.isArray(entry.inject) ? entry.inject : Object.keys(entry.inject);
18
+ assert.ok(!keys.includes("required") && !keys.includes("optional"), "inject 键:" + keys.join(","));
19
+ });
20
+
21
+ test("apply 返回 undefined,且不读任何服务属性(cordis 会因未 inject 直接抛)", () => {
22
+ const ctx = new Proxy({}, {
23
+ get(_t, key) { throw new Error("apply 不该读 ctx." + String(key)); },
24
+ });
25
+ assert.equal(entry.apply(ctx, {}), undefined);
26
+ });
@@ -0,0 +1,98 @@
1
+ // dsh-lean-router 票 05 —— 三带配置表与解析纯函数单测(宿主纯 node --test 可跑)
2
+ import test from "node:test";
3
+ import assert from "node:assert/strict";
4
+
5
+ import {
6
+ BANDS,
7
+ BAND_IDS,
8
+ DIRECTIVE_BAND_IDS,
9
+ TASK_ECHO_PREFIX,
10
+ classifyTask,
11
+ diligenceReminder,
12
+ isComplexTask,
13
+ parseRouterDirective,
14
+ } from "../lib/bands.js";
15
+
16
+ test("BANDS 三带形状:id/label/personaText/guideText 齐备且工具面宽窄标记符合实测", () => {
17
+ assert.deepEqual([...Object.keys(BANDS)].sort(), [...BAND_IDS].sort());
18
+ for (const id of BAND_IDS) {
19
+ const band = BANDS[id];
20
+ assert.equal(band.id, id);
21
+ assert.equal(typeof band.label, "string");
22
+ assert.ok(band.label.length > 0, id + " label");
23
+ assert.equal(typeof band.personaText, "string");
24
+ assert.ok(band.personaText.length > 20, id + " personaText 非空");
25
+ assert.equal(typeof band.guideText, "string");
26
+ assert.ok(band.guideText.startsWith("Near-field guide"), id + " guideText 固定前缀");
27
+ assert.ok(/Recall/.test(band.personaText), id + " persona 含回顾锚");
28
+ assert.ok(/Converge/.test(band.personaText), id + " persona 含收敛锚");
29
+ assert.ok(/drift/.test(band.personaText), id + " persona 含反跑题锚");
30
+ }
31
+ assert.equal(BANDS.react.toolsWide, true);
32
+ assert.equal(BANDS.spec.toolsWide, false);
33
+ assert.equal(BANDS.weak.toolsWide, false);
34
+ });
35
+
36
+ test("引导文本:不再强制任何开头句式,只保留固定前缀与固定基调", () => {
37
+ // 曾经要求 spec 以 "We need …"、react 以 "I'm doing …" 开头;实机使用后判定无收益,已移除。
38
+ // 这里反向断言,防止句式被重新加回来。
39
+ for (const id of BAND_IDS) {
40
+ assert.ok(!BANDS[id].guideText.includes("We need"), id + " 引导不含 We need 句式");
41
+ assert.ok(!BANDS[id].guideText.includes("I'm doing"), id + " 引导不含 I'm doing 句式");
42
+ assert.ok(!BANDS[id].guideText.includes("first reply"), id + " 引导不约束回复首句");
43
+ }
44
+ // 固定前缀与固定基调不变(GUIDE_TAIL 仍保留)。
45
+ for (const id of BAND_IDS) {
46
+ assert.ok(BANDS[id].guideText.startsWith("Near-field guide"), id + " 引导固定前缀");
47
+ assert.ok(BANDS[id].guideText.includes("Recall what you have already done"), id + " 保留 GUIDE_TAIL");
48
+ assert.ok(BANDS[id].guideText.includes("Converge"), id + " 保留收敛锚");
49
+ }
50
+ // personaText 当前无运行时消费者:保持原样,不新增句式。
51
+ for (const id of BAND_IDS) {
52
+ assert.ok(!BANDS[id].personaText.includes("We need"), id + " persona 不含新句式");
53
+ assert.ok(!BANDS[id].personaText.includes("I'm doing"), id + " persona 不含新句式");
54
+ }
55
+ });
56
+
57
+ test("classifyTask:build -> react,fix -> spec,平票/无匹配/空 -> weak", () => {
58
+ assert.equal(classifyTask("帮我开发一个新的贪吃蛇网页游戏"), "react");
59
+ assert.equal(classifyTask("Create a new CLI tool from scratch"), "react");
60
+ assert.equal(classifyTask("修复登录页 500 报错"), "spec");
61
+ assert.equal(classifyTask("排查登录页为什么崩溃"), "spec");
62
+ assert.equal(classifyTask("看看这个"), "weak");
63
+ assert.equal(classifyTask(""), "weak");
64
+ });
65
+
66
+ test("classifyTask 混合证据:计数占优者赢,平票 weak", () => {
67
+ assert.equal(classifyTask("开发一个新工具并实现脚本生成网页"), "react");
68
+ assert.equal(classifyTask("修一下再开发"), "weak");
69
+ });
70
+
71
+ test("parseRouterDirective:解析 [router] band/task 指令,auto/大小写/无指令均正确", () => {
72
+ assert.deepEqual(parseRouterDirective("[router] band=spec task=修复登录超时"), { band: "spec", task: "修复登录超时" });
73
+ assert.deepEqual(parseRouterDirective("[router] band=REACT"), { band: "react" });
74
+ assert.deepEqual(parseRouterDirective("[router] band=auto"), { band: "auto" });
75
+ assert.deepEqual(parseRouterDirective("[router]band=weak"), { band: "weak" });
76
+ assert.equal(parseRouterDirective("普通消息没有指令"), null);
77
+ assert.equal(parseRouterDirective(null), null);
78
+ assert.equal(parseRouterDirective("[router] band=mixed"), null);
79
+ });
80
+
81
+ test("isComplexTask:长文本或架构词为复杂任务", () => {
82
+ assert.equal(isComplexTask("hello"), false);
83
+ assert.equal(isComplexTask("x".repeat(121)), true);
84
+ assert.equal(isComplexTask("重构整个模块"), true);
85
+ assert.equal(isComplexTask("system architecture"), true);
86
+ });
87
+
88
+ test("diligenceReminder:固定句式含轮次号", () => {
89
+ const r = diligenceReminder(30);
90
+ assert.ok(r.includes("Diligence self-check (turn 30)"));
91
+ assert.ok(r.includes("deliverable"));
92
+ assert.notEqual(diligenceReminder(10), diligenceReminder(20));
93
+ });
94
+
95
+ test("DIRECTIVE_BAND_IDS = 三带 + auto", () => {
96
+ assert.deepEqual([...DIRECTIVE_BAND_IDS].sort(), ["auto", "react", "spec", "weak"]);
97
+ assert.equal(TASK_ECHO_PREFIX, "Task: ");
98
+ });
@@ -0,0 +1,215 @@
1
+ // dsh-lean-router 票 05 —— 思考链路由层假 ctx 集成单测(宿主纯 node --test 可跑)
2
+ // 断言面(约束台账 acceptance 3):首轮捕获 / [router] 解析 / 改档 / 近场引导注入 / 勤勉轮次。
3
+ import test from "node:test";
4
+ import assert from "node:assert/strict";
5
+
6
+ import { BANDS, GUIDE_CONTEXT_NAME, internals, apply } from "../lib/router.js";
7
+
8
+ const { capture, guideTextFor, executeBandCommand } = internals;
9
+
10
+ /** 最小假 ctx:on / systemPrompt.context / commands.register 全部可观测。 */
11
+ function makeCtx() {
12
+ const handlers = new Map();
13
+ const contexts = new Map();
14
+ const commands = new Map();
15
+ const ctx = {
16
+ on(type, handler) {
17
+ if (!handlers.has(type)) handlers.set(type, []);
18
+ handlers.get(type).push(handler);
19
+ return () => {};
20
+ },
21
+ systemPrompt: {
22
+ context(def) {
23
+ contexts.set(def.name, def);
24
+ return () => {};
25
+ },
26
+ },
27
+ commands: {
28
+ register(def) {
29
+ commands.set(def.name, def);
30
+ return () => {};
31
+ },
32
+ },
33
+ };
34
+ return { ctx, handlers, contexts, commands };
35
+ }
36
+
37
+ async function emitClaimed(ctx, handlers, agent, message, turn) {
38
+ for (const handler of handlers.get("agent/inbox/claimed") ?? []) await handler({ agent, message, turn });
39
+ }
40
+
41
+ const userMessage = (text) => ({ source: { kind: "user" }, content: [{ type: "text", text }] });
42
+ const agentA = { session: {} };
43
+ const agentB = { session: {} };
44
+
45
+ async function routedSetup(options) {
46
+ const { ctx, handlers, contexts, commands } = makeCtx();
47
+ assert.equal(apply(ctx, options), undefined, 'apply must not return a plain object (cordis "Invalid effect")');
48
+ return { ctx, handlers, contexts, commands };
49
+ }
50
+
51
+ test("apply 默认注册 hooks 与 commands", async () => {
52
+ const { handlers, contexts, commands } = await routedSetup({});
53
+ assert.ok(handlers.has("agent/inbox/claimed"));
54
+ assert.ok(contexts.has(GUIDE_CONTEXT_NAME));
55
+ assert.ok(commands.has("band"));
56
+ // 2.0.0:/skill /panel /lean-audit 随技能子系统一起下线,只保留 /band。
57
+ assert.equal(commands.size, 1);
58
+ });
59
+
60
+ test("每个命令的 input hint 都非空(宿主 normalizeDefinition 会抛 TypeError)", async () => {
61
+ // 1.0.6 回归:/panel 曾用 input:{hint:""},0.2.3 真机报
62
+ // TypeError: command "panel" input hint must not be empty,整条 lean-router-commands 不激活。
63
+ const { commands } = await routedSetup({});
64
+ assert.ok(commands.size >= 1, "至少要有 /band");
65
+ for (const [name, def] of commands) {
66
+ if (def.input === undefined) continue;
67
+ assert.equal(typeof def.input.hint, "string", name + " 的 input.hint 必须是字符串");
68
+ assert.ok(def.input.hint.trim().length > 0, "命令 " + name + " 的 input hint 不能为空");
69
+ }
70
+ });
71
+
72
+ test("options 分流:hooks:false 只留命令;commands:false 只留钩子", async () => {
73
+ const a = await routedSetup({ hooks: false });
74
+ assert.ok(!a.handlers.has("agent/inbox/claimed"));
75
+ assert.ok(!a.contexts.has(GUIDE_CONTEXT_NAME));
76
+ assert.ok(a.commands.has("band"));
77
+ const b = await routedSetup({ commands: false });
78
+ assert.ok(b.handlers.has("agent/inbox/claimed"));
79
+ assert.ok(!b.commands.has("band"));
80
+ });
81
+
82
+ test("resolveOptions:未知键报错、diligenceEvery 校验", async () => {
83
+ assert.throws(() => internals.resolveOptions({ nope: 1 }), /unknown option/i);
84
+ assert.throws(() => internals.resolveOptions({ diligenceEvery: -1 }), /diligenceEvery/);
85
+ assert.equal(internals.resolveOptions({}).diligenceEvery, 10);
86
+ });
87
+
88
+ test("首轮捕获:只认 source.kind === user;build 词首轮即 react", async () => {
89
+ const { ctx, handlers, contexts } = await routedSetup({});
90
+ const text = contexts.get(GUIDE_CONTEXT_NAME).text;
91
+ // 插件注入的消息不触发(kind != user)
92
+ await emitClaimed(ctx, handlers, agentA, { source: { kind: "plugin" }, content: [{ type: "text", text: "开发新功能" }] }, 1);
93
+ assert.equal(text({ agent: agentA }), "");
94
+ // 真实用户消息:build 证据 -> react 引导
95
+ await emitClaimed(ctx, handlers, agentA, userMessage("帮我开发一个贪吃蛇游戏"), 1);
96
+ assert.equal(text({ agent: agentA }), BANDS.react.guideText);
97
+ });
98
+
99
+ test("[router] 指令解析优先:band=spec + task 回显进引导文本", async () => {
100
+ const { ctx, handlers, contexts } = await routedSetup({});
101
+ const text = contexts.get(GUIDE_CONTEXT_NAME).text;
102
+ await emitClaimed(ctx, handlers, agentA, userMessage("[router] band=spec task=修复登录超时"), 1);
103
+ const guide = text({ agent: agentA });
104
+ assert.ok(guide.startsWith(BANDS.spec.guideText));
105
+ assert.ok(guide.includes("Task: 修复登录超时"));
106
+ });
107
+
108
+ test("近场引导注入:同状态文本恒定(缓存友好),auto/未路由为空串", async () => {
109
+ const { ctx, handlers, contexts } = await routedSetup({});
110
+ const text = contexts.get(GUIDE_CONTEXT_NAME).text;
111
+ await emitClaimed(ctx, handlers, agentA, userMessage("开发新工具"), 2);
112
+ assert.equal(text({ agent: agentA }), BANDS.react.guideText);
113
+ await emitClaimed(ctx, handlers, agentA, userMessage("继续"), 3);
114
+ assert.equal(text({ agent: agentA }), BANDS.react.guideText, "同带不同轮文本恒定");
115
+ // band=auto:显式回退未路由 -> 空串(不渲染、零写入)
116
+ await emitClaimed(ctx, handlers, agentB, userMessage("[router] band=auto"), 1);
117
+ assert.equal(text({ agent: agentB }), "");
118
+ // 无 agent 的装配上下文:空串
119
+ assert.equal(text({}), "");
120
+ });
121
+
122
+ test("勤勉轮次:每 10 轮追加自检文本,其余轮零变化", async () => {
123
+ const { ctx, handlers, contexts } = await routedSetup({ diligenceEvery: 10 });
124
+ const text = contexts.get(GUIDE_CONTEXT_NAME).text;
125
+ await emitClaimed(ctx, handlers, agentA, userMessage("开发新工具"), 9);
126
+ const turn9 = text({ agent: agentA });
127
+ assert.ok(!turn9.includes("Diligence"), "第 9 轮无自检");
128
+ await emitClaimed(ctx, handlers, agentA, userMessage("继续"), 10);
129
+ const turn10 = text({ agent: agentA });
130
+ assert.ok(turn10.startsWith(BANDS.react.guideText));
131
+ assert.ok(turn10.includes("Diligence self-check (turn 10)"), "第 10 轮追加自检");
132
+ await emitClaimed(ctx, handlers, agentA, userMessage("继续"), 11);
133
+ const turn11 = text({ agent: agentA });
134
+ assert.ok(!turn11.includes("Diligence"), "第 11 轮回落(文本与第 9 轮一致)");
135
+ assert.equal(turn11, turn9);
136
+ });
137
+
138
+ test("diligenceEvery: 0 关闭勤勉自检", async () => {
139
+ const { ctx, handlers, contexts } = await routedSetup({ diligenceEvery: 0 });
140
+ const text = contexts.get(GUIDE_CONTEXT_NAME).text;
141
+ await emitClaimed(ctx, handlers, agentA, userMessage("开发新工具"), 20);
142
+ assert.equal(text({ agent: agentA }), BANDS.react.guideText);
143
+ });
144
+
145
+ test("路径承诺:已路由后关键词不改带;显式 [router] 指令仍可改带", async () => {
146
+ const { ctx, handlers, contexts } = await routedSetup({});
147
+ const text = contexts.get(GUIDE_CONTEXT_NAME).text;
148
+ await emitClaimed(ctx, handlers, agentA, userMessage("开发新工具"), 1);
149
+ assert.equal(text({ agent: agentA }), BANDS.react.guideText);
150
+ // fix 关键词消息:不改带
151
+ await emitClaimed(ctx, handlers, agentA, userMessage("顺手修复一下报错"), 2);
152
+ assert.equal(text({ agent: agentA }), BANDS.react.guideText);
153
+ // 显式指令:改带
154
+ await emitClaimed(ctx, handlers, agentA, userMessage("[router] band=spec"), 3);
155
+ assert.ok(text({ agent: agentA }).startsWith(BANDS.spec.guideText));
156
+ });
157
+
158
+ test("子代理/继承会话跳过路由(skipInherited 默认 true,可关)", async () => {
159
+ const { ctx, handlers, contexts } = await routedSetup({});
160
+ const text = contexts.get(GUIDE_CONTEXT_NAME).text;
161
+ const child = { session: { parentSession: "session-parent" } };
162
+ await emitClaimed(ctx, handlers, child, userMessage("开发新工具"), 1);
163
+ assert.equal(text({ agent: child }), "");
164
+ const open = await routedSetup({ skipInherited: false });
165
+ const openText = open.contexts.get(GUIDE_CONTEXT_NAME).text;
166
+ await emitClaimed(open.ctx, open.handlers, child, userMessage("开发新工具"), 1);
167
+ assert.equal(openText({ agent: child }), BANDS.react.guideText);
168
+ });
169
+
170
+ test("/band:显示/改档/auto 回退/非法值报错", async () => {
171
+ const { ctx, handlers, contexts, commands } = await routedSetup({});
172
+ const text = contexts.get(GUIDE_CONTEXT_NAME).text;
173
+ const band = commands.get("band");
174
+ // 无参显示
175
+ const shown = band.handler({ agent: agentA, rawInput: "" });
176
+ assert.equal(shown.kind, "success");
177
+ assert.ok(shown.text.includes("▲ auto"), "第一行必须是一行状态");
178
+ assert.ok(shown.text.includes("| 档位 |"), "三带差异必须用表格展示");
179
+ assert.ok(shown.text.includes("最近一次自动分类:暂无"), "没有用户消息时给出明确说明");
180
+ assert.ok(shown.text.includes("下一条消息自动分类"), "未分类时来源说明为自动");
181
+ assert.ok(shown.text.includes("**▲ auto**"), "当前档位在表格里用 ▲ 粗体标记");
182
+ // 改档
183
+ const set = band.handler({ agent: agentA, rawInput: "react task = 发布 MVP" });
184
+ assert.equal(set.kind, "success");
185
+ assert.ok(set.text.includes("react"));
186
+ assert.ok(set.text.includes("Task: 发布 MVP"));
187
+ assert.equal(text({ agent: agentA }), BANDS.react.guideText + "\nTask: 发布 MVP");
188
+ // auto 回退
189
+ const back = band.handler({ agent: agentA, rawInput: "auto" });
190
+ assert.equal(back.kind, "success");
191
+ assert.equal(text({ agent: agentA }), "");
192
+ // 非法值
193
+ const bad = band.handler({ agent: agentA, rawInput: "mixed" });
194
+ assert.equal(bad.kind, "error");
195
+ // 无 agent
196
+ assert.equal(band.handler({}).kind, "error");
197
+ });
198
+
199
+ test("capture/guideTextFor 内部实现直调(状态隔离:两个 agent 互不影响)", () => {
200
+ const states = new WeakMap();
201
+ const changedA = capture(states, agentA, userMessage("修复报错"), 1, true);
202
+ assert.equal(changedA, true);
203
+ assert.equal(guideTextFor(states, agentA, 10), BANDS.spec.guideText);
204
+ assert.equal(guideTextFor(states, agentB, 10), "");
205
+ // 非 user 消息不触发
206
+ assert.equal(capture(states, agentB, { source: { kind: "tool" } }, 1, true), false);
207
+ assert.equal(guideTextFor(states, agentB, 10), "");
208
+ });
209
+
210
+ test("messageText:防御嵌套 message 形状与字符串 content", () => {
211
+ assert.equal(internals.messageText(userMessage("hello")), "hello");
212
+ assert.equal(internals.messageText({ source: { kind: "user" }, message: { content: [{ type: "text", text: "nested" }] } }), "nested");
213
+ assert.equal(internals.messageText({ source: { kind: "user" }, content: ["plain"] }), "plain");
214
+ assert.equal(internals.messageText(null), "");
215
+ });