Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(cli): a reservation left half-written by a crash can still be cleared

0xken1507863c701629
diff --git a/cli/src/lib/stoppedAgents.spec.ts b/cli/src/lib/stoppedAgents.spec.ts
index d360f6da3..b743f4481 100644
--- a/cli/src/lib/stoppedAgents.spec.ts
+++ b/cli/src/lib/stoppedAgents.spec.ts
@@ -125,6 +125,27 @@ it('reserves allocation across daemon restarts and different receipt IDs', async
expect(store.beginResume(saved.agentId)).not.toBeNull()
})
+// A reservation left behind by a crash can be half-written, and that is exactly the one the
+// age-based takeover in `resumeAgentService` has to be able to clear.
+it('reports how long a reservation has been held, and clears a half-written one', async () => {
+ const { store, saved } = await fixture()
+ const before = Date.now()
+ expect(store.resumeReservedAt(saved.agentId)).toBeNull()
+ const token = store.beginResume(saved.agentId)!
+ const held = store.resumeReservedAt(saved.agentId)
+ expect(held).not.toBeNull()
+ expect(held!).toBeGreaterThanOrEqual(before - 1000)
+
+ writeFileSync(join(directory, 'stopped-agents', `${saved.agentId}.resume`), '{"token": "trunc')
+ // A caller clearing ITS OWN reservation still has to read the marker to know it is its own.
+ expect(() => store.finishResume(saved.agentId, token)).toThrow()
+ expect(store.resumeReservedAt(saved.agentId)).not.toBeNull()
+ // A caller taking it over is not asking whose it is.
+ store.finishResume(saved.agentId)
+ expect(store.resumeReservedAt(saved.agentId)).toBeNull()
+ expect(store.beginResume(saved.agentId)).not.toBeNull()
+})
+
it('keeps readable archives discoverable beside corrupt, unsupported and mismatched records', async () => {
const { saved, store } = await fixture()
expect(store.list()).toEqual([])
diff --git a/cli/src/lib/stoppedAgents.ts b/cli/src/lib/stoppedAgents.ts
index a14db21b9..2f62ea23c 100644
--- a/cli/src/lib/stoppedAgents.ts
+++ b/cli/src/lib/stoppedAgents.ts
@@ -113,14 +113,23 @@ export class StoppedAgentStore {
try { return statSync(join(this.directory, `${agentId}.resume`)).mtimeMs } catch { return null }
}
- /** Clear only a verified outcome. Unknown allocation/readiness keeps its reservation. */
+ /**
+ * Clear only a verified outcome. Unknown allocation/readiness keeps its reservation.
+ *
+ * `token` asks for the reservation to be cleared only if it is still the caller's own, which can
+ * only be answered by parsing the marker. Without one the caller is clearing it unconditionally,
+ * so the CONTENTS are not parsed — a reservation left behind by a crash can be half-written, and
+ * refusing to clear THAT is refusing exactly the case a takeover exists for. The file is still
+ * opened the same guarded way either way, so a symlink or anything else unsafe in its place
+ * fails closed rather than being unlinked on trust.
+ */
finishResume(agentId: string, token?: string): void {
if (!SAFE_ID.test(agentId)) return
const file = join(this.directory, `${agentId}.resume`)
try {
secureStateDirectory(this.directory, false)
- const marker = JSON.parse(readPrivateStateFile(file, 1024))
- if (token !== undefined && marker.token !== token) return
+ const marker = readPrivateStateFile(file, 1024)
+ if (token !== undefined && JSON.parse(marker).token !== token) return
unlinkSync(file)
this.syncDirectory()
} catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error }