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(agent): let Stop reach a Run whose Computer declined a yield

jeffrey6be9ab088f0b
diff --git a/apps/computer/local-daemon/README.md b/apps/computer/local-daemon/README.md
index 9ad5e9a3..d1951cfc 100644
--- a/apps/computer/local-daemon/README.md
+++ b/apps/computer/local-daemon/README.md
@@ -285,8 +285,9 @@ for a browser sign-in when signed out.
and the Room shows the Agent working from that event. The executor can then
no longer start a fresh session, so the adapter opens `session/new` in the
same process and the terminal result carries the new session. Such a Run
- announced the old session, so the Daemon declines a scheduler yield for it
- and lets it finish. A Run that resumes after a yield ends as
+ announced the old session, so the Daemon declines one scheduler yield for it
+ and lets it finish. Agent Service sends a later Stop as its own cancel; a
+ resent yield cancels the Run. A Run that resumes after a yield ends as
`session_not_found` instead. A Run yields to
the scheduler only after native work, because its session holds no prompt
before that.
diff --git a/apps/computer/local-daemon/internal/daemon/daemon.go b/apps/computer/local-daemon/internal/daemon/daemon.go
index a9c01c37..e5df5a99 100644
--- a/apps/computer/local-daemon/internal/daemon/daemon.go
+++ b/apps/computer/local-daemon/internal/daemon/daemon.go
@@ -206,6 +206,9 @@ type activeOperation struct {
// after announcing its retained one; it then declines a scheduler
// yield, whose follow-up would resume the session that is gone.
sessionMoved atomic.Bool
+ // yieldDeclined is set once a yield was declined. An Agent Service
+ // that resends the same yield as the user's Stop then cancels the Run.
+ yieldDeclined bool
}
func New(options Options) (*Daemon, error) {
diff --git a/apps/computer/local-daemon/internal/daemon/daemon_test.go b/apps/computer/local-daemon/internal/daemon/daemon_test.go
index 9c62e239..1eeb8faa 100644
--- a/apps/computer/local-daemon/internal/daemon/daemon_test.go
+++ b/apps/computer/local-daemon/internal/daemon/daemon_test.go
@@ -435,13 +435,13 @@ func TestSchedulerYieldIsDeclinedAfterTheSessionMoved(t *testing.T) {
if ctx.Err() != nil || active.reason != "" {
t.Fatalf("the yield cancelled the Run: reason %q", active.reason)
}
- stop := operationCancelCommand(start)
- stop.Payload = mustJSON(map[string]string{"reason": "user_stop"})
- if err := d.handleOperationCancel(stop); err != nil {
+ // An Agent Service that keeps the yield command resends it as the
+ // user's Stop, which must cancel the Run.
+ if err := d.handleOperationCancel(yield); err != nil {
t.Fatal(err)
}
- if ctx.Err() == nil || active.reason != "user_stop" {
- t.Fatalf("a Stop did not cancel the Run: reason %q", active.reason)
+ if ctx.Err() == nil || active.reason != schedulerYield {
+ t.Fatalf("a resent yield did not cancel the Run: reason %q", active.reason)
}
}
diff --git a/apps/computer/local-daemon/internal/daemon/operations.go b/apps/computer/local-daemon/internal/daemon/operations.go
index 41d7a776..b80cbf64 100644
--- a/apps/computer/local-daemon/internal/daemon/operations.go
+++ b/apps/computer/local-daemon/internal/daemon/operations.go
@@ -147,9 +147,10 @@ func (d *Daemon) handleOperationCancel(command protocol.Command) error {
if payload.Reason == "" {
payload.Reason = "Operation cancelled"
}
- if payload.Reason == schedulerYield && active.sessionMoved.Load() {
+ if payload.Reason == schedulerYield && active.sessionMoved.Load() && !active.yieldDeclined {
// The Run finishes instead; Agent Service accepts a natural
// terminal after it asked for a yield.
+ active.yieldDeclined = true
slog.Info("runner.operation.yield_declined", "operation_id", command.OperationID, "reason", "session_moved")
return nil
}
diff --git a/apps/node-services/src/agent/runner-gateway/runner-operation.ts b/apps/node-services/src/agent/runner-gateway/runner-operation.ts
index 39a27187..f56704fb 100644
--- a/apps/node-services/src/agent/runner-gateway/runner-operation.ts
+++ b/apps/node-services/src/agent/runner-gateway/runner-operation.ts
@@ -662,6 +662,14 @@ export class RunnerOperationState {
if (this.#terminal) {
return;
}
+ // Any other cancellation after a yield request stops the Run. The
+ // Computer may have declined the yield, so resending it would not stop
+ // the Run, and a yield terminal would resume it.
+ if (this.#yieldRequested && reason !== "scheduler_yield") {
+ this.#yieldRequested = false;
+ this.#cancelCommand = createCancel(this.start, this.#createId(), reason);
+ return;
+ }
this.#cancelCommand ??= createCancel(this.start, this.#createId(), reason);
}
diff --git a/apps/node-services/tests/agent/runner-gateway/runner-operations-public.test.ts b/apps/node-services/tests/agent/runner-gateway/runner-operations-public.test.ts
index 4777dca7..e1e14357 100644
--- a/apps/node-services/tests/agent/runner-gateway/runner-operations-public.test.ts
+++ b/apps/node-services/tests/agent/runner-gateway/runner-operations-public.test.ts
@@ -383,6 +383,105 @@ describe("RunnerGateway", () => {
}
});
+ it("stops a Run whose Computer declined a yield", async () => {
+ const gateway = createTestRunnerGateway({
+ createId: sequenceIds(),
+ operationCancelTimeoutMs: 1_000,
+ });
+ const peer = new RecordingPeer();
+ const connection = await acceptReadyConnection(
+ gateway,
+ peer,
+ {
+ ...hello([], 1),
+ runtimes: ["antigravity"],
+ runtime_diagnostics: runtimeDiagnostics({
+ link: "api",
+ ready: ["antigravity"],
+ }),
+ },
+ { ...defaultAgentPlacement(), runtime: { type: "antigravity" } },
+ );
+ try {
+ const target = runTarget();
+ const task = await gateway.startOperation({
+ ...target,
+ request: { ...target.request, runtime: { type: "antigravity" } },
+ runKind: "task",
+ });
+ const start = peer.sent.find(
+ (command) => command.type === "operation.start",
+ )!;
+ const chat = gateway.startOperation({
+ ...target,
+ ...readyContextScope("chat"),
+ request: { ...target.request, runtime: { type: "antigravity" } },
+ runKind: "chat",
+ });
+ chat.then(
+ (interactive) => interactive.result.catch(() => undefined),
+ () => undefined,
+ );
+ await connection.receive(
+ event(start, 1, {
+ type: "operation.accepted",
+ command_id: start.command_id,
+ runner_pid: 100,
+ }),
+ );
+ await connection.receive(
+ event(start, 2, {
+ type: "operation.event",
+ event: {
+ type: "run.started",
+ turn_id: "turn-task",
+ session: { threadId: "task-session", canYield: true },
+ },
+ }),
+ );
+ await connection.receive(
+ event(start, 3, {
+ type: "operation.event",
+ event: {
+ type: "tool.called",
+ call_id: "read",
+ tool_name: "read",
+ input: {},
+ },
+ }),
+ );
+ const cancels = () =>
+ peer.sent.flatMap((command) =>
+ command.type === "cancel" ? [command] : [],
+ );
+ await vi.waitFor(() =>
+ expect(cancels().map((command) => command.reason)).toEqual([
+ "scheduler_yield",
+ ]),
+ );
+ // The Computer declined the yield and the Run goes on; Stop must
+ // reach it as a Stop, not as the same yield again.
+ const stopped = expect(task.result).rejects.toMatchObject({
+ code: "cancelled",
+ });
+ const stopping = task.cancel("user_stop");
+ await vi.waitFor(() =>
+ expect(cancels().map((command) => command.reason)).toEqual([
+ "scheduler_yield",
+ "user_stop",
+ ]),
+ );
+ expect(cancels()[1]!.command_id).not.toBe(cancels()[0]!.command_id);
+ await connection.receive(
+ event(start, 4, { type: "operation.cancelled", reason: "user_stop" }),
+ );
+ await stopping;
+ await stopped;
+ } finally {
+ connection.disconnect();
+ }
+ });
+
it("keeps a user-stopped Task's place through the cancel timeout and a later chat", async () => {
const gateway = createTestRunnerGateway({
createId: sequenceIds(),