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
58 changes: 50 additions & 8 deletions agents/_pyauto_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@
(PyAutoBrain/, PyAutoMind/, PyAutoHeart/, ...). It is *derived*, never named:

1. an explicit ``PYAUTO_ROOT`` in the environment — the operator's word, taken
verbatim;
2. the parent of this checkout, when that parent holds at least one sibling
verbatim. It is still reported as verified or not, because a wrong value
exported by a hook should show up in the reason rather than pass silently;
2. the nearest ancestor of this checkout holding a ``.pyauto-root`` marker
file. The marker is the only rule that survives the workspace growing
subdirectories: it names the root instead of inferring it from what happens
to sit beside a checkout;
3. the parent of this checkout, when that parent holds at least one sibling
organ. It asks where the code actually *is* rather than where it is usually
kept, so it is true on a developer box and in a remote session alike;
3. the parent of this checkout regardless — the best available guess, reported
kept, so it is true on a developer box and in a remote session alike. A
single-repo remote checkout has no root above it to mark, so this stays —
demoted, not removed;
4. the parent of this checkout regardless — the best available guess, reported
as unverified so a caller can say "I looked here and it was not a
workspace" instead of silently finding nothing.

Expand All @@ -25,6 +32,12 @@
naming any absolute workspace path here would reintroduce it for the next
environment that does not match.

Step 2 exists for the mirror-image failure. Step 3 asks only "does this
directory hold an organ?", which a family subdirectory such as ``organs/``
would answer yes to the moment the organs move into one — resolving the root
one level too deep, where every ``root / <repo>`` join below misses and,
again, nothing raises. The marker is what a directory says about itself.

Stdlib only.
"""

Expand All @@ -38,6 +51,7 @@
"pyauto_wt_root",
"workspace_root_reason",
"SIBLING_ORGANS",
"ROOT_MARKER",
]

# Any one of these beside a directory marks it as a workspace root. PyAutoMind
Expand All @@ -53,6 +67,11 @@
"PyAutoGut",
)

# A workspace root says so itself by holding this file. It is deliberately
# unversioned — the root is not a git repo — so it marks a checkout layout on
# one machine, not a fact about the organism.
ROOT_MARKER = ".pyauto-root"

_BRAIN_HOME = Path(__file__).resolve().parents[1]


Expand All @@ -61,6 +80,19 @@ def _is_root(path: Path) -> bool:
return any((path / organ).is_dir() for organ in SIBLING_ORGANS)


def _marked_root(start: Path) -> Path | None:
"""The nearest ancestor of `start` holding the marker, or None.

The layout-independent walk the workspace already uses elsewhere to find a
project root: walk up, take the first ancestor carrying the marker file.
Ancestors only — a checkout is never its own workspace root.
"""
for candidate in start.parents:
if (candidate / ROOT_MARKER).is_file():
return candidate
return None


def pyauto_root() -> Path:
"""The workspace root, by the order documented above."""
return Path(workspace_root_reason()[0])
Expand All @@ -75,13 +107,23 @@ def workspace_root_reason() -> tuple[Path, str]:
"""
env = os.environ.get("PYAUTO_ROOT")
if env:
return Path(env), "PYAUTO_ROOT"
# The operator's word either way; the marker only decides how the
# reason reads, so a value exported by a mistaken hook is visible
# rather than silent.
if (Path(env) / ROOT_MARKER).is_file():
return Path(env), "PYAUTO_ROOT"
return Path(env), f"PYAUTO_ROOT (unverified - no {ROOT_MARKER} marker)"
marked = _marked_root(_BRAIN_HOME)
if marked is not None:
return marked, f"{ROOT_MARKER} marker"
parent = _BRAIN_HOME.parent
if _is_root(parent):
# No marker anywhere above us: a single-repo remote checkout, a CI
# matrix, a spawned template. What sits beside us is all there is.
return parent, "beside this checkout"
# No sibling organ next to us. Still the best guess available — but say so,
# so a degraded caller can report "looked in X, not a workspace" rather
# than an empty result that reads like "nothing to do".
# No sibling organ next to us either. Still the best guess available — but
# say so, so a degraded caller can report "looked in X, not a workspace"
# rather than an empty result that reads like "nothing to do".
return parent, "unverified (no sibling organ beside this checkout)"


Expand Down
4 changes: 2 additions & 2 deletions agents/conductors/clone/_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
from pathlib import Path

