Files
orca/src/shared/agent-hook-status-cache.ts
T
NeilandOrca 879aad7dd6 oom(foundation): bound shared readers/limits + add BoundedMap primitive (#10299)
* oom(01): A1-shared-readers — reintroduce #10179 subset

Files: 18 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(02): A2-shared-image-media — reintroduce #10179 subset

Files: 7 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(03): A3-shared-fs-listing — reintroduce #10179 subset

Files: 21 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(04): A4-shared-remote-relay — reintroduce #10179 subset

Files: 8 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(05): A5-shared-misc — reintroduce #10179 subset

Files: 28 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(06): B-shared-wiring — reintroduce #10179 subset

Files: 81 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-24 21:36:57 -07:00

70 lines
2.0 KiB
TypeScript

import {
clearPaneCacheState,
type AgentHookEventPayload,
type HookListenerState
} from './agent-hook-listener'
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')
}
state.lastStatusByPaneKey.delete(entry.paneKey)
state.lastStatusByPaneKey.set(entry.paneKey, entry)
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
)
}