fix(session_ids,id_order): optional --session resolution, fresh-marker retention, and canonical cwd matching (Issue #63)

This commit is contained in:
Godopu committed 2026-10-21 18:24:14 +00:00
1 parent 8c9a68cf1c
commit c747374aa6
3 files changed
+273 -23

No files matched your search

+26 -4
View File
@@ -21,10 +21,19 @@ from lib_py.agents.base import DiscoveryContext, POST_DISCOVERED
from lib_py.agents.registry import agent_of_row, get_adapter
from lib_py.paths import resolve_home
from lib_py.state import load_state_json
from lib_py.verify_session import verify_session_uuid, workspace_key
from lib_py.verify_session import verify_session_uuid
from lib_py.workspace_uuid import find_workspace_uuid
def canonical_cwd(path: str) -> str:
if not path:
return ""
try:
return os.path.realpath(path)
except Exception:
return os.path.abspath(path)
def own_id(row: Optional[Dict[str, Any]], agent: str) -> str:
adapter = get_adapter(agent)
if not adapter or not isinstance(row, dict):
@@ -51,13 +60,13 @@ def claiming_rows(row: Dict[str, Any], uuid: str, state: Dict[str, Any]) -> List
def same_type_rows(state: Dict[str, Any], cwd: str, agent: str) -> List[Dict[str, Any]]:
if not cwd or not agent:
return []
target_key = workspace_key(cwd)
target_cwd = canonical_cwd(cwd)
out: List[Dict[str, Any]] = []
for s in state.get("herdr_sessions") or []:
if not isinstance(s, dict):
continue
s_cwd = (s.get("pane") or {}).get("cwd") or ""
if s_cwd and workspace_key(s_cwd) == target_key and agent_of_row(s) == agent:
if s_cwd and canonical_cwd(s_cwd) == target_cwd and agent_of_row(s) == agent:
out.append(s)
return out
@@ -195,9 +204,22 @@ def decide_resume(state: Dict[str, Any], name: str, agent: str, workspace: str,
home_resolved = resolve_home(home)
claude_resolved = claude_dir or os.environ.get("CLAUDE_PROJECT_DIR") or f"{home_resolved}/.claude/projects"
row = _find_row(state, name)
adapter = get_adapter(agent)
if row is None and not name and workspace:
same = same_type_rows(state, workspace, agent)
if len(same) == 1:
row = same[0]
name = str(row.get("name") or "")
elif adapter is None or adapter.id_lifecycle != POST_DISCOVERED:
creator = next(
(s for s in same if str(s.get("name") or "").endswith(f"-creator-{agent}")),
None,
)
if creator is not None:
row = creator
name = str(row.get("name") or "")
row_ref = row if row is not None else ({"name": name} if name else {})
claimed = set(claimed_elsewhere(row_ref, state))
adapter = get_adapter(agent)
own = own_id(row, agent)
if own: