Plan and execute a commit stack. Determines mode automatically based on input:
The user describes work to be done. Build the stack from scratch.
-
Understand the task from $ARGUMENTS. If the description is vague, ask
clarifying questions.
-
Design the commit stack following the ordering in
references/philosophy.md:
- Refactoring / cleanup (separate from feature work, always first)
- Dependencies / build config changes
- Scaffolding / types / interfaces / schemas
- Core business logic
- Edge cases / error handling
- Tests
For each commit, note:
- One-sentence commit message (Conventional Commits format)
- Which files will be created or modified
- Estimated line count (target 50-200 per commit)
- Dependencies on prior commits (no forward references)
-
Verify no forward references: each commit must only reference files,
imports, and config that exist in it or earlier commits. Documentation goes
WITH the feature it documents. Dependencies arrive in the commit that first
uses them.
-
Present the plan to the user:
Proposed stack (N commits):
1. type(scope): description — files X, Y (~80 lines)
2. type(scope): description — files A, B (~120 lines)
3. type(scope): description — files C, D, E (~150 lines)
...
Wait for user approval. Adjust if they have feedback.
-
Execute the plan — implement each commit in order:
git add <files>
git commit -m "type(scope): description"
git add <files>
git commit -m "type(scope): description"
After each commit, verify the codebase is in a working state.
Uncommitted changes exist. Classify and commit them as a stack.
Existing commits need to be reorganized into a clean atomic stack.
-
Parse the range from $ARGUMENTS. Resolve to a <base> and <tip>:
BASE=<resolved base>
TIP=<resolved tip>
BASE=$(git merge-base main <branch>)
TIP=<branch>
BASE=<empty tree>
TIP=HEAD
-
Understand the current state:
git sl
git log --oneline --reverse $BASE..$TIP
git log --reverse --stat --format="=== %h %s ===" $BASE..$TIP
git diff --stat $BASE..$TIP
git log --oneline --reverse --root
git log --reverse --stat --format="=== %h %s ===" --root
git diff --stat $(git hash-object -t tree /dev/null) HEAD
Read the full diff and per-commit stats. Count total lines changed.
-
Classify every change into logical groups (same as Working Tree mode
step 2).
-
Identify files with intermediate states: before planning the commit
sequence, list every file that will have DIFFERENT content across multiple
commits (e.g. README.md, CLAUDE.md, flake.nix growing incrementally).
For each, note what content exists at each commit boundary. This prevents
accidentally committing the final version too early and having to reset.
-
Present the plan (same format as Plan mode step 4).
Wait for user approval.
-
Check for uncommitted work before flattening:
git status --short
If there are uncommitted changes, warn the user and ask whether to stash
or commit them first.
-
Create a backup branch:
git branch backup-before-restructure
If a branch with this name already exists, delete it first or use a unique
name (e.g., backup-restructure-$(date +%s)).
-
Save the tree hash for post-verification:
FINAL_TREE=$(git rev-parse HEAD^{tree})
-
Flatten the range into the working tree:
git reset --soft $BASE
git restore --staged .
git checkout --orphan restructure-wip
git reset
Verify the working tree matches the original diff:
git diff --stat
git status --short | wc -l
-
Commit each group in plan order (same as Working Tree mode step 4).
For files with intermediate states (identified in step 4), use the Write
or Edit tool to set the correct content BEFORE staging for each commit.
Do not rely on the final working tree content — it represents the end
state, not intermediate states.
-
Move branch pointer (from-root only):
git checkout -B main
-
Verify the result:
git sl
git log --oneline --reverse
Show the user the new stack.
-
Confirm total diff is identical (Restructure mode only — $BASE is
only defined in Restructure mode, not Working Tree mode):
git diff $BASE..HEAD --stat
test "$(git rev-parse HEAD^{tree})" = "$FINAL_TREE" && echo "trees match"
git diff backup-before-restructure HEAD --stat
-
Clean up stale artifacts: check for self-referencing symlinks or
other working tree debris:
git status --short
Remove any untracked artifacts that weren't in the original tree.
-
Run tests if a test command is identifiable:
git test run -x '<test-command>' 'stack()'
Report any commits that break the build.