A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Take over a same-host state.lock whose heartbeat has stopped
Fei2411c3b6c57db3c4
diff --git a/ds-bot/README.md b/ds-bot/README.mdindex 71f2967d..c594ea2c 100644--- a/ds-bot/README.md+++ b/ds-bot/README.md@@ -191,7 +191,7 @@ Files in `bot/`:|---|---|| `state.json` | The team: Bots, groups, Bot-to-Bot messages, preferences and open questions. A long message keeps only its first 2,000 characters here; all of it is in the Session it went to. || `group-lines/` | Long group lines, up to the 20,000 characters a member reads, one file per line; each is removed when its line leaves the group's latest 60 lines or the group is deleted |-| `state.lock` | The dsh that is using this data (pid and host). It is removed when that dsh exits. |+| `state.lock` | The dsh that is using this data (pid and host), its mtime bumped as a heartbeat. It is removed when that dsh exits; a leftover whose heartbeat has stopped is taken over by the next dsh to load. || `chat-index.sqlite` | Full-text index of older chat parts (`read_own_chat`); rebuilt if deleted || `usage.sqlite` | Token counts for the Usage page; rebuilt from the Session logs if deleted, losing only the calls outside the logs and the names of deleted Bots || `memory/` | Memory (see [Memory](#memory)): `bots/<id>/`, `groups/<id>/` and `team/` (`MEMORY.md` and topics), `journal.jsonl` (every change), `snapshots/` (the section each Session froze), `summaries/` (the panel's summary; safe to delete), `reviewed.json` (how far the memory review has read) and `archive/` (deleted Bots and group chats) |diff --git a/ds-bot/src/host/home-lock.js b/ds-bot/src/host/home-lock.jsindex 90ddd598..1655cff3 100644--- a/ds-bot/src/host/home-lock.js+++ b/ds-bot/src/host/home-lock.js@@ -3,9 +3,10 @@// other's changes. The first host to load takes `state.lock`; a second one only reads.//// The lock is a small JSON file (pid, host, a random token) whose mtime the holder bumps-// on a timer. On the same host, its process decides whether it is live; `staleMs` only-// covers other hosts and unreadable files. A reused pid after a crash keeps the second-// host read-only until that process ends. The token, not the pid, decides ownership: a+// on a timer. A live holder on the same host needs both: a process that still runs under+// that pid and a heartbeat fresher than `staleMs` — a crashed holder's pid may be reused+// by an unrelated process, and without the heartbeat check that process would hold the+// team read-only for as long as it runs. The token, not the pid, decides ownership: a// holder that finds another token has lost the lock.import { randomUUID } from 'node:crypto'import { readFileSync, statSync, unlinkSync, utimesSync, writeFileSync } from 'node:fs'@@ -39,7 +40,7 @@ export function homeLock(path, { staleMs = 60_000, beatMs = 15_000, host = hostnconst live = (holder) => {if (holder === undefined) return falseif (holder === null) return age() < staleMs- if (holder.host === host) return holder.pid !== pid && isAlive(holder.pid)+ if (holder.host === host) return holder.pid !== pid && isAlive(holder.pid) && age() < staleMsreturn age() < staleMs}const owns = () => read()?.token === tokendiff --git a/ds-bot/test/home-lock.test.js b/ds-bot/test/home-lock.test.jsindex bd8a98bf..e2c66a9f 100644--- a/ds-bot/test/home-lock.test.js+++ b/ds-bot/test/home-lock.test.js@@ -40,14 +40,26 @@ test('a lock whose same-host process has exited is taken over', (t) => {dead.release()})-test('an old same-host lock stays live while its process is alive', (t) => {+test('a same-host lock without a fresh heartbeat is taken over even while its pid lives', (t) => {const path = join(tempHome(t), 'state.lock')writeFileSync(path, JSON.stringify({ pid: 101, host: hostname(), token: 'old' }))const old = new Date(Date.now() - 120_000)utimesSync(path, old, old)+ // A crashed holder's pid may be reused by an unrelated process; the stopped heartbeat,+ // not the pid, says the holder is gone.const late = homeLock(path, { pid: 202, isAlive: () => true })t.after(() => late.release())- assert.equal(late.claim().token, 'old')+ assert.equal(late.claim(), undefined)+ assert.equal(late.owns(), true)+ assert.equal(JSON.parse(readFileSync(path, 'utf8')).pid, 202)+})++test('a fresh same-host lock stays live while its process is alive', (t) => {+ const path = join(tempHome(t), 'state.lock')+ writeFileSync(path, JSON.stringify({ pid: 101, host: hostname(), token: 'live' }))+ const late = homeLock(path, { pid: 202, isAlive: () => true })+ t.after(() => late.release())+ assert.equal(late.claim().token, 'live')assert.equal(late.owns(), false)})