Skip to content

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

Write nothing when the first run is quit before it finishes

Fei24117a3fc802c2c2
diff --git a/app/src/first-run.mjs b/app/src/first-run.mjs
index 7d0efb51..37093bb1 100644
--- a/app/src/first-run.mjs
+++ b/app/src/first-run.mjs
@@ -1,7 +1,7 @@
// The first start: where the app keeps its data and the Bots' files, and whether it copies the
// Bot team of the user's own DSH. The answers are kept in the app's own data.
import { app, dialog } from 'electron'
-import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { homedir } from 'node:os'
import { dirname, join } from 'node:path'
import { findTeam, newerSessions, teamInUse, userDshHome, within } from './import.mjs'
@@ -50,17 +50,24 @@ export const localeText = table => (app.getLocale().toLowerCase().startsWith('zh
export const text = () => localeText(TEXT)
const PROFILE_NAMES = { desktop: 'DSH Desktop', web: 'DSH Web' }
-const ask = options => dialog.showMessageBoxSync({ title: 'DeepSeek Bot', noLink: true, ...options })
const locationsFile = () => join(app.getPath('userData'), 'locations.json')
-/** Ask for the places and the copy again at the next start, as after quitting the first start. */
-export function forgetLocations() {
- rmSync(locationsFile(), { force: true })
+// A quit request while a first-run dialog is open stops the first run right after that dialog.
+class Quit extends Error {}
+
+async function ask(options, signal) {
+ const { response } = await dialog.showMessageBox({ title: 'DeepSeek Bot', noLink: true, ...options, signal })
+ // A quit that arrived while a synchronous macOS box was open gets to run first.
+ await new Promise(resolve => setImmediate(resolve))
+ if (signal.aborted) throw new Quit()
+ return response
}
-function pick(title, current) {
- const picked = dialog.showOpenDialogSync({ title, defaultPath: existsSync(current) ? current : dirname(current), properties: ['openDirectory', 'createDirectory', 'promptToCreate'] })
- return picked?.[0] ?? current
+async function pick(title, current, signal) {
+ const { canceled, filePaths } = await dialog.showOpenDialog({ title, defaultPath: existsSync(current) ? current : dirname(current), properties: ['openDirectory', 'createDirectory', 'promptToCreate'] })
+ await new Promise(resolve => setImmediate(resolve))
+ if (signal.aborted) throw new Quit()
+ return canceled ? current : filePaths[0]
}
function placeProblem(t, dataDir, workspace) {
@@ -83,7 +90,7 @@ function hasTeam(dataDir) {
}
}
-function offerImport(t, dataDir) {
+async function offerImport(t, dataDir, signal) {
const from = userDshHome()
let found
try {
@@ -92,27 +99,34 @@ function offerImport(t, dataDir) {
console.error(error)
}
if (found === undefined) return undefined
- if (ask({ type: 'question', message: t.found(found.bots, found.rooms), detail: t.foundDetail(from), buttons: [t.copy, t.skip], defaultId: 0, cancelId: 1 }) === 1) return undefined
+ if (await ask({ type: 'question', message: t.found(found.bots, found.rooms), detail: t.foundDetail(from), buttons: [t.copy, t.skip], defaultId: 0, cancelId: 1 }, signal) === 1) return undefined
while (teamInUse(from)) {
- if (ask({ type: 'warning', message: t.inUse, buttons: [t.retry, t.skip], defaultId: 0, cancelId: 1 }) === 1) return undefined
+ if (await ask({ type: 'warning', message: t.inUse, buttons: [t.retry, t.skip], defaultId: 0, cancelId: 1 }, signal) === 1) return undefined
}
const newer = newerSessions(from, found.team)
if (newer.length > 0) {
- if (ask({ type: 'warning', message: t.newer(newer.length), buttons: [t.quit, t.startEmpty], defaultId: 0, cancelId: 0 }) === 0) return { quit: true }
+ if (await ask({ type: 'warning', message: t.newer(newer.length), buttons: [t.quit, t.startEmpty], defaultId: 0, cancelId: 0 }, signal) === 0) return { quit: true }
return undefined
}
const { profiles } = found
const profile = profiles.length < 2 ? profiles[0]?.name
- : profiles[ask({ type: 'question', message: t.whichModels, buttons: profiles.map(entry => PROFILE_NAMES[entry.name] ?? entry.name), defaultId: 0, cancelId: 0 })].name
+ : profiles[await ask({ type: 'question', message: t.whichModels, buttons: profiles.map(entry => PROFILE_NAMES[entry.name] ?? entry.name), defaultId: 0, cancelId: 0 }, signal)].name
return { from, profile }
}
+/** Write the chosen places once the first run is finished; quitting before that asks again. */
+export function saveLocations({ dataDir, workspace }) {
+ const file = locationsFile()
+ mkdirSync(dirname(file), { recursive: true })
+ writeFileSync(file, `${JSON.stringify({ dataDir, workspace }, null, 2)}\n`)
+}
+
/**
* Where the app keeps its data and the Bots' files, asked at the first start.
* A run on throwaway folders (DS_BOT_HOME, for development and tests) asks nothing and copies nothing.
- * @returns {{ dataDir: string, workspace?: string, import?: { from: string, profile?: string }, quit?: true }}
+ * @returns {Promise<{ dataDir: string, workspace?: string, import?: { from: string, profile?: string }, quit?: true }>}
*/
-export function chooseLocations() {
+export async function chooseLocations(signal) {
if (process.env.DS_BOT_HOME) return { dataDir: process.env.DS_BOT_HOME, workspace: process.env.DS_BOT_WORKSPACE || undefined }
const file = locationsFile()
if (existsSync(file)) {
@@ -122,20 +136,27 @@ export function chooseLocations() {
const t = text()
let dataDir = join(homedir(), '.ds-bot')
let workspace = join(homedir(), 'DeepSeek Bot')
- for (;;) {
- const choice = ask({ type: 'info', message: t.welcome, detail: t.places(dataDir, workspace), buttons: [t.start, t.change], defaultId: 0, cancelId: 0 })
- if (choice === 1) {
- dataDir = pick(t.pickData, dataDir)
- workspace = pick(t.pickWorkspace, workspace)
- continue
+ try {
+ for (;;) {
+ const choice = await ask({ type: 'info', message: t.welcome, detail: t.places(dataDir, workspace), buttons: [t.start, t.change, t.quit], defaultId: 0, cancelId: 2 }, signal)
+ if (choice === 2) return { quit: true }
+ if (choice === 1) {
+ dataDir = await pick(t.pickData, dataDir, signal)
+ workspace = await pick(t.pickWorkspace, workspace, signal)
+ continue
+ }
+ const problem = placeProblem(t, dataDir, workspace)
+ if (problem === undefined) break
+ await ask({ type: 'warning', message: problem, buttons: ['OK'] }, signal)
}
- const problem = placeProblem(t, dataDir, workspace)
- if (problem === undefined) break
- ask({ type: 'warning', message: problem, buttons: ['OK'] })
+ const imported = await offerImport(t, dataDir, signal)
+ if (imported?.quit) return { quit: true }
+ if (imported !== undefined) return { dataDir, workspace, import: imported }
+ } catch (error) {
+ if (error instanceof Quit) return { quit: true }
+ throw error
}
- const imported = offerImport(t, dataDir)
- if (imported?.quit) return { dataDir, workspace, quit: true }
- mkdirSync(dirname(file), { recursive: true })
- writeFileSync(file, `${JSON.stringify({ dataDir, workspace }, null, 2)}\n`)
- return { dataDir, workspace, ...(imported ? { import: imported } : {}) }
+ // There is nothing to copy, so the first run ends with the chosen places.
+ saveLocations({ dataDir, workspace })
+ return { dataDir, workspace }
}
diff --git a/app/src/import.mjs b/app/src/import.mjs
index cfa419f0..68cb5921 100644
--- a/app/src/import.mjs
+++ b/app/src/import.mjs
@@ -154,16 +154,21 @@ async function writeModelRows(to, rows) {
* so that DSH does not take the copied Sessions' folders for its own projects. The old team's
* workspace files go into `workspace`, without replacing a file that is there.
* Everything but the keys, the model settings and the workspace files is put together in a
- * folder beside the data and moved in at the end; a failure leaves that folder behind.
- * @param {{ from: string, to: string, workspace: string, profile?: string, dsh?: string }} options
+ * folder beside the data and moved in at the end; a copy that stopped or failed leaves that
+ * folder behind, and the next copy removes it.
+ * @param {{ from: string, to: string, workspace: string, profile?: string, dsh?: string, signal?: AbortSignal }} options
*/
-export async function importTeam({ from, to, workspace, profile, dsh }) {
+export async function importTeam({ from, to, workspace, profile, dsh, signal }) {
const found = findTeam(from)
if (found === undefined) throw new Error(`There is no Bot team in ${from}`)
const present = existsSync(join(to, 'bot', 'state.json')) ? readJson(join(to, 'bot', 'state.json')) : undefined
if (Object.keys(present?.bots ?? {}).length > 0) throw new Error(`${to} already has a Bot team`)
+ for (const name of readdirSync(to).filter(entry => /^\.import-\d+$/u.test(entry))) {
+ await rm(join(to, name), { recursive: true, force: true })
+ }
const stage = join(to, `.import-${String(Date.now())}`)
const oldBot = join(from, 'bot')
+ signal?.throwIfAborted()
await cp(oldBot, join(stage, 'bot'), { recursive: true, filter: path => !LEFT_BEHIND.has(relative(oldBot, path).split(/[\\/]/u)[0]) })
const team = readJson(join(stage, 'bot', 'state.json'))
// The new data location registers its own team workspace. A DSH Agent's Sessions do not move
@@ -172,20 +177,25 @@ export async function importTeam({ from, to, workspace, profile, dsh }) {
team.agents = {}
await writeFile(join(stage, 'bot', 'state.json'), JSON.stringify(team, null, 2))
const { found: sessions, missing } = sessionDirs(from, sessionIdsOf(found.team))
- await copySessionDirs(from, stage, sessions)
+ signal?.throwIfAborted()
+ await copySessionDirs(from, stage, sessions, signal)
+ signal?.throwIfAborted()
if (existsSync(join(from, 'attachments'))) await cp(join(from, 'attachments'), join(stage, 'attachments'), { recursive: true })
+ signal?.throwIfAborted()
await mkdir(workspace, { recursive: true })
if (existsSync(join(oldBot, 'workspace'))) await cp(join(oldBot, 'workspace'), workspace, { recursive: true, force: false })
+ signal?.throwIfAborted()
const keys = []
for (const name of ['.credentials.yaml', '.env']) if (await copyKey(from, to, name)) keys.push(name)
const rows = found.profiles.find(entry => entry.name === profile)?.rows ?? []
if (rows.length > 0) await writeModelRows(to, rows)
- await rm(join(to, 'bot'), { recursive: true, force: true })
- await rename(join(stage, 'bot'), join(to, 'bot'))
+ signal?.throwIfAborted()
for (const { id, group } of sessions) {
await mkdir(join(to, 'sessions', group), { recursive: true })
+ // A copy that stopped can have moved the Session in already.
+ await rm(join(to, 'sessions', group, id), { recursive: true, force: true })
await rename(join(stage, 'sessions', group, id), join(to, 'sessions', group, id))
}
if (existsSync(join(stage, 'attachments'))) await cp(join(stage, 'attachments'), join(to, 'attachments'), { recursive: true, force: false })
@@ -194,7 +204,10 @@ export async function importTeam({ from, to, workspace, profile, dsh }) {
bots: found.bots, rooms: found.rooms, sessions: sessions.length, missingSessions: missing,
keys, modelSettings: rows.length > 0 ? profile : null,
}
- await writeFile(join(to, 'bot', 'imported.json'), `${JSON.stringify(summary, null, 2)}\n`)
+ await writeFile(join(stage, 'bot', 'imported.json'), `${JSON.stringify(summary, null, 2)}\n`)
+ // Until this rename the data has no Bot team, so a copy that stopped can run again.
+ await rm(join(to, 'bot'), { recursive: true, force: true })
+ await rename(join(stage, 'bot'), join(to, 'bot'))
await rm(stage, { recursive: true, force: true })
return summary
}
diff --git a/app/src/main.mjs b/app/src/main.mjs
index 82980569..612b206f 100644
--- a/app/src/main.mjs
+++ b/app/src/main.mjs
@@ -2,7 +2,7 @@
// The dsh-app scheme, the request forwarding and the WebSocket header rewrite follow
// DeepSeek Harness apps/desktop/src/main.ts (MIT); see NOTICE.
import { app, BrowserWindow, dialog, ipcMain, Menu, nativeTheme, protocol, session, shell } from 'electron'
-import { mkdirSync, writeFileSync } from 'node:fs'
+import { mkdirSync, readdirSync, writeFileSync } from 'node:fs'
import { rm } from 'node:fs/promises'
import { createRequire } from 'node:module'
import { dirname, join } from 'node:path'
@@ -10,7 +10,7 @@ import { setTimeout as sleep } from 'node:timers/promises'
import { fileURLToPath } from 'node:url'
import { getDshRuntimeVersion } from '@deepseek-ai/dsh-app-boot'
import electronUpdater from 'electron-updater'
-import { chooseLocations, forgetLocations, localeText, text } from './first-run.mjs'
+import { chooseLocations, localeText, saveLocations, text } from './first-run.mjs'
import { HostProcess } from './host-process.mjs'
import { copySessions, findTeam, importTeam } from './import.mjs'
import { CHECK_PROFILE, PROFILE } from './profile.mjs'
@@ -43,9 +43,11 @@ let updates
let stopping = false
let stopped = false
let failed = false
-// Quitting waits for the first-run check, so that its copy of the team's chats goes as well.
-let checking
-const checkAbort = new AbortController()
+// Quitting while a first-run dialog is open stops the first run, and during the copy it waits
+// for the copy to stop at a safe point, so that it leaves no half team and no temporary copy.
+let choosing
+let copying
+const copyAbort = new AbortController()
if (!app.requestSingleInstanceLock()) {
app.exit(0)
@@ -53,11 +55,16 @@ if (!app.requestSingleInstanceLock()) {
app.on('second-instance', focusWindow)
app.on('window-all-closed', () => { app.quit() })
app.on('before-quit', (event) => {
- if (checking !== undefined) {
+ if (choosing !== undefined) {
event.preventDefault()
- if (checkAbort.signal.aborted) return
- checkAbort.abort()
- void checking.finally(() => { checking = undefined; app.quit() })
+ choosing.abort()
+ return
+ }
+ if (copying !== undefined) {
+ event.preventDefault()
+ if (copyAbort.signal.aborted) return
+ copyAbort.abort()
+ void copying.finally(() => { copying = undefined; app.quit() })
return
}
if (stopped || host === undefined) return
@@ -74,8 +81,9 @@ function stopHost() {
return hostStop
}
-function start() {
- const where = chooseLocations()
+async function start() {
+ choosing = new AbortController()
+ const where = await chooseLocations(choosing.signal).finally(() => { choosing = undefined })
if (where.quit) { app.exit(0); return }
const pnpm = join(app.getAppPath(), 'node_modules', 'pnpm', 'bin', 'pnpm.mjs')
const profileDir = join(where.dataDir, 'profiles', PROFILE)
@@ -94,11 +102,13 @@ function start() {
return new HostProcess({ entry: join(here, 'host.mjs'), cwd, env: { ...process.env, DSH_HOME: home, DS_BOT_CHECK: list }, pnpm, onFailure: () => {} })
}
const ready = (async () => {
- const copied = where.import ? await copyTeam(where, newHost, newCheck) : undefined
- if (copied === 'quit') {
- forgetLocations()
- app.exit(0)
+ let copied
+ if (where.import) {
+ copying = copyTeam(where, newHost, newCheck, copyAbort.signal)
+ copied = await copying
+ copying = undefined
}
+ if (copied === 'quit') app.exit(0)
// The window may be waiting for the Host; it goes away with the app.
if (copied !== undefined) return new Promise(() => {})
// Not during the import, which a restart would cut short; before the Host, so that an
@@ -150,36 +160,47 @@ function start() {
// Before that, the app's own DSH reads every chat of the team once: a newer DSH can write
// records that this one refuses while the log format stays the same. Such a chat stops the
// copy, as a newer log format does; 'quit' means the user updates DeepSeek Bot first, and
-// 'stopped' that the app quit during the check.
-async function copyTeam(where, newHost, newCheck) {
- checking = checkTeam(where, newCheck, checkAbort.signal)
- const unreadable = await checking
- if (checkAbort.signal.aborted) return 'stopped'
- checking = undefined
+// 'stopped' that the app quit during the copy.
+async function copyTeam(where, newHost, newCheck, signal) {
+ const unreadable = await checkTeam(where, newCheck, signal)
+ if (signal.aborted) return 'stopped'
if (unreadable > 0) {
const t = text()
const { response } = await dialog.showMessageBox({ type: 'warning', title: 'DeepSeek Bot', noLink: true, message: t.newer(unreadable), buttons: [t.quit, t.startEmpty], defaultId: 0, cancelId: 0 })
- return response === 0 ? 'quit' : undefined
+ if (response === 0) return 'quit'
+ saveLocations(where)
+ return undefined
}
- host = newHost()
+ const warm = newHost()
+ host = warm
+ const stopWarm = () => { void warm.stop() }
+ signal.addEventListener('abort', stopWarm, { once: true })
try {
- await host.start()
+ await warm.start()
+ if (signal.aborted) return 'stopped'
mainWindow?.setProgressBar(2)
- await host.stop()
+ await warm.stop()
host = undefined
- const summary = await importTeam({ ...where.import, to: where.dataDir, workspace: where.workspace, dsh: getDshRuntimeVersion() })
+ const summary = await importTeam({ ...where.import, to: where.dataDir, workspace: where.workspace, dsh: getDshRuntimeVersion(), signal })
console.log(`DeepSeek Bot: copied ${String(summary.bots)} Bots and ${String(summary.sessions)} Sessions from ${summary.from}`)
} catch (error) {
+ if (signal.aborted) return 'stopped'
console.error(error)
await dialog.showMessageBox({ type: 'warning', title: 'DeepSeek Bot', message: text().failed(error instanceof Error ? error.message : String(error)) })
} finally {
+ signal.removeEventListener('abort', stopWarm)
mainWindow?.setProgressBar(-1)
}
+ saveLocations(where)
return undefined
}
// A copy of the chats in a DSH home of its own, so that nothing of the check stays in the data.
async function checkTeam(where, newCheck, signal) {
+ // A run that was killed can leave its temporary check home behind.
+ for (const name of readdirSync(where.dataDir).filter(entry => /^\.import-check-\d+$/u.test(entry))) {
+ await rm(join(where.dataDir, name), { recursive: true, force: true }).catch((error) => { console.error(error) })
+ }
const home = join(where.dataDir, `.import-check-${String(Date.now())}`)
try {
const found = findTeam(where.import.from)
diff --git a/app/test/import.test.mjs b/app/test/import.test.mjs
index 1465b6bb..b02ad06e 100644
--- a/app/test/import.test.mjs
+++ b/app/test/import.test.mjs
@@ -137,6 +137,33 @@ describe('importTeam', () => {
})
})
+describe('a copy that stopped', () => {
+ let root, from
+ before(() => {
+ root = mkdtempSync(join(tmpdir(), 'ds-bot-app-stopped-'))
+ from = oldHome(root)
+ })
+ after(() => { rmSync(root, { recursive: true, force: true }) })
+
+ test('a copy that stopped part way runs again', async () => {
+ const { to, workspace } = newHome(root)
+ write(join(to, '.import-1', 'junk.txt'), 'left over')
+ write(join(to, 'sessions', '--site--', 'session-b', 'session.v4.jsonl.zstd'), 'junk')
+ const summary = await importTeam({ from, to, workspace })
+ assert.equal(summary.sessions, 5)
+ assert.deepEqual(Object.keys(readJson(join(to, 'bot', 'state.json')).bots), ['session-a', 'session-b'])
+ assert.equal(readFileSync(join(to, 'sessions', '--site--', 'session-b', 'session.v4.jsonl.zstd'), 'utf8'), 'session-b')
+ assert.deepEqual(readdirSync(to).filter(name => /^\.import-\d+$/u.test(name)), [])
+ })
+
+ test('a stopped copy leaves no Bot team', async () => {
+ const to = join(root, 'stopped')
+ mkdirSync(to, { recursive: true })
+ await assert.rejects(importTeam({ from, to, workspace: join(root, 'stopped-files'), signal: AbortSignal.abort() }), { name: 'AbortError' })
+ assert.equal(existsSync(join(to, 'bot')), false)
+ })
+})
+
describe('finding the team', () => {
let root, from
before(() => {