Skip to content

Commit 92baa73

Browse files
committed
fix(agents): bound steer message injection
1 parent 0327574 commit 92baa73

6 files changed

Lines changed: 139 additions & 0 deletions

File tree

‎src/agents/pi-embedded-runner/runs.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures";
22
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { MAX_INJECTED_STEER_MESSAGE_CHARS } from "../../shared/steer-message-injection-policy.js";
34
import {
45
__testing,
56
abortEmbeddedPiRun,
@@ -90,6 +91,25 @@ describe("pi-embedded runner run registry", () => {
9091
expect(queueMessage).toHaveBeenCalledWith("keep going");
9192
});
9293

94+
it("rejects oversized messages for active runs", () => {
95+
const queueMessage = vi.fn(async () => {});
96+
97+
setActiveEmbeddedRun(
98+
"session-too-large",
99+
createRunHandle({
100+
queueMessage,
101+
}),
102+
);
103+
104+
expect(
105+
queueEmbeddedPiMessage(
106+
"session-too-large",
107+
"x".repeat(MAX_INJECTED_STEER_MESSAGE_CHARS + 1),
108+
),
109+
).toBe(false);
110+
expect(queueMessage).not.toHaveBeenCalled();
111+
});
112+
93113
it("keeps the streaming fallback for handles without stopped state", () => {
94114
const queueMessage = vi.fn(async () => {});
95115

‎src/agents/pi-embedded-runner/runs.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
logMessageQueued,
1313
logSessionStateChange,
1414
} from "../../logging/diagnostic.js";
15+
import { validateSteerMessageInjection } from "../../shared/steer-message-injection-policy.js";
1516
import { normalizeOptionalString } from "../../shared/string-coerce.js";
1617
import {
1718
ACTIVE_EMBEDDED_RUNS,
@@ -67,6 +68,11 @@ export function queueEmbeddedPiMessage(sessionId: string, text: string): boolean
6768
diag.debug(`queue message failed: sessionId=${sessionId} reason=no_active_run`);
6869
return false;
6970
}
71+
const injectionPolicy = validateSteerMessageInjection({ sessionId, text });
72+
if (!injectionPolicy.ok) {
73+
diag.debug(`queue message failed: sessionId=${sessionId} reason=${injectionPolicy.reason}`);
74+
return false;
75+
}
7076
let isMessageInjectable = false;
7177
try {
7278
isMessageInjectable =

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { MAX_INJECTED_STEER_MESSAGE_CHARS } from "../../shared/steer-message-injection-policy.js";
23
import {
34
__testing,
45
abortActiveReplyRuns,
@@ -88,6 +89,31 @@ describe("reply run registry", () => {
8889
expect(queueMessage).toHaveBeenCalledWith("hello");
8990
});
9091

92+
it("rejects oversized messages through active running backends", async () => {
93+
const queueMessage = vi.fn(async () => {});
94+
const operation = createReplyOperation({
95+
sessionKey: "agent:main:main",
96+
sessionId: "session-running",
97+
resetTriggered: false,
98+
});
99+
100+
operation.attachBackend({
101+
kind: "embedded",
102+
cancel: vi.fn(),
103+
isStreaming: () => true,
104+
queueMessage,
105+
});
106+
operation.setPhase("running");
107+
108+
expect(
109+
queueReplyRunMessage(
110+
"session-running",
111+
"x".repeat(MAX_INJECTED_STEER_MESSAGE_CHARS + 1),
112+
),
113+
).toBe(false);
114+
expect(queueMessage).not.toHaveBeenCalled();
115+
});
116+
91117
it("queues messages through active non-streaming backends with stopped state", async () => {
92118
const queueMessage = vi.fn(async () => {});
93119
const operation = createReplyOperation({

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolveGlobalSingleton } from "../../shared/global-singleton.js";
22
import { normalizeOptionalString } from "../../shared/string-coerce.js";
3+
import { validateSteerMessageInjection } from "../../shared/steer-message-injection-policy.js";
34

45
export type ReplyRunKey = string;
56

@@ -472,6 +473,9 @@ export function queueReplyRunMessage(sessionId: string, text: string): boolean {
472473
if (!operation || operation.phase !== "running" || !backend?.queueMessage) {
473474
return false;
474475
}
476+
if (!validateSteerMessageInjection({ sessionId, text }).ok) {
477+
return false;
478+
}
475479
if (!isReplyBackendMessageInjectable(backend)) {
476480
return false;
477481
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { beforeEach, describe, expect, it } from "vitest";
2+
import {
3+
INJECTED_STEER_RATE_LIMIT_MS,
4+
MAX_INJECTED_STEER_MESSAGE_CHARS,
5+
resetSteerMessageInjectionPolicyForTests,
6+
validateSteerMessageInjection,
7+
} from "./steer-message-injection-policy.js";
8+
9+
describe("steer message injection policy", () => {
10+
beforeEach(() => {
11+
resetSteerMessageInjectionPolicyForTests();
12+
});
13+
14+
it("rejects oversized steer injections", () => {
15+
expect(
16+
validateSteerMessageInjection({
17+
sessionId: "session-large",
18+
text: "x".repeat(MAX_INJECTED_STEER_MESSAGE_CHARS + 1),
19+
}),
20+
).toEqual({ ok: false, reason: "message_too_large" });
21+
});
22+
23+
it("rate limits repeated steer injections for the same session", () => {
24+
expect(
25+
validateSteerMessageInjection({
26+
sessionId: "session-rate",
27+
text: "first",
28+
nowMs: 10_000,
29+
enforceRateLimit: true,
30+
}),
31+
).toEqual({ ok: true });
32+
expect(
33+
validateSteerMessageInjection({
34+
sessionId: "session-rate",
35+
text: "second",
36+
nowMs: 10_000 + INJECTED_STEER_RATE_LIMIT_MS - 1,
37+
enforceRateLimit: true,
38+
}),
39+
).toEqual({ ok: false, reason: "rate_limited" });
40+
expect(
41+
validateSteerMessageInjection({
42+
sessionId: "session-rate",
43+
text: "third",
44+
nowMs: 10_000 + INJECTED_STEER_RATE_LIMIT_MS,
45+
enforceRateLimit: true,
46+
}),
47+
).toEqual({ ok: true });
48+
});
49+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export const MAX_INJECTED_STEER_MESSAGE_CHARS = 4_000;
2+
export const INJECTED_STEER_RATE_LIMIT_MS = 2_000;
3+
4+
export type SteerMessageInjectionRejectReason = "message_too_large" | "rate_limited";
5+
6+
const lastInjectedSteerMessageAtBySession = new Map<string, number>();
7+
8+
export function validateSteerMessageInjection(params: {
9+
sessionId: string;
10+
text: string;
11+
nowMs?: number;
12+
enforceRateLimit?: boolean;
13+
}): { ok: true } | { ok: false; reason: SteerMessageInjectionRejectReason } {
14+
if (params.text.length > MAX_INJECTED_STEER_MESSAGE_CHARS) {
15+
return { ok: false, reason: "message_too_large" };
16+
}
17+
18+
const enforceRateLimit = params.enforceRateLimit ?? process.env.VITEST !== "true";
19+
if (!enforceRateLimit) {
20+
return { ok: true };
21+
}
22+
23+
const nowMs = params.nowMs ?? Date.now();
24+
const lastInjectedAt = lastInjectedSteerMessageAtBySession.get(params.sessionId) ?? 0;
25+
if (nowMs - lastInjectedAt < INJECTED_STEER_RATE_LIMIT_MS) {
26+
return { ok: false, reason: "rate_limited" };
27+
}
28+
lastInjectedSteerMessageAtBySession.set(params.sessionId, nowMs);
29+
return { ok: true };
30+
}
31+
32+
export function resetSteerMessageInjectionPolicyForTests(): void {
33+
lastInjectedSteerMessageAtBySession.clear();
34+
}

0 commit comments

Comments
 (0)