cli_commands_worktree.py 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. """``/worktree new``'s step into the tree it just created, split out of ``cli_commands_mixin``."""
  2. from __future__ import annotations
  3. from agent.i18n import t
  4. from hermes_cli.cli_commands_session_tools import _t
  5. def enter_new_worktree(cli, wt_info) -> bool:
  6. """Move *cli*'s session into the fresh tree -> True when the session's tools now run there.
  7. A gateway refusal (e.g. ``session_busy``) moved nothing: the fresh tree is removed now instead of
  8. sitting unlisted until quit, and an earlier active tree stays active. A chdir failure after the
  9. gateway accepted the move leaves only this process behind, so it is reported as that."""
  10. import cli as _cli
  11. from hermes_cli.cli_chat_error_copy import refusal_text
  12. from hermes_cli.gateway_automation import gateway_connection
  13. from hermes_cli.gateway_client import GatewayClientError
  14. from hermes_cli.gateway_session_store import move_cli_workspace
  15. try:
  16. move_cli_workspace(cli, wt_info) # the gateway session's tools, then this process
  17. except GatewayClientError as e:
  18. print(f" {_t('worktree.enter_failed', error=refusal_text(e))}")
  19. active = _cli._active_worktree
  20. _cli._cleanup_worktree(wt_info)
  21. _cli._active_worktree = active
  22. return False
  23. except OSError as e:
  24. if gateway_connection(cli) is None: # a local session: nothing moved
  25. print(f" {_t('worktree.enter_failed', error=e)}")
  26. return False
  27. print(f" {t('cli.session.cwd_enter_failed', path=wt_info['path'], error=e)}")
  28. return True