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): hold inputs after a tab change until the Agent reads the window
jeffreyedc9f5de2ff6
diff --git a/apps/computer/local-daemon/README.md b/apps/computer/local-daemon/README.mdindex 15d64b3f..8ea8d4e2 100644--- a/apps/computer/local-daemon/README.md+++ b/apps/computer/local-daemon/README.md@@ -376,11 +376,12 @@ the task; `rate_limited` also puts the site (its registrable domain) in aOnce a task opens a site there, the browser tools refuse that site for therest of the task, and `launch_app` refuses to open it in another browser.A change of the front tab between the task's inputs that also changed the-number of tabs refuses that one input with `COMPUTER_TAB_CHANGED`, because it-could reach another page: the Agent reads the window and goes on only when-the front tab is its page. A tab its own input opened late, after the reads-that followed the input, looks the same, so the task is not stopped, and the-next input is held to the tabs the window shows then. The Daemon reads the tab strip before an input+number of tabs refuses inputs there with `COMPUTER_TAB_CHANGED`, because they+could reach another page, until the Agent reads the window with+`get_window_state`; it goes on only when the front tab is its page. A tab its+own input opened late, after the reads that followed the input, looks the+same, so the task is not stopped, and the next input is held to the tabs the+window shows then. The Daemon reads the tab strip before an inputonly when the window's title in `list_windows` differs from the front tab'stitle after the last input, so a tab opened or closed behind the front tabdoes not stop the task: the input still reaches the task's page, and itsdiff --git a/apps/computer/local-daemon/internal/linger/localtools/agent_chrome.go b/apps/computer/local-daemon/internal/linger/localtools/agent_chrome.goindex a784b454..7f4e7151 100644--- a/apps/computer/local-daemon/internal/linger/localtools/agent_chrome.go+++ b/apps/computer/local-daemon/internal/linger/localtools/agent_chrome.go@@ -691,10 +691,13 @@ func (c *cuaSession) agentChromeWindowTitle(ctx context.Context, pid int, window// heldTabs is what a window the task opened with launch_app showed after// the task's last input there: its tab count and its front tab's title. A-// count of 0 holds nothing, as for a strip that could not be read.+// count of 0 holds nothing, as for a strip that could not be read. unread+// marks a window whose change of tabs refused an input and that the Agent+// has not read since: until it does, every input there is refused.type heldTabs struct {- count int- title string+ count int+ title string+ unread bool}// holdAgentChromeWindow makes window, in the browser that runs as pid, the@@ -733,15 +736,20 @@ func (c *cuaSession) agentChromeTabWindow(pid, window any) (int, float64, heldTa// checkAgentChromeTabs holds the task to the tabs it left: the Computer's// own Chrome keeps the tab count that followed this task's last input, so a// different count means a tab opened or closed since, and the tab in front-// may not be this task's. The input is then refused, once. A strip that cannot be read, or a task that has no count yet, is not-// held. The strip is read only when the window's title, which list_windows-// gives without a read, differs from the front tab's title after that input.+// may not be this task's. Inputs are then refused until the Agent reads the+// window. A strip that cannot be read, or a task that has no count yet, is+// not held. The strip is read only when the window's title, which+// list_windows gives without a read, differs from the front tab's title+// after that input.func (c *cuaSession) checkAgentChromeTabs(ctx context.Context, pid, window any) error {number, id, held, ok := c.agentChromeTabWindow(pid, window)want, title := held.count, held.titleif !ok || want == 0 {return nil}+ if held.unread {+ return fmt.Errorf("COMPUTER_TAB_CHANGED: the tabs of the Computer's own browser changed and this task has not read the window since, so the tab in front may not be this task's. Nothing was sent. Read the window with get_window_state: go on only if the tab in front is this task's page, otherwise report what was done and end the task: %w", errNotSent)+ }if title != "" && c.agentChromeWindowTitle(ctx, number, id) == title {// The front tab still shows what this task's last input left. A// tab someone opened behind it changes nothing this input reaches,@@ -758,13 +766,27 @@ func (c *cuaSession) checkAgentChromeTabs(ctx context.Context, pid, window any)return nil}// A tab that the task's own input opened after the reads that followed- // it looks the same as one someone else opened, so this input is- // refused, not the task: the Agent reads the window and judges the- // front tab, and the next input is held to what the window shows now.+ // it looks the same as one someone else opened, so inputs are refused,+ // not the task: once the Agent has read the window and judged the front+ // tab, its next input is held to what the window shows now.c.op.mu.Lock()- c.op.chromeHeld[id] = heldTabs{count: got, title: front}+ c.op.chromeHeld[id] = heldTabs{count: got, title: front, unread: true}+ c.op.mu.Unlock()+ return fmt.Errorf("COMPUTER_TAB_CHANGED: the Computer's own browser had %d tab(s) after this task's last input and has %d now, with %q in front; a tab may have opened late after that input, or someone else changed the tabs. Nothing was sent. Read the window with get_window_state: go on only if the tab in front is this task's page, otherwise report what was done and end the task: %w", want, got, front, errNotSent)+}++// readAgentChromeWindow takes the Agent's own read of a window it opened+// with launch_app as its look at the tab in front after a change of tabs,+// so inputs there go through again.+func (c *cuaSession) readAgentChromeWindow(pid, window any) {+ _, id, held, ok := c.agentChromeTabWindow(pid, window)+ if !ok || !held.unread {+ return+ }+ held.unread = false+ c.op.mu.Lock()+ c.op.chromeHeld[id] = heldc.op.mu.Unlock()- return fmt.Errorf("COMPUTER_TAB_CHANGED: the Computer's own browser had %d tab(s) after this task's last input and has %d now, with %q in front; a tab may have opened late after that input, or someone else changed the tabs. Nothing was sent. Read the window: go on only if the tab in front is this task's page, otherwise report what was done and end the task: %w", want, got, front, errNotSent)}// recordAgentChromeTabs keeps the tab count after this task's own input ordiff --git a/apps/computer/local-daemon/internal/linger/localtools/cua.go b/apps/computer/local-daemon/internal/linger/localtools/cua.goindex 5eb31dfd..e1da5c94 100644--- a/apps/computer/local-daemon/internal/linger/localtools/cua.go+++ b/apps/computer/local-daemon/internal/linger/localtools/cua.go@@ -946,6 +946,7 @@ func (c *cuaSession) finishInput(ctx context.Context, name string, arguments map}if err == nil && name == "get_window_state" && arguments["include_accessibility_tree"] != false {c.op.reads.observe(result)+ c.readAgentChromeWindow(arguments["pid"], arguments["window_id"])}if err == nil && inputTools[name] && ctx.Value(cuaInternalInput{}) == nil && runtime.GOOS != "darwin" {result = c.withWindowState(ctx, arguments, result)diff --git a/apps/computer/local-daemon/internal/linger/localtools/localtools_test.go b/apps/computer/local-daemon/internal/linger/localtools/localtools_test.goindex a6147e8e..e44912ca 100644--- a/apps/computer/local-daemon/internal/linger/localtools/localtools_test.go+++ b/apps/computer/local-daemon/internal/linger/localtools/localtools_test.go@@ -1063,7 +1063,7 @@ func TestLocalToolCuaMacAgentChromeOpensAURLOnlyInANewTab(t *testing.T) {}}-func TestLocalToolCuaMacAgentChromeStopsWhenSomeoneElseChangesTheTabs(t *testing.T) {+func TestLocalToolCuaMacAgentChromeRefusesInputsWhenSomeoneElseChangesTheTabs(t *testing.T) {if runtime.GOOS != "darwin" {t.Skip("the Computer's own Chrome is macOS only")}@@ -1104,8 +1104,16 @@ func TestLocalToolCuaMacAgentChromeStopsWhenSomeoneElseChangesTheTabs(t *testingif c.op.chromeHeld[9].count != 3 {t.Fatalf("tabs held after the refusal=%d, want 3", c.op.chromeHeld[9].count)}+ // The Agent's old token still names a row, so only its own read of the+ // window lets inputs through again.+ if _, err := c.call(t.Context(), "click", json.RawMessage(`{"pid":42,"window_id":9,"element_token":"s1:1"}`)); !errors.Is(err, errNotSent) || !strings.Contains(err.Error(), "COMPUTER_TAB_CHANGED") {+ t.Fatalf("input before a read err=%v, want refused", err)+ }+ if _, err := c.call(t.Context(), "get_window_state", json.RawMessage(`{"pid":42,"window_id":9}`)); err != nil {+ t.Fatal(err)+ }if _, err := c.call(t.Context(), "click", json.RawMessage(`{"pid":42,"window_id":9,"element_token":"s1:1"}`)); err != nil {- t.Fatalf("input after the refusal: %v", err)+ t.Fatalf("input after the read: %v", err)}if err := c.drain(); err != nil {t.Fatal(err)diff --git a/apps/computer/local-daemon/internal/provider/native/codex_process.go b/apps/computer/local-daemon/internal/provider/native/codex_process.goindex 0c8624ed..cfb37cd1 100644--- a/apps/computer/local-daemon/internal/provider/native/codex_process.go+++ b/apps/computer/local-daemon/internal/provider/native/codex_process.go@@ -306,13 +306,17 @@ func (p *codexProcess) stopTree() bool {p.mu.Unlock()p.reapWithGrace()gone := p.groupGone()- // A cleanup that failed because the process had gone needs no excuse- // beyond the reaped leader and the empty group: nothing is left to run- // the turn or its terminals. Who ended the process does not matter, and- // cannot be told reliably: the leader can exit just before the stop- // above closes its stdin, which then counts the exit as the stop's.- excused := cleanupErr == nil || errors.Is(cleanupErr, errCodexChildGone)- return p.confirmIf(p.isReaped() && gone && excused, logger)+ return p.confirmIf(p.isReaped() && gone && codexCleanupExcused(cleanupErr), logger)+}++// codexCleanupExcused reports whether a turn cleanup leaves the stop+// confirmable once the leader is reaped and its group is empty. A cleanup+// that failed because the process had gone needs no other excuse: nothing is+// left to run the turn or its terminals. Who ended the process does not+// matter, and cannot be told reliably: the leader can exit just before the+// stop closes its stdin, which then counts the exit as the stop's.+func codexCleanupExcused(cleanupErr error) bool {+ return cleanupErr == nil || errors.Is(cleanupErr, errCodexChildGone)}func (p *codexProcess) reapWithGrace() {diff --git a/apps/computer/local-daemon/internal/provider/native/codex_units_test.go b/apps/computer/local-daemon/internal/provider/native/codex_units_test.goindex ff86bda8..9be1179f 100644--- a/apps/computer/local-daemon/internal/provider/native/codex_units_test.go+++ b/apps/computer/local-daemon/internal/provider/native/codex_units_test.go@@ -4,6 +4,7 @@ import ("context""encoding/json""errors"+ "fmt""io""log/slog""os"@@ -493,3 +494,22 @@ func TestCodexToolOutputTokenLimitFollowsContextWindow(t *testing.T) {t.Fatal("windows that keep the same limit must keep the thread")}}++// A cleanup that failed only because the process had gone, which the stop+// may have counted as its own exit, still lets a reaped, empty group confirm+// the stop; any other cleanup failure does not.+func TestCodexCleanupExcusedWhenTheProcessHadGone(t *testing.T) {+ gone := fmt.Errorf("turn/start outcome unconfirmed: %w", errCodexChildGone)+ for _, tc := range []struct {+ err error+ want bool+ }{+ {nil, true},+ {gone, true},+ {fmt.Errorf("interrupt Codex turn: %w", context.DeadlineExceeded), false},+ } {+ if got := codexCleanupExcused(tc.err); got != tc.want {+ t.Fatalf("codexCleanupExcused(%v) = %v, want %v", tc.err, got, tc.want)+ }+ }+}