Skip to content

Commit 3d9fb6c

Browse files
committed
fix(logs): persist shadow helper attribution
1 parent caf2035 commit 3d9fb6c

6 files changed

Lines changed: 70 additions & 4 deletions

File tree

‎src/server/request-log.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export interface RequestLogContext {
6565
/** Stable non-PII Codex Pool account identity for durable usage attribution. */
6666
accountLogLabel?: string;
6767
requestedModel?: string;
68+
/** Original bare helper model when the opt-in shadow-call route rewrote this request. */
69+
shadowCallRewrittenFrom?: string;
6870
/** Internal structural combo identity; omitted from RequestLogEntry/JSONL. */
6971
comboId?: string;
7072
requestedEffort?: string;
@@ -142,6 +144,8 @@ export interface RequestLogEntry {
142144
/** Best-effort chat/session correlation for Logs grouping (#330). */
143145
conversationId?: string;
144146
requestedModel?: string;
147+
/** Original bare helper model when the opt-in shadow-call route rewrote this request. */
148+
shadowCallRewrittenFrom?: string;
145149
requestedEffort?: string;
146150
effectiveEffort?: string;
147151
reasoningWireField?: string;
@@ -255,6 +259,9 @@ export function requestLogEntryFromPersistedUsage(entry: PersistedUsageEntry): R
255259
? { accountLogLabel: entry.accountLogLabel }
256260
: {}),
257261
...(entry.requestedModel ? { requestedModel: entry.requestedModel } : {}),
262+
...(entry.shadowCallRewrittenFrom
263+
? { shadowCallRewrittenFrom: entry.shadowCallRewrittenFrom }
264+
: {}),
258265
...(entry.requestedEffort ? { requestedEffort: entry.requestedEffort } : {}),
259266
...(entry.effectiveEffort ? { effectiveEffort: entry.effectiveEffort } : {}),
260267
...(entry.reasoningWireField ? { reasoningWireField: entry.reasoningWireField } : {}),
@@ -358,6 +365,9 @@ export function addRequestLog(entry: RequestLogEntry) {
358365
...(entry.conversationId ? { conversationId: entry.conversationId } : {}),
359366
...(entry.resolvedModel ? { resolvedModel: entry.resolvedModel } : {}),
360367
...(entry.requestedModel ? { requestedModel: entry.requestedModel } : {}),
368+
...(entry.shadowCallRewrittenFrom
369+
? { shadowCallRewrittenFrom: entry.shadowCallRewrittenFrom }
370+
: {}),
361371
...(entry.requestedEffort ? { requestedEffort: entry.requestedEffort } : {}),
362372
...(entry.effectiveEffort ? { effectiveEffort: entry.effectiveEffort } : {}),
363373
...(entry.reasoningWireField ? { reasoningWireField: entry.reasoningWireField } : {}),
@@ -905,6 +915,7 @@ export function addFinalRequestLog(
905915
const loggedUsage = aggregate?.usage ?? existing.usage;
906916
const usageStatus = aggregate?.status ?? existing.status;
907917
const totalTokens = aggregate?.totalTokens ?? existing.totalTokens;
918+
const shadowCallRewrittenFrom = sanitizeLogMetadataString(logCtx.shadowCallRewrittenFrom);
908919
addLog({
909920
requestId,
910921
timestamp: start,
@@ -919,6 +930,9 @@ export function addFinalRequestLog(
919930
: {}),
920931
...(logCtx.conversationId ? { conversationId: logCtx.conversationId } : {}),
921932
...(logCtx.requestedModel ? { requestedModel: logCtx.requestedModel } : {}),
933+
...(shadowCallRewrittenFrom
934+
? { shadowCallRewrittenFrom }
935+
: {}),
922936
...(logCtx.requestedEffort ? { requestedEffort: logCtx.requestedEffort } : {}),
923937
...(logCtx.effectiveEffort ? { effectiveEffort: logCtx.effectiveEffort } : {}),
924938
...(logCtx.reasoningWireField ? { reasoningWireField: logCtx.reasoningWireField } : {}),

‎src/server/responses/core.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,7 @@ async function handleResponsesInner(
18381838
if (parsed._rawBody && typeof parsed._rawBody === "object") {
18391839
(parsed._rawBody as Record<string, unknown>).reasoning = { effort: "low" };
18401840
}
1841-
(logCtx as unknown as Record<string, unknown>).shadowCallRewrittenFrom = _sciOriginal;
1841+
logCtx.shadowCallRewrittenFrom = sanitizeLogMetadataString(_sciOriginal);
18421842
// Helpers must not resume/append into the parent thread's Cursor conversation.
18431843
parsed._cursorIsolateConversation = true;
18441844
}

‎src/usage/log.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ export interface PersistedUsageEntry {
8484
conversationId?: string;
8585
resolvedModel?: string;
8686
requestedModel?: string;
87+
/** Original bare helper model when the opt-in shadow-call route rewrote this request. */
88+
shadowCallRewrittenFrom?: string;
8789
/** Reasoning effort / service-tier metadata for GUI Logs after restart. */
8890
requestedEffort?: string;
8991
/** Adapter-normalized tier and exact upstream parameter emitted for this request. */
@@ -427,6 +429,7 @@ function normalizeUsageEntry(entry: PersistedUsageEntry): PersistedUsageEntry {
427429
const tierOutcome = entry.tierOutcome ? normalizeAttemptTierOutcome(entry.tierOutcome) : undefined;
428430
const callerServiceTier = sanitizeLogMetadataString(entry.callerServiceTier);
429431
const responseServiceTier = sanitizeLogMetadataString(entry.responseServiceTier);
432+
const shadowCallRewrittenFrom = sanitizeLogMetadataString(entry.shadowCallRewrittenFrom);
430433
const routeDecision = entry.routeDecision
431434
? normalizeRouteDecisionTrace(entry.routeDecision)
432435
: undefined;
@@ -453,6 +456,7 @@ function normalizeUsageEntry(entry: PersistedUsageEntry): PersistedUsageEntry {
453456
: {}),
454457
...(entry.resolvedModel ? { resolvedModel: entry.resolvedModel } : {}),
455458
...(entry.requestedModel ? { requestedModel: entry.requestedModel } : {}),
459+
...(shadowCallRewrittenFrom ? { shadowCallRewrittenFrom } : {}),
456460
...(typeof entry.requestedEffort === "string" && entry.requestedEffort
457461
? { requestedEffort: capMetadataString(entry.requestedEffort) }
458462
: {}),

‎structure/05_gui-and-management-api.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ keeps the saved state and renders fixed `ocx sync` guidance without server/accou
320320
## Usage accounting
321321

322322
`src/usage/log.ts` writes append-only JSONL to `~/.opencodex/usage.jsonl` with file mode `0o600`.
323+
An opt-in shadow-call rewrite persists the bounded, redacted original helper model as
324+
`shadowCallRewrittenFrom`, so helper traffic remains identifiable after restart without storing
325+
request content or inferring a helper subtype from timing.
323326
`src/usage/summary.ts` turns that file into the `/api/usage` shape — totals, daily zero-filled
324327
grid, model and provider breakdowns, and `measured / reported / unreported / unsupported / estimated` counts.
325328
A Codex-surface response also includes an `accounts` breakdown keyed by the stable non-PII

‎tests/request-log.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,39 @@ describe("request log metadata", () => {
258258
expect(captured2[0]).not.toHaveProperty("firstOutputMs");
259259
});
260260

261+
test("persists the shadow helper source marker to usage.jsonl", () => {
262+
const home = mkdtempSync(join(tmpdir(), "ocx-shadow-usage-"));
263+
const previousHome = process.env.OPENCODEX_HOME;
264+
const rawSecret = `sk-proj-${"x".repeat(96)}`;
265+
const unsafeMarker = `gpt-5.6-luna\r\nBearer ${rawSecret}\u0007 ${"tail".repeat(32)}`;
266+
process.env.OPENCODEX_HOME = home;
267+
try {
268+
clearRequestLogsForTests();
269+
resetUsageReadCacheForTests();
270+
addFinalRequestLog("ocx-shadow-marker", 1, {
271+
model: "grok-4.5",
272+
provider: "xai",
273+
requestedModel: "gpt-5.6-luna",
274+
shadowCallRewrittenFrom: unsafeMarker,
275+
}, 200);
276+
277+
const [persisted] = readUsageEntries();
278+
const persistedMarker = persisted?.shadowCallRewrittenFrom;
279+
const inMemoryMarker = getRequestLogEntries()[0]?.shadowCallRewrittenFrom;
280+
expect(persistedMarker).toBe(inMemoryMarker);
281+
expect(persistedMarker).toBeDefined();
282+
expect(persistedMarker!.length).toBeLessThanOrEqual(64);
283+
expect(persistedMarker).not.toContain(rawSecret);
284+
expect(persistedMarker).not.toMatch(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/);
285+
} finally {
286+
clearRequestLogsForTests();
287+
if (previousHome === undefined) delete process.env.OPENCODEX_HOME;
288+
else process.env.OPENCODEX_HOME = previousHome;
289+
resetUsageReadCacheForTests();
290+
rmSync(home, { recursive: true, force: true });
291+
}
292+
});
293+
261294
test("records ordered attempts with sealed identity, fresh estimates, and deduplicated recoveries", () => {
262295
const a = beginRequestAttempt(1, "provisional-a", "model-a", "openai-chat");
263296
noteAttemptSend(a, 100);
@@ -1312,6 +1345,7 @@ describe("request log restart hydrate", () => {
13121345
provider: "chatgpt-pabcdef",
13131346
model: "gpt-5.6-sol",
13141347
requestedModel: "gpt-5.6-sol",
1348+
shadowCallRewrittenFrom: "gpt-5.6-luna",
13151349
requestedEffort: "high",
13161350
effectiveEffort: "high",
13171351
reasoningWireField: "reasoning_effort",
@@ -1334,6 +1368,7 @@ describe("request log restart hydrate", () => {
13341368
provider: "chatgpt-pabcdef",
13351369
model: "gpt-5.6-sol",
13361370
requestedModel: "gpt-5.6-sol",
1371+
shadowCallRewrittenFrom: "gpt-5.6-luna",
13371372
requestedEffort: "high",
13381373
effectiveEffort: "high",
13391374
reasoningWireField: "reasoning_effort",
@@ -1381,6 +1416,7 @@ describe("request log restart hydrate", () => {
13811416
terminalStatus: "failed",
13821417
closeReason: "terminal",
13831418
upstreamError: "Provider unreachable",
1419+
shadowCallRewrittenFrom: "gpt-5.6-luna",
13841420
},
13851421
];
13861422

@@ -1392,6 +1428,7 @@ describe("request log restart hydrate", () => {
13921428
errorCode: "upstream_server_error",
13931429
upstreamError: "Provider unreachable",
13941430
requestedEffort: "xhigh",
1431+
shadowCallRewrittenFrom: "gpt-5.6-luna",
13951432
});
13961433

13971434
// Idempotent: a second start in the same process must not duplicate.

‎tests/responses-shadow-intercept.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { join } from "node:path";
1010
import { handleResponses, isShadowSourceModel } from "../src/server/responses";
1111
import { shouldInterceptShadowCall } from "../src/lib/shadow-call";
1212
import { handleManagementAPI } from "../src/server/management-api";
13+
import type { RequestLogContext } from "../src/server/request-log";
1314
import type { OcxConfig } from "../src/types";
1415
import { catalogConvergenceFactory } from "./helpers/catalog-convergence";
1516

@@ -90,7 +91,12 @@ function interceptConfig(): OcxConfig {
9091
} as OcxConfig;
9192
}
9293

93-
async function post(config: OcxConfig, model: string, requestKind?: string): Promise<Response> {
94+
async function post(
95+
config: OcxConfig,
96+
model: string,
97+
requestKind?: string,
98+
logCtx: RequestLogContext = { model: "", provider: "" },
99+
): Promise<Response> {
94100
const headers: Record<string, string> = { "content-type": "application/json" };
95101
if (requestKind) {
96102
headers["x-codex-turn-metadata"] = JSON.stringify({ request_kind: requestKind });
@@ -104,7 +110,7 @@ async function post(config: OcxConfig, model: string, requestKind?: string): Pro
104110
stream: false,
105111
reasoning: { effort: "high" },
106112
}),
107-
}), config, { model: "", provider: "" });
113+
}), config, logCtx);
108114
}
109115

110116
describe("shadow call intercept request path (issue #311)", () => {
@@ -130,6 +136,7 @@ describe("shadow call intercept request path (issue #311)", () => {
130136

131137
test("rewrites a gpt-5.6-luna turn request too (#1684)", async () => {
132138
const bodies: Array<Record<string, unknown>> = [];
139+
const logCtx: RequestLogContext = { model: "", provider: "" };
133140
globalThis.fetch = (async (_url: unknown, init?: RequestInit) => {
134141
bodies.push(JSON.parse(String(init?.body ?? "{}")) as Record<string, unknown>);
135142
return new Response(JSON.stringify({
@@ -138,10 +145,11 @@ describe("shadow call intercept request path (issue #311)", () => {
138145
}), { status: 200, headers: { "content-type": "application/json" } });
139146
}) as typeof fetch;
140147

141-
await post(interceptConfig(), "gpt-5.6-luna", "turn");
148+
await post(interceptConfig(), "gpt-5.6-luna", "turn", logCtx);
142149

143150
expect(bodies.length).toBe(1);
144151
expect(String(bodies[0]?.model ?? "")).toContain("grok-4.5");
152+
expect(logCtx.shadowCallRewrittenFrom).toBe("gpt-5.6-luna");
145153
});
146154

147155
test("leaves gpt-5.6-terra requests unrewritten", async () => {

0 commit comments

Comments
 (0)