Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
refactor(cli_agent): use `--continue` for memory continuity; dead code removal
trohitgd3883d5bac0f
diff --git a/server/nodes/agent/claude_code_agent/__init__.py b/server/nodes/agent/claude_code_agent/__init__.pyindex 9acc901c..4fdb7ef5 100644--- a/server/nodes/agent/claude_code_agent/__init__.py+++ b/server/nodes/agent/claude_code_agent/__init__.py@@ -17,7 +17,6 @@ and passed to ``run_batch()`` so the MCP server can scope its responses.from __future__ import annotationsimport time-import uuidfrom datetime import datetimefrom pathlib import Pathfrom typing import Any, List, Optional@@ -170,48 +169,29 @@ class ClaudeCodeAgentNode(ActionNode):# Memory bridge: claude maintains its own session JSONL on disk# under `<CLAUDE_CONFIG_DIR>/projects/<cwd-encoded>/<UUID>.jsonl`.# The project_key is derived from cwd (`[^a-zA-Z0-9.-] -> -`),- # so memory continuity needs (a) a STABLE cwd across runs and- # (b) the right session_id passed on argv.- #- # (a) is handled by AICliService passing memory_bound=True so+ # so memory continuity needs only a STABLE cwd across runs.+ # That's handled by AICliService passing memory_bound=True so# AICliSession spawns under repo_root instead of an ephemeral# worktree — see `services/cli_agent/session.py:cwd()`.#- # (b) splits on whether claude has run for this memory before:- #- # - First run (no last_session_id): pass `--session-id <UUID5>`- # where UUID5 is deterministically derived from- # (memory_node_id, simpleMemory.session_id). Claude creates- # the session at exactly that UUID, writes its JSONL to a- # predictable location. _persist_memory saves the same- # UUID as last_session_id after success.+ # Continuity flag: when memory is wired, we set+ # ``continue_session=True`` on the task spec. The argv-builder+ # emits ``--continue`` and claude auto-loads the most recent+ # conversation under the cwd (per code.claude.com/docs/en/+ # cli-reference). No UUID round-trip through the memory node's+ # params required — claude tracks its own sessions on disk and+ # the auto-find-latest is the cleaner primitive than the+ # pre-cutover UUID5-pre-mint + `--session-id <UUID5>` dance.#- # - Subsequent runs: pass `--resume <last_session_id>`.- # Claude finds its own JSONL and continues.- #- # Stale-`last_session_id` self-healing: if `--resume` fails with- # "No conversation found", `_persist_memory` clears the stale- # UUID; the next run falls into the first-run branch and- # recovers automatically.- resume_session_id: Optional[str] = None- first_run_session_uuid: Optional[str] = None+ # First run: ``--continue`` with no prior session under the cwd+ # is a benign no-op; claude starts fresh. Subsequent runs find+ # and continue the prior JSONL.+ continue_session = bool(memory_data)if memory_data:- last = memory_data.get("last_session_id") or None- if last:- resume_session_id = last- else:- raw = memory_data.get("session_id") or memory_data["node_id"]- name = f"{memory_data['node_id']}:{raw}"- first_run_session_uuid = str(- uuid.uuid5(uuid.NAMESPACE_OID, name)- )logger.info(- "[Claude Code memory] memory_node=%s last_session_id=%s "- "-> %s",+ "[Claude Code memory] memory_node=%s -> --continue "+ "(claude auto-finds latest session under cwd)",memory_data.get("node_id"),- last,- f"--resume {resume_session_id}" if resume_session_id- else f"--session-id {first_run_session_uuid} (first run)",)tasks = list(params.tasks)@@ -237,11 +217,8 @@ class ClaudeCodeAgentNode(ActionNode):"prompt": prompt,"model": params.model,"system_prompt": params.system_prompt,+ "continue_session": continue_session,}- if resume_session_id:- spec_kwargs["resume_session_id"] = resume_session_id- elif first_run_session_uuid:- spec_kwargs["session_id"] = first_run_session_uuidtasks = [ClaudeTaskSpec(**spec_kwargs)]else:# Apply node-level defaults to tasks that don't override.@@ -251,11 +228,14 @@ class ClaudeCodeAgentNode(ActionNode):changed["model"] = params.modelif not t.system_prompt and params.system_prompt:changed["system_prompt"] = params.system_prompt- if not t.session_id and not t.resume_session_id:- if resume_session_id:- changed["resume_session_id"] = resume_session_id- elif first_run_session_uuid:- changed["session_id"] = first_run_session_uuid+ # Only auto-enable --continue when memory is wired AND+ # the task didn't explicitly opt in/out or pick a UUID.+ if (+ continue_session+ and not t.continue_session+ and not t.resume_session_id+ ):+ changed["continue_session"] = Trueif changed:tasks[i] = t.model_copy(update=changed)diff --git a/server/nodes/skill/simple_memory/__init__.py b/server/nodes/skill/simple_memory/__init__.pyindex 71de402c..cd418f08 100644--- a/server/nodes/skill/simple_memory/__init__.py+++ b/server/nodes/skill/simple_memory/__init__.py@@ -73,10 +73,11 @@ class SimpleMemoryParams(BaseModel):title="Last Claude Session ID",description=("Internal: the session UUID claude returned on the most "- "recent successful run. Drives `--resume <UUID>` on the "- "next spawn so claude finds and continues its own JSONL "- "transcript on disk. Hidden from the UI; clearing the "- "memory wipes this too."+ "recent successful run. Display-only — `claude_code_agent` "+ "no longer reads this field. Continuity is driven by "+ "`--continue` so claude tracks its own latest session per "+ "cwd. Kept for back-compat + diagnostic display; clearing "+ "the memory wipes it."),json_schema_extra={"hidden": True},)diff --git a/server/services/cli_agent/providers/anthropic_claude.py b/server/services/cli_agent/providers/anthropic_claude.pyindex 5fa81972..5a7aa2e1 100644--- a/server/services/cli_agent/providers/anthropic_claude.py+++ b/server/services/cli_agent/providers/anthropic_claude.py@@ -172,13 +172,26 @@ class AnthropicClaudeProvider:)argv += ["--model", model]- # Resume — no `--session-id` in interactive mode. First-run- # claude assigns its own UUID, which the JsonlDirWatcher- # discovers from the new file appearing under- # `<CLAUDE_CONFIG_DIR>/projects/<project_key>/` and which- # `_persist_memory` saves into `simpleMemory.last_session_id`.+ # Session continuity. Three valid states, mutually exclusive:+ # - ``resume_session_id`` set → ``--resume <UUID>`` (explicit;+ # used by ``--fork-session`` UI + any caller that already+ # knows the exact UUID).+ # - ``continue_session=True`` → ``--continue`` (claude+ # auto-loads the latest conversation under the current+ # cwd, per code.claude.com/docs/en/cli-reference). The+ # cleaner default for memory-bound runs because it avoids+ # ferrying a UUID through the memory node's params — claude+ # tracks its own sessions on disk under+ # ``<CLAUDE_CONFIG_DIR>/projects/<project_key>/`` and+ # ``--continue`` picks the newest.+ # - Neither → no flag (fresh session; claude assigns a new+ # UUID which the post-spawn JSONL locator discovers).+ # ``--session-id`` is intentionally NOT emitted in interactive+ # mode — the CLI rejects it.if task.resume_session_id:argv += ["--resume", task.resume_session_id]+ elif task.continue_session:+ argv += ["--continue"]# Allowed tools — built-in defaults plus every workflow tool we# exposed via the per-batch FastMCP bridge. Same shape as thediff --git a/server/services/cli_agent/session.py b/server/services/cli_agent/session.pyindex 1c666173..8ca0178c 100644--- a/server/services/cli_agent/session.py+++ b/server/services/cli_agent/session.py@@ -72,7 +72,11 @@ _FIRST_RUN_JSONL_TIMEOUT = 5.0class AICliSession(BaseProcessSupervisor):"""One CLI subprocess for one task."""- pipe_streams = True+ # ``pipe_streams`` was the parent's flag for whether ``_do_start``+ # should pipe stdout/stderr through ``drain_stream`` loggers. We+ # override ``_do_start`` entirely (PTY-based spawn) so it's+ # irrelevant — left as False to match reality.+ pipe_streams = Falseterminate_grace_seconds = 5.0graceful_shutdown = sys.platform == "win32"@@ -126,7 +130,6 @@ class AICliSession(BaseProcessSupervisor):# Streaming stateself._events: List[Dict[str, Any]] = []- self._stderr_lines: List[str] = [] # populated only on PTY error envelopesself._exit_code: Optional[int] = Noneself._lockfile_path: Optional[Path] = None@@ -375,26 +378,19 @@ class AICliSession(BaseProcessSupervisor):)resume_uuid = getattr(self._task, "resume_session_id", None)- baseline_files: set[str] = set()if resume_uuid:# Resume runs: known location. Claude opens this file and# appends to it on each turn.self._session_jsonl_path = project_dir / f"{resume_uuid}.jsonl"- else:- # First-run: snapshot the dir BEFORE spawn so we can- # identify the new file claude creates afterwards.- try:- baseline_files = {- p.name for p in project_dir.iterdir()- if p.suffix == ".jsonl"- }- except OSError:- pass+ # Else: post-spawn we'll discover the file via mtime — works+ # uniformly for fresh-spawn (claude creates new) and+ # --continue (claude appends to existing).# Spawn via the cross-platform PTY transport. The PtyHandle# owns the subprocess; `self._proc` stays `None` for# BaseProcessSupervisor-compat — `is_running` / `_do_stop` are# overridden below to consult `self._pty_handle` instead.+ spawn_time = time.monotonic()transport = get_pty_transport()argv = self.argv()try:@@ -414,12 +410,16 @@ class AICliSession(BaseProcessSupervisor):self._branch, self.cwd(),)- # Resolve the JSONL path. For first-run we wait for claude to- # create the file; for resume the path is already known.+ # Resolve the JSONL path. For known-UUID `--resume` runs the+ # path is already known; otherwise (fresh OR `--continue`)+ # discover via mtime — claude either creates a new file (fresh)+ # or updates the latest existing file's mtime by appending an+ # init event (--continue). Either way "newest .jsonl with+ # mtime >= spawn_time - grace" is the right file.if self._session_jsonl_path is None:try:- self._session_jsonl_path = await self._wait_for_new_jsonl(- project_dir, baseline_files,+ self._session_jsonl_path = await self._wait_for_session_jsonl(+ project_dir, spawn_time,)except TimeoutError as exc:# Surface a clean error envelope rather than letting the@@ -463,39 +463,41 @@ class AICliSession(BaseProcessSupervisor):project_key = _PROJECT_KEY_RE.sub("-", str(cwd))return Path(MACHINA_CLAUDE_DIR) / "projects" / project_key- async def _wait_for_new_jsonl(- self, project_dir: Path, baseline: set[str],+ async def _wait_for_session_jsonl(+ self, project_dir: Path, spawn_time: float,) -> Path:- """Poll ``project_dir`` for a new ``.jsonl`` file that wasn't- present in ``baseline``. Used on first-run spawns when we don't- know the UUID claude will assign. If multiple new files appear- (a race we don't expect), pick the newest by mtime.-- Raises :class:`TimeoutError` after :data:`_FIRST_RUN_JSONL_TIMEOUT`- seconds — surfaces as a clean spawn failure rather than a- silently dead session.+ """Find the JSONL claude is using post-spawn via mtime.++ Works for both fresh-spawn (claude creates a new file) and+ ``--continue`` (claude appends an init event to the most+ recent existing file, updating its mtime). Picks the newest+ ``.jsonl`` whose mtime is at least ``spawn_time - 1s``+ (1s grace for clock skew + mtime resolution).++ Raises :class:`TimeoutError` after+ :data:`_FIRST_RUN_JSONL_TIMEOUT` seconds — surfaces as a+ clean spawn failure rather than a silently dead session."""- deadline = time.monotonic() + _FIRST_RUN_JSONL_TIMEOUT+ deadline = spawn_time + _FIRST_RUN_JSONL_TIMEOUT+ mtime_floor = spawn_time - 1.0 # grace for clock skewwhile time.monotonic() < deadline:try:- current_files = {- p.name for p in project_dir.iterdir()+ jsonls = [+ p for p in project_dir.iterdir()if p.suffix == ".jsonl"- }+ ]except OSError:- current_files = set()- new_names = current_files - baseline- if new_names:- candidates = [project_dir / n for n in new_names]- # Pick newest by mtime — tolerates a stale baseline race- # where an unrelated session was deleted concurrently.+ jsonls = []+ if jsonls:try:- return max(candidates, key=lambda p: p.stat().st_mtime)+ newest = max(jsonls, key=lambda p: p.stat().st_mtime)+ if newest.stat().st_mtime >= mtime_floor:+ return newestexcept OSError:- return candidates[0]+ passawait asyncio.sleep(0.1)raise TimeoutError(- f"No session JSONL appeared under {project_dir} within "+ f"No session JSONL touched under {project_dir} within "f"{_FIRST_RUN_JSONL_TIMEOUT}s — claude may have failed to "f"start or the CLAUDE_CONFIG_DIR is misconfigured.")@@ -799,9 +801,13 @@ class AICliSession(BaseProcessSupervisor):success: bool,error: Optional[str] = None,) -> SessionResult:+ # PTY merges stdout + stderr onto one stream; we never read it+ # (the JSONL on disk is the protocol surface). Pass empty+ # stderr — the provider's event_to_session_result reconstructs+ # error context from JSONL `result.subtype == "error"` events.provider_result = self._provider.event_to_session_result(self._events,- "\n".join(self._stderr_lines),+ "",self._exit_code if self._exit_code is not None else -1,)diff --git a/server/services/cli_agent/session_pool.py b/server/services/cli_agent/session_pool.pyindex 35847a4d..50a08006 100644--- a/server/services/cli_agent/session_pool.py+++ b/server/services/cli_agent/session_pool.py@@ -411,16 +411,6 @@ class ClaudeSessionPool:project_dir, exc,)- # Snapshot existing JSONL files BEFORE spawn so we can pick out- # the new one claude creates.- try:- baseline_files = {- p.name for p in project_dir.iterdir()- if p.suffix == ".jsonl"- }- except OSError:- baseline_files = set()-# Build argv WITHOUT the prompt — we'll write the first turn's# prompt to the PTY in `send_turn` so the spawn-to-ready path# is the same as the post-/clear path.@@ -435,12 +425,14 @@ class ClaudeSessionPool:# Spawn via the cross-platform PTY transport.transport = get_pty_transport()+ spawn_time = time.monotonic()pty_handle = await transport.spawn(argv, cwd=cwd, env=env)- # Wait for the JSONL file to appear (claude writes the- # system/init event almost immediately on startup).- jsonl_path = await self._wait_for_new_jsonl(- project_dir, baseline_files,+ # Locate the JSONL claude is using. Works uniformly for fresh+ # spawns (new file appears) and ``--continue`` spawns (existing+ # file's mtime updates as claude appends a system/init event).+ jsonl_path = await self._wait_for_session_jsonl(+ project_dir, spawn_time,)session_uuid = session_uuid_from_jsonl_path(jsonl_path) or ""@@ -632,31 +624,37 @@ class ClaudeSessionPool:)await session.jsonl_watcher.start()- async def _wait_for_new_jsonl(- self, project_dir: Path, baseline: set[str],+ async def _wait_for_session_jsonl(+ self, project_dir: Path, spawn_time: float,) -> Path:- """Mirror of ``AICliSession._wait_for_new_jsonl``. Local copy so- the pool doesn't depend on a private method of the session- class."""- deadline = time.monotonic() + _CLEAR_JSONL_TIMEOUT+ """Find the JSONL claude is using post-spawn via mtime.++ Works for both fresh-spawn (claude creates a new file) and+ ``--continue`` (claude appends an init event to the most+ recent existing file, updating its mtime). Picks the newest+ ``.jsonl`` whose mtime is at least ``spawn_time - 1s`` (grace+ for clock skew + filesystem mtime resolution).+ """+ deadline = spawn_time + _CLEAR_JSONL_TIMEOUT+ mtime_floor = spawn_time - 1.0while time.monotonic() < deadline:try:- current = {- p.name for p in project_dir.iterdir()+ jsonls = [+ p for p in project_dir.iterdir()if p.suffix == ".jsonl"- }+ ]except OSError:- current = set()- new_names = current - baseline- if new_names:- candidates = [project_dir / n for n in new_names]+ jsonls = []+ if jsonls:try:- return max(candidates, key=lambda p: p.stat().st_mtime)+ newest = max(jsonls, key=lambda p: p.stat().st_mtime)+ if newest.stat().st_mtime >= mtime_floor:+ return newestexcept OSError:- return candidates[0]+ passawait asyncio.sleep(0.1)raise TimeoutError(- f"No session JSONL appeared under {project_dir} within "+ f"No session JSONL touched under {project_dir} within "f"{_CLEAR_JSONL_TIMEOUT}s")diff --git a/server/services/cli_agent/types.py b/server/services/cli_agent/types.pyindex c9f943cd..32deceae 100644--- a/server/services/cli_agent/types.py+++ b/server/services/cli_agent/types.py@@ -65,12 +65,26 @@ class ClaudeTaskSpec(BaseAICliTaskSpec):session_id: Optional[str] = Field(default=None,description="Start a named session. Pair with `resume_session_id` "- "to chain conversations.",+ "to chain conversations. Note: silently dropped in "+ "interactive mode (claude assigns its own UUID); "+ "kept for back-compat.",)resume_session_id: Optional[str] = Field(default=None,- description="Resume from a prior session_id (returned in a previous "- "task's `result` event).",+ description="Resume from a specific prior session UUID. Mutually "+ "exclusive with `continue_session`. Generally unset by "+ "claude_code_agent — set `continue_session=True` for "+ "memory-bound runs and let claude find its own latest.",+ )+ continue_session: bool = Field(+ default=False,+ description="Emit `--continue` so claude auto-loads the most "+ "recent conversation under the current cwd (per "+ "code.claude.com/docs/en/cli-reference). The cleaner "+ "alternative to passing a specific UUID via "+ "`resume_session_id` — claude handles session "+ "tracking itself, no UUID round-trip through the "+ "memory node's params required.",)max_turns: Optional[int] = Field(default=None, ge=1,diff --git a/server/tests/services/cli_agent/test_providers.py b/server/tests/services/cli_agent/test_providers.pyindex 6badaea4..90a04dc1 100644--- a/server/tests/services/cli_agent/test_providers.py+++ b/server/tests/services/cli_agent/test_providers.py@@ -121,6 +121,40 @@ class TestClaudeArgv:assert "--resume" in argvassert "prior-sess" in argvassert "--session-id" not in argv+ # `--continue` is mutually exclusive with `--resume`; explicit+ # UUID wins.+ assert "--continue" not in argv++ def test_continue_flag_emitted(self, claude_provider):+ """``continue_session=True`` produces ``--continue`` — the+ cleaner default for memory-bound runs (claude auto-finds the+ latest conversation under cwd, no UUID round-trip)."""+ task = ClaudeTaskSpec(prompt="x", continue_session=True)+ argv = claude_provider.interactive_argv(task, defaults={})+ assert "--continue" in argv+ assert "--resume" not in argv+ assert "--session-id" not in argv++ def test_resume_wins_over_continue(self, claude_provider):+ """If both are set (caller bug), explicit ``--resume`` wins —+ matches the argv-builder's mutually-exclusive guard."""+ task = ClaudeTaskSpec(+ prompt="x",+ resume_session_id="prior",+ continue_session=True,+ )+ argv = claude_provider.interactive_argv(task, defaults={})+ assert "--resume" in argv+ assert "--continue" not in argv++ def test_no_continuity_flag_by_default(self, claude_provider):+ """Fresh task with no continuity hint → no flag. Claude assigns+ a new session UUID."""+ task = ClaudeTaskSpec(prompt="x")+ argv = claude_provider.interactive_argv(task, defaults={})+ assert "--resume" not in argv+ assert "--continue" not in argv+ assert "--session-id" not in argvdef test_system_prompt_propagates(self, claude_provider):task = ClaudeTaskSpec(prompt="x", system_prompt="be concise")