Skip to content

Commit ad0bf37

Browse files
committed
fix(server): retain restart note delivery across steered attempts
1 parent b8c3d46 commit ad0bf37

4 files changed

Lines changed: 66 additions & 24 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ export const layer: Layer.Layer<
953953
run,
954954
projection.runs,
955955
projection.providerTurns,
956+
projection.attempts,
956957
);
957958
const restartCancelledWork = pendingRestartCancelledBackgroundWork({
958959
runs: projection.runs,
@@ -967,9 +968,7 @@ export const layer: Layer.Layer<
967968
.map((candidate) => candidate.id),
968969
),
969970
run,
970-
runAttemptIds: projection.attempts
971-
.filter((candidate) => candidate.runId === run.id)
972-
.map((candidate) => candidate.id),
971+
attempts: projection.attempts,
973972
});
974973
const restartNote =
975974
restartCancelledWork.length === 0

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ it("keeps the note for the provider thread that lost the work across a provider
5454
providerTurns: runs.filter((source) => source.id !== target.id).map(turnFor),
5555
compactionMessageIds: new Set(),
5656
run: target,
57-
runAttemptIds: target.activeAttemptId === null ? [] : [target.activeAttemptId],
57+
attempts: [{ id: target.activeAttemptId!, runId: target.id }],
5858
});
5959

6060
// Codex never lost the work, so it neither receives nor consumes the note.
@@ -83,7 +83,7 @@ it("delivers a resumed queued run's note even after a higher-ordinal run", () =>
8383
providerTurns: [resumed, ranFirst].map(turnFor),
8484
compactionMessageIds: new Set(),
8585
run: next,
86-
runAttemptIds: next.activeAttemptId === null ? [] : [next.activeAttemptId],
86+
attempts: [{ id: next.activeAttemptId!, runId: next.id }],
8787
}),
8888
lost,
8989
);
@@ -150,7 +150,7 @@ it("does not repeat the note when a steer restarts the run on a new attempt", ()
150150
],
151151
compactionMessageIds: new Set(),
152152
run: steered,
153-
runAttemptIds,
153+
attempts: runAttemptIds.map((id) => ({ id, runId: steered.id })),
154154
});
155155

