@firstpick/pi-package-webui 0.9.5 → 0.9.7

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 (82) hide show
  1. package/README.md +11 -5
  2. package/bin/pi-webui.mjs +457 -77
  3. package/lib/git-workflow-preferences.mjs +21 -1
  4. package/lib/optional-feature-catalog.mjs +1 -1
  5. package/lib/session-actions.mjs +20 -2
  6. package/lib/session-tree-event-targets.mjs +70 -0
  7. package/lib/subagent-launch-policy.mjs +375 -0
  8. package/lib/subagent-launch-slots.mjs +2 -1
  9. package/package.json +1 -1
  10. package/public/app.js +2539 -292
  11. package/public/index.html +87 -31
  12. package/public/service-worker.js +4 -1
  13. package/public/stream-derived-output.mjs +799 -0
  14. package/public/stream-markdown-tail.mjs +204 -0
  15. package/public/stream-output-controller.mjs +193 -51
  16. package/public/stream-render-scheduler.mjs +85 -0
  17. package/public/styles.css +501 -19
  18. package/public/transcript-renderer.mjs +73 -24
  19. package/tests/boot-failure-diagnostics.test.mjs +3 -3
  20. package/tests/browser/composer-action-grid-reorder.spec.mjs +114 -31
  21. package/tests/browser/control-visibility.spec.mjs +141 -2
  22. package/tests/browser/events-tree-context-menu.spec.mjs +240 -0
  23. package/tests/browser/guided-git-fallback.spec.mjs +153 -0
  24. package/tests/browser/mobile-foundation.spec.mjs +28 -0
  25. package/tests/browser/stream-output-isolation.spec.mjs +308 -3
  26. package/tests/browser/user-noticeable-fixes.spec.mjs +204 -0
  27. package/tests/codex-fast-mode-static.test.mjs +2 -2
  28. package/tests/compact-sections-static.test.mjs +4 -0
  29. package/tests/composer-action-grid-reorder-static.test.mjs +8 -7
  30. package/tests/control-visibility-static.test.mjs +66 -3
  31. package/tests/controls-layout-static.test.mjs +3 -3
  32. package/tests/cwd-footer-reliability-static.test.mjs +1 -1
  33. package/tests/events-tree-context-menu-static.test.mjs +131 -0
  34. package/tests/fast-mode-client-static.test.mjs +5 -2
  35. package/tests/feature-decision-output-popup-static.test.mjs +2 -2
  36. package/tests/file-viewer-image-static.test.mjs +3 -3
  37. package/tests/fixtures/fake-pi.mjs +57 -10
  38. package/tests/fixtures/streaming-workloads.mjs +36 -0
  39. package/tests/footer-provider-usage-visual-static.test.mjs +15 -0
  40. package/tests/foreground-transcript-catch-up-static.test.mjs +7 -1
  41. package/tests/git-workflow-preferences.test.mjs +30 -1
  42. package/tests/guided-git-fallback-runtime.test.mjs +418 -0
  43. package/tests/guided-git-fallback-ui-static.test.mjs +60 -0
  44. package/tests/guided-git-message-reliability-static.test.mjs +8 -7
  45. package/tests/interaction-state-stability-static.test.mjs +3 -1
  46. package/tests/intercom-conversation-viewer-static.test.mjs +3 -3
  47. package/tests/intercom-conversations-http.test.mjs +16 -0
  48. package/tests/main-output-loading-static.test.mjs +3 -3
  49. package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
  50. package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
  51. package/tests/mobile-foundation-static.test.mjs +3 -3
  52. package/tests/mobile-phone-experience-static.test.mjs +1 -1
  53. package/tests/mobile-static.test.mjs +80 -67
  54. package/tests/open-issue-wizard-static.test.mjs +1 -1
  55. package/tests/persistent-ui-layout-static.test.mjs +4 -4
  56. package/tests/prompt-input-vertical-scroll-static.test.mjs +26 -0
  57. package/tests/questionnaire-dialog.test.mjs +3 -3
  58. package/tests/remote-auth-settings-harness.test.mjs +23 -0
  59. package/tests/resource-defaults-helper.test.mjs +12 -0
  60. package/tests/runtime-error-visibility.test.mjs +3 -3
  61. package/tests/session-auth-harness.test.mjs +26 -1
  62. package/tests/session-sampling-helper.test.mjs +1 -0
  63. package/tests/session-sampling-ui-static.test.mjs +3 -3
  64. package/tests/session-tree-event-targets.test.mjs +106 -0
  65. package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
  66. package/tests/stream-derived-output.test.mjs +121 -0
  67. package/tests/stream-markdown-tail.test.mjs +218 -0
  68. package/tests/stream-output-controller.test.mjs +119 -4
  69. package/tests/stream-output-isolation-static.test.mjs +27 -6
  70. package/tests/stream-output-workloads.test.mjs +40 -3
  71. package/tests/stream-render-scheduler.test.mjs +115 -0
  72. package/tests/streaming-ui-coupling.test.mjs +1 -1
  73. package/tests/subagent-launch-policy.test.mjs +532 -0
  74. package/tests/subagent-observability-static.test.mjs +2 -2
  75. package/tests/subagents-helper.test.mjs +211 -1
  76. package/tests/syntax-highlighting-static.test.mjs +3 -3
  77. package/tests/tab-close-continuity-static.test.mjs +23 -0
  78. package/tests/theme-customizer-static.test.mjs +1 -1
  79. package/tests/thinking-format-parser.test.mjs +15 -0
  80. package/tests/todo-filter-static.test.mjs +148 -0
  81. package/tests/transcript-markdown-tail.test.mjs +137 -0
  82. package/webui-rpc-helper.mjs +220 -3
