Skip to content

Commit a330061

Browse files
committed
fix(auto-reply): align CLI fallback delivery
1 parent 5b35c34 commit a330061

2 files changed

Lines changed: 88 additions & 15 deletions

File tree

‎src/agents/embedded-agent-runner/run.prepared-harness-source-delivery.integration.test.ts‎

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("prepared harness source delivery", () => {
5050
it.each([
5151
{
5252
name: "delivers one streamed answer when preparation changes tool ownership to automatic",
53-
failsCliPrimary: true,
53+
candidatePath: "cli-failure-embedded" as const,
5454
preliminaryVisibleReplies: "message_tool" as const,
5555
preparedVisibleReplies: "automatic" as const,
5656
expectedTransitions: ["message_tool_only", "automatic"],
@@ -61,7 +61,7 @@ describe("prepared harness source delivery", () => {
6161
},
6262
{
6363
name: "suppresses live output when preparation changes automatic ownership to tool",
64-
failsCliPrimary: false,
64+
candidatePath: "embedded" as const,
6565
preliminaryVisibleReplies: "automatic" as const,
6666
preparedVisibleReplies: "message_tool" as const,
6767
expectedTransitions: ["message_tool_only"],
@@ -72,7 +72,7 @@ describe("prepared harness source delivery", () => {
7272
},
7373
{
7474
name: "lets implicit built-in automatic ownership yield to a prepared tool owner",
75-
failsCliPrimary: false,
75+
candidatePath: "embedded" as const,
7676
preliminaryVisibleReplies: undefined,
7777
preparedVisibleReplies: "message_tool" as const,
7878
expectedTransitions: ["message_tool_only"],
@@ -83,15 +83,37 @@ describe("prepared harness source delivery", () => {
8383
},
8484
{
8585
name: "keeps prepared tool ownership after a failed CLI primary",
86-
failsCliPrimary: true,
86+
candidatePath: "cli-failure-embedded" as const,
8787
preliminaryVisibleReplies: "automatic" as const,
8888
preparedVisibleReplies: "message_tool" as const,
89-
expectedTransitions: ["message_tool_only", "message_tool_only"],
89+
expectedTransitions: ["automatic", "message_tool_only"],
9090
expectedDeliveries: 0,
9191
expectedPartials: 0,
9292
expectedBlocks: 0,
9393
expectedFinals: 0,
9494
},
95+
{
96+
name: "delivers a successful direct CLI reply with its session-stable ownership",
97+
candidatePath: "cli" as const,
98+
preliminaryVisibleReplies: undefined,
99+
preparedVisibleReplies: "automatic" as const,
100+
expectedTransitions: ["automatic"],
101+
expectedDeliveries: 1,
102+
expectedPartials: 0,
103+
expectedBlocks: 0,
104+
expectedFinals: 1,
105+
},
106+
{
107+
name: "delivers a successful API-to-CLI fallback with its session-stable ownership",
108+
candidatePath: "embedded-failure-cli" as const,
109+
preliminaryVisibleReplies: undefined,
110+
preparedVisibleReplies: "automatic" as const,
111+
expectedTransitions: ["automatic", "automatic"],
112+
expectedDeliveries: 1,
113+
expectedPartials: 0,
114+
expectedBlocks: 0,
115+
expectedFinals: 1,
116+
},
95117
])("$name", async (testCase) => {
96118
await useProductionEmbeddedRunExecutionParamsForTest();
97119
const { createBlockReplyDeliveryHandler } = await vi.importActual<
@@ -146,6 +168,9 @@ describe("prepared harness source delivery", () => {
146168
await attemptParams.onBlockReply?.({ text: "Streaming progress" });
147169
return makeAttemptResult({ assistantTexts: ["Short fallback final"] });
148170
});
171+
if (testCase.candidatePath === "embedded-failure-cli") {
172+
mockedRunEmbeddedAttempt.mockRejectedValueOnce(new Error("api primary failed"));
173+
}
149174
useOpenAIPlatformAuthFixture();
150175
let embeddedError: unknown;
151176
let embeddedParams: unknown;
@@ -161,11 +186,35 @@ describe("prepared harness source delivery", () => {
161186
runnerState.isCliProviderMock.mockImplementation(
162187
(provider: unknown) => provider === "anthropic",
163188
);
164-
runnerState.runCliAgentMock.mockRejectedValueOnce(new Error("cli failed"));
189+
if (testCase.candidatePath === "cli-failure-embedded") {
190+
runnerState.runCliAgentMock.mockRejectedValueOnce(new Error("cli failed"));
191+
} else {
192+
runnerState.runCliAgentMock.mockResolvedValue({
193+
payloads: [{ text: "Short fallback final" }],
194+
meta: {},
195+
});
196+
}
165197
runnerState.runWithModelFallbackMock.mockImplementationOnce(
166198
async (params: FallbackRunnerParams) => {
167-
if (testCase.failsCliPrimary) {
168-
await params.run("anthropic", "primary").catch(() => undefined);
199+
if (testCase.candidatePath === "cli-failure-embedded") {
200+
await params.run("anthropic", "cli-primary").catch(() => undefined);
201+
}
202+
if (testCase.candidatePath === "cli") {
203+
return {
204+
result: await params.run("anthropic", "cli-primary"),
205+
provider: "anthropic",
206+
model: "cli-primary",
207+
attempts: [],
208+
};
209+
}
210+
if (testCase.candidatePath === "embedded-failure-cli") {
211+
await params.run("custom", "api-primary").catch(() => undefined);
212+
return {
213+
result: await params.run("anthropic", "cli-fallback"),
214+
provider: "anthropic",
215+
model: "cli-fallback",
216+
attempts: [],
217+
};
169218
}
170219
return {
171220
result: await params.run("custom", "plugin-fallback"),
@@ -249,6 +298,15 @@ describe("prepared harness source delivery", () => {
249298
extraSystemPromptBySourceReplyDeliveryMode[
250299
runtimeOpts.sourceReplyDeliveryMode ?? "automatic"
251300
];
301+
const sessionStableDeliveryMode =
302+
runtimeOpts.sessionPromptSourceReplyDeliveryMode ??
303+
runtimeOpts.sourceReplyDeliveryMode ??
304+
"automatic";
305+
followupRun.run.cliSessionBindingFacts = {
306+
extraSystemPromptStatic:
307+
extraSystemPromptBySourceReplyDeliveryMode[sessionStableDeliveryMode],
308+
sourceReplyDeliveryMode: sessionStableDeliveryMode,
309+
};
252310
const sourceReplyDeliveryRuntime = createSourceReplyDeliveryRuntime({
253311
origin: runtimeOpts.sourceReplyDeliveryModeOrigin ?? "stable_policy",
254312
initialMode: runtimeOpts.sourceReplyDeliveryMode ?? "automatic",
@@ -311,11 +369,17 @@ describe("prepared harness source delivery", () => {
311369
});
312370
await settleReplyDispatcher({ dispatcher });
313371

314-
expect(mockedGlobalHookRunner.runBeforeModelResolve).toHaveBeenCalledWith(
315-
{ prompt: "hello" },
316-
expect.any(Object),
317-
);
318-
expect(emittedStreamingCallbacks).toEqual(["partial", "block"]);
372+
if (testCase.candidatePath === "cli") {
373+
expect(mockedGlobalHookRunner.runBeforeModelResolve).not.toHaveBeenCalled();
374+
} else {
375+
expect(mockedGlobalHookRunner.runBeforeModelResolve).toHaveBeenCalledWith(
376+
{ prompt: "hello" },
377+
expect.any(Object),
378+
);
379+
}
380+
const cliSucceeded =
381+
testCase.candidatePath === "cli" || testCase.candidatePath === "embedded-failure-cli";
382+
expect(emittedStreamingCallbacks).toEqual(cliSucceeded ? [] : ["partial", "block"]);
319383
expect(onPartialReply).toHaveBeenCalledTimes(testCase.expectedPartials);
320384
expect(result.queuedFinal).toBe(testCase.expectedDeliveries === 1);
321385
expect(deliver).toHaveBeenCalledTimes(testCase.expectedDeliveries + testCase.expectedBlocks);
@@ -342,7 +406,14 @@ describe("prepared harness source delivery", () => {
342406
});
343407
expect(dispatcher.getFailedCounts()).toEqual({ tool: 0, block: 0, final: 0 });
344408
expect(modeTransitions).toEqual(testCase.expectedTransitions);
345-
if (testCase.preparedVisibleReplies === "automatic") {
409+
if (cliSucceeded) {
410+
const cliParams = runnerState.runCliAgentMock.mock.calls.at(-1)?.[0] as {
411+
cliSessionBindingFacts?: { sourceReplyDeliveryMode?: string };
412+
sourceReplyDeliveryMode?: string;
413+
};
414+
expect(cliParams.cliSessionBindingFacts?.sourceReplyDeliveryMode).toBe("automatic");
415+
expect(cliParams.sourceReplyDeliveryMode).toBe("automatic");
416+
} else if (testCase.preparedVisibleReplies === "automatic") {
346417
expect(modelVisiblePrompt).toContain("Current-session final text normally routes to source");
347418
expect(modelVisiblePrompt).toContain(
348419
"Your replies are automatically sent to this conversation",

‎src/auto-reply/reply/agent-runner-fallback-candidate.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,11 @@ export async function runAgentFallbackCandidates(params: AgentFallbackCycleParam
194194
);
195195
const candidateRun = runtime.candidateRun;
196196
bindSourceReplyDeliveryRuntime(candidateRun, sourceReplyDeliveryRuntime);
197+
// CLI prompts are fixed to their session binding, so dispatch must publish that
198+
// same stable mode or a valid assistant reply can be silently suppressed.
197199
const candidateSourceReplyDeliveryMode =
198200
sourceReplyDeliveryModeOrigin === "runtime_default" && runtime.useCliExecution
199-
? "message_tool_only"
201+
? (candidateRun.cliSessionBindingFacts?.sourceReplyDeliveryMode ?? "automatic")
200202
: sourceReplyDeliveryRuntime.currentMode;
201203
const applySourceReplyDeliveryModeBeforeInvocation =
202204
sourceReplyDeliveryModeOrigin !== "runtime_default" || runtime.useCliExecution;

0 commit comments

Comments
 (0)