diff --git a/docs/guide.md b/docs/guide.md index 55bd81c..484bb5c 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -428,7 +428,7 @@ offload agent enable --agent codex --json # also: disable, --agent claude|al - `profiles --json` prints `schema_version` (1), `config_exists`, `configured_default`, `active`, `active_source` (`environment`, `config` or `none`), `delegation_mode`, `profiles` (sorted by name) and `warnings`. Each profile has `name`, `backend`, `base_url`, `base_url_redacted`, `model`, `endpoint`, `reasoning_effort`, `service_tier` and `auth_mode` (`token_cmd`, `token` or `none`). It never prints a token or token command, and never runs the token command. A `base_url` containing credentials, a query or a fragment is withheld (empty, `base_url_redacted: true`). A missing config is not an error (`config_exists: false`, empty list). `active` follows the run rules, so `OFFLOAD_PROFILE` wins over the saved default. - `profile save --json` reads one JSON object from stdin (at most 64 KiB, unknown fields rejected) and prints the updated, redacted list. Fields: `name` (exact, required, cannot be renamed), `create` (bool), and optional patches `backend`, `base_url`, `model`, `endpoint`, `reasoning_effort`, `service_tier`, `auth_mode` (`preserve` default, `token`, `token_cmd`, `none`) and `secret`. Omitted fields keep their stored value, including current credentials and a private base URL. `secret` is required for `token` and `token_cmd` and refused with `preserve` or `none`; it is only accepted on stdin, never as an argument. A new profile defaults to the Claude backend and becomes the saved default, as in `offload config`. Claude profiles need authentication; Codex profiles do not. Creating a Codex profile with `service_tier` omitted defaults to `priority` (Fast On); explicit `default` keeps Fast Off and an empty string inherits the native setting. Editing an existing profile never changes its tier unless that field is supplied. Errors are fixed messages that never echo request values. There is no rename or delete. -- `agent status|enable|disable` act on user scope only (Codex policy under `$CODEX_HOME` or `~/.codex`, skill under `~/.agents/skills`; Claude under `$CLAUDE_CONFIG_DIR` or `~/.claude`), using the install planning shared with `install-agent`. Status reports each agent as `enabled`, `disabled`, `partial`, `blocked` or `shadowed` (a nonempty Codex `AGENTS.override.md` at the user configuration root takes precedence). The state describes the files on disk, not what a running session has loaded: **start a new agent session after enabling or disabling**. Only offload-owned skill files and marked policy blocks are written or removed, and unrelated instructions are kept. A modified, symlinked or unmanaged skill, or a malformed or non-regular policy file, is reported as `blocked` and left untouched. The change is not atomic across files: a failure rolls back what it can, and any state that could not be restored is reported (run `offload agent status`). +- `agent status|enable|disable` act on user scope only (Codex policy under `$CODEX_HOME` or `~/.codex`, skill under `~/.agents/skills`; Claude under `$CLAUDE_CONFIG_DIR` or `~/.claude`), using the install planning shared with `install-agent`. Status reports each agent as `enabled`, `disabled`, `partial`, `blocked` or `shadowed` (a nonempty Codex `AGENTS.override.md` at the user configuration root takes precedence). The state describes the files on disk, not what a running session has loaded: **start a new agent session after enabling or disabling**. Only offload-owned skill files and marked policy blocks are written or removed, and unrelated instructions are kept. A modified, symlinked or unmanaged skill, or a malformed or non-regular policy file, is reported as `blocked` and left untouched. The change is not atomic across files: a failure rolls back what it can, and any state that could not be restored is reported (run `offload agent status`). `disable` is remembered (`disabled_agents` in the config): `offload mode`, `offload enable|disable` and a plain `install-agent` skip that agent until `agent enable` or `install-agent -agent ` turns it back on. ### Stats and the macOS menu bar monitor diff --git a/internal/agentinstall/control.go b/internal/agentinstall/control.go index f8e7ce8..6ef492a 100644 --- a/internal/agentinstall/control.go +++ b/internal/agentinstall/control.go @@ -126,6 +126,13 @@ func RunControl(args []string, stdout, stderr io.Writer) int { if err != nil { return fail(stderr, err) } + names := make([]string, len(selected)) + for k, i := range selected { + names[k] = controlAgentNames[i] + } + if err := config.SetAgentsDisabled(config.Path(), names, action == "disable"); err != nil { + return fail(stderr, fmt.Errorf("agent %s: files updated but the choice was not saved, so a later mode or global switch may undo it: %w", action, err)) + } if status, _, err = controlSnapshot(); err != nil { return fail(stderr, fmt.Errorf("agent %s: changes applied but status could not be read: %w", action, err)) } diff --git a/internal/agentinstall/install.go b/internal/agentinstall/install.go index 124b959..72a4e6a 100644 --- a/internal/agentinstall/install.go +++ b/internal/agentinstall/install.go @@ -99,6 +99,11 @@ func Run(args []string, stdout, stderr io.Writer) int { if err != nil { return fail(stderr, err) } + if *scope == "user" { + if targets, err = respectDisabledAgents(*agent, targets, stdout); err != nil { + return fail(stderr, err) + } + } disabled, err := config.LoadDisabled(config.Path()) if err != nil { return fail(stderr, err) @@ -120,6 +125,11 @@ func Run(args []string, stdout, stderr io.Writer) int { } fmt.Fprintf(stdout, "%s %s\n", f.status, f.path) } + if *scope == "user" && *agent != "all" { + if err := config.SetAgentsDisabled(config.Path(), []string{*agent}, false); err != nil { + return fail(stderr, fmt.Errorf("install-agent: %s installed but its disabled choice was not cleared: %w", *agent, err)) + } + } for _, target := range targets { if filepath.Base(target.policyPath) == "AGENTS.md" { override := filepath.Join(filepath.Dir(target.policyPath), "AGENTS.override.md") @@ -131,6 +141,28 @@ func Run(args []string, stdout, stderr io.Writer) int { return 0 } +// respectDisabledAgents keeps user-scope agents turned off with `offload agent disable` off when +// every agent is refreshed (mode and global switches, plain install-agent). Naming one agent +// explicitly installs it; Run then clears that choice. +func respectDisabledAgents(agent string, targets []agentInstallTarget, stdout io.Writer) ([]agentInstallTarget, error) { + if agent != "all" { + return targets, nil + } + off, err := config.LoadDisabledAgents(config.Path()) + if err != nil { + return nil, err + } + var kept []agentInstallTarget + for i, t := range targets { + if name := controlAgentNames[i]; off[name] { + fmt.Fprintf(stdout, "skipped %s: disabled by the user (offload agent enable --agent %s turns it back on)\n", name, name) + continue + } + kept = append(kept, t) + } + return kept, nil +} + func agentInstallTargets(agent, scope, dir string) ([]agentInstallTarget, error) { if agent != "codex" && agent != "claude" && agent != "all" { return nil, fmt.Errorf("install-agent: unknown agent %q (want codex, claude, or all)", agent) diff --git a/internal/agentinstall/mode_test.go b/internal/agentinstall/mode_test.go index 3cd381d..3c371de 100644 --- a/internal/agentinstall/mode_test.go +++ b/internal/agentinstall/mode_test.go @@ -401,3 +401,69 @@ func TestAgentControlFollowsGlobalSwitch(t *testing.T) { t.Fatalf("policy after enable:\n%s", got) } } + +// An agent turned off with `offload agent disable` stays off through mode and global switches +// until the user turns it back on for that agent. +func TestDisabledAgentSurvivesModeAndGlobalSwitches(t *testing.T) { + home := modeHome(t) + claudeSkill := filepath.Join(home, ".claude", "skills", "offload") + codexSkill := filepath.Join(home, ".agents", "skills", "offload", "SKILL.md") + if code, _, errOut := runMode("proactive"); code != 0 { + t.Fatal(errOut) + } + controlState(t, "disable", "--agent", "claude") + assertOff := func(step string) { + t.Helper() + if _, err := os.Stat(claudeSkill); !os.IsNotExist(err) { + t.Fatalf("%s re-enabled the claude skill", step) + } + if strings.Contains(string(installTestRead(t, filepath.Join(home, ".claude", "CLAUDE.md"))), offloadPolicyBegin) { + t.Fatalf("%s restored the claude policy", step) + } + } + code, out, errOut := runMode("basic") + if code != 0 || !strings.Contains(out, "skipped claude") { + t.Fatalf("%d %s %s", code, out, errOut) + } + assertOff("mode switch") + if !strings.Contains(string(installTestRead(t, codexSkill)), "Delegation mode: basic") { + t.Fatal("codex was not refreshed") + } + for _, cmd := range []string{"disable", "enable"} { + var o, e bytes.Buffer + if code := RunSwitch([]string{cmd}, &o, &e); code != 0 { + t.Fatal(e.String()) + } + assertOff("global " + cmd) + } + var o, e bytes.Buffer + if code := Run(nil, &o, &e); code != 0 { + t.Fatal(e.String()) + } + assertOff("install-agent") + + st := controlState(t, "enable", "--agent", "claude") + if st.Agents[1].Status != "enabled" || st.Agents[1].Mode != "basic" { + t.Fatalf("enable: %+v", st.Agents[1]) + } + if off, _ := config.LoadDisabledAgents(config.Path()); len(off) != 0 { + t.Fatalf("choice not cleared: %v", off) + } + if code, out, _ := runMode("proactive"); code != 0 || strings.Contains(out, "skipped") { + t.Fatal(out) + } +} + +func TestExplicitInstallClearsDisabledChoice(t *testing.T) { + modeHome(t) + if err := config.SetAgentsDisabled(config.Path(), []string{"claude"}, true); err != nil { + t.Fatal(err) + } + var o, e bytes.Buffer + if code := Run([]string{"-agent", "claude"}, &o, &e); code != 0 { + t.Fatal(e.String()) + } + if off, _ := config.LoadDisabledAgents(config.Path()); off["claude"] { + t.Fatal("explicit install kept the disabled choice") + } +} diff --git a/internal/config/mode.go b/internal/config/mode.go index 41086e7..34607f3 100644 --- a/internal/config/mode.go +++ b/internal/config/mode.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "sort" ) // LoadMode does not require worker profiles or create a config file. @@ -128,3 +129,61 @@ func SetDisabled(path string, disabled bool) error { fields["disabled"], _ = json.Marshal(disabled) return writeSettings(path, fields) } + +// LoadDisabledAgents returns the agents whose Offload instructions the user turned off with +// `offload agent disable`. Their absence on disk alone cannot tell "never installed" from +// "turned off", so the choice is saved here and later instruction refreshes respect it. +func LoadDisabledAgents(path string) (map[string]bool, error) { + fields, err := modeSettings(path) + if err != nil { + return nil, err + } + var names []string + if raw, ok := fields["disabled_agents"]; ok { + if err := json.Unmarshal(raw, &names); err != nil { + return nil, fmt.Errorf("invalid disabled_agents: %w", err) + } + } + out := map[string]bool{} + for _, n := range names { + out[n] = true + } + return out, nil +} + +// SetAgentsDisabled records or clears the per-agent choice, preserving every other field. +func SetAgentsDisabled(path string, agents []string, disabled bool) error { + fields, err := modeSettings(path) + if err != nil { + return err + } + current, err := LoadDisabledAgents(path) + if err != nil { + return err + } + changed := false + for _, a := range agents { + if current[a] != disabled { + changed = true + } + if disabled { + current[a] = true + } else { + delete(current, a) + } + } + if !changed { + return nil + } + if len(current) == 0 { + delete(fields, "disabled_agents") + } else { + names := make([]string, 0, len(current)) + for n := range current { + names = append(names, n) + } + sort.Strings(names) + fields["disabled_agents"], _ = json.Marshal(names) + } + return writeSettings(path, fields) +}