Skip to content

The agent that grows with you

fix(delegation): keep fenced/prose-wrapped schema answers valid when redacting

Teknium85086088e792
diff --git a/tests/tools/test_delegated_output_redaction.py b/tests/tools/test_delegated_output_redaction.py
index c45e7697afd..83bc37b222f 100644
--- a/tests/tools/test_delegated_output_redaction.py
+++ b/tests/tools/test_delegated_output_redaction.py
@@ -134,16 +134,23 @@ def test_redacted_schema_verdict_describes_delivered_summary_without_rerunning_c
assert len(child.calls) == 1
-def test_structured_summary_is_redacted_per_value_and_stays_valid_json():
- """A text pass over JSON lets ``KEY=***`` swallow the closing quote and comma."""
+ "wrap",
+ [lambda s: s, lambda s: "```json\n" + s + "\n```", lambda s: "Here is the answer:\n" + s + "\nDone."],
+ ids=["bare", "fenced", "prose"],
+)
+def test_structured_summary_is_redacted_per_value_and_stays_valid_json(wrap):
+ """A text pass over JSON lets ``KEY=***`` swallow the closing quote and comma; every
+ wrapper the schema validator accepts must survive redaction the same way."""
+ from tools.delegation_output_schema import extract_json_candidate
from tools.delegation_redaction import redact_child_result
answer = {"env": "OPENAI_API_KEY=sk-proj-" + "a" * 40, "n": 2}
entry = redact_child_result(
- {"summary": json.dumps(answer), "schema_valid": True}, {"type": "object"}
+ {"summary": wrap(json.dumps(answer)), "schema_valid": True}, {"type": "object"}
)
- parsed = json.loads(entry["summary"])
+ parsed = json.loads(extract_json_candidate(entry["summary"]))
assert parsed["n"] == 2 and "sk-proj-" + "a" * 40 not in parsed["env"]
assert entry["schema_valid"] is True
diff --git a/tools/delegation_redaction.py b/tools/delegation_redaction.py
index 5a5386944f7..e5ab7c7d185 100644
--- a/tools/delegation_redaction.py
+++ b/tools/delegation_redaction.py
@@ -104,18 +104,27 @@ def redact_child_value(value: Any, *, force: bool = True, _depth: int = 0) -> An
def _redact_summary(summary: Any, structured: bool, force: bool) -> Any:
- """Structured (schema) summaries that parse as JSON are redacted per value and
- re-serialized: a text pass can eat JSON delimiters (``KEY=***`` swallowing the
- closing quote and comma) and break an answer that was valid. Unchanged JSON keeps
- its original formatting; anything that does not parse gets the text pass."""
+ """Structured (schema) summaries are redacted per value and re-serialized: a text
+ pass can eat JSON delimiters (``KEY=***`` swallowing the closing quote and comma)
+ and break an answer that was valid. The JSON is located with the validator's own
+ extraction grammar (code fence / surrounding prose), so every wrapper the validator
+ accepts stays accepted; the wrapper text gets the text pass. Unchanged output keeps
+ its original formatting; anything without a parseable candidate gets the text pass."""
if structured and isinstance(summary, str):
+ from tools.delegation_output_schema import extract_json_candidate
+
+ candidate = extract_json_candidate(summary)
+ start = summary.find(candidate) if candidate else -1
+ parsed: Any = None
try:
- parsed = json.loads(summary)
+ parsed = json.loads(candidate) if start >= 0 else None
except ValueError:
- pass
- else:
+ start = -1
+ if start >= 0:
+ prefix, suffix = summary[:start], summary[start + len(candidate):]
cleaned = redact_child_value(parsed, force=force)
- return summary if cleaned == parsed else json.dumps(cleaned, ensure_ascii=False)
+ body = candidate if cleaned == parsed else json.dumps(cleaned, ensure_ascii=False)
+ return redact_child_text(prefix, force=force) + body + redact_child_text(suffix, force=force)
return redact_child_value(summary, force=force)