# Workspace root via the one shared resolver (agents/_pyauto_root.py, mirrored
# by bin/_pyauto_root.sh): PYAUTO_ROOT, else beside this checkout, else the
# developer box. Naming an absolute workspace path as the *default* here
# by bin/_pyauto_root.sh): PYAUTO_ROOT, else the nearest ancestor holding a
# .pyauto-root marker, else beside this checkout, else the parent anyway. Naming an absolute workspace path as the *default* here
# resolved into
# a non-existent tree in a remote session and reported empty rather than
# failing.
Expand Down
4 changes: 2 additions & 2 deletions agents/conductors/community/_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
from pathlib import Path

# Workspace root via the one shared resolver (agents/_pyauto_root.py, mirrored
# by bin/_pyauto_root.sh): PYAUTO_ROOT, else beside this checkout, else the
# developer box. Naming an absolute workspace path as the *default* here
# by bin/_pyauto_root.sh): PYAUTO_ROOT, else the nearest ancestor holding a
# .pyauto-root marker, else beside this checkout, else the parent anyway. Naming an absolute workspace path as the *default* here
# resolved into
# a non-existent tree in a remote session and reported empty rather than
# failing.
Expand Down
4 changes: 2 additions & 2 deletions agents/conductors/profiling/_profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
from typing import Any

# Workspace root via the one shared resolver (agents/_pyauto_root.py, mirrored
# by bin/_pyauto_root.sh): PYAUTO_ROOT, else beside this checkout, else the
# developer box. Naming an absolute workspace path as the *default* here
# by bin/_pyauto_root.sh): PYAUTO_ROOT, else the nearest ancestor holding a
# .pyauto-root marker, else beside this checkout, else the parent anyway. Naming an absolute workspace path as the *default* here
# resolved into
# a non-existent tree in a remote session and reported empty rather than
# failing.
Expand Down
4 changes: 2 additions & 2 deletions agents/conductors/workspace/_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from pathlib import Path

# Workspace root via the one shared resolver (agents/_pyauto_root.py, mirrored
# by bin/_pyauto_root.sh): PYAUTO_ROOT, else beside this checkout, else the
# developer box. Naming an absolute workspace path as the *default* here
# by bin/_pyauto_root.sh): PYAUTO_ROOT, else the nearest ancestor holding a
# .pyauto-root marker, else beside this checkout, else the parent anyway. Naming an absolute workspace path as the *default* here
# resolved into
# a non-existent tree in a remote session and reported empty rather than
# failing.
Expand Down
79 changes: 70 additions & 9 deletions bin/_pyauto_root.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,23 @@
# this way (BRAIN_HOME.parent); this file makes that the one convention instead
# of the majority one.
#
# The order, identical to agents/_pyauto_root.py's:
#
# 1. an explicit PYAUTO_ROOT — the operator's word, taken verbatim, but
# reported as verified or not so a wrong value from a hook is visible;
# 2. the nearest ancestor holding a .pyauto-root marker file — the only rule
# that survives the workspace growing subdirectories, because "does this
# directory hold an organ?" is answered yes by a family directory such as
# organs/ the moment the organs move into one;
# 3. the parent of this checkout when it holds a sibling organ — still true
# for a single-repo remote checkout, which has no root above it to mark;
# 4. the parent regardless, reported as unverified.
#
# Usage (from anywhere in this repo):
# . "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../bin/_pyauto_root.sh"
# then read $PYAUTO_ROOT. An explicit PYAUTO_ROOT in the environment always
# wins, so a caller can still point the tooling at another workspace.
# then read $PYAUTO_ROOT, and $PYAUTO_ROOT_REASON when reporting a degraded
# result. An explicit PYAUTO_ROOT in the environment always wins, so a caller
# can still point the tooling at another workspace.

# Guard against re-sourcing: several agents source both _common.sh and a bin
# helper that each pull this in.
Expand All @@ -39,6 +52,32 @@ _pyauto_brain_home() {
cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/.." && pwd
}

# A workspace root says so itself by holding this file. It is deliberately
# unversioned — the root is not a git repo — so it marks a checkout layout on
# one machine, not a fact about the organism.
PYAUTO_ROOT_MARKER=".pyauto-root"
export PYAUTO_ROOT_MARKER