@@ -0,0 +1,218 @@
1
+ import assert from "node:assert/strict";
2
+ import {
3
+ STREAMING_MARKDOWN_PLAIN_TAIL_LIMIT,
4
+ advanceStreamingMarkdownTail,
5
+ } from "../public/stream-markdown-tail.mjs";
6
+
7
+ function append(state, text, options = {}) {
8
+ return advanceStreamingMarkdownTail(state, text, { appendOnly: !!state, ...options });
9
+ }
10
+
11
+ function referenceTail(text, plainTailLimit = STREAMING_MARKDOWN_PLAIN_TAIL_LIMIT) {
12
+ const value = String(text || "");
13
+ const lines = value.split("\n");
14
+ // CommonMark: an opening fence is 3+ backticks (optionally followed by an info string); the
15
+ // closing fence must have at least as many backticks and nothing but whitespace after them.
16
+ let inFence = false;
17
+ let fenceTicks = 0;
18
+ let boundary = 0;
19
+ let offset = 0;
20
+ let fenceContentOffset = -1;
21
+ let fenceLanguage = "";
22
+ const closingFence = (line) => {
23
+ const match = line.match(/^\s*(`{3,})\s*$/);
24
+ return Boolean(match && match[1].length >= fenceTicks);
25
+ };
26
+ const openingFence = (line) => {
27
+ const match = line.match(/^\s*(`{3,})\s*([\w.+-]*)\s*$/);
28
+ return match ? { ticks: match[1].length, language: match[2] || "" } : null;
29
+ };
30
+ for (let index = 0; index < lines.length - 1; index += 1) {
31
+ const line = lines[index];
32
+ if (inFence) {
33
+ if (closingFence(line)) {
34
+ inFence = false;
35
+ fenceTicks = 0;
36
+ fenceContentOffset = -1;
37
+ fenceLanguage = "";
38
+ boundary = offset + line.length + 1;
39
+ }
40
+ } else {
41
+ const opening = openingFence(line);
42
+ if (opening) {
43
+ inFence = true;
44
+ fenceTicks = opening.ticks;
45
+ fenceContentOffset = offset + line.length + 1;
46
+ fenceLanguage = opening.language;
47
+ }
48
+ }
49
+ offset += line.length + 1;
50
+ if (!inFence && !line.trim()) boundary = offset;
51
+ }
52
+ const pendingRaw = lines.at(-1) || "";
53
+ const pending = pendingRaw.endsWith("\r") ? pendingRaw.slice(0, -1) : pendingRaw;
54
+ const pendingOpening = !inFence ? openingFence(pending) : null;
55
+ const pendingClose = inFence && closingFence(pending);
56
+ if (pendingClose) return { boundary, liveMode: "authoritative", tailKind: "closed-fence", fenceContentOffset, fenceLanguage };
57
+ if (inFence) return { boundary, liveMode: "open-fence", tailKind: "open-fence", fenceContentOffset, fenceLanguage };
58
+ if (pendingOpening) return { boundary, liveMode: "open-fence", tailKind: "open-fence", fenceContentOffset: value.length, fenceLanguage: pendingOpening.language };
59
+ if (value.length - boundary > plainTailLimit) return { boundary, liveMode: "plain", tailKind: "long-text", fenceContentOffset, fenceLanguage };
60
+ return { boundary, liveMode: "markdown", tailKind: "text", fenceContentOffset, fenceLanguage };
61
+ }
62
+
63
+ for (const sample of [
64
+ "paragraph without a newline",
65
+ "alpha\r\n\r\n ``` ts \r\nconst value = `x`;\r\n```\r\nafter",
66
+ "```mermaid\nflowchart LR\n```\n",
67
+ "```js\ncode\n```` still code\n```",
68
+ " ``` c++\nline one\nline two\n ``` \r\n",
69
+ ]) {
70
+ let state = null;
71
+ for (let end = 1; end <= sample.length; end += 1) {
72
+ const text = sample.slice(0, end);
73
+ const result = append(state, text);
74
+ state = result.state;
75
+ const expected = referenceTail(text);
76
+ assert.deepEqual(
77
+ {
78
+ boundary: result.boundary,
79
+ liveMode: result.liveMode,
80
+ tailKind: result.tailKind,
81
+ fenceContentOffset: result.fenceContentOffset,
82
+ fenceLanguage: result.fenceLanguage,
83
+ },
84
+ expected,
85
+ `incremental line state must match authoritative boundary semantics at split ${end} of ${JSON.stringify(sample)}`,
86
+ );
87
+ assert.equal(result.scannedChars, 1);
88
+ assert.equal(state.scanOffset, text.length);
89
+ }
90
+ }
91
+
92
+ {
93
+ let state = null;
94
+ let text = "stable paragraph\n\n";
95
+ let result = append(state, text);
96
+ state = result.state;
97
+ assert.equal(result.boundary, text.length);
98
+ assert.equal(result.scannedChars, text.length);
99
+
100
+ for (const suffix of ["```js", "\n", "const smile = '😀';", "\nconsole.log(smile);"]) {
101
+ text += suffix;
102
+ result = append(state, text);
103
+ state = result.state;
104
+ assert.equal(result.scannedChars, suffix.length, "only the newly appended UTF-16 suffix may be boundary-scanned");
105
+ assert.equal(result.boundary, "stable paragraph\n\n".length, "the committed prefix must not be rescanned or moved inside an open fence");
106
+ assert.equal(result.liveMode, "open-fence");
107
+ assert.equal(result.tailKind, "open-fence");
108
+ }
109
+
110
+ text += "\n```";
111
+ result = append(state, text);
112
+ state = result.state;
113
+ assert.equal(result.scannedChars, 4);
114
+ assert.equal(result.liveMode, "authoritative", "a recognized closing delimiter must request one authoritative tail render");
115
+ assert.equal(result.tailKind, "closed-fence");
116
+
117
+ text += "\n";
118
+ result = append(state, text);
119
+ assert.equal(result.scannedChars, 1);
120
+ assert.equal(result.boundary, text.length, "a newline-terminated closed fence becomes a safe committed block checkpoint");
121
+ }
122
+
123
+ {
124
+ const prefix = "alpha\n\n";
125
+ let result = append(null, prefix);
126
+ const previous = result.state;
127
+ result = append(previous, `${prefix}beta`);
128
+ assert.equal(result.scannedChars, 4);
129
+ assert.equal(result.fallback, false);
130
+ result = append(result.state, `${prefix}replacement`, { appendOnly: false });
131
+ assert.equal(result.fallback, true, "a divergent snapshot must use the authoritative scanner fallback");
132
+ assert.equal(result.scannedChars, `${prefix}replacement`.length);
133
+ }
134
+
135
+ {
136
+ const longTail = "x".repeat(STREAMING_MARKDOWN_PLAIN_TAIL_LIMIT + 1);
137
+ const result = append(null, longTail);
138
+ assert.equal(result.liveMode, "plain", "ambiguous tails above the bound must use the cheap live representation");
139
+ assert.equal(result.tailKind, "long-text");
140
+ const completed = append(result.state, longTail, { complete: true });
141
+ assert.equal(completed.boundary, longTail.length);
142
+ assert.equal(completed.liveMode, "authoritative", "completion must restore authoritative Markdown semantics");
143
+ assert.equal(completed.scannedChars, 0, "completion of an unchanged value does not rescan the committed prefix");
144
+ }
145
+
146
+ {
147
+ let state = null;
148
+ let text = "```mermaid\nflowchart LR";
149
+ let result = append(state, text);
150
+ state = result.state;
151
+ assert.equal(result.liveMode, "open-fence", "incomplete Mermaid remains plain live code");
152
+ assert.equal(result.fenceLanguage, "mermaid");
153
+ text += "\n```";
154
+ result = append(state, text);
155
+ assert.equal(result.liveMode, "authoritative", "Mermaid becomes authoritative only after its close delimiter");
156
+ }
157
+
158
+ {
159
+ let state = null;
160
+ let text = "";
161
+ let result;
162
+ const length = STREAMING_MARKDOWN_PLAIN_TAIL_LIMIT + 4096;
163
+ for (let index = 0; index < length; index += 1) {
164
+ text += index === length - 1 ? "😀" : "x";
165
+ const suffixLength = index === length - 1 ? 2 : 1;
166
+ result = append(state, text);
167
+ state = result.state;
168
+ assert.equal(result.scannedChars, suffixLength, "a growing no-newline paragraph must examine exactly its new suffix");
169
+ }
170
+ assert.equal(result.liveMode, "plain");
171
+ assert.equal(result.tailKind, "long-text");
172
+ assert.equal(result.boundary, 0);
173
+ assert.equal(state.scanOffset, text.length, "the scan cursor must advance past a known non-fence partial line");
174
+ assert.equal(state.lineStage, "invalid", "a known non-fence line must retain only bounded classification state");
175
+ }
176
+
177
+ {
178
+ let state = null;
179
+ let text = "";
180
+ let result;
181
+ for (const suffix of [" ", "`", "`", "`", " ", "j", "s", "\r", "\n"]) {
182
+ text += suffix;
183
+ result = append(state, text);
184
+ state = result.state;
185
+ assert.equal(result.scannedChars, suffix.length, "split opening-fence and CRLF input must scan only each suffix");
186
+ }
187
+ assert.equal(result.liveMode, "open-fence");
188
+ assert.equal(result.fenceLanguage, "js");
189
+ assert.equal(result.fenceContentOffset, text.length);
190
+
191
+ const codeLength = STREAMING_MARKDOWN_PLAIN_TAIL_LIMIT + 2048;
192
+ for (let index = 0; index < codeLength; index += 3) {
193
+ const suffix = "abc".slice(0, Math.min(3, codeLength - index));
194
+ text += suffix;
195
+ result = append(state, text);
196
+ state = result.state;
197
+ assert.equal(result.scannedChars, suffix.length, "a long open-fence code line must scan only its appended chunk");
198
+ assert.equal(result.liveMode, "open-fence");
199
+ assert.equal(result.fenceLanguage, "js");
200
+ assert.equal(state.scanOffset, text.length, "open-fence code scanning must advance past the accumulated partial line");
201
+ }
202
+ assert.equal(state.lineStage, "invalid", "ordinary code content must stop fence-candidate work after its first decisive character");
203
+
204
+ for (const suffix of ["\r", "\n", "`", "`", "`", " ", " ", "\r"]) {
205
+ text += suffix;
206
+ result = append(state, text);
207
+ state = result.state;
208
+ assert.equal(result.scannedChars, suffix.length, "split closing-fence input must scan only each suffix");
209
+ }
210
+ assert.equal(result.liveMode, "authoritative");
211
+ assert.equal(result.tailKind, "closed-fence");
212
+ text += "\n";
213
+ result = append(state, text);
214
+ assert.equal(result.scannedChars, 1);
215
+ assert.equal(result.boundary, text.length);
216
+ }
217
+
218
+ console.log("stream-markdown-tail.test.mjs passed");
@@ -108,33 +108,47 @@ assert.equal(reconcileTranscriptThinkingSnapshot("draft reasoning", "provider co
108
108
  assert.deepEqual(applied, [chunks.join("")], "coalescing must preserve exact output bytes and order");
109
109
  }
110
110
 
111
- // Alternating kinds cannot coalesce, so count overflow drains synchronously
112
- // instead of dropping or retaining an unbounded queue.
111
+ // First pressure leaves the primary queue within its configured bound and uses
112
+ // one explicit urgent slot. Repeated same-task pressure falls back synchronously
113
+ // rather than dropping, reordering, or growing another slot.
113
114
  {
114
115
  const frames = new FakeFrames();
116
+ const pressureTasks = new FakeFrames();
115
117
  const calls = [];
116
118
  const overflows = [];
119
+ const diagnostics = [];
117
120
  const controller = createStreamOutputController({
118
121
  scheduleFrame: frames.schedule,
119
122
  cancelFrame: frames.cancel,
123
+ schedulePressureDrain: pressureTasks.schedule,
124
+ cancelPressureDrain: pressureTasks.cancel,
120
125
  maxPendingEntries: 4,
121
126
  maxPendingBytes: 10_000,
122
127
  applyTextUpdate: (event) => calls.push(`t${event.assistantMessageEvent.delta}`),
123
128
  applyThinkingUpdate: (event) => calls.push(`h${event.assistantMessageEvent.delta}`),
124
129
  onOverflow: (overflow) => overflows.push(overflow.reason),
130
+ onDiagnostic: (record) => diagnostics.push(record),
125
131
  });
126
132
  const expected = [];
127
133
  for (let index = 0; index < 21; index += 1) {
128
134
  const text = index % 2 === 0;
129
135
  expected.push(`${text ? "t" : "h"}${index}`);
130
136
  controller.dispatch(messageUpdate(text ? "text_delta" : "thinking_delta", String(index)));
131
- assert.ok(controller.pendingCount() <= 4, "pending count must never exceed its configured bound");
132
- assert.ok(controller.pendingBytes() <= 10_000, "pending bytes must never exceed their configured bound");
137
+ assert.ok(controller.pendingCount() <= controller.limits().maxPendingEntries + controller.limits().maxUrgentEntries);
138
+ assert.ok(controller.pendingBytes() <= controller.limits().maxPendingBytes + controller.limits().maxUrgentBytes);
139
+ if (index === 4) {
140
+ assert.deepEqual(calls, [], "first pressure must not apply inside dispatch");
141
+ assert.equal(pressureTasks.callbacks.size, 1, "first pressure should schedule one urgent drain task");
142
+ assert.equal(controller.hasScheduledPressureDrain(), true);
143
+ }
133
144
  }
134
145
  controller.barrier("test-overflow-tail");
135
146
  assert.deepEqual(calls, expected);
136
147
  assert.ok(overflows.includes("entry-limit"));
148
+ assert.ok(diagnostics.some((record) => record.type === "pressure" && record.action === "deferred"));
149
+ assert.ok(diagnostics.some((record) => record.type === "pressure" && record.action === "synchronous-fallback"));
137
150
  assert.equal(frames.callbacks.size, 0);
151
+ assert.equal(pressureTasks.callbacks.size, 0);
138
152
  }
139
153
 
140
154
  // A single oversize event is applied directly after the prior batch. It is not
@@ -254,7 +268,108 @@ for (const mode of ["normal", "compact-v1"]) {
254
268
  assert.deepEqual(seen, [`${mode}:same output`, `${mode}:follow`], `${mode} should use the same coalescing and ordering contract`);
255
269
  }
256
270
 
271
+ // Conservative accounting covers supported Unicode, envelope, content-index,
272
+ // tool-ID, structured, and unknown message-update shapes without undercounting
273
+ // their JSON UTF-16 representation.
274
+ {
275
+ const frames = new FakeFrames();
276
+ const samples = [
277
+ messageUpdate("text_delta", "emoji 😀 and newline\nquote \" slash \\ lone \ud800", { contentIndex: 0, provider: "α" }),
278
+ messageUpdate("future_delta", "unknown", { contentIndex: 7, nested: { values: [1, true, null, "雪"] } }),
279
+ { type: "tool_execution_update", toolCallId: "tool-雪", partialResult: { content: "structured 😀", details: [{ ok: true }] } },
280
+ ];
281
+ for (const sample of samples) {
282
+ const controller = createStreamOutputController({ scheduleFrame: frames.schedule, cancelFrame: frames.cancel });
283
+ controller.dispatch(sample);
284
+ assert.ok(controller.pendingBytes() >= JSON.stringify(sample).length * 2, "supported event accounting must not undercount JSON UTF-16 bytes");
285
+ controller.cancel();
286
+ }
287
+ }
288
+
289
+ // Delta merges add only escaped payload bytes while adopting the incoming
290
+ // envelope, exactly matching the merged event even when metadata changes.
291
+ {
292
+ const frames = new FakeFrames();
293
+ const first = { ...messageUpdate("text_delta", "A😀", { contentIndex: 3 }), envelope: "old-long-envelope" };
294
+ const second = { ...messageUpdate("text_delta", "\nB", { contentIndex: 3 }), envelope: "new", toolCallId: "ignored-for-text" };
295
+ const expected = {
296
+ ...second,
297
+ assistantMessageEvent: { ...second.assistantMessageEvent, delta: "A😀\nB" },
298
+ };
299
+ const controller = createStreamOutputController({ scheduleFrame: frames.schedule, cancelFrame: frames.cancel });
300
+ controller.dispatch(first);
301
+ controller.dispatch(second);
302
+ assert.equal(controller.pendingCount(), 1);
303
+ assert.ok(controller.pendingBytes() >= JSON.stringify(expected).length * 2);
304
+
305
+ controller.dispatch(messageUpdate("text_delta", "different-index", { contentIndex: 4 }));
306
+ assert.equal(controller.pendingCount(), 2, "content-index changes must prevent merging");
307
+ controller.cancel();
308
+
309
+ const toolController = createStreamOutputController({ scheduleFrame: frames.schedule, cancelFrame: frames.cancel });
310
+ toolController.dispatch(messageUpdate("toolcall_delta", "{", { contentIndex: 2, toolCall: { id: "tool-a" } }));
311
+ toolController.dispatch(messageUpdate("toolcall_delta", "}", { contentIndex: 2, toolCall: { id: "tool-b" } }));
312
+ assert.equal(toolController.pendingCount(), 2, "tool-call IDs must partition adjacent argument deltas");
313
+ toolController.cancel();
314
+ }
315
+
316
+ // Exact merge limits remain meaningful: the merged representation fits at its
317
+ // measured limit, while one byte less uses the bounded urgent pressure slot.
318
+ {
319
+ const first = messageUpdate("text_delta", "abc", { contentIndex: 0 });
320
+ const second = messageUpdate("text_delta", "雪😀", { contentIndex: 0 });
321
+ const merged = { ...second, assistantMessageEvent: { ...second.assistantMessageEvent, delta: "abc雪😀" } };
322
+ const exactBytes = JSON.stringify(merged).length * 2;
323
+ const frames = new FakeFrames();
324
+ const pressureTasks = new FakeFrames();
325
+ const exact = createStreamOutputController({
326
+ scheduleFrame: frames.schedule,
327
+ cancelFrame: frames.cancel,
328
+ schedulePressureDrain: pressureTasks.schedule,
329
+ cancelPressureDrain: pressureTasks.cancel,
330
+ maxPendingBytes: exactBytes,
331
+ });
332
+ exact.dispatch(first);
333
+ exact.dispatch(second);
334
+ assert.equal(exact.pendingCount(), 1);
335
+ assert.equal(exact.hasScheduledPressureDrain(), false);
336
+ exact.cancel();
337
+
338
+ const constrained = createStreamOutputController({
339
+ scheduleFrame: frames.schedule,
340
+ cancelFrame: frames.cancel,
341
+ schedulePressureDrain: pressureTasks.schedule,
342
+ cancelPressureDrain: pressureTasks.cancel,
343
+ maxPendingBytes: exactBytes - 1,
344
+ });
345
+ constrained.dispatch(first);
346
+ constrained.dispatch(second);
347
+ assert.equal(constrained.pendingCount(), 2, "primary plus one urgent entry should retain both source deltas");
348
+ assert.equal(constrained.hasScheduledPressureDrain(), true);
349
+ constrained.barrier("merge-limit");
350
+ }
351
+
352
+ // Superseding partial tool execution remains latest-wins with accounting based
353
+ // on the retained latest structured event.
354
+ {
355
+ const frames = new FakeFrames();
356
+ const applied = [];
357
+ const latest = { type: "tool_execution_update", toolCallId: "same", partialResult: { content: "latest 😀", nested: { done: false } } };
358
+ const controller = createStreamOutputController({
359
+ scheduleFrame: frames.schedule,
360
+ cancelFrame: frames.cancel,
361
+ applyToolExecutionUpdate: (event) => applied.push(event.partialResult.content),
362
+ });
363
+ controller.dispatch({ type: "tool_execution_update", toolCallId: "same", partialResult: { content: "older" } });
364
+ controller.dispatch(latest);
365
+ assert.equal(controller.pendingCount(), 1);
366
+ assert.ok(controller.pendingBytes() >= JSON.stringify(latest).length * 2);
367
+ frames.run();
368
+ assert.deepEqual(applied, ["latest 😀"]);
369
+ }
370
+
257
371
  assert.throws(() => createStreamOutputController({ maxPendingEntries: 0 }), /positive safe integer/);
258
372
  assert.throws(() => createStreamOutputController({ maxPendingBytes: -1 }), /positive safe integer/);
373
+ assert.throws(() => createStreamOutputController({ schedulePressureDrain: null }), /must be functions/);
259
374
 
260
375
  console.log("stream-output-controller.test.mjs passed");
@@ -6,6 +6,7 @@ import { fileURLToPath } from "node:url";
6
6
  const root = join(dirname(fileURLToPath(import.meta.url)), "..");
7
7
  const app = await readFile(join(root, "public", "app.js"), "utf8");
8
8
  const controller = await readFile(join(root, "public", "stream-output-controller.mjs"), "utf8");
9
+ const derivedOutput = await readFile(join(root, "public", "stream-derived-output.mjs"), "utf8");
9
10
 
10
11
  function functionBody(source, name) {
11
12
  const match = new RegExp(`function\\s+${name}\\s*\\(`).exec(source);
@@ -37,14 +38,22 @@ const forbiddenSinks = [
37
38
  ".focus(", "scrollIntoView", "fetch(", "api(",
38
39
  ];
39
40
 
41
+ assert.match(app, /import \{ createStreamDerivedOutputState \} from "\.\/stream-derived-output\.mjs";/);
42
+ assert.match(app, /import \{ createLatestWinsRenderScheduler \} from "\.\/stream-render-scheduler\.mjs";/);
40
43
  assert.match(app, /import \{[^}]*classifyTranscriptStreamEvent[^}]*createStreamOutputController[^}]*\} from "\.\/stream-output-controller\.mjs";/);
