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, 07:15 UTC
fix(server): turn starts no longer rescan every attempt per ended run (#38825)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Navid Nadali committed
Nov 19, 2026 at 05:45 UTC
33d48867f83d3ee81036256b35507392fef501fb
2 files changed
+89
-24
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+56
@@ -2164,6 +2164,62 @@ effectIt.effect(
2164
}),
2165
);
2166
2167
+it("finds abandoned turns in a long history reading each record a bounded number of times", () => {
2168
+ let reads = 0;
2169
+ const counted = <T extends object>(record: T): T =>
2170
+ new Proxy(record, {
2171
+ get: (target, key, receiver) => {
2172
+ reads += 1;
2173
+ return Reflect.get(target, key, receiver);
2174
+ },
2175
+ });
2176
+ const providerThreadId = ProviderThreadId.make("provider-thread-long-history");
2177
+ const endings = ["failed", "interrupted", "cancelled"] as const;
2178
+ const runs = [];
2179
+ const attempts = [];
2180
+ const providerTurns = [];
2181
+ for (let index = 0; index < 2_000; index += 1) {
2182
+ // Every tenth run ended without completing, after a steering restart.
2183
+ const status = index % 10 === 0 ? endings[(index / 10) % endings.length]! : "completed";
2184
+ const attemptIds = status === "completed" ? [`a${index}`] : [`a${index}-steered`, `a${index}`];
2185
+ runs.push({
2186
+ id: `r${index}`,
2187
+ ordinal: index + 1,
2188
+ status,
2189
+ providerThreadId,
2190
+ activeAttemptId: attemptIds.at(-1),
2191
+ });
2192
+ for (const attemptId of attemptIds) {
2193
+ attempts.push({ id: attemptId, runId: `r${index}` });
2194
+ const running = attemptId === "a10-steered" || attemptId === "a20" || attemptId === "a21";
2195
+ providerTurns.push({
2196
+ id: `t-${attemptId}`,
2197
+ providerThreadId,
2198
+ runAttemptId: attemptId,
2199
+ status: running ? "running" : "completed",
2200
+ });
2201
+ }
2202
+ }
2203
+ const projection = {
2204
+ runs: runs.map(counted),
2205
+ attempts: attempts.map(counted),
2206
+ providerTurns: providerTurns.map(counted),
2207
+ } as unknown as Parameters<typeof ProviderTurnStart.abandonedProviderTurns>[0];
2208
+
2209
+ const abandoned = ProviderTurnStart.abandonedProviderTurns(
2210
+ projection,
2211
+ { ordinal: 2_001 },
2212
+ providerThreadId,
2213
+ );
2214
+
2215
+ expect(abandoned.map(({ turn, run }) => [turn.id, run.id])).toEqual([
2216
+ ["t-a10-steered", "r10"],
2217
+ ["t-a20", "r20"],
2218
+ ]);
2219
+ const records = runs.length + attempts.length + providerTurns.length;
2220
+ expect(reads).toBeLessThanOrEqual(4 * records);
2221
+});
2222
+
2223
effectIt.effect("sends new steering text when restarting a manual continuation", () =>
2224
Effect.gen(function* () {
2225
const harness = makeLocalCommandHarness({
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+33
-24
@@ -95,6 +95,38 @@ const NON_COMPLETING_TERMINAL_RUN_STATUSES: ReadonlySet<OrchestrationV2Run["stat
95
"rolled_back",
96
]);
97
98
+export function abandonedProviderTurns(
99
+ projection: Pick<OrchestrationV2ThreadProjection, "runs" | "attempts" | "providerTurns">,
100
+ run: Pick<OrchestrationV2Run, "ordinal">,
101
+ providerThreadId: OrchestrationV2ProviderThread["id"],
102
+) {
103
+ const attemptsByRun = Map.groupBy(projection.attempts, (attempt) => attempt.runId);
104
+ const abandonedRuns = new Map<string | null, OrchestrationV2Run>();
105
+ for (const previous of projection.runs) {
106
+ if (
107
+ previous.ordinal >= run.ordinal ||
108
+ previous.providerThreadId !== providerThreadId ||
109
+ !NON_COMPLETING_TERMINAL_RUN_STATUSES.has(previous.status)
110
+ ) {
111
+ continue;
112
+ }
113
+ if (previous.activeAttemptId !== null) {
114
+ abandonedRuns.set(previous.activeAttemptId, previous);
115
+ }
116
+ for (const attempt of attemptsByRun.get(previous.id) ?? []) {
117
+ abandonedRuns.set(attempt.id, previous);
118
+ }
119
+ }
120
+ return projection.providerTurns.flatMap((turn) => {
121
+ const previous = abandonedRuns.get(turn.runAttemptId);
122
+ return turn.providerThreadId === providerThreadId &&
123
+ turn.status === "running" &&
124
+ previous !== undefined
125
+ ? [{ turn, run: previous }]
126
+ : [];
127
+ });
128
+}
129
+
130
export const isProviderTurnStartError = Schema.is(ProviderTurnStartError);
131
const isProviderAdapterProtocolError = Schema.is(ProviderAdapter.ProviderAdapterProtocolError);
132
const isProviderAdapterOpenSessionError = Schema.is(
@@ -799,30 +831,7 @@ export const layer: Layer.Layer<
831
// running when the agent ignored Stop or an interrupt timed out. Every attempt
832
// of such a run owns a possible abandoned turn, including attempts a steering
833
// restart superseded, and the session cannot be reused until those turns stop.
802
- const abandonedRuns = new Map<string | null, OrchestrationV2Run>();
803
- for (const previous of projection.runs) {
804
- if (
805
- previous.ordinal >= run.ordinal ||
806
- previous.providerThreadId !== providerThread.id ||
807
- !NON_COMPLETING_TERMINAL_RUN_STATUSES.has(previous.status)
808
- ) {
809
- continue;
810
- }
811
- if (previous.activeAttemptId !== null) {
812
- abandonedRuns.set(previous.activeAttemptId, previous);
813
- }
814
- for (const attempt of projection.attempts) {
815
- if (attempt.runId === previous.id) abandonedRuns.set(attempt.id, previous);
816
- }
817
- }
818
- const abandonedTurns = projection.providerTurns.flatMap((turn) => {
819
- const previous = abandonedRuns.get(turn.runAttemptId);
820
- return turn.providerThreadId === providerThread.id &&
821
- turn.status === "running" &&
822
- previous !== undefined
823
- ? [{ turn, run: previous }]
824
- : [];
825
- });
834
+ const abandonedTurns = abandonedProviderTurns(projection, run, providerThread.id);
835
const abandonedTurn = abandonedTurns.at(-1)?.turn;
836
if (abandonedTurn !== undefined) {
837
const liveSession = yield* providerSessions.get(providerSessionId);