/** * Trajectory context management for benchmark/training traces. * * Node.js: AsyncLocalStorage for async-safe propagation (initialized * synchronously to avoid race with first message processing). * Node AsyncLocalStorage is required. */ import type { TrajectoryProviderAttribution } from "./runtime/trajectory-provider-attribution.js"; import type { PseudonymSession } from "./security/pii-pseudonymizer.js"; import type { SecretSwapSession } from "./security/secret-swap.js"; import type { RoleGateRole } from "./types/contexts.js"; import type { State } from "./types/state.js"; export interface TrajectoryContext { /** Active trajectory identifier, when the logger separates trajectory and step ids. */ trajectoryId?: string; trajectoryStepId?: string; /** Task-local, single-flight capture activation; idle workers allocate no rows. */ activateTaskCapture?: () => Promise<{ trajectoryId: string; trajectoryStepId: string; } | undefined>; /** * Single-flight start of this scope's deferred child step at its first * model capture (provider steps); a scope with no model call leaves none. */ activateChildStep?: () => { trajectoryId: string; trajectoryStepId: string; parentStepId: string; } | undefined; /** * Root-turn correlation id. Minted at the message.ts turn boundary * so DB persistence and sub-agent spawns downstream can read one shared * `traceId` and stitch the file, DB, and orchestrator trace stores together. */ traceId?: string; /** Current runtime run identifier associated with the active trajectory step. */ runId?: string; /** Room context for pipeline/model hooks emitted during trajectory logging. */ roomId?: string; /** Source message identifier associated with the active trajectory context. */ messageId?: string; /** Sender role resolved for the active message, used for prompt identity and role-aware logging. */ userRole?: RoleGateRole; /** * Shared promises for immutable reads repeated within one message turn. * The map follows AsyncLocalStorage scope, so it disappears at the turn * boundary without a TTL or cross-turn stale-data policy. */ turnMemo?: Map>; /** Pipeline stage purpose for trajectory logging (e.g. "should_respond", "response", "action", "evaluation"). */ purpose?: string; /** Evaluator owner for an evaluation child step. */ evaluatorName?: string; /** * Latest composed provider contribution snapshot for the active step. * `providerAttributionState` retains provider text so model-call writers can * rebind spans against the exact prompt they persist; precomputed * `providerAttributions` may carry spans only for the composition snapshot * and must not be copied onto a larger model prompt without rebinding. */ providerOrder?: string[]; providerAttributions?: TrajectoryProviderAttribution[]; /** Minimal State used to re-locate provider spans for a consuming model call. */ providerAttributionState?: State; /** * Turn-scoped secret-swap session. Minted on the first `useModel` * call of a turn when secret-swap is enabled, then reused by every subsequent * model call so all share one nonce, and read at the action-execution boundary * (`executePlannedToolCall`) to restore real secrets into handler args. Absent * when secret-swap is disabled — the egress restore is then a no-op. */ secretSwapSession?: SecretSwapSession; /** * Turn-scoped PII pseudonymization session. Minted on the * first `useModel` call of a turn when PII swap is enabled, then reused by * every subsequent model call so a real entity maps to the same surrogate all * turn, and read at the action-execution boundary (`executePlannedToolCall`) * to restore real names/orgs/addresses into handler args and reply text. * Absent when PII swap is disabled — the egress restore is then a no-op. */ piiSwapSession?: PseudonymSession; /** * Step ID of the parent trajectory step, when the current step was * dispatched from inside another. Persistence layers use this to attach * child step IDs to the parent's `childSteps` array. */ parentStepId?: string; } export interface ITrajectoryContextManager { run(context: TrajectoryContext | undefined, fn: () => T | Promise): T | Promise; active(): TrajectoryContext | undefined; } export declare function setTrajectoryContextManager(manager: ITrajectoryContextManager): void; /** * The shared global slot is the single source of truth (no module-local * cache): under a duplicated core bundle every copy must observe the same * manager, and `setTrajectoryContextManager` must be visible everywhere. */ export declare function getTrajectoryContextManager(): ITrajectoryContextManager; export declare function runWithTrajectoryContext(context: TrajectoryContext | undefined, fn: () => T | Promise): T | Promise; export declare function getTrajectoryContext(): TrajectoryContext | undefined; export declare function memoizeTurnWork(key: string, work: () => Promise): Promise; export declare function invalidateTurnMemo(key: string): void; export declare function invalidateTurnMemoPrefix(prefix: string): void; /** * Run `fn` with the ambient trajectory context preserved and only `purpose` * overridden. * * Passing a bare `{ purpose }` object to {@link runWithTrajectoryContext} * REPLACES the active context: `trajectoryStepId` is dropped, so the runtime * never records the nested `useModel` call (and the purpose tag is lost with * it), and the turn's secret-swap/PII sessions stop propagating. Use this * helper to tag a model call with a purpose while keeping the active * step/run/room identifiers and swap sessions intact. */ export declare function runWithTrajectoryPurpose(purpose: string, fn: () => T | Promise): T | Promise; /** * Set the pipeline purpose on the current trajectory context. * Mutates in place so nested useModel calls pick up the correct stage. */ export declare function setTrajectoryPurpose(purpose: string): void;