Skip to content

Add state bootstrapping for existing worktrees #117

Closed

Description

@admarble

Summary

Provide a way to populate .sequant/state.json for worktrees that already exist but have no state tracking.

Problem

When state tracking is introduced, existing worktrees won't have entries in the state file. Need a way to:

  1. Detect existing worktrees
  2. Infer their current phase from logs or git state
  3. Populate state file with reasonable defaults

Proposed Commands

sequant state init
# → Scans for worktrees, creates state entries for any not tracked

sequant state rebuild
# → Rebuilds entire state file from git worktrees + logs
# → Use when state is corrupted or out of sync

sequant state clean
# → Removes entries for worktrees that no longer exist

Detection Logic

  1. Run git worktree list to find all worktrees
  2. For each worktree with issue-* or feature/* pattern:
    • Extract issue number from branch name
    • Fetch issue title from GitHub
    • Check .sequant/logs/ for any phase history
    • Infer current phase (last completed + 1, or not_started)
  3. Write to state file

Edge Cases

  • Worktree exists but issue was closed/deleted
  • Multiple worktrees for same issue (shouldn't happen, but handle gracefully)
  • Logs exist but worktree was deleted
  • No logs, no worktree, but state entry exists (stale)

Acceptance Criteria

  • sequant state init populates state for untracked worktrees
  • sequant state rebuild recreates state from scratch
  • sequant state clean removes orphaned entries
  • Commands are idempotent (safe to run multiple times)
  • Clear output showing what was added/removed/updated

Dependencies

Activity

  1. added
    state
    enhancement
    on Jan 18, 2026
  2. admarble commented on Jan 19, 2026

    @admarble
    ContributorAuthor

    Worktree Setup

    Feature worktree already created from feature/dashboard branch:

    # Worktree location
    ../worktrees/feature/117-bootstrap-state-from-logs/
    
    # Branch
    feature/117-bootstrap-state-from-logs (based on feature/dashboard)

    This is part of the dashboard feature integration - all dashboard work (#114, #116, #117, #118, #119, #120) branches from feature/dashboard rather than main.

    Note: sequant run doesn't support custom base branches yet (see #122), so this worktree was created manually.

  3. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    📋 Implementation Plan for State Bootstrapping

    Acceptance Criteria with Verification

    AC Description Verification Status
    AC-1 sequant state init populates state for untracked worktrees Integration Test ⬜
    AC-2 sequant state rebuild recreates state from scratch Integration Test ⬜
    AC-3 sequant state clean removes orphaned entries Unit + Integration Test ⬜
    AC-4 Commands are idempotent (safe to run multiple times) Unit Test ⬜
    AC-5 Clear output showing what was added/removed/updated Manual Test ⬜

    Implementation Phases

    Phase 1: CLI Infrastructure

    • Create /src/commands/state.ts with subcommand routing
    • Register sequant state in CLI with init, rebuild, clean subcommands

    Phase 2: Worktree Discovery & State Init

    • Add discoverUntrackedWorktrees() to state-utils.ts
    • Add fetchIssueTitle() helper using gh CLI
    • Implement state init command

    Phase 3: Rebuild & Clean Enhancement

    • Enhance rebuildStateFromLogs() to include worktree scanning
    • Implement state rebuild with backup option
    • Implement state clean wrapper

    Phase 4: Quality

    • Add unit tests for new state-utils functions
    • Add integration tests for state commands

    Key Assumptions

    Open Decisions

    1. Backward compat: Keep sequant status --rebuild as alias → Recommend: Yes
    2. Missing GitHub access: Use placeholder title → Recommend: Proceed with "(title unavailable)"
    3. Rebuild confirmation: Require --force → Recommend: Yes

    Effort Breakdown

    Phase Scope
    Phase 1 CLI scaffolding
    Phase 2 Core worktree discovery logic
    Phase 3 Integration with existing utilities
    Phase 4 Test coverage

    Feature Branch

    ⚠️ Important: This issue is part of the dashboard feature set. Use:

    npx sequant run 117 --base feature/dashboard

    🤖 Generated by /spec workflow

  4. added
    planned
    cli
    on Jan 20, 2026
  5. added a commit that references this issue on Jan 20, 2026
  6. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    Implementation Complete

    AC Coverage

    AC Description Status
    AC-1 sequant state init populates state for untracked worktrees ✅ Done
    AC-2 sequant state rebuild recreates state from scratch ✅ Done
    AC-3 sequant state clean removes orphaned entries ✅ Done
    AC-4 Commands are idempotent (safe to run multiple times) ✅ Done
    AC-5 Clear output showing what was added/removed/updated ✅ Done

    Key Files Changed

    • src/commands/state.ts - New CLI command with subcommand routing
    • src/lib/workflow/state-utils.ts - Add discoverUntrackedWorktrees() function
    • bin/cli.ts - Register the new state command and subcommands
    • src/commands/state.test.ts - Tests for idempotency and output structure
    • src/lib/workflow/state-utils.test.ts - Tests for worktree discovery

    Test Results

    • npm test: 444 passing (2 unrelated failures in doctor tests due to version check)
    • npm run build: ✅ Passes

    PR

    Created: #134

    Notes

    The implementation supports:

    • Branch patterns: feature/<N>-*, issue-<N>, <N>-*
    • Automatic title fetch from GitHub via gh CLI (with fallback placeholder)
    • Phase inference from existing run logs
    • JSON output mode for scripting (--json flag)
    • Dry-run mode for clean command (--dry-run flag)
    • Force flag for rebuild to prevent accidental state loss (--force flag)

    🤖 Generated by /exec workflow

  7. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    QA Review for Issue #117

    AC Coverage

    AC Description Status Notes
    AC-1 sequant state init populates state for untracked worktrees ✅ MET Correctly discovers worktrees, extracts issue numbers, fetches titles from GitHub, and populates state. Tested with --json output.
    AC-2 sequant state rebuild recreates state from scratch ✅ MET Combines log parsing + worktree discovery. Requires --force flag as safety measure.
    AC-3 sequant state clean removes orphaned entries ✅ MET Identifies orphaned worktrees, supports --dry-run and --max-age options.
    AC-4 Commands are idempotent (safe to run multiple times) ✅ MET Tests verify idempotency - running twice yields same result. Implemented with proper de-duplication.
    AC-5 Clear output showing what was added/removed/updated ✅ MET Human-readable output with colored status, plus --json flag for scripting.

    Coverage: 5/5 AC items fully met


    Quality Metrics

    Metric Value Status
    Type issues (any) 0 ✅ OK
    Deleted tests 0 ✅ OK
    Files changed 5 ✅ OK
    Lines added +917 -
    Lines deleted -1 -
    Security scan PASS ✅ OK

    Code Review

    Strengths:

    • Clean separation of concerns: CLI commands in state.ts, core logic in state-utils.ts
    • Comprehensive TypeScript interfaces for all options and results
    • Proper error handling with graceful fallbacks (e.g., GitHub title fetch)
    • Good test coverage with idempotency and output structure tests
    • Safe command execution patterns - no dynamic command injection
    • Zod schema validation for log parsing
    • Well-documented function signatures with JSDoc

    Issues:

    • None identified

    Suggestions:

    • Consider adding --backup option to rebuild to save existing state before overwriting
    • The duplicate worktree detection (two worktrees for same issue Add state bootstrapping for existing worktrees #117) is logged correctly but could potentially warn users more prominently

    Self-Evaluation

    • Verified working: Yes - manually tested all three subcommands (init, rebuild, clean)
    • Test efficacy: High - tests cover idempotency (AC-4) and output structure (AC-5), plus core utility functions
    • Likely failure mode: GitHub CLI unavailable (handled gracefully with placeholder title)
    • Verdict confidence: High - all commands executed successfully with expected output

    Verdict: READY_FOR_MERGE

    All acceptance criteria are met. The implementation is well-structured with proper error handling, comprehensive tests, and good developer experience (JSON output, dry-run modes, force flags for destructive operations).

    Documentation

    • N/A - CLI commands are self-documenting (--help), and this is an internal workflow tool

    Next Steps

    1. ✅ Merge PR feat(#117): add state bootstrapping for existing worktrees #134
    2. Run /docs 117 to generate feature documentation if needed

    🤖 Generated by /qa workflow

  8. added
    ready-for-review
    on Jan 20, 2026
  9. added a commit that references this issue on Jan 20, 2026
  10. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    Documentation Generated

    File: docs/state-command.md

    Sections Included:

    • Quick Start - Overview of what the command does
    • Subcommands Reference - Detailed docs for init, rebuild, clean
    • Options Tables - All flags with descriptions
    • Common Workflows - Bootstrap, recover, cleanup scenarios
    • JSON Output - Scripting integration examples
    • Troubleshooting - Common issues and solutions

    Documentation Highlights:

    Subcommand Purpose
    sequant state init Bootstrap state for existing worktrees
    sequant state rebuild Recreate state from logs + worktrees
    sequant state clean Remove orphaned entries

    Next Steps:

    1. Review generated documentation for accuracy
    2. Merge PR feat(#117): add state bootstrapping for existing worktrees #134 to deploy

    🤖 Generated by /docs workflow

  11. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    Smoke Test Results ✅

    Test Result
    state init (first run) ✅ Added 5 worktrees
    state init (second run) ✅ "All worktrees are already tracked" (idempotent)
    state clean --dry-run ✅ "No orphaned entries found"
    status --issues ✅ Shows 3 tracked issues

    Note: Issues with multiple worktrees (e.g., #117, #119) are tracked by issue number as unique key - last worktree wins. This is expected behavior.

    All tests pass. Merging PR #134.

  12. added a commit that references this issue on Jan 20, 2026
  13. admarble commented on Jan 20, 2026

    @admarble
    ContributorAuthor

    Merged via PR #134 (commit 6c85d2f)

  14. added a commit that references this issue on Jan 20, 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

    clienhancementplannedready-for-reviewstate

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions