|
| 1 | +## Goal |
| 2 | + |
| 3 | +Improve the `pre-bash` hook's redirect-path checker so that simple literal shell variable |
| 4 | +assignments defined earlier in the same script block are expanded before the worktree-isolation |
| 5 | +check runs. |
| 6 | + |
| 7 | +**Current behaviour:** Any redirect (`>`, `>>`, `2>`) whose target contains a `${VAR}` or `$VAR` |
| 8 | +reference is unconditionally rejected with: |
| 9 | + |
| 10 | +``` |
| 11 | +WARNING: Cannot verify Bash redirect to variable-expanded path: "${VAR}" |
| 12 | +One or more variables in the path are unset in the hook process environment. |
| 13 | +``` |
| 14 | + |
| 15 | +This fires even when the variable is clearly defined as a literal path in the same script, e.g.: |
| 16 | + |
| 17 | +```bash |
| 18 | +OUT="/workspace/.cat/work/worktrees/my-issue/.cat/work/out.txt" |
| 19 | +some-command > "${OUT}" # ← blocked despite OUT being a known literal |
| 20 | +``` |
| 21 | + |
| 22 | +**Desired behaviour:** Before rejecting, the hook performs a lightweight static scan of the |
| 23 | +script text for assignments of the form `VAR="<literal>"` or `VAR='<literal>'` that precede the |
| 24 | +redirect. If the variable resolves to a literal path, the hook substitutes it and continues with |
| 25 | +the normal worktree-isolation check on the resolved path. |
| 26 | + |
| 27 | +Paths that contain command substitutions (`$(...)`, `` `...` ``), arithmetic expansions, or other |
| 28 | +non-literal forms remain rejected as before — they cannot be safely evaluated statically. |
| 29 | + |
| 30 | +## Scope |
| 31 | + |
| 32 | +- `BlockWorktreeIsolationViolation.java` — the Java class that implements the pre-bash hook logic |
| 33 | +- Unit tests in `BlockWorktreeIsolationViolationTest.java` |
| 34 | + |
| 35 | +## Post-conditions |
| 36 | + |
| 37 | +- [ ] A redirect to `> "${VAR}"` where `VAR="/workspace/.cat/work/worktrees/my-issue/file.txt"` is |
| 38 | + defined earlier in the same script is allowed (path is inside the active worktree) |
| 39 | +- [ ] A redirect to `> "${VAR}"` where `VAR` is genuinely undefined remains blocked |
| 40 | +- [ ] A redirect to `> "${VAR}"` where `VAR` is set via command substitution (`VAR=$(mktemp)`) |
| 41 | + remains blocked (cannot statically resolve) |
| 42 | +- [ ] A redirect to `> "${VAR}"` where `VAR` resolves to a path outside the active worktree is |
| 43 | + blocked with the existing worktree-isolation violation message |
| 44 | +- [ ] All existing tests pass (`mvn -f client/pom.xml verify -e`) |
| 45 | +- [ ] New unit tests cover the three new cases above |
0 commit comments