From 95b542ec287bfdf269278f4416facee740c9f463 Mon Sep 17 00:00:00 2001 From: Pravin Kamble Date: Tue, 10 Nov 2026 09:56:46 +0530 Subject: [PATCH] DRU-1092 -- Strip OpenAI citation markers from Codex output --- backend/druks/harnesses/codex.py | 7 +++++++ backend/tests/test_cost_capture.py | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/backend/druks/harnesses/codex.py b/backend/druks/harnesses/codex.py index b50b71ab..ff17a55d 100644 --- a/backend/druks/harnesses/codex.py +++ b/backend/druks/harnesses/codex.py @@ -2,6 +2,7 @@ import json import logging import os +import re import shlex from collections.abc import Collection, Iterator from dataclasses import dataclass @@ -43,6 +44,9 @@ _TOKEN_COUNT_MARKERS = ('"type":"token_count"', '"type": "token_count"') _CHATGPT_HOST = "chatgpt.com" _SUBSCRIPTION_TOKEN = "codex_subscription_token" +# OpenAI web search leaves this citation in model text. The pattern runs on +# serialized JSON. It stops at a quote, so a match stays in one string. +_CITATION_MARKER = re.compile(r' *\ue200cite(?:\ue202[^\ue201"]*)?\ue201') @dataclass(frozen=True) @@ -566,6 +570,9 @@ def parse(self, result: HarnessRunResult, *, artifact_dir: Path, run_id: str) -> call_dir / "output.json", name=self.name, ) + payload = json.loads( + _CITATION_MARKER.sub("", json.dumps(payload, ensure_ascii=False)), + ) (call_dir / "output.json").write_text( json.dumps(payload, indent=2, sort_keys=True), ) diff --git a/backend/tests/test_cost_capture.py b/backend/tests/test_cost_capture.py index e2e90012..155f50ee 100644 --- a/backend/tests/test_cost_capture.py +++ b/backend/tests/test_cost_capture.py @@ -138,6 +138,29 @@ def test_failed_codex_call_writes_cost_before_raising(tmp_path: Path): assert metadata["output_tokens"] == 1_000 +def test_codex_parse_strips_citation_markers(tmp_path: Path): + call_dir = tmp_path / "call" + call_dir.mkdir() + citation = "\ue200cite\ue202turn905355view5\ue202turn430332view7\ue201" + output = { + "summary": f"Calls can require fresh execution. {citation}", + "sources": [f"Docs {citation} say so.", "\ue200cite\ue201"], + } + (call_dir / "output.json").write_text(json.dumps(output)) + harness = CodexHarness(model="openai/gpt-5.5", fast_mode=False, effort=None) + + payload = harness.parse( + HarnessRunResult(returncode=0, stdout=b"", stderr=b""), + artifact_dir=tmp_path, + run_id="call", + ) + + assert payload == { + "summary": "Calls can require fresh execution.", + "sources": ["Docs say so.", ""], + } + + def test_failed_pi_call_writes_cost_before_raising(tmp_path: Path): message = { "type": "message_end",