- create_session.sh: add explicit case fallback for delegate_agent (R1) - lib_py.agents: add spawn-spec, resume-spec, exit-key argv CLI subcommands (R2) - resume_session.sh, stop_session.sh: replace python string interpolation with safe argv subcommands (R2) - stop_session.sh: dynamically iterate adapter.identity_cache_fields (R3) - verify_session.py, workspace_uuid.py: remove dead imports (R9/N4) - tests/test_a4_adapter_contract.py: add CLI bridge subcommand, clean-env PYTHONPATH safety, and fallback contract tests (R12/N1) - SKILL.md, docs, logs: synchronize IMPROVEMENTS.md, LOG.md, and resolve_session_id wording (R8/R10/R11) - promote verified peer review reports for cline (e7b9812b) and claude (31730364)
119 lines
4.2 KiB
Python
119 lines
4.2 KiB
Python
# workspace_uuid.py — workspace UUID discovery logic
|
|
# Extracted from lib.sh find_workspace_uuid PYEOF block
|
|
|
|
import os, sys, json, sqlite3
|
|
from lib_py.verify_session import verify_session_uuid, mam_orchestrator_uuids, mam_row_own_uuid
|
|
|
|
OWN_KEY = {
|
|
'claude': 'claude_session_id_own',
|
|
'agy': 'agy_conversation_id_own',
|
|
'hermes': 'hermes_conversation_id_own',
|
|
'cline': 'cline_conversation_id_own'
|
|
}
|
|
|
|
from lib_py.paths import resolve_home
|
|
|
|
def find_workspace_uuid_main():
|
|
ws = os.environ['WS_ABS']
|
|
agent = os.environ['AGENT']
|
|
home = resolve_home()
|
|
claude_project_dir = os.environ.get('CLAUDE_PROJECT_DIR', f"{home}/.claude/projects")
|
|
target = os.environ.get('TARGET_SESSION', '')
|
|
|
|
try:
|
|
d = json.loads(os.environ.get('MAM_STATE_JSON', '{}'))
|
|
except Exception:
|
|
d = {}
|
|
|
|
running_ids = set()
|
|
for s_item in d.get('herdr_sessions', []):
|
|
if s_item.get('status') == 'running':
|
|
if target and s_item.get('name') == target:
|
|
continue
|
|
for k in ['claude_session_id_own', 'agy_conversation_id_own', 'hermes_conversation_id_own', 'cline_conversation_id_own']:
|
|
val = s_item.get(k)
|
|
if val:
|
|
running_ids.add(val)
|
|
|
|
orchestrator_ids = set(mam_orchestrator_uuids())
|
|
if target:
|
|
for s_item in d.get('herdr_sessions', []):
|
|
if s_item.get('name') == target:
|
|
own = mam_row_own_uuid(s_item)
|
|
if own:
|
|
orchestrator_ids.discard(own)
|
|
|
|
def emit(u):
|
|
if u in running_ids or u in orchestrator_ids:
|
|
return
|
|
print(u)
|
|
sys.exit(0)
|
|
|
|
# Fetch all sessions matching this workspace
|
|
sessions = []
|
|
for s in d.get('herdr_sessions', []):
|
|
s_cwd = (s.get('pane') or {}).get('cwd', '') if isinstance(s, dict) else ''
|
|
if s_cwd and (s_cwd == ws or os.path.realpath(s_cwd) == os.path.realpath(ws)):
|
|
sessions.append(s)
|
|
|
|
if target:
|
|
for s in sessions:
|
|
if s.get('name') != target:
|
|
continue
|
|
cand = s.get(OWN_KEY.get(agent, ''), None)
|
|
if cand and verify_session_uuid(ws, agent, cand, s, mode="revalidate"):
|
|
emit(cand)
|
|
|
|
for s in sessions:
|
|
name = s.get('name', '')
|
|
if name.endswith(f"-creator-{agent}"):
|
|
cand = s.get(OWN_KEY.get(agent, ''))
|
|
if cand and verify_session_uuid(ws, agent, cand, s, mode="revalidate"):
|
|
emit(cand)
|
|
|
|
from lib_py.agents.registry import get_adapter
|
|
from lib_py.agents.base import DiscoveryContext
|
|
|
|
adapter = get_adapter(agent)
|
|
if adapter:
|
|
ctx = DiscoveryContext(workspace=ws, agent_name=agent, home_dir=home, claude_dir=claude_project_dir)
|
|
for cand in adapter.discover(ctx):
|
|
emit(cand)
|
|
|
|
ai = d.get('agent_identities') if isinstance(d, dict) else None
|
|
if not isinstance(ai, dict) or not ai:
|
|
ai = {}
|
|
try:
|
|
yaml_path = os.environ['YAML_PATH']
|
|
db_path = os.path.splitext(yaml_path)[0] + '.db'
|
|
if os.path.exists(db_path):
|
|
conn = sqlite3.connect(db_path, timeout=60.0)
|
|
conn.execute('PRAGMA busy_timeout = 60000')
|
|
try:
|
|
row = conn.execute('SELECT data FROM state WHERE id=1').fetchone()
|
|
if row:
|
|
ai = json.loads(row[0]).get('agent_identities') or {}
|
|
except sqlite3.OperationalError:
|
|
pass
|
|
conn.close()
|
|
elif os.path.exists(yaml_path):
|
|
import yaml
|
|
with open(yaml_path) as f:
|
|
_ydoc = yaml.safe_load(f) or {}
|
|
ai = _ydoc.get('agent_identities') or {}
|
|
except Exception as e:
|
|
print(f"WARN: tier-3 identity lookup failed: {e}", file=sys.stderr)
|
|
if not isinstance(ai, dict):
|
|
ai = {}
|
|
|
|
ai_agent = ai.get(agent) or {}
|
|
if ai_agent.get('project_cwd') == ws:
|
|
cand = ai_agent.get('session_id') or ai_agent.get('conversation_id')
|
|
if cand and verify_session_uuid(ws, agent, cand, mode="revalidate"):
|
|
emit(cand)
|
|
|
|
print('')
|
|
|
|
if __name__ == '__main__':
|
|
find_workspace_uuid_main()
|