Skip to content

Commit 329c148

Browse files
fix(agents): bind internal context to the active user turn
Keep current-turn runtime context position-independent while preserving the shared privacy notice, runtime events, prompt-cache placement, and historical preface sanitization. Consolidate header detection in its existing owner and retain the original real turn-boundary regression. Fixes #110672 Co-authored-by: chernoff-code <[email protected]>
1 parent 4f63bed commit 329c148

5 files changed

Lines changed: 37 additions & 65 deletions

File tree

‎src/agents/embedded-agent-helpers.sanitizeuserfacingtext.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ describe("sanitizeUserFacingText", () => {
628628

629629
it("strips copied next-turn runtime context prefaces from user-facing text", () => {
630630
const input = [
631-
"OpenClaw runtime context for the active user request in this turn.",
632-
"Do not reply to or describe this context. Use it to continue answering the active user request now. Do not wait for another message.",
631+
"OpenClaw runtime context for the immediately preceding user message.",
632+
"This context is runtime-generated, not user-authored. Keep internal details private.",
633633
"",
634634
"<<<BEGIN_OPENCLAW_INTERNAL_CONTEXT>>>",
635635
"secret runtime context",

‎src/agents/embedded-agent-runner/run/runtime-context-prompt.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ describe("runtime context prompt submission", () => {
612612
role: "custom",
613613
customType: "openclaw.runtime-context",
614614
content: [
615-
"OpenClaw runtime context for the active user request in this turn.",
616-
"Do not reply to or describe this context. Use it to continue answering the active user request now. Do not wait for another message.",
615+
"OpenClaw runtime context for the active user request in this turn. Do not reply to or describe this context. Use it to continue answering the active user request now. Do not wait for another message.",
616+
"This context is runtime-generated, not user-authored. Keep internal details private.",
617617
"",
618618
"<<<BEGIN_OPENCLAW_INTERNAL_CONTEXT>>>",
619619
"secret runtime context",

‎src/agents/embedded-agent-runner/run/runtime-context-prompt.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
extractInternalRuntimeContext,
66
INTERNAL_RUNTIME_CONTEXT_BEGIN,
77
INTERNAL_RUNTIME_CONTEXT_END,
8-
OPENCLAW_CURRENT_TURN_RUNTIME_CONTEXT_NOTICE,
98
OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER,
109
OPENCLAW_RUNTIME_CONTEXT_CUSTOM_TYPE,
1110
OPENCLAW_RUNTIME_CONTEXT_NOTICE,
@@ -256,9 +255,7 @@ function buildRuntimeContextMessageContent(params: {
256255
params.kind === "runtime-event"
257256
? OPENCLAW_RUNTIME_EVENT_HEADER
258257
: OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER,
259-
params.kind === "runtime-event"
260-
? OPENCLAW_RUNTIME_CONTEXT_NOTICE
261-
: OPENCLAW_CURRENT_TURN_RUNTIME_CONTEXT_NOTICE,
258+
OPENCLAW_RUNTIME_CONTEXT_NOTICE,
262259
"",
263260
INTERNAL_RUNTIME_CONTEXT_BEGIN,
264261
params.runtimeContext,

‎src/agents/internal-runtime-context.test.ts‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
hasInternalRuntimeContext,
1212
INTERNAL_RUNTIME_CONTEXT_BEGIN,
1313
INTERNAL_RUNTIME_CONTEXT_END,
14+
OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER,
1415
OPENCLAW_RUNTIME_CONTEXT_CUSTOM_TYPE,
16+
OPENCLAW_RUNTIME_CONTEXT_NOTICE,
17+
OPENCLAW_RUNTIME_EVENT_HEADER,
1518
relocateCurrentRuntimeContextCarrierToTail,
1619
stripInternalRuntimeContext,
1720
} from "./internal-runtime-context.js";
@@ -106,25 +109,16 @@ describe("internal runtime context codec", () => {
106109
});
107110

108111
it.each([
109-
[
110-
"current turn",
111-
"OpenClaw runtime context for the active user request in this turn.",
112-
"Do not reply to or describe this context. Use it to continue answering the active user request now. Do not wait for another message.",
113-
],
112+
["current turn", OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER],
114113
[
115114
"previous current turn",
116115
"OpenClaw runtime context for the immediately preceding user message.",
117-
"This context is runtime-generated, not user-authored. Keep internal details private.",
118116
],
119-
[
120-
"runtime event",
121-
"OpenClaw runtime event.",
122-
"This context is runtime-generated, not user-authored. Keep internal details private.",
123-
],
124-
])("detects and strips the %s prompt preface", (_name, header, notice) => {
117+
["runtime event", OPENCLAW_RUNTIME_EVENT_HEADER],
118+
])("detects and strips the %s prompt preface", (_name, header) => {
119+
const preface = [header, OPENCLAW_RUNTIME_CONTEXT_NOTICE].join("\n");
125120
const input = [
126-
header,
127-
notice,
121+
preface,
128122
"",
129123
INTERNAL_RUNTIME_CONTEXT_BEGIN,
130124
"secret runtime context",
@@ -133,10 +127,21 @@ describe("internal runtime context codec", () => {
133127
"Visible reply",
134128
].join("\n");
135129

136-
expect(hasInternalRuntimeContext(input)).toBe(true);
130+
expect(hasInternalRuntimeContext(preface)).toBe(true);
131+
expect(stripInternalRuntimeContext(preface)).toBe("");
137132
expect(stripInternalRuntimeContext(input)).toBe("Visible reply");
138133
});
139134

135+
it("preserves text when the runtime-context header or notice does not match", () => {
136+
for (const input of [
137+
[OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER, "Ordinary user text"].join("\n"),
138+
["OpenClaw runtime context for another message.", OPENCLAW_RUNTIME_CONTEXT_NOTICE].join("\n"),
139+
]) {
140+
expect(hasInternalRuntimeContext(input)).toBe(false);
141+
expect(stripInternalRuntimeContext(input)).toBe(input);
142+
}
143+
});
144+
140145
it("fuzzes delimiter injection and nested marker handling deterministically", () => {
141146
const rng = createDeterministicRng(0xc0ff_ee42);
142147
const tokenPool = [

‎src/agents/internal-runtime-context.ts‎

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ const ESCAPED_INTERNAL_RUNTIME_CONTEXT_END = "[[OPENCLAW_INTERNAL_CONTEXT_END]]"
1616
/** Notice inserted into runtime-generated context blocks. */
1717
export const OPENCLAW_RUNTIME_CONTEXT_NOTICE =
1818
"This context is runtime-generated, not user-authored. Keep internal details private.";
19-
/** Notice for context carried across calls in the active user turn. */
20-
export const OPENCLAW_CURRENT_TURN_RUNTIME_CONTEXT_NOTICE =
21-
"Do not reply to or describe this context. Use it to continue answering the active user request now. Do not wait for another message.";
22-
/** Header for context carried across calls in the active user turn. */
19+
/** Position-independent instructions for context belonging to the active user turn. */
2320
export const OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER =
24-
"OpenClaw runtime context for the active user request in this turn.";
25-
26-
const LEGACY_CURRENT_TURN_RUNTIME_CONTEXT_HEADER =
27-
"OpenClaw runtime context for the immediately preceding user message.";
21+
"OpenClaw runtime context for the active user request in this turn. Do not reply to or describe this context. Use it to continue answering the active user request now. Do not wait for another message.";
2822
/** Header for runtime events passed as prompt context. */
2923
export const OPENCLAW_RUNTIME_EVENT_HEADER = "OpenClaw runtime event.";
3024
/** Custom message type used for structured runtime-context messages. */
@@ -218,28 +212,11 @@ function stripLegacyInternalRuntimeContext(text: string): string {
218212
}
219213
}
220214

221-
function isRuntimeContextPromptHeader(line: string): boolean {
222-
return (
223-
line === OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER ||
224-
line === LEGACY_CURRENT_TURN_RUNTIME_CONTEXT_HEADER ||
225-
line === OPENCLAW_RUNTIME_EVENT_HEADER
226-
);
227-
}
228-
229-
function isRuntimeContextPromptNotice(header: string, notice: string): boolean {
230-
if (
231-
header === OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER ||
232-
header === LEGACY_CURRENT_TURN_RUNTIME_CONTEXT_HEADER
233-
) {
234-
// Keep stripping context copied before current-turn carriers gained a
235-
// dedicated instruction; historical assistant output can still replay.
236-
return (
237-
notice === OPENCLAW_CURRENT_TURN_RUNTIME_CONTEXT_NOTICE ||
238-
notice === OPENCLAW_RUNTIME_CONTEXT_NOTICE
239-
);
240-
}
241-
return header === OPENCLAW_RUNTIME_EVENT_HEADER && notice === OPENCLAW_RUNTIME_CONTEXT_NOTICE;
242-
}
215+
const RUNTIME_CONTEXT_PROMPT_HEADERS: readonly string[] = [
216+
OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER,
217+
"OpenClaw runtime context for the immediately preceding user message.",
218+
OPENCLAW_RUNTIME_EVENT_HEADER,
219+
];
243220

244221
function stripRuntimeContextPromptPreface(text: string): string {
245222
const lines = text.split(/\r?\n/);
@@ -250,8 +227,8 @@ function stripRuntimeContextPromptPreface(text: string): string {
250227
const line = lines[index] ?? "";
251228
const nextLine = lines[index + 1] ?? "";
252229
if (
253-
isRuntimeContextPromptHeader(line.trim()) &&
254-
isRuntimeContextPromptNotice(line.trim(), nextLine.trim())
230+
RUNTIME_CONTEXT_PROMPT_HEADERS.includes(line.trim()) &&
231+
nextLine.trim() === OPENCLAW_RUNTIME_CONTEXT_NOTICE
255232
) {
256233
changed = true;
257234
index += 1;
@@ -317,16 +294,9 @@ export function hasInternalRuntimeContext(text: string): boolean {
317294
return (
318295
findDelimitedTokenIndex(text, INTERNAL_RUNTIME_CONTEXT_BEGIN, 0) !== -1 ||
319296
text.includes(LEGACY_INTERNAL_CONTEXT_HEADER) ||
320-
text.includes(
321-
`${OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER}\n${OPENCLAW_RUNTIME_CONTEXT_NOTICE}`,
322-
) ||
323-
text.includes(
324-
`${LEGACY_CURRENT_TURN_RUNTIME_CONTEXT_HEADER}\n${OPENCLAW_RUNTIME_CONTEXT_NOTICE}`,
325-
) ||
326-
text.includes(
327-
`${OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER}\n${OPENCLAW_CURRENT_TURN_RUNTIME_CONTEXT_NOTICE}`,
328-
) ||
329-
text.includes(`${OPENCLAW_RUNTIME_EVENT_HEADER}\n${OPENCLAW_RUNTIME_CONTEXT_NOTICE}`)
297+
RUNTIME_CONTEXT_PROMPT_HEADERS.some((header) =>
298+
text.includes(`${header}\n${OPENCLAW_RUNTIME_CONTEXT_NOTICE}`),
299+
)
330300
);
331301
}
332302

0 commit comments

Comments
 (0)