Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions hermes_cli/edit_in_place_repos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
"""Single source of truth for which repo roots are EDIT-IN-PLACE.

Some repo checkouts are not build targets you cut a branch/PR from β€” they ARE
the running deploy. The canonical case is the Hermes home checkout (``~/.hermes``
is the working tree of ``cwest/hermes-config``): the checkout is the live
install, changes land directly in it, and an hourly ``backup_commit`` cron owns
commit+push to ``main``. There is no PR flow for such a repo.

The kanban dir-workspace resolver (:func:`hermes_cli.kanban_db._resolve_dir_workspace`)
redirects a ``dir`` card whose ``workspace_path`` is a repo ROOT to a per-task
linked worktree on a ``wt/<id>`` branch β€” correct for a shared deploy clone
(``~/src/office``), wrong for an edit-in-place root, where it makes the worker
commit to a branch and open an unwanted PR against a repo that has no PR flow.

This module makes "which roots are edit-in-place" a DECLARED fact both core and
the homestead filing side assert against, rather than a heuristic ("path is under
$HOME/.hermes", "repo has no remote") that would silently change behavior for
repos nobody audited, or prose restated across several skills that can drift.
It mirrors the shape of ``scripts/merge_authority.py`` on the homestead side:
a small explicit set resolved to real paths, plus a predicate.

The set is resolved at call time (not import time) so it honours the active
profile / ``HERMES_HOME`` β€” the edit-in-place root of a profiled install is that
profile's home, and a Docker/custom deployment with ``HERMES_HOME`` outside
``~/.hermes`` is still classified correctly. This is a declaration ("the Hermes
home checkout is edit-in-place"), resolved to a real path β€” not a guess.
"""

from __future__ import annotations

from pathlib import Path

__all__ = ["edit_in_place_roots", "is_edit_in_place_root"]


def edit_in_place_roots() -> frozenset[Path]:
"""Return the resolved absolute paths that are edit-in-place repo roots.

Currently the Hermes home checkout(s). Both the ``HERMES_HOME``-honouring
root (``get_default_hermes_root()``, correct for Docker / custom / profiled
deployments) and the canonical ``~/.hermes`` under ``$HOME`` are included, so
the classification matches ``_card_requires_pr``'s existing edit-in-place
exclusion regardless of how the process resolved its home. Paths that cannot
be resolved are simply omitted rather than raising β€” this predicate must
never crash the dispatcher.
"""
roots: set[Path] = set()

try:
from hermes_constants import get_default_hermes_root

roots.add(Path(get_default_hermes_root()).expanduser().resolve(strict=False))
except Exception:
pass

try:
roots.add((Path.home() / ".hermes").expanduser().resolve(strict=False))
except Exception:
pass

return frozenset(roots)


def is_edit_in_place_root(path: Path | str | None) -> bool:
"""True when *path* is a DECLARED edit-in-place repo root.

The comparison is on the fully-resolved (symlink-normalized) path against the
resolved declared roots, so a symlinked or ``~``-relative spelling of the
Hermes home still matches. A subdirectory of an edit-in-place root is NOT a
match β€” only the root itself, because the carve-out is about handing the
worker the deploy checkout as-is, not about scoping into it.
"""
if path is None:
return False
try:
resolved = Path(path).expanduser().resolve(strict=False)
except Exception:
return False
return resolved in edit_in_place_roots()
35 changes: 35 additions & 0 deletions hermes_cli/kanban_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -9649,6 +9649,24 @@ def _resolve_worktree_workspace(
)
requested_resolved = requested.resolve(strict=False)

# Refuse a worktree card explicitly aimed at an EDIT-IN-PLACE repo root. Such
# a root (``~/.hermes`` β€” the live install) is the deploy target itself, not a
# repo you cut a branch/PR from: asking for an isolated worktree on it is a
# caller error (the card should be ``workspace_kind='dir'``, which returns the
# checkout in place β€” see ``_resolve_dir_workspace``). Surface it loudly rather
# than silently coercing to the in-place path, so the filing mistake is caught
# instead of masked. The declaration is shared with the dir resolver, so the
# two agree on which roots are edit-in-place.
from hermes_cli.edit_in_place_repos import is_edit_in_place_root

if is_edit_in_place_root(requested_resolved):
raise ValueError(
f"task {task.id} has workspace_kind=worktree aimed at edit-in-place "
f"repo root {task.workspace_path!r}: that checkout is the deploy "
f"target itself and must be edited in place, not from an isolated "
f"worktree/branch. File this card with workspace_kind='dir' instead."
)

if requested.exists() and _is_linked_worktree_checkout(requested):
actual_branch = _git_current_branch(requested)
if actual_branch == branch_name:
Expand Down Expand Up @@ -9724,6 +9742,23 @@ def _resolve_dir_workspace(task: Task) -> Path:
if repo_root is None or p.resolve(strict=False) != repo_root:
return p

# Carve-out: an EDIT-IN-PLACE repo root is the deploy target itself, not a
# shared clone to isolate from. ``~/.hermes`` is the working tree of
# ``cwest/hermes-config`` β€” the live running install, edited in place, with
# an hourly ``backup_commit`` cron owning commit+push to ``main`` and no PR
# flow at all. Redirecting it to a ``wt/<id>`` worktree makes the worker
# commit to a branch and open an unwanted PR, and leaves a stray worktree +
# branch in the live install. Return the path unchanged: no worktree, no
# branch, and the default-branch/upstream guard below does NOT run (it is
# meaningless for a checkout that is itself the deploy target). The set of
# edit-in-place roots is DECLARED (``hermes_cli.edit_in_place_repos``), not
# inferred from a heuristic. Every non-edit-in-place repo root keeps the
# redirect-and-guard behavior below exactly.
from hermes_cli.edit_in_place_repos import is_edit_in_place_root

if is_edit_in_place_root(repo_root):
return repo_root

# A linked worktree's own root IS its git toplevel, so it reaches here too β€”
# but a worktree correctly sitting on its own topic branch is the whole point
# of a worktree, not a deploy clone left off main. Skip the fast-forward
Expand Down
Loading
Loading