@firstpick/pi-package-webui 0.9.5 → 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 (61) hide show
  1. package/README.md +5 -3
  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/package.json +1 -1
  6. package/public/app.js +1823 -262
  7. package/public/index.html +51 -31
  8. package/public/service-worker.js +4 -1
  9. package/public/stream-derived-output.mjs +799 -0
  10. package/public/stream-markdown-tail.mjs +204 -0
  11. package/public/stream-output-controller.mjs +193 -51
  12. package/public/stream-render-scheduler.mjs +85 -0
  13. package/public/styles.css +249 -18
  14. package/public/transcript-renderer.mjs +73 -24
  15. package/tests/boot-failure-diagnostics.test.mjs +3 -3
  16. package/tests/browser/composer-action-grid-reorder.spec.mjs +114 -31
  17. package/tests/browser/mobile-foundation.spec.mjs +28 -0
  18. package/tests/browser/stream-output-isolation.spec.mjs +308 -3
  19. package/tests/browser/user-noticeable-fixes.spec.mjs +204 -0
  20. package/tests/codex-fast-mode-static.test.mjs +2 -2
  21. package/tests/composer-action-grid-reorder-static.test.mjs +8 -7
  22. package/tests/control-visibility-static.test.mjs +3 -3
  23. package/tests/controls-layout-static.test.mjs +3 -3
  24. package/tests/cwd-footer-reliability-static.test.mjs +1 -1
  25. package/tests/fast-mode-client-static.test.mjs +1 -1
  26. package/tests/feature-decision-output-popup-static.test.mjs +2 -2
  27. package/tests/file-viewer-image-static.test.mjs +3 -3
  28. package/tests/fixtures/fake-pi.mjs +57 -10
  29. package/tests/fixtures/streaming-workloads.mjs +36 -0
  30. package/tests/footer-provider-usage-visual-static.test.mjs +15 -0
  31. package/tests/foreground-transcript-catch-up-static.test.mjs +7 -1
  32. package/tests/interaction-state-stability-static.test.mjs +3 -1
  33. package/tests/intercom-conversation-viewer-static.test.mjs +3 -3
  34. package/tests/intercom-conversations-http.test.mjs +16 -0
  35. package/tests/main-output-loading-static.test.mjs +3 -3
  36. package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
  37. package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
  38. package/tests/mobile-foundation-static.test.mjs +3 -3
  39. package/tests/mobile-phone-experience-static.test.mjs +1 -1
  40. package/tests/mobile-static.test.mjs +74 -63
  41. package/tests/open-issue-wizard-static.test.mjs +1 -1
  42. package/tests/persistent-ui-layout-static.test.mjs +4 -4
  43. package/tests/prompt-input-vertical-scroll-static.test.mjs +26 -0
  44. package/tests/questionnaire-dialog.test.mjs +3 -3
  45. package/tests/runtime-error-visibility.test.mjs +3 -3
  46. package/tests/session-auth-harness.test.mjs +26 -1
  47. package/tests/session-sampling-ui-static.test.mjs +3 -3
  48. package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
  49. package/tests/stream-derived-output.test.mjs +121 -0
  50. package/tests/stream-markdown-tail.test.mjs +218 -0
  51. package/tests/stream-output-controller.test.mjs +119 -4
  52. package/tests/stream-output-isolation-static.test.mjs +27 -6
  53. package/tests/stream-output-workloads.test.mjs +40 -3
  54. package/tests/stream-render-scheduler.test.mjs +115 -0
  55. package/tests/streaming-ui-coupling.test.mjs +1 -1
  56. package/tests/subagent-observability-static.test.mjs +2 -2
  57. package/tests/syntax-highlighting-static.test.mjs +3 -3
  58. package/tests/theme-customizer-static.test.mjs +1 -1
  59. package/tests/thinking-format-parser.test.mjs +15 -0
  60. package/tests/todo-filter-static.test.mjs +148 -0
  61. package/tests/transcript-markdown-tail.test.mjs +137 -0
