diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 8daac6770..12582f1c8 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -23,7 +23,7 @@ something else shows up as drift. | Named omp profiles | omp keeps a profile's settings under `~/.omp/profiles/` (or under `PI_CONFIG_DIR`), each with its own `config.yml` and `models.yml`. Every such profile is an agent of its own — `omp#` on Windows, `omp#@wsl:` in a distro — so each can be connected, drifted and disconnected on its own. They share omp's model lists (`ListsFor`), and the default `omp` row is the folder `ompDir`/`ompIn` already pick, so an `OMP_PROFILE` that row is already pointed at is not listed twice. A name omp refuses is not a row: `default`, a trailing dot, or a Windows reserved device name (`aux`, `nul`, `com1`, `lpt9.x`, `con.work`), on every platform (`ompProfileOK`). A distro's profile names come from its probe and are kept in `wsl.json`; a stopped distro is never started to list them, its profile rows showing what was last seen (`asleep`). | [`omp.go`](../../internal/agent/omp.go), [`wsl.go`](../../internal/agent/wsl.go) | | WSL twins | On Windows each running distro is probed once, without starting a stopped one. The `wslKinds` agents found there become agents of their own, `@wsl:`, edited through `\\wsl.localhost\`. A kind is found by its folder under the distro's home or by its command there (not one on a Windows drive). Where another tool shares one of them, the kind leaves it out: Gemini CLI is found by its command alone, because Antigravity keeps `~/.gemini` too. Goose is found by its folder alone, because a `goose` command can be pressly's migration tool. Reasonix also needs the version its command gives (`accept`), because the old npm client is named `reasonix` too. The gateway address and key follow the distro's networking: mirrored or NAT (`place`, `keyAt`). `ListsFor` keeps a twin's model picks under the Windows twin's id (#927). | [`wsl.go`](../../internal/agent/wsl.go), [`gatewaykey.go`](../../internal/agent/gatewaykey.go) | | Settings tuned from use | Usage › Context's "Tuned for you" advises, per agent, the settings that would have spent the fewest tokens on the user's own calls of the range, and puts one in on a click. Package `tune` replays the calls `sessions.Calls` reads from the agents' session files under each candidate value (`Compact`, `CacheTTL`); `tuneOf` picks the agents and models (Claude Code per model, Codex pooled). `TuneRead`, `TuneApply` and `TuneUndo` know where each setting lives: Claude Code's `modelSettings..autoCompactWindow` (where its `/autocompact` saves), `promptCacheTtl` and `subagentPromptCacheTtl` in `~/.claude/settings.json`; Codex's top-level `model_auto_compact_token_limit`. | [`internal/tune`](../../internal/tune/tune.go), [`tune.go`](../../internal/agent/tune.go), [`internal/gui/tune.go`](../../internal/gui/tune.go) | -| Environment-only agents | Agents that read the gateway only from the environment get a `Launch` command. Cursor Private Inference instead gets user-level variables (`cursorLocalOn`, `KeepCursorLocalEnv`), and magpie writes none of Cursor's own files. Its effort field is kept in magpie (`provider.AgentEffort`) and asked by the gateway, its levels those of the models it lists (`cursorLocalEfforts`, #1003). | [`cursorlocal.go`](../../internal/agent/cursorlocal.go) | +| Environment-only agents | Agents that read the gateway only from the environment get a `Launch` command. Cursor Private Inference instead gets user-level variables (`cursorLocalOn`, `KeepCursorLocalEnv`), and magpie writes none of Cursor's own files. It is found by its product.json in the apps folders, in the folder Windows' list of installed programs names (`cursorLocalInstalls`, any folder picked in its installer), or as its Linux AppImage (`Cursor_Private_Inference-…AppImage`) in the folders downloads are kept in (#1254). Its effort field is kept in magpie (`provider.AgentEffort`) and asked by the gateway, its levels those of the models it lists (`cursorLocalEfforts`, #1003). | [`cursorlocal.go`](../../internal/agent/cursorlocal.go) | ## Runtime path diff --git a/internal/agent/cursorlocal.go b/internal/agent/cursorlocal.go index b5e3d28f4..ebaad7749 100644 --- a/internal/agent/cursorlocal.go +++ b/internal/agent/cursorlocal.go @@ -50,7 +50,7 @@ var cursorLocalApp = func() string { cursorLocalSeen.Lock() defer cursorLocalSeen.Unlock() if time.Since(cursorLocalSeen.at) > 30*time.Second { - cursorLocalSeen.app, cursorLocalSeen.at = findCursorLocal(cursorLocalRoots()), time.Now() + cursorLocalSeen.app, cursorLocalSeen.at = findCursorLocal(cursorLocalRoots(), cursorLocalInstalls()...), time.Now() } return cursorLocalSeen.app } @@ -64,6 +64,9 @@ var cursorLocalSeen struct { } // cursorLocalRoots are the folders apps are installed in on this system. +// On Linux the build comes as an AppImage (Cursor_Private_Inference-- +// x86_64.AppImage), kept wherever it was downloaded, so the folders it is +// usually kept in are looked in too. func cursorLocalRoots() []string { home, _ := os.UserHomeDir() switch runtime.GOOS { @@ -78,46 +81,78 @@ func cursorLocalRoots() []string { } return out } - return []string{"/opt", "/usr/share", "/usr/lib", filepath.Join(home, ".local", "share"), filepath.Join(home, "Applications")} + return []string{"/opt", "/usr/share", "/usr/lib", filepath.Join(home, ".local", "share"), filepath.Join(home, "Applications"), + filepath.Join(home, ".local", "bin"), filepath.Join(home, "bin"), filepath.Join(home, "Downloads"), home} } // findCursorLocal is the program of the Cursor Private Inference among the -// apps in roots: one whose product.json names it, a Mac's bundle by its -// Info.plist's CFBundleExecutable, else by the applicationName -// product.json gives (cursor, Cursor.exe). -func findCursorLocal(roots []string) string { +// apps in roots, else the first of installs (folders it was installed in, +// as Windows' list of installed programs gives them, which may be any +// folder the user picked in its installer). +func findCursorLocal(roots []string, installs ...string) string { for _, root := range roots { ents, err := os.ReadDir(root) if err != nil { continue } for _, e := range ents { - dir := filepath.Join(root, e.Name()) - if strings.HasSuffix(e.Name(), ".app") { - res := filepath.Join(dir, "Contents", "Resources", "app") - if p, ok := cursorLocalProduct(res); ok { - if exe := plistExecutable(filepath.Join(dir, "Contents", "Info.plist")); exe != "" { - return filepath.Join(dir, "Contents", "MacOS", exe) - } - return filepath.Join(dir, "Contents", "MacOS", p.macExe()) - } - continue - } - if p, ok := cursorLocalProduct(filepath.Join(dir, "resources", "app")); ok { - name := p.exe() - if runtime.GOOS == "windows" { - name = p.NameShort + ".exe" - if !isFile(filepath.Join(dir, name)) { - name = p.exe() + ".exe" - } - } - return filepath.Join(dir, name) + if exe := cursorLocalAt(filepath.Join(root, e.Name())); exe != "" { + return exe } } } + for _, dir := range installs { + if exe := cursorLocalAt(dir); exe != "" { + return exe + } + } return "" } +// cursorLocalAt is the program of the build installed at dir, "" when dir +// isn't it: one whose product.json names it, a Mac's bundle by its +// Info.plist's CFBundleExecutable, else by the applicationName product.json +// gives (cursor, Cursor.exe); or its AppImage on Linux, by the name it is +// downloaded under. +func cursorLocalAt(dir string) string { + name := filepath.Base(dir) + if strings.HasSuffix(name, ".app") { + res := filepath.Join(dir, "Contents", "Resources", "app") + if p, ok := cursorLocalProduct(res); ok { + if exe := plistExecutable(filepath.Join(dir, "Contents", "Info.plist")); exe != "" { + return filepath.Join(dir, "Contents", "MacOS", exe) + } + return filepath.Join(dir, "Contents", "MacOS", p.macExe()) + } + return "" + } + if cursorLocalAppImage(name) && isFile(dir) { + return dir + } + if p, ok := cursorLocalProduct(filepath.Join(dir, "resources", "app")); ok { + exe := p.exe() + if runtime.GOOS == "windows" { + exe = p.NameShort + ".exe" + if !isFile(filepath.Join(dir, exe)) { + exe = p.exe() + ".exe" + } + } + return filepath.Join(dir, exe) + } + return "" +} + +// cursorLocalAppImage says a file's name is the build's AppImage as it is +// downloaded (Cursor_Private_Inference-3.24.9-x86_64.AppImage), which +// regular Cursor's (Cursor-3.24.9-x86_64.AppImage) isn't. +func cursorLocalAppImage(name string) bool { + n := strings.ToLower(name) + if !strings.HasSuffix(n, ".appimage") { + return false + } + return strings.HasPrefix(strings.NewReplacer("_", "", "-", "", " ", "").Replace(n), "cursorprivateinference") +} + type cursorProduct struct { NameShort string `json:"nameShort"` ApplicationName string `json:"applicationName"` diff --git a/internal/agent/cursorlocal_installs_other.go b/internal/agent/cursorlocal_installs_other.go new file mode 100644 index 000000000..f5df22e58 --- /dev/null +++ b/internal/agent/cursorlocal_installs_other.go @@ -0,0 +1,7 @@ +//go:build !windows + +package agent + +// cursorLocalInstalls: only Windows keeps a list of where programs are +// installed; elsewhere the folders in cursorLocalRoots are looked in. +func cursorLocalInstalls() []string { return nil } diff --git a/internal/agent/cursorlocal_installs_windows.go b/internal/agent/cursorlocal_installs_windows.go new file mode 100644 index 000000000..0b718c589 --- /dev/null +++ b/internal/agent/cursorlocal_installs_windows.go @@ -0,0 +1,52 @@ +//go:build windows + +package agent + +import ( + "path/filepath" + "strings" + + "golang.org/x/sys/windows/registry" +) + +// cursorLocalInstalls are the folders Windows' list of installed programs +// says Cursor Private Inference is in ("Cursor Private Inference (User)"): +// its installer takes any folder the user picks, not only +// %LOCALAPPDATA%\Programs\cursor (#1254). +func cursorLocalInstalls() []string { + var dirs []string + for _, hive := range []registry.Key{registry.CURRENT_USER, registry.LOCAL_MACHINE} { + for _, root := range []string{`Software\Microsoft\Windows\CurrentVersion\Uninstall`, `Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall`} { + k, err := registry.OpenKey(hive, root, registry.READ) + if err != nil { + continue + } + names, _ := k.ReadSubKeyNames(-1) + for _, name := range names { + entry, err := registry.OpenKey(k, name, registry.QUERY_VALUE) + if err != nil { + continue + } + display, _, _ := entry.GetStringValue("DisplayName") + if strings.HasPrefix(strings.ToLower(display), strings.ToLower(cursorLocalName)) { + dir, _, _ := entry.GetStringValue("InstallLocation") + if dir == "" { + icon, _, _ := entry.GetStringValue("DisplayIcon") + if at := strings.LastIndex(icon, ","); at >= 0 { + icon = icon[:at] + } + if icon != "" { + dir = filepath.Dir(strings.Trim(icon, `"`)) + } + } + if filepath.IsAbs(dir) { + dirs = append(dirs, filepath.Clean(dir)) + } + } + entry.Close() + } + k.Close() + } + } + return dirs +} diff --git a/internal/agent/cursorlocal_test.go b/internal/agent/cursorlocal_test.go index fb84ee972..7e6901e20 100644 --- a/internal/agent/cursorlocal_test.go +++ b/internal/agent/cursorlocal_test.go @@ -215,3 +215,34 @@ func TestCursorLocalEffort(t *testing.T) { t.Fatalf("offered %q while not connected", got) } } + +// TestCursorLocalFoundWhereInstalled: the build installed outside the apps +// folders (a folder picked in its Windows installer, which Windows' list +// of installed programs names) and its Linux AppImage, as downloaded, are +// found; regular Cursor's AppImage isn't (#1254). +func TestCursorLocalFoundWhereInstalled(t *testing.T) { + root, elsewhere := t.TempDir(), filepath.Join(t.TempDir(), "D", "Cursor PI") + // the Windows install's layout, as its 3.24.9 user setup leaves it + res := filepath.Join(elsewhere, "resources", "app") + if err := os.MkdirAll(res, 0o755); err != nil { + t.Fatal(err) + } + os.WriteFile(filepath.Join(res, "product.json"), []byte(`{"nameShort":"Cursor Private Inference","nameLong":"Cursor Private Inference","applicationName":"cursor","dataFolderName":".cursor","win32DirName":"cursor"}`), 0o644) + os.WriteFile(filepath.Join(elsewhere, "Cursor Private Inference.exe"), nil, 0o755) + if got := findCursorLocal([]string{root}); got != "" { + t.Fatalf("found %q with nothing in the apps folder", got) + } + if got := findCursorLocal([]string{root}, filepath.Join(root, "gone"), elsewhere); filepath.Dir(got) != elsewhere { + t.Fatalf("installed in a folder of the user's: found %q, want a program in %q", got, elsewhere) + } + + os.WriteFile(filepath.Join(root, "Cursor-3.24.9-x86_64.AppImage"), nil, 0o755) + if got := findCursorLocal([]string{root}); got != "" { + t.Fatalf("regular Cursor's AppImage found as it: %q", got) + } + img := filepath.Join(root, "Cursor_Private_Inference-3.24.9-x86_64.AppImage") + os.WriteFile(img, nil, 0o755) + if got := findCursorLocal([]string{root}); got != img { + t.Fatalf("its AppImage: found %q, want %q", got, img) + } +}