fix(webui): retain system context graph after compaction (#6608)
This commit is contained in:
1 parent
d789e1bfc7
commit
4212ad726d
25 files changed
+748
-102
No files matched your search
@@ -341,6 +341,45 @@ test("synthetic HTTP-first cumulative text suppresses delayed covered chunks but
|
||||
backend.healthy();
|
||||
});
|
||||
|
||||
test("compaction keeps active system files in the context graph through live delivery and replay", async ({ page }) => {
|
||||
const { backend } = await openFixture(page);
|
||||
const base = closedTurn("before-graph", "Previous question", "Previous answer.");
|
||||
await backend.release(await backend.read(), history(base));
|
||||
const question = input(TURN, "Question requiring compaction");
|
||||
const system = { path: "/SOUL.md", name: "SOUL.md", state: "loaded" as const, tokens: 40 };
|
||||
const guide = { path: "/guide.md", name: "guide.md", state: "discovered" as const, tokens: 0 };
|
||||
const previous = { path: "/before.py", name: "before.py", state: "loaded" as const, tokens: 100 };
|
||||
const systemGraph = { nodes: [system, guide], edges: [{ source: system.path, target: guide.path }] };
|
||||
const started: ThreadProjectionEvent = { ...at(TURN, 1, "activity"), event: "model_request",
|
||||
run_id: "run", request_id: "active", sequence: 1, phase: "started",
|
||||
context_graph: { nodes: [system, guide, previous], edges: systemGraph.edges } };
|
||||
const compact: ThreadProjectionEvent = { ...at(TURN, 2, "activity"), event: "context_compaction",
|
||||
compaction_id: "active", phase: "succeeded", context_graph: systemGraph };
|
||||
await backend.send(question);
|
||||
await backend.send(started);
|
||||
await page.getByTestId("composer-context-usage").click();
|
||||
const sidebar = page.getByTestId("context-sidebar");
|
||||
await expect(sidebar.getByRole("button", { name: "before.py", exact: true })).toBeVisible();
|
||||
await backend.send(compact);
|
||||
await expect(sidebar.getByRole("button", { name: "SOUL.md", exact: true })).toBeVisible();
|
||||
await expect(sidebar.getByRole("button", { name: "guide.md", exact: true })).toBeVisible();
|
||||
await expect(sidebar.getByRole("button", { name: "before.py", exact: true })).toHaveCount(0);
|
||||
|
||||
const replay = history([...base, question, started, compact], { active: TURN });
|
||||
backend.refreshThread();
|
||||
await backend.release(await backend.read(A, null, 1), replay);
|
||||
await expect(sidebar.getByRole("button", { name: "SOUL.md", exact: true })).toBeVisible();
|
||||
await expect(sidebar.getByRole("button", { name: "before.py", exact: true })).toHaveCount(0);
|
||||
await page.reload();
|
||||
await backend.ready();
|
||||
await backend.release(await backend.read(A, null, 2), replay);
|
||||
if (!await sidebar.isVisible()) await page.getByTestId("composer-context-usage").click();
|
||||
await expect(sidebar.getByRole("button", { name: "SOUL.md", exact: true })).toBeVisible();
|
||||
await expect(sidebar.getByRole("button", { name: "guide.md", exact: true })).toBeVisible();
|
||||
await expect(sidebar.getByRole("button", { name: "before.py", exact: true })).toHaveCount(0);
|
||||
backend.healthy();
|
||||
});
|
||||
|
||||
test("the open context sidebar keeps its slot through hydration and empty conversation transitions", async ({ page }) => {
|
||||
const { backend } = await openFixture(page);
|
||||
await page.emulateMedia({ reducedMotion: "no-preference" });
|
||||
|
||||
@@ -385,6 +385,7 @@ function parseThreadProjectionEvent(value: unknown, validatedGraphs: ReadonlySet
|
||||
if (value.text !== undefined && typeof value.text !== "string") break;
|
||||
return value as unknown as ThreadProjectionEvent;
|
||||
case "context_compaction":
|
||||
if (value.context_graph !== undefined && !validatedGraphs.has(value.context_graph) && !isContextGraph(value.context_graph)) break;
|
||||
if (value.context_snapshot !== undefined && !isContextInspection(value.context_snapshot, (graph): graph is ContextGraph => validatedGraphs.has(graph) || isContextGraph(graph))) break;
|
||||
if (typeof value.compaction_id !== "string" || !value.compaction_id
|
||||
|| !isCompactionPhase(value.phase)
|
||||
|
||||
@@ -1050,7 +1050,7 @@ export function projectThreadEvent(
|
||||
id, graph: event.context_snapshot.graph,
|
||||
estimatedTokens: event.context_snapshot.estimated_tokens,
|
||||
contextWindowTokens: event.context_snapshot.context_window_tokens,
|
||||
} } : {}),
|
||||
} } : event.context_graph ? { contextSnapshot: { id, graph: event.context_graph } } : {}),
|
||||
...(!options.persisted ? { announce: true } : {}),
|
||||
},
|
||||
...positionedTurnFields(state.messages[existing], turnFieldsForProjection(state, event, "activity"), options),
|
||||
|
||||
@@ -1588,7 +1588,7 @@ export type InboundEvent =
|
||||
agent_ui?: AgentUIBlob;
|
||||
} & InboundTurnMetadata)
|
||||
| (Exclude<NotificationEvent, { event: "context_compaction" }> & InboundTurnMetadata)
|
||||
| (Extract<NotificationEvent, { event: "context_compaction" }> & InboundTurnMetadata & { context_snapshot?: ContextInspection })
|
||||
| (Extract<NotificationEvent, { event: "context_compaction" }> & InboundTurnMetadata & { context_snapshot?: ContextInspection; context_graph?: ContextGraph })
|
||||
| ({
|
||||
event: "file_edit";
|
||||
chat_id: string;
|
||||
|
||||
@@ -40,9 +40,14 @@ function reloadPayload(body: WebuiThreadPersistedPayload): unknown {
|
||||
const { context_graph, ...rest } = event;
|
||||
return { ...rest, context_graph_ref: reference(context_graph) };
|
||||
}
|
||||
if (event.event === "context_compaction" && event.context_snapshot) {
|
||||
const { graph, ...snapshot } = event.context_snapshot;
|
||||
return { ...event, context_snapshot: { ...snapshot, graph_ref: reference(graph) } };
|
||||
if (event.event === "context_compaction" && (event.context_graph || event.context_snapshot)) {
|
||||
const { context_graph, context_snapshot, ...rest } = event;
|
||||
const snapshot = context_snapshot && (({ graph, ...fields }) => ({ ...fields, graph_ref: reference(graph) }))(context_snapshot);
|
||||
return {
|
||||
...rest,
|
||||
...(context_graph ? { context_graph_ref: reference(context_graph) } : {}),
|
||||
...(snapshot ? { context_snapshot: snapshot } : {}),
|
||||
};
|
||||
}
|
||||
return event;
|
||||
});
|
||||
|
||||
@@ -257,3 +257,21 @@ it.each(["failed", "cancelled", "completed"] as const)("does not borrow previous
|
||||
expect(frames[0].usage).toEqual(usage(1));
|
||||
expect(frames[1].usage).toBeUndefined();
|
||||
});
|
||||
|
||||
|
||||
it.each([false, true])("shows the active system graph immediately after compaction (persisted=%s)", (persisted) => {
|
||||
const transcript = new ThreadTranscript();
|
||||
const oldGraph = { nodes: [{ path: "/before.py", name: "before.py", state: "loaded" as const, tokens: 50 }], edges: [] };
|
||||
const systemGraph = { nodes: [{ path: "/SOUL.md", name: "SOUL.md", state: "loaded" as const, tokens: 40 }], edges: [] };
|
||||
project(transcript, request(1, { request_id: "active", context_graph: oldGraph }), persisted);
|
||||
const event = parseWebuiThreadPayload({ schemaVersion: 3, projection: "events", events: [{ ...envelope,
|
||||
event: "context_compaction", compaction_id: "boundary", phase: "succeeded", turn_seq: 2, context_graph: systemGraph,
|
||||
}] }).events[0];
|
||||
project(transcript, event, persisted);
|
||||
expect(composerContextSnapshots(transcript.messages, transcript.modelRequests)).toEqual([
|
||||
{ id: "compaction-boundary", graph: systemGraph },
|
||||
]);
|
||||
project(transcript, request(3, { request_id: "active", phase: "completed", context_graph: systemGraph }), persisted);
|
||||
expect(composerContextSnapshots(transcript.messages, transcript.modelRequests).map((snapshot) => snapshot.graph))
|
||||
.toEqual([systemGraph, systemGraph]);
|
||||
});
|
||||
@@ -43,6 +43,7 @@ describe("tab reload cache", () => {
|
||||
const body = parseWebuiThreadPayload({ schemaVersion: 3, sessionKey: "websocket:growing", revision: "growing-50",
|
||||
projection: "events", active_turn_id: null, page: { before_cursor: "older", has_more_before: true },
|
||||
events: [{ event: "context_compaction", chat_id: "growing", compaction_id: "checkpoint", phase: "succeeded",
|
||||
...(format === "inline" ? { context_graph: structuredClone(graphs[0]) } : { context_graph_ref: "0" }),
|
||||
context_snapshot: { estimated_tokens: 100, context_window_tokens: 100000,
|
||||
...(format === "inline" ? { graph: structuredClone(graphs[0]) } : { graph_ref: "0" }) } }, ...events],
|
||||
...(format === "references" ? { context_graphs: table, context_graph_nodes: nodes, context_graph_edges: edges } : {}),
|
||||
|
||||
Reference in new issue
Block a user