Skip to content

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

Try again for a moment when Windows reports a file busy as it is replaced

Fei24114f74f086503b
diff --git a/ds-bot/src/host/files.js b/ds-bot/src/host/files.js
new file mode 100644
index 00000000..86e0b5a6
--- /dev/null
+++ b/ds-bot/src/host/files.js
@@ -0,0 +1,20 @@
+import { rename } from 'node:fs/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
+// readers let go within moments.
+const BUSY = new Set(['EPERM', 'EACCES', 'EBUSY'])
+const TRIES = 6
+const WAIT_MS = 40
+
+/** Moves `temp` over `path`, trying again for a moment while Windows reports `path` busy. */
+export async function replaceFile(temp, path) {
+ 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 error
+ await new Promise(resolve => setTimeout(resolve, WAIT_MS * attempt))
+ }
+ }
+}
diff --git a/ds-bot/src/host/images.js b/ds-bot/src/host/images.js
index 6f9833c8..df263088 100644
--- a/ds-bot/src/host/images.js
+++ b/ds-bot/src/host/images.js
@@ -2,10 +2,11 @@
// reaches a model that cannot read them, each image becomes text from a vision model.
// Texts are kept per attachment, so later requests repeat the same words and the
// prompt cache still hits.
-import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
+import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { BlockAssembler } from '@deepseek-ai/dsh-llm'
import { OCR_CACHE_MAX, OCR_PROMPT, OCR_RETRY_MS, OCR_TIMEOUT_MS } from './constants.js'
+import { replaceFile } from './files.js'
import { splitRef } from './text.js'
export function offloadTargets(session, beforeSeq) {
@@ -82,7 +83,7 @@ export function install(rt) {
await mkdir(home, { recursive: true })
const temp = `${ocrPath}.${process.pid}.tmp`
await writeFile(temp, snapshot, 'utf8')
- await rename(temp, ocrPath)
+ await replaceFile(temp, ocrPath)
}).catch(error => rt.warn('ds-bot: OCR cache write failed: %s', error))
}
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.js
index 547e923a..952a8ec5 100644
--- a/ds-bot/src/host/memory.js
+++ b/ds-bot/src/host/memory.js
@@ -26,6 +26,7 @@ import { appendFile, mkdir, rename, unlink, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { BlockAssembler } from '@deepseek-ai/dsh-llm'
import { defineTool } from '@deepseek-ai/dsh-tools'
+import { replaceFile } from './files.js'
import { splitRef, unquote } from './text.js'
import { speakerOf } from '../shared/mentions.js'
import { output } from './tools.js'
@@ -457,7 +458,7 @@ export function install(rt) {
await mkdir(dirname(path), { recursive: true })
const temp = `${path}.${process.pid}.tmp`
await writeFile(temp, text, 'utf8')
- await rename(temp, path)
+ await replaceFile(temp, path)
}
async function journal(record) {
await mkdir(root, { recursive: true })
diff --git a/ds-bot/src/host/secrets.js b/ds-bot/src/host/secrets.js
index a78ffd02..86c3e3b3 100644
--- a/ds-bot/src/host/secrets.js
+++ b/ds-bot/src/host/secrets.js
@@ -3,9 +3,10 @@
// their shell, tool results lose it before they are saved, and every model request
// has it replaced by [secret:<NAME>] before it leaves.
import { randomUUID } from 'node:crypto'
-import { chmod, mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises'
+import { chmod, mkdir, readFile, unlink, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { defineTool } from '@deepseek-ai/dsh-tools'
+import { replaceFile } from './files.js'
import { output } from './tools.js'
export const SECRET_NAME = /^[A-Z][A-Z0-9_]{0,63}$/
@@ -150,7 +151,7 @@ export function install(rt) {
try {
await writeFile(temp, snapshot, { encoding: 'utf8', mode: 0o600 })
await chmod(temp, 0o600)
- await rename(temp, path)
+ await replaceFile(temp, path)
} catch (error) {
await unlink(temp).catch(() => {})
throw error
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.js
index 29ddf070..22a4cb6a 100644
--- a/ds-bot/src/host/store.js
+++ b/ds-bot/src/host/store.js
@@ -2,9 +2,10 @@
// host on the same data from writing it, and the indexes that map a Session to the Bot
// or group chat it belongs to.
import { mkdirSync, statSync } from 'node:fs'
-import { copyFile, mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises'
+import { copyFile, mkdir, readFile, unlink, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { LEGACY_THEMES, MAX_EXCHANGES, ROOM_MODES, TEXT_KEPT } from './constants.js'
+import { replaceFile } from './files.js'
import { homeLock } from './home-lock.js'
import { currentColor, headOf, normalize, unquote } from './text.js'
@@ -299,7 +300,7 @@ export function install(rt) {
ensureLock()
await writeFile(temp, snapshot, 'utf8')
ensureLock()
- await rename(temp, statePath)
+ await replaceFile(temp, statePath)
seen = stamp()
rt.capabilitiesStateSaved?.()
rt.stateSaved?.()
diff --git a/ds-bot/test/files.test.js b/ds-bot/test/files.test.js
new file mode 100644
index 00000000..242a3de7
--- /dev/null
+++ b/ds-bot/test/files.test.js
@@ -0,0 +1,30 @@
+import assert from 'node:assert/strict'
+import { closeSync, mkdtempSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { test } from 'node:test'
+import { replaceFile } from '../src/host/files.js'
+
+const folder = (t) => {
+ const dir = mkdtempSync(join(tmpdir(), 'ds-bot-files-'))
+ t.after(() => { rmSync(dir, { recursive: true, force: true }) })
+ return dir
+}
+
+test('replaces a file that another program holds open for a moment', { 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')
+ setTimeout(() => { closeSync(reader) }, 60)
+ await replaceFile(`${path}.tmp`, path)
+ assert.equal(readFileSync(path, 'utf8'), 'new')
+})
+
+test('gives up at once on an error that waiting does not fix', async (t) => {
+ const dir = folder(t)
+ const started = Date.now()
+ await assert.rejects(replaceFile(join(dir, 'missing.tmp'), join(dir, 'state.json')), { code: 'ENOENT' })
+ assert.ok(Date.now() - started < 40)
+})