@hugesb/dsh-stream-agent 0.0.0-stage → 0.3.1

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 (158) hide show
  1. package/CHANGELOG.md +497 -0
  2. package/CONTRIBUTING.md +72 -0
  3. package/LICENSE +202 -0
  4. package/README.en.md +349 -0
  5. package/README.md +300 -3
  6. package/cordis.patch.yml +77 -0
  7. package/dist/entry/action-gate.js +206 -0
  8. package/dist/entry/adapter-kit.js +192 -0
  9. package/dist/entry/context-render.js +119 -0
  10. package/dist/entry/control-bar-cli.js +289 -0
  11. package/dist/entry/control-bar.js +123 -0
  12. package/dist/entry/control-session.js +115 -0
  13. package/dist/entry/control-state.js +125 -0
  14. package/dist/entry/cordis-compat.js +95 -0
  15. package/dist/entry/eval-probe.js +112 -0
  16. package/dist/entry/intent-log.js +121 -0
  17. package/dist/entry/json-persistence.js +306 -0
  18. package/dist/entry/loop.js +3852 -0
  19. package/dist/entry/mock-provider.js +536 -0
  20. package/dist/entry/obs-broker.js +178 -0
  21. package/dist/entry/ptc-bridge.js +405 -0
  22. package/dist/entry/state-handle.js +217 -0
  23. package/dist/entry/takeover-registry.js +60 -0
  24. package/dist/kernel/control-bar.js +559 -0
  25. package/dist/kernel/final.js +62 -0
  26. package/dist/kernel/gate.js +464 -0
  27. package/dist/kernel/modes.js +253 -0
  28. package/dist/kernel/obs.js +517 -0
  29. package/dist/kernel/parser.js +632 -0
  30. package/dist/kernel/render.js +444 -0
  31. package/dist/kernel/state.js +416 -0
  32. package/dist/version.js +9 -0
  33. package/docs/TEST-PROPOSITIONS.md +139 -0
  34. package/docs/adr-001-segment-boundary.md +61 -0
  35. package/docs/architecture.md +104 -0
  36. package/docs/desktop-ui-feasibility.md +277 -0
  37. package/docs/disabled-lines-audit.md +53 -0
  38. package/docs/limitations.md +181 -0
  39. package/docs/protocol.md +99 -0
  40. package/docs/v0.4.0-plan.md +50 -0
  41. package/eval/METRICS.md +128 -0
  42. package/eval/RESULTS.md +246 -0
  43. package/eval/tasks/fixtures/big-report.md +298 -0
  44. package/eval/tasks/fixtures/build-manifest.txt +5 -0
  45. package/eval/tasks/fixtures/deploy.yaml +4 -0
  46. package/eval/tasks/fixtures/error.log +200 -0
  47. package/eval/tasks/fixtures/features.yaml +4 -0
  48. package/eval/tasks/fixtures/pipeline.yaml +4 -0
  49. package/eval/tasks/fixtures/sales-q1.csv +61 -0
  50. package/eval/tasks/fixtures/settings.yaml +4 -0
  51. package/eval/tasks/fixtures/users-roster.json +304 -0
  52. package/eval/tasks/fixtures/users-scores.json +20 -0
  53. package/eval/tasks/gen-fixtures.cjs +80 -0
  54. package/eval/tasks/tasks.json +150 -0
  55. package/eval/tools/f6-kill-drill.cjs +176 -0
  56. package/eval/tools/f6-kill-drill.worker.cjs +131 -0
  57. package/eval/tools/gen-results.cjs +191 -0
  58. package/eval/tools/machine-test.cjs +200 -0
  59. package/eval/tools/manual-code-scenarios.md +59 -0
  60. package/eval/tools/replay-check.cjs +311 -0
  61. package/eval/tools/runner.cjs +219 -0
  62. package/eval/tools/session-log.cjs +106 -0
  63. package/eval/tools/summarize.cjs +185 -0
  64. package/eval/tools/t3-cells.cjs +173 -0
  65. package/package.json +73 -6
  66. package/patches/adapters-smoke.yml +41 -0
  67. package/patches/eval-account.yml +36 -0
  68. package/patches/eval-baseline.yml +33 -0
  69. package/patches/eval-stream-code-off.yml +46 -0
  70. package/patches/eval-stream-code-on.yml +47 -0
  71. package/patches/eval-stream.yml +54 -0
  72. package/patches/takeover.yml +42 -0
  73. package/patches/test-preset.yml +23 -0
  74. package/preset/stream/cordis.patch.yml +39 -0
  75. package/preset/stream/package.json +13 -0
  76. package/preset.yml +56 -0
  77. package/profiles/stream/README.md +66 -0
  78. package/profiles/stream/cordis.patch.yml +12 -0
  79. package/profiles/stream/package.json +17 -0
  80. package/profiles/stream-headless/package.json +16 -0
  81. package/scripts/build.mjs +103 -0
  82. package/scripts/check-consistency.mjs +146 -0
  83. package/scripts/gate.mjs +138 -0
  84. package/scripts/install-check.mjs +116 -0
  85. package/scripts/install-cycle.mjs +88 -0
  86. package/scripts/release.mjs +415 -0
  87. package/scripts/repair-session-logs.mjs +403 -0
  88. package/src/entry/action-gate.ts +206 -0
  89. package/src/entry/adapter-kit.ts +192 -0
  90. package/src/entry/context-render.ts +119 -0
  91. package/src/entry/control-bar-cli.ts +289 -0
  92. package/src/entry/control-bar.ts +123 -0
  93. package/src/entry/control-session.ts +115 -0
  94. package/src/entry/control-state.ts +125 -0
  95. package/src/entry/cordis-compat.ts +95 -0
  96. package/src/entry/eval-probe.ts +112 -0
  97. package/src/entry/intent-log.ts +121 -0
  98. package/src/entry/json-persistence.ts +306 -0
  99. package/src/entry/loop.ts +3852 -0
  100. package/src/entry/mock-provider.ts +536 -0
  101. package/src/entry/obs-broker.ts +178 -0
  102. package/src/entry/ptc-bridge.ts +405 -0
  103. package/src/entry/state-handle.ts +217 -0
  104. package/src/entry/takeover-registry.ts +60 -0
  105. package/src/kernel/control-bar.ts +559 -0
  106. package/src/kernel/final.ts +62 -0
  107. package/src/kernel/gate.ts +464 -0
  108. package/src/kernel/modes.ts +253 -0
  109. package/src/kernel/obs.ts +517 -0
  110. package/src/kernel/parser.ts +632 -0
  111. package/src/kernel/render.ts +444 -0
  112. package/src/kernel/state.ts +416 -0
  113. package/src/version.ts +9 -0
  114. package/test/activation.test.ts +187 -0
  115. package/test/adapters.test.ts +676 -0
  116. package/test/control-bar.test.ts +486 -0
  117. package/test/desktop-ui.test.ts +359 -0
  118. package/test/eval-probe.test.ts +139 -0
  119. package/test/f5-plan.test.ts +221 -0
  120. package/test/f6-intent.test.ts +168 -0
  121. package/test/f7-segment.test.ts +190 -0
  122. package/test/fix-limits.test.ts +599 -0
  123. package/test/gate.test.ts +380 -0
  124. package/test/helpers/stub-loop.ts +240 -0
  125. package/test/kernel/obs.test.ts +329 -0
  126. package/test/kernel/parser.test.ts +450 -0
  127. package/test/loop.test.ts +1393 -0
  128. package/test/mock-provider.test.ts +386 -0
  129. package/test/ptc-bridge.test.ts +356 -0
  130. package/test/render.test.ts +418 -0
  131. package/test/s2-install-check.test.ts +46 -0
  132. package/test/scenarios/3-file-basic.json +13 -0
  133. package/test/scenarios/5-file-basic.json +22 -0
  134. package/test/scenarios/burn-more.json +9 -0
  135. package/test/scenarios/degraded-final.json +10 -0
  136. package/test/scenarios/fault-abort-xml.json +11 -0
  137. package/test/scenarios/fault-malformed-command.json +12 -0
  138. package/test/scenarios/fault-midstream-abort.json +11 -0
  139. package/test/scenarios/fault-saturating-output.json +9 -0
  140. package/test/scenarios/fault-timeout.json +9 -0
  141. package/test/scenarios/peek-first.json +11 -0
  142. package/test/scenarios/peek-second.json +11 -0
  143. package/test/scenarios/peek-third.json +10 -0
  144. package/test/scenarios/recover-final.json +10 -0
  145. package/test/scenarios/reroute-first.json +17 -0
  146. package/test/scenarios/reroute-second.json +16 -0
  147. package/test/scenarios/starvation-final.json +10 -0
  148. package/test/scenarios/starvation-promote.json +11 -0
  149. package/test/scenarios/wait-tools-continue.json +10 -0
  150. package/test/scenarios/wait-tools-xml.json +22 -0
  151. package/test/scenarios/wait-tools.json +15 -0
  152. package/test/session-format.test.ts +234 -0
  153. package/test/state.test.ts +335 -0
  154. package/test/stream-v02.test.ts +264 -0
  155. package/test/v031-display.test.ts +333 -0
  156. package/ui/client.js +576 -0
  157. package/ui/index.js +282 -0
  158. package/ui/package.json +26 -0
