Sync all mirrored docs idempotently in frontend build (#5420)

frontend-modern/scripts/sync-public-docs.mjs previously copied only 10
hardcoded docs and invoked fs.copyFile unconditionally on every build.
That silently left the other 57 mirrored docs under public/docs/ out of
sync during npm run build and failed with EPERM (while unlinking
public/docs/README.md) on worktrees where unchanged mirrored docs carry
group/ACL permissions.

Discover all mirrored markdown files under frontend-modern/public/docs/
using the same root-sourced set as scripts/check_docs_mirror.py and
docsLinks.test.ts while retaining the 10 baseline docs for minimal build
fixtures, skip writing byte-identical targets, and add unit tests in
frontend-modern/scripts/__tests__/sync-public-docs.test.mjs.

---
Fleet candidate `20261128T161326Z-web-product` from the `web-product`
lane. Reviewed tip `c6b46dcecdd4769df9ea79aa76ba62453d01ea12` on `main`.

[How Pulse handles
triage](https://github.com/rcourtman/Pulse/blob/main/docs/AI_TRANSPARENCY.md)
This commit is contained in:
pulse-triage[bot]
2026-11-28 17:25:27 +00:00
committed by GitHub
2 changed files with 209 additions and 23 deletions

View File

@@ -0,0 +1,118 @@
// @vitest-environment node
import {
chmodSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
statSync,
writeFileSync,
} from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { CORE_SHIPPED_DOCS, resolveDocSourcePath, syncPublicDocs } from '../sync-public-docs.mjs';
const tempDirs = [];
function createFixture() {
const root = mkdtempSync(path.join(os.tmpdir(), 'pulse-sync-docs-'));
tempDirs.push(root);
const targetDocsDir = path.join(root, 'frontend-modern', 'public', 'docs');
for (const rel of CORE_SHIPPED_DOCS) {
const source = resolveDocSourcePath(rel, root);
mkdirSync(path.dirname(source), { recursive: true });
writeFileSync(source, `# ${rel}\n`);
}
return { root, targetDocsDir };
}
afterEach(() => {
while (tempDirs.length > 0) {
const dir = tempDirs.pop();
try {
// Restore write permission on any read-only test file before cleanup.
const readme = path.join(dir, 'frontend-modern', 'public', 'docs', 'README.md');
chmodSync(readme, 0o644);
} catch {
// Ignore missing files.
}
rmSync(dir, { recursive: true, force: true });
}
});
describe('sync-public-docs', () => {
it('seeds core shipped docs and updates all mirrored docs including root-sourced and nested docs', async () => {
const { root, targetDocsDir } = createFixture();
// Add extra mirrored docs (both docs/-sourced, nested, and root-sourced)
// with stale copies under public/docs.
const extraFiles = [
{
source: path.join(root, 'docs', 'ALERTING.md'),
mirror: path.join(targetDocsDir, 'ALERTING.md'),
fresh: '# Fresh Alerting\n',
},
{
source: path.join(root, 'docs', 'guides', 'WEBHOOKS.md'),
mirror: path.join(targetDocsDir, 'guides', 'WEBHOOKS.md'),
fresh: '# Fresh Webhooks\n',
},
{
source: path.join(root, 'ARCHITECTURE.md'),
mirror: path.join(targetDocsDir, 'ARCHITECTURE.md'),
fresh: '# Fresh Architecture\n',
},
{
source: path.join(root, 'CONTRIBUTING.md'),
mirror: path.join(targetDocsDir, 'CONTRIBUTING.md'),
fresh: '# Fresh Contributing\n',
},
];
for (const item of extraFiles) {
mkdirSync(path.dirname(item.source), { recursive: true });
writeFileSync(item.source, item.fresh);
mkdirSync(path.dirname(item.mirror), { recursive: true });
writeFileSync(item.mirror, '# Stale copy\n');
}
const first = await syncPublicDocs({ repoRoot: root, targetDocsDir });
expect(first.total).toBe(CORE_SHIPPED_DOCS.length + extraFiles.length);
expect(first.updated).toBe(first.total);
expect(first.unchanged).toBe(0);
for (const item of extraFiles) {
expect(readFileSync(item.mirror, 'utf8')).toBe(item.fresh);
}
expect(readFileSync(path.join(targetDocsDir, 'SECURITY.md'), 'utf8')).toBe('# SECURITY.md\n');
});
it('skips byte-identical targets without rewriting or failing on read-only unchanged mirrors', async () => {
const { root, targetDocsDir } = createFixture();
const first = await syncPublicDocs({ repoRoot: root, targetDocsDir });
expect(first.updated).toBe(CORE_SHIPPED_DOCS.length);
const readmeMirror = path.join(targetDocsDir, 'README.md');
chmodSync(readmeMirror, 0o444);
const beforeMtimeMs = statSync(readmeMirror).mtimeMs;
const second = await syncPublicDocs({ repoRoot: root, targetDocsDir });
expect(second.updated).toBe(0);
expect(second.unchanged).toBe(CORE_SHIPPED_DOCS.length);
expect(statSync(readmeMirror).mtimeMs).toBe(beforeMtimeMs);
expect(readFileSync(readmeMirror, 'utf8')).toBe('# README.md\n');
});
it('fails clearly when a mirrored doc has no repository source', async () => {
const { root, targetDocsDir } = createFixture();
mkdirSync(targetDocsDir, { recursive: true });
writeFileSync(path.join(targetDocsDir, 'ORPHAN.md'), '# Missing source\n');
await expect(syncPublicDocs({ repoRoot: root, targetDocsDir })).rejects.toThrow(/ORPHAN\.md/);
});
});

View File

@@ -1,37 +1,105 @@
import { copyFile, mkdir } from 'node:fs/promises';
import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const frontendRoot = path.resolve(__dirname, '..');
const repoRoot = path.resolve(frontendRoot, '..');
const targetDocsDir = path.join(frontendRoot, 'public', 'docs');
const defaultFrontendRoot = path.resolve(__dirname, '..');
const defaultRepoRoot = path.resolve(defaultFrontendRoot, '..');
const defaultTargetDocsDir = path.join(defaultFrontendRoot, 'public', 'docs');
const shippedDocs = [
{ source: path.join(repoRoot, 'docs', 'README.md'), target: 'README.md' },
{
source: path.join(repoRoot, 'docs', 'MIGRATION_UNIFIED_NAV.md'),
target: 'MIGRATION_UNIFIED_NAV.md',
},
{ source: path.join(repoRoot, 'docs', 'PRIVACY.md'), target: 'PRIVACY.md' },
{ source: path.join(repoRoot, 'docs', 'CONFIGURATION.md'), target: 'CONFIGURATION.md' },
{ source: path.join(repoRoot, 'docs', 'UNIFIED_AGENT.md'), target: 'UNIFIED_AGENT.md' },
{ source: path.join(repoRoot, 'docs', 'AGENT_SECURITY.md'), target: 'AGENT_SECURITY.md' },
{ source: path.join(repoRoot, 'docs', 'PROXY_AUTH.md'), target: 'PROXY_AUTH.md' },
{ source: path.join(repoRoot, 'docs', 'TRUENAS.md'), target: 'TRUENAS.md' },
{ source: path.join(repoRoot, 'SECURITY.md'), target: 'SECURITY.md' },
{ source: path.join(repoRoot, 'TERMS.md'), target: 'TERMS.md' },
// Shipped from the repository root rather than docs/. Matches
// scripts/check_docs_mirror.py and docsLinks.test.ts.
export const ROOT_SOURCED_DOCS = new Set([
'SECURITY.md',
'TERMS.md',
'ARCHITECTURE.md',
'CONTRIBUTING.md',
]);
// Baseline docs seeded even when public/docs does not yet exist (e.g. minimal
// build fixtures). Existing markdown files under public/docs are discovered
// dynamically so all shipped mirrors stay in sync with repo docs.
export const CORE_SHIPPED_DOCS = [
'README.md',
'MIGRATION_UNIFIED_NAV.md',
'PRIVACY.md',
'CONFIGURATION.md',
'UNIFIED_AGENT.md',
'AGENT_SECURITY.md',
'PROXY_AUTH.md',
'TRUENAS.md',
'SECURITY.md',
'TERMS.md',
];
export async function syncPublicDocs() {
await mkdir(targetDocsDir, { recursive: true });
export function resolveDocSourcePath(relativeTarget, repoRoot = defaultRepoRoot) {
const normalized = relativeTarget.split(path.sep).join('/');
if (ROOT_SOURCED_DOCS.has(normalized)) {
return path.join(repoRoot, normalized);
}
return path.join(repoRoot, 'docs', ...normalized.split('/'));
}
for (const { source, target } of shippedDocs) {
await copyFile(source, path.join(targetDocsDir, target));
async function collectMirroredMarkdown(dir, prefix = '') {
let entries;
try {
entries = await readdir(dir, { withFileTypes: true });
} catch (error) {
if (error?.code === 'ENOENT') return [];
throw error;
}
console.log(`Synced shipped docs to ${targetDocsDir}`);
const found = [];
for (const entry of entries) {
const relative = prefix ? `${prefix}/${entry.name}` : entry.name;
if (entry.isDirectory()) {
found.push(...(await collectMirroredMarkdown(path.join(dir, entry.name), relative)));
} else if (entry.isFile() && entry.name.endsWith('.md')) {
found.push(relative);
}
}
return found;
}
export async function syncPublicDocs({
repoRoot = defaultRepoRoot,
targetDocsDir = defaultTargetDocsDir,
} = {}) {
await mkdir(targetDocsDir, { recursive: true });
const discovered = await collectMirroredMarkdown(targetDocsDir);
const targets = Array.from(new Set([...CORE_SHIPPED_DOCS, ...discovered])).sort();
let updated = 0;
let unchanged = 0;
for (const relativeTarget of targets) {
const sourcePath = resolveDocSourcePath(relativeTarget, repoRoot);
const targetPath = path.join(targetDocsDir, ...relativeTarget.split('/'));
const sourceBytes = await readFile(sourcePath);
let existingBytes = null;
try {
existingBytes = await readFile(targetPath);
} catch (error) {
if (error?.code !== 'ENOENT') throw error;
}
if (existingBytes !== null && existingBytes.equals(sourceBytes)) {
unchanged += 1;
continue;
}
await mkdir(path.dirname(targetPath), { recursive: true });
await writeFile(targetPath, sourceBytes);
updated += 1;
}
console.log(
`Synced shipped docs to ${targetDocsDir} (${updated} updated, ${unchanged} unchanged)`,
);
return { updated, unchanged, total: targets.length };
}
const isMainModule = process.argv[1]