fix(reset): record captured old_id in spec, exit on signals, and reject unknown spec agents (#78)

This commit is contained in:
Godopu committed 2026-10-29 12:52:17 +00:00
1 parent fb1895030f
commit b5040cffab
4 files changed
+342 -9

No files matched your search

+58 -4
View File
@@ -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)
@@ -104,7 +104,15 @@ esac
_release_reset_lock() {
mam_release_loop_lock "$MARKER" || true
}
trap _release_reset_lock EXIT INT TERM HUP
_on_reset_signal() {
local sig_rc="$1"
_release_reset_lock
exit "$sig_rc"
}
trap _release_reset_lock EXIT
trap '_on_reset_signal 130' INT
trap '_on_reset_signal 143' TERM
trap '_on_reset_signal 129' HUP
mam_update_admit_release
STATE_JSON=""
@@ -199,6 +207,12 @@ for target_name in ${ENTRY_NAMES[@]+"${ENTRY_NAMES[@]}"}; do
FAILED_SPEC="$R_NAME:stop:1"
break
fi
if [ -z "$R_OLD_ID" ]; then
MAM_STATE_JSON="$STATE_JSON" "$PY" -m lib_py.reset record-id --workspace "$WS" --session "$R_NAME" || {
FAILED_SPEC="$R_NAME:stop:1"
break
}
fi
entry_line="$(MAM_STATE_JSON="$STATE_JSON" "$PY" -m lib_py.reset entries --workspace "$WS" --session "$R_NAME")" || {
FAILED_SPEC="$R_NAME:stop:1"
break