A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Copy the old team workspace with its links at the first-run import
Fei2411ac102a35c8c3
diff --git a/app/src/copy-tree.mjs b/app/src/copy-tree.mjsnew file mode 100644index 00000000..6df32487--- /dev/null+++ b/app/src/copy-tree.mjs@@ -0,0 +1,81 @@+// Copying a folder of the user's whole, with its links, for the first-run import and the data move.+import { existsSync, realpathSync } from 'node:fs'+import { cp, lstat, readlink, realpath, stat, symlink } from 'node:fs/promises'+import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'++export const within = (inner, outer) => {+ const path = relative(resolve(outer), resolve(inner))+ return path === '' || (!path.startsWith('..') && !path.startsWith(sep) && !/^[a-z]:/iu.test(path))+}++// The path as the file system has it: through links and junctions, from the deepest part that+// exists. A link can make a path that looks apart from a folder lead into it.+export function real(path) {+ const rest = []+ let at = resolve(path)+ while (!existsSync(at)) {+ const up = dirname(at)+ if (up === at) return resolve(path)+ rest.unshift(basename(at))+ at = up+ }+ return join(realpathSync.native(at), ...rest)+}++// A link that leads inside the copied folder leads to the same place inside the copy: a relative+// one keeps its text, an absolute one is moved over. One that leads outside keeps leading+// there, as an absolute link. Windows gives a junction's target with \\?\, and makes only+// junctions, which hold absolute paths, without special rights.+async function relink(source, path, roots, to) {+ const text = (await readlink(source)).replace(/^\\\\\?\\UNC\\/u, '\\\\').replace(/^\\\\\?\\/u, '')+ const written = resolve(dirname(source), text)+ // Through another link the target can lead inside the copied folder too.+ const physical = await realpath(written).catch(() => written)+ const direct = roots.some(folder => within(written, folder))+ const target = direct ? written : physical+ const root = roots.find(folder => within(target, folder))+ const folder = (await stat(source).catch(() => undefined))?.isDirectory() === true+ const windows = process.platform === 'win32'+ const inside = root === undefined ? written : join(to, relative(root, target))+ await symlink(direct && !isAbsolute(text) && !(windows && folder) ? text : inside, path, folder ? (windows ? 'junction' : 'dir') : 'file')+}++/**+ * Copy the folder `from` into `into`, making its links again (fs.cp makes symbolic links, which+ * Windows refuses without special rights). An entry that cannot be read, such as a junction to+ * nothing, or a link that cannot be made again, is skipped and listed.+ * @param {string} from+ * @param {string} into+ * @param {{ force?: boolean, leave?: (path: string) => boolean, as?: string, signal?: AbortSignal }} [options]+ * force: false keeps what is there already; leave: relative paths not to copy; as: where the+ * copy will end up, when `into` is a staging folder that takes that name later+ * @returns {Promise<string[]>} the relative paths that were skipped+ */+export async function copyTree(from, into, { force = true, leave = () => false, as = into, signal } = {}) {+ // The folder can itself be a link; its contents are what is copied.+ const origin = real(from)+ const links = []+ const skipped = []+ await cp(origin, into, {+ recursive: true,+ force,+ filter: async (source) => {+ signal?.throwIfAborted()+ const path = relative(origin, source)+ if (path !== '' && leave(path)) return false+ if (path === '') return true+ const info = await lstat(source).catch(() => undefined)+ if (info === undefined) skipped.push(path)+ else if (info.isSymbolicLink()) links.push(path)+ return info?.isSymbolicLink() === false+ },+ })+ const roots = [...new Set([resolve(from), origin])]+ for (const path of links) {+ signal?.throwIfAborted()+ const at = join(into, path)+ if (!force && await lstat(at).then(() => true, () => false)) continue+ await relink(join(origin, path), at, roots, resolve(as)).catch(() => { skipped.push(path) })+ }+ return skipped+}diff --git a/app/src/import.mjs b/app/src/import.mjsindex 7524f378..b5e4451c 100644--- a/app/src/import.mjs+++ b/app/src/import.mjs@@ -2,10 +2,13 @@import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'import { chmod, cp, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'import { homedir, hostname } from 'node:os'-import { join, relative, resolve, sep } from 'node:path'+import { join, relative, resolve } from 'node:path'import { parseSessionFormatLogFilename } from '@deepseek-ai/dsh-session-format'import { sessionFormatCatalog } from '@deepseek-ai/dsh-session-format-catalog'import { parse, stringify } from 'yaml'+import { copyTree, within } from './copy-tree.mjs'++export { within }// The rows of a profile patch that hold model settings: DeepSeek's API and account, other// providers, the default model and the agent presets. Rows of other plugins may name plugins@@ -22,11 +25,6 @@ export function userDshHome(env = process.env) {return resolve(value === '~' ? homedir() : /^~[\\/]/u.test(value) ? join(homedir(), value.slice(2)) : value)}-export const within = (inner, outer) => {- const path = relative(resolve(outer), resolve(inner))- return path === '' || (!path.startsWith('..') && !path.startsWith(sep) && !/^[a-z]:/iu.test(path))-}-const readJson = path => JSON.parse(readFileSync(path, 'utf8'))/** Every Session the team names: each Bot's and group's parts and the group conversations. */@@ -184,7 +182,11 @@ export async function importTeam({ from, to, workspace, profile, dsh, signal })signal?.throwIfAborted()await mkdir(workspace, { recursive: true })- if (existsSync(join(oldBot, 'workspace'))) await cp(join(oldBot, 'workspace'), workspace, { recursive: true, force: false })+ if (existsSync(join(oldBot, 'workspace'))) {+ // Bots that ran npm or pnpm there leave links, some of them to nothing.+ const skipped = await copyTree(join(oldBot, 'workspace'), workspace, { force: false, signal })+ if (skipped.length > 0) console.warn('DeepSeek Bot: the import skipped what it could not read in the old team workspace:', skipped)+ }signal?.throwIfAborted()const keys = []for (const name of ['.credentials.yaml', '.env']) if (await copyKey(from, to, name)) keys.push(name)diff --git a/app/src/move-data.mjs b/app/src/move-data.mjsindex f0ebd755..40fa1b43 100644--- a/app/src/move-data.mjs+++ b/app/src/move-data.mjs@@ -1,10 +1,10 @@// Moving the app's data to another folder. The copy is put together in a folder beside the new// one and takes its name only once it is whole, so a copy that stopped or failed leaves nothing// that looks like data. The old folder stays as it was.-import { existsSync, readdirSync, realpathSync, statSync } from 'node:fs'-import { cp, lstat, mkdir, readlink, realpath, rename, rm, rmdir, stat, symlink, writeFile } from 'node:fs/promises'-import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path'-import { within } from './import.mjs'+import { existsSync, readdirSync, statSync } from 'node:fs'+import { mkdir, rename, rm, rmdir, writeFile } from 'node:fs/promises'+import { basename, dirname, join, resolve } from 'node:path'+import { copyTree, real, within } from './copy-tree.mjs'// The engine's lock on the team belongs to the old folder; the first-run copy and its check// leave their temporary folders only when they were cut short.@@ -12,20 +12,6 @@ const LEFT_BEHIND = [/^bot[\\/]state\.lock$/u, /^\.import(-check)?-\d+$/u]// Marks a staging folder as this app's, so that only those are removed.const MARK = '.ds-bot-moving'-// The path as the file system has it: through links and junctions, from the deepest part that-// exists. A link can make a path that looks apart from a folder lead into it.-function real(path) {- const rest = []- let at = resolve(path)- while (!existsSync(at)) {- const up = dirname(at)- if (up === at) return resolve(path)- rest.unshift(basename(at))- at = up- }- return join(realpathSync.native(at), ...rest)-}-/** The folder that an update or uninstall replaces: on macOS the whole .app bundle. */export function installFolder(exe, platform = process.platform) {if (platform === 'darwin') {@@ -54,24 +40,6 @@ export function moveProblem(places) {const escaped = text => text.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&')-// A link that leads inside the old folder leads to the same place inside the copy: a relative-// one keeps its text, an absolute one is moved over. One that leads outside keeps leading-// there, as an absolute link. Windows gives a junction's target with \\?\, and makes only-// junctions, which hold absolute paths, without special rights.-async function relink(source, path, roots, to) {- const text = (await readlink(source)).replace(/^\\\\\?\\UNC\\/u, '\\\\').replace(/^\\\\\?\\/u, '')- const written = resolve(dirname(source), text)- // Through another link the target can lead inside the old folder too.- const physical = await realpath(written).catch(() => written)- const direct = roots.some(folder => within(written, folder))- const target = direct ? written : physical- const root = roots.find(folder => within(target, folder))- const folder = (await stat(source).catch(() => undefined))?.isDirectory() === true- const windows = process.platform === 'win32'- const inside = root === undefined ? written : join(to, relative(root, target))- await symlink(direct && !isAbsolute(text) && !(windows && folder) ? text : inside, path, folder ? (windows ? 'junction' : 'dir') : 'file')-}-/*** Copy the data in `from` to `to`, an empty or new folder. The signal stops it between files.* @param {string} from @param {string} to @param {AbortSignal} [signal]@@ -88,25 +56,8 @@ export async function copyData(from, to, signal) {await mkdir(stage)try {await writeFile(join(stage, MARK), '')- const links = []- // The data folder can itself be a link; its contents are what moves.- const origin = real(from)- await cp(origin, stage, {- recursive: true,- filter: async (source) => {- signal?.throwIfAborted()- const path = relative(origin, source)- if (LEFT_BEHIND.some(pattern => pattern.test(path))) return false- if (path === '' || !(await lstat(source)).isSymbolicLink()) return true- links.push(path)- return false- },- })- const roots = [...new Set([resolve(from), origin])]- for (const path of links) {- signal?.throwIfAborted()- await relink(join(origin, path), join(stage, path), roots, resolve(to))- }+ const skipped = await copyTree(from, stage, { as: to, leave: path => LEFT_BEHIND.some(pattern => pattern.test(path)), signal })+ if (skipped.length > 0) console.warn('DeepSeek Bot: the data move skipped what it could not read:', skipped)signal?.throwIfAborted()if (existsSync(to)) await rmdir(to)await rename(stage, to)diff --git a/app/test/import.test.mjs b/app/test/import.test.mjsindex 090b0f4e..89682ad9 100644--- a/app/test/import.test.mjs+++ b/app/test/import.test.mjs@@ -1,7 +1,7 @@import assert from 'node:assert/strict'import { spawnSync } from 'node:child_process'import { createHash } from 'node:crypto'-import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from 'node:fs'+import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, utimesSync, writeFileSync } from 'node:fs'import { homedir, tmpdir } from 'node:os'import { dirname, join, relative } from 'node:path'import { after, before, describe, test } from 'node:test'@@ -185,6 +185,29 @@ describe('a copy that stopped', () => {})})+describe('links in the old team workspace', () => {+ let root+ before(() => { root = mkdtempSync(join(tmpdir(), 'ds-bot-app-links-')) })+ after(() => { rmSync(root, { recursive: true, force: true }) })++ test('makes a package link again inside the chosen folder and gets past a link to nothing', async () => {+ const from = oldHome(root)+ const old = join(from, 'bot', 'workspace')+ write(join(old, '.pnpm', 'pkg', 'index.js'), 'package')+ mkdirSync(join(old, 'node_modules'), { recursive: true })+ const type = process.platform === 'win32' ? 'junction' : 'dir'+ symlinkSync(join(old, '.pnpm', 'pkg'), join(old, 'node_modules', 'pkg'), type)+ mkdirSync(join(old, '.pnpm-store'), { recursive: true })+ symlinkSync(join(root, 'gone'), join(old, '.pnpm-store', '.ignored_pkg'), type)+ const { to, workspace } = newHome(root)+ const summary = await importTeam({ from, to, workspace })+ assert.equal(summary.sessions, 5)+ assert.equal(readFileSync(join(workspace, 'node_modules', 'pkg', 'index.js'), 'utf8'), 'package')+ assert.equal(realpathSync.native(join(workspace, 'node_modules', 'pkg')), realpathSync.native(join(workspace, '.pnpm', 'pkg')))+ assert.equal(readFileSync(join(workspace, 'notes.txt'), 'utf8'), 'old notes')+ })+})+describe('finding the team', () => {let root, frombefore(() => {