From da20fb2fb2f7dd0f9b2d1d8b3a960bbfbbbd7a19 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Fri, 4 Dec 2026 13:54:52 +0700 Subject: [PATCH 1/2] fix(control): keep a taken message open past expires_at so its final ack lands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Symptom: a message the machine acked `taken` and was still delivering was marked `expired` ("expired before the machine took it") once its expires_at passed. The machine's final ack (e.g. `delivered` with its reply) was then answered 202 but dropped, so the reply was lost. Cause: expireQueue skipped only rows whose outcome was final. `taken` is not final (CONTROL.md §10: the machine is delivering it and a final ack follows), so the receiver overwrote it, and ack() ignores acks for rows that are already final. Fix: expireQueue leaves a taken row open for the taken pause (TAKEN_PAUSE_MS, 35 minutes from takenAt, the window that already pauses redelivery), and never longer than that past expires_at, since takenAt is the machine's clock. A taken row with no final word by then is closed `unconfirmed`, as remote.ts closes a job taken and never heard of again, not `expired`, which says the machine never took it. The pool send note counts a taken row as on its way past expires_at, and the orchestrate comment that relied on the old expiry says when it now happens. Signed-off-by: Connor Burgess --- server/control/control.test.ts | 35 ++++++++++++++++++++++++++++++++++ server/control/state.ts | 17 ++++++++++++++--- server/orchestrate/index.ts | 4 ++-- web/src/control/usePoolSend.ts | 4 +++- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/server/control/control.test.ts b/server/control/control.test.ts index cf49dc2f..392acba0 100644 --- a/server/control/control.test.ts +++ b/server/control/control.test.ts @@ -32,6 +32,7 @@ import { fileHash } from "./files.js"; import { type Control, createControl } from "./index.js"; import { parseAccounts, parseAcks, parseHello } from "./parse.js"; import { pairUrlFor } from "./routes.js"; +import { TAKEN_PAUSE_MS } from "./state.js"; import { commandStr, signCommand, signWithDevice, TestDayKey, TestDeviceKey, TestPasskey } from "./testkeys.js"; import { readKey, readProof } from "./ui.js"; @@ -709,6 +710,40 @@ describe("the queue", () => { expect(b.control.activity().find((a) => a.id === r.body.id)?.outcome).toBe("expired"); }); + it("a message the machine has taken is not expired at expires_at; its final ack is kept", async () => { + vi.useFakeTimers({ toFake: ["Date"] }); + const m = await enrolled(b, pk, "m_takenexpiry000000001"); + const r = await send(m.machine, await signedPrompt(pk, day, m.machine), { expires_in_s: 60 }); + const id = r.body.id as string; + expect((await poll(m)).body.messages!.map((x) => x.id)).toContain(id); + await ack(m, [{ id, outcome: "taken", mode: "sdk" }]); + vi.setSystemTime(Date.now() + 2 * 60_000); + expect((await poll(m)).status).toBe(204); + expect(b.control.activity().find((a) => a.id === id)).toMatchObject({ outcome: "taken" }); + await ack(m, [{ id, outcome: "delivered", reply: "Done." }]); + expect(b.control.activity().find((a) => a.id === id)).toMatchObject({ outcome: "delivered", reply: "Done." }); + + // Taken, then silence: once the taken pause is over it is closed `unconfirmed`, since the + // machine did take it. + const s = await send(m.machine, await signedPrompt(pk, day, m.machine), { expires_in_s: 60 }); + const sid = s.body.id as string; + await poll(m); + await ack(m, [{ id: sid, outcome: "taken" }]); + vi.setSystemTime(Date.now() + TAKEN_PAUSE_MS + 1000); + expect((await poll(m)).status).toBe(204); + expect(b.control.activity().find((a) => a.id === sid)).toMatchObject({ outcome: "unconfirmed" }); + + // A taken ack stamped by a machine clock far ahead holds the row no longer than the + // pause past expires_at. + const f = await send(m.machine, await signedPrompt(pk, day, m.machine), { expires_in_s: 60 }); + const fid = f.body.id as string; + await poll(m); + await ack(m, [{ id: fid, outcome: "taken", at: new Date(Date.now() + 24 * 3600_000).toISOString() }]); + vi.setSystemTime(Date.now() + 60_000 + TAKEN_PAUSE_MS); + await poll(m); + expect(b.control.activity().find((a) => a.id === fid)).toMatchObject({ outcome: "unconfirmed" }); + }); + it("off removes the machine, its token and its queue; forget does the same locally", async () => { const m = await enrolled(b, pk, "m_off00000000000000001"); await send(m.machine, await signedPrompt(pk, day, m.machine)); diff --git a/server/control/state.ts b/server/control/state.ts index 89fd3a80..406c5885 100644 --- a/server/control/state.ts +++ b/server/control/state.ts @@ -610,9 +610,20 @@ export class ControlState { // spacesheep's to settle (applyRemoteJobs): its day is its own, and it may still // hand the job out after this receiver's clock said the row was over. if (isRemoteRow(row)) continue; - row.outcome = "expired"; - row.ackedAt = new Date(now).toISOString(); - row.detail = "expired before the machine took it"; + // A `taken` row is the machine's to finish: its final ack follows (CONTROL.md §10). + // It stays open for the taken pause, and never longer than that past expires_at + // (takenAt is the machine's clock). After that it is `unconfirmed`, as remote.ts + // closes a job taken and never heard of again: the machine did take it. + if (row.takenAt) { + const taken = Date.parse(row.takenAt); + if (now - taken < TAKEN_PAUSE_MS && now - Date.parse(row.expiresAt) < TAKEN_PAUSE_MS) continue; + row.outcome = "unconfirmed"; + row.ackedAt = new Date(now).toISOString(); + } else { + row.outcome = "expired"; + row.ackedAt = new Date(now).toISOString(); + row.detail = "expired before the machine took it"; + } const m = this.machinesById.get(id); if (m && row.kind === "key.add") this.keyAddSettled(m, row); changed = true; diff --git a/server/orchestrate/index.ts b/server/orchestrate/index.ts index 455a231e..435783cf 100644 --- a/server/orchestrate/index.ts +++ b/server/orchestrate/index.ts @@ -81,8 +81,8 @@ export interface Orchestrate { /** The longest input a run takes, like a prompt's text. */ const INPUT_MAX = 20_000; /** How long a step's command stays queued: the queue's longest (a day), not its default - * hour. A step is an agent's whole task; expireQueue expires a `taken` row too, so under - * the default an implement step still running after an hour failed as `expired`. The + * hour. A step is an agent's whole task; expireQueue closes a `taken` row too (35 minutes + * on), so under the default an implement step still running after an hour failed. The * body's own `expires_in_s` is not the signer's to shorten here: the run owns the step. */ export const STEP_EXPIRES_MS = EXPIRES_MAX_MS; diff --git a/web/src/control/usePoolSend.ts b/web/src/control/usePoolSend.ts index c77234ee..fdae022d 100644 --- a/web/src/control/usePoolSend.ts +++ b/web/src/control/usePoolSend.ts @@ -42,7 +42,9 @@ export interface PoolSend { } const isLogin = (a: ControlActivity): boolean => !!a.poolAction?.startsWith("login"); -const onItsWay = (a: ControlActivity, now: number): boolean => !isFinal(a) && Date.parse(a.expiresAt) > now; +// A taken row stays open past expires_at until its final ack (state.ts expireQueue). +const onItsWay = (a: ControlActivity, now: number): boolean => + !isFinal(a) && (a.outcome === "taken" || Date.parse(a.expiresAt) > now); export function usePoolSend(m: MachineSummary): PoolSend { const control = m.control; From c17283185a531726ad0d101e3ce01fca7e6f12e2 Mon Sep 17 00:00:00 2001 From: Connor Burgess Date: Sat, 5 Dec 2026 07:19:29 +0700 Subject: [PATCH 2/2] docs: record the fix under Unreleased Signed-off-by: Connor Burgess --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5299e266..a2fa7ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to `sessionpipe-gui`. The format follows ## [Unreleased] +### Fixed + +- A message acknowledged as taken stays open past its expiry for the bounded delivery pause, allowing its final acknowledgement to be applied. + ## [0.8.18] - 2026-12-04 ### Fixed