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(view): a code reference never opens another file [B23]
Corey Olsond89f2092fc13
diff --git a/src/cockpit.js b/src/cockpit.jsindex dd3deca2..7aed8e28 100644--- a/src/cockpit.js+++ b/src/cockpit.js@@ -699,9 +699,11 @@ function render() {// Every #id in a shout that names an item opens it, whatever stands next to it. The ids are found in// the raw text and each piece is escaped on its own, so an apostrophe's ' is never read as one;- // a number that names no item stays text. A path:lines@commit reference opens that code (B23).+ // a number that names no item stays text. A path:lines@commit reference opens that code (B23): it+ // is the whole run of path characters before the colon, never a tail of it, so what it opens is+ // what it says, and the view refuses what is no path in the repo.const titles = new Map(p.items.map((i) => [String(i.id), i.title]));- const linked = (text) => String(text ?? '').split(/(#\\d+|[\\w.-]+(?:\\/[\\w.-]+)*:\\d+(?:-\\d+)?@[0-9a-f]{7,40}(?!\\w))/).map((part, n) => {+ const linked = (text) => String(text ?? '').split(/(#\\d+|(?<![\\w./-])[\\w./-]+:\\d+(?:-\\d+)?@[0-9a-f]{7,40}(?!\\w))/).map((part, n) => {if (n % 2 && part[0] !== '#') return codeRef(part);const id = /^#\\d+$/.test(part) ? String(Number(part.slice(1))) : '';return titles.has(id) ? '<button class="ref" data-go="item:' + id + '" title="' + esc(titles.get(id)) + '" type="button">' + esc(part) + '</button>' : esc(part);diff --git a/src/serve.js b/src/serve.jsindex 894554b7..d0b6c677 100644--- a/src/serve.js+++ b/src/serve.js@@ -140,7 +140,10 @@ const CODE_BYTES = 2_000_000;/*** The lines a shout's path:lines@commit reference names, as they were at that commit (B23). Git* alone reads them, from the commit's own tree: never the working tree, never a path outside the- * repo, never a revision that is not a plain SHA, and never more than CODE_LINES lines.+ * repo, never a revision that is not a plain SHA, never lines past the file's end, and never more+ * than CODE_LINES lines. A path cannot hold a space, so when the commit holds a path with one that+ * ends in this one, the words before the reference may be its start, and it is refused rather than+ * opened as a different file.** @param {string} root* @param {{ path: string, from: number, to: number, commit: string }} ref@@ -157,13 +160,16 @@ export function codeAt(root, { path, from, to, commit }) {const object = `${full}:${path}`;const type = spawnSync('git', ['cat-file', '-t', object], { cwd: root, encoding: 'utf8' });if (type.status !== 0 || type.stdout.trim() !== 'blob') throw new Refused('NO_FILE', `no file ${path} at ${commit}`);+ const names = spawnSync('git', ['ls-tree', '-r', '-z', '--name-only', full], { cwd: root, encoding: 'utf8', maxBuffer: 64_000_000 }).stdout.split('\0');+ const longer = names.find((name) => name.endsWith(` ${path}`));+ if (longer) throw new Refused('AMBIGUOUS', `${path} may be the end of "${longer}" at ${commit}, a path with a space, which a reference cannot name`);const size = Number(spawnSync('git', ['cat-file', '-s', object], { cwd: root, encoding: 'utf8' }).stdout);if (!(size <= CODE_BYTES)) throw new Refused('TOO_LARGE', `${path} is over ${CODE_BYTES / 1_000_000} MB, too large to show`);const lines = spawnSync('git', ['cat-file', 'blob', object], { cwd: root, encoding: 'utf8', maxBuffer: CODE_BYTES + 1 }).stdout.split('\n');if (lines.at(-1) === '') lines.pop();- if (from > lines.length) throw new Refused('NO_LINES', `${path} has ${lines.length} lines at ${commit}`);- const last = Math.min(to, lines.length, from + CODE_LINES - 1);- return { path, commit: full, from, to: last, lines: lines.slice(from - 1, last), more: last < Math.min(to, lines.length) };+ if (to > lines.length) throw new Refused('NO_LINES', `${path} has ${lines.length} lines at ${commit}`);+ const last = Math.min(to, from + CODE_LINES - 1);+ return { path, commit: full, from, to: last, lines: lines.slice(from - 1, last), more: last < to };}/**diff --git a/test/cockpit.test.js b/test/cockpit.test.jsindex 111f397c..f42c02cc 100644--- a/test/cockpit.test.js+++ b/test/cockpit.test.js@@ -1317,6 +1317,10 @@ test("a shout's code reference opens that code as it was at that commit [B23]",const first = box.git(alpha.web, 'rev-parse', 'HEAD');writeFileSync(join(alpha.web, 'web', 'greeting.html'), ' hello <b>there</b>\n second line\n');writeFileSync(join(alpha.web, 'web', 'long.txt'), Array.from({ length: 100 }, (_, n) => `line ${n + 1}`).join('\n') + '\n');+ // A path with a space ends in another file's path: text naming it must never open the other.+ writeFileSync(join(alpha.web, 'web', 'note.txt'), 'plain note\n');+ mkdirSync(join(alpha.web, 'web', 'my web'));+ writeFileSync(join(alpha.web, 'web', 'my web', 'note.txt'), 'spaced note\n');box.git(alpha.web, 'add', '-A');box.git(alpha.web, 'commit', '-q', '-m', 'feat(web): a warmer greeting [G1]');const second = box.git(alpha.web, 'rev-parse', 'HEAD');@@ -1324,6 +1328,8 @@ test("a shout's code reference opens that code as it was at that commit [B23]",const now = `web/greeting.html:1-2@${second.slice(0, 12)}`;const long = `web/long.txt:1-100@${second}`;box.run(alpha.web, 'shout', 'all', `#1 was ${was}, is ${now}; see ${long}.`);+ const spaced = `web/note.txt:1@${second.slice(0, 7)}`;+ box.run(alpha.web, 'shout', 'all', `absolute /${was} and spaced web/my ${spaced}`);// A file only the working tree has, which no commit holds.writeFileSync(join(alpha.repo, 'draft.txt'), 'not committed\n');const view = await startView(box);@@ -1349,6 +1355,15 @@ test("a shout's code reference opens that code as it was at that commit [B23]",await page.click({ code: was });assert.ok(page.show('feed').includes(button(was, false) + ', is'), 'a second click closes it');+ // A reference is the whole path the text gives, never a tail of it; what is no path in the repo,+ // or may be the end of a path with a space, says so rather than open another file.+ assert.ok(page.show('feed').includes(`absolute ${button('/' + was, false)} and spaced web/my ${button(spaced, false)}`));+ await page.click({ code: '/' + was });+ await page.click({ code: spaced });+ feed = page.show('feed');+ assert.ok(feed.includes(button('/' + was, true) + `<span class="code no">[BAD_REF] name a file inside the repo by its path from the top, such as src/serve.js (saw "/web/greeting.html")</span>`), 'escaped, as every refusal');+ assert.ok(feed.includes(button(spaced, true) + `<span class="code no">[AMBIGUOUS] web/note.txt may be the end of "web/my web/note.txt" at ${second.slice(0, 7)}, a path with a space, which a reference cannot name</span>`));+const ask = async (ref, { root = alpha.repo, key = view.key } = {}) => {const res = await fetch(`${view.base}/api/code?root=${encodeURIComponent(root)}&ref=${encodeURIComponent(ref)}`, { headers: { 'x-pullboard-key': key } });return [res.status, (await res.json()).error ?? ''];@@ -1364,6 +1379,7 @@ test("a shout's code reference opens that code as it was at that commit [B23]",assert.deepEqual(await ask(`web/greeting.html:0@${sha}`), [400, '[BAD_REF] name the lines as 12, or 12-30']);assert.deepEqual(await ask(`web/greeting.html:3-2@${sha}`), [400, '[BAD_REF] name the lines as 12, or 12-30']);assert.deepEqual(await ask(`web/greeting.html:9@${sha}`), [400, `[NO_LINES] web/greeting.html has 1 lines at ${sha}`]);+ assert.deepEqual(await ask(`web/greeting.html:1-2@${sha}`), [400, `[NO_LINES] web/greeting.html has 1 lines at ${sha}`], 'nor lines that run past the end');assert.deepEqual(await ask(`web/greeting.html@${sha}`), [400, 'a code reference reads path:lines@commit, such as src/serve.js:12-30@be4356b']);assert.deepEqual(await ask(was, { root: box.dir }), [400, 'not a project on this machine']);assert.equal((await ask(was, { key: 'wrong' }))[0], 403, 'and nothing without the secret');