import { a as FlowTrace, F as FlowSpan } from '../flow-types-Cb_AblZs.js';
import { S as StepAgentActivity } from '../agent-activity-C8ZG0F0M.js';

/**
 * Mission trace context — mint + thread the trace ids that join a mission's
 * step attempts and its delegated agent runs into ONE trace tree.
 *
 * ID formats are byte-compatible with agent-runtime's trace propagation
 * (`readTraceContextFromEnv` / OTLP export): 32 lowercase hex chars for a
 * trace id (16 bytes), 16 for a span id (8 bytes). The env pair from
 * {@link traceEnv} is exactly what agent-runtime's MCP subprocess reads at
 * startup (`TRACE_ID` + `PARENT_SPAN_ID`), so a delegation dispatched with it
 * parents its loop→round→iteration spans under the mission's step span.
 *
 * Pure functions, no deps. Ids are DETERMINISTIC when a key is supplied
 * (missionId / step-attempt seed) so a crashed driver re-mints the identical
 * context on re-dispatch and the re-run joins the same trace instead of
 * forking a new one; without a key they are random.
 */
interface MissionTraceContext {
    /** 32-hex trace id shared by every span in the mission's tree. */
    traceId: string;
    /** 16-hex span id of the mission root — the parent of every step span. */
    rootSpanId: string;
}
interface StepSpanContext {
    traceId: string;
    /** 16-hex span id of this step attempt (or any nested unit of work). */
    spanId: string;
    /** The span this one nests under. */
    parentSpanId: string;
}
/**
 * Mint a mission's trace context. With `missionId` the ids are a pure
 * function of it; omitted, both ids are random.
 */
declare function createMissionTraceContext(missionId?: string): MissionTraceContext;
/**
 * Derive a child span context under `parent` — one per step attempt (seed
 * e.g. `"${stepId}#${attempt}"`), or nested under another step span. With a
 * seed the span id is deterministic for the same parent + seed; omitted, it
 * is random.
 */
declare function childSpanContext(parent: MissionTraceContext | StepSpanContext, seed?: string): StepSpanContext;
/**
 * The env pair a delegation subprocess inherits — agent-runtime's
 * `readTraceContextFromEnv` reads exactly these names. `PARENT_SPAN_ID` is
 * the span the dispatched work nests under: the root for a mission context,
 * the step-attempt span for a step context.
 */
declare function traceEnv(ctx: MissionTraceContext | StepSpanContext): {
    TRACE_ID: string;
    PARENT_SPAN_ID: string;
};

/**
 * Delegation → FlowSpan converters — render what a mission's delegated agent
 * runs actually did as ONE FlowTrace, drawable by the existing
 * `renderWaterfall` (or any viewer that consumes FlowSpans).
 *
 * Two fidelities, one tree:
 *  - COARSE: `delegationActivityToFlowSpans` draws one 'tool' span per
 *    delegation from the StepAgentActivity snapshot (startedAt/durationMs) —
 *    available live, from the step's journaled lane.
 *  - FINE: `loopTraceEventsToFlowSpans` reconstructs agent-runtime's
 *    loop → round → iteration hierarchy from the LoopTraceEvent journal a
 *    delegation persists. FlowSpans carry no parent ids, so nesting rides the
 *    span NAME (`loop ▸ round 0 ▸ iter 1 (coder)`), matching how the ASCII
 *    waterfall reads.
 *
 * `composeMissionFlowTrace` lays a whole mission out: one 'pipeline' span per
 * step, each step's delegations beneath it. Pure data transforms — the
 * structural `LoopTraceEventLike` keeps this module free of the optional
 * agent-runtime peer.
 */

/** Structural mirror of agent-runtime's `LoopTraceEvent` — same fields, no
 *  import, so journals parsed from JSON feed straight in. */
interface LoopTraceEventLike {
    kind: string;
    runId: string;
    /** Epoch ms. */
    timestamp: number;
    payload: object;
}
/**
 * One 'tool' FlowSpan per delegation, positioned relative to `turnStartMs`
 * (the epoch-ms origin of the trace — usually the step or mission start).
 * A row whose `startedAt` does not parse cannot be placed on a timeline and
 * is omitted from the WATERFALL (it stays in the lane itself). A run without
 * `durationMs` is still in flight: its span extends to `opts.nowMs` when
 * given, else renders as a point — `approx` flags both.
 */
