A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Fail a queued save whose snapshot outlived an adoption
Fei24115977ffceaf49
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex 313babd6..b808a5a3 100644--- a/ds-bot/src/host/store.js+++ b/ds-bot/src/host/store.js@@ -34,6 +34,9 @@ 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 the in-memory state is replaced from the file, so a queued+ // save cannot write a snapshot taken of the state it replaced.+ let lockEpoch = 0const stamp = () => {try {const { mtimeMs, size } = statSync(statePath)@@ -106,6 +109,7 @@ export function install(rt) {// Fill `state` from the saved file and bring an older team up to date. Returns the// groups whose Session titles still need the new name.const adopt = (parsed) => {+ lockEpoch += 1const hadCapabilities = parsed !== null && parsed !== undefined && Object.hasOwn(parsed, 'capabilities')for (const key of Object.keys(state)) delete state[key]Object.assign(state, JSON.parse(EMPTY), parsed ?? {})@@ -350,6 +354,7 @@ export function install(rt) {}// The snapshot is taken here, so a save made while an adopt was pending// writes the adopted state, not the stale one it had.+ const epoch = lockEpochstate.revision += 1state.exchanges = state.exchanges.slice(-MAX_EXCHANGES)const snapshot = JSON.stringify(state)@@ -363,7 +368,9 @@ export function install(rt) {// instead of being written over; a removed file is just written again.const changed = owned && stamp() !== '' && stamp() !== seenif (changed) void adoptAfterReclaim().catch(() => {})- if (!owned || changed) {+ // A different epoch means an adopt replaced the state this snapshot was+ // taken from — another host's writes, or an outside edit it adopted.+ if (!owned || changed || lockEpoch !== epoch) {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 errordiff --git a/ds-bot/test/store.test.js b/ds-bot/test/store.test.jsindex 0dca678a..b64472bd 100644--- a/ds-bot/test/store.test.js+++ b/ds-bot/test/store.test.js@@ -371,6 +371,46 @@ test('a save queued before the lock was lost does not write over an outside edit}})+test('a save paused while another host held the team does not overwrite its final state', async (t) => {+ const { rt, home, statePath } = runtime(t)+ await rt.load()+ rt.state.prefs.marker = 'ours'+ 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.prefs.after = 'stale snapshot'+ const saving = rt.save()+ await atMkdir+ // Another dsh takes the lock and writes; this host turns read-only and adopts.+ fs.writeFileSync(join(home, 'state.lock'), JSON.stringify({ pid: process.ppid, host: hostname(), token: 'other-host', since: Date.now() }))+ fs.writeFileSync(statePath, JSON.stringify({ version: 1, revision: 900, prefs: { marker: 'other write' } }))+ await rt.refresh()+ assert.match(rt.readOnly(), /another dsh/)+ // The other dsh writes its final state and exits; this host takes over.+ fs.writeFileSync(statePath, JSON.stringify({ version: 1, revision: 901, prefs: { marker: 'final' } }))+ fs.rmSync(join(home, 'state.lock'))+ const refreshing = rt.refresh()+ unblock()+ await assert.rejects(saving, { code: 'ELOCKLOST' })+ await refreshing+ assert.equal(rt.state.prefs.marker, 'final')+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'final')+ } finally {+ unblock()+ restore()+ }+})+test('a changed file is adopted when another host left its lock behind', async (t) => {const { rt, home, statePath } = runtime(t)await rt.load()