Skip to content

Commit 0df8335

Browse files
fix(logs): keep literal codex://threads session ids searchable
Devin Review on #711: a client that literally sends a codex://threads/… session id has it hashed whole for storage, but unwrapping the paste before hashing meant that digest could never match again — the unwrap must add a match path, not replace one. logConversationQueryCandidates now yields the unwrapped id AND the untouched paste; the server matcher tries each (direct + digest) and the GUI hashes every candidate so filterLogs gets both digests. Co-Authored-By: Epinephrine <[email protected]>
1 parent 6d8225f commit 0df8335

7 files changed

Lines changed: 75 additions & 25 deletions

File tree

‎gui/src/log-conversation-id.ts‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,36 @@ export function unwrapLogConversationQuery(query: string): string {
4040
return segment !== "" && !/[/\s]/.test(segment) ? segment : trimmed;
4141
}
4242

43-
/** SHA-256 hex prefix used as the persisted conversation id. */
44-
export async function hashLogConversationQuery(raw: string): Promise<string | undefined> {
45-
const trimmed = unwrapLogConversationQuery(raw);
46-
if (!trimmed) return undefined;
47-
if (hasControlChars(trimmed)) return undefined;
48-
if (trimmed.length > 4096) return undefined;
49-
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(trimmed));
50-
return toHex(digest).slice(0, LOG_CONVERSATION_ID_LEN);
43+
/**
44+
* The candidate ids a conversation query may legitimately match: the unwrapped link id
45+
* and the untouched paste — a client may literally send a `codex://threads/…` session
46+
* id, hashed whole for storage. Mirrors the server matcher's candidate set.
47+
*/
48+
function logConversationQueryCandidates(query: string): string[] {
49+
const trimmed = query.trim();
50+
const unwrapped = unwrapLogConversationQuery(trimmed);
51+
return unwrapped === trimmed ? [unwrapped] : [unwrapped, trimmed];
52+
}
53+
54+
/** SHA-256 hex prefixes used as persisted conversation ids, one per query candidate. */
55+
export async function hashLogConversationQuery(raw: string): Promise<string[]> {
56+
const hashes: string[] = [];
57+
for (const candidate of logConversationQueryCandidates(raw)) {
58+
if (!candidate || hasControlChars(candidate) || candidate.length > 4096) continue;
59+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(candidate));
60+
hashes.push(toHex(digest).slice(0, LOG_CONVERSATION_ID_LEN));
61+
}
62+
return hashes;
5163
}
5264

5365
export function matchesLogConversationId(
5466
stored: string | undefined,
5567
query: string,
56-
queryHash?: string,
68+
queryHash?: readonly string[],
5769
): boolean {
5870
if (!stored) return false;
59-
const trimmed = unwrapLogConversationQuery(query);
60-
if (!trimmed) return false;
61-
if (stored === trimmed) return true;
62-
return queryHash !== undefined && stored === queryHash;
71+
const candidates = logConversationQueryCandidates(query);
72+
if (!candidates.some(Boolean)) return false;
73+
if (candidates.includes(stored)) return true;
74+
return queryHash !== undefined && queryHash.includes(stored);
6375
}

‎gui/src/pages/Logs.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,11 @@ export default function Logs({ apiBase }: { apiBase: string }) {
723723
setFilters(prev => prev.conversationQueryHash === undefined ? prev : { ...prev, conversationQueryHash: undefined });
724724
return;
725725
}
726-
void hashLogConversationQuery(conversationQuery).then(hash => {
727-
if (!cancelled) setFilters(prev => prev.conversationQueryHash === hash ? prev : { ...prev, conversationQueryHash: hash });
726+
void hashLogConversationQuery(conversationQuery).then(hashes => {
727+
if (!cancelled) setFilters(prev =>
728+
prev.conversationQueryHash?.join(" ") === hashes.join(" ")
729+
? prev
730+
: { ...prev, conversationQueryHash: hashes });
728731
});
729732
return () => { cancelled = true; };
730733
}, [conversationQuery]);

‎gui/src/pages/logs-filter.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface LogFilterState {
2222
maxTokPerSec?: number;
2323
interceptedOnly: boolean;
2424
conversationId: string;
25-
conversationQueryHash?: string;
25+
conversationQueryHash?: readonly string[];
2626
/** Absent means "all", so filter states saved before this field existed stay valid. */
2727
protocolMode?: LogProtocolModeFilter;
2828
}

