Skip to content

Commit f5cf809

Browse files
only21milclaude
andcommitted
perf(server): drive shell last-error lookup from thread bindings
The last_error subquery in the shell thread query and in the usage-limit recovery query joined provider sessions to bindings without a fixed order. With no planner statistics, SQLite drove it from sessions keyed only on provider_instance_id, so every thread walked every session of its provider instance: threads x sessions probes per call. Keep bindings outermost with CROSS JOIN, as the pending-secret lookup in the same query already does. Each thread now reads its own one or two bindings through bindings_thread_idx, then each session by primary key. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8c777fb commit f5cf809

2 files changed

Lines changed: 111 additions & 6 deletions

File tree

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,109 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
29142914
}),
29152915
);
29162916

2917+
it.effect("looks up a thread's last provider error through its own session bindings", () =>
2918+
Effect.gen(function* () {
2919+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2920+
const sql = yield* SqlClient.SqlClient;
2921+
const now = yield* DateTime.now;
2922+
const later = DateTime.add(now, { minutes: 1 });
2923+
const projectId = ProjectId.make("project:projection-session-error-lookup");
2924+
const attachErroredSession = Effect.fnUntraced(function* (
2925+
name: string,
2926+
lastError: string,
2927+
updatedAt: DateTime.Utc,
2928+
) {
2929+
const threadId = ThreadId.make(`thread:projection-session-error-lookup:${name}`);
2930+
yield* projectionStore.apply({
2931+
id: EventId.make(`event:projection-session-error-lookup:${name}:thread`),
2932+
type: "thread.created",
2933+
threadId,
2934+
occurredAt: now,
2935+
payload: {
2936+
createdBy: "user",
2937+
creationSource: "web",
2938+
id: threadId,
2939+
projectId,
2940+
title: name,
2941+
providerInstanceId,
2942+
modelSelection,
2943+
runtimeMode: "full-access",
2944+
interactionMode: "default",
2945+
branch: null,
2946+
worktreePath: null,
2947+
activeProviderThreadId: null,
2948+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2949+
forkedFrom: null,
2950+
createdAt: now,
2951+
updatedAt: now,
2952+
archivedAt: null,
2953+
settledOverride: null,
2954+
settledAt: null,
2955+
lastVisitedAt: null,
2956+
deletedAt: null,
2957+
},
2958+
});
2959+
yield* projectionStore.apply({
2960+
id: EventId.make(`event:projection-session-error-lookup:${name}:session`),
2961+
type: "provider-session.attached",
2962+
threadId,
2963+
driver,
2964+
providerInstanceId,
2965+
occurredAt: updatedAt,
2966+
payload: {
2967+
id: ProviderSessionId.make(`provider-session:projection-session-error-lookup:${name}`),
2968+
driver,
2969+
providerInstanceId,
2970+
status: "error",
2971+
cwd: "/workspace",
2972+
model: modelSelection.model,
2973+
capabilities: CodexProviderCapabilitiesV2,
2974+
createdAt: updatedAt,
2975+
updatedAt,
2976+
lastError,
2977+
},
2978+
});
2979+
return threadId;
2980+
});
2981+
2982+
const threadId = yield* attachErroredSession("own", "own session failed", now);
2983+
// Every thread on the provider instance has its own session, and these
2984+
// are newer. They must neither leak into the thread nor be walked for it.
2985+
for (let index = 0; index < 24; index++) {
2986+
yield* attachErroredSession(`other-${index}`, "other session failed", later);
2987+
}
2988+
2989+
const { statements, tracer } = traceSqlStatements();
2990+
const shell = yield* projectionStore.getShellSnapshot().pipe(Effect.withTracer(tracer));
2991+
assert.equal(
2992+
shell.threads.find((thread) => thread.id === threadId)?.lastError,
2993+
"own session failed",
2994+
);
2995+
yield* projectionStore
2996+
.getLimitRecoveryCandidates({ now, autoResume: true, snooze: false })
2997+
.pipe(Effect.withTracer(tracer));
2998+
2999+
// One provider instance usually holds most sessions. Driving the lookup
3000+
// from sessions walks all of them for every thread in the snapshot.
3001+
for (const marker of ["AS blocking_failure_payload_json", "AS failure_payload_json"]) {
3002+
const statement = statements.find((query) => query.includes(marker));
3003+
assert.isDefined(statement, marker);
3004+
const plan = (yield* sql.unsafe<{ readonly detail: string }>(
3005+
`EXPLAIN QUERY PLAN ${statement}`,
3006+
)).map((row) => row.detail);
3007+
const bindingLookup = plan.findIndex((detail) => detail.startsWith("SEARCH binding "));
3008+
const sessionLookup = plan.findIndex((detail) => detail.startsWith("SEARCH session "));
3009+
assert.isAtLeast(bindingLookup, 0, marker);
3010+
assert.include(
3011+
plan[bindingLookup],
3012+
"orchestration_v2_projection_provider_session_bindings_thread_idx (thread_id=?)",
3013+
);
3014+
assert.isAbove(sessionLookup, bindingLookup, marker);
3015+
assert.include(plan[sessionLookup], "(provider_session_id=?)");
3016+
}
3017+
}),
3018+
);
3019+
29173020
it.effect(
29183021
"reads checkpoint context without decoding transcript or checkpoint file payloads",
29193022
() =>

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,9 +3468,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
34683468
item.payload_json AS failure_payload_json,
34693469
(
34703470
SELECT json_extract(session.payload_json, '$.lastError')
3471-
FROM orchestration_v2_projection_provider_sessions session
3472-
INNER JOIN orchestration_v2_projection_provider_session_bindings binding
3473-
ON binding.provider_session_id = session.provider_session_id
3471+
-- Keep bindings outermost: one provider instance can hold every session.
3472+
FROM orchestration_v2_projection_provider_session_bindings binding
3473+
CROSS JOIN orchestration_v2_projection_provider_sessions session
3474+
ON session.provider_session_id = binding.provider_session_id
34743475
WHERE binding.thread_id = t.thread_id
34753476
AND session.provider_instance_id = t.provider_instance_id
34763477
ORDER BY session.updated_at DESC, session.provider_session_id DESC
@@ -5180,9 +5181,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
51805181
) AS activity_run_started_at,
51815182
(
51825183
SELECT json_extract(session.payload_json, '$.lastError')
5183-
FROM orchestration_v2_projection_provider_sessions session
5184-
INNER JOIN orchestration_v2_projection_provider_session_bindings binding
5185-
ON binding.provider_session_id = session.provider_session_id
5184+
-- Keep bindings outermost: one provider instance can hold every session.
5185+
FROM orchestration_v2_projection_provider_session_bindings binding
5186+
CROSS JOIN orchestration_v2_projection_provider_sessions session
5187+
ON session.provider_session_id = binding.provider_session_id
51865188
WHERE binding.thread_id = t.thread_id
51875189
AND session.provider_instance_id = t.provider_instance_id
51885190
ORDER BY session.updated_at DESC, session.provider_session_id DESC

0 commit comments

Comments
 (0)