Files
multi-agent-mux/.agents/skills/lib_py/verify_session.py
T
Godopu b4821fafa8 feat(a4,c3b): complete A-4 Phase 2 agent knowledge migration & Option B isolation removal
- Option B / C-3b: Deprecate isolation.root and remove its 4 legacy consumers in lib.sh, workspace_uuid.py, verify_session.py, and stop_session.sh.
- M2~M7 Migration: Implement artifact_path, verify_artifact, purge_artifacts, spawn_spec, resume_spec, auth_ok, discover, ready_tokens, exit_key, and delegate_agent_key across BaseAgentAdapter and all 4 adapters (Claude, Agy, Hermes, Cline).
- Migrate shell duplications in lib.sh, create_session.sh, resume_session.sh, stop_session.sh, and reconcile.sh to python -m lib_py.agents facts.
- Hardening & Corrections: Fix Cline resume_spec flag to '-i --id', apply shlex.quote across all facts variables with MAM_ prefix, add safe fallback in wait_for_tui_ready.
- Add comprehensive contract tests in tests/test_a4_adapter_contract.py and sync IMPROVEMENTS.md / LOG.md.
- Verified: 100% PASS across unit, component, contract, and integration tests.
2026-08-16 23:15:24 +09:00

111 lines
3.9 KiB
Python

# verify_session.py — session UUID verification logic
# Extracted from lib.sh VERIFY_SESSION_PYTHON block
_MAM_ORC_CACHE = None
def mam_orchestrator_uuids():
global _MAM_ORC_CACHE
if _MAM_ORC_CACHE is not None:
return _MAM_ORC_CACHE
import os, sys, json, sqlite3, yaml
override = os.environ.get("MAM_ORCHESTRATOR_UUIDS")
if override is not None:
if not override.strip():
_MAM_ORC_CACHE = []
return _MAM_ORC_CACHE
if override.strip().startswith("["):
try:
parsed = json.loads(override)
if isinstance(parsed, list):
_MAM_ORC_CACHE = [str(x) for x in parsed if x]
return _MAM_ORC_CACHE
except Exception:
pass
_MAM_ORC_CACHE = [x.strip() for x in override.split(",") if x.strip()]
return _MAM_ORC_CACHE
res = []
d_obj = None
try:
if "MAM_STATE_JSON" in os.environ and os.environ.get("MAM_STATE_JSON"):
d_obj = json.loads(os.environ.get("MAM_STATE_JSON"))
except Exception:
d_obj = None
if d_obj is None or "orchestrator_uuids" not in d_obj:
yaml_p = os.environ.get("AGENT_SESSIONS_YAML") or os.environ.get("YAML_PATH")
if yaml_p:
db_p = os.path.splitext(yaml_p)[0] + ".db"
if os.path.exists(db_p):
try:
conn = sqlite3.connect(db_p, timeout=5.0)
r = conn.execute("SELECT data FROM state WHERE id=1").fetchone()
if r and r[0]:
d_obj = json.loads(r[0])
conn.close()
except Exception:
pass
if (d_obj is None or "orchestrator_uuids" not in d_obj) and os.path.exists(yaml_p):
try:
with open(yaml_p) as f:
d_obj = yaml.safe_load(f) or {}
except Exception:
pass
raw = d_obj.get("orchestrator_uuids") if isinstance(d_obj, dict) else None
if raw is not None:
if isinstance(raw, list) and all(isinstance(x, str) and x.strip() for x in raw):
res = list(raw)
else:
sys.stderr.write("WARNING: malformed orchestrator_uuids in state, disabling orchestrator exclusion gate\n")
res = []
_MAM_ORC_CACHE = res
return _MAM_ORC_CACHE
def mam_row_own_uuid(row):
if not isinstance(row, dict):
return None
for k in ["claude_session_id_own", "agy_conversation_id_own", "hermes_conversation_id_own", "cline_conversation_id_own"]:
v = row.get(k)
if v:
return v
return None
def workspace_key(path):
import os
try:
p = os.path.realpath(path)
except Exception:
p = path
return p.replace("/", "-").replace("_", "-")
from lib_py.paths import resolve_home
def verify_session_uuid(ws, agent, uuid, row=None, home_dir=None, claude_dir=None, mode="discover"):
import os, json, sqlite3
from lib_py.paths import resolve_home
from lib_py.agents.registry import get_adapter
from lib_py.agents.base import DiscoveryContext
row = row or {}
epoch = row.get("herdr_session_epoch", 0) if mode == "discover" else 0
cwd = row.get("pane", {}).get("cwd", "") or ws
if workspace_key(cwd) != workspace_key(ws):
return False
if mode == "discover" and uuid in mam_orchestrator_uuids():
if uuid != mam_row_own_uuid(row):
return False
if (mode == "revalidate" and row.get("session_id_source") == "assigned"
and not row.get("session_id_verified")):
return True
adapter = get_adapter(agent)
if not adapter:
return False
ctx = DiscoveryContext(workspace=ws, agent_name=agent, home_dir=home_dir, claude_dir=claude_dir, epoch=epoch, row=row, mode=mode)
return adapter.verify_artifact(uuid, ctx)