Skip to content

test: state.test.ts idempotency and state-utils discoverUntrackedWorktrees cases fail under full-suite load, pass alone #1322

Closed

Description

@admarble

Epic: graph-2026-10-freeze Tier: judgment Execution: split: sequant_run writes the fix; the runner session produces the full-suite evidence (AC-1, AC-4), since phases skip the full suite

Seen in the full npm test run for #1292 (branch at 8e5d95a, base origin/main 3cbb5a2). Neither file is touched by that diff.

Failing in the full run:

  • src/commands/state.test.ts > state command > idempotency (AC-4) > state init should be idempotent - running twice yields same result
  • src/lib/workflow/state-utils.test.ts > state-utils > discoverUntrackedWorktrees > should not discover worktrees that are already tracked

scripts/settle-against-base.sh for each: PASS at HEAD and PASS at base when run alone. Alone they take 14.8 s and 24.4 s. state.test.ts's describe block has a 30 s timeout. Load-induced timeouts are the likely cause, but this is not confirmed: the full-run failure messages were not captured.

Next step: re-run the full suite and capture the failure output to tell a timeout from shared-state interference (compare #1164).

Acceptance Criteria

  • AC-1: The full-suite failure output for both tests is captured and the cause is classified as load-induced timeout or shared-state interference (test(mcp): sequant_status tests reconcile and rewrite the checkout's real .sequant/state.json #1164: three MCP tests touch the real .sequant/state.json). Evidence: the captured failure messages pasted in a comment on this issue, with the one-line classification.
  • AC-2: If the cause is shared state, both tests run against an isolated state path and no longer read or write the checkout's real .sequant/state.json; otherwise this AC records n/a with the AC-1 classification. Evidence: a test or setup change that points the state path at a temp dir, or the n/a line.
  • AC-3: If the cause is load, the tests' timeouts or setup cost change so they pass under full-suite load; otherwise this AC records n/a with the AC-1 classification. Evidence: the timeout or setup diff, or the n/a line.
  • AC-4: Both tests pass in 3 consecutive full-suite runs on the PR branch. Evidence: the three run summaries (Tests … passed) pasted in the PR body.

Prior fixes / producers

May touch

src/commands/state.test.ts, src/lib/workflow/state-utils.test.ts, test setup files.

Non-Goals

  • Changing state-file behaviour in production code, unless AC-1 shows a production bug (then file it separately).
  • Any edit under plugin/, .claude/skills/ or templates/skills/ (plugin freeze, I-1 of the graph doc).

Activity

  1. added
    bug
    graph-2026-10-freeze
    on Oct 5, 2026
  2. added a commit that references this issue on Oct 10, 2026
  3. admarble commented on Oct 10, 2026

    @admarble
    ContributorAuthor

    Superseded: this classification was wrong; see the correction below (shared state, not load).

    AC-1 evidence (runner, 2026-10-10): classified as a load-induced timeout

    I ran two full suites on origin/main @ c8762a2c in a detached worktree, with SEQUANT_* env vars unset (npx vitest run --project unit --project integration, JSON reporter):

    Test Alone (this issue) Run 1: quiet machine Run 2: two sequant run agents running alongside Describe timeout
    state-utils.test.ts > discoverUntrackedWorktrees > should not discover worktrees that are already tracked 24.4 s 19.8 s, pass 29.775 s, pass 30 s
    state.test.ts > idempotency (AC-4) > state init should be idempotent 14.8 s 10.4 s, pass 16.1 s, pass 30 s

    Classification: load-induced timeout.

    • Neither run reproduced the failure, so there is no failure message to paste. The durations carry the evidence instead.
    • Each test's time scales with machine load: the first one went from 19.8 s to 29.8 s, and passed with 225 ms to spare under two concurrent agents.
    • A shared-state failure would show up as an assertion message at normal speed, not as a run time that tracks load.
    • Both runs also kept these tests apart from the test(mcp): sequant_status tests reconcile and rewrite the checkout's real .sequant/state.json #1164 tests that touch the real .sequant/state.json. The suites ran in a separate worktree, and the tests still passed.

    For the fix: AC-2 is n/a and AC-3 applies. A single test that takes 20–30 s points to setup cost (spawned git/CLI processes per case), not a test that needs more time. Cutting that cost is better than raising the timeout alone.

    Unrelated local failures in the same runs, not this issue's: driver-conformance.test.ts > codex commit (git exit 128; the local codex sandbox) and semgrep.test.ts timeouts (local semgrep under load). CI on main is green for both.

  4. admarble commented on Oct 10, 2026

    @admarble
    ContributorAuthor

    Runner context for the phase agents (graph-2026-10-freeze, wave 1). This applies to spec, exec and qa by name.

    • AC-1 is done by the runner: see the comment above. The classification is a load-induced timeout, so AC-2 is n/a and the fix is AC-3. Don't re-derive the classification. Record n/a for AC-2 citing that comment.
    • For AC-3, first measure where each test's 10–30 s goes: per-case git init, spawned CLI processes, npm calls. Cut that setup cost (a shared beforeAll fixture, an in-process call instead of a spawn) rather than only raising the 30 s timeout. If you do raise a timeout as well, say why.
    • No full npm test in exec or qa. Run the two files alone, in the foreground, with timeout, after a WIP commit, and report each file's per-test durations before and after. The runner produces AC-4's three full-suite runs on the PR branch after the PR opens.
    • AC-4 stays unchecked in the PR until the runner pastes the three run summaries.
    • Plugin freeze (graph doc I-1): no edits under plugin/, .claude/skills/ or templates/skills/.
  5. admarble commented on Oct 10, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (4 ACs, 2 directories)

    AC Quality Check

    lintAcceptanceCriteria: all 4 ACs pass. Every AC is on one line and has an Evidence: clause.

    • AC-3's evidence ("the timeout or setup diff") is also met by a diff that only raises the timeout. The runner comment says to cut setup cost instead. Derived AC-5 below adds a bar you can measure.

    Scope Assessment

    Non-Goals: present. No production state-file change. No edits under plugin/, .claude/skills/ or templates/skills/.

    Metric Value Status
    Feature count 1 ✅
    AC items 4 (+1 derived) ✅
    Directory spread 2 (src/commands/, src/lib/workflow/) ✅

    Verdict: ✅ SCOPE_OK


    Root cause (measured from the code, not guessed)

    discoverUntrackedWorktrees() (src/lib/workflow/worktree-discovery.ts:210) takes no cwd and no injected dependencies. In both tests it does three things against the real developer checkout:

    1. git worktree list --porcelain (:122). This checkout has 25 worktrees, and 22 of them are feature/* branches with issue numbers.
    2. For each untracked issue worktree it calls fetchIssueTitle, which calls GitHubProvider.fetchIssueTitleSync. That is a synchronous gh issue view network call with a 10 s timeout each (platforms/github.ts:510).
    3. inferPhaseFromLogs (:163) reads and Zod-parses every file in the real .sequant/logs: 97 files, 2 MB, once per discovered worktree.

    Each failing test calls discovery twice, so it makes about 2 × 22 sequential gh calls. That accounts for the 10–30 s run times and for times that grow with load. It also means:

    • The runtime depends on the machine. In CI there is a single worktree, so discovered.length === 0, the if branch never runs, and both tests assert nothing (vacuous on CI). They only exercise real logic on a developer machine, and that is where they time out.
    • AC-1's classification (load) is correct as filed. The reason for the load sensitivity is the per-worktree network and log I/O on real checkout state.

    Acceptance Criteria

    AC-1: Capture + classify

    Verification: Manual (done)
    Status: Done by the runner: classification comment. Load-induced timeout. Don't re-derive it.

    AC-2: Isolated state path (if shared state)

    Verification: Manual
    Status: Record n/a — AC-1 classified as load-induced timeout (runner comment, 2026-10-10). Both tests already use a mkdtemp statePath. The fix below also removes the reads of the real git worktree list and .sequant/logs, which goes further than AC-2 asked.

    AC-3: Cut setup cost so the tests pass under load

    Verification: Integration Test (the two files run alone, per-test durations before/after)
    Scenario: Given a fixture worktree list with N issue branches → When discovery runs twice with one issue tracked in between → Then the assertions hold, with no gh spawn and no real git worktree list.
    Assumptions: child_process can be mocked by the args each call receives (state-utils.test.ts already does this for spawnSync).

    AC-4: 3 consecutive full-suite passes

    Verification: Integration Test, runner-owned. It stays unchecked in the PR until the runner pastes the three Tests … passed summaries. Exec and qa do not run the full suite.


    Implementation Plan

    Phase 1: Measure (before)

    1. WIP commit. Then, in the foreground, run timeout 300 npx vitest run src/commands/state.test.ts src/lib/workflow/state-utils.test.ts --reporter=verbose and record the per-test durations. The PR body needs these as the "before" numbers.

    Phase 2: Hermetic fixture (recommended option A)

    1. Add a small shared helper, e.g. src/lib/workflow/__test-helpers__/worktree-discovery-fixture.ts (follow whatever test-helper location the repo already uses). It installs a spawnSync mockImplementation keyed on command + args, not an ordered queue (see the ordered-mock pitfall). It returns:
      • for git worktree list --porcelain: a fixture porcelain string with the main worktree, a detached HEAD, a non-issue branch and at least 2 feature/<N>-… branches. Return a Buffer stdout, because getWorktreeDetails calls .toString() and passes no encoding.
      • for gh issue view <N> --json title: a canned title string, status: 0.
      • for anything else: pass through to the real spawnSync.
    2. state-utils.test.ts: in the discoverUntrackedWorktrees describe (:287), install the fixture in beforeEach. Remove the if (discovered.length > 0) … else fallback, so the test asserts that discovery found 2 or more worktrees, that tracking one makes discovered shrink by exactly 1, and that alreadyTracked === 1. Note that mockSpawnSync is already vi.mocked in this file. Reset it in afterEach so the fixture doesn't leak into checkPRMergeStatus and the other describes.
    3. state.test.ts: it has no child_process mock today. Add the same vi.mock("child_process", …) pass-through and install the fixture for the idempotency and clear output describes. Apply the same tightening: drop the if, and assert result2.discovered.length === 0 and alreadyTracked === result1.discovered.length, with result1.discovered.length >= 2.
    4. Keep the 30 s describe timeouts only for describes that still do real I/O. Where they're no longer needed, drop them to the 5 s default. If any stay, say why in the PR.

    Phase 3: Measure (after)

    1. Run the same command as step 1 again and record the "after" durations. Target: each discovery test < 1 s alone (derived AC-5).

    Rejected options

    • B: only raise the timeout to 60 s. The cost keeps growing with the number of worktrees and with network latency, and the tests stay vacuous on CI. The runner comment rules this out as the only fix.
    • C: add a cwd/deps injection parameter to discoverUntrackedWorktrees. This is a cleaner seam, but it changes production code (Non-Goal) for a test-only problem. Mocking child_process matches the existing pattern in state-utils.test.ts.

    Design Review

    1. Where does this belong? Only in the test layer: the two test files plus one shared fixture helper. Production code is not touched.
    2. Simplest correct approach? Mock spawnSync keyed on args for git worktree list and gh issue view. Rejected: timeout-only (B) and production DI (C), both above.
    3. Pattern / re-patch count. The pattern is the args-keyed mockGit already in state-utils.test.ts:886. git log -S "timeout: 30000" on these files: one prior change, 0776efe3 ("stabilize flaky tests with project-based isolation"). That commit raised the timeout and left the real-checkout dependency in place. This is the second patch to this flake. One prior fix is under the stop threshold of 2, but this plan fixes the class (real checkout I/O) instead of the symptom, so there will not be a third timeout bump.
    4. What a senior reviewer would challenge. "Doesn't the fixture stop you from testing the real git worktree list parsing?" The parsing still runs: the mock returns a realistic porcelain string, and getWorktreeDetails parses it. Only the process spawn is faked. "inferPhaseFromLogs still reads the real .sequant/logs." Yes. With 2–3 fixture worktrees that is about 300 file parses. If the after-measurement shows that cost matters, see Open Question 1.
    5. Producers/consumers (whole-repo grep). discoverUntrackedWorktrees is defined in worktree-discovery.ts and re-exported from state-utils.ts:41. Its consumers are src/commands/state.ts (production, unchanged), state.test.ts, state-utils.test.ts and state-exit-code.test.ts. The last one already mocks the function completely, so it is unaffected. There is one producer, so no parity test is needed.

    Sibling sites (do these in scope, same files): state.test.ts:155 ("clear output" discovery test) and state-utils.test.ts:292 ("should return success when called") make the same real calls, once each. They should use the same fixture. It's the same describe and the same beforeEach, so no scope expansion is needed.


    Feature Quality Planning

    All standard checks pass. Notable items:

    • Vacuous on CI: today the conditional assertions skip on any checkout with no issue worktrees. The fixture makes them always run. Mutation-verify that (see Derived AC-6).
    • Mock leakage: state-utils.test.ts shares one mockSpawnSync across describes. Reset it in afterEach of the discovery describe.

    Derived ACs

    Source Derived AC Priority
    Performance (AC-3 bar) AC-5: each discoverUntrackedWorktrees test in both files takes < 1 s when its file runs alone. Evidence: the before/after per-test durations in the PR body. High
    Test validity AC-6: the two named tests fail when discovery's already-tracked check is broken. Evidence: a SEQUANT_MUTATION record in the PR body. Mutation: in worktree-discovery.ts, comment out the trackedIssues.has(issueNumber) continue; src/lib/workflow/state-utils.test.ts > state-utils > discoverUntrackedWorktrees > should not discover worktrees that are already tracked fails; restore. High

    Open Questions

    1. Stub inferPhaseFromLogs's real .sequant/logs read too? Recommendation: no, unless the after-measurement is still over 1 s. It is a read with no side effects and costs little at fixture scale. Impact if wrong: a smaller remaining load sensitivity. The fix then is vi.mock of run-log-schema.js's LOG_PATHS pointing at the temp dir.
    2. Mock child_process or GitHubProvider.prototype.fetchIssueTitleSync? Recommendation: child_process, because one mock covers both the git and the gh calls and matches the file's existing pattern. Impact if wrong: none functionally.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: A test-only bug fix in two files. The runner already did AC-1, AC-2 is n/a, and AC-4 is runner-owned. No testgen, because the work is tests. Exec and qa run only the two files, in the foreground, with timeout, after a WIP commit. No full npm test.


    Label Review

    Current: bug, graph-2026-10-freeze
    Recommended: add planned
    Quality Loop: won't auto-enable (SCOPE_OK, no complexity labels)

  6. added
    planned
    on Oct 10, 2026
  7. admarble commented on Oct 10, 2026

    @admarble
    ContributorAuthor

    QA Verdict: NEEDS_VERIFICATION

    AC coverage: 4/6 met

    Gaps:

    • AC-1 load classification conflicts with exec's assertion-failure evidence; fix covers both causes — dropped: fix is cause-agnostic
    • AC-1's classification conflicts with exec's own evidence (above). The fix covers both causes, so I record the conflict here and don't block on it.
    • No verbatim motivating-example fixtures were needed; the issue cites test names only.
    • No Non-Goal was crossed: production code is untouched.

    Suggestions:

    • inferPhaseFromLogs still reads the real .sequant/logs. At fixture scale this costs a few milliseconds, and the spec's Open Question 1 accepts it. A future log entry for a fixture issue number (9001–9003) could change the inferred phase, but no test asserts on the phase.

    Pending ACs:

    • AC-4

    Needs human review:

    • Required CI test check still pending
    • Three consecutive full-suite run summaries not yet pasted
  8. admarble commented on Oct 10, 2026

    @admarble
    ContributorAuthor

    Correction to AC-1: the cause is shared state, not load. My classification above was wrong.

    The exec phase (PR #1395) found the mechanism, and I checked it against the code. On main, both tests call discoverUntrackedWorktrees against the real checkout's git worktree list --porcelain (worktree-discovery.ts:122), then run one gh issue view per issue worktree.

    • Run time grows with how many worktrees the developer's checkout has (25 here) and with network latency, not with CPU load. My second suite run coincided with sequant run creating new worktrees, which explains the 19.8 s → 29.8 s jump that I read as load.
    • Assertion failures happen when the real worktree list changes during the test: a concurrent sequant run adds or removes a worktree between the test's two discovery calls. Exec's "before" run failed on assertions at state.test.ts:58 and state-utils.test.ts:320 (PR test(#1322): state.test.ts idempotency and state-utils discoverUntrackedWorktrees cases fail under full-suite load, pass alone #1395 body).
    • CI never fails because a CI checkout has no issue worktrees. The if (discovered.length > 0) branches never ran, so the tests asserted nothing there.

    So the classification is shared-state interference with the machine's worktree list and GitHub. It is not the #1164 .sequant/state.json path: both tests already used a temp statePath.

  9. added a commit that references this issue on Oct 10, 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

    buggraph-2026-10-freezeplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions