Files
memind/scripts/verify-public-finish-sync-runtime.mjs
2026-07-27 15:34:35 +08:00

85 lines
2.7 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {
hasRecentOwnPublicHtmlReference,
materializeMissingPublicHtmlWrites,
} from '../mindspace-public-finish-sync.mjs';
const root = path.resolve(new URL('..', import.meta.url).pathname);
const runtimeServer = path.join(root, '.runtime', 'portal', 'server.mjs');
const currentUser = { id: 'a6fb1e97-2b0f-447b-b138-4561d8e5c53e', username: 'john' };
const toolRequestOnlyMessages = [
{
role: 'assistant',
content: [
{
type: 'toolRequest',
toolCall: {
value: {
name: 'write',
arguments: {
path: 'public/guarded-release.html',
content: '<!doctype html><title>Guarded release</title>',
},
},
},
},
],
},
...Array.from({ length: 12 }, (_, index) => ({
role: index % 2 === 0 ? 'assistant' : 'user',
content: [{ type: 'text', text: `validation step ${index + 1}` }],
})),
];
assert.equal(
hasRecentOwnPublicHtmlReference(toolRequestOnlyMessages, currentUser),
true,
'finish sync must detect textless write tool requests beyond the old eight-message window',
);
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'public-finish-release-guard-'));
try {
const result = materializeMissingPublicHtmlWrites({
messages: toolRequestOnlyMessages,
publishDir,
});
assert.deepEqual(result.materialized, ['public/guarded-release.html']);
assert.equal(
fs.readFileSync(path.join(publishDir, 'public/guarded-release.html'), 'utf8'),
'<!doctype html><title>Guarded release</title>',
);
} finally {
fs.rmSync(publishDir, { recursive: true, force: true });
}
if (!fs.existsSync(runtimeServer)) {
throw new Error(`runtime server bundle is missing: ${runtimeServer}`);
}
const runtimeSource = fs.readFileSync(runtimeServer, 'utf8');
const requiredRuntimePatterns = [
[/normalizedName === "write"/, 'plain write tool request detection'],
[/normalizedName\.endsWith\("__write"\)/, 'namespaced write tool request detection'],
[/normalizedName === "edit_file"/, 'edit_file tool request detection'],
[/applyPublicHtmlEdit/, 'public HTML edit materialization'],
[/recentCount = 80/, 'wide recent message scan window'],
[
/if \(extractPublicHtmlWriteArtifacts\(\[message\], \{ publishDir(?:: \w+)? \}\)\.length > 0\) \{/,
'textless session-event HTML write prefilter',
],
];
for (const [pattern, label] of requiredRuntimePatterns) {
assert.ok(
pattern.test(runtimeSource),
`runtime server bundle is missing public finish sync guard: ${label}`,
);
}
console.log('public finish sync runtime guard ok');