44
+ assert.match(derivedOutput, /export function createStreamDerivedOutputState/);
45
+ assert.match(derivedOutput, /export function deriveStreamOutputAuthoritative/);
46
+ assert.doesNotMatch(derivedOutput, /document\.|window\.|HTMLElement|requestAnimationFrame|fetch\(/, "derived-output state should remain a pure independently testable browser module");
41
47
  assert.match(controller, /export function classifyTranscriptStreamEvent/);
42
48
  assert.match(controller, /export function createStreamOutputController/);
43
- assert.match(controller, /let frameHandle = null;[\s\S]*?let pending = \[\];[\s\S]*?let pendingBytes = 0;/, "controller should retain one cancellable bounded queue");
49
+ assert.match(controller, /let frameHandle = null;[\s\S]*?let pressureHandle = null;[\s\S]*?let pending = \[\];[\s\S]*?let pendingBytes = 0;[\s\S]*?let urgentEntry = null;/, "controller should retain one primary queue and one bounded urgent pressure entry");
44
50
  assert.match(controller, /if \(classification\.barrier\) \{[\s\S]*?flush\(\);/, "recognized raw end/error barriers should flush synchronously");
45
51
  assert.match(controller, /COALESCIBLE_DELTA_TYPES[\s\S]*?function mergedAdjacentEntry[\s\S]*?previousKey !== entryCoalesceKey\(incoming\)/, "adjacent compatible stream deltas should coalesce before rendering");
46
52
  assert.match(controller, /pending\.length >= entryLimit|pendingBytes \+ entry\.bytes > byteLimit/, "pending count and bytes should have explicit bounds");
47
- assert.match(controller, /reason: "oversize-event"[\s\S]*?applyOversize\(entry\)/, "single oversize events should be applied directly rather than dropped");
53
+ assert.match(controller, /URGENT_PRESSURE_ENTRY_LIMIT = 1[\s\S]*?action: "deferred"[\s\S]*?schedulePressure\(\)/, "first pressure should use one bounded deferred urgent slot");
54
+ assert.match(controller, /action: "synchronous-fallback"[\s\S]*?flush\(\)/, "repeated pressure should retain a synchronous lossless fallback");
55
+ assert.match(controller, /reportOverflow\("oversize-event", entry\);\s+applyOversize\(entry\)/, "single oversize events should be applied directly rather than dropped");
56
+ assert.doesNotMatch(controller, /function eventByteSize[\s\S]*?JSON\.stringify\(event\)\.length/, "growing merged deltas must not be repeatedly serialized for byte accounting");
48
57
  assert.match(controller, /if \(!ownerIsCurrent\(owner\)\)[\s\S]*?reportStale/, "stale owners should fail closed");
49
58
 
50
59
  for (const sink of forbiddenSinks) {
@@ -53,6 +62,9 @@ for (const sink of forbiddenSinks) {
53
62
 
54
63
  const dispatch = functionBody(app, "dispatchTranscriptStreamEvent");
55
64
  const rawMessageHandler = functionBody(app, "handleMessageUpdate");
65
+ const appendStreamRawText = functionBody(app, "appendStreamRawText");
66
+ const setStreamRawText = functionBody(app, "setStreamRawText");
67
+ const recordStreamDerivedOutputDiagnostic = functionBody(app, "recordStreamDerivedOutputDiagnostic");
56
68
  const handleEvent = functionBody(app, "handleEvent");
57
69
  const toolUpdate = functionBody(app, "applyTranscriptToolExecutionUpdate");
58
70
  const dispatchIndex = handleEvent.indexOf("dispatchTranscriptStreamEvent(event)");
@@ -67,19 +79,28 @@ for (const sink of forbiddenSinks) {
67
79
  assert.equal(rawMessageHandler.includes(sink), false, `message-update transcript sink must not reach forbidden sink ${sink}`);
68
80
  }
69
81
  assert.doesNotMatch(rawMessageHandler, /setRunIndicatorActivity|scheduleLiveTodoProgressWidgetSync|scheduleStreamingAssistantTextRender|scheduleChatFollowScroll/, "raw message deltas must bypass composer activity, widgets, and parallel schedulers");
82
+ assert.match(rawMessageHandler, /update\.type === "text_delta" \|\| update\.type === "text_end"\)[\s\S]*?syncStreamRawTextFromUpdate\(event, update\);[\s\S]*?if \(update\.type === "text_end"\) fallbackStreamDerivedOutput\("settlement:text_end"\);[\s\S]*?assistantStreamRenderScheduler\.request\(\{ complete: update\.type === "text_end" \}\);[\s\S]*?assistantStreamRenderScheduler\.flushNow\("text_end"\)/, "text_end should force authoritative derivation and synchronously flush final Markdown completion");
83
+ assert.match(app, /createLatestWinsRenderScheduler\([\s\S]*?renderStreamingAssistantText[\s\S]*?createLatestWinsRenderScheduler\([\s\S]*?setStreamingThinkingText/, "normal assistant and thinking formatting should each use the independently tested cadence scheduler");
84
+ assert.match(app, /function requestNormalStreamFollowAfterRender\(\)[\s\S]*?normalStreamRenderPending\(\)[\s\S]*?normalStreamFollowPending = true;[\s\S]*?scheduleChatFollowScroll\(\);/, "follow-scroll should wait for deferred normal formatting");
85
+ assert.match(appendStreamRawText, /ensureStreamDerivedOutputState\(\)\.append\(text\)/, "text deltas should advance append-only derived state with only the new suffix");
86
+ assert.doesNotMatch(appendStreamRawText, /stripTodoProgressLines|splitThinkingFormatText|resetStreamDerivedTextCache/, "text deltas must not invalidate or run a full-parse cache");
87
+ assert.match(setStreamRawText, /\.replace\(nextText, \{ reason \}\)/, "snapshots should take the explicit authoritative fallback path");
88
+ assert.match(recordStreamDerivedOutputDiagnostic, /diagnostic\.scannedCodeUnits \* 2/, "derive diagnostics should report bytes actually scanned");
89
+ assert.match(recordStreamDerivedOutputDiagnostic, /type: "derive-fallback"[\s\S]*?type: "derive-shadow-mismatch"/, "fallbacks and shadow mismatches should remain observable in debug diagnostics");
70
90
  assert.match(rawMessageHandler, /compactOutputActive\(\) && handleCompactMessageUpdate\(event\)/, "normal and compact modes should share the isolated controller sink");
71
91
  assert.match(toolUpdate, /renderLiveToolRun\(run, \{ scroll: false \}\)/, "isolated tool updates should render in the controller frame rather than queue another tool frame");
72
92
  assert.doesNotMatch(toolUpdate, /scheduleLiveToolRunRender/, "isolated tool updates must not create a parallel render queue");
73
93
  assert.match(app, /applyToolExecutionUpdate: \(event\) => \{\s+if \(!compactOutputActive\(\) && !isIntercomTransportToolName\(event\?\.toolName\)\) applyTranscriptToolExecutionUpdate\(event\);/, "raw non-Intercom tool execution updates should use only the transcript sink");
74
94
  assert.doesNotMatch(app.slice(app.indexOf("const streamOutputController"), app.indexOf("let assistantErrorSurfacedThisRun")), /renderWidgets|renderFooter|renderTabs|renderStatus|renderFeedbackTray|setRunIndicatorActivity|scheduleLiveTodoProgressWidgetSync|addEvent|fetch\(|api\(/, "controller integration must inject transcript-only sinks");
75
95
  const connectEvents = functionBody(app, "connectEvents");
76
- assert.match(connectEvents, /streamOutputController\.cancel\(\);[\s\S]*?resetCompactLiveOutput\(\);/, "reconnect and tab switches should cancel retained raw frames before resetting live output");
77
- assert.match(connectEvents, /source\.onerror = \(\) => \{[\s\S]*?streamOutputController\.flush\(\);[\s\S]*?streamOutputController\.cancel\(\);/, "an actual EventSource disconnect must flush and cancel retained transcript work before browser retry");
96
+ assert.match(connectEvents, /flushNormalStreamRenders\("reconnect"\);[\s\S]*?cancelNormalStreamRenders\(\);[\s\S]*?streamOutputController\.cancel\(\);[\s\S]*?resetCompactLiveOutput\(\);/, "reconnect and tab switches should flush normal formatting before canceling retained raw frames");
97
+ assert.match(connectEvents, /source\.onerror = \(\) => \{[\s\S]*?streamOutputController\.flush\(\);[\s\S]*?flushNormalStreamRenders\("disconnect"\);[\s\S]*?streamOutputController\.cancel\(\);/, "an actual EventSource disconnect must flush formatting and cancel retained transcript work before browser retry");
78
98
  const barrier = functionBody(app, "flushTranscriptStreamBarrier");
79
99
  const abort = functionBody(app, "abortActiveRun");
80
- assert.match(barrier, /streamOutputController\.barrier\(event\.type\)/, "semantic lifecycle boundaries should flush raw output first");
100
+ assert.match(barrier, /streamOutputController\.barrier\(event\.type\);[\s\S]*?fallbackStreamDerivedOutput[\s\S]*?flushNormalStreamRenders\(event\.type\);/, "semantic lifecycle boundaries should flush raw state, reconcile authoritative derivation, and then publish pending formatting");
81
101
  assert.match(app, /"auto_retry_start",\s+"auto_retry_end",/, "retry start/end should be explicit stream barriers");
82
- assert.ok(abort.indexOf('streamOutputController.barrier("user-abort")') < abort.indexOf("abortRequestInFlight = true"), "accepted user abort must flush transcript output before chrome/RPC transitions");
102
+ assert.ok(abort.indexOf('streamOutputController.barrier("user-abort")') < abort.indexOf('flushNormalStreamRenders("user-abort")'), "accepted user abort should flush raw ordering before pending formatting");
103
+ assert.ok(abort.indexOf('flushNormalStreamRenders("user-abort")') < abort.indexOf("abortRequestInFlight = true"), "accepted user abort must flush formatted transcript output before chrome/RPC transitions");
83
104
  assert.doesNotMatch(handleEvent, /case "message_update"|case "tool_execution_update"/, "raw event switch cases consumed by the controller must be removed");
84
105
  assert.doesNotMatch(app, /function scheduleStreamingAssistantTextRender|function handleToolExecutionUpdate|scheduleLiveToolRunRender/, "superseded raw render schedulers must be removed");
85
106
  assert.match(app, /onUnknownStreamEvent: preserveUnknownTranscriptEvidence/, "unknown transcript-shaped events should retain transcript-only evidence");
@@ -5,8 +5,10 @@ import {
5
5
  createStreamOutputController,
6
6
  } from "../public/stream-output-controller.mjs";
7
7
  import {
8
+ backgroundForegroundReconciliationScenario,
8
9
  concatenatedDeltaText,
9
10
  longParagraphDeltaEvents,
11
+ longTranscriptActiveStreamScenario,
10
12
  messageUpdateEvent,
11
13
  mixedSemanticBurstEvents,
12
14
  openFenceDeltaEvents,
@@ -113,8 +115,8 @@ function harness({ diagnostics = true, nowStepMs = 1 } = {}) {
113
115
  const overflow = run.records.filter((record) => record.type === "overflow");
114
116
  assert.ok(overflow.length > 0, "1 MB through a 256 KB queue must report overflow");
115
117
  for (const record of run.records.filter((entry) => entry.type === "queued")) {
116
- assert.ok(record.pendingBytes <= DEFAULT_STREAM_PENDING_BYTE_LIMIT, "pending bytes must never exceed the configured bound");
117
- assert.ok(record.pendingCount <= DEFAULT_STREAM_PENDING_ENTRY_LIMIT, "pending entries must never exceed the configured bound");
118
+ assert.ok(record.pendingBytes <= DEFAULT_STREAM_PENDING_BYTE_LIMIT * 2, "primary plus one urgent entry must remain within the explicit total bound");
119
+ assert.ok(record.pendingCount <= DEFAULT_STREAM_PENDING_ENTRY_LIMIT + 1, "primary plus one urgent entry must remain bounded");
118
120
  }
119
121
  }
120
122
 
@@ -181,7 +183,7 @@ for (const codeBytes of [40 * 1024, 100 * 1024]) {
181
183
  const entryOverflows = run.records.filter((record) => record.type === "overflow" && record.reason === "entry-limit");
182
184
  assert.ok(entryOverflows.length > 0, "400 non-coalescible events must hit the 128-entry limit");
183
185
  for (const record of run.records.filter((entry) => entry.type === "queued")) {
184
- assert.ok(record.pendingCount <= DEFAULT_STREAM_PENDING_ENTRY_LIMIT);
186
+ assert.ok(record.pendingCount <= DEFAULT_STREAM_PENDING_ENTRY_LIMIT + 1);
185
187
  }
186
188
  // Alternation must be preserved within each flushed window: text i before thinking i.
187
189
  const textIndex = run.applied.order.filter((kind) => kind.startsWith("text") || kind.startsWith("thinking"));
@@ -236,4 +238,39 @@ for (const codeBytes of [40 * 1024, 100 * 1024]) {
236
238
  assert.equal(run.applied.text.length, DEFAULT_STREAM_PENDING_BYTE_LIMIT, "oversize events are applied, never dropped");
237
239
  }
238
240
 
241
+ // 11) Background events are deterministic and reconcile to one exact snapshot.
242
+ {
243
+ const first = backgroundForegroundReconciliationScenario(1000);
244
+ const second = backgroundForegroundReconciliationScenario(1000);
245
+ assert.deepEqual(second, first, "background fixture must be byte-for-byte deterministic");
246
+ assert.deepEqual(first.hiddenEvents.map((event) => event.baselineIndex), Array.from({ length: 1000 }, (_, index) => index));
247
+ assert.equal(first.authoritativeText, concatenatedDeltaText(first.hiddenEvents));
248
+
249
+ const run = harness();
250
+ // Hidden-tab policy suppresses controller dispatch entirely. Foreground
251
+ // reconciliation applies the authoritative snapshot once, represented here
252
+ // by the same exact text delta consumed by the sink.
253
+ assert.equal(run.applied.text, "", "hidden events must not mutate live output");
254
+ run.controller.dispatch(messageUpdateEvent("text_delta", first.authoritativeText));
255
+ run.frames.runAll();
256
+ assert.equal(run.applied.text, first.authoritativeText, "foreground snapshot must restore exact hidden output once");
257
+ assert.equal(run.records.filter((record) => record.type === "receipt").length, 1, "catch-up must not replay hidden transport cadence");
258
+ }
259
+
260
+ // 12) A large retained transcript does not change active-stream order or text.
261
+ {
262
+ const scenario = longTranscriptActiveStreamScenario({ messageCount: 1000, deltaCount: 1000 });
263
+ const retainedBefore = structuredClone(scenario.retainedMessages);
264
+ assert.equal(scenario.retainedMessages.length, 1000);
265
+ assert.equal(scenario.retainedMessages[0].content.startsWith("LONG-TRANSCRIPT-0000"), true);
266
+ assert.equal(scenario.retainedMessages.at(-1).content[0].text.startsWith("LONG-TRANSCRIPT-0999"), true);
267
+
268
+ const run = harness();
269
+ run.dispatchAll(scenario.activeEvents);
270
+ run.frames.runAll();
271
+ assert.equal(run.applied.text, concatenatedDeltaText(scenario.activeEvents), "active stream must remain exact beside a long transcript");
272
+ assert.deepEqual(scenario.retainedMessages, retainedBefore, "active streaming must not mutate retained transcript fixtures");
273
+ assert.equal(run.records.filter((record) => record.type === "receipt").length, 1000);
274
+ }
275
+
239
276
  console.log("stream-output-workloads tests passed");
@@ -0,0 +1,115 @@
1
+ import assert from "node:assert/strict";
2
+ import {
3
+ NORMAL_STREAM_RENDER_INTERVAL_MS,
4
+ createLatestWinsRenderScheduler,
5
+ } from "../public/stream-render-scheduler.mjs";
6
+
7
+ class FakeClock {
8
+ nowMs = 0;
9
+ next = 1;
10
+ timers = new Map();
11
+
12
+ now = () => this.nowMs;
13
+
14
+ schedule = (callback, delay) => {
15
+ const handle = this.next++;
16
+ this.timers.set(handle, { callback, at: this.nowMs + delay });
17
+ return handle;
18
+ };
19
+
20
+ cancel = (handle) => {
21
+ this.timers.delete(handle);
22
+ };
23
+
24
+ advance(ms) {
25
+ this.nowMs += ms;
26
+ while (true) {
27
+ const due = [...this.timers.entries()]
28
+ .filter(([, timer]) => timer.at <= this.nowMs)
29
+ .sort((left, right) => left[1].at - right[1].at || left[0] - right[0])[0];
30
+ if (!due) return;
31
+ const [handle, timer] = due;
32
+ this.timers.delete(handle);
33
+ timer.callback();
34
+ }
35
+ }
36
+ }
37
+
38
+ assert.equal(NORMAL_STREAM_RENDER_INTERVAL_MS, 40, "the measured candidate cadence should remain inside the approved 32–50 ms range");
39
+
40
+ // First output is synchronous. Sustained writes are latest-wins and publish no
41
+ // more than once per 40 ms interval.
42
+ {
43
+ const clock = new FakeClock();
44
+ const rendered = [];
45
+ const diagnostics = [];
46
+ const scheduler = createLatestWinsRenderScheduler({
47
+ render: (value, metadata) => rendered.push({ value, reason: metadata.reason, at: clock.nowMs }),
48
+ now: clock.now,
49
+ schedule: clock.schedule,
50
+ cancelSchedule: clock.cancel,
51
+ onDiagnostic: (record) => diagnostics.push(record),
52
+ });
53
+
54
+ assert.equal(scheduler.request("first"), true);
55
+ assert.deepEqual(rendered, [{ value: "first", reason: "immediate", at: 0 }]);
56
+ scheduler.request("second");
57
+ scheduler.request("latest");
58
+ assert.equal(scheduler.pending(), true);
59
+ assert.equal(clock.timers.size, 1, "sustained requests should share one bounded timer");
60
+ clock.advance(39);
61
+ assert.equal(rendered.length, 1);
62
+ clock.advance(1);
63
+ assert.deepEqual(rendered[1], { value: "latest", reason: "cadence", at: 40 });
64
+ assert.equal(scheduler.pending(), false);
65
+ assert.equal(diagnostics.filter((record) => record.action === "defer").length, 1);
66
+ assert.equal(diagnostics.filter((record) => record.action === "flush").length, 2);
67
+ }
68
+
69
+ // Semantic barriers synchronously publish the latest value and cancel the
70
+ // scheduled callback, so completion cannot be stranded or duplicated.
71
+ {
72
+ const clock = new FakeClock();
73
+ const rendered = [];
74
+ const scheduler = createLatestWinsRenderScheduler({
75
+ render: (value, metadata) => rendered.push(`${metadata.reason}:${value}`),
76
+ now: clock.now,
77
+ schedule: clock.schedule,
78
+ cancelSchedule: clock.cancel,
79
+ });
80
+ scheduler.request("visible-first");
81
+ clock.advance(5);
82
+ scheduler.request("partial");
83
+ scheduler.request("authoritative-end");
84
+ assert.equal(scheduler.flushNow("text_end"), true);
85
+ assert.deepEqual(rendered, ["immediate:visible-first", "text_end:authoritative-end"]);
86
+ assert.equal(clock.timers.size, 0);
87
+ clock.advance(100);
88
+ assert.equal(rendered.length, 2, "the canceled cadence callback must not duplicate completion");
89
+ }
90
+
91
+ // Cancellation resets first-output eligibility for a new stream and never
92
+ // publishes the canceled owner.
93
+ {
94
+ const clock = new FakeClock();
95
+ const rendered = [];
96
+ const scheduler = createLatestWinsRenderScheduler({
97
+ render: (value) => rendered.push(value),
98
+ now: clock.now,
99
+ schedule: clock.schedule,
100
+ cancelSchedule: clock.cancel,
101
+ intervalMs: 32,
102
+ });
103
+ scheduler.request("old-first");
104
+ scheduler.request("old-pending");
105
+ scheduler.cancel();
106
+ assert.equal(scheduler.pending(), false);
107
+ assert.equal(clock.timers.size, 0);
108
+ scheduler.request("new-first");
109
+ assert.deepEqual(rendered, ["old-first", "new-first"]);
110
+ }
111
+
112
+ assert.throws(() => createLatestWinsRenderScheduler(), /render is required/);
113
+ assert.throws(() => createLatestWinsRenderScheduler({ render() {}, intervalMs: 0 }), /positive number/);
114
+
115
+ console.log("stream-render-scheduler.test.mjs passed");
@@ -172,7 +172,7 @@ futureInvariant("theory #3: streaming markdown must not full-rebuild when earlie
172
172
  const reconcileMarkdownSurface = findFunctionBody(transcriptRenderer, "reconcileMarkdownSurface");
173
173
  assert.match(renderStreamingMarkdown, /transcriptRenderer\.reconcileMarkdownSurface\(\{[\s\S]*?stableBoundary: streamingMarkdownStableBoundary[\s\S]*?renderInto: renderMarkdownInto/, "streaming markdown must route through the coordinator's committed-block/mutable-tail reconciler");
174
174
  assert.doesNotMatch(renderStreamingMarkdown, /block\.replaceChildren\(\)/, "streaming markdown must not replace the whole block directly");
175
- assert.match(reconcileMarkdownSurface, /!value\.startsWith\(state\.stableText\)[\s\S]*?invalidateSelection: diverged/, "retroactive todo/thinking rewrites must be explicit invalidations, not silent full rebuilds");
175
+ assert.match(reconcileMarkdownSurface, /!value\.startsWith\(state\.value\)[\s\S]*?invalidateSelection: diverged/, "retroactive todo/thinking rewrites must compare the whole previous surface value and explicitly invalidate selection");
176
176
  assert.match(reconcileMarkdownSurface, /if \(boundary > state\.stableText\.length\)/, "committed blocks must stay mounted for append-only updates");
177
177
  assert.match(reconcileMarkdownSurface, /for \(const node of state\.tailNodes\) node\.remove\(\)/, "only the mutable tail may be re-parsed per delta batch");
178
178
  });