Skip to content

Commit 0ce0eae

Browse files
authored
Merge pull request #7784 from loopx-project/codex/context-failed-tool-read-20261101
fix(context): reject reader pages from failed MCP results
2 parents 0e78a19 + 9839182 commit 0ce0eae

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

‎loopx/control_plane/turn_driver/codex_context.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ def _context_pages(value: object) -> list[dict[str, Any]]:
365365
"""Decode native shell/MCP JSON records; never classify arbitrary prose.
366366
367367
Incomplete JSON has no successful page. The pending delivery stays pending.
368+
Enclosing tool failures override embedded successful reader pages.
368369
Native output truncation that is not reported here remains an assumption gap.
369370
"""
370371
if isinstance(value, str):
@@ -391,14 +392,15 @@ def _context_pages(value: object) -> list[dict[str, Any]]:
391392
return [value]
392393
if isinstance(value.get("output"), str):
393394
pages = _context_pages(value["output"])
394-
if pages and "exit_code" in value and value["exit_code"] != 0:
395-
return [{"schema_version": "context_capture_page_v1", "ok": False}]
396-
return pages
397-
content = value.get("content")
398-
if isinstance(content, list):
399-
return [page for item in content if isinstance(item, dict)
400-
for page in _context_pages(item.get("text"))]
401-
return []
395+
failed = "exit_code" in value and value["exit_code"] != 0
396+
else:
397+
content = value.get("content")
398+
pages = [page for item in content if isinstance(item, dict)
399+
for page in _context_pages(item.get("text"))] if isinstance(content, list) else []
400+
failed = False
401+
if pages and (failed or value.get("isError") is True):
402+
return [{"schema_version": "context_capture_page_v1", "ok": False}]
403+
return pages
402404

403405

404406
def _verified_context_pages(state: dict[str, Any], pages: list[dict[str, Any]]) -> list[dict[str, Any]]:

‎tests/control_plane/test_codex_context_lifecycle.py‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ def test_complete_reader_delivery_allows_delta_without_diagnostics_or_ack(tmp_pa
175175
context.codex_context_hook(str(tmp_path), {**event, "hook_event_name": name,
176176
"tool_response": "ordinary tool output"})
177177
assert context._state(tmp_path) == state
178+
context.codex_context_hook(str(tmp_path), {**event, "hook_event_name": "PostToolUse",
179+
"tool_response": {"isError": True, "content": [{"type": "text", "text": "Unrelated tool failed."}]}})
180+
assert context._state(tmp_path) == state
178181
_, second = project(tmp_path, "two", payload=payload)
179182
assert second["mode"] == "delta" and second["continuity_basis"] == "captured_file"
180183
assert not (tmp_path / "traces").exists()
@@ -443,7 +446,8 @@ def test_known_incomplete_delivery_cannot_establish_baseline(tmp_path, monkeypat
443446

444447

445448
@pytest.mark.parametrize("mode", ["full", "delta"])
446-
@pytest.mark.parametrize("damage", ["changed_bytes", "missing_file", "changed_file", "symlink"])
449+
@pytest.mark.parametrize("damage", ["changed_bytes", "missing_file", "changed_file", "symlink",
450+
"failed_mcp", "failed_mcp_nested"])
447451
def test_hook_cli_rejects_changed_page_bytes_and_recovers_original(tmp_path, monkeypatch, mode, damage):
448452
event = setup(tmp_path, monkeypatch)
449453
payload = {"ok": True, "plan_summary": {"requirements": "Preserve every obligation. " * 1000}}
@@ -469,7 +473,7 @@ def test_hook_cli_rejects_changed_page_bytes_and_recovers_original(tmp_path, mon
469473
assert json.loads("".join(page["content"] for page in changed))
470474
elif damage == "changed_file":
471475
source.write_bytes(source_raw + b" ") # valid JSON, different pinned digest
472-
else:
476+
elif damage in {"missing_file", "symlink"}:
473477
source.unlink()
474478
if damage == "symlink":
475479
alias_target = tmp_path / "same-bytes.json"
@@ -481,18 +485,21 @@ def test_hook_cli_rejects_changed_page_bytes_and_recovers_original(tmp_path, mon
481485
"--format", "json", "quota", "context-hook", "--context-delivery", "on", "--codex-app",
482486
"--codex-context-dir", str(tmp_path)]
483487

484-
def hook(values):
485-
response = {"content": [{"type": "text", "text": json.dumps(page)} for page in values]}
488+
def hook(values, *, failed=False, nested=False):
489+
response = {"isError": failed,
490+
"content": [{"type": "text", "text": json.dumps(page)} for page in values]}
491+
if nested:
492+
response = {"exit_code": 0, "output": json.dumps(response)}
486493
result = subprocess.run(cmd, input=json.dumps({**event, "hook_event_name": "PostToolUse",
487494
"tool_use_id": "capture-read", "tool_response": response}), capture_output=True, text=True,
488495
cwd=tmp_path, env={**os.environ, "PYTHONPATH": str(repo)}, timeout=20)
489496
assert result.returncode == 0, result.stderr
490497
assert json.loads(result.stdout) == {}
491498

492-
hook(changed)
499+
hook(changed, failed=damage.startswith("failed_mcp"), nested=damage == "failed_mcp_nested")
493500
assert "candidate" not in context._state(tmp_path)
494501
assert context._state(tmp_path)["recovery"] == recovery
495-
if damage != "changed_bytes":
502+
if damage in {"missing_file", "changed_file", "symlink"}:
496503
source.unlink(missing_ok=True)
497504
source.write_bytes(source_raw)
498505
source.chmod(0o600)

0 commit comments

Comments
 (0)