Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-api): r6-b1 converge pruned feed cursors

Josué1c4688e0d348
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 15465495b..6e248e919 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.653",
+ "version": "0.1.654",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.653",
+ "version": "0.1.654",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index 99dedcfef..182cc1cf8 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.653",
+ "version": "0.1.654",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/db.ts b/control-api/src/db.ts
index 94d93020d..300e22880 100644
--- a/control-api/src/db.ts
+++ b/control-api/src/db.ts
@@ -14,7 +14,10 @@ import {
applyCodexSubscriptionConnectionSchema,
} from './services/codexSubscriptionConnection.js'
import { applyCodexSubscriptionOAuthStateSchema } from './services/codexSubscriptionOAuthState.js'
-import { applyEntityChangeSchema } from './services/entityChangeSchema.js'
+import {
+ applyEntityChangeCheckpointSchema,
+ applyEntityChangeSchema,
+} from './services/entityChangeSchema.js'
import {
applyGfsUploadCleanupSchema,
applyGfsUploadFinalizingSchema,
@@ -6269,6 +6272,10 @@ export const CONTROL_API_MIGRATIONS: DbMigration[] = [
legacyVersions: ['0116_durable_entity_change_feed', '0117_durable_entity_change_feed'],
apply: applyEntityChangeSchema,
},
+ {
+ version: '0120_entity_change_checkpoint_cursor_convergence',
+ apply: applyEntityChangeCheckpointSchema,
+ },
]
async function consolidateWorkflowAllowedUsersToTriggers(db: DbClient): Promise<void> {
diff --git a/control-api/src/services/entityChangeSchema.ts b/control-api/src/services/entityChangeSchema.ts
index f2f68e83a..22816800c 100644
--- a/control-api/src/services/entityChangeSchema.ts
+++ b/control-api/src/services/entityChangeSchema.ts
@@ -226,6 +226,20 @@ export async function applyEntityChangeSchema(db: DbClient): Promise<void> {
END;
$$;
+ REVOKE ALL ON FUNCTION entity_change_dispatch_batch(INTEGER, INTEGER) FROM PUBLIC;
+ GRANT EXECUTE ON FUNCTION entity_change_dispatch_batch(INTEGER, INTEGER) TO control_api_runtime;
+ `)
+
+ await applyEntityChangeCheckpointSchema(db)
+}
+
+/**
+ * Repairable checkpoint function contract. Kept separate so deployed databases
+ * can receive correctness fixes through an additive migration instead of
+ * relying on an already-applied migration body to execute again.
+ */
+export async function applyEntityChangeCheckpointSchema(db: DbClient): Promise<void> {
+ await db.query(`
DROP FUNCTION IF EXISTS entity_change_read_checkpoint(UUID);
CREATE OR REPLACE FUNCTION entity_change_read_checkpoint(
requested_cursor UUID,
@@ -265,8 +279,10 @@ export async function applyEntityChangeSchema(db: DbClient): Promise<void> {
SELECT sequence INTO cursor_sequence FROM public.entity_change_feed
WHERE cursor = requested_cursor;
END IF;
- IF cursor_sequence IS NULL AND requested_cursor = current_cursor AND
- current_watermark > pruned_watermark THEN
+ -- The current cursor is caught up even when retention has pruned every
+ -- feed row at or before the watermark. Advancing to this cursor after a
+ -- resync must converge instead of returning resync_required on every poll.
+ IF requested_cursor = current_cursor THEN
cursor_sequence := current_watermark;
END IF;
IF cursor_sequence IS NULL OR cursor_sequence < pruned_watermark OR
@@ -287,9 +303,7 @@ export async function applyEntityChangeSchema(db: DbClient): Promise<void> {
END;
$$;
- REVOKE ALL ON FUNCTION entity_change_dispatch_batch(INTEGER, INTEGER) FROM PUBLIC;
REVOKE ALL ON FUNCTION entity_change_read_checkpoint(UUID, INTEGER) FROM PUBLIC;
- GRANT EXECUTE ON FUNCTION entity_change_dispatch_batch(INTEGER, INTEGER) TO control_api_runtime;
GRANT EXECUTE ON FUNCTION entity_change_read_checkpoint(UUID, INTEGER) TO control_api_runtime;
`)
}
diff --git a/control-api/test/db.migrationsOrder.test.ts b/control-api/test/db.migrationsOrder.test.ts
index 1e8d5e71d..9c0643a56 100644
--- a/control-api/test/db.migrationsOrder.test.ts
+++ b/control-api/test/db.migrationsOrder.test.ts
@@ -95,6 +95,22 @@ describe('CONTROL_API_MIGRATIONS ordering invariant', () => {
}
})
+ it('registers an additive migration for expired entity-change cursor convergence', async () => {
+ const { CONTROL_API_MIGRATIONS } = await import('../src/db.js')
+ const migration = CONTROL_API_MIGRATIONS.find(
+ candidate => candidate.version === '0120_entity_change_checkpoint_cursor_convergence'
+ )
+ expect(migration).toBeDefined()
+ if (!migration) return
+
+ const query = vi.fn().mockResolvedValue({ rows: [], rowCount: 0 })
+ await migration.apply({ query } as never)
+
+ const sql = query.mock.calls.map(([statement]) => String(statement)).join('\n')
+ expect(sql).toMatch(/IF requested_cursor = current_cursor THEN/i)
+ expect(sql).not.toMatch(/current_watermark > pruned_watermark/i)
+ })
+
it('requires 0116_mcp_secret_rollback_permits to persist expiring rollback permits', async () => {
const { CONTROL_API_MIGRATIONS } = await import('../src/db.js')
// Renumbered twice while syncing onto dev: 0101 -> 0109 -> 0116. The earlier
diff --git a/control-api/test/entityChangeFeed.realPostgres.integration.test.ts b/control-api/test/entityChangeFeed.realPostgres.integration.test.ts
index 12c683397..c9c4a2103 100644
--- a/control-api/test/entityChangeFeed.realPostgres.integration.test.ts
+++ b/control-api/test/entityChangeFeed.realPostgres.integration.test.ts
@@ -399,6 +399,7 @@ describeRealPostgres('entity change feed real PostgreSQL contract', () => {
await instancePool.query('SELECT * FROM entity_change_dispatch_batch(1000, 86400)')
const expired = await instancePool.query<{
needs_resync: boolean
+ current_cursor: string
invalidated_scopes: string[]
}>('SELECT * FROM entity_change_read_checkpoint($1::uuid, $2)', [
lastEvent.rows[0]?.cursor,
@@ -407,6 +408,33 @@ describeRealPostgres('entity change feed real PostgreSQL contract', () => {
expect(expired.rows[0]?.needs_resync).toBe(true)
expect(expired.rows[0]?.invalidated_scopes).toEqual(['gfs', 'authorization'])
expect(JSON.stringify(expired.rows)).not.toContain(resource.rows[0]?.resource_id)
+
+ // The resync response advances to the current watermark. Reusing that
+ // server-issued cursor is caught up even when retention has pruned every
+ // corresponding feed row; an idle client must not receive resync forever.
+ const returnedCursor = expired.rows[0]?.current_cursor
+ expect(returnedCursor).toBeTruthy()
+ for (let attempt = 0; attempt < 3; attempt += 1) {
+ const resumed = await instancePool.query<{
+ needs_resync: boolean
+ current_cursor: string
+ invalidated_scopes: string[]
+ }>('SELECT * FROM entity_change_read_checkpoint($1::uuid, $2)', [returnedCursor, 10000])
+ expect(resumed.rows[0]?.needs_resync).toBe(false)
+ expect(resumed.rows[0]?.current_cursor).toBe(returnedCursor)
+ expect(resumed.rows[0]?.invalidated_scopes).toEqual([])
+ }
+
+ const freshClient = await instancePool.query<{
+ needs_resync: boolean
+ current_cursor: string
+ invalidated_scopes: string[]
+ }>('SELECT * FROM entity_change_read_checkpoint($1::uuid, $2)', [
+ '00000000-0000-0000-0000-000000000000',
+ 10000,
+ ])
+ expect(freshClient.rows[0]?.needs_resync).toBe(true)
+ expect(freshClient.rows[0]?.invalidated_scopes).toEqual(['gfs', 'authorization'])
})
it('coalesces latest-state invalidations and keeps feed storage behind the runtime boundary', async () => {