- 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.
105 lines
3.4 KiB
Python
105 lines
3.4 KiB
Python
# base.py — BaseAgentAdapter abstract base class, SpawnSpec, and DiscoveryContext
|
|
|
|
import os, json, sqlite3
|
|
from typing import Optional, Dict, Any, List
|
|
from lib_py.paths import resolve_home
|
|
from lib_py.verify_session import verify_session_uuid, workspace_key
|
|
|
|
class SpawnSpec:
|
|
def __init__(self, agent_name: str, session_name: str, command: str, env: Optional[Dict[str, str]] = None):
|
|
self.agent_name = agent_name
|
|
self.session_name = session_name
|
|
self.command = command
|
|
self.env = env or {}
|
|
|
|
class DiscoveryContext:
|
|
def __init__(self, workspace: str, agent_name: str = "", home_dir: Optional[str] = None,
|
|
claude_dir: Optional[str] = None, epoch: int = 0,
|
|
row: Optional[Dict[str, Any]] = None, mode: str = "discover"):
|
|
self.workspace = workspace
|
|
self.agent_name = agent_name
|
|
self.home_dir = resolve_home(home_dir)
|
|
self.claude_dir = claude_dir or os.environ.get("CLAUDE_PROJECT_DIR", f"{self.home_dir}/.claude/projects")
|
|
self.epoch = epoch
|
|
self.row = row or {}
|
|
self.mode = mode
|
|
|
|
@property
|
|
def ws_key(self) -> str:
|
|
return workspace_key(self.workspace)
|
|
|
|
@property
|
|
def cwd(self) -> str:
|
|
return (self.row.get("pane", {}).get("cwd", "") if isinstance(self.row, dict) else "") or self.workspace
|
|
|
|
class BaseAgentAdapter:
|
|
@property
|
|
def name(self) -> str:
|
|
raise NotImplementedError
|
|
|
|
@property
|
|
def own_key(self) -> str:
|
|
raise NotImplementedError
|
|
|
|
@property
|
|
def ready_tokens(self) -> str:
|
|
raise NotImplementedError
|
|
|
|
@property
|
|
def exit_key(self) -> str:
|
|
raise NotImplementedError
|
|
|
|
@property
|
|
def delegate_agent_key(self) -> str:
|
|
raise NotImplementedError
|
|
|
|
@property
|
|
def identity_cache_fields(self) -> tuple:
|
|
raise NotImplementedError
|
|
|
|
@property
|
|
def input_prompt(self) -> Optional[str]:
|
|
return None
|
|
|
|
@property
|
|
def input_placeholder(self) -> Optional[str]:
|
|
return None
|
|
|
|
@property
|
|
def input_rule_pattern(self) -> Optional[str]:
|
|
return None
|
|
|
|
def derive_session_name(self, slug: str, role: str = "creator") -> str:
|
|
r = role.lower()
|
|
return f"{slug}-{r}-{self.name}"
|
|
|
|
def matches_session_name(self, session_name: str) -> bool:
|
|
for r in ('creator', 'planner', 'reviewer'):
|
|
if session_name.endswith(f"-{r}-{self.name}"):
|
|
return True
|
|
return session_name.endswith(f"-{self.name}")
|
|
|
|
def verify_session(self, ws: str, uuid: str, row: Optional[Dict[str, Any]] = None, mode: str = "discover") -> bool:
|
|
return verify_session_uuid(ws, self.name, uuid, row=row, mode=mode)
|
|
|
|
def artifact_path(self, uuid: str, ctx: DiscoveryContext) -> str:
|
|
raise NotImplementedError
|
|
|
|
def verify_artifact(self, uuid: str, ctx: DiscoveryContext) -> bool:
|
|
raise NotImplementedError
|
|
|
|
def purge_artifacts(self, uuid: str, ctx: DiscoveryContext) -> list:
|
|
raise NotImplementedError
|
|
|
|
def spawn_spec(self, binary: str, session_uuid: str = "", use_wrapper: bool = False) -> str:
|
|
raise NotImplementedError
|
|
|
|
def resume_spec(self, binary: str, session_uuid: str, materialized: bool = False) -> str:
|
|
raise NotImplementedError
|
|
|
|
def auth_ok(self, run_cmd: Optional[Any] = None) -> bool:
|
|
raise NotImplementedError
|
|
|
|
def discover(self, ctx: DiscoveryContext) -> list:
|
|
raise NotImplementedError
|