Skip to content

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

Wait for the diagnostics log when the plugin stops

Fei2411cd74bb9d310e
diff --git a/ds-bot/src/host/diagnostics.js b/ds-bot/src/host/diagnostics.js
index 60abbd00..1d27d5c0 100644
--- a/ds-bot/src/host/diagnostics.js
+++ b/ds-bot/src/host/diagnostics.js
@@ -118,6 +118,14 @@ export function install(rt) {
let fresh = []
let writing = Promise.resolve()
let fileFailed = false
+ let stopped = false
+
+ // The log is written in the background. A stop waits for what is queued and writes
+ // nothing after it, so nothing changes in `home` once the plugin has stopped.
+ ctx.effect(() => () => {
+ stopped = true
+ return writing
+ }, 'ds-bot: diagnostics log')
const clean = text => cleanText(text, { redact: rt.redactSecrets })
@@ -135,7 +143,7 @@ export function install(rt) {
let cleared = 0
const append = (entry) => {
- if (!toFile) return
+ if (!toFile || stopped) return
writing = writing.then(async () => {
await mkdir(dirname(logPath), { recursive: true })
const size = await stat(logPath).then(info => info.size, () => 0)
@@ -240,7 +248,7 @@ export function install(rt) {
async function clear() {
cleared = Date.now()
fresh = []
- if (!toFile) return
+ if (!toFile || stopped) return
writing = writing.then(() => writeFile(logPath, '', 'utf8')).catch(() => {})
await writing
}
diff --git a/ds-bot/test/diagnostics.test.js b/ds-bot/test/diagnostics.test.js
index 4b3e33c8..1c378d2a 100644
--- a/ds-bot/test/diagnostics.test.js
+++ b/ds-bot/test/diagnostics.test.js
@@ -33,10 +33,16 @@ test('cleanText hides standalone credentials it was never given', () => {
assert.equal(out, '[hidden] [hidden] [hidden] [hidden] [hidden] [hidden]')
})
+// `stop()` runs the cleanups, as the plugin's stop does.
function host(home, extra = {}) {
const warnings = []
+ const cleanups = []
+ const effect = (execute) => {
+ const cleanup = execute()
+ if (typeof cleanup === 'function') cleanups.push(cleanup)
+ }
const rt = {
- ctx: { logger: { warn: (...args) => warnings.push(args) } },
+ ctx: { logger: { warn: (...args) => warnings.push(args) }, effect },
home,
config: {},
state: { bots: { a: { id: 'a', name: 'Chief', model: 'deepseek/deepseek-chat' } }, mainBotIds: ['a'], rooms: {} },
@@ -44,6 +50,7 @@ function host(home, extra = {}) {
...extra,
}
installDiagnostics(rt)
+ rt.stop = () => Promise.all(cleanups.map(cleanup => cleanup()))
return rt
}
@@ -73,7 +80,7 @@ test('warnings are kept, cleaned, written to the log, and read back after a rest
assert.equal(report.entries[0].code, 'RATE_LIMIT', 'newest first')
// Written as JSON lines; a new host reads them back.
- await new Promise(resolve => setTimeout(resolve, 50))
+ await rt.stop()
assert.equal(readFileSync(join(home, 'logs', 'diagnostics.jsonl'), 'utf8').trim().split('\n').length, 2)
const again = host(home)
assert.deepEqual((await again.diagnostics.entries()).map(entry => entry.code), ['EACCES', 'RATE_LIMIT'])
@@ -133,6 +140,49 @@ test('diagnostics append fallback redacts an error message', async (t) => {
assert.match(stderr.join('\n'), /api_key=\[hidden\]/)
})
+// #226: a test removed `home` right after the stop while a warning was still being
+// written there, and the removal failed with ENOTEMPTY.
+test('a stop waits for the log writes queued before it, and nothing is written after it', async (t) => {
+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-diag-'))
+ t.after(() => rmSync(home, { recursive: true, force: true, maxRetries: 5 }))
+ t.mock.method(console, 'error', () => {})
+ const log = join(home, 'logs', 'diagnostics.jsonl')
+ const originalAppend = fsp.appendFile
+ let reached
+ const appending = new Promise((resolve) => { reached = resolve })
+ let release
+ const gate = new Promise((resolve) => { release = resolve })
+ fsp.appendFile = async (...args) => {
+ reached()
+ await gate
+ return originalAppend(...args)
+ }
+ syncBuiltinESMExports()
+ t.after(() => {
+ fsp.appendFile = originalAppend
+ syncBuiltinESMExports()
+ })
+ const rt = host(home)
+ rt.warn('ds-bot: before the stop')
+ await appending
+ let stopped = false
+ const stopping = rt.stop().then(() => { stopped = true })
+ await Promise.resolve()
+ assert.equal(stopped, false, 'the stop waits for the write in progress')
+ release()
+ await stopping
+ assert.match(readFileSync(log, 'utf8'), /before the stop/)
+
+ rt.warn('ds-bot: after the stop')
+ await rt.diagnostics.clear()
+ // A write queued after the stop would be part of what a second stop waits for.
+ await rt.stop()
+ const lines = readFileSync(log, 'utf8').trim().split('\n')
+ assert.equal(lines.length, 1, 'neither the warning nor the clear reached the file')
+ assert.match(lines[0], /before the stop/)
+ assert.equal((await rt.diagnostics.entries()).length, 0, 'the clear still empties the list in memory')
+})
+
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/install.test.js b/ds-bot/test/install.test.js
index 1f3d7522..1fcfe681 100644
--- a/ds-bot/test/install.test.js
+++ b/ds-bot/test/install.test.js
@@ -34,6 +34,7 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {
t.after(stop)
assert.equal(applyResult, undefined)
assert.deepEqual(ctx.calls, [
+ 'effect ds-bot: diagnostics log',
'effect ds-bot: state lock',
'on llm/adapters-updated',
'on session/event',