Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.2 KB
Raw Blame
// Cache derived keys per frozen record to avoid repeating work on each evidence edge, alias validation and retired-binding lookup.

import {
  serializeAgentChildWorkAliasKey,
  type AgentChildWorkAliasInput,
  type AgentChildWorkAliasRecord
} from './agent-status-child-work-alias'
import {
  deserializeAgentChildWorkBindingKey,
  serializeAgentChildWorkBindingKey
} from './agent-status-child-work-binding'
import type { AgentStatusTombstoneRecord } from './agent-status-store-contract'
import { deserializeAgentStatusFactKey } from './agent-status-store-fact-codec'
import { deserializeAgentStatusSubject } from './agent-status-subject'

function memoized<R extends object, V extends {} | null>(
  cache: WeakMap<R, V>,
  record: R,
  derive: (record: R) => V
): V {
  const cached = cache.get(record)
  if (cached !== undefined) {
    return cached
  }
  const value = derive(record)
  if (Object.isFrozen(record)) {
    cache.set(record, value)
  }
  return value
}

const bindingKeys = new WeakMap<AgentChildWorkAliasRecord, string>()
type RetiredAlias = { alias: AgentChildWorkAliasInput; key: string }
const retiredAliases = new WeakMap<AgentStatusTombstoneRecord, RetiredAlias | null>()

export function storedBindingKey(alias: AgentChildWorkAliasRecord): string {
  return memoized(bindingKeys, alias, serializeAgentChildWorkBindingKey)
}

/** The binding a retired alias's tombstone names, with its alias key; null for any other tombstone. */
export function storedRetiredAlias(tombstone: AgentStatusTombstoneRecord): RetiredAlias | null {
  return memoized(retiredAliases, tombstone, (record) => {
    const alias = record.entity === 'alias' ? deserializeAgentChildWorkBindingKey(record.key) : null
    return alias ? { alias, key: serializeAgentChildWorkAliasKey(alias) } : null
  })
}

export function tombstoneKeyIsValid(tombstone: AgentStatusTombstoneRecord): boolean {
  switch (tombstone.entity) {
    case 'parent':
      return deserializeAgentStatusSubject(tombstone.key) !== null
    case 'alias':
      return deserializeAgentChildWorkBindingKey(tombstone.key) !== null
    case 'fact':
      return deserializeAgentStatusFactKey(tombstone.key) !== null
    case 'child':
      return true
  }
}