A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Adopt the file on disk on every lock reclaim, serialized with saves
Fei24117f937b4abed8
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex 55439ca5..1750a32f 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 this host takes the lock, so a save queued before a loss+ // does not write across a reclaim it could not see.+ let lockEpoch = 0const stamp = () => {try {const { mtimeMs, size } = statSync(statePath)@@ -48,6 +51,7 @@ 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.@@ -247,6 +251,32 @@ 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.+ let adopting+ const adoptAfterReclaim = () => {+ const run = (adopting ?? writing).then(async () => {+ warnedReadOnly = false+ if (stamp() !== seen) {+ const aside = `${statePath}.replaced-${Date.now()}`+ try {+ await writeFile(aside, JSON.stringify(state))+ warn('ds-bot: %s changed while no host held the team; it is adopted and this host\'s state is kept as %s', statePath, aside)+ } catch (error) {+ warn('ds-bot: %s changed while no host held the team; it is adopted, and this host\'s state is lost because it could not be kept aside: %s', statePath, error)+ }+ }+ adopt(readFileState())+ })+ writing = run.catch(() => {})+ const tracked = run.finally(() => { if (adopting === tracked) adopting = undefined })+ adopting = tracked+ return tracked+ }+// A reading host follows the file and takes over once the other host has gone.const refresh = async () => {await load()@@ -255,8 +285,7 @@ export function install(rt) {claimLock()if (holder === undefined) {warn('ds-bot: the other dsh has closed; this one now saves the Bot team')- warnedReadOnly = false- adopt(readFileState())+ await adoptAfterReclaim()return}} else if (!lockBroken && !lock.owns()) {@@ -266,24 +295,14 @@ export function install(rt) {claimLock()if (lockBroken) returnif (holder === undefined) {- warnedReadOnly = false- // 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; when it is not what this- // host last saw, this host's own state is kept aside first.- if (stamp() !== seen) {- const aside = `${statePath}.replaced-${Date.now()}`- try {- await writeFile(aside, JSON.stringify(state))- warn('ds-bot: %s changed while no host held the team; it is adopted and this host\'s state is kept as %s', statePath, aside)- } catch (error) {- warn('ds-bot: %s changed while no host held the team; it is adopted, and this host\'s state is lost because it could not be kept aside: %s', statePath, error)- }- }- adopt(readFileState())+ // 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()return}warn('ds-bot: %s', readOnly())}+ if (adopting !== undefined) return adoptingif ((stuck !== undefined || holder !== undefined) && stamp() !== seen) adopt(readFileState())}@@ -299,10 +318,14 @@ export function install(rt) {if (lockBroken) return trueif (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.+ // 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`.if (holder === undefined) {claimLock()- if (holder === undefined) return true+ if (holder === undefined) {+ void adoptAfterReclaim().catch(() => {})+ return true+ }}return warnReadOnly()}@@ -312,16 +335,21 @@ 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- state.revision += 1- state.exchanges = state.exchanges.slice(-MAX_EXCHANGES)- const snapshot = JSON.stringify(state)+ const epoch = lockEpochconst 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.+ state.revision += 1+ state.exchanges = state.exchanges.slice(-MAX_EXCHANGES)+ const snapshot = JSON.stringify(state)const temp = `${statePath}.${process.pid}.tmp`try {await mkdir(home, { recursive: true })const ensureLock = () => {if (lockBroken) return- if (!lock.owns()) {+ // 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')error.code = 'ELOCKLOST'throw errordiff --git a/ds-bot/test/store.test.js b/ds-bot/test/store.test.jsindex c5c78101..64ee4cea 100644--- a/ds-bot/test/store.test.js+++ b/ds-bot/test/store.test.js@@ -225,6 +225,100 @@ test('a changed file is still adopted when keeping this host\'s state aside failassert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'outside edit')})+test('a save that takes the lock back adopts a changed file first', async (t) => {+ const { rt, home, statePath, warnings } = runtime(t)+ await rt.load()+ rt.state.prefs.marker = 'ours'+ await rt.save()+ // A tool with full file access edits the team file and removes the lock; the+ // next save runs before any refresh sees it.+ fs.writeFileSync(statePath, JSON.stringify({ version: 1, revision: 900, prefs: { marker: 'outside edit' } }))+ fs.rmSync(join(home, 'state.lock'))+ rt.state.prefs.after = 'late change'+ await rt.save()+ // The file on disk was adopted, not overwritten by the old in-memory state.+ const saved = JSON.parse(fs.readFileSync(statePath, 'utf8'))+ assert.equal(saved.prefs.marker, 'outside edit')+ assert.equal(rt.state.prefs.marker, 'outside edit')+ assert.equal(rt.state.prefs.after, undefined)+ assert.ok(warnings.some(args => /adopted/.test(String(args)) && /kept as/.test(String(args))))+ const backups = fs.readdirSync(home).filter(name => name.startsWith('state.json.replaced-'))+ assert.equal(backups.length, 1)+ const aside = JSON.parse(fs.readFileSync(join(home, backups[0]), 'utf8'))+ assert.equal(aside.prefs.marker, 'ours')+ assert.equal(aside.prefs.after, 'late change')+})++test('a save cannot slip between a reclaim and the adopt it waits on', async (t) => {+ const { rt, home, statePath } = runtime(t)+ await rt.load()+ rt.state.prefs.marker = 'ours'+ await rt.save()+ fs.writeFileSync(statePath, JSON.stringify({ version: 1, revision: 900, prefs: { marker: 'outside edit' } }))+ fs.rmSync(join(home, 'state.lock'))+ let entered+ let unblock+ const atBackup = new Promise(resolve => { entered = resolve })+ const gate = new Promise(resolve => { unblock = resolve })+ const restore = patch('writeFile', originalWrite => async (path, ...args) => {+ if (String(path).startsWith(`${statePath}.replaced-`)) {+ entered()+ await gate+ }+ return originalWrite(path, ...args)+ })+ try {+ const refreshing = rt.refresh()+ await atBackup+ // The reclaim already happened; this save must wait for the adopt, not write+ // the in-memory state it still holds.+ rt.state.prefs.after = 'racing save'+ const racing = rt.save()+ unblock()+ await refreshing+ await racing+ assert.equal(rt.state.prefs.marker, 'outside edit')+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'outside edit')+ } finally {+ unblock()+ restore()+ }+})++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()+ 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 = 'queued save'+ const racing = rt.save()+ await atMkdir+ fs.writeFileSync(statePath, JSON.stringify({ version: 1, revision: 900, prefs: { marker: 'outside edit' } }))+ fs.rmSync(join(home, 'state.lock'))+ const refreshing = rt.refresh()+ unblock()+ await assert.rejects(racing, { code: 'ELOCKLOST' })+ await refreshing+ assert.equal(rt.state.prefs.marker, 'outside edit')+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'outside edit')+ } 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()