ralph-teams 1.0.32 → 1.0.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,7 @@ You are the hands. You implement. You do NOT choose overall scope or verify your
21
21
  3. **Understand the task** — Read the acceptance criteria or validation findings, the requested scope, and any retry feedback.
22
22
  4. **Create or update automated tests first when they should change** — If planning context includes test work, implement those tests. If no planning context exists and the scope is new behavior, work TDD-style: define the automated tests first, confirm they fail on the current code, then proceed.
23
23
  5. **Implement** — Write clean, minimal code that satisfies the assigned scope and makes the relevant tests pass.
24
- 6. **Infer project commands, then run quality checks** — Determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
24
+ 6. **Use the Team Lead's setup commands when provided, then run quality checks** — If the Team Lead already provided bootstrap, build, or test commands, follow those exact commands first and do not rediscover them unless they fail. Otherwise determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
25
25
  7. **Commit** — Use a conventional commit message that matches the assigned scope.
26
26
  8. **Get the commit SHA** — After committing, run `git rev-parse HEAD` to get the full commit SHA.
27
27
  9. **Report back** — Return the exact commit SHA and a concise summary so validators can inspect exactly what changed.
@@ -58,6 +58,7 @@ If the Team Lead reassigns the scope with validator feedback:
58
58
  - Keep changes minimal and focused on the acceptance criteria or findings.
59
59
  - Do NOT gold-plate.
60
60
  - Treat automated coverage as part of the assignment, not optional cleanup. Do not finish with zero new or updated tests unless the Team Lead explicitly said coverage is already sufficient or you can point to a concrete repository-based reason automated coverage is not possible.
61
+ - If the Team Lead gives exact bootstrap, build, or test commands, use them instead of re-probing the repository with generic commands. Only fall back to fresh command discovery if the provided commands fail or are clearly incomplete.
61
62
  - Infer project commands from the repository before running them. Check `AGENTS.md`, `README*`, and repo instructions first, prefer repo-defined scripts and task runners, and only use generic ecosystem defaults when the repo is unambiguous.
62
63
  - Do not validate your own work against the acceptance criteria beyond normal sanity checks. A separate validator may do that.
63
64
  - Do NOT skip quality checks.
@@ -18,7 +18,7 @@ You are the hands. You implement. You do NOT choose overall scope or verify your
18
18
  3. **Understand the task** — Read the acceptance criteria or validation findings, the requested scope, and any retry feedback.
19
19
  4. **Create or update automated tests first when they should change** — If planning context includes test work, implement those tests. If no planning context exists and the scope is new behavior, work TDD-style: define the automated tests first, confirm they fail on the current code, then proceed.
20
20
  5. **Implement** — Write clean, minimal code that satisfies the assigned scope and makes the relevant tests pass.
21
- 6. **Infer project commands, then run quality checks** — Determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
21
+ 6. **Use the Team Lead's setup commands when provided, then run quality checks** — If the Team Lead already provided bootstrap, build, or test commands, follow those exact commands first and do not rediscover them unless they fail. Otherwise determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
22
22
  7. **Commit** — Use a conventional commit message that matches the assigned scope.
23
23
  8. **Get the commit SHA** — After committing, run `git rev-parse HEAD` to get the full commit SHA.
24
24
  9. **Report back** — Return the exact commit SHA and a concise summary so validators can inspect exactly what changed.
@@ -55,6 +55,7 @@ If the Team Lead reassigns the scope with validator feedback:
55
55
  - Keep changes minimal and focused on the acceptance criteria or findings.
56
56
  - Do NOT gold-plate.
57
57
  - Treat automated coverage as part of the assignment, not optional cleanup. Do not finish with zero new or updated tests unless the Team Lead explicitly said coverage is already sufficient or you can point to a concrete repository-based reason automated coverage is not possible.
58
+ - If the Team Lead gives exact bootstrap, build, or test commands, use them instead of re-probing the repository with generic commands. Only fall back to fresh command discovery if the provided commands fail or are clearly incomplete.
58
59
  - Infer project commands from the repository before running them. Check `AGENTS.md`, `README*`, and repo instructions first, prefer repo-defined scripts and task runners, and only use generic ecosystem defaults when the repo is unambiguous.
59
60
  - Do not validate your own work against the acceptance criteria beyond normal sanity checks. A separate validator may do that.
60
61
  - Do NOT skip quality checks.
@@ -21,7 +21,7 @@ You are the hands. You implement. You do NOT choose overall scope or verify your
21
21
  3. **Understand the task** — Read the acceptance criteria or validation findings, the requested scope, and any retry feedback.
22
22
  4. **Create or update automated tests first when they should change** — If planning context includes test work, implement those tests. If no planning context exists and the scope is new behavior, work TDD-style: define the automated tests first, confirm they fail on the current code, then proceed.
23
23
  5. **Implement** — Write clean, minimal code that satisfies the assigned scope and makes the relevant tests pass.
24
- 6. **Infer project commands, then run quality checks** — Determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
24
+ 6. **Use the Team Lead's setup commands when provided, then run quality checks** — If the Team Lead already provided bootstrap, build, or test commands, follow those exact commands first and do not rediscover them unless they fail. Otherwise determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
25
25
  7. **Commit** — Use a conventional commit message that matches the assigned scope.
26
26
  8. **Get the commit SHA** — After committing, run `git rev-parse HEAD` to get the full commit SHA.
27
27
  9. **Report back** — Return the exact commit SHA and a concise summary so validators can inspect exactly what changed.
@@ -58,6 +58,7 @@ If the Team Lead reassigns the scope with validator feedback:
58
58
  - Keep changes minimal and focused on the acceptance criteria or findings.
