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.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
fix(gate): select documentation readers on submit [V4,C7]
Corey Olsonadca3819a092
diff --git a/src/affected-tests.js b/src/affected-tests.jsindex 937bbdef..08483845 100644--- a/src/affected-tests.js+++ b/src/affected-tests.js@@ -6,9 +6,11 @@ import { importsIn } from './module-imports.js';const MODULE = /\.[cm]?js$/u;const TEST = /\.(?:test|spec)\.[cm]?js$/u;+const DOC = /^docs\/.+\.md$/u;// A scanner declares this header rather than executing code while the graph is read.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 */';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']);/** Read exact objects from this checkout, ignoring ambient Git selectors and replacement refs. */@@ -58,6 +60,56 @@ function modulesAt(root, commit) {return new Map(entries.map(entry => [entry.path, sources.get(entry.hash)]));}+/**+ * Find literal documentation inputs without executing repository code. File references are resolved+ * both from the repository and from the reader; directory references deliberately overselect their+ * subtree. Split path arguments and template prefixes count as directory reads. Comments may add+ * readers, which is safe: this is conservative input discovery, not JavaScript data-flow analysis.+ * Computed Markdown names select the docs tree; opaque helpers declare DOCS_TREE_INPUT. The+ * landing gate remains complete and never uses this submit-only optimization.+ */+function documentationInputs(path, source, imports) {+ const inputs = new Set();+ if (source.split(/\r?\n/u).includes(DOCS_TREE_INPUT)) inputs.add('docs');+ const markdownReader = imports.some(target => target === 'node:fs' || target === 'node:fs/promises');+ const literals = /(['"])(?:\\[\s\S]|(?!\1)[^\\\r\n])*\1|`(?:\\[\s\S]|[^`\\])*`/gu;+ for (const match of source.matchAll(literals)) {+ let value;+ if (match[0][0] === '`') {+ const text = match[0].slice(1,-1);+ const rootDoc = /(?:^|\/)docs(?:\/|$)/u.exec(text);+ if (!rootDoc && text.includes('${') && text.endsWith('.md') && markdownReader) inputs.add('docs');+ value = (rootDoc ? text.slice(rootDoc.index + (rootDoc[0].startsWith('/') ? 1 : 0)) : text).split('${',1)[0];+ }+ else {+ // Only a complete quoted string matched above is evaluated, never a template or expression.+ try { value = match[0].includes('\\') ? new Function(`return ${match[0]};`)() : match[0].slice(1,-1); }+ catch { continue; }+ }+ if (typeof value !== 'string') continue;+ value = value.split(/[?#]/u)[0].replace(/\/$/u,'');+ const embeddedRoot = /\/docs(?:\/|$)/u.exec(value);+ if (embeddedRoot) value = value.slice(embeddedRoot.index + 1);+ const wildcard = value.search(/[\*\[\{]/u);+ if (wildcard !== -1) value = value.slice(0,value.lastIndexOf('/',wildcard) + 1);+ if (value === '.md' && markdownReader) { inputs.add('docs'); continue; }+ if (!value) continue;+ const directoryReader = /\b(?:readdir|glob|opendir)/u.test(source);+ if (!value.includes('/') && !value.endsWith('.md') && value !== 'docs' && !directoryReader) continue;+ const prefix = value === 'docs' || value.endsWith('.md') ? source.slice(0,match.index).trimEnd() : '';+ if (markdownReader && value.endsWith('.md') && !value.includes('docs/')+ && (/\+$/u.test(prefix) || /^\s*\+/u.test(source.slice(match.index + match[0].length)))) inputs.add('docs');+ // Bare "docs" is also a commit type or lane. In a call/path assignment, or a directory+ // scanner, it can name an input; a declaration of commit types alone is not a reader.+ const callArgument = /[,(]$/u.test(prefix) && prefix.lastIndexOf('(') > prefix.lastIndexOf('[');+ if (value === 'docs' && !callArgument && !/(?<![=!<>])=$/u.test(prefix) && !directoryReader) continue;+ for (const candidate of [posix.normalize(value), posix.normalize(posix.join(posix.dirname(path),value))]) {+ if (candidate === 'docs' || candidate.startsWith('docs/')) inputs.add(candidate.replace(/\/$/u,''));+ }+ }+ return inputs;+}+/** Compile import edges for one tree; computed imports stay attached to their importing file. */function graphAt(root, commit) {const modules = modulesAt(root, commit);@@ -68,11 +120,14 @@ function graphAt(root, commit) {const always = new Set();const testTreeReaders = new Set();const sourceTreeReaders = new Set();+ const docInputs = new Map();paths.forEach((path, index) => {+ docInputs.set(path, documentationInputs(path, modules.get(path), imports[index]));if (TEST.test(path)) {for (const line of modules.get(path).split(/\r?\n/u)) {if (line === TEST_TREE_INPUT) testTreeReaders.add(path);else if (line === SOURCE_TREE_INPUT) sourceTreeReaders.add(path);+ else if (line === DOCS_TREE_INPUT) continue;else break;}}@@ -90,13 +145,13 @@ function graphAt(root, commit) {reverse.get(dependency).add(path);}});- return { paths, reverse, unknown, always, testTreeReaders, sourceTreeReaders };+ return { paths, reverse, unknown, always, testTreeReaders, sourceTreeReaders, docInputs };}/*** Select changed tests and every transitive importer in the old and new trees. Old edges cover* deletions and renames; declared tree readers also cover changed test or source inputs. Other filesystem- * reads and process launches are deliberately not import edges.+ * reads and process launches are deliberately not import edges, except literal documentation inputs.** @param {string} root* @param {{base:string|null, trunk?:string, commit?:string, changed?:string[]}} options@@ -120,11 +175,20 @@ export function selectAffectedTests(root, { base, trunk, commit = 'HEAD', changeconst fallback = changed.find(path => GLOBAL.has(path) || path.startsWith('.githooks/'));if (fallback) return { full: true, reason: `${fallback} controls the gate or dependency selection`, files };if (!files.length) return { full: true, reason: 'no committed Node test files can be selected', files };- const unsupported = changed.find(path => !MODULE.test(path));+ const unsupported = changed.find(path => !MODULE.test(path) && !DOC.test(path));if (unsupported) return { full: true, reason: `import selection cannot compute the effect of ${unsupported}`, files };const before = graphAt(root, base);const reached = new Set(changed);const queue = [...changed];+ for (const doc of changed.filter(path => DOC.test(path))) {+ for (const graph of [before, after]) {+ for (const [reader, inputs] of graph.docInputs) {+ if ([...inputs].some(input => input === doc || doc.startsWith(input + '/'))) {+ if (!reached.has(reader)) { reached.add(reader); queue.push(reader); }+ }+ }+ }+ }for (let at = 0; at < queue.length; at += 1) {const path = queue[at];for (const graph of [before, after]) {diff --git a/test/affected-tests.test.js b/test/affected-tests.test.jsnew file mode 100644index 00000000..2acc6d86--- /dev/null+++ b/test/affected-tests.test.js@@ -0,0 +1,81 @@+/** Documentation input selection remains conservative across committed trees [V4,C7]. */+import assert from 'node:assert/strict';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { dirname, join } from 'node:path';+import { test } from 'node:test';+import { runFixtureGit } from './fixture-child.js';+import { selectAffectedTests } from '../src/affected-tests.js';++/** Make a real committed ES-module graph without executing any candidate source. */+function graph(t) {+ const root = mkdtempSync(join(tmpdir(), 'pullboard-doc-selection-'));+ t.after(() => rmSync(root, { recursive:true, force:true }));+ const env = { ...process.env, GIT_CONFIG_GLOBAL:'/dev/null', GIT_CONFIG_NOSYSTEM:'1',+ GIT_AUTHOR_NAME:'Test Agent', GIT_AUTHOR_EMAIL:'[email protected]',+ GIT_COMMITTER_NAME:'Test Agent', GIT_COMMITTER_EMAIL:'[email protected]' };+ /** Run Git with the fixture's own identity and configuration. */+ const git = (...args) => runFixtureGit(args, { cwd:root, env });+ /** Write a fixture module or document, creating its parent directories. */+ const put = (path, body) => { mkdirSync(dirname(join(root,path)), { recursive:true }); writeFileSync(join(root,path),body); };+ /** Snapshot real Git objects so selection cannot read uncommitted working-tree bytes. */+ const commit = () => { git('add','-A'); git('commit','-qm','chore: selection fixture'); return git('rev-parse','HEAD').trim(); };+ git('init','-q','-b','main');+ put('package.json','{"type":"module"}\n');+ put('src/api.js','export const version = 1;\n');+ put('test/api.test.js',"import '../src/api.js';\n");+ put('src/labels.js',"import {readFileSync} from 'node:fs'; export const types=['feat','fix','docs','test']; export const config=()=>readFileSync('config.json');\n");+ put('test/unrelated.test.js',"import '../src/labels.js'; import assert from 'node:assert/strict'; assert.ok(true);\n");+ put('docs/api.md','# API\n');+ put('docs/other.md','# Other\n');+ put('docs/api.test.js',"import {readFileSync} from 'node:fs'; readFileSync(new URL('./api.md',import.meta.url),'utf8');\n");+ return { root,git,put,commit };+}++test('a docs change selects the tests that read it [V4,C7]', (t) => {+ const box = graph(t);+ box.put('test/docs.test.js',"import {readdirSync} from 'node:fs'; readdirSync('docs');\n");+ const base = box.commit();+ box.put('src/api.js','export const version = 2;\n');+ box.put('docs/api.md','# Updated API\n');+ const commit = box.commit();+ const combined = selectAffectedTests(box.root,{base,commit});+ assert.equal(combined.full,false,'documenting an API change must retain focused selection');+ assert.deepEqual(combined.files,['docs/api.test.js','test/api.test.js','test/docs.test.js']);+ const docs = selectAffectedTests(box.root,{base,commit,changed:['docs/api.md']});+ assert.equal(docs.full,false);+ assert.deepEqual(docs.files,['docs/api.test.js','test/docs.test.js']);+ for (const path of ['package.json','config.json','unknown.txt','docs/image.svg']) {+ assert.equal(selectAffectedTests(box.root,{base,commit,changed:[path]}).full,true,`${path} still needs the full suite`);+ }+});++test('doc readers include helper imports, directory paths and old edges [V4,C7]', (t) => {+ const box = graph(t);+ box.put('test/doc-helper.js',"import {readFileSync} from 'node:fs'; export const text=readFileSync(new URL('../docs/api.md',import.meta.url),'utf8');\n");+ box.put('test/helper-reader.test.js',"import './doc-helper.js';\n");+ box.put('test/split-reader.test.js',"import {readFileSync} from 'node:fs'; import {join} from 'node:path'; readFileSync(join('docs','api.md'),'utf8');\n");+ box.put('test/template-reader.test.js',"import {readdirSync} from 'node:fs'; const name='api'; readdirSync(`docs/${name}`);\n");+ box.put('test/escaped-reader.test.js',"import {readFileSync} from 'node:fs'; readFileSync('do\\x63s/api.md');\n");+ box.put('test/root-template.test.js',"import {readFileSync} from 'node:fs'; const root='.'; readFileSync(`${root}/docs/api.md`);\n");+ box.put('test/glob-reader.test.js',"import {globSync} from 'node:fs'; globSync('docs/**/*.md');\n");+ box.put('test/prefixed-reader.test.js',"import {readFileSync} from 'node:fs'; const root='.'; readFileSync(root + '/docs/api.md');\n");+ box.put('test/computed-reader.test.js',"import {readFileSync} from 'node:fs'; import {join} from 'node:path'; const name='api'; readFileSync(join('d'+'ocs',name+'.md'));\n");+ box.put('test/declared-reader.test.js',"/** @pullboard-test-input docs-tree */\nimport {readFileSync} from 'node:fs'; readFileSync(process.env.INPUT);\n");+ const base = box.commit();+ box.put('test/doc-helper.js','export const text="no longer read";\n');+ const commit = box.commit();+ const selection = selectAffectedTests(box.root,{base,commit,changed:['docs/api.md']});+ assert.equal(selection.full,false);+ assert.deepEqual(selection.files,['docs/api.test.js','test/computed-reader.test.js','test/declared-reader.test.js','test/escaped-reader.test.js','test/glob-reader.test.js','test/helper-reader.test.js','test/prefixed-reader.test.js','test/root-template.test.js','test/split-reader.test.js','test/template-reader.test.js']);+});++test('an unread doc adds no tests and doc readers come from Git objects [V4,C7]', (t) => {+ const box = graph(t);+ const base = box.commit();+ box.put('docs/unread.md','# No readers\n');+ const commit = box.commit();+ box.put('test/unrelated.test.js',"import {readFileSync} from 'node:fs'; readFileSync('docs/unread.md');\n");+ assert.deepEqual(selectAffectedTests(box.root,{base,commit}),{full:false,reason:'',files:[]});+ assert.deepEqual(selectAffectedTests(box.root,{base,commit,changed:['src/api.js','docs/unread.md']}).files,['test/api.test.js']);+});