Skip to content

Commit 0955847

Browse files
fix(server): idle threads resume without a snapshot; keep anchors of retained stream heads
A thread idle since before the pruned boundary resumes at its own high-water, so it missed nothing; it no longer resends its full body on every reconnect. A stream's newest row is kept even below the boundary, so the prompt anchor it borrows is now kept too; deleting it left the row undecodable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 05efb69 commit 0955847

5 files changed

Lines changed: 88 additions & 4 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,36 @@ it.effect("prunes events the projections cover once the retained day has passed"
302302
}).pipe(Effect.provide(layerTest)),
303303
);
304304

305+
it.effect("keeps the anchor a retained stream head borrows from", () =>
306+
Effect.gen(function* () {
307+
const sink = yield* EventSink.EventSinkV2;
308+
const eventStore = yield* EventStore.EventStoreV2;
309+
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
310+
const now = yield* DateTime.now;
311+
const threadId = ThreadId.make("thread:prune-head");
312+
yield* sink.write({ events: [threadCreated(makeThread(threadId, now), now)] });
313+
yield* sink.write({ events: subagentEvents(threadId, now, 0) });
314+
// The stream's newest row borrows prompt A, so it is kept below the boundary.
315+
yield* sink.write({ events: subagentEvents(threadId, now, 0, "repeat") });
316+
yield* maintenance.pruneEventStore();
317+
yield* TestClock.adjust("1 day");
318+
assert.isAbove((yield* maintenance.pruneEventStore()).deletedEventCount, 0);
319+
320+
assert.include(
321+
yield* remainingEventIds(threadId),
322+
"event:turn-item-compaction:subagent-item:0",
323+
);
324+
const stored = yield* eventStore.read({ threadId }).pipe(Stream.runCollect);
325+
const head = Array.from(stored).at(-1)?.event;
326+
assert.deepEqual(
327+
head?.type === "turn-item.updated" && head.payload.type === "subagent"
328+
? head.payload.prompt
329+
: null,
330+
"Review prompt A. ".repeat(40),
331+
);
332+
}).pipe(Effect.provide(layerTest)),
333+
);
334+
305335
it.effect("keeps an anchor that a row appended during the pass borrows from", () =>
306336
Effect.scoped(
307337
Effect.gen(function* () {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ export const layer: Layer.Layer<ProjectionMaintenanceV2, never, SqlClient.SqlCli
100100
betweenPages: Effect.Effect<void>,
101101
) {
102102
const referenced = yield* readPromptReferences(throughSequence, head, betweenPages);
103+
// Stream heads at or below the boundary are kept, so the anchors they borrow must stay too.
104+
const keptHeadReferences = yield* sql<{ readonly prompt_sequence: number }>`
105+
SELECT json_extract(event.payload_json, '$.promptSequence') AS prompt_sequence
106+
FROM (
107+
SELECT MAX(sequence) AS sequence FROM orchestration_events
108+
GROUP BY aggregate_kind, stream_id
109+
) AS heads
110+
JOIN orchestration_events AS event ON event.sequence = heads.sequence
111+
WHERE heads.sequence <= ${throughSequence}
112+
AND event.event_type IN ('subagent.updated', 'turn-item.updated')
113+
AND json_type(event.payload_json, '$.promptSequence') = 'integer'
114+
`.pipe(NodeSqliteClient.readOnly);
115+
for (const row of keptHeadReferences) referenced.add(row.prompt_sequence);
116+
yield* betweenPages;
103117
let referencesThrough = head;
104118
let deleted = 0;
105119
let cursor = 0;

‎apps/server/src/ws.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,3 +1091,38 @@ it.effect.each(["thread", "shell"] as const)(
10911091
assert.deepEqual(Exit.isFailure(exit) ? Cause.squash(exit.cause) : exit, "snapshot");
10921092
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
10931093
);
1094+
1095+
// A thread idle since before the boundary missed nothing; resuming must not resend its body.
1096+
it.effect("thread resume at its own high-water below the pruned boundary skips the snapshot", () =>
1097+
Effect.gen(function* () {
1098+
const sql = yield* SqlClient.SqlClient;
1099+
const now = "2026-10-13T00:00:00.000Z";
1100+
const threadId = ThreadId.make("thread:idle");
1101+
const [last] = yield* sql<{ sequence: number }>`
1102+
INSERT INTO orchestration_events (
1103+
event_id, aggregate_kind, stream_id, stream_version, event_type,
1104+
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
1105+
) VALUES (
1106+
'idle:0', 'thread', ${threadId}, 0, 'provider-session.detached',
1107+
${now}, 'server', ${JSON.stringify({ providerSessionId: "session:idle", detachedAt: now })},
1108+
'{}', 2
1109+
) RETURNING sequence
1110+
`;
1111+
yield* sql`
1112+
INSERT INTO orchestration_v2_projection_metadata (
1113+
projection_name, schema_version, last_sequence, updated_at
1114+
) VALUES (${OrchestrationEventStore.EVENT_LOG_PRUNED_THROUGH}, 2, ${last!.sequence + 10}, ${now})
1115+
`;
1116+
const exit = yield* subscribeOrchestrationV2Thread({
1117+
threadId,
1118+
afterSequence: last!.sequence,
1119+
}).pipe(
1120+
Effect.provideService(ThreadManagement.ThreadManagementService, {
1121+
ensureLegacyTranscript: () => Effect.void,
1122+
getThreadSnapshot: () => Effect.die("snapshot"),
1123+
} as unknown as ThreadManagement.ThreadManagementService["Service"]),
1124+
Effect.exit,
1125+
);
1126+
assert.notDeepEqual(Exit.isFailure(exit) ? Cause.squash(exit.cause) : "resumed", "snapshot");
1127+
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
1128+
);

‎apps/server/src/ws.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,11 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
982982
}),
983983
),
984984
);
985-
if (input.afterSequence > highWater || input.afterSequence < prunedThrough) {
985+
// An idle thread's cursor sits at its own high-water: nothing was missed, so replay nothing.
986+
if (
987+
input.afterSequence > highWater ||
988+
(input.afterSequence < highWater && input.afterSequence < prunedThrough)
989+
) {
986990
return yield* snapshotThenLive();
987991
}
988992
const stats = yield* applicationEvents

‎docs/internals/overview.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ command-id retries.
6363
[ProjectionMaintenance](../../apps/server/src/orchestration-v2/ProjectionMaintenance.ts) deletes
6464
older events the projections cover, so projections cannot be rebuilt from the log. It keeps each
6565
stream's newest event (high-water marks, stream versions, snapshot sequences), `thread.created`
66-
(the legacy importer's marker), the first `running` update of each run attempt (Codex usage
67-
history), and prompt anchors that retained subagent rows reference. A client cursor below the
68-
recorded pruned-through sequence resumes from a snapshot. Command receipts stay for idempotency; a
66+
(the legacy importer's marker), every `running` run update (Codex usage history reads the first
67+
of each attempt), and prompt anchors that retained subagent rows reference. A client whose cursor
68+
is below both its thread's newest event and the recorded pruned-through sequence missed pruned
69+
events, so it resumes from a snapshot; an idle thread's cursor resumes without one. Command receipts stay for idempotency; a
6970
retry older than the retained history can find its events gone and must answer from projections.
7071

7172
Retained events must remain decodable on replay. Changing a schema affects old environments at

0 commit comments

Comments
 (0)