Skip to content

Commit ed4ff90

Browse files
authored
fix(cron): stop marking silent agentTurn runs as errors after successful tools (#114528)
1 parent a4e524f commit ed4ff90

4 files changed

Lines changed: 159 additions & 4 deletions

File tree

‎src/agents/embedded-agent-runner/run.incomplete-turn.test.ts‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4649,6 +4649,145 @@ describe("runEmbeddedAgent incomplete-turn safety", () => {
46494649
expect(result.meta.livenessState).toBe("working");
46504650
});
46514651

4652+
it("treats reply-optional post-tool empty stops as silent even after side-effecting tools", () => {
4653+
// Regression: a cron agentTurn without a delivery route ran a successful
4654+
// replay-unsafe sessions patch and intentionally sent no final text; the run
4655+
// must finish silent, not as an incomplete-turn error.
4656+
const sideEffectToolAttempt = makeAttemptResult({
4657+
assistantTexts: [],
4658+
toolMetas: [{ toolName: "sessions", meta: "patch archived", replaySafe: false }],
4659+
lastAssistant: {
4660+
role: "assistant",
4661+
stopReason: "stop",
4662+
provider: "openai",
4663+
model: "gpt-5.5",
4664+
content: [{ type: "text", text: "" }],
4665+
} as unknown as EmbeddedRunAttemptResult["lastAssistant"],
4666+
});
4667+
4668+
expect(
4669+
shouldTreatEmptyAssistantReplyAsSilent({
4670+
allowEmptyAssistantReplyAsSilent: true,
4671+
terminalReplyExpectation: "optional",
4672+
payloadCount: 0,
4673+
aborted: false,
4674+
timedOut: false,
4675+
attempt: sideEffectToolAttempt,
4676+
}),
4677+
).toBe(true);
4678+
// A required or unspecified terminal reply keeps the ambiguous-failure path.
4679+
expect(
4680+
shouldTreatEmptyAssistantReplyAsSilent({
4681+
allowEmptyAssistantReplyAsSilent: true,
4682+
terminalReplyExpectation: "required",
4683+
payloadCount: 0,
4684+
aborted: false,
4685+
timedOut: false,
4686+
attempt: sideEffectToolAttempt,
4687+
}),
4688+
).toBe(false);
4689+
expect(
4690+
shouldTreatEmptyAssistantReplyAsSilent({
4691+
allowEmptyAssistantReplyAsSilent: true,
4692+
payloadCount: 0,
4693+
aborted: false,
4694+
timedOut: false,
4695+
attempt: sideEffectToolAttempt,
4696+
}),
4697+
).toBe(false);
4698+
});
4699+
4700+
it("keeps reply-optional runs erroring on real failure states", () => {
4701+
const toolErrorAttempt = makeAttemptResult({
4702+
assistantTexts: [],
4703+
toolMetas: [{ toolName: "sessions", meta: "patch failed", replaySafe: false, isError: true }],
4704+
lastToolError: { toolName: "sessions", error: "patch failed" },
4705+
lastAssistant: {
4706+
role: "assistant",
4707+
stopReason: "stop",
4708+
provider: "openai",
4709+
model: "gpt-5.5",
4710+
content: [{ type: "text", text: "" }],
4711+
} as unknown as EmbeddedRunAttemptResult["lastAssistant"],
4712+
});
4713+
const errorStopAttempt = makeAttemptResult({
4714+
assistantTexts: [],
4715+
toolMetas: [{ toolName: "sessions", meta: "patch archived", replaySafe: false }],
4716+
lastAssistant: {
4717+
role: "assistant",
4718+
stopReason: "error",
4719+
provider: "openai",
4720+
model: "gpt-5.5",
4721+
content: [],
4722+
} as unknown as EmbeddedRunAttemptResult["lastAssistant"],
4723+
});
4724+
4725+
expect(
4726+
shouldTreatEmptyAssistantReplyAsSilent({
4727+
allowEmptyAssistantReplyAsSilent: true,
4728+
terminalReplyExpectation: "optional",
4729+
payloadCount: 0,
4730+
aborted: false,
4731+
timedOut: false,
4732+
attempt: toolErrorAttempt,
4733+
}),
4734+
).toBe(false);
4735+
expect(
4736+
shouldTreatEmptyAssistantReplyAsSilent({
4737+
allowEmptyAssistantReplyAsSilent: true,
4738+
terminalReplyExpectation: "optional",
4739+
payloadCount: 0,
4740+
aborted: false,
4741+
timedOut: false,
4742+
attempt: errorStopAttempt,
4743+
}),
4744+
).toBe(false);
4745+
expect(
4746+
shouldTreatEmptyAssistantReplyAsSilent({
4747+
allowEmptyAssistantReplyAsSilent: true,
4748+
terminalReplyExpectation: "optional",
4749+
payloadCount: 0,
4750+
aborted: true,
4751+
timedOut: false,
4752+
attempt: errorStopAttempt,
4753+
}),
4754+
).toBe(false);
4755+
});
4756+
4757+
it("returns NO_REPLY for reply-optional cron-style runs whose side-effecting tools succeeded", async () => {
4758+
mockedClassifyFailoverReason.mockReturnValue(null);
4759+
mockedRunEmbeddedAttempt.mockResolvedValue(
4760+
makeAttemptResult({
4761+
assistantTexts: [],
4762+
toolMetas: [{ toolName: "sessions", meta: "patch archived", replaySafe: false }],
4763+
itemLifecycle: { startedCount: 1, completedCount: 1, activeCount: 0 },
4764+
lastAssistant: {
4765+
role: "assistant",
4766+
stopReason: "stop",
4767+
provider: "openai",
4768+
model: "gpt-5.5",
4769+
content: [{ type: "text", text: "" }],
4770+
} as unknown as EmbeddedRunAttemptResult["lastAssistant"],
4771+
}),
4772+
);
4773+
4774+
const result = await runEmbeddedAgent({
4775+
...overflowBaseRunParams,
4776+
allowEmptyAssistantReplyAsSilent: true,
4777+
terminalReplyExpectation: "optional",
4778+
provider: "openai",
4779+
model: "gpt-5.5",
4780+
runId: "run-reply-optional-post-tool-silent",
4781+
});
4782+
4783+
expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(1);
4784+
expectNoWarnMessageWith("incomplete turn detected");
4785+
expect(result.payloads).toEqual([{ text: "NO_REPLY" }]);
4786+
expect(result.meta.error).toBeUndefined();
4787+
expect(result.meta.terminalReplyKind).toBe("silent-empty");
4788+
expect(result.meta.livenessState).toBe("working");
4789+
});
4790+
46524791
it("keeps retrying and surfacing clean empty assistant turns without the silence flag", async () => {
46534792
mockedClassifyFailoverReason.mockReturnValue(null);
46544793
mockedRunEmbeddedAttempt.mockResolvedValue(

‎src/agents/embedded-agent-runner/run/attempt-result.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ export function completeEmbeddedAttemptResult(
347347
(silentToolResultReplyPayload ? 1 : 0);
348348
const emptyAssistantReplyIsSilent = shouldTreatEmptyAssistantReplyAsSilent({
349349
allowEmptyAssistantReplyAsSilent: attempt.allowEmptyAssistantReplyAsSilent,
350+
terminalReplyExpectation: attempt.terminalReplyExpectation,
350351
payloadCount: 0,
351352
aborted: terminal.aborted,
352353
timedOut: terminal.timedOut,

‎src/agents/embedded-agent-runner/run/incomplete-turn.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,8 @@ function shouldSkipNonVisibleTurnRetry(params: {
667667
aborted: boolean;
668668
timedOut: boolean;
669669
attempt: IncompleteTurnAttempt;
670+
/** Reply-optional silent classification tolerates committed side effects; retries never can. */
671+
tolerateSideEffects?: boolean;
670672
}): boolean {
671673
return Boolean(
672674
params.aborted ||
@@ -676,20 +678,30 @@ function shouldSkipNonVisibleTurnRetry(params: {
676678
params.attempt.didSendDeterministicApprovalPrompt ||
677679
params.attempt.lastToolError ||
678680
hasAcceptedSessionSpawn(params.attempt.acceptedSessionSpawns) ||
679-
resolveAttemptReplayMetadata(params.attempt).hadPotentialSideEffects,
681+
(params.tolerateSideEffects !== true &&
682+
resolveAttemptReplayMetadata(params.attempt).hadPotentialSideEffects),
680683
);
681684
}
682685

683686
/** Allows configured silent handling for replay-safe empty, reasoning-only, or explicit silent turns. */
684687
export function shouldTreatEmptyAssistantReplyAsSilent(params: {
685688
allowEmptyAssistantReplyAsSilent?: boolean;
686689
onlyExplicitSilentReply?: boolean;
690+
terminalReplyExpectation?: "required" | "optional";
687691
payloadCount: number;
688692
aborted: boolean;
689693
timedOut: boolean;
690694
attempt: IncompleteTurnAttempt;
691695
}): boolean {
692-
if (!params.allowEmptyAssistantReplyAsSilent || shouldSkipNonVisibleTurnRetry(params)) {
696+
// "optional" is the run consumer's declaration that no user-facing reply is
697+
// owed (e.g. cron without a delivery route). Silence after side-effecting
698+
// tools is intentional there; retry is replay-unsafe, so erroring would mark
699+
// successful tool-only runs as failures.
700+
const terminalReplyOptional = params.terminalReplyExpectation === "optional";
701+
if (
702+
!params.allowEmptyAssistantReplyAsSilent ||
703+
shouldSkipNonVisibleTurnRetry({ ...params, tolerateSideEffects: terminalReplyOptional })
704+
) {
693705
return false;
694706
}
695707
if (hasCommittedMessagingToolDeliveryEvidence(params.attempt)) {
@@ -706,9 +718,10 @@ export function shouldTreatEmptyAssistantReplyAsSilent(params: {
706718
if (params.onlyExplicitSilentReply) {
707719
return false;
708720
}
709-
// Post-tool empty stops are ambiguous provider failures, not intentional silence.
710-
// Let the retry/incomplete-turn paths decide whether replay is safe.
721+
// Post-tool empty stops are ambiguous provider failures when a reply is still
722+
// expected; reply-optional runs settle their work in the tools themselves.
711723
if (
724+
!terminalReplyOptional &&
712725
params.attempt.toolMetas.length > 0 &&
713726
isEmptyResponseAssistantTurn({
714727
payloadCount: params.payloadCount,

‎src/agents/embedded-agent-runner/run/terminal-resolution.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export function resolveSettledTurnFinalizationRequest(input: {
9797
: 0;
9898
const emptyAssistantReplyIsSilent = shouldTreatEmptyAssistantReplyAsSilent({
9999
allowEmptyAssistantReplyAsSilent: input.runParams.allowEmptyAssistantReplyAsSilent,
100+
terminalReplyExpectation: input.runParams.terminalReplyExpectation,
100101
onlyExplicitSilentReply: false,
101102
payloadCount,
102103
aborted: terminalAborted,
@@ -206,6 +207,7 @@ export async function resolveEmbeddedRunTerminal(input: {
206207
const settledTurnFinalizationAttempted = input.settledTurnFinalizationAttempted;
207208
const emptyAssistantReplyIsSilent = shouldTreatEmptyAssistantReplyAsSilent({
208209
allowEmptyAssistantReplyAsSilent: runParams.allowEmptyAssistantReplyAsSilent,
210+
terminalReplyExpectation: runParams.terminalReplyExpectation,
209211
onlyExplicitSilentReply: settledTurnFinalizationAttempted,
210212
payloadCount,
211213
aborted: terminalAborted,

0 commit comments

Comments
 (0)