@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
71 lines • 4.21 kB
TypeScript
/**
* Which session - and which of its tools - the host event loop is working for.
*
* Every in-process session shares one loop, so a stall observed by the loop-lag
* watchdog has to be attributed to the work that was running, not to the host at
* large. Attribution is carried in `AsyncLocalStorage` (so anything an attributed
* command starts can read it) and mirrored in a process-local activity registry the
* watchdog can sample from a timer callback, where the async context of the blocked
* work is long gone.
*/
export interface SessionAttribution {
/** Routing handle of the session this work belongs to. */
readonly sessionId?: string;
/** Tool currently executing for that session. */
readonly tool?: string;
/** Kind of non-tool job holding the loop (senpi#3566): a hook, the event fanout, a parse, a prepare, a spool. */
readonly job?: JobKind;
}
export type JobKind = `hook:${string}` | "fanout" | "session.parse" | "compaction.prepare" | "history.spool";
/**
* Subscribe to the start of attributed work (senpi#3738): the loop-lag watchdog slows its tick while the
* host is idle and uses this to return to its fast tick before the work that could stall the loop runs.
*/
export declare function onSessionActivityStart(listener: () => void): () => void;
/**
* Run `task` attributed to `attribution`: its async chain reads the attribution from
* `AsyncLocalStorage`, and the work stays blamable for as long as it is IN FLIGHT.
*
* The whole lifetime matters, not just the first synchronous segment: a routed command
* almost never blocks the loop before its first await - an extension request, a tool, a
* turn all block in a later continuation, by which time a synchronous-only registration
* has already been retired (observed on a live host: `no attributed session` for a
* 1.7 s stall caused by one session's extension request).
*/
export declare function runWithSessionAttribution<T>(attribution: SessionAttribution, task: () => T): T;
/**
* Open-ended attribution for work that outlives its dispatch, i.e. a tool executing
* inside a turn. The span inherits the ambient session id when the caller does not
* name one, and stays blamable until the returned disposer runs.
*/
export declare function openSessionAttributionSpan(attribution: SessionAttribution): () => void;
/** A job-kind span that inherits the ambient session; it stays blamable until the disposer runs. */
export declare function openJobSpan(job: JobKind): () => void;
/** Run `task` as a job of `job`'s kind, attributed for its whole lifetime like a routed command. */
export declare function runWithJob<T>(job: JobKind, task: () => T): T;
/** One stall line's attribution clause: `sessionId=<id>[ tool=<name>][ job=<kind>]`. */
export declare function describeAttribution(attribution: SessionAttribution | undefined): string;
/** Current activity counter; the watchdog samples it once per tick. */
export declare function sessionActivityMark(): number;
/**
* The activity to blame for work that ran after `mark`. A synchronous body that
* finished inside the window wins (it is the code that just held the loop); otherwise
* the newest open span - a tool mid-execution - takes the blame. Neither means the
* stall belongs to the host itself, and the record is emitted without a session.
*/
export declare function sessionActivitySince(mark: number): SessionAttribution | undefined;
/** Tool spans of one session, driven by the records its runtime emits. */
export interface ToolAttributionSpans {
/** Inspect one session record; tool execution records open and close spans. */
observe(record: object): void;
/** Close every span of this session (turn settled, or the binding went away). */
closeAll(): void;
}
/**
* Track which tool a session is executing, from the record stream its runtime already
* produces. In-process sessions run their tools ON the host loop, so this is where a
* blocking tool becomes attributable; worker sessions block their own isolate and
* deliberately have no spans here.
*/
export declare function createToolAttributionSpans(sessionId: string): ToolAttributionSpans;
//# sourceMappingURL=session-attribution.d.ts.map