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 weighs its whole line, not four words [B23]
Corey Olson59b1cdc19879
diff --git a/src/cockpit.js b/src/cockpit.jsindex 4f111a4e..3d8ba4fd 100644--- a/src/cockpit.js+++ b/src/cockpit.js@@ -386,8 +386,8 @@ const stateOf = (i) => i.status === 'claimed' ? 'building' : i.status === 'submiconst STATES = { building: ['building', 'busy'], verify: ['to verify', 'warn'], back: ['sent back', 'no'], verified: ['verified', 'ok'], open: ['open', ''], withdrawn: ['withdrawn', ''] };const chip = (s) => '<span class="chip ' + STATES[s][1] + '">' + STATES[s][0] + '</span>';// A shout's path:lines@commit reference (B23): a button, and under it, once opened, that code as it-// was at that commit, with its line numbers. The words before it go with it, so the view can refuse-// a reference they may make part of a path with a space.+// was at that commit, with its line numbers. The text before it on its line goes with it, so the view+// can refuse a reference that text may make part of a longer path.const codeRef = (ref, before) => {const c = view.code[view.root + '\\n' + before + '\\n' + ref];const open = Boolean(c && c.open);@@ -395,14 +395,6 @@ const codeRef = (ref, before) => {: '<span class="code">' + c.lines.map((line, n) => '<span><i>' + (c.from + n) + '</i>' + esc(line) + '</span>').join('') + (c.more ? '<span class="more">the first ' + c.lines.length + ' lines</span>' : '') + '</span>';return '<button class="ref" data-code="' + esc(ref) + '"' + (before ? ' data-before="' + esc(before) + '"' : '') + ' type="button" aria-expanded="' + open + '">' + esc(ref) + '</button>' + shown;};-// The words written just before a reference that could be part of a path, nearest last, up to four:-// a path with a space would start among them.-const wordsBefore = (prior) => {- const words = /[ \\t]$/.test(prior) ? prior.split('\\n').pop().trim().split(/[ \\t]+/) : [];- const kept = [];- while (words.length && kept.length < 4 && /^[^:@()[\\]{}"'\`]+$/.test(words.at(-1))) kept.unshift(words.pop());- return kept.join(' ');-};const tone = (s) => s === 'approved' ? 'ok' : s === 'pending' ? 'no' : s === 'draft' ? 'warn' : '';const count = (n, one, many = one) => n + ' ' + (n === 1 ? one : many);// What needs the person in a project, as its Needs-you list counts it: work sent back or waiting for a@@ -713,7 +705,7 @@ function render() {// 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+|(?<![^\\s([{"'\`])[^\\s:@()[\\]{}"'\`]+:\\d+(?:-\\d+)?@[0-9a-f]{7,40}(?![^\\s)\\]}"'\`.,;:!?]))/).map((part, n, parts) => {- if (n % 2 && part[0] !== '#') return codeRef(part, wordsBefore(parts.slice(0, n).join('')));+ if (n % 2 && part[0] !== '#') return codeRef(part, parts.slice(0, n).join('').split('\\n').pop().slice(-2000));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);}).join('');diff --git a/src/serve.js b/src/serve.jsindex c6dbcf88..befba6ca 100644--- a/src/serve.js+++ b/src/serve.js@@ -141,9 +141,9 @@ 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, never lines past the file's end, and never more- * than CODE_LINES lines. A path cannot hold a space, so the words written just before a reference- * may be the start of one: when they and the path make a file the commit holds, the reference may- * mean that file, and it is refused rather than opened as a different one.+ * than CODE_LINES lines. A reference is one word, so the text written before it on its line may be+ * the start of a longer path that ends in this one: when the commit holds such a file, the reference+ * may mean it, and it is refused rather than opened as a different file.** @param {string} root* @param {{ path: string, from: number, to: number, commit: string, before?: string }} ref@@ -160,13 +160,10 @@ export function codeAt(root, { path, from, to, commit, before = '' }) {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 words = String(before).split(' ').filter(Boolean).slice(-4);- for (let k = 1; k <= words.length; k += 1) {- const longer = `${words.slice(-k).join(' ')} ${path}`;- if (spawnSync('git', ['cat-file', '-e', `${full}:${longer}`], { cwd: root }).status === 0) {- throw new Refused('AMBIGUOUS', `the words before it may make it "${longer}", a path with a space, which a reference cannot name`);- }- }+ const written = `${before}${path}`;+ 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.length > path.length && name.endsWith(path) && written.endsWith(name));+ if (longer) throw new Refused('AMBIGUOUS', `the text before it may make it "${longer}", 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');@@ -263,7 +260,7 @@ export function serveView({ port = 0, secret = randomBytes(18).toString('base64uconst ref = /^([^:@]+):(\d+)(?:-(\d+))?@([^@]+)$/.exec(url.searchParams.get('ref') ?? '');if (!ref) return json(res, 400, { error: 'a code reference reads path:lines@commit, such as src/serve.js:12-30@be4356b' });try {- const before = (url.searchParams.get('before') ?? '').slice(-200);+ const before = (url.searchParams.get('before') ?? '').slice(-2000);return json(res, 200, codeAt(root, { path: ref[1], from: Number(ref[2]), to: Number(ref[3] ?? ref[2]), commit: ref[4], before }));} catch (error) {if (error instanceof Refused) return json(res, 400, { error: error.message });diff --git a/test/cockpit.test.js b/test/cockpit.test.jsindex 2aea9e4a..2b13200a 100644--- a/test/cockpit.test.js+++ b/test/cockpit.test.js@@ -1321,64 +1321,76 @@ test("a shout's code reference opens that code as it was at that commit [B23]",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');+ mkdirSync(join(alpha.web, 'web', 'one two three four five web'));+ writeFileSync(join(alpha.web, 'web', 'one two three four five web', 'note.txt'), 'longer 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');const was = `web/greeting.html:1@${first.slice(0, 7)}`;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 first_ = `#1 was ${was}, is ${now}; see ${long}.`;+ box.run(alpha.web, 'shout', 'all', first_);const spaced = `web/note.txt:1@${second.slice(0, 7)}`;- box.run(alpha.web, 'shout', 'all', `absolute /${was} and spaced web/my ${spaced}`);+ const odd = `absolute /${was} and spaced web/my ${spaced}`;+ box.run(alpha.web, 'shout', 'all', odd);box.run(alpha.web, 'shout', 'all', `plus x+${spaced}`);box.run(alpha.web, 'shout', 'all', spaced);+ box.run(alpha.web, 'shout', 'all', `web/one two three four five ${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);try {const page = await openPage(view);- // A button carries the words just before it that could be part of a path.+ // A button carries the text written before it on its line.const button = (ref, open, before = '') => `<button class="ref" data-code="${ref}"${before ? ` data-before="${before}"` : ''} type="button" aria-expanded="${open}">${ref}</button>`;- assert.ok(page.show('feed').includes(`#1</button> was ${button(was, false, '#1 was')}, is ${button(now, false, 'is')}; see ${button(long, false, 'see')}.`), 'each reference is a button in the text');+ const prior = (text, ref) => text.slice(0, text.indexOf(ref));+ const [b1, b2, b3] = [was, now, long].map((ref) => prior(first_, ref));+ assert.ok(page.show('feed').includes(`#1</button> was ${button(was, false, b1)}, is ${button(now, false, b2)}; see ${button(long, false, b3)}.`), 'each reference is a button in the text');- await page.click({ code: was, before: '#1 was' });- await page.click({ code: now, before: 'is' });+ await page.click({ code: was, before: b1 });+ await page.click({ code: now, before: b2 });let feed = page.show('feed');- assert.ok(feed.includes(button(was, true, '#1 was') + '<span class="code"><span><i>1</i>greeting.html</span></span>'), 'the code as it was at that commit');- assert.ok(feed.includes(button(now, true, 'is') + '<span class="code"><span><i>1</i> hello <b>there</b></span><span><i>2</i> second line</span></span>'), 'escaped, every indent kept');+ assert.ok(feed.includes(button(was, true, b1) + '<span class="code"><span><i>1</i>greeting.html</span></span>'), 'the code as it was at that commit');+ assert.ok(feed.includes(button(now, true, b2) + '<span class="code"><span><i>1</i> hello <b>there</b></span><span><i>2</i> second line</span></span>'), 'escaped, every indent kept');- await page.click({ code: long, before: 'see' });+ await page.click({ code: long, before: b3 });feed = page.show('feed');- const shown = feed.slice(feed.indexOf(button(long, true, 'see')));+ const shown = feed.slice(feed.indexOf(button(long, true, b3)));assert.equal([...shown.matchAll(/<span><i>(\d+)<\/i>line \1<\/span>/g)].length, 60, 'at most sixty lines');assert.match(shown, /<i>60<\/i>line 60<\/span><span class="more">the first 60 lines<\/span><\/span>/);await page.run('seen = ""; refresh()');- assert.ok(page.show('feed').includes(button(was, true, '#1 was') + '<span class="code">'), 'a refresh keeps it open');- await page.click({ code: was, before: '#1 was' });- assert.ok(page.show('feed').includes(button(was, false, '#1 was') + ', is'), 'a second click closes it');+ assert.ok(page.show('feed').includes(button(was, true, b1) + '<span class="code">'), 'a refresh keeps it open');+ await page.click({ code: was, before: b1 });+ assert.ok(page.show('feed').includes(button(was, false, b1) + ', is'), 'a second click closes it');// A reference is a whole word, never the tail of one: what is no path in the repo says so, and so- // does one the words before it may make part of a path with a space, rather than open another file.+ // does one the text before it may make part of a longer path, however long, rather than open+ // another file.+ const b4 = prior(odd, spaced);+ const b5 = 'web/one two three four five ';feed = page.show('feed');- assert.ok(feed.includes(`absolute ${button('/' + was, false, 'absolute')} and spaced web/my ${button(spaced, false, 'and spaced web/my')}`));- assert.ok(feed.includes(`plus ${button('x+' + spaced, false, 'plus')}`));- await page.click({ code: '/' + was, before: 'absolute' });- await page.click({ code: spaced, before: 'and spaced web/my' });- await page.click({ code: 'x+' + spaced, before: 'plus' });+ assert.ok(feed.includes(`absolute ${button('/' + was, false, 'absolute ')} and spaced web/my ${button(spaced, false, b4)}`));+ assert.ok(feed.includes(`plus ${button('x+' + spaced, false, 'plus ')}`));+ await page.click({ code: '/' + was, before: 'absolute ' });+ await page.click({ code: spaced, before: b4 });+ await page.click({ code: 'x+' + spaced, before: 'plus ' });await page.click({ code: spaced });+ await page.click({ code: spaced, before: b5 });feed = page.show('feed');- assert.ok(feed.includes(button('/' + was, true, 'absolute') + '<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('x+' + spaced, true, 'plus') + '<span class="code no">[BAD_REF] name a file inside the repo by its path from the top, such as src/serve.js (saw "x+web/note.txt")</span>'));- assert.ok(feed.includes(button(spaced, true, 'and spaced web/my') + '<span class="code no">[AMBIGUOUS] the words before it may make it "web/my web/note.txt", a path with a space, which a reference cannot name</span>'));+ assert.ok(feed.includes(button('/' + was, true, 'absolute ') + '<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('x+' + spaced, true, 'plus ') + '<span class="code no">[BAD_REF] name a file inside the repo by its path from the top, such as src/serve.js (saw "x+web/note.txt")</span>'));+ assert.ok(feed.includes(button(spaced, true, b4) + '<span class="code no">[AMBIGUOUS] the text before it may make it "web/my web/note.txt", which a reference cannot name</span>'));+ assert.ok(feed.includes(button(spaced, true, b5) + '<span class="code no">[AMBIGUOUS] the text before it may make it "web/one two three four five web/note.txt", which a reference cannot name</span>'), 'however many words the path has');assert.ok(feed.includes(button(spaced, true) + '<span class="code"><span><i>1</i>plain note</span></span>'), 'a reference standing alone opens, whatever other files there are');const ask = async (ref, { root = alpha.repo, key = view.key, before = '' } = {}) => {const res = await fetch(`${view.base}/api/code?root=${encodeURIComponent(root)}&ref=${encodeURIComponent(ref)}&before=${encodeURIComponent(before)}`, { headers: { 'x-pullboard-key': key } });return [res.status, (await res.json()).error ?? ''];};- assert.deepEqual(await ask(spaced, { before: 'web/my' }), [400, '[AMBIGUOUS] the words before it may make it "web/my web/note.txt", a path with a space, which a reference cannot name']);- assert.deepEqual(await ask(spaced, { before: 'see the' }), [200, ''], 'words that make no file leave it be');+ assert.deepEqual(await ask(spaced, { before: 'see web/my ' }), [400, '[AMBIGUOUS] the text before it may make it "web/my web/note.txt", which a reference cannot name']);+ assert.deepEqual(await ask(spaced, { before: 'see the ' }), [200, ''], 'text that makes no file leaves it be');const sha = first.slice(0, 7);for (const ref of [`../outside.txt:1@${sha}`, `/etc/passwd:1@${sha}`, `web/../../outside.txt:1@${sha}`, `./web/greeting.html:1@${sha}`]) {assert.deepEqual(await ask(ref), [400, `[BAD_REF] name a file inside the repo by its path from the top, such as src/serve.js (saw "${ref.split(':')[0]}")`], `${ref} is refused`);@@ -1398,7 +1410,7 @@ test("a shout's code reference opens that code as it was at that commit [B23]",// A refused reference says why where its code would be.box.run(alpha.web, 'shout', 'all', `gone: web/greeting.html:7@${sha}`);await page.run('refresh()');- await page.click({ code: `web/greeting.html:7@${sha}` });+ await page.click({ code: `web/greeting.html:7@${sha}`, before: 'gone: ' });assert.ok(page.show('feed').includes(`<span class="code no">[NO_LINES] web/greeting.html has 1 lines at ${sha}</span>`));} finally {await view.stop();