Skip to content

feat(cli): sequant mutation begin | check | render — mutation verification refuses a dirty tree, requires a real failing run, restores by patch, and is the only producer of SEQUANT_MUTATION markers #1604

Closed

Description

@admarble

Epic: graph-2026-10-ergonomics Tier: judgment Execution: sequant_run Blocked by: #1602

Problem

CLAUDE.md's gate-test rule and /qa §6i ask the agent to mutate, run, restore and then type a <!-- SEQUANT_MUTATION … --> line by hand. That procedure has wiped uncommitted work five times (#817, #906, #939, #1044, #1050; #883 deleted a worktree), tripped 2 of 4 gate PRs on the marker contract (#1104), lost markers to a } (#1305) and to a four-hop PR-body chain (#1316). Nothing today proves the cited test ran: §6i checks only that the named file is in the diff. 33 of 184 memories are some form of "QA claimed it without running it". §4 of the founding doc.

Decisions

ID Decision Rejected alternative
D-2.1 begin --issue N exits 2 on a dirty tree ("commit first"), records HEAD + time in .sequant/mutations/<N>.json tolerating a dirty tree and stashing (the stash is the thing that gets lost)
D-2.2 The agent applies the mutation with its own edit tools a mutation DSL
D-2.3 check --issue N --ac AC-K --test <file> --name "<describe> > <case>" --mutation "<text>", in order, aborting at the first failure with the state file untouched: (1) no commit since begin; (2) npx vitest list <file> -t <escaped name> prints exactly one test; (3) git add -A then git diff --cached --binary captured as the patch (created and deleted files included); (4) npx vitest run <file> -t <escaped name> exits non-zero; (5) git apply -R --index the patch, then git status --porcelain empty; (6) the same run exits 0 without the mutation (a broken test cannot be certified); (7) append {ac, test, name, mutation, mutatedExit, restoredExit, ranAt, sha}. On abort the patch stays at .sequant/mutations/<N>.patch. All paths resolve against the current worktree, never the main checkout reverting with git checkout -- <file> (misses deletions and new files; the historical wipe); accepting any non-zero exit without the restored-pass check
D-2.4 The PR body stays the source of truth (#1316 owner decision, ADR-0004); QA keeps reading $PR_BODY. render --issue N prints the marker lines via renderMutationMarker from the state file, exec pastes them into its ## Summary, and under the orchestrator the PR-body assembly can read the same state file directly. Skills stop typing markers (the swap is #1606). #1316 asked whether to move the channel; this leaves the channel and fixes the origin moving the channel off the PR body (rejected on #1316); keeping hand-typed markers as a fallback (two producers)
D-2.5 Scope stays gate-test ACs; widening is #939 AC-5's backtest widening now

Acceptance Criteria

  • AC-1: begin refuses a dirty tree and records HEAD on a clean one. Evidence: npx vitest run src/commands/mutation.test.ts -t "begin" → ≥ 2 passed, 0 failed
  • AC-2: check records a result only when the named test fails under the mutation and passes after the restore, and restores the tree byte-for-byte including a deleted and a newly added file. Evidence: npx vitest run src/commands/mutation.test.ts -t "check restores" → ≥ 4 passed, 0 failed (test passes under mutation → exit 2, no record; test fails under mutation and passes after restore → one record with mutatedExit != 0 and restoredExit == 0; test fails both ways → exit 2, no record, message names the broken test; a mutation that deletes one file and adds another → git status --porcelain empty after)
  • AC-3: A commit between begin and check aborts the check. Evidence: npx vitest run src/commands/mutation.test.ts -t "commit mid-mutation" → ≥ 1 passed, 0 failed
  • AC-4: -t matching exactly one test is required, checked with vitest list before anything runs. Evidence: npx vitest run src/commands/mutation.test.ts -t "ambiguous name" → ≥ 2 passed, 0 failed (two tests match → exit 2 naming both; zero match → exit 2)
  • AC-5: render output round-trips through parseMutationMarkers, including a mutation text containing } and >. Evidence: npx vitest run src/lib/workflow/mutation-marker.test.ts -t "round-trip from state" → ≥ 2 passed, 0 failed
  • AC-6: Under the orchestrator, exec's PR body carries the markers from the state file without the agent writing them. Evidence: npx vitest run src/lib/workflow/batch-executor.test.ts -t "mutation state" → ≥ 1 passed, 0 failed (a fake state file with two records → two marker lines in the body; empty state → none)
  • AC-7: A reference page documents the three subcommands and the state file. Evidence: grep -cE '^## (begin|check|render)' docs/reference/mutation-verification.md → 3
  • AC-8: No skill-directory edits. Evidence: git diff --name-only $(git merge-base origin/main HEAD)..HEAD | grep -cE '^(plugin/|\.claude/skills/|templates/skills/)' → 0 (constraint)