@@ -0,0 +1,26 @@
1
+ import assert from "node:assert/strict";
2
+ import { readFile } from "node:fs/promises";
3
+ import { dirname, join } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ const root = join(dirname(fileURLToPath(import.meta.url)), "..");
7
+ const [app, css, html, serviceWorker] = await Promise.all([
8
+ readFile(join(root, "public", "app.js"), "utf8"),
9
+ readFile(join(root, "public", "styles.css"), "utf8"),
10
+ readFile(join(root, "public", "index.html"), "utf8"),
11
+ readFile(join(root, "public", "service-worker.js"), "utf8"),
12
+ ]);
13
+
14
+ const resizeStart = app.indexOf("function resizePromptInput()");
15
+ const resizeEnd = app.indexOf("\nfunction updateComposerModeButtons", resizeStart);
16
+ assert.ok(resizeStart >= 0 && resizeEnd > resizeStart, "resizePromptInput should remain inspectable");
17
+ const resizePromptInput = app.slice(resizeStart, resizeEnd);
18
+
19
+ assert.match(css, /#promptInput \{[\s\S]*max-height: min\(25vh, 10rem\);[\s\S]*overflow-y: auto;[\s\S]*overscroll-behavior-y: contain;/, "the prompt should retain bounded growth with native vertical scrolling as its CSS fallback");
20
+ assert.match(resizePromptInput, /input\.style\.height = "auto";[\s\S]*Math\.min\(input\.scrollHeight, maxHeight\)[\s\S]*input\.style\.overflowY = "auto";/, "every auto-resize pass should preserve vertical scrolling after the height cap is reached or growth stalls");
21
+ assert.doesNotMatch(resizePromptInput, /overflowY[^\n]+"hidden"/, "auto-resizing must not disable the prompt's vertical overflow fallback");
22
+ assert.match(html, /styles\.css\?v=132/, "the page should request the scroll-enabled stylesheet revision");
23
+ assert.match(html, /app\.js\?v=157/, "the page should request the scroll-enabled app revision");
24
+ assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v123"/, "the PWA cache identity should advance with the prompt scroll assets");
25
+
26
+ console.log("prompt-input-vertical-scroll-static.test.mjs passed");
@@ -504,8 +504,8 @@ assert.match(css, /@media \(max-width: 720px\), \(max-device-width: 720px\), \(p
504
504
  assert.match(css, /\.questionnaire-options button\s*\{\s*min-height:\s*3\.35rem/, "mobile questionnaire targets must exceed the 44px floor");
505
505
  assert.match(css, /@media \(prefers-reduced-motion: reduce\)\s*\{[\s\S]*?\*, \*::before, \*::after[\s\S]*?transition-duration:\s*1ms !important/, "global reduced-motion rules must cover questionnaire transitions");
506
506
 
507
- assert.match(html, /styles\.css\?v=129/, "questionnaire CSS needs the current stylesheet revision");
508
- assert.match(html, /app\.js\?v=150/, "questionnaire behavior needs the current app revision");
509
- assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v115"/, "questionnaire public assets need a coherent PWA cache revision");
507
+ assert.match(html, /styles\.css\?v=132/, "questionnaire CSS needs the current stylesheet revision");
508
+ assert.match(html, /app\.js\?v=157/, "questionnaire behavior needs the current app revision");
509
+ assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v123"/, "questionnaire public assets need a coherent PWA cache revision");
510
510
 
511
511
  console.log("questionnaire-dialog.test.mjs passed");
@@ -8,7 +8,7 @@ const app = await readFile(join(root, "public", "app.js"), "utf8");
8
8
 
9
9
  assert.match(
10
10
  app,
11
- /function surfaceRuntimeDiagnostic\(title, content, level = "error"\)[\s\S]*?addEvent\(message, level\);[\s\S]*?addTransientMessage\(\{ role: level === "error" \? "error" : "warn", title, content: message, level \}\);/,
11
+ /function surfaceRuntimeDiagnostic\(title, content, level = "error", \{ actions = null \} = \{\}\)[\s\S]*?addEvent\(message, level, \{ notify: false \}\);[\s\S]*?addTransientMessage\(\{ role: level === "error" \? "error" : "warn", title, content: message, level, \.\.\.\(actions \? \{ actions \} : \{\}\) \}\);/,
12
12
  "runtime diagnostics should be written to both the event log and the visible transcript",
13
13
  );
14
14
  assert.match(
@@ -23,7 +23,7 @@ assert.match(
23
23
  );
24
24
  assert.match(
25
25
  app,
26
- /case "message_end":[\s\S]*?event\.message\.stopReason === "error"[\s\S]*?surfaceRuntimeDiagnostic\("Assistant error", message\)[\s\S]*?assistantErrorSurfacedThisRun = true/,
26
+ /case "message_end":[\s\S]*?event\.message\.stopReason === "error"[\s\S]*?surfaceRuntimeDiagnostic\("Assistant error", describeAssistantFailure\(message\), "error", \{ actions: assistantFailureActions\([\s\S]*?assistantErrorSurfacedThisRun = true/,
27
27
  "final assistant errors should remain visible after transcript reconciliation",
28
28
  );
29
29
  assert.match(
@@ -33,7 +33,7 @@ assert.match(
33
33
  );
34
34
  assert.match(
35
35
  app,
36
- /case "agent_end":[\s\S]*?!assistantErrorSurfacedThisRun && event\.willRetry !== true[\s\S]*?assistantErrorFromAgentEnd\(event\)[\s\S]*?surfaceRuntimeDiagnostic\("Assistant error", message\)/,
36
+ /case "agent_end":[\s\S]*?!assistantErrorSurfacedThisRun && event\.willRetry !== true[\s\S]*?assistantErrorFromAgentEnd\(event\)[\s\S]*?surfaceRuntimeDiagnostic\("Assistant error", describeAssistantFailure\(message\)/,
37
37
  "agent_end should visibly surface a non-retrying provider error when message_end was absent",
38
38
  );
39
39
  assert.match(
@@ -1,6 +1,6 @@
1
1
  import assert from "node:assert/strict";
2
2
  import { existsSync } from "node:fs";
3
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import { mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
4
4
  import { tmpdir } from "node:os";
5
5
  import path from "node:path";
6
6
  import { SessionManager } from "@earendil-works/pi-coding-agent";
@@ -75,6 +75,31 @@ try {
75
75
  assert.equal(isSessionPathAllowed(path.join(tempDir, "..", "escape.jsonl"), [tempDir]), false);
76
76
  assert.equal(isSessionPathAllowed(outsidePath, []), true, "empty allowedDirs must mean no confinement");
77
77
 
78
+ // Symlinked session dir (e.g. ~/.pi -> dotfiles): a not-yet-created session
79
+ // file must still canonicalize through the symlink, otherwise every fresh
80
+ // session is rejected with "sessionPath must stay inside the Pi session
81
+ // directory" until its file first exists on disk.
82
+ const realSessionDir = await mkdtemp(path.join(tmpdir(), "pi-webui-real-sessions-"));
83
+ const symlinkParent = await mkdtemp(path.join(tmpdir(), "pi-webui-link-parent-"));
84
+ const symlinkedSessionDir = path.join(symlinkParent, "sessions-link");
85
+ await symlink(realSessionDir, symlinkedSessionDir, "dir");
86
+ const pendingSessionFile = path.join(symlinkedSessionDir, "not-created-yet.jsonl");
87
+ assert.equal(
88
+ isSessionPathAllowed(pendingSessionFile, [realSessionDir]),
89
+ true,
90
+ "non-existent session file under a symlinked session dir must be allowed",
91
+ );
92
+ assert.equal(
93
+ isSessionPathAllowed(path.join(realSessionDir, "not-created-yet.jsonl"), [symlinkedSessionDir]),
94
+ true,
95
+ "real path target must be allowed when the configured dir is the symlink",
96
+ );
97
+ assert.equal(
98
+ isSessionPathAllowed(path.join(symlinkedSessionDir, "..", "escape.jsonl"), [realSessionDir]),
99
+ false,
100
+ "traversal through a symlinked session dir must stay blocked",
101
+ );
102
+
78
103
  const blockedOutside = validateSessionDelete(outsidePath, {
79
104
  openSessionFiles: new Set(),
80
105
  currentSessionFile: undefined,
@@ -57,10 +57,10 @@ assert.match(css, /\.sampling-parameter-control\.unsupported:hover:not\(\.sampli
57
57
  assert.match(css, /\.sampling-parameter-control\.unsupported:focus-visible:not\(\.sampling-tooltip-dismissed\) > \.sampling-parameter-unsupported-tooltip/, "keyboard focus should reveal dismissible unsupported reasons");
58
58
  assert.match(css, /\.sampling-parameter-unsupported-tooltip\.sampling-tooltip-below/, "bounded placement should support flipping below top-edge rows");
59
59
  assert.match(css, /pointer-events: auto;/, "a visible unsupported tooltip should remain pointer-hoverable");
60
- assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v115"/, "native-control assets should advance the cache identity");
60
+ assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v123"/, "native-control assets should advance the cache identity");
61
61
  assert.match(serviceWorker, /"\/sampling-parameter-controls\.mjs"/, "the pure sampling module should be part of the offline app shell");
62
- assert.match(html, /styles\.css\?v=129/, "the changed stylesheet should have a new revision");
63
- assert.match(html, /app\.js\?v=150/, "the changed app module should have a new revision");
62
+ assert.match(html, /styles\.css\?v=132/, "the changed stylesheet should have a new revision");
63
+ assert.match(html, /app\.js\?v=157/, "the changed app module should have a new revision");
64
64
 
65
65
  // --- Browser state behavior -------------------------------------------------
66
66
 
@@ -27,8 +27,8 @@ assert.doesNotMatch(layout, /sectionHeightMin|sectionHeightMax|validateSectionHe
27
27
  assert.match(app, /const UI_LAYOUT_SIDE_PANEL_FIELDS = \["placement", "sectionLayout", "collapsedSectionIds", "hiddenSectionIds", "collapsedPanels", "panelWidths"\];/, "the browser durable layout field list should omit section heights");
28
28
  assert.match(app, /function clearRemovedSidePanelSectionHeightState\(\)[\s\S]*localStorage\.removeItem\(REMOVED_SIDE_PANEL_SECTION_HEIGHT_STORAGE_KEY\)[\s\S]*record\.field === "sidePanel" && record\.subfield === "sectionHeights"/, "startup should remove the retired cache and pending mutations");
29
29
  assert.match(app, /clearRemovedSidePanelSectionHeightState\(\);\s*restoreDurableUiLayoutPendingJournal\(\);/, "retired height state should be cleared before pending layout restoration");
30
- assert.match(html, /styles\.css\?v=129/, "removing section resize styles should advance the stylesheet revision");
31
- assert.match(html, /app\.js\?v=150/, "removing section resize behavior should advance the app revision");
32
- assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v115"/, "removing browser assets should advance the PWA cache identity");
30
+ assert.match(html, /styles\.css\?v=132/, "removing section resize styles should advance the stylesheet revision");
31
+ assert.match(html, /app\.js\?v=157/, "removing section resize behavior should advance the app revision");
32
+ assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v123"/, "removing browser assets should advance the PWA cache identity");
33
33
 
34
34
  console.log("side-panel-section-height-removal-static.test.mjs passed");
@@ -0,0 +1,121 @@
1
+ import assert from "node:assert/strict";
2
+ import {
3
+ createStreamDerivedOutputState,
4
+ deriveStreamOutputAuthoritative,
5
+ splitThinkingFormatTextAuthoritative,
6
+ stripTodoProgressLinesAuthoritative,
7
+ } from "../public/stream-derived-output.mjs";
8
+
9
+ function plain(value) {
10
+ return JSON.parse(JSON.stringify(value));
11
+ }
12
+
13
+ function assertCurrent(state, rawText, options, label) {
14
+ assert.deepEqual(
15
+ plain(state.value()),
16
+ plain(deriveStreamOutputAuthoritative(rawText, options)),
17
+ label,
18
+ );
19
+ }
20
+
21
+ function verifyEverySplit(rawText, options = {}) {
22
+ for (let split = 0; split <= rawText.length; split += 1) {
23
+ const state = createStreamDerivedOutputState({ ...options, shadow: true });
24
+ const first = rawText.slice(0, split);
25
+ const second = rawText.slice(split);
26
+ state.append(first);
27
+ assertCurrent(state, first, options, `prefix at code-unit split ${split}: ${JSON.stringify(rawText)}`);
28
+ state.append(second);
29
+ assertCurrent(state, rawText, options, `complete at code-unit split ${split}: ${JSON.stringify(rawText)}`);
30
+ assert.equal(state.takeDiagnostics().shadowMismatch, false, `shadow parse should agree at split ${split}`);
31
+ }
32
+
33
+ const state = createStreamDerivedOutputState({ ...options, shadow: true });
34
+ for (let index = 0; index < rawText.length; index += 1) {
35
+ state.append(rawText.slice(index, index + 1));
36
+ assertCurrent(state, rawText.slice(0, index + 1), options, `single-code-unit chunk ${index}: ${JSON.stringify(rawText)}`);
37
+ assert.equal(state.takeDiagnostics().shadowMismatch, false, `shadow parse should agree after code unit ${index}`);
38
+ }
39
+ }
40
+
41
+ assert.equal(
42
+ stripTodoProgressLinesAuthoritative("Goal: ship\r\n- [ ] first\r\n- [x] second\r\nAnswer", { streaming: true }),
43
+ "Goal: ship\nAnswer",
44
+ "authoritative todo filtering should retain current CRLF normalization",
45
+ );
46
+ assert.equal(
47
+ stripTodoProgressLinesAuthoritative("```md\n- [ ] literal\n```\n- [ ] transport\nAnswer", { streaming: true }),
48
+ "```md\n- [ ] literal\n```\nAnswer",
49
+ "todo-looking lines inside fences must stay visible",
50
+ );
51
+ assert.deepEqual(plain(splitThinkingFormatTextAuthoritative("<think>first</think><think>second</think>\nanswer", { streaming: true })), {
52
+ hasThinkingFormat: true,
53
+ thinkingText: "first\n\nsecond",
54
+ finalText: "answer",
55
+ complete: true,
56
+ });
57
+
58
+ const representativeCases = [
59
+ "Plain assistant output with no derived markers.",
60
+ "Goal: preserve semantics\r\n- [ ] pending\r\n1. [x] complete\r\nFinal answer 😀",
61
+ "Before\n\n\n```md\r\n- [ ] literal checklist\r\n```\n\n\nAfter",
62
+ "- [",
63
+ "- [not a todo after all",
64
+ "<th",
65
+ "<think>reasoning</thi",
66
+ "<think>reasoning</think>\r\nfinal answer",
67
+ "<think data-kind=\"analysis\">reasoning</think >\nfinal",
68
+ "<think>The user mentioned `<think>example</think>` inside the reasoning.</think>\nfinal answer",
69
+ "<think>first</think><think>second</think>\nanswer",
70
+ "<think>first</think> \r\n<think>second</think>\r\nanswer",
71
+ "<think>first</think><th",
72
+ "<|analysis>channel reasoning<analysis|>\nanswer",
73
+ "<|analysis>one<analysis|><|reflection>two<reflection|>answer",
74
+ "<|Analysis>Unicode 😀 e\u0301 漢字<ANALYSIS|>\r\nfinal 🚀",
75
+ "<think>nested <think>inner</think> tail</think>\nanswer",
76
+ "<think>partial close with spaces</thi ",
77
+ "<think>todo in thought\n- [ ] hidden</think>\n- [x] transport\nvisible",
78
+ ];
79
+
80
+ for (const rawText of representativeCases) verifyEverySplit(rawText, { todoProgressDetected: true });
81
+ for (const rawText of representativeCases.slice(0, 6)) verifyEverySplit(rawText, { todoProgressDetected: false });
82
+
83
+ const suffixState = createStreamDerivedOutputState({ todoProgressDetected: true, shadow: false });
84
+ const longPrefix = "x".repeat(100_000);
85
+ suffixState.append(longPrefix);
86
+ assert.deepEqual(suffixState.takeDiagnostics(), {
87
+ scannedCodeUnits: longPrefix.length,
88
+ fallbackReason: "",
89
+ shadowMismatch: false,
90
+ });
91
+ suffixState.append("y");
92
+ assert.deepEqual(suffixState.takeDiagnostics(), {
93
+ scannedCodeUnits: 1,
94
+ fallbackReason: "",
95
+ shadowMismatch: false,
96
+ }, "a normal append should report only the newly scanned suffix");
97
+
98
+ suffixState.replace(`${longPrefix}snapshot`, { reason: "authoritative-snapshot" });
99
+ assert.deepEqual(suffixState.takeDiagnostics(), {
100
+ scannedCodeUnits: longPrefix.length + "snapshot".length,
101
+ fallbackReason: "authoritative-snapshot",
102
+ shadowMismatch: false,
103
+ }, "snapshot replacement should expose its full authoritative fallback scan");
104
+
105
+ const divergenceState = createStreamDerivedOutputState({ shadow: true });
106
+ divergenceState.append("- ");
107
+ divergenceState.takeDiagnostics();
108
+ divergenceState.append("[");
109
+ const divergenceDiagnostic = divergenceState.takeDiagnostics();
110
+ assert.equal(divergenceDiagnostic.fallbackReason, "assistant-divergence", "retroactive partial-todo hiding should use an observable conservative fallback");
111
+ assert.equal(divergenceDiagnostic.shadowMismatch, false);
112
+ assertCurrent(divergenceState, "- [", {}, "fallback output should remain authoritative");
113
+
114
+ const modeState = createStreamDerivedOutputState({ todoProgressDetected: false });
115
+ modeState.append("- [ ] visible while todo transport is absent");
116
+ modeState.takeDiagnostics();
117
+ modeState.setTodoProgressDetected(true);
118
+ assert.equal(modeState.value().finalText, "");
119
+ assert.equal(modeState.takeDiagnostics().fallbackReason, "todo-mode-change", "todo capability changes should force an observable rebuild");
120
+
121
+ console.log("stream-derived-output.test.mjs passed");
@@ -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");