Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

fix(preview): preserve incompatible backend identity during discovery

罗高9d9f133c5ff6
diff --git a/preview.ts b/preview.ts
index ebd9fef6..1003c222 100644
--- a/preview.ts
+++ b/preview.ts
@@ -20,12 +20,13 @@ export type PreviewRuntime = {
attempts: number;
};
-async function probe(port: number): Promise<Probe> {
+export async function probe(port: number): Promise<Probe> {
try {
const response = await fetch(`http://127.0.0.1:${port}/api/health`, { signal: AbortSignal.timeout(1200) });
if (!response.ok) return { kind: 'busy' };
const health = await response.json() as Health;
- return health.service === 'motion-preview' && health.protocol === 2 && health.ready && typeof health.dataDirectory === 'string'
+ // Preserve authority identity so discovery can reject it before launching another backend.
+ return health.service === 'motion-preview' && typeof health.dataDirectory === 'string'
? { kind: 'motion', health } : { kind: 'busy' };
} catch (error) {
const code = (error as { cause?: { code?: string } }).cause?.code;
@@ -88,7 +89,7 @@ export async function openPreview(store = new WorkspaceStore(), environment: Pre
const port = await registeredPort(store);
if (port) {
const found = await probe(port);
- existingLegacy = found.kind === 'motion' && found.health.version === VERSION && path.resolve(found.health.dataDirectory) === store.directory;
+ existingLegacy = found.kind === 'motion' && found.health.protocol === 2 && found.health.ready && found.health.version === VERSION && path.resolve(found.health.dataDirectory) === store.directory;
}
}
if (sharedEditorEnabled && environment === runtime && !existingLegacy) {
diff --git a/tests/preview.test.ts b/tests/preview.test.ts
index 918e1071..0d157b7e 100644
--- a/tests/preview.test.ts
+++ b/tests/preview.test.ts
@@ -1,12 +1,14 @@
import test from 'node:test';
import assert from 'node:assert/strict';
-import { readFile, writeFile } from 'node:fs/promises';
+import { createServer } from 'node:http';
+import { readFile, realpath, writeFile } from 'node:fs/promises';
import path from 'node:path';
-import { ensureService, openPreview, previewUrl, VERSION } from '../preview.ts';
+import { ensureService, openPreview, previewUrl, probe, VERSION } from '../preview.ts';
import type { Probe, PreviewRuntime } from '../preview.ts';
import { WorkspaceStore } from '../workspace.ts';
import { pluginRoot } from '../hyperframes.ts';
import { phase1McpCapabilities } from '../shared/phase1-tools.ts';
+import { sharedEditorEnabled } from '../editor-runtime.ts';
import { fixture } from './fixtures.ts';
function runtime(store: WorkspaceStore) {
@@ -88,6 +90,38 @@ test('phase1 port discovery refuses an incompatible same-workspace authority wit
assert.deepEqual(await store.get(), before);
}, false));
});
+test('real HTTP discovery preserves Motion identity before checking compatibility and readiness', () => fixture(async temporary => {
+ const store = new WorkspaceStore(await realpath(temporary.directory));
+ const health = { service: 'motion-preview', protocol: 999, version: '0.6.4', dataDirectory: store.directory, ffmpeg: true, ready: true, mcp: phase1McpCapabilities };
+ const server = createServer((req, res) => {
+ assert.equal(req.url, '/api/health');
+ res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(health));
+ });
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
+ try {
+ const address = server.address(); assert.ok(address && typeof address !== 'string');
+ await writeFile(path.join(store.directory, 'preview.json'), JSON.stringify({ port: address.port }));
+ const before = await store.get(), mock = runtime(store);
+ mock.environment.probe = probe;
+ mock.environment.launch = async port => { mock.launches.push(port); throw new Error('Unexpected competing backend launch'); };
+ for (const change of [{ protocol: 999, ready: true }, { protocol: 2, ready: false }]) {
+ Object.assign(health, change);
+ assert.deepEqual(await probe(address.port), { kind: 'motion', health });
+ await assert.rejects(ensureService(store, mock.environment, 'motion_export_request'), { code: 'EDITOR_RUNTIME_INCOMPATIBLE' });
+ assert.deepEqual(mock.launches, []); assert.deepEqual(mock.stops, []);
+ assert.deepEqual(await store.get(), before);
+ assert.deepEqual(JSON.parse(await readFile(path.join(store.directory, 'preview.json'), 'utf8')), { port: address.port });
+ if (sharedEditorEnabled) {
+ health.version = VERSION;
+ await assert.rejects(openPreview(store), /旧预览服务/);
+ health.version = '0.6.4';
+ }
+ }
+ health.ready = true;
+ assert.equal((await ensureService(store, mock.environment, 'motion_export_request')).reused, true);
+ assert.deepEqual(mock.launches, []);
+ } finally { server.closeAllConnections(); await new Promise<void>(resolve => server.close(() => resolve())); }
+}, false));
test('concurrent preview requests create one process and preserve existing source', () => fixture(async store => {
const current = await store.get(); await store.writeFiles(current.revision, [{ path: 'notes.md', content: 'preserve me' }]);
const before = await store.get(), mock = runtime(store);