fix(storage): canonicalize retained Windows virtual data roots without reopening SQLite

This commit is contained in:
Agent Workbench Dev
2026-10-31 14:54:58 +08:00
parent dd31eefc77
commit 85e0f0e5ce
3 changed files with 46 additions and 0 deletions
+4
View File
@@ -94,6 +94,10 @@ export class Store extends EventEmitter {
}
ensureStorage(options = {}) {
// A retained pre-desktop Store may still name an MSIX-virtualized LocalAppData
// directory. Bind new path-scoped services to that same physical directory,
// including when SQLite is already open; do not reopen or move its data.
this.directory = fs.realpathSync.native(this.directory);
if (this.storage?.database) {
const missing = CORE_KINDS.filter(kind => !this.core?.[kind]);
if (missing.length) {
+12
View File
@@ -13,6 +13,18 @@ const create = f => { const provider = new Provider(); provider.setKey(''); retu
const add = (hub, name, extra = {}) => hub.upsert({ name, model: name, baseUrl: 'http://127.0.0.1:12001/v1', apiKey: 'owned-vault-' + name, ...extra });
const reopen = f => { f.store.close(); const store = new Store(f.data); f.onCleanup(() => store.close()); const provider = new Provider(); provider.setKey(''); return { store, provider, hub: provider.modelHub(store) }; };
test('a Windows virtual data-directory name binds new storage and credentials to its actual physical root', t => {
const f = fixture(t), logical = path.join(f.root, 'virtual-local-data'), physical = path.join(f.root, 'physical-local-data');
fs.mkdirSync(logical); fs.mkdirSync(physical);
const realpath = fs.realpathSync.native;
t.mock.method(fs.realpathSync, 'native', (file, ...options) => path.resolve(file) === logical ? physical : realpath(file, ...options));
const store = new Store(logical); f.onCleanup(() => store.close());
const provider = new Provider(); provider.setKey(''); const hub = provider.modelHub(store);
assert.equal(store.directory, physical); assert.equal(store.storage.filename, path.join(physical, 'workbench.sqlite'));
assert.equal(hub.vault().view(hub).unavailable, false);
assert.equal(fs.existsSync(path.join(physical, 'workbench.sqlite')), true); assert.deepEqual(fs.readdirSync(logical), []);
});
test('actual Windows CurrentUser DPAPI roundtrips owned UTF-8 and rejects damaged bytes and wrong entropy', () => {
const plain = Buffer.from('自有测试凭据 owned-key-only', 'utf8'), entropy = randomBytes(32), encrypted = protectCredential(plain, entropy);
assert.notDeepEqual(encrypted, plain); assert.deepEqual(unprotectCredential(encrypted, entropy), plain);
+30
View File
@@ -1,6 +1,8 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fixture, workspace } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
@@ -19,6 +21,34 @@ async function setup(t) {
return { ...f, context, provider, hub, runtime, server };
}
test('hot upgrade adopts a retained virtual data root without reopening SQLite or changing rows, Provider or routing', async t => {
const f = await setup(t), logical = path.join(f.root, 'virtual-local-data'), physical = f.store.directory;
fs.mkdirSync(logical); const realpath = fs.realpathSync.native;
t.mock.method(fs.realpathSync, 'native', (file, ...options) => path.resolve(file) === logical ? physical : realpath(file, ...options));
const storage = f.store.storage, database = storage.database, hub = f.hub, credentials = f.provider.credentials;
const rows = () => ['metadata', 'projects', 'sessions', 'records', 'events', 'outbox'].map(table => database.prepare('SELECT * FROM ' + table + ' ORDER BY rowid').all().filter(row => table !== 'metadata' || row.key !== 'runtimeOwner'));
const before = rows(), state = structuredClone(hub.state);
f.store.directory = logical;
installUpgrade(f.context, { appDirectory: workspace, updateServiceInfo: false });
assert.equal(f.store.directory, physical); assert.equal(f.store.storage, storage); assert.equal(storage.database, database);
assert.equal(f.provider.hub, hub); assert.equal(f.provider.credentials, credentials); assert.deepEqual(hub.state, state);
assert.deepEqual(rows(), before); assert.deepEqual(fs.readdirSync(logical), []);
const response = await fetch('http://127.0.0.1:' + f.server.address().port + '/api/model-center/credentials', { headers: { 'X-Workbench-Token': f.context.token } });
assert.equal(response.status, 200); assert.equal((await response.json()).unavailable, false);
assert.deepEqual(rows(), before); assert.equal(f.provider.apiKey, 'owned-upgrade-fixture');
});
test('failed adoption restores the original virtual data-root name and original open database', async t => {
const f = await setup(t), logical = path.join(f.root, 'virtual-local-data'), physical = f.store.directory;
fs.mkdirSync(logical); const realpath = fs.realpathSync.native;
t.mock.method(fs.realpathSync, 'native', (file, ...options) => path.resolve(file) === logical ? physical : realpath(file, ...options));
const storage = f.store.storage, handler = f.server.listeners('request')[0]; f.store.directory = logical;
f.store.claimRuntime = () => { throw new Error('owned directory adoption failure'); };
assert.throws(() => installUpgrade(f.context, { appDirectory: workspace, updateServiceInfo: false }), /owned directory adoption failure/);
assert.equal(f.store.directory, logical); assert.equal(f.store.storage, storage); assert.equal(f.server.listeners('request')[0], handler);
assert.equal(f.provider.apiKey, 'owned-upgrade-fixture'); assert.deepEqual(fs.readdirSync(logical), []);
});
test('hot upgrade refreshes retained model and importer methods while preserving credential and routing objects', async t => {
const f = await setup(t), credentials = f.provider.credentials, state = f.hub.state;
const oldHub = Object.create(ModelHub.prototype), oldImporter = Object.create(ConfigImporter.prototype);