Skip to content

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

TypeScript

fix(control-api): fence the refresh rejection on the caller's lock token (#739)

Alfredo Lopez58d463656c17
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index f1b260b97..e82d0cdbe 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.618",
+ "version": "0.1.619",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.618",
+ "version": "0.1.619",
"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 2bee1119c..39a202743 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.618",
+ "version": "0.1.619",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/services/codexSubscriptionConnection.ts b/control-api/src/services/codexSubscriptionConnection.ts
index 7d25aa16e..0b29bf380 100644
--- a/control-api/src/services/codexSubscriptionConnection.ts
+++ b/control-api/src/services/codexSubscriptionConnection.ts
@@ -649,12 +649,16 @@ export async function updateCodexAccessTokenInPlace(
/**
* The vendor rejected the stored refresh token. Fenced on the revision the
- * refresh read, so a grant replaced in the meantime is left alone.
+ * refresh read, so a grant replaced in the meantime is left alone, and on the
+ * caller's own refresh lock token: an in-place refresh does not bump the
+ * revision, so a holder that took the lock after ours expired may already
+ * have rotated the refresh token this rejection is about.
*/
export async function markCodexRefreshRejected(
db: DbClient,
connectionKey: string,
- expectedRevision: number
+ expectedRevision: number,
+ lockToken: string
): Promise<CodexSubscriptionSafeConnection | null> {
const result = await db.query(
`UPDATE codex_subscription_connections
@@ -662,9 +666,10 @@ export async function markCodexRefreshRejected(
updated_at = now()
WHERE connection_key = $1
AND credential_revision = $2
+ AND refresh_lock_token = $3
AND revoked_at IS NULL
RETURNING ${SAFE_CONNECTION_COLUMNS}`,
- [normalizeCodexConnectionKey(connectionKey), expectedRevision]
+ [normalizeCodexConnectionKey(connectionKey), expectedRevision, lockToken]
)
const row = result.rows[0] as SafeConnectionRow | undefined
return row ? toSafeConnection(row) : null
diff --git a/control-api/src/services/codexSubscriptionOAuth.ts b/control-api/src/services/codexSubscriptionOAuth.ts
index 467725dc3..f606612b9 100644
--- a/control-api/src/services/codexSubscriptionOAuth.ts
+++ b/control-api/src/services/codexSubscriptionOAuth.ts
@@ -402,6 +402,7 @@ export async function refreshCodexSubscriptionConnection(
const token = await exchangeRefreshToken(deps, secrets.refreshToken, {
expectedRevision: secrets.credentialRevision,
connectionKey: key,
+ lockToken,
})
try {
return await rotateCodexSubscriptionCredentials(
@@ -654,6 +655,7 @@ export async function ensureFreshCodexAccessToken(deps: CodexOAuthDeps): Promise
const token = await exchangeRefreshToken(deps, latest.refreshToken, {
expectedRevision: latest.credentialRevision,
connectionKey: key,
+ lockToken,
})
accountId = token.chatgptAccountId || chatgptAccountIdFromJwt(token.accessToken) || accountId
await updateCodexAccessTokenInPlace(
@@ -759,13 +761,14 @@ async function exchangeAuthorizationCode(
}
/**
- * Both callers hold the refresh lock and pass the revision they read under it,
- * so a rejection can be told apart from a refresh that lost a race.
+ * Both callers hold the refresh lock and pass its token and the revision they
+ * read under it, so a rejection can be told apart from a refresh that lost a
+ * race.
*/
async function exchangeRefreshToken(
deps: CodexOAuthDeps,
refreshToken: string,
- fence: { expectedRevision: number; connectionKey: string }
+ fence: { expectedRevision: number; connectionKey: string; lockToken: string }
): Promise<ParsedCodexToken> {
const result = await postForm(deps, CODEX_OAUTH_TOKEN_URL, {
grant_type: 'refresh_token',
@@ -791,11 +794,12 @@ async function exchangeRefreshToken(
const marked = await markCodexRefreshRejected(
deps.db,
fence.connectionKey,
- fence.expectedRevision
+ fence.expectedRevision,
+ fence.lockToken
)
- // The fenced UPDATE matched nothing: the grant was revoked or replaced
- // after the check above, so this is the same lost race and the row was
- // not written.
+ // The fenced UPDATE matched nothing: the grant was revoked or replaced,
+ // or another holder took the lock, after the check above, so this is the
+ // same lost race and the row was not written.
if (!marked) {
throw new CodexSubscriptionOAuthError(
'stale_revision',