Files
hermes-agent/hermes_cli/main_resume_args.py
kshitijk4poor 26409f994c fix(cli): a classic -w session gets its clean tree back on resume
`hermes -w` (classic, not `--tui -w`) runs its session's tools in the
new worktree: the gateway policy freezes the tree as the session's cwd.
Quit removes a clean tree, and a later `hermes -c` / `--resume` of that
session then had every turn refused `workspace_unavailable`: the owner
refuses a missing directory.

The `--tui -w` resume fix (N8) already recreates a missing tree on
resume, but only one its launcher recorded, and it looked for it in
the session row's cwd. A classic `-w` launcher recorded nothing, and
its row keeps main's stamp, the repo root (e504e030e3, @teknium1,
2026-10-12), not the tree its tools run in.

- The classic launcher passes the profile's state.db to
  `_setup_worktree`, as `--tui -w` does: the tree is recorded for
  resume, and its lock names the store. The row's cwd stays the repo
  root, so a reaped clean classic `-w` tree is not kept; it is
  recreated on resume.
- The resume step reads the session's frozen policy cwd (its local
  receipt; a read) before the row's cwd, since that is the directory
  the owner checks. A store error on that read falls back to the row's
  cwd.

Trees are still removed on quit.

Refs #134889 (CLI-15)
Refs #139158
2026-10-20 15:08:20 +05:30

57 lines
2.4 KiB
Python

"""``hermes chat`` resume-argument steps split out of ``hermes_cli.main``: ``--resume @claude/@codex``
imports, and recreating a removed ``--tui -w`` worktree a resumed session needs (N8)."""
import os
import sys
def import_foreign_resume(args) -> None:
"""--resume @claude / @codex: import a foreign session and resume it."""
_resume_foreign = getattr(args, "resume", None)
if not (isinstance(_resume_foreign, str) and _resume_foreign.strip().lower() in ("@claude", "@codex")):
return
from hermes_cli.foreign_sessions import import_foreign_session, pick_foreign_session
_picked = pick_foreign_session(_resume_foreign.strip().lower().lstrip("@"))
if _picked is None:
sys.exit(1)
try:
_imported_id = import_foreign_session(_picked.source, _picked.path)
except ValueError as e:
print(f"Error: {e}")
sys.exit(1)
print(f"✓ Imported as {_imported_id} — resuming it now.")
print(f" (later: hermes --resume {_imported_id})")
args.resume = _imported_id
def _frozen_cwd(db, session_id) -> str:
"""The cwd the owner runs *session_id*'s tools in (its local policy), or "" without one. The
row's cwd is main's stamp, which for a classic ``-w`` session is the repo root, not the tree."""
import sqlite3
from hermes_state_local import local_receipt
from hermes_state_runtime import RuntimeStoreError
try:
return str(local_receipt(db, session_id)["policy"].get("cwd") or "").strip()
except (RuntimeStoreError, KeyError, AttributeError, sqlite3.Error): # no readable policy: use the row's cwd
return ""
def recreate_resumed_tui_worktree(args) -> None:
"""A resumed session whose frozen cwd is a ``-w`` / ``--tui -w`` tree that quit removed gets that
tree back before attaching, or the owner refuses every turn ``workspace_unavailable`` (N8)."""
if not getattr(args, "resume", None) or getattr(args, "worktree", False):
return
cwd = ""
from hermes_cli.main import _session_db
with _session_db() as db: # a store error is swallowed: nothing to recreate
cwd = (_frozen_cwd(db, args.resume)
or ((db.get_session(args.resume) or {}).get("cwd") or "").strip())
if not cwd or os.path.isdir(cwd):
return
from hermes_cli.tui_worktree_resume import recreate_tui_worktree
tree = recreate_tui_worktree(cwd, args.resume)
if tree:
print(f"↪ recreated the session's worktree: {tree}")