UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

56 lines (55 loc) • 3.5 kB
/** * The leg's context room: how many more tokens one model reply's tool calls may still pull into the * context before it crosses the compaction point. Read's `allow_large` raises its size limits up to this * room, and books what it served against it, so two raised reads in the same reply share one budget. * * One seat per leg. `open(toolCallId)` snapshots the conversation once and finds the assistant message * that issued the call (its parent); the ledger is keyed by that message, so a new reply starts a new * ledger. The room of a call is * * max(0, floor((compactAt − (held + pending)) × 0.7)) * * where `compactAt` is the leg's compaction point (the autocompact window minus the repaired reserve — * read whether or not compaction is switched on), `held` is the larger of the parent message's billed * usage and the measured occupancy of the whole context (the same `measureContext` the compaction trigger * reads), and `pending` is what raised reads of this reply booked whose results the snapshot does not yet * hold. A result already in the context is counted once, inside `held`; one still in flight is counted * once, in `pending` — so a ledger lost with a process restart loses nothing the context does not * already show. * * No room (`undefined`): the call has no parent assistant message in the session (a host-issued call), or * the window is unknown — the latter is reported once through `onNoWindow`. */ import { type AgentMessage, type CompactionSettings } from "../../internal/harness.js"; import type { Model } from "../../internal/llm.js"; import { type UnevaluableWindow } from "../auto-compaction.js"; import type { ContextWindowOverlay } from "../context-settings.js"; /** The share of the measured headroom a read may take (the rest is left for the reply that reads it). */ export declare const CONTEXT_ROOM_SHARE = 0.7; /** One tool call's view of the room. `room()` and `claim()` are synchronous: a read rechecks and books with no await between. */ export interface ContextRoomTicket { /** The room this call may still take, as of now. */ room(): number; /** Book `tokens` this call served against the reply's room. */ claim(tokens: number): void; /** The model's characters-per-token coefficient the room was measured with (the read side measures with the same one). */ readonly charsPerToken: number; } export interface ContextRoomSeat { /** This call's ticket on its reply's ledger; `undefined` = there is no room to measure where this call runs. */ open(toolCallId: string | undefined): Promise<ContextRoomTicket | undefined>; } export interface ContextRoomSeatInput { /** The session context as the next request would carry it, read at the call. */ readonly messages: () => Promise<AgentMessage[]>; /** The model the leg is on now (its window and structural coefficient). */ readonly model: () => Model; readonly contextOverlay: () => ContextWindowOverlay | undefined; /** The leg's compaction settings (their `enabled` is not read). */ readonly settings: () => CompactionSettings; /** The fixed prompt overhead the compaction trigger adds to an anchor-less estimate. */ readonly overheadTokens: () => number | undefined; /** Called when a ticket is asked for on a window the measurement cannot use. */ readonly onNoWindow?: (u: UnevaluableWindow) => void; } export declare function createContextRoomSeat(input: ContextRoomSeatInput): ContextRoomSeat;