Skip to content

Commit c7e3993

Browse files
Yash-Singh1juliusmarmingeclaude
authored
fix(server): speed up shell snapshots on large databases (pingdotgg#23751)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 200dad3 commit c7e3993

5 files changed

Lines changed: 281 additions & 72 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,6 +2374,170 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
23742374
}),
23752375
);
23762376

2377+
it.effect("looks up shell stop requests through the interrupt request index", () =>
2378+
Effect.gen(function* () {
2379+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2380+
const sql = yield* SqlClient.SqlClient;
2381+
const { projectId } = yield* assertProjectScopedShells("sql-shell-stop-index");
2382+
const { statements, tracer } = traceSqlStatements();
2383+
yield* projectionStore
2384+
.getShellSnapshot({ projectId, location: "active" })
2385+
.pipe(Effect.withTracer(tracer));
2386+
2387+
const threadQuery = statements.find((statement) =>
2388+
statement.includes("AS latest_run_stop_requested"),
2389+
);
2390+
assert.isDefined(threadQuery);
2391+
const stopRequestLookups = (yield* sql.unsafe<{ readonly detail: string }>(
2392+
`EXPLAIN QUERY PLAN ${threadQuery}`,
2393+
))
2394+
.map((row) => row.detail)
2395+
.filter((detail) => detail.startsWith("SEARCH stop_request "));
2396+
assert.lengthOf(stopRequestLookups, 2);
2397+
for (const lookup of stopRequestLookups) {
2398+
assert.include(lookup, "orchestration_v2_projection_turn_items_stop_request_idx");
2399+
}
2400+
}),
2401+
);
2402+
2403+
it.effect(
2404+
"counts each shell's own runless and live-run items, skipping rolled back and missing runs",
2405+
() =>
2406+
Effect.gen(function* () {
2407+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2408+
const now = yield* DateTime.now;
2409+
const projectId = ProjectId.make("project:shell-item-counts");
2410+
const busyThreadId = ThreadId.make("thread:shell-item-counts:busy");
2411+
const emptyThreadId = ThreadId.make("thread:shell-item-counts:empty");
2412+
const otherThreadId = ThreadId.make("thread:shell-item-counts:other");
2413+
for (const threadId of [busyThreadId, emptyThreadId, otherThreadId]) {
2414+
yield* projectionStore.apply({
2415+
id: EventId.make(`event:${threadId}:created`),
2416+
type: "thread.created",
2417+
threadId,
2418+
occurredAt: now,
2419+
payload: {
2420+
createdBy: "user",
2421+
creationSource: "web",
2422+
id: threadId,
2423+
projectId,
2424+
title: threadId,
2425+
providerInstanceId,
2426+
modelSelection,
2427+
runtimeMode: "full-access",
2428+
interactionMode: "default",
2429+
branch: null,
2430+
worktreePath: null,
2431+
activeProviderThreadId: null,
2432+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2433+
forkedFrom: null,
2434+
createdAt: now,
2435+
updatedAt: now,
2436+
archivedAt: null,
2437+
settledOverride: null,
2438+
settledAt: null,
2439+
lastVisitedAt: null,
2440+
deletedAt: null,
2441+
},
2442+
});
2443+
}
2444+
const applyRun = (
2445+
threadId: ThreadId,
2446+
suffix: string,
2447+
ordinal: number,
2448+
rolledBack: boolean,
2449+
) => {
2450+
const runId = RunId.make(`run:shell-item-counts:${suffix}`);
2451+
const rootNodeId = NodeId.make(`node:shell-item-counts:${suffix}`);
2452+
return projectionStore
2453+
.apply({
2454+
id: EventId.make(`event:shell-item-counts:${suffix}:run`),
2455+
type: "run.created",
2456+
threadId,
2457+
runId,
2458+
nodeId: rootNodeId,
2459+
driver,
2460+
occurredAt: now,
2461+
payload: {
2462+
id: runId,
2463+
threadId,
2464+
ordinal,
2465+
providerInstanceId,
2466+
modelSelection,
2467+
providerThreadId: null,
2468+
userMessageId: MessageId.make(`message:shell-item-counts:${suffix}:user`),
2469+
rootNodeId,
2470+
activeAttemptId: null,
2471+
status: rolledBack ? "rolled_back" : "completed",
2472+
requestedAt: now,
2473+
startedAt: now,
2474+
completedAt: now,
2475+
checkpointId: null,
2476+
contextHandoffId: null,
2477+
},
2478+
})
2479+
.pipe(Effect.as(runId));
2480+
};
2481+
let ordinal = 0;
2482+
const applyItem = (threadId: ThreadId, runId: RunId | null) => {
2483+
ordinal += 1;
2484+
return projectionStore.apply({
2485+
id: EventId.make(`event:shell-item-counts:item:${ordinal}`),
2486+
type: "turn-item.updated",
2487+
threadId,
2488+
...(runId === null ? {} : { runId }),
2489+
occurredAt: now,
2490+
payload: {
2491+
id: TurnItemId.make(`turn-item:shell-item-counts:${ordinal}`),
2492+
threadId,
2493+
runId,
2494+
nodeId: null,
2495+
providerThreadId: null,
2496+
providerTurnId: null,
2497+
nativeItemRef: null,
2498+
parentItemId: null,
2499+
ordinal,
2500+
status: "completed",
2501+
title: null,
2502+
startedAt: now,
2503+
completedAt: now,
2504+
updatedAt: now,
2505+
type: "assistant_message",
2506+
messageId: MessageId.make(`message:shell-item-counts:${ordinal}`),
2507+
text: `item ${ordinal}`,
2508+
streaming: false,
2509+
},
2510+
});
2511+
};
2512+
2513+
const completedRunId = yield* applyRun(busyThreadId, "busy-completed", 1, false);
2514+
const rolledBackRunId = yield* applyRun(busyThreadId, "busy-rolled-back", 2, true);
2515+
const otherRunId = yield* applyRun(otherThreadId, "other", 1, false);
2516+
yield* applyItem(busyThreadId, null);
2517+
yield* applyItem(busyThreadId, null);
2518+
yield* applyItem(busyThreadId, completedRunId);
2519+
yield* applyItem(busyThreadId, completedRunId);
2520+
yield* applyItem(busyThreadId, rolledBackRunId);
2521+
yield* applyItem(busyThreadId, RunId.make("run:shell-item-counts:missing"));
2522+
yield* applyItem(otherThreadId, otherRunId);
2523+
2524+
const snapshot = yield* projectionStore.getShellSnapshot({ projectId });
2525+
const expected = [
2526+
[busyThreadId, 4],
2527+
[emptyThreadId, 0],
2528+
[otherThreadId, 1],
2529+
] as const;
2530+
for (const [threadId, itemCount] of expected) {
2531+
const shell = yield* projectionStore.getThreadShell(threadId);
2532+
assert.deepEqual(
2533+
snapshot.threads.find((thread) => thread.id === threadId),
2534+
shell ?? undefined,
2535+
);
2536+
assert.equal(shell?.itemCount, itemCount);
2537+
}
2538+
}),
2539+
);
2540+
23772541
it.effect("does not treat visited or marked-unread state as thread activity", () =>
23782542
Effect.gen(function* () {
23792543
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;

0 commit comments

Comments
 (0)