Skip to content

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

TypeScript

fix(authorize): answer a committed authorize instead of relabelling it

Alfredo Lopez8262337e6f14
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 4c05778ab..5ad5a6c01 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.721",
+ "version": "0.1.722",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.721",
+ "version": "0.1.722",
"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 1b5e6ed95..68c864d42 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.721",
+ "version": "0.1.722",
"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 ae81db69a..5da3ba2a2 100644
--- a/control-api/src/db.ts
+++ b/control-api/src/db.ts
@@ -6845,7 +6845,8 @@ export async function withTransaction<T>(
commitSent = true
await client.query('COMMIT')
transactionFinished = true
- signal?.throwIfAborted()
+ // An acknowledged COMMIT is the outcome, even if the signal aborted
+ // meanwhile: the finally still evicts the session instead of restoring it.
return result
} catch (error) {
if (commitSent || !transactionStarted) {
diff --git a/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts b/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts
index e8a80500b..50c88838c 100644
--- a/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts
+++ b/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts
@@ -221,7 +221,11 @@ export function createMcpHostLlmProviderAttemptRoutes(gateway: K8sGateway): Rout
signal,
resolveAssignment: hostRef => resolveHostAssignedAssignment(gateway, hostRef, signal),
})
- signal.throwIfAborted()
+ // A resolved authorize has committed its ticket and reservation, even
+ // when the work clock or the client stopped meanwhile. It is answered
+ // while the client can still read it and never relabelled as
+ // authorize_timeout; the admission then sees the abort and returns.
+ if (res.destroyed || res.writableEnded) return
res.status(200).json(result)
}
try {
diff --git a/control-api/test/llmProviderAttemptAuthorizer.cancellation.test.ts b/control-api/test/llmProviderAttemptAuthorizer.cancellation.test.ts
index 5d5290759..6abfd4061 100644
--- a/control-api/test/llmProviderAttemptAuthorizer.cancellation.test.ts
+++ b/control-api/test/llmProviderAttemptAuthorizer.cancellation.test.ts
@@ -819,6 +819,55 @@ describe('cancellable transaction server idle backstop with native pool max one'
await expect(withTransaction(async () => 'recovered', fixture.pool)).resolves.toBe('recovered')
})
+ // Review 5426789128: an abort after the COMMIT reply arrived must not
+ // relabel the committed result as a cancellation.
+ it('keeps an acknowledged COMMIT when the signal aborts as its reply resolves', async () => {
+ const fixture = await postgresPeer()
+ const controller = new AbortController()
+ const reason = interruption('authorize_timeout')
+ fixture.pool.once('acquire', client => {
+ const query = client.query.bind(client) as (text: string) => Promise<unknown>
+ vi.spyOn(client, 'query').mockImplementation(((text: string, ...rest: unknown[]) => {
+ const reply = (query as (...args: unknown[]) => Promise<unknown>)(text, ...rest)
+ // Abort in the continuation of the acknowledged COMMIT, before the
+ // transaction helper resumes.
+ return text === 'COMMIT'
+ ? reply.then(result => {
+ controller.abort(reason)
+ return result
+ })
+ : reply
+ }) as never)
+ })
+ const outcome = withTransaction(async () => 'committed', fixture.pool, {
+ signal: controller.signal,
+ })
+ await expect(within(outcome)).resolves.toBe('committed')
+ // Witness: the abort did land, after COMMIT was acknowledged.
+ expect(controller.signal.aborted).toBe(true)
+ expect(fixture.queries).toContain('COMMIT')
+ expect(fixture.queries).not.toContain('ROLLBACK')
+ // The aborted session is evicted, not restored to the pool.
+ expect(fixture.sessionChanges).toHaveLength(1)
+ expect(fixture.pool.totalCount).toBe(0)
+ })
+
+ it('keeps an acknowledged COMMIT when the signal aborts during session restoration', async () => {
+ const fixture = await postgresPeer()
+ fixture.stall(settingSql, 2)
+ const controller = new AbortController()
+ const outcome = withTransaction(async () => 'committed', fixture.pool, {
+ signal: controller.signal,
+ })
+ await observed(() => fixture.sessionChanges.length === 2)
+ // Witness: COMMIT was acknowledged and the restoration is in flight.
+ expect(fixture.queries.indexOf('COMMIT')).toBeLessThan(fixture.queries.lastIndexOf(settingSql))
+ controller.abort(interruption('authorize_aborted'))
+ await expect(within(outcome)).resolves.toBe('committed')
+ expect(fixture.queries).not.toContain('ROLLBACK')
+ expect(fixture.pool.totalCount).toBe(0)
+ })
+
it('evicts a failed restoration after ROLLBACK and keeps the original work failure', async () => {
const fixture = await postgresPeer()
fixture.fail(settingSql, 2)
diff --git a/control-api/test/routes.mcp-host.llmProviderAttempts.bodyAdmission.test.ts b/control-api/test/routes.mcp-host.llmProviderAttempts.bodyAdmission.test.ts
index 3d21f5dbe..772814016 100644
--- a/control-api/test/routes.mcp-host.llmProviderAttempts.bodyAdmission.test.ts
+++ b/control-api/test/routes.mcp-host.llmProviderAttempts.bodyAdmission.test.ts
@@ -821,13 +821,14 @@ describe('createApp retained authorize-body ownership', () => {
})
}
- it('returns terminal authorize_timeout after the real work clock aborts, without publishing late success', async () => {
- const started = deferred()
- const abortObserved = deferred<AbortSignal>()
+ // The work clock's abort reaches the authorizer. A rejection means nothing
+ // was committed and is answered as authorize_timeout; a resolution means the
+ // transaction committed, so its result is answered (review 5426789128).
+ function shortenWorkClock() {
const nativeSetTimeout = globalThis.setTimeout
// Only shorten the owner clock for this socket fixture. The production
// policy remains unchanged; this test makes no numeric safety claim.
- const clock = vi
+ return vi
.spyOn(globalThis, 'setTimeout')
.mockImplementation((fn, ms, ...args) =>
nativeSetTimeout(
@@ -836,6 +837,11 @@ describe('createApp retained authorize-body ownership', () => {
...args
)
)
+ }
+ function authorizeAfterAbort(outcome: 'reject' | 'resolve') {
+ const started = deferred()
+ const abortObserved = deferred<AbortSignal>()
+ const settled = deferred()
vi.mocked(authorizer.authorizeLlmProviderAttempt).mockImplementationOnce(
async (_claims, _body, deps) => {
const signal = deps?.signal
@@ -851,15 +857,20 @@ describe('createApp retained authorize-body ownership', () => {
{ once: true }
)
)
- // Deliberately produce a late result: the route must check the signal
- // immediately before publishing an execution ticket.
+ settled.resolve()
+ if (outcome === 'reject') signal.throwIfAborted()
return SUCCESS
}
)
+ return { started, abortObserved, settled }
+ }
+
+ it('returns terminal authorize_timeout when the real work clock aborts an uncommitted authorize', async () => {
+ const clock = shortenWorkClock()
+ const { started, abortObserved } = authorizeAfterAbort('reject')
try {
await withApps(1, async ([app]) => {
- const sent = send(app.url, headers('work-timeout-host'), '{}')
- const outcome = sent.response
+ const outcome = send(app.url, headers('work-timeout-host'), '{}').response
await started.promise
const signal = await abortObserved.promise
expect(signal.reason).toBeInstanceOf(AuthorizeWorkInterrupted)
@@ -878,6 +889,49 @@ describe('createApp retained authorize-body ownership', () => {
}
})
+ it('answers a committed authorize with 200 after the real work clock aborted', async () => {
+ const clock = shortenWorkClock()
+ const { started, abortObserved } = authorizeAfterAbort('resolve')
+ try {
+ await withApps(1, async ([app]) => {
+ const outcome = send(app.url, headers('late-commit-host'), '{}').response
+ await started.promise
+ const signal = await abortObserved.promise
+ expect(signal.reason).toBeInstanceOf(AuthorizeWorkInterrupted)
+ expect(signal.reason.code).toBe('authorize_timeout')
+ const response = await outcome
+ expect(response.status).toBe(200)
+ expect(JSON.parse(response.body)).toEqual(SUCCESS)
+ expect((app.observations[0].request as Request).body).toBeUndefined()
+ const recovered = await send(app.url, headers('after-late-commit-host'), '{}').response
+ expect(recovered.status).toBe(200)
+ })
+ } finally {
+ clock.mockRestore()
+ }
+ })
+
+ it('writes nothing for a committed authorize whose client already disconnected', async () => {
+ const { started, abortObserved, settled } = authorizeAfterAbort('resolve')
+ await withApps(1, async ([app]) => {
+ const sent = send(app.url, headers('late-commit-gone-host'), '{}')
+ const outcome = sent.response.catch(error => error)
+ await started.promise
+ sent.client.destroy()
+ const signal = await abortObserved.promise
+ expect(signal.reason).toBeInstanceOf(AuthorizeWorkInterrupted)
+ expect(signal.reason.code).toBe('authorize_aborted')
+ // Witness: the authorizer resolved its committed result after the abort.
+ await settled.promise
+ await outcome
+ await vi.waitFor(() => expect(app.observations[0].request.destroyed).toBe(true))
+ const recovered = await send(app.url, headers('after-late-commit-gone-host'), '{}').response
+ expect(recovered.status).toBe(200)
+ expect(authorizer.authorizeLlmProviderAttempt).toHaveBeenCalledTimes(2)
+ expect(app.observations[0].responseWritesAfterClose).toBe(0)
+ })
+ })
+
it('stops a stalled body reader on the real read clock before releasing and admits the next request', async () => {
const owner = observeHeldOwner()
const nativeSetTimeout = globalThis.setTimeout
diff --git a/docs/architecture/codex-subscription-transport-contract.md b/docs/architecture/codex-subscription-transport-contract.md
index 0d19c1f48..dac94d272 100644
--- a/docs/architecture/codex-subscription-transport-contract.md
+++ b/docs/architecture/codex-subscription-transport-contract.md
@@ -148,6 +148,11 @@ read and send timeouts to 90 s, above the 80 s a retained authorize can take
(queue, read and work), so control-api's own answer reaches the Host. A bare
504 from the gateway (a read or send timeout on an accepted connection) is
read as `authorize_timeout`, never as a provider outage.
+A resolved authorize has committed its ticket and budget reservation, so it is
+answered 200 while the client is still connected, even when the work clock
+expired meanwhile. An `authorize_timeout` or a gateway 504 does not prove a
+rollback: when the COMMIT reply itself is lost, the ticket stays until its own
+TTL and the reservation until its TTL, about 36 min.
The critical Codex test title migrated from "visual admission overflow answers
503 provider_unavailable and logs visual_gate" to "visual admission overflow
diff --git a/docs/architecture/grok-subscription-transport-contract.md b/docs/architecture/grok-subscription-transport-contract.md
index 9182948a6..f3147111d 100644
--- a/docs/architecture/grok-subscription-transport-contract.md
+++ b/docs/architecture/grok-subscription-transport-contract.md
@@ -272,6 +272,11 @@ Errors:
(queue, read and work), so control-api's own answer reaches the Host. A bare
504 from the gateway (a read or send timeout on an accepted connection) is
read as `authorize_timeout`, never as a provider outage.
+ A resolved authorize has committed its ticket and budget reservation, so it
+ is answered 200 while the client is still connected, even when the work
+ clock expired meanwhile. An `authorize_timeout` or a gateway 504 does not
+ prove a rollback: when the COMMIT reply itself is lost, the ticket stays
+ until its own TTL and the reservation until its TTL, about 36 min.
Desktop. The composer budget for `grok-subscription` is 16 MiB per image and
16 MiB decoded in total, with no dimension bound. That is the shared rpc-proxy