Skip to content

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

Update an installed DS Bot under hot reload

Fei24111ce34078ea65
diff --git a/README.md b/README.md
index 93af6d42..be7f520f 100644
--- a/README.md
+++ b/README.md
@@ -39,6 +39,8 @@ First install DSH and set up at least one model in it. DS Bot uses the models fr
If npm is not reachable, in step 2 you can also enter the full path of `ds-bot-0.2.1.tgz` downloaded from [Releases](https://github.com/Fei2411/DeepSeek-Bot/releases), or `github:Fei2411/DeepSeek-Bot#v0.2.1&path:/ds-bot` (needs Git).
+DS Bot 0.2.1 and earlier cannot update themselves in DSH Desktop 0.2.1-rc.2 or later: on the Plugins page, remove `ds-bot` and add `ds-bot` again. The Bots, chats and memory stay.
+
**DSH Web (Windows, macOS, Linux)**
Needs Node.js `22.19` or later, and pnpm.
diff --git a/README.zh.md b/README.zh.md
index 7be981df..3bdddb97 100644
--- a/README.zh.md
+++ b/README.zh.md
@@ -39,6 +39,8 @@ DeepSeek Bot(简称 DS Bot)是一个基于 [DeepSeek Harness](https://github
连不上 npm 时,第 2 步也可以填 [Releases](https://github.com/Fei2411/DeepSeek-Bot/releases) 里下载的 `ds-bot-0.2.1.tgz` 的完整路径,或者 `github:Fei2411/DeepSeek-Bot#v0.2.1&path:/ds-bot`(需要装 Git)。
+DS Bot 0.2.1 及更早的版本在 DSH Desktop 0.2.1-rc.2 及以后的版本里不能自己更新:在插件页移除 `ds-bot`,再添加 `ds-bot`。Bot、聊天和记忆都会保留。
+
**dsh Web(Windows、macOS、Linux)**
需要 Node.js `22.19` 以上和 pnpm。
diff --git a/ds-bot/client.js b/ds-bot/client.js
index fa98b9f8..e5526636 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -492,6 +492,7 @@ var ZH = {
"DS Bot {version} is available": "DS Bot {version} 可以更新",
"Updating DS Bot…": "正在更新 DS Bot…",
"DS Bot {version} is installed. Restart DSH to use it.": "DS Bot {version} 已安装,重启 DSH 后生效",
+ "DS Bot {version} is installed.": "DS Bot {version} 已安装。",
"DS Bot update": "DS Bot 更新",
"DS Bot {version}": "DS Bot {version}",
"Update installed": "更新已安装",
@@ -8648,14 +8649,14 @@ function UpdateButton({ useUpdate, actions }) {
const dsh = update?.dsh;
const dshPhase = dsh?.phase ?? "idle";
const dshRow = showsDshRow(dsh, update?.app);
- if (!update?.available && !dshRow && phase === "idle" && dshPhase === "idle") return null;
- const cloudPhase = phase === "installing" || dshPhase === "installing" ? "installing" : phase !== "idle" ? phase : dshPhase;
const tick = useViewport();
(0, import_react14.useLayoutEffect)(() => {
if (!open) return;
const rect = button.current?.getBoundingClientRect();
if (rect) setBox({ left: Math.max(8, Math.min(rect.left, window.innerWidth - 288)), top: rect.bottom + 6 });
}, [open, tick]);
+ if (!update?.available && !dshRow && phase === "idle" && dshPhase === "idle") return null;
+ const cloudPhase = phase === "installing" || dshPhase === "installing" ? "installing" : phase !== "idle" ? phase : dshPhase;
const toggle = () => setOpen((value) => !value);
const start = () => {
setError("");
@@ -8665,7 +8666,7 @@ function UpdateButton({ useUpdate, actions }) {
setDshError("");
void actions.installDshUpdate().catch((failure) => setDshError(hostText(failure?.message ?? String(failure))));
};
- const label = phase === "installing" ? t("Updating DS Bot…") : dshPhase === "installing" ? t("Updating DSH…") : phase === "installed" ? t("DS Bot {version} is installed. Restart DSH to use it.", { version: update.installed ?? update.latest }) : dshPhase === "installed" ? t("Restart DSH to start using DSH {version}.", { version: dsh.installed ?? dsh.verified }) : update?.available ? t("DS Bot {version} is available", { version: update.latest }) : t("DSH {version} is available", { version: dsh?.verified });
+ const label = phase === "installing" ? t("Updating DS Bot…") : dshPhase === "installing" ? t("Updating DSH…") : phase === "installed" ? t("DS Bot {version} is installed. Restart DSH to use it.", { version: update.installed ?? update.latest }) : phase === "updated" ? t("DS Bot {version} is installed.", { version: update.installed ?? update.latest }) : dshPhase === "installed" ? t("Restart DSH to start using DSH {version}.", { version: dsh.installed ?? dsh.verified }) : update?.available ? t("DS Bot {version} is available", { version: update.latest }) : t("DSH {version} is available", { version: dsh?.verified });
const message = error || (phase === "failed" ? update.error : "");
const dshMessage = dshError || (dshPhase === "failed" ? dsh?.error : "");
const dshText = dshPhase === "installed" ? t("Restart DSH to start using DSH {version}.", { version: dsh.installed ?? dsh.verified }) : dsh?.available === false && dsh?.blockedBy ? t("Update DS Bot {version} first.", { version: dsh.blockedBy }) : t("You have {version}.", { version: dsh?.current });
@@ -8682,21 +8683,21 @@ function UpdateButton({ useUpdate, actions }) {
"aria-haspopup": "dialog",
"aria-expanded": open,
onClick: toggle
- }, (0, import_react14.createElement)(CloudDownloadIcon, { done: phase === "installed" }), phase === "failed" || dshPhase === "failed" ? (0, import_react14.createElement)("span", { className: "bt-update-dot", "aria-hidden": true }) : null),
+ }, (0, import_react14.createElement)(CloudDownloadIcon, { done: phase === "installed" || phase === "updated" }), phase === "failed" || dshPhase === "failed" ? (0, import_react14.createElement)("span", { className: "bt-update-dot", "aria-hidden": true }) : null),
open && box ? (0, import_react_dom3.createPortal)((0, import_react14.createElement)(
"div",
{ className: "bt-update-pop", role: "dialog", "aria-label": update?.available || phase !== "idle" ? t("DS Bot update") : t("DSH update"), style: box },
update?.available || phase !== "idle" ? (0, import_react14.createElement)(
import_react14.Fragment,
null,
- (0, import_react14.createElement)("div", { className: "bt-update-title" }, phase === "installed" ? t("Update installed") : t("DS Bot {version}", { version: update.latest })),
- (0, import_react14.createElement)("div", { className: "bt-update-text" }, phase === "installed" ? t("Restart DSH to start using DS Bot {version}.", { version: update.installed ?? update.latest }) : t("You have {version}.", { version: update.current })),
+ (0, import_react14.createElement)("div", { className: "bt-update-title" }, phase === "installed" || phase === "updated" ? t("Update installed") : t("DS Bot {version}", { version: update.latest })),
+ (0, import_react14.createElement)("div", { className: "bt-update-text" }, phase === "installed" ? t("Restart DSH to start using DS Bot {version}.", { version: update.installed ?? update.latest }) : phase === "updated" ? t("DS Bot {version} is installed.", { version: update.installed ?? update.latest }) : t("You have {version}.", { version: update.current })),
message ? (0, import_react14.createElement)("div", { className: "bt-update-error", role: "alert" }, t("The update did not finish: {reason}", { reason: message })) : null,
(0, import_react14.createElement)(
"div",
{ className: "bt-update-actions" },
update.notesUrl ? (0, import_react14.createElement)("a", { className: "bt-update-notes", href: update.notesUrl, target: "_blank", rel: "noopener noreferrer" }, t("What's new")) : null,
- phase === "installed" ? null : (0, import_react14.createElement)("button", {
+ phase === "installed" || phase === "updated" ? null : (0, import_react14.createElement)("button", {
type: "button",
className: "bt-update-go",
disabled: phase === "installing",
@@ -14401,6 +14402,7 @@ var starCount = (count) => new Intl.NumberFormat(dateLocale(), { notation: "comp
function botHint(update) {
if (update.phase === "installing") return t("Updating DS Bot…");
if (update.phase === "installed") return t("Restart DSH to start using DS Bot {version}.", { version: update.installed ?? update.latest });
+ if (update.phase === "updated") return t("DS Bot {version} is installed.", { version: update.installed ?? update.latest });
if (update.phase === "failed") return t("The update did not finish: {reason}", { reason: hostText(update.error ?? "") });
if (update.available) return t("DS Bot {version} is available", { version: update.latest });
if (update.source === "app") return t("Updates come with the DeepSeek Bot app.");
@@ -14451,12 +14453,12 @@ function AboutPage({ useUpdate, actions, run: run2, Section, Row: Row3 }) {
const dsh = update.dsh;
const dshPhase = dsh?.phase ?? "idle";
(0, import_react34.useEffect)(() => {
- if (phase === "installed" || dshPhase === "installed") setCheer(true);
+ if (phase === "installed" || phase === "updated" || dshPhase === "installed") setCheer(true);
}, [phase, dshPhase]);
const markState = checking ? "searching" : phase === "installing" || dshPhase === "installing" ? "working" : cheer ? "done" : "idle";
const dshNew = showsDshRow(dsh, update.app);
const dshShown = dshNew && dsh.available;
- const botControl = phase === "installed" || update.source === "app" ? null : update.available || phase === "installing" ? (0, import_react34.createElement)(
+ const botControl = phase === "installed" || phase === "updated" || update.source === "app" ? null : update.available || phase === "installing" ? (0, import_react34.createElement)(
"button",
{ type: "button", className: "bt-fb-primary", disabled: phase === "installing", onClick: () => run2(actions.installUpdate()) },
phase === "installing" ? t("Updating…") : phase === "failed" ? t("Try again") : t("Update")
diff --git a/ds-bot/src/client/about.js b/ds-bot/src/client/about.js
index f1a58fac..232a25d7 100644
--- a/ds-bot/src/client/about.js
+++ b/ds-bot/src/client/about.js
@@ -18,6 +18,7 @@ const starCount = count => new Intl.NumberFormat(dateLocale(), { notation: 'comp
function botHint(update) {
if (update.phase === 'installing') return t('Updating DS Bot…')
if (update.phase === 'installed') return t('Restart DSH to start using DS Bot {version}.', { version: update.installed ?? update.latest })
+ if (update.phase === 'updated') return t('DS Bot {version} is installed.', { version: update.installed ?? update.latest })
if (update.phase === 'failed') return t('The update did not finish: {reason}', { reason: hostText(update.error ?? '') })
if (update.available) return t('DS Bot {version} is available', { version: update.latest })
if (update.source === 'app') return t('Updates come with the DeepSeek Bot app.')
@@ -63,12 +64,12 @@ export function AboutPage({ useUpdate, actions, run, Section, Row }) {
const phase = update.phase ?? 'idle'
const dsh = update.dsh
const dshPhase = dsh?.phase ?? 'idle'
- useEffect(() => { if (phase === 'installed' || dshPhase === 'installed') setCheer(true) }, [phase, dshPhase])
+ useEffect(() => { if (phase === 'installed' || phase === 'updated' || dshPhase === 'installed') setCheer(true) }, [phase, dshPhase])
const markState = checking ? 'searching' : phase === 'installing' || dshPhase === 'installing' ? 'working' : cheer ? 'done' : 'idle'
// Same rule as the sidebar card.
const dshNew = showsDshRow(dsh, update.app)
const dshShown = dshNew && dsh.available
- const botControl = phase === 'installed' || update.source === 'app' ? null
+ const botControl = phase === 'installed' || phase === 'updated' || update.source === 'app' ? null
: update.available || phase === 'installing'
? h('button', { type: 'button', className: 'bt-fb-primary', disabled: phase === 'installing', onClick: () => run(actions.installUpdate()) },
phase === 'installing' ? t('Updating…') : phase === 'failed' ? t('Try again') : t('Update'))
diff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.js
index 1eba67a5..e0792252 100644
--- a/ds-bot/src/client/i18n.js
+++ b/ds-bot/src/client/i18n.js
@@ -433,6 +433,7 @@ const ZH = {
'DS Bot {version} is available': 'DS Bot {version} 可以更新',
'Updating DS Bot…': '正在更新 DS Bot…',
'DS Bot {version} is installed. Restart DSH to use it.': 'DS Bot {version} 已安装,重启 DSH 后生效',
+ 'DS Bot {version} is installed.': 'DS Bot {version} 已安装。',
'DS Bot update': 'DS Bot 更新',
'DS Bot {version}': 'DS Bot {version}',
'Update installed': '更新已安装',
diff --git a/ds-bot/src/client/sidebar.js b/ds-bot/src/client/sidebar.js
index 2d7017cd..b6c7c7a1 100644
--- a/ds-bot/src/client/sidebar.js
+++ b/ds-bot/src/client/sidebar.js
@@ -347,14 +347,16 @@ function UpdateButton({ useUpdate, actions }) {
const dsh = update?.dsh
const dshPhase = dsh?.phase ?? 'idle'
const dshRow = showsDshRow(dsh, update?.app)
- if (!update?.available && !dshRow && phase === 'idle' && dshPhase === 'idle') return null
- const cloudPhase = phase === 'installing' || dshPhase === 'installing' ? 'installing' : phase !== 'idle' ? phase : dshPhase
+ // Hooks first: an update that turns visible after the sidebar mounted would
+ // otherwise add hooks on a later render and crash the slot.
const tick = useViewport()
useLayoutEffect(() => {
if (!open) return
const rect = button.current?.getBoundingClientRect()
if (rect) setBox({ left: Math.max(8, Math.min(rect.left, window.innerWidth - 288)), top: rect.bottom + 6 })
}, [open, tick])
+ if (!update?.available && !dshRow && phase === 'idle' && dshPhase === 'idle') return null
+ const cloudPhase = phase === 'installing' || dshPhase === 'installing' ? 'installing' : phase !== 'idle' ? phase : dshPhase
const toggle = () => setOpen(value => !value)
const start = () => {
setError('')
@@ -367,6 +369,7 @@ function UpdateButton({ useUpdate, actions }) {
const label = phase === 'installing' ? t('Updating DS Bot…')
: dshPhase === 'installing' ? t('Updating DSH…')
: phase === 'installed' ? t('DS Bot {version} is installed. Restart DSH to use it.', { version: update.installed ?? update.latest })
+ : phase === 'updated' ? t('DS Bot {version} is installed.', { version: update.installed ?? update.latest })
: dshPhase === 'installed' ? t('Restart DSH to start using DSH {version}.', { version: dsh.installed ?? dsh.verified })
: update?.available ? t('DS Bot {version} is available', { version: update.latest })
: t('DSH {version} is available', { version: dsh?.verified })
@@ -381,17 +384,18 @@ function UpdateButton({ useUpdate, actions }) {
h('button', {
ref: button, type: 'button', className: 'bt-round bt-update', 'data-phase': cloudPhase, 'aria-label': label, title: label,
'aria-haspopup': 'dialog', 'aria-expanded': open, onClick: toggle,
- }, h(CloudDownloadIcon, { done: phase === 'installed' }), phase === 'failed' || dshPhase === 'failed' ? h('span', { className: 'bt-update-dot', 'aria-hidden': true }) : null),
+ }, h(CloudDownloadIcon, { done: phase === 'installed' || phase === 'updated' }), phase === 'failed' || dshPhase === 'failed' ? h('span', { className: 'bt-update-dot', 'aria-hidden': true }) : null),
open && box ? createPortal(h('div', { className: 'bt-update-pop', role: 'dialog', 'aria-label': update?.available || phase !== 'idle' ? t('DS Bot update') : t('DSH update'), style: box },
update?.available || phase !== 'idle' ? h(Fragment, null,
- h('div', { className: 'bt-update-title' }, phase === 'installed' ? t('Update installed') : t('DS Bot {version}', { version: update.latest })),
+ h('div', { className: 'bt-update-title' }, phase === 'installed' || phase === 'updated' ? t('Update installed') : t('DS Bot {version}', { version: update.latest })),
h('div', { className: 'bt-update-text' }, phase === 'installed'
? t('Restart DSH to start using DS Bot {version}.', { version: update.installed ?? update.latest })
- : t('You have {version}.', { version: update.current })),
+ : phase === 'updated' ? t('DS Bot {version} is installed.', { version: update.installed ?? update.latest })
+ : t('You have {version}.', { version: update.current })),
message ? h('div', { className: 'bt-update-error', role: 'alert' }, t('The update did not finish: {reason}', { reason: message })) : null,
h('div', { className: 'bt-update-actions' },
update.notesUrl ? h('a', { className: 'bt-update-notes', href: update.notesUrl, target: '_blank', rel: 'noopener noreferrer' }, t("What's new")) : null,
- phase === 'installed' ? null : h('button', {
+ phase === 'installed' || phase === 'updated' ? null : h('button', {
type: 'button', className: 'bt-update-go', disabled: phase === 'installing', onClick: start,
}, phase === 'installing' ? t('Updating…') : phase === 'failed' ? t('Try again') : t('Update')))) : null,
dshRow ? h('div', { className: 'bt-update-dsh' },
diff --git a/ds-bot/src/host/update.js b/ds-bot/src/host/update.js
index 7831eff9..108956c3 100644
--- a/ds-bot/src/host/update.js
+++ b/ds-bot/src/host/update.js
@@ -2,7 +2,7 @@
// installed from (the npm registry, or a GitHub tag for a versioned git install)
// and, when the user asks, reinstall it through DSH's plugin manager. A new version
// runs from the next DSH start. A linked or local checkout is never offered one.
-import { readFileSync } from 'node:fs'
+import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
const PKG = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'))
@@ -123,11 +123,35 @@ export function install(rt) {
let target = null
let checking = null
+ // A remove-and-install update under hot reload drops this host with its bundle;
+ // a note in the data folder carries the result to the start that follows it.
+ const markerPath = typeof rt.home === 'string' ? join(rt.home, 'update.json') : null
+ if (markerPath !== null && existsSync(markerPath)) {
+ let marker = null
+ try {
+ marker = JSON.parse(readFileSync(markerPath, 'utf8'))
+ } catch (error) {
+ rt.warn('ds-bot: could not read the update note: %s', error?.message ?? error)
+ }
+ if (marker === null) {
+ try { unlinkSync(markerPath) } catch { /* gone is what we want */ }
+ } else if (marker.to === PKG.version) {
+ Object.assign(status, { phase: 'updated', installed: PKG.version })
+ try { unlinkSync(markerPath) } catch { /* gone is what we want */ }
+ } else if (typeof marker.error === 'string' || marker.from !== PKG.version) {
+ rt.warn('ds-bot: the update to %s did not finish: %s', marker.to ?? 'a newer version', typeof marker.error === 'string' ? marker.error.slice(0, 600) : 'the install did not complete')
+ try { unlinkSync(markerPath) } catch { /* gone is what we want */ }
+ }
+ // A clean note on the version that wrote it stays: a reload can still be
+ // running the old modules (the hoisted profile layout keeps one path), and
+ // the next start that runs the new code reports the update then.
+ }
+
const view = () => ({
name: status.name, current: status.current, latest: status.latest, source: status.source, notesUrl: status.notesUrl,
repoUrl: repoUrl(), enabled,
checkedAt: status.checkedAt, phase: status.phase, installed: status.installed, error: status.error,
- available: status.phase !== 'installed' && target !== null && status.latest !== null && newer(status.latest, status.current),
+ available: status.phase !== 'installed' && status.phase !== 'updated' && target !== null && status.latest !== null && newer(status.latest, status.current),
})
// Asked only while the About page is open, so a closed page never calls GitHub.
@@ -211,7 +235,37 @@ export function install(rt) {
if (typeof service?.installBundle !== 'function') throw new Error('The DSH plugin manager is not available in this profile')
const chosen = target
Object.assign(status, { phase: 'installing', error: null })
- void Promise.resolve(service.installBundle(chosen.spec, chosen.registry ? { registry: chosen.registry } : {}))
+ const options = chosen.registry ? { registry: chosen.registry } : {}
+ // Removing this bundle disposes our context before the promise answers, so the
+ // continuation uses plain fs for the note and a guarded logger.
+ const note = (template, ...args) => { try { rt.warn(template, ...args) } catch { /* our context is disposed */ } }
+ const mark = (error) => {
+ if (markerPath === null) return
+ try {
+ writeFileSync(markerPath, JSON.stringify({
+ from: PKG.version, to: chosen.version, spec: chosen.spec, startedAt: new Date().toISOString(),
+ ...(error === undefined ? {} : { error: String(error).slice(0, 600) }),
+ }))
+ } catch { /* a note that cannot be written just is not there */ }
+ }
+ void Promise.resolve(service.installBundle(chosen.spec, options))
+ .then(async (result) => {
+ if (result?.error?.code !== 'already-installed') return result
+ // With HMR the plugin manager refuses to install a package the profile already
+ // depends on: remove the bundle (which unloads this host) and install it again.
+ if (typeof service.removeBundle !== 'function') return { error: { code: 'not-removable' } }
+ mark()
+ const removed = await service.removeBundle(PKG.name)
+ if (removed?.error) return { error: removed.error }
+ try {
+ const again = await service.installBundle(chosen.spec, options)
+ if (again?.error) mark(again.error.diagnostic ?? again.error.code ?? 'install failed')
+ return again
+ } catch (error) {
+ mark(error?.message ?? error)
+ throw error
+ }
+ })
.then((result) => {
if (result?.error || !['applied', 'restart-required'].includes(result?.application)) {
throw new Error(result?.error?.diagnostic ?? result?.error?.code ?? `install ${result?.application ?? 'failed'}`)
@@ -219,7 +273,7 @@ export function install(rt) {
Object.assign(status, { phase: 'installed', installed: cleanVersion(result.version) ?? chosen.version })
})
.catch((error) => {
- rt.warn('ds-bot: update to %s failed: %s', chosen.spec, error?.message ?? error)
+ note('ds-bot: update to %s failed: %s', chosen.spec, error?.message ?? error)
Object.assign(status, { phase: 'failed', error: String(error?.message ?? error).slice(0, 600) })
})
return view()
diff --git a/ds-bot/test/update.test.js b/ds-bot/test/update.test.js
index b647b621..ca711729 100644
--- a/ds-bot/test/update.test.js
+++ b/ds-bot/test/update.test.js
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict'
-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, test } from 'node:test'
@@ -11,9 +11,10 @@ const realFetch = globalThis.fetch
afterEach(() => { globalThis.fetch = realFetch })
// The update module alone, on a stub plugin manager and a scripted fetch.
-function start({ source, registries, answers = {}, result = { application: 'restart-required' }, config = {}, profileDir }) {
+function start({ source, registries, answers = {}, result = { application: 'restart-required' }, remove = { application: 'applied' }, config = {}, profileDir, home }) {
const fetched = []
const installs = []
+ const removes = []
const warnings = []
globalThis.fetch = async (url) => {
fetched.push(url)
@@ -25,15 +26,17 @@ function start({ source, registries, answers = {}, result = { application: 'rest
const pluginManager = {
listBundles: async () => [{ name: 'other', source: 'other@^1.0.0' }, ...(source === undefined ? [] : [{ name: PKG.name, source }])],
registries: async () => registries ?? { registry: null, fallbackRegistries: ['https://registry.npmmirror.com/'], resolved: 'https://registry.npmjs.org/' },
- installBundle: async (spec, options) => { installs.push([spec, options]); return typeof result === 'function' ? result() : result },
+ installBundle: async (spec, options) => { installs.push([spec, options]); return typeof result === 'function' ? result(installs.length) : result },
+ removeBundle: async (name) => { removes.push(name); return typeof remove === 'function' ? remove() : remove },
}
const rt = {
ctx: { get: name => (name === 'pluginManager' ? pluginManager : name === 'profileContext' && profileDir ? { dir: profileDir } : undefined), effect() {} },
config,
+ home,
warn: (...args) => warnings.push(args),
}
install(rt)
- return { rt, fetched, installs, warnings }
+ return { rt, fetched, installs, removes, warnings }
}
test('versions compare in semver order and lose their v and build metadata', () => {
@@ -234,3 +237,144 @@ test('a failed install reports why and can be tried again', async () => {
assert.equal(installs.length, 2)
assert.deepEqual(installs[0][1], { registry: 'https://registry.example/' })
})
+
+// Under HMR the manager refuses a spec the profile already depends on with
+// 'already-installed'; the update then removes the bundle and installs it again,
+// leaving a note at <home>/update.json for the start that reloads the new copy.
+const NOTE = `${PKG.name}@9.0.0`
+const newHome = () => mkdtempSync(join(tmpdir(), 'ds-bot-update-home-'))
+const noteIn = home => JSON.parse(readFileSync(join(home, 'update.json'), 'utf8'))
+
+test('a plain refusal or success needs no removal and writes no note', async () => {
+ const home = newHome()
+ const { rt, installs, removes } = start({
+ source: `${PKG.name}@^${PKG.version}`,
+ config: { updateRegistry: 'https://registry.example' },
+ answers: { [`https://registry.example/${PKG.name}/latest`]: { version: '9.0.0' } },
+ result: { application: 'restart-required', version: '9.0.0' },
+ home,
+ })
+ await rt.updateCheck()
+ rt.updateStart()
+ await flush()
+ assert.equal((await rt.updateCheck()).phase, 'installed')
+ assert.equal(installs.length, 1)
+ assert.deepEqual(removes, [])
+ assert.equal(existsSync(join(home, 'update.json')), false)
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+})
+
+test('an already-installed refusal removes the bundle, reinstalls and leaves a note', async () => {
+ const home = newHome()
+ const calls = []
+ const { rt, installs, removes } = start({
+ source: `${PKG.name}@^${PKG.version}`,
+ config: { updateRegistry: 'https://registry.example' },
+ answers: { [`https://registry.example/${PKG.name}/latest`]: { version: '9.0.0' } },
+ result: (count) => {
+ calls.push(`install${count}`)
+ return count === 1 ? { error: { code: 'already-installed' } } : { application: 'applied', version: '9.0.0' }
+ },
+ remove: { application: 'applied' },
+ home,
+ })
+ await rt.updateCheck()
+ rt.updateStart()
+ await flush()
+ assert.deepEqual(calls, ['install1', 'install2'])
+ assert.deepEqual(installs, [[NOTE, { registry: 'https://registry.example/' }], [NOTE, { registry: 'https://registry.example/' }]])
+ assert.deepEqual(removes, [PKG.name])
+ // The note was written before the removal — and still carries no error on success.
+ const note = noteIn(home)
+ assert.equal(note.from, PKG.version)
+ assert.equal(note.to, '9.0.0')
+ assert.equal(note.spec, NOTE)
+ assert.equal(typeof note.startedAt, 'string')
+ assert.equal(note.error, undefined)
+ assert.equal((await rt.updateCheck()).phase, 'installed')
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+})
+
+test('a failed removal fails the update without a second install', async () => {
+ const home = newHome()
+ const { rt, installs, removes } = start({
+ source: `${PKG.name}@^${PKG.version}`,
+ config: { updateRegistry: 'https://registry.example' },
+ answers: { [`https://registry.example/${PKG.name}/latest`]: { version: '9.0.0' } },
+ result: count => (count === 1 ? { error: { code: 'already-installed' } } : { application: 'applied', version: '9.0.0' }),
+ remove: { error: { code: 'bundle-in-use' } },
+ home,
+ })
+ await rt.updateCheck()
+ rt.updateStart()
+ await flush()
+ assert.equal(installs.length, 1)
+ assert.deepEqual(removes, [PKG.name])
+ const view = await rt.updateCheck()
+ assert.equal(view.phase, 'failed')
+ assert.equal(view.error, 'bundle-in-use')
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+})
+
+test('a failed reinstall writes its reason into the note', async () => {
+ const home = newHome()
+ const { rt } = start({
+ source: `${PKG.name}@^${PKG.version}`,
+ config: { updateRegistry: 'https://registry.example' },
+ answers: { [`https://registry.example/${PKG.name}/latest`]: { version: '9.0.0' } },
+ result: count => (count === 1 ? { error: { code: 'already-installed' } } : { error: { code: 'operation-error', diagnostic: 'pnpm: tarball 500' } }),
+ home,
+ })
+ await rt.updateCheck()
+ rt.updateStart()
+ await flush()
+ assert.equal((await rt.updateCheck()).phase, 'failed')
+ const note = noteIn(home)
+ assert.equal(note.to, '9.0.0')
+ assert.equal(note.error, 'pnpm: tarball 500')
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+})
+
+test('the note for the version now running shows the update once and is consumed', async () => {
+ const home = newHome()
+ writeFileSync(join(home, 'update.json'), JSON.stringify({ from: '0.2.0', to: PKG.version, spec: `${PKG.name}@${PKG.version}`, startedAt: new Date().toISOString() }))
+ const { rt, warnings } = start({ home })
+ const view = rt.updateView()
+ assert.equal(view.phase, 'updated')
+ assert.equal(view.installed, PKG.version)
+ assert.equal(view.available, false)
+ assert.equal(existsSync(join(home, 'update.json')), false)
+ assert.deepEqual(warnings, [])
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+})
+
+test('a note for another version warns and is consumed', async () => {
+ const home = newHome()
+ writeFileSync(join(home, 'update.json'), JSON.stringify({ from: '0.2.0', to: '9.0.0', spec: NOTE, startedAt: new Date().toISOString(), error: 'pnpm: tarball 500' }))
+ const { rt, warnings } = start({ home })
+ assert.equal(rt.updateView().phase, 'idle')
+ assert.equal(warnings.length, 1)
+ assert.match(String(warnings[0][1] ?? warnings[0][0]), /9\.0\.0|pnpm/)
+ assert.equal(existsSync(join(home, 'update.json')), false)
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+})
+
+test('a clean note on the version that wrote it waits for a start that runs the target', async () => {
+ const home = newHome()
+ writeFileSync(join(home, 'update.json'), JSON.stringify({ from: PKG.version, to: '9.0.0', spec: NOTE, startedAt: new Date().toISOString() }))
+ const { rt, warnings } = start({ home })
+ assert.equal(rt.updateView().phase, 'idle')
+ assert.deepEqual(warnings, [])
+ assert.equal(existsSync(join(home, 'update.json')), true)
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+})
+
+test('a note that cannot be read is warned about and deleted', async () => {
+ const home = newHome()
+ writeFileSync(join(home, 'update.json'), 'not json {')
+ const { rt, warnings } = start({ home })
+ assert.equal(rt.updateView().phase, 'idle')
+ assert.equal(warnings.length, 1)
+ assert.equal(existsSync(join(home, 'update.json')), false)
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+})