A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Check the state lock again before each new try to replace the state file
Fei24116e678cbc23ed
diff --git a/ds-bot/src/host/files.js b/ds-bot/src/host/files.jsindex 86e0b5a6..b98edf27 100644--- a/ds-bot/src/host/files.js+++ b/ds-bot/src/host/files.js@@ -7,14 +7,19 @@ const BUSY = new Set(['EPERM', 'EACCES', 'EBUSY'])const TRIES = 6const WAIT_MS = 40-/** Moves `temp` over `path`, trying again for a moment while Windows reports `path` busy. */-export async function replaceFile(temp, path) {+/**+ * Moves `temp` over `path`, trying again for a moment while Windows reports `path` busy.+ * `check` runs before each new try and throws to stop, as when the lock that allows the+ * write was lost in the meantime.+ */+export async function replaceFile(temp, path, check = () => {}) {for (let attempt = 1; ; attempt += 1) {try {return await rename(temp, path)} catch (error) {if (process.platform !== 'win32' || attempt >= TRIES || !BUSY.has(error?.code)) throw errorawait new Promise(resolve => setTimeout(resolve, WAIT_MS * attempt))+ check()}}}diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex 22a4cb6a..4432a2cd 100644--- a/ds-bot/src/host/store.js+++ b/ds-bot/src/host/store.js@@ -300,7 +300,7 @@ export function install(rt) {ensureLock()await writeFile(temp, snapshot, 'utf8')ensureLock()- await replaceFile(temp, statePath)+ await replaceFile(temp, statePath, ensureLock)seen = stamp()rt.capabilitiesStateSaved?.()rt.stateSaved?.()diff --git a/ds-bot/test/files.test.js b/ds-bot/test/files.test.jsindex 242a3de7..4cf280c0 100644--- a/ds-bot/test/files.test.js+++ b/ds-bot/test/files.test.js@@ -22,6 +22,21 @@ test('replaces a file that another program holds open for a moment', { skip: proassert.equal(readFileSync(path, 'utf8'), 'new')})+test('stops trying when the check before a new try throws, and leaves the file as it was', { skip: process.platform !== 'win32' && 'only Windows refuses it' }, async (t) => {+ const dir = folder(t)+ const path = join(dir, 'state.json')+ writeFileSync(path, 'old')+ writeFileSync(`${path}.tmp`, 'new')+ const reader = openSync(path, 'r')+ const lost = new Error('lost the lock')+ try {+ await assert.rejects(replaceFile(`${path}.tmp`, path, () => { throw lost }), lost)+ } finally {+ closeSync(reader)+ }+ assert.equal(readFileSync(path, 'utf8'), 'old')+})+test('gives up at once on an error that waiting does not fix', async (t) => {const dir = folder(t)const started = Date.now()