A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Harden state saves and lock ownership
lulu feizhud464962d25d2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 685ad0a3..bc64b3fe 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,9 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change### Fixed+- When DS Bot cannot save the Bot team, the change now fails with an error instead of being lost silently.+- A Bot team file that cannot be read is no longer replaced by an empty team; a damaged file is kept as a backup.+- Warnings in the host log hide keys and tokens.- In a group chat, a conversation referenced in a message reached none of the members, and a reference whose title started with a member's name called that member. Now the members who answer the message get the referenced conversation.- In a group chat, typing `@` and a Bot's name and pressing Enter inserted a conversation reference instead of the Bot's name.- A long conversation's fresh part keeps the Bot's model and thinking level. Before, the new part fell back to the default model.diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.jsindex 2282cfb7..f7049806 100644--- a/ds-bot/src/host/delivery.js+++ b/ds-bot/src/host/delivery.js@@ -49,7 +49,7 @@ export function install(rt) {}if (record && EXCHANGE_ROLES.has(role)) {state.exchanges.push({ id: exchangeId, from: fromId, to: toId, text, role, time: Date.now(), ...(replyTo ? { replyTo } : {}), ...(audience === 'user' ? { audience } : {}) })- void rt.save()+ void rt.save().catch(() => {})}const agent = await resolveAgent(sessionId ?? rt.chatOf(toId))// A steered message cuts into a running turn at its next step; a follow-updiff --git a/ds-bot/src/host/diagnostics.js b/ds-bot/src/host/diagnostics.jsindex 01cad761..60abbd00 100644--- a/ds-bot/src/host/diagnostics.js+++ b/ds-bot/src/host/diagnostics.js@@ -144,7 +144,7 @@ export function install(rt) {}).catch((error) => {if (fileFailed) returnfileFailed = true- console.error('[ds-bot] diagnostics log not written:', error?.code ?? error?.message ?? error)+ console.error('[ds-bot] diagnostics log not written:', error?.code ?? clean(error?.message ?? String(error)))})}@@ -164,13 +164,15 @@ export function install(rt) {}const warn = (template, ...args) => {- ctx.logger.warn(template, ...args)- console.error(`[ds-bot] ${template}`, ...args.map(arg => arg?.stack ?? arg))const error = args.find(arg => arg instanceof Error)+ const line = clean(format(template, ...args.map(arg => (arg instanceof Error ? errorText(arg) : arg))))+ const frames = typeof error?.stack === 'string' ? error.stack.split('\n').filter(item => /^\s+at /.test(item)).slice(0, STACK_LINES).map(item => item.trim()) : []+ ctx.logger.warn('%s', line)+ console.error(`[ds-bot] ${line}${frames.length > 0 ? `\n${clean(frames.join('\n'))}` : ''}`)note({level: 'warn',source: 'host',- text: format(template, ...args.map(arg => (arg instanceof Error ? errorText(arg) : arg))).replace(/^ds-bot: /, ''),+ text: line.replace(/^ds-bot: /, ''),code: typeof error?.code === 'string' ? error.code : undefined,stack: error?.stack,})diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.jsindex dddc6167..6982950d 100644--- a/ds-bot/src/host/groups.js+++ b/ds-bot/src/host/groups.js@@ -250,7 +250,7 @@ export function install(rt) {slot.status = 'posted'appendRoom(room, { who: member.name, botId: member.id, text: reply })round.replies.push({ botId: member.id, name: member.name, color: member.color, text: reply, time: Date.now() })- void rt.save()+ void rt.save().catch(() => {})// A called member moves up if it is still waiting, or gets one more turn if it// already spoke; either way it goes right after this speaker. Only the admin's// "@all" calls everyone, so a member cannot set off a storm.@@ -357,7 +357,7 @@ export function install(rt) {for (const round of rt.roomProgress.values()) {if (round.roomId === room.id) round.replies.push({ kind: 'lookup', botId: member.id, name: member.name, color: member.color, text, time: Date.now() })}- void rt.save()+ void rt.save().catch(() => {})}const partsNote = parts.length > 1 ? `\n\nYour own chat has ${parts.length} parts: ${rt.partIndex(parts)}. Call read_own_chat with part N for the note that closed an earlier part, or with words to search all parts.` : ''if (query === '' && around !== '') {diff --git a/ds-bot/src/host/home-lock.js b/ds-bot/src/host/home-lock.jsindex db6e7234..90ddd598 100644--- a/ds-bot/src/host/home-lock.js+++ b/ds-bot/src/host/home-lock.js@@ -3,10 +3,10 @@// other's changes. The first host to load takes `state.lock`; a second one only reads.//// The lock is a small JSON file (pid, host, a random token) whose mtime the holder bumps-// on a timer. It counts as gone when its process has exited (same host) or when it has-// not been bumped for `staleMs`, which also covers a reused pid after a crash. On another-// host only the age can tell, so a lock on shared storage waits out `staleMs`. The token,-// not the pid, decides ownership: a holder that finds another token has lost the lock.+// on a timer. On the same host, its process decides whether it is live; `staleMs` only+// covers other hosts and unreadable files. A reused pid after a crash keeps the second+// host read-only until that process ends. The token, not the pid, decides ownership: a+// holder that finds another token has lost the lock.import { randomUUID } from 'node:crypto'import { readFileSync, statSync, unlinkSync, utimesSync, writeFileSync } from 'node:fs'import { hostname } from 'node:os'@@ -38,11 +38,9 @@ export function homeLock(path, { staleMs = 60_000, beatMs = 15_000, host = hostn}const live = (holder) => {if (holder === undefined) return false- if (age() >= staleMs) return false- if (holder === null) return true- if (holder.host !== host) return true- // A lock this same process left (a plugin reload whose dispose never ran) is no rival.- return holder.pid !== pid && isAlive(holder.pid)+ if (holder === null) return age() < staleMs+ if (holder.host === host) return holder.pid !== pid && isAlive(holder.pid)+ return age() < staleMs}const owns = () => read()?.token === token@@ -75,7 +73,15 @@ export function homeLock(path, { staleMs = 60_000, beatMs = 15_000, host = hostn}const holder = read()if (live(holder)) return holder ?? {}- try { unlinkSync(path) } catch {}+ const current = read()+ const unchanged = holder === null+ ? current === null+ : holder?.token !== undefined+ ? current?.token === holder.token+ : JSON.stringify(current) === JSON.stringify(holder)+ if (unchanged) {+ try { unlinkSync(path) } catch {}+ }}return read() ?? {}}diff --git a/ds-bot/src/host/roster.js b/ds-bot/src/host/roster.jsindex 36500d76..07cd7474 100644--- a/ds-bot/src/host/roster.js+++ b/ds-bot/src/host/roster.js@@ -114,7 +114,7 @@ export function install(rt) {const bot = rt.botOf(botId)if (bot?.kickoff === undefined) returndelete bot.kickoff- void rt.save()+ void rt.save().catch(() => {})}async function duplicateBot(id) {diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex 984d730f..8e528a9d 100644--- a/ds-bot/src/host/store.js+++ b/ds-bot/src/host/store.js@@ -2,7 +2,7 @@// 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 { mkdir, readFile, rename, writeFile } from 'node:fs/promises'+import { copyFile, mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises'import { join } from 'node:path'import { LEGACY_THEMES, MAX_EXCHANGES, ROOM_MODES } from './constants.js'import { homeLock } from './home-lock.js'@@ -21,8 +21,10 @@ 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 lockBroken = false+ let lockError+ let stucklet warnedReadOnly = false+ let stopped = false// mtime and size of the state.json this host last read or wrote.let seen = ''const stamp = () => {@@ -35,32 +37,62 @@ export function install(rt) {}const lock = homeLock(join(home, 'state.lock'))const claimLock = () => {- if (lockBroken) returntry {mkdirSync(home, { recursive: true })holder = lock.claim()+ lockError = undefined} catch (error) {- // An unusable lock (a read-only or odd file system) must not stop the team.- lockBroken = true+ lockError = errorholder = 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 undefinedconst where = holder.pid ? `pid ${holder.pid}${holder.host ? ` on ${holder.host}` : ''}` : 'unknown process'return `The Bot team in ${home} is open in another dsh (Web or Desktop, ${where}). This window shows the team but cannot change it; close the other one and reload.`}const readFileState = async () => {+ let contentstry {+ contents = await readFile(statePath, 'utf8')+ } catch (error) {seen = stamp()- const parsed = JSON.parse(await readFile(statePath, 'utf8'))- return parsed?.version === 1 ? parsed : undefined+ if (error?.code === 'ENOENT') {+ stuck = undefined+ return undefined+ }+ stuck = error?.code ?? error?.message ?? String(error)+ warn('ds-bot: unreadable state %s: %s', statePath, error)+ return undefined+ }+ seen = stamp()+ let parsed+ try {+ parsed = JSON.parse(contents)} catch (error) {- if (error?.code !== 'ENOENT') warn('ds-bot: unreadable state %s: %s', statePath, error)+ const backup = `${statePath}.broken-${Date.now()}`+ try {+ await copyFile(statePath, backup)+ stuck = undefined+ warn('ds-bot: unreadable state %s: %s; backed up as %s', statePath, error, backup)+ } catch (copyError) {+ stuck = copyError?.code ?? copyError?.message ?? error?.message ?? String(error)+ warn('ds-bot: unreadable state %s: %s; backup failed: %s', statePath, error, copyError)+ }return undefined}+ if (parsed?.version === 1) {+ stuck = undefined+ return parsed+ }+ stuck = 'saved by a newer DS Bot; update DS Bot'+ return undefined}// Fill `state` from the saved file and bring an older team up to date. Returns the@@ -129,7 +161,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) await save()+ if (migrated && stuck === undefined) await save()// Not awaited: renaming a session must not hold up the first roster read.for (const room of stale) void rt.retitleRoom(room)})()@@ -137,51 +169,82 @@ export function install(rt) {// A reading host follows the file and takes over once the other host has gone.const refresh = async () => {await load()- if (holder === undefined) return- claimLock()- if (holder === undefined) {- warn('ds-bot: the other dsh has closed; this one now saves the Bot team')- warnedReadOnly = false- adopt(await readFileState())- return+ const hadHolder = holder !== undefined+ if (hadHolder || lockError !== undefined) {+ claimLock()+ if (holder === undefined && lockError === undefined) {+ if (hadHolder) warn('ds-bot: the other dsh has closed; this one now saves the Bot team')+ warnedReadOnly = false+ adopt(await readFileState())+ return+ }}- if (stamp() !== seen) adopt(await readFileState())+ if ((stuck !== undefined || holder !== undefined) && stamp() !== seen) adopt(await readFileState())}+ const warnReadOnly = () => {+ if (!warnedReadOnly) {+ warnedReadOnly = true+ warn('ds-bot: not saving the Bot team: %s', readOnly())+ }+ return false+ }const writable = () => {- if (lockBroken) return true+ if (stuck !== undefined) return warnReadOnly()+ if (holder === undefined && lockError !== undefined) claimLock()+ if (lockError !== undefined) return warnReadOnly()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}- if (!warnedReadOnly) {- warnedReadOnly = true- warn('ds-bot: not saving the Bot team: %s', readOnly())- }- return false+ return warnReadOnly()}const save = () => {+ if (stopped) return writingindexSessions()// A reading host keeps the file's revision, so its window does not skip the next change.if (!writable()) return writingstate.revision += 1state.exchanges = state.exchanges.slice(-MAX_EXCHANGES)const snapshot = JSON.stringify(state, null, 2)- writing = writing.then(async () => {- await mkdir(home, { recursive: true })+ const run = writing.then(async () => {const temp = `${statePath}.${process.pid}.tmp`- await writeFile(temp, snapshot, 'utf8')- await rename(temp, statePath)- seen = stamp()- }).catch(error => { warn('ds-bot: state write failed: %s', error) })- return writing+ try {+ await mkdir(home, { recursive: true })+ const ensureLock = () => {+ if (lockError !== undefined) throw lockError+ if (!lock.owns()) {+ const error = new Error('ds-bot lost the state lock to another dsh')+ error.code = 'ELOCKLOST'+ throw error+ }+ }+ ensureLock()+ await writeFile(temp, snapshot, 'utf8')+ ensureLock()+ await rename(temp, statePath)+ seen = stamp()+ } catch (error) {+ await unlink(temp).catch(() => {})+ throw error+ }+ })+ writing = run.catch(() => {})+ return run.catch(error => {+ warn('ds-bot: state write failed: %s', error)+ throw error+ })}- ctx.effect(() => () => lock.release(), 'ds-bot: state lock')+ ctx.effect(() => () => {+ stopped = true+ return writing.finally(() => lock.release())+ }, 'ds-bot: state lock')const botOf = id => (id === undefined ? undefined : state.bots[id])const roomOf = id => (id === undefined ? undefined : state.rooms[id])diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.jsindex 0db9647b..4f8b639a 100644--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@ -73,14 +73,14 @@ export function install(rt) {if (!turns.includes(turn)) {turns.push(turn)if (turns.length > 400) turns.splice(0, turns.length - 400)- void rt.save()+ void rt.save().catch(() => {})}}const unmarkExchangeTurn = (sessionId, turn) => {const turns = state.exchangeTurns[sessionId] ?? []if (!turns.includes(turn)) returnstate.exchangeTurns[sessionId] = turns.filter(other => other !== turn)- void rt.save()+ void rt.save().catch(() => {})}// A request whose audience is the user is answered in the recipient's own chat: that@@ -209,7 +209,7 @@ export function install(rt) {// Any user message answers or supersedes the open question card.if (state.questions[id] !== undefined) {delete state.questions[id]- void rt.save()+ void rt.save().catch(() => {})}}breakdiff --git a/ds-bot/test/diagnostics.test.js b/ds-bot/test/diagnostics.test.jsindex d64333fa..00d7a121 100644--- a/ds-bot/test/diagnostics.test.js+++ b/ds-bot/test/diagnostics.test.js@@ -1,5 +1,7 @@import assert from 'node:assert/strict'-import { mkdtempSync, readFileSync, rmSync } from 'node:fs'+import fs, { mkdtempSync, readFileSync, rmSync } from 'node:fs'+import fsp from 'node:fs/promises'+import { syncBuiltinESMExports } from 'node:module'import { homedir, tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'@@ -82,6 +84,55 @@ test('warnings are kept, cleaned, written to the log, and read back after a restquiet.mock.restore()})+test('warnings send only cleaned text to the logger and stderr', (t) => {+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-diag-'))+ t.after(() => rmSync(home, { recursive: true, force: true }))+ const values = ['sk-synthetic-key-123456', 'synthetic-bearer-token-12345', 'synthetic-query-token-12345']+ const error = new Error(`key ${values[0]} Bearer ${values[1]} https://example.invalid/?token=${values[2]}`)+ error.stack = `${error.name}: ${error.message}\n at fake (https://example.invalid/?token=${values[2]}:1:1)`+ const warnings = []+ const stderr = []+ const quiet = t.mock.method(console, 'error', (...args) => stderr.push(args.join(' ')))+ const rt = host(home, { config: { diagnosticsFile: false } })+ rt.ctx.logger.warn = (...args) => warnings.push(args)+ rt.warn('ds-bot: warning: %s', error)+ quiet.mock.restore()++ assert.equal(warnings[0][0], '%s')+ assert.match(warnings[0][1], /\[hidden\]/)+ assert.match(stderr[0], /\[hidden\]/)+ assert.match(stderr[0], /at fake/)+ for (const value of values) {+ assert.equal(warnings.flat().join('\n').includes(value), false)+ assert.equal(stderr.join('\n').includes(value), false)+ }+})++test('diagnostics append fallback redacts an error message', async (t) => {+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-diag-'))+ t.after(() => rmSync(home, { recursive: true, force: true }))+ const value = 'sk-synthetic-append-key-123456'+ const stderr = []+ const quiet = t.mock.method(console, 'error', (...args) => stderr.push(args.join(' ')))+ const rt = host(home)+ const originalAppend = fsp.appendFile+ fsp.appendFile = async (path, ...args) => {+ if (path === join(home, 'logs', 'diagnostics.jsonl')) throw new Error(`synthetic append failure ${value}`)+ return originalAppend(path, ...args)+ }+ syncBuiltinESMExports()+ try {+ rt.noteFailure({ text: 'synthetic entry' })+ await rt.diagnostics.clear()+ } finally {+ fsp.appendFile = originalAppend+ syncBuiltinESMExports()+ quiet.mock.restore()+ }+ assert.equal(stderr.join('\n').includes(value), false)+ assert.match(stderr.join('\n'), /sk-\[hidden\]/)+})+test('reportMarkdown says when nothing went wrong', () => {const text = reportMarkdown({ facts: { botVersion: '0.1.0', os: 'linux' } })assert.match(text, /No host errors recorded\./)diff --git a/ds-bot/test/home-lock.test.js b/ds-bot/test/home-lock.test.jsindex 51bdde21..fac8c922 100644--- a/ds-bot/test/home-lock.test.js+++ b/ds-bot/test/home-lock.test.js@@ -1,5 +1,7 @@import assert from 'node:assert/strict'+import fs from 'node:fs'import { existsSync, mkdtempSync, readFileSync, rmSync, utimesSync, writeFileSync } from 'node:fs'+import { syncBuiltinESMExports } from 'node:module'import { hostname, tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'@@ -28,7 +30,7 @@ test('the first host takes the lock and a second live host only reads', (t) => {assert.equal(second.owns(), true)})-test('a lock whose process has exited, or that nobody keeps fresh, is taken over', (t) => {+test('a lock whose same-host process has exited is taken over', (t) => {const path = join(tempHome(t), 'state.lock')writeFileSync(path, JSON.stringify({ pid: 101, host: hostname(), token: 'gone' }))const dead = homeLock(path, { pid: 202, isAlive: () => false })@@ -36,19 +38,54 @@ test('a lock whose process has exited, or that nobody keeps fresh, is taken overassert.equal(dead.claim(), undefined)assert.equal(dead.owns(), true)dead.release()+})- // A live pid (maybe reused) on a lock not bumped for staleMs, and a lock from another host.+test('an old same-host lock stays live while its process is alive', (t) => {+ const path = join(tempHome(t), 'state.lock')writeFileSync(path, JSON.stringify({ pid: 101, host: hostname(), token: 'old' }))const old = new Date(Date.now() - 120_000)utimesSync(path, old, old)const late = homeLock(path, { pid: 202, isAlive: () => true })t.after(() => late.release())- assert.equal(late.claim(), undefined)- late.release()+ assert.equal(late.claim().token, 'old')+ assert.equal(late.owns(), false)+})+test('an old lock from another host is taken over', (t) => {+ const path = join(tempHome(t), 'state.lock')writeFileSync(path, JSON.stringify({ pid: 101, host: 'elsewhere', token: 'far' }))+ const old = new Date(Date.now() - 120_000)+ utimesSync(path, old, old)const near = homeLock(path, { pid: 202, isAlive: () => false })- assert.equal(near.claim().host, 'elsewhere')+ t.after(() => near.release())+ assert.equal(near.claim(), undefined)+ assert.equal(near.owns(), true)+})++test('a stale-lock recheck does not delete a new owner token', (t) => {+ const path = join(tempHome(t), 'state.lock')+ writeFileSync(path, JSON.stringify({ pid: 101, host: hostname(), token: 'stale' }))+ const old = new Date(Date.now() - 120_000)+ utimesSync(path, old, old)+ const originalRead = fs.readFileSync+ let reads = 0+ fs.readFileSync = (file, ...args) => {+ const contents = originalRead(file, ...args)+ if (file === path && ++reads === 3) {+ writeFileSync(path, JSON.stringify({ pid: 303, host: hostname(), token: 'new-owner' }))+ return originalRead(file, ...args)+ }+ return contents+ }+ syncBuiltinESMExports()+ const late = homeLock(path, { pid: 202, isAlive: pid => pid === 303 })+ t.after(() => {+ late.release()+ fs.readFileSync = originalRead+ syncBuiltinESMExports()+ })+ assert.equal(late.claim().token, 'new-owner')+ assert.equal(JSON.parse(readFileSync(path, 'utf8')).token, 'new-owner')})test('a holder that finds another token in the file knows it lost the lock', (t) => {diff --git a/ds-bot/test/store.test.js b/ds-bot/test/store.test.jsnew file mode 100644index 00000000..ede3abd4--- /dev/null+++ b/ds-bot/test/store.test.js@@ -0,0 +1,255 @@+import assert from 'node:assert/strict'+import fs from 'node:fs'+import fsp from 'node:fs/promises'+import { syncBuiltinESMExports } from 'node:module'+import { hostname, tmpdir } from 'node:os'+import { join } from 'node:path'+import { test } from 'node:test'+import { install } from '../src/host/store.js'++function patch(method, replace) {+ const original = fsp[method]+ fsp[method] = replace(original)+ syncBuiltinESMExports()+ return () => {+ fsp[method] = original+ syncBuiltinESMExports()+ }+}++function patchSync(method, replace) {+ const original = fs[method]+ fs[method] = replace(original)+ syncBuiltinESMExports()+ return () => {+ fs[method] = original+ syncBuiltinESMExports()+ }+}++function runtime(t, saved) {+ const home = fs.mkdtempSync(join(tmpdir(), 'ds-bot-store-'))+ const statePath = join(home, 'state.json')+ if (saved !== undefined) fs.writeFileSync(statePath, typeof saved === 'string' ? saved : JSON.stringify(saved))+ const warnings = []+ const disposers = []+ let disposed+ const ctx = {+ logger: { warn: (...args) => warnings.push(args) },+ effect(execute) {+ const dispose = execute()+ if (typeof dispose === 'function') disposers.push(dispose)+ },+ }+ const rt = {+ ctx,+ home,+ statePath,+ autoRoomName: () => 'Room',+ retitleRoom: async () => {},+ warn: (...args) => warnings.push(args),+ }+ install(rt)+ const dispose = () => {+ disposed ??= Promise.all(disposers.splice(0).map(cleanup => cleanup()))+ return disposed+ }+ t.after(async () => {+ await dispose()+ fs.rmSync(home, { recursive: true, force: true })+ })+ return { rt, home, statePath, warnings, dispose }+}++test('a failed state write rejects and leaves the saved file unchanged', async (t) => {+ const { rt, statePath } = runtime(t)+ await rt.load()+ await rt.save()+ const original = fs.readFileSync(statePath)+ fs.mkdirSync(`${statePath}.${process.pid}.tmp`)+ rt.state.prefs.marker = 'not saved'+ await assert.rejects(rt.save(), { code: 'EISDIR' })+ assert.deepEqual(fs.readFileSync(statePath), original)+})++test('a failed state rename rejects and preserves the saved file', async (t) => {+ const { rt, statePath } = runtime(t)+ await rt.load()+ await rt.save()+ const original = fs.readFileSync(statePath)+ const restore = patch('rename', originalRename => async (from, to) => {+ if (to === statePath) throw Object.assign(new Error('synthetic rename failure'), { code: 'ENOSPC' })+ return originalRename(from, to)+ })+ try {+ rt.state.prefs.marker = 'not saved'+ await assert.rejects(rt.save(), { code: 'ENOSPC' })+ assert.deepEqual(fs.readFileSync(statePath), original)+ } finally {+ restore()+ }+})++test('malformed state is backed up before a later save', async (t) => {+ const original = '{"version":1,'+ const { rt, statePath } = runtime(t, original)+ await rt.load()+ const backups = fs.readdirSync(rt.home).filter(name => name.startsWith('state.json.broken-'))+ assert.equal(backups.length, 1)+ assert.equal(fs.readFileSync(join(rt.home, backups[0]), 'utf8'), original)+ rt.state.prefs.marker = 'recovered'+ await rt.save()+ assert.equal(JSON.parse(fs.readFileSync(statePath, 'utf8')).prefs.marker, 'recovered')+})++test('malformed state stays read-only when its backup cannot be copied', async (t) => {+ const original = '{"version":1,'+ const { rt, statePath } = runtime(t, original)+ const restore = patch('copyFile', () => async () => {+ throw Object.assign(new Error('synthetic copy failure'), { code: 'EACCES' })+ })+ try {+ await rt.load()+ assert.match(rt.readOnly(), /could not be read/)+ rt.state.prefs.marker = 'not saved'+ await rt.save()+ assert.equal(fs.readFileSync(statePath, 'utf8'), original)+ assert.deepEqual(fs.readdirSync(rt.home).filter(name => name.startsWith('state.json.broken-')), [])+ } finally {+ restore()+ }+})++test('a state from a newer version remains untouched', async (t) => {+ const original = JSON.stringify({ version: 999, revision: 77, recoveryMarker: 'keep' })+ const { rt, statePath } = runtime(t, original)+ await rt.load()+ assert.match(rt.readOnly(), /saved by a newer DS Bot; update DS Bot/)+ rt.state.prefs.marker = 'not saved'+ await rt.save()+ assert.equal(fs.readFileSync(statePath, 'utf8'), original)+})++test('a read error keeps the existing state read-only', async (t) => {+ const original = JSON.stringify({ version: 1, revision: 77, recoveryMarker: 'keep' })+ const { rt, statePath } = runtime(t, original)+ const restore = patch('readFile', originalRead => async (path, ...args) => {+ if (path === statePath) throw Object.assign(new Error('synthetic read failure'), { code: 'EACCES' })+ return originalRead(path, ...args)+ })+ try {+ await rt.load()+ } finally {+ restore()+ }+ assert.match(rt.readOnly(), /could not be read \(EACCES\)/)+ await rt.save()+ assert.equal(fs.readFileSync(statePath, 'utf8'), original)+})++test('refresh adopts a fixed state file and clears read-only mode', async (t) => {+ const { rt, statePath } = runtime(t, { version: 999, revision: 77 })+ await rt.load()+ assert.match(rt.readOnly(), /could not be read/)+ const fixed = { version: 1, revision: 78, recoveryMarker: 'fixed file' }+ fs.writeFileSync(statePath, JSON.stringify(fixed))+ await rt.refresh()+ assert.equal(rt.readOnly(), undefined)+ assert.equal(rt.state.recoveryMarker, 'fixed file')+ rt.state.prefs.marker = 'saved after repair'+ await rt.save()+ 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)+ 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' })+ return originalWrite(path, ...args)+ })+ try {+ await rt.load()+ assert.match(rt.readOnly(), /cannot lock the Bot team.*EACCES/)+ rt.state.prefs.marker = 'not saved'+ await rt.save()+ assert.equal(fs.existsSync(statePath), false)+ } finally {+ restore()+ }+})++test('a queued save rejects after another host takes the lock', async (t) => {+ const { rt, home, statePath } = runtime(t)+ await rt.load()+ await rt.save()+ let entered+ let unblock+ const atMkdir = new Promise(resolve => { entered = resolve })+ const gate = new Promise(resolve => { unblock = resolve })+ let paused = false+ const restore = patch('mkdir', originalMkdir => async (path, ...args) => {+ if (path === home && !paused) {+ paused = true+ entered()+ await gate+ }+ return originalMkdir(path, ...args)+ })+ try {+ rt.state.prefs.marker = 'old owner'+ const active = rt.save()+ await atMkdir+ const queued = rt.save()+ fs.writeFileSync(join(home, 'state.lock'), JSON.stringify({ pid: process.ppid, host: hostname(), token: 'new-owner' }))+ const ownerState = JSON.stringify({ version: 1, revision: 900, prefs: { marker: 'new owner' } })+ fs.writeFileSync(statePath, ownerState)+ unblock()+ await assert.rejects(active, { code: 'ELOCKLOST' })+ await assert.rejects(queued, { code: 'ELOCKLOST' })+ assert.equal(fs.readFileSync(statePath, 'utf8'), ownerState)+ assert.equal(JSON.parse(fs.readFileSync(join(home, 'state.lock'), 'utf8')).token, 'new-owner')+ } finally {+ unblock()+ restore()+ }+})++test('dispose waits for a pending save and prevents later writes', async (t) => {+ const { rt, home, statePath, dispose } = runtime(t)+ await rt.load()+ await rt.save()+ let entered+ let unblock+ const atRename = new Promise(resolve => { entered = resolve })+ const gate = new Promise(resolve => { unblock = resolve })+ const restore = patch('rename', originalRename => async (from, to) => {+ if (to === statePath) {+ entered()+ await gate+ }+ return originalRename(from, to)+ })+ try {+ rt.state.prefs.marker = 'pending save'+ const pending = rt.save()+ await atRename+ const disposing = dispose()+ assert.equal(fs.existsSync(join(home, 'state.lock')), true)+ const revision = rt.state.revision+ const afterDispose = rt.save()+ assert.equal(rt.state.revision, revision)+ unblock()+ await pending+ await disposing+ await afterDispose+ assert.equal(fs.existsSync(join(home, 'state.lock')), false)+ const saved = fs.readFileSync(statePath, 'utf8')+ await rt.save()+ assert.equal(rt.state.revision, revision)+ assert.equal(fs.readFileSync(statePath, 'utf8'), saved)+ } finally {+ unblock()+ restore()+ }+})diff --git a/ds-bot/test/usage.test.js b/ds-bot/test/usage.test.jsindex 0a31d875..bd8388b8 100644--- a/ds-bot/test/usage.test.js+++ b/ds-bot/test/usage.test.js@@ -220,7 +220,10 @@ test('a subagent Session is read through its parent, even a parent that is gone;assert.equal(totals(view)['other/chat'], 44 + 9)// No parent on record: it cannot be addressed, so it is not asked for.assert.ok(!pages.includes('s-lost'))- const unread = () => warnings.filter(([template]) => template.includes('usage could not read')).map(([, sessionId]) => sessionId)+ const unread = () => warnings+ .map(([template, ...args]) => template === '%s' ? args[0] : template)+ .filter(line => line.includes('usage could not read'))+ .map(line => line.match(/usage could not read (\S+):/)?.[1])assert.deepEqual(unread(), ['s-broken'])t.mock.timers.tick(60_000)