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.

perf(core): seek activation by qualified receipt sequence [E21,H16]

Corey Olson7e3bcb8733b5
diff --git a/src/board-hot.js b/src/board-hot.js
index 480cee8e..446a2036 100644
--- a/src/board-hot.js
+++ b/src/board-hot.js
@@ -1,5 +1,7 @@
import { ensureFactIdentityIndexes } from './fact-identity.js';
+import { missingFactIdentityIndexes } from './fact-identity.js';
import { ensureGrantActivationIndex } from './relay-activation.js';
+import { missingGrantActivationIndex } from './relay-activation.js';
/**
* The indexes and bounded reads behind the board's hot queries [C7]. Status, list, show, next, resume,
@@ -26,6 +28,16 @@ const HOT_INDEXES = [
*/
export function ensureHotIndexes(db) {
for (const sql of HOT_INDEXES) db.exec(sql);
+ ensureAdditiveIndexes(db);
+}
+
+/** Detect the additive upgrades eligible for an owning, already-current board. */
+export function missingAdditiveIndexes(db) {
+ return missingFactIdentityIndexes(db).length > 0 || missingGrantActivationIndex(db);
+}
+
+/** Install only additive fact/activation upgrades, inside the opener's existing atomic restamp. */
+export function ensureAdditiveIndexes(db) {
ensureFactIdentityIndexes(db);
ensureGrantActivationIndex(db);
}
diff --git a/src/board.js b/src/board.js
index 8eaef15b..7329e0a5 100644
--- a/src/board.js
+++ b/src/board.js
@@ -199,12 +199,10 @@ import {
boardBusyError,
} from './board-access.js';
import { ensureHotIndexes } from './board-hot.js';
-import { ensureGrantActivationIndex } from './relay-activation.js';
-import { missingGrantActivationIndex } from './relay-activation.js';
+import { ensureAdditiveIndexes } from './board-hot.js';
+import { missingAdditiveIndexes } from './board-hot.js';
import {
- ensureFactIdentityIndexes,
factIdentityExists,
- missingFactIdentityIndexes,
requireFactIdentityIndexes,
} from './fact-identity.js';
import { NEXT_STEPS } from './next-step-words.js';
@@ -433,10 +431,9 @@ export function openBoard(file, clock = systemClock, { isMain, onDiagnostic, wri
});
// Index-only upgrades keep the data version/digest unchanged. Install and restamp together;
// drifted or read-only stores never take this path, and old clients accept the resulting stamp.
- if (standing !== 'drifted' && (missingFactIdentityIndexes(db).length || missingGrantActivationIndex(db))) atomic(board, () => {
+ if (standing !== 'drifted' && missingAdditiveIndexes(db)) atomic(board, () => {
if (boardStanding(db, SCHEMA_DIGEST, SCHEMA_VERSION) !== 'current') return;
- ensureFactIdentityIndexes(db);
- ensureGrantActivationIndex(db);
+ ensureAdditiveIndexes(db);
recordSchemaStamp(db, SCHEMA_DIGEST);
});
if (standing === 'drifted') { warmCounters = false; reportDrift(file, onDiagnostic); }
diff --git a/src/relay-activation.js b/src/relay-activation.js
index c3116db9..efd65847 100644
--- a/src/relay-activation.js
+++ b/src/relay-activation.js
@@ -1,7 +1,7 @@
/** Additive activation-witness index; native receipts and their engine meaning stay unchanged [E21,H16,C7]. */
import { AGENT_GRANT_ENGINE } from './agent-grant-engine.js';
-export const GRANT_ACTIVATION_INDEX = 'board_meta_grant_activation_v1';
+export const GRANT_ACTIVATION_INDEX = 'board_meta_grant_activation_v2';
const UNSAFE_INDEX = 'board_meta_grant_activation_unsafe_v1';
const RECEIPTS = "meta_key GLOB 'relay_receipt_*'";
// Guard the outer envelope before extracting its nested move: either layer can be malformed.
@@ -10,6 +10,7 @@ const READABLE = `CASE WHEN json_valid(meta_value, 10) THEN
THEN 1 ELSE 0 END ELSE 0 END`;
const ENGINE = `CASE WHEN (${READABLE}) THEN json_extract(json_extract(meta_value, '$.move'), '$.engine') END`;
const SEQUENCE = "CASE WHEN json_valid(meta_value, 10) THEN json_extract(meta_value, '$.sequence') END";
+const QUALIFIED = `(${ENGINE}) >= ${AGENT_GRANT_ENGINE}`;
let warnedMissingIndex = false;
/** Owning writable opens install the derived index once, including on already-current boards. */
@@ -18,11 +19,11 @@ export function missingGrantActivationIndex(db) {
.get(GRANT_ACTIVATION_INDEX, UNSAFE_INDEX).n !== 2;
}
-/** Seek engine-qualified receipts first while retaining the original SQLite JSON comparisons. */
+/** Index only qualifying engines by sequence, so future receipts cannot become a growing filter scan. */
export function ensureGrantActivationIndex(db) {
db.exec(`CREATE INDEX IF NOT EXISTS ${GRANT_ACTIVATION_INDEX} ON board_meta (
- ${ENGINE}, ${SEQUENCE}
- ) WHERE ${RECEIPTS};
+ ${SEQUENCE}
+ ) WHERE ${RECEIPTS} AND ${QUALIFIED};
CREATE INDEX IF NOT EXISTS ${UNSAFE_INDEX} ON board_meta (meta_key)
WHERE ${RECEIPTS} AND (${READABLE}) = 0`);
}
@@ -53,6 +54,6 @@ export function recordedGrantActivation(board) {
// Only unsafe historical data takes this unbounded compatibility path; ordinary receipts never scan it.
if (unsafe) return legacyActivation(db, sequence);
return Boolean(db.prepare(`SELECT 1 FROM board_meta INDEXED BY ${GRANT_ACTIVATION_INDEX}
- WHERE ${RECEIPTS} AND (${SEQUENCE}) <= ? AND (${ENGINE}) >= ? LIMIT 1`)
- .get(sequence, AGENT_GRANT_ENGINE));
+ WHERE ${RECEIPTS} AND ${QUALIFIED} AND (${SEQUENCE}) <= ? LIMIT 1`)
+ .get(sequence));
}
diff --git a/test/relay-activation.test.js b/test/relay-activation.test.js
index 4bceab15..da29cdcc 100644
--- a/test/relay-activation.test.js
+++ b/test/relay-activation.test.js
@@ -49,7 +49,8 @@ function lookupPlan(board, probe) {
const plans = [];
board.db.prepare = sql => {
if (sql.includes("meta_key GLOB 'relay_receipt_*'") && sql.includes('json_extract')) {
- const bindings = sql.includes('$.sequence') ? [appliedSequence(board), AGENT_GRANT_ENGINE] : [];
+ const placeholders = (sql.match(/\?/gu) ?? []).length;
+ const bindings = [appliedSequence(board), AGENT_GRANT_ENGINE].slice(0, placeholders);
plans.push(...prepare(`EXPLAIN QUERY PLAN ${sql}`).all(...bindings).map(row => row.detail));
}
return prepare(sql);
@@ -131,7 +132,7 @@ test('a receipt beyond the applied cursor cannot activate the recorded prefix [E
assert.equal(agentGrantMinimum(box.board, box.probe), AGENT_GRANT_ENGINE, 'the exact boundary includes its witness');
});
-test('activation lookup seeks the engine index as genuine old receipts grow [E21,C7]', t => {
+test('activation lookup excludes old engines and seeks sequence as genuine receipts grow [E21,C7]', t => {
const box = fixture(t);
const observations = [];
for (const population of [8, 1024]) {
@@ -144,9 +145,12 @@ test('activation lookup seeks the engine index as genuine old receipts grow [E21
t.diagnostic(JSON.stringify({ method: 'actual predicate EXPLAIN QUERY PLAN at two native receipt populations; no wall-time inference', observations }));
for (const { floor, plans } of observations) {
assert.equal(floor, 1);
- assert.ok(plans.some(plan => /SEARCH board_meta USING INDEX board_meta_grant_activation_v1.*<expr>>\?/u.test(plan)),
- `lookup must seek qualifying engines instead of scanning every receipt: ${JSON.stringify(plans)}`);
+ assert.ok(plans.some(plan => /SEARCH board_meta USING INDEX board_meta_grant_activation_v2.*<expr><\?/u.test(plan)),
+ `lookup must seek the applied sequence inside qualifying engines: ${JSON.stringify(plans)}`);
}
+ const definition = box.board.db.prepare("SELECT sql FROM sqlite_master WHERE name='board_meta_grant_activation_v2'").get().sql;
+ assert.ok(definition.includes("json_extract(json_extract(meta_value, '$.move'), '$.engine')"));
+ assert.match(definition, new RegExp(`>= ${AGENT_GRANT_ENGINE}\\b`, 'u'), 'the index excludes old-engine rows before the sequence seek');
box.activate();
assert.equal(lookupPlan(box.board, box.probe).floor, AGENT_GRANT_ENGINE);
});
@@ -222,17 +226,17 @@ test('owning open upgrades an old indexless board once without changing native d
document = exportBoard(board);
version = board.db.prepare('PRAGMA user_version').get().user_version;
// Model an otherwise-current legacy board with its original, indexless schema stamp.
- board.db.exec('DROP INDEX IF EXISTS board_meta_grant_activation_v1');
+ board.db.exec('DROP INDEX IF EXISTS board_meta_grant_activation_v2; DROP INDEX IF EXISTS board_meta_grant_activation_unsafe_v1');
recordSchemaStamp(board.db, SCHEMA_DIGEST);
} finally { store.closeBoard(board); }
const read = store.openBoardForRead(file);
try {
assert.equal(read.current, true, 'old indexless boards remain readable at the same native schema version');
- assert.equal(read.db.prepare("SELECT 1 FROM sqlite_master WHERE name='board_meta_grant_activation_v1'").get(), undefined);
+ assert.equal(read.db.prepare("SELECT 1 FROM sqlite_master WHERE name='board_meta_grant_activation_v2'").get(), undefined);
} finally { read.db.close(); }
board = store.openBoard(file, undefined, { isMain: false });
try {
- assert.equal(board.db.prepare("SELECT 1 FROM sqlite_master WHERE name='board_meta_grant_activation_v1'").get(), undefined,
+ assert.equal(board.db.prepare("SELECT 1 FROM sqlite_master WHERE name='board_meta_grant_activation_v2'").get(), undefined,
'a non-owner never installs the additive index');
const probe = { operation: 'editItem', args: [1, { agentId: 'unmarked-web-seat' }] };
const warnings = [];
@@ -244,12 +248,12 @@ test('owning open upgrades an old indexless board once without changing native d
} finally { console.warn = warn; }
assert.equal(warnings.length, 1, 'missing-index fallback emits only one notice per process');
assert.match(warnings[0], /^\[RELAY_ACTIVATION_INDEX_MISSING\].*owning main checkout/u);
- assert.equal(board.db.prepare("SELECT 1 FROM sqlite_master WHERE name='board_meta_grant_activation_v1'").get(), undefined,
+ assert.equal(board.db.prepare("SELECT 1 FROM sqlite_master WHERE name='board_meta_grant_activation_v2'").get(), undefined,
'the fallback remains read-only and does not refuse or install an index');
} finally { store.closeBoard(board); }
board = store.openBoard(file);
try {
- assert.ok(board.db.prepare("SELECT 1 FROM sqlite_master WHERE type='index' AND name='board_meta_grant_activation_v1'").get(),
+ assert.ok(board.db.prepare("SELECT 1 FROM sqlite_master WHERE type='index' AND name='board_meta_grant_activation_v2'").get(),
'the owning writable opener installs the missing index on an already-current board');
assert.equal(board.db.prepare('PRAGMA user_version').get().user_version, version);
assert.deepEqual(exportBoard(board), document, 'index installation leaves every native row and export field unchanged');
@@ -259,7 +263,7 @@ test('owning open upgrades an old indexless board once without changing native d
finally { stamped.db.close(); }
board = store.openBoard(file);
try {
- assert.equal(board.db.prepare("SELECT count(*) AS n FROM sqlite_master WHERE name='board_meta_grant_activation_v1'").get().n, 1);
+ assert.equal(board.db.prepare("SELECT count(*) AS n FROM sqlite_master WHERE name='board_meta_grant_activation_v2'").get().n, 1);
assert.deepEqual(exportBoard(board), document, 'a second owning open is idempotent');
} finally { store.closeBoard(board); }
});