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(view): search shows new Spec and Doctrine rows first [N26]

Corey Olsona55616cd711d
diff --git a/src/cockpit.js b/src/cockpit.js
index 00599c54..179204d2 100644
--- a/src/cockpit.js
+++ b/src/cockpit.js
@@ -1657,7 +1657,11 @@ function findAll(words) {
const has = (...parts) => parts.join(' ').toLowerCase().includes(q);
const items = p.items.filter((i) => has('#' + i.id, i.title, i.lane, i.specs.join(' '), i.criterion || ''))
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt)).map((i) => ({ go: 'item:' + i.id, id: '#' + i.id, text: i.title, note: stateLabel(i) }));
- const rows = (kind, list) => list.filter((r) => has(r.id, r.text)).map((r) => ({ go: kind + ':' + r.id, id: r.id, text: r.text, note: r.stage || r.status }));
+ // A Spec row or Doctrine rule is as new as the commit that added it (0 not yet committed; a standard rule, never added
+ // here, last); rows one commit added together read newest first by their place in the file.
+ const rows = (kind, list) => list.map((r, at) => ({ r, at })).filter(({ r }) => has(r.id, r.text))
+ .sort((a, b) => (a.r.added ?? Infinity) - (b.r.added ?? Infinity) || b.at - a.at)
+ .map(({ r }) => ({ go: kind + ':' + r.id, id: r.id, text: r.text, note: r.stage || r.status }));
const shouts = p.shouts.filter((x) => has(x.shout_from, x.shout_to, x.shout_text)).map((x) => ({ go: 'shout:' + x.shout_id, id: '#' + x.shout_id, text: firstLine(x.shout_text), note: shoutKind(x) }));
return [['Items', items], ['Spec', rows('spec', p.spec)], ['Doctrine', rows('doctrine', p.practice)], ['Shouts', shouts]].filter(([, list]) => list.length);
}
diff --git a/src/serve.js b/src/serve.js
index 1670673e..dec6fb20 100644
--- a/src/serve.js
+++ b/src/serve.js
@@ -15,8 +15,9 @@ import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:pat
import * as store from './board.js';
import { cockpitPage } from './cockpit.js';
import { COORDINATOR, loadConfig } from './config.js';
-import { loadDoctrine, standardDoctrine } from './doctrine.js';
+import { doctrineHistory, loadDoctrine, standardDoctrine } from './doctrine.js';
import { repoInfo, resolveCommit } from './git.js';
+import { committedIds } from './history.js';
import { productSummaries } from './products.js';
import { listApiProjects, registryFile } from './projects.js';
import { Refused } from './refused.js';
@@ -163,6 +164,30 @@ function withProject(root, read, providedBoard = null) {
}
}
+/**
+ * How new each row of a spec or doctrine file is, by the commit that added it: 1 for a row the newest commit to touch
+ * the file added, 2 for the commit before, and so on. A row on disk but not yet committed is missing here, and the
+ * state counts it 0, newer than any. Walking a file's history is the slowest read behind a state, so each file's ages
+ * are kept until HEAD moves, one entry a file.
+ *
+ * @param {string} root
+ * @param {string} file
+ * @param {(root: string, file: string) => { ids: Map<string, string> }} [history]
+ * @returns {Map<string, number>}
+ */
+function rowAges(root, file, history = committedIds) {
+ const head = resolveCommit(root, 'HEAD');
+ const key = root + '\0' + file, kept = agesKept.get(key);
+ if (kept && kept.head === head) return kept.ages;
+ const log = head ? spawnSync('git', ['log', '--follow', '--format=%H', 'HEAD', '--', file], { cwd: root, encoding: 'utf8' }) : null;
+ const place = new Map((log?.status === 0 ? log.stdout : '').split('\n').filter(Boolean).map((commit, n) => [commit, n + 1]));
+ const ages = new Map([...history(root, file).ids].map(([id, commit]) => [id, place.get(commit) ?? place.size + 1]));
+ agesKept.set(key, { head, ages });
+ return ages;
+}
+/** @type {Map<string, { head: string | null, ages: Map<string, number> }>} */
+const agesKept = new Map();
+
/**
* Everything the page shows for one project. Given the newest shout the person has seen there,
* it also counts every shout since, which the forty it sends cannot always show.
@@ -175,8 +200,13 @@ export function projectState(root, { seen = null, board: providedBoard = null }
return withProject(root, (board, info, config) => {
const all = store.listItems(board, { all: true });
const status = new Map(all.map((item) => [item.item_id, item.item_status]));
- const rows = (file) => loadSpec(info.root, { ...config, spec: file }).rows.map(({ id, status: state, tier, text, gate, serves, section }) => ({ id, status: state, tier, text, gate, serves, section }));
+ const rows = (file) => {
+ const ages = rowAges(info.root, file);
+ return loadSpec(info.root, { ...config, spec: file }).rows.map(({ id, status: state, tier, text, gate, serves, section }) => ({ id, status: state, tier, text, gate, serves, section, added: ages.get(id) ?? 0 }));
+ };
const doctrine = loadDoctrine(info.root, config);
+ // A standard rule was never added to this repo, so it has no age here; the repo's own rows have.
+ const doctrineAges = rowAges(info.root, doctrine.name, doctrineHistory);
// A decline's text is its reason. Keep the inherited rule available so the page strikes the
// rule itself, while showing the repo's reason beside it.
const inherited = new Map(standardDoctrine().rows.map((row) => [row.id, row.text]));
@@ -226,7 +256,7 @@ export function projectState(root, { seen = null, board: providedBoard = null }
holds: store.laneHolds(board),
spec: rows(config.spec),
practice: doctrine.rows.map(({ id, status: state, tier, text, gate, serves, section, origin, version, reason }) => ({
- id, status: state, tier, text, gate, serves, section, origin, version, reason,
+ id, status: state, tier, text, gate, serves, section, origin, version, reason, added: origin === 'standard' ? null : doctrineAges.get(id) ?? 0,
...(state === 'wont' && inherited.has(id) ? { standardText: inherited.get(id) } : {}),
})),
unseen: seen === null ? null : { since: seen, count: board.db.prepare('SELECT COUNT(*) AS n FROM shout WHERE shout_id > ?').get(seen).n },
diff --git a/test/cockpit.test.js b/test/cockpit.test.js
index 601351a6..c9e7181f 100644
--- a/test/cockpit.test.js
+++ b/test/cockpit.test.js
@@ -7311,7 +7311,7 @@ test('search in the top bar finds anything on the board [N26]', { timeout: 150_0
inTop: !!document.querySelector('.top .top-find #q'), toolbar: [...document.querySelector('.toolbar').children].filter((e) => e.offsetParent !== null || e.tagName === 'SELECT').map((e) => e.id || e.className),
field: box(document.querySelector('#q')), tabs: box(document.querySelector('#tabs')), top: box(document.querySelector('header.top')), theme: box(document.querySelector('#theme')),
bar: [...document.querySelectorAll('.toolbar #state-chips button, .toolbar #lane-pick, .toolbar #new-item')].filter((e) => e.getClientRects().length > 0)
- .map((e) => ({ name: e.dataset.state || e.id, middle: Math.round(e.getBoundingClientRect().top + e.getBoundingClientRect().height / 2) })),
+ .map((e) => ({ name: e.dataset.state || e.id, on: e.classList.contains('on'), middle: Math.round(e.getBoundingClientRect().top + e.getBoundingClientRect().height / 2) })),
shoutIds: data.project.shouts.map((x) => '#' + x.shout_id), focused: document.activeElement?.id ?? null, value: document.querySelector('#q').value,
shown: !results.hidden, groups: [...results.querySelectorAll('h4')].map((h) => [h.firstChild.textContent, h.querySelector('span').textContent]),
hits: [...results.querySelectorAll('.find-hit')].map((hit) => ({ go: hit.dataset.find, id: hit.querySelector('code').textContent, note: hit.querySelector('small').textContent, on: hit.classList.contains('on') })),
@@ -7381,6 +7381,40 @@ test('search in the top bar finds anything on the board [N26]', { timeout: 150_0
await key('Enter', 'Enter', 13, '\r');
await chrome.waitFor(`view.tab === 'items' && view.item === ${Number(third.slice(5))} && document.querySelector('#find-results').hidden`);
+ // On a phone, a search that opens an item shows All, since All is then the selected filter, so the person sees where
+ // they are and can leave it; Active again drops All.
+ for (const scheme of ['light', 'dark']) {
+ await chrome.send('Emulation.setDeviceMetricsOverride', { width: 375, height: 900, deviceScaleFactor: 1, mobile: false });
+ await chrome.send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: scheme }] });
+ await chrome.waitFor(`innerWidth === 375 && matchMedia('(prefers-color-scheme: ${scheme})').matches`);
+ await search('greeting');
+ await key('Enter', 'Enter', 13, '\r');
+ await chrome.waitFor("view.tab === 'items' && !!view.item && document.querySelector('#find-results').hidden");
+ const opened = (await read()).bar;
+ assert.deepEqual(opened.map((part) => [part.name, part.on]), [['active', false], ['verified', false], ['all', true], ['new-item', false]], `375px ${scheme}: a search that opens an item shows All, selected: ${JSON.stringify(opened)}`);
+ assert.ok(opened.every((part) => Math.abs(part.middle - opened[0].middle) <= 2), `375px ${scheme}: still one row: ${JSON.stringify(opened)}`);
+ await chrome.evaluate("document.querySelector('[data-state=active]').click()");
+ assert.deepEqual((await read()).bar.map((part) => part.name), ['active', 'verified', 'new-item'], `375px ${scheme}: back on Active, All drops again`);
+ }
+ await chrome.send('Emulation.setDeviceMetricsOverride', { width: 1280, height: 900, deviceScaleFactor: 1, mobile: false });
+ await chrome.send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: 'light' }] });
+ await chrome.waitFor('innerWidth === 1280');
+
+ // Spec rows and Doctrine rules read newest first by when each was added, not by where it sits in its file: R1 is
+ // committed, then R2 and G4 appended, then G5 inserted above G3.
+ box.git(alpha.repo, 'add', 'ways.md');
+ box.git(alpha.repo, 'commit', '-q', '-m', 'docs(rules): keep the local rules [G1]');
+ writeFileSync(join(alpha.repo, 'ways.md'), '# Local rules\n\n## Team\n- R1 [approved, must] Every greeting is read by a person. | gate: review\n- R2 [approved, must] A greeting never shouts. | gate: review\n');
+ writeFileSync(join(alpha.repo, 'SPEC.md'), `${spec}- G4 [approved, must] The greeting waves back. | gate: web test\n`);
+ box.git(alpha.repo, 'commit', '-q', '-am', 'docs(spec): the greeting waves back [G1]');
+ writeFileSync(join(alpha.repo, 'SPEC.md'), spec.replace('- G3 ', '- G5 [approved, must] The greeting fits a phone. | gate: web test\n- G3 ') + '- G4 [approved, must] The greeting waves back. | gate: web test\n');
+ box.git(alpha.repo, 'commit', '-q', '-am', 'docs(spec): the greeting fits a phone [G1]');
+ await chrome.waitFor("data.project.spec.some((row) => row.id === 'G5') && data.project.practice.some((row) => row.id === 'R2')", 15_000);
+ await search('greeting');
+ const added = (await read()).hits.filter((hit) => /^(spec|doctrine):/.test(hit.go)).map((hit) => hit.id);
+ assert.deepEqual(added, ['G5', 'G4', 'G3', 'R2', 'R1'], `Spec and Doctrine newest first by when each row was added: ${JSON.stringify(added)}`);
+ await key('Escape', 'Escape', 27);
+
// A click opens a Spec row, a Doctrine rule or a shout where it lives.
await search('greeting');
await tap('#find-results [data-find="spec:G3"]');