declare function delegationActivityToFlowSpans(activity: StepAgentActivity[], turnStartMs: number, opts?: {
    nowMs?: number;
}): FlowSpan[];
/**
 * Reconstruct one delegation's loop → round → iteration tree from its
 * journaled LoopTraceEvents, as FlowSpans relative to `loop.started` (or the
 * first event). Mirrors agent-runtime's `buildLoopOtelSpans` topology:
 * rounds open on `loop.plan` and flush on the next plan / `loop.decision` /
 * `loop.ended`; iterations span `loop.iteration.started` → `.ended`.
 */
declare function loopTraceEventsToFlowSpans(events: LoopTraceEventLike[]): FlowSpan[];
/**
 * A single step's activity lane as its own FlowTrace — what a per-step
 * drill-in renders. Origin defaults to the earliest delegation start so the
 * waterfall begins at the lane's first run.
 */
declare function stepActivityFlowTrace(activity: StepAgentActivity[], opts?: {
    startedAt?: number;
    nowMs?: number;
}): FlowTrace;
interface MissionFlowStep {
    id: string;
    intent: string;
    status?: string;
    /** Epoch ms the step attempt started. Absent → laid out sequentially after
     *  the previous step (missions run steps in order), `approx` flagged. */
    startedAt?: number;
    durationMs?: number;
}
/**
 * Compose a mission-wide FlowTrace: one 'pipeline' span per step, the step's
 * delegated runs ('tool' spans, from `activity[stepId]`) beneath it. A step
 * span always covers its delegations' extent. Cost is the sum of delegation
 * `costUsd`; token counts are not knowable from the activity lane and stay 0.
 */
declare function composeMissionFlowTrace(input: {
    steps: MissionFlowStep[];
    /** Delegated-run snapshots keyed by step id (the step's `agentActivity`). */
    activity?: Record<string, StepAgentActivity[]>;
    /** Epoch-ms origin. Default: the earliest known step/delegation start. */
    startedAt?: number;
}): FlowTrace;

/**
 * `@tangle-network/agent-app/trace` — flow observability for agent turns.
 *
 * The turn buffer stamps `_t` (ms since turn start) on every event, so any
 * live stream OR any historical turn replayed from a TurnEventStore can be
 * reconstructed into a span trace: pipeline overhead, model segments (with
 * thinking TTFT), tool executions, token usage, and cost. Renderers turn
 * traces and multi-run samples into ASCII waterfalls and histograms — the
 * default artifact for "how did this run actually behave" questions across
 * evals, hill-climbs, and production debugging.
 *
 * Span boundaries derived from a buffered stream are quantized by the
 * pump's flush window and the reader's poll cadence (~100–400ms); spans
 * carry `approx: true` to keep reports honest about that.
 */

interface TimedEvent {
    /** ms since turn start (`_t` stamped by pumpBufferedTurn). */
    t: number;
    event: Record<string, unknown>;
}
/** Parse stored turn-event lines (JSON strings with `_t`) into TimedEvents. */
declare function timedEventsFromLines(lines: string[]): TimedEvent[];
/**
 * Derive a span trace from timestamped turn events. Model segments are runs
 * of text/reasoning deltas; a tool span opens at the last delta before its
 * tool_call emission and closes at the matching tool_result.
 */
declare function buildFlowTrace(events: TimedEvent[], opts?: {
    pricing?: {
        prompt?: string | number;
        completion?: string | number;
    };
}): FlowTrace;
/** ASCII waterfall cascade — the default artifact for explaining a flow. */
declare function renderWaterfall(trace: FlowTrace, opts?: {
    width?: number;
}): string;
interface DistributionSummary {
    n: number;
    min: number;
    p50: number;
    p90: number;
    max: number;
}
declare function summarize(values: number[]): DistributionSummary;
/** ASCII histogram for multi-run samples (eval latencies, costs, scores). */
declare function renderHistogram(values: number[], opts?: {
    buckets?: number;
    width?: number;
    unit?: string;
    format?: (v: number) => string;
}): string;

export { type DistributionSummary, FlowSpan, FlowTrace, type LoopTraceEventLike, type MissionFlowStep, type MissionTraceContext, type StepSpanContext, type TimedEvent, buildFlowTrace, childSpanContext, composeMissionFlowTrace, createMissionTraceContext, delegationActivityToFlowSpans, loopTraceEventsToFlowSpans, renderHistogram, renderWaterfall, stepActivityFlowTrace, summarize, timedEventsFromLines, traceEnv };
