From fdc21b0eadc14bb30f7e140d30589d09df2ed324 Mon Sep 17 00:00:00 2001 From: Qiuner Date: Mon, 30 Nov 2026 12:12:23 -0300 Subject: [PATCH] Commit desktop upgrades only after the chat view opens --- apps/desktop/src/main.mjs | 17 +++-- apps/desktop/test/upgrade-handoff.test.mjs | 89 ++++++++++++++++++++++ 2 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 apps/desktop/test/upgrade-handoff.test.mjs diff --git a/apps/desktop/src/main.mjs b/apps/desktop/src/main.mjs index 38f72cf94..4dfa8e980 100644 --- a/apps/desktop/src/main.mjs +++ b/apps/desktop/src/main.mjs @@ -500,7 +500,9 @@ async function openContent(ready, installed) { status(ready); content.focus(); if (WINDOWS) buildMenu(); + return true; } + return false; } function trustedShell(event) { @@ -567,6 +569,13 @@ async function recoverInterruptedUpgrade() { await writeJSON(transactionFile, { ...transaction, phase: 'recovered' }); } +async function openAndActivate(ready, installed, transaction) { + if (!await openContent(ready, installed) || quitting) return false; + await writeJSON(activeFile, installed.manifest); + if (transaction) await writeJSON(transactionFile, { ...transaction, phase: 'complete' }); + return true; +} + async function logStartupFailure(error) { // Bundle extraction can fail before the supervisor creates the runtime log. console.error(error); @@ -624,11 +633,9 @@ async function boot() { } const ready = await launchRuntime(active); if (quitting) return; - await writeJSON(activeFile, active.manifest); - if (transaction) await writeJSON(transactionFile, { ...transaction, phase: 'complete' }); watchInterface(); await refreshInterface(); - await openContent(ready, active); + await openAndActivate(ready, active, transaction); } catch (error) { if (quitting) return; await logStartupFailure(error); @@ -668,9 +675,7 @@ async function installUpdate() { transaction = { phase: 'testing', previous: active.manifest, candidate: next.manifest, backup }; await writeJSON(transactionFile, transaction); const ready = await launchRuntime(next); - await openContent(ready, next); - await writeJSON(activeFile, next.manifest); - await writeJSON(transactionFile, { ...transaction, phase: 'complete' }); + if (!await openAndActivate(ready, next, transaction)) return; active = next; buildMenu(); publish(); diff --git a/apps/desktop/test/upgrade-handoff.test.mjs b/apps/desktop/test/upgrade-handoff.test.mjs new file mode 100644 index 000000000..d51ce24ae --- /dev/null +++ b/apps/desktop/test/upgrade-handoff.test.mjs @@ -0,0 +1,89 @@ +import assert from 'node:assert/strict'; +import { fork } from 'node:child_process'; +import { once } from 'node:events'; +import { cp, mkdir, mkdtemp, readFile, realpath, rename, rm, symlink, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { DatabaseSync } from 'node:sqlite'; +import { test } from 'node:test'; +import { fileURLToPath } from 'node:url'; +import { runInNewContext } from 'node:vm'; + +const main = await readFile(new URL('../src/main.mjs', import.meta.url), 'utf8'); +const functions = [...main.matchAll(/^async function (writeJSON|readJSON|recoverInterruptedUpgrade|openAndActivate|boot|installUpdate)\([^]*?^\}/gm)] + .map(match => match[0]).join('\n'); +const previous = { runtimeId: 'old' }; +const candidate = { runtimeId: 'new' }; + +function session(root, action) { + const database = new DatabaseSync(path.join(root, 'sessions.sqlite3')); + try { return action(database); } finally { database.close(); } +} + +function run(root, command, openContent) { + const runtimeHome = path.join(root, 'workspace'); + return runInNewContext(`${functions}\n${command}`, { + userRoot: root, runtimeHome, activeFile: path.join(root, 'active.json'), + transactionFile: path.join(root, 'upgrade.json'), + path, mkdir, readFile, writeFile, rename, cp, openContent, + busy: false, quitting: false, active: { manifest: previous }, publicKey: '', + window: {}, t: key => key, buildMenu() {}, publish() {}, status() {}, stopWorker() {}, + dialog: { + showOpenDialog: () => ({ filePaths: ['seed'] }), + showMessageBox: () => ({ response: 1 }), + }, + startupManifest: () => ({ manifestFile: 'seed', previous, installed: 'old' }), + installBundle: () => ({ manifest: candidate }), + launchRuntime: async () => { + session(runtimeHome, database => database.exec("UPDATE messages SET content = 'candidate answer'")); + await writeFile(path.join(runtimeHome, 'knowledge/document.txt'), 'candidate document'); + return { url: 'http://localhost/' }; + }, + watchInterface() {}, refreshInterface() {}, logStartupFailure(error) { throw error; }, + }); +} + +if (process.argv[2] === 'interrupt') { + await run(process.argv[3], process.argv[4], () => { + process.send('loading'); + return new Promise(() => setInterval(() => {}, 1000)); + }); +} else for (const [entry, command] of [['overlay', 'boot()'], ['manual update', 'installUpdate()']]) { + test(`an interrupted ${entry} restores the previous manifest and workspace before retry`, { timeout: 20000 }, async t => { + const root = await mkdtemp(path.join(tmpdir(), 'desktop-upgrade-')); + t.after(() => rm(root, { recursive: true, force: true })); + const physical = path.join(root, 'data'); + const linked = path.join(root, 'linked'); + await mkdir(path.join(physical, 'workspace/knowledge'), { recursive: true }); + await symlink(physical, linked, process.platform === 'win32' ? 'junction' : 'dir'); + await writeFile(path.join(linked, 'active.json'), JSON.stringify(previous)); + session(path.join(linked, 'workspace'), database => database.exec( + "CREATE TABLE messages (id INTEGER PRIMARY KEY, content TEXT); INSERT INTO messages VALUES (42, 'previous answer')", + )); + await writeFile(path.join(linked, 'workspace/knowledge/document.txt'), 'previous document'); + const json = async name => JSON.parse(await readFile(path.join(linked, name), 'utf8')); + const child = fork(fileURLToPath(import.meta.url), ['interrupt', linked, command], { + execArgv: [], stdio: ['ignore', 'ignore', 'inherit', 'ipc'], + }); + const exited = once(child, 'exit'); + try { + assert.equal(await Promise.race([once(child, 'message').then(([message]) => message), exited]), 'loading'); + } finally { + child.kill('SIGKILL'); + await exited; + } + assert.equal((await json('upgrade.json')).phase, 'testing'); + assert.deepEqual(await json('active.json'), previous); + await run(linked, 'recoverInterruptedUpgrade()'); + assert.equal(await realpath(linked), await realpath(physical)); + assert.equal((await json('upgrade.json')).phase, 'recovered'); + assert.deepEqual(await json('active.json'), previous); + const answer = session(path.join(physical, 'workspace'), database => + database.prepare('SELECT content FROM messages WHERE id = 42').get()); + assert.equal(answer.content, 'previous answer'); + assert.equal(await readFile(path.join(physical, 'workspace/knowledge/document.txt'), 'utf8'), 'previous document'); + await run(linked, command, async () => true); + assert.deepEqual(await json('active.json'), candidate); + assert.equal((await json('upgrade.json')).phase, 'complete'); + }); +}