A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Copy the group Sessions an earlier import left out
Fei2411ee84a193830a
diff --git a/app/src/import.mjs b/app/src/import.mjsindex 68ab5d81..de230039 100644--- a/app/src/import.mjs+++ b/app/src/import.mjs@@ -111,6 +111,16 @@ async function copySessionDirs(from, to, sessions, signal) {}}+async function moveSessionDirs(stage, to, sessions, signal) {+ for (const { id, group } of sessions) {+ signal?.throwIfAborted()+ 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))+ }+}+/*** Copy the team's Sessions into the DSH home `to`, as they lie in `from`; an abort stops it between Sessions.* @param {string} from @param {any} team @param {string} to @param {AbortSignal} [signal]@@ -123,12 +133,44 @@ export async function copySessions(from, team, to, signal) {}const logVersion = name => parseSessionFormatLogFilename(name.replace(/\.(zstd|zst|gz|br)$/u, ''))+const isNewer = (from, { id, group }) => readdirSync(join(from, 'sessions', group, id))+ .some(name => (logVersion(name) ?? 0) > sessionFormatCatalog.currentVersion)/** The Sessions whose log is in a newer format than the pinned DSH reads. */export function newerSessions(from, team) {const { found } = sessionDirs(from, sessionIdsOf(team))- return found.filter(({ id, group }) => readdirSync(join(from, 'sessions', group, id))- .some(name => (logVersion(name) ?? 0) > sessionFormatCatalog.currentVersion)).map(({ id }) => id)+ return found.filter(session => isNewer(from, session)).map(({ id }) => id)+}++/**+ * Copy into the data location `to` the Sessions its team names that are not there, from the DSH+ * home the team was imported from. Imports before 0.2.3 left out each group's own Session, and+ * such a group does not open (#551). Each Session is put together beside the data and moved in+ * whole; one this DSH cannot read stays where it is. Nothing in the old home changes.+ * @param {string} to @param {AbortSignal} [signal]+ * @returns {Promise<{ copied: string[], newer: string[] }>} the ids copied, and those left for their newer format+ */+export async function restoreSessions(to, signal) {+ const record = join(to, 'bot', 'imported.json')+ const statePath = join(to, 'bot', 'state.json')+ const none = { copied: [], newer: [] }+ if (!existsSync(record) || !existsSync(statePath)) return none+ const { from } = readJson(record)+ if (typeof from !== 'string' || resolve(from) === resolve(to)) return none+ for (const name of readdirSync(to).filter(entry => /^\.restore-\d+$/u.test(entry))) {+ await rm(join(to, name), { recursive: true, force: true })+ }+ const { missing } = sessionDirs(to, sessionIdsOf(readJson(statePath)))+ const { found } = sessionDirs(from, missing)+ const newer = found.filter(session => isNewer(from, session))+ const sessions = found.filter(session => !newer.includes(session))+ if (sessions.length > 0) {+ const stage = join(to, `.restore-${String(Date.now())}`)+ await copySessionDirs(from, stage, sessions, signal)+ await moveSessionDirs(stage, to, sessions, signal)+ await rm(stage, { recursive: true, force: true })+ }+ return { copied: sessions.map(({ id }) => id), newer: newer.map(({ id }) => id) }}async function copyKey(from, to, name) {@@ -194,13 +236,7 @@ export async function importTeam({ from, to, workspace, profile, dsh, signal })const rows = found.profiles.find(entry => entry.name === profile)?.rows ?? []if (rows.length > 0) await writeModelRows(to, rows)- for (const { id, group } of sessions) {- signal?.throwIfAborted()- 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))- }+ await moveSessionDirs(stage, to, sessions, signal)signal?.throwIfAborted()if (existsSync(join(stage, 'attachments'))) await cp(join(stage, 'attachments'), join(to, 'attachments'), { recursive: true, force: false })const summary = {diff --git a/app/src/main.mjs b/app/src/main.mjsindex ffeb108e..5f138aa4 100644--- a/app/src/main.mjs+++ b/app/src/main.mjs@@ -21,7 +21,7 @@ import { chooseLocations, localeText, programFolder, saveLocations, text } fromimport { configureIdentity } from './legacy.mjs'import { hostPort } from './host-port.mjs'import { HostProcess } from './host-process.mjs'-import { copySessions, findTeam, importTeam, teamInUse, userDshHome } from './import.mjs'+import { copySessions, findTeam, importTeam, restoreSessions, teamInUse, userDshHome } from './import.mjs'import { copyData, moveProblem } from './move-data.mjs'import { CHECK_PROFILE, PROFILE } from './profile.mjs'import { createShell, SHELL_TEXT } from './shell.mjs'@@ -209,6 +209,11 @@ async function start() {// update can still come when this version's Host does not start.updates = startUpdates()updates?.subscribe((state) => { mainWindow?.webContents.send(IPC.appUpdateChanged, state) })+ // Before the Host, which reads the Sessions.+ await restoreSessions(where.dataDir).then(({ copied, newer }) => {+ if (copied.length > 0) console.log(`DS Bot: copied ${copied.join(', ')}, which the import left out`)+ if (newer.length > 0) console.warn(`DS Bot: this DSH cannot read ${newer.join(', ')}, which the import left out`)+ }, (error) => { console.error('DS Bot: the Sessions the import left out were not copied:', error) })host = newHost()host.onMessage(message => {if (message?.type !== 'ds-bot:browser-endpoint' || typeof message.id !== 'string' || !isBotId(message.botId)) returndiff --git a/app/test/import.test.mjs b/app/test/import.test.mjsindex 7f9527cb..1ae43df6 100644--- a/app/test/import.test.mjs+++ b/app/test/import.test.mjs@@ -6,7 +6,7 @@ import { homedir, tmpdir } from 'node:os'import { dirname, join, relative } from 'node:path'import { after, before, describe, test } from 'node:test'import { parse } from 'yaml'-import { copySessions, findTeam, importTeam, newerSessions, sessionIdsOf, teamInUse, userDshHome } from '../src/import.mjs'+import { copySessions, findTeam, importTeam, newerSessions, restoreSessions, sessionIdsOf, teamInUse, userDshHome } from '../src/import.mjs'const write = (path, text) => {mkdirSync(dirname(path), { recursive: true })@@ -189,6 +189,50 @@ describe('a copy that stopped', () => {})})+describe('Sessions an earlier import left out', () => {+ let root, from, to, ownSession+ before(async () => {+ root = mkdtempSync(join(tmpdir(), 'ds-bot-app-restore-'))+ from = oldHome(root)+ const home = newHome(root)+ to = home.to+ await importTeam({ from, to, workspace: home.workspace })+ // An import before 0.2.3 did not copy the group's own Session.+ ownSession = join(to, 'sessions', '--team--', 'session-r1')+ rmSync(ownSession, { recursive: true, force: true })+ })+ after(() => { rmSync(root, { recursive: true, force: true }) })++ test('copies the group\'s own Session from the old home, and changes nothing there', async () => {+ const oldTree = treeHash(from)+ const present = join(to, 'sessions', '--team--', 'session-a', 'session.v4.jsonl.zstd')+ write(present, 'written here since')+ write(join(to, '.restore-1', 'junk.txt'), 'left over')+ assert.deepEqual(await restoreSessions(to), { copied: ['session-r1'], newer: [] })+ assert.equal(readFileSync(join(ownSession, 'session.v4.jsonl.zstd'), 'utf8'), 'session-r1')+ assert.equal(readFileSync(present, 'utf8'), 'written here since')+ assert.deepEqual(readdirSync(to).filter(name => name.startsWith('.restore-')), [])+ assert.equal(treeHash(from), oldTree)+ })++ test('copies nothing once every Session is there', async () => {+ assert.deepEqual(await restoreSessions(to), { copied: [], newer: [] })+ })++ test('leaves a Session this DSH cannot read in the old home', async () => {+ rmSync(ownSession, { recursive: true, force: true })+ write(join(from, 'sessions', '--team--', 'session-r1', 'session.v99.jsonl.zstd'), 'newer')+ assert.deepEqual(await restoreSessions(to), { copied: [], newer: ['session-r1'] })+ assert.equal(existsSync(ownSession), false)+ rmSync(join(from, 'sessions', '--team--', 'session-r1', 'session.v99.jsonl.zstd'))+ })++ test('does nothing for data that was not imported', async () => {+ const { to: fresh } = newHome(join(root, 'fresh'))+ assert.deepEqual(await restoreSessions(fresh), { copied: [], newer: [] })+ })+})+describe('links in the old team workspace', () => {let rootbefore(() => { root = mkdtempSync(join(tmpdir(), 'ds-bot-app-links-')) })