Skip to content

refactor(setup): group steps into four phases ordered by failure probability#1903

Open
DavidMiserak wants to merge 1 commit into
garrytan:mainfrom
DavidMiserak:fix/setup-phase-reorder
Open

refactor(setup): group steps into four phases ordered by failure probability#1903
DavidMiserak wants to merge 1 commit into
garrytan:mainfrom
DavidMiserak:fix/setup-phase-reorder

Conversation

@DavidMiserak
Copy link
Copy Markdown

Closes #1902.

This is the "(3) phase reorder last, against fresh main, with an issue
describing the failure modes it fixes" split @jbetala7 asked for on #1883.

What this PR is

A behavior-preserving reorganization of setup into four phases
bracketed by header comments. See #1902 for the failure modes that
motivate it.

Phase What runs Fail risk
A Filesystem: ~/.gstack/, welcome, GBrain detection (scan) Always succeeds
B Local compute: bun build, skill linking, migrations, GBrain regen Low
C settings.json mutation: team + plan-tune hooks Medium, reversible
D External network / sudo / package managers: coreutils, font, Playwright Highest

Each phase header is a comment block in the script. Steps inside are
renumbered (A1, A2, A3, B1, B2a, ...).

What this PR is NOT

To keep this reviewable as a structural move, the two behavior changes
that were originally bundled with the reorder on #1883 are not here:

In this PR's diff:

This is intentional. A reviewer can read this diff as "did anything
besides moving lines around and splitting one block?" The answer should
be: just the GBrain detect/regen split (entailed by the new order) and
one safety guard for that split (see next section).

The one behavior change this PR DOES contain

The phase reorder requires splitting GBrain detection from GBrain regen:

  • A3 runs detection (a filesystem scan via gstack-gbrain-detect,
    no dependencies). Writes the result to ~/.gstack/gbrain-detection.json.
  • B6 reads that file and decides whether to regenerate Claude
    SKILL.md with brain-aware blocks (after the binary is built + skill
    docs are generated).

This split is necessary because A runs before bun is prepared, so the
regen call (which needs bun) can't live in A. Splitting introduces one
new failure mode: what happens if detection fails in A3 but a stale
gbrain-detection.json from a prior successful run still exists?
Without a guard, the stale "ok" file would survive a failed detect and
silently trigger brain-aware regen in B6 — applying ~250 tokens of
overhead per planning skill to a user whose gbrain may have been
removed.

The guard: A3 removes both the temp file AND any pre-existing
gbrain-detection.json when gstack-gbrain-detect exits non-zero.

if "\$DETECT_BIN" > "\$DETECTION_FILE.tmp" 2>/dev/null; then
  mv "\$DETECTION_FILE.tmp" "\$DETECTION_FILE"
else
  rm -f "\$DETECTION_FILE.tmp" "\$DETECTION_FILE"    # <-- the guard
  log "  warning: gstack-gbrain-detect failed — brain-aware blocks will stay suppressed"
fi

test/setup-gbrain-detection.test.ts pins this with a static-source
check so a future refactor that drops the guard fails CI.

Tests

$ bun test test/setup-*.test.ts
 66 pass / 0 fail / 152 expect() calls

All 7 existing setup test files still green on the reordered structure.

Test plan

Relationship to #1883

This PR is the third and final split from #1883. Once this is open,
#1883 stays as the parking lot only long enough to add the three split
links and a close note.

Sibling PRs:

Recommended merge order

  1. fix(setup): route gen:skill-docs through bun_cmd; drop tail -3 pipe that masks bun failures #1898 first (tiny, independent).
  2. fix(setup): pin Playwright browser install to bundled version so headed browse connect works (#1829) #1838 + fix(setup): Playwright Chromium failure becomes best-effort warn, not exit 1 #1900 together (both touch the Playwright block;
    either can land first then the other rebases).
  3. This PR last, rebased after the above land so the diff stays
    small and reviewable.

If preferred, this PR can land before #1898 / #1900 — they're against
fresh main with no overlap, but rebasing them on top of this would make
their diffs harder to read because they'd be against the reordered
structure. The chosen order (this PR last) keeps each diff minimal
against the structure its reviewer already knows.

…ability

setup used to interleave trivial filesystem ops, expensive compute,
settings.json mutations, and network/sudo steps in whatever order the
script grew. A Playwright Chromium download failing at step 2 would
abort before skills got registered at step 4, hooks landed at step 10,
or migrations ran at step 8.

This commit groups work by where failures actually come from:

  Phase A — filesystem (always succeeds): ~/.gstack/, welcome, GBrain detection
  Phase B — local compute (low fail): bun build, skill linking, migrations, regen
  Phase C — settings.json mutation (medium fail, reversible): team-mode + plan-tune hooks
  Phase D — external network/sudo/package managers (highest fail): coreutils, font, Playwright

Each phase is bracketed with a header comment; steps within are
renumbered (A1, A2, B1, B2a...).

The biggest behavior shift is the GBrain detect/regen split that the
new order requires: detection moves to A3 (filesystem scan only, no
dependencies), regen moves to B6 (runs after the binary is built and
skill docs are generated). Detection persists to
~/.gstack/gbrain-detection.json so B6 can read it.

A safety guard for that split: when gstack-gbrain-detect exits non-zero,
A3 removes both the temp file AND any pre-existing detection file from a
prior successful run. Without the guard, a stale "ok" detection file
would survive a failed detect and silently apply ~250 tokens of overhead
per planning skill to a user whose gbrain may no longer be installed.

Behavior preservation:
- bun_cmd routing in link_{codex,factory,opencode}_skill_dirs is
  unchanged (still buggy here). The fix is in garrytan#1898.
- gen:skill-docs:user still pipes through `tail -3` in B6 (still buggy
  here). The fix is in garrytan#1898.
- Playwright Chromium install/verify in D3 still exits 1 on failure
  (unchanged). The warn-don't-exit conversion is in garrytan#1900 and
  coordinates with garrytan#1838.

Splitting those keeps this PR reviewable as "a move plus the gbrain
split that the move requires," per @jbetala7's request on garrytan#1883.
@trunk-io
Copy link
Copy Markdown

trunk-io Bot commented Jun 7, 2026

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

setup: Playwright Chromium download failure aborts setup before skills/hooks/migrations land

1 participant