@firstpick/pi-package-webui 0.9.4 → 0.9.6

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 (70) hide show
  1. package/README.md +7 -2
  2. package/bin/pi-webui.mjs +90 -5
  3. package/lib/optional-feature-catalog.mjs +1 -1
  4. package/lib/session-actions.mjs +20 -2
  5. package/lib/ui-layout-settings.mjs +37 -6
  6. package/package.json +1 -1
  7. package/public/app.js +2311 -257
  8. package/public/index.html +82 -55
  9. package/public/service-worker.js +4 -1
  10. package/public/stream-derived-output.mjs +799 -0
  11. package/public/stream-markdown-tail.mjs +204 -0
  12. package/public/stream-output-controller.mjs +222 -51
  13. package/public/stream-render-scheduler.mjs +85 -0
  14. package/public/styles.css +276 -20
  15. package/public/transcript-renderer.mjs +73 -24
  16. package/tests/boot-failure-diagnostics.test.mjs +3 -3
  17. package/tests/browser/composer-action-grid-reorder.spec.mjs +114 -31
  18. package/tests/browser/control-deck-side-panels.spec.mjs +42 -0
  19. package/tests/browser/control-visibility.spec.mjs +396 -0
  20. package/tests/browser/mobile-foundation.spec.mjs +28 -0
  21. package/tests/browser/persistent-ui-layout.spec.mjs +9 -9
  22. package/tests/browser/stream-output-isolation.spec.mjs +308 -3
  23. package/tests/browser/user-noticeable-fixes.spec.mjs +204 -0
  24. package/tests/codex-fast-mode-static.test.mjs +2 -2
  25. package/tests/composer-action-grid-reorder-static.test.mjs +8 -7
  26. package/tests/control-deck-side-panels-static.test.mjs +3 -1
  27. package/tests/control-visibility-static.test.mjs +294 -0
  28. package/tests/controls-layout-static.test.mjs +3 -3
  29. package/tests/cwd-footer-reliability-static.test.mjs +1 -1
  30. package/tests/fast-mode-client-static.test.mjs +1 -1
  31. package/tests/feature-decision-output-popup-static.test.mjs +2 -2
  32. package/tests/file-viewer-image-static.test.mjs +3 -3
  33. package/tests/fixtures/fake-pi.mjs +57 -10
  34. package/tests/fixtures/streaming-workloads.mjs +167 -0
  35. package/tests/footer-provider-usage-visual-static.test.mjs +15 -0
  36. package/tests/foreground-transcript-catch-up-static.test.mjs +7 -1
  37. package/tests/git-workflow-preferences.test.mjs +2 -2
  38. package/tests/http-endpoints-harness.test.mjs +4 -4
  39. package/tests/interaction-state-stability-static.test.mjs +3 -1
  40. package/tests/intercom-conversation-viewer-static.test.mjs +4 -4
  41. package/tests/intercom-conversations-http.test.mjs +16 -0
  42. package/tests/main-output-loading-static.test.mjs +3 -3
  43. package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
  44. package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
  45. package/tests/mobile-foundation-static.test.mjs +3 -3
  46. package/tests/mobile-phone-experience-static.test.mjs +1 -1
  47. package/tests/mobile-static.test.mjs +77 -66
  48. package/tests/open-issue-wizard-static.test.mjs +2 -2
  49. package/tests/persistent-ui-layout-static.test.mjs +7 -7
  50. package/tests/prompt-input-vertical-scroll-static.test.mjs +26 -0
  51. package/tests/questionnaire-dialog.test.mjs +3 -3
  52. package/tests/runtime-error-visibility.test.mjs +3 -3
  53. package/tests/session-auth-harness.test.mjs +26 -1
  54. package/tests/session-sampling-ui-static.test.mjs +3 -3
  55. package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
  56. package/tests/stream-derived-output.test.mjs +121 -0
  57. package/tests/stream-markdown-tail.test.mjs +218 -0
  58. package/tests/stream-output-controller.test.mjs +119 -4
  59. package/tests/stream-output-isolation-static.test.mjs +34 -6
  60. package/tests/stream-output-workloads.test.mjs +276 -0
  61. package/tests/stream-render-scheduler.test.mjs +115 -0
  62. package/tests/streaming-ui-coupling.test.mjs +1 -1
  63. package/tests/subagent-observability-static.test.mjs +2 -2
  64. package/tests/syntax-highlighting-static.test.mjs +3 -3
  65. package/tests/theme-customizer-static.test.mjs +1 -1
  66. package/tests/thinking-format-parser.test.mjs +15 -0
  67. package/tests/todo-filter-static.test.mjs +148 -0
  68. package/tests/transcript-markdown-tail.test.mjs +137 -0
  69. package/tests/ui-layout-settings.test.mjs +54 -7
  70. package/tests/webui-settings-locking.test.mjs +3 -3