59
59
  - Do NOT gold-plate.
60
60
  - Treat automated coverage as part of the assignment, not optional cleanup. Do not finish with zero new or updated tests unless the Team Lead explicitly said coverage is already sufficient or you can point to a concrete repository-based reason automated coverage is not possible.
61
+ - If the Team Lead gives exact bootstrap, build, or test commands, use them instead of re-probing the repository with generic commands. Only fall back to fresh command discovery if the provided commands fail or are clearly incomplete.
61
62
  - Infer project commands from the repository before running them. Check `AGENTS.md`, `README*`, and repo instructions first, prefer repo-defined scripts and task runners, and only use generic ecosystem defaults when the repo is unambiguous.
62
63
  - Do not validate your own work against the acceptance criteria beyond normal sanity checks. A separate validator may do that.
63
64
  - Do NOT skip quality checks.
@@ -21,7 +21,7 @@ You are the hands. You implement. You do NOT choose overall scope or verify your
21
21
  3. **Understand the task** — Read the acceptance criteria or validation findings, the requested scope, and any retry feedback.
22
22
  4. **Create or update automated tests first when they should change** — If planning context includes test work, implement those tests. If no planning context exists and the scope is new behavior, work TDD-style: define the automated tests first, confirm they fail on the current code, then proceed.
23
23
  5. **Implement** — Write clean, minimal code that satisfies the assigned scope and makes the relevant tests pass.
24
- 6. **Infer project commands, then run quality checks** — Determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
24
+ 6. **Use the Team Lead's setup commands when provided, then run quality checks** — If the Team Lead already provided bootstrap, build, or test commands, follow those exact commands first and do not rediscover them unless they fail. Otherwise determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
25
25
  7. **Commit** — Use a conventional commit message that matches the assigned scope.
26
26
  8. **Get the commit SHA** — After committing, run `git rev-parse HEAD` to get the full commit SHA.
27
27
  9. **Report back** — Return the exact commit SHA and a concise summary so validators can inspect exactly what changed.
@@ -58,6 +58,7 @@ If the Team Lead reassigns the scope with validator feedback:
58
58
  - Keep changes minimal and focused on the acceptance criteria or findings.
59
59
  - Do NOT gold-plate.
60
60
  - Treat automated coverage as part of the assignment, not optional cleanup. Do not finish with zero new or updated tests unless the Team Lead explicitly said coverage is already sufficient or you can point to a concrete repository-based reason automated coverage is not possible.
61
+ - If the Team Lead gives exact bootstrap, build, or test commands, use them instead of re-probing the repository with generic commands. Only fall back to fresh command discovery if the provided commands fail or are clearly incomplete.
61
62
  - Infer project commands from the repository before running them. Check `AGENTS.md`, `README*`, and repo instructions first, prefer repo-defined scripts and task runners, and only use generic ecosystem defaults when the repo is unambiguous.
62
63
  - Do not validate your own work against the acceptance criteria beyond normal sanity checks. A separate validator may do that.
63
64
  - Do NOT skip quality checks.
package/README.md CHANGED
@@ -44,9 +44,9 @@ This diagram shows the default `balanced` workflow only.
44
44
 
45
45
  ```mermaid
46
46
  flowchart TD
47
- A[Start run] --> B[Validate PRD and create loop branch]
47
+ A[Start run] --> B[Validate PRD and create loop branch plus loop worktree]
48
48
  B --> C[Pick ready epic]
49
- C --> D[Create worktree and run-scoped epic branch]
49
+ C --> D[Create epic worktree and run-scoped epic branch]
50
50
  D --> E[Team Lead decides on epic planner]
51
51
  E --> F[Team Lead runs stories with builder]
52
52
  F --> G{Epic validator needed?}
@@ -553,9 +553,9 @@ The current execution contract is:
553
553
  - blocked epics are skipped until dependencies are complete
554
554
  - runs sequentially by default
555
555
  - experimental wave parallelism is enabled only with `--parallel <n>`
556
- - at run start Ralph auto-commits any dirty worktree changes, then creates a fresh loop branch from your current branch
556
+ - at run start Ralph auto-commits any dirty source-worktree changes, then creates a fresh loop branch and checks it out in `.ralph-teams/.worktrees/loop`
557
557
  - each epic gets its own worktree and a run-scoped branch rooted from that loop branch, using `ralph/epic/<loop-run>/<epic-id>`
558
- - before the Team Lead starts, Ralph creates the worktree and hands repo inspection, setup, build, and test command inference to the agents
558
+ - before the Team Lead starts, Ralph creates the worktree and expects the Team Lead to establish that epic worktree's setup once, then hand the exact bootstrap, build, and test commands to Builders
559
559
  - agents are expected to prefer repo-defined scripts and docs over generic ecosystem defaults when choosing setup and verification commands
560
560
  - the shell-built Team Lead prompt must keep literal filenames shell-safe; do not add raw Markdown backticks inside that Bash string because Bash will treat them as command substitution
561
561
  - when an epic completes successfully, the Team Lead is expected to merge its epic branch back into the loop branch and write the merge-result artifact
@@ -638,9 +638,9 @@ Install or relink the package so the bundled JSON CLI is on your `PATH`:
638
638
  npm install -g ralph-teams
639
639
  ```
640
640
 
641
- ### Ralph needs to switch branches but the worktree is dirty
641
+ ### Ralph needs a clean base commit before creating the loop worktree
642
642
 
