Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions apps/computer/local-daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,26 @@ model.
Daemon rereads the status, which carries what the phase reserved or freed
(pool use, placement, models unloaded first), and applies the job's line on
top; later lines of the phase update that snapshot without another read.
- Pi. When the loaded models change, the Daemon asks the Runtime monitor for a
full discovery, so TeamHub sees Pi's new model list. Each model's
`pi_model` (`<provider>/<id>`, as an Agent's `runtime_model` stores it) is
passed through as `localmodels` reports it; the endpoint keeps only the
loaded engine's `base_url`.
- Pi. The Daemon aligns `PI_CODING_AGENT_DIR` on `localmodels` tool calls with
`workspace.PiSourceEnvironment` (`~/.pi/agent` when unset) so loaded models
land in the same Pi profile that runtime discovery and execution read. When
a snapshot reports a loaded model (or a load completes) on a Computer where Pi
is enabled, unconfigured in `runtime_paths`, and not already discovered
externally, `internal/localmodels` installs the pinned Pi runtime
asynchronously in the background under `state/local-models/harness` (passing
the bundled Node binary via `--node` when available, validating the plan
contract, retrying with exponential backoff on transient failures, and
cancelling/waiting cleanly on `Close()`) before asking the Runtime monitor for
a full discovery with the managed Pi executable and binary directory on
`PATH`. Each model's `pi_model` (`<provider>/<id>`, as an Agent's
`runtime_model` stores it) is passed through as `localmodels` reports it; the
endpoint keeps only the loaded engine's `base_url`.
- Shared weight cache. On multi-container Computer hosts, mount a shared cache
directory and pass `LINGER_LOCALMODELS_CACHE_DIR` or
`LINGER_LOCALMODELS_REUSE_DIRS` in the Daemon environment so `localmodels`
receives `LOCALMODELS_CACHE_DIR` and `LOCALMODELS_REUSE_DIRS` (when those
`LOCALMODELS_*` variables are not already set) and reuses SHA256-verified
GGUF weights without re-downloading.

## Codex models cache