‎gui/tests/logs-filter-bar.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ test.each(["pointer", "keyboard"] as const)("LogsFilterBar %s reset restores foc
125125
await withFilterBar({
126126
...DEFAULT_LOG_FILTER_STATE, surface: "grok", status: "errors", provider: "xai",
127127
model: "model-a", timeWindow: "1h", minTokPerSec: 50, interceptedOnly: true,
128-
conversationId: "conversation-a", conversationQueryHash: "cached-hash",
128+
conversationId: "conversation-a", conversationQueryHash: ["cached-hash"],
129129
}, async ui => {
130130
expect(ui.container.textContent).toContain("Showing 1 of 2");
131131
const reset = ui.container.querySelector<HTMLButtonElement>(".logs-filter-status button")!;

‎gui/tests/logs-filter.test.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,19 @@ describe("codex://threads paste in the conversation filter", () => {
205205
expect(unwrapLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}?hostId=durable`)).toBe(CODEX_THREAD_ID);
206206
expect(unwrapLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}?hostId=remote-control%3Aexample-environment`)).toBe(CODEX_THREAD_ID);
207207
expect(unwrapLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}#frag`)).toBe(CODEX_THREAD_ID);
208-
expect(await hashLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}`)).toBe(digest32(CODEX_THREAD_ID));
209-
expect(await hashLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}?hostId=durable`)).toBe(digest32(CODEX_THREAD_ID));
208+
expect(await hashLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}`))
209+
.toEqual([digest32(CODEX_THREAD_ID), digest32(`codex://threads/${CODEX_THREAD_ID}`)]);
210+
expect(await hashLogConversationQuery(`codex://threads/${CODEX_THREAD_ID}?hostId=durable`))
211+
.toEqual([digest32(CODEX_THREAD_ID), digest32(`codex://threads/${CODEX_THREAD_ID}?hostId=durable`)]);
212+
expect(await hashLogConversationQuery(CODEX_THREAD_ID)).toEqual([digest32(CODEX_THREAD_ID)]);
213+
});
214+
215+
test("a literal codex://threads id a client sent still matches its whole-string digest", async () => {
216+
const uri = `codex://threads/${CODEX_THREAD_ID}`;
217+
const rows = [{ id: "literal", conversationId: digest32(uri) }];
218+
const queryHash = await hashLogConversationQuery(uri);
219+
expect(filterLogs(rows, { ...DEFAULT_LOG_FILTER_STATE, conversationId: uri, conversationQueryHash: queryHash }, NOW)
220+
.map(row => row.id)).toEqual(["literal"]);
210221
});
211222

212223
test("malformed or oversized pastes stay plain queries and never match", () => {
@@ -218,7 +229,7 @@ describe("codex://threads paste in the conversation filter", () => {
218229
expect(filterLogs(rows, {
219230
...DEFAULT_LOG_FILTER_STATE,
220231
conversationId: `codex://threads/${CODEX_THREAD_ID}?hostId=durable`,
221-
conversationQueryHash: digest32(CODEX_THREAD_ID),
232+
conversationQueryHash: [digest32(CODEX_THREAD_ID)],
222233
}, NOW).map(row => row.id)).toEqual(["hashed"]);
223234
});
224235

‎src/server/request-log-conversation.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ export function unwrapLogConversationQuery(query: string): string {
6666
return segment !== "" && !/[/\s]/.test(segment) ? segment : trimmed;
6767
}
6868

69+
/**
70+
* The candidate ids a conversation query may legitimately match. Unwrapping a pasted
71+
* `codex://threads/<id>` link must add a match path, not replace one: a client can
72+
* literally send a `codex://threads/…` session id, which is hashed whole for storage,
73+
* so the untouched paste stays a candidate too.
74+
*/
75+
export function logConversationQueryCandidates(query: string): string[] {
76+
const trimmed = query.trim();
77+
const unwrapped = unwrapLogConversationQuery(trimmed);
78+
return unwrapped === trimmed ? [unwrapped] : [unwrapped, trimmed];
79+
}
80+
6981
/**
7082
* Filter match: accept either the persisted digest or the original preimage
7183
* (so pasting from Logs detail or the client-facing session id both work).
@@ -75,11 +87,13 @@ export function matchesLogConversationId(
7587
query: string | undefined | null,
7688
): boolean {
7789
if (!stored) return false;
78-
const trimmed = unwrapLogConversationQuery(typeof query === "string" ? query : "");
79-
if (!trimmed) return false;
80-
if (stored === trimmed) return true;
81-
const hashed = normalizeLogConversationId(trimmed);
82-
return hashed !== undefined && stored === hashed;
90+
for (const candidate of logConversationQueryCandidates(typeof query === "string" ? query : "")) {
91+
if (!candidate) continue;
92+
if (stored === candidate) return true;
93+
const hashed = normalizeLogConversationId(candidate);
94+
if (hashed !== undefined && stored === hashed) return true;
95+
}
96+
return false;
8397
}
8498

8599
/**

‎tests/usage/request-log-conversation.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ describe("matchesLogConversationId", () => {
8787
}
8888
});
8989

90+
test("a literal codex://threads session id stays findable by its whole-string digest", () => {
91+
const uri = "codex://threads/019f6482-67d5-77c2-a643-02daddaa7115";
92+
const stored = digest32(uri);
93+
expect(matchesLogConversationId(stored, uri)).toBe(true);
94+
const withHost = `${uri}?hostId=durable`;
95+
expect(matchesLogConversationId(digest32(withHost), withHost)).toBe(true);
96+
expect(matchesLogConversationId(stored, withHost)).toBe(false);
97+
expect(matchesLogConversationId(stored, "codex://threads/other-thread")).toBe(false);
98+
});
99+
90100
test("rejects malformed or oversized codex://threads pastes without backtracking", () => {
91101
const stored = digest32("019f6482-67d5-77c2-a643-02daddaa7115");
92102
const slashFlood = `codex://threads/${"/".repeat(4000)}\u2028x`;

0 commit comments

Comments
 (0)