@@ -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,23 +79,39 @@ 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");
86
107
  assert.match(app, /reconcileUnknownTranscriptEvidenceAtBarrier\(event\)/, "unknown evidence should request authoritative reconciliation only at a semantic barrier");
87
108
  assert.match(app, /streamIsolationDebug[\s\S]*?__piStreamIsolationDebug/, "the opt-in test diagnostic ledger should be exposed only behind the explicit debug flag");
109
+ assert.match(controller, /receivedAt: diagnosticsEnabled \? now\(\) : undefined/, "receipt timing must be disabled with diagnostics");
110
+ assert.match(controller, /maxAgeMs[\s\S]*?drainMs/, "diagnostic batches should report queue age and drain duration");
111
+ assert.match(app, /version: 2,[\s\S]*?batchPaintMaxMs[\s\S]*?transcriptNodeMax[\s\S]*?currentMessageNodeMax/, "ledger v2 should expose paint and DOM-node high-water counters");
112
+ assert.match(app, /function scheduleStreamPaintDiagnostic[\s\S]*?requestAnimationFrame[\s\S]*?type: "paint-opportunity"/, "paint opportunity timing should run only from the opt-in diagnostic path");
113
+ assert.match(app, /function streamDiagnosticBytes[\s\S]*?length \* 2/, "stream byte metrics should use conservative UTF-16 accounting");
114
+ assert.match(app, /new PerformanceObserver[\s\S]*?type: "longtask"[\s\S]*?type: "long-animation-frame"/, "supported long-task and long-frame entries should feed the ledger");
115
+ assert.match(app, /type: "markdown-commit"[\s\S]*?tailBytes[\s\S]*?tailKind/, "mutable Markdown tail diagnostics should include size and block kind");
88
116
 
89
117
  console.log("stream-output-isolation-static.test.mjs passed");
