package setup import ( "context" "errors" "fmt" "io/fs" "maps" "os" "path/filepath" "runtime" "slices" "strings" "time" "github.com/MakeNowJust/heredoc/v2" "github.com/spf13/cobra" "gitlab.com/gitlab-org/cli/internal/api" "gitlab.com/gitlab-org/cli/internal/cmdutils" "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/claudehooks" "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/fallbacksync" "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/gaig" "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/opencodeplugin" "gitlab.com/gitlab-org/cli/internal/commands/govern/internal/syncstate" "gitlab.com/gitlab-org/cli/internal/config" "gitlab.com/gitlab-org/cli/internal/dbg" "gitlab.com/gitlab-org/cli/internal/iostreams" "gitlab.com/gitlab-org/cli/internal/mcpannotations" "gitlab.com/gitlab-org/cli/internal/text" ) type options struct { io *iostreams.IOStreams executor cmdutils.Executor buildInfo api.BuildInfo goos string yes bool noFallbackSync bool includeHistory bool agents []string agentsChanged bool uninstall bool settingsPathOverride string } func NewCmd(f cmdutils.Factory) *cobra.Command { opts := &options{ io: f.IO(), executor: f.Executor(), buildInfo: f.BuildInfo(), goos: runtime.GOOS, } cmd := &cobra.Command{ Use: "setup", Short: "Configure this machine to record AI agent sessions. (EXPERIMENTAL)", Long: heredoc.Docf(` Configure the current machine for AI governance session capture. Choose the agents to sync with %[1]s--agents%[1]s. Without it, setup syncs the agents set up by an earlier run, or Claude Code if there are none. For each agent, setup installs: - Claude Code (%[1]sclaude-code%[1]s): a Stop hook in %[1]s~/.claude/settings.json%[1]s, which syncs the session after every agent turn, and a SessionEnd hook, which syncs the session and marks it completed. - OpenCode (%[1]sopencode%[1]s), version 1.4 or later: a plugin in %[1]s~/.config/opencode/plugins/glab-govern.js%[1]s (under %[1]s$XDG_CONFIG_HOME%[1]s when it is set), which syncs the session every time it goes idle. OpenCode loads it for all your sessions, in every project. Later setups keep it up to date. - Codex (%[1]scodex%[1]s) and Cursor (%[1]scursor%[1]s): nothing. The fallback periodic sync finds their sessions by scanning their transcripts in %[1]s~/.codex/sessions%[1]s and %[1]s~/.cursor/projects%[1]s, and uploads those from repositories on GitLab hosts you are logged in to with glab. Their sessions are marked completed once they have been idle for 24 hours. Setup also installs the fallback periodic sync: a launchd agent (%[1]s~/Library/LaunchAgents/com.gitlab.glab-govern-audit-sync.plist%[1]s) on macOS, or a systemd user timer (%[1]s~/.config/systemd/user/glab-govern-audit-sync.timer%[1]s) on Linux. The fallback periodic sync runs %[1]sglab minder audit sync --all%[1]s every 30 minutes, and on Linux also 5 minutes after boot, until you remove it. It uploads anything the hooks missed for sessions they have already recorded (see %[1]sglab minder audit sync --help%[1]s for what is uploaded), using your stored glab credentials and the glab configuration directory in use when you run setup. Each session goes to the project and host of the Git repository the agent ran in. It also marks sessions completed once they have been idle for 24 hours. It pauses sessions from agents that use hooks while those hooks are not installed. Pass %[1]s--no-fallback-sync%[1]s to install only the hooks and plugin, which Codex and Cursor cannot be synced without. The fallback periodic sync is not available on Windows. By default, glab uploads only what each agent does after you first set it up: not its sessions from before, and not the earlier turns of a session from before that you resume. Cursor records no times, so setup notes where each existing Cursor transcript ends, and only what is added to it afterwards is uploaded. A chat that Cursor copies under a new ID, for example when you duplicate it, counts as a new session and is uploaded in full. Pass %[1]s--include-history%[1]s to also upload activity from the last 89 days, including those sessions and turns, for the agents this setup sets up for the first time. For Cursor, that is every transcript changed in the last 89 days. An agent set up earlier keeps what it uploads, so to add its history, run %[1]s--uninstall%[1]s first. If you set up an agent with an earlier glab version, which did not record when, its activity from the last 89 days is still uploaded until you run setup again. Run setup again with %[1]s--agents%[1]s to add agents. Agents set up by an earlier run stay set up, so to stop syncing an agent, run %[1]s--uninstall%[1]s and then set up the agents you want. Every setup prompt names the agents that are synced. Safe to run multiple times: existing hooks are not duplicated, and an existing fallback periodic sync job is replaced. Run %[1]sglab minder doctor%[1]s afterwards to verify the setup. Run %[1]sglab minder setup --uninstall%[1]s to remove everything setup installed: the hooks, the OpenCode plugin, the fallback periodic sync job, and Codex and Cursor session syncing. It also removes glab's local records of your agent sessions and forgets the agents you set up and when, so the next setup starts with Claude Code again. Other hooks and settings in %[1]s~/.claude/settings.json%[1]s, and other OpenCode plugins, are left unchanged. `, "`") + text.ExperimentalString, Example: heredoc.Doc(` # Sync Claude Code sessions, or the agents set up earlier $ glab minder setup # Sync OpenCode sessions $ glab minder setup --agents opencode # Sync Claude Code, OpenCode, and Codex sessions $ glab minder setup --agents claude-code,opencode,codex # Also upload activity from the last 89 days, including sessions from before setup $ glab minder setup --include-history # Install only the hooks and plugin, without the fallback periodic sync $ glab minder setup --no-fallback-sync # Remove everything setup installed $ glab minder setup --uninstall `), Args: cobra.NoArgs, ValidArgsFunction: cobra.NoFileCompletions, Annotations: map[string]string{ mcpannotations.Destructive: "true", }, RunE: func(cmd *cobra.Command, args []string) error { opts.agentsChanged = cmd.Flags().Changed("agents") if err := validateAgents(opts.agents, opts.agentsChanged, opts.noFallbackSync); err != nil { return err } if opts.uninstall { return runUninstall(cmd.Context(), opts) } return runSetup(cmd.Context(), opts) }, } fl := cmd.Flags() fl.BoolVarP(&opts.yes, "yes", "y", false, "Skip the confirmation prompt, and agree to what it would describe: the changes and, for setup, the uploads.") fl.BoolVar(&opts.noFallbackSync, "no-fallback-sync", false, "Install only the hooks and plugin, without the fallback periodic sync job.") fl.BoolVar(&opts.includeHistory, "include-history", false, "Also upload the activity from the last 89 days of the agents set up for the first time, including sessions from before setup and the earlier turns of sessions you resume.") fl.BoolVar(&opts.uninstall, "uninstall", false, "Remove the hooks, the OpenCode plugin, the fallback periodic sync job, and the local session records, and stop syncing Codex and Cursor sessions.") fl.StringSliceVar(&opts.agents, "agents", nil, fmt.Sprintf("Agents to sync sessions from: %s. Added to the agents set up by an earlier run. The OpenCode plugin runs for all your OpenCode sessions. Multiple agents can be comma-separated or specified by repeating the flag.", strings.Join(allAgents(), ", "))) cmd.MarkFlagsMutuallyExclusive("uninstall", "no-fallback-sync") cmd.MarkFlagsMutuallyExclusive("agents", "uninstall") cmd.MarkFlagsMutuallyExclusive("include-history", "uninstall") return cmd } // plan is what a setup run installs. type plan struct { claudeHooks bool // openCodePlugin is the path to install the OpenCode plugin at, or empty. openCodePlugin string // discovered are the agents the fallback periodic sync discovers. discovered []string // recorded are the agent setups recorded by earlier setups. recorded fallbacksync.AgentSetups uploads uploadScope } // uploadScope groups the agents a setup syncs by how far back their activity // is uploaded. type uploadScope struct { fromNow []string // fromEarlier are the agents an earlier setup recorded. fromEarlier []string history []string } // planUploads groups agents by how far back their activity is uploaded after // a setup. An agent set up earlier keeps the time it was first set up and its // history choice, because a later setup cannot bring back what was skipped. func planUploads(recorded fallbacksync.AgentSetups, agents []string, history bool) uploadScope { var scope uploadScope for _, a := range agents { setup, earlier := recorded[a] switch { case setup.History, !earlier && history: scope.history = append(scope.history, a) case earlier: scope.fromEarlier = append(scope.fromEarlier, a) default: scope.fromNow = append(scope.fromNow, a) } } return scope } // setups returns the agent setups to record for a setup at now: recorded, and // the agents set up for the first time. func (s uploadScope) setups(recorded fallbacksync.AgentSetups, now time.Time) fallbacksync.AgentSetups { setups := maps.Clone(recorded) if setups == nil { setups = fallbacksync.AgentSetups{} } for _, a := range s.fromNow { setups[a] = fallbacksync.AgentSetup{SetUpAt: now} } for _, a := range s.history { if _, earlier := setups[a]; !earlier { setups[a] = fallbacksync.AgentSetup{SetUpAt: now, History: true} } } return setups } // isNew reports whether a setup recording s sets up agent for the first time. func (s uploadScope) isNew(recorded fallbacksync.AgentSetups, agent string) bool { _, earlier := recorded[agent] return !earlier && (slices.Contains(s.fromNow, agent) || slices.Contains(s.history, agent)) } const ( notEarlier = "not earlier sessions or the earlier turns of sessions you resume" withEarlier = "including earlier sessions and the earlier turns of sessions you resume" ) // prompt says what the setup prompt agrees to upload. Agents are named only // when they are uploaded from different times. func (s uploadScope) prompt() string { groups := 0 for _, g := range [][]string{s.fromNow, s.fromEarlier, s.history} { if len(g) > 0 { groups++ } } if groups == 1 { switch { case len(s.fromNow) > 0: return ". Only activity from now on will be uploaded, " + notEarlier case len(s.fromEarlier) > 0: return ". Only activity from when you first set up each agent will be uploaded, " + notEarlier default: return ". Activity from the last 89 days and from now on will be uploaded, " + withEarlier } } var prompt string if len(s.fromNow) > 0 { prompt += fmt.Sprintf(". For %s, only activity from now on will be uploaded, %s", joinAnd(s.fromNow), notEarlier) } if len(s.fromEarlier) > 0 { pronoun := "them" if len(s.fromEarlier) == 1 { pronoun = "it" } prompt += fmt.Sprintf(". For %s, only activity from when you first set %s up will be uploaded, %s", joinAnd(s.fromEarlier), pronoun, notEarlier) } if len(s.history) > 0 { prompt += fmt.Sprintf(". For %s, activity from the last 89 days and from now on will be uploaded, %s", joinAnd(s.history), withEarlier) } return prompt } func setupPrompt(goos string, noFallbackSync bool, p plan) string { var items []string if p.claudeHooks { items = append(items, "agent session hooks in ~/.claude/settings.json") } if p.openCodePlugin != "" { items = append(items, "an OpenCode plugin in "+opencodeplugin.Display(p.openCodePlugin)+" that runs for all your OpenCode sessions") } if !noFallbackSync { items = append(items, fmt.Sprintf("%s that syncs sessions in the background every %d minutes, replacing any existing job", fallbacksync.Description(goos), int(fallbacksync.Interval.Minutes()))) } prompt := "glab will install " + joinAnd(items) prompt += ". Agent sessions will be uploaded to the GitLab project of the repository they run in, including your prompts and each tool call's arguments, such as commands and the contents of files the agent writes. Common token and key formats are replaced first, but other secrets in them are uploaded as they are" prompt += ". glab also keeps a local record of each agent session in its configuration directory, with the session ID, the agent, the transcript path and how far it was synced, including sessions outside GitLab repositories, which are never uploaded" if !noFallbackSync && len(p.discovered) > 0 { prompt += fmt.Sprintf(". The job will also upload your %s sessions the same way", strings.Join(p.discovered, " and ")) } return prompt + p.uploads.prompt() + ". Do you wish to continue?" } func uninstallPrompt(hooks bool, openCodePlugin string, job bool, agents []string, sessionState bool) string { var items []string if hooks { items = append(items, "the agent session hooks from ~/.claude/settings.json") } if openCodePlugin != "" { items = append(items, "the OpenCode plugin at "+opencodeplugin.Display(openCodePlugin)) } if job { items = append(items, "the fallback periodic sync job") } if sessionState { items = append(items, "glab's local records of your agent sessions") } var prompt string switch { case len(items) > 0: prompt = "glab will remove " + joinAnd(items) if len(agents) > 0 { prompt += fmt.Sprintf(". It will also stop syncing %s sessions", strings.Join(agents, " and ")) } case len(agents) > 0: prompt = fmt.Sprintf("glab will stop syncing %s sessions", strings.Join(agents, " and ")) default: prompt = "glab will turn off session discovery" } return prompt + ". Do you wish to continue?" } func joinAnd(items []string) string { switch len(items) { case 0: return "" case 1: return items[0] case 2: return items[0] + " and " + items[1] default: return strings.Join(items[:len(items)-1], ", ") + ", and " + items[len(items)-1] } } func allAgents() []string { return slices.Concat(fallbacksync.HookAgents, fallbacksync.DiscoverableAgents) } func validateAgents(agents []string, changed, noFallbackSync bool) error { if changed && len(agents) == 0 { return cmdutils.FlagError{Err: errors.New("--agents needs at least one agent; to remove everything setup installed, use --uninstall")} } for _, a := range agents { switch { case !slices.Contains(allAgents(), a): return cmdutils.FlagError{Err: fmt.Errorf("unsupported agent %q in --agents; use %s", a, strings.Join(allAgents(), ", "))} case noFallbackSync && slices.Contains(fallbacksync.DiscoverableAgents, a): return cmdutils.FlagError{Err: fmt.Errorf("%s sessions are only synced by the fallback periodic sync, so --agents %s cannot be used with --no-fallback-sync", a, a)} } } return nil } func confirm(ctx context.Context, opts *options, prompt string) (bool, error) { io := opts.io switch { case opts.yes: return true, nil case !io.CanPrompt(): return false, fmt.Errorf("cannot prompt for confirmation in non-interactive mode; pass --yes to skip") } var confirmed bool if err := io.Confirm(ctx, &confirmed, prompt); err != nil { return false, err } return confirmed, nil } func runSetup(ctx context.Context, opts *options) error { io := opts.io c := io.Color() hookAgents, discovered, err := chosenAgents(opts) if err != nil { return err } p, err := setupPlan(opts, hookAgents, discovered) if err != nil { return err } confirmed, err := confirm(ctx, opts, setupPrompt(opts.goos, opts.noFallbackSync, p)) if err != nil { return err } if !confirmed { io.LogInfo("Setup cancelled.") return nil } if p.uploads.isNew(p.recorded, "cursor") { if err := startCursorSessionsAtEnd(slices.Contains(p.uploads.history, "cursor")); err != nil { return fmt.Errorf("failed to record how far your Cursor sessions go: %w", err) } } // Recorded before anything is installed, so that no hook uploads more // than the user just agreed to. if err := fallbacksync.SetAgentSetups(p.uploads.setups(p.recorded, time.Now())); err != nil { return fmt.Errorf("failed to record when the agents were set up: %w", err) } if p.claudeHooks { io.LogInfo("Installing agent session hooks...") if err := installClaudeHooks(opts); err != nil { return fmt.Errorf("failed to install agent session hooks: %w", err) } io.LogInfof("%s Hooks installed in ~/%s\n", c.GreenCheck(), claudehooks.SettingsFile) } if p.openCodePlugin != "" { if _, err := opencodeplugin.Install(p.openCodePlugin); err != nil { return fmt.Errorf("failed to install the OpenCode plugin: %w", err) } io.LogInfof("%s OpenCode plugin installed in %s\n", c.GreenCheck(), opencodeplugin.Display(p.openCodePlugin)) } if err := fallbacksync.SetHookAgents(hookAgents); err != nil { return fmt.Errorf("failed to record which agents are synced: %w", err) } if !opts.noFallbackSync { if err := installFallbackSync(ctx, opts); err != nil { io.LogErrorf("warning: could not install fallback periodic sync: %v\nAny previous fallback periodic sync job has been removed. Run 'glab minder setup' again once this is fixed.\n", err) } else { io.LogInfof("%s Fallback periodic sync installed: %s.\n", c.GreenCheck(), fallbacksync.Description(opts.goos)) } } if opts.agentsChanged { if err := fallbacksync.SetDiscoveredAgents(discovered); err != nil { return fmt.Errorf("failed to enable session discovery: %w", err) } if len(discovered) > 0 { io.LogInfof("%s Syncing sessions from: %s.\n", c.GreenCheck(), strings.Join(discovered, ", ")) } } io.LogInfo("\nSetup complete. Run 'glab minder doctor' to verify.") return nil } // chosenAgents returns the agents to install a hook or plugin for, and the // agents for the fallback periodic sync to discover: those set up by an // earlier run, and those passed with --agents. func chosenAgents(opts *options) ([]string, []string, error) { discovered, err := fallbacksync.DiscoveredAgents() if err != nil { return nil, nil, agentsReadError(err) } settings, err := settingsPath(opts) if err != nil { return nil, nil, err } hookAgents, err := fallbacksync.ResolvedHookAgents(settings) if err != nil { return nil, nil, agentsReadError(err) } if !opts.agentsChanged { return fallbacksync.DefaultHookAgents(hookAgents, discovered), discovered, nil } hookAgents, discovered = slices.Clone(hookAgents), slices.Clone(discovered) for _, a := range opts.agents { switch { case slices.Contains(hookAgents, a) || slices.Contains(discovered, a): case slices.Contains(fallbacksync.HookAgents, a): hookAgents = append(hookAgents, a) default: discovered = append(discovered, a) } } return hookAgents, discovered, nil } // startCursorSessionsAtEnd makes the syncing of each Cursor session start at // the end of its transcript as it is now. Cursor records no times, so this is // how sync tells a session's turns from before setup from those after. With // history, it does so only for transcripts last changed longer ago than glab // uploads, whose turns sync would otherwise time by a later change. func startCursorSessionsAtEnd(history bool) error { transcripts, err := gaig.CursorTranscripts() if err != nil { return err } for _, t := range transcripts { if history && time.Since(t.ModTime) <= gaig.MaxEventAge { continue } if _, err := syncstate.PositionPath(t.ID); err != nil { dbg.Debugf("skipping %s, which glab cannot sync: %v", t.Path, err) continue } if err := syncstate.StartAt(t.ID, t.Size); err != nil { return err } } return nil } func agentsReadError(err error) error { return fmt.Errorf("could not tell which agents are set up: %w; %s", err, fallbacksync.AgentsReadFix(err)) } // setupPlan works out what to install for the chosen agents. func setupPlan(opts *options, hookAgents, discovered []string) (plan, error) { p := plan{ claudeHooks: slices.Contains(hookAgents, "claude-code"), discovered: discovered, } switch { case opts.noFallbackSync && len(hookAgents) == 0: return plan{}, cmdutils.FlagError{Err: fmt.Errorf("%s sessions are only synced by the fallback periodic sync, so they cannot be set up with --no-fallback-sync", strings.Join(discovered, " and "))} case len(discovered) > 0 && !fallbacksync.Supported(opts.goos): return plan{}, fmt.Errorf("%s sessions need the fallback periodic sync: %w", strings.Join(discovered, " and "), fallbacksync.ErrUnsupportedOS) case p.claudeHooks && opts.goos == "windows": return plan{}, errors.New("hook installation is not supported on Windows") } recorded, err := fallbacksync.ReadAgentSetups() if err != nil { return plan{}, agentsReadError(err) } synced := hookAgents if !opts.noFallbackSync { synced = slices.Concat(hookAgents, discovered) } if opts.includeHistory && !slices.ContainsFunc(synced, func(a string) bool { _, earlier := recorded[a]; return !earlier }) { return plan{}, cmdutils.FlagError{Err: fmt.Errorf("--include-history applies only to agents set up for the first time, and every agent this setup syncs (%s) is set up already. To upload history from before an earlier setup, run 'glab minder setup --uninstall', then set up again with --include-history", strings.Join(synced, ", "))} } p.recorded = recorded p.uploads = planUploads(recorded, synced, opts.includeHistory) if !slices.Contains(hookAgents, "opencode") { return p, nil } if opts.goos == "windows" { return plan{}, errors.New("the OpenCode plugin is not supported on Windows") } path, err := opencodeplugin.Path() if err != nil { return plan{}, err } switch state, err := opencodeplugin.Check(path); { case err != nil: return plan{}, fmt.Errorf("could not read the OpenCode plugin: %w", err) case state == opencodeplugin.Foreign: return plan{}, opencodeplugin.ForeignError(path) } p.openCodePlugin = path return p, nil } func runUninstall(ctx context.Context, opts *options) error { io := opts.io c := io.Color() home, err := os.UserHomeDir() if err != nil { return err } // An unreadable list must not block uninstall, which is the way to // recover from it. It is reset with the rest. agents, err := fallbacksync.DiscoveredAgents() unreadable := err != nil if unreadable { dbg.Debugf("could not read which agents are synced: %v", err) } jobPresent := jobMayBePresent(ctx, opts, home) // An unreadable settings file must not block removing the rest, but the // user is told before confirming that its hooks will stay. hooksPresent, hooksErr := claudeHooksInstalled(opts) openCodePlugin, pluginErr := installedOpenCodePlugin() statePresent, stateErr := sessionStatePresent() // Hooks or a plugin that could not be read stay installed and would write // new records, so the records are kept for a later uninstall to remove. removeState := statePresent && hooksErr == nil && pluginErr == nil if !jobPresent && !hooksPresent && openCodePlugin == "" && len(agents) == 0 && !unreadable && !removeState { if hooksErr != nil || pluginErr != nil || stateErr != nil { return errors.Join(uninstallReadErrors(hooksErr, pluginErr, stateErr)...) } forgetHookAgents() io.LogInfo("Nothing to uninstall: no hooks, OpenCode plugin, or fallback periodic sync job found.") return nil } if hooksErr != nil { io.LogErrorf("warning: could not read ~/%s, so its hooks will not be removed: %v\n", claudehooks.SettingsFile, hooksErr) } if pluginErr != nil { io.LogErrorf("warning: could not check for the OpenCode plugin, so it will not be removed: %v\n", pluginErr) } if stateErr != nil { io.LogErrorf("warning: could not check glab's local records of your agent sessions, so they will not be removed: %v\n", stateErr) } confirmed, err := confirm(ctx, opts, uninstallPrompt(hooksPresent, openCodePlugin, jobPresent, agents, removeState)) if err != nil { return err } if !confirmed { io.LogInfo("Uninstall cancelled.") return nil } forgetHookAgents() // Discovery is turned off too, so a later setup does not bring it back // without the user choosing it again. if len(agents) > 0 || unreadable { if err := fallbacksync.SetDiscoveredAgents(nil); err != nil { return fmt.Errorf("failed to turn off session discovery: %w", err) } } if len(agents) > 0 { io.LogInfof("%s Stopped syncing %s sessions.\n", c.GreenCheck(), strings.Join(agents, ", ")) } if openCodePlugin != "" { removed, err := opencodeplugin.Remove(openCodePlugin) switch { case err != nil: return fmt.Errorf("failed to remove the OpenCode plugin: %w", err) case removed: io.LogInfof("%s OpenCode plugin removed from %s\n", c.GreenCheck(), opencodeplugin.Display(openCodePlugin)) default: io.LogInfo("No OpenCode plugin found.") } } if jobPresent { removed, err := fallbacksync.Uninstall(ctx, opts.executor, opts.goos, home, os.Getuid()) switch { case err != nil: return fmt.Errorf("failed to remove fallback periodic sync: %w", err) case removed: io.LogInfof("%s Fallback periodic sync removed.\n", c.GreenCheck()) default: io.LogInfo("No fallback periodic sync job found.") } } if hooksPresent { removed, err := removeClaudeHooks(opts) switch { case err != nil: return fmt.Errorf("failed to remove agent session hooks: %w", err) case removed: io.LogInfof("%s Hooks removed from ~/%s\n", c.GreenCheck(), claudehooks.SettingsFile) default: io.LogInfo("No agent session hooks found.") } } // Removed last, after the job and the hooks that write to it. if removeState { if err := os.RemoveAll(gaig.StateDir()); err != nil { return fmt.Errorf("failed to remove the local records of your agent sessions: %w", err) } io.LogInfof("%s Local records of your agent sessions removed.\n", c.GreenCheck()) } return errors.Join(uninstallReadErrors(hooksErr, pluginErr, stateErr)...) } func sessionStatePresent() (bool, error) { entries, err := os.ReadDir(gaig.StateDir()) switch { case errors.Is(err, fs.ErrNotExist): return false, nil case err != nil: return false, err } return len(entries) > 0, nil } // uninstallReadErrors reports what uninstall left in place because it could // not be read. func uninstallReadErrors(hooksErr, pluginErr, stateErr error) []error { var errs []error if hooksErr != nil { errs = append(errs, fmt.Errorf("the agent session hooks were not removed; fix ~/%s and run 'glab minder setup --uninstall' again: %w", claudehooks.SettingsFile, hooksErr)) } if pluginErr != nil { errs = append(errs, fmt.Errorf("the OpenCode plugin may still be installed; fix it and run 'glab minder setup --uninstall' again: %w", pluginErr)) } if stateErr != nil { errs = append(errs, fmt.Errorf("glab's local records of your agent sessions were not removed; fix %s and run 'glab minder setup --uninstall' again: %w", gaig.StateDir(), stateErr)) } return errs } // forgetHookAgents forgets the agents chosen by an earlier setup, so that a // later setup starts from the default agents again. func forgetHookAgents() { if err := fallbacksync.ForgetHookAgents(); err != nil { dbg.Debugf("could not forget which agents are synced: %v", err) } } // installedOpenCodePlugin returns the path of the OpenCode plugin glab // installed, or an empty string when there is none. func installedOpenCodePlugin() (string, error) { path, err := opencodeplugin.Path() if err != nil { return "", err } state, err := opencodeplugin.Check(path) if err != nil || state == opencodeplugin.NotInstalled || state == opencodeplugin.Foreign { return "", err } return path, nil } func claudeHooksInstalled(opts *options) (bool, error) { path, err := settingsPath(opts) if err != nil { return false, err } raw, err := claudehooks.LoadRawSettings(path) if err != nil { return false, err } hooks, err := claudehooks.HooksFromRaw(raw) if err != nil { return false, err } return claudehooks.GlabHookPresent(hooks, "Stop") || claudehooks.GlabHookPresent(hooks, "SessionEnd"), nil } // jobMayBePresent reports whether uninstall has anything to remove: the job's // files, or a job still loaded after its files were deleted. When either // check fails, it errs towards true so that uninstall still runs. func jobMayBePresent(ctx context.Context, opts *options, home string) bool { _, err := fallbacksync.InstalledBinary(opts.goos, home) switch { case errors.Is(err, fallbacksync.ErrUnsupportedOS): return false case !errors.Is(err, fallbacksync.ErrNotInstalled): return true } state, err := fallbacksync.State(ctx, opts.executor, opts.goos, os.Getuid()) if err != nil { dbg.Debugf("could not check whether the fallback job is loaded: %v", err) return true } return state.Loaded } func settingsPath(opts *options) (string, error) { if opts != nil && opts.settingsPathOverride != "" { return opts.settingsPathOverride, nil } return claudehooks.SettingsPath() } func installClaudeHooks(opts *options) error { if runtime.GOOS == "windows" { return fmt.Errorf("hook installation is not supported on Windows") } path, err := settingsPath(opts) if err != nil { return err } raw, err := claudehooks.LoadRawSettings(path) if err != nil { return err } hooks, err := claudehooks.HooksFromRaw(raw) if err != nil { return err } if !claudehooks.InstallGlabHooks(hooks) { return nil } hooksJSON, err := claudehooks.MarshalHooks(hooks) if err != nil { return fmt.Errorf("could not serialise hooks: %w", err) } raw["hooks"] = hooksJSON return claudehooks.WriteSettings(path, raw) } func removeClaudeHooks(opts *options) (bool, error) { path, err := settingsPath(opts) if err != nil { return false, err } raw, err := claudehooks.LoadRawSettings(path) if err != nil { return false, err } hooks, err := claudehooks.HooksFromRaw(raw) if err != nil { return false, err } if !claudehooks.RemoveGlabHooks(hooks) { return false, nil } if len(hooks) == 0 { delete(raw, "hooks") return true, claudehooks.WriteSettings(path, raw) } hooksJSON, err := claudehooks.MarshalHooks(hooks) if err != nil { return false, fmt.Errorf("could not serialise hooks: %w", err) } raw["hooks"] = hooksJSON return true, claudehooks.WriteSettings(path, raw) } func installFallbackSync(ctx context.Context, opts *options) error { home, err := os.UserHomeDir() if err != nil { return err } glabPath, err := glabBinaryPath(ctx, opts.executor, opts.buildInfo) if err != nil { return err } job := fallbacksync.Job{GlabPath: glabPath, ConfigDir: config.ConfigDir()} return fallbacksync.Install(ctx, opts.executor, opts.goos, home, job, os.Getuid()) } // glabBinaryPath returns the path the scheduled job should run: the glab on // PATH if it is the same build as the running glab, otherwise the running // glab. Symlinks are deliberately left unresolved: package managers such as // Homebrew, asdf, and mise point a stable path (or shim) at a versioned // install directory that is deleted on upgrade. Builds are compared by // version rather than by file, because a shim is a different file from the // binary it runs. func glabBinaryPath(ctx context.Context, executor cmdutils.Executor, running api.BuildInfo) (string, error) { want := fallbacksync.BuildVersion(running) onPath, err := executor.LookPath("glab") if err != nil { dbg.Debugf("glab is not in PATH, scheduling the running glab: %v", err) } else { got, err := fallbacksync.BinaryVersion(ctx, executor, onPath) switch { case err != nil: dbg.Debugf("could not run %s version, scheduling the running glab: %v", onPath, err) case got != want: dbg.Debugf("%s reports %q, not %q, scheduling the running glab", onPath, got, want) default: return filepath.Abs(onPath) } } path, err := os.Executable() if err != nil { return "", fmt.Errorf("could not find glab binary: %w", err) } return filepath.Abs(path) }