Skip to content

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

TypeScript

refactor(rpc-proxy): name the wake capacity limit for what it actually counts

Alfredo Lopez9a9de8289821
diff --git a/rpc-proxy/src/services/__tests__/wakeAndHold.test.ts b/rpc-proxy/src/services/__tests__/wakeAndHold.test.ts
index 61a32b2a9..0f5c887c2 100644
--- a/rpc-proxy/src/services/__tests__/wakeAndHold.test.ts
+++ b/rpc-proxy/src/services/__tests__/wakeAndHold.test.ts
@@ -33,7 +33,7 @@ function makeCoordinator(overrides?: Partial<WakeCoordinatorDeps>): {
maxHoldMs: MAX_HOLD_MS,
pollMs: POLL_MS,
retriggerMs: RETRIGGER_MS,
- maxTrackedHosts: 1_000,
+ maxTrackedCoordinations: 1_000,
...overrides,
})
return { coordinator, requestWake, probeReady }
@@ -95,7 +95,7 @@ describe('WakeAndHoldCoordinator branch matrix', () => {
expect(requestWake).toHaveBeenCalledTimes(1)
expect(requestWake).toHaveBeenCalledWith('chatllm', 'rpc-token')
expect(probeReady).not.toHaveBeenCalled()
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('200 active with wakeGeneration (drain-cancel) also resolves proceed immediately', async () => {
@@ -105,7 +105,7 @@ describe('WakeAndHoldCoordinator branch matrix', () => {
const outcome = await coordinator.hold(holdParams())
expect(outcome.kind).toBe('proceed')
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('202 wake-requested holds, polls readiness, and proceeds when the host is up', async () => {
@@ -116,13 +116,13 @@ describe('WakeAndHoldCoordinator branch matrix', () => {
const pending = coordinator.hold(holdParams())
await vi.advanceTimersByTimeAsync(POLL_MS) // first probe: not ready
expect(probeReady).toHaveBeenCalledTimes(1)
- expect(coordinator.trackedHostCount()).toBe(1)
+ expect(coordinator.trackedCoordinationCount()).toBe(1)
await vi.advanceTimersByTimeAsync(POLL_MS) // second probe: ready
const outcome = await pending
expect(outcome).toEqual({ kind: 'proceed', lastKnownState: 'wake-requested' })
expect(probeReady).toHaveBeenCalledTimes(2)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('409 not-stateless resolves legacy (today error path)', async () => {
@@ -164,7 +164,7 @@ describe('WakeAndHoldCoordinator branch matrix', () => {
const outcome = await coordinator.hold(holdParams())
expect(outcome).toEqual({ kind: 'legacy', reason: 'wake-endpoint-unreachable' })
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
})
@@ -192,7 +192,7 @@ describe('bounded hold', () => {
lastKnownState: 'wake-requested',
})
// Waiter release evicts the whole entry: no residual poll/retrigger/TTL timers.
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
})
@@ -208,7 +208,7 @@ describe('token TTL', () => {
const outcome = await pending
expect(outcome).toMatchObject({ kind: 'waking', reason: 'token-expiring' })
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('rejects an already-expiring token synchronously without calling the wake endpoint', async () => {
@@ -232,7 +232,7 @@ describe('local dedup', () => {
coordinator.hold(holdParams()),
coordinator.hold(holdParams()),
]
- expect(coordinator.trackedHostCount()).toBe(1)
+ expect(coordinator.trackedCoordinationCount()).toBe(1)
await vi.advanceTimersByTimeAsync(POLL_MS) // shared probe #1
expect(probeReady).toHaveBeenCalledTimes(1)
@@ -244,11 +244,11 @@ describe('local dedup', () => {
}
expect(requestWake).toHaveBeenCalledTimes(1)
expect(probeReady).toHaveBeenCalledTimes(2)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('evicts the oldest host loudly when the tracked-host cap is exceeded', async () => {
- const { coordinator, requestWake, probeReady } = makeCoordinator({ maxTrackedHosts: 2 })
+ const { coordinator, requestWake, probeReady } = makeCoordinator({ maxTrackedCoordinations: 2 })
requestWake.mockResolvedValue({ kind: 'wake-requested', wakeGeneration: 1 })
probeReady.mockResolvedValue(false)
@@ -256,19 +256,19 @@ describe('local dedup', () => {
await vi.advanceTimersByTimeAsync(1)
const second = coordinator.hold(holdParams({ hostRef: 'host-b' }))
await vi.advanceTimersByTimeAsync(1)
- expect(coordinator.trackedHostCount()).toBe(2)
+ expect(coordinator.trackedCoordinationCount()).toBe(2)
const third = coordinator.hold(holdParams({ hostRef: 'host-c' }))
const firstOutcome = await first
expect(firstOutcome).toMatchObject({ kind: 'waking', reason: 'capacity-evicted' })
- expect(coordinator.trackedHostCount()).toBe(2)
+ expect(coordinator.trackedCoordinationCount()).toBe(2)
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('wake-hold capacity exceeded'))
// Release the survivors so afterEach can assert zero dangling timers.
await vi.advanceTimersByTimeAsync(MAX_HOLD_MS)
await Promise.all([second, third])
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('waiter-deadline expiry evicts an empty entry (TTL semantics), stopping all loops', async () => {
@@ -280,7 +280,7 @@ describe('local dedup', () => {
await vi.advanceTimersByTimeAsync(MAX_HOLD_MS)
await pending
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
// Poll/retrigger loops stopped with the entry: advancing time makes no calls.
const probeCalls = probeReady.mock.calls.length
const wakeCalls = requestWake.mock.calls.length
@@ -329,7 +329,7 @@ describe('kill-switch and poll-source failure while held', () => {
expect(requestWake).toHaveBeenCalledTimes(1)
// ...and the hold survives: the entry is still tracked, not settled, so the
// readiness poll can still release this waiter.
- expect(coordinator.trackedHostCount()).toBe(1)
+ expect(coordinator.trackedCoordinationCount()).toBe(1)
// Drain: let the hold reach its deadline so the pending promise resolves.
await vi.advanceTimersByTimeAsync(MAX_HOLD_MS)
@@ -348,7 +348,7 @@ describe('kill-switch and poll-source failure while held', () => {
const outcome = await pending
expect(outcome).toEqual({ kind: 'legacy', reason: 'not-stateless' })
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('releases held requests when the host becomes ready mid-hold', async () => {
@@ -373,7 +373,7 @@ describe('kill-switch and poll-source failure while held', () => {
const outcome = await pending
expect(outcome).toMatchObject({ kind: 'waking', reason: 'max-hold-exceeded' })
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('a failing retrigger keeps the hold alive until the deadline instead of crashing it', async () => {
@@ -513,7 +513,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
await pending
expect(attemptUpstream).toHaveBeenCalledTimes(1)
expect(res.statusCode).toBe(200)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
const wakeCalls = requestWake.mock.calls.length
const probeCalls = probeReady.mock.calls.length
@@ -522,7 +522,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
expect(attemptUpstream).toHaveBeenCalledTimes(1) // ZERO further upstream forwards
expect(requestWake.mock.calls.length).toBe(wakeCalls)
expect(probeReady.mock.calls.length).toBe(probeCalls)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('(b) after a terminal (non-availability) failure resolves the request, no artifact re-forwards', async () => {
@@ -551,7 +551,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
expect(attemptUpstream).toHaveBeenCalledTimes(1)
expect(respondLegacy).toHaveBeenCalledTimes(1)
expect(res.statusCode).toBe(502)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
const wakeCalls = requestWake.mock.calls.length
await vi.advanceTimersByTimeAsync(MAX_HOLD_MS * 3)
@@ -577,7 +577,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
await pending
expect(attemptUpstream).toHaveBeenCalledTimes(3) // pre-resolution retries are the feature
expect(res.statusCode).toBe(200)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('(d) two concurrent holds for different hosts do not cross-cancel each other', async () => {
@@ -620,7 +620,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
// B is untouched by A's resolution: still held, still polling/retriggering.
expect(attemptB).not.toHaveBeenCalled()
expect(resB.headersSent).toBe(false)
- expect(coordinator.trackedHostCount()).toBe(1)
+ expect(coordinator.trackedCoordinationCount()).toBe(1)
await vi.advanceTimersByTimeAsync(RETRIGGER_MS)
const wakeCallsForB = requestWake.mock.calls.filter(call => call[0] === 'host-b').length
@@ -632,7 +632,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
expect(attemptB).toHaveBeenCalledTimes(1)
expect(resB.statusCode).toBe(200)
expect(attemptA).toHaveBeenCalledTimes(1) // and B's resolution never re-forwarded A
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('incident shape: a wake-eligible error thrown AFTER the 200 was committed never re-forwards', async () => {
@@ -655,7 +655,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('wake-hold post-response failure suppressed')
)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('refuses to park a request whose response is already committed, loudly and without waking', async () => {
@@ -671,7 +671,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('wake-hold refused: response already committed')
)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('a readiness probe landing after resolution no-ops loudly instead of re-settling', async () => {
@@ -698,7 +698,7 @@ describe('post-resolution re-forward hardening (respondWithWakeAndHold)', () =>
await pending
expect(attemptUpstream).toHaveBeenCalledTimes(1)
expect(res.statusCode).toBe(200)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
// The in-flight probe finally lands AFTER resolution: loud no-op, no re-forward.
expect(resolveProbe).not.toBeNull()
diff --git a/rpc-proxy/src/services/__tests__/wakeCoordinator.principal.test.ts b/rpc-proxy/src/services/__tests__/wakeCoordinator.principal.test.ts
index 956b14569..3ad2db1d7 100644
--- a/rpc-proxy/src/services/__tests__/wakeCoordinator.principal.test.ts
+++ b/rpc-proxy/src/services/__tests__/wakeCoordinator.principal.test.ts
@@ -53,7 +53,7 @@ function makeCoordinator(overrides?: Partial<WakeCoordinatorDeps>): {
maxHoldMs: MAX_HOLD_MS,
pollMs: POLL_MS,
retriggerMs: RETRIGGER_MS,
- maxTrackedHosts: 1_000,
+ maxTrackedCoordinations: 1_000,
...overrides,
})
return { coordinator, requestWake, probeReady }
@@ -123,7 +123,7 @@ describe('§13.3 wake coordination matrix', () => {
})
const pB = hold(coordinator, nonWakeLater, 'tok-nonwake')
- expect(coordinator.trackedHostCount()).toBe(1) // same principal group
+ expect(coordinator.trackedCoordinationCount()).toBe(1) // same principal group
expect(requestWake).toHaveBeenCalledTimes(1) // the non-wake waiter triggered NO new wake
expect(requestWake).not.toHaveBeenCalledWith('chatllm', 'tok-nonwake')
@@ -160,7 +160,7 @@ describe('§13.3 wake coordination matrix', () => {
const t2 = makeClaims({ accessScope: 'team', teamId: 'team-2' })
const p1 = hold(coordinator, t1, 'tok-t1')
const p2 = hold(coordinator, t2, 'tok-t2')
- expect(coordinator.trackedHostCount()).toBe(2)
+ expect(coordinator.trackedCoordinationCount()).toBe(2)
expect(requestWake).toHaveBeenCalledTimes(2)
coordinator.drain()
await Promise.all([p1, p2])
@@ -172,7 +172,7 @@ describe('§13.3 wake coordination matrix', () => {
const b = makeClaims({ sub: 'user-b' })
const pa = hold(coordinator, a, 'tok-a')
const pb = hold(coordinator, b, 'tok-b')
- expect(coordinator.trackedHostCount()).toBe(2)
+ expect(coordinator.trackedCoordinationCount()).toBe(2)
expect(requestWake).toHaveBeenCalledTimes(2)
coordinator.drain()
await Promise.all([pa, pb])
@@ -184,7 +184,7 @@ describe('§13.3 wake coordination matrix', () => {
const outcome = await hold(coordinator, nonWake, 'tok-nonwake')
expect(outcome.kind).toBe('legacy')
expect(requestWake).not.toHaveBeenCalled()
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('key is delimiter-safe: boundary-shifting identifiers cannot collide two principals', () => {
@@ -204,7 +204,7 @@ describe('§13.3 wake coordination matrix', () => {
lastKnownState: 'unknown',
})
expect(requestWake).not.toHaveBeenCalled()
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('row 6: expired wake authorization + non-wake waiter → deterministic terminal, no wake', async () => {
@@ -221,7 +221,7 @@ describe('§13.3 wake coordination matrix', () => {
await vi.advanceTimersByTimeAsync(2_000) // short wake token budget expires
expect((await pShort).kind).toBe('waking')
- expect(coordinator.trackedHostCount()).toBe(1) // entry persists for the long waiter
+ expect(coordinator.trackedCoordinationCount()).toBe(1) // entry persists for the long waiter
await vi.advanceTimersByTimeAsync(2_000) // now past the wake token's exp
const late = makeClaims({ scopes: ['host:session:read'], expMs: Date.now() + 3_600_000 })
@@ -241,7 +241,7 @@ describe('§13.3 wake coordination matrix', () => {
hold(coordinator, a, 'tok-2'),
hold(coordinator, a, 'tok-3'),
]
- expect(coordinator.trackedHostCount()).toBe(1)
+ expect(coordinator.trackedCoordinationCount()).toBe(1)
expect(requestWake).toHaveBeenCalledTimes(1) // one wake sequence for the principal group
probeReady.mockResolvedValue(true)
@@ -249,7 +249,7 @@ describe('§13.3 wake coordination matrix', () => {
for (const outcome of await Promise.all(pendings)) {
expect(outcome.kind).toBe('proceed')
}
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('row 8: waiter settles, a later probe failure does not re-settle', async () => {
@@ -259,13 +259,13 @@ describe('§13.3 wake coordination matrix', () => {
const pending = hold(coordinator, makeClaims(), 'tok-1')
await vi.advanceTimersByTimeAsync(POLL_MS)
expect((await pending).kind).toBe('proceed')
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
// Advancing past every window makes NO further probe/settle — group is gone.
const probeCalls = probeReady.mock.calls.length
await vi.advanceTimersByTimeAsync(MAX_HOLD_MS * 2)
expect(probeReady.mock.calls.length).toBe(probeCalls)
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
})
@@ -283,7 +283,7 @@ describe('§13.4 operation outcomes (unit level)', () => {
requestWake.mockResolvedValue({ kind: 'not-stateless' })
const outcome = await hold(coordinator, makeClaims(), 'tok')
expect(outcome).toEqual({ kind: 'legacy', reason: 'not-stateless' })
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('suspended + no wake grant → no fallback grant; deterministic legacy result', async () => {
@@ -300,7 +300,7 @@ describe('a mid-hold wake auth rejection distinguishes initial from retrigger',
requestWake.mockResolvedValue({ kind: 'auth', status: 403 })
const outcome = await hold(coordinator, makeClaims(), 'tok')
expect(outcome).toEqual({ kind: 'legacy', reason: 'wake-auth-403' })
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
it('a retrigger wake 403 does NOT collapse still-valid waiters (initial guard)', async () => {
@@ -313,12 +313,12 @@ describe('a mid-hold wake auth rejection distinguishes initial from retrigger',
const pending = hold(coordinator, makeClaims(), 'tok')
await vi.advanceTimersByTimeAsync(RETRIGGER_MS) // fire one retrigger (auth 403)
// The group is still held (bounded by deadlines), not collapsed to legacy.
- expect(coordinator.trackedHostCount()).toBe(1)
+ expect(coordinator.trackedCoordinationCount()).toBe(1)
await vi.advanceTimersByTimeAsync(MAX_HOLD_MS)
const outcome = await pending
expect(outcome).toMatchObject({ kind: 'waking' })
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
})
@@ -335,13 +335,13 @@ describe('shutdown drain', () => {
conn('other'),
'other'
)
- expect(coordinator.trackedHostCount()).toBe(2)
+ expect(coordinator.trackedCoordinationCount()).toBe(2)
coordinator.drain()
const [oa, ob] = await Promise.all([a, b])
expect(oa.kind).toBe('waking')
expect(ob.kind).toBe('waking')
- expect(coordinator.trackedHostCount()).toBe(0)
+ expect(coordinator.trackedCoordinationCount()).toBe(0)
})
})
diff --git a/rpc-proxy/src/services/wakeAndHold.ts b/rpc-proxy/src/services/wakeAndHold.ts
index 8deb30309..a60c3a9d1 100644
--- a/rpc-proxy/src/services/wakeAndHold.ts
+++ b/rpc-proxy/src/services/wakeAndHold.ts
@@ -29,12 +29,15 @@ import { forwardHostHealth } from './mcpHostRestService.js'
const TOKEN_EXP_SAFETY_MARGIN_MS = 2_000
const ENTRY_TTL_MARGIN_MS = 5_000
/**
- * Capacity of the coordination map. Entries are per (principal, host) — a
- * fleet of H hosts with P concurrently-holding principals per host consumes
- * up to H×P slots (review F2). Oldest-first eviction settles the victim
+ * Capacity of the coordination map, counted in COORDINATIONS, not hosts. The
+ * map is keyed by `wakeCoordinationKey(claims, hostRef)` = principal × Host, so
+ * N distinct principals waking ONE Host consume N slots: a fleet of H hosts
+ * with P concurrently-holding principals per host consumes up to H×P slots
+ * (review F2). Named for what it bounds — a "hosts" name would understate the
+ * real limit by a factor of P. Oldest-first eviction settles the victim
* retryable (503 host_waking), so overflow degrades politely.
*/
-const MAX_TRACKED_PRINCIPAL_HOSTS = 1_000
+const MAX_TRACKED_WAKE_COORDINATIONS = 1_000
const WAKE_SCOPE = 'host:wake:write' as const
/** Short upstream retry schedule after a wake reports the host is up. */
const PROCEED_RETRY_DELAYS_MS = [0, 250, 1_000]
@@ -92,8 +95,11 @@ export type WakeCoordinatorDeps = {
maxHoldMs: number
pollMs: number
retriggerMs: number
- /** Capacity in (principal, host) entries — see MAX_TRACKED_PRINCIPAL_HOSTS. */
- maxTrackedHosts: number
+ /**
+ * Capacity in (principal, host) coordination entries — NOT in distinct hosts.
+ * See MAX_TRACKED_WAKE_COORDINATIONS.
+ */
+ maxTrackedCoordinations: number
now?: () => number
}
@@ -142,8 +148,11 @@ export class WakeAndHoldCoordinator {
constructor(private readonly deps: WakeCoordinatorDeps) {}
- /** Exposed for tests and observability. */
- trackedHostCount(): number {
+ /**
+ * Live (principal, host) coordination entries — NOT a count of distinct
+ * hosts. Exposed for tests and observability.
+ */
+ trackedCoordinationCount(): number {
return this.entries.size
}
@@ -280,14 +289,14 @@ export class WakeAndHoldCoordinator {
}
private enforceCapacity(): void {
- if (this.entries.size < this.deps.maxTrackedHosts) return
+ if (this.entries.size < this.deps.maxTrackedCoordinations) return
let oldest: HostWakeEntry | null = null
for (const candidate of this.entries.values()) {
if (!oldest || candidate.createdAt < oldest.createdAt) oldest = candidate
}
if (!oldest) return
console.warn(
- `[RPC_PROXY] wake-hold capacity exceeded (max=${this.deps.maxTrackedHosts} principal-scoped entries) — evicting oldest entry host=${oldest.hostRef} waiters=${oldest.waiters.size}`
+ `[RPC_PROXY] wake-hold capacity exceeded (max=${this.deps.maxTrackedCoordinations} principal-scoped entries) — evicting oldest entry host=${oldest.hostRef} waiters=${oldest.waiters.size}`
)
this.settle(oldest, {
kind: 'waking',
@@ -551,7 +560,7 @@ export const hostWakeCoordinator = new WakeAndHoldCoordinator({
maxHoldMs: config.wakeMaxHoldMs,
pollMs: config.wakePollMs,
retriggerMs: config.wakeRetriggerMs,
- maxTrackedHosts: MAX_TRACKED_PRINCIPAL_HOSTS,
+ maxTrackedCoordinations: MAX_TRACKED_WAKE_COORDINATIONS,
})
export type RespondWithWakeAndHoldOptions = {