Expand Down
111 changes: 100 additions & 11 deletions apps/computer/local-daemon/cmd/linger-computer-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/signal"
"path/filepath"
"runtime"
"slices"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -131,12 +132,32 @@ func run(args []string) (resultErr error) {
slog.Info("pruned abandoned Operation temp directories", "removed", removed, "bytes_freed", bytesFreed)
}
providerEnvironment := runtimeProviderEnvironment(environment, computer.GatewayToken, computer.CodexAuthFile)
var localModelsTool *localmodels.Tool
if tool := computer.LocalModels; tool != nil {
localModelsTool = &localmodels.Tool{
Python: tool.Python, ModuleDirectory: tool.ModuleDirectory,
StateDirectory: tool.StateDirectory, BaseDirectory: tool.BaseDirectory,
}
}
harnessDirectory := filepath.Join(computer.StateDirectory, "local-models", "harness")
piEnabled := piRuntimeEnabled(computer.EnabledRuntimes)
var managedPi string
if localModelsTool != nil && piEnabled && computer.RuntimePaths["pi"] == "" {
inspectCtx, cancelInspect := context.WithTimeout(context.Background(), 20*time.Second)
found, inspectErr := localModelsTool.InspectPi(inspectCtx, providerEnvironment, harnessDirectory)
cancelInspect()
if inspectErr != nil {
slog.Warn("local_models.pi_inspect_failed", "error", inspectErr)
} else {
managedPi = found
}
}
discovered, err := provider.Discover(context.Background(), provider.DiscoveryOptions{
ConfiguredPaths: computer.RuntimePaths,
ConfiguredPaths: withManagedPiPath(computer.RuntimePaths, managedPi),
Runtimes: computer.EnabledRuntimes,
PreferredRuntime: computer.PreferredRuntime,
RuntimeHomeRoot: filepath.Join(computer.StateDirectory, "runtime-homes"),
Environment: providerEnvironment,
Environment: withManagedPiEnvironment(providerEnvironment, computer.RuntimePaths, managedPi),
})
if err != nil {
return err
Expand Down Expand Up @@ -176,7 +197,7 @@ func run(args []string) (resultErr error) {
if err != nil {
return err
}
providers, runtimes := providerInstallations(discovered, providerEnvironment)
providers, runtimes := providerInstallations(discovered, withManagedPiEnvironment(providerEnvironment, computer.RuntimePaths, managedPi))
providerCatalog := lingercontext.NewProviderCatalog(providers)
workspaceGrants := make(map[string]lingercontext.WorkspaceGrant)
wireGrants := make([]map[string]any, 0, len(computer.WorkspaceGrants))
Expand Down Expand Up @@ -285,16 +306,20 @@ func run(args []string) (resultErr error) {
// full Runtime discovery.
runtimeRefresh := make(chan struct{}, 1)
var localDaemon *daemon.Daemon
var localModelsTool *localmodels.Tool
if tool := computer.LocalModels; tool != nil {
localModelsTool = &localmodels.Tool{
Python: tool.Python, ModuleDirectory: tool.ModuleDirectory,
StateDirectory: tool.StateDirectory, BaseDirectory: tool.BaseDirectory,
}
libraryHarnessDir := ""
if shouldProvisionManagedPi(computer.EnabledRuntimes, computer.RuntimePaths, discovered, managedPi) {
libraryHarnessDir = harnessDirectory
}
var bundledNodePath string
if toolInstallation != nil {
bundledNodePath = toolInstallation.NodePath
}
localModels := localmodels.New(localmodels.Options{
Tool: localModelsTool, Environment: providerEnvironment,
JobDirectory: filepath.Join(computer.StateDirectory, "local-models", "jobs"),
JobDirectory: filepath.Join(computer.StateDirectory, "local-models", "jobs"),
HarnessDirectory: libraryHarnessDir,
InitialPiExecutable: managedPi,
NodePath: bundledNodePath,
// The library publishes only from Act and Refresh, which the Daemon
// calls once it runs.
Publish: func(snapshot wire.LocalModelsSnapshot) { localDaemon.PublishLocalModels(snapshot) },
Expand Down Expand Up @@ -412,9 +437,15 @@ func run(args []string) (resultErr error) {
return localDaemon.ActiveOperationCount() > 0
},
Refresh: runtimeRefresh,
Discover: func(discoverCtx context.Context, discoverOptions provider.DiscoveryOptions) (provider.DiscoveryResult, error) {
managed := localModels.PiExecutable()
discoverOptions.Environment = withManagedPiEnvironment(discoverOptions.Environment, discoverOptions.ConfiguredPaths, managed)
discoverOptions.ConfiguredPaths = withManagedPiPath(discoverOptions.ConfiguredPaths, managed)
return provider.Discover(discoverCtx, discoverOptions)
},
OnChanged: func(next provider.DiscoveryResult) error {
logRuntimeDiagnostics(next.Diagnostics)
nextProviders, nextRuntimes := providerInstallations(next, providerEnvironment)
nextProviders, nextRuntimes := providerInstallations(next, withManagedPiEnvironment(providerEnvironment, computer.RuntimePaths, localModels.PiExecutable()))
providerCatalog.Replace(nextProviders)
controller.UpdateDiagnostics(next.Diagnostics)
return inventory.publish(ctx, nextRuntimes, next.Diagnostics)
Expand Down Expand Up @@ -710,11 +741,69 @@ func installedTeamHubCLIBinAt(executable string) string {
return directory
}

func piRuntimeEnabled(enabledRuntimes []string) bool {
return len(enabledRuntimes) == 0 || slices.Contains(enabledRuntimes, "pi")
}

func shouldProvisionManagedPi(enabledRuntimes []string, runtimePaths map[string]string, discovered provider.DiscoveryResult, managedPi string) bool {
if !piRuntimeEnabled(enabledRuntimes) || runtimePaths["pi"] != "" {
return false
}
_, hasPi := discovered.Installations["pi"]
return !hasPi || managedPi != ""
}

func withManagedPiPath(paths map[string]string, managed string) map[string]string {
if managed == "" || paths["pi"] != "" {
return paths
}
cloned := maps.Clone(paths)
if cloned == nil {
cloned = make(map[string]string, 1)
}
cloned["pi"] = managed
return cloned
}

func withManagedPiEnvironment(environment, configuredPaths map[string]string, managed string) map[string]string {
if managed == "" || (configuredPaths["pi"] != "" && configuredPaths["pi"] != managed) {
return environment
}
dir := filepath.Dir(managed)
if dir == "" || dir == "." {
return environment
}
cloned := maps.Clone(environment)
if cloned == nil {
cloned = make(map[string]string, 1)
}
existing := cloned["PATH"]
if existing == "" {
cloned["PATH"] = dir
return cloned
}
entries := filepath.SplitList(existing)
filtered := make([]string, 0, len(entries)+1)
filtered = append(filtered, dir)
cleanDir := filepath.Clean(dir)
for _, entry := range entries {
if entry == "" || filepath.Clean(entry) == cleanDir {
continue
}
filtered = append(filtered, entry)
}
cloned["PATH"] = strings.Join(filtered, string(os.PathListSeparator))
return cloned
}

func runtimeProviderEnvironment(source map[string]string, gatewayToken, codexAuthFile string) map[string]string {
result := computerconfig.RuntimeEnvironment(source, gatewayToken)
if codexAuthFile != "" {
result["CODEX_HOME"] = filepath.Dir(codexAuthFile)
}
if piEnv, err := localworkspace.PiSourceEnvironment(result); err == nil && piEnv["PI_CODING_AGENT_DIR"] != "" {
result["PI_CODING_AGENT_DIR"] = piEnv["PI_CODING_AGENT_DIR"]
}
return result
}

Expand Down
97 changes: 97 additions & 0 deletions apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/computerconfig"
"github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/linger/assignment"
"github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/provider"
localworkspace "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace"
)

// Extend the transient protocol failure regression through the real discovery
Expand Down Expand Up @@ -233,3 +234,99 @@ func TestProviderInstallationsKeepDiscoveredContextWindows(t *testing.T) {
t.Fatalf("context windows = %v", windows)
}
}

func TestRuntimeProviderEnvironmentAlignsPiCodingAgentDir(t *testing.T) {
home := t.TempDir()
wantDefault, err := localworkspace.PiSourceEnvironment(map[string]string{"HOME": home})
if err != nil {
t.Fatal(err)
}
gotDefault := runtimeProviderEnvironment(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": ""}, "", "")
if gotDefault["PI_CODING_AGENT_DIR"] != wantDefault["PI_CODING_AGENT_DIR"] {
t.Fatalf("default PI_CODING_AGENT_DIR = %q, want %q", gotDefault["PI_CODING_AGENT_DIR"], wantDefault["PI_CODING_AGENT_DIR"])
}

custom := filepath.Join(t.TempDir(), "pi-agent")
if err := os.MkdirAll(custom, 0o700); err != nil {
t.Fatal(err)
}
wantCustom, err := localworkspace.PiSourceEnvironment(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": custom + string(filepath.Separator) + "."})
if err != nil {
t.Fatal(err)
}
gotCustom := runtimeProviderEnvironment(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": custom + string(filepath.Separator) + "."}, "", "")
if gotCustom["PI_CODING_AGENT_DIR"] != wantCustom["PI_CODING_AGENT_DIR"] {
t.Fatalf("custom PI_CODING_AGENT_DIR = %q, want %q", gotCustom["PI_CODING_AGENT_DIR"], wantCustom["PI_CODING_AGENT_DIR"])
}
}

func TestWithManagedPiPathRespectsConfiguredPiAndClonesSafely(t *testing.T) {
if got := withManagedPiPath(nil, ""); got != nil {
t.Fatalf("empty managed path returned %v", got)
}
configured := map[string]string{"codex": "/bin/codex", "pi": "/custom/pi"}
if got := withManagedPiPath(configured, "/managed/pi"); got["pi"] != "/custom/pi" {
t.Fatalf("managed Pi overwrote configured path: %v", got)
}
original := map[string]string{"codex": "/bin/codex"}
merged := withManagedPiPath(original, "/managed/pi")
if merged["pi"] != "/managed/pi" || original["pi"] != "" {
t.Fatalf("merged=%v original=%v", merged, original)
}
}

func TestWithManagedPiEnvironmentPrependsBinDirectoryOnPATH(t *testing.T) {
root := t.TempDir()
bin1 := filepath.Join(root, "usr", "bin")
bin2 := filepath.Join(root, "bin")
base := map[string]string{"HOME": root, "PATH": bin1 + string(os.PathListSeparator) + bin2}
managed := filepath.Join(root, "state", "local-models", "harness", "tools", "pi", "1.3.0", "pi")
managedDir := filepath.Dir(managed)

if got := withManagedPiEnvironment(base, nil, ""); got["PATH"] != base["PATH"] {
t.Fatalf("empty managed path altered PATH: %v", got)
}
if got := withManagedPiEnvironment(base, map[string]string{"pi": filepath.Join(root, "custom", "pi")}, managed); got["PATH"] != base["PATH"] {
t.Fatalf("configured pi path was overridden: %v", got)
}

updated := withManagedPiEnvironment(base, nil, managed)
want := managedDir + string(os.PathListSeparator) + bin1 + string(os.PathListSeparator) + bin2
if updated["PATH"] != want {
t.Fatalf("updated PATH = %q, want %q", updated["PATH"], want)
}
if base["PATH"] != bin1+string(os.PathListSeparator)+bin2 {
t.Fatalf("caller environment mutated: %v", base)
}

deduped := withManagedPiEnvironment(map[string]string{"PATH": bin1 + string(os.PathListSeparator) + managedDir}, map[string]string{"pi": managed}, managed)
if deduped["PATH"] != managedDir+string(os.PathListSeparator)+bin1 {
t.Fatalf("deduped PATH = %q", deduped["PATH"])
}

emptyPath := withManagedPiEnvironment(nil, nil, managed)
if emptyPath["PATH"] != managedDir {
t.Fatalf("empty PATH = %q, want %q", emptyPath["PATH"], managedDir)
}
}

func TestShouldProvisionManagedPi(t *testing.T) {
noPi := provider.DiscoveryResult{Installations: map[string]provider.RuntimeDiagnostic{}}
externalPi := provider.DiscoveryResult{Installations: map[string]provider.RuntimeDiagnostic{"pi": {Type: "pi", ExecutablePath: "/usr/local/bin/pi"}}}

if shouldProvisionManagedPi([]string{"codex", "claude"}, nil, noPi, "") {
t.Fatal("provisioned Pi when disabled by enabled_runtimes")
}
if shouldProvisionManagedPi(nil, map[string]string{"pi": "/custom/pi"}, noPi, "") {
t.Fatal("provisioned Pi when runtime_paths.pi is configured")
}
if shouldProvisionManagedPi(nil, nil, externalPi, "") {
t.Fatal("provisioned Pi when external Pi was already discovered")
}
if !shouldProvisionManagedPi(nil, nil, noPi, "") {
t.Fatal("did not provision Pi when enabled and absent")
}
if !shouldProvisionManagedPi([]string{"pi"}, nil, externalPi, "/state/local-models/harness/tools/pi/1.3.0/pi") {
t.Fatal("did not keep harness directory when managed Pi was already discovered")
}
}
Loading