Index host operations by kind to avoid polling history scans

This commit is contained in:
2026-10-05 08:40:37 -04:00
parent caa7672bec
commit 9472bb5184
2 changed files with 26 additions and 0 deletions
+22
View File
@@ -15,6 +15,28 @@ async function enrolled() {
}
describe('native host enrollment and scoped authentication', () => {
it('narrows cancellation lookups by kind instead of scanning completed health history', async () => {
const api = await enrolled();
api.heartbeat(api.host.id, { ...inventory, capabilities: ['instance.cancel-create', 'operations.health-lane'] });
const { db } = await import('../../src/lib/server/db/index.js');
const handle = db();
const insert = handle.prepare(`INSERT INTO ec_host_operations
(id,host_id,kind,payload,state,created_at,updated_at) VALUES(?,?,'instance.inspect','{}','completed',?,?)`);
const at = new Date().toISOString();
handle.transaction(() => {
for (let i = 0; i < 10_000; i++) insert.run(`history-${i}`, api.host.id, at, at);
})();
const inspect = api.queueHostOperation(api.host.id, 'instance.inspect', {});
const prepare = vi.spyOn(handle, 'prepare');
expect(api.pendingHostOperations(api.host.id, 'health').map(op => op.id)).toEqual([inspect.id]);
const query = prepare.mock.calls.map(([sql]) => sql).find(sql => sql.includes('SELECT * FROM ec_host_operations o'))!;
prepare.mockRestore();
// Assert the actual poll's access path, avoiding hardware-dependent timing thresholds.
const plan = handle.prepare(`EXPLAIN QUERY PLAN ${query}`).all(api.host.id, 1, 1) as { detail: string }[];
expect(plan.find(row => row.detail.includes('SEARCH cancellation'))?.detail)
.toContain('(host_id=? AND kind=?)');
});
it('reserves health delivery while a file backlog is pending and bounds background batches', async () => {
const api = await enrolled();
api.heartbeat(api.host.id, { ...inventory, capabilities: ['operations.health-lane'] });