Skip to content

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

Keep saving the team when the state lock cannot be used

FeiZhuLulu0e92260f1e52
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.js
index 9bd6e596..d29df440 100644
--- a/ds-bot/src/host/store.js
+++ b/ds-bot/src/host/store.js
@@ -21,7 +21,7 @@ export function install(rt) {
// The other dsh host that holds state.lock while this one only reads (see home-lock.js);
// undefined while this host may write.
let holder
- let lockError
+ let lockBroken = false
let stuck
let warnedReadOnly = false
let stopped = false
@@ -37,20 +37,19 @@ export function install(rt) {
}
const lock = homeLock(join(home, 'state.lock'))
const claimLock = () => {
+ if (lockBroken) return
try {
mkdirSync(home, { recursive: true })
holder = lock.claim()
- lockError = undefined
} catch (error) {
- lockError = error
+ // An unusable lock (a read-only or odd file system) must not stop the team; a
+ // save that then cannot write still rejects.
+ lockBroken = true
holder = undefined
+ warn('ds-bot: cannot lock %s, so another dsh on the same data could undo changes: %s', home, error)
}
}
const readOnly = () => {
- if (lockError !== undefined) {
- const code = lockError.code ?? 'unknown error'
- return `DS Bot cannot lock the Bot team in ${home} (${code}), so it does not save changes. Check that the folder is writable and reload.`
- }
if (stuck !== undefined) return `The Bot team file ${statePath} could not be read (${stuck}). DS Bot does not change it; fix or move the file and reload.`
if (holder === undefined) return undefined
const where = holder.pid ? `pid ${holder.pid}${holder.host ? ` on ${holder.host}` : ''}` : 'unknown process'
@@ -161,7 +160,7 @@ export function install(rt) {
if (holder !== undefined) warn('ds-bot: %s', readOnly())
const { migrated, stale } = adopt(await readFileState())
if (holder !== undefined) return
- if (migrated && stuck === undefined && lockError === undefined) {
+ if (migrated && stuck === undefined) {
// A failed write must not poison `loaded`: the migrated state stays in
// memory and the next save() writes it.
try {
@@ -178,20 +177,20 @@ export function install(rt) {
const refresh = async () => {
await load()
const hadHolder = holder !== undefined
- if (hadHolder || lockError !== undefined) {
+ if (hadHolder) {
claimLock()
- if (holder === undefined && lockError === undefined) {
- if (hadHolder) warn('ds-bot: the other dsh has closed; this one now saves the Bot team')
+ if (holder === undefined) {
+ warn('ds-bot: the other dsh has closed; this one now saves the Bot team')
warnedReadOnly = false
adopt(await readFileState())
return
}
- } else if (!lock.owns()) {
+ } else if (!lockBroken && !lock.owns()) {
// 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.
claimLock()
- if (lockError !== undefined) return
+ if (lockBroken) return
if (holder === undefined) {
// The lock was free: still the writer, but the file may carry changes
// written while no host held it.
@@ -213,14 +212,12 @@ export function install(rt) {
}
const writable = () => {
if (stuck !== undefined) return warnReadOnly()
- if (holder === undefined && lockError !== undefined) claimLock()
- if (lockError !== undefined) return warnReadOnly()
+ if (lockBroken) return true
if (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.
if (holder === undefined) {
claimLock()
- if (lockError !== undefined) return warnReadOnly()
if (holder === undefined) return true
}
return warnReadOnly()
@@ -239,7 +236,7 @@ export function install(rt) {
try {
await mkdir(home, { recursive: true })
const ensureLock = () => {
- if (lockError !== undefined) throw lockError
+ if (lockBroken) return
if (!lock.owns()) {
const error = new Error('ds-bot lost the state lock to another dsh')
error.code = 'ELOCKLOST'
diff --git a/ds-bot/test/diagnostics.test.js b/ds-bot/test/diagnostics.test.js
index 0c5683e0..bc6d8a47 100644
--- a/ds-bot/test/diagnostics.test.js
+++ b/ds-bot/test/diagnostics.test.js
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict'
-import fs, { mkdtempSync, readFileSync, rmSync } from 'node:fs'
+import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import fsp from 'node:fs/promises'
import { syncBuiltinESMExports } from 'node:module'
import { homedir, tmpdir } from 'node:os'
diff --git a/ds-bot/test/store.test.js b/ds-bot/test/store.test.js
index 8c7f9ac7..c6281312 100644
--- a/ds-bot/test/store.test.js
+++ b/ds-bot/test/store.test.js
@@ -176,8 +176,8 @@ 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('an unusable state lock fails closed', async (t) => {
- const { rt, home, statePath } = runtime(t)
+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')
const restore = patchSync('writeFileSync', originalWrite => (path, ...args) => {
if (path === lockPath) throw Object.assign(new Error('synthetic lock failure'), { code: 'EACCES' })
@@ -185,10 +185,13 @@ test('an unusable state lock fails closed', async (t) => {
})
try {
await rt.load()
- assert.match(rt.readOnly(), /cannot lock the Bot team.*EACCES/)
- rt.state.prefs.marker = 'not saved'
+ assert.equal(rt.readOnly(), undefined)
+ assert.equal(warnings.filter(([template]) => template.includes('cannot lock')).length, 1)
+ rt.state.prefs.marker = 'saved without a lock'
await rt.save()
- assert.equal(fs.existsSync(statePath), false)
+ await rt.refresh()
+ assert.equal(rt.state.prefs.marker, 'saved without a lock')
+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'saved without a lock')
} finally {
restore()
}