156156
// The first attempt reached the provider with the note; its replacement must not repeat it.
@@ -164,12 +164,13 @@ it("counts a prompted mid-turn or chained continuation as delivering the note",
164164
const cutTurn = { ...turnFor(cut), status: "cancelled" as const };
165165
const continuation = run(2, claudeThread, { restartContinuationOfRunId: cut.id });
166166
// A turn cut mid-way that lost work is prompted with the note, not resumed natively.
167-
assert.isTrue(isRestartNoteContinuation(continuation, [cut, continuation], [cutTurn]));
167+
assert.isTrue(isRestartNoteContinuation(continuation, [cut, continuation], [cutTurn], []));
168168
assert.isFalse(
169169
isRestartNoteContinuation(
170170
continuation,
171171
[{ ...cut, restartCancelledBackgroundWork: [] }, continuation],
172172
[cutTurn],
173+
[],
173174
),
174175
);
175176
const later = run(3, claudeThread);
@@ -179,7 +180,7 @@ it("counts a prompted mid-turn or chained continuation as delivering the note",
179180
providerTurns: [cutTurn, ...(delivered ? [turnFor(continuation)] : [])],
180181
compactionMessageIds: new Set(),
181182
run: later,
182-
runAttemptIds: [later.activeAttemptId!],
183+
attempts: [{ id: later.activeAttemptId!, runId: later.id }],
183184
});
184185
assert.deepEqual(pending(true), []);
185186
// A continuation that never reached the provider delivered nothing.
@@ -188,16 +189,16 @@ it("counts a prompted mid-turn or chained continuation as delivering the note",
188189
// Its own continuation carries the original note forward.
189190
const unstarted = { ...continuation, status: "cancelled" as const };
190191
const chained = run(3, claudeThread, { restartContinuationOfRunId: unstarted.id });
191-
assert.deepEqual(restartContinuationNote(unstarted, [cut, unstarted], [cutTurn]), {
192+
assert.deepEqual(restartContinuationNote(unstarted, [cut, unstarted], [cutTurn], []), {
192193
work: lost,
193194
settled: false,
194195
});
195-
assert.isTrue(isRestartNoteContinuation(chained, [cut, unstarted, chained], [cutTurn]));
196+
assert.isTrue(isRestartNoteContinuation(chained, [cut, unstarted, chained], [cutTurn], []));
196197
// Claude announces running before accepting the prompt. A second restart
197198
// can cancel that turn without delivering anything to the provider.
198199
for (const status of ["running", "cancelled"] as const) {
199200
const turns = [cutTurn, { ...turnFor(unstarted), status }];
200-
assert.deepEqual(restartContinuationNote(unstarted, [cut, unstarted], turns), {
201+
assert.deepEqual(restartContinuationNote(unstarted, [cut, unstarted], turns, []), {
201202
work: lost,
202203
settled: false,
203204
});
@@ -207,9 +208,30 @@ it("counts a prompted mid-turn or chained continuation as delivering the note",
207208
providerTurns: turns,
208209
compactionMessageIds: new Set(),
209210
run: later,
210-
runAttemptIds: [later.activeAttemptId!],
211+
attempts: [{ id: later.activeAttemptId!, runId: later.id }],
211212
}),
212213
lost,
213214
);
214215
}
216+
217+
// A steer replaced the attempt after its completed turn delivered the note.
218+
const steered = { ...unstarted, activeAttemptId: RunAttemptId.make("attempt:replacement") };
219+
const attempts = [{ id: unstarted.activeAttemptId!, runId: steered.id }];
220+
const turns = [
221+
cutTurn,
222+
turnFor(unstarted),
223+
{ ...turnFor(steered), status: "cancelled" as const },
224+
];
225+
assert.deepEqual(restartContinuationNote(steered, [cut, steered], turns, attempts).work, []);
226+
assert.isFalse(isRestartNoteContinuation(chained, [cut, steered, chained], turns, attempts));
227+
assert.deepEqual(
228+
pendingRestartCancelledBackgroundWork({
229+
runs: [cut, steered, later],
230+
providerTurns: turns,
231+
compactionMessageIds: new Set(),
232+
run: later,
233+
attempts,
234+
}),
235+
[],
236+
);
215237
});

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import type {
33
OrchestrationV2ProviderTurn,
44
OrchestrationV2RestartCancelledBackgroundWork,
55
OrchestrationV2Run,
6+
OrchestrationV2RunAttempt,
67
OrchestrationV2TurnItem,
78
} from "@t3tools/contracts";
89
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
910

1011
type Work = OrchestrationV2RestartCancelledBackgroundWork;
12+
type Attempt = Pick<OrchestrationV2RunAttempt, "id" | "runId">;
1113

1214
const MAX_LABEL_LENGTH = 160;
1315

@@ -132,15 +134,21 @@ export function restartContinuationNote(
132134
source: OrchestrationV2Run,
133135
runs: ReadonlyArray<OrchestrationV2Run>,
134136
providerTurns: ReadonlyArray<Pick<ProviderTurnState, "runAttemptId" | "status">>,
137+
attempts: ReadonlyArray<Attempt>,
135138
): { readonly work: ReadonlyArray<Work>; readonly settled: boolean } {
139+
const completedAttempts = new Set(
140+
providerTurns.filter((turn) => turn.status === "completed").map((turn) => turn.runAttemptId),
141+
);
142+
const completedRuns = new Set(
143+
attempts.filter((attempt) => completedAttempts.has(attempt.id)).map((attempt) => attempt.runId),
144+
);
136145
let current = source;
137146
let work = current.restartCancelledBackgroundWork ?? [];
138147
const visited = new Set([current.id]);
139148
while (
140149
current.restartContinuationOfRunId !== undefined &&
141-
!providerTurns.some(
142-
(turn) => turn.runAttemptId === current.activeAttemptId && turn.status === "completed",
143-
)
150+
!completedRuns.has(current.id) &&
151+
!(current.activeAttemptId !== null && completedAttempts.has(current.activeAttemptId))
144152
) {
145153
const previous = runs.find((candidate) => candidate.id === current.restartContinuationOfRunId);
146154
if (previous === undefined || visited.has(previous.id)) break;
@@ -160,13 +168,15 @@ export function isRestartNoteContinuation(
160168
run: Pick<OrchestrationV2Run, "restartContinuationOfRunId">,
161169
runs: ReadonlyArray<OrchestrationV2Run>,
162170
providerTurns: ReadonlyArray<Pick<ProviderTurnState, "runAttemptId" | "status">>,
171+
attempts: ReadonlyArray<Attempt>,
163172
): boolean {
164173
const source =
165174
run.restartContinuationOfRunId === undefined
166175
? undefined
167176
: runs.find((candidate) => candidate.id === run.restartContinuationOfRunId);
168177
return (
169-
source !== undefined && restartContinuationNote(source, runs, providerTurns).work.length > 0
178+
source !== undefined &&
179+
restartContinuationNote(source, runs, providerTurns, attempts).work.length > 0
170180
);
171181
}
172182

@@ -193,8 +203,8 @@ export function pendingRestartCancelledBackgroundWork(input: {
193203
| "restartContinuationOfRunId"
194204
| "activeAttemptId"
195205
>;
196-
/** Every attempt id of `run`; a steer replaces the attempt but not the run. */
197-
readonly runAttemptIds: ReadonlyArray<OrchestrationV2Run["activeAttemptId"] & string>;
206+
/** Include earlier attempts: a steer replaces the attempt but not the run. */
207+
readonly attempts: ReadonlyArray<Attempt>;
198208
}): ReadonlyArray<Work> {
199209
const isCompaction = (run: typeof input.run) => input.compactionMessageIds.has(run.userMessageId);
200210
// The current run prepends the note unless it is a compaction or a
@@ -217,15 +227,21 @@ export function pendingRestartCancelledBackgroundWork(input: {
217227
candidate.id !== input.run.id &&
218228
candidate.activeAttemptId !== null &&
219229
candidate.status !== "rolled_back" &&
220-
deliveredAttemptIds.has(candidate.activeAttemptId) &&
230+
(deliveredAttemptIds.has(candidate.activeAttemptId) ||
231+
input.attempts.some(
232+
(attempt) => attempt.runId === candidate.id && deliveredAttemptIds.has(attempt.id),
233+
)) &&
221234
!isCompaction(candidate) &&
222235
(candidate.restartContinuationOfRunId === undefined ||
223-
isRestartNoteContinuation(candidate, input.runs, input.providerTurns)),
236+
isRestartNoteContinuation(candidate, input.runs, input.providerTurns, input.attempts)),
224237
);
225238
// A steer restarts this run on a new attempt: an earlier attempt that already
226239
// reached the provider delivered the note, so the replacement must not repeat it.
227-
const alreadyDelivered = input.runAttemptIds.some(
228-
(attemptId) => attemptId !== input.run.activeAttemptId && deliveredAttemptIds.has(attemptId),
240+
const alreadyDelivered = input.attempts.some(
241+
(attempt) =>
242+
attempt.runId === input.run.id &&
243+
attempt.id !== input.run.activeAttemptId &&
244+
deliveredAttemptIds.has(attempt.id),
229245
);
230246
if (alreadyDelivered) return [];
231247
return sameThread

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const continueRestartedRun = Effect.fn("RestartContinuation.continueResta
107107
const messageId = MessageId.make(`message:restart-continuation:${input.sourceRunId}`);
108108
const projection = yield* threads.getThreadRecords(
109109
input.threadId,
110-
["messages", "runs", "providerTurns"],
110+
["messages", "runs", "providerTurns", "attempts"],
111111
{ messageIds: [messageId] },
112112
);
113113
if (
@@ -152,7 +152,12 @@ export const continueRestartedRun = Effect.fn("RestartContinuation.continueResta
152152
(message) => message.id === source.userMessageId,
153153
);
154154
if (sourceMessage !== undefined && isNativeMaintenanceCommand(sourceMessage)) return;
155-
const note = restartContinuationNote(source, projection.runs, projection.providerTurns);
155+
const note = restartContinuationNote(
156+
source,
157+
projection.runs,
158+
projection.providerTurns,
159+
projection.attempts,
160+
);
156161
const noteText =
157162
note.work.length === 0 ? undefined : restartCancelledBackgroundWorkNote(note.work);
158163
yield* threads.dispatch({

0 commit comments

Comments
 (0)