main_resume_args.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """``hermes chat`` resume-argument steps split out of ``hermes_cli.main``: ``--resume @claude/@codex``
  2. imports, and recreating a removed ``--tui -w`` worktree a resumed session needs (N8)."""
  3. import os
  4. import sys
  5. def import_foreign_resume(args) -> None:
  6. """--resume @claude / @codex: import a foreign session and resume it."""
  7. _resume_foreign = getattr(args, "resume", None)
  8. if not (isinstance(_resume_foreign, str) and _resume_foreign.strip().lower() in ("@claude", "@codex")):
  9. return
  10. from hermes_cli.foreign_sessions import import_foreign_session, pick_foreign_session
  11. _picked = pick_foreign_session(_resume_foreign.strip().lower().lstrip("@"))
  12. if _picked is None:
  13. sys.exit(1)
  14. try:
  15. _imported_id = import_foreign_session(_picked.source, _picked.path)
  16. except ValueError as e:
  17. print(f"Error: {e}")
  18. sys.exit(1)
  19. print(f"✓ Imported as {_imported_id} — resuming it now.")
  20. print(f" (later: hermes --resume {_imported_id})")
  21. args.resume = _imported_id
  22. def _frozen_cwd(db, session_id) -> str:
  23. """The cwd the owner runs *session_id*'s tools in (its local policy), or "" without one. The
  24. row's cwd is main's stamp, which for a classic ``-w`` session is the repo root, not the tree."""
  25. import sqlite3
  26. from hermes_state_local import local_receipt
  27. from hermes_state_runtime import RuntimeStoreError
  28. try:
  29. return str(local_receipt(db, session_id)["policy"].get("cwd") or "").strip()
  30. except (RuntimeStoreError, KeyError, AttributeError, sqlite3.Error): # no readable policy: use the row's cwd
  31. return ""
  32. def recreate_resumed_tui_worktree(args) -> None:
  33. """A resumed session whose frozen cwd is a ``-w`` / ``--tui -w`` tree that quit removed gets that
  34. tree back before attaching, or the owner refuses every turn ``workspace_unavailable`` (N8)."""
  35. if not getattr(args, "resume", None) or getattr(args, "worktree", False):
  36. return
  37. cwd = ""
  38. from hermes_cli.main import _session_db
  39. with _session_db() as db: # a store error is swallowed: nothing to recreate
  40. cwd = (_frozen_cwd(db, args.resume)
  41. or ((db.get_session(args.resume) or {}).get("cwd") or "").strip())
  42. if not cwd or os.path.isdir(cwd):
  43. return
  44. from hermes_cli.tui_worktree_resume import recreate_tui_worktree
  45. tree = recreate_tui_worktree(cwd, args.resume)
  46. if tree:
  47. print(f"↪ recreated the session's worktree: {tree}")