fix(auto-reply): guard missing dispatcher getFailedCounts without weakening the SDK type (#89318)

Summary:
- Adds defensive failed-count reads in auto-reply/ACP accounting and Feishu fallback paths, plus a focused regression test, while keeping `ReplyDispatcher.getFailedCounts` required.
- PR surface: Source +24, Tests +35. Total +59 across 5 files.
- Reproducibility: yes. from source inspection. Current main calls `dispatcher.getFailedCounts().final` and si ... issing that method follows a clear TypeError path; the source PR also supplied terminal before/after proof.

Automerge notes:
- PR branch already contained follow-up commit before automerge: fix(auto-reply): guard missing dispatcher getFailedCounts without wea…

Validation:
- ClawSweeper review passed for head 0bdfb4adeb.
- Required merge gates passed before the squash merge.

Prepared head SHA: 0bdfb4adeb
Review: https://github.com/openclaw/openclaw/pull/89318#issuecomment-4598624344

Co-authored-by: Alix-007 <li.long15@xydigit.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com>
Co-authored-by: clawsweeper[bot] <274271284+clawsweeper[bot]@users.noreply.github.com>
Approved-by: takhoffman
Co-authored-by: takhoffman <781889+takhoffman@users.noreply.github.com>
This commit is contained in:
clawsweeper[bot]
2026-06-02 04:16:58 +00:00
committed by GitHub
parent e6b5083660
commit 63ed9adfe9
5 changed files with 66 additions and 7 deletions

View File

@@ -1575,7 +1575,7 @@ export async function handleFeishuMessage(params: {
turnResult.dispatched &&
shouldSendNoVisibleReplyFallback({
...turnResult.dispatchResult,
failedCounts: dispatcher.getFailedCounts(),
failedCounts: dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 },
})
) {
await ensureNoVisibleReplyFallback("broadcast-dispatch-complete-no-visible-reply");
@@ -1771,7 +1771,7 @@ export async function handleFeishuMessage(params: {
if (
shouldSendNoVisibleReplyFallback({
...dispatchResult,
failedCounts: dispatcher.getFailedCounts(),
failedCounts: dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 },
})
) {
await ensureNoVisibleReplyFallback("dispatch-complete-no-visible-reply");

View File

@@ -16,6 +16,7 @@ import type { FinalizedMsgContext } from "../templating.js";
import type { ReplyPayload } from "../types.js";
import { waitForReplyDispatcherIdle } from "./reply-dispatcher.js";
import type { ReplyDispatchKind, ReplyDispatcher } from "./reply-dispatcher.types.js";
import { readDispatcherFailedCounts } from "./reply-dispatcher.types.js";
import { resolveRoutedDeliveryThreadId } from "./routed-delivery-thread.js";
const routeReplyRuntimeLoader = createLazyImportLoader(() => import("./route-reply.runtime.js"));
@@ -257,7 +258,7 @@ export function createAcpDispatchDeliveryCoordinator(params: {
state.settledDirectVisibleText = true;
hasPendingDirectBlockReplyDelivery = false;
await params.dispatcher.waitForIdle();
const failedCounts = params.dispatcher.getFailedCounts();
const failedCounts = readDispatcherFailedCounts(params.dispatcher);
const failedVisibleCount = failedCounts.block + failedCounts.final;
if (failedVisibleCount > 0) {
state.failedVisibleTextDelivery = true;

View File

@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";
import { getDispatcherFinalOutcomeCounts } from "./dispatch-from-config.js";
import type { ReplyDispatcher } from "./reply-dispatcher.types.js";
describe("getDispatcherFinalOutcomeCounts (#89116)", () => {
it("returns failed: 0 when the dispatcher does not implement getFailedCounts", () => {
// Some ReplyDispatcher variants omit the optional count methods entirely; the
// previous code called dispatcher.getFailedCounts() unguarded and threw
// "TypeError: dispatcher.getFailedCounts is not a function".
const dispatcher = {
getCancelledCounts: () => ({ tool: 0, block: 0, final: 2 }),
// getFailedCounts intentionally absent
} as unknown as ReplyDispatcher;
expect(() => getDispatcherFinalOutcomeCounts(dispatcher)).not.toThrow();
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 2, failed: 0 });
});
it("returns cancelled: 0 when getCancelledCounts is absent (existing behavior preserved)", () => {
const dispatcher = {
getFailedCounts: () => ({ tool: 0, block: 1, final: 3 }),
} as unknown as ReplyDispatcher;
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 0, failed: 3 });
});
it("uses the real final counts when both methods are present", () => {
const dispatcher = {
getCancelledCounts: () => ({ tool: 0, block: 0, final: 1 }),
getFailedCounts: () => ({ tool: 0, block: 0, final: 5 }),
} as unknown as ReplyDispatcher;
expect(getDispatcherFinalOutcomeCounts(dispatcher)).toEqual({ cancelled: 1, failed: 5 });
});
});

View File

@@ -132,7 +132,12 @@ import { withFullRuntimeReplyConfig } from "./get-reply-fast-path.js";
import { claimInboundDedupe, commitInboundDedupe, releaseInboundDedupe } from "./inbound-dedupe.js";
import { resolveOriginMessageProvider } from "./origin-routing.js";
import { waitForReplyDispatcherIdle } from "./reply-dispatcher.js";
import type { ReplyDispatchKind, ReplyDispatcher } from "./reply-dispatcher.types.js";
import type {
DispatcherOutcomeCountsView,
ReplyDispatchKind,
ReplyDispatcher,
} from "./reply-dispatcher.types.js";
import { readDispatcherFailedCounts } from "./reply-dispatcher.types.js";
import { replyRunRegistry, type ReplyOperation } from "./reply-run-registry.js";
import { isReplyProfilerEnabled } from "./reply-timing-tracker.js";
import { admitReplyTurn, resolveReplyTurnKind } from "./reply-turn-admission.js";
@@ -774,13 +779,13 @@ async function mirrorInternalSourceReplyToTranscript(params: {
}
}
function getDispatcherFinalOutcomeCounts(dispatcher: ReplyDispatcher): {
export function getDispatcherFinalOutcomeCounts(dispatcher: DispatcherOutcomeCountsView): {
cancelled: number;
failed: number;
} {
return {
cancelled: dispatcher.getCancelledCounts?.().final ?? 0,
failed: dispatcher.getFailedCounts().final,
failed: readDispatcherFailedCounts(dispatcher).final,
};
}
@@ -903,7 +908,7 @@ function createAbortAwareDispatcher(params: {
sendFinalReply: sendIfActive(params.dispatcher.sendFinalReply),
waitForIdle: () => params.dispatcher.waitForIdle(),
getQueuedCounts: () => params.dispatcher.getQueuedCounts(),
getFailedCounts: () => params.dispatcher.getFailedCounts(),
getFailedCounts: () => readDispatcherFailedCounts(params.dispatcher),
markComplete: () => {
if (!params.isAborted()) {
params.dispatcher.markComplete();

View File

@@ -18,3 +18,21 @@ export type ReplyDispatcher = {
getFailedCounts: () => Record<ReplyDispatchKind, number>;
markComplete: () => void;
};
/**
* Internal view for defensive outcome-count accounting. Some non-conforming
* runtime dispatcher variants (for example plugin-provided dispatchers) may omit
* these readers even though the public ReplyDispatcher contract requires
* getFailedCounts. Read the counters through this view so the guards stay
* type-correct without weakening the SDK-visible ReplyDispatcher type.
*/
export type DispatcherOutcomeCountsView = {
getCancelledCounts?: () => Record<ReplyDispatchKind, number>;
getFailedCounts?: () => Record<ReplyDispatchKind, number>;
};
export function readDispatcherFailedCounts(
dispatcher: DispatcherOutcomeCountsView,
): Record<ReplyDispatchKind, number> {
return dispatcher.getFailedCounts?.() ?? { tool: 0, block: 0, final: 0 };
}