Skip to content

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

TypeScript

test(control-api): require the refresh-rejection fence to hold our lock token (#739)

Alfredo Lopezc0c0576d5ad7
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 8090d616a..f1b260b97 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.617",
+ "version": "0.1.618",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.617",
+ "version": "0.1.618",
"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 9e658aedb..2bee1119c 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.617",
+ "version": "0.1.618",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/services.codexSubscriptionRefreshRejected.realPostgres.integration.test.ts b/control-api/test/services.codexSubscriptionRefreshRejected.realPostgres.integration.test.ts
index efa7a4146..bc6e641cf 100644
--- a/control-api/test/services.codexSubscriptionRefreshRejected.realPostgres.integration.test.ts
+++ b/control-api/test/services.codexSubscriptionRefreshRejected.realPostgres.integration.test.ts
@@ -14,9 +14,12 @@ import { initDb } from '../src/db.js'
import { deriveOAuthEncryptionKey } from '../src/oauth/encryption.js'
import type { CodexCatalogTransport } from '../src/services/codexSubscriptionCatalog.js'
import {
+ acquireCodexSubscriptionRefreshLock,
getSafeCodexSubscriptionConnection,
insertInitialCodexSubscriptionConnection,
listLiveCodexSubscriptionConnections,
+ loadCodexSubscriptionSecrets,
+ updateCodexAccessTokenInPlace,
} from '../src/services/codexSubscriptionConnection.js'
import {
CODEX_OAUTH_TOKEN_URL,
@@ -314,6 +317,55 @@ describeRealPostgres('Codex refresh rejected by the vendor on real PostgreSQL (#
expect(await statusOf(key)).toBe('connected')
})
+ it('T-753j invalid_grant after another holder took the lock and refreshed in place is a lost race', async () => {
+ const key = 'codex-753j-takeover'
+ await seedConnected(key)
+ const takeover: { locked?: boolean; revision?: number } = {}
+ // Our lock expires mid-exchange; another holder acquires it with its own
+ // token and rotates the refresh token in place. In-place refresh does not
+ // bump credential_revision, so only the lock token tells the two apart.
+ const fetchFn = tokenEndpoint(400, { error: 'invalid_grant' }, async () => {
+ await pool.query(
+ `UPDATE codex_subscription_connections
+ SET refresh_lock_expires_at = now() - interval '1 second'
+ WHERE connection_key = $1`,
+ [key]
+ )
+ const locked = await acquireCodexSubscriptionRefreshLock(pool, 'other-holder', 30_000, key)
+ const before = await loadCodexSubscriptionSecrets(pool, KEY, key)
+ if (!before) throw new Error('seeded grant disappeared before the takeover')
+ await updateCodexAccessTokenInPlace(
+ pool,
+ KEY,
+ before.credentialRevision,
+ { accessToken: 'access-other-holder', refreshToken: 'refresh-other-holder' },
+ key
+ )
+ takeover.locked = locked
+ takeover.revision = before.credentialRevision
+ })
+
+ const failure = await ensureFreshCodexAccessToken(deps(key, fetchFn as typeof fetch)).then(
+ () => null,
+ (err: unknown) => err
+ )
+ const after = await loadCodexSubscriptionSecrets(pool, KEY, key)
+
+ // Liveness witness: the exchange ran, the other holder really took the
+ // lock, and its write is the one stored, at the revision we also read.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(takeover.locked).toBe(true)
+ expect(typeof takeover.revision).toBe('number')
+ expect(after).toMatchObject({
+ refreshToken: 'refresh-other-holder',
+ accessToken: 'access-other-holder',
+ credentialRevision: takeover.revision,
+ })
+ expect(failure).toBeInstanceOf(CodexSubscriptionOAuthError)
+ expect(failure).toMatchObject({ code: 'stale_revision', persistedConnectionStatus: false })
+ expect(await statusOf(key)).toBe('connected')
+ })
+
it('T-753h a revoke landing between the fence check and the mark is a lost race, not a persisted reauth', async () => {
const key = 'codex-753h-revoked'
await seedConnected(key)