fix(reset): record captured old_id in spec, exit on signals, and reject unknown spec agents (#78)
This commit is contained in:
1 parent
fb1895030f
commit
b5040cffab
4 files changed
+342
-9
No files matched your search
@@ -21,7 +21,7 @@ import tempfile
|
||||
from typing import Dict, List, Mapping, Optional, Tuple
|
||||
|
||||
from lib_py.agents.base import AGENT_SESSION_ENV
|
||||
from lib_py.agents.registry import agent_of_row, own_key
|
||||
from lib_py.agents.registry import agent_of_row, get_adapter, own_key
|
||||
from lib_py.update import open_jobs, row_server
|
||||
|
||||
SPEC_VERSION = 1
|
||||
@@ -227,15 +227,57 @@ def read_spec(workspace: str) -> dict:
|
||||
role = item.get("role")
|
||||
if not (isinstance(name, str) and name and isinstance(agent, str) and agent and isinstance(role, str) and role):
|
||||
raise SpecError("spec file %s has an incomplete agent entry; check it, then remove it by hand" % path)
|
||||
try:
|
||||
own_key(agent)
|
||||
except KeyError:
|
||||
adapter = get_adapter(agent)
|
||||
if adapter is None or adapter.name != agent or not own_key(agent):
|
||||
raise SpecError(
|
||||
"spec file %s has unknown agent %r; check it, then remove it by hand" % (path, agent)
|
||||
)
|
||||
return doc
|
||||
|
||||
|
||||
def record_old_id(workspace: str, state: dict, session_name: str) -> Optional[str]:
|
||||
"""Persist a newly captured conversation id onto an entry with old_id=None.
|
||||
|
||||
Called after step 1 (normal stop) captures a POST_DISCOVERED session's id
|
||||
onto the original instance's registry row, so a subsequent resume + rerun
|
||||
cannot mistake the original conversation for a new one (review F1).
|
||||
Reads the id from `state` (never from command-line args).
|
||||
"""
|
||||
ws_real = _real(workspace)
|
||||
spec = read_spec(ws_real)
|
||||
rows_by_name = {
|
||||
str(s.get("name")): s
|
||||
for s in (state.get("herdr_sessions") or [])
|
||||
if isinstance(s, dict) and s.get("name")
|
||||
}
|
||||
row = rows_by_name.get(session_name)
|
||||
if row is None:
|
||||
return None
|
||||
changed = False
|
||||
recorded: Optional[str] = None
|
||||
for entry in spec["agents"]:
|
||||
if str(entry.get("name") or "") != session_name:
|
||||
continue
|
||||
raw_old = entry.get("old_id")
|
||||
old_id = str(raw_old).strip() if isinstance(raw_old, str) and raw_old.strip() else None
|
||||
if old_id:
|
||||
return old_id
|
||||
cur_created_at = str(row.get("herdr_session_created_at") or "")
|
||||
old_created_at = str(entry.get("old_created_at") or "")
|
||||
if cur_created_at != old_created_at:
|
||||
return None
|
||||
key = own_key(entry["agent"])
|
||||
raw_cur = row.get(key)
|
||||
cur_id = str(raw_cur).strip() if isinstance(raw_cur, str) and raw_cur.strip() else None
|
||||
if cur_id:
|
||||
entry["old_id"] = cur_id
|
||||
recorded = cur_id
|
||||
changed = True
|
||||
if changed:
|
||||
write_spec(ws_real, spec["agents"])
|
||||
return recorded
|
||||
|
||||
|
||||
def new_running_rows(workspace: str, state: dict, spec_agents: List[dict]) -> List[str]:
|
||||
"""Names of running control-root rows not present in spec_agents."""
|
||||
known = {str(a.get("name") or "") for a in spec_agents}
|
||||
@@ -652,6 +694,10 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
s.add_argument("--session", default="")
|
||||
s.add_argument("--warn-new", action="store_true")
|
||||
|
||||
s = sub.add_parser("record-id")
|
||||
s.add_argument("--workspace", required=True)
|
||||
s.add_argument("--session", required=True)
|
||||
|
||||
s = sub.add_parser("table")
|
||||
s.add_argument("--workspace", required=True)
|
||||
s.add_argument("--skipped", action="append", default=[])
|
||||
@@ -731,6 +777,14 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
print(format_entry_line(e, rows_by_name.get(e["name"])))
|
||||
return 0
|
||||
|
||||
if a.cmd == "record-id":
|
||||
try:
|
||||
record_old_id(ws, state, a.session)
|
||||
except (SpecError, OSError) as exc:
|
||||
print("ERROR: %s" % exc, file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
|
||||
if a.cmd == "table":
|
||||
try:
|
||||
spec = read_spec(ws)
|
||||
|
||||
Reference in new issue
Block a user