Skip to content

Commit 0f496fb

Browse files
committed
fix(gateway): reject stale steering owners
1 parent bc209cc commit 0f496fb

3 files changed

Lines changed: 74 additions & 9 deletions

File tree

‎src/auto-reply/reply/reply-run-registry.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,28 +1216,40 @@ describe("reply run registry", () => {
12161216
expect(queueMessage).toHaveBeenCalledWith("hello");
12171217
});
12181218

1219-
it("refuses stale reply-run steering until real activity resumes", () => {
1219+
it("refuses stale injectable owners for admission and delivery until activity resumes", () => {
12201220
vi.useFakeTimers();
12211221
try {
12221222
const queueMessage = vi.fn(async () => {});
12231223
const operation = createTestReplyOperation({
12241224
sessionId: "session-running",
1225+
originatingLeafEntryId: "leaf-a",
12251226
});
12261227
operation.attachBackend({
12271228
kind: "embedded",
12281229
cancel: vi.fn(),
1229-
isStreaming: () => true,
1230+
isStreaming: () => false,
1231+
isStopped: () => false,
12301232
queueMessage,
12311233
});
12321234
operation.setPhase("running");
12331235

1236+
expect(
1237+
replyRunRegistry.isMessageInjectableFromOriginatingLeaf("agent:main:main", "leaf-a"),
1238+
).toBe(true);
1239+
12341240
vi.advanceTimersByTime(RUN_STALE_TAKEOVER_MS + 1);
12351241

1242+
expect(
1243+
replyRunRegistry.isMessageInjectableFromOriginatingLeaf("agent:main:main", "leaf-a"),
1244+
).toBe(false);
12361245
expect(queueReplyRunMessage("session-running", "stale")).toBe(false);
12371246
expect(queueMessage).not.toHaveBeenCalled();
12381247

12391248
operation.recordActivity();
12401249

1250+
expect(
1251+
replyRunRegistry.isMessageInjectableFromOriginatingLeaf("agent:main:main", "leaf-a"),
1252+
).toBe(true);
12411253
expect(queueReplyRunMessage("session-running", "fresh")).toBe(true);
12421254
expect(queueMessage).toHaveBeenCalledWith("fresh");
12431255
} finally {

‎src/auto-reply/reply/reply-run-registry.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,15 @@ function isReplyBackendMessageInjectable(backend: ReplyBackendHandle): boolean {
429429
}
430430
}
431431

432+
function isReplyOperationMessageInjectable(
433+
operation: ReplyOperation,
434+
backend: ReplyBackendHandle,
435+
): boolean {
436+
// Admission and delivery must share freshness or a stale owner can waive the
437+
// transcript-leaf fence and then reject the same steer during injection.
438+
return !isReplyRunEvidenceStale(operation) && isReplyBackendMessageInjectable(backend);
439+
}
440+
432441
/** Run work after an operation no longer owns its session lane. */
433442
export function runAfterReplyOperationClear(
434443
operation: ReplyOperation,
@@ -1186,7 +1195,7 @@ export const replyRunRegistry: ReplyRunRegistry = {
11861195
return false;
11871196
}
11881197
const backend = getAttachedBackend(operation);
1189-
return backend ? isReplyBackendMessageInjectable(backend) : false;
1198+
return backend ? isReplyOperationMessageInjectable(operation, backend) : false;
11901199
},
11911200
abort(sessionKey) {
11921201
const operation = this.get(sessionKey);
@@ -1295,12 +1304,7 @@ export function queueReplyRunMessage(
12951304
if (!operation || operation.phase !== "running" || !backend?.queueMessage) {
12961305
return false;
12971306
}
1298-
// Steering into an evidence-dead run swallows the human message that would
1299-
// otherwise trigger stale takeover through normal reply admission.
1300-
if (isReplyRunEvidenceStale(operation)) {
1301-
return false;
1302-
}
1303-
if (!isReplyBackendMessageInjectable(backend)) {
1307+
if (!isReplyOperationMessageInjectable(operation, backend)) {
13041308
return false;
13051309
}
13061310
if (resolveReplyBackendQueueMessageMismatch(backend, options)) {

‎src/gateway/server-methods/chat.directive-tags.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { waitForSessionTranscriptIndexReconcile } from "../../config/sessions/se
3333
import { resolveMirroredTranscriptText } from "../../config/sessions/transcript-mirror.js";
3434
import { withOwnedSessionTranscriptWrites } from "../../config/sessions/transcript-write-context.js";
3535
import { getAgentRunContext } from "../../infra/agent-run-registry.js";
36+
import { RUN_STALE_TAKEOVER_MS } from "../../logging/diagnostic-run-activity.js";
3637
import { runExclusiveSessionLifecycleMutation } from "../../sessions/session-lifecycle-admission.js";
3738
import {
3839
disposeOpenClawAgentDatabaseByPath,
@@ -1404,6 +1405,54 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
14041405
expect(mockState.lastDispatchOriginatingLeafEntryId).toBe("leaf-before-active-run-output");
14051406
});
14061407

1408+
it("rejects a moved-leaf steer when the non-streaming owner evidence is stale", async () => {
1409+
await createGatewayUserTurnSqliteFixture("openclaw-chat-send-steer-stale-owner-");
1410+
await appendTranscriptMessage(transcriptScope(), {
1411+
eventId: "current-leaf",
1412+
message: { role: "assistant", content: "stale tool work" },
1413+
now: 1,
1414+
parentId: null,
1415+
});
1416+
const { context, respond, send } = createChatRequestFixture();
1417+
vi.useFakeTimers({ toFake: ["Date"] });
1418+
const operation = replyRunRegistry.begin({
1419+
sessionKey: "main",
1420+
sessionId: mockState.sessionId,
1421+
resetTriggered: false,
1422+
originatingLeafEntryId: "leaf-before-stale-run-output",
1423+
});
1424+
operation.setPhase("running");
1425+
operation.attachBackend({
1426+
kind: "embedded",
1427+
cancel: () => {},
1428+
isStreaming: () => false,
1429+
isStopped: () => false,
1430+
queueMessage: async () => {},
1431+
});
1432+
1433+
try {
1434+
vi.advanceTimersByTime(RUN_STALE_TAKEOVER_MS + 1);
1435+
await send({
1436+
idempotencyKey: "idem-steer-stale-owner",
1437+
requestParams: {
1438+
expectedLeafEntryId: "leaf-before-stale-run-output",
1439+
queueMode: "steer",
1440+
},
1441+
waitFor: "none",
1442+
});
1443+
} finally {
1444+
operation.complete();
1445+
vi.useRealTimers();
1446+
}
1447+
1448+
expect(lastRespondCall(respond)).toEqual([
1449+
false,
1450+
undefined,
1451+
expect.objectContaining({ details: { reason: "active-leaf-changed" } }),
1452+
]);
1453+
expect(context.addChatRun).not.toHaveBeenCalled();
1454+
});
1455+
14071456
it("rejects a stale explicit steer when a different leaf owns the active run", async () => {
14081457
await createGatewayUserTurnSqliteFixture("openclaw-chat-send-steer-different-owner-");
14091458
await appendTranscriptMessage(transcriptScope(), {

0 commit comments

Comments
 (0)