Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

fix(server): expire a host cancel nobody collects (#734)

ANIRUDDHA ADAK63f99cf3118d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3322d8a..242ecf1e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,11 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+- A cancellation the native host never collected is now dropped instead of being reported as
+ pending forever. Every timed-out, stopped or revoked host operation queues a cancel for the desktop
+ worker, and only that worker ever removed it: a worker that stopped polling left the entry in
+ memory for the life of the process, so the Host access panel showed an operation that could never
+ finish. A desktop that reconnects within the operation timeout is still told to stop.
- A Bot's shell now honours a Stop that landed before the command was spawned, not only one that
arrives afterwards. A person who pressed Stop in the window between the request reaching the
computer and the command starting got no answer until that command finished on its own.
diff --git a/server/src/host-access/broker.ts b/server/src/host-access/broker.ts
index fabb4a6f..76a0f0c3 100644
--- a/server/src/host-access/broker.ts
+++ b/server/src/host-access/broker.ts
@@ -80,18 +80,30 @@ export function createHostAccessBroker(
actorId: state.actorId,
...(state.grantId ? { grantId: state.grantId } : {}),
};
- operations.set(cancelOperation.operationId, {
+ const cancelState: OperationState = {
operation: cancelOperation,
actorId: state.actorId,
botId: state.botId,
grantId: state.grantId,
leasedUntil: null,
- expiresAt: null,
+ expiresAt: now() + operationTtlMs,
expiryTimer: null,
resolve: () => {},
reject: () => {},
settled: false,
- });
+ };
+ // A cancel is addressed to a desktop that may never come back for it, and this map has no
+ // other bound on it: nothing but `resolveDesktopOperation` ever removed one, so a worker that
+ // did not return left the entry here for the life of the process, growing `operations` by one
+ // per abandoned operation and leaving `statusFor` reporting it to the person as pending
+ // forever. Expiring on the same clock as any other queued operation keeps the window open
+ // long enough for a desktop that reconnects to still be told to stop, and gives up on one that
+ // does not. Deleted rather than failed, because a cancel has nobody left to reject.
+ cancelState.expiryTimer = setTimeout(() => {
+ operations.delete(cancelOperation.operationId);
+ }, operationTtlMs);
+ unrefTimer(cancelState.expiryTimer);
+ operations.set(cancelOperation.operationId, cancelState);
}
function failOperation(state: OperationState, reason: string) {
diff --git a/server/tests/host-access-broker.test.ts b/server/tests/host-access-broker.test.ts
index 164273ec..2c0bfb4d 100644
--- a/server/tests/host-access-broker.test.ts
+++ b/server/tests/host-access-broker.test.ts
@@ -358,4 +358,44 @@ describe("host access broker", () => {
actorId: "user-a",
});
});
+
+ test("a cancel nobody collects does not stay pending for the life of the process", async () => {
+ const broker = createHostAccessBroker(Date.now, {
+ // Long enough that the lease is not what ends the operation, so this is the operation's
+ // own timeout queuing the cancel.
+ desktopLeaseMs: 60_000,
+ operationTtlMs: 40,
+ });
+ broker.rememberGrant({
+ id: "grant-1",
+ botId: "bot-a",
+ actorId: "user-a",
+ displayName: "Project",
+ revoked: false,
+ });
+
+ const running = broker.callHost({
+ kind: "read_file",
+ botId: "bot-a",
+ actorId: "user-a",
+ grantId: "grant-1",
+ relativePath: "notes.txt",
+ });
+ expect(broker.nextDesktopOperation()?.operations[0]).toMatchObject({
+ kind: "read_file",
+ });
+
+ // It runs out its own time, which queues a cancel for the desktop that has to stop
+ // working on it.
+ await Promise.allSettled([running]);
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ expect(broker.statusFor("user-a").pending).toHaveLength(1);
+ expect(broker.statusFor("user-a").pending[0]?.kind).toBe("cancel");
+
+ // That desktop never comes back for it. The cancel cannot outlive the chance to deliver
+ // it: nothing else ever removes this entry, so `operations` grew by one per abandoned
+ // operation and `statusFor` kept reporting it to the person as pending.
+ await new Promise((resolve) => setTimeout(resolve, 150));
+ expect(broker.statusFor("user-a").pending).toEqual([]);
+ }, 10_000);
});