fix(compaction): align native lifecycle and reconcile replay notices
Refs: NAN-326, NAN-327
This commit is contained in:
1 parent
f9257c3548
commit
be43d2a5f8
8 files changed
+298
-78
No files matched your search
@@ -112,6 +112,10 @@ async function installBackend(page: Page) {
|
||||
// The fixture's non-consuming observer acknowledges client dispatch, not a model installation.
|
||||
await expect(page.getByLabel("Synthetic frame receipt")).toHaveText(`${"chat_id" in event ? event.chat_id : "-"}/${turn}/${event.event}/${sequence}`);
|
||||
},
|
||||
refreshThread() {
|
||||
expect(socket, "The fixture must establish its WebSocket before refresh").toBeDefined();
|
||||
socket!.send(JSON.stringify({ event: "session_updated", chat_id: A, scope: "thread" }));
|
||||
},
|
||||
async ready() {
|
||||
await expect(page.getByLabel("Synthetic transport status")).toHaveText("open");
|
||||
await expect.poll(() => attached.includes(A)).toBe(true);
|
||||
@@ -156,6 +160,52 @@ async function expectBefore(body: Locator, first: string, second: string) {
|
||||
expect(text.indexOf(second), `${second} must follow ${first}`).toBeGreaterThan(text.indexOf(first));
|
||||
}
|
||||
|
||||
test("idle compaction stays singular across live updates, thread refresh and cold replay", async ({ page }) => {
|
||||
const { backend, body } = await openFixture(page);
|
||||
const base = closedTurn("idle-completed", "Before idle compression", "Completed answer before idle compression.");
|
||||
await backend.release(await backend.read(), history(base));
|
||||
await expect(body.getByText("Completed answer before idle compression.", { exact: true })).toBeVisible();
|
||||
const compaction = { event: "context_compaction" as const, chat_id: A, compaction_id: "idle-once" };
|
||||
await backend.send({ ...compaction, phase: "started" });
|
||||
await expect(body.locator('[data-context-compaction="started"]')).toHaveCount(1);
|
||||
await backend.send({ ...compaction, phase: "succeeded" });
|
||||
await expect(body.locator('[data-context-compaction="succeeded"]')).toHaveCount(1);
|
||||
|
||||
const completed = history([...base, { ...compaction, phase: "succeeded" }]);
|
||||
backend.refreshThread();
|
||||
await backend.release(await backend.read(A, null, 1), completed);
|
||||
await expect(body.locator('[data-context-compaction="succeeded"]')).toHaveCount(1);
|
||||
await expectBefore(body, "Completed answer before idle compression.", "Context compacted");
|
||||
await page.reload();
|
||||
await backend.ready();
|
||||
await backend.release(await backend.read(A, null, 2), completed);
|
||||
await expect(body.locator('[data-context-compaction="succeeded"]')).toHaveCount(1);
|
||||
await expectBefore(body, "Completed answer before idle compression.", "Context compacted");
|
||||
backend.healthy();
|
||||
});
|
||||
|
||||
test("pre-request compaction precedes the answer in live output and history replay", async ({ page }) => {
|
||||
const { backend, body } = await openFixture(page);
|
||||
const base = closedTurn("before-native", "Previous question", "Previous answer.");
|
||||
await backend.release(await backend.read(), history(base));
|
||||
await expect(body.getByText("Previous answer.", { exact: true })).toBeVisible();
|
||||
const question = input(TURN, "Question requiring native compression");
|
||||
const compact: ThreadProjectionEvent = { ...at(TURN, 2, "activity"), event: "context_compaction", compaction_id: "native-before-answer", phase: "succeeded" };
|
||||
await backend.send(question);
|
||||
await backend.send({ event: "context_compaction", chat_id: A, compaction_id: "native-before-answer", phase: "started" });
|
||||
await backend.send(compact);
|
||||
await backend.send(delta(3, LIVE));
|
||||
await backend.send(end(4, LIVE, "live-wire"));
|
||||
const complete: ThreadProjectionEvent = { ...at(TURN, 5, "complete"), event: "turn_end" };
|
||||
await backend.send(complete);
|
||||
await expectBefore(body, "Context compacted", LIVE);
|
||||
backend.refreshThread();
|
||||
await backend.release(await backend.read(A, null, 1), history([...base, question, compact, end(4, LIVE, "live-wire"), complete]));
|
||||
await expect(body.locator('[data-context-compaction="succeeded"]')).toHaveCount(1);
|
||||
await expectBefore(body, "Context compacted", LIVE);
|
||||
backend.healthy();
|
||||
});
|
||||
|
||||
for (const width of [1440, 390]) {
|
||||
test(`synthetic live-first latest admits its prefix without refresh, then prepends two cursor pages (${width}px)`, async ({ page }) => {
|
||||
const { backend, body } = await openFixture(page, width);
|
||||
|
||||
@@ -178,8 +178,9 @@ export class ThreadTranscript {
|
||||
|
||||
registerInput(row: UIMessage, received = true): void {
|
||||
if (row.role === "user" && row.turnId) this.knownTurns.add(row.turnId);
|
||||
const identity = row.sessionMessage?.message_id
|
||||
? `input:${row.sessionMessage.message_id}` : row.role === "user" && row.turnId
|
||||
const identity = row.kind === "compaction" && row.compaction
|
||||
? `compaction:${row.compaction.id}` : row.sessionMessage?.message_id
|
||||
? `input:${row.sessionMessage.message_id}` : row.role === "user" && row.turnId
|
||||
? `user:${row.turnId}` : row.turnId && row.turnSeq !== undefined
|
||||
? `fact:${row.turnId}:${row.turnSeq}` : `replay:${row.id}`;
|
||||
if (this.bySource.has(identity)) return;
|
||||
@@ -200,6 +201,9 @@ export class ThreadTranscript {
|
||||
for (const [first, last] of receipt.ranges) addRange(ranges, first, last);
|
||||
members.set(receipt.turn_id, ranges);
|
||||
}
|
||||
if (event.event === "context_compaction") {
|
||||
return { source: `compaction:${event.compaction_id}`, members, text: null };
|
||||
}
|
||||
if (event.event === "user_message") {
|
||||
return { source: event.provenance?.session_message?.message_id
|
||||
? `input:${event.provenance.session_message.message_id}` : turn ? `user:${turn}` : `replay:${event.projection_id}`,
|
||||
|
||||
@@ -41,6 +41,52 @@ function receive(thread: ThreadTranscript, ...events: ThreadProjectionEvent[]):
|
||||
}
|
||||
|
||||
describe("ThreadTranscript admission", () => {
|
||||
it.each(["succeeded", "failed", "cancelled"] as const)("reconciles one idle compaction across live phases and history (%s)", (phase) => {
|
||||
const thread = new ThreadTranscript();
|
||||
const base = [
|
||||
input("turn-before-idle", "Question"),
|
||||
{ ...at("turn-before-idle", 2), event: "stream_end" as const, stream_id: "answer-before-idle", text: "Answer" },
|
||||
{ ...at("turn-before-idle", 3), event: "turn_end" as const },
|
||||
];
|
||||
const completed = { completed_turn_ids: ["turn-before-idle"] };
|
||||
expect(thread.accept(history(base, completed), thread.begin("initial"))).toBe(true);
|
||||
const compaction = { event: "context_compaction" as const, chat_id: "hydration-chat", compaction_id: "idle-one" };
|
||||
receive(thread, { ...compaction, phase: "started" });
|
||||
const staleRead = thread.begin("latest");
|
||||
receive(thread, { ...compaction, phase });
|
||||
expect(thread.accept(history(base, completed), staleRead)).toBe(true);
|
||||
expect(thread.messages.map((row) => row.id).filter((id) => id === "compaction-idle-one")).toHaveLength(1);
|
||||
expect(thread.messages.at(-1)?.compaction?.phase).toBe(phase);
|
||||
|
||||
const body = history([...base, { ...compaction, phase }], completed);
|
||||
expect(thread.accept(body, thread.begin("latest"))).toBe(true);
|
||||
expect(thread.messages.map((row) => row.content)).toEqual(["Question", "Answer", ""]);
|
||||
expect(thread.messages[2]).toMatchObject({ id: "compaction-idle-one", compaction: { phase } });
|
||||
const owners = thread.save().units.filter((unit) => unit.rows.has("compaction-idle-one"));
|
||||
expect(owners).toHaveLength(1);
|
||||
expect(thread.accept(body, thread.begin("latest"))).toBe(true);
|
||||
const restored = new ThreadTranscript([], thread.save());
|
||||
receive(restored, { ...compaction, phase });
|
||||
expect(restored.accept(body, restored.begin("latest"))).toBe(true);
|
||||
expect(restored.messages.map((row) => row.id).filter((id) => id === "compaction-idle-one")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("keeps distinct native and idle compactions when history refreshes a live idle notice", () => {
|
||||
const thread = new ThreadTranscript();
|
||||
const native: ThreadProjectionEvent = { ...at("turn-native", 2), event: "context_compaction", compaction_id: "native", phase: "succeeded" };
|
||||
const base: ThreadProjectionEvent[] = [
|
||||
input("turn-native", "Question"), native,
|
||||
{ ...at("turn-native", 3), event: "stream_end", stream_id: "answer-native", text: "Answer" },
|
||||
{ ...at("turn-native", 4), event: "turn_end" },
|
||||
];
|
||||
const completed = { completed_turn_ids: ["turn-native"] };
|
||||
expect(thread.accept(history(base, completed), thread.begin("initial"))).toBe(true);
|
||||
const idle: ThreadProjectionEvent = { event: "context_compaction", chat_id: "hydration-chat", compaction_id: "idle", phase: "succeeded" };
|
||||
receive(thread, idle);
|
||||
expect(thread.accept(history([...base, idle], completed), thread.begin("latest"))).toBe(true);
|
||||
expect(thread.messages.map((row) => row.compaction?.id ?? row.content)).toEqual(["Question", "native", "Answer", "idle"]);
|
||||
});
|
||||
|
||||
it("installs an older safe prefix without replacing or redirecting the received foreground", () => {
|
||||
const thread = new ThreadTranscript();
|
||||
const read = thread.begin("initial");
|
||||
|
||||
Reference in new issue
Block a user