A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Do not adopt a state file changed while the lock was gone
Fei24119660df922170
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex a20a0b3e..e1bea53c 100644--- a/ds-bot/src/host/store.js+++ b/ds-bot/src/host/store.js@@ -261,13 +261,28 @@ export function install(rt) {// A cached "writer" is stale once the lock file no longer names this host:// another dsh may have taken it over, or the file may just be gone. Re-claim// and re-check instead of trusting the flag.+ const previous = lock.read()claimLock()if (lockBroken) returnif (holder === undefined) {- // The lock was free: still the writer, but the file may carry changes- // written while no host held it.warnedReadOnly = false- adopt(await readFileState())+ // A lock file another dsh left behind means the team changed hands, so its+ // file is adopted. Without one the state should still be what this host+ // last wrote; a file that changed anyway came from outside, so it is kept+ // as a copy and this host keeps its own state.+ if (previous !== undefined) {+ adopt(await readFileState())+ return+ }+ if (stamp() !== seen) {+ const backup = `${statePath}.edited-${Date.now()}`+ try {+ await copyFile(statePath, backup)+ warn('ds-bot: %s changed while no host held the team and its lock was gone; the change was not adopted, the file is kept as %s', statePath, backup)+ } catch (error) {+ warn('ds-bot: %s changed while no host held the team and its lock was gone; the change was not adopted and keeping a copy failed: %s', statePath, error)+ }+ }return}warn('ds-bot: %s', readOnly())diff --git a/ds-bot/test/store.test.js b/ds-bot/test/store.test.jsindex 03cd46f6..38c219df 100644--- a/ds-bot/test/store.test.js+++ b/ds-bot/test/store.test.js@@ -176,6 +176,56 @@ test('refresh adopts a fixed state file and clears read-only mode', async (t) =>assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'saved after repair')})+test('a state file changed while the lock was gone is set aside, not adopted', async (t) => {+ const { rt, home, statePath, warnings } = runtime(t)+ await rt.load()+ rt.state.prefs.marker = 'kept'+ await rt.save()+ // A tool with full file access can edit the team file and remove the lock.+ fs.writeFileSync(statePath, JSON.stringify({ version: 1, revision: 900, prefs: { marker: 'outside edit' } }))+ fs.rmSync(join(home, 'state.lock'))+ await rt.refresh()+ assert.equal(rt.state.prefs.marker, 'kept')+ assert.equal(rt.readOnly(), undefined)+ assert.ok(warnings.some(args => /not adopted/.test(String(args))))+ const backups = fs.readdirSync(home).filter(name => name.startsWith('state.json.edited-'))+ assert.equal(backups.length, 1)+ assert.equal(JSON.parse(fs.readFileSync(join(home, backups[0]), 'utf8')).prefs.marker, 'outside edit')+ // The next save writes this host's own state over the outside file.+ await rt.save()+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'kept')+})++test('a changed file is adopted when another host left its lock behind', async (t) => {+ const { rt, home, statePath } = runtime(t)+ await rt.load()+ rt.state.prefs.marker = 'before'+ await rt.save()+ // A dsh that took over and is gone leaves its lock beside the file it wrote.+ fs.writeFileSync(join(home, 'state.lock'), JSON.stringify({ pid: 99999999, host: hostname(), token: 'other-host', since: Date.now() }))+ fs.writeFileSync(statePath, JSON.stringify({ version: 1, revision: 900, prefs: { marker: 'taken over' } }))+ await rt.refresh()+ assert.equal(rt.state.prefs.marker, 'taken over')+ assert.equal(rt.readOnly(), undefined)+ await rt.save()+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'taken over')+})++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' } })+ 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/)+ // The other dsh wrote its last state and closed, releasing the lock.+ fs.writeFileSync(statePath, JSON.stringify({ version: 1, revision: 5, prefs: { marker: 'handed over' } }))+ fs.rmSync(join(home, 'state.lock'))+ await rt.refresh()+ assert.equal(rt.readOnly(), undefined)+ assert.equal(rt.state.prefs.marker, 'handed over')+ await rt.save()+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'handed over')+})+test('an unusable state lock warns and keeps the team saving', async (t) => {const { rt, home, statePath, warnings } = runtime(t)const lockPath = join(home, 'state.lock')