Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Adopt on reclaim only when this host held the lock and the file changed

Fei2411b5aca6851589
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.js
index 1750a32f..313babd6 100644
--- a/ds-bot/src/host/store.js
+++ b/ds-bot/src/host/store.js
@@ -34,9 +34,6 @@ export function install(rt) {
let stopped = false
// mtime and size of the state.json this host last read or wrote.
let seen = ''
- // Bumped each time this host takes the lock, so a save queued before a loss
- // does not write across a reclaim it could not see.
- let lockEpoch = 0
const stamp = () => {
try {
const { mtimeMs, size } = statSync(statePath)
@@ -51,7 +48,6 @@ export function install(rt) {
try {
mkdirSync(home, { recursive: true })
holder = lock.claim()
- if (holder === undefined) lockEpoch += 1
} catch (error) {
// An unusable lock (a read-only or odd file system) must not stop the team; a
// save that then cannot write still rejects.
@@ -251,15 +247,14 @@ export function install(rt) {
for (const room of stale) void rt.retitleRoom(room)
})()
- // The lock is back in this host's hands, whether a refresh or a save retook it:
- // the file on disk is adopted, after this host's own state is put aside when the
- // file is not what it last saw. The step is queued behind `writing` — and behind
- // any adopt already queued — so no save or second refresh slips between the
- // reclaim and the adopt.
+ // An outside edit of state.json while this host held the lock, found at a
+ // reclaim or by a queued write that saw the file change: this host's in-memory
+ // state goes aside first, then the file on disk is adopted. Queued behind
+ // `writing` — and behind any adopt already queued — so no save or second
+ // refresh slips between finding the change and the adopt.
let adopting
const adoptAfterReclaim = () => {
const run = (adopting ?? writing).then(async () => {
- warnedReadOnly = false
if (stamp() !== seen) {
const aside = `${statePath}.replaced-${Date.now()}`
try {
@@ -277,6 +272,16 @@ export function install(rt) {
return tracked
}
+ // The lock was this host's and is its own again. An adopt still queued is
+ // awaited; otherwise the file is compared with what this host last saw:
+ // unchanged means nothing external happened and the in-memory state stays,
+ // changed means an outside edit is adopted the usual way.
+ const reclaimHeld = () => {
+ warnedReadOnly = false
+ if (adopting !== undefined) return adopting
+ return stamp() === seen ? undefined : adoptAfterReclaim()
+ }
+
// A reading host follows the file and takes over once the other host has gone.
const refresh = async () => {
await load()
@@ -285,7 +290,8 @@ export function install(rt) {
claimLock()
if (holder === undefined) {
warn('ds-bot: the other dsh has closed; this one now saves the Bot team')
- await adoptAfterReclaim()
+ warnedReadOnly = false
+ adopt(readFileState())
return
}
} else if (!lockBroken && !lock.owns()) {
@@ -295,9 +301,7 @@ export function install(rt) {
claimLock()
if (lockBroken) return
if (holder === undefined) {
- // The lock was free and is this host's again. A hand-over and an outside
- // edit look the same here, so the file on disk is adopted either way.
- await adoptAfterReclaim()
+ await reclaimHeld()
return
}
warn('ds-bot: %s', readOnly())
@@ -318,12 +322,13 @@ export function install(rt) {
if (lockBroken) return true
if (holder === undefined && lock.owns()) return true
// Lost the lock: retake it when nobody else holds it (its file was deleted), else
- // another host took it over and this one stops writing. A retake adopts the file
- // first, and the save queues behind that adopt on `writing`.
+ // another host took it over and this one stops writing.
if (holder === undefined) {
claimLock()
if (holder === undefined) {
- void adoptAfterReclaim().catch(() => {})
+ // A changed file is adopted first; the save queues behind that adopt.
+ const pending = reclaimHeld()
+ if (pending !== undefined) void pending.catch(() => {})
return true
}
}
@@ -335,10 +340,16 @@ export function install(rt) {
indexSessions()
// A reading host keeps the file's revision, so its window does not skip the next change.
if (!writable()) return required ? Promise.reject(new Error(`This host cannot save the Bot team: ${readOnly()}`)) : writing
- const epoch = lockEpoch
const run = writing.then(async () => {
- // The snapshot is taken once the queued write runs, so a save made while an
- // adopt was pending writes the adopted state, not the stale one it had.
+ // Re-check writability once the queued write runs: an adopt queued before
+ // it may have made this host read-only (e.g. a file a newer DS Bot wrote).
+ if (stuck !== undefined || holder !== undefined) {
+ if (required) throw new Error(`This host cannot save the Bot team: ${readOnly()}`)
+ warnReadOnly()
+ return
+ }
+ // The snapshot is taken here, so a save made while an adopt was pending
+ // writes the adopted state, not the stale one it had.
state.revision += 1
state.exchanges = state.exchanges.slice(-MAX_EXCHANGES)
const snapshot = JSON.stringify(state)
@@ -347,10 +358,13 @@ export function install(rt) {
await mkdir(home, { recursive: true })
const ensureLock = () => {
if (lockBroken) return
- // A different epoch means this host lost and retook the lock since the
- // save was queued: the write must not slip across that reclaim.
- if (!lock.owns() || lockEpoch !== epoch) {
- const error = new Error('ds-bot lost the state lock to another dsh')
+ const owned = lock.owns()
+ // An outside edit under this host's lock goes through the adopt step
+ // instead of being written over; a removed file is just written again.
+ const changed = owned && stamp() !== '' && stamp() !== seen
+ if (changed) void adoptAfterReclaim().catch(() => {})
+ if (!owned || changed) {
+ const error = new Error(changed ? 'ds-bot: the Bot team file changed under this host\'s lock' : 'ds-bot lost the state lock to another dsh')
error.code = 'ELOCKLOST'
throw error
}
diff --git a/ds-bot/test/store.test.js b/ds-bot/test/store.test.js
index 64ee4cea..0dca678a 100644
--- a/ds-bot/test/store.test.js
+++ b/ds-bot/test/store.test.js
@@ -285,6 +285,58 @@ test('a save cannot slip between a reclaim and the adopt it waits on', async (t)
}
})
+test('a save writes the unsaved change when the lock went away but the file did not', async (t) => {
+ const { rt, home, statePath } = runtime(t)
+ await rt.load()
+ await rt.save()
+ let entered
+ let unblock
+ const atMkdir = new Promise(resolve => { entered = resolve })
+ const gate = new Promise(resolve => { unblock = resolve })
+ const restore = patch('mkdir', originalMkdir => async (path, ...args) => {
+ if (path === home) {
+ entered()
+ await gate
+ }
+ return originalMkdir(path, ...args)
+ })
+ try {
+ rt.state.bots.b1 = { id: 'b1', name: 'Renamed' }
+ const saving = rt.save()
+ await atMkdir
+ // The lock file is deleted while the save waits, but the file is unchanged:
+ // the reclaim keeps the in-memory state and the save writes the change.
+ fs.rmSync(join(home, 'state.lock'))
+ const refreshing = rt.refresh()
+ unblock()
+ await saving
+ await refreshing
+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).bots.b1.name, 'Renamed')
+ assert.deepEqual(fs.readdirSync(home).filter(name => name.startsWith('state.json.replaced-')), [])
+ } finally {
+ unblock()
+ restore()
+ }
+})
+
+test('a save writes nothing after adopting a file from a newer DS Bot', async (t) => {
+ const { rt, home, statePath } = runtime(t)
+ await rt.load()
+ rt.state.prefs.marker = 'ours'
+ await rt.save()
+ const future = JSON.stringify({ version: 999, revision: 77, prefs: { marker: 'future' } })
+ fs.writeFileSync(statePath, future)
+ fs.rmSync(join(home, 'state.lock'))
+ rt.state.prefs.after = 'not saved'
+ await rt.save()
+ assert.match(rt.readOnly(), /saved by a newer DS Bot/)
+ assert.equal(fs.readFileSync(statePath, 'utf8'), future)
+ // The outside edit was still kept aside before it was adopted.
+ const backups = fs.readdirSync(home).filter(name => name.startsWith('state.json.replaced-'))
+ assert.equal(backups.length, 1)
+ assert.equal(JSON.parse(fs.readFileSync(join(home, backups[0]), 'utf8')).prefs.marker, 'ours')
+})
+
test('a save queued before the lock was lost does not write over an outside edit', async (t) => {
const { rt, home, statePath } = runtime(t)
await rt.load()
@@ -338,7 +390,7 @@ test('a changed file is adopted when another host left its lock behind', async (
})
test('a reading host takes over the team once the dsh that held it is gone', async (t) => {
- const { rt, home, statePath } = runtime(t, { version: 1, revision: 4, prefs: { marker: 'first write' } })
+ const { rt, home, statePath, warnings } = runtime(t, { version: 1, revision: 4, prefs: { marker: 'first write' } })
fs.writeFileSync(join(home, 'state.lock'), JSON.stringify({ pid: process.ppid, host: hostname(), token: 'other-host', since: Date.now() }))
await rt.load()
assert.match(rt.readOnly(), /another dsh/)
@@ -348,6 +400,9 @@ test('a reading host takes over the team once the dsh that held it is gone', asy
await rt.refresh()
assert.equal(rt.readOnly(), undefined)
assert.equal(rt.state.prefs.marker, 'handed over')
+ // A normal hand-over adopts silently: no copy of this host's state, no diagnostic.
+ assert.deepEqual(fs.readdirSync(home).filter(name => name.startsWith('state.json.replaced-')), [])
+ assert.ok(!warnings.some(args => /adopted/.test(String(args))))
await rt.save()
assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'handed over')
})