refactor(workspace_uuid): find_workspace_uuid() returns the id and skips exclude_ids; main prints it (Issue #63)

This commit is contained in:
Godopu committed 2026-10-21 16:28:34 +00:00
1 parent dc886ecd6b
commit ead490bddc
2 files changed
+180 -22

No files matched your search

+33 -22
View File
@@ -15,17 +15,10 @@ OWN_KEY = {
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 = {}
def find_workspace_uuid(ws, agent, target='', state=None, home=None, claude_dir=None, exclude_ids=()):
d = state if isinstance(state, dict) else {}
home = resolve_home(home)
claude_project_dir = claude_dir or os.environ.get('CLAUDE_PROJECT_DIR', f"{home}/.claude/projects")
running_ids = set()
for s_item in d.get('herdr_sessions', []):
@@ -38,6 +31,10 @@ def find_workspace_uuid_main():
running_ids.add(val)
orchestrator_ids = set(mam_orchestrator_uuids())
if "MAM_ORCHESTRATOR_UUIDS" not in os.environ:
raw_orc = d.get('orchestrator_uuids')
if isinstance(raw_orc, list) and all(isinstance(x, str) and x.strip() for x in raw_orc):
orchestrator_ids |= set(raw_orc)
if target:
for s_item in d.get('herdr_sessions', []):
if s_item.get('name') == target:
@@ -45,11 +42,7 @@ def find_workspace_uuid_main():
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)
excluded = running_ids | orchestrator_ids | {u for u in (exclude_ids or ()) if u}
# Fetch all sessions matching this workspace
sessions = []
@@ -63,15 +56,17 @@ def find_workspace_uuid_main():
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)
if cand and verify_session_uuid(ws, agent, cand, s, home_dir=home, claude_dir=claude_project_dir, mode="revalidate"):
if cand not in excluded:
return 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)
if cand and verify_session_uuid(ws, agent, cand, s, home_dir=home, claude_dir=claude_project_dir, mode="revalidate"):
if cand not in excluded:
return cand
from lib_py.agents.registry import get_adapter
from lib_py.agents.base import DiscoveryContext
@@ -80,9 +75,25 @@ def find_workspace_uuid_main():
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)
if cand and cand not in excluded:
return cand
print('')
return ''
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 = {}
print(find_workspace_uuid(ws, agent, target, d, home, claude_project_dir))
if __name__ == '__main__':
find_workspace_uuid_main()