Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 55 additions & 18 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,20 +1087,17 @@ export const layer: Layer.Layer<
};
const prepareRetryHandoff = Effect.fn("ProviderTurnStartService.prepareRetryHandoff")(
function* (context: ReturnType<typeof handoffContextForThread>) {
const { missedRunIds, coveredItemIds, missedRuns } = context;
const missedItems =
const { missedRunIds, coveredItemIds, missedRuns, acceptedAttempts } = context;
const missedHistory =
missedRunIds.size === 0
? []
: (yield* projectionStore.getTurnStartHistory(input.threadId, [
...missedRunIds,
])).filter(
(item) =>
item.runId !== null &&
missedRunIds.has(item.runId) &&
!coveredItemIds.has(item.id) &&
historicalMessage(item) !== null,
);
const retryHandoff =
])).filter((item) => item.runId !== null && missedRunIds.has(item.runId));
const missedItems = missedHistory.filter(
(item) => !coveredItemIds.has(item.id) && historicalMessage(item) !== null,
);
const handoffs =
missedItems.length === 0
? []
: [
Expand All @@ -1122,7 +1119,34 @@ export const layer: Layer.Layer<
createdAt: yield* DateTime.now,
}),
];
return retryHandoff;
// History delivery (inline or injected) is text only, so missed images ride on prompts
// until a later run's is accepted. Compaction and /goal starts carry none. A missed run's
// own accepted attempt predates its unsent steering input, so it does not count.
const lastPromptOrdinal = Math.max(
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
0,
...acceptedAttempts.flatMap((attempt) => {
if (missedRunIds.has(attempt.runId)) return [];
const source = projection.runs.find((candidate) => candidate.id === attempt.runId);
const message = projection.messages.find(
(candidate) => candidate.id === source?.userMessageId,
);
return source === undefined ||
(message !== undefined &&
(isCompactionCommandMessage(message) ||
(goalDriver && parseGoalCommand(message) !== null)))
? []
: [source.ordinal];
}),
);
const attachments = missedHistory.flatMap((item) =>
item.type === "user_message" &&
missedRuns.some(
(source) => source.id === item.runId && source.ordinal > lastPromptOrdinal,
)
? item.attachments
: [],
);
return { handoffs, attachments };
},
);
const prepareRecoveryHandoff = Effect.fn("ProviderTurnStartService.prepareRecoveryHandoff")(
Expand Down Expand Up @@ -1181,7 +1205,7 @@ export const layer: Layer.Layer<
...effectiveHandoffs.filter((candidate) => candidate.targetRunId === run.id),
];
};
let resumedRetryHandoff: ReadonlyArray<OrchestrationV2ContextHandoff> | undefined;
let resumedRetryHandoff: Effect.Success<ReturnType<typeof prepareRetryHandoff>> | undefined;
let resumedHandoffContext: ReturnType<typeof handoffContextForThread> | undefined;
let portableForkFallback: OrchestrationV2ContextHandoff | undefined;
let recoveryTurnItem: OrchestrationV2TurnItem | undefined;
Expand Down Expand Up @@ -1421,7 +1445,7 @@ export const layer: Layer.Layer<
resumedHandoffContext = context;
resumedRetryHandoff = yield* prepareRetryHandoff(context);
const fits = yield* deliverContextHandoffs({
handoffs: [...handoffs, ...resumedRetryHandoff],
handoffs: [...handoffs, ...resumedRetryHandoff.handoffs],
providerThread: { ...resumed.success, id: providerThread.id },
budget: context.budget,
deferInline: isCompactionCommandMessage(providerMessage) || goalCommand,
Expand Down Expand Up @@ -1468,7 +1492,7 @@ export const layer: Layer.Layer<
}
}
resumedHandoffContext = undefined;
resumedRetryHandoff = [];
resumedRetryHandoff = { handoffs: [], attachments: [] };
if (!(yield* isCurrentAttemptInStatus("starting"))) return undefined;
// Another client is still writing to the native thread. A fresh
// session here would silently split the conversation from it.
Expand Down Expand Up @@ -1793,7 +1817,7 @@ export const layer: Layer.Layer<
const startWithHandoffs = Effect.fn("ProviderTurnStartService.startWithHandoffs")(
function* (turnInput: Parameters<typeof session.startTurn>[0], compact = false) {
const delivery = yield* deliverContextHandoffs({
handoffs: [...effectiveHandoffs, ...retryHandoff],
handoffs: [...effectiveHandoffs, ...retryHandoff.handoffs],
deferInline: compact || goalCommand,
providerThread: runningProviderThread,
budget,
Expand Down Expand Up @@ -1824,14 +1848,26 @@ export const layer: Layer.Layer<
const startInput = noteContinuation ? promptedInput : turnInput;
const { resume: _resume, ...contextInput } = startInput;
const { message: resumeMessage, resume: _resumeIntent, ...resumeInput } = startInput;
const missedAttachments =
compact || goalCommand
? []
: retryHandoff.attachments.filter(
(missed) =>
!turnInput.message.attachments.some((current) => current.id === missed.id),
);
// Like pending history, pending images need a prompt; native resume carries none.
const prompted = context !== "" || missedAttachments.length > 0;
const operation =
turnInput.resume && context === "" && session.resumeTurn !== undefined
turnInput.resume && !prompted && session.resumeTurn !== undefined
? session.resumeTurn({ ...resumeInput, messageId: resumeMessage.messageId })
: start({
...(context === "" ? startInput : contextInput),
...(prompted ? contextInput : startInput),
message: {
...turnInput.message,
text,
...(missedAttachments.length === 0
? {}
: { attachments: [...missedAttachments, ...turnInput.message.attachments] }),
},
...(text === userText
? {}
Expand Down Expand Up @@ -1892,7 +1928,8 @@ export const layer: Layer.Layer<
);
const deliverySession =
effectiveHandoffs.length === 0 &&
retryHandoff.length === 0 &&
retryHandoff.handoffs.length === 0 &&
retryHandoff.attachments.length === 0 &&
sideChatHandoff === undefined &&
restartNote === "" &&
!noteContinuation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1965,10 +1965,17 @@ describe("orchestration v2 provider switching", () => {
detail: "Claude provider query ended before the turn could start.",
}),
],
] as const)("keeps the native session after turns refused for %s", ([, refusalCause]) =>
// Injected history is text only, so a refusal after injection must not settle the image.
[
"background work after native history injection",
new ClaudeBackgroundWorkBlocksQueryReplacementError(),
true,
],
] as const)("keeps the native session after turns refused for %s", ([, refusalCause, inject]) =>
Effect.gen(function* () {
const cwd = yield* checkpointWorkspace("refused-start-keeps-native");
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
const refuseStarts = yield* Ref.make(0);
const generation = yield* Ref.make(0);
const registry = ProviderAdapterRegistry.layerFromAdapters([
Expand All @@ -1982,11 +1989,19 @@ describe("orchestration v2 provider switching", () => {
refuseStarts,
refusalCause,
nativeThreadGeneration: generation,
...(inject === true ? { injectedHistory } : {}),
}),
]);
yield* Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
const missedImage: ChatAttachment = {
type: "image",
id: "refused-start-image",
name: "screenshot.png",
mimeType: "image/png",
sizeBytes: 100_000,
};
const run = Effect.fn("run")(function* (ordinal: number) {
yield* orchestrator.dispatch({
type: "message.dispatch",
Expand All @@ -1996,7 +2011,7 @@ describe("orchestration v2 provider switching", () => {
createdBy: "user",
creationSource: "web",
text: `Request ${ordinal}`,
attachments: [],
attachments: ordinal === 2 ? [missedImage] : [],
modelSelection: CLAUDE_MODEL_SELECTION,
dispatchMode: { type: "start_immediately" },
});
Expand Down Expand Up @@ -2036,8 +2051,10 @@ describe("orchestration v2 provider switching", () => {
assert.equal(turns.length, 2);
assert.equal(yield* Ref.get(generation), 1);
assert.equal(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId);
assert.include(turns[1]?.text, "Request 2");
assert.include(turns[1]?.text, "Request 3");
const delivered = `${turns[1]?.text}${JSON.stringify(yield* Ref.get(injectedHistory))}`;
assert.include(delivered, "Request 2");
assert.include(delivered, "Request 3");
assert.deepEqual(turns[1]?.attachments, [missedImage]);
}).pipe(
Effect.provide(
ProviderReplayHarness.layerWithRegistry(
Expand Down
Loading