Skip to content

sequant run: SDK exits with code 1 but direct claude invocation works #42

Closed

Description

@admarble

Summary

When running sequant run <issue>, the exec phase fails with "Claude Code process exited with code 1" after only 2-3 seconds, but invoking the same skill directly via claude -p "Run /exec <issue>" works perfectly.

Observed Behavior

  1. npx sequant run 26 starts successfully
  2. Spec phase completes (1-2 minutes)
  3. Exec phase starts in worktree
  4. After ~2-3 seconds: ✗ exec: Claude Code process exited with code 1
  5. No additional error context provided

Expected Behavior

  • Exec phase should complete successfully (like spec phase does)
  • OR provide actionable error message explaining why it failed

Reproduction Steps

# 1. Run sequant for any issue
npx sequant run <issue-number>

# 2. Observe exec phase fails quickly with exit code 1

# 3. Try direct invocation in the same worktree
cd ../worktrees/feature/<branch>/
claude -p "Run /exec <issue>" --dangerously-skip-permissions

# 4. Direct invocation succeeds

Investigation Findings

What Works

  • claude -p "Run /exec 26" --dangerously-skip-permissions in worktree ✅
  • Spec phase via SDK (runs in main repo) ✅
  • Skills exist and are properly formatted in worktree ✅

What Fails

  • Exec phase via SDK query() call in worktree ❌

SDK Call Configuration (from run.ts)

const queryInstance = query({
  prompt,
  options: {
    abortController,
    cwd: worktreePath,  // Exec runs in worktree
    settingSources: ["project"],
    systemPrompt: { type: "preset", preset: "claude_code" },
    tools: { type: "preset", preset: "claude_code" },
    permissionMode: "bypassPermissions",
    allowDangerouslySkipPermissions: true,
    env: {
      SEQUANT_WORKTREE: worktreePath,
      SEQUANT_ISSUE: String(issueNumber),
    },
  },
});

Error Source

The error comes from @anthropic-ai/claude-agent-sdk/sdk.mjs:7850:

return new Error(`Claude Code process exited with code ${code}`);

Root Cause Hypothesis

  1. SDK process spawning issue - Something about the worktree context causes the spawned process to fail
  2. Permission/settings issue - The SDK may not be loading settings correctly from worktree
  3. Environment variable issue - Custom env vars may conflict with SDK internals
  4. Skill loading issue - SDK may fail to load skill differently than direct CLI

Proposed Fix

  1. Add stderr/stdout capture before process exit for diagnostics
  2. Add pre-flight validation that SDK can initialize in the target cwd
  3. Consider falling back to direct claude CLI invocation if SDK fails

Workaround

Use direct CLI invocation instead of SDK:

cd <worktree-path>
claude -p "Run /exec <issue>" --dangerously-skip-permissions

Impact

  • Severity: Medium (workaround exists)
  • Frequency: 100% reproducible
  • Affected: sequant run with worktree isolation (exec, test, qa phases)

Related

Activity

  1. admarble commented on Jan 9, 2026

    @admarble
    ContributorAuthor

    Implementation Plan

    Acceptance Criteria

    • AC-1: Capture stderr output from SDK process for diagnostics
    • AC-2: Add pre-flight validation that SDK can initialize in target cwd
    • AC-3: Provide actionable error messages explaining failure reason
    • AC-4: (Optional) Consider fallback to direct CLI invocation if SDK fails

    Verification Methods

    AC Method Details
    AC-1 Unit Test + Manual Verify stderr callback receives data; verify error includes stderr
    AC-2 Manual Test Verify validation catches missing settings/skills
    AC-3 Manual Test Verify error message is actionable
    AC-4 Manual Test Verify CLI fallback works if implemented

    Implementation Steps

    Phase 1: stderr capture (primary fix)

    1. Add stderr callback to SDK query() options in executePhase()
    2. Accumulate stderr messages in buffer during execution
    3. Include stderr content in error message when process exits with code 1

    Phase 2: Pre-flight validation
    4. Add validateWorktreeReady() function to check:

    • Worktree directory exists and is accessible
    • .claude/settings.local.json exists
    • .claude/skills/ directory has skill files
    • node_modules exists
    1. Call validation before isolated phases (exec, test, qa)

    Phase 3: CLI fallback (deferred)
    6. Add optional --fallback-to-cli flag for retry with direct claude command

    • Recommendation: Defer to focus on diagnostics first

    Key Decisions

    Decision Choice Rationale
    Fallback mechanism Opt-in flag Avoid masking SDK bugs
    stderr display Last 20 lines + full in verbose Balance noise vs info
    Debug flag New --debug-sdk flag Enable DEBUG_CLAUDE_AGENT_SDK when needed

    Consolidated Assumptions

    • SDK options.stderr callback receives stderr data before process exits
    • stderr contains useful diagnostic info for failure root cause
    • Worktree setup (via ensureWorktree) copies all required files
    • Direct CLI invocation works reliably as fallback

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: Bug fix with clear root cause analysis. Implementation is straightforward SDK option changes.

  2. added
    bug
    on Jan 9, 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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions