Skip to content

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

Fix Windows file retries under frozen test clocks

Fei24115ef3ff447487
diff --git a/ds-bot/src/host/files.js b/ds-bot/src/host/files.js
index b98edf27..ef1fa8aa 100644
--- a/ds-bot/src/host/files.js
+++ b/ds-bot/src/host/files.js
@@ -1,4 +1,5 @@
import { rename } from 'node:fs/promises'
+import { setTimeout as delay } from 'node:timers/promises'
// On Windows a file cannot be replaced while another program has it open: another DSH
// reading the team's state, the search indexer, a virus scanner, a sync client. Those
@@ -18,7 +19,8 @@ export async function replaceFile(temp, path, check = () => {}) {
return await rename(temp, path)
} catch (error) {
if (process.platform !== 'win32' || attempt >= TRIES || !BUSY.has(error?.code)) throw error
- await new Promise(resolve => setTimeout(resolve, WAIT_MS * attempt))
+ // Real filesystem contention must clear even while tests freeze application timers.
+ await delay(WAIT_MS * attempt)
check()
}
}
diff --git a/ds-bot/test/files.test.js b/ds-bot/test/files.test.js
index 3ee7601d..6ba1b34d 100644
--- a/ds-bot/test/files.test.js
+++ b/ds-bot/test/files.test.js
@@ -1,8 +1,11 @@
import assert from 'node:assert/strict'
import { closeSync, mkdtempSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import fsp from 'node:fs/promises'
+import { syncBuiltinESMExports } from 'node:module'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
+import { clearTimeout as realClearTimeout, setTimeout as realTimeout } from 'node:timers'
import { replaceFile } from '../src/host/files.js'
const folder = (t) => {
@@ -43,3 +46,41 @@ test('gives up at once on an error that waiting does not fix', async (t) => {
await assert.rejects(replaceFile(join(dir, 'missing.tmp'), join(dir, 'state.json')), { code: 'ENOENT' })
assert.equal(wait.mock.callCount(), 0)
})
+
+test('a Windows file retry finishes while the application clock is frozen', async (t) => {
+ const dir = folder(t)
+ const path = join(dir, 'state.json')
+ writeFileSync(path, 'old')
+ writeFileSync(`${path}.tmp`, 'new')
+ const platform = Object.getOwnPropertyDescriptor(process, 'platform')
+ const rename = fsp.rename
+ let attempts = 0
+ let deadline
+ Object.defineProperty(process, 'platform', { value: 'win32' })
+ fsp.rename = async (...args) => {
+ attempts += 1
+ if (attempts === 1) throw Object.assign(new Error('file is busy'), { code: 'EPERM' })
+ return rename(...args)
+ }
+ syncBuiltinESMExports()
+ t.mock.timers.enable({ apis: ['setTimeout'] })
+ try {
+ const pending = replaceFile(`${path}.tmp`, path)
+ const finished = await Promise.race([
+ pending.then(() => true),
+ new Promise(resolve => { deadline = realTimeout(resolve, 1000, false) }),
+ ])
+ // Release the old implementation too, so a regression fails instead of hanging.
+ t.mock.timers.runAll()
+ await pending
+ assert.equal(finished, true, 'the real filesystem must not wait for a mock timer tick')
+ assert.equal(attempts, 2)
+ assert.equal(readFileSync(path, 'utf8'), 'new')
+ } finally {
+ realClearTimeout(deadline)
+ t.mock.timers.reset()
+ fsp.rename = rename
+ syncBuiltinESMExports()
+ Object.defineProperty(process, 'platform', platform)
+ }
+})