Skip to content

Commit 067f73d

Browse files
committed
fix(reply): let active turns reach queue policy
Same-session visible turns could be received promptly but still sit behind dispatch admission until the active reply operation went idle. That meant messages.queue.mode=steer never got a chance to inject into the active embedded run; the turn only reached get-reply-run after the original run completed. Tried treating this as iMessage inbound debounce first, but live stability events showed message.received and message.queued happened during the shell sleep. The hold was later: dispatch admission waited for the active reply operation before queue policy resolution. This keeps the Slack routed-thread bypass as-is, keeps different non-Slack routed threads serialized, and only lets visible turns in the same route thread pass through to the active-run queue policy. That lets steer/followup/collect/interrupt own the active-run decision instead of the admission fence preempting it. Scaffolding: this is source-level and tested in the vendored checkout. The installed OpenClaw bundle still needs a build/install or hot patch before the live gateway uses it.
1 parent 74cf5c7 commit 067f73d

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,59 @@ describe("dispatchReplyFromConfig", () => {
12241224
}
12251225
});
12261226

1227+
it("lets a same-session iMessage turn reach active-run queue policy while another turn is active", async () => {
1228+
setNoAbort();
1229+
const sessionKey = "agent:main:imessage:direct:+15551234567";
1230+
const activeOperation = createReplyOperation({
1231+
sessionKey,
1232+
sessionId: "active-session",
1233+
resetTriggered: false,
1234+
});
1235+
activeOperation.setPhase("running");
1236+
const dispatcher = createDispatcher();
1237+
const replyResolver = vi.fn(async () => ({ text: "queued for policy" }) satisfies ReplyPayload);
1238+
1239+
const resultPromise = dispatchReplyFromConfig({
1240+
ctx: buildTestCtx({
1241+
Provider: "imessage",
1242+
Surface: "imessage",
1243+
OriginatingChannel: "imessage",
1244+
OriginatingTo: "imessage:+15551234567",
1245+
ChatType: "direct",
1246+
SessionKey: sessionKey,
1247+
BodyForAgent: "steer the active run",
1248+
}),
1249+
cfg: {
1250+
messages: {
1251+
queue: {
1252+
mode: "steer",
1253+
byChannel: {
1254+
imessage: "steer",
1255+
},
1256+
},
1257+
},
1258+
} as const satisfies OpenClawConfig,
1259+
dispatcher,
1260+
replyResolver,
1261+
});
1262+
1263+
try {
1264+
const result = await Promise.race([
1265+
resultPromise,
1266+
new Promise<"blocked">((resolve) => setTimeout(() => resolve("blocked"), 1_000)),
1267+
]);
1268+
1269+
expect(result).toMatchObject({
1270+
queuedFinal: true,
1271+
counts: { tool: 0, block: 0, final: 0 },
1272+
});
1273+
expect(replyResolver).toHaveBeenCalledTimes(1);
1274+
} finally {
1275+
activeOperation.complete();
1276+
await resultPromise;
1277+
}
1278+
});
1279+
12271280
it("keeps non-Slack routed direct turns behind the active reply operation", async () => {
12281281
setNoAbort();
12291282
const sessionKey = "agent:main:telegram:direct:1";

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ function shouldLetSlackRoutedThreadBypassBusyReplyOperation(params: {
238238
);
239239
}
240240

241+
function shouldLetActiveReplyOperationReachQueuePolicy(params: {
242+
activeOperation?: ReplyOperation;
243+
routeThreadId?: string | number;
244+
}): boolean {
245+
return (
246+
params.activeOperation !== undefined &&
247+
!routeThreadIdsDiffer(params.activeOperation.routeThreadId, params.routeThreadId)
248+
);
249+
}
250+
241251
const routeReplyRuntimeLoader = createLazyImportLoader(() => import("./route-reply.runtime.js"));
242252
const getReplyFromConfigRuntimeLoader = createLazyImportLoader(
243253
() => import("./get-reply-from-config.runtime.js"),
@@ -1240,10 +1250,18 @@ export async function dispatchReplyFromConfig(
12401250
crypto.randomUUID();
12411251
const replyTurnKind = resolveReplyTurnKind(params.replyOptions);
12421252
const allowActivePreDispatch = phase === "pre_dispatch" && replyTurnKind === "visible";
1253+
const activeReplyOperation = replyRunRegistry.get(dispatchOperationSessionKey);
1254+
const allowActiveQueuePolicy =
1255+
phase === "dispatch" &&
1256+
replyTurnKind === "visible" &&
1257+
shouldLetActiveReplyOperationReachQueuePolicy({
1258+
activeOperation: activeReplyOperation,
1259+
routeThreadId,
1260+
});
12431261
const allowSlackRoutedThreadBypass =
12441262
phase === "dispatch" &&
12451263
shouldLetSlackRoutedThreadBypassBusyReplyOperation({
1246-
activeOperation: replyRunRegistry.get(dispatchOperationSessionKey),
1264+
activeOperation: activeReplyOperation,
12471265
ctx,
12481266
routeThreadId,
12491267
});
@@ -1254,13 +1272,20 @@ export async function dispatchReplyFromConfig(
12541272
resetTriggered: false,
12551273
routeThreadId,
12561274
upstreamAbortSignal: params.replyOptions?.abortSignal,
1257-
waitForActive: !allowActivePreDispatch && !allowSlackRoutedThreadBypass,
1275+
waitForActive:
1276+
!allowActivePreDispatch && !allowActiveQueuePolicy && !allowSlackRoutedThreadBypass,
12581277
});
12591278
if (admission.status === "skipped") {
12601279
if (allowActivePreDispatch && admission.reason === "active-run") {
12611280
preDispatchAbortOperation = admission.activeOperation;
12621281
return { status: "ready" };
12631282
}
1283+
if (allowActiveQueuePolicy && admission.reason === "active-run") {
1284+
logVerbose(
1285+
`dispatch-from-config: allowing visible turn for ${dispatchOperationSessionKey} to reach active-run queue policy while a reply operation is active`,
1286+
);
1287+
return { status: "ready" };
1288+
}
12641289
if (
12651290
admission.reason === "active-run" &&
12661291
shouldLetSlackRoutedThreadBypassBusyReplyOperation({

0 commit comments

Comments
 (0)