Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
feat(computer): provision Pi automatically for loaded local models
Dongdong Chen6aa24db673fd
diff --git a/apps/computer/local-daemon/README.md b/apps/computer/local-daemon/README.mdindex 63f5e43c..6f86be65 100644--- a/apps/computer/local-daemon/README.md+++ b/apps/computer/local-daemon/README.md@@ -159,11 +159,19 @@ model.with that code; the model's next accepted job, or a row that moved on,drops them. They live in memory, so after a Daemon restart the row showsonly its code.-- 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 sets `PI_CODING_AGENT_DIR` on `localmodels` tool calls+ through `OverlayEnvironment` and `workspace.PiSourceEnvironment` so loaded+ models land in the profile Pi probes and runs. When a model is loaded on a+ Computer with Pi enabled and not yet installed, `internal/localmodels`+ installs the pinned Pi runtime asynchronously in the background under+ `HarnessRoot` (passing the bundled Node binary via `--node` when available,+ matching the installer's 2 m status/plan and 20 m apply budgets, and backing+ off from 30 s up to 5 m on failure while surfacing+ `RUNTIME_DIAGNOSTIC_FAILED` after a failed attempt) before asking the Runtime+ monitor for a full discovery, so TeamHub sees Pi become ready with the loaded+ model. 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`.## Model sourcediff --git a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.goindex f3378a0e..276dd413 100644--- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go+++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main.go@@ -139,9 +139,14 @@ func run(args []string) (resultErr error) {currentProviderEnvironment := func() map[string]string {return localmodels.OverlayEnvironment(baseProviderEnvironment, localModelsStateDir)}+ var localModels *localmodels.LibrarycurrentDiscoveryOptions := func() provider.DiscoveryOptions {+ var managedPi string+ if localModels != nil {+ managedPi = localModels.PiExecutable()+ }return provider.DiscoveryOptions{- ConfiguredPaths: localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir),+ ConfiguredPaths: withManagedPiPath(localmodels.OverlayRuntimePaths(computer.RuntimePaths, localModelsStateDir), managedPi),Runtimes: computer.EnabledRuntimes,PreferredRuntime: computer.PreferredRuntime,RuntimeHomeRoot: filepath.Join(computer.StateDirectory, "runtime-homes"),@@ -152,6 +157,9 @@ func run(args []string) (resultErr error) {result, err := provider.Discover(ctx, options)if err == nil {result.Diagnostics = byokRuntimeCatalogs(result.Diagnostics, localmodels.ReadOverlay(localModelsStateDir))+ if localModels != nil {+ result.Diagnostics = withPiProvisionStatus(result.Diagnostics, localModels.PiProvisionFailed())+ }}return result, err}@@ -313,9 +321,21 @@ func run(args []string) (resultErr error) {}}jobDir := filepath.Join(computer.StateDirectory, "local-models", "jobs")- localModels := localmodels.New(localmodels.Options{+ var nodePath string+ if toolInstallation != nil {+ nodePath = toolInstallation.NodePath+ }+ piEnabled := len(computer.EnabledRuntimes) == 0 || slices.Contains(computer.EnabledRuntimes, "pi")+ _, hasPi := discovered.Installations["pi"]+ libraryHarnessDir := ""+ if piEnabled && !hasPi {+ libraryHarnessDir = localmodels.HarnessRoot(localModelsStateDir)+ }+ localModels = localmodels.New(localmodels.Options{Tool: localModelsTool, Environment: baseProviderEnvironment,- JobDirectory: jobDir,+ JobDirectory: jobDir,+ HarnessDirectory: libraryHarnessDir,+ NodePath: nodePath,// The library publishes only from Act and Refresh, which the Daemon// calls once it runs.Publish: func(snapshot wire.LocalModelsSnapshot) { localDaemon.PublishLocalModels(snapshot) },@@ -336,10 +356,6 @@ func run(args []string) (resultErr error) {}return inventory.publish(publishCtx, nextRuntimes, next.Diagnostics)}- var nodePath string- if toolInstallation != nil {- nodePath = toolInstallation.NodePath- }modelSource := localmodels.NewModelSource(localmodels.ModelSourceOptions{Tool: localModelsTool,Environment: baseProviderEnvironment,@@ -758,6 +774,35 @@ func installedTeamHubCLIBinAt(executable string) string {return directory}+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 withPiProvisionStatus(diagnostics []provider.RuntimeDiagnostic, failed bool) []provider.RuntimeDiagnostic {+ if !failed {+ return diagnostics+ }+ cloned := slices.Clone(diagnostics)+ for i := range cloned {+ if cloned[i].Type == "pi" && cloned[i].Status == "not_installed" {+ cloned[i].Status = "error"+ cloned[i].Error = &provider.DiagnosticError{+ Code: "RUNTIME_DIAGNOSTIC_FAILED",+ Message: "Automatic Pi runtime setup failed",+ }+ }+ }+ return cloned+}+func runtimeProviderEnvironment(source map[string]string, gatewayToken, codexAuthFile string) map[string]string {result := computerconfig.RuntimeEnvironment(source, gatewayToken)if codexAuthFile != "" {diff --git a/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go b/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.goindex 5630d534..89626588 100644--- a/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go+++ b/apps/computer/local-daemon/cmd/linger-computer-daemon/main_test.go@@ -262,3 +262,46 @@ func TestByokRuntimeCatalogsShowTheEnteredModel(t *testing.T) {}}}++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 TestWithPiProvisionStatusMarksNotInstalledPiAsErrorWhenFailed(t *testing.T) {+ original := []provider.RuntimeDiagnostic{+ {Type: "codex", Status: "not_installed", Error: &provider.DiagnosticError{Code: "RUNTIME_NOT_INSTALLED", Message: "missing"}},+ {Type: "pi", Status: "not_installed", Error: &provider.DiagnosticError{Code: "RUNTIME_NOT_INSTALLED", Message: "missing"}},+ {Type: "claude", Status: "ready"},+ }+ unchanged := withPiProvisionStatus(original, false)+ if unchanged[1].Status != "not_installed" {+ t.Fatalf("expected not_installed when failed=false, got %+v", unchanged[1])+ }++ marked := withPiProvisionStatus(original, true)+ if marked[1].Status != "error" || marked[1].Error == nil || marked[1].Error.Code != "RUNTIME_DIAGNOSTIC_FAILED" {+ t.Fatalf("expected Pi error status when failed=true, got %+v", marked[1])+ }+ if marked[0].Status != "not_installed" || marked[2].Status != "ready" {+ t.Fatalf("non-Pi diagnostics changed unexpectedly: %+v", marked)+ }+ if original[1].Status != "not_installed" {+ t.Fatalf("input diagnostics mutated in place: %+v", original[1])+ }++ readyPi := []provider.RuntimeDiagnostic{{Type: "pi", Status: "ready"}}+ if got := withPiProvisionStatus(readyPi, true); got[0].Status != "ready" {+ t.Fatalf("ready Pi overwritten: %+v", got[0])+ }+}diff --git a/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go b/apps/computer/local-daemon/internal/localmodels/fake_tool_test.goindex ea965530..a6f6f653 100644--- a/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go+++ b/apps/computer/local-daemon/internal/localmodels/fake_tool_test.go@@ -35,8 +35,9 @@ type fakeScript struct {}type fakeLine struct {- Line json.RawMessage `json:"line"`- DelayMS int `json:"delay_ms,omitempty"`+ Line json.RawMessage `json:"line"`+ DelayMS int `json:"delay_ms,omitempty"`+ WaitFile string `json:"wait_file,omitempty"`// CancelAware lines stop the call with CANCELLED once a cancel ran.CancelAware bool `json:"cancel_aware,omitempty"`}@@ -46,13 +47,16 @@ func runFakeTool(args []string) int {if len(args) >= 6 && strings.Join(args[:5], " ") == "-P -s -m localmodels source" {return runFakeSourceTool(state, args[5:])}+ if len(args) >= 6 && strings.Join(args[:5], " ") == "-P -s -m localmodels harness" {+ return runFakeHarness(state, args[5:])+ }if len(args) < 6 || strings.Join(args[:5], " ") != "-P -s -m localmodels library" {fmt.Fprintln(os.Stderr, "unexpected invocation", args)return 3}verb := args[5]log, _ := os.OpenFile(filepath.Join(state, "calls.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)- _, _ = fmt.Fprintf(log, "%s|PYTHONPATH=%s|cwd=%s\n", strings.Join(args[5:], " "), os.Getenv("PYTHONPATH"), workingDirectory())+ _, _ = fmt.Fprintf(log, "%s|PYTHONPATH=%s|cwd=%s|PI_CODING_AGENT_DIR=%s\n", strings.Join(args[5:], " "), os.Getenv("PYTHONPATH"), workingDirectory(), os.Getenv("PI_CODING_AGENT_DIR"))_ = log.Close()if verb == "status" {if code, err := os.ReadFile(filepath.Join(state, "status-exit")); err == nil {@@ -80,6 +84,9 @@ func runFakeTool(args []string) int {_ = os.WriteFile(filepath.Join(state, "cancelled"), nil, 0o600)}for _, line := range script.Lines {+ if line.WaitFile != "" {+ waitForGateRemoval(filepath.Join(state, line.WaitFile), 30*time.Second)+ }time.Sleep(time.Duration(line.DelayMS) * time.Millisecond)if _, err := os.Stat(filepath.Join(state, "cancelled")); line.CancelAware && err == nil {fmt.Printf(`{"schema":"localmodels.library-event/v1","event":"error","ok":false,"code":"CANCELLED","detail":"the download was cancelled","model":%q}`+"\n", args[6])@@ -93,11 +100,85 @@ func runFakeTool(args []string) int {return script.Exit}+func waitForGateRemoval(gatePath string, timeout time.Duration) {+ deadline := time.Now().Add(timeout)+ for time.Now().Before(deadline) {+ if _, err := os.Stat(gatePath); err != nil {+ return+ }+ time.Sleep(10 * time.Millisecond)+ }+}+func workingDirectory() string {directory, _ := os.Getwd()return directory}+func runFakeHarness(state string, args []string) int {+ if len(args) < 1 {+ return 3+ }+ verb := args[0]+ log, _ := os.OpenFile(filepath.Join(state, "calls.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)+ _, _ = fmt.Fprintf(log, "harness %s|PI_CODING_AGENT_DIR=%s\n", strings.Join(args, " "), os.Getenv("PI_CODING_AGENT_DIR"))+ _ = log.Close()+ argValue := func(flag string) string {+ for index := 0; index+1 < len(args); index++ {+ if args[index] == flag {+ return args[index+1]+ }+ }+ return ""+ }+ root := argValue("--root")+ installedPath := filepath.Join(root, "tools", "pi", "1.3.0", "pi")+ statusJSON := func(installed bool) string {+ if installed {+ return fmt.Sprintf(`{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":true,"executable":%q,"argvPrefix":[]}]}`, installedPath)+ }+ return `{"schema":"localmodels.harness-status/v1","runtimes":[{"runtime":"pi","installed":false,"executable":null,"argvPrefix":[]}]}`+ }+ switch verb {+ case "status":+ if _, err := os.Stat(filepath.Join(state, "harness-status-fail")); err == nil {+ return 1+ }+ _, err := os.Stat(filepath.Join(state, "harness-pi-installed"))+ fmt.Println(statusJSON(err == nil))+ return 0+ case "plan":+ waitGate := filepath.Join(state, "harness-plan-wait")+ if _, err := os.Stat(waitGate); err == nil {+ _ = os.WriteFile(filepath.Join(state, "harness-plan-entered"), []byte("1"), 0o600)+ waitForGateRemoval(waitGate, 30*time.Second)+ }+ if _, err := os.Stat(filepath.Join(state, "harness-plan-fail")); err == nil {+ return 1+ }+ outPath := argValue("--out")+ plan := fmt.Sprintf(`{"schema":"localmodels.harness-plan/v1","id":%q,"runtime":"pi","root":%q}`, strings.Repeat("a", 64), root)+ if outPath != "" {+ _ = os.WriteFile(outPath, []byte(plan), 0o600)+ }+ fmt.Println(plan)+ return 0+ case "apply":+ if _, err := os.Stat(filepath.Join(state, "harness-apply-fail")); err == nil {+ return 1+ }+ if argValue("--approve") != strings.Repeat("a", 64) {+ return 2+ }+ _ = os.WriteFile(filepath.Join(state, "harness-pi-installed"), []byte(installedPath), 0o600)+ fmt.Println(`{"kind":"stage","stage":"install","state":"running"}`)+ fmt.Println(statusJSON(true))+ return 0+ default:+ return 3+ }+}+// fakeTool lays out a Tool that runs the fake.type fakeTool struct {t *testing.Tdiff --git a/apps/computer/local-daemon/internal/localmodels/library.go b/apps/computer/local-daemon/internal/localmodels/library.goindex f2a1cd05..27c943db 100644--- a/apps/computer/local-daemon/internal/localmodels/library.go+++ b/apps/computer/local-daemon/internal/localmodels/library.go@@ -24,9 +24,12 @@ import (// Defaults for Options left zero.const (- defaultProgressInterval = 2 * time.Second- defaultStatusTimeout = 2 * time.Minute- defaultActionTimeout = 15 * time.Minute+ defaultProgressInterval = 2 * time.Second+ defaultStatusTimeout = 2 * time.Minute+ defaultActionTimeout = 15 * time.Minute+ defaultEnsurePiTimeout = 24 * time.Minute+ defaultEnsurePiRetryInterval = 30 * time.Second+ maxEnsurePiRetryInterval = 5 * time.Minute// maxRereadInterval bounds the backoff of status reads after failures.maxRereadInterval = time.MinutetailPollInterval = 100 * time.Millisecond@@ -41,6 +44,11 @@ type Options struct {Environment map[string]string// JobDirectory holds the output file of each running download or load.JobDirectory string+ // HarnessDirectory holds the on-demand Pi runtime provisioned when a+ // local model is loaded on a Computer without Pi.+ HarnessDirectory string+ // NodePath is an optional local Node executable passed via --node to harness plan.+ NodePath string// Publish receives every new snapshot, in order. It is called only from// Act and Refresh and the jobs they start.Publish func(wire.LocalModelsSnapshot)@@ -48,9 +56,11 @@ type Options struct {// changes the model list Pi reports.RuntimeChanged func()// ProgressInterval bounds how often job progress is published.- ProgressInterval time.Duration- StatusTimeout time.Duration- ActionTimeout time.Duration+ ProgressInterval time.Duration+ StatusTimeout time.Duration+ ActionTimeout time.Duration+ EnsurePiTimeout time.Duration+ EnsurePiRetryInterval time.Duration}// Library runs the localmodels tool for one Computer.@@ -72,29 +82,39 @@ type Options struct {// reads the job records localmodels keeps on disk, and polls it while a job// it did not start is running.type Library struct {- tool *Tool- environment map[string]string- jobDirectory string- publish func(wire.LocalModelsSnapshot)- runtimeChanged func()- progressInterval time.Duration- statusTimeout time.Duration- actionTimeout time.Duration- now func() time.Time- ctx context.Context- stop context.CancelFunc+ tool *Tool+ environment map[string]string+ jobDirectory string+ harnessDirectory string+ nodePath string+ publish func(wire.LocalModelsSnapshot)+ runtimeChanged func()+ progressInterval time.Duration+ statusTimeout time.Duration+ actionTimeout time.Duration+ ensurePiTimeout time.Duration+ ensurePiRetryInterval time.Duration+ now func() time.Time+ ctx context.Context+ stop context.CancelFunc// refreshMu serializes status reads so an older read never replaces a// newer one.refreshMu sync.Mutex// publishMu orders updates of current with their Publish calls.publishMu sync.Mutex+ piWg sync.WaitGroupmu sync.Mutex+ closed boolcurrent *wire.LocalModelsSnapshot// loaded identifies the loaded models of the last status read; nil// before the first one.- loaded *string- jobs map[string]*job+ loaded *string+ piExecutable string+ piEnsuring bool+ piFailures int+ piRetryAt time.Time+ jobs map[string]*job// causes holds, by model, the cause the last job's error line gave. The// tool's status keeps only the code, so each read puts the cause back on// a row that still fails with that code.@@ -116,8 +136,10 @@ func New(options Options) *Library {ctx, stop := context.WithCancel(context.Background())library := &Library{tool: options.Tool, environment: options.Environment, jobDirectory: options.JobDirectory,+ harnessDirectory: options.HarnessDirectory, nodePath: options.NodePath,publish: options.Publish, runtimeChanged: options.RuntimeChanged,progressInterval: options.ProgressInterval, statusTimeout: options.StatusTimeout, actionTimeout: options.ActionTimeout,+ ensurePiTimeout: options.EnsurePiTimeout, ensurePiRetryInterval: options.EnsurePiRetryInterval,now: time.Now, ctx: ctx, stop: stop, jobs: make(map[string]*job), causes: make(map[string]*wire.LocalModelError),}if library.progressInterval <= 0 {@@ -129,6 +151,12 @@ func New(options Options) *Library {if library.actionTimeout <= 0 {library.actionTimeout = defaultActionTimeout}+ if library.ensurePiTimeout <= 0 {+ library.ensurePiTimeout = defaultEnsurePiTimeout+ }+ if library.ensurePiRetryInterval <= 0 {+ library.ensurePiRetryInterval = defaultEnsurePiRetryInterval+ }if library.publish == nil {library.publish = func(wire.LocalModelsSnapshot) {}}@@ -146,25 +174,95 @@ func New(options Options) *Library {func (l *Library) Close() {l.stop()l.mu.Lock()+ l.closed = trueif l.progressTimer != nil {l.progressTimer.Stop()}l.mu.Unlock()+ l.piWg.Wait()+}++// PiExecutable returns the managed Pi executable provisioned for local+// models, or an empty string when none has been resolved.+func (l *Library) PiExecutable() string {+ l.mu.Lock()+ defer l.mu.Unlock()+ return l.piExecutable+}++// PiProvisionFailed reports whether automatic Pi setup has failed and not yet+// recovered, so Runtime diagnostics can surface an error instead of leaving Pi+// in not_installed indefinitely.+func (l *Library) PiProvisionFailed() bool {+ l.mu.Lock()+ defer l.mu.Unlock()+ return l.piExecutable == "" && !l.piEnsuring && l.piFailures > 0+}++func (l *Library) scheduleEnsurePi() {+ if l.harnessDirectory == "" || l.tool == nil || !l.tool.installed() {+ return+ }+ l.mu.Lock()+ now := l.now()+ if l.closed || l.ctx.Err() != nil || l.piExecutable != "" || l.piEnsuring || (!l.piRetryAt.IsZero() && now.Before(l.piRetryAt)) {+ l.mu.Unlock()+ return+ }+ l.piEnsuring = true+ l.piWg.Add(1)+ l.mu.Unlock()++ go func() {+ defer l.piWg.Done()+ ctx, cancel := context.WithTimeout(l.ctx, l.ensurePiTimeout)+ executable, err := l.tool.EnsurePi(ctx, l.environment, l.harnessDirectory, l.nodePath)+ cancel()++ l.mu.Lock()+ l.piEnsuring = false+ if l.closed || l.ctx.Err() != nil {+ l.mu.Unlock()+ return+ }+ if err == nil && executable != "" {+ l.piExecutable = executable+ l.piFailures = 0+ l.piRetryAt = time.Time{}+ l.mu.Unlock()+ slog.Info("local_models.pi_ready", "executable", executable)+ l.runtimeChanged()+ return+ }+ l.piFailures+++ firstFailure := l.piFailures == 1+ backoff := min(l.ensurePiRetryInterval<<min(l.piFailures-1, 4), maxEnsurePiRetryInterval)+ l.piRetryAt = l.now().Add(backoff)+ l.mu.Unlock()+ if err != nil {+ slog.Warn("local_models.pi_install_failed", "error", err, "retry_in", backoff)+ }+ if firstFailure {+ l.runtimeChanged()+ }+ }()}// Refresh reads the library now, publishes the snapshot and returns it.func (l *Library) Refresh(ctx context.Context) wire.LocalModelsSnapshot {l.refreshMu.Lock()- defer l.refreshMu.Unlock()snapshot := l.read(ctx)if ctx.Err() != nil {// A read cut short by its caller says nothing about the library.+ l.refreshMu.Unlock()return snapshot}changed := false+ hasLoaded := falsel.mu.Lock()if snapshot.Available {key := loadedKey(snapshot)+ hasLoaded = key != ""changed = l.loaded != nil && *l.loaded != keyl.loaded = &key}@@ -176,6 +274,10 @@ func (l *Library) Refresh(ctx context.Context) wire.LocalModelsSnapshot {l.runtimeChanged()}l.reconcile(snapshot)+ l.refreshMu.Unlock()+ if hasLoaded {+ l.scheduleEnsurePi()+ }return cloneSnapshot(snapshot)}@@ -275,42 +377,10 @@ func (l *Library) Act(ctx context.Context, action Action, deadline time.Time) (r// start or restart a long-lived server (the local model gateway) that// inherits its output and process group, and that server must survive.func (l *Library) runOnce(ctx context.Context, action Action) ([]byte, int, error) {- stdout, err := l.jobOutput()- if err != nil {- return nil, -1, err- }- defer func() { _ = os.Remove(stdout.Name()) }()- stderr, err := l.jobOutput()- if err != nil {- _ = stdout.Close()- return nil, -1, err- }- defer func() { _ = os.Remove(stderr.Name()) }()- cmd := l.tool.command(l.environment, action)- cmd.Stdout, cmd.Stderr = stdout, stderr- process.Prepare(cmd)- err = cmd.Start()- _ = stdout.Close()- _ = stderr.Close()- if err != nil {- return nil, -1, err- }- exited := make(chan error, 1)- go func() { exited <- cmd.Wait() }()- select {- case <-exited:- case <-ctx.Done():- _ = cmd.Process.Kill()- <-exited- return nil, -1, context.Cause(ctx)- }- exitCode := cmd.ProcessState.ExitCode()- output, err := readBounded(stdout.Name(), maxEventLine)- if exitCode != 0 {+ output, stderrBytes, exitCode, err := l.tool.runBuffered(ctx, l.environment, l.jobDirectory, "job", maxEventLine, l.tool.arguments(action), nil)+ if exitCode != 0 && len(stderrBytes) > 0 {// Local diagnostics only: tool prose can name local paths.- if diagnostic, readErr := readBounded(stderr.Name(), 4096); readErr == nil && len(diagnostic) > 0 {- slog.Debug("local_models.tool_stderr", "action", action.Name, "stderr", string(diagnostic))- }+ slog.Debug("local_models.tool_stderr", "action", action.Name, "stderr", string(stderrBytes))}return output, exitCode, err}diff --git a/apps/computer/local-daemon/internal/localmodels/pi_runtime.go b/apps/computer/local-daemon/internal/localmodels/pi_runtime.gonew file mode 100644index 00000000..adeaa910--- /dev/null+++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime.go@@ -0,0 +1,183 @@+package localmodels++import (+ "bytes"+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "log/slog"+ "os"+ "path/filepath"+ "regexp"+ "time"++ "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/platform/privatefs"+)++const (+ harnessStatusSchema = "localmodels.harness-status/v1"+ harnessPlanSchema = "localmodels.harness-plan/v1"+ maxHarnessOutput = 4 << 20+ harnessStatusTimeout = 2 * time.Minute+ harnessPlanTimeout = 2 * time.Minute+ harnessApplyTimeout = 20 * time.Minute+)++var hex64Pattern = regexp.MustCompile(`^[a-f0-9]{64}$`)++type harnessStatusDoc struct {+ Schema string `json:"schema"`+ Runtimes []struct {+ Runtime string `json:"runtime"`+ Installed bool `json:"installed"`+ Executable string `json:"executable"`+ ArgvPrefix []string `json:"argvPrefix"`+ } `json:"runtimes"`+}++type harnessPlanDoc struct {+ Schema string `json:"schema"`+ ID string `json:"id"`+ Runtime string `json:"runtime"`+}++// InspectPi reports the verified Pi executable managed under harnessDir, or an+// empty string when Pi is not installed there yet.+func (t Tool) InspectPi(ctx context.Context, environment map[string]string, harnessDir string) (string, error) {+ if harnessDir == "" || !t.installed() {+ return "", nil+ }+ info, err := os.Stat(harnessDir)+ if errors.Is(err, os.ErrNotExist) {+ return "", nil+ }+ if err != nil {+ return "", err+ }+ if !info.IsDir() {+ return "", errors.New("harness root is not a directory")+ }+ statusCtx, cancelStatus := context.WithTimeout(ctx, harnessStatusTimeout)+ defer cancelStatus()+ stdout, exitCode, err := t.runHarness(+ statusCtx,+ environment,+ []string{"-P", "-s", "-m", "localmodels", "harness", "status", "--root", harnessDir, "--runtime", "pi", "--json"},+ )+ if err != nil {+ return "", err+ }+ if exitCode != 0 {+ return "", fmt.Errorf("harness status exited with code %d", exitCode)+ }+ return parsePiFromHarnessStatus(stdout)+}++// EnsurePi returns the Pi executable managed under harnessDir, installing the+// pinned Pi runtime through localmodels harness when it is not installed yet.+// When nodePath is non-empty and names an existing file, it is passed via --node+// so harness does not need to download a separate Node binary.+func (t Tool) EnsurePi(ctx context.Context, environment map[string]string, harnessDir, nodePath string) (string, error) {+ if harnessDir == "" || !t.installed() {+ return "", nil+ }+ if existing, err := t.InspectPi(ctx, environment, harnessDir); err == nil && existing != "" {+ return existing, nil+ }+ if err := privatefs.EnsureDir(harnessDir); err != nil {+ return "", err+ }+ planDir, err := os.MkdirTemp(harnessDir, ".pi-plan-*")+ if err != nil {+ return "", err+ }+ defer func() { _ = os.RemoveAll(planDir) }()+ if err := privatefs.EnsureDir(planDir); err != nil {+ return "", err+ }+ planPath := filepath.Join(planDir, "plan.json")+ planArgs := []string{"-P", "-s", "-m", "localmodels", "harness", "plan", "--runtime", "pi", "--root", harnessDir}+ if nodePath != "" {+ if info, statErr := os.Stat(nodePath); statErr == nil && !info.IsDir() {+ planArgs = append(planArgs, "--node", nodePath)+ }+ }+ planArgs = append(planArgs, "--out", planPath, "--json")+ planCtx, cancelPlan := context.WithTimeout(ctx, harnessPlanTimeout)+ _, planExitCode, err := t.runHarness(planCtx, environment, planArgs)+ cancelPlan()+ if err != nil {+ return "", err+ } else if planExitCode != 0 {+ return "", fmt.Errorf("harness plan exited with code %d", planExitCode)+ }+ encodedPlan, err := os.ReadFile(planPath)+ if err != nil {+ return "", err+ }+ var plan harnessPlanDoc+ if err := json.Unmarshal(encodedPlan, &plan); err != nil {+ return "", fmt.Errorf("decode harness plan: %w", err)+ }+ if plan.Schema != harnessPlanSchema || plan.Runtime != "pi" || !hex64Pattern.MatchString(plan.ID) {+ return "", errors.New("harness plan is outside the contract")+ }+ applyCtx, cancelApply := context.WithTimeout(ctx, harnessApplyTimeout)+ stdout, exitCode, err := t.runHarness(+ applyCtx,+ environment,+ []string{"-P", "-s", "-m", "localmodels", "harness", "apply", planPath, "--approve", plan.ID, "--jsonl"},+ )+ cancelApply()+ if err != nil {+ return "", err+ }+ if exitCode != 0 {+ return "", fmt.Errorf("harness apply exited with code %d", exitCode)+ }+ if executable, parseErr := parseLastHarnessStatus(stdout); parseErr == nil && executable != "" {+ return executable, nil+ }+ return t.InspectPi(ctx, environment, harnessDir)+}++func (t Tool) runHarness(ctx context.Context, environment map[string]string, args []string) ([]byte, int, error) {+ stdout, stderrBytes, exitCode, err := t.runBuffered(ctx, environment, "", "localmodels-harness", maxHarnessOutput, args, nil)+ if exitCode != 0 && len(stderrBytes) > 0 {+ slog.Debug("local_models.harness_stderr", "stderr", string(stderrBytes))+ }+ return stdout, exitCode, err+}++func parseLastHarnessStatus(stdout []byte) (string, error) {+ lines := bytes.Split(stdout, []byte{'\n'})+ for index := len(lines) - 1; index >= 0; index-- {+ line := bytes.TrimSpace(lines[index])+ if len(line) == 0 {+ continue+ }+ return parsePiFromHarnessStatus(line)+ }+ return "", errors.New("harness output is empty")+}++func parsePiFromHarnessStatus(encoded []byte) (string, error) {+ var doc harnessStatusDoc+ if err := json.Unmarshal(bytes.TrimSpace(encoded), &doc); err != nil {+ return "", fmt.Errorf("decode harness status: %w", err)+ }+ if doc.Schema != harnessStatusSchema {+ return "", fmt.Errorf("harness status schema is %q", doc.Schema)+ }+ for _, row := range doc.Runtimes {+ if row.Runtime != "pi" {+ continue+ }+ if !row.Installed || len(row.ArgvPrefix) != 0 || !filepath.IsAbs(row.Executable) {+ return "", nil+ }+ return row.Executable, nil+ }+ return "", nil+}diff --git a/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.gonew file mode 100644index 00000000..3e06821a--- /dev/null+++ b/apps/computer/local-daemon/internal/localmodels/pi_runtime_test.go@@ -0,0 +1,362 @@+package localmodels++import (+ "context"+ "encoding/json"+ "os"+ "path/filepath"+ "slices"+ "strings"+ "sync"+ "testing"+ "time"+)++func TestToolCommandAlignsPiCodingAgentDir(t *testing.T) {+ fake := newFakeTool(t)+ home := filepath.Join(filepath.Dir(fake.state), "home")+ explicit := filepath.Join(filepath.Dir(fake.state), "custom-pi")++ cmdDefault := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": ""}, Action{Name: ActionRefresh})+ wantDefault := "PI_CODING_AGENT_DIR=" + filepath.Join(home, ".pi", "agent")+ if !slices.Contains(cmdDefault.Env, wantDefault) {+ t.Fatalf("expected %q in env %v", wantDefault, cmdDefault.Env)+ }++ cmdExplicit := fake.tool.command(map[string]string{"HOME": home, "pi_coding_agent_dir": explicit}, Action{Name: ActionRefresh})+ wantExplicit := "PI_CODING_AGENT_DIR=" + explicit+ if !slices.Contains(cmdExplicit.Env, wantExplicit) {+ t.Fatalf("expected %q in env %v", wantExplicit, cmdExplicit.Env)+ }++ gatewayPi := filepath.Join(fake.tool.StateDirectory, "gateway-home", ".pi", "agent")+ if err := os.MkdirAll(gatewayPi, 0o700); err != nil {+ t.Fatal(err)+ }+ writeModelSourceJSON(t, fake.tool.StateDirectory, map[string]any{+ "schema": modelSourceSchema,+ "mode": "local",+ "runtime": "pi",+ })+ cmdOverlay := fake.tool.command(map[string]string{"HOME": home}, Action{Name: ActionRefresh})+ wantOverlay := "PI_CODING_AGENT_DIR=" + gatewayPi+ if !slices.Contains(cmdOverlay.Env, wantOverlay) {+ t.Fatalf("expected local overlay %q in env %v", wantOverlay, cmdOverlay.Env)+ }++ // Local model-source overlay beats an explicit PI_CODING_AGENT_DIR in the+ // base environment, matching OverlayEnvironment.+ cmdExplicitAndOverlay := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": explicit}, Action{Name: ActionRefresh})+ if !slices.Contains(cmdExplicitAndOverlay.Env, wantOverlay) {+ t.Fatalf("expected local overlay %q to override explicit base env %v", wantOverlay, cmdExplicitAndOverlay.Env)+ }++ // Switching away from local strips a managed gateway-home Pi dir and falls+ // back to the source Pi profile (~/.pi/agent).+ writeModelSourceJSON(t, fake.tool.StateDirectory, map[string]any{+ "schema": modelSourceSchema,+ "mode": "subscription",+ "runtime": "claude",+ })+ cmdSubscription := fake.tool.command(map[string]string{"HOME": home, "PI_CODING_AGENT_DIR": gatewayPi}, Action{Name: ActionRefresh})+ if !slices.Contains(cmdSubscription.Env, wantDefault) {+ t.Fatalf("expected stale gateway path stripped back to %q in %v", wantDefault, cmdSubscription.Env)+ }+}++func TestLoadProvisionsPiInHarnessDirectoryWithNodePathAndNotifiesRuntimeChanged(t *testing.T) {+ fake := newFakeTool(t)+ fake.write("status.json", status(model(testModel, "idle")))+ loaded, _ := json.Marshal(status(model(testModel, "loaded")))+ fake.script("load", fakeScript{+ Lines: []fakeLine{+ {Line: progressLine(testModel, "load", 0, 1)},+ {Line: resultLine(testModel), DelayMS: 20},+ },+ StatusAfter: loaded,+ })+ harnessDir := filepath.Join(filepath.Dir(fake.state), "harness")+ home := filepath.Join(filepath.Dir(fake.state), "home")+ nodePath := filepath.Join(t.TempDir(), "node")+ if err := os.WriteFile(nodePath, []byte("#!/bin/sh\n"), 0o700); err != nil {+ t.Fatal(err)+ }+ record := &recorder{}+ var seenMu sync.Mutex+ var seenPiAtChange string+ library := New(Options{+ Tool: &fake.tool,+ Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": home, "PATH": os.Getenv("PATH")},+ JobDirectory: filepath.Join(t.TempDir(), "jobs"),+ HarnessDirectory: harnessDir,+ NodePath: nodePath,+ Publish: record.publish,+ ProgressInterval: 20 * time.Millisecond,+ })+ library.runtimeChanged = func() {+ if executable := library.PiExecutable(); executable != "" {+ seenMu.Lock()+ seenPiAtChange = executable+ seenMu.Unlock()+ }+ record.runtimeChanged()+ }+ t.Cleanup(library.Close)++ library.Refresh(context.Background())+ if library.PiExecutable() != "" {+ t.Fatalf("idle library unexpectedly provisioned Pi: %q", library.PiExecutable())+ }++ if refusal, answered := library.Act(context.Background(), Action{Name: ActionLoad, Model: testModel}, soon()); refusal != nil || !answered {+ t.Fatalf("load refusal=%+v answered=%v", refusal, answered)+ }+ want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi")+ waitFor(t, "Pi provisioned and runtimeChanged fired", func() bool {+ seenMu.Lock()+ seen := seenPiAtChange+ seenMu.Unlock()+ return record.changes() > 0 && library.PiExecutable() == want && seen == want+ })+ calls := fake.calls()+ if !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness status ") }) ||+ !slices.ContainsFunc(calls, func(c string) bool {+ return strings.HasPrefix(c, "harness plan ") && strings.Contains(c, "--node "+nodePath)+ }) ||+ !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "harness apply ") }) {+ t.Fatalf("expected harness status/plan(--node)/apply in calls: %v", calls)+ }+ wantPiDir := "PI_CODING_AGENT_DIR=" + filepath.Join(home, ".pi", "agent")+ if !slices.ContainsFunc(calls, func(c string) bool { return strings.HasPrefix(c, "load ") && strings.Contains(c, wantPiDir) }) {+ t.Fatalf("expected load call with %s in %v", wantPiDir, calls)+ }+}++func TestInitialRefreshProvisionsPiAndInspectPiFindsExisting(t *testing.T) {+ fake := newFakeTool(t)+ fake.write("status.json", status(model(testModel, "loaded")))+ harnessDir := filepath.Join(filepath.Dir(fake.state), "harness")+ record := &recorder{}+ env := map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")}+ library := New(Options{+ Tool: &fake.tool,+ Environment: env,+ JobDirectory: filepath.Join(t.TempDir(), "jobs"),+ HarnessDirectory: harnessDir,+ Publish: record.publish,+ RuntimeChanged: record.runtimeChanged,+ ProgressInterval: time.Second,+ })+ t.Cleanup(library.Close)++ library.Refresh(context.Background())+ want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi")+ waitFor(t, "initial loaded model provisions Pi asynchronously", func() bool {+ return library.PiExecutable() == want && record.changes() == 1+ })++ inspected, err := fake.tool.InspectPi(context.Background(), env, harnessDir)+ if err != nil {+ t.Fatalf("InspectPi error: %v", err)+ }+ if inspected != want {+ t.Fatalf("InspectPi=%q want=%q", inspected, want)+ }++ planCount := func() int {+ count := 0+ for _, call := range fake.calls() {+ if strings.HasPrefix(call, "harness plan ") {+ count+++ }+ }+ return count+ }+ beforePlans := planCount()++ reusedRecord := &recorder{}+ reused := New(Options{+ Tool: &fake.tool,+ Environment: env,+ JobDirectory: filepath.Join(t.TempDir(), "jobs"),+ HarnessDirectory: harnessDir,+ Publish: reusedRecord.publish,+ RuntimeChanged: reusedRecord.runtimeChanged,+ ProgressInterval: time.Second,+ })+ t.Cleanup(reused.Close)+ reused.Refresh(context.Background())+ waitFor(t, "existing Pi resolved via InspectPi without replanning", func() bool {+ return reused.PiExecutable() == want && reusedRecord.changes() == 1+ })+ if afterPlans := planCount(); afterPlans != beforePlans {+ t.Fatalf("expected InspectPi to reuse existing Pi without harness plan (%d -> %d)", beforePlans, afterPlans)+ }+}++func TestFailedPiProvisioningDoesNotFailLoadedSnapshot(t *testing.T) {+ fake := newFakeTool(t)+ fake.write("status.json", status(model(testModel, "loaded")))+ if err := os.WriteFile(filepath.Join(fake.state, "harness-plan-fail"), []byte("1"), 0o600); err != nil {+ t.Fatal(err)+ }+ harnessDir := filepath.Join(filepath.Dir(fake.state), "harness")+ library := New(Options{+ Tool: &fake.tool,+ Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "PATH": os.Getenv("PATH")},+ JobDirectory: filepath.Join(t.TempDir(), "jobs"),+ HarnessDirectory: harnessDir,+ EnsurePiRetryInterval: time.Minute,+ })+ t.Cleanup(library.Close)++ snapshot := library.Refresh(context.Background())+ if !snapshot.Available || modelState(snapshot, testModel).State != "loaded" {+ t.Fatalf("snapshot=%+v", snapshot)+ }+ waitFor(t, "failed Pi attempt recorded", func() bool {+ return library.PiProvisionFailed()+ })+ if library.PiExecutable() != "" {+ t.Fatalf("unexpected PiExecutable=%q", library.PiExecutable())+ }+}++func TestEnsurePiRunsOutsideRefreshLockAndBacksOffOnFailure(t *testing.T) {+ fake := newFakeTool(t)+ const otherModel = "other-model:Q4_K_M"+ fake.write("status.json", status(model(testModel, "loaded"), model(otherModel, "absent")))+ waitGate := filepath.Join(fake.state, "harness-plan-wait")+ if err := os.WriteFile(waitGate, []byte("1"), 0o600); err != nil {+ t.Fatal(err)+ }+ harnessDir := filepath.Join(filepath.Dir(fake.state), "harness")+ record := &recorder{}+ library := New(Options{+ Tool: &fake.tool,+ Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")},+ JobDirectory: filepath.Join(t.TempDir(), "jobs"),+ HarnessDirectory: harnessDir,+ Publish: record.publish,+ RuntimeChanged: record.runtimeChanged,+ ProgressInterval: 10 * time.Millisecond,+ })+ t.Cleanup(library.Close)++ first := library.Refresh(context.Background())+ if !first.Available || modelState(first, testModel).State != "loaded" {+ t.Fatalf("first Refresh=%+v", first)+ }+ waitFor(t, "background harness plan started", func() bool {+ _, err := os.Stat(filepath.Join(fake.state, "harness-plan-entered"))+ return err == nil+ })++ // While EnsurePi is blocked on harness plan, another Refresh and a job's+ // phaseStarted status reread must proceed without blocking on refreshMu.+ second := library.Refresh(context.Background())+ if !second.Available || modelState(second, testModel).State != "loaded" {+ t.Fatalf("second Refresh=%+v", second)+ }+ downloaded, _ := json.Marshal(status(model(testModel, "loaded"), model(otherModel, "idle")))+ downloadWait := filepath.Join(fake.state, "download-wait")+ if err := os.WriteFile(downloadWait, []byte("1"), 0o600); err != nil {+ t.Fatal(err)+ }+ fake.script("download", fakeScript{+ Lines: []fakeLine{+ {Line: progressLine(otherModel, "download", 10, 100)},+ {Line: resultLine(otherModel), WaitFile: "download-wait"},+ },+ StatusAfter: downloaded,+ })+ if refusal, answered := library.Act(context.Background(), Action{Name: ActionDownload, Model: otherModel}, soon()); refusal != nil || !answered {+ t.Fatalf("download refusal=%+v answered=%v", refusal, answered)+ }+ waitFor(t, "phaseStarted published downloading snapshot while EnsurePi waits", func() bool {+ state := modelState(record.last(), otherModel)+ return state != nil && state.State == "downloading"+ })+ if got := library.PiExecutable(); got != "" {+ t.Fatalf("EnsurePi finished before waitGate was released: %q", got)+ }++ if err := os.Remove(downloadWait); err != nil {+ t.Fatal(err)+ }+ if err := os.Remove(waitGate); err != nil {+ t.Fatal(err)+ }+ want := filepath.Join(harnessDir, "tools", "pi", "1.3.0", "pi")+ waitFor(t, "blocked EnsurePi finished after gate released", func() bool {+ return library.PiExecutable() == want && record.changes() == 1+ })+ waitFor(t, "gated download completed after gate released", func() bool {+ state := modelState(record.last(), otherModel)+ return state != nil && state.State == "idle"+ })++ // Failed installs back off instead of retrying on every Refresh, then retry+ // once the backoff window elapses.+ fake2 := newFakeTool(t)+ fake2.write("status.json", status(model(testModel, "loaded")))+ failMarker := filepath.Join(fake2.state, "harness-plan-fail")+ if err := os.WriteFile(failMarker, []byte("1"), 0o600); err != nil {+ t.Fatal(err)+ }+ harnessDir2 := filepath.Join(filepath.Dir(fake2.state), "harness")+ record2 := &recorder{}+ var clockMu sync.Mutex+ currentTime := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC)+ library2 := New(Options{+ Tool: &fake2.tool,+ Environment: map[string]string{"LOCALMODELS_FAKE_TOOL": "1", "HOME": t.TempDir(), "PATH": os.Getenv("PATH")},+ JobDirectory: filepath.Join(t.TempDir(), "jobs"),+ HarnessDirectory: harnessDir2,+ Publish: record2.publish,+ RuntimeChanged: record2.runtimeChanged,+ EnsurePiRetryInterval: 30 * time.Second,+ })+ library2.now = func() time.Time {+ clockMu.Lock()+ defer clockMu.Unlock()+ return currentTime+ }+ t.Cleanup(library2.Close)++ library2.Refresh(context.Background())+ waitFor(t, "initial Pi install failure recorded", func() bool {+ return library2.PiProvisionFailed() && record2.changes() == 1+ })+ planCalls := func() int {+ count := 0+ for _, call := range fake2.calls() {+ if strings.HasPrefix(call, "harness plan ") {+ count+++ }+ }+ return count+ }+ if got := planCalls(); got != 1 {+ t.Fatalf("expected 1 harness plan attempt, got %d (%v)", got, fake2.calls())+ }++ library2.Refresh(context.Background())+ library2.Refresh(context.Background())+ if got := planCalls(); got != 1 {+ t.Fatalf("expected backoff to suppress retries before window elapses, got %d calls", got)+ }++ if err := os.Remove(failMarker); err != nil {+ t.Fatal(err)+ }+ clockMu.Lock()+ currentTime = currentTime.Add(31 * time.Second)+ clockMu.Unlock()++ library2.Refresh(context.Background())+ want2 := filepath.Join(harnessDir2, "tools", "pi", "1.3.0", "pi")+ waitFor(t, "Pi install retried and succeeded after backoff elapsed", func() bool {+ return library2.PiExecutable() == want2 && !library2.PiProvisionFailed() && record2.changes() == 2+ })+}diff --git a/apps/computer/local-daemon/internal/localmodels/source.go b/apps/computer/local-daemon/internal/localmodels/source.goindex 00c45356..e6ba4d4c 100644--- a/apps/computer/local-daemon/internal/localmodels/source.go+++ b/apps/computer/local-daemon/internal/localmodels/source.go@@ -1,7 +1,6 @@package localmodelsimport (- "bytes""context""encoding/json""errors"@@ -15,7 +14,6 @@ import ("sync""time"- "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/process"localworkspace "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace"wire "github.com/simplexai-labs/linger/contracts-go/runnergateway")@@ -192,8 +190,8 @@ func OverlayRuntimePaths(configured map[string]string, stateDir string) map[stri}}}- if stateDir != "" {- binDir := filepath.Clean(filepath.Join(stateDir, "..", "..", "harness", "bin"))+ if harness := HarnessRoot(stateDir); harness != "" {+ binDir := filepath.Join(harness, "bin")for _, rt := range []string{"claude", "codex", "pi"} {if result[rt] != "" {continue@@ -251,8 +249,8 @@ type ModelSource struct {// NewModelSource constructs a ModelSource controller for the given localmodels Tool.func NewModelSource(options ModelSourceOptions) *ModelSource {harnessRoot := options.HarnessRoot- if harnessRoot == "" && options.Tool != nil && options.Tool.StateDirectory != "" {- harnessRoot = filepath.Clean(filepath.Join(options.Tool.StateDirectory, "..", "..", "harness"))+ if harnessRoot == "" && options.Tool != nil {+ harnessRoot = HarnessRoot(options.Tool.StateDirectory)}statusTimeout := options.StatusTimeoutif statusTimeout <= 0 {@@ -660,53 +658,10 @@ func (m *ModelSource) runTool(ctx context.Context, label string, args []string,dir := m.jobDirectoryif dir == "" {dir = os.TempDir()- } else if err := os.MkdirAll(dir, 0o700); err != nil {- return nil, -1, err}- stdout, err := os.CreateTemp(dir, "model-source-*.out")- if err != nil {- return nil, -1, err- }- defer func() { _ = os.Remove(stdout.Name()) }()-- cmd := exec.Command(m.tool.Python, args...)- cmd.Dir = m.tool.ModuleDirectory- values := make(map[string]string, len(m.environment)+4)- for key, value := range m.environment {- switch strings.ToUpper(key) {- case "PYTHONHOME", "PYTHONSTARTUP", "PYTHONINSPECT", "PYTHONPATH", "LOCALMODELS_STATE":- continue- }- values[key] = value- }- values["PYTHONPATH"] = m.tool.ModuleDirectory- values["LOCALMODELS_STATE"] = m.tool.StateDirectory- values["PYTHONNOUSERSITE"] = "1"- values["PYTHONDONTWRITEBYTECODE"] = "1"- cmd.Env = make([]string, 0, len(values))- for key, value := range values {- cmd.Env = append(cmd.Env, key+"="+value)+ output, _, exitCode, err := m.tool.runBuffered(ctx, m.environment, dir, "model-source", maxEventLine, args, stdin)+ if err != nil && ctx.Err() != nil {+ return nil, -1, errors.Join(fmt.Errorf("%s cancelled", label), err)}- if len(stdin) > 0 {- cmd.Stdin = bytes.NewReader(stdin)- }- cmd.Stdout = stdout- process.Prepare(cmd)- startErr := cmd.Start()- _ = stdout.Close()- if startErr != nil {- return nil, -1, startErr- }- exited := make(chan error, 1)- go func() { exited <- cmd.Wait() }()- select {- case <-exited:- case <-ctx.Done():- _ = cmd.Process.Kill()- <-exited- return nil, -1, errors.Join(fmt.Errorf("%s cancelled", label), context.Cause(ctx))- }- exitCode := cmd.ProcessState.ExitCode()- output, readErr := readBounded(stdout.Name(), maxEventLine)- return output, exitCode, readErr+ return output, exitCode, err}diff --git a/apps/computer/local-daemon/internal/localmodels/tool.go b/apps/computer/local-daemon/internal/localmodels/tool.goindex 3f938d9b..4c579aaa 100644--- a/apps/computer/local-daemon/internal/localmodels/tool.go+++ b/apps/computer/local-daemon/internal/localmodels/tool.go@@ -6,11 +6,26 @@package localmodelsimport (+ "bytes"+ "context""os""os/exec"+ "path/filepath""strings"++ "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/process"+ "github.com/simplexai-labs/linger/apps/computer/local-daemon/internal/workspace")+// HarnessRoot returns the shared runtime/harness directory sibling to a+// localmodels StateDirectory, or an empty string when stateDir is unset.+func HarnessRoot(stateDir string) string {+ if stateDir == "" {+ return ""+ }+ return filepath.Clean(filepath.Join(stateDir, "..", "..", "harness"))+}+// Tool is where the installer put the localmodels tool and its state, as// the Computer config records it.type Tool struct {@@ -58,31 +73,107 @@ func (t Tool) arguments(action Action) []string {}}-// command builds one tool invocation the way the installer runs it: the-// sealed kit on PYTHONPATH and as the working directory, no user site-// packages, no bytecode writes into the sealed kit.+// command builds one library invocation for the tool.func (t Tool) command(environment map[string]string, action Action) *exec.Cmd {- cmd := exec.Command(t.Python, t.arguments(action)...)+ return t.commandArgs(environment, t.arguments(action))+}++// commandArgs builds one tool invocation the way the installer runs it: the+// sealed kit on PYTHONPATH and as the working directory, no user site+// packages, no bytecode writes into the sealed kit, and PI_CODING_AGENT_DIR+// resolved through OverlayEnvironment and workspace.PiSourceEnvironment so+// localmodels reads and writes the same Pi profile the Daemon probes and runs.+func (t Tool) commandArgs(environment map[string]string, args []string) *exec.Cmd {+ cmd := exec.Command(t.Python, args...)cmd.Dir = t.ModuleDirectory- values := make(map[string]string, len(environment)+4)+ cleaned := make(map[string]string, len(environment))for key, value := range environment {switch strings.ToUpper(key) {case "PYTHONHOME", "PYTHONSTARTUP", "PYTHONINSPECT", "PYTHONPATH", "LOCALMODELS_STATE":continue+ case "PI_CODING_AGENT_DIR":+ if trimmed := strings.TrimSpace(value); trimmed != "" {+ cleaned["PI_CODING_AGENT_DIR"] = trimmed+ }+ continue}- values[key] = value+ cleaned[key] = value}- values["PYTHONPATH"] = t.ModuleDirectory- values["LOCALMODELS_STATE"] = t.StateDirectory- values["PYTHONNOUSERSITE"] = "1"- values["PYTHONDONTWRITEBYTECODE"] = "1"- cmd.Env = make([]string, 0, len(values))- for key, value := range values {+ effective := OverlayEnvironment(cleaned, t.StateDirectory)+ if piEnv, err := workspace.PiSourceEnvironment(effective); err == nil && piEnv["PI_CODING_AGENT_DIR"] != "" {+ effective["PI_CODING_AGENT_DIR"] = piEnv["PI_CODING_AGENT_DIR"]+ }+ effective["PYTHONPATH"] = t.ModuleDirectory+ effective["LOCALMODELS_STATE"] = t.StateDirectory+ effective["PYTHONNOUSERSITE"] = "1"+ effective["PYTHONDONTWRITEBYTECODE"] = "1"+ cmd.Env = make([]string, 0, len(effective))+ for key, value := range effective {cmd.Env = append(cmd.Env, key+"="+value)}return cmd}+// runBuffered executes localmodels with stdout and stderr redirected to+// temporary files (never pipes) so any background gateway child spawned by+// localmodels survives after the CLI process exits.+func (t Tool) runBuffered(+ ctx context.Context,+ environment map[string]string,+ tempDir, prefix string,+ maxBytes int64,+ args []string,+ stdin []byte,+) (stdoutBytes, stderrBytes []byte, exitCode int, err error) {+ if tempDir != "" {+ if mkErr := os.MkdirAll(tempDir, 0o700); mkErr != nil {+ return nil, nil, -1, mkErr+ }+ }+ stdout, err := os.CreateTemp(tempDir, prefix+"-*.out")+ if err != nil {+ return nil, nil, -1, err+ }+ defer func() { _ = os.Remove(stdout.Name()) }()+ stderr, err := os.CreateTemp(tempDir, prefix+"-*.err")+ if err != nil {+ _ = stdout.Close()+ return nil, nil, -1, err+ }+ defer func() { _ = os.Remove(stderr.Name()) }()++ cmd := t.commandArgs(environment, args)+ if len(stdin) > 0 {+ cmd.Stdin = bytes.NewReader(stdin)+ }+ cmd.Stdout, cmd.Stderr = stdout, stderr+ process.Prepare(cmd)+ startErr := cmd.Start()+ _ = stdout.Close()+ _ = stderr.Close()+ if startErr != nil {+ return nil, nil, -1, startErr+ }+ exited := make(chan error, 1)+ go func() { exited <- cmd.Wait() }()+ select {+ case <-exited:+ case <-ctx.Done():+ _ = cmd.Process.Kill()+ <-exited+ return nil, nil, -1, context.Cause(ctx)+ }+ code := cmd.ProcessState.ExitCode()+ out, readErr := readBounded(stdout.Name(), maxBytes)+ var errOut []byte+ if code != 0 {+ if diagnostic, diagErr := readBounded(stderr.Name(), 4096); diagErr == nil {+ errOut = diagnostic+ }+ }+ return out, errOut, code, readErr+}+// installed reports whether the configured interpreter still exists.func (t Tool) installed() bool {info, err := os.Stat(t.Python)