Skip to content

Commit fb88a28

Browse files
fix(auto-reply): carry deliberate silence into dispatch
1 parent 1d8de39 commit fb88a28

7 files changed

Lines changed: 66 additions & 10 deletions

‎src/auto-reply/reply/agent-runner-result-payloads.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export async function prepareReplyAgentPayloads(state: {
9797
verboseEnabled,
9898
} = accounting;
9999
let { activeSessionEntry, didLogHeartbeatStrip } = accounting;
100+
const deliberateSilentTerminalReply = hasDeliberateSilentTerminalReply(runResult);
101+
if (deliberateSilentTerminalReply) {
102+
opts?.onDeliberateSilentTerminalReply?.();
103+
}
100104

101105
const successfulSourceReplyDelivery = hasSuccessfulSourceReplyDelivery({
102106
blockReplyPipeline,
@@ -142,7 +146,7 @@ export async function prepareReplyAgentPayloads(state: {
142146
"message_tool_only",
143147
hasPendingContinuation:
144148
runResult.meta?.yielded === true || (runResult.meta?.pendingToolCalls?.length ?? 0) > 0,
145-
hasExplicitSilentReply: hasDeliberateSilentTerminalReply(runResult),
149+
hasExplicitSilentReply: deliberateSilentTerminalReply,
146150
hasCommittedDelivery: successfulTerminalDelivery,
147151
sessionCtx,
148152
cfg,

‎src/auto-reply/reply/agent-runner.misc.runreplyagent.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,7 @@ describe("runReplyAgent private message_tool_only final warning (#85714)", () =>
33133313
strandedReplyRetry?: boolean;
33143314
sendPolicyDenied?: boolean;
33153315
isHeartbeat?: boolean;
3316+
onDeliberateSilentTerminalReply?: () => void;
33163317
onObservedReplyDelivery?: () => Promise<void> | void;
33173318
replyOperation?: ReturnType<typeof createReplyOperation>;
33183319
turnAdoptionLifecycle?: FollowupRun["turnAdoptionLifecycle"];
@@ -3428,10 +3429,17 @@ describe("runReplyAgent private message_tool_only final warning (#85714)", () =>
34283429
resolvedBlockStreamingBreak: "message_end",
34293430
shouldInjectGroupIntro: false,
34303431
typingMode: "instant",
3431-
...(params.isHeartbeat || params.onObservedReplyDelivery
3432+
...(params.isHeartbeat ||
3433+
params.onDeliberateSilentTerminalReply ||
3434+
params.onObservedReplyDelivery
34323435
? {
34333436
opts: {
34343437
...(params.isHeartbeat ? { isHeartbeat: true } : {}),
3438+
...(params.onDeliberateSilentTerminalReply
3439+
? {
3440+
onDeliberateSilentTerminalReply: params.onDeliberateSilentTerminalReply,
3441+
}
3442+
: {}),
34353443
...(params.onObservedReplyDelivery
34363444
? { onObservedReplyDelivery: params.onObservedReplyDelivery }
34373445
: {}),
@@ -3614,10 +3622,13 @@ describe("runReplyAgent private message_tool_only final warning (#85714)", () =>
36143622
// Assistant went silent (NO_REPLY), but a verbose/usage metadata payload
36153623
// survives in finalPayloads. The warn must key off the assistant text, not
36163624
// the payload bundle, so no private-final warning should fire.
3625+
const onDeliberateSilentTerminalReply = vi.fn();
36173626
await runPrivateFinalCase({
36183627
finalAssistantText: "no_reply",
3628+
onDeliberateSilentTerminalReply,
36193629
payloadText: "Auto-compaction complete (count 1).",
36203630
});
3631+
expect(onDeliberateSilentTerminalReply).toHaveBeenCalledOnce();
36213632
expect(warnPrivateFinalSpy).not.toHaveBeenCalled();
36223633
expect(vi.mocked(enqueueFollowupRun)).not.toHaveBeenCalled();
36233634
});

‎src/auto-reply/reply/dispatch-from-config.events.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { CommandSessionMetadataChange } from "./command-session-metadata.js
33
import type { ReplySessionBinding } from "./get-reply.types.js";
44

55
export type InternalReplyResolverOptions = {
6+
onDeliberateSilentTerminalReply?: () => void;
67
onSessionMetadataChanges?: (changes: CommandSessionMetadataChange[]) => void;
78
onSessionPrepared?: (binding: ReplySessionBinding) => void;
89
};

‎src/auto-reply/reply/dispatch-from-config.execute.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export async function executeDispatch(state: PrepareDispatchExecutionReadyState)
117117
waitForPendingDirectBlockReplyDelivery,
118118
wrapProgressCallback,
119119
} = state;
120+
let deliberateSilentTerminalReply = false;
120121
const replyResult = await runWithDispatchLifecycleAdmission(
121122
async () =>
122123
await runWithDispatchAbortSignal(
@@ -131,6 +132,9 @@ export async function executeDispatch(state: PrepareDispatchExecutionReadyState)
131132
sourceReplyDeliveryMode,
132133
sessionPromptSourceReplyDeliveryMode: sessionStableSourceReplyDeliveryMode,
133134
...({
135+
onDeliberateSilentTerminalReply: () => {
136+
deliberateSilentTerminalReply = true;
137+
},
134138
onSessionMetadataChanges: notifySessionMetadataChanges,
135139
onSessionPrepared: notePreparedSession,
136140
} satisfies InternalReplyResolverOptions),
@@ -650,7 +654,11 @@ export async function executeDispatch(state: PrepareDispatchExecutionReadyState)
650654
}
651655
}
652656
}
653-
const nextState = extendPreparedDispatchState(state, { replyResult }, {});
657+
const nextState = extendPreparedDispatchState(
658+
state,
659+
{ deliberateSilentTerminalReply, replyResult },
660+
{},
661+
);
654662
return { status: "ready" as const, state: nextState };
655663
}
656664

‎src/auto-reply/reply/dispatch-from-config.finalize.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
markReplyPayloadAsTtsSupplement,
99
type ReplyPayload,
1010
} from "../reply-payload.js";
11-
import { isSilentReplyPayloadText } from "../tokens.js";
1211
import { isDispatchReplyOperationAbortedError } from "./dispatch-from-config.abort.js";
1312
import type { ExecuteDispatchReadyState } from "./dispatch-from-config.execute.js";
1413
import {
@@ -35,6 +34,7 @@ export async function finalizeDispatchAndAudit(state: ExecuteDispatchReadyState)
3534
ctx,
3635
deliveryChannel,
3736
deliverySuppressionReason,
37+
deliberateSilentTerminalReply,
3838
dispatcher,
3939
emptyFinalAllowedAsSilent,
4040
explicitCommandTurnCtx,
@@ -69,7 +69,6 @@ export async function finalizeDispatchAndAudit(state: ExecuteDispatchReadyState)
6969
waitForPendingDirectBlockReplyDelivery,
7070
} = state;
7171
const replies = replyResult ? (Array.isArray(replyResult) ? replyResult : [replyResult]) : [];
72-
const hasExplicitSilentReply = replies.some((reply) => isSilentReplyPayloadText(reply.text));
7372
const pendingFinalDelivery = {
7473
storePath: sessionStoreEntry.storePath,
7574
sessionKey: sessionStoreEntry.sessionKey ?? sessionKey,
@@ -293,7 +292,7 @@ export async function finalizeDispatchAndAudit(state: ExecuteDispatchReadyState)
293292
!sendPolicyDenied &&
294293
sourceReplyDeliveryMode !== "message_tool_only" &&
295294
!emptyFinalAllowedAsSilent &&
296-
!hasExplicitSilentReply &&
295+
!deliberateSilentTerminalReply &&
297296
!getObservedReplyDelivery() &&
298297
!replyAcceptedByActiveRun &&
299298
!turnLedger.hasVisibleDelivery() &&
@@ -309,8 +308,8 @@ export async function finalizeDispatchAndAudit(state: ExecuteDispatchReadyState)
309308
}
310309
let counts = dispatcher.getQueuedCounts();
311310
let noVisibleReplyFallbackDelivered = false;
312-
// Visible agent turns with genuinely empty model completions get a core
313-
// fallback final. An explicit NO_REPLY is intentional silence, not emptiness.
311+
// The agent-result classifier owns terminal silence; carry that fact here
312+
// because reply payloads are filtered projections and cannot safely rederive it.
314313
// An aborted or timed-out settle leaves delivery state unknown; admission
315314
// then keeps its legacy trust and the turn ends without a fallback.
316315
if (queuedSettleResult === "settled" && noVisibleReplyFallbackAllowed()) {
@@ -394,7 +393,7 @@ export async function finalizeDispatchAndAudit(state: ExecuteDispatchReadyState)
394393
!getObservedReplyDelivery() &&
395394
!replyAcceptedByActiveRun &&
396395
!emptyFinalAllowedAsSilent &&
397-
!hasExplicitSilentReply
396+
!deliberateSilentTerminalReply
398397
? { noVisibleReplyFallbackEligible: true }
399398
: {}),
400399
...(noVisibleReplyFallbackDelivered ? { noVisibleReplyFallbackDelivered: true } : {}),

‎src/auto-reply/reply/dispatch-from-config.hooks-and-send-policy.test-utils.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
describe1BeforeEach0,
3232
describe2BeforeEach0,
3333
} from "./dispatch-from-config.test-harness.js";
34+
import type { InternalGetReplyOptions } from "./get-reply.types.js";
3435
import { PROVIDER_CONVERSATION_STATE_ERROR_USER_MESSAGE } from "./provider-request-error-classifier.js";
3536
import { createReplyDispatcher } from "./reply-dispatcher.js";
3637
import { resolveReplyOperationRunState } from "./reply-operation-run-state.js";
@@ -470,7 +471,10 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
470471
setNoAbort();
471472
const deliver = vi.fn(async () => {});
472473
const dispatcher = createReplyDispatcher({ deliver });
473-
const replyResolver = vi.fn(async () => ({ text: "NO_REPLY" }) satisfies ReplyPayload);
474+
const replyResolver = vi.fn(async (_ctx: MsgContext, opts?: GetReplyOptions) => {
475+
(opts as InternalGetReplyOptions | undefined)?.onDeliberateSilentTerminalReply?.();
476+
return undefined;
477+
});
474478

475479
const result = await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
476480
dispatcher.markComplete();
@@ -482,6 +486,34 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
482486
expect(result.noVisibleReplyFallbackEligible).toBeUndefined();
483487
});
484488

489+
it("does not infer terminal silence from a sibling NO_REPLY payload", async () => {
490+
setNoAbort();
491+
const deliveredTexts: string[] = [];
492+
const dispatcher = createReplyDispatcher({
493+
deliver: vi.fn(async (payload) => {
494+
deliveredTexts.push(payload.text ?? "");
495+
}),
496+
beforeDeliver: async (payload) =>
497+
payload.text === NO_VISIBLE_REPLY_FALLBACK_TEXT ? payload : null,
498+
});
499+
const replyResolver = vi.fn(
500+
async () => [{ text: "visible reply" }, { text: "NO_REPLY" }] satisfies ReplyPayload[],
501+
);
502+
503+
const result = await dispatchReplyFromConfig({
504+
ctx: buildTestCtx({ ChatType: "direct" }),
505+
cfg: emptyConfig,
506+
dispatcher,
507+
replyResolver,
508+
});
509+
dispatcher.markComplete();
510+
await dispatcher.waitForIdle();
511+
512+
expect(deliveredTexts).not.toContain("visible reply");
513+
expect(deliveredTexts).toContain(NO_VISIBLE_REPLY_FALLBACK_TEXT);
514+
expect(result.noVisibleReplyFallbackDelivered).toBe(true);
515+
});
516+
485517
it("delivers core no-visible-reply fallback for disallowed empty mentioned group turns", async () => {
486518
setNoAbort();
487519
const dispatcher = createDispatcher();

‎src/auto-reply/reply/get-reply.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type ReplySessionBinding = {
1717

1818
type InternalReplySessionOptions = {
1919
expectedExistingSessionId?: string;
20+
onDeliberateSilentTerminalReply?: () => void;
2021
onSessionPrepared?: (binding: ReplySessionBinding) => void;
2122
/** Prevent implicit rollover after a caller has durably admitted this exact session. */
2223
pinExpectedExistingSession?: boolean;

0 commit comments

Comments
 (0)