Skip to content

The agent that grows with you

5.8 KB
Raw Blame
/**
 * Pure routing helpers for the contrib wiring controller.
 *
 * Kept out of wiring.tsx so they can be unit-tested without importing the whole
 * React/Electron controller module.
 */

import type { SessionOwnerRoute, SessionOwnerScope } from '@/store/session-request-router'


/**
 * Resolve a runtime session id back to its stored id by reverse-scanning the
 * stored->runtime binding map — the same ladder use-session-tile-delegate's
 * `storedSessionIdForRuntime` uses. Returns undefined when the id isn't a known
 * runtime id, so the caller can treat it as already a stored id.
 */
export function findStoredIdForRuntimeId(bindings: Map<string, string>, runtimeId: string): string | undefined {
  for (const [storedId, mapped] of bindings) {
    if (mapped === runtimeId) {
      return storedId
    }
  }

  return undefined
}

/**
 * The stored session id a session-scoped RPC should route by.
 *
 * Route by the session the RPC TARGETS (its `session_id` param), not by the
 * window's focused tile: `requestGateway` is one shared closure for every
 * session RPC, so keying off the focused tile sent a non-focused tile's RPC
 * (a bot chat while another pane is active) to the focused tile's backend — the
 * Bot Mode misroute. `session_id` is a RUNTIME id while tiles/rows key on the
 * STORED id, so translate via the state cache, then the reverse binding scan;
 * an unknown id is already a stored id (several RPCs pass stored ids directly).
 * With no `session_id` at all (ambient/config calls) fall back to the focused
 * then selected tile.
 */
export function resolveRoutingSessionId(args: {
  paramSessionId: string | undefined
  storedIdForRuntime: (runtimeId: string) => string | undefined
  focusedStoredSessionId: null | string
  selectedStoredSessionId: null | string
}): null | string {
  const { focusedStoredSessionId, paramSessionId, selectedStoredSessionId, storedIdForRuntime } = args

  if (paramSessionId) {
    return storedIdForRuntime(paramSessionId) ?? paramSessionId
  }

  return focusedStoredSessionId ?? selectedStoredSessionId
}

/** The owner shapes the ladder below can return: an exact route (connection +
 *  profile), a bare profile name, or undefined (unknown — probe, never
 *  "active"). The type-only import keeps this module runtime-import-free. */
export type SessionRpcOwnerRoute = SessionOwnerRoute

/** A connection-bearing owner. Checked structurally so this module stays
 *  runtime-import-free (the type guard lives in store/session-request-router). */
const isRouteOwner = (owner: unknown): owner is SessionRpcOwnerRoute =>
  Boolean(owner && typeof owner === 'object' && 'connectionId' in owner)

/**
 * The SYNC owner a session-scoped RPC routes to, resolved in this order:
 *
 *   1. the persisted tile owner route (a bot chat / split tile records the
 *      exact connectionId + profile it was opened with, survives relaunch);
 *   2. the exact, UNIQUE session owner hint (recorded the moment a routed
 *      session.create returns, or at plugin open time; persisted, bounded);
 *   3. the session row's owner — an EXACT route when the row is
 *      connection-tagged (optimistic row from a routed create, the unified
 *      list splice, or a tag carried across a refresh), else its bare
 *      profile (the cross-profile aggregator tags rows, but a bare profile
 *      loses the connection and can lag the create);
 *   4. `eventOwner`, an exact route an inbound runtime event already proved
 *      for this session (store/session-states sessionOwnerByRuntimeId). It is
 *      consulted whenever every rung above named nothing better than a bare
 *      profile name, because a bare profile has NO connection: the profile
 *      door (requestGatewayForProfile → gatewayForProfile) resolves a bare
 *      name equal to the primary profile against the PRIMARY socket, so an
 *      ordinary session that runs on a non-primary connection — two
 *      connections both exposing `default` is enough — has its RPCs answered
 *      by another machine ("4001 session not found" from the 2nd prompt on);
 *   5. undefined → the caller runs the cross-profile probe, and fails closed
 *      if that misses too.
 *
 * An EXACT durable route (1–3 tagged, or 4) always outranks a bare profile
 * name; only a genuinely unknown owner falls through.
 *
 * The hint outranks the row because the row is presentation state that can
 * be stamped from the AMBIENT profile (an optimistic row minted while
 * All-profiles / Bot routing left `default` active), and because it carries
 * no connection: a fresh chat created on `local::omar` whose row read
 * `default` ran its first turn on omar and then 4001'd "session not found"
 * on the second, when the row's `default` owner won the route. The
 * connection-tagged row rung is what keeps two-turn continuity from resting
 * on the transient hint alone (bounded, evictable, gone after a relaunch).
 */
export function resolveSessionRpcOwner(args: {
  routingSessionId: null | string
  tileOwnerRoute: (storedSessionId: string) => SessionRpcOwnerRoute | undefined
  sessionOwnerHint: (storedSessionId: string) => SessionRpcOwnerRoute | undefined
  sessionRowOwner: (storedSessionId: string) => null | SessionRpcOwnerRoute | string | undefined
  eventOwner?: (storedSessionId: string) => SessionOwnerScope | undefined
}): SessionRpcOwnerRoute | string | undefined {
  const { eventOwner, routingSessionId, sessionOwnerHint, sessionRowOwner, tileOwnerRoute } = args

  if (!routingSessionId) {
    return undefined
  }

  const fromRow = sessionRowOwner(routingSessionId)
  const rowOwner = typeof fromRow === 'string' ? fromRow.trim() || undefined : (fromRow ?? undefined)

  const durable = tileOwnerRoute(routingSessionId) ?? sessionOwnerHint(routingSessionId) ?? rowOwner

  if (isRouteOwner(durable)) {
    return durable
  }

  return eventOwner?.(routingSessionId) ?? durable
}