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

+148
View File
@@ -1092,8 +1092,156 @@ def test_orc_onboard_codex_flag_tolerant_regex():
assert res.stdout.strip() == "12345678-1234-1234-1234-123456789abc"
def test_stop_session_codex_sends_c_c_chord_not_text_prompt(mam_sandbox, mock_herdr, mock_agents):
"""R1: Verify graceful stop sends C-c key chord rather than text prompt for codex."""
import subprocess, json
# 1. Create a codex session
create_script = mam_sandbox / ".agents" / "skills" / "multi-agent-mux-create" / "scripts" / "create_session.sh"
res = subprocess.run([
"bash", str(create_script),
"--workspace", str(mam_sandbox),
"--agent", "codex",
"--role", "creator"
], capture_output=True, text=True, cwd=str(mam_sandbox))
assert res.returncode == 0, res.stderr
yaml_path = mam_sandbox / ".mam" / "agent-sessions.yaml"
import yaml
with open(yaml_path) as f:
data = yaml.safe_load(f)
session_name = data["herdr_sessions"][0]["name"]
# 2. Stop the session
stop_script = mam_sandbox / ".agents" / "skills" / "multi-agent-mux-stop" / "scripts" / "stop_session.sh"
res_stop = subprocess.run([
"bash", str(stop_script),
"--session", session_name,
"--agent", "codex"
], capture_output=True, text=True, cwd=str(mam_sandbox))
assert res_stop.returncode == 0, res_stop.stderr
# 3. Verify mock_herdr recorded C-c send-keys call and did NOT record Ctrl+C prompt
with open(mock_herdr) as f:
state = json.load(f)
calls = state.get("calls", [])
pane_send_keys = [c for c in calls if "send-keys" in c and "C-c" in c]
agent_prompts = [c for c in calls if "prompt" in c and any("Ctrl+C" in str(arg) for arg in c)]
assert len(pane_send_keys) >= 1
assert len(agent_prompts) == 0
def test_status_resume_on_disk_codex_stale_and_valid(mam_sandbox):
"""R5: Verify status.sh resume_on_disk reports MISSING for stale DB rows and yes for existing files."""
import sqlite3, json, subprocess
codex_home = mam_sandbox / ".codex"
codex_home.mkdir(parents=True, exist_ok=True)
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)")
# 1. Stale row: rollout_path does not exist
stale_uuid = "stale-1234-1234-1234-123456789abc"
conn.execute("INSERT INTO threads (id, cwd, rollout_path) VALUES (?, ?, ?)",
(stale_uuid, str(mam_sandbox), str(codex_home / "nonexistent.jsonl")))
# 2. Valid row: rollout_path exists
valid_uuid = "valid-1234-1234-1234-123456789abc"
valid_file = codex_home / f"rollout-{valid_uuid}.jsonl"
valid_file.write_text(json.dumps({"payload": {"id": valid_uuid, "cwd": str(mam_sandbox)}}) + "\n")
conn.execute("INSERT INTO threads (id, cwd, rollout_path) VALUES (?, ?, ?)",
(valid_uuid, str(mam_sandbox), str(valid_file)))
conn.commit()
conn.close()
yaml_path = mam_sandbox / ".mam" / "agent-sessions.yaml"
yaml_path.write_text(f"""herdr_sessions:
- name: test-stale-codex
status: running
codex_session_id_own: {stale_uuid}
pane:
cwd: {mam_sandbox}
- name: test-valid-codex
status: running
codex_session_id_own: {valid_uuid}
pane:
cwd: {mam_sandbox}
""")
script_path = mam_sandbox / "skills" / "multi-agent-mux-status" / "scripts" / "status.sh"
env = {**os.environ, "CODEX_HOME": str(codex_home)}
res = subprocess.run(["bash", str(script_path), "--json"], capture_output=True, text=True, env=env)
assert res.returncode == 0
data = json.loads(res.stdout)
details = {d["name"]: d["resume_state"] for d in data["sessions_detail"]}
assert details["test-stale-codex"] == "MISSING"
assert details["test-valid-codex"] == "yes"
def test_reconcile_drift_b_unmanaged_session_ignored(mam_sandbox, mock_herdr):
"""R6 / T2: Verify drift-B ignores unmanaged in-workspace sessions while registering managed positive control."""
import subprocess, json, yaml
reconcile_script = mam_sandbox / ".agents" / "skills" / "multi-agent-mux-monitor" / "scripts" / "reconcile.sh"
assert reconcile_script.exists()
# Create dummy yaml with no sessions
yaml_path = mam_sandbox / ".mam" / "agent-sessions.yaml"
yaml_path.parent.mkdir(parents=True, exist_ok=True)
yaml_path.write_text("herdr_sessions: []\n")
unmanaged_name = "personal-session"
managed_name = "test-proj-creator-codex"
# Seed mock_herdr with an unmanaged session and a managed control session running codex in workspace
state = {
"workspaces": [{"workspace_id": "w1", "label": "default", "cwd": str(mam_sandbox)}],
"agents": {
unmanaged_name: {
"name": unmanaged_name,
"status": "running",
"cwd": str(mam_sandbox),
"command": "codex",
"pid": 12345
},
managed_name: {
"name": managed_name,
"status": "running",
"cwd": str(mam_sandbox),
"command": "codex",
"pid": 54321
}
},
"calls": []
}
with open(mock_herdr, "w") as f:
json.dump(state, f)
# Assert candidate is visible to listing and pane metadata lookup before running reconcile
r_ls = subprocess.run(["herdr", "ls", "-F", "#{session_name}|#{session_created}"], capture_output=True, text=True)
assert r_ls.returncode == 0
assert unmanaged_name in r_ls.stdout
assert managed_name in r_ls.stdout
r_pm = subprocess.run(["herdr", "list-panes", "-t", unmanaged_name, "-F", "#{pane_pid}|#{pane_current_path}|#{pane_current_command}"], capture_output=True, text=True)
assert r_pm.returncode == 0
assert f"12345|{str(mam_sandbox)}|codex" in r_pm.stdout
res = subprocess.run(["bash", str(reconcile_script), "--once", "--emit-diff"],
capture_output=True, text=True, cwd=str(mam_sandbox),
env={**os.environ, "WORKSPACE_ROOT": str(mam_sandbox)})
assert res.returncode == 0, res.stderr
out = json.loads(res.stdout)
drifts = out.get("drifts", [])
# Personal session must NOT be registered as drift B
assert not any(d.get("name") == unmanaged_name for d in drifts)
# Managed positive control MUST be registered as drift B
b_registered = [d.get("name") for d in drifts if d.get("class") == "B"]
assert managed_name in b_registered
# Verify YAML updated with managed session but without unmanaged session
with open(yaml_path) as f:
data = yaml.safe_load(f)
registered_names = [s.get("name") for s in data.get("herdr_sessions", [])]
assert unmanaged_name not in registered_names
assert managed_name in registered_names