Skip to content

Commit 260ee5f

Browse files
committed
fix(codex): report native project docs truthfully
1 parent a57d9b5 commit 260ee5f

9 files changed

Lines changed: 174 additions & 40 deletions

File tree

‎docs/plugins/codex-harness.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ This byte budget is separate from the character-based workspace bootstrap
266266
limits configured through `agents.defaults.bootstrapMaxChars` and
267267
`agents.defaults.bootstrapTotalMaxChars`.
268268

269+
`/context` reports native project documents as unverified because app-server
270+
exposes their source paths but not the retained byte counts needed to tell
271+
whether any individual file was fully loaded or truncated.
272+
269273
### Compaction
270274

271275
Do not set `compaction.model` or `compaction.provider` on Codex-backed

‎extensions/codex/src/app-server/attempt-context.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,12 +459,23 @@ function buildCodexBootstrapInjectionStats(params: {
459459
? undefined
460460
: (readCodexIndexedContextFileContent(injectedIndex, pathValue, fileName) ??
461461
readCodexIndexedContextFileContent(developerInstructionIndex, pathValue, fileName));
462-
let injectedChars = memoryToolRoutedFile ? 0 : (injected?.length ?? 0);
463-
let truncated = memoryToolRoutedFile ? false : !file.missing && injectedChars < rawChars;
464-
if (injected === undefined && CODEX_NATIVE_PROJECT_DOC_BASENAMES.has(baseName)) {
465-
injectedChars = rawChars;
466-
truncated = false;
462+
if (
463+
!file.missing &&
464+
injected === undefined &&
465+
CODEX_NATIVE_PROJECT_DOC_BASENAMES.has(baseName)
466+
) {
467+
return {
468+
name: displayName,
469+
path: pathValue,
470+
missing: false,
471+
rawChars,
472+
injectionStatus: "native_unverified",
473+
injectedChars: null,
474+
truncated: null,
475+
};
467476
}
477+
const injectedChars = memoryToolRoutedFile ? 0 : (injected?.length ?? 0);
478+
const truncated = memoryToolRoutedFile ? false : !file.missing && injectedChars < rawChars;
468479
return {
469480
name: displayName,
470481
path: pathValue,

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,8 +3879,9 @@ describe("runCodexAppServerAttempt", () => {
38793879
});
38803880
expect(fileStats.get("AGENTS.md")).toMatchObject({
38813881
rawChars: agentsGuidance.length,
3882-
injectedChars: agentsGuidance.length,
3883-
truncated: false,
3882+
injectionStatus: "native_unverified",
3883+
injectedChars: null,
3884+
truncated: null,
38843885
});
38853886
});
38863887
it("adds memory recall guidance when dated memory notes exist without root MEMORY.md", async () => {
@@ -4012,7 +4013,7 @@ describe("runCodexAppServerAttempt", () => {
40124013
const run = runCodexAppServerAttempt(params);
40134014
await harness.waitForMethod("turn/start");
40144015
await harness.completeTurn({ threadId: "thread-1", turnId: "turn-1" });
4015-
await run;
4016+
const result = await run;
40164017

40174018
const threadStart = harness.requests.find((request) => request.method === "thread/start");
40184019
if (!threadStart) {
@@ -4037,6 +4038,14 @@ describe("runCodexAppServerAttempt", () => {
40374038
).collaborationMode?.settings?.developer_instructions ?? "";
40384039
expect(collaborationInstructions).toContain(soulGuidance);
40394040
expect(collaborationInstructions).not.toContain(agentsGuidance);
4041+
const agentWorkspaceStats = result.systemPromptReport?.injectedWorkspaceFiles.find(
4042+
(file) => file.path === path.join(agentWorkspaceDir, "AGENTS.md"),
4043+
);
4044+
expect(agentWorkspaceStats).toMatchObject({
4045+
rawChars: agentsGuidance.length,
4046+
injectedChars: agentsGuidance.length,
4047+
truncated: false,
4048+
});
40404049

40414050
const updatedGuidance = "Updated AGENTS guidance must wait for a new session.";
40424051
await fs.writeFile(path.join(agentWorkspaceDir, "AGENTS.md"), updatedGuidance);

‎src/auto-reply/reply/commands-context-report.test.ts‎

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function makeParams(
3030
storePath?: string;
3131
agentId?: string;
3232
currentTurn?: NonNullable<SessionEntry["systemPromptReport"]>["currentTurn"];
33+
nativeUnverified?: boolean;
3334
},
3435
): HandleCommandsParams {
3536
const totalTokensFresh = options?.totalTokensFresh ?? true;
@@ -68,16 +69,28 @@ function makeParams(
6869
nonProjectContextChars: 500,
6970
},
7071
...(options?.currentTurn ? { currentTurn: options.currentTurn } : {}),
71-
injectedWorkspaceFiles: [
72-
{
73-
name: "AGENTS.md",
74-
path: "/tmp/workspace/AGENTS.md",
75-
missing: false,
76-
rawChars: truncated ? 200_000 : 10_000,
77-
injectedChars: truncated ? 12_000 : 10_000,
78-
truncated,
79-
},
80-
],
72+
injectedWorkspaceFiles: options?.nativeUnverified
73+
? [
74+
{
75+
name: "AGENTS.md",
76+
path: "/tmp/workspace/AGENTS.md",
77+
missing: false,
78+
rawChars: 10_000,
79+
injectionStatus: "native_unverified",
80+
injectedChars: null,
81+
truncated: null,
82+
},
83+
]
84+
: [
85+
{
86+
name: "AGENTS.md",
87+
path: "/tmp/workspace/AGENTS.md",
88+
missing: false,
89+
rawChars: truncated ? 200_000 : 10_000,
90+
injectedChars: truncated ? 12_000 : 10_000,
91+
truncated,
92+
},
93+
],
8194
skills: {
8295
promptChars: 10,
8396
entries: [{ name: "checks", blockChars: 10 }],
@@ -157,6 +170,21 @@ describe("buildContextReply", () => {
157170
expect(result.text).not.toContain("Bootstrap context is over configured limits");
158171
});
159172

173+
it("reports native Codex project docs as unverified without OpenClaw limit advice", async () => {
174+
const result = await buildContextReply(
175+
makeParams("/context detail", false, { nativeUnverified: true }),
176+
);
177+
178+
expect(result.text).toContain(
179+
"- AGENTS.md: NATIVE/UNVERIFIED | raw(local) 10,000 chars (~2,500 tok) | injected unknown",
180+
);
181+
expect(result.text).toContain("one aggregate root-to-CWD byte budget");
182+
expect(result.text).toContain("later AGENTS.md files can be partial");
183+
expect(result.text).toContain("read the relevant scoped file directly");
184+
expect(result.text).not.toContain("Bootstrap context is over configured limits");
185+
expect(result.text).not.toContain("agents.entries.*.bootstrapMaxChars");
186+
});
187+
160188
it("falls back to config defaults when legacy reports are missing bootstrap limits", async () => {
161189
const result = await buildContextReply(
162190
makeParams("/context list", false, {
@@ -357,6 +385,24 @@ describe("buildContextReply", () => {
357385
}
358386
});
359387

388+
it("omits unknown native project-document bytes from context maps", async () => {
389+
const result = await buildContextReply(
390+
makeParams("/context map", false, {
391+
contextTokens: 8_192,
392+
totalTokens: 900,
393+
nativeUnverified: true,
394+
}),
395+
);
396+
if (!result.mediaUrl) {
397+
throw new Error("missing context map media path");
398+
}
399+
try {
400+
expect(result.text).toContain("Tracked: 1,020 chars");
401+
} finally {
402+
await unlink(result.mediaUrl);
403+
}
404+
});
405+
360406
it("includes transcript conversation size in context maps", async () => {
361407
await withTranscript(
362408
[

‎src/auto-reply/reply/commands-context-report.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,21 @@ export async function buildContextReply(params: HandleCommandsParams): Promise<R
273273
}
274274

275275
const fileLines = report.injectedWorkspaceFiles.map((f) => {
276-
const status = f.missing ? "MISSING" : f.truncated ? "TRUNCATED" : "OK";
276+
const nativeUnverified = f.injectionStatus === "native_unverified";
277+
const status = nativeUnverified
278+
? "NATIVE/UNVERIFIED"
279+
: f.missing
280+
? "MISSING"
281+
: f.truncated
282+
? "TRUNCATED"
283+
: "OK";
277284
const raw = f.missing ? "0" : formatCharsAndTokens(f.rawChars);
278-
const injected = f.missing ? "0" : formatCharsAndTokens(f.injectedChars);
279-
return `- ${f.name}: ${status} | raw ${raw} | injected ${injected}`;
285+
const injected = nativeUnverified
286+
? "unknown"
287+
: f.missing
288+
? "0"
289+
: formatCharsAndTokens(f.injectedChars);
290+
return `- ${f.name}: ${status} | raw${nativeUnverified ? "(local)" : ""} ${raw} | injected ${injected}`;
280291
});
281292

282293
const sandboxLine = `Sandbox: mode=${report.sandbox?.mode ?? "unknown"} sandboxed=${report.sandbox?.sandboxed ?? false}`;
@@ -314,7 +325,9 @@ export async function buildContextReply(params: HandleCommandsParams): Promise<R
314325
const bootstrapMaxLabel = `${formatInt(bootstrapMaxChars)} chars`;
315326
const bootstrapTotalLabel = `${formatInt(bootstrapTotalMaxChars)} chars`;
316327
const bootstrapAnalysis = analyzeBootstrapBudget({
317-
files: report.injectedWorkspaceFiles,
328+
files: report.injectedWorkspaceFiles.filter(
329+
(file) => file.injectionStatus !== "native_unverified",
330+
),
318331
bootstrapMaxChars,
319332
bootstrapTotalMaxChars,
320333
});
@@ -346,6 +359,15 @@ export async function buildContextReply(params: HandleCommandsParams): Promise<R
346359
"Tip: increase this agent's `agents.entries.*.bootstrapMaxChars` / `agents.entries.*.bootstrapTotalMaxChars` override, or the matching `agents.defaults.*` fallback, if this truncation is not intentional.",
347360
]
348361
: [];
362+
const hasNativeUnverifiedFiles = report.injectedWorkspaceFiles.some(
363+
(file) => file.injectionStatus === "native_unverified",
364+
);
365+
const nativeUnverifiedWarningLines = hasNativeUnverifiedFiles
366+
? [
367+
"⚠ Native Codex project instructions are unverified: Codex applies one aggregate root-to-CWD byte budget, and app-server does not report exact per-file retained bytes, so later AGENTS.md files can be partial.",
368+
"Keep earlier/root files concise and read the relevant scoped file directly if guidance appears missing.",
369+
]
370+
: [];
349371

350372
const contextWindowLabel = session.contextTokens != null ? formatInt(session.contextTokens) : "?";
351373
const totalsLine =
@@ -359,6 +381,7 @@ export async function buildContextReply(params: HandleCommandsParams): Promise<R
359381
sandboxLine,
360382
systemPromptLine,
361383
...(bootstrapWarningLines.length ? ["", ...bootstrapWarningLines] : []),
384+
...(nativeUnverifiedWarningLines.length ? ["", ...nativeUnverifiedWarningLines] : []),
362385
"",
363386
"Injected workspace files:",
364387
...fileLines,

‎src/auto-reply/reply/context-treemap.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function buildGroups(params: {
341341
}): TreemapGroup[] {
342342
const { report } = params;
343343
const injectedTotal = report.injectedWorkspaceFiles.reduce(
344-
(sum, file) => sum + file.injectedChars,
344+
(sum, file) => (file.injectionStatus === "native_unverified" ? sum : sum + file.injectedChars),
345345
0,
346346
);
347347
const projectFrameChars = Math.max(0, report.systemPrompt.projectContextChars - injectedTotal);
@@ -360,10 +360,12 @@ function buildGroups(params: {
360360
name: "Workspace files",
361361
color: rgba(58, 145, 91),
362362
leaves: [
363-
...report.injectedWorkspaceFiles.map((file) => ({
364-
name: file.name,
365-
value: file.injectedChars,
366-
})),
363+
...report.injectedWorkspaceFiles
364+
.filter((file) => file.injectionStatus !== "native_unverified")
365+
.map((file) => ({
366+
name: file.name,
367+
value: file.injectedChars,
368+
})),
367369
{ name: "Project context frame", value: projectFrameChars },
368370
],
369371
}),

‎src/config/sessions/session-system-prompt-report.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,25 @@ export type SessionSystemPromptReport = {
3636
// persisted transcript prompt; consumers add it on top of transcript sums.
3737
modelOnlyPromptChars?: number;
3838
};
39-
injectedWorkspaceFiles: Array<{
40-
name: string;
41-
path: string;
42-
missing: boolean;
43-
rawChars: number;
44-
injectedChars: number;
45-
truncated: boolean;
46-
}>;
39+
injectedWorkspaceFiles: Array<
40+
{
41+
name: string;
42+
path: string;
43+
missing: boolean;
44+
rawChars: number;
45+
} & (
46+
| {
47+
injectionStatus?: "verified";
48+
injectedChars: number;
49+
truncated: boolean;
50+
}
51+
| {
52+
injectionStatus: "native_unverified";
53+
injectedChars: null;
54+
truncated: null;
55+
}
56+
)
57+
>;
4758
skills: {
4859
promptChars: number;
4960
hash?: string;

‎ui/src/pages/usage/view-details.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ describe("renderSessionDetailPanel filtered usage", () => {
376376
injectedChars: 30,
377377
truncated: false,
378378
},
379+
{
380+
name: "AGENTS.md",
381+
path: "/AGENTS.md",
382+
missing: false,
383+
rawChars: 100,
384+
injectionStatus: "native_unverified",
385+
injectedChars: null,
386+
truncated: null,
387+
},
379388
],
380389
};
381390
const onToggleContextExpanded = vi.fn();
@@ -397,7 +406,7 @@ describe("renderSessionDetailPanel filtered usage", () => {
397406
const cards = [...container.querySelectorAll(".context-breakdown-card")];
398407
expect(
399408
cards.map((card) => card.querySelector(".context-breakdown-title")?.textContent?.trim()),
400-
).toEqual(["Skills (5)", "Tools (2)", "Files (2)"]);
409+
).toEqual(["Skills (5)", "Tools (2)", "Files (3)"]);
401410
expect(
402411
[...(cards[0]?.querySelectorAll(".context-breakdown-item .mono") ?? [])].map(
403412
(entry) => entry.textContent,
@@ -406,7 +415,13 @@ describe("renderSessionDetailPanel filtered usage", () => {
406415
expect(cards[1]?.querySelector(".context-breakdown-item .mono")?.textContent).toBe(
407416
"larger-tool",
408417
);
409-
expect(cards[2]?.querySelector(".context-breakdown-item .mono")?.textContent).toBe("large.md");
418+
const fileEntries = [...(cards[2]?.querySelectorAll(".context-breakdown-item") ?? [])];
419+
expect(fileEntries.map((entry) => entry.querySelector(".mono")?.textContent)).toEqual([
420+
"large.md",
421+
"small.md",
422+
"AGENTS.md",
423+
]);
424+
expect(fileEntries[2]?.querySelector(".muted")?.textContent).toBe("unknown");
410425
expect(cards[0]?.querySelector(".context-breakdown-more")?.textContent).toContain("1 more");
411426
container.querySelector<HTMLButtonElement>(".context-breakdown-header button")?.click();
412427
expect(onToggleContextExpanded).toHaveBeenCalledOnce();

‎ui/src/pages/usage/view-details.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,11 @@ function renderContextPanel(
832832
className: "files",
833833
labelKey: "usage.details.files",
834834
tokens: charsToTokens(
835-
contextWeight.injectedWorkspaceFiles.reduce((sum, file) => sum + file.injectedChars, 0),
835+
contextWeight.injectedWorkspaceFiles.reduce(
836+
(sum, file) =>
837+
file.injectionStatus === "native_unverified" ? sum : sum + file.injectedChars,
838+
0,
839+
),
836840
),
837841
entries: contextWeight.injectedWorkspaceFiles.map(({ name, injectedChars }) => ({
838842
name,
@@ -843,7 +847,12 @@ function renderContextPanel(
843847
className,
844848
labelKey,
845849
tokens,
846-
entries: entries.toSorted((left, right) => right.chars - left.chars),
850+
entries: entries.toSorted((left, right) => {
851+
if (left.chars === null) {
852+
return right.chars === null ? 0 : 1;
853+
}
854+
return right.chars === null ? -1 : right.chars - left.chars;
855+
}),
847856
}));
848857
const categories = [
849858
{
@@ -915,7 +924,11 @@ function renderContextPanel(
915924
({ name, chars }) => html`
916925
<div class="context-breakdown-item">
917926
<span class="mono" title=${name}>${name}</span>
918-
<span class="muted">~${formatUsageTokens(charsToTokens(chars))}</span>
927+
<span class="muted"
928+
>${chars === null
929+
? t("usage.common.unknown")
930+
: `~${formatUsageTokens(charsToTokens(chars))}`}</span
931+
>
919932
</div>
920933
`,
921934
)}

0 commit comments

Comments
 (0)