diff --git a/.claude/hooks/session-start.sh b/.claude/hooks/session-start.sh index 9ac559ce..b85694d7 100755 --- a/.claude/hooks/session-start.sh +++ b/.claude/hooks/session-start.sh @@ -133,11 +133,11 @@ holds_an_organ() { [ -d "$1/PyAutoMind" ] || [ -d "$1/PyAutoBrain" ] || [ -d "$1/PyAutoHeart" ] \ || [ -d "$1/PyAutoHands" ] || [ -d "$1/PyAutoMemory" ] || [ -d "$1/PyAutoGut" ] \ || [ -d "$1/PyAutoNerves" ] || [ -d "$1/PyAutoCortex" ] \ - || [ -d "$1/PyAutoScientist" ] || [ -d "$1/organs/PyAutoMind" ] \ + || [ -d "$1/organs/PyAutoMind" ] \ || [ -d "$1/organs/PyAutoBrain" ] || [ -d "$1/organs/PyAutoHeart" ] \ || [ -d "$1/organs/PyAutoHands" ] || [ -d "$1/organs/PyAutoMemory" ] \ || [ -d "$1/organs/PyAutoGut" ] || [ -d "$1/organs/PyAutoNerves" ] \ - || [ -d "$1/organs/PyAutoCortex" ] || [ -d "$1/organs/PyAutoScientist" ] + || [ -d "$1/organs/PyAutoCortex" ] } # Assigns WORKSPACE_ROOT and WORKSPACE_ROOT_REASON rather than printing: one diff --git a/bin/regroup_workspace.py b/bin/regroup_workspace.py index 0a50548f..2cd91fcf 100644 --- a/bin/regroup_workspace.py +++ b/bin/regroup_workspace.py @@ -162,6 +162,29 @@ def plan(root, state, bundles_root=None, manifest_root=None): after = json.dumps(settings, indent=2) + '\n' if before != after: configs.append({'path': str(path), 'old': before, 'new': after}) + # Older bundles predate organ environment overrides. Sourcing them after + # the canonical activation must select their own flat dependency links. + task_roots = {Path(wt).parent for row in records for wt in row['worktrees'] + if wt != row['old']} + if bundles_root is not None and bundles_root.is_dir(): + task_roots.update(p for p in bundles_root.iterdir() if p.is_dir() and not p.is_symlink()) + import shlex + for task_root in sorted(task_roots): + task_activation = task_root / 'activate.sh' + if not task_activation.is_file() or task_activation.is_symlink(): + continue + before = task_activation.read_text() + if 'Generated by PyAutoBrain/bin/worktree.sh' not in before: + continue # An unrelated/user-authored activation is not ours to rewrite. + if not all((task_root / name).is_dir() for name in + ('PyAutoBrain', 'PyAutoMind', 'PyAutoHeart', 'PyAutoHands')): + continue + exports = '\n# Select this bundle after a canonical workspace activation.\n' + for variable, name in [('BRAIN', 'PyAutoBrain'), ('MIND', 'PyAutoMind'), + ('HEART', 'PyAutoHeart'), ('HANDS', 'PyAutoHands')]: + exports += f'export PYAUTO_{variable}={shlex.quote(str(task_root / name))}\n' + exports += 'export PATH="$PYAUTO_BRAIN/bin:$PYAUTO_HEART/bin:$PYAUTO_HANDS/bin:$PATH"\n' + configs.append({'path': str(task_activation), 'old': before, 'new': before + exports}) activation = root / 'activate.sh' brain_relative = str(remap(repo_path(root, 'PyAutoBrain'), moves).relative_to(root)) heart_relative = str(remap(repo_path(root, 'PyAutoHeart'), moves).relative_to(root)) diff --git a/tests/test_regroup_workspace.py b/tests/test_regroup_workspace.py index 321891ae..25d10713 100644 --- a/tests/test_regroup_workspace.py +++ b/tests/test_regroup_workspace.py @@ -36,6 +36,9 @@ def fixture(tmp_path): task = bundles / 'task' task.mkdir(parents=True) git(repo, 'worktree', 'add', '-qb', 'task', str(task / 'Demo')) + (task / 'activate.sh').write_text('# Generated by PyAutoBrain/bin/worktree.sh\nexport PYAUTO_ROOT=' + str(task) + '\n') + for name in ['PyAutoBrain', 'PyAutoMind', 'PyAutoHeart', 'PyAutoHands']: + (task / name).mkdir() dependency = bundles / 'other' dependency.mkdir() (dependency / 'Demo').symlink_to(repo, target_is_directory=True) @@ -63,7 +66,9 @@ def test_apply_and_rollback_preserve_dirty_data_and_linked_worktree(tmp_path): assert '${CLAUDE_PROJECT_DIR}/science/Demo/hook.py' in (root / '.claude/settings.json').read_text() import json assert json.loads((root / '.claude/settings.json').read_text())['env']['PYTHONPATH'] == str(new) + assert f'export PYAUTO_MIND={task}/PyAutoMind' in (task / 'activate.sh').read_text() migration.rollback(journal, data) + assert 'PYAUTO_MIND=' not in (task / 'activate.sh').read_text() assert migration.snapshot(repo) == before assert not new.exists() assert (bundles / 'other/Demo').resolve() == repo @@ -130,3 +135,12 @@ def test_moving_manifest_checkout_keeps_journal_and_rollback_working(tmp_path): migration.verify(data) migration.rollback(journal, data) assert manifest.exists() + + +def test_unrelated_activation_is_not_rewritten(tmp_path): + root, _, bundles, task = fixture(tmp_path) + activation = task / 'activate.sh' + activation.write_text('export USER_PROJECT=keep\n') + journal = tmp_path / 'journal.json' + data = migration.plan(root, journal, bundles) + assert str(activation) not in [row['path'] for row in data['configs']]