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): report a turn that finished while it was stopped as completed

LeoXing02af7df18075
diff --git a/apps/computer/local-daemon/internal/provider/native/claude_cleanup_unix_test.go b/apps/computer/local-daemon/internal/provider/native/claude_cleanup_unix_test.go
index c1c64b2a..a91b873d 100644
--- a/apps/computer/local-daemon/internal/provider/native/claude_cleanup_unix_test.go
+++ b/apps/computer/local-daemon/internal/provider/native/claude_cleanup_unix_test.go
@@ -61,7 +61,7 @@ func TestClaudeInitRejectionCleansUpTermIgnoringTree(t *testing.T) {
func TestClaudeCallerCancelDuringTurn(t *testing.T) {
claudeLowerGrace(t, 300*time.Millisecond)
- for _, policy := range []string{"report", "normal", "ignore", "child"} {
+ for _, policy := range []string{"report", "normal", "ignore", "child", "completed"} {
t.Run(policy, func(t *testing.T) {
dir := t.TempDir()
profile := claudeFakeProfile(t, "signal", map[string]string{"CLAUDE_FAKE_DIR": dir, "CLAUDE_FAKE_POLICY": policy})
@@ -81,7 +81,11 @@ func TestClaudeCallerCancelDuringTurn(t *testing.T) {
t.Fatalf("leader %d alive when the Outcome was published", pid)
}
}
- if outcome.Status != statusAborted || outcome.Output != "" || !outcome.ExitConfirmed ||
+ wantStatus, wantOutput := statusAborted, ""
+ if policy == "completed" {
+ wantStatus, wantOutput = statusCompleted, "done"
+ }
+ if outcome.Status != wantStatus || outcome.Output != wantOutput || !outcome.ExitConfirmed ||
outcome.Diagnostics.ExitCode != nil || outcome.Diagnostics.Signal != "" || time.Since(started) > 8*time.Second {
t.Fatalf("outcome = %+v", outcome)
}
diff --git a/apps/computer/local-daemon/internal/provider/native/claude_fake_test.go b/apps/computer/local-daemon/internal/provider/native/claude_fake_test.go
index 058f068d..b76a2e04 100644
--- a/apps/computer/local-daemon/internal/provider/native/claude_fake_test.go
+++ b/apps/computer/local-daemon/internal/provider/native/claude_fake_test.go
@@ -228,6 +228,9 @@ func (f *claudeFake) signalled() int {
if policy == "report" {
f.emit(`{"type":"result","subtype":"error","is_error":true,"result":"interrupted by caller"}`)
}
+ if policy == "completed" {
+ f.emit(`{"type":"result","subtype":"success","terminal_reason":"completed","result":"done"}`)
+ }
return 0
}
diff --git a/apps/computer/local-daemon/internal/provider/native/claude_outcome.go b/apps/computer/local-daemon/internal/provider/native/claude_outcome.go
index d702d390..18def0e8 100644
--- a/apps/computer/local-daemon/internal/provider/native/claude_outcome.go
+++ b/apps/computer/local-daemon/internal/provider/native/claude_outcome.go
@@ -40,6 +40,7 @@ func (t *claudeTurn) compute(end claudeEnding) Outcome {
interrupted = interruptedStatus(t.runCtx)
}
terminal := t.terminal
+ completedBeforeStop := end.confirmed && terminal != nil && terminal.TerminalReason == "completed" && !terminal.IsError
promptTooLong := false
if terminal != nil {
out.Output = terminal.Result
@@ -68,13 +69,13 @@ func (t *claudeTurn) compute(end claudeEnding) Outcome {
switch {
case interrupted == statusTimeout:
out.Status, out.Error = statusTimeout, "Claude execution deadline exceeded"
- case interrupted == statusAborted:
+ case interrupted == statusAborted && !completedBeforeStop:
out.Status, out.Error = statusAborted, "execution cancelled"
case end.readErr != nil:
out.Status, out.Error = statusFailed, "Claude stdout read error: "+end.readErr.Error()
case t.inputErr != nil && t.observedSession == "":
out.Status, out.Error = statusFailed, "write Claude input: "+t.inputErr.Error()
- case end.exited && t.proc.exitError() != nil:
+ case end.exited && t.proc.exitError() != nil && !(completedBeforeStop && interrupted == statusAborted):
out.Status, out.Error = statusFailed, "Claude exited with error: "+t.proc.exitError().Error()
case terminal == nil:
out.Status, out.Error = statusFailed, "Claude stream ended without terminal result"
@@ -90,7 +91,7 @@ func (t *claudeTurn) compute(end claudeEnding) Outcome {
out.Status, out.Error = statusFailed, "Claude native Session cleanup was not confirmed"
}
if failure == "" && !end.parked && errors.Is(t.callerCtx.Err(), context.Canceled) &&
- (terminal == nil || t.terminalAfterCancel) && !promptTooLong {
+ (terminal == nil || t.terminalAfterCancel) && !promptTooLong && !completedBeforeStop {
out.Status, out.Error = statusAborted, "execution cancelled"
}
if out.Status == statusCompleted && t.spec.ResumeRequired && t.spec.ResumeID != "" && t.observedSession != t.spec.ResumeID {
diff --git a/apps/computer/local-daemon/internal/provider/native/codex_cleanup.go b/apps/computer/local-daemon/internal/provider/native/codex_cleanup.go
index 0c0c9a1b..5abc0b14 100644
--- a/apps/computer/local-daemon/internal/provider/native/codex_cleanup.go
+++ b/apps/computer/local-daemon/internal/provider/native/codex_cleanup.go
@@ -126,10 +126,12 @@ func (p *codexProcess) cleanupTurn() error {
}
thread, turn := snapshot.threadID, snapshot.turnID
if !snapshot.completed {
- if _, _, err := p.transport.call(ctx, "turn/interrupt", map[string]any{"threadId": thread, "turnId": turn}, 0, nil); err != nil {
- return fmt.Errorf("interrupt Codex turn: %w", err)
- }
+ // The turn can finish between the snapshot and Codex handling the interrupt.
+ _, _, interruptErr := p.transport.call(ctx, "turn/interrupt", map[string]any{"threadId": thread, "turnId": turn}, 0, nil)
if _, err := p.waitTracker(ctx, func(t codexTurnTracker) bool { return t.completed }); err != nil {
+ if interruptErr != nil {
+ return fmt.Errorf("interrupt Codex turn: %w", interruptErr)
+ }
return fmt.Errorf("interrupted turn did not settle: %w", err)
}
}
diff --git a/apps/computer/local-daemon/internal/provider/native/codex_fake_test.go b/apps/computer/local-daemon/internal/provider/native/codex_fake_test.go
index 8bfd1767..2cbad567 100644
--- a/apps/computer/local-daemon/internal/provider/native/codex_fake_test.go
+++ b/apps/computer/local-daemon/internal/provider/native/codex_fake_test.go
@@ -215,6 +215,11 @@ func (s *codexFakeServer) handle(line []byte) (int, bool) {
case "turn/steer":
return s.steer(request.ID, request.Params)
case "turn/interrupt":
+ if s.modes["complete_on_interrupt"] {
+ s.reject(request.ID, -32600, "no active turn to interrupt")
+ s.send(map[string]any{"method": "turn/completed", "params": map[string]any{"threadId": s.thread, "turn": map[string]any{"id": s.current, "status": "completed"}}})
+ return 0, false
+ }
s.result(request.ID, map[string]any{})
s.send(map[string]any{"method": "turn/completed", "params": map[string]any{"threadId": s.thread, "turn": map[string]any{"id": s.current, "status": "interrupted"}}})
case "thread/backgroundTerminals/list":
diff --git a/apps/computer/local-daemon/internal/provider/native/codex_runtime.go b/apps/computer/local-daemon/internal/provider/native/codex_runtime.go
index ded1fec0..8f35c5bc 100644
--- a/apps/computer/local-daemon/internal/provider/native/codex_runtime.go
+++ b/apps/computer/local-daemon/internal/provider/native/codex_runtime.go
@@ -248,8 +248,6 @@ func (op *codexOperation) finish(ctx context.Context, failure codexFailure) Outc
op.mu.Lock()
out.TurnID = op.turnID
out.NativeResume = op.nativeResume
- out.Diagnostics = op.turnDiag
- out.FailureCode = op.failureCode
fingerprint := op.boundFingerprint
setupMethod, setupSent := op.setupMethod, op.setupSentAt
op.mu.Unlock()
@@ -289,6 +287,14 @@ func (op *codexOperation) finish(ctx context.Context, failure codexFailure) Outc
if !parked {
confirmed = codexResidents.retire(proc)
out.ExitConfirmed, out.ExitUnconfirmed = confirmed, !confirmed
+ if confirmed && out.Status == statusAborted {
+ select {
+ case <-op.terminal:
+ terminal := op.terminalFailure()
+ out.Status, out.Error = terminal.status, terminal.err
+ default:
+ }
+ }
code = "reaped"
if !confirmed {
code = "unconfirmed"
@@ -300,6 +306,9 @@ func (op *codexOperation) finish(ctx context.Context, failure codexFailure) Outc
}
}
}
+ op.mu.Lock()
+ out.Diagnostics, out.FailureCode = op.turnDiag, op.failureCode
+ op.mu.Unlock()
recordTiming(op.logger, time.Now(), timingCodexCleanupComplete, "thread_id", out.SessionID, "turn_id", out.TurnID, "code", code)
if failure.process {
out.Error = withStderr(out.Error, "codex", proc.stderr.Tail())
diff --git a/apps/computer/local-daemon/internal/provider/native/codex_runtime_test.go b/apps/computer/local-daemon/internal/provider/native/codex_runtime_test.go
index d99b4a74..69443752 100644
--- a/apps/computer/local-daemon/internal/provider/native/codex_runtime_test.go
+++ b/apps/computer/local-daemon/internal/provider/native/codex_runtime_test.go
@@ -135,6 +135,27 @@ func TestCodexTurnOwnershipAndDedupe(t *testing.T) {
}
}
+func TestCodexCompletionRacingInterrupt(t *testing.T) {
+ turn := codexFakeTurn{Items: []map[string]any{codexAgentMessage("m1", "done", "final_answer")}, Stall: true}
+ fake := newCodexFake(t, []codexFakeTurn{turn}, map[string]string{"FAKE_MODES": "complete_on_interrupt"})
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+ run, err := fake.runtime(nil).Start(ctx, "go", fake.spec())
+ if err != nil {
+ t.Fatal(err)
+ }
+ for event := range run.Events {
+ if event.Kind == EventText {
+ cancel()
+ break
+ }
+ }
+ out, _ := codexCollect(t, run)
+ if out.Status != statusCompleted || out.Output != "done" || !out.ExitConfirmed || out.ExitUnconfirmed || fake.count("turn/interrupt") != 1 {
+ t.Fatalf("outcome = %+v, methods = %v", out, fake.methods())
+ }
+}
+
func TestCodexFirstProgressTimeoutInterrupts(t *testing.T) {
fake := newCodexFake(t, []codexFakeTurn{{Stall: true}}, nil)
spec := fake.spec()
diff --git a/apps/computer/local-daemon/internal/provider/native/pi_fake_test.go b/apps/computer/local-daemon/internal/provider/native/pi_fake_test.go
index f8ec96dd..5ea0061e 100644
--- a/apps/computer/local-daemon/internal/provider/native/pi_fake_test.go
+++ b/apps/computer/local-daemon/internal/provider/native/pi_fake_test.go
@@ -175,6 +175,11 @@ func (f *piFakeProcess) handle(id, kind, message string) int {
if !f.cleared {
return 24
}
+ if f.mode == "complete-on-abort" {
+ f.finish()
+ f.reply(id, kind, nil)
+ return -1
+ }
f.send(map[string]any{"type": "agent_settled", "aborted": true})
f.reply(id, kind, nil)
default:
@@ -185,7 +190,7 @@ func (f *piFakeProcess) handle(id, kind, message string) int {
func (f *piFakeProcess) act() int {
switch f.mode {
- case "steer", "steer-race", "steer-unconsumed":
+ case "steer", "steer-race", "steer-unconsumed", "complete-on-abort":
case "retry":
f.assistantEnd("", "error", "retryable")
f.send(map[string]any{"type": "agent_end"})
diff --git a/apps/computer/local-daemon/internal/provider/native/pi_finish.go b/apps/computer/local-daemon/internal/provider/native/pi_finish.go
index dac22f5a..dce29c69 100644
--- a/apps/computer/local-daemon/internal/provider/native/pi_finish.go
+++ b/apps/computer/local-daemon/internal/provider/native/pi_finish.go
@@ -137,7 +137,16 @@ func (t *piTurn) finish(began time.Time, status, failure string, interrupted boo
t.abortQueue()
}
confirmed := p.stop()
- if status == statusCompleted {
+ if confirmed && status == piStatusCancelled {
+ // A natural settle can arrive while the abort request is in flight.
+ select {
+ case <-t.settled:
+ if settled, reason := t.settledStatus(); settled != statusAborted {
+ status, failure = settled, reason
+ }
+ default:
+ }
+ } else if status == statusCompleted {
status, failure = t.settledStatus()
}
outcome := t.outcomeFor(began, status, failure)
diff --git a/apps/computer/local-daemon/internal/provider/native/pi_runtime_test.go b/apps/computer/local-daemon/internal/provider/native/pi_runtime_test.go
index 7a9d0b80..334e39f7 100644
--- a/apps/computer/local-daemon/internal/provider/native/pi_runtime_test.go
+++ b/apps/computer/local-daemon/internal/provider/native/pi_runtime_test.go
@@ -267,6 +267,19 @@ func TestPiSteerCompletionRace(t *testing.T) {
}
}
+func TestPiCompletionRacingAbort(t *testing.T) {
+ h := piNewHarness(t, "complete-on-abort", nil)
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+ run := h.start(ctx, "go", RunSpec{})
+ piWaitRunning(t, run)
+ cancel()
+ _, out := piCollect(t, run)
+ if out.Status != statusCompleted || out.Output != "done" || !out.ExitConfirmed || out.ExitUnconfirmed {
+ t.Fatalf("outcome = %+v", out)
+ }
+}
+
func TestPiCancelClearsQueueAndReapsChildren(t *testing.T) {
pidFile := filepath.Join(t.TempDir(), "child.pid")
commands := filepath.Join(t.TempDir(), "commands")
diff --git a/docs/model-scheduling.md b/docs/model-scheduling.md
index 3fcd4ea6..e26ec14d 100644
--- a/docs/model-scheduling.md
+++ b/docs/model-scheduling.md
@@ -46,7 +46,8 @@ and a chat can swap out a Task or a routine. A Run can yield once the Runtime
has a resumable native session. On Codex, the Run must also have reached its
first model response or tool event so that its original input is in its
history. A pending tool approval or human interaction is answered before
-yielding. The waiting work starts after the Computer confirms cleanup.
+yielding. The waiting work starts after the Computer confirms cleanup. A Run
+that finishes on its own while it is asked to yield completes normally.
The yielded Run keeps its ID, running status and claim while it waits, and
continues in the same session when admitted again. A user stop still ends it.
If a yielded Run's cleanup cannot be confirmed, the Computer refuses further