Skip to content

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

Fix three test/dev-tooling defects on Node 22 and Windows

lulu feizhud9af38d58a96
diff --git a/ds-bot/dev/mock-llm.mjs b/ds-bot/dev/mock-llm.mjs
index eb99cdab..8cb930a9 100644
--- a/ds-bot/dev/mock-llm.mjs
+++ b/ds-bot/dev/mock-llm.mjs
@@ -160,7 +160,9 @@ function decide(body) {
// "长任务 N [K]": N steps in one turn, each adding about K thousand characters (default 4).
const openerAt = messages.findLastIndex(m => m.role === 'user' && !isResults(m) && /^长任务\s*\d+/.test(textOf(m.content)))
if (openerAt !== -1 && used?.name === 'list_bots') {
- const [, steps, size = 4] = /^长任务\s*(\d+)(?:\s+(\d+))?/.exec(textOf(messages[openerAt].content)).map(Number)
+ const task = /^长任务\s*(\d+)(?:\s+(\d+))?/.exec(textOf(messages[openerAt].content))
+ const steps = Number(task[1])
+ const size = Number(task[2] ?? 4)
const done = messages.slice(openerAt + 1).filter(isResults).length
if (done < steps) return [text(filler(size * 1000, `步骤${done + 1}`)), tool('list_bots', {})]
return [text(`长任务做完了,一共 ${done} 步。`)]
@@ -235,7 +237,9 @@ function decide(body) {
// interrupt or a relayed note); only a fresh opener or "[Stopped by" ends it.
const longOpener = messages.findLastIndex(m => m.role === 'user' && !isResults(m) && /^长任务\s*\d+/.test(textOf(m.content)))
if (longOpener !== -1 && /^\[(?:Interrupt|Message|Reply) from /.test(userText)) {
- const [, steps, size = 4] = /^长任务\s*(\d+)(?:\s+(\d+))?/.exec(textOf(messages[longOpener].content)).map(Number)
+ const task = /^长任务\s*(\d+)(?:\s+(\d+))?/.exec(textOf(messages[longOpener].content))
+ const steps = Number(task[1])
+ const size = Number(task[2] ?? 4)
const done = messages.slice(longOpener + 1).filter(isResults).length
if (done < steps) return [text(filler(size * 1000, `步骤${done}`)), tool('list_bots', {})]
}
diff --git a/ds-bot/test/mock-llm.test.js b/ds-bot/test/mock-llm.test.js
new file mode 100644
index 00000000..ce139039
--- /dev/null
+++ b/ds-bot/test/mock-llm.test.js
@@ -0,0 +1,70 @@
+import assert from 'node:assert/strict'
+import { spawn } from 'node:child_process'
+import { mkdtempSync, rmSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { test } from 'node:test'
+import { fileURLToPath } from 'node:url'
+
+const PORT = 8917
+const url = `http://127.0.0.1:${PORT}/v1/messages`
+const MOCK = fileURLToPath(new URL('../dev/mock-llm.mjs', import.meta.url))
+
+// One Anthropic-shaped request, its SSE reply folded into { text, tools }.
+async function call(messages) {
+ const res = await fetch(url, {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ model: 'mock', stream: true, tools: [{ name: 'list_bots' }], messages }),
+ })
+ assert.equal(res.status, 200)
+ const events = (await res.text()).split('\n\n')
+ .map(chunk => /^data: (.+)$/m.exec(chunk)?.[1]).filter(Boolean).map(line => JSON.parse(line))
+ let text = ''
+ const tools = []
+ for (const event of events) {
+ if (event.type === 'content_block_start' && event.content_block.type === 'tool_use') tools.push({ id: event.content_block.id, name: event.content_block.name })
+ if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') text += event.delta.text
+ }
+ return { text, tools }
+}
+
+// Append the reply and its tool results, as the next model request would carry them.
+function fold(messages, out) {
+ messages.push(
+ { role: 'assistant', content: [...(out.text ? [{ type: 'text', text: out.text }] : []), ...out.tools.map(used => ({ type: 'tool_use', id: used.id, name: used.name, input: {} }))] },
+ { role: 'user', content: out.tools.map(used => ({ type: 'tool_result', tool_use_id: used.id, content: '[]' })) },
+ )
+}
+
+// "长任务 N [K]" adds about K×1000 characters per step, defaulting to 4. A missing K once
+// parsed as NaN, so every step after the opener silently sent no text block.
+test('长任务 writes text on every step when the step size is left out', async (t) => {
+ const home = mkdtempSync(join(tmpdir(), 'dsb-mock-'))
+ const mock = spawn(process.execPath, [MOCK], {
+ env: { ...process.env, MOCK_PORT: String(PORT), MOCK_LOG: join(home, 'mock.jsonl'), MOCK_DELAY_MS: '5', MOCK_JITTER_MS: '0' },
+ })
+ t.after(() => { mock.kill(); rmSync(home, { recursive: true, force: true }) })
+ await new Promise((resolve, reject) => {
+ mock.stdout.on('data', chunk => { if (String(chunk).includes('/v1')) resolve() })
+ mock.once('exit', code => reject(new Error(`mock-llm exited ${code}`)))
+ })
+
+ const messages = [{ role: 'user', content: '长任务 3' }]
+ let out = await call(messages)
+ assert.equal(out.text.length, 4000) // 步骤0
+ fold(messages, out)
+
+ out = await call(messages)
+ assert.equal(out.text.length, 4000) // a tool result continues the task
+ fold(messages, out)
+
+ // A spliced Bot message mid-task hits the other parse of the same command.
+ messages.push({ role: 'user', content: '[Message from HelperBot] 进度如何?' })
+ out = await call(messages)
+ assert.equal(out.text.length, 4000)
+ fold(messages, out)
+
+ out = await call(messages)
+ assert.match(out.text, /长任务做完了,一共 3 步。/)
+})
diff --git a/ds-bot/test/secrets.test.js b/ds-bot/test/secrets.test.js
index 878047d4..e7a55c35 100644
--- a/ds-bot/test/secrets.test.js
+++ b/ds-bot/test/secrets.test.js
@@ -149,7 +149,8 @@ test('a card saves a key the Bot never sees, and the shell gets it', async (t) =
assert.match((await bench.call('secret-set', { requestId: request.id, value })).error.message, /already answered/)
const file = join(bench.home, 'secrets.json')
- assert.equal(statSync(file).mode & 0o777, 0o600)
+ // Windows has no POSIX mode bits: a writable file always stats as 0o666 there.
+ if (process.platform !== 'win32') assert.equal(statSync(file).mode & 0o777, 0o600)
assert.equal(JSON.parse(readFileSync(file, 'utf8')).secrets[0].value, value)
assert.deepEqual(readdirSync(bench.home).filter(name => name.endsWith('.tmp')), [])
team = await bench.view()
diff --git a/ds-bot/web-search.js b/ds-bot/web-search.js
index 8fb95f9f..b99041f1 100644
--- a/ds-bot/web-search.js
+++ b/ds-bot/web-search.js
@@ -472,6 +472,15 @@ export const ENGINES = {
const aborted = signal => new WebError('web search aborted', 'WEB_ABORTED', { cause: signal?.reason })
+// AbortSignal.timeout's wait is unref'd, so under Node 22's test runner a pending subtest
+// is cancelled as soon as nothing ref'd holds the loop — and a mocked fetch holds nothing.
+// A plain setTimeout keeps the loop honest; the abort reason stays a TimeoutError.
+const timeoutAfter = (ms) => {
+ const timeout = new AbortController()
+ const timer = setTimeout(() => timeout.abort(new DOMException('The operation timed out.', 'TimeoutError')), ms)
+ return { timeout, timer }
+}
+
function checkSearchApi(api, index) {
const where = `web-search-auto: searchApis[${index}]`
if (!isRecord(api)) throw new Error(`${where} must be an object`)
@@ -551,7 +560,7 @@ export function createAutoSearchProvider(deps, config = {}) {
async function attempt(candidate, route, credential, request, signal, budget, known) {
const call = candidate.request({ query: request.query, maxResults: request.maxResults })
- const timeout = AbortSignal.timeout(budget)
+ const { timeout, timer } = timeoutAfter(budget)
let response
let text
try {
@@ -561,12 +570,14 @@ export function createAutoSearchProvider(deps, config = {}) {
redirect: 'error',
headers: { 'content-type': 'application/json', accept: 'application/json', 'user-agent': USER_AGENT, ...route.headers, ...call.headers, ...credential.headers(candidate) },
body: JSON.stringify(call.body),
- signal: signal === undefined ? timeout : AbortSignal.any([signal, timeout]),
+ signal: signal === undefined ? timeout.signal : AbortSignal.any([signal, timeout.signal]),
})
text = await response.text()
} catch (error) {
if (signal?.aborted) throw aborted(signal)
- return { reason: timeout.aborted ? `timed out after ${seconds(budget)}` : `request failed: ${reasonOf(error)}` }
+ return { reason: timeout.signal.aborted ? `timed out after ${seconds(budget)}` : `request failed: ${reasonOf(error)}` }
+ } finally {
+ clearTimeout(timer)
}
if (!response.ok) {
const detail = errorDetail(text)
@@ -651,14 +662,16 @@ export function createAutoSearchProvider(deps, config = {}) {
const remaining = deadline - now()
if (remaining <= 0) { attempts.push(`${host}: skipped, out of time`); continue }
// Leave later engines a share of the budget when an earlier one hangs.
- const timeout = AbortSignal.timeout(index === options.engines.length - 1 ? remaining : Math.min(remaining, Math.ceil(options.fallbackTimeoutMs / 2)))
+ const { timeout, timer } = timeoutAfter(index === options.engines.length - 1 ? remaining : Math.min(remaining, Math.ceil(options.fallbackTimeoutMs / 2)))
let fetched
try {
- fetched = await fetchPage(engine, url, signal === undefined ? timeout : AbortSignal.any([signal, timeout]))
+ fetched = await fetchPage(engine, url, signal === undefined ? timeout.signal : AbortSignal.any([signal, timeout.signal]))
} catch (error) {
if (signal?.aborted) throw aborted(signal)
- attempts.push(`${host}: ${timeout.aborted ? 'timed out' : `${error?.code ? `${error.code} ` : ''}${reasonOf(error)}`}`)
+ attempts.push(`${host}: ${timeout.signal.aborted ? 'timed out' : `${error?.code ? `${error.code} ` : ''}${reasonOf(error)}`}`)
continue
+ } finally {
+ clearTimeout(timer)
}
const at = new URL(fetched.url).host
if (fetched.page.statusCode !== 200) { attempts.push(`${at}: HTTP ${fetched.page.statusCode}`); continue }