643
- Ralph auto-commits current changes before creating or switching to the loop branch. If you do not want that commit, clean up the worktree yourself before starting the run.
643
+ Ralph auto-commits current source-worktree changes before creating the loop branch and loop worktree. If you do not want that commit, clean up the worktree yourself before starting the run.
644
644
 
645
645
  ## Development
646
646
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ralph-teams",
3
- "version": "1.0.32",
3
+ "version": "1.0.34",
4
4
  "description": "CLI tool for Ralph Teams",
5
5
  "bin": {
6
6
  "ralph-teams": "dist/index.js",
@@ -19,7 +19,7 @@ You are the hands. You implement. You do NOT choose overall scope or verify your
19
19
  3. **Understand the task** — Read the acceptance criteria or validation findings, the requested scope, and any retry feedback.
20
20
  4. **Create or update automated tests first when they should change** — If planning context includes test work, implement those tests. If no planning context exists and the scope is new behavior, work TDD-style: define the automated tests first, confirm they fail on the current code, then proceed.
21
21
  5. **Implement** — Write clean, minimal code that satisfies the assigned scope and makes the relevant tests pass.
22
- 6. **Infer project commands, then run quality checks** — Determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
22
+ 6. **Use the Team Lead's setup commands when provided, then run quality checks** — If the Team Lead already provided bootstrap, build, or test commands, follow those exact commands first and do not rediscover them unless they fail. Otherwise determine the setup, build, and test commands from repo instructions and manifests. Check `AGENTS.md`, `README*`, and contributor docs first. Prefer repo-defined scripts or task runners over ecosystem defaults, then run the relevant verification commands for the assigned scope. Fix issues before committing.
23
23
  7. **Commit** — Use a conventional commit message that matches the assigned scope.
24
24
  8. **Get the commit SHA** — After committing, run `git rev-parse HEAD` to get the full commit SHA.
25
25
  9. **Report back** — Return the exact commit SHA and a concise summary so validators can inspect exactly what changed.
@@ -56,6 +56,7 @@ If the Team Lead reassigns the scope with validator feedback:
56
56
  - Keep changes minimal and focused on the acceptance criteria or findings.
57
57
  - Do NOT gold-plate.
58
58
  - Treat automated coverage as part of the assignment, not optional cleanup. Do not finish with zero new or updated tests unless the Team Lead explicitly said coverage is already sufficient or you can point to a concrete repository-based reason automated coverage is not possible.
59
+ - If the Team Lead gives exact bootstrap, build, or test commands, use them instead of re-probing the repository with generic commands. Only fall back to fresh command discovery if the provided commands fail or are clearly incomplete.
59
60
  - Infer project commands from the repository before running them. Check `AGENTS.md`, `README*`, and repo instructions first, prefer repo-defined scripts and task runners, and only use generic ecosystem defaults when the repo is unambiguous.
60
61
  - Do not validate your own work against the acceptance criteria beyond normal sanity checks. A separate validator may do that.
61
62
  - Do NOT skip quality checks.
@@ -20,6 +20,10 @@ You coordinate epic execution. For clearly easy, low-risk mechanical tasks, you
20
20
  - Start with repository instructions: `AGENTS.md`, `README*`, contributor docs, and any project-local guidance files referenced there. Then prefer repository-defined task runners and scripts over language defaults: `Makefile`, `justfile`, `Taskfile.yml`, package scripts, wrapper scripts, or documented commands.
21
21
  - Then inspect ecosystem manifests such as `package.json`, `pyproject.toml`, `requirements.txt`, `Cargo.toml`, `go.mod`, `Gemfile`, `pom.xml`, `build.gradle*`, `mix.exs`, `Dockerfile`, and `docker-compose*.yml`.
22
22
  - Prefer explicit repository commands over generic ecosystem defaults even when the language is obvious.
23
+ - Before the first Builder for an epic, prepare the epic worktree environment once. Do not make each Builder rediscover basic setup from scratch.
24
+ - Start by checking whether the source checkout path provided in the runtime prompt already contains reusable dependency or generated setup artifacts that the epic worktree can safely reuse.
25
+ - If safe reuse is possible, materialize that reuse inside the epic worktree and continue. If reuse is not possible, run the repository's native bootstrap or install command inside the epic worktree before delegating implementation.
26
+ - Once the bootstrap, build, and test commands are established, pass the exact commands and any required environment-prep steps to every Builder for that epic.
23
27
  - Only use generic defaults when the repository is unambiguous.
24
28
  - If setup or verification remains ambiguous after inspection, do not guess wildly. Mark the attempt failed with a short concrete reason describing the ambiguity.
25
29
 
@@ -47,7 +51,8 @@ You coordinate epic execution. For clearly easy, low-risk mechanical tasks, you
47
51
 
48
52
  - Before starting a story, check the epic state file. If the story has `passes: true`, skip it.
49
53
  - For clearly easy, low-risk mechanical stories, you may implement directly in the Team Lead session when delegation overhead would exceed the work. Keep the change narrowly scoped and still run the required verification yourself before counting the story complete.
50
- - Before delegating a story, determine the likely setup/build/test commands for this repository and pass the relevant commands or repository-based guidance to the Builder.
54
+ - Before delegating the first story, ensure the epic worktree environment is actually runnable.
55
+ - Before delegating any story, pass the exact bootstrap, build, and test commands already established for this epic to the Builder.
51
56
  - If an epic plan exists, give the Builder the story, acceptance criteria, relevant plan section, and especially the story's planned test design.
52
57
  - If a story planner was used, give the Builder the story planner output too.
53
58
  - Require the Builder to add or update automated tests for the story and make them pass before the story can count as complete.
@@ -7,12 +7,20 @@ You are the Team Lead for this epic. Read the epic below and execute it.
7
7
  ALL work for this epic MUST happen in this directory: {{WORKTREE_ABS_PATH}}
8
8
  Do NOT modify files outside this directory, except for the epic state file below and the final merge workflow paths listed later in this prompt.
9
9
 
10
+ ## Source Checkout
11
+ - Source checkout path: {{SOURCE_ROOT_DIR}}
12
+ - You may inspect this source checkout read-only to understand repo-level setup and to reuse existing dependency or build artifacts when that is safer or faster than reinstalling inside the epic worktree.
13
+ - Do NOT modify the source checkout during normal implementation. Any reuse should materialize inside the epic worktree, for example by creating a symlink there or copying a cacheable artifact into the worktree.
14
+
10
15
  ## Project Setup Strategy
11
16
  - Ralph does not preinstall dependencies or preselect build/test commands for this repo.
12
- - Before delegating implementation, infer the setup, build, and test commands from project context.
17
+ - Before delegating implementation, establish the epic worktree environment once for this epic: determine the setup, build, and test commands, then make the worktree runnable before the first Builder starts.
13
18
  - Check repo instructions first: 'AGENTS.md', 'README*', contributor docs, and project-local guidance files. Then check repo-defined task runners or scripts such as 'Makefile', 'justfile', 'Taskfile.yml', package scripts, wrapper scripts, or documented commands.
14
19
  - Then inspect ecosystem manifests such as 'package.json', 'pyproject.toml', 'requirements.txt', 'Cargo.toml', 'go.mod', 'Gemfile', 'pom.xml', 'build.gradle*', 'mix.exs', 'Dockerfile', and 'docker-compose*.yml'.
15
20
  - Prefer explicit repository commands over generic ecosystem defaults.
21
+ - If the epic worktree is missing dependencies or other generated setup artifacts, first check whether the source checkout already has reusable artifacts that can be safely reused from the worktree.
22
+ - Prefer safe reuse from the source checkout when the repository structure and lockfiles make that reuse trustworthy; otherwise run the repository's native bootstrap/install step inside the epic worktree.
23
+ - After you determine the correct bootstrap, build, and test commands, pass those exact commands to every Builder for this epic and tell Builders not to rediscover them unless the provided commands fail.
16
24
  - Only fall back to generic defaults when the repository is unambiguous.
17
25
  - If setup remains ambiguous after inspection, stop guessing and fail the story attempt with a short concrete reason describing what you found.
18
26
 
package/ralph.sh CHANGED
@@ -331,15 +331,17 @@ if [ ! -f "$PRD_FILE" ]; then
331
331
  exit 1
332
332
  fi
333
333
 
334
- ROOT_DIR="$(pwd)"
335
- RALPH_RUNTIME_DIR="${ROOT_DIR}/${RALPH_RUNTIME_DIRNAME}"
334
+ SOURCE_ROOT_DIR="$(pwd)"
335
+ ROOT_DIR="$SOURCE_ROOT_DIR"
336
+ LOOP_ROOT_DIR=""
337
+ RALPH_RUNTIME_DIR="${SOURCE_ROOT_DIR}/${RALPH_RUNTIME_DIRNAME}"
336
338
  PROGRESS_FILE="${RALPH_RUNTIME_DIR}/progress.txt"
337
339
  PLANS_DIR="${RALPH_RUNTIME_DIR}/plans"
338
340
  LOGS_DIR="${RALPH_RUNTIME_DIR}/logs"
339
341
  STATE_DIR="${RALPH_RUNTIME_DIR}/state"
340
342
  WORKTREES_DIR="${RALPH_RUNTIME_DIR}/.worktrees"
341
343
 
342
- repair_root_runtime_dir_if_needed() {
344
+ repair_source_runtime_dir_if_needed() {
343
345
  if [ -L "$RALPH_RUNTIME_DIR" ]; then
344
346
  local runtime_link_target=""
345
347
  runtime_link_target=$(readlink "$RALPH_RUNTIME_DIR" 2>/dev/null || true)
@@ -357,7 +359,7 @@ unstage_runtime_artifacts() {
357
359
  git rm --cached -r --ignore-unmatch "$RALPH_RUNTIME_DIRNAME" >/dev/null 2>&1 || true
358
360
  }
359
361
 
360
- repair_root_runtime_dir_if_needed
362
+ repair_source_runtime_dir_if_needed
361
363
  mkdir -p "$RALPH_RUNTIME_DIR" "$PLANS_DIR" "$LOGS_DIR" "$STATE_DIR" "$WORKTREES_DIR"
362
364
 
363
365
  ensure_runtime_rjq_bin() {
@@ -581,7 +583,7 @@ ensure_runtime_gitignore_entries() {
581
583
  prompt_to_commit_dirty_worktree() {
582
584
  local target_branch="$1"
583
585
 
584
- echo "Worktree has uncommitted changes and Ralph needs to create or switch to branch '$target_branch'."
586
+ echo "Worktree has uncommitted changes and Ralph needs a clean base commit before creating loop worktree branch '$target_branch'."
585
587
  echo "Ralph will now stage and commit all current changes before the run."
586
588
  git status --short
587
589
  git add -A
@@ -617,6 +619,15 @@ auto_remove_stale_worktree_dir() {
617
619
  rm -rf "$worktree_path"
618
620
  }
619
621
 
622
+ find_worktree_for_branch() {
623
+ local branch_name="$1"
624
+
625
+ git worktree list --porcelain 2>/dev/null | awk -v branch="refs/heads/${branch_name}" '
626
+ $1 == "worktree" { path = substr($0, 10); next }
627
+ $1 == "branch" && $2 == branch { print path; exit }
628
+ '
629
+ }
630
+
620
631
  cleanup_epic_worktree_artifacts() {
621
632
  local worktree_path="$1"
622
633
  local branch_name="$2"
@@ -637,34 +648,56 @@ cleanup_epic_worktree_artifacts() {
637
648
  git branch -D "$branch_name" >/dev/null 2>&1 || true
638
649
  }
639
650
 
640
- # --- Ensure loop branch exists and is checked out ---
651
+ # --- Ensure loop branch exists and has a dedicated worktree ---
641
652
  ensure_runtime_gitignore_entries
642
653
  ensure_repo_has_initial_commit
643
654
 
644
655
  mkdir -p "$RALPH_RUNTIME_DIR" "$PLANS_DIR" "$LOGS_DIR" "$STATE_DIR" "$WORKTREES_DIR"
645
656
 
646
- ensure_loop_branch_ready() {
647
- local checkout_output=""
657
+ ensure_loop_worktree_ready() {
658
+ local loop_worktree_path="${WORKTREES_DIR}/loop"
659
+ local add_output=""
660
+ local retry_output=""
661
+ local existing_worktree=""
662
+
663
+ existing_worktree="$(find_worktree_for_branch "$LOOP_BRANCH" || true)"
664
+ if [ -n "$existing_worktree" ] && [ -d "$existing_worktree" ]; then
665
+ LOOP_ROOT_DIR="$existing_worktree"
666
+ ensure_worktree_runtime_link "$LOOP_ROOT_DIR"
667
+ return
668
+ fi
669
+
670
+ if ! git diff --quiet 2>/dev/null || ! git diff --cached --quiet 2>/dev/null; then
671
+ prompt_to_commit_dirty_worktree "$LOOP_BRANCH"
672
+ fi
648
673
 
649
- if [ "$CURRENT_BRANCH" != "$LOOP_BRANCH" ]; then
650
- if ! git diff --quiet 2>/dev/null || ! git diff --cached --quiet 2>/dev/null; then
651
- prompt_to_commit_dirty_worktree "$LOOP_BRANCH"
674
+ if ! git show-ref --verify --quiet "refs/heads/${LOOP_BRANCH}"; then
675
+ echo "Creating loop branch: $LOOP_BRANCH (from $CURRENT_BRANCH)"
676
+ if ! add_output=$(git branch "$LOOP_BRANCH" "$CURRENT_BRANCH" 2>&1 >/dev/null); then
677
+ echo "Error: failed to create loop branch '$LOOP_BRANCH' from '$CURRENT_BRANCH'." >&2
678
+ [ -n "$add_output" ] && echo "$add_output" >&2
679
+ exit 1
652
680
  fi
681
+ fi
653
682
 
654
- if git show-ref --verify --quiet "refs/heads/${LOOP_BRANCH}"; then
655
- echo "Switching to loop branch: $LOOP_BRANCH"
656
- if ! checkout_output=$(git checkout "$LOOP_BRANCH" 2>&1 >/dev/null); then
657
- echo "Error: failed to switch to loop branch '$LOOP_BRANCH'." >&2
658
- [ -n "$checkout_output" ] && echo "$checkout_output" >&2
659
- exit 1
660
- fi
661
- else
662
- echo "Creating loop branch: $LOOP_BRANCH (from $CURRENT_BRANCH)"
663
- if ! checkout_output=$(git checkout -b "$LOOP_BRANCH" 2>&1 >/dev/null); then
664
- echo "Error: failed to create loop branch '$LOOP_BRANCH' from '$CURRENT_BRANCH'." >&2
665
- [ -n "$checkout_output" ] && echo "$checkout_output" >&2
666
- exit 1
667
- fi
683
+ git worktree prune >/dev/null 2>&1 || true
684
+ if [ -d "$loop_worktree_path" ] && ! git worktree list | grep -Fq "$loop_worktree_path"; then
685
+ auto_remove_stale_worktree_dir "$loop_worktree_path" "$LOOP_BRANCH"
686
+ fi
687
+
688
+ echo "Creating loop worktree: $loop_worktree_path ($LOOP_BRANCH)"
689
+ if ! add_output=$(git worktree add "$loop_worktree_path" "$LOOP_BRANCH" 2>&1 >/dev/null); then
690
+ echo "Loop worktree creation for '$LOOP_BRANCH' failed on the first attempt; pruning stale state and retrying once." >&2
691
+ [ -n "$add_output" ] && echo "$add_output" >&2
692
+ git worktree prune >/dev/null 2>&1 || true
693
+ git worktree remove "$loop_worktree_path" --force >/dev/null 2>&1 || true
694
+ if [ -d "$loop_worktree_path" ]; then
695
+ auto_remove_stale_worktree_dir "$loop_worktree_path" "$LOOP_BRANCH"
696
+ fi
697
+ if ! retry_output=$(git worktree add "$loop_worktree_path" "$LOOP_BRANCH" 2>&1 >/dev/null); then
698
+ echo "Error: failed to create loop worktree '$loop_worktree_path' for '$LOOP_BRANCH'." >&2
699
+ [ -n "$retry_output" ] && echo "$retry_output" >&2
700
+ exit 1
668
701
  fi
669
702
  fi
670
703
 
@@ -673,15 +706,17 @@ ensure_loop_branch_ready() {
673
706
  exit 1
674
707
  fi
675
708
 
676
- CURRENT_BRANCH=$(git branch --show-current 2>/dev/null || echo "")
677
- if [ "$CURRENT_BRANCH" != "$LOOP_BRANCH" ]; then
678
- echo "Error: expected current branch '$LOOP_BRANCH' after setup, got '${CURRENT_BRANCH:-<none>}'." >&2
709
+ LOOP_ROOT_DIR="$loop_worktree_path"
710
+ ensure_worktree_runtime_link "$LOOP_ROOT_DIR"
711
+
712
+ local loop_branch_name=""
713
+ loop_branch_name=$(git -C "$LOOP_ROOT_DIR" branch --show-current 2>/dev/null || echo "")
714
+ if [ "$loop_branch_name" != "$LOOP_BRANCH" ]; then
715
+ echo "Error: expected loop worktree branch '$LOOP_BRANCH' after setup, got '${loop_branch_name:-<none>}'." >&2
679
716
  exit 1
680
717
  fi
681
718
  }
682
719
 
683
- ensure_loop_branch_ready
684
-
685
720
  epic_branch_name() {
686
721
  local epic_id="$1"
687
722
  echo "ralph/epic/${LOOP_BRANCH#ralph/}/${epic_id}"
@@ -746,7 +781,7 @@ create_epic_worktree() {
746
781
  local epic_id="$1"
747
782
  local branch_name
748
783
  branch_name=$(epic_branch_name "$epic_id")
749
- local worktree_path="${RALPH_RUNTIME_DIRNAME}/.worktrees/${epic_id}"
784
+ local worktree_path="${WORKTREES_DIR}/${epic_id}"
750
785
  local add_output
751
786
  local retry_output
752
787
 
@@ -785,10 +820,20 @@ ensure_worktree_runtime_link() {
785
820
  local worktree_abs_path="$1"
786
821
  local worktree_runtime_path="${worktree_abs_path}/${RALPH_RUNTIME_DIRNAME}"
787
822
 
788
- if [ -L "$worktree_runtime_path" ]; then
823
+ if [ "$worktree_abs_path" = "$SOURCE_ROOT_DIR" ]; then
789
824
  return 0
790
825
  fi
791
826
 
827
+ if [ -L "$worktree_runtime_path" ]; then
828
+ local current_target
829
+ current_target="$(readlink "$worktree_runtime_path")"
830
+ if [ "$current_target" = "$RALPH_RUNTIME_DIR" ]; then
831
+ return 0
832
+ fi
833
+ log_warn "Stale runtime symlink in worktree (points to '${current_target}', expected '${RALPH_RUNTIME_DIR}') — relinking"
834
+ rm -f "$worktree_runtime_path"
835
+ fi
836
+
792
837
  if [ -e "$worktree_runtime_path" ] && [ ! -L "$worktree_runtime_path" ]; then
793
838
  rm -rf "$worktree_runtime_path"
794
839
  fi
@@ -796,15 +841,20 @@ ensure_worktree_runtime_link() {
796
841
  ln -s "$RALPH_RUNTIME_DIR" "$worktree_runtime_path"
797
842
  }
798
843
 
844
+ ensure_loop_worktree_ready
845
+ ROOT_DIR="$LOOP_ROOT_DIR"
846
+ cd "$ROOT_DIR"
847
+ CURRENT_BRANCH=$(git branch --show-current 2>/dev/null || echo "")
848
+
799
849
  # Removes a worktree. The branch is kept for potential merge by a later agent.
800
850
  cleanup_epic_worktree() {
801
851
  local epic_id="$1"
802
- git worktree remove "${RALPH_RUNTIME_DIRNAME}/.worktrees/${epic_id}" --force 2>/dev/null || true
852
+ git worktree remove "${WORKTREES_DIR}/${epic_id}" --force 2>/dev/null || true
803
853
  }
804
854
 
805
855
  # Removes ALL .worktrees/* entries (used on EXIT).
806
856
  cleanup_all_worktrees() {
807
- for dir in "${RALPH_RUNTIME_DIRNAME}"/.worktrees/*/; do
857
+ for dir in "${WORKTREES_DIR}"/*/; do
808
858
  [ -d "$dir" ] && git worktree remove "$dir" --force 2>/dev/null || true
809
859
  done
810
860
  }
@@ -912,8 +962,8 @@ CURRENT_STORY_ID=""
912
962
 
913
963
  # Resolve absolute path to PRD file so team lead always has the correct path
914
964
  PRD_ABS_PATH="$(cd "$(dirname "$PRD_FILE")" && pwd)/$(basename "$PRD_FILE")"
915
- if [ "${PRD_ABS_PATH#"$ROOT_DIR"/}" != "$PRD_ABS_PATH" ]; then
916
- PRD_REL_PATH="${PRD_ABS_PATH#"$ROOT_DIR"/}"
965
+ if [ "${PRD_ABS_PATH#"$SOURCE_ROOT_DIR"/}" != "$PRD_ABS_PATH" ]; then
966
+ PRD_REL_PATH="${PRD_ABS_PATH#"$SOURCE_ROOT_DIR"/}"
917
967
  else
918
968
  PRD_REL_PATH="$(basename "$PRD_FILE")"
919
969
  fi
@@ -1305,12 +1355,13 @@ prepare_codex_agent_configs
1305
1355
  LOOP_STARTED_AT=$(date +%s)
1306
1356
 
1307
1357
  # Cleanup worktrees on exit only when NOT interrupted (on interrupt, worktrees are preserved for resume)
1308
- trap 'if [ "$INTERRUPTED" = false ]; then cleanup_all_worktrees; fi; [ -n "${CODEX_AGENT_RUNTIME_DIR:-}" ] && rm -rf "${CODEX_AGENT_RUNTIME_DIR}"; kill $(jobs -p) 2>/dev/null || true' EXIT
1358
+ trap 'if [ "$INTERRUPTED" = false ]; then cd "$SOURCE_ROOT_DIR" 2>/dev/null || true; cleanup_all_worktrees; fi; [ -n "${CODEX_AGENT_RUNTIME_DIR:-}" ] && rm -rf "${CODEX_AGENT_RUNTIME_DIR}"; kill $(jobs -p) 2>/dev/null || true' EXIT
1309
1359
 
1310
1360
  render_team_lead_prompt() {
1311
1361
  TEAM_LEAD_TEMPLATE_PATH="$TEAM_LEAD_PROMPT_FILE" \
1312
1362
  TEAM_LEAD_TEMPLATE_PROJECT="$PROJECT" \
1313
1363
  TEAM_LEAD_TEMPLATE_WORKTREE_ABS_PATH="$WORKTREE_ABS_PATH" \
1364
+ TEAM_LEAD_TEMPLATE_SOURCE_ROOT_DIR="$SOURCE_ROOT_DIR" \
1314
1365
  TEAM_LEAD_TEMPLATE_WORKTREE_STATE_FILE="$WORKTREE_STATE_FILE" \
1315
1366
  TEAM_LEAD_TEMPLATE_WORKTREE_PRD_PATH="$WORKTREE_PRD_PATH" \
1316
1367
  TEAM_LEAD_TEMPLATE_LOOP_BRANCH="$LOOP_BRANCH" \
@@ -1550,7 +1601,7 @@ spawn_epic_bg() {
1550
1601
  local WORKTREE_PATH
1551
1602
  WORKTREE_PATH=$(create_epic_worktree "$EPIC_ID")
1552
1603
  local WORKTREE_ABS_PATH
1553
- WORKTREE_ABS_PATH="$(cd "${ROOT_DIR}/${WORKTREE_PATH}" && pwd)"
1604
+ WORKTREE_ABS_PATH="$WORKTREE_PATH"
1554
1605
  local EPIC_BRANCH
1555
1606
  EPIC_BRANCH=$(epic_branch_name "$EPIC_ID")
1556
1607
  ensure_worktree_runtime_link "$WORKTREE_ABS_PATH"
@@ -1586,7 +1637,11 @@ spawn_epic_bg() {
1586
1637
  ) &
1587
1638
  elif [ "$BACKEND" = "codex" ]; then
1588
1639
  (
1589
- run_codex_exec "$WORKTREE_ABS_PATH" "$TEAM_PROMPT" --add-dir "$ROOT_DIR" --add-dir "$SCRIPT_DIR" > "$EPIC_LOG" 2>&1
1640
+ if [ "$SOURCE_ROOT_DIR" != "$ROOT_DIR" ]; then
1641
+ run_codex_exec "$WORKTREE_ABS_PATH" "$TEAM_PROMPT" --add-dir "$ROOT_DIR" --add-dir "$SOURCE_ROOT_DIR" --add-dir "$SCRIPT_DIR" > "$EPIC_LOG" 2>&1
1642
+ else
1643
+ run_codex_exec "$WORKTREE_ABS_PATH" "$TEAM_PROMPT" --add-dir "$ROOT_DIR" --add-dir "$SCRIPT_DIR" > "$EPIC_LOG" 2>&1
1644
+ fi
1590
1645
  ) &
1591
1646
  else
1592
1647
  (
@@ -1623,6 +1678,8 @@ merge_wave() {
1623
1678
  for epic_id in "${completed_epic_ids[@]}"; do
1624
1679
  local branch_name
1625
1680
  branch_name=$(epic_branch_name "$epic_id")
1681
+ local epic_index
1682
+ epic_index=$(rjq find-index "$PRD_FILE" .epics id "$epic_id")
1626
1683
 
1627
1684
  # Check if branch exists
1628
1685
  if ! git show-ref --verify --quiet "refs/heads/${branch_name}"; then
@@ -1647,15 +1704,18 @@ merge_wave() {
1647
1704
  # be stripped before the merge result is recorded in git history.
1648
1705
  if git merge "${branch_name}" --no-commit --no-ff 2>/dev/null; then
1649
1706
  unstage_runtime_artifacts
1650
- repair_root_runtime_dir_if_needed
1707
+ repair_source_runtime_dir_if_needed
1651
1708
  if [ -f ".git/MERGE_HEAD" ]; then
1652
1709
  git commit --no-edit >/dev/null 2>&1 || true
1653
1710
  fi
1711
+ if [ -n "$epic_index" ]; then
1712
+ rjq set "$PRD_FILE" ".epics[$epic_index].status" '"completed"'
1713
+ fi
1654
1714
  echo " [$epic_id] Merge successful (clean)"
1655
1715
  echo "[$epic_id] MERGED (clean) — $(date)" >> "$PROGRESS_FILE"
1656
1716
  else
1657
1717
  unstage_runtime_artifacts
1658
- repair_root_runtime_dir_if_needed
1718
+ repair_source_runtime_dir_if_needed
1659
1719
  local conflicted_files
1660
1720
  conflicted_files=$(git diff --name-only --diff-filter=U 2>/dev/null || true)
1661
1721
  local conflict_count
@@ -1669,8 +1729,6 @@ merge_wave() {
1669
1729
  [ -n "$merge_error" ] && echo " [$epic_id] Working tree state:" && printf '%s\n' "$merge_error" | sed 's/^/ /'
1670
1730
  merge_failures=$((merge_failures + 1))
1671
1731
 
1672
- local epic_index
1673
- epic_index=$(rjq find-index "$PRD_FILE" .epics id "$epic_id")
1674
1732
  if [ -n "$epic_index" ]; then
1675
1733
  rjq set "$PRD_FILE" ".epics[$epic_index].status" '"merge-failed"'
1676
1734
  fi
@@ -1687,6 +1745,9 @@ merge_wave() {
1687
1745
  git checkout --ours -- prd.json
1688
1746
  git add prd.json
1689
1747
  git commit --no-edit >/dev/null 2>&1 || true
1748
+ if [ -n "$epic_index" ]; then
1749
+ rjq set "$PRD_FILE" ".epics[$epic_index].status" '"completed"'
1750
+ fi
1690
1751
  echo " [$epic_id] Merge successful (kept projected prd.json)"
1691
1752
  echo "[$epic_id] MERGED (projected prd.json) — $(date)" >> "$PROGRESS_FILE"
1692
1753
  git branch -d "${branch_name}" 2>/dev/null || true
@@ -1698,8 +1759,6 @@ merge_wave() {
1698
1759
  echo "[$epic_id] MERGE FAILED (conflicts remain, files: ${conflicted_files}) — $(date)" >> "$PROGRESS_FILE"
1699
1760
  merge_failures=$((merge_failures + 1))
1700
1761
 
1701
- local epic_index
1702
- epic_index=$(rjq find-index "$PRD_FILE" .epics id "$epic_id")
1703
1762
  if [ -n "$epic_index" ]; then
1704
1763
  rjq set "$PRD_FILE" ".epics[$epic_index].status" '"merge-failed"'
1705
1764
  fi
@@ -1797,17 +1856,32 @@ run_backend_agent_session() {
1797
1856
  local prompt_file="${SCRIPT_DIR}/prompts/agents/${agent_name}.md"
1798
1857
  local role_body
1799
1858
  role_body="$(extract_prompt_body "$prompt_file")"
1800
- printf 'Runtime note: This Codex session runs in exec mode. `request_user_input` is unavailable. Never call it. Do not stop to ask the user questions. Make a reasonable assumption and continue.\n\n%s\n\n## Assignment\n%s\n' "$role_body" "$prompt" | codex \
1801
- -a never \
1802
- exec \
1803
- -C "$workdir" \
1804
- -m "$model" \
1805
- -s workspace-write \
1806
- --skip-git-repo-check \
1807
- --color never \
1808
- --add-dir "$ROOT_DIR" \
1809
- --add-dir "$SCRIPT_DIR" \
1810
- - > "$log_file" 2>&1 || true
1859
+ if [ "$SOURCE_ROOT_DIR" != "$ROOT_DIR" ]; then
1860
+ printf 'Runtime note: This Codex session runs in exec mode. `request_user_input` is unavailable. Never call it. Do not stop to ask the user questions. Make a reasonable assumption and continue.\n\n%s\n\n## Assignment\n%s\n' "$role_body" "$prompt" | codex \
1861
+ -a never \
1862
+ exec \
1863
+ -C "$workdir" \
1864
+ -m "$model" \
1865
+ -s workspace-write \
1866
+ --skip-git-repo-check \
1867
+ --color never \
1868
+ --add-dir "$ROOT_DIR" \
1869
+ --add-dir "$SOURCE_ROOT_DIR" \
1870
+ --add-dir "$SCRIPT_DIR" \
1871
+ - > "$log_file" 2>&1 || true
1872
+ else
1873
+ printf 'Runtime note: This Codex session runs in exec mode. `request_user_input` is unavailable. Never call it. Do not stop to ask the user questions. Make a reasonable assumption and continue.\n\n%s\n\n## Assignment\n%s\n' "$role_body" "$prompt" | codex \
1874
+ -a never \
1875
+ exec \
1876
+ -C "$workdir" \
1877
+ -m "$model" \
1878
+ -s workspace-write \
1879
+ --skip-git-repo-check \
1880
+ --color never \
1881
+ --add-dir "$ROOT_DIR" \
1882
+ --add-dir "$SCRIPT_DIR" \
1883
+ - > "$log_file" 2>&1 || true
1884
+ fi
1811
1885
  ;;
1812
1886
  esac
1813
1887
  }
@@ -1905,6 +1979,24 @@ $validation_result_file
1905
1979
 
1906
1980
  }
1907
1981
 
1982
+ persist_loop_branch_state_if_dirty() {
1983
+ local dirty_status=""
1984
+ dirty_status=$(git status --porcelain --untracked-files=no 2>/dev/null || true)
1985
+ [ -n "$dirty_status" ] || return 0
1986
+
1987
+ git add -u
1988
+ unstage_runtime_artifacts
1989
+
1990
+ if git diff --cached --quiet >/dev/null 2>&1; then
1991
+ git reset >/dev/null 2>&1 || true
1992
+ return 0
1993
+ fi
1994
+
1995
+ if git commit -m "chore: persist loop branch state" >/dev/null 2>&1; then
1996
+ echo "Persisted loop branch state on ${LOOP_BRANCH}"
1997
+ fi
1998
+ }
1999
+
1908
2000
  if ! recover_pending_merges "resume/startup"; then
1909
2001
  initialize_counters
1910
2002
  fi
@@ -2309,6 +2401,8 @@ if ! run_final_validation_cycle; then
2309
2401
  FAILED=$((FAILED + 1))
2310
2402
  fi
2311
2403
 
2404
+ persist_loop_branch_state_if_dirty
2405
+
2312
2406
  # --- Summary ---
2313
2407
  REMAINING=$((TOTAL_EPICS - COMPLETED - FAILED))
2314
2408
  echo ""