# The nearest ANCESTOR of $1 holding the marker, printed; non-zero when there
# is none. A checkout is never its own workspace root, so the walk starts one
# level up — the layout-independent walk the workspace already uses elsewhere
# to find a project root.
_pyauto_marked_root() {
_pyauto_d=$(dirname "$1")
while [ "$_pyauto_d" != "/" ]; do
if [ -f "$_pyauto_d/$PYAUTO_ROOT_MARKER" ]; then
printf '%s' "$_pyauto_d"
return 0
fi
_pyauto_d=$(dirname "$_pyauto_d")
done
if [ -f "/$PYAUTO_ROOT_MARKER" ]; then
printf '%s' "/"
return 0
fi
return 1
}

# A directory counts as a workspace root if it holds at least one sibling organ
# besides this one. The Mind is the strongest signal (it carries repos.yaml,
# the body map every consumer wants), but a Brain-only remote session is still
Expand All @@ -49,21 +88,43 @@ _pyauto_is_root() {
|| [ -d "$1/PyAutoCortex" ]
}

# Assigns PYAUTO_ROOT and PYAUTO_ROOT_REASON rather than printing: one
# decision produces two answers, and a command substitution would drop the
# reason. Called directly, so it runs in this shell, not a subshell.
_pyauto_resolve_root() {
# 1. An explicit override is the operator's word; never second-guess it.
# The marker only decides how the reason reads.
if [ -n "${PYAUTO_ROOT:-}" ]; then
printf '%s' "$PYAUTO_ROOT"
if [ -f "$PYAUTO_ROOT/$PYAUTO_ROOT_MARKER" ]; then
PYAUTO_ROOT_REASON="PYAUTO_ROOT"
else
PYAUTO_ROOT_REASON="PYAUTO_ROOT (unverified - no $PYAUTO_ROOT_MARKER marker)"
fi
return 0
fi
# 2. Beside this checkout — the layout that is true in every environment
# the organism actually runs in, remote sessions included. 3. Failing
# that, the parent anyway: the best guess available, and a real path the
# caller can name in a diagnostic.
printf '%s' "$(dirname "$(_pyauto_brain_home)")"
_pyauto_home="$(_pyauto_brain_home)"
# 2. A marked ancestor — the root naming itself, which stays right however
# deep under it this checkout sits.
if _pyauto_marked="$(_pyauto_marked_root "$_pyauto_home")"; then
PYAUTO_ROOT="$_pyauto_marked"
PYAUTO_ROOT_REASON="$PYAUTO_ROOT_MARKER marker"
return 0
fi
# 3. Beside this checkout — no marker anywhere above us, so what sits
# beside us is all there is: a single-repo remote session, a CI matrix,
# a spawned template. 4. Failing that, the parent anyway: the best guess
# available, and a real path the caller can name in a diagnostic.
PYAUTO_ROOT="$(dirname "$_pyauto_home")"
if _pyauto_is_root "$PYAUTO_ROOT"; then
PYAUTO_ROOT_REASON="beside this checkout"
else
PYAUTO_ROOT_REASON="unverified (no sibling organ beside this checkout)"
fi
}

PYAUTO_ROOT="$(_pyauto_resolve_root)"
_pyauto_resolve_root
export PYAUTO_ROOT
export PYAUTO_ROOT_REASON

# Task worktrees live beside the workspace root, not inside it, so they derive
# from whatever the root resolved to rather than re-deriving from $HOME.
Expand Down
21 changes: 21 additions & 0 deletions bin/worktree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ worktree_create() {

mkdir -p "$root" || return 1

# Mark the bundle as a workspace root of its own. Without this, a tool run
# inside the bundle walks past it to the canonical workspace above — the
# sibling-symlink loop below is `"$PYAUTO_MAIN"/*`, which does not glob
# dotfiles, so the canonical marker never arrives here either. Unversioned
# and per-machine, like the canonical one; `worktree_remove` takes it with
# the bundle.
cat > "$root/$PYAUTO_ROOT_MARKER" <<'EOF'
# Task-bundle root marker.
#
# Its presence is what makes this directory a workspace root: the bundle
# holding this task's real git worktrees, plus a symlink back to the canonical
# checkout for every repo the task did not claim.
# `PyAutoBrain/agents/_pyauto_root.py` and `PyAutoBrain/bin/_pyauto_root.sh`
# walk UP from a checkout and take the first ancestor holding this file, so
# work done in here resolves to the bundle rather than to the workspace above.
#
# Written by worktree_create (PyAutoBrain/bin/worktree.sh). Intentionally
# unversioned: a bundle root is not a git repo, so this file belongs to no
# repository.
EOF

local repo
for repo in "$@"; do
if ! _worktree_add_one "$task" "$repo"; then
Expand Down
Loading
Loading