fix(codex): resolve peer-review findings R1-R6 and T1-T2

- R1: use herdr send-keys C-c for graceful stop
- R2: preserve JSONL fallback discovery when database is empty
- R3: apply workspace-scoped SQLite query before bounding results
- R4: tighten artifact validation to fail closed on missing/invalid metadata
- R5: verify actual rollout presence before reporting resumable in status
- R6: remove unconditional drift-B backend command fallback
- T1: parameterize artifact metadata regression with verified finder fixture
- T2: seed unmanaged and managed sessions in mock herdr for drift-B testing
- Add final loop review reports (100% PASS)
This commit is contained in:
Godopu committed 2026-09-06 00:18:19 +09:00
1 parent 76f10b2096
commit 47640e2fd7
10 files changed
+504 -34

No files matched your search

+165
View File
@@ -995,7 +995,172 @@ def test_codex_reconcile_full_block_integration(tmp_path):
assert valid_candidates == ['u-target']
def test_codex_verify_artifact_metadata_fail_closed(tmp_path, monkeypatch):
"""R4 / T1: Verify verify_artifact fails closed on missing/invalid metadata, id mismatch, or db inconsistency."""
import json, sqlite3
from lib_py.agents.registry import get_adapter
from lib_py.agents.base import DiscoveryContext
ws = str(tmp_path / "ws")
home = str(tmp_path / "home")
os.makedirs(ws, exist_ok=True)
os.makedirs(home, exist_ok=True)
adapter = get_adapter('codex')
ctx = DiscoveryContext(workspace=ws, agent_name='codex', home_dir=home)
uuid = "12345678-1234-1234-1234-123456789abc"
rollout_file = tmp_path / "rollout.jsonl"
rollout_file.write_text("{}\n")
# Install finder override using pytest monkeypatch before all cases
monkeypatch.setattr(adapter, "_find_rollout", lambda u, c: str(rollout_file))
# Assert finder resolves the fixture before testing malformed contents
resolved_path = adapter._find_rollout(uuid, ctx)
assert resolved_path == str(rollout_file)
assert os.path.exists(resolved_path)
# Parameterized invalid metadata cases
invalid_payloads = [
"{}", # Empty dict (R4 finding)
json.dumps({"payload": {"cwd": ws}}), # Missing payload.id
json.dumps({"payload": {"id": uuid}}), # Missing payload.cwd
json.dumps({"payload": {"id": "other-id", "cwd": ws}}), # Mismatched session id
json.dumps({"payload": {"id": uuid, "cwd": "/wrong/path"}}), # Mismatched workspace
"not-valid-json", # Non-JSON content
json.dumps([]), # Non-dict JSON
json.dumps({"payload": "not-a-dict"}), # Non-dict payload
]
for inv in invalid_payloads:
rollout_file.write_text(inv + "\n")
assert adapter.verify_artifact(uuid, ctx) is False, f"Expected False for payload: {inv}"
# Database / transcript metadata inconsistency check
db_path = tmp_path / "state_5.sqlite"
conn = sqlite3.connect(str(db_path))
conn.execute("CREATE TABLE threads (id TEXT PRIMARY KEY, cwd TEXT, rollout_path TEXT)")
conn.execute("INSERT INTO threads VALUES (?, ?, ?)", (uuid, "/conflicting/db/cwd", str(rollout_file)))
conn.commit()
conn.close()
monkeypatch.setattr(adapter, "_state_db", lambda c: str(db_path))
# Even if transcript has valid ws, DB cwd conflict must fail closed
rollout_file.write_text(json.dumps({"payload": {"id": uuid, "cwd": ws}}) + "\n")
assert adapter.verify_artifact(uuid, ctx) is False
# Valid control case (matching DB cwd and transcript cwd)
conn = sqlite3.connect(str(db_path))
conn.execute("UPDATE threads SET cwd=? WHERE id=?", (ws, uuid))
conn.commit()
conn.close()
assert adapter.verify_artifact(uuid, ctx) is True
def test_codex_discover_empty_stale_db_fallback(tmp_path):
"""R2: Verify discover falls back to JSONL transcripts when DB is empty or has stale rows."""
import json, sqlite3, time
from lib_py.agents.registry import get_adapter
from lib_py.agents.base import DiscoveryContext
ws = str(tmp_path / "ws")
home = str(tmp_path / "home")
os.makedirs(ws, exist_ok=True)
os.makedirs(home, exist_ok=True)
codex_home = tmp_path / "home" / ".codex"
os.makedirs(codex_home, exist_ok=True)
adapter = get_adapter('codex')
ctx = DiscoveryContext(workspace=ws, agent_name='codex', home_dir=home)
# Create a valid date-nested rollout on disk
uuid_valid = "11111111-1111-1111-1111-111111111111"
sessions_dir = codex_home / "sessions" / "2026" / "09" / "05"
sessions_dir.mkdir(parents=True, exist_ok=True)
valid_file = sessions_dir / f"rollout-2026-09-05T00-00-00-{uuid_valid}.jsonl"
valid_file.write_text(json.dumps({"payload": {"id": uuid_valid, "cwd": ws}}) + "\n")
# Empty DB: threads table exists with 0 rows
db_path = codex_home / "state_5.sqlite"
conn = sqlite3.connect(str(db_path))
conn.execute("CREATE TABLE threads (id TEXT PRIMARY KEY, cwd TEXT, rollout_path TEXT, updated_at INTEGER)")
conn.commit()
conn.close()
# Should fall through to JSONL file and discover uuid_valid
found = adapter.discover(ctx)
assert uuid_valid in found
# Stale DB: insert row pointing to nonexistent file
conn = sqlite3.connect(str(db_path))
conn.execute("INSERT INTO threads (id, cwd, rollout_path, updated_at) VALUES (?, ?, ?, ?)",
("stale-uuid", ws, str(codex_home / "nonexistent.jsonl"), int(time.time())))
conn.commit()
conn.close()
found_stale = adapter.discover(ctx)
assert uuid_valid in found_stale
def test_codex_discover_workspace_scoping_over_50_foreign_rows(tmp_path):
"""R3: Verify global LIMIT does not hide target-workspace threads behind >50 foreign threads."""
import json, sqlite3, time
from pathlib import Path
from lib_py.agents.registry import get_adapter
from lib_py.agents.base import DiscoveryContext
ws_target = str(tmp_path / "ws_target")
ws_foreign = str(tmp_path / "ws_foreign")
home = str(tmp_path / "home")
os.makedirs(ws_target, exist_ok=True)
os.makedirs(ws_foreign, exist_ok=True)
os.makedirs(home, exist_ok=True)
adapter = get_adapter('codex')
ctx_target = DiscoveryContext(workspace=ws_target, agent_name='codex', home_dir=home)
codex_home = Path(home) / ".codex"
sessions_dir = codex_home / "sessions" / "2026" / "09" / "05"
sessions_dir.mkdir(parents=True, exist_ok=True)
db_path = codex_home / "state_5.sqlite"
conn = sqlite3.connect(str(db_path))
conn.execute("CREATE TABLE IF NOT EXISTS threads (id TEXT PRIMARY KEY, cwd TEXT, rollout_path TEXT, updated_at INTEGER)")
now = int(time.time())
# Seed 1 valid target thread
target_uuid = "target-1234-1234-1234-123456789abc"
target_file = sessions_dir / f"rollout-001-{target_uuid}.jsonl"
target_file.write_text(json.dumps({"payload": {"id": target_uuid, "cwd": ws_target}}) + "\n")
conn.execute("INSERT INTO threads (id, cwd, rollout_path, updated_at) VALUES (?, ?, ?, ?)",
(target_uuid, ws_target, str(target_file), now - 100))
# Seed 60 newer foreign threads
for i in range(60):
f_uuid = f"foreign-{i:04d}-1234-1234-123456789abc"
f_file = sessions_dir / f"rollout-001-{f_uuid}.jsonl"
f_file.write_text(json.dumps({"payload": {"id": f_uuid, "cwd": ws_foreign}}) + "\n")
conn.execute("INSERT INTO threads (id, cwd, rollout_path, updated_at) VALUES (?, ?, ?, ?)",
(f_uuid, ws_foreign, str(f_file), now + i))
conn.commit()
conn.close()
candidates = adapter.discover(ctx_target)
assert target_uuid in candidates
def test_codex_discover_symlink_canonical_alias(tmp_path):
"""R3: Verify discover resolves symlink workspace aliases correctly."""
from lib_py.agents.registry import get_adapter
from lib_py.agents.base import DiscoveryContext
real_ws = tmp_path / "real_ws"
real_ws.mkdir()
symlink_ws = tmp_path / "symlink_ws"
symlink_ws.symlink_to(real_ws, target_is_directory=True)
home = str(tmp_path / "home")
os.makedirs(home, exist_ok=True)
adapter = get_adapter('codex')
ctx = DiscoveryContext(workspace=str(symlink_ws), agent_name='codex', home_dir=home)
uuid = "sym-1234-1234-1234-123456789abc"
_seed_codex_session(home, uuid, str(real_ws), 2000.0)
candidates = adapter.discover(ctx)
assert uuid in candidates