@devries-cameron20 / t3code Mirror · read-only / commits / cdeacd547a
Fork 0

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

perf(server): turn start reads a long thread's restart-note history once (#39241)

Julius Marminge committed Nov 19, 2026 at 13:20 UTC cdeacd547afac554a12bbdc7eb122a7bcec4c395
3 files changed +126 -77
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+28 -28
@@ -1005,15 +1005,16 @@ export const layer: Layer.Layer<
1005 const restartCancelledWork = pendingRestartCancelledBackgroundWork({
1006 runs: projection.runs,
1007 providerTurns: projection.providerTurns,
1008 - compactionMessageIds: new Set(
1009 - projection.messages
1010 - .filter(
1011 - (candidate) =>
1012 - isCompactionCommandMessage(candidate) ||
1013 - (goalDriver && parseGoalCommand(candidate) !== null),
1014 - )
1015 - .map((candidate) => candidate.id),
1016 - ),
1008 + compactionMessageIds: () =>
1009 + new Set(
1010 + projection.messages
1011 + .filter(
1012 + (candidate) =>
1013 + isCompactionCommandMessage(candidate) ||
1014 + (goalDriver && parseGoalCommand(candidate) !== null),
1015 + )
1016 + .map((candidate) => candidate.id),
1017 + ),
1018 run,
1019 attempts: projection.attempts,
1020 });
@@ -1213,27 +1214,26 @@ export const layer: Layer.Layer<
1214 // History delivery (inline or injected) is text only, so missed images ride on prompts
1215 // until a later run's is accepted. Compaction and /goal starts carry none. A missed run's
1216 // own accepted attempt predates its unsent steering input, so it does not count.
1216 - const lastPromptOrdinal = Math.max(
1217 - 0,
1218 - ...acceptedAttempts.flatMap((attempt) => {
1219 - if (missedRunIds.has(attempt.runId)) return [];
1220 - const source = projection.runs.find((candidate) => candidate.id === attempt.runId);
1221 - const message = projection.messages.find(
1222 - (candidate) => candidate.id === source?.userMessageId,
1223 - );
1224 - return source === undefined ||
1225 - (message !== undefined &&
1226 - (isCompactionCommandMessage(message) ||
1227 - (goalDriver && parseGoalCommand(message) !== null)))
1228 - ? []
1229 - : [source.ordinal];
1230 - }),
1231 - );
1217 + const runsById = new Map(projection.runs.map((source) => [source.id, source]));
1218 + const messagesById = new Map(projection.messages.map((message) => [message.id, message]));
1219 + let lastPromptOrdinal = 0;
1220 + for (const attempt of acceptedAttempts) {
1221 + if (missedRunIds.has(attempt.runId)) continue;
1222 + const source = runsById.get(attempt.runId);
1223 + if (source === undefined) continue;
1224 + const message = messagesById.get(source.userMessageId);
1225 + if (
1226 + message !== undefined &&
1227 + (isCompactionCommandMessage(message) ||
1228 + (goalDriver && parseGoalCommand(message) !== null))
1229 + )
1230 + continue;
1231 + lastPromptOrdinal = Math.max(lastPromptOrdinal, source.ordinal);
1232 + }
1233 const attachments = missedHistory.flatMap((item) =>
1234 item.type === "user_message" &&
1234 - missedRuns.some(
1235 - (source) => source.id === item.runId && source.ordinal > lastPromptOrdinal,
1236 - )
1235 + item.runId !== null &&
1236 + (runsById.get(item.runId)?.ordinal ?? 0) > lastPromptOrdinal
1237 ? item.attachments
1238 : [],
1239 );
apps/server/src/orchestration-v2/RestartBackgroundNote.test.ts
+6 -6
@@ -52,7 +52,7 @@ it("keeps the note for the provider thread that lost the work across a provider
52 pendingRestartCancelledBackgroundWork({
53 runs,
54 providerTurns: runs.filter((source) => source.id !== target.id).map(turnFor),
55 - compactionMessageIds: new Set(),
55 + compactionMessageIds: () => new Set(),
56 run: target,
57 attempts: [{ id: target.activeAttemptId!, runId: target.id }],
58 });
@@ -77,7 +77,7 @@ it("delivers a resumed queued run's note even after a higher-ordinal run", () =>
77 pendingRestartCancelledBackgroundWork({
78 runs,
79 providerTurns: [resumed, ranFirst].map(turnFor),
80 - compactionMessageIds: new Set(),
80 + compactionMessageIds: () => new Set(),
81 run: next,
82 attempts: [{ id: next.activeAttemptId!, runId: next.id }],
83 }),
@@ -198,7 +198,7 @@ it("does not repeat the note when a steer restarts the run on a new attempt", ()
198 ]
199 : []),
200 ],
201 - compactionMessageIds: new Set(),
201 + compactionMessageIds: () => new Set(),
202 run: steered,
203 attempts: runAttemptIds.map((id) => ({ id, runId: steered.id })),
204 });
@@ -225,7 +225,7 @@ it("counts a prompted mid-turn or chained continuation as delivering the note",
225 pendingRestartCancelledBackgroundWork({
226 runs: [cut, continuation, later],
227 providerTurns: [cutTurn, ...(delivered ? [turnFor(continuation)] : [])],
228 - compactionMessageIds: new Set(),
228 + compactionMessageIds: () => new Set(),
229 run: later,
230 attempts: [{ id: later.activeAttemptId!, runId: later.id }],
231 });
@@ -249,7 +249,7 @@ it("counts a prompted mid-turn or chained continuation as delivering the note",
249 pendingRestartCancelledBackgroundWork({
250 runs: [cut, unstarted, later],
251 providerTurns: turns,
252 - compactionMessageIds: new Set(),
252 + compactionMessageIds: () => new Set(),
253 run: later,
254 attempts: [{ id: later.activeAttemptId!, runId: later.id }],
255 }),
@@ -270,7 +270,7 @@ it("counts a prompted mid-turn or chained continuation as delivering the note",
270 pendingRestartCancelledBackgroundWork({
271 runs: [cut, steered, later],
272 providerTurns: turns,
273 - compactionMessageIds: new Set(),
273 + compactionMessageIds: () => new Set(),
274 run: later,
275 attempts,
276 }),
apps/server/src/orchestration-v2/RestartBackgroundNote.ts
+92 -43
@@ -151,6 +151,15 @@ type ProviderTurnState = Pick<
151 export function isRestartNoteSource(
152 source: Pick<OrchestrationV2Run, "status" | "activeAttemptId" | "restartCancelledBackgroundWork">,
153 providerTurns: ReadonlyArray<Pick<ProviderTurnState, "runAttemptId" | "status">>,
154 +): boolean {
155 + return isNoteSource(source, (attemptId) =>
156 + providerTurns.some((turn) => turn.runAttemptId === attemptId && turn.status === "cancelled"),
157 + );
158 +}
159 +
160 +function isNoteSource(
161 + source: Pick<OrchestrationV2Run, "status" | "activeAttemptId" | "restartCancelledBackgroundWork">,
162 + attemptCancelled: (attemptId: OrchestrationV2Run["activeAttemptId"]) => boolean,
163 ): boolean {
164 return (
165 // Failed and interrupted turns never continue; recovery-cancelled waiting runs still qualify.
@@ -158,9 +167,7 @@ export function isRestartNoteSource(
167 source.status === "waiting" ||
168 source.status === "cancelled") &&
169 (source.restartCancelledBackgroundWork?.length ?? 0) > 0 &&
161 - !providerTurns.some(
162 - (turn) => turn.runAttemptId === source.activeAttemptId && turn.status === "cancelled",
163 - )
170 + !attemptCancelled(source.activeAttemptId)
171 );
172 }
173
@@ -326,6 +333,28 @@ export function isRestartContinuationSource(
333 : source.status === "cancelled";
334 }
335
336 +/** Built once per call so long threads are read a bounded number of times, not once per run. */
337 +function continuationLookup(
338 + runs: ReadonlyArray<OrchestrationV2Run>,
339 + providerTurns: ReadonlyArray<Pick<ProviderTurnState, "runAttemptId" | "status">>,
340 + attempts: ReadonlyArray<Attempt>,
341 +) {
342 + const runsById = new Map<string, OrchestrationV2Run>();
343 + for (const run of runs) if (!runsById.has(run.id)) runsById.set(run.id, run);
344 + const completedAttempts = new Set<string | null>();
345 + const cancelledAttempts = new Set<string | null>();
346 + for (const turn of providerTurns) {
347 + if (turn.status === "completed") completedAttempts.add(turn.runAttemptId);
348 + else if (turn.status === "cancelled") cancelledAttempts.add(turn.runAttemptId);
349 + }
350 + const completedRuns = new Set<string>();
351 + for (const attempt of attempts)
352 + if (completedAttempts.has(attempt.id)) completedRuns.add(attempt.runId);
353 + return { runsById, completedAttempts, cancelledAttempts, completedRuns };
354 +}
355 +
356 +type ContinuationLookup = ReturnType<typeof continuationLookup>;
357 +
358 /** Running does not prove prompt acceptance; carry continuation notes until a completed turn proves delivery. */
359 export function restartContinuationNote(
360 source: OrchestrationV2Run,
@@ -333,12 +362,13 @@ export function restartContinuationNote(
362 providerTurns: ReadonlyArray<Pick<ProviderTurnState, "runAttemptId" | "status">>,
363 attempts: ReadonlyArray<Attempt>,
364 ): { readonly work: ReadonlyArray<Work>; readonly settled: boolean } {
336 - const completedAttempts = new Set(
337 - providerTurns.filter((turn) => turn.status === "completed").map((turn) => turn.runAttemptId),
338 - );
339 - const completedRuns = new Set(
340 - attempts.filter((attempt) => completedAttempts.has(attempt.id)).map((attempt) => attempt.runId),
341 - );
365 + return continuationNote(source, continuationLookup(runs, providerTurns, attempts));
366 +}
367 +
368 +function continuationNote(
369 + source: OrchestrationV2Run,
370 + { runsById, completedAttempts, cancelledAttempts, completedRuns }: ContinuationLookup,
371 +): { readonly work: ReadonlyArray<Work>; readonly settled: boolean } {
372 let current = source;
373 let work = current.restartCancelledBackgroundWork ?? [];
374 const visited = new Set([current.id]);
@@ -347,13 +377,16 @@ export function restartContinuationNote(
377 !completedRuns.has(current.id) &&
378 !(current.activeAttemptId !== null && completedAttempts.has(current.activeAttemptId))
379 ) {
350 - const previous = runs.find((candidate) => candidate.id === current.restartContinuationOfRunId);
380 + const previous = runsById.get(current.restartContinuationOfRunId);
381 if (previous === undefined || visited.has(previous.id)) break;
382 visited.add(previous.id);
383 current = previous;
384 work = mergeRestartCancelledBackgroundWork(current.restartCancelledBackgroundWork ?? [], work);
385 }
356 - return { work, settled: isRestartNoteSource(current, providerTurns) };
386 + return {
387 + work,
388 + settled: isNoteSource(current, (attemptId) => cancelledAttempts.has(attemptId)),
389 + };
390 }
391
392 export function isRestartNoteContinuation(
@@ -361,22 +394,30 @@ export function isRestartNoteContinuation(
394 runs: ReadonlyArray<OrchestrationV2Run>,
395 providerTurns: ReadonlyArray<Pick<ProviderTurnState, "runAttemptId" | "status">>,
396 attempts: ReadonlyArray<Attempt>,
397 +): boolean {
398 + return (
399 + run.restartContinuationOfRunId !== undefined &&
400 + isNoteContinuation(run, continuationLookup(runs, providerTurns, attempts))
401 + );
402 +}
403 +
404 +function isNoteContinuation(
405 + run: Pick<OrchestrationV2Run, "restartContinuationOfRunId">,
406 + lookup: ContinuationLookup,
407 ): boolean {
408 const source =
409 run.restartContinuationOfRunId === undefined
410 ? undefined
368 - : runs.find((candidate) => candidate.id === run.restartContinuationOfRunId);
369 - return (
370 - source !== undefined &&
371 - restartContinuationNote(source, runs, providerTurns, attempts).work.length > 0
372 - );
411 + : lookup.runsById.get(run.restartContinuationOfRunId);
412 + return source !== undefined && continuationNote(source, lookup).work.length > 0;
413 }
414
415 /** Derive delivery from later completed turns on the same provider thread, excluding compactions, resumes, and rolled-back runs. */
416 export function pendingRestartCancelledBackgroundWork(input: {
417 readonly runs: ReadonlyArray<OrchestrationV2Run>;
418 readonly providerTurns: ReadonlyArray<ProviderTurnState>;
379 - readonly compactionMessageIds: ReadonlySet<string>;
419 + /** Called only when an earlier run on the provider thread carries restart-note work. */
420 + readonly compactionMessageIds: () => ReadonlySet<string>;
421 readonly run: Pick<
422 OrchestrationV2Run,
423 | "id"
@@ -390,33 +431,24 @@ export function pendingRestartCancelledBackgroundWork(input: {
431
432 readonly attempts: ReadonlyArray<Attempt>;
433 }): ReadonlyArray<Work> {
393 - const isCompaction = (run: typeof input.run) => input.compactionMessageIds.has(run.userMessageId);
394 - if (
395 - input.run.providerThreadId === null ||
396 - input.run.restartContinuationOfRunId !== undefined ||
397 - isCompaction(input.run)
398 - )
434 + if (input.run.providerThreadId === null || input.run.restartContinuationOfRunId !== undefined)
435 return [];
436 const providerThreadId = input.run.providerThreadId;
437 + const sameThread = input.runs.filter((run) => run.providerThreadId === providerThreadId);
438 + const sources = sameThread.filter(
439 + (source) =>
440 + (source.restartCancelledBackgroundWork?.length ?? 0) > 0 && runRanAfter(input.run, source),
441 + );
442 + // Threads whose server never restarted carry no notes; skip every other lookup for them.
443 + if (sources.length === 0) return [];
444 + const compactionMessageIds = input.compactionMessageIds();
445 + const isCompaction = (run: typeof input.run) => compactionMessageIds.has(run.userMessageId);
446 + if (isCompaction(input.run)) return [];
447 const deliveredAttemptIds = new Set(
448 input.providerTurns
449 .filter((turn) => turn.providerThreadId === providerThreadId && turn.status === "completed")
450 .map((turn) => turn.runAttemptId),
451 );
406 - const sameThread = input.runs.filter((run) => run.providerThreadId === providerThreadId);
407 - const prompted = sameThread.filter(
408 - (candidate) =>
409 - candidate.id !== input.run.id &&
410 - candidate.activeAttemptId !== null &&
411 - candidate.status !== "rolled_back" &&
412 - (deliveredAttemptIds.has(candidate.activeAttemptId) ||
413 - input.attempts.some(
414 - (attempt) => attempt.runId === candidate.id && deliveredAttemptIds.has(attempt.id),
415 - )) &&
416 - !isCompaction(candidate) &&
417 - (candidate.restartContinuationOfRunId === undefined ||
418 - isRestartNoteContinuation(candidate, input.runs, input.providerTurns, input.attempts)),
419 - );
452 // Earlier attempts may already have delivered the note; a steer's replacement attempt must not repeat it.
453 const alreadyDelivered = input.attempts.some(
454 (attempt) =>
@@ -425,13 +457,30 @@ export function pendingRestartCancelledBackgroundWork(input: {
457 deliveredAttemptIds.has(attempt.id),
458 );
459 if (alreadyDelivered) return [];
428 - return sameThread
429 - .filter(
430 - (source) =>
431 - runRanAfter(input.run, source) &&
432 - (source.restartCancelledBackgroundWork?.length ?? 0) > 0 &&
433 - !prompted.some((later) => runRanAfter(later, source)),
460 + const deliveredRunIds = new Set<string>();
461 + for (const attempt of input.attempts)
462 + if (deliveredAttemptIds.has(attempt.id)) deliveredRunIds.add(attempt.runId);
463 + let lookup: ContinuationLookup | undefined;
464 + // runRanAfter is a total order, so a source precedes some prompted run iff it precedes the latest one.
465 + let latestPrompted: OrchestrationV2Run | undefined;
466 + for (const candidate of sameThread) {
467 + if (
468 + candidate.id !== input.run.id &&
469 + candidate.activeAttemptId !== null &&
470 + candidate.status !== "rolled_back" &&
471 + (deliveredAttemptIds.has(candidate.activeAttemptId) || deliveredRunIds.has(candidate.id)) &&
472 + !isCompaction(candidate) &&
473 + (candidate.restartContinuationOfRunId === undefined ||
474 + isNoteContinuation(
475 + candidate,
476 + (lookup ??= continuationLookup(input.runs, input.providerTurns, input.attempts)),
477 + )) &&
478 + (latestPrompted === undefined || runRanAfter(candidate, latestPrompted))
479 )
480 + latestPrompted = candidate;
481 + }
482 + return sources
483 + .filter((source) => latestPrompted === undefined || !runRanAfter(latestPrompted, source))
484 .reduce<ReadonlyArray<Work>>(
485 (work, source) =>
486 mergeRestartCancelledBackgroundWork(work, source.restartCancelledBackgroundWork ?? []),