Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Fail OK scanning gates when a declared root goes missing (#5325)

Nick Gomez2771be919f81
diff --git a/knip.config.ts b/knip.config.ts
index 37d9ce912..be41ab16c 100644
--- a/knip.config.ts
+++ b/knip.config.ts
@@ -145,15 +145,11 @@ export default {
'packages/server': {
entry: ['src/**/*.test.ts', 'src/parse-worker.ts'],
project: 'src/**',
- ignoreDependencies: [
- '@types/shell-quote',
- ],
+ ignoreDependencies: ['@types/shell-quote'],
},
'packages/cli': {
entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],
- ignoreDependencies: [
- 'yjs',
- ],
+ ignoreDependencies: ['yjs'],
},
'packages/desktop': {
entry: [
diff --git a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
index a28cc2b18..8a60e5826 100644
--- a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
+++ b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
@@ -1,4 +1,3 @@
-
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
index 4fc2c11f9..6fcdc23f2 100644
--- a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
+++ b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { normalizeBridge } from '@inkeep/open-knowledge-core';
import { updateYFragment } from '@tiptap/y-tiptap';
diff --git a/packages/app/tests/integration/interior-edit-freshness.test.ts b/packages/app/tests/integration/interior-edit-freshness.test.ts
index 9f2f6feb6..40b5cdfd6 100644
--- a/packages/app/tests/integration/interior-edit-freshness.test.ts
+++ b/packages/app/tests/integration/interior-edit-freshness.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
index 2a085742b..93666af70 100644
--- a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
+++ b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/origin-undoability-sweep.uncached.test.ts b/packages/app/tests/integration/origin-undoability-sweep.uncached.test.ts
index 2bc65ec82..236a2f60f 100644
--- a/packages/app/tests/integration/origin-undoability-sweep.uncached.test.ts
+++ b/packages/app/tests/integration/origin-undoability-sweep.uncached.test.ts
@@ -1,5 +1,5 @@
-import { globSync, readdirSync, readFileSync, statSync } from 'node:fs';
-import { basename, join } from 'node:path';
+import { existsSync, globSync, readdirSync, readFileSync, statSync } from 'node:fs';
+import { basename, join, relative } from 'node:path';
import { describe, expect, test } from 'vitest';
import { isTestOnlySourceFile } from '../../../../test-support/test-only-source-file.mjs';
@@ -200,6 +200,12 @@ function contractFileHits(file: string): string[] {
);
}
+function missingSearchRoots(roots: readonly string[]): string[] {
+ return roots
+ .filter((root) => !existsSync(root))
+ .map((root) => relative(join(HERE, '../../..'), root));
+}
+
function enumerateOriginConstants(): string[] {
const files: string[] = [];
for (const root of SRC_ROOTS) walkTsFiles(root, files);
@@ -276,6 +282,17 @@ describe('origin-undoability sweep', () => {
expect(contractFileHits(basename(import.meta.filename))).toHaveLength(1);
});
+ test('every contract search root exists, and a missing one is named (planted positive)', () => {
+ expect(
+ missingSearchRoots(CONTRACT_SEARCH_ROOTS),
+ 'contract search roots that do not exist: the resolver would stop searching them, so point ' +
+ 'CONTRACT_SEARCH_ROOTS at where those sources moved',
+ ).toEqual([]);
+ expect(
+ missingSearchRoots([...CONTRACT_SEARCH_ROOTS, join(HERE, '__no_such_search_root__')]),
+ ).toEqual([join('app', 'tests', 'integration', '__no_such_search_root__')]);
+ });
+
test('the reserved machine-merge undo row is documented for the conflict-spec extension point', () => {
expect(RESERVED_UNDO_ROWS['machine-merge']?.undo).toBe('system-not-undoable');
});
diff --git a/packages/app/tests/integration/producer-guard-wiring.test.ts b/packages/app/tests/integration/producer-guard-wiring.test.ts
index 878d5196b..867598d34 100644
--- a/packages/app/tests/integration/producer-guard-wiring.test.ts
+++ b/packages/app/tests/integration/producer-guard-wiring.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
index 4ada3cf26..0970ac12f 100644
--- a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
+++ b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
@@ -1,4 +1,3 @@
-
import { randomUUID } from 'node:crypto';
import { afterEach, describe, expect, test } from 'vitest';
import { agentWriteMd, createTestServer, type TestServer } from './test-harness';
diff --git a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
index cbe2b705f..b632185e1 100644
--- a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
+++ b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
@@ -1,4 +1,3 @@
-
import { execFileSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { join } from 'node:path';
diff --git a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
index 89bc9b4d9..c530ce4ba 100644
--- a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
+++ b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
@@ -1,4 +1,3 @@
-
import { randomUUID } from 'node:crypto';
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';
import type * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-after-rollback.test.ts b/packages/app/tests/integration/undo-after-rollback.test.ts
index a78aeb0e4..d3767adc9 100644
--- a/packages/app/tests/integration/undo-after-rollback.test.ts
+++ b/packages/app/tests/integration/undo-after-rollback.test.ts
@@ -1,4 +1,3 @@
-
import { ROLLBACK_ORIGIN } from '@inkeep/open-knowledge-server';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-recycle-reset.test.ts b/packages/app/tests/integration/undo-recycle-reset.test.ts
index e7cc69c3f..b076f955c 100644
--- a/packages/app/tests/integration/undo-recycle-reset.test.ts
+++ b/packages/app/tests/integration/undo-recycle-reset.test.ts
@@ -1,4 +1,3 @@
-
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/undo-rederive-survival.test.ts b/packages/app/tests/integration/undo-rederive-survival.test.ts
index 9ddcd0730..7935e35f8 100644
--- a/packages/app/tests/integration/undo-rederive-survival.test.ts
+++ b/packages/app/tests/integration/undo-rederive-survival.test.ts
@@ -1,4 +1,3 @@
-
import { prependFrontmatter } from '@inkeep/open-knowledge-core';
import { Plugin, PluginKey } from '@tiptap/pm/state';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
diff --git a/packages/app/tests/meta/substrate-vocabulary-drift.uncached.test.ts b/packages/app/tests/meta/substrate-vocabulary-drift.uncached.test.ts
index 262906395..1497210be 100644
--- a/packages/app/tests/meta/substrate-vocabulary-drift.uncached.test.ts
+++ b/packages/app/tests/meta/substrate-vocabulary-drift.uncached.test.ts
@@ -1,19 +1,21 @@
-import { readdirSync, readFileSync, type Stats, statSync } from 'node:fs';
-import { join, resolve } from 'node:path';
+import { type SpawnSyncReturns, spawnSync } from 'node:child_process';
+import {
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ statSync,
+ symlinkSync,
+ writeFileSync,
+} from 'node:fs';
+import { tmpdir } from 'node:os';
+import { basename, dirname, join, relative, resolve } from 'node:path';
import { describe, expect, test } from 'vitest';
+import { gitCleanEnv } from '../../../../scripts/git-clean-env.mjs';
import { isTestOnlySourceFile } from '../../../../test-support/test-only-source-file.mjs';
import { getRegisteredDescriptors } from '../../src/editor/registry/index.ts';
-const APP_ROOT = resolve(import.meta.dirname, '../..');
-const PACKAGES_ROOT = resolve(APP_ROOT, '..');
-const ROOTS = [
- join(APP_ROOT, 'src'),
- join(APP_ROOT, 'tests'),
- ...['core', 'server', 'cli', 'desktop', 'plugin'].flatMap((pkg) => [
- join(PACKAGES_ROOT, pkg, 'src'),
- join(PACKAGES_ROOT, pkg, 'tests'),
- ]),
-];
+const OK_ROOT = resolve(import.meta.dirname, '../../../..');
const SELF_FILE = resolve(import.meta.dirname, 'substrate-vocabulary-drift.uncached.test.ts');
const TEST_FIXTURE_KNOWN_NONREGISTERED: Record<string, string> = {
@@ -21,64 +23,112 @@ const TEST_FIXTURE_KNOWN_NONREGISTERED: Record<string, string> = {
'Mermaid.tsx — renders MermaidFence compat with substrate-type="mermaid" for DOM targeting',
};
-function* walkTestFiles(dir: string): Generator<string> {
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
- const full = join(dir, entry.name);
- if (entry.isDirectory()) {
- if (entry.name === 'node_modules' || entry.name === 'dist') continue;
- yield* walkTestFiles(full);
- } else if (
- entry.isFile() &&
- (isTestOnlySourceFile(entry.name, 'vitest') || isTestOnlySourceFile(entry.name, 'playwright'))
- ) {
- yield full;
- }
- }
+const COMPONENT_TYPE_LITERAL = /data-component-type="([^"'$]+)"|data-component-type='([^"'$]+)'/g;
+
+interface Reference {
+ readonly file: string;
+ readonly line: number;
+ readonly name: string;
}
-const COMPONENT_TYPE_LITERAL = /data-component-type="([^"'$]+)"|data-component-type='([^"'$]+)'/g;
+function listPackageTestFiles(root: string): string[] {
+ return testFilesFromListing(
+ root,
+ spawnSync(
+ 'git',
+ ['ls-files', '-z', '--cached', '--others', '--exclude-standard', '--', 'packages'],
+ {
+ cwd: root,
+ encoding: 'utf8',
+ env: gitCleanEnv(),
+ maxBuffer: 64 * 1024 * 1024,
+ windowsHide: true,
+ },
+ ),
+ );
+}
-describe('substrate vocabulary drift — every test reference must resolve', () => {
- test('every `data-component-type="<name>"` literal is a registered descriptor or a known exception', () => {
- const registered = new Set<string>();
- for (const d of getRegisteredDescriptors()) {
- registered.add(d.name.toLowerCase());
- }
- registered.add('*');
+function testFilesFromListing(root: string, listed: SpawnSyncReturns<string>): string[] {
+ if (listed.error || listed.status !== 0 || listed.stderr.trim() !== '') {
+ const cause =
+ listed.error?.message ??
+ `status ${listed.status}, signal ${listed.signal}, stderr ${JSON.stringify(listed.stderr.trim())}`;
+ throw new Error(
+ `git ls-files could not list every file under ${join(root, 'packages')} (${cause}), so the substrate vocabulary gate cannot see the test files it checks.`,
+ );
+ }
+ return listed.stdout
+ .split('\0')
+ .filter((path) => {
+ const name = basename(path);
+ return (
+ (isTestOnlySourceFile(name, 'vitest') || isTestOnlySourceFile(name, 'playwright')) &&
+ statSync(join(root, path), { throwIfNoEntry: false }) !== undefined
+ );
+ })
+ .map((path) => join(root, path));
+}
- const violations: Array<{ file: string; line: number; name: string }> = [];
+function registeredNames(): Set<string> {
+ const registered = new Set<string>();
+ for (const d of getRegisteredDescriptors()) {
+ registered.add(d.name.toLowerCase());
+ }
+ registered.add('*');
+ return registered;
+}
- for (const root of ROOTS) {
- let rootStat: Stats;
- try {
- rootStat = statSync(root);
- } catch {
- continue;
- }
- if (!rootStat.isDirectory()) continue;
-
- for (const file of walkTestFiles(root)) {
- if (file === SELF_FILE) continue;
- const content = readFileSync(file, 'utf-8');
- const lines = content.split('\n');
- for (let i = 0; i < lines.length; i++) {
- const line = lines[i];
- if (!line) continue;
- COMPONENT_TYPE_LITERAL.lastIndex = 0;
- let match: RegExpExecArray | null = COMPONENT_TYPE_LITERAL.exec(line);
- while (match !== null) {
- const name = match[1] ?? match[2];
- if (name && name !== '...') {
- const lower = name.toLowerCase();
- if (!registered.has(lower) && !(lower in TEST_FIXTURE_KNOWN_NONREGISTERED)) {
- violations.push({ file, line: i + 1, name: lower });
- }
- }
- match = COMPONENT_TYPE_LITERAL.exec(line);
- }
+function findReferences(files: readonly string[]): Reference[] {
+ const references: Reference[] = [];
+ for (const file of files) {
+ if (file === SELF_FILE) continue;
+ const lines = readFileSync(file, 'utf-8').split('\n');
+ for (let i = 0; i < lines.length; i++) {
+ const line = lines[i];
+ if (!line) continue;
+ COMPONENT_TYPE_LITERAL.lastIndex = 0;
+ let match: RegExpExecArray | null = COMPONENT_TYPE_LITERAL.exec(line);
+ while (match !== null) {
+ const name = match[1] ?? match[2];
+ if (name && name !== '...') {
+ references.push({ file, line: i + 1, name: name.toLowerCase() });
}
+ match = COMPONENT_TYPE_LITERAL.exec(line);
}
}
+ }
+ return references;
+}
+
+function unresolvedReferences(
+ references: readonly Reference[],
+ registered: ReadonlySet<string>,
+): Reference[] {
+ return references.filter(
+ ({ name }) => !registered.has(name) && !(name in TEST_FIXTURE_KNOWN_NONREGISTERED),
+ );
+}
+
+function runGit(root: string, args: readonly string[]): void {
+ const result = spawnSync('git', args, {
+ cwd: root,
+ encoding: 'utf8',
+ env: gitCleanEnv(),
+ windowsHide: true,
+ });
+ expect(result.status, result.stderr).toBe(0);
+}
+
+describe('substrate vocabulary drift — every test reference must resolve', () => {
+ test('every `data-component-type="<name>"` literal is a registered descriptor or a known exception', () => {
+ const references = findReferences(listPackageTestFiles(OK_ROOT));
+ expect(
+ references.length,
+ 'git lists no test file under packages/ that holds a data-component-type literal, so this ' +
+ 'gate would pass having checked nothing',
+ ).toBeGreaterThan(0);
+
+ const violations = unresolvedReferences(references, registeredNames());
if (violations.length > 0) {
const lines = violations.map(
@@ -97,4 +147,114 @@ describe('substrate vocabulary drift — every test reference must resolve', ()
expect(violations).toHaveLength(0);
});
+
+ test('flags an unregistered literal in an untracked test file git lists under packages/, and not in an ignored directory, a non-test file or outside packages/ (planted positive)', () => {
+ const root = mkdtempSync(join(tmpdir(), 'ok-substrate-vocabulary-drift-'));
+ try {
+ runGit(root, ['init', '-q']);
+ const unregistered = `export const markup = '<div data-component-type="planted-unregistered-substrate"></div>';\n`;
+ const plant = (path: string, content: string) => {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ writeFileSync(join(root, path), content);
+ };
+ plant('.gitignore', 'ignored/\n');
+ plant(join('packages', 'new', 'tests', 'stray.test.ts'), unregistered);
+ plant(join('packages', 'new', 'src', 'clean.test.ts'), 'export const clean = true;\n');
+ plant(join('packages', 'new', 'src', 'source.ts'), unregistered);
+ plant(join('packages', 'new', 'ignored', 'hidden.test.ts'), unregistered);
+ plant(join('elsewhere', 'outside.test.ts'), unregistered);
+
+ const files = listPackageTestFiles(root);
+
+ expect(files.map((file) => relative(root, file)).sort()).toEqual([
+ join('packages', 'new', 'src', 'clean.test.ts'),
+ join('packages', 'new', 'tests', 'stray.test.ts'),
+ ]);
+ expect(
+ unresolvedReferences(findReferences(files), registeredNames()).map(({ file, name }) => [
+ relative(root, file),
+ name,
+ ]),
+ ).toEqual([
+ [join('packages', 'new', 'tests', 'stray.test.ts'), 'planted-unregistered-substrate'],
+ ]);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+
+ test('throws when git exits 0 but warns it could not read a .gitignore under packages/ (planted positive)', () => {
+ const root = mkdtempSync(join(tmpdir(), 'ok-substrate-vocabulary-drift-'));
+ try {
+ runGit(root, ['init', '-q']);
+ const dir = join(root, 'packages', 'a');
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, 'ignore-rules'), 'ignored/\n');
+ symlinkSync('ignore-rules', join(dir, '.gitignore'));
+
+ expect(() => listPackageTestFiles(root)).toThrow(
+ /status 0, signal null, stderr ".*unable to access/,
+ );
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+
+ test('throws when a test file git lists sits behind a symlink loop (planted positive)', () => {
+ const root = mkdtempSync(join(tmpdir(), 'ok-substrate-vocabulary-drift-'));
+ try {
+ runGit(root, ['init', '-q']);
+ const tests = join(root, 'packages', 'p', 'tests');
+ mkdirSync(tests, { recursive: true });
+ writeFileSync(join(tests, 'loop.test.ts'), '');
+ runGit(root, ['add', 'packages']);
+ rmSync(tests, { recursive: true });
+ symlinkSync('tests', tests);
+
+ expect(() => listPackageTestFiles(root)).toThrow(/ELOOP/);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+
+ test('throws when the root is not a git work tree (planted positive)', () => {
+ const root = mkdtempSync(join(tmpdir(), 'ok-substrate-vocabulary-drift-'));
+ try {
+ expect(() => listPackageTestFiles(root)).toThrow(
+ /status 128, signal null, stderr "fatal: not a git repository/,
+ );
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+
+ test('throws on a listing that was killed by a signal or exited non-zero without writing stderr, and accepts a clean one (planted positive)', () => {
+ const root = mkdtempSync(join(tmpdir(), 'ok-substrate-vocabulary-drift-'));
+ try {
+ const listed = join('packages', 'p', 'tests', 'listed.test.ts');
+ mkdirSync(dirname(join(root, listed)), { recursive: true });
+ writeFileSync(join(root, listed), '');
+ const result = (
+ status: number | null,
+ signal: SpawnSyncReturns<string>['signal'],
+ ): SpawnSyncReturns<string> => ({
+ pid: 0,
+ output: [],
+ stdout: `${listed}\0`,
+ stderr: '',
+ status,
+ signal,
+ });
+
+ expect(() => testFilesFromListing(root, result(null, 'SIGKILL'))).toThrow(
+ /status null, signal SIGKILL, stderr ""/,
+ );
+ expect(() => testFilesFromListing(root, result(1, null))).toThrow(
+ /status 1, signal null, stderr ""/,
+ );
+ expect(testFilesFromListing(root, result(0, null))).toEqual([join(root, listed)]);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
});
diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.ts
index 2a597ec3d..f4cc8a286 100644
--- a/packages/core/src/bridge/compose.ts
+++ b/packages/core/src/bridge/compose.ts
@@ -1,4 +1,3 @@
-
import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';
export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';
diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.ts
index 01ef76a9f..8fb428b90 100644
--- a/packages/core/src/bridge/doc-boundary-space.ts
+++ b/packages/core/src/bridge/doc-boundary-space.ts
@@ -1,4 +1,3 @@
-
import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';
import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';
diff --git a/packages/core/src/config/auto-sync-mode-confinement.uncached.test.ts b/packages/core/src/config/auto-sync-mode-confinement.uncached.test.ts
index b749fec41..bc8322333 100644
--- a/packages/core/src/config/auto-sync-mode-confinement.uncached.test.ts
+++ b/packages/core/src/config/auto-sync-mode-confinement.uncached.test.ts
@@ -1,4 +1,14 @@
-import { readdirSync, readFileSync, statSync } from 'node:fs';
+import {
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ readdirSync,
+ readFileSync,
+ rmSync,
+ statSync,
+ writeFileSync,
+} from 'node:fs';
+import { tmpdir } from 'node:os';
import { join, relative, resolve } from 'node:path';
import { describe, expect, test } from 'vitest';
import { isTestOnlySourceFile } from '../../../../test-support/test-only-source-file.mjs';
@@ -41,20 +51,53 @@ function* sourceFiles(dir: string): Generator<string> {
}
}
+const PACKAGES_WITHOUT_SOURCE = new Set(['plugin']);
+
+function isDirectory(path: string): boolean {
+ return statSync(path, { throwIfNoEntry: false })?.isDirectory() ?? false;
+}
+
+function packagesWithoutSource(packagesDir: string): string[] {
+ return readdirSync(packagesDir)
+ .filter((name) => existsSync(join(packagesDir, name, 'package.json')))
+ .filter(
+ (name) => !isDirectory(join(packagesDir, name, 'src')) && !PACKAGES_WITHOUT_SOURCE.has(name),
+ )
+ .sort();
+}
+
describe('autoSync.enabled read confinement', () => {
+ test('every package with a manifest has a src directory, or is a manifest-only package', () => {
+ expect(
+ packagesWithoutSource(join(findSubtreeRoot(), 'packages')),
+ 'packages with a package.json but no src directory: this check would skip their source, so ' +
+ 'give the package a src directory, or add it to PACKAGES_WITHOUT_SOURCE if it carries no source',
+ ).toEqual([]);
+ });
+
+ test('names a package with a manifest and no src, and not a manifest-only package or a leftover directory (planted positive)', () => {
+ const packagesDir = mkdtempSync(join(tmpdir(), 'ok-auto-sync-packages-'));
+ try {
+ mkdirSync(join(packagesDir, 'with-src', 'src'), { recursive: true });
+ writeFileSync(join(packagesDir, 'with-src', 'package.json'), '{}\n');
+ mkdirSync(join(packagesDir, 'without-src'));
+ writeFileSync(join(packagesDir, 'without-src', 'package.json'), '{}\n');
+ mkdirSync(join(packagesDir, 'plugin'));
+ writeFileSync(join(packagesDir, 'plugin', 'package.json'), '{}\n');
+ mkdirSync(join(packagesDir, 'leftover', 'node_modules'), { recursive: true });
+ expect(packagesWithoutSource(packagesDir)).toEqual(['without-src']);
+ } finally {
+ rmSync(packagesDir, { recursive: true, force: true });
+ }
+ });
+
test('legacy enabled is only read by the mode-derive path', () => {
const root = findSubtreeRoot();
const offenders: string[] = [];
const packagesDir = join(root, 'packages');
for (const pkg of readdirSync(packagesDir)) {
const src = join(packagesDir, pkg, 'src');
- let srcStat: ReturnType<typeof statSync>;
- try {
- srcStat = statSync(src);
- } catch {
- continue;
- }
- if (!srcStat.isDirectory()) continue;
+ if (!isDirectory(src)) continue;
for (const file of sourceFiles(src)) {
const rel = relative(root, file);
if (ALLOWED_READERS.has(rel)) continue;
diff --git a/packages/core/src/logging/renderer-capture-scrub-coverage.uncached.test.ts b/packages/core/src/logging/renderer-capture-scrub-coverage.uncached.test.ts
index 8e59735f1..9a7caba18 100644
--- a/packages/core/src/logging/renderer-capture-scrub-coverage.uncached.test.ts
+++ b/packages/core/src/logging/renderer-capture-scrub-coverage.uncached.test.ts
@@ -1,4 +1,14 @@
-import { readdirSync, readFileSync, statSync } from 'node:fs';
+import {
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ readdirSync,
+ readFileSync,
+ rmSync,
+ statSync,
+ writeFileSync,
+} from 'node:fs';
+import { tmpdir } from 'node:os';
import { join, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, test } from 'vitest';
@@ -38,11 +48,7 @@ function findCaptureSites(): string[] {
for (const pkg of readdirSync(PACKAGES_DIR, { withFileTypes: true })) {
if (!pkg.isDirectory() || SKIP_DIR_NAMES.has(pkg.name)) continue;
const srcDir = join(PACKAGES_DIR, pkg.name, 'src');
- try {
- if (!statSync(srcDir).isDirectory()) continue;
- } catch {
- continue;
- }
+ if (!isDirectory(srcDir)) continue;
for (const file of listSourceFiles(srcDir)) {
const rel = relative(PACKAGES_DIR, file);
if (rel.startsWith(DEFINITION_DIR) || rel.includes(join('/', DEFINITION_DIR))) continue;
@@ -54,7 +60,46 @@ function findCaptureSites(): string[] {
return sites.sort();
}
+const PACKAGES_WITHOUT_SOURCE = new Set(['plugin']);
+
+function isDirectory(path: string): boolean {
+ return statSync(path, { throwIfNoEntry: false })?.isDirectory() ?? false;
+}
+
+function packagesWithoutSource(packagesDir: string): string[] {
+ return readdirSync(packagesDir)
+ .filter((name) => existsSync(join(packagesDir, name, 'package.json')))
+ .filter(
+ (name) => !isDirectory(join(packagesDir, name, 'src')) && !PACKAGES_WITHOUT_SOURCE.has(name),
+ )
+ .sort();
+}
+
describe('renderer-console capture sites', () => {
+ test('every package with a manifest has a src directory, or is a manifest-only package', () => {
+ expect(
+ packagesWithoutSource(PACKAGES_DIR),
+ 'packages with a package.json but no src directory: this sweep would skip their source, so ' +
+ 'give the package a src directory, or add it to PACKAGES_WITHOUT_SOURCE if it carries no source',
+ ).toEqual([]);
+ });
+
+ test('names a package with a manifest and no src, and not a manifest-only package or a leftover directory (planted positive)', () => {
+ const packagesDir = mkdtempSync(join(tmpdir(), 'ok-renderer-capture-packages-'));
+ try {
+ mkdirSync(join(packagesDir, 'with-src', 'src'), { recursive: true });
+ writeFileSync(join(packagesDir, 'with-src', 'package.json'), '{}\n');
+ mkdirSync(join(packagesDir, 'without-src'));
+ writeFileSync(join(packagesDir, 'without-src', 'package.json'), '{}\n');
+ mkdirSync(join(packagesDir, 'plugin'));
+ writeFileSync(join(packagesDir, 'plugin', 'package.json'), '{}\n');
+ mkdirSync(join(packagesDir, 'leftover', 'node_modules'), { recursive: true });
+ expect(packagesWithoutSource(packagesDir)).toEqual(['without-src']);
+ } finally {
+ rmSync(packagesDir, { recursive: true, force: true });
+ }
+ });
+
test('the sweep finds the capture sites that exist today', () => {
expect(findCaptureSites()).toEqual(expect.arrayContaining(KNOWN_CAPTURE_SITES));
});
diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.ts
index fb0ba5a7c..fc267cbea 100644
--- a/packages/core/src/markdown/comment-promoter.ts
+++ b/packages/core/src/markdown/comment-promoter.ts
@@ -1,4 +1,3 @@
-
import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.ts
index fa3cfcd86..bfd7e3109 100644
--- a/packages/core/src/markdown/merged-walker.ts
+++ b/packages/core/src/markdown/merged-walker.ts
@@ -1,4 +1,3 @@
-
import type { Nodes, Parent, Root } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.ts
index 8be632ba4..4d3250d3a 100644
--- a/packages/core/src/markdown/safe-url.ts
+++ b/packages/core/src/markdown/safe-url.ts
@@ -1,4 +1,3 @@
-
export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;
const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');
diff --git a/packages/server/src/agent-write-spine-walk-root-coverage-contract.uncached.test.ts b/packages/server/src/agent-write-spine-walk-root-coverage-contract.uncached.test.ts
index 9ae9c9540..195ffa57a 100644
--- a/packages/server/src/agent-write-spine-walk-root-coverage-contract.uncached.test.ts
+++ b/packages/server/src/agent-write-spine-walk-root-coverage-contract.uncached.test.ts
@@ -1,4 +1,12 @@
-import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import {
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ readdirSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -42,14 +50,19 @@ function declaresServerDependency(manifestPath: string): boolean {
].some((deps) => deps !== undefined && Object.hasOwn(deps, SERVER_PACKAGE_NAME));
}
-function listServerConsumerSrcRoots(): string[] {
- return listWorkspaceManifests(monorepoRoot, 4)
+function listServerConsumerSrcRoots(workspaceRoot: string): string[] {
+ return listWorkspaceManifests(workspaceRoot, 4)
.filter(declaresServerDependency)
.map((manifestPath) => join(dirname(manifestPath), 'src'))
- .filter((srcRoot) => existsSync(srcRoot))
.sort();
}
+function consumersWithoutSource(srcRoots: readonly string[], workspaceRoot: string): string[] {
+ return srcRoots
+ .filter((srcRoot) => !existsSync(srcRoot))
+ .map((srcRoot) => relative(workspaceRoot, dirname(srcRoot)));
+}
+
function listCensusFiles(): string[] {
return readdirSync(here, { withFileTypes: true })
.filter(
@@ -81,11 +94,16 @@ function spineWalkRootArguments(censusFile: string): string[] {
describe('agent-write spine walk root coverage contract', () => {
it('keeps every applyAgentMarkdownWrite call site inside the walk root both censuses use', () => {
- const consumerSrcRoots = listServerConsumerSrcRoots();
+ const consumerSrcRoots = listServerConsumerSrcRoots(monorepoRoot);
expect(
consumerSrcRoots.length,
`no ${SERVER_PACKAGE_NAME} consumer src roots derived from the workspace manifests`,
).toBeGreaterThan(0);
+ expect(
+ consumersWithoutSource(consumerSrcRoots, monorepoRoot),
+ `${SERVER_PACKAGE_NAME} consumers without a src directory: their applyAgentMarkdownWrite call ` +
+ 'sites would go unchecked, so keep each consumer source under src',
+ ).toEqual([]);
const outsideCensus = consumerSrcRoots
.flatMap((srcRoot) =>
listAgentWriteSpineFiles(srcRoot).map((path) =>
@@ -118,4 +136,22 @@ describe('agent-write spine walk root coverage contract', () => {
rmSync(plantedRoot, { recursive: true, force: true });
}
});
+
+ it('names a server consumer with no src directory, and not one that has src or a package that is no consumer (planted positive)', () => {
+ const workspace = mkdtempSync(join(tmpdir(), 'ok-agent-write-consumers-'));
+ const consumerManifest = `${JSON.stringify({ dependencies: { [SERVER_PACKAGE_NAME]: 'workspace:*' } })}\n`;
+ try {
+ mkdirSync(join(workspace, 'with-src', 'src'), { recursive: true });
+ writeFileSync(join(workspace, 'with-src', 'package.json'), consumerManifest);
+ mkdirSync(join(workspace, 'without-src'));
+ writeFileSync(join(workspace, 'without-src', 'package.json'), consumerManifest);
+ mkdirSync(join(workspace, 'not-a-consumer'));
+ writeFileSync(join(workspace, 'not-a-consumer', 'package.json'), '{}\n');
+ expect(consumersWithoutSource(listServerConsumerSrcRoots(workspace), workspace)).toEqual([
+ 'without-src',
+ ]);
+ } finally {
+ rmSync(workspace, { recursive: true, force: true });
+ }
+ });
});
diff --git a/packages/server/src/normalize-write-sink-gate.uncached.test.ts b/packages/server/src/normalize-write-sink-gate.uncached.test.ts
index f729d790c..6aebf6915 100644
--- a/packages/server/src/normalize-write-sink-gate.uncached.test.ts
+++ b/packages/server/src/normalize-write-sink-gate.uncached.test.ts
@@ -105,13 +105,7 @@ function isScannableFile(name: string): boolean {
function collectCandidateFiles(root: string): string[] {
const out: string[] = [];
const walk = (dir: string): void => {
- let entries: string[];
- try {
- entries = readdirSync(dir);
- } catch {
- return;
- }
- for (const entry of entries) {
+ for (const entry of readdirSync(dir)) {
if (entry === 'node_modules' || entry === 'dist' || entry === '__mocks__') continue;
const abs = join(dir, entry);
if (statSync(abs).isDirectory()) walk(abs);
diff --git a/packages/server/src/producer-origin-gate.uncached.test.ts b/packages/server/src/producer-origin-gate.uncached.test.ts
index eb4235e1e..f21ef01d7 100644
--- a/packages/server/src/producer-origin-gate.uncached.test.ts
+++ b/packages/server/src/producer-origin-gate.uncached.test.ts
@@ -188,13 +188,7 @@ function isScannableFile(name: string): boolean {
function collectCandidateFiles(root: string): string[] {
const out: string[] = [];
const walk = (dir: string): void => {
- let entries: string[];
- try {
- entries = readdirSync(dir);
- } catch {
- return;
- }
- for (const entry of entries) {
+ for (const entry of readdirSync(dir)) {
if (entry === 'node_modules' || entry === 'dist' || entry === '__mocks__') continue;
const abs = join(dir, entry);
const st = statSync(abs);
diff --git a/test-support/bun-global-shim.test.ts b/test-support/bun-global-shim.test.ts
index eb922f6a0..93a1432e2 100644
--- a/test-support/bun-global-shim.test.ts
+++ b/test-support/bun-global-shim.test.ts
@@ -1,14 +1,27 @@
-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import fs, { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { stripTypeScriptTypes } from 'node:module';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
-import { describe, expect, test } from 'vitest';
+import { describe, expect, test, vi } from 'vitest';
import { bunFacade, installBunGlobal } from './bun-global-shim';
const selfPath = fileURLToPath(import.meta.url);
const selfDir = fileURLToPath(new URL('.', import.meta.url));
+function thrownCode(run: () => unknown): string | undefined {
+ try {
+ run();
+ } catch (error) {
+ return (error as NodeJS.ErrnoException).code;
+ }
+ return undefined;
+}
+
+function missingDirectory(): string {
+ return join(tmpdir(), `bun-glob-missing-${process.pid}-${Date.now()}`);
+}
+
describe('Bun global facade', () => {
test('installs globalThis.Bun (idempotently)', () => {
installBunGlobal();
@@ -71,6 +84,64 @@ describe('Bun global facade', () => {
}
});
+ test('Bun.Glob.scanSync throws ENOENT at the call for a missing cwd, as Bun does', () => {
+ expect(
+ thrownCode(() => new bunFacade.Glob('**/*.ts').scanSync({ cwd: missingDirectory() })),
+ ).toBe('ENOENT');
+ });
+
+ test('Bun.Glob.scanSync throws ENOTDIR when cwd is a file, as Bun does', () => {
+ expect(thrownCode(() => new bunFacade.Glob('**/*.ts').scanSync({ cwd: selfPath }))).toBe(
+ 'ENOTDIR',
+ );
+ });
+
+ test('Bun.Glob.scanSync throws EACCES for an unreadable directory it walks, as Bun does', () => {
+ const root = mkdtempSync(join(tmpdir(), 'bun-glob-unreadable-'));
+ const locked = join(root, 'locked');
+ mkdirSync(locked);
+ writeFileSync(join(locked, 'inside.ts'), '');
+ const realReaddirSync = fs.readdirSync;
+ const readdirSpy = vi
+ .spyOn(fs, 'readdirSync')
+ .mockImplementation((target: fs.PathLike, options?: unknown) => {
+ if (String(target) === locked) {
+ throw Object.assign(new Error(`EACCES: permission denied, scandir '${locked}'`), {
+ code: 'EACCES',
+ });
+ }
+ return Reflect.apply(realReaddirSync, fs, [target, options]);
+ });
+ try {
+ expect(thrownCode(() => new bunFacade.Glob('**/*.ts').scanSync({ cwd: root }))).toBe(
+ 'EACCES',
+ );
+ } finally {
+ readdirSpy.mockRestore();
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+
+ test('Bun.Glob.scanSync yields nothing for an empty cwd or a missing directory named in the pattern, as Bun does', () => {
+ const root = mkdtempSync(join(tmpdir(), 'bun-glob-empty-'));
+ try {
+ expect([...new bunFacade.Glob('**/*.ts').scanSync({ cwd: root })]).toEqual([]);
+ mkdirSync(join(root, 'src'));
+ writeFileSync(join(root, 'src', 'present.ts'), '');
+ expect([...new bunFacade.Glob('lib/**/*.ts').scanSync({ cwd: root })]).toEqual([]);
+ expect([...new bunFacade.Glob('src/**/*.ts').scanSync({ cwd: root })]).toEqual([
+ 'src/present.ts',
+ ]);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+
+ test('Bun.Glob.scan rejects on the first step for a missing cwd, as Bun does', async () => {
+ const steps = new bunFacade.Glob('**/*.ts').scan({ cwd: missingDirectory() });
+ await expect(steps.next()).rejects.toMatchObject({ code: 'ENOENT' });
+ });
+
test('Bun.TOML parses on Node', () => {
expect(bunFacade.TOML.parse('a = 1\n[b]\nc = "x"')).toEqual({ a: 1, b: { c: 'x' } });
});
diff --git a/test-support/bun-global-shim.ts b/test-support/bun-global-shim.ts
index e074ab294..09bda2e2d 100644
--- a/test-support/bun-global-shim.ts
+++ b/test-support/bun-global-shim.ts
@@ -82,20 +82,14 @@ class BunGlobFacade {
return this.#regex.test(candidate);
}
- *scanSync(options: GlobScanOptions | string = {}): Generator<string> {
+ scanSync(options: GlobScanOptions | string = {}): Generator<string> {
const opts = typeof options === 'string' ? { cwd: options } : options;
const cwd = opts.cwd ?? process.cwd();
const absolute = opts.absolute ?? false;
const onlyFiles = opts.onlyFiles ?? true;
const walk = (dir: string, prefix: string): string[] => {
const results: string[] = [];
- let entries: fs.Dirent[];
- try {
- entries = fs.readdirSync(dir, { withFileTypes: true });
- } catch {
- return results;
- }
- for (const entry of entries) {
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name.startsWith('.')) continue;
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
if (entry.isDirectory()) {
@@ -107,9 +101,10 @@ class BunGlobFacade {
}
return results;
};
- for (const rel of walk(cwd, '')) {
- yield absolute ? path.join(cwd, rel) : rel;
- }
+ const found = walk(cwd, '');
+ return (function* () {
+ for (const rel of found) yield absolute ? path.join(cwd, rel) : rel;
+ })();
}
async *scan(options: GlobScanOptions | string = {}): AsyncGenerator<string> {
@@ -196,8 +191,7 @@ function bunWhich(command: string, options?: { PATH?: string; cwd?: string }): s
try {
fs.accessSync(candidate, fs.constants.X_OK);
return candidate;
- } catch {
- }
+ } catch {}
}
return null;
}