Skip to content

feat(run): per-phase agent selection — run.phases.<phase>.agent #1150

Closed

Description

@admarble

Context

sequant run picks one agent driver for the whole run: run.agent (default claude-code), which becomes ExecutionConfig.agent. #1137 (claude-cloud driver) needs exec to run in a claude.ai cloud session and qa to run locally, because the qa skill is ~45K tokens per turn and a local run bills the subscription. Split out of #1137 by owner decision on 2026-09-24, because the change is generic and has no cloud dependency.

run.phases.<phase> (PhasePolicy, #914) holds model/effort only, and its doc comments describe it as claude-code-only. PhasePolicySchema is a plain z.object, so an undeclared agent key would be silently stripped, not rejected.

Every site that reads the run-level agent today

  • src/lib/workflow/phase-executor.ts:1345/1348: the skill-prompt builder (buildSkillPrompt)
  • src/lib/workflow/phase-executor.ts:1369/1375: the driver label
  • src/lib/workflow/phase-executor.ts:1396: getPhasePrompt
  • src/lib/workflow/phase-executor.ts:1449: file context for file-oriented drivers
  • src/lib/workflow/phase-executor.ts:1598: the driver instance
  • src/lib/workflow/phase-executor.ts:2050: the usesSdkMcp MCP-fallback gate
  • src/lib/workflow/run-orchestrator.ts:1151/1154: the skills pre-flight (feat(run): pre-flight the skills install — missing .claude/skills/ should fail fast, not flail for 30 minutes as a spec retry #813)
  • src/lib/workflow/config-resolver.ts:610 and :631: role: resolution for phase policies and the model ladder, both resolved against settings.run.agent
  • src/commands/doctor.ts:683/705/774: per-driver health checks

Fix shape

Add agent?: string to PhasePolicy and PhasePolicySchema. Add one resolver, resolvePhaseAgent(config, phase), which returns phasePolicies[phase].agent ?? config.agent ?? "claude-code", and make every site above read through it. Role references (role:<name>) in a phase's model, and ladder rungs applied to that phase, resolve against that phase's agent. Validate every per-phase agent name at config resolution, before the first phase runs.

Acceptance criteria

  • AC-1: run.phases.<phase>.agent survives settings parsing (not stripped by PhasePolicySchema) and appears on the resolved phasePolicies — Evidence: npx vitest run src/lib/settings.phase-policy.test.ts src/lib/workflow/config-resolver.phase-policy.test.ts
  • AC-2: an unknown per-phase agent name fails at config resolution with the same "Unknown agent driver" message run.agent gives, before any phase executes — Evidence: npx vitest run src/lib/workflow/config-resolver.phase-policy.test.ts src/lib/workflow/skills-preflight.test.ts (the throw and its message; RunOrchestrator.run rejecting with no worktree, pre-flight or phase)
  • AC-3: with run.agent: "codex" and run.phases.qa.agent: "claude-code", exec runs through the codex driver and qa through the claude-code driver — Evidence: npx vitest run src/lib/workflow/phase-executor.phase-agent.test.ts case asserting which driver ran each phase, the registry mocked to return real driver instances
  • AC-4: no file under src/lib/workflow/ reads config.agent except the resolver — Evidence: grep -rn "config\.agent" src/lib/workflow --include='*.ts' | grep -v "\.test\." prints only lines inside resolvePhaseAgent
  • AC-5: a phase's role: model reference and any ladder rung applied to that phase resolve against that phase's agent, not run.agent — Evidence: npx vitest run src/lib/workflow/config-resolver.model-roles.test.ts src/lib/workflow/model-ladder.test.ts with a driver-keyed role map and a per-phase agent
  • AC-6: the skills pre-flight runs when any phase's resolved driver resolves skills, not only when the run-level one does — Evidence: npx vitest run src/lib/workflow/skills-preflight.test.ts case with run.agent: "aider" and run.phases.exec.agent: "claude-code"
  • AC-7: a resume handle created by one phase's driver is never passed to a different driver in a later phase — Evidence: npx vitest run src/lib/workflow/phase-executor.phase-agent.test.ts case asserting resumeHandle is undefined when the driver changes
  • AC-8: sequant doctor runs the driver checks for every distinct resolved agent (run-level and per-phase), not only run.agent — Evidence: src/commands/doctor.test.ts case with run.phases.exec.agent: "codex" and no codex binary, asserting the codex check is reported
  • AC-9: with no agent key anywhere, ExecutionConfig and every phase's driver are identical to today — Evidence: npx vitest run src/lib/workflow/ready-gate-run.test.ts src/lib/workflow/phase-executor.test.ts (the fix(ready-gate): driver settings never reach the gate (RunReadyGateOptions lacks the fields) — silently runs claude-code for aider/opencode configs #863 parity test is in the first) green without edits to existing cases
  • AC-10: docs/reference/run-command.md documents run.phases.<phase>.agent, its precedence over run.agent, and that model/effort go to whichever driver the phase resolves to — Evidence: grep -n "phases.<phase>.agent" docs/reference/run-command.md non-empty
  • AC-11: npm run build && npm test green — Evidence: CI test job

Non-Goals

Graph metadata

Related

#914 (per-phase model/effort) · #975 (model roles) · #971 (model ladder) · #813 (skills pre-flight) · #674 (resume handles) · #1137

Activity

  1. added
    enhancement
    multi-agent
    on Sep 24, 2026
  2. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Exec progress

    PR #1163 is open. All 11 ACs are implemented and have tests; the per-AC evidence table and mutation results are in the PR body.

    • Implementation: a new resolver, src/lib/workflow/phase-agent.ts, is used by phase-executor.ts, skills-preflight.ts, run-orchestrator.ts, model-ladder.ts, config-resolver.ts and commands/doctor.ts. drivers/index.ts gains assertKnownDriver, and PhasePolicy gains agent in both the settings schema and the resolver.
    • Checks: build, lint and script typecheck pass. Full suite: 341 files, 8425 passed, 1 expected fail, 3 skipped.
    • Evidence paths: I changed the AC-3 and AC-7 evidence in the issue body to phase-executor.phase-agent.test.ts. The existing phase-executor.test.ts doesn't mock the driver registry, and mocking it there would change all its other tests.
    • Extra fix: role: references and ladder rungs now resolve against the effective run-level agent (--agent included), not settings.run.agent.
  3. added a commit that references this issue on Sep 25, 2026
  4. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    QA Review for Issue #1150

    PR #1163, HEAD c9bbe339. Reviewed in the feature/1150-… worktree: 0 commits behind main, clean tree.

    AC Coverage

    AC Status Evidence (each command run verbatim this pass)
    AC-1 MET settings.phase-policy.test.ts + config-resolver.phase-policy.test.ts: 28 passed. PhasePolicySchema.agent survives parsing, no unknown-key warning, and the key reaches phasePolicies.
    AC-2 MET config-resolver.phase-policy.test.ts + skills-preflight.test.ts: 43 passed. The throw's message equals getDriver's. The real RunOrchestrator.run rejects with no worktree, pre-flight or runIssue call. resolveConfig runs before ensureWorktrees, confirmed at run-orchestrator.ts (resolveConfig at the top of run, ensureWorktrees at :1117).
    AC-3 MET phase-executor.phase-agent.test.ts: 3 passed. The real CodexDriver runs exec and the real ClaudeCodeDriver runs qa.
    AC-4 MET The grep prints one line: phase-agent.ts:23 (inside resolvePhaseAgent). The wider \.agent\b sweep of src/lib/workflow, src/commands and bin also found no bypass.
    AC-5 MET config-resolver.model-roles.test.ts + model-ladder.test.ts: 98 passed. Covers role and ladder resolution per agent, the rung-error naming the phase, codex vs claude-code escalation, and the pin rung.
    AC-6 MET skills-preflight.test.ts: 27 passed. An aider run with a claude-code exec requires only exec, and the error names claude-code.
    AC-7 MET phase-executor.phase-agent.test.ts: a codex handle reaches the codex exec and is withheld from the claude-code qa, through the real canResume.
    AC-8 MET doctor.test.ts: 66 passed. Reports "configured as agent for phase exec".
    AC-9 MET ready-gate-run.test.ts + phase-executor.test.ts: 190 passed. git diff on both files is empty (no edits to existing cases).
    AC-10 MET grep -n "phases.<phase>.agent" docs/reference/run-command.md: 3 lines (§ Per-Phase Agent).
    AC-11 MET CI test SUCCESS. Local npm test: 341 files, 8428 passed, 1 expected fail, 3 skipped. Build and lint pass.

    Coverage: 11/11 MET

    Precheck Findings

    Section Status Surfaced
    Fixtures not_applicable none
    Sibling-grep pass 0 sibling sites
    AC literal-diff pass none missing

    CI Status

    test, canary, typecheck, node-floor-guard, validate-skills, validate-plugin, tautology-advisory and structural are SUCCESS. model-backed was SKIPPED (secret-gated).

    CI Summary: 8 passed, 0 pending, 0 failed

    Quality Checks (sub-agents, sequential)

    Check Status Notes
    Type safety / deleted tests Pass No any, @ts-ignore or non-null assertions in added lines. tsc is clean. No test files or expect lines removed.
    Scope / size Pass 20 files, +785/-100. No hunk outside #1150 and no Non-Goal touched. The unrelated #1141 reformat was reverted before QA.
    Security Pass 0 critical, 0 warning. assertKnownDriver uses Object.hasOwn (__proto__/constructor are rejected). The agent string never reaches a shell.

    Test Coverage Analysis

    Every changed source file has tests. The new phase-agent.ts has phase-agent.test.ts. The per-file mutation results are in the PR body: 4 mutations, and each fails exactly its targeted tests.

    Call-Site Review

    The new exports are resolvePhaseAgent, resolveRunAgent, resolvePhaseAgents, assertKnownDriver, runResolvesSkills and ladderForPhase. Each one is called:

    • in phase-executor.ts (5 sites, all keyed by the running phase);
    • in config-resolver.ts (validation and the phase-agent map);
    • in skills-preflight.ts and the orchestrator gate;
    • in model-ladder.ts (3 ladder reads);
    • in doctor.ts.

    No unused export. The loop in resolveModelLadder is intended: once per distinct overriding agent.

    Execution Evidence / CLI Registration

    N/A: no scripts/ or bin/ change, and no option-interface field was added (settings-only, by the Non-Goal).

    Risk Assessment

    • Likely failure mode: --agent codex combined with a role: model under the default (string-shorthand) roles now fails at config time. This is intended, pinned by two tests, and in the CHANGELOG, but it is a visible behavior change.
    • Not tested: a live two-driver sequant run, since it needs the codex binary. Dispatch is covered with real driver classes.
    • Sibling sites considered: src/lib/ci/ has its own agent input (a Non-Goal). init.ts --agent provisioning is run-level only (a Non-Goal). driverOverrides is read only by getPhasePrompt, which now gets the resolved agent.
    • Sibling-line audit: every config.agent read in phase-executor.ts (6 sites) was converted, as AC-4's grep confirms.

    Trust-Boundary Check

    Finding: Clean. The diff implements only the issue's product requirements; no agent-directed external-text instruction was acted on.
    Status: Clean

    Behavior-Rule Survival Check

    Status: N/A. detectBehaviorRule ran on all 11 stored ACs of #1150 this pass and none triggered.

    Declared-Evidence Execution

    Status: Complete. All 11 evidence commands were run verbatim this pass, with results as in the AC table.

    Mutation Verification

    Status: Not-Applicable. No AC is a gate test under isGateTestEvidence. The PR body records 4 mutations anyway.

    CHANGELOG Verification

    Added: a per-phase agent entry (#1150). Fixed: role: follows --agent (#1150). Result: met.

    Adversarial Re-Read

    Findings:

    1. Discoverability gap, pre-existing. The settings reference table generated in src/lib/settings.ts (near :1782) has no phases row at all (missing since feat(run): per-phase model and effort configuration for the claude-code driver #914), so run.phases.<phase>.agent isn't listed there. Its agent row also omits codex. docs/reference/run-command.md does document the key. This is a one-row fix.
    2. Test-isolation leak, pre-existing, not caused by this diff. During npm test in this worktree, some test rewrote the checkout's real .sequant/state.json to an empty state at 14:20. feat(run): per-phase agent selection — run.phases.<phase>.agent #1150's stored ACs vanished mid-QA and I restored them. I ran all three new test files alone and none touches it; the rewrite carries a lastSynced stamp, which suggests a reconcile path. Worth an issue: running the suite in a real checkout silently wipes its workflow state.

    Also checked:

    Status: Gaps Found (both pre-existing, non-blocking)

    Verdict: AC_MET_BUT_NOT_A_PLUS

    All 11 ACs are MET with their evidence commands executed, CI is green, and the full suite passes. It isn't A+ because of the two pre-existing gaps above. Gap 1 is a one-line doc fix that could land in this PR. Gap 2 should be its own issue. Neither blocks merge.

    Documentation

    • docs/reference/run-command.md § Per-Phase Agent
    • CHANGELOG entries
    • Settings reference table row for phases (gap 1)

    Next Steps

    1. Optionally add a phases row, noting agent/model/effort, to the settings reference table in src/lib/settings.ts.
    2. File a follow-up for the suite rewriting the checkout's .sequant/state.json.
    3. Merge feat(#1150): per-phase agent selection — run.phases.<phase>.agent #1163 on owner approval.
  5. added
    needs-improvement
    on Sep 25, 2026
  6. added 2 commits that reference this issue on Sep 25, 2026
  7. added a commit that references this issue on Sep 25, 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

    enhancementmulti-agentneeds-improvement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions