Skip to content

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.

fix(computer): recover Context workspaces no retained View claims

claude-opus-5-5-mediumf96daf3c53c2
diff --git a/apps/computer/local-daemon/internal/daemon/context_view.go b/apps/computer/local-daemon/internal/daemon/context_view.go
index aac0e98b..fdc8344e 100644
--- a/apps/computer/local-daemon/internal/daemon/context_view.go
+++ b/apps/computer/local-daemon/internal/daemon/context_view.go
@@ -58,8 +58,12 @@ func (d *Daemon) ensureContextView(ctx context.Context, command protocol.Command
ContentURL: contentURL,
FetchSkillPackage: d.skillPackageFetcher(command.TenantID, command.AgentID),
})
- slog.Info("runner.context_view.ensured", "command_id", command.CommandID, "agent_id", command.AgentID,
- "view_digest", ref.ViewDigest, "duration_ms", time.Since(startedAt).Milliseconds(), "ok", err == nil)
+ attributes := []any{"command_id", command.CommandID, "agent_id", command.AgentID,
+ "view_digest", ref.ViewDigest, "duration_ms", time.Since(startedAt).Milliseconds(), "ok", err == nil}
+ if err != nil {
+ attributes = append(attributes, "error", err.Error())
+ }
+ slog.Info("runner.context_view.ensured", attributes...)
return err
}
diff --git a/apps/computer/local-daemon/internal/linger/context/orphaned_projection.go b/apps/computer/local-daemon/internal/linger/context/orphaned_projection.go
new file mode 100644
index 00000000..fdfbcb87
--- /dev/null
+++ b/apps/computer/local-daemon/internal/linger/context/orphaned_projection.go
@@ -0,0 +1,99 @@
+package lingercontext
+
+import (
+ "errors"
+ "fmt"
+ "os"
+ "path/filepath"
+ "sort"
+)
+
+// orphanedProjectionPrefix names the directory beside a workspace that holds
+// read-only projections no retained View owned.
+const orphanedProjectionPrefix = "orphaned-projection-"
+
+// sameWorkspacePath reports whether a retained row names this workspace. One
+// directory has several spellings on Windows: letter case, an 8.3 short name,
+// or a path through a junction. Two existing paths that open the same
+// directory are the same workspace.
+func sameWorkspacePath(retained, workspace string) bool {
+ if retained == workspace {
+ return true
+ }
+ if retained == "" || workspace == "" {
+ return false
+ }
+ left, err := os.Stat(retained)
+ if err != nil {
+ return false
+ }
+ right, err := os.Stat(workspace)
+ if err != nil {
+ return false
+ }
+ return os.SameFile(left, right)
+}
+
+// setAsideOrphanedProjection moves the next View's read-only roots out of a
+// workspace that no retained row claims. A store lost or replaced while its
+// workspaces survived (a data folder moved between app versions, a SQLite
+// file left behind by a locked copy) leaves projected files nothing owns, and
+// every later View would refuse them. Nothing is deleted: the roots move to a
+// new directory beside the workspace, so a local edit stays recoverable.
+// It returns that directory, or "" when no root existed.
+func setAsideOrphanedProjection(workspace string, mappings []Mapping) (string, error) {
+ next := projectionOwnership{}
+ if err := next.addMappings(workspace, mappings); err != nil {
+ return "", err
+ }
+ roots := append([]string(nil), next.ReadOnlyRoots...)
+ // An outer root carries the roots inside it.
+ sort.Slice(roots, func(left, right int) bool { return len(roots[left]) < len(roots[right]) })
+ destination := ""
+ for _, root := range roots {
+ for _, writable := range next.WritableRoots {
+ if pathWithin(root, writable) || pathWithin(writable, root) {
+ // A writable tree is agent work; it is never moved.
+ return "", contextConflict("read-only mapping %s overlaps writable mapping %s", root, writable)
+ }
+ }
+ source := filepath.Join(workspace, root)
+ info, err := os.Lstat(source)
+ if errors.Is(err, os.ErrNotExist) {
+ continue
+ } else if err != nil {
+ return "", err
+ }
+ // A published root is read-only, and moving a directory rewrites it.
+ // A link is moved as itself; chmod would follow it.
+ if info.IsDir() && !directoryLink(info) {
+ if err := os.Chmod(source, 0o700); err != nil { //nolint:gosec // owner-only directory mode
+ return "", err
+ }
+ }
+ if destination == "" {
+ created, err := os.MkdirTemp(filepath.Dir(workspace), orphanedProjectionPrefix+"*")
+ if err != nil {
+ return "", err
+ }
+ destination = created
+ }
+ target := filepath.Join(destination, root)
+ if err := os.MkdirAll(filepath.Dir(target), 0o700); err != nil {
+ return destination, err
+ }
+ // The target is new, so this rename never replaces a directory; Windows
+ // refuses that. A file another process holds open fails the rename and
+ // the next Ensure tries again.
+ if err := os.Rename(source, target); err != nil {
+ return destination, fmt.Errorf("set aside orphaned Context projection %s: %w", root, err)
+ }
+ }
+ return destination, nil
+}
+
+// contextConflict names the path and rule behind an ErrContextConflict, so a
+// failed Run says what blocked it instead of only that something did.
+func contextConflict(format string, args ...any) error {
+ return fmt.Errorf("%w: %s", ErrContextConflict, fmt.Sprintf(format, args...))
+}
diff --git a/apps/computer/local-daemon/internal/linger/context/orphaned_projection_test.go b/apps/computer/local-daemon/internal/linger/context/orphaned_projection_test.go
new file mode 100644
index 00000000..da19924c
--- /dev/null
+++ b/apps/computer/local-daemon/internal/linger/context/orphaned_projection_test.go
@@ -0,0 +1,156 @@
+package lingercontext
+
+import (
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func orphanTestView(fixture *projectionRecoveryFixture, seed, content string) View {
+ view := testView(seed)
+ view.Mappings = []Mapping{
+ fixture.mapping("inbox_view", "inbox", "read_only", "thread.md", content),
+ {Kind: "playground", TargetPath: "playground", Access: "read_write"},
+ }
+ return view
+}
+
+func orphanedDirectories(t *testing.T, workspace string) []string {
+ t.Helper()
+ matches, err := filepath.Glob(filepath.Join(filepath.Dir(workspace), orphanedProjectionPrefix+"*"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ return matches
+}
+
+func requireContent(t *testing.T, path, expected string) {
+ t.Helper()
+ content, err := os.ReadFile(path)
+ if err != nil || string(content) != expected {
+ t.Fatalf("%s = %q, %v; want %q", path, content, err, expected)
+ }
+}
+
+// A store lost while its workspaces survived, as when a data folder moves
+// between app versions without its SQLite file, used to refuse every later
+// View of the Context with the same conflict.
+func TestLostStoreSetsAsideOrphanedProjectionAndRecovers(t *testing.T) {
+ fixture := newProjectionRecoveryFixture(t)
+ first := requireProjection(t, fixture, orphanTestView(fixture, "a", "old thread"))
+ work := filepath.Join(first.WorkspacePath, "playground", "report.md")
+ if err := os.WriteFile(work, []byte("agent work"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := fixture.store.db.Exec(`DELETE FROM context_views_v3`); err != nil {
+ t.Fatal(err)
+ }
+ fixture.restart(t)
+
+ for _, seed := range []string{"b", "c"} {
+ ready := requireProjection(t, fixture, orphanTestView(fixture, seed, "new thread "+seed))
+ if ready.WorkspacePath != first.WorkspacePath {
+ t.Fatal("recovery moved the workspace")
+ }
+ requireContent(t, filepath.Join(ready.WorkspacePath, "inbox", "thread.md"), "new thread "+seed)
+ }
+ requireContent(t, work, "agent work")
+ orphaned := orphanedDirectories(t, first.WorkspacePath)
+ if len(orphaned) != 1 {
+ t.Fatalf("orphaned projections = %v; want one, set aside only once", orphaned)
+ }
+ requireContent(t, filepath.Join(orphaned[0], "inbox", "thread.md"), "old thread")
+ if _, err := os.Stat(filepath.Join(orphaned[0], "playground")); !errors.Is(err, os.ErrNotExist) {
+ t.Fatal("a writable mapping was set aside", err)
+ }
+}
+
+// A data folder moved under a new name leaves rows naming the old path. The
+// directory those rows name is gone, so no row claims the current workspace.
+func TestRowNamingMovedDataFolderRecovers(t *testing.T) {
+ fixture := newProjectionRecoveryFixture(t)
+ first := requireProjection(t, fixture, orphanTestView(fixture, "a", "old thread"))
+ moved := filepath.Join(filepath.Dir(fixture.viewsPath), "Simplex", "views", filepath.Base(filepath.Dir(first.WorkspacePath)), "workspace")
+ if _, err := fixture.store.db.Exec(`UPDATE context_views_v3 SET workspace_path=?`, moved); err != nil {
+ t.Fatal(err)
+ }
+ fixture.restart(t)
+ ready := requireProjection(t, fixture, orphanTestView(fixture, "b", "new thread"))
+ requireContent(t, filepath.Join(ready.WorkspacePath, "inbox", "thread.md"), "new thread")
+ record, err := fixture.store.CurrentView(context.Background(), ready.View.ExecutionContextKey, ready.View.ViewDigest)
+ if err != nil || record.WorkspacePath != first.WorkspacePath {
+ t.Fatal("the row was not rewritten to the current workspace", record.WorkspacePath, err)
+ }
+}
+
+// Windows spells one directory several ways (letter case, 8.3 short names,
+// junctions). A row naming another spelling still owns its files, so nothing
+// is set aside and unchanged files are reused.
+func TestRowNamingAnotherSpellingOfWorkspaceKeepsOwnership(t *testing.T) {
+ fixture := newProjectionRecoveryFixture(t)
+ first := requireProjection(t, fixture, orphanTestView(fixture, "a", "same thread"))
+ alias := filepath.Join(filepath.Dir(fixture.viewsPath), "views-alias")
+ if err := os.Symlink(fixture.viewsPath, alias); err != nil {
+ t.Skip("directory links are unavailable:", err)
+ }
+ spelled := filepath.Join(alias, filepath.Base(filepath.Dir(first.WorkspacePath)), "workspace")
+ if _, err := fixture.store.db.Exec(`UPDATE context_views_v3 SET workspace_path=?`, spelled); err != nil {
+ t.Fatal(err)
+ }
+ fixture.restart(t)
+ view := orphanTestView(fixture, "b", "same thread")
+ ready := requireProjection(t, fixture, view)
+ requireContent(t, filepath.Join(ready.WorkspacePath, "inbox", "thread.md"), "same thread")
+ if orphaned := orphanedDirectories(t, first.WorkspacePath); len(orphaned) != 0 {
+ t.Fatalf("an owned projection was set aside: %v", orphaned)
+ }
+}
+
+// A workspace the store still claims keeps refusing unowned files, and the
+// refusal names the file.
+func TestClaimedWorkspaceConflictNamesItsCause(t *testing.T) {
+ fixture := newProjectionRecoveryFixture(t)
+ first := requireProjection(t, fixture, orphanTestView(fixture, "a", "thread"))
+ skills := filepath.Join(first.WorkspacePath, ".agents", "skills")
+ if err := os.MkdirAll(skills, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(skills, "note.txt"), []byte("local"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ next := orphanTestView(fixture, "b", "thread")
+ next.Mappings = append(next.Mappings, fixture.mapping("skills", ".agents/skills", "read_only", "note.txt", "remote"))
+ _, err := fixture.store.applyTestView(context.Background(), next, fixture.server.URL)
+ if !errors.Is(err, ErrContextConflict) || !strings.Contains(err.Error(), "note.txt") {
+ t.Fatalf("conflict = %v; want ErrContextConflict naming note.txt", err)
+ }
+ requireContent(t, filepath.Join(skills, "note.txt"), "local")
+ if orphaned := orphanedDirectories(t, first.WorkspacePath); len(orphaned) != 0 {
+ t.Fatalf("a claimed workspace was set aside: %v", orphaned)
+ }
+}
+
+func TestSameWorkspacePath(t *testing.T) {
+ root := t.TempDir()
+ existing := filepath.Join(root, "workspace")
+ if err := os.Mkdir(existing, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ for _, test := range []struct {
+ retained string
+ want bool
+ }{
+ {existing, true},
+ {existing + string(filepath.Separator) + ".", true},
+ {"", false},
+ {filepath.Join(root, "missing"), false},
+ {root, false},
+ } {
+ if got := sameWorkspacePath(test.retained, existing); got != test.want {
+ t.Errorf("sameWorkspacePath(%q) = %v; want %v", test.retained, got, test.want)
+ }
+ }
+}
diff --git a/apps/computer/local-daemon/internal/linger/context/projection.go b/apps/computer/local-daemon/internal/linger/context/projection.go
index 74148583..5dfa0f63 100644
--- a/apps/computer/local-daemon/internal/linger/context/projection.go
+++ b/apps/computer/local-daemon/internal/linger/context/projection.go
@@ -31,7 +31,7 @@ func validateProjectionUpdate(workspace string, previous projectionOwnership, co
for _, readonly := range current.ReadOnlyRoots {
for _, writable := range current.WritableRoots {
if pathWithin(readonly, writable) || pathWithin(writable, readonly) {
- return ErrContextConflict
+ return contextConflict("read-only mapping %s overlaps writable mapping %s", readonly, writable)
}
}
}
@@ -47,7 +47,7 @@ func validateProjectionUpdate(workspace string, previous projectionOwnership, co
for _, writable := range previous.WritableRoots {
if pathWithin(readonly, writable) || pathWithin(writable, readonly) {
if _, err := root.Lstat(writable); err == nil {
- return ErrContextConflict
+ return contextConflict("read-only mapping %s would replace writable files at %s", readonly, writable)
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
@@ -69,7 +69,7 @@ func validateProjectionUpdate(workspace string, previous projectionOwnership, co
return err
}
if !owned {
- return ErrContextConflict
+ return contextConflict("workspace file %s is not a file a retained View projected", relative)
}
}
return nil
@@ -225,7 +225,7 @@ func (s *ViewStore) publishProjection(workspace, staged string, previous project
return err
}
if !replace {
- return ErrContextConflict
+ return contextConflict("workspace file %s is not a file a retained View projected", workspacePath)
}
} else if !errors.Is(err, os.ErrNotExist) {
return err
@@ -322,7 +322,7 @@ func (s *ViewStore) publishProjection(workspace, staged string, previous project
return err
}
if !owned {
- return ErrContextConflict
+ return contextConflict("retired workspace file %s changed after it was projected", workspacePath)
}
if err := writableDirectories(root, filepath.Dir(relative)); err != nil {
return err
@@ -391,7 +391,7 @@ func pruneProjectionDirectories(root *os.Root, workspace string, previous, prote
return err
}
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
- return ErrContextConflict
+ return contextConflict("retired projection directory %s is not a plain directory", candidate)
}
// Darwin rmdir can silently remove dot-underscore metadata files.
// Only attempt removal after verifying the directory is empty.
diff --git a/apps/computer/local-daemon/internal/linger/context/view_store.go b/apps/computer/local-daemon/internal/linger/context/view_store.go
index 48e81496..78a614d9 100644
--- a/apps/computer/local-daemon/internal/linger/context/view_store.go
+++ b/apps/computer/local-daemon/internal/linger/context/view_store.go
@@ -38,7 +38,7 @@ var (
// refusal and lost its cause.
func viewReadError(err error) error {
if errors.Is(err, sql.ErrNoRows) {
- return ErrContextConflict
+ return contextConflict("the Context has no retained View")
}
if errors.Is(err, ErrProjectionRecoveryRequired) || errors.Is(err, ErrContextConflict) {
return err
@@ -503,14 +503,14 @@ func (s *ViewStore) apply(ctx context.Context, view View, contentURL string, tim
return ViewRecord{}, readErr
}
if readErr == nil && !sameContextOwner(current.View, view) {
- return ViewRecord{}, ErrContextConflict
+ return ViewRecord{}, contextConflict("the retained View belongs to another owner")
}
if err := s.checkSharedTaskScope(ctx, view); err != nil {
return ViewRecord{}, err
}
workspacePath := contextWorkspace(s.root, view.ExecutionContextKey)
projectionPrevious := current
- if readErr != nil || current.WorkspacePath != workspacePath {
+ if readErr != nil || !sameWorkspacePath(current.WorkspacePath, workspacePath) {
// Another directory never inherits the old row's file claims.
projectionPrevious = ViewRecord{}
}
@@ -555,7 +555,25 @@ func (s *ViewStore) apply(ctx context.Context, view View, contentURL string, tim
if err != nil {
return ViewRecord{}, err
}
- if err := validateProjectionUpdate(workspacePath, ownership, projectionPrevious.View.Mappings, view.Mappings); err != nil {
+ err = validateProjectionUpdate(workspacePath, ownership, projectionPrevious.View.Mappings, view.Mappings)
+ if errors.Is(err, ErrContextConflict) && projectionPrevious.WorkspacePath == "" {
+ // No row claims this directory, so no View owns its read-only files and
+ // every later View would refuse them the same way.
+ orphaned, setAsideErr := setAsideOrphanedProjection(workspacePath, view.Mappings)
+ if setAsideErr != nil {
+ return ViewRecord{}, setAsideErr
+ }
+ if orphaned != "" {
+ logger := s.logger
+ if logger == nil {
+ logger = slog.Default()
+ }
+ logger.Warn("runner.context_view.orphaned_projection_set_aside",
+ "agent_id", view.AgentID, "view_digest", view.ViewDigest, "cause", err.Error(), "directory", orphaned)
+ err = validateProjectionUpdate(workspacePath, ownership, projectionPrevious.View.Mappings, view.Mappings)
+ }
+ }
+ if err != nil {
return ViewRecord{}, err
}
if _, err := os.Lstat(workspacePath); err == nil {
@@ -661,7 +679,7 @@ func (s *ViewStore) ValidatedWorkspace(ctx context.Context, executionContextKey
func (s *ViewStore) ValidatedView(ctx context.Context, executionContextKey string, encoded json.RawMessage) (ViewRecord, error) {
var ref ViewRef
if err := json.Unmarshal(encoded, &ref); err != nil || executionContextKey == "" || !viewDigestPattern.MatchString(ref.ViewDigest) {
- return ViewRecord{}, ErrContextConflict
+ return ViewRecord{}, contextConflict("the Operation names no valid View")
}
return s.CurrentView(ctx, executionContextKey, ref.ViewDigest)
}
@@ -677,8 +695,11 @@ func (s *ViewStore) CurrentView(ctx context.Context, executionContextKey, viewDi
if err != nil {
return ViewRecord{}, viewReadError(err)
}
- if record.Unready || (viewDigest != "" && record.View.ViewDigest != viewDigest) {
- return ViewRecord{}, ErrContextConflict
+ if record.Unready {
+ return ViewRecord{}, contextConflict("the View is still being published")
+ }
+ if viewDigest != "" && record.View.ViewDigest != viewDigest {
+ return ViewRecord{}, contextConflict("the workspace is at another View")
}
if err := validateWorkspaceLinks(record.WorkspacePath, record.View.Mappings); err != nil {
return ViewRecord{}, err