The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
fix(server): restart recovery no longer reads every idle thread on a shared session (#32396)
Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Jolymmiles committed
Nov 19, 2026 at 23:03 UTC
800b0748bcd26591c914844e22297175a0cccb59
2 files changed
+78
-22
apps/server/src/orchestration-v2/ProjectionRecovery.test.ts
+40
-7
@@ -763,8 +763,10 @@ it.effect("includes shared sessions and provider-owned background rosters in rec
763
const now = yield* DateTime.now;
764
const first = yield* createThread("shared-first");
765
const second = yield* createThread("shared-second", { archivedAt: now });
766
+ const idleShared = [first, second];
767
+ const sharedWork = yield* createThread("shared-work");
768
const providerSessionId = ProviderSessionId.make("session:recovery:shared");
767
- for (const threadId of [first, second]) {
769
+ for (const threadId of [...idleShared, sharedWork]) {
770
yield* projections.apply({
771
id: EventId.make(`event:${threadId}:session`),
772
type: "provider-session.attached",
@@ -786,6 +788,34 @@ it.effect("includes shared sessions and provider-owned background rosters in rec
788
},
789
});
790
}
791
+ yield* projections.apply({
792
+ id: EventId.make("event:recovery:shared-work"),
793
+ type: "provider-thread.updated",
794
+ threadId: sharedWork,
795
+ driver,
796
+ providerInstanceId,
797
+ occurredAt: now,
798
+ payload: {
799
+ id: ProviderThreadId.make("provider-thread:recovery:shared-work"),
800
+ appThreadId: sharedWork,
801
+ ownerNodeId: null,
802
+ driver,
803
+ providerInstanceId,
804
+ providerSessionId,
805
+ nativeThreadRef: { driver, nativeId: "native:shared-work", strength: "strong" },
806
+ nativeConversationHeadRef: null,
807
+ status: "idle",
808
+ firstRunOrdinal: 1,
809
+ lastRunOrdinal: 1,
810
+ handoffIds: [],
811
+ forkedFrom: null,
812
+ createdAt: now,
813
+ updatedAt: now,
814
+ pendingBackgroundTasks: [
815
+ { taskId: "awaited", description: "Awaited subagent", kind: "subagent" },
816
+ ],
817
+ },
818
+ });
819
const roster = yield* createThread("roster");
820
yield* projections.apply({
821
id: EventId.make("event:recovery:roster"),
@@ -869,13 +899,16 @@ it.effect("includes shared sessions and provider-owned background rosters in rec
899
restartContinuationOfRunId: RunId.make("run:recovery:source"),
900
});
901
assert.deepEqual(
872
- new Set(yield* projections.getRecoveryThreadIds("runtime")),
873
- new Set([first, second, roster, prepared]),
902
+ new Set(yield* projections.getRecoveryThreadIds("restart-continuations")),
903
+ new Set([sharedWork, prepared]),
904
);
905
+ // One idle bound thread stops the shared session; the rest have nothing to recover.
906
+ const runtime = yield* projections.getRecoveryThreadIds("runtime");
907
assert.deepEqual(
876
- new Set(yield* projections.getRecoveryThreadIds("restart-continuations")),
877
- new Set([first, second, prepared]),
908
+ new Set(runtime.filter((threadId) => !idleShared.includes(threadId))),
909
+ new Set([sharedWork, roster, prepared]),
910
);
911
+ assert.equal(runtime.filter((threadId) => idleShared.includes(threadId)).length, 1);
912
assert.deepEqual(
913
yield* projections.getRecoveryThreadIds("restart-continuations", { include: [] }),
914
[],
@@ -892,7 +925,7 @@ it.effect("includes shared sessions and provider-owned background rosters in rec
925
include: ["project:recovery"],
926
}),
927
),
895
- new Set([first, second, prepared]),
928
+ new Set([sharedWork, prepared]),
929
);
930
const preparedState = yield* projections.getRuntimeRecoveryProjection(prepared);
931
assert.deepEqual(
@@ -908,7 +941,7 @@ it.effect("includes shared sessions and provider-owned background rosters in rec
941
`;
942
assert.deepEqual(
943
new Set(yield* projections.getUnreadableThreadIds()),
911
- new Set([first, second]),
944
+ new Set([...idleShared, sharedWork]),
945
);
946
}).pipe(Effect.provide(layerTest)),
947
);
apps/server/src/orchestration-v2/ProjectionStore.ts
+38
-15
@@ -5110,18 +5110,33 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5110
`;
5111
case "restart-continuations":
5112
return sql`
5113
- SELECT bindings.thread_id
5114
- FROM orchestration_v2_projection_provider_sessions AS sessions
5115
- JOIN orchestration_v2_projection_provider_session_bindings AS bindings
5116
- ON bindings.provider_session_id = sessions.provider_session_id
5117
- WHERE sessions.status NOT IN ('stopped', 'error')
5118
- UNION
5119
- SELECT thread_id FROM orchestration_v2_projection_provider_sessions AS sessions
5120
- WHERE status NOT IN ('stopped', 'error') AND thread_id IS NOT NULL
5121
- AND NOT EXISTS (
5122
- SELECT 1 FROM orchestration_v2_projection_provider_session_bindings AS bindings
5123
- WHERE bindings.provider_session_id = sessions.provider_session_id
5124
- )
5113
+ SELECT thread_id FROM (
5114
+ SELECT bindings.thread_id
5115
+ FROM orchestration_v2_projection_provider_sessions AS sessions
5116
+ JOIN orchestration_v2_projection_provider_session_bindings AS bindings
5117
+ ON bindings.provider_session_id = sessions.provider_session_id
5118
+ WHERE sessions.status NOT IN ('stopped', 'error')
5119
+ UNION
5120
+ SELECT thread_id FROM orchestration_v2_projection_provider_sessions AS sessions
5121
+ WHERE status NOT IN ('stopped', 'error') AND thread_id IS NOT NULL
5122
+ AND NOT EXISTS (
5123
+ SELECT 1 FROM orchestration_v2_projection_provider_session_bindings AS bindings
5124
+ WHERE bindings.provider_session_id = sessions.provider_session_id
5125
+ )
5126
+ )
5127
+ -- A live session alone continues nothing: restartContinuationRun
5128
+ -- also needs an active provider thread or open background work.
5129
+ WHERE thread_id IN (
5130
+ SELECT thread_id FROM orchestration_v2_projection_provider_threads
5131
+ WHERE status = 'active'
5132
+ OR CASE WHEN json_valid(payload_json)
5133
+ THEN json_array_length(payload_json, '$.pendingBackgroundTasks') > 0
5134
+ ELSE 0 END
5135
+ UNION
5136
+ SELECT thread_id FROM orchestration_v2_projection_turn_items
5137
+ WHERE type IN ('command_execution', 'dynamic_tool', 'subagent')
5138
+ AND status IN ('pending', 'running', 'waiting')
5139
+ )
5140
UNION
5141
SELECT thread_id FROM orchestration_v2_projection_runs
5142
WHERE status = 'starting'
@@ -5153,10 +5168,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5168
SELECT thread_id FROM orchestration_v2_projection_runtime_requests
5169
WHERE status = 'pending'
5170
UNION
5156
- SELECT bindings.thread_id
5171
+ -- One bound thread stops a session; the other arms select the
5172
+ -- bound threads with work, so idle threads on a shared session stay out.
5173
+ SELECT (
5174
+ SELECT bindings.thread_id
5175
+ FROM orchestration_v2_projection_provider_session_bindings AS bindings
5176
+ JOIN orchestration_v2_projection_threads AS threads
5177
+ ON threads.thread_id = bindings.thread_id
5178
+ WHERE bindings.provider_session_id = sessions.provider_session_id
5179
+ AND threads.deleted_at IS NULL
5180
+ ORDER BY bindings.thread_id LIMIT 1
5181
+ )
5182
FROM orchestration_v2_projection_provider_sessions AS sessions
5158
- CROSS JOIN orchestration_v2_projection_provider_session_bindings AS bindings
5159
- ON bindings.provider_session_id = sessions.provider_session_id
5183
WHERE sessions.status NOT IN ('stopped', 'error')
5184
OR (sessions.status = 'error' AND CASE WHEN json_valid(sessions.payload_json)
5185
THEN json_array_length(sessions.payload_json, '$.extensionStatuses') > 0