Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: pin index uniqueness and ordered-scan plan shape for the human_activity composites (#5715)

Adam Eivydc4e55b993f5
diff --git a/server/lib/db.catalogDdlParity.test.js b/server/lib/db.catalogDdlParity.test.js
index 618d45bcd0..185c33269a 100644
--- a/server/lib/db.catalogDdlParity.test.js
+++ b/server/lib/db.catalogDdlParity.test.js
@@ -106,18 +106,21 @@ function extractIndexNames(source, prefix = 'idx_catalog_') {
}
// Names alone can't catch an index that exists in both files under the same
-// name but over different columns (or a different sort order) — that ships a
-// fresh install an index the boot-time DDL never intended. Pair each name with
-// its normalized column list so the parity check compares definitions.
+// name but over different columns (or a different sort order, or having lost
+// its UNIQUE) — that ships a fresh install an index the boot-time DDL never
+// intended, and a dropped UNIQUE breaks the ON CONFLICT idempotency contract
+// the store relies on. Pair each name with its normalized definition so the
+// parity check compares shape, not just identity.
function extractIndexDefs(source, prefix) {
const out = new Map();
const re = new RegExp(
- `CREATE (?:UNIQUE )?INDEX IF NOT EXISTS\\s+(${prefix}\\w+)\\s+ON\\s+\\w+\\s*\\(([^)]*)\\)`,
+ `CREATE (UNIQUE )?INDEX IF NOT EXISTS\\s+(${prefix}\\w+)\\s+ON\\s+\\w+\\s*\\(([^)]*)\\)`,
'gi',
);
let m;
while ((m = re.exec(source)) !== null) {
- out.set(m[1], m[2].replace(/\s+/g, ' ').trim().toLowerCase());
+ const cols = m[3].replace(/\s+/g, ' ').trim().toLowerCase();
+ out.set(m[2], `${m[1] ? 'unique ' : ''}(${cols})`);
}
return out;
}
@@ -212,10 +215,10 @@ describe('catalog DDL parity (init-db.sql ↔ db.js ensureSchema)', () => {
// happened_at walk — dropping either from both files, or silently losing
// the DESC ordering, would otherwise read as a passing set match.
const expected = {
- idx_human_activity_dedupe: 'source, dedupe_key',
- idx_human_activity_happened: 'happened_at',
- idx_human_activity_source_happened: 'source, happened_at desc',
- idx_human_activity_source_kind_happened: 'source, kind, happened_at desc',
+ idx_human_activity_dedupe: 'unique (source, dedupe_key)',
+ idx_human_activity_happened: '(happened_at)',
+ idx_human_activity_source_happened: '(source, happened_at desc)',
+ idx_human_activity_source_kind_happened: '(source, kind, happened_at desc)',
};
expect(Object.fromEntries(sqlIdx)).toEqual(expected);
expect(Object.fromEntries(jsIdx)).toEqual(expected);
diff --git a/server/services/humanActivity.db.test.js b/server/services/humanActivity.db.test.js
index b56ead99a0..9073cb1f11 100644
--- a/server/services/humanActivity.db.test.js
+++ b/server/services/humanActivity.db.test.js
@@ -252,9 +252,22 @@ describe.skipIf(!runDb)('source-scoped read plan (#5715)', () => {
const explainWithoutSeqScan = (sql, params) => withTransaction(async (client) => {
await client.query('SET LOCAL enable_seqscan = off');
const res = await client.query(`EXPLAIN (FORMAT JSON) ${sql}`, params);
- return JSON.stringify(res.rows[0]['QUERY PLAN']);
+ return res.rows[0]['QUERY PLAN'][0].Plan;
});
+ const planNodes = (node) => [node, ...(node.Plans || []).flatMap(planNodes)];
+
+ // The point of the composite is that the index supplies the ordering, so the
+ // LIMIT stops early. A bitmap scan feeding a Sort would still name the index
+ // while re-reading and sorting every matching row — the exact regression this
+ // pins — so assert an ordered Index Scan AND the absence of a Sort node.
+ const expectOrderedIndexScan = (plan, indexName) => {
+ const nodes = planNodes(plan);
+ expect(nodes.map((n) => n['Node Type'])).not.toContain('Sort');
+ expect(nodes.some((n) => /^Index (Only )?Scan$/.test(n['Node Type']) && n['Index Name'] === indexName))
+ .toBe(true);
+ };
+
it('uses idx_human_activity_source_happened for a source-scoped newest-first read', async () => {
await recordEvents([mk({ dedupeKey: 'plan-1' }), mk({ dedupeKey: 'plan-2' })]);
const plan = await explainWithoutSeqScan(
@@ -264,7 +277,7 @@ describe.skipIf(!runDb)('source-scoped read plan (#5715)', () => {
LIMIT 50`,
[SOURCE],
);
- expect(plan).toContain('idx_human_activity_source_happened');
+ expectOrderedIndexScan(plan, 'idx_human_activity_source_happened');
});
it('uses idx_human_activity_source_kind_happened when the read is also kind-scoped', async () => {
@@ -276,6 +289,6 @@ describe.skipIf(!runDb)('source-scoped read plan (#5715)', () => {
LIMIT 50`,
[SOURCE, 'message.received'],
);
- expect(plan).toContain('idx_human_activity_source_kind_happened');
+ expectOrderedIndexScan(plan, 'idx_human_activity_source_kind_happened');
});
});