May touch

src/commands/mutation.ts (new) and test, bin/cli.ts, src/lib/workflow/mutation-marker.ts and test, the PR-body assembly after exec (in batch-executor.ts or the stage module #1507 split it into), docs/reference/mutation-verification.md (new), CHANGELOG.

Non-Goals

Done when

The only way a SEQUANT_MUTATION marker reaches a PR body is through a recorded run that actually failed, from a tree that was clean before and after.

Activity

  1. added
    graph-2026-10-ergonomics
    on Oct 26, 2026
  2. admarble commented on Oct 27, 2026

    @admarble
    ContributorAuthor

    Runner context (2026-10-26, before the W0 launch). For the spec, exec and qa phases.

    • The founding doc is on main: docs/internal/graph-2026-10-ergonomics/PROJECT-DISCUSSION.md (the §N citations in this issue point there) and phase0-evidence.md. Read the cited section before planning; D-rows there are decisions, not suggestions. If implementing shows a D-row is wrong, say so in the PR body rather than quietly deviating.
    • Plugin freeze (C-1): do not edit .claude/skills/**, templates/skills/** or plugin/**. Skill-side changes belong to feat(skills): the human-surface reference — decisions via AskUserQuestion, waiting per mode, completion with a link; skills call sequant comment render and sequant mutation … instead of hand-rendering #1606.
    • Test suite, exec phase and qa phase both: run the full npm test at most once, in the foreground, with timeout 1500, and only after a WIP commit. Prefer targeted npx vitest run <file> while iterating. Each phase has a hard 30-minute wall; the suite takes 10–13 minutes.
    • Gate tests follow the repo CLAUDE.md rule: a mutation-verified record in the PR body, failedTest starting with the test file path. A mutation must still type-check (delete the block, never false &&).

    Issue-specific: §4 D-2.3 fixes the check sequence (clean tree → vitest list resolves exactly one test → git add -A + git diff --cached --binary as the patch → run must fail under the mutation → git apply -R --index → tree clean → run must pass without the mutation → record under .sequant/mutations/<N>.json). D-2.4: the PR body stays the marker's source of truth; only the producer changes. renderMutationMarker / parseMutationMarkers / classifyMutationMarker already live in src/lib/workflow/mutation-marker.ts; sequant mutation render must call the existing renderer, not a second one. If the PR-body assembly turns out to have more than one writer, surface it in the PR body instead of adding a third.

  3. admarble commented on Oct 27, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (8 ACs, 4 directories: src/commands, src/lib/workflow, bin, docs/reference)

    AC Quality Check

    All 8 ACs are one line, each has an Evidence: clause, and the issue has a ## Non-Goals section.

    • Evidence run against base (d6d66a9cf). AC-1–4 name a test file that does not exist yet, so they fail today. AC-7's file does not exist. AC-5 and AC-6 exit 0 today: npx vitest run src/lib/workflow/mutation-marker.test.ts -t "round-trip from state" exits 0 with 26 skipped, because vitest exits 0 when -t matches nothing. Those ACs gate only on the ≥ N passed count, not on the exit code. QA has to read the passed count.
    • AC-8 is a constraint. It passes on base by design.

    Scope Assessment

    Non-Goals: present (no edits to /qa §6i or /exec prose; no full suite run; no widening beyond gate-test ACs).

    • Feature count: 1 (a CLI with three subcommands, plus one consumer change) ✅
    • AC items: 8 ✅
    • Directory spread: 4 ⚠️

    Verdict: ⚠️ SCOPE_WARNING (directory spread). This turns the quality loop on.

    Acceptance Criteria

    • AC-1 begin. Unit test with a real git repo in a tmpdir. Given a dirty tree, when begin runs, then it exits 2 with "commit first" and writes no file. Given a clean tree, it writes {sha: HEAD, startedAt} to .sequant/mutations/<N>.json. Assumption: .sequant/* is gitignored (.gitignore:50), so the state file never makes the tree dirty.
    • AC-2 check restores. Unit test: real git and an injected vitest runner. The 4 cases are listed in the issue. The restore assertion is git status --porcelain empty plus a byte compare of the deleted file.
    • AC-3 commit mid-mutation. Unit test: commit after begin, then check exits 2. The state file is unchanged and the tree is not touched.
    • AC-4 ambiguous name. Unit test: the runner's list returns 2 matches, then 0 matches. Both exit 2 before any run. The message names both matches.
    • AC-5 round-trip from state. Unit test in mutation-marker.test.ts: record → renderMutationMarker → parseMutationMarkers, with } and > in mutation.
    • AC-6 mutation state. Unit test in batch-executor.test.ts: two records → two markers in the body createPR receives; an empty or missing state file → none.
    • AC-7 reference page. Manual (grep). docs/reference/mutation-verification.md with ## begin, ## check, ## render and the state-file schema.
    • AC-8 no skill-directory edits. Constraint (git diff).

    Implementation Plan

    Phase 1: Core module

    1. Add src/lib/workflow/mutation-state.ts. It holds the zod schema for {issue, sha, startedAt, records: [{ac, test, name, mutation, mutatedExit, restoredExit, ranAt, sha}]}, read and write helpers keyed on a cwd (git rev-parse --show-toplevel of the current worktree), and recordToMarker(r) → {ac, mutation, failedTest: \${r.test} > ${r.name}`}. failedTest` starts with the repo-relative test file path, as §6i requires.
    2. check takes a VitestRunner interface (list(file, pattern) returns the matched names; run(file, pattern) returns {exit, testState}). The default runner shells out to npx vitest list --json and npx vitest run --reporter=json. Tests inject a fake. This keeps mutation.test.ts within the 5 s unit-project timeout and keeps real-vitest-inside-vitest out of the suite.

    Phase 2: CLI, src/commands/mutation.ts + bin/cli.ts

    1. begin, check and render are subcommands of program.command("mutation"), the same shape as stateCmd (bin/cli.ts:778). check runs the D-2.3 steps in order. It aborts with exit 2 at the first failure, leaves the state file untouched, and writes the patch to .sequant/mutations/<N>.patch once step 3 has captured it. It also aborts on an empty patch ("no mutation applied").
    2. render --issue N prints one renderMutationMarker(recordToMarker(r)) line per record, latest per AC. It calls the existing renderer; it does not add a second one.

    Phase 3: Orchestrator consumer

    1. In openOrUpdatePR (batch-executor.ts:1355), read <prPath>/.sequant/mutations/<issue>.json and pass the rendered markers to createPR through a new opts.mutationMarkers. composeExecSummary / buildAutomatedPRBody (worktree-manager.ts:2483, :2661) append them after the same key-based dedup they already do. A marker exec pasted from render and the same marker read from the file then appear once.
    2. postReadFirstForPR (batch-executor.ts:3083) reads its markers from exec output. Point it at the same state-file reader.

    Phase 4: Docs

    1. Add docs/reference/mutation-verification.md and a CHANGELOG line. ADR: see Design Review Q4.

    Design Review

    1. Where it belongs. The state and the record-to-marker mapping go in src/lib/workflow/ next to mutation-marker.ts. The git and vitest procedure goes in the command. The PR body keeps the one composer it has today, buildAutomatedPRBody. This step adds a source to it, not a second writer.
    2. Simplest correct approach. Steps 3–6 and 2a as planned. Rejected: putting the PR-body read in the skill (frozen, feat(skills): the human-surface reference — decisions via AskUserQuestion, waiting per mode, completion with a link; skills call sequant comment render and sequant mutation … instead of hand-rendering #1606); a separate mutation-render module (D-2.4 requires the existing renderer).
    3. Pattern and how often it was re-patched. The SEQUANT_MUTATION marker has had repeated fixes: docs(exec/qa): the SEQUANT_MUTATION failedTest contract (<test file> > <name>) is enforced by the classifier but never told to authors — tripped 2 of 4 gate PRs #1104, fix(run): PR body falls back to the placeholder when exec writes no "## Summary" heading, dropping exec's mutation markers and AC table #1297 (PR fix(#1297): PR body falls back to the placeholder when exec writes no "## Summary" heading, dropping exec's mutation markers and AC table #1300), fix(qa): a SEQUANT_MUTATION marker whose text contains "}" parses to nothing — renderMutationMarker and the parser do not round-trip #1305 (PR fix(#1305): a SEQUANT_MUTATION marker containing } now round-trips #1465), investigation(qa): mutation markers travel through a four-hop PR-body chain that has broken four ways — should QA's evidence move to a sequant-owned channel? #1316. That is past the ≥2 threshold. This issue is the class fix (C-3: one producer), so it is not one more local patch. Not a divergence.
    4. What a senior reviewer would challenge. "exits non-zero certifies the wrong failures." If the mutation deletes the test file or breaks its import, vitest exits 1 ("No test files found", or a collection error), and D-2.3 step 4 would certify a test that never ran. Recommendation: step 4 requires the named test's own state to be fail in vitest's JSON report, not just a non-zero exit. Step 6 requires pass. This makes D-2.3 stricter, not looser. Per the runner context, say so in the PR body. An ADR in docs/adr/ (next is 0022) records this and the injected-runner choice.
    5. Every producer and consumer of the marker (whole-repo grep). Producers: (a) agent text in exec output, read by parseMutationMarkersFromExecOutput in composeExecSummary (worktree-manager.ts:2496) and in postReadFirstForPR (batch-executor.ts:3085); (b) new: the state file. Consumers: /qa §6i ($PR_BODY), review-residue (read-first), and the tests worktree-manager.build-pr-body.test.ts and exec-orchestrated-no-pr-skill.test.ts. There are still 2 producers until feat(skills): the human-surface reference — decisions via AskUserQuestion, waiting per mode, completion with a link; skills call sequant comment render and sequant mutation … instead of hand-rendering #1606 (see OQ-1). There is one PR-body writer (createPR, fix(run): sequant run has two PR producers — exec opens the PR itself and createPR returns it untouched, dropping the QA note, ready-gate report and summary #1247), so no third writer is added.

    Feature Quality Planning

    • Data safety: once begin has passed, check never runs git checkout, stash or reset. Restoring uses only git apply -R --index of the captured patch. A gitignored file the mutation creates is not in the patch; the docs page says so.
    • Escaping: vitest -t is a regex. Escape the regex metacharacters in --name. The full name in vitest list --json uses > (checked: mutation-marker > renderMutationMarker round-trip (#1305) > …). Exec must check how -t matches a >-joined name in vitest 5.0.4 and convert the separator if it needs to. That is an assumption to check before writing code.
    • Exit codes: 2 for refusals, 1 for unexpected errors (as in state-exit-code).
    • Mutation tests for the gate tests: AC-1–6 are gate tests, so each needs a SEQUANT_MUTATION record. Dogfood it: exec generates its own records with sequant mutation check (from the worktree's dist/) once the command is built.

    Open Questions

    1. Should hand-typed markers in exec output still reach the PR body in this PR? Recommendation: yes, until feat(skills): the human-surface reference — decisions via AskUserQuestion, waiting per mode, completion with a link; skills call sequant comment render and sequant mutation … instead of hand-rendering #1606. This PR adds the state file as a source and dedupes. feat(skills): the human-surface reference — decisions via AskUserQuestion, waiting per mode, completion with a link; skills call sequant comment render and sequant mutation … instead of hand-rendering #1606 removes the hand-typed path when skills switch over. Impact if wrong: filtering now would drop every gate-test marker from orchestrated runs until feat(skills): the human-surface reference — decisions via AskUserQuestion, waiting per mode, completion with a link; skills call sequant comment render and sequant mutation … instead of hand-rendering #1606 lands, because the frozen skills still tell exec to type them, and §6i would floor those ACs at AC_NOT_MET. The "Done when" line is fully met only after feat(skills): the human-surface reference — decisions via AskUserQuestion, waiting per mode, completion with a link; skills call sequant comment render and sequant mutation … instead of hand-rendering #1606. The PR body says so.
    2. Step 4: test state or exit code? Recommendation: test state (Q4). Impact if wrong: a deleted test file certifies itself.
    3. Where is the state file under the orchestrator? Recommendation: the worktree's .sequant/mutations/, which is the prPath openOrUpdatePR already gets. A QA-only re-run with no worktree finds no file and adds nothing.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: enabled
    Reasoning: The ACs declare unit-test evidence, but their test names are already fixed in the issue, so testgen stubs would only repeat them. testgen is skipped on purpose. The command runs git operations that can lose uncommitted work, and the directory spread is ⚠️, so the quality loop is on.

    Label Review

    Current: graph-2026-10-ergonomics. Recommended: add planned, enhancement. The quality loop is on because of the scope warning.

  4. added
    planned
    on Oct 27, 2026
  5. admarble commented on Oct 27, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    Gaps:

    • Three exports in src/commands/mutation.ts are used only in that file, which turns the health-advisory CI check red
    • If the vitest run throws after the patch is captured, the tree stays mutated and staged and the error does not name the patch path
  6. added 2 commits that reference this issue on Oct 27, 2026
  7. added a commit that references this issue on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    graph-2026-10-ergonomicsplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions