Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(core): header-only loads, expanded globs, decoded URLs [C7]

Corey Olson426adfef6edd
diff --git a/src/affected-tests.js b/src/affected-tests.js
index 5c82da0d..bc221485 100644
--- a/src/affected-tests.js
+++ b/src/affected-tests.js
@@ -1,6 +1,8 @@
/** Select committed Node test files through import edges; uncertainty requires the full gate [V4,C7]. */
import { spawnSync } from 'node:child_process';
import { posix } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { pathToFileURL } from 'node:url';
import { cleanGitEnvironment } from './git.js';
import {
commonJsIn,
@@ -14,14 +16,55 @@ const DOC = /^docs\/.+\.md$/u;
const TEST_TREE_INPUT = '/** @pullboard-test-input test-tree */';
const SOURCE_TREE_INPUT = '/** @pullboard-test-input source-tree */';
const DOCS_TREE_INPUT = '/** @pullboard-test-input docs-tree */';
-// A computed-import test may declare the repository modules it can load; a comment anywhere in the file counts.
-const LOADS = /^\/\*\* @pullboard-test-loads (.+?) \*\/$/gmu;
+// A computed-import test may declare the repository modules it can load, in its leading comment header only.
+const LOADS = /^\/\*\* @pullboard-test-loads (.+?) \*\/$/u;
const GLOBAL = new Set(['bin/run-tests.js', 'package.json', 'package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock', 'pnpm-lock.yaml', 'src/gate.js', 'src/cli.js', 'src/hooks.js', 'src/trusted-policy.js', 'src/affected-tests.js', 'src/module-imports.js']);
-/** The repository modules a computed-import test declares it can load, normalized; anything outside the repository is ignored. */
-function declaredLoads(source) {
- return [...source.matchAll(LOADS)].flatMap(match => match[1].trim().split(/\s+/u)).map(path => posix.normalize(path))
- .filter(path => MODULE.test(path) && !path.startsWith('../') && !path.startsWith('/'));
+/**
+ * The file's leading comment header: its lines before the first line of code. A declaration counts only here, so
+ * declaration-looking text inside a string or template later in the file is never read as one (#803).
+ */
+function headerLines(source) {
+ const lines = [];
+ let inBlock = false;
+ for (const [index, line] of source.split(/\r?\n/u).entries()) {
+ const text = line.trim();
+ if (inBlock) { lines.push(line); if (text.includes('*/')) inBlock = false; continue; }
+ if (!text || (index === 0 && text.startsWith('#!')) || text.startsWith('//')) { lines.push(line); continue; }
+ if (!text.startsWith('/*')) break;
+ lines.push(line);
+ inBlock = !text.slice(2).includes('*/');
+ }
+ return lines;
+}
+
+/** Match a declared path glob: * and ? stay within one folder, ** crosses folders; null for other glob syntax. */
+function globPattern(glob) {
+ if (/[[\]{}]/u.test(glob)) return null;
+ const source = glob.split(/(\*\*\/|\*\*|\*|\?)/u).map(part => part === '**/' ? '(?:.*/)?' : part === '**' ? '.*'
+ : part === '*' ? '[^/]*' : part === '?' ? '[^/]' : part.replace(/[.+^$()|\\]/gu, '\\$&')).join('');
+ return new RegExp(`^${source}$`, 'u');
+}
+
+/**
+ * The repository modules a computed-import test declares in its header that it can load, normalized, with globs
+ * expanded over the committed modules; anything outside the repository is ignored. A glob form this cannot expand
+ * makes the whole declaration unusable (null), so the caller falls back to the whole suite (#803).
+ *
+ * @returns {string[] | null}
+ */
+function declaredLoads(source, modulePaths) {
+ const declared = headerLines(source).map(line => LOADS.exec(line)).filter(Boolean)
+ .flatMap(match => match[1].trim().split(/\s+/u)).map(path => posix.normalize(path))
+ .filter(path => !path.startsWith('../') && !path.startsWith('/'));
+ const loads = [];
+ for (const path of declared) {
+ if (!/[*?[\]{}]/u.test(path)) { if (MODULE.test(path)) loads.push(path); continue; }
+ const pattern = globPattern(path);
+ if (!pattern) return null;
+ loads.push(...modulePaths.filter(module => pattern.test(module)));
+ }
+ return loads;
}
/** The one computed form the selector resolves (#803): new URL('<literal relative path>', import.meta.url), with or without .href. */
@@ -47,8 +90,11 @@ function literalLoads(source, path, modulePaths) {
for (; end < source.length && depth; end += 1) depth += source[end] === '(' ? 1 : source[end] === ')' ? -1 : 0;
const literal = URL_LITERAL.exec(source.slice(start, end - 1));
if (!literal) return null;
- const value = literal[2].split(/[?#]/u)[0];
- const target = posix.normalize(posix.join(posix.dirname(path), value));
+ // Resolve as the runtime does: a file URL against this file, percent-escapes decoded; anything else is unresolved.
+ let target;
+ try { target = posix.relative('/repo', fileURLToPath(new URL(literal[2], pathToFileURL(posix.join('/repo', path))))); }
+ catch { return null; }
+ if (target.startsWith('../') || posix.isAbsolute(target)) return null;
if (!MODULE.test(target) || !modulePaths.includes(target)) return null;
targets.add(target);
}
@@ -226,8 +272,8 @@ function graphAt(root, commit, changedDocs = []) {
}
if (target.startsWith('node:')) continue;
if (target === '(computed)' && TEST.test(path)) {
- const declared = declaredLoads(modules.get(path));
- const loads = declared.length ? declared : literalLoads(modules.get(path), path, paths) ?? [];
+ const declared = declaredLoads(modules.get(path), paths);
+ const loads = declared === null ? [] : declared.length ? declared : literalLoads(modules.get(path), path, paths) ?? [];
if (!loads.length) undeclared.set(path, 'a computed import declares no @pullboard-test-loads and names no module it can read');
for (const load of loads) {
if (!reverse.has(load)) reverse.set(load, new Set());
diff --git a/test/affected-tests.test.js b/test/affected-tests.test.js
index a81b8d0c..b5269d1a 100644
--- a/test/affected-tests.test.js
+++ b/test/affected-tests.test.js
@@ -280,3 +280,34 @@ test('a computed import with an environment fallback reads every candidate, not
assert.equal(selection.full, true, 'an unresolved branch falls back to the whole suite instead of a partial read set');
assert.match(selection.reason, /test\/loader\.test\.js/u, 'the fallback names the loader it cannot read');
});
+
+test('a declaration counts only in the header, globs expand, and literal URLs resolve as the runtime does [V4,C7]', (t) => {
+ const box = graph(t);
+ box.put('src/actual.js', 'export const value = 1;\n');
+ box.put('src/%61ctual.js', 'export const value = "decoy";\n');
+ // Declaration-looking text inside a template is not a declaration; the undeclared import falls back (#803 review).
+ box.put('test/template.test.js', "const banner = `\n/** @pullboard-test-loads src/safe.js */\n`;\nawait import(process.env.TARGET ?? '../src/actual.js');\n");
+ const base = box.commit();
+ box.put('src/actual.js', 'export const value = 2;\n');
+ const commit = box.commit();
+ const hidden = selectAffectedTests(box.root, { base, commit, changed: ['src/actual.js'] });
+ assert.equal(hidden.full, true, 'a declaration inside a template string never stops the fallback');
+ assert.match(hidden.reason, /test\/template\.test\.js/u, 'the fallback names the undeclared file');
+
+ // A declared glob reads every committed module it matches.
+ box.put('test/template.test.js', "/** @pullboard-test-loads src/*.js */\nawait import(process.env.TARGET ?? '../src/actual.js');\n");
+ // A literal URL with a percent-escape reads the decoded file, never the encoded decoy.
+ box.put('test/encoded.test.js', "await import(new URL('../src/%61ctual.js', import.meta.url).href);\n");
+ const declared = box.commit();
+ box.put('src/actual.js', 'export const value = 3;\n');
+ const after = box.commit();
+ const selected = selectAffectedTests(box.root, { base: declared, commit: after, changed: ['src/actual.js'] });
+ assert.equal(selected.full, false, selected.reason);
+ assert.ok(selected.files.includes('test/template.test.js'), 'the declared glob src/*.js selects its reader of src/actual.js');
+ assert.ok(selected.files.includes('test/encoded.test.js'), 'the percent-escaped literal reads src/actual.js, as Node loads it');
+
+ // Glob syntax the selector cannot expand is not a usable declaration.
+ box.put('test/template.test.js', "/** @pullboard-test-loads src/{a,b}.js */\nawait import(process.env.TARGET ?? '../src/actual.js');\n");
+ const braces = box.commit();
+ assert.equal(selectAffectedTests(box.root, { base: after, commit: braces, changed: ['src/actual.js'] }).full, true, 'an unexpandable glob falls back');
+});