@@ -0,0 +1,450 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { TfStreamParser, parseStream, type ParserEvent, type TfCommand } from '../../src/kernel/parser.ts';
4
+
5
+ function commandsOf(events: ParserEvent[]): TfCommand[] {
6
+ return events.filter((e) => e.type === 'command').map((e) => (e as { command: TfCommand }).command);
7
+ }
8
+
9
+ function feedAll(parser: TfStreamParser, deltas: string[]): ParserEvent[] {
10
+ const out: ParserEvent[] = [];
11
+ for (const d of deltas) out.push(...parser.feed(d));
12
+ out.push(...parser.end());
13
+ return out;
14
+ }
15
+
16
+ // ---------------------------------------------------------------------------
17
+ // 基本解析与命令闭合
18
+ // ---------------------------------------------------------------------------
19
+
20
+ test('单条自闭合命令:闭合即产出', () => {
21
+ const p = new TfStreamParser();
22
+ const events = p.feed('<tf-read path="src/app.ts" id="r1"/>');
23
+ const cmds = commandsOf(events);
24
+ assert.equal(cmds.length, 1);
25
+ assert.equal(cmds[0].name, 'tf-read');
26
+ assert.equal(cmds[0].id, 'r1');
27
+ assert.equal(cmds[0].attrs['path'], 'src/app.ts');
28
+ assert.deepEqual(cmds[0].depends_on, []);
29
+ });
30
+
31
+ test('tf-write 正文跨 chunk 边界闭合(拆 3 个 delta 喂完一条命令)', () => {
32
+ const p = new TfStreamParser();
33
+ const deltas = [
34
+ '前情提要。<tf-write path="out.txt" id="w1" depends_on="r1">\n第一段正文...',
35
+ '...第二段正文,中间被切断 > 重定向符号也在正文里\n',
36
+ '...最后一段</tf-write>收尾 prose',
37
+ ];
38
+ const events = feedAll(p, deltas);
39
+ const cmds = commandsOf(events);
40
+ assert.equal(cmds.length, 1);
41
+ assert.equal(cmds[0].id, 'w1');
42
+ assert.equal(
43
+ cmds[0].body,
44
+ '\n第一段正文......第二段正文,中间被切断 > 重定向符号也在正文里\n...最后一段',
45
+ );
46
+ assert.deepEqual(cmds[0].depends_on, ['r1']);
47
+ // prose 透传:开头与结尾的文本原样保留
48
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
49
+ assert.equal(text, '前情提要。收尾 prose');
50
+ // 事件顺序:text → command → text(闭合即产出,不等流结束)
51
+ assert.equal(events[0].type, 'text');
52
+ assert.equal(events[1].type, 'command');
53
+ assert.equal(events[2].type, 'text');
54
+ });
55
+
56
+ test('头部属性值含 > (shell 重定向)不破坏头部扫描', () => {
57
+ const p = new TfStreamParser();
58
+ const events = p.feed('<tf-bash cmd="npm run build > out.log 2>&1" id="b1"/>');
59
+ const cmds = commandsOf(events);
60
+ assert.equal(cmds.length, 1);
61
+ assert.equal(cmds[0].attrs['cmd'], 'npm run build > out.log 2>&1');
62
+ });
63
+
64
+ test('id 缺省自动编号;空元素配对形式 <tf-read ...></tf-read> 合法', () => {
65
+ const p = new TfStreamParser();
66
+ const events = feedAll(p, ['<tf-read path="a"/><tf-final></tf-final>']);
67
+ const cmds = commandsOf(events);
68
+ assert.equal(cmds.length, 2);
69
+ assert.equal(cmds[0].id, 'cmd1');
70
+ assert.equal(cmds[1].id, 'cmd2');
71
+ assert.equal(cmds[1].name, 'tf-final');
72
+ });
73
+
74
+ test('tf-peek 带行号属性;tf-final 带 summary', () => {
75
+ const p = new TfStreamParser();
76
+ const events = feedAll(p, [
77
+ '<tf-peek handle="obs:read:c1@v1" from_line="10" to_line="20" id="p1"/>',
78
+ '<tf-final id="f1" summary="完成了"/>',
79
+ ]);
80
+ const cmds = commandsOf(events);
81
+ assert.equal(cmds.length, 2);
82
+ assert.equal(cmds[0].attrs['handle'], 'obs:read:c1@v1');
83
+ assert.equal(cmds[0].attrs['from_line'], '10');
84
+ assert.equal(cmds[0].attrs['to_line'], '20');
85
+ assert.equal(cmds[1].attrs['summary'], '完成了');
86
+ });
87
+
88
+ // ---------------------------------------------------------------------------
89
+ // prose 透传(绝不吞非协议文本)
90
+ // ---------------------------------------------------------------------------
91
+
92
+ test('模型 prose 原样透传(含 < 和 > 的普通文本不丢失)', () => {
93
+ const p = new TfStreamParser();
94
+ const prose = '分析如下:if (a < b) { ... } 否则 x > y。结论:需要读文件。';
95
+ const events = feedAll(p, [prose, '<tf-read path="a.ts" id="r1"/>', '读完了,总结。']);
96
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
97
+ assert.equal(text, `${prose}读完了,总结。`);
98
+ assert.equal(commandsOf(events).length, 1);
99
+ });
100
+
101
+ test('prose 被 chunk 任意切割后拼接仍逐字还原', () => {
102
+ const p = new TfStreamParser();
103
+ const prose = '这段长文本会被切成很多小块,包括引号 " 与反斜杠 \\ 与单引号 \',全部保留。';
104
+ const events: ParserEvent[] = [];
105
+ for (const ch of prose) events.push(...p.feed(ch));
106
+ events.push(...p.end());
107
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
108
+ assert.equal(text, prose);
109
+ });
110
+
111
+ // ---------------------------------------------------------------------------
112
+ // truncated:未闭合即流终止
113
+ // ---------------------------------------------------------------------------
114
+
115
+ test('tf-write 正文未闭合 → truncated(loop 重入依据)', () => {
116
+ const p = new TfStreamParser();
117
+ p.feed('<tf-write path="a.ts" id="w1">\npartial body');
118
+ const events = p.end();
119
+ const truncated = events.find((e) => e.type === 'truncated') as { raw: string; reason: string };
120
+ assert.ok(truncated);
121
+ assert.equal(truncated.reason, 'unclosed-command-body:tf-write');
122
+ assert.ok(truncated.raw.includes('partial body'));
123
+ assert.equal(events[events.length - 1].type, 'done');
124
+ });
125
+
126
+ test('头部标签未闭合 → truncated', () => {
127
+ const p = new TfStreamParser();
128
+ const events = p.feed('ok prose <tf-read path="a.ts"');
129
+ events.push(...p.end());
130
+ const texts = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
131
+ assert.equal(texts, 'ok prose ');
132
+ const truncated = events.find((e) => e.type === 'truncated') as { raw: string; reason: string };
133
+ assert.equal(truncated.reason, 'unclosed-command-tag');
134
+ assert.equal(truncated.raw, '<tf-read path="a.ts"');
135
+ });
136
+
137
+ test('<tf- 前缀残片悬在流尾 → truncated;普通 < 残片 → text', () => {
138
+ const p1 = new TfStreamParser();
139
+ const e1 = p1.end();
140
+ assert.deepEqual(e1.map((x) => x.type), ['done']);
141
+
142
+ const p2 = new TfStreamParser();
143
+ const e2 = p2.feed('abc');
144
+ e2.push(...p2.end());
145
+ assert.deepEqual(e2.map((x) => x.type), ['text', 'done']);
146
+
147
+ const p3 = new TfStreamParser();
148
+ const e3 = p3.feed('<tf-wr'); // need-more:无事件
149
+ e3.push(...p3.end());
150
+ assert.deepEqual(e3.map((x) => x.type), ['truncated', 'done']);
151
+ });
152
+
153
+ test('边界:非 tf 的 < 立即判 prose;悬空的 < 留到 end() 按 text 透传', () => {
154
+ const p1 = new TfStreamParser();
155
+ const e1 = p1.feed('a<b');
156
+ e1.push(...p1.end());
157
+ assert.deepEqual(e1.map((x) => x.type), ['text', 'text', 'done']);
158
+ assert.equal(e1.filter((x) => x.type === 'text').map((x) => (x as { text: string }).text).join(''), 'a<b');
159
+
160
+ const p2 = new TfStreamParser();
161
+ const e2 = p2.feed('abc<'); // '<' 无法判定(need-more),留缓冲
162
+ assert.deepEqual(e2, []);
163
+ e2.push(...p2.end());
164
+ assert.deepEqual(e2.map((x) => x.type), ['text', 'done']);
165
+ assert.equal((e2[0] as { text: string }).text, 'abc<');
166
+ });
167
+
168
+ test('feed 之后再 feed 抛错(流生命周期不可复用)', () => {
169
+ const p = new TfStreamParser();
170
+ p.feed('x');
171
+ p.end();
172
+ assert.throws(() => p.feed('y'), /already ended/);
173
+ });
174
+
175
+ // ---------------------------------------------------------------------------
176
+ // malformed:解析失败不静默丢弃
177
+ // ---------------------------------------------------------------------------
178
+
179
+ test('未知 tf-* 命令名 → malformed(不静默)', () => {
180
+ const p = new TfStreamParser();
181
+ const events = feedAll(p, ['<tf-frobnicate path="x"/>']);
182
+ const m = events.find((e) => e.type === 'malformed') as { reason: string; raw: string };
183
+ assert.ok(m, '必须产出 malformed 事件');
184
+ assert.equal(m.reason, 'unknown-command');
185
+ assert.equal(m.raw, '<tf-frobnicate path="x"/>');
186
+ });
187
+
188
+ test('缺必填属性 → malformed;未知属性 → malformed;非法行号 → malformed', () => {
189
+ const p = new TfStreamParser();
190
+ const events = feedAll(p, [
191
+ '<tf-read id="r1"/>', // 缺 path
192
+ '<tf-read path="a" bogus="1"/>', // 未知属性
193
+ '<tf-peek handle="h" from_line="x"/>', // 非法整数
194
+ ]);
195
+ const reasons = events.filter((e) => e.type === 'malformed').map((e) => (e as { reason: string }).reason);
196
+ assert.equal(reasons.length, 3);
197
+ assert.ok(reasons[0].startsWith('missing-attribute:path'));
198
+ assert.ok(reasons[1].startsWith('unknown-attribute:bogus'));
199
+ assert.ok(reasons[2].startsWith('bad-integer-attribute:from_line'));
200
+ });
201
+
202
+ test('属性语法错误与重复属性 → malformed;重复 id 透传(v0.2 起由驱动器裁决幂等/冲突)', () => {
203
+ const p = new TfStreamParser();
204
+ const events = feedAll(p, [
205
+ '<tf-read path=a.ts/>', // 属性值没有引号
206
+ '<tf-read path="a" path="b"/>', // 重复属性
207
+ '<tf-read path="a" id="dup"/>',
208
+ '<tf-read path="b" id="dup"/>', // 重复 id:parser 透传,不在本层拦截
209
+ ]);
210
+ const reasons = events.filter((e) => e.type === 'malformed').map((e) => (e as { reason: string }).reason);
211
+ assert.equal(reasons.length, 2);
212
+ assert.ok(reasons[0].startsWith('bad-attribute-syntax'));
213
+ assert.ok(reasons[1].startsWith('duplicate-attribute:path'));
214
+ assert.equal(commandsOf(events).length, 2, '重复 id 命令透传给驱动器(幂等/冲突语义在账本层)');
215
+ });
216
+
217
+ test('非 body 命令带非空正文 → malformed', () => {
218
+ const p = new TfStreamParser();
219
+ const events = p.feed('<tf-read path="a">junk</tf-read>');
220
+ const m = events.find((e) => e.type === 'malformed') as { reason: string };
221
+ assert.ok(m);
222
+ assert.equal(m.reason, 'unexpected-body');
223
+ });
224
+
225
+ // ---------------------------------------------------------------------------
226
+ // depends_on:可执行集合与环检测
227
+ // ---------------------------------------------------------------------------
228
+
229
+ test('readyCommands:依赖全部完成才进入可执行集合', () => {
230
+ const p = new TfStreamParser();
231
+ const events = feedAll(p, [
232
+ '<tf-read path="a.ts" id="r1"/>',
233
+ '<tf-write path="b.ts" id="w1" depends_on="r1, s1">',
234
+ 'body',
235
+ '</tf-write>',
236
+ '<tf-bash cmd="npm test" id="b1" depends_on="w1"/>',
237
+ ]);
238
+ const cmds = commandsOf(events);
239
+ assert.equal(cmds.length, 3);
240
+
241
+ // 初始:只有 r1 就绪(w1 依赖 r1 与未出现的 s1;b1 依赖 w1)
242
+ assert.deepEqual(p.readyCommands().map((c) => c.id), ['r1']);
243
+ p.markDone('r1');
244
+ assert.deepEqual(p.readyCommands().map((c) => c.id), []); // s1 未出现,w1 不就绪
245
+ });
246
+
247
+ test('readyCommands:依赖全部完成后逐级解锁', () => {
248
+ const p = new TfStreamParser();
249
+ feedAll(p, [
250
+ '<tf-read path="a.ts" id="r1"/>',
251
+ '<tf-write path="b.ts" id="w1" depends_on="r1">body</tf-write>',
252
+ '<tf-bash cmd="npm test" id="b1" depends_on="w1"/>',
253
+ ]);
254
+ assert.deepEqual(p.readyCommands().map((c) => c.id), ['r1']);
255
+ p.markDone('r1');
256
+ assert.deepEqual(p.readyCommands().map((c) => c.id), ['w1']);
257
+ p.markDone('w1');
258
+ assert.deepEqual(p.readyCommands().map((c) => c.id), ['b1']);
259
+ p.markDone('b1');
260
+ assert.deepEqual(p.readyCommands().map((c) => c.id), []);
261
+ });
262
+
263
+ test('depends_on 去重保序;自依赖 → cycle 事件', () => {
264
+ const p = new TfStreamParser();
265
+ const events = p.feed('<tf-read path="a" id="r1" depends_on="r1, r1, , w2"/>');
266
+ const cmds = commandsOf(events);
267
+ assert.deepEqual(cmds[0].depends_on, ['r1', 'w2']);
268
+ const cycle = events.find((e) => e.type === 'cycle') as { cycle: readonly string[] };
269
+ assert.ok(cycle, '自依赖必须产出 cycle 事件');
270
+ assert.deepEqual([...cycle.cycle], ['r1', 'r1']);
271
+ });
272
+
273
+ test('A↔B 互依赖 → cycle 事件(后闭合的一方触发检测)', () => {
274
+ const p = new TfStreamParser();
275
+ const e1 = p.feed('<tf-write path="a" id="A" depends_on="B"/>');
276
+ assert.equal(e1.find((e) => e.type === 'cycle'), undefined); // B 还没出现
277
+ const e2 = p.feed('<tf-read path="b" id="B" depends_on="A"/>');
278
+ const cycle = e2.find((e) => e.type === 'cycle') as { cycle: readonly string[] };
279
+ assert.ok(cycle, 'B 闭合时 A↔B 环必须被检出');
280
+ assert.deepEqual([...cycle.cycle], ['A', 'B', 'A']);
281
+ // 同一环只报一次
282
+ const e3 = p.end();
283
+ assert.equal(e3.find((e) => e.type === 'cycle'), undefined);
284
+ });
285
+
286
+ test('三节点环 A→B→C→A', () => {
287
+ const p = new TfStreamParser();
288
+ const events = feedAll(p, [
289
+ '<tf-read path="a" id="A" depends_on="C"/>',
290
+ '<tf-read path="b" id="B" depends_on="A"/>',
291
+ '<tf-read path="c" id="C" depends_on="B"/>',
292
+ ]);
293
+ const cycle = events.find((e) => e.type === 'cycle') as { cycle: readonly string[] };
294
+ assert.ok(cycle);
295
+ assert.equal(cycle.cycle.length, 4); // [A, B, C, A]
296
+ assert.equal(cycle.cycle[0], cycle.cycle[3]);
297
+ });
298
+
299
+ // ---------------------------------------------------------------------------
300
+ // AsyncIterable 入口与混合流
301
+ // ---------------------------------------------------------------------------
302
+
303
+ test('parseStream:AsyncIterable 全流解析(含 done)', async () => {
304
+ async function* src(): AsyncIterable<string> {
305
+ yield '第一步。<tf-read path="a.ts" id="r1"/>';
306
+ yield '<tf-write path="b.ts" id="w1" depends_on="r1">content</tf-write>';
307
+ yield '<tf-final id="f1"/>';
308
+ }
309
+ const events = await parseStream(src());
310
+ assert.equal(events[events.length - 1].type, 'done');
311
+ const cmds = commandsOf(events);
312
+ assert.deepEqual(cmds.map((c) => c.id), ['r1', 'w1', 'f1']);
313
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
314
+ assert.equal(text, '第一步。');
315
+ });
316
+
317
+ test('混合流:prose + 三条命令 + malformed 混合,全部事件齐全', () => {
318
+ const p = new TfStreamParser();
319
+ const events = feedAll(p, [
320
+ '开始。\n',
321
+ '<tf-read path="a.ts" id="r1"/>',
322
+ '\n中间的思考。\n',
323
+ '<tf-bogus/>',
324
+ '<tf-write path="c.ts" id="w1">\n内容\n</tf-write>',
325
+ '<tf-final id="f1" summary="done"/>',
326
+ ]);
327
+ const types = events.map((e) => e.type);
328
+ assert.deepEqual(types, [
329
+ 'text', 'command', 'text', 'malformed', 'command', 'command', 'done',
330
+ ]);
331
+ const cmds = commandsOf(events);
332
+ assert.equal(cmds[0].id, 'r1');
333
+ assert.equal(cmds[1].body, '\n内容\n');
334
+ assert.equal(cmds[2].name, 'tf-final');
335
+ });
336
+
337
+ // ---------------------------------------------------------------------------
338
+ // DSML 噪声归一化(v0.3.1:思考过程乱码修复的解析半边)
339
+ // ---------------------------------------------------------------------------
340
+
341
+ test('DSML 包装行:malformed 上报并丢弃,不进 prose(真机形态)', () => {
342
+ const p = new TfStreamParser();
343
+ const events = feedAll(p, [
344
+ '\n\n<||DSML|| calls>\n',
345
+ '<||DSML|| invoke name="tf-plan" id="p1">\n- [a1] 验收项\n</tf-plan>',
346
+ ]);
347
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
348
+ assert.equal(text, '\n\n\n', 'DSML 标签本身不得作为 prose 透传(标签间空白换行保留)');
349
+ const malformed = events.filter((e) => e.type === 'malformed').map((e) => e as { reason: string });
350
+ assert.ok(malformed.some((m) => m.reason === 'dsml-wrapper'), '包装行应按 dsml-wrapper 上报');
351
+ // invoke + 配对闭合 → 命令不丢
352
+ const cmds = commandsOf(events);
353
+ assert.equal(cmds.length, 1);
354
+ assert.equal(cmds[0].name, 'tf-plan');
355
+ assert.equal(cmds[0].id, 'p1');
356
+ assert.equal(cmds[0].body, '\n- [a1] 验收项\n');
357
+ });
358
+
359
+ test('DSML invoke 自闭合:改写回 tf-* 命令(属性透传)', () => {
360
+ const p = new TfStreamParser();
361
+ const events = p.feed('<||DSML|| invoke name="tf-read" path="data/x.md" id="r9" offset="3" limit="4"/>');
362
+ const cmds = commandsOf(events);
363
+ assert.equal(cmds.length, 1);
364
+ assert.equal(cmds[0].name, 'tf-read');
365
+ assert.equal(cmds[0].id, 'r9');
366
+ assert.equal(cmds[0].attrs['path'], 'data/x.md');
367
+ assert.equal(cmds[0].attrs['offset'], '3');
368
+ assert.equal(cmds[0].attrs['limit'], '4');
369
+ });
370
+
371
+ test('DSML 残缺属性(真机 mangled 形态):malformed 丢弃,不作为 prose 泄漏', () => {
372
+ const p = new TfStreamParser();
373
+ const events = feedAll(p, [
374
+ '<||DSML|| invoke name="tf-read path="data/big-report.md" id="r1" offset="130" limit="15"/>',
375
+ ]);
376
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
377
+ assert.equal(text, '', 'mangled DSML 不得进 prose');
378
+ assert.ok(events.some((e) => e.type === 'malformed'));
379
+ assert.equal(commandsOf(events).length, 0);
380
+ });
381
+
382
+ test('DSML 尾巴(流终止未闭合):malformed 上报,不进 prose', () => {
383
+ const p = new TfStreamParser();
384
+ const events = feedAll(p, ['回答正文。<||DSML|| calls']);
385
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
386
+ assert.equal(text, '回答正文。');
387
+ assert.ok(events.some((e) => e.type === 'malformed' && (e as { reason: string }).reason === 'dsml-wrapper'));
388
+ });
389
+
390
+ test('全角竖线出现在普通 prose 中(非标签起始)不受影响', () => {
391
+ const p = new TfStreamParser();
392
+ const events = feedAll(p, ['评分 9|10 分,很好。']);
393
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
394
+ assert.equal(text, '评分 9|10 分,很好。');
395
+ assert.equal(events.filter((e) => e.type === 'malformed').length, 0);
396
+ });
397
+
398
+ test('DSML 跨 chunk 边界:闭合跨流仍能改写(增量喂入)', () => {
399
+ const p = new TfStreamParser();
400
+ const events = feedAll(p, [
401
+ '<||DSM',
402
+ 'L|| invoke name="tf-glob" pattern="**/*.md" id="gl1"/>',
403
+ ]);
404
+ const cmds = commandsOf(events);
405
+ assert.equal(cmds.length, 1);
406
+ assert.equal(cmds[0].name, 'tf-glob');
407
+ assert.equal(cmds[0].id, 'gl1');
408
+ });
409
+
410
+ // ---------------------------------------------------------------------------
411
+ // 孤立闭合标签(v0.3.1「任务半途截断」修复)
412
+ // 真机证据:模型给自闭合命令又补了一次闭合,`</tf-bash>` 以纯文本落进正文,
413
+ // 桌面端过程区里就能看到裸协议残片。
414
+ // ---------------------------------------------------------------------------
415
+
416
+ test('孤立闭合标签不进 prose:记 malformed(stray-close-tag) 后丢弃', () => {
417
+ const p = new TfStreamParser();
418
+ const events = feedAll(p, ['先看一眼\n<tf-glob pattern="**/*" id="gl4"/>\n继续\n</tf-bash>\n']);
419
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
420
+ assert.equal(text, '先看一眼\n\n继续\n\n');
421
+ assert.ok(!text.includes('</tf-bash>'), '裸闭合标签不得进正文');
422
+ const mal = events.filter((e) => e.type === 'malformed') as Array<{ reason: string }>;
423
+ assert.equal(mal.length, 1);
424
+ assert.equal(mal[0]!.reason, 'stray-close-tag:tf-bash');
425
+ assert.equal(commandsOf(events).length, 1, '正常命令不受影响');
426
+ });
427
+
428
+ test('孤立的配对式闭合(跨 chunk)同样丢弃', () => {
429
+ const p = new TfStreamParser();
430
+ const events = feedAll(p, ['abc</t', 'f-write>xyz']);
431
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
432
+ assert.equal(text, 'abcxyz');
433
+ assert.ok(events.some((e) => e.type === 'malformed' && (e as { reason: string }).reason === 'stray-close-tag:tf-write'));
434
+ });
435
+
436
+ test('流末尾半截闭合标签:不留在 prose 里', () => {
437
+ const p = new TfStreamParser();
438
+ const events = feedAll(p, ['文字</tf-ba']);
439
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
440
+ assert.equal(text, '文字');
441
+ assert.ok(events.some((e) => e.type === 'malformed' && (e as { reason: string }).reason.startsWith('stray-close-tag')));
442
+ });
443
+
444
+ test('普通 HTML 闭合标签仍按正文透传(不过度拦截)', () => {
445
+ const p = new TfStreamParser();
446
+ const events = feedAll(p, ['正文 </div> 结束,</tf 不在标签位']);
447
+ const text = events.filter((e) => e.type === 'text').map((e) => (e as { text: string }).text).join('');
448
+ assert.ok(text.includes('</div>'), 'HTML 闭合标签属于正常正文');
449
+ assert.equal(events.filter((e) => e.type === 'malformed').length, 0);
450
+ });