Skip to content

Commit e17bc58

Browse files
fix: tail context answers the current user message (#117364)
* fix(agents): apply image context to current turn * fix(agents): align runtime context with prompt order * fix(agents): make turn context position independent * 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]> --------- Co-authored-by: chernoff-code <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 6aee279 commit e17bc58

4 files changed

Lines changed: 54 additions & 21 deletions

File tree

‎src/agents/embedded-agent-runner/run/attempt.llm-boundary.test.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
normalizeMessagesForLlmBoundary,
1212
} from "./attempt.llm-boundary.js";
1313
import { resolveUserTranscriptMessages } from "./attempt.user-message-boundary.js";
14+
import { buildRuntimeContextCustomMessage } from "./runtime-context-prompt.js";
1415

1516
describe("normalizeMessagesForLlmBoundary", () => {
1617
it("strips inbound metadata from historical user turns before model replay", () => {
@@ -937,13 +938,7 @@ describe("normalizeMessagesForLlmBoundary", () => {
937938
content: [{ type: "text", text: "old answer" }],
938939
timestamp: 1,
939940
},
940-
{
941-
role: "custom",
942-
customType: "openclaw.runtime-context",
943-
content: "current runtime context",
944-
display: false,
945-
timestamp: 2,
946-
},
941+
buildRuntimeContextCustomMessage("current runtime context"),
947942
{
948943
role: "user",
949944
content: [{ type: "text", text: "visible ask" }],
@@ -963,8 +958,10 @@ describe("normalizeMessagesForLlmBoundary", () => {
963958
]);
964959
expect(modelInput[2]).toMatchObject({
965960
customType: "openclaw.runtime-context",
966-
content: "current runtime context",
967961
});
962+
expect(modelInput[2]?.content).toContain(
963+
"Use it to continue answering the active user request now.",
964+
);
968965
// User messages are form-canonicalized from array to plain string.
969966
expect(modelInput[0]?.content).toBe("old ask");
970967
expect(modelInput[3]?.content).toBe("visible ask");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ describe("runtime context prompt submission", () => {
612612
role: "custom",
613613
customType: "openclaw.runtime-context",
614614
content: [
615-
"OpenClaw runtime context for the immediately preceding user 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.",
616616
"This context is runtime-generated, not user-authored. Keep internal details private.",
617617
"",
618618
"<<<BEGIN_OPENCLAW_INTERNAL_CONTEXT>>>",

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

Lines changed: 37 additions & 0 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";
@@ -105,6 +108,40 @@ describe("internal runtime context codec", () => {
105108
).toBe(false);
106109
});
107110

111+
it.each([
112+
["current turn", OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER],
113+
[
114+
"previous current turn",
115+
"OpenClaw runtime context for the immediately preceding user message.",
116+
],
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");
120+
const input = [
121+
preface,
122+
"",
123+
INTERNAL_RUNTIME_CONTEXT_BEGIN,
124+
"secret runtime context",
125+
INTERNAL_RUNTIME_CONTEXT_END,
126+
"",
127+
"Visible reply",
128+
].join("\n");
129+
130+
expect(hasInternalRuntimeContext(preface)).toBe(true);
131+
expect(stripInternalRuntimeContext(preface)).toBe("");
132+
expect(stripInternalRuntimeContext(input)).toBe("Visible reply");
133+
});
134+
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+
108145
it("fuzzes delimiter injection and nested marker handling deterministically", () => {
109146
const rng = createDeterministicRng(0xc0ff_ee42);
110147
const tokenPool = [

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +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-
/** Header for context attached to the immediately preceding user message. */
19+
/** Position-independent instructions for context belonging to the active user turn. */
2020
export const OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER =
21-
"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.";
2222
/** Header for runtime events passed as prompt context. */
2323
export const OPENCLAW_RUNTIME_EVENT_HEADER = "OpenClaw runtime event.";
2424
/** Custom message type used for structured runtime-context messages. */
@@ -212,11 +212,11 @@ function stripLegacyInternalRuntimeContext(text: string): string {
212212
}
213213
}
214214

215-
function isRuntimeContextPromptHeader(line: string): boolean {
216-
return (
217-
line === OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER || line === OPENCLAW_RUNTIME_EVENT_HEADER
218-
);
219-
}
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+
];
220220

221221
function stripRuntimeContextPromptPreface(text: string): string {
222222
const lines = text.split(/\r?\n/);
@@ -227,7 +227,7 @@ function stripRuntimeContextPromptPreface(text: string): string {
227227
const line = lines[index] ?? "";
228228
const nextLine = lines[index + 1] ?? "";
229229
if (
230-
isRuntimeContextPromptHeader(line.trim()) &&
230+
RUNTIME_CONTEXT_PROMPT_HEADERS.includes(line.trim()) &&
231231
nextLine.trim() === OPENCLAW_RUNTIME_CONTEXT_NOTICE
232232
) {
233233
changed = true;
@@ -294,10 +294,9 @@ export function hasInternalRuntimeContext(text: string): boolean {
294294
return (
295295
findDelimitedTokenIndex(text, INTERNAL_RUNTIME_CONTEXT_BEGIN, 0) !== -1 ||
296296
text.includes(LEGACY_INTERNAL_CONTEXT_HEADER) ||
297-
text.includes(
298-
`${OPENCLAW_NEXT_TURN_RUNTIME_CONTEXT_HEADER}\n${OPENCLAW_RUNTIME_CONTEXT_NOTICE}`,
299-
) ||
300-
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+
)
301300
);
302301
}
303302

0 commit comments

Comments
 (0)