Skip to content

Commit 08f1d03

Browse files
committed
fix(codex): stop quoted context from triggering explicit skill mentions
Codex byte-scans every turn text input for $name skill mentions and [@name](plugin://...) links (codex-rs/skills/src/mentions.rs), so historical $skill tokens inside OpenClaw's projected <conversation_context> block and inbound quoted-reply/room-backlog context counted as current explicit invocations and injected skill bodies the user never requested. Neutralize mention sigils with same-length fullwidth lookalikes (the escapeCodexChatText technique) in projected history and inbound context; only the raw current request stays selectable. Fixes #122812
1 parent b7b3314 commit 08f1d03

4 files changed

Lines changed: 64 additions & 5 deletions

File tree

‎extensions/codex/src/app-server/context-engine-projection.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ describe("projectContextEngineAssemblyForCodex", () => {
7171
expect(ordered.prePromptMessageCount).toBe(1);
7272
});
7373

74+
it("neutralizes explicit mention sigils in projected history but not the current request", () => {
75+
const result = projectContextEngineAssemblyForCodex({
76+
assembledMessages: [
77+
textMessage("assistant", "The user did not invoke $example-manual."),
78+
textMessage("user", "see [$other-skill](skill://other) and [@pkg](plugin://pkg@mp)"),
79+
],
80+
originalHistoryMessages: [],
81+
prompt: "run $current-skill now",
82+
});
83+
84+
const context = result.promptText.slice(0, result.promptContextRange?.end);
85+
// Codex byte-scans the whole turn text for `$name`; historical tokens must
86+
// not survive in scannable form (codex-rs/skills/src/mentions.rs).
87+
expect(context).not.toContain("$example-manual");
88+
expect(context).toContain("$example-manual");
89+
expect(context).toContain("[$other-skill](skill://other)");
90+
expect(context).toContain("[@pkg](plugin://pkg@mp)");
91+
expect(result.promptText).toContain("Current user request:\nrun $current-skill now");
92+
});
93+
7494
it("frames projected history as reference data and omits tool payloads", () => {
7595
const result = projectContextEngineAssemblyForCodex({
7696
assembledMessages: [

‎extensions/codex/src/app-server/context-engine-projection.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ const DEFAULT_CODEX_PROJECTION_RESERVE_TOKENS = 20_000;
3838
const MIN_PROMPT_BUDGET_RATIO = 0.5;
3939
const MIN_PROMPT_BUDGET_TOKENS = 8_000;
4040

41+
// Codex scans every turn text input byte-for-byte for explicit `$name` skill
42+
// mentions and `[@name](plugin://…)` links (codex-rs/skills/src/mentions.rs);
43+
// quoted history must never count as a current explicit invocation, so swap
44+
// the sigils to same-length fullwidth lookalikes (same technique as
45+
// escapeCodexChatText). Only the raw current request stays selectable.
46+
export function neutralizeCodexExplicitMentionSigils(text: string): string {
47+
return text
48+
.replace(/\$(?=[A-Za-z0-9_:-])/gu, "$")
49+
.replace(/\[@(?=[A-Za-z0-9_:-]+\]\()/gu, "[@");
50+
}
51+
4152
/** Projects assembled OpenClaw context-engine messages into Codex prompt inputs. */
4253
export function projectContextEngineAssemblyForCodex(params: {
4354
assembledMessages: AgentMessage[];
@@ -50,10 +61,12 @@ export function projectContextEngineAssemblyForCodex(params: {
5061
const prompt = params.prompt.trim();
5162
const contextMessages = dropDuplicateTrailingPrompt(params.assembledMessages, prompt);
5263
const maxRenderedContextChars = normalizeRenderedContextMaxChars(params.maxRenderedContextChars);
53-
const renderedContext = renderMessagesForCodexContext(contextMessages, {
54-
maxTextPartChars: resolveTextPartMaxChars(maxRenderedContextChars),
55-
toolPayloadMode: params.toolPayloadMode ?? "elide",
56-
});
64+
const renderedContext = neutralizeCodexExplicitMentionSigils(
65+
renderMessagesForCodexContext(contextMessages, {
66+
maxTextPartChars: resolveTextPartMaxChars(maxRenderedContextChars),
67+
toolPayloadMode: params.toolPayloadMode ?? "elide",
68+
}),
69+
);
5770
const boundedContext = renderedContext
5871
? truncateOlderContext(renderedContext, maxRenderedContextChars)
5972
: undefined;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Codex tests cover run-attempt prompt state helpers.
2+
import { describe, expect, it } from "vitest";
3+
import { prependCurrentInboundContext } from "./run-attempt-state.js";
4+
5+
describe("prependCurrentInboundContext", () => {
6+
it("neutralizes explicit mention sigils in inbound context but not the prompt", () => {
7+
const joined = prependCurrentInboundContext("run $current-skill now", {
8+
text: "Quoted reply: please try $example-manual later",
9+
});
10+
11+
expect(joined).toBe(
12+
"Quoted reply: please try $example-manual later\n\nrun $current-skill now",
13+
);
14+
});
15+
16+
it("returns the prompt unchanged without inbound context", () => {
17+
expect(prependCurrentInboundContext("run $current-skill now", undefined)).toBe(
18+
"run $current-skill now",
19+
);
20+
});
21+
});

‎extensions/codex/src/app-server/run-attempt-state.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "openclaw/plugin-sdk/string-coerce-runtime";
1111
import type { EmbeddedRunAttemptResult } from "./attempt-terminal.js";
1212
import { CodexAppServerRpcError } from "./client.js";
13+
import { neutralizeCodexExplicitMentionSigils } from "./context-engine-projection.js";
1314
import { isJsonObject, type CodexServerNotification } from "./protocol.js";
1415
import type {
1516
CodexAppServerBindingIdentity,
@@ -136,8 +137,12 @@ export function prependCurrentInboundContext(
136137
prompt: string,
137138
context: EmbeddedRunAttemptParams["currentInboundContext"],
138139
): string {
140+
// Inbound context carries quoted replies and room backlog, not the raw
141+
// current request; Codex must not resolve explicit mentions from it.
139142
const text = context?.text.trim();
140-
return text ? [text, prompt].filter(Boolean).join("\n\n") : prompt;
143+
return text
144+
? [neutralizeCodexExplicitMentionSigils(text), prompt].filter(Boolean).join("\n\n")
145+
: prompt;
141146
}
142147

143148
export function waitForCodexNotificationDispatchTurn(): Promise<void> {

0 commit comments

Comments
 (0)