From baa1bf2f957d0bfa5b7fac628150f0b343c6989a Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Mon, 5 Oct 2026 19:55:28 +0100 Subject: [PATCH 1/2] fix: worktree_create never writes through a symlinked activate.sh (#469) The sibling-symlink loop linked the workspace root's activate.sh into each new bundle, and the bundle's activate.sh was then written through that link, clobbering the unversioned root file and retargeting every earlier bundle. - worktree_create skips the names it writes itself (activate.sh, the .pyauto-root marker) in both sibling-symlink loops, and rm -f's activate.sh before writing it so the write never follows a link. - New opt-in worktree_repair_activate [task...]: regenerates symlinked bundle activate.sh files as regular files; bundle paths only, never resolves or writes the link target, never called automatically. - tests/test_worktree_activate.py: hermetic fabricated PYAUTO_MAIN / PYAUTO_WT_ROOT; root bytes unchanged, bundles own regular activate.sh, second bundle does not retarget the first, repair converts links. Co-Authored-By: Claude Opus 5.5 --- bin/worktree.sh | 51 +++++++++++++- tests/test_worktree_activate.py | 118 ++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 tests/test_worktree_activate.py diff --git a/bin/worktree.sh b/bin/worktree.sh index 3242ff97..41ab7a57 100644 --- a/bin/worktree.sh +++ b/bin/worktree.sh @@ -113,8 +113,12 @@ _worktree_add_one() { # Creates $PYAUTO_WT_ROOT// and inside it: # - a real git worktree for each listed repo, on branch feature/ # (or checked out if the branch already exists) -# - a symlink back to $PYAUTO_MAIN/ for every other top-level entry -# - activate.sh with the per-task PYTHONPATH / cache overrides +# - a symlink back to $PYAUTO_MAIN/ for every other top-level entry, +# EXCEPT the names worktree_create writes itself (activate.sh and the +# root marker) — a linked activate.sh would make the write below follow +# the link and clobber the unversioned workspace-root file (#469) +# - activate.sh with the per-task PYTHONPATH / cache overrides, always a +# regular file of its own (any pre-existing link is removed first) # The branch is created from the repo's default branch if it doesn't exist. # Accepts both library repos (PyAutoFit, etc.) and workspace repos # (autolens_workspace, etc.). @@ -171,6 +175,9 @@ EOF # even when the main workspace stores it inside a family directory. local entry name family local -A families=() + # Names this function writes into the bundle root itself: never symlink + # them, or the write would land in $PYAUTO_MAIN (#469). + local -A own=([activate.sh]=1 ["$PYAUTO_ROOT_MARKER"]=1) while IFS=$'\t' read -r name entry; do [[ -n "$entry" ]] || continue if [[ "$(dirname "$entry")" != "$PYAUTO_MAIN" ]]; then @@ -181,17 +188,23 @@ EOF for entry in "$PYAUTO_MAIN"/*; do name="$(basename "$entry")" [[ -n "${families[$name]:-}" ]] && continue + [[ -n "${own[$name]:-}" ]] && continue [[ -d "$entry/.git" || -f "$entry/.git" ]] && continue [[ -e "$root/$name" ]] && continue # already a real worktree ln -s "$entry" "$root/$name" done while IFS=$'\t' read -r name entry; do [[ -n "$name" && -n "$entry" ]] || continue + [[ -n "${own[$name]:-}" ]] && continue [[ -d "$entry" ]] || continue [[ -e "$root/$name" ]] && continue ln -s "$entry" "$root/$name" done <<< "$repo_listing" + # Belt-and-braces: remove whatever is there (a symlink included) so the + # redirect below creates a fresh regular file and can never follow a link + # into the workspace root. + rm -f "$root/activate.sh" worktree_activate_script "$task" > "$root/activate.sh" chmod +x "$root/activate.sh" @@ -199,6 +212,40 @@ EOF echo " activate with: source $root/activate.sh" } +# worktree_repair_activate [task-name ...] +# Opt-in, never called automatically. Converts a bundle activate.sh that is a +# SYMLINK (pre-#469 damage: it pointed at the workspace-root activate.sh, so +# every bundle followed whichever task wrote the root last) into a regenerated +# regular file. With no arguments, scans every bundle under $PYAUTO_WT_ROOT. +# Acts on bundle paths only: the link is removed, its target is never resolved +# or written, and the workspace root is never touched. Regular-file +# activate.sh entries are left as they are. +worktree_repair_activate() { + local -a tasks=("$@") + local b task + if [[ ${#tasks[@]} -eq 0 ]]; then + for b in "$PYAUTO_WT_ROOT"/*/; do + [[ -d "$b" ]] || continue + tasks+=("$(basename "$b")") + done + fi + local repaired=0 + for task in "${tasks[@]}"; do + b="$(worktree_root_path "$task")" + if [[ ! -d "$b" ]]; then + echo "worktree_repair_activate: $b does not exist — skipping" >&2 + continue + fi + [[ -L "$b/activate.sh" ]] || continue + rm -f "$b/activate.sh" || return 1 + worktree_activate_script "$task" > "$b/activate.sh" || return 1 + chmod +x "$b/activate.sh" + echo "worktree_repair_activate: regenerated $b/activate.sh" + repaired=$((repaired + 1)) + done + echo "worktree_repair_activate: $repaired bundle activate.sh file(s) repaired" +} + # worktree_add_repo # Attaches a new repo worktree to an existing task root. Used to bring a # workspace repo into a task that previously only claimed library repos diff --git a/tests/test_worktree_activate.py b/tests/test_worktree_activate.py new file mode 100644 index 00000000..95a8205f --- /dev/null +++ b/tests/test_worktree_activate.py @@ -0,0 +1,118 @@ +"""worktree_create never writes through a symlinked activate.sh (#469). + +The sibling-symlink loop used to link the workspace root's activate.sh into +the bundle, and the bundle's activate.sh was then written THROUGH that link, +clobbering the unversioned root file and retargeting every earlier bundle. +Hermetic: everything lives under tmp_path; the real workspace is never read +or written. +""" + +import os +import subprocess +from pathlib import Path + +import pytest + + +WORKTREE_SH = Path(__file__).resolve().parents[1] / "bin/worktree.sh" +ROOT_ACTIVATE = b"# fabricated root activate.sh - must never change\nexport FAKE_ROOT=1\n" + + +def _git(*args): + subprocess.run(["git", *map(str, args)], check=True, capture_output=True) + + +@pytest.fixture +def workspace(tmp_path): + main = tmp_path / "main" + mind = main / "PyAutoMind" + mind.mkdir(parents=True) + (mind / "repos.yaml").write_text("repos:\n Demo:\n path: Demo\n") + (main / "activate.sh").write_bytes(ROOT_ACTIVATE) + demo = main / "Demo" + demo.mkdir() + remote = tmp_path / "remote.git" + _git("init", "--bare", remote) + _git("init", "-b", "main", demo) + _git("-C", demo, "config", "user.email", "test@example.com") + _git("-C", demo, "config", "user.name", "Test") + (demo / "README.md").write_text("demo\n") + _git("-C", demo, "add", "README.md") + _git("-C", demo, "commit", "-m", "initial") + _git("-C", demo, "remote", "add", "origin", remote) + _git("-C", demo, "push", "-u", "origin", "main") + + bundles = tmp_path / "bundles" + env = os.environ | { + "PYAUTO_MAIN": str(main), + "PYAUTO_WT_ROOT": str(bundles), + "PYAUTO_WT_FORCE": "1", + } + return main, bundles, env + + +def _run(env, cmd): + result = subprocess.run( + ["bash", "-c", f'source "{WORKTREE_SH}"; {cmd}'], + env=env, capture_output=True, text=True, + ) + assert result.returncode == 0, result.stderr + return result + + +def _assert_own_activate(bundle, task): + activate = bundle / "activate.sh" + assert not activate.is_symlink() + assert activate.is_file() + text = activate.read_text() + assert f'export PYAUTO_TASK="{task}"' in text + assert f"{bundle}/PyAutoFit" in text + + +def test_create_leaves_root_activate_untouched(workspace, tmp_path): + main, bundles, env = workspace + _run(env, "worktree_create task Demo") + bundle = bundles / "task" + assert tmp_path in bundle.parents + assert (main / "activate.sh").read_bytes() == ROOT_ACTIVATE + _assert_own_activate(bundle, "task") + assert (bundle / ".pyauto-root").is_file() + assert not (bundle / ".pyauto-root").is_symlink() + + +def test_second_bundle_does_not_retarget_first(workspace): + main, bundles, env = workspace + _run(env, "worktree_create first Demo") + _run(env, "worktree_create second Demo") + assert (main / "activate.sh").read_bytes() == ROOT_ACTIVATE + _assert_own_activate(bundles / "first", "first") + _assert_own_activate(bundles / "second", "second") + + +def test_repair_converts_symlinked_bundle_activate(workspace): + main, bundles, env = workspace + _run(env, "worktree_create task Demo") + bundle = bundles / "task" + # Recreate the pre-fix damage: the bundle's activate.sh is a link to root. + (bundle / "activate.sh").unlink() + (bundle / "activate.sh").symlink_to(main / "activate.sh") + + _run(env, "worktree_repair_activate task") + + assert (main / "activate.sh").read_bytes() == ROOT_ACTIVATE + _assert_own_activate(bundle, "task") + + +def test_repair_defaults_to_every_bundle(workspace): + main, bundles, env = workspace + _run(env, "worktree_create one Demo") + _run(env, "worktree_create two Demo") + for task in ("one", "two"): + (bundles / task / "activate.sh").unlink() + (bundles / task / "activate.sh").symlink_to(main / "activate.sh") + + _run(env, "worktree_repair_activate") + + assert (main / "activate.sh").read_bytes() == ROOT_ACTIVATE + _assert_own_activate(bundles / "one", "one") + _assert_own_activate(bundles / "two", "two") From e4e2f818cca3b34e5c0be33ff179a1a18b0aeeb9 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Tue, 6 Oct 2026 09:07:19 +0100 Subject: [PATCH 2/2] test: use synthetic repo names in test_worktree_activate.py (tenant firewall) Assert the bundle's activate.sh carries its own PYAUTO_ROOT and opens PYTHONPATH with the bundle's PyAutoNerves path (an organ name) instead of naming a satellite library, which tripped the organ-code tenant firewall. Co-Authored-By: Claude Opus 5.5 --- tests/test_worktree_activate.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_worktree_activate.py b/tests/test_worktree_activate.py index 95a8205f..5422eb38 100644 --- a/tests/test_worktree_activate.py +++ b/tests/test_worktree_activate.py @@ -66,7 +66,12 @@ def _assert_own_activate(bundle, task): assert activate.is_file() text = activate.read_text() assert f'export PYAUTO_TASK="{task}"' in text - assert f"{bundle}/PyAutoFit" in text + # The bundle's own library paths are on its PYTHONPATH. PyAutoNerves (an + # organ, first in PYAUTO_LIBS) stands in for the library list: satellite + # repo names in organ code trip the tenant firewall + # (PyAutoMind/scripts/repos_sync.py). + assert f'export PYAUTO_ROOT="{bundle}"' in text + assert f'export PYTHONPATH="{bundle}/PyAutoNerves:' in text def test_create_leaves_root_activate_untouched(workspace, tmp_path):