feat(v0.6): --agent flag on ctask new selects agent type

Resolver gains SetCLIFlagAgent; DefaultAgent now layers CLIFlag above
EnvVar so doctor/info attribution renders the correct precedence chain
(CLIFlag overrides EnvVar overrides ConfigFile overrides Builtin).

ctask new --agent <type> writes agent.type into the new workspace's
task.yaml. Resolution and validation run before workspace.Create, so
--agent custom without a companion command refuses ("type custom
requires command") with no half-created workspace left on disk. The
deferred Phase 1 test TestCLIFlagOverridesEnvVar lands here.

--agent on resume/last/attach is unchanged (one-shot agent.command
override on the AgentSpec — Open Q 1).
This commit is contained in:
2026-05-15 11:11:16 -04:00
parent b75b82e676
commit a61f900c86
4 changed files with 151 additions and 15 deletions
+34 -2
View File
@@ -79,6 +79,24 @@ type Resolver struct {
envAgent string
envSessionMode string
envEditor string
// cliFlagAgent, when non-empty, is the value of the --agent CLI flag.
// Set by SetCLIFlagAgent after Cobra parses flags; consulted by
// DefaultAgent as the highest-priority layer (SettingSource.CLIFlag).
cliFlagAgent string
}
// SetCLIFlagAgent records the value of the --agent CLI flag for
// resolution. Cmd-layer code calls this AFTER Cobra parses flags, so the
// flag value can participate in source attribution. Empty string means
// the flag was not passed; the env-var layer is consulted instead.
//
// Currently used only by `ctask new` (where --agent is a type selector,
// per v0.6 spec §5 + Open Q 1). Other commands' --agent flags act as
// one-shot agent.command overrides on the AgentSpec, NOT as resolver
// inputs — they do not call this setter.
func (r *Resolver) SetCLIFlagAgent(value string) {
r.cliFlagAgent = value
}
// configPathForTest, when non-empty, overrides ConfigFilePath() for
@@ -223,14 +241,28 @@ func (r *Resolver) SeedDir() ResolvedSetting {
defaultSeedDir("seed"), cfgVal, r.envSeedDir, expandPath)
}
// DefaultAgent resolves the default agent command.
// DefaultAgent resolves the default agent command. Layering is
// Builtin → ConfigFile → EnvVar → CLIFlag: when SetCLIFlagAgent has
// recorded a non-empty --agent value, it wins and the previously
// resolved setting is chained as Override so doctor/info can render the
// full precedence path.
func (r *Resolver) DefaultAgent() ResolvedSetting {
cfgVal := ""
if r.cfg != nil {
cfgVal = r.cfg.DefaultAgent
}
return r.stringSetting("default_agent", "CTASK_AGENT",
base := r.stringSetting("default_agent", "CTASK_AGENT",
"claude", cfgVal, r.envAgent, nil)
if r.cliFlagAgent != "" {
prev := base
return ResolvedSetting{
Key: "default_agent",
Value: r.cliFlagAgent,
Source: CLIFlag,
Override: &prev,
}
}
return base
}
// DefaultCategory resolves the default category for new workspaces.
+39
View File
@@ -288,3 +288,42 @@ func TestResolverEditorFromConfigAndEnv(t *testing.T) {
t.Errorf("Editor override should chain to config (vim), got %+v", s.Override)
}
}
func TestCLIFlagOverridesEnvVar(t *testing.T) {
// Phase 1 deferred this — Phase 2 activates SettingSource.CLIFlag for
// --agent. The test sets CTASK_AGENT in the env, then applies a CLI
// flag override and asserts the resolved value, source, and override
// chain.
SetConfigPathForTest(t, filepath.Join(t.TempDir(), "no-config.yaml"))
t.Setenv("CTASK_AGENT", "opencode")
r := LoadResolver()
r.SetCLIFlagAgent("claude")
s := r.DefaultAgent()
if s.Value != "claude" {
t.Errorf("Value = %q, want claude", s.Value)
}
if s.Source != CLIFlag {
t.Errorf("Source = %v, want CLIFlag", s.Source)
}
if s.Override == nil {
t.Fatal("Override = nil, want non-nil pointing at the EnvVar layer")
}
if s.Override.Source != EnvVar || s.Override.Value != "opencode" {
t.Errorf("Override = {%v %q}, want {EnvVar opencode}", s.Override.Source, s.Override.Value)
}
}
func TestCLIFlagDoesNotApplyWhenUnset(t *testing.T) {
SetConfigPathForTest(t, filepath.Join(t.TempDir(), "no-config.yaml"))
t.Setenv("CTASK_AGENT", "opencode")
r := LoadResolver()
// No SetCLIFlagAgent call.
s := r.DefaultAgent()
if s.Source != EnvVar || s.Value != "opencode" {
t.Errorf("got {%v %q}, want {EnvVar opencode}", s.Source, s.Value)
}
}