@@ -0,0 +1,276 @@
1
+ import assert from "node:assert/strict";
2
+ import {
3
+ DEFAULT_STREAM_PENDING_BYTE_LIMIT,
4
+ DEFAULT_STREAM_PENDING_ENTRY_LIMIT,
5
+ createStreamOutputController,
6
+ } from "../public/stream-output-controller.mjs";
7
+ import {
8
+ backgroundForegroundReconciliationScenario,
9
+ concatenatedDeltaText,
10
+ longParagraphDeltaEvents,
11
+ longTranscriptActiveStreamScenario,
12
+ messageUpdateEvent,
13
+ mixedSemanticBurstEvents,
14
+ openFenceDeltaEvents,
15
+ overflowBurstEvents,
16
+ smallTextDeltaEvents,
17
+ thinkingStreamEvents,
18
+ } from "./fixtures/streaming-workloads.mjs";
19
+
20
+ class FakeFrames {
21
+ next = 1;
22
+ callbacks = new Map();
23
+
24
+ schedule = (callback) => {
25
+ const handle = this.next++;
26
+ this.callbacks.set(handle, callback);
27
+ return handle;
28
+ };
29
+
30
+ cancel = (handle) => {
31
+ this.callbacks.delete(handle);
32
+ };
33
+
34
+ runAll() {
35
+ while (this.callbacks.size) {
36
+ const [handle, callback] = this.callbacks.entries().next().value;
37
+ this.callbacks.delete(handle);
38
+ callback();
39
+ }
40
+ }
41
+ }
42
+
43
+ function harness({ diagnostics = true, nowStepMs = 1 } = {}) {
44
+ const frames = new FakeFrames();
45
+ const records = [];
46
+ const applied = { text: "", thinking: "", toolCall: "", toolExecution: [], errors: 0, order: [] };
47
+ let clock = 0;
48
+ let nowCalls = 0;
49
+ const controller = createStreamOutputController({
50
+ scheduleFrame: frames.schedule,
51
+ cancelFrame: frames.cancel,
52
+ now: () => {
53
+ nowCalls += 1;
54
+ clock += nowStepMs;
55
+ return clock;
56
+ },
57
+ applyTextUpdate: (event) => {
58
+ applied.text += event.assistantMessageEvent.delta || "";
59
+ applied.order.push(`text:${event.assistantMessageEvent.type}`);
60
+ },
61
+ applyThinkingUpdate: (event) => {
62
+ applied.thinking += event.assistantMessageEvent.delta || "";
63
+ applied.order.push(`thinking:${event.assistantMessageEvent.type}`);
64
+ },
65
+ applyToolCallUpdate: (event) => {
66
+ applied.toolCall += event.assistantMessageEvent.delta || "";
67
+ applied.order.push(`tool-call:${event.assistantMessageEvent.type}`);
68
+ },
69
+ applyToolExecutionUpdate: (event) => {
70
+ applied.toolExecution.push(event.partialResult?.content || "");
71
+ applied.order.push("tool-execution");
72
+ },
73
+ applyStreamError: () => {
74
+ applied.errors += 1;
75
+ applied.order.push("stream-error");
76
+ },
77
+ onDiagnostic: diagnostics ? (record) => records.push(record) : undefined,
78
+ });
79
+ return {
80
+ frames,
81
+ records,
82
+ applied,
83
+ controller,
84
+ nowCallCount: () => nowCalls,
85
+ dispatchAll(events) {
86
+ for (const event of events) controller.dispatch(event);
87
+ },
88
+ };
89
+ }
90
+
91
+ // 1) Small-delta burst: lossless coalescing collapses transport events into few batches.
92
+ {
93
+ const run = harness();
94
+ const events = smallTextDeltaEvents(1000);
95
+ run.dispatchAll(events);
96
+ run.frames.runAll();
97
+ assert.equal(run.applied.text, concatenatedDeltaText(events), "coalesced text must equal exact concatenation");
98
+ const batches = run.records.filter((record) => record.type === "batch");
99
+ const receipts = run.records.filter((record) => record.type === "receipt");
100
+ assert.equal(receipts.length, 1000, "every transport event must be received");
101
+ assert.ok(batches.length <= 5, `render batches (${batches.length}) must stay far below transport events`);
102
+ const totalSources = batches.reduce((total, batch) => total + batch.sourceCount, 0);
103
+ assert.equal(totalSources, 1000, "batch sourceCounts must account for every source event");
104
+ }
105
+
106
+ // 2) Long unbroken paragraph (1 MB): byte-limit overflow drains stay lossless and bounded.
107
+ {
108
+ const run = harness();
109
+ const events = longParagraphDeltaEvents(1024 * 1024, 512);
110
+ run.dispatchAll(events);
111
+ run.frames.runAll();
112
+ run.controller.flush();
113
+ assert.equal(run.applied.text.length, 1024 * 1024, "no paragraph bytes may be lost");
114
+ assert.equal(run.applied.text, concatenatedDeltaText(events), "paragraph must survive overflow drains byte-for-byte");
115
+ const overflow = run.records.filter((record) => record.type === "overflow");
116
+ assert.ok(overflow.length > 0, "1 MB through a 256 KB queue must report overflow");
117
+ for (const record of run.records.filter((entry) => entry.type === "queued")) {
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");
120
+ }
121
+ }
122
+
123
+ // 3) Open code fences: under-limit and over-limit fixtures both stream losslessly.
124
+ for (const codeBytes of [40 * 1024, 100 * 1024]) {
125
+ const run = harness();
126
+ const events = openFenceDeltaEvents(codeBytes);
127
+ run.dispatchAll(events);
128
+ run.frames.runAll();
129
+ run.controller.flush();
130
+ const expected = concatenatedDeltaText(events);
131
+ assert.equal(run.applied.text, expected, `open fence (${codeBytes} bytes) must stream losslessly`);
132
+ assert.ok(expected.startsWith("```js\n"), "fixture must open a fence");
133
+ assert.ok(!/```\s*$/.test(expected), "fixture fence must remain open");
134
+ }
135
+
136
+ // 4) Thinking stream: start/end barriers flush synchronously and text is exact.
137
+ {
138
+ const run = harness();
139
+ const events = thinkingStreamEvents(500);
140
+ run.dispatchAll(events);
141
+ assert.equal(run.controller.pendingCount(), 0, "thinking_end barrier must leave nothing queued");
142
+ assert.equal(run.applied.thinking, concatenatedDeltaText(events, "thinking_delta"), "thinking text must be exact");
143
+ const barriers = run.records.filter((record) => record.type === "barrier");
144
+ assert.ok(barriers.length >= 1, "the thinking_end barrier flush must be diagnosed");
145
+ }
146
+
147
+ // 5) Mixed semantic burst: cross-kind order is preserved exactly.
148
+ {
149
+ const run = harness();
150
+ run.dispatchAll(mixedSemanticBurstEvents());
151
+ run.frames.runAll();
152
+ run.controller.flush();
153
+ assert.deepEqual(run.applied.order, [
154
+ "thinking:thinking_start",
155
+ "thinking:thinking_delta",
156
+ "thinking:thinking_end",
157
+ "text:text_start",
158
+ "text:text_delta",
159
+ "tool-call:toolcall_start",
160
+ "tool-call:toolcall_delta",
161
+ "tool-call:toolcall_end",
162
+ "tool-execution",
163
+ "text:text_delta",
164
+ "stream-error",
165
+ "text:text_end",
166
+ ], "semantic order must match server order with only adjacent same-kind coalescing");
167
+ assert.equal(run.applied.thinking, "planning steps");
168
+ assert.equal(run.applied.text, "Running the command now. Done.");
169
+ assert.equal(run.applied.toolCall, '{"cmd":"ls"}');
170
+ assert.deepEqual(run.applied.toolExecution, ["file-a\nfile-b\n"], "superseding partial tool results must coalesce to the latest");
171
+ assert.equal(run.applied.errors, 1);
172
+ }
173
+
174
+ // 6) Overflow burst: alternating kinds defeat coalescing, hit the entry limit, stay ordered.
175
+ {
176
+ const run = harness();
177
+ const events = overflowBurstEvents({ pairs: 200 });
178
+ run.dispatchAll(events);
179
+ run.frames.runAll();
180
+ run.controller.flush();
181
+ assert.equal(run.applied.text, concatenatedDeltaText(events, "text_delta"));
182
+ assert.equal(run.applied.thinking, concatenatedDeltaText(events, "thinking_delta"));
183
+ const entryOverflows = run.records.filter((record) => record.type === "overflow" && record.reason === "entry-limit");
184
+ assert.ok(entryOverflows.length > 0, "400 non-coalescible events must hit the 128-entry limit");
185
+ for (const record of run.records.filter((entry) => entry.type === "queued")) {
186
+ assert.ok(record.pendingCount <= DEFAULT_STREAM_PENDING_ENTRY_LIMIT + 1);
187
+ }
188
+ // Alternation must be preserved within each flushed window: text i before thinking i.
189
+ const textIndex = run.applied.order.filter((kind) => kind.startsWith("text") || kind.startsWith("thinking"));
190
+ assert.equal(textIndex[0], "text:text_delta", "first applied event must be the first dispatched kind");
191
+ }
192
+
193
+ // 7) Batch latency and drain duration are measured with the injected clock.
194
+ {
195
+ const run = harness({ nowStepMs: 5 });
196
+ run.controller.dispatch(messageUpdateEvent("text_delta", "a"));
197
+ run.controller.dispatch(messageUpdateEvent("text_delta", "b"));
198
+ run.frames.runAll();
199
+ const batch = run.records.find((record) => record.type === "batch");
200
+ assert.ok(batch, "a batch record must exist");
201
+ assert.equal(typeof batch.maxAgeMs, "number", "batch latency must be reported");
202
+ assert.equal(typeof batch.drainMs, "number", "drain duration must be reported");
203
+ assert.ok(batch.maxAgeMs > 0, "coalesced batches must report the oldest entry's age");
204
+ assert.ok(batch.drainMs > 0, "drain duration must be measured across application");
205
+ }
206
+
207
+ // 8) Coalesced entries keep the oldest receipt time so latency is not hidden by merging.
208
+ {
209
+ const run = harness({ nowStepMs: 10 });
210
+ run.controller.dispatch(messageUpdateEvent("text_delta", "first"));
211
+ run.controller.dispatch(messageUpdateEvent("text_delta", "second"));
212
+ run.controller.dispatch(messageUpdateEvent("text_delta", "third"));
213
+ run.frames.runAll();
214
+ const batch = run.records.find((record) => record.type === "batch");
215
+ // Clock: dispatch receipts at 10, 20, 30; drain start at 40 → oldest age 30.
216
+ assert.equal(batch.maxAgeMs, 30, "merged entry must retain the first source's receipt time");
217
+ }
218
+
219
+ // 9) Disabled diagnostics never touch the clock: the hot path is measurement-free.
220
+ {
221
+ const run = harness({ diagnostics: false });
222
+ run.dispatchAll(smallTextDeltaEvents(100));
223
+ run.frames.runAll();
224
+ run.controller.flush();
225
+ assert.equal(run.nowCallCount(), 0, "the disabled hot path must never call now()");
226
+ assert.equal(run.applied.text, concatenatedDeltaText(smallTextDeltaEvents(100)));
227
+ }
228
+
229
+ // 10) Oversize direct application also reports latency fields.
230
+ {
231
+ const run = harness({ nowStepMs: 2 });
232
+ const oversize = messageUpdateEvent("text_delta", "x".repeat(DEFAULT_STREAM_PENDING_BYTE_LIMIT));
233
+ run.controller.dispatch(oversize);
234
+ const direct = run.records.find((record) => record.type === "batch" && record.direct === true);
235
+ assert.ok(direct, "an oversize event must be applied directly");
236
+ assert.equal(typeof direct.maxAgeMs, "number");
237
+ assert.equal(typeof direct.drainMs, "number");
238
+ assert.equal(run.applied.text.length, DEFAULT_STREAM_PENDING_BYTE_LIMIT, "oversize events are applied, never dropped");
239
+ }
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
+
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
  });