@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.
- package/README.md +5 -3
- package/bin/pi-webui.mjs +90 -5
- package/lib/optional-feature-catalog.mjs +1 -1
- package/lib/session-actions.mjs +20 -2
- package/package.json +1 -1
- package/public/app.js +1823 -262
- package/public/index.html +51 -31
- package/public/service-worker.js +4 -1
- package/public/stream-derived-output.mjs +799 -0
- package/public/stream-markdown-tail.mjs +204 -0
- package/public/stream-output-controller.mjs +193 -51
- package/public/stream-render-scheduler.mjs +85 -0
- package/public/styles.css +249 -18
- package/public/transcript-renderer.mjs +73 -24
- package/tests/boot-failure-diagnostics.test.mjs +3 -3
- package/tests/browser/composer-action-grid-reorder.spec.mjs +114 -31
- package/tests/browser/mobile-foundation.spec.mjs +28 -0
- package/tests/browser/stream-output-isolation.spec.mjs +308 -3
- package/tests/browser/user-noticeable-fixes.spec.mjs +204 -0
- package/tests/codex-fast-mode-static.test.mjs +2 -2
- package/tests/composer-action-grid-reorder-static.test.mjs +8 -7
- package/tests/control-visibility-static.test.mjs +3 -3
- package/tests/controls-layout-static.test.mjs +3 -3
- package/tests/cwd-footer-reliability-static.test.mjs +1 -1
- package/tests/fast-mode-client-static.test.mjs +1 -1
- package/tests/feature-decision-output-popup-static.test.mjs +2 -2
- package/tests/file-viewer-image-static.test.mjs +3 -3
- package/tests/fixtures/fake-pi.mjs +57 -10
- package/tests/fixtures/streaming-workloads.mjs +36 -0
- package/tests/footer-provider-usage-visual-static.test.mjs +15 -0
- package/tests/foreground-transcript-catch-up-static.test.mjs +7 -1
- package/tests/interaction-state-stability-static.test.mjs +3 -1
- package/tests/intercom-conversation-viewer-static.test.mjs +3 -3
- package/tests/intercom-conversations-http.test.mjs +16 -0
- package/tests/main-output-loading-static.test.mjs +3 -3
- package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
- package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
- package/tests/mobile-foundation-static.test.mjs +3 -3
- package/tests/mobile-phone-experience-static.test.mjs +1 -1
- package/tests/mobile-static.test.mjs +74 -63
- package/tests/open-issue-wizard-static.test.mjs +1 -1
- package/tests/persistent-ui-layout-static.test.mjs +4 -4
- package/tests/prompt-input-vertical-scroll-static.test.mjs +26 -0
- package/tests/questionnaire-dialog.test.mjs +3 -3
- package/tests/runtime-error-visibility.test.mjs +3 -3
- package/tests/session-auth-harness.test.mjs +26 -1
- package/tests/session-sampling-ui-static.test.mjs +3 -3
- package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
- package/tests/stream-derived-output.test.mjs +121 -0
- package/tests/stream-markdown-tail.test.mjs +218 -0
- package/tests/stream-output-controller.test.mjs +119 -4
- package/tests/stream-output-isolation-static.test.mjs +27 -6
- package/tests/stream-output-workloads.test.mjs +40 -3
- package/tests/stream-render-scheduler.test.mjs +115 -0
- package/tests/streaming-ui-coupling.test.mjs +1 -1
- package/tests/subagent-observability-static.test.mjs +2 -2
- package/tests/syntax-highlighting-static.test.mjs +3 -3
- package/tests/theme-customizer-static.test.mjs +1 -1
- package/tests/thinking-format-parser.test.mjs +15 -0
- package/tests/todo-filter-static.test.mjs +148 -0
- package/tests/transcript-markdown-tail.test.mjs +137 -0
|
@@ -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
|
|
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, /
|
|
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
|
|
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\)
|
|
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("
|
|
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, "
|
|
117
|
-
assert.ok(record.pendingCount <= DEFAULT_STREAM_PENDING_ENTRY_LIMIT, "
|
|
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\.
|
|
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
|
});
|
|
@@ -28,7 +28,7 @@ assert.match(app, /const SUBAGENT_ASYNC_WIDGET_KEY = "subagent-async"[\s\S]*case
|
|
|
28
28
|
assert.match(app, /for \(const \[key, value\] of widgets\)[\s\S]*if \(key === SUBAGENT_ASYNC_WIDGET_KEY\) continue;/, "stale cached async-status widgets should remain hidden defensively");
|
|
29
29
|
assert.match(css, /\.subagent-source-badge[\s\S]*text-overflow: ellipsis[\s\S]*@media \(max-width: 720px\)/, "source badges should remain compact and responsive");
|
|
30
30
|
assert.match(html, /Managed and registered Pi agent runs[\s\S]*External agents/, "help should explain managed, registered, and external groups");
|
|
31
|
-
assert.match(html, /styles\.css\?v=
|
|
32
|
-
assert.match(worker, /pi-webui-pwa-
|
|
31
|
+
assert.match(html, /styles\.css\?v=132[\s\S]*app\.js\?v=157/, "browser assets should use the current subagent-observability revisions");
|
|
32
|
+
assert.match(worker, /pi-webui-pwa-v123/, "PWA cache identity should advance with subagent widget suppression");
|
|
33
33
|
|
|
34
34
|
console.log("subagent-observability-static.test.mjs passed");
|
|
@@ -60,9 +60,9 @@ assert.match(css, /\.markdown-code \.syntax-comment \{[^}]*font-style: italic;/,
|
|
|
60
60
|
|
|
61
61
|
// --- Offline/PWA startup closure and coherent revisions ---
|
|
62
62
|
assert.match(serviceWorker, /"\/syntax-highlight\.mjs",/, "the eagerly imported tokenizer must be part of the offline app shell");
|
|
63
|
-
assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-
|
|
64
|
-
assert.match(html, /styles\.css\?v=
|
|
65
|
-
assert.match(html, /data-app-src="\/app\.js\?v=
|
|
63
|
+
assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v123"/, "adding a startup module should advance the PWA cache identity");
|
|
64
|
+
assert.match(html, /styles\.css\?v=132/, "new token styles should advance the stylesheet revision");
|
|
65
|
+
assert.match(html, /data-app-src="\/app\.js\?v=157"/, "new renderer wiring should advance the app module revision");
|
|
66
66
|
assert.match(pkg, /node --check public\/syntax-highlight\.mjs/, "the package check should syntax-check the startup-critical tokenizer");
|
|
67
67
|
|
|
68
68
|
const appShell = serviceWorker.slice(serviceWorker.indexOf("const APP_SHELL"), serviceWorker.indexOf("];", serviceWorker.indexOf("const APP_SHELL")));
|
|
@@ -34,7 +34,7 @@ assert.match(html, /role="alert" hidden>[\s\S]*Replace this exact target\?/, "ov
|
|
|
34
34
|
|
|
35
35
|
assert.match(app, /from "\.\/theme-contract\.mjs"/, "frontend should consume the shared browser-safe theme contract");
|
|
36
36
|
assert.match(serviceWorker, /"\/theme-contract\.mjs"/, "the PWA app shell should cache the startup-critical theme contract");
|
|
37
|
-
assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-
|
|
37
|
+
assert.match(serviceWorker, /const CACHE_NAME = "pi-webui-pwa-v123"/, "adding the theme contract to the startup graph should advance the PWA cache identity");
|
|
38
38
|
assert.match(pkg, /node --check public\/theme-contract\.mjs/, "the package check should syntax-check the startup-critical theme contract");
|
|
39
39
|
assert.match(app, /function themeDraftFromCatalog\(theme, name = theme\?\.name\)[\s\S]*colors: cloneThemeDraft\(theme\?\.colors \|\| \{\}\)/, "catalog metadata should be projected into a schema-only editable draft");
|
|
40
40
|
assert.match(app, /for \(const \{ name, label \} of group\.tokens\)/, "visual controls should render from the exact shared token inventory");
|
|
@@ -3,6 +3,7 @@ import { readFile } from "node:fs/promises";
|
|
|
3
3
|
import vm from "node:vm";
|
|
4
4
|
import { dirname, join } from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { splitThinkingFormatTextAuthoritative } from "../public/stream-derived-output.mjs";
|
|
6
7
|
|
|
7
8
|
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
8
9
|
const app = await readFile(join(root, "public", "app.js"), "utf8");
|
|
@@ -61,4 +62,18 @@ assert.deepEqual(parse("<|analysis>channel reasoning<analysis|>\nanswer"), {
|
|
|
61
62
|
complete: true,
|
|
62
63
|
}, "channel-style thinking delimiters should remain unchanged");
|
|
63
64
|
|
|
65
|
+
for (const [text, options] of [
|
|
66
|
+
["<th", { streaming: true }],
|
|
67
|
+
["<think>reasoning</thi", { streaming: true }],
|
|
68
|
+
["<think>first</think><th", { streaming: true }],
|
|
69
|
+
["<think>first</think><think>second</think>\r\nanswer", { streaming: true }],
|
|
70
|
+
["<|Analysis>Unicode 😀<ANALYSIS|>\nanswer", { streaming: true }],
|
|
71
|
+
]) {
|
|
72
|
+
assert.deepEqual(
|
|
73
|
+
parse(text, options),
|
|
74
|
+
JSON.parse(JSON.stringify(splitThinkingFormatTextAuthoritative(text, options))),
|
|
75
|
+
`stream-derived authoritative parsing should stay aligned for ${JSON.stringify(text)}`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
64
79
|
console.log("thinking-format-parser.test.mjs passed");
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import vm from "node:vm";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { stripTodoProgressLinesAuthoritative } from "../public/stream-derived-output.mjs";
|
|
7
|
+
|
|
8
|
+
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
9
|
+
const app = await readFile(join(root, "public", "app.js"), "utf8");
|
|
10
|
+
|
|
11
|
+
// Extract the contiguous region containing textLines and stripTodoProgressLines.
|
|
12
|
+
// textLines is a shared helper; stripTodoProgressLines references it and the
|
|
13
|
+
// todo-progress regex constants. The region ends at the next function boundary.
|
|
14
|
+
const textLinesStart = app.indexOf("\nfunction textLines(");
|
|
15
|
+
assert.ok(textLinesStart >= 0, "textLines helper should exist in app.js");
|
|
16
|
+
const nextFnAfterStrip = app.indexOf("\nfunction parseTodoProgressItemLine(", textLinesStart);
|
|
17
|
+
assert.ok(nextFnAfterStrip > textLinesStart, "parseTodoProgressItemLine should follow stripTodoProgressLines");
|
|
18
|
+
const todoFilterRegion = app.slice(textLinesStart, nextFnAfterStrip);
|
|
19
|
+
|
|
20
|
+
// Also need the regex constants from the module-level scope.
|
|
21
|
+
assert.ok(app.includes("const TODO_PROGRESS_LINE_REGEX = "), "TODO_PROGRESS_LINE_REGEX should be defined");
|
|
22
|
+
assert.ok(app.includes("const TODO_PROGRESS_PARTIAL_LINE_REGEX = "), "TODO_PROGRESS_PARTIAL_LINE_REGEX should be defined");
|
|
23
|
+
|
|
24
|
+
// VM context: provide the regex constants and stub isOptionalFeatureDetected.
|
|
25
|
+
const context = {
|
|
26
|
+
TODO_PROGRESS_LINE_REGEX: /^\s*(?:(?:[-*]|\d+[.)])\s*)?\[(?: |x|X|-)\]\s+.+$/,
|
|
27
|
+
TODO_PROGRESS_PARTIAL_LINE_REGEX: /^\s*(?:(?:[-*]|\d+[.)])\s*)?\[(?: |x|X|-)?\]?\s*.*$/,
|
|
28
|
+
isOptionalFeatureDetected: () => true,
|
|
29
|
+
};
|
|
30
|
+
vm.runInNewContext(
|
|
31
|
+
`${todoFilterRegion}\nthis.stripTodoProgressLines = stripTodoProgressLines;`,
|
|
32
|
+
context,
|
|
33
|
+
{ filename: "todo-filter-helpers.js" },
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
function appStrip(text, options) {
|
|
37
|
+
return context.stripTodoProgressLines(text, options);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Representative cases for todo-progress line filtering equivalence:
|
|
42
|
+
* - todo lines with all markers: [ ], [x], [X], [-]
|
|
43
|
+
* - ordered/unordered/numeric prefixes: * [ ], - [ ], 1. [ ]
|
|
44
|
+
* - partial todo tails with and without trailing newline
|
|
45
|
+
* - fenced code containing todo-like lines
|
|
46
|
+
* - CRLF normalization
|
|
47
|
+
* - blank-line collapsing
|
|
48
|
+
* - Unicode
|
|
49
|
+
* - feature-disabled passthrough
|
|
50
|
+
*/
|
|
51
|
+
const representativeCases = [
|
|
52
|
+
// Simple todo lines with all markers
|
|
53
|
+
{ text: "- [ ] pending\n- [x] done\n- [-] partial\n- [X] done caps\nfinal", opts: { streaming: true } },
|
|
54
|
+
// Ordered/numeric prefixes
|
|
55
|
+
{ text: "Goal: steps\n* [ ] task one\n- [x] task two\n1. [ ] task three\n4) [ ] task four\nfinal", opts: { streaming: true } },
|
|
56
|
+
// Partial todo tails without trailing newline (streaming)
|
|
57
|
+
{ text: "Goal: ship\n- [", opts: { streaming: true } },
|
|
58
|
+
// Partial todo tails with trailing newline
|
|
59
|
+
{ text: "Goal: ship\n- [\nvisible", opts: { streaming: true } },
|
|
60
|
+
// Partial todo tails with incomplete markers
|
|
61
|
+
{ text: "Goal: ship\n- [x", opts: { streaming: true } },
|
|
62
|
+
{ text: "Some text\n- [ ] partial item", opts: { streaming: true } },
|
|
63
|
+
{ text: "Some text\n- [ ] partial item\n", opts: { streaming: true } },
|
|
64
|
+
// Fenced code containing todo-like lines
|
|
65
|
+
{ text: "```md\n- [ ] literal checklist\n```\n- [ ] transport\nAnswer", opts: { streaming: true } },
|
|
66
|
+
// Nested fences and edge cases
|
|
67
|
+
{ text: "```\n- [ ] inside\n```` still code\n```\nafter\n- [ ] filtered", opts: { streaming: true } },
|
|
68
|
+
// CRLF
|
|
69
|
+
{ text: "Goal: ship\r\n- [ ] first\r\n- [x] second\r\n- [-] third\r\nAnswer", opts: { streaming: true } },
|
|
70
|
+
{ text: "Goal: ship\r\n- [ ] first\r\nAnswer\r\n- [x] leftover", opts: { streaming: true } },
|
|
71
|
+
// Blank-line collapsing
|
|
72
|
+
{ text: "- [ ] filtered\n\n\n\nvisible\n\n\n- [x] filtered\n\nkeep", opts: { streaming: true } },
|
|
73
|
+
// Unicode
|
|
74
|
+
{ text: "😀 live\n- [ ] 隐藏\n- [x] 完成\n🚀 final", opts: { streaming: true } },
|
|
75
|
+
// Unicode with code fence
|
|
76
|
+
{ text: "```émoji\n- [ ] literal\n```\n- [ ] transport\nfinal 🌟", opts: { streaming: true } },
|
|
77
|
+
// No todo lines at all (passthrough)
|
|
78
|
+
{ text: "Plain assistant output with no derived markers.", opts: { streaming: true } },
|
|
79
|
+
// Empty string
|
|
80
|
+
{ text: "", opts: { streaming: true } },
|
|
81
|
+
// Only todo lines
|
|
82
|
+
{ text: "- [ ] only todos\n- [x] all filtered", opts: { streaming: true } },
|
|
83
|
+
// Streaming=false (complete output)
|
|
84
|
+
{ text: "- [ ] line\nvisible", opts: { streaming: false } },
|
|
85
|
+
// Feature-disabled passthrough (isOptionalFeatureDetected returns false)
|
|
86
|
+
{ text: "- [ ] visible when todo feature is absent\n- [x] also visible", opts: { streaming: true, _featureDisabled: true } },
|
|
87
|
+
// Partial incomplete fence
|
|
88
|
+
{ text: "```\n- [ ] hidden inside incomplete fence\n", opts: { streaming: true } },
|
|
89
|
+
// Whitespace-prefixed todo lines
|
|
90
|
+
{ text: "spaced:\n - [ ] indented\n\t- [x] tabbed\nvisible", opts: { streaming: true } },
|
|
91
|
+
// Consecutive blank-line handling
|
|
92
|
+
{ text: "- [ ] filtered\n\nkeep\n\n\n- [x] filtered\nend", opts: { streaming: true } },
|
|
93
|
+
// Single line partial
|
|
94
|
+
{ text: "- [ ]", opts: { streaming: true } },
|
|
95
|
+
{ text: "- [ ]\n", opts: { streaming: true } },
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
for (const { text, opts } of representativeCases) {
|
|
99
|
+
const { streaming = false, _featureDisabled = false } = opts || {};
|
|
100
|
+
const appResult = _featureDisabled
|
|
101
|
+
? (() => {
|
|
102
|
+
// Simulate feature-disabled passthrough without modifying the vm context
|
|
103
|
+
const raw = String(text || "");
|
|
104
|
+
// When isOptionalFeatureDetected returns false, stripTodoProgressLines returns text as-is.
|
|
105
|
+
// Re-create that logic manually since we can't re-run the vm context with a different stub.
|
|
106
|
+
return raw;
|
|
107
|
+
})()
|
|
108
|
+
: appStrip(text, { streaming });
|
|
109
|
+
const authResult = stripTodoProgressLinesAuthoritative(text, { streaming, todoProgressDetected: !_featureDisabled });
|
|
110
|
+
|
|
111
|
+
const label = _featureDisabled
|
|
112
|
+
? `feature-disabled ${JSON.stringify(text)}`
|
|
113
|
+
: `${JSON.stringify(text)} streaming=${streaming}`;
|
|
114
|
+
assert.deepEqual(appResult, authResult, `app stripTodoProgressLines must match authoritative at ${label}`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Additional deep-equal checks via vm extraction of a known instance
|
|
118
|
+
// to ensure function bodies produce the exact same string output line by line.
|
|
119
|
+
const targetedCases = [
|
|
120
|
+
["Goal: ship\r\n- [ ] first\r\n- [x] second\r\nAnswer", { streaming: true }],
|
|
121
|
+
["Goal: ship\r\n- [ ] first\r\n- [x] second\r\nAnswer", { streaming: false }],
|
|
122
|
+
["```md\n- [ ] literal\n```\n- [ ] transport\nAnswer", { streaming: true }],
|
|
123
|
+
["```\n- [ ] one\n```\n- [x] two\nfinal", { streaming: true }],
|
|
124
|
+
["Alpha\n\n\n- [ ] filtered\n\n\nBeta", { streaming: true }],
|
|
125
|
+
["😀unicode🌍\n- [ ] hidden\nkeep", { streaming: true }],
|
|
126
|
+
["", { streaming: true }],
|
|
127
|
+
["", { streaming: false }],
|
|
128
|
+
["- [ ]", { streaming: true }],
|
|
129
|
+
["- [ ] done", { streaming: false }],
|
|
130
|
+
["- [ ] done\n- [x] all\n- [-] three\nvisible", { streaming: true }],
|
|
131
|
+
// Partial tails without newline
|
|
132
|
+
["text\n- [", { streaming: true }],
|
|
133
|
+
["text\n- [x", { streaming: true }],
|
|
134
|
+
["text\n1. [", { streaming: true }],
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
for (const [text, opts] of targetedCases) {
|
|
138
|
+
const { streaming = false } = opts || {};
|
|
139
|
+
const appResult = appStrip(text, { streaming });
|
|
140
|
+
const authResult = stripTodoProgressLinesAuthoritative(text, { streaming });
|
|
141
|
+
assert.deepEqual(
|
|
142
|
+
appResult,
|
|
143
|
+
authResult,
|
|
144
|
+
`targeted case should match: ${JSON.stringify(text)} streaming=${streaming}`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
console.log("todo-filter-static.test.mjs passed");
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { advanceStreamingMarkdownTail } from "../public/stream-markdown-tail.mjs";
|
|
3
|
+
import { createTranscriptRenderer } from "../public/transcript-renderer.mjs";
|
|
4
|
+
|
|
5
|
+
class FakeNode {
|
|
6
|
+
static ELEMENT_NODE = 1;
|
|
7
|
+
static TEXT_NODE = 3;
|
|
8
|
+
constructor(nodeType) {
|
|
9
|
+
this.nodeType = nodeType;
|
|
10
|
+
this.childNodes = [];
|
|
11
|
+
this.parentNode = null;
|
|
12
|
+
this.isConnected = false;
|
|
13
|
+
}
|
|
14
|
+
get firstChild() { return this.childNodes[0] || null; }
|
|
15
|
+
append(...nodes) {
|
|
16
|
+
for (const node of nodes) {
|
|
17
|
+
if (node.nodeType === 11) {
|
|
18
|
+
this.append(...[...node.childNodes]);
|
|
19
|
+
node.childNodes = [];
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
node.parentNode = this;
|
|
23
|
+
node.isConnected = this.isConnected;
|
|
24
|
+
this.childNodes.push(node);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
remove() {
|
|
28
|
+
if (this.parentNode) this.parentNode.childNodes = this.parentNode.childNodes.filter((node) => node !== this);
|
|
29
|
+
this.parentNode = null;
|
|
30
|
+
this.setConnected(false);
|
|
31
|
+
}
|
|
32
|
+
setConnected(value) {
|
|
33
|
+
this.isConnected = value;
|
|
34
|
+
for (const child of this.childNodes) child.setConnected(value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
class FakeText extends FakeNode {
|
|
39
|
+
constructor(data = "") { super(FakeNode.TEXT_NODE); this.data = data; }
|
|
40
|
+
appendData(value) { this.data += value; }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class FakeElement extends FakeNode {
|
|
44
|
+
constructor() { super(FakeNode.ELEMENT_NODE); this.dataset = {}; }
|
|
45
|
+
addEventListener() {}
|
|
46
|
+
closest() { return null; }
|
|
47
|
+
matches() { return false; }
|
|
48
|
+
querySelectorAll() { return []; }
|
|
49
|
+
contains(candidate) {
|
|
50
|
+
if (candidate === this) return true;
|
|
51
|
+
return this.childNodes.some((child) => child === candidate || (child.contains?.(candidate) ?? false));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class FakeFragment extends FakeNode {
|
|
56
|
+
constructor() { super(11); }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
globalThis.Node = FakeNode;
|
|
60
|
+
globalThis.document = {
|
|
61
|
+
addEventListener() {},
|
|
62
|
+
createDocumentFragment: () => new FakeFragment(),
|
|
63
|
+
createTextNode: (value) => new FakeText(value),
|
|
64
|
+
createElement: () => new FakeElement(),
|
|
65
|
+
};
|
|
66
|
+
globalThis.window = {
|
|
67
|
+
addEventListener() {},
|
|
68
|
+
getSelection: () => ({ rangeCount: 0, isCollapsed: true }),
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const chat = new FakeElement();
|
|
72
|
+
chat.setConnected(true);
|
|
73
|
+
const surface = new FakeElement();
|
|
74
|
+
chat.append(surface);
|
|
75
|
+
const renderer = createTranscriptRenderer({ chat });
|
|
76
|
+
let liveCreates = 0;
|
|
77
|
+
let liveAppends = 0;
|
|
78
|
+
let authoritativeRenders = 0;
|
|
79
|
+
|
|
80
|
+
const options = {
|
|
81
|
+
surface,
|
|
82
|
+
messageKey: "live:test",
|
|
83
|
+
kind: "assistant-final",
|
|
84
|
+
stableBoundary: (text, state, advanceOptions) => advanceStreamingMarkdownTail(state, text, advanceOptions),
|
|
85
|
+
renderInto(parent, text) {
|
|
86
|
+
authoritativeRenders += 1;
|
|
87
|
+
const block = new FakeElement();
|
|
88
|
+
block.append(new FakeText(text));
|
|
89
|
+
parent.append(block);
|
|
90
|
+
},
|
|
91
|
+
renderLiveTail(parent, tail) {
|
|
92
|
+
liveCreates += 1;
|
|
93
|
+
const block = new FakeElement();
|
|
94
|
+
block.append(new FakeText(tail));
|
|
95
|
+
parent.append(block);
|
|
96
|
+
},
|
|
97
|
+
appendLiveTail(nodes, previousTail, tail) {
|
|
98
|
+
liveAppends += 1;
|
|
99
|
+
nodes[0].firstChild.appendData(tail.slice(previousTail.length));
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const first = renderer.reconcileMarkdownSurface({ ...options, text: "```js\none" });
|
|
104
|
+
const mountedTail = surface.firstChild;
|
|
105
|
+
assert.equal(first.liveMode, "open-fence");
|
|
106
|
+
assert.equal(liveCreates, 1);
|
|
107
|
+
assert.equal(authoritativeRenders, 0, "an incomplete fence must not run authoritative Markdown/tokenization");
|
|
108
|
+
|
|
109
|
+
const second = renderer.reconcileMarkdownSurface({ ...options, text: "```js\nonetwo" });
|
|
110
|
+
assert.equal(second.reusedTail, true);
|
|
111
|
+
assert.equal(surface.firstChild, mountedTail, "live open-fence appends must retain the mounted tail node identity");
|
|
112
|
+
assert.equal(liveCreates, 1);
|
|
113
|
+
assert.equal(liveAppends, 1);
|
|
114
|
+
assert.equal(authoritativeRenders, 0);
|
|
115
|
+
|
|
116
|
+
const closedText = "```js\nonetwo\n```";
|
|
117
|
+
renderer.reconcileMarkdownSurface({ ...options, text: closedText });
|
|
118
|
+
assert.equal(authoritativeRenders, 1, "closing the fence must perform one authoritative highlighted render");
|
|
119
|
+
assert.notEqual(surface.firstChild, mountedTail);
|
|
120
|
+
const highlightedTail = surface.firstChild;
|
|
121
|
+
|
|
122
|
+
renderer.reconcileMarkdownSurface({ ...options, text: closedText, complete: true });
|
|
123
|
+
assert.equal(authoritativeRenders, 1, "explicit completion must promote an already authoritative close without highlighting twice");
|
|
124
|
+
assert.equal(surface.firstChild, highlightedTail, "authoritative completion must preserve the completed block identity");
|
|
125
|
+
|
|
126
|
+
const longPlain = "x".repeat(16 * 1024 + 1);
|
|
127
|
+
renderer.reconcileMarkdownSurface({ ...options, text: longPlain });
|
|
128
|
+
assert.equal(surface.firstChild.firstChild.data, longPlain);
|
|
129
|
+
renderer.reconcileMarkdownSurface({ ...options, text: longPlain, complete: true });
|
|
130
|
+
assert.equal(authoritativeRenders, 2, "completion must replace the cheap long-tail representation with authoritative Markdown");
|
|
131
|
+
|
|
132
|
+
const incompleteFence = "```js\nunfinished";
|
|
133
|
+
renderer.reconcileMarkdownSurface({ ...options, text: incompleteFence });
|
|
134
|
+
renderer.reconcileMarkdownSurface({ ...options, text: incompleteFence, complete: true });
|
|
135
|
+
assert.equal(authoritativeRenders, 3, "completion must authoritatively tokenize even an unclosed fence exactly once");
|
|
136
|
+
|
|
137
|
+
console.log("transcript-markdown-tail.test.mjs passed");
|