/** * Presentation-facing readings of what the model is currently carrying. * * Two authorities, deliberately kept apart because they answer different * questions and cost different amounts: * * - the `contextPressure` and `contextBreakdown` session projections are O(1) * folds Harness already maintains, so occupancy and composition are free to * read on every frame; * - `ctx.tokenMeter.measure(session)` prices every node of the current surface * and clones the result, which its own contract states is O(surface) per * call. That is the X-ray, and nothing but an open inspector may ask for it. * * Nothing here knows about a terminal or a Context, so all of it is testable * directly. * @module dshline/context/model */ import type { Session } from '@deepseek-ai/dsh-session'; import { SessionSeq } from '@deepseek-ai/dsh-session'; import type { ProjectionSnapshot } from '@deepseek-ai/dsh-session-projection'; import type { TokenMeasurement } from '@deepseek-ai/dsh-token-meter'; /** * How full the model's context is, as the authoritative projection states it. * * `tokens` is `projectedTokens`, which upstream defines as what the NEXT * request's prompt would cost: the provider's own sample for the last request * plus the heuristic repricing of everything the surface gained or lost since. * It is therefore ONE kind of figure — a projection — and is presented as one. * * An earlier revision marked it `~` whenever `projectedTokens` differed from * `pressureTokens` and left the mark off when they matched. That equality does * not prove what it looked like it proved: several changes can net to a zero * heuristic delta, so a bare figure could claim the provider's own precision for * a surface that had in fact moved. Rather than add shadow state to make the * claim true, the claim is gone: the projection is a projection, and the word * `projected` says so once. */ export interface ContextOccupancy { /** Prompt tokens the next request is projected to carry. */ readonly tokens: number; /** The newest route capacity, when an adapter advertised one. */ readonly capacity: number | undefined; } /** * What the next request's prompt is made of, under the meter's fixed estimator. * * These three are a COMPOSITION, never a total: upstream states that the same * estimator systematically underprices CJK text and JSON schemas, which is the * error the provider anchoring in {@link ContextOccupancy} exists to keep out * of that figure. So the shares below are shares of `total` — the sum of these * three — and are never divided into an occupancy figure. */ export interface ContextComposition { /** * Estimated tokens of the last nonempty surviving system prompt in surface * order — the one actually in force. Superseded system nodes still on the * surface are priced into {@link ContextComposition.messages}, because they * are still occupying context. */ readonly system: number; /** Estimated tokens of the newest request envelope's assembled tool schemas. */ readonly tools: number; /** Estimated tokens of every other visible surface node. */ readonly messages: number; /** The three added together, which is the only figure their shares divide. */ readonly total: number; } /** Everything the cheap projections can truthfully say about current context. */ export interface ContextReading { /** Whether this profile mounted the generic projection infrastructure. */ readonly projections: boolean; /** Whether the token meter's projection units are registered in this process. */ readonly metered: boolean; /** Occupancy, once a provider has reported a prompt size. */ readonly occupancy: ContextOccupancy | undefined; /** Composition, once the meter's breakdown unit has a value. */ readonly composition: ContextComposition | undefined; } /** * Read current context occupancy and composition from one projection cut. * * Takes the snapshot rather than the observer so a caller that also reads other * units — the status line reads Todo from the same cut — pays for one snapshot * per frame instead of one per consumer. * @param snapshot - the authoritative cut, or undefined when the profile mounts no registry. * @returns the small terminal-facing reading. */ export declare function contextReading(snapshot: ProjectionSnapshot | undefined): ContextReading; /** * The occupancy number the status line draws, or nothing to draw. * * Deliberately the projection's figure and not `tokenMeter.measure()`: the * status line needs one number, `measure()` prices and clones every surface * node to produce it, and the status line is redrawn on every spinner * heartbeat, every streamed delta, and every tool transition. * @param reading - the current cheap reading. * @returns prompt tokens for the next request, or undefined before a sample. */ export declare function contextPressureTokens(reading: ContextReading): number | undefined; /** What kind of context one current surface node is. */ export type ContextEntryKind = /** * The rendered system prompt. * * A surface node like any other since Session format V3: the loop appends the * first `system/message` as node 0 and a route that reads a later one appends * changes after the cached history, so a session can carry several — of which * the last nonempty one is in force. Named here because it is regularly the * largest node in a fresh session, and reading `context entry` for it answered * "what is occupying the context" with a shrug. */ 'system' /** A prompt the human typed. */ | 'user' /** Producer-supplied context the model was given but nobody typed. */ | 'context' /** * A compaction checkpoint: the summary standing in for a replaced span. * * Claimed only for a node carrying compaction's own durable provenance, never * for any replacement. See {@link isCompactionCheckpoint}. */ | 'summary' /** One assistant reply, tool calls included. */ | 'assistant' /** One tool's model-facing result. */ | 'tool-result' /** A surface node whose event this frontend has never seen a type for. */ | 'other'; /** One current surface node, resolved into something a person can read. */ export interface ContextEntry { /** Durable seq of the surface event; the entry's stable focus identity. */ readonly seq: SessionSeq; /** Position of this node in the current surface, one-based. */ readonly position: number; /** The node's priced tokens, as the meter reports them. Always an estimate. */ readonly tokens: number; /** * Share of the measured MESSAGE surface, 0 to 1; 0 when that total is 0. * * Deliberately not a share of the whole request context: the denominator is * `TokenMeasurement.surfaceTokens`, which prices the model-visible message * surface and nothing else. That surface now includes the system prompt's own * node, but the tool schemas are still priced by a different authority in a * different vocabulary (see {@link ContextComposition}), and adding the two * together to reach a whole-context percentage would be dshline inventing a * total neither authority states. Presentation must say which context this * divides. */ readonly share: number; /** What kind of context this is. */ readonly kind: ContextEntryKind; /** The producer's declared context form, for a `context` entry. */ readonly form: string | undefined; /** The tool's registered name, paired by call id; undefined when unpaired. */ readonly tool: string | undefined; /** The turn and step the event belongs to, when its type records them. */ readonly turn: number | undefined; readonly step: number | undefined; /** * Whether this node entered the surface by replacing an earlier range. * * The generic surface fact and nothing more. Harness permits a replacement * from any producer, so this says a range was replaced — not who replaced it, * and not that the replacement was a reduction. */ readonly replaced: boolean; } /** The expensive per-node reading, produced only while an inspector is open. */ export interface ContextSurvey { /** Whether a per-node measurement was available and answered. */ readonly available: boolean; /** * Total priced tokens across the current surface — the share denominator. * Deliberately the measurement's own total rather than the occupancy figure: * shares must divide the number the node prices add up to, not one anchored * to a different vocabulary. */ readonly surfaceTokens: number; /** How many nodes the current surface has, before the display bound. */ readonly nodes: number; /** The largest entries, descending, bounded by the surveyor's limit. */ readonly entries: readonly ContextEntry[]; } /** The one thing a surveyor needs from the optional Harness token meter. */ export interface ContextMeter { /** * Price and clone every node of one session's current surface. * @param session - the session to measure. * @returns the detached measurement. */ measure(session: Session): TokenMeasurement; } /** Inputs a context surveyor needs from the runner. */ export interface ContextSurveyorSpec { /** The optional Harness meter, read fresh so a late mount is picked up. */ readonly meter: () => ContextMeter | undefined; /** Exact session whose surface is surveyed. */ readonly session: Session; /** Most entries to resolve; the inspector scrolls within them. */ readonly limit: number; } /** * The largest entries of one session's current surface, measured on demand. * * The cache key is every input a node price depends on and nothing else — the * surface revision and the effective pricing route (see {@link * ContextSurveyor.revision}) — rather than the log length. So an inspector left * open through a streaming reply, a spinner, or a turn's worth of tool calls * measures once, while a landed compaction or a route change is picked up on * the next paint. No timer and no polling: the overlay redraws on Harness's own change * feed and this answers from the cache until a priced input moves. */ export declare class ContextSurveyor { private readonly spec; private cached; /** * @param spec - meter accessor, exact session, and the display bound. */ constructor(spec: ContextSurveyorSpec); /** * The current survey, measuring only when a priced input has moved. * @returns the bounded per-node reading. */ read(): ContextSurvey; /** * Identity of everything a measurement's node prices depend on. * * Two inputs, both O(1) amortized reads of folds Harness already maintains: * * - the SURFACE revision — its node count and Harness's own monotonic * `replaceGeneration`. Node count alone would serve a stale survey after a * compaction, which SHRINKS the surface; the generation alone would miss an * ordinary append. Together they also cover a system-prompt change, which is * a surface change since Session format V3 — an in-history route appends a * `system/message` node and an incapable one replaces the head — so there is * nothing extra to track for the prompt. * - the effective pricing ROUTE, read from the folded request envelope. It is * what `measure()` prices with: the header's provider and model select the * routed adapter's declared image pricing, so the same surface reprices when * the route changes. Read through `session.requestHeader()`, which folds each * header event once, rather than through `request/context` — that record is * route METADATA and upstream documents it as taking no part in request * reconstruction, while the header is the thing the meter actually reads. * * Deliberately NOT the log length: that moves on every committed event a turn * logs — request headers, steps, tool calls and results, settled Assistant * attempts — and remeasuring the whole surface for each would pay O(surface) * repeatedly for no new priced fact. * @returns a string identity for the current priced inputs. */ private revision; /** Drop the cache, for a caller that knows the measurement is stale. */ invalidate(): void; /** Measure and resolve once. */ private survey; } /** * The largest measured nodes, resolved against the durable log. * * Sorted by price descending, then by seq ascending so equal sizes keep one * stable order across paints rather than swapping under the cursor. * @param session - the session the measurement came from. * @param measurement - the meter's detached snapshot. * @param limit - most entries to resolve. * @returns the bounded resolved entries. */ export declare function resolveEntries(session: Session, measurement: TokenMeasurement, limit: number): readonly ContextEntry[]; /** One bounded preview of an entry's model-facing content. */ export interface ContextPreview { /** Raw text, still unescaped: the caller escapes before it adds colour. */ readonly text: string; /** Whether the bound cut further content. */ readonly truncated: boolean; /** Whether the event could be read at all. */ readonly available: boolean; } /** * What one surface node actually contains, bounded for a terminal. * * Read through `deriveEventMessage`, which is the one per-node projection rule * Harness builds its own requests from — so the preview is the content the * model carries, not a second interpretation of the log. * @param session - the session holding the durable log. * @param seq - the node's durable seq. * @returns bounded raw text and whether it was cut. */ export declare function contextPreview(session: Session, seq: SessionSeq): ContextPreview; //# sourceMappingURL=model.d.ts.map