A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Index Sessions and load keys before a shell can resolve
Fei2411d6587b3b7bb7
diff --git a/ds-bot/src/host/secrets.js b/ds-bot/src/host/secrets.jsindex 058deccb..476c5ce7 100644--- a/ds-bot/src/host/secrets.js+++ b/ds-bot/src/host/secrets.js@@ -3,7 +3,8 @@// 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, copyFile, mkdir, readFile, unlink, writeFile } from 'node:fs/promises'+import { chmodSync, copyFileSync, readFileSync } from 'node:fs'+import { chmod, mkdir, unlink, writeFile } from 'node:fs/promises'import { join } from 'node:path'import { defineTool } from '@deepseek-ai/dsh-tools'import { replaceFile } from './files.js'@@ -118,11 +119,13 @@ export function install(rt) {if (loaded !== null && (loadedReadOnly || rt.readOnly() !== undefined || blocked !== undefined)) loaded = nullreturn loaded ??= readSecrets()}+ // Synchronous on purpose: a shell resolves its environment without an await, so the+ // map must be full before the first resolve after a start reads it.const readSecrets = async () => {loadedReadOnly = rt.readOnly() !== undefinedlet contentstry {- contents = await readFile(path, 'utf8')+ contents = readFileSync(path, 'utf8')} catch (error) {if (error?.code === 'ENOENT') {blocked = undefined@@ -145,7 +148,7 @@ export function install(rt) {// the store goes on with an empty one.const backup = `${path}.broken-${Date.now()}`try {- await copyFile(path, backup)+ copyFileSync(path, backup)blocked = undefinedsecrets.clear()rt.warn('ds-bot: unreadable secrets file %s; backed up as %s', path, backup)@@ -171,7 +174,7 @@ export function install(rt) {...(Number.isFinite(entry.lastUsedAt) ? { lastUsedAt: entry.lastUsedAt } : {}),})}- if (!loadedReadOnly) await chmod(path, 0o600).catch(() => {})+ if (!loadedReadOnly) try { chmodSync(path, 0o600) } catch {}changed()}diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex a20a0b3e..3518a751 100644--- a/ds-bot/src/host/store.js+++ b/ds-bot/src/host/store.js@@ -1,8 +1,8 @@// Store: the saved team (state.json), its migrations, the lock that keeps a second dsh// 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, unlink, writeFile } from 'node:fs/promises'+import { copyFileSync, mkdirSync, readFileSync, statSync } from 'node:fs'+import { mkdir, 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'@@ -63,10 +63,12 @@ export function install(rt) {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 () => {+ // Synchronous on purpose: the saved team and the session indexes built from it+ // exist before the first tool or shell env call can resolve a Session.+ const readFileState = () => {let contentstry {- contents = await readFile(statePath, 'utf8')+ contents = readFileSync(statePath, 'utf8')} catch (error) {seen = stamp()if (error?.code === 'ENOENT') {@@ -84,7 +86,7 @@ export function install(rt) {} catch (error) {const backup = `${statePath}.broken-${Date.now()}`try {- await copyFile(statePath, backup)+ copyFileSync(statePath, backup)stuck = undefinedwarn('ds-bot: unreadable state %s: %s; backed up as %s', statePath, error, backup)} catch (copyError) {@@ -230,7 +232,7 @@ export function install(rt) {const load = () => loaded ??= (async () => {claimLock()if (holder !== undefined) warn('ds-bot: %s', readOnly())- const { migrated, stale } = adopt(await readFileState())+ const { migrated, stale } = adopt(readFileState())if (holder !== undefined) returnif (migrated && stuck === undefined) {// A failed write must not poison `loaded`: the migrated state stays in@@ -254,7 +256,7 @@ export function install(rt) {if (holder === undefined) {warn('ds-bot: the other dsh has closed; this one now saves the Bot team')warnedReadOnly = false- adopt(await readFileState())+ adopt(readFileState())return}} else if (!lockBroken && !lock.owns()) {@@ -267,12 +269,12 @@ export function install(rt) {// The lock was free: still the writer, but the file may carry changes// written while no host held it.warnedReadOnly = false- adopt(await readFileState())+ adopt(readFileState())return}warn('ds-bot: %s', readOnly())}- if ((stuck !== undefined || holder !== undefined) && stamp() !== seen) adopt(await readFileState())+ if ((stuck !== undefined || holder !== undefined) && stamp() !== seen) adopt(readFileState())}const warnReadOnly = () => {diff --git a/ds-bot/test/secrets.test.js b/ds-bot/test/secrets.test.jsindex 373bafe8..291e3808 100644--- a/ds-bot/test/secrets.test.js+++ b/ds-bot/test/secrets.test.js@@ -354,6 +354,19 @@ test('tool results and model requests lose the values', async (t) => {assert.equal(passed.length, 2)})+test('a restarted host resolves its team Sessions and their keys before the first shell call', (t) => {+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-secrets-'))+ const value = fresh()+ writeFileSync(join(home, 'secrets.json'), JSON.stringify({ version: 1, secrets: [{ name: 'DEMO_TOKEN', value, purpose: 'p', scope: 'all', requestedBy: 'b2', createdAt: 1, updatedAt: 1 }] }))+ const bench = start({ home })+ t.after(bench.stop)+ // apply() has just returned: no endpoint or tool has run yet, but a restored+ // Session's first shell call already resolves its Bot and its variables.+ assert.deepEqual(bench.env('b2'), { DSH_SECRET_DEMO_TOKEN: value })+ assert.deepEqual(bench.env('g-b2'), { DSH_SECRET_DEMO_TOKEN: value })+ assert.deepEqual(bench.env('elsewhere'), {})+})+test('a secrets file that cannot be read blocks every write instead of being overwritten', async (t) => {// A directory reads as EISDIR on every platform; no chmod games needed. It has to// exist before the plugin loads, or the first read has already found the file empty.diff --git a/ds-bot/test/store.test.js b/ds-bot/test/store.test.jsindex 03cd46f6..9ac0765a 100644--- a/ds-bot/test/store.test.js+++ b/ds-bot/test/store.test.js@@ -120,7 +120,7 @@ test('malformed state is backed up before a later save', async (t) => {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 () => {+ const restore = patchSync('copyFileSync', () => () => {throw Object.assign(new Error('synthetic copy failure'), { code: 'EACCES' })})try {@@ -148,7 +148,7 @@ test('a state from a newer version remains untouched', async (t) => {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) => {+ const restore = patchSync('readFileSync', originalRead => (path, ...args) => {if (path === statePath) throw Object.assign(new Error('synthetic read failure'), { code: 'EACCES' })return originalRead(path, ...args)})