Files
orca/src/shared/agent-hook-status-cache.ts
Brennan Benson 9ab0a18e82 refactor(agent-status): isolate legacy status ingress behind one admission point (#20716)
* refactor(agent-status): isolate legacy status ingress

* fix(agent-hooks): move advertised-capability source onto the ingest envelope

ingestRemote() gained a third positional argument in this PR
(advertisedAgentStatusCapabilities) to satisfy a new ratchet requiring
every legacy-ingress call site to name its capability source. Both
production callers pass the same constant every time, so the argument
carries zero runtime information — but Vitest's toHaveBeenCalledWith
matches argument count exactly, so the pre-existing SSH relay
integration test (which asserts a 2-argument call) started failing
even though nothing about the actual admission decision changed.

Capabilities are a property of the producing peer/connection, not an
orthogonal call parameter, so move the field onto the envelope object
instead of adding a third positional argument: ingestRemote reads
envelope.advertisedAgentStatusCapabilities (defaulting to the
unadvertised-legacy-peer set), and both call sites stamp the constant
onto their envelope literal. Call arity stays at two arguments, so the
pre-existing evidence test needs no change.

The envelope never crosses the wire in either caller: SSH rebuilds it
field-by-field from the RPC params, and the WSL path copies (never
mutates) the wire-deserialized notification before stamping the field
on, so this is purely an internal main-process shape change.

Also strengthens the ingress ratchet test that required this: it
previously only checked that the capability constant's name appeared
somewhere in each caller's source, which a stray unused import could
satisfy. It now asserts the actual
`advertisedAgentStatusCapabilities: AGENT_STATUS_LEGACY_UNADVERTISED_PEER_CAPABILITIES`
key:value binding is present.
2026-09-15 10:26:44 -07:00

81 lines
2.3 KiB
TypeScript

import {
admitLegacyAgentStatus,
clearPaneCacheState,
type HookListenerState
} from './agent-hook-listener/listener-state'
import type { AgentHookEventPayload } from './agent-hook-listener/listener-event'
import { AGENT_STATUS_2A_CURRENT_PRODUCER_MODE } from './agent-status-legacy-adapter'
import { AGENT_STATUS_STALE_AFTER_MS } from './agent-status-types'
export const MAX_AGENT_HOOK_STATUS_CACHE_PANES = 500
export type AgentHookStatusCacheEviction = {
paneKey: string
entry: AgentHookEventPayload
}
export function upsertBoundedAgentHookStatus(
state: HookListenerState,
entry: AgentHookEventPayload,
options: { maxPanes?: number; now?: number } = {}
): AgentHookStatusCacheEviction[] {
const maxPanes = options.maxPanes ?? MAX_AGENT_HOOK_STATUS_CACHE_PANES
if (!Number.isSafeInteger(maxPanes) || maxPanes < 1) {
throw new RangeError('Agent hook status cache limit must be a positive safe integer')
}
if (
!admitLegacyAgentStatus(
state,
'shared-bounded-status-cache',
entry,
AGENT_STATUS_2A_CURRENT_PRODUCER_MODE,
{ moveToEnd: true }
)
) {
return []
}
const evicted: AgentHookStatusCacheEviction[] = []
const now = options.now ?? Date.now()
while (state.lastStatusByPaneKey.size > maxPanes) {
const paneKey = selectEvictionCandidate(state, entry.paneKey, now)
if (!paneKey) {
break
}
const cached = state.lastStatusByPaneKey.get(paneKey)
if (!cached) {
break
}
evicted.push({ paneKey, entry: cached })
clearPaneCacheState(state, paneKey)
}
return evicted
}
function selectEvictionCandidate(
state: HookListenerState,
currentPaneKey: string,
now: number
): string | undefined {
let oldestFallback: string | undefined
for (const [paneKey, entry] of state.lastStatusByPaneKey) {
if (paneKey === currentPaneKey) {
continue
}
oldestFallback ??= paneKey
if (entry.payload.state === 'done' || isStaleStatus(entry, now)) {
return paneKey
}
}
return oldestFallback
}
function isStaleStatus(entry: AgentHookEventPayload, now: number): boolean {
const receivedAt = (entry as AgentHookEventPayload & { receivedAt?: unknown }).receivedAt
return (
typeof receivedAt === 'number' &&
Number.isFinite(receivedAt) &&
now - receivedAt > AGENT_STATUS_STALE_AFTER_MS
)
}