fix: refresh artifact API when upgrading a live SQLite host
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# Owned 1.9.0 artifact storage fixture
|
||||
|
||||
`artifact-store.mjs` is the unchanged source at commit `91981c1`. It retains the
|
||||
pre-browser text-only artifact API. Upgrade regressions use its actual object
|
||||
and existing blobs to verify a new binary API after the host upgrades, and
|
||||
rollback to the old object if installing the new request handler fails.
|
||||
@@ -0,0 +1,40 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
|
||||
export class ArtifactStore {
|
||||
constructor(directory) {
|
||||
this.root = path.join(directory, 'artifacts'); fs.mkdirSync(this.root, { recursive: true });
|
||||
if (fs.lstatSync(this.root).isSymbolicLink()) throw new Error('执行工件目录不能是链接。');
|
||||
}
|
||||
write(session, run, value, { kind = 'tool-result', mediaType = 'application/json' } = {}) {
|
||||
const bytes = Buffer.from(typeof value === 'string' ? value : JSON.stringify(value), 'utf8');
|
||||
if (bytes.length > 8 * 1024 * 1024) throw new Error('单项执行工件超过8MB,请缩小输出范围。');
|
||||
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
||||
const relative = sha256.slice(0, 2) + '/' + sha256 + '.blob';
|
||||
const file = path.join(this.root, relative);
|
||||
fs.mkdirSync(path.dirname(file), { recursive: true });
|
||||
if (fs.lstatSync(path.dirname(file)).isSymbolicLink() || fs.existsSync(file) && fs.lstatSync(file).isSymbolicLink()) throw new Error('执行工件不能通过链接写入。');
|
||||
if (fs.existsSync(file)) {
|
||||
const existing = fs.readFileSync(file);
|
||||
if (existing.length !== bytes.length || createHash('sha256').update(existing).digest('hex') !== sha256) {
|
||||
throw new Error('已有执行工件校验失败,实际结果需要核对。');
|
||||
}
|
||||
} else {
|
||||
const temporary = file + '.' + randomUUID() + '.tmp';
|
||||
const handle = fs.openSync(temporary, 'wx', 0o600);
|
||||
try { fs.writeFileSync(handle, bytes); fs.fsyncSync(handle); } finally { fs.closeSync(handle); }
|
||||
try { fs.renameSync(temporary, file); } finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); }
|
||||
}
|
||||
return { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: run?.taskId ?? session.taskId ?? null,
|
||||
runId: run?.id ?? session.runId ?? null, kind, mediaType, sha256, bytes: bytes.length, relative, createdAt: new Date().toISOString(), status: 'stored' };
|
||||
}
|
||||
read(artifact) {
|
||||
if (!/^[a-f0-9]{2}\/[a-f0-9]{64}\.blob$/.test(artifact.relative)) throw new Error('执行工件路径无效。');
|
||||
const file = path.join(this.root, artifact.relative);
|
||||
if (fs.lstatSync(path.dirname(file)).isSymbolicLink() || fs.lstatSync(file).isSymbolicLink()) throw new Error('执行工件不能是链接。');
|
||||
const bytes = fs.readFileSync(file);
|
||||
if (bytes.length !== artifact.bytes || createHash('sha256').update(bytes).digest('hex') !== artifact.sha256) throw new Error('执行工件校验失败,实际结果需要核对。');
|
||||
return bytes.toString('utf8');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import http from 'node:http';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { fixture, workspace } from './helpers.mjs';
|
||||
import { createBrowserProject, browserSteps } from './fixtures/browser-project.mjs';
|
||||
import { ArtifactStore as LegacyArtifacts } from './fixtures/legacy-artifacts-1.9/artifact-store.mjs';
|
||||
import { Provider } from '../src/server/provider.mjs';
|
||||
import { Runtime } from '../src/server/runtime.mjs';
|
||||
import { createRequestHandler } from '../src/server/app.mjs';
|
||||
import { installUpgrade } from '../src/server/hot-upgrade.mjs';
|
||||
import { verifyBrowserJob } from '../src/core/browser/browser-manager.mjs';
|
||||
|
||||
async function legacyContext(t) {
|
||||
const f = createBrowserProject(fixture(t));
|
||||
const provider = new Provider(); provider.setKey('owned-upgrade-fixture-key');
|
||||
const runtime = new Runtime(f.store, provider, { appDirectory: workspace });
|
||||
f.store.artifactStore = new LegacyArtifacts(f.data);
|
||||
const context = { store: f.store, provider, runtime, server: http.createServer(), token: 'owned-upgrade-token',
|
||||
clients: new Set(), started: Date.now(), exampleProject: null, shutdown: async () => {}, getPort: () => context.server.address().port };
|
||||
context.server.on('request', createRequestHandler({ ...context, appDirectory: workspace }));
|
||||
await new Promise(resolve => context.server.listen(0, '127.0.0.1', resolve));
|
||||
f.onCleanup(async () => { context.server.closeAllConnections(); await new Promise(resolve => context.server.close(resolve)); });
|
||||
return { f, context };
|
||||
}
|
||||
|
||||
test('a live SQLite host with the actual 1.9 artifact object upgrades, keeps its original blobs and verifies real browser PNG bytes', { timeout: 30000 }, async t => {
|
||||
const { f, context } = await legacyContext(t), original = context.store.artifactStore;
|
||||
const run = f.store.core.run.get(f.session.runId), value = { original: '旧版实际 UTF-8 工件', n: 19 };
|
||||
const artifact = original.write(f.session, run, value); f.store.core.artifact.set(artifact.id, artifact); f.store.save(f.session);
|
||||
const bytes = fs.readFileSync(path.join(original.root, artifact.relative));
|
||||
const beforeSession = structuredClone(f.store.publicSession(f.session));
|
||||
assert.equal(typeof original.readBytes, 'undefined');
|
||||
const upgraded = installUpgrade(context, { appDirectory: workspace, updateServiceInfo: false });
|
||||
assert.equal(upgraded.hasKey, true); assert.equal(context.provider, context.runtime.provider);
|
||||
assert.deepEqual(f.store.publicSession(f.session), beforeSession);
|
||||
assert.deepEqual(f.store.artifactStore.readBytes(artifact), bytes);
|
||||
assert.deepEqual(JSON.parse(f.store.artifactStore.read(artifact)), value);
|
||||
const planned = await context.runtime.browserManager().plan(f.session, { steps: browserSteps });
|
||||
const active = await context.runtime.browserManager().start(f.session, planned.id); await active.promise;
|
||||
assert.equal(active.job.status, 'passed'); assert.equal(active.job.cleanup.exited, true);
|
||||
const record = verifyBrowserJob(f.store, f.session, active.job); assert.equal(record.assertions.passed, record.assertions.total);
|
||||
const image = active.job.screenshots[0], stored = f.store.owned('artifact', image.artifactId, f.session.id);
|
||||
const response = await fetch('http://127.0.0.1:' + context.server.address().port + '/api/sessions/' + f.session.id + '/browser-jobs/' + active.job.id + '/images/' + image.artifactId,
|
||||
{ headers: { 'X-Workbench-Token': context.token, Connection: 'close' } });
|
||||
assert.equal(response.status, 200); assert.equal(response.headers.get('content-type'), 'image/png');
|
||||
const actual = Buffer.from(await response.arrayBuffer());
|
||||
assert.ok(actual.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])));
|
||||
assert.equal(createHash('sha256').update(actual).digest('hex'), stored.sha256);
|
||||
assert.equal(fs.readFileSync(path.join(original.root, artifact.relative)).equals(bytes), true);
|
||||
});
|
||||
|
||||
test('failed upgrade restores the original artifact object, provider and request handler', async t => {
|
||||
const { f, context } = await legacyContext(t), original = f.store.artifactStore, handlers = context.server.listeners('request');
|
||||
const runtimeProperties = { appDirectory: context.runtime.appDirectory, browser: context.runtime.browser };
|
||||
fs.mkdirSync(path.join(f.data, 'service-info.json'));
|
||||
assert.throws(() => installUpgrade(context, { appDirectory: workspace }));
|
||||
assert.equal(f.store.artifactStore, original); assert.equal(typeof f.store.artifactStore.readBytes, 'undefined');
|
||||
assert.deepEqual(context.server.listeners('request'), handlers); assert.equal(context.provider.hasKey(), true);
|
||||
assert.equal(context.runtime.appDirectory, runtimeProperties.appDirectory); assert.equal(context.runtime.browser, runtimeProperties.browser);
|
||||
const artifact = original.write(f.session, f.store.core.run.get(f.session.runId), { rollback: true });
|
||||
assert.deepEqual(JSON.parse(original.read(artifact)), { rollback: true });
|
||||
});
|
||||
Reference in New Issue
Block a user