import {
  canonicalize,
  hashJson
} from "./chunk-VSMTAMNK.js";
import {
  NotFoundError,
  ReplayError
} from "./chunk-QYJT52YW.js";

// src/trace-analyst/prompts.ts
var TRACE_ANALYST_ACTOR_DESCRIPTION = `You answer questions about an OTLP-shaped JSONL trace dataset using the trace tools provided in the \`traces\` namespace.

DISCOVERY \u2192 NARROW \u2192 DEEP-READ protocol \u2014 follow exactly:

1. ALWAYS call \`traces.getDatasetOverview({})\` FIRST without a regex_pattern. The result tells you total_traces, raw_jsonl_bytes, services, agents, models, and sample_trace_ids (real ids \u2014 never fabricate one).

2. Use raw_jsonl_bytes to gauge how expensive raw scans will be. \`filters.regex_pattern\` is the one scan-heavy filter on getDatasetOverview / queryTraces / countTraces \u2014 narrow with indexed fields (has_errors, model_names, service_names, agent_names, time bounds) BEFORE adding a regex on a large dataset.

3. To list more traces than the sample, call \`traces.queryTraces({ filters?, limit, offset? })\`. Each summary carries raw_jsonl_bytes \u2014 use it to choose between viewTrace and searchTrace BEFORE calling either.

4. Per-trace inspection:
   - SMALL trace (raw_jsonl_bytes well under 150_000): call \`traces.viewTrace({ trace_id })\`. Returns all spans. Per-attribute payloads are head-capped at ~4KB; large \`input.value\` / \`output.value\` / \`llm.input_messages\` will show a \`[trace-analyst truncated: N bytes]\` marker.
   - LARGE trace (raw_jsonl_bytes near or above 150_000, or you saw an \`oversized\` response): use \`traces.searchTrace({ trace_id, regex_pattern })\` to get bounded SpanMatchRecords (span metadata + matched text + surrounding context). Then call \`traces.viewSpans({ trace_id, span_ids: [...] })\` for surgical reads (~16KB cap, 4\xD7 higher than discovery), or \`traces.searchSpan({ trace_id, span_id, regex_pattern })\` for one large span. Stays bounded regardless of trace size.
   - Useful regex patterns: \`STATUS_CODE_ERROR\` (failures), tool names like \`grep\` or \`view_trace\`, error strings like \`MaxTurnsExceeded\`, model names, attribute keys.

5. ONLY call viewTrace / viewSpans / searchTrace / searchSpan with trace/span ids you have already seen in sample_trace_ids, a queryTraces page, or a previous search result. Never invent ids.

5a. **Result-shape contract** \u2014 searchTrace and searchSpan return \`{ trace_id, hits, total_matches, has_more }\`. Iterate \`result.hits\` (NOT result.matches). Each hit has \`{ span_id, span_name, span_kind, attribute_path, matched_text, context_before, context_after, match_offset }\`. viewTrace returns \`{ trace_id, spans }\` (or \`oversized\`). viewSpans returns \`{ trace_id, spans, missing_span_ids, truncated_attribute_count }\`. Never assume a field name \u2014 log the result shape first if unsure.

6. If viewTrace returns an \`oversized\` summary instead of \`spans\`, DO NOT retry the same call. Read the summary's top_span_names, span_count, span_response_bytes_max, error_span_count to plan a follow-up: switch to searchTrace (or searchSpan for one large span), then viewSpans on a smaller, surgical span_ids set.

7. If searchTrace or searchSpan returns has_more=true, REFINE the regex to be more specific rather than blindly raising max_matches.

8. If a tool errors (invalid regex, range error), STOP and reconsider \u2014 don't retry with a guessed id or argument. Use the discovery tools above to recover.

9. If a ~4KB-truncated payload from viewTrace / searchTrace matters for your answer, first try viewSpans on that span id (~16KB cap). If a 16KB-truncated payload from viewSpans still matters, narrow further with searchSpan against a more specific regex rather than asking for the full payload again.

10. If maxDepth > 0 and the question splits into independent semantic branches, delegate well-defined subtasks to subagents using \`await llmQuery(...)\`. Pass narrow context and a focused query. Examples:

    const reviews = await llmQuery([
      { query: 'Drill into trace abc123 \u2014 what tool calls preceded the failure?', context: { trace_id: 'abc123' } },
      { query: 'Drill into trace def456 \u2014 same failure mode?', context: { trace_id: 'def456' } },
    ]);

OBSERVABILITY rules:
- Each non-final actor turn must emit at least one \`console.log(...)\` for evidence. Up to 3 logs per turn is fine when correlating multiple data sources (e.g. one log for findings list, one for source-file content, one for derived analysis).
- Do NOT combine \`console.log\` with \`final(...)\` or \`askClarification(...)\` in the same turn \u2014 finish gathering data first, then call final on its own turn.
- Reuse runtime variables across turns; don't recompute.
- When done, call \`await final(answer)\` with the fully-formed report. The responder rewrites the answer into output fields; if you only pass a vague summary string the responder has nothing concrete to format.

CRITICAL \u2014 \`final()\` payload contract for evidence-grounded analysis tasks:
- Pass a STRUCTURED object as the second arg with the actual data the responder needs to format the answer. Do NOT pass abstract instructions; pass evidence.
- Example for per-item verdict tasks:
  \`\`\`js
  await final("Format the per-item verdict report from the evidence below.", {
    findings: [
      { id: 'sub-1-finding-1', claim: '...', verdict: 'TRUE-POSITIVE', evidence: 'lines 42-45 of contracts/X.sol show ...' },
      ...all items
    ],
    systemic_summary: '3 sentences I wrote based on the evidence above'
  });
  \`\`\`
- Calling \`final("answer", {})\` with no evidence is a failure mode \u2014 the responder will hallucinate or echo back the field names. Always include the gathered data.
- Premature final after a single viewSpans call is INSUFFICIENT for per-finding analysis tasks. Read the requested attributes (e.g. \`spans[i].attributes['redteam.finding.title']\`), and for each one perform the requested cross-reference (e.g. read the source SPAN's \`attributes['source.content']\`).

OUTPUT contract \u2014 your final answer must include:
- A clear prose conclusion answering the user's question.
- Trace ids and span ids cited as evidence for each claim.
- Failure modes named in the user's domain language, with frequency and concrete examples.

Do NOT invent trace ids, span ids, error messages, or model names. Every fact must be traceable to a tool result.`;
var TRACE_ANALYST_ACTOR_DESCRIPTION_VERSION = "trace-analyst-actor-v5-2026-05-06";
var TRACE_ANALYST_SUBAGENT_DESCRIPTION = `You are a trace-analyst subagent. Your parent has delegated a focused trace-inspection question. Use the same DISCOVERY \u2192 NARROW \u2192 DEEP-READ protocol but stay tightly scoped: do exactly what was asked, return a concise compact answer, do NOT spawn further subagents unless the parent's question is genuinely multi-branch.

Cite trace ids and span ids for every claim. Do NOT invent ids.`;

// src/trace-analyst/types.ts
var DEFAULT_TRACE_ANALYST_BUDGETS = {
  perCallByteCeiling: 15e4,
  perAttributeViewBudget: 4096,
  perAttributeSpanBudget: 16384,
  perMatchTextBudget: 1024
};
var TRACE_ANALYST_TRUNCATION_MARKER_PREFIX = "[trace-analyst truncated:";

// src/trace-analyst/store-otlp.ts
import { readFile, stat } from "fs/promises";

// src/trace-analyst/store.ts
function compileSearchRegex(pattern) {
  let source = pattern;
  let flags = "m";
  if (source.startsWith("(?i)")) {
    source = source.slice(4);
    flags += "i";
  }
  return new RegExp(source, flags);
}
function truncateForBudget(value, byteCap) {
  const original = Buffer.byteLength(value, "utf8");
  if (original <= byteCap) return value;
  const ratio = byteCap / original;
  let cut = Math.max(0, Math.floor(value.length * ratio));
  while (cut > 0 && Buffer.byteLength(value.slice(0, cut), "utf8") > byteCap) {
    cut -= 1;
  }
  return `${value.slice(0, cut)}
[trace-analyst truncated: original ${original} bytes]`;
}

// src/trace-analyst/store-otlp.ts
var OtlpFileTraceStore = class {
  path;
  perAttributeViewBudget;
  perAttributeSpanBudget;
  perCallByteCeiling;
  perMatchTextBudget;
  indexPromise;
  /** Cached UTF-8 buffer of the file. We pin it once because every
   *  read needs slice access and re-reading on each call balloons the
   *  syscall count. */
  bufferPromise;
  constructor(opts) {
    this.path = opts.path;
    this.perAttributeViewBudget = opts.perAttributeViewBudget ?? DEFAULT_TRACE_ANALYST_BUDGETS.perAttributeViewBudget;
    this.perAttributeSpanBudget = opts.perAttributeSpanBudget ?? DEFAULT_TRACE_ANALYST_BUDGETS.perAttributeSpanBudget;
    this.perCallByteCeiling = opts.perCallByteCeiling ?? DEFAULT_TRACE_ANALYST_BUDGETS.perCallByteCeiling;
    this.perMatchTextBudget = opts.perMatchTextBudget ?? DEFAULT_TRACE_ANALYST_BUDGETS.perMatchTextBudget;
  }
  // ─── Public API ────────────────────────────────────────────────────
  async getOverview(filters) {
    const idx = await this.index();
    const matched = await this.matchedTraces(idx, filters);
    const services = /* @__PURE__ */ new Set();
    const agents = /* @__PURE__ */ new Set();
    const models = /* @__PURE__ */ new Set();
    const tools = /* @__PURE__ */ new Set();
    let rawBytes = 0;
    let earliest = null;
    let latest = null;
    let errorTraceCount = 0;
    let errorSpanCount = 0;
    for (const t of matched) {
      if (t.service_name) services.add(t.service_name);
      if (t.agent_name) agents.add(t.agent_name);
      for (const m of t.models) models.add(m);
      for (const tn of t.tools) tools.add(tn);
      rawBytes += t.raw_jsonl_bytes;
      if (!earliest || t.start_time < earliest) earliest = t.start_time;
      if (!latest || t.end_time > latest) latest = t.end_time;
      if (t.has_errors) {
        errorTraceCount += 1;
        for (const s of t.spans) if (s.status === "ERROR") errorSpanCount += 1;
      }
    }
    const sample_trace_ids = matched.slice(0, 20).map((t) => t.trace_id);
    return {
      total_traces: matched.length,
      raw_jsonl_bytes: rawBytes,
      services: [...services].sort(),
      agents: [...agents].sort(),
      models: [...models].sort(),
      tool_names: [...tools].sort(),
      sample_trace_ids,
      errors: { trace_count: errorTraceCount, span_count: errorSpanCount },
      time_range: earliest && latest ? { earliest, latest } : null
    };
  }
  async queryTraces(opts) {
    if (!Number.isInteger(opts.limit) || opts.limit < 1 || opts.limit > 200) {
      throw new RangeError(`queryTraces.limit must be 1..200, got ${opts.limit}`);
    }
    const offset = opts.offset ?? 0;
    if (!Number.isInteger(offset) || offset < 0) {
      throw new RangeError(`queryTraces.offset must be >=0, got ${offset}`);
    }
    const idx = await this.index();
    const matched = await this.matchedTraces(idx, opts.filters);
    const slice = matched.slice(offset, offset + opts.limit);
    return {
      traces: slice.map((t) => this.toSummary(t)),
      total: matched.length,
      has_more: offset + slice.length < matched.length
    };
  }
  async countTraces(filters) {
    const idx = await this.index();
    const matched = await this.matchedTraces(idx, filters);
    return matched.length;
  }
  async viewTrace(opts) {
    const idx = await this.index();
    const trace = idx.byTrace.get(opts.trace_id);
    if (!trace) {
      throw new TraceNotFoundError(opts.trace_id);
    }
    const cap = opts.per_attribute_byte_cap ?? this.perAttributeViewBudget;
    const buf = await this.buffer();
    const spans = [];
    let runningBytes = 0;
    let span_response_bytes_max = 0;
    for (const s of trace.spans) {
      const projected = await this.projectSpan(buf, trace.trace_id, s, cap);
      const bytes = Buffer.byteLength(JSON.stringify(projected), "utf8");
      span_response_bytes_max = Math.max(span_response_bytes_max, bytes);
      runningBytes += bytes;
      if (runningBytes > this.perCallByteCeiling) {
        return {
          trace_id: trace.trace_id,
          oversized: this.buildOversizedSummary(trace, span_response_bytes_max)
        };
      }
      spans.push(projected);
    }
    return { trace_id: trace.trace_id, spans };
  }
  async viewSpans(opts) {
    const idx = await this.index();
    const trace = idx.byTrace.get(opts.trace_id);
    if (!trace) throw new TraceNotFoundError(opts.trace_id);
    if (opts.span_ids.length === 0) {
      return {
        trace_id: trace.trace_id,
        spans: [],
        missing_span_ids: [],
        truncated_attribute_count: 0
      };
    }
    if (opts.span_ids.length > 100) {
      throw new RangeError(`viewSpans.span_ids cap is 100, got ${opts.span_ids.length}`);
    }
    const cap = opts.per_attribute_byte_cap ?? this.perAttributeSpanBudget;
    const wantSet = new Set(opts.span_ids);
    const found = trace.spans.filter((s) => wantSet.has(s.span_id));
    const missing = opts.span_ids.filter((id) => !found.some((f2) => f2.span_id === id));
    const buf = await this.buffer();
    const spans = [];
    let truncated = 0;
    let runningBytes = 0;
    for (const s of found) {
      const before = truncationCounter(this);
      const projected = await this.projectSpan(buf, trace.trace_id, s, cap);
      truncated += before.delta();
      const bytes = Buffer.byteLength(JSON.stringify(projected), "utf8");
      runningBytes += bytes;
      if (runningBytes > this.perCallByteCeiling) {
        break;
      }
      spans.push(projected);
    }
    return {
      trace_id: trace.trace_id,
      spans,
      missing_span_ids: missing,
      truncated_attribute_count: truncated
    };
  }
  async searchTrace(opts) {
    const max_matches = opts.max_matches ?? 50;
    if (!Number.isInteger(max_matches) || max_matches < 1 || max_matches > 500) {
      throw new RangeError(`searchTrace.max_matches must be 1..500, got ${max_matches}`);
    }
    const idx = await this.index();
    const trace = idx.byTrace.get(opts.trace_id);
    if (!trace) throw new TraceNotFoundError(opts.trace_id);
    const re = compileSearchRegex(opts.regex_pattern);
    const buf = await this.buffer();
    const hits = [];
    let total = 0;
    let capped = false;
    for (const s of trace.spans) {
      const remaining = max_matches - hits.length;
      const localHits = await this.scanSpanForMatches(
        buf,
        trace.trace_id,
        s,
        re,
        this.perMatchTextBudget,
        remaining
      );
      total += localHits.total;
      for (const h of localHits.records) {
        if (hits.length >= max_matches) break;
        hits.push(h);
      }
      if (hits.length >= max_matches) {
        capped = true;
        total = Math.max(total, hits.length + 1);
        break;
      }
    }
    return {
      trace_id: trace.trace_id,
      hits,
      total_matches: total,
      has_more: capped || total > hits.length
    };
  }
  async searchSpan(opts) {
    const max_matches = opts.max_matches ?? 50;
    if (!Number.isInteger(max_matches) || max_matches < 1 || max_matches > 500) {
      throw new RangeError(`searchSpan.max_matches must be 1..500, got ${max_matches}`);
    }
    const idx = await this.index();
    const trace = idx.byTrace.get(opts.trace_id);
    if (!trace) throw new TraceNotFoundError(opts.trace_id);
    const span = trace.spans.find((s) => s.span_id === opts.span_id);
    if (!span) {
      throw new SpanNotFoundError(opts.trace_id, opts.span_id);
    }
    const re = compileSearchRegex(opts.regex_pattern);
    const buf = await this.buffer();
    const localHits = await this.scanSpanForMatches(
      buf,
      trace.trace_id,
      span,
      re,
      this.perMatchTextBudget,
      max_matches
    );
    return {
      trace_id: trace.trace_id,
      span_id: span.span_id,
      hits: localHits.records,
      total_matches: localHits.total,
      has_more: localHits.total > localHits.records.length
    };
  }
  // ─── Index building ────────────────────────────────────────────────
  /** Force the index to materialise. Useful to amortise startup cost
   *  before the first agent call. */
  async ensureIndexed() {
    await this.index();
  }
  async buffer() {
    if (!this.bufferPromise) {
      this.bufferPromise = readFile(this.path);
    }
    return this.bufferPromise;
  }
  async index() {
    if (!this.indexPromise) {
      this.indexPromise = this.buildIndex();
    }
    return this.indexPromise;
  }
  async buildIndex() {
    let buf;
    try {
      buf = await this.buffer();
    } catch (err) {
      const stats = await stat(this.path).catch(() => null);
      if (!stats) {
        throw new TraceFileMissingError(this.path);
      }
      throw err;
    }
    const byTrace = /* @__PURE__ */ new Map();
    let cursor = 0;
    while (cursor < buf.length) {
      const newlineIndex = buf.indexOf(10, cursor);
      const lineEnd = newlineIndex === -1 ? buf.length : newlineIndex;
      const lineLength = lineEnd - cursor;
      if (lineLength === 0) {
        cursor = lineEnd + 1;
        continue;
      }
      const lineSlice = buf.subarray(cursor, lineEnd).toString("utf8");
      const lineOffset = cursor;
      cursor = lineEnd + 1;
      let parsed;
      try {
        parsed = JSON.parse(lineSlice);
      } catch {
        continue;
      }
      if (!parsed || typeof parsed !== "object") continue;
      const span = readOtlpSpan(parsed);
      if (!span) continue;
      let entry = byTrace.get(span.trace_id);
      if (!entry) {
        entry = {
          trace_id: span.trace_id,
          service_name: span.service_name,
          agent_name: span.agent_name,
          span_count: 0,
          has_errors: false,
          start_time: span.start_time,
          end_time: span.end_time,
          duration_ms: 0,
          raw_jsonl_bytes: 0,
          models: /* @__PURE__ */ new Set(),
          tools: /* @__PURE__ */ new Set(),
          spans: []
        };
        byTrace.set(span.trace_id, entry);
      } else {
        if (!entry.service_name && span.service_name) entry.service_name = span.service_name;
        if (!entry.agent_name && span.agent_name) entry.agent_name = span.agent_name;
      }
      const indexEntry = {
        span_id: span.span_id,
        parent_span_id: span.parent_span_id,
        name: span.name,
        kind: span.kind,
        start_time: span.start_time,
        end_time: span.end_time,
        duration_ms: span.duration_ms,
        status: span.status,
        status_message: span.status_message,
        service_name: span.service_name,
        agent_name: span.agent_name,
        model_name: span.model_name,
        tool_name: span.tool_name,
        line_byte_offset: lineOffset,
        line_byte_length: lineLength
      };
      entry.spans.push(indexEntry);
      entry.span_count += 1;
      entry.raw_jsonl_bytes += lineLength + 1;
      if (span.status === "ERROR") entry.has_errors = true;
      if (span.start_time < entry.start_time) entry.start_time = span.start_time;
      if (span.end_time > entry.end_time) entry.end_time = span.end_time;
      if (span.model_name) entry.models.add(span.model_name);
      if (span.tool_name) entry.tools.add(span.tool_name);
    }
    let totalRawBytes = 0;
    for (const t of byTrace.values()) {
      totalRawBytes += t.raw_jsonl_bytes;
      t.spans.sort(
        (a, b) => a.start_time.localeCompare(b.start_time) || a.line_byte_offset - b.line_byte_offset
      );
      t.duration_ms = Math.max(0, new Date(t.end_time).getTime() - new Date(t.start_time).getTime());
    }
    const sortedTraceIds = [...byTrace.keys()].sort();
    return { byTrace, totalRawBytes, sortedTraceIds };
  }
  // ─── Filter pipeline ───────────────────────────────────────────────
  async matchedTraces(idx, filters) {
    const traces = idx.sortedTraceIds.map((id) => idx.byTrace.get(id)).filter(isPresent);
    if (!filters) return traces;
    const indexedFiltered = traces.filter((t) => {
      if (filters.has_errors !== void 0 && t.has_errors !== filters.has_errors) return false;
      if (filters.service_names && filters.service_names.length > 0) {
        if (!t.service_name || !filters.service_names.includes(t.service_name)) return false;
      }
      if (filters.agent_names && filters.agent_names.length > 0) {
        if (!t.agent_name || !filters.agent_names.includes(t.agent_name)) return false;
      }
      if (filters.model_names && filters.model_names.length > 0) {
        if (![...t.models].some((m) => filters.model_names.includes(m))) return false;
      }
      if (filters.tool_names && filters.tool_names.length > 0) {
        if (![...t.tools].some((tn) => filters.tool_names.includes(tn))) return false;
      }
      if (filters.start_time_after && t.start_time < filters.start_time_after) return false;
      if (filters.start_time_before && t.start_time > filters.start_time_before) return false;
      return true;
    });
    if (!filters.regex_pattern) return indexedFiltered;
    const re = compileSearchRegex(filters.regex_pattern);
    const buf = await this.buffer();
    const out = [];
    for (const t of indexedFiltered) {
      let matched = false;
      for (const s of t.spans) {
        const slice = buf.subarray(s.line_byte_offset, s.line_byte_offset + s.line_byte_length);
        if (re.test(slice.toString("utf8"))) {
          matched = true;
          break;
        }
      }
      if (matched) out.push(t);
    }
    return out;
  }
  toSummary(t) {
    return {
      trace_id: t.trace_id,
      service_name: t.service_name,
      agent_name: t.agent_name,
      span_count: t.span_count,
      has_errors: t.has_errors,
      start_time: t.start_time,
      end_time: t.end_time,
      duration_ms: t.duration_ms,
      raw_jsonl_bytes: t.raw_jsonl_bytes,
      models: [...t.models].sort(),
      tools: [...t.tools].sort()
    };
  }
  // ─── Span projection (lazy attribute reads) ────────────────────────
  async projectSpan(buf, trace_id, s, perAttrCap) {
    const slice = buf.subarray(s.line_byte_offset, s.line_byte_offset + s.line_byte_length).toString("utf8");
    let raw = {};
    try {
      const parsed = JSON.parse(slice);
      if (parsed && typeof parsed === "object") raw = parsed;
    } catch {
    }
    const attrs = extractAttributes(raw);
    const projected = {};
    for (const [k, v] of Object.entries(attrs)) {
      if (typeof v === "string") {
        const trunc = truncateForBudget(v, perAttrCap);
        if (trunc !== v) trackTruncation(this);
        projected[k] = trunc;
      } else if (Array.isArray(v) || v && typeof v === "object") {
        const json = JSON.stringify(v);
        const trunc = truncateForBudget(json, perAttrCap);
        if (trunc !== json) {
          trackTruncation(this);
          projected[k] = trunc;
        } else {
          projected[k] = v;
        }
      } else {
        projected[k] = v;
      }
    }
    return {
      trace_id,
      span_id: s.span_id,
      parent_span_id: s.parent_span_id,
      name: s.name,
      kind: s.kind,
      start_time: s.start_time,
      end_time: s.end_time,
      duration_ms: s.duration_ms,
      status: s.status,
      status_message: s.status_message,
      service_name: s.service_name,
      agent_name: s.agent_name,
      model_name: s.model_name,
      tool_name: s.tool_name,
      attributes: projected
    };
  }
  buildOversizedSummary(t, span_response_bytes_max) {
    const counts = /* @__PURE__ */ new Map();
    let errorCount = 0;
    for (const s of t.spans) {
      counts.set(s.name, (counts.get(s.name) ?? 0) + 1);
      if (s.status === "ERROR") errorCount += 1;
    }
    const top = [...counts.entries()].sort((a, b) => b[1] - a[1]).slice(0, 20);
    return {
      span_count: t.span_count,
      top_span_names: top,
      span_response_bytes_max,
      error_span_count: errorCount
    };
  }
  async scanSpanForMatches(buf, trace_id, s, re, textBudget, recordCap) {
    const slice = buf.subarray(s.line_byte_offset, s.line_byte_offset + s.line_byte_length).toString("utf8");
    const records = [];
    const globalRe = new RegExp(re.source, re.flags.includes("g") ? re.flags : `${re.flags}g`);
    let total = 0;
    let hasMore = false;
    let m;
    while ((m = globalRe.exec(slice)) !== null) {
      total += 1;
      if (m.index === globalRe.lastIndex) globalRe.lastIndex += 1;
      if (records.length >= recordCap) {
        hasMore = true;
        break;
      }
      const before = slice.slice(Math.max(0, m.index - textBudget / 2), m.index);
      const after = slice.slice(
        m.index + m[0].length,
        m.index + m[0].length + Math.floor(textBudget / 2)
      );
      records.push({
        trace_id,
        span_id: s.span_id,
        span_name: s.name,
        span_kind: s.kind,
        attribute_path: bestAttributePathForOffset(slice, m.index) ?? "span.raw",
        matched_text: truncateForBudget(m[0], textBudget),
        context_before: truncateForBudget(before, textBudget),
        context_after: truncateForBudget(after, textBudget),
        match_offset: m.index
      });
    }
    return { records, total, hasMore };
  }
};
var TraceFileMissingError = class extends NotFoundError {
  constructor(path) {
    super(`trace file not found: ${path}`);
  }
};
var TraceNotFoundError = class extends NotFoundError {
  trace_id;
  constructor(trace_id) {
    super(`trace not found: ${trace_id}`);
    this.trace_id = trace_id;
  }
};
var SpanNotFoundError = class extends NotFoundError {
  trace_id;
  span_id;
  constructor(trace_id, span_id) {
    super(`span ${span_id} not found in trace ${trace_id}`);
    this.trace_id = trace_id;
    this.span_id = span_id;
  }
};
function readOtlpSpan(raw) {
  const trace_id = stringField(raw, "trace_id") ?? stringField(raw, "traceId");
  const span_id = stringField(raw, "span_id") ?? stringField(raw, "spanId");
  if (!trace_id || !span_id) return null;
  const parent_id = stringField(raw, "parent_span_id") ?? stringField(raw, "parentSpanId") ?? null;
  const name = stringField(raw, "name") ?? "unknown";
  const start_time = stringField(raw, "start_time") ?? stringField(raw, "startTime") ?? "";
  const end_time = stringField(raw, "end_time") ?? stringField(raw, "endTime") ?? start_time;
  const status = readStatus(raw);
  const attrs = extractAttributes(raw);
  const service_name = asString(attrs["service.name"]) ?? asString(attrs["resource.attributes.service.name"]) ?? null;
  const agent_name = asString(attrs["agent.name"]) ?? asString(attrs["inference.agent.name"]) ?? null;
  const model_name = asString(attrs["llm.model_name"]) ?? asString(attrs["inference.llm.model_name"]) ?? null;
  const tool_name = asString(attrs["tool.name"]) ?? asString(attrs["inference.tool.name"]) ?? null;
  const kind = inferKind(attrs);
  let duration_ms = 0;
  if (start_time && end_time) {
    const a = Date.parse(start_time);
    const b = Date.parse(end_time);
    if (!Number.isNaN(a) && !Number.isNaN(b)) duration_ms = Math.max(0, b - a);
  }
  return {
    trace_id,
    span_id,
    parent_span_id: parent_id && parent_id.length > 0 ? parent_id : null,
    name,
    kind,
    start_time,
    end_time,
    duration_ms,
    status: status.code,
    status_message: status.message,
    service_name,
    agent_name,
    model_name,
    tool_name
  };
}
function readStatus(raw) {
  const status = raw.status;
  if (status && typeof status === "object" && !Array.isArray(status)) {
    const codeRaw = status.code;
    const code = codeRaw === "STATUS_CODE_OK" || codeRaw === "OK" ? "OK" : codeRaw === "STATUS_CODE_ERROR" || codeRaw === "ERROR" ? "ERROR" : "UNSET";
    const messageRaw = status.message;
    const message = typeof messageRaw === "string" && messageRaw.length > 0 ? messageRaw : void 0;
    return { code, message };
  }
  return { code: "UNSET", message: void 0 };
}
function inferKind(attrs) {
  const opik = asString(attrs["openinference.span.kind"]) ?? asString(attrs["inference.observation_kind"]);
  if (opik) {
    const upper = opik.toUpperCase();
    if (upper === "AGENT" || upper === "LLM" || upper === "TOOL" || upper === "CHAIN" || upper === "GUARDRAIL" || upper === "SPAN") {
      return upper;
    }
  }
  return "UNKNOWN";
}
function extractAttributes(raw) {
  const out = {};
  const resource = raw.resource;
  if (resource && typeof resource === "object" && !Array.isArray(resource)) {
    const ra = resource.attributes;
    if (ra && typeof ra === "object" && !Array.isArray(ra)) {
      for (const [k, v] of Object.entries(ra)) {
        out[k] = v;
      }
    }
  }
  const spanAttrs = raw.attributes;
  if (spanAttrs && typeof spanAttrs === "object" && !Array.isArray(spanAttrs)) {
    for (const [k, v] of Object.entries(spanAttrs)) {
      out[k] = v;
    }
  }
  return out;
}
function stringField(raw, key) {
  const v = raw[key];
  return typeof v === "string" ? v : void 0;
}
function asString(v) {
  return typeof v === "string" && v.length > 0 ? v : null;
}
function isPresent(v) {
  return v !== void 0;
}
var truncationCounters = /* @__PURE__ */ new WeakMap();
function trackTruncation(store) {
  let c = truncationCounters.get(store);
  if (!c) {
    c = { value: 0 };
    truncationCounters.set(store, c);
  }
  c.value += 1;
}
function truncationCounter(store) {
  const before = truncationCounters.get(store)?.value ?? 0;
  return {
    delta() {
      const after = truncationCounters.get(store)?.value ?? 0;
      return after - before;
    }
  };
}
function bestAttributePathForOffset(slice, offset) {
  let i = offset;
  while (i > 0 && slice[i] !== '"') i -= 1;
  if (i <= 0) return null;
  let j = i - 1;
  while (j > 0 && slice[j] !== ":") j -= 1;
  if (j <= 0) return null;
  let k = j - 1;
  while (k > 0 && slice[k] !== '"') k -= 1;
  let l = k - 1;
  while (l > 0 && slice[l] !== '"') l -= 1;
  if (l <= 0) return null;
  return slice.slice(l + 1, k);
}

// src/trace-analyst/tools.ts
import { f, fn } from "@ax-llm/ax";
var NAMESPACE = "traces";
var filtersField = f.json("Filter set. ALL fields are AND-composed. Leave empty to scan everything.").optional();
function buildTraceAnalystTools(opts) {
  const { store } = opts;
  const getDatasetOverview = fn("getDatasetOverview").description(
    "Dataset rollup: total traces, raw_jsonl_bytes, services, agents, models, tools, and sample_trace_ids (real ids passable to view/search). Always call this FIRST without a regex_pattern."
  ).namespace(NAMESPACE).arg("filters", filtersField).returns(f.json("DatasetOverview")).handler(async ({ filters }) => store.getOverview(parseFilters(filters))).build();
  const queryTraces = fn("queryTraces").description(
    "Paginated trace summaries. Each summary carries raw_jsonl_bytes \u2014 use it to size traces BEFORE calling viewTrace. Narrow with indexed filters before adding regex_pattern."
  ).namespace(NAMESPACE).arg("filters", filtersField).arg("limit", f.number("Page size, 1..200")).arg("offset", f.number("Page offset; default 0").optional()).returns(f.json("QueryTracesPage")).handler(
    async ({ filters, limit, offset }) => store.queryTraces({
      filters: parseFilters(filters),
      limit: assertPageLimit(limit),
      offset: assertOffset(offset)
    })
  ).build();
  const countTraces = fn("countTraces").description(
    "Count traces matching `filters`. Use as a cheap pre-flight before opting into a regex_pattern scan."
  ).namespace(NAMESPACE).arg("filters", filtersField).returns(f.number("count")).handler(async ({ filters }) => store.countTraces(parseFilters(filters))).build();
  const viewTrace = fn("viewTrace").description(
    "Return ALL spans for a single trace, with each attribute capped at ~4KB. If the response would exceed the per-call ceiling the result carries `oversized` instead of `spans` \u2014 DO NOT retry with the same trace_id; switch to searchTrace / viewSpans."
  ).namespace(NAMESPACE).arg("trace_id", f.string("Real trace id from a prior overview/query")).returns(f.json("ViewTraceResult")).handler(
    async ({ trace_id }) => store.viewTrace({ trace_id: assertString(trace_id, "trace_id") })
  ).build();
  const viewSpans = fn("viewSpans").description(
    "Surgical read of specific spans within a trace, with each attribute capped at ~16KB (4\xD7 the discovery cap). Use after searchTrace narrows to specific span_ids."
  ).namespace(NAMESPACE).arg("trace_id", f.string("Real trace id")).arg("span_ids", f.string("Span ids to fetch").array()).returns(f.json("ViewSpansResult")).handler(
    async ({ trace_id, span_ids }) => store.viewSpans({
      trace_id: assertString(trace_id, "trace_id"),
      span_ids: assertStringArray(span_ids, "span_ids")
    })
  ).build();
  const searchTrace = fn("searchTrace").description(
    "Regex search across all spans of one trace. Returns `{trace_id, hits: SpanMatchRecord[], total_matches, has_more}`. **Iterate `result.hits`, NOT `result.matches`** \u2014 the field is `hits`. Each hit has `{span_id, span_name, span_kind, attribute_path, matched_text, context_before, context_after, match_offset}`. Bounded regardless of trace size by max_matches (1..500, default 50). If has_more=true, REFINE the regex rather than blindly raising max_matches."
  ).namespace(NAMESPACE).arg("trace_id", f.string("Real trace id")).arg("regex_pattern", f.string("JS-compatible regex, multiline")).arg("max_matches", f.number("Max records returned, 1..500; default 50").optional()).returns(f.json("SearchTraceResult")).handler(
    async ({ trace_id, regex_pattern, max_matches }) => store.searchTrace({
      trace_id: assertString(trace_id, "trace_id"),
      regex_pattern: assertRegex(regex_pattern),
      max_matches: assertMaxMatches(max_matches)
    })
  ).build();
  const searchSpan = fn("searchSpan").description(
    "Regex search inside a single span. Use when viewSpans returned a 16KB-truncated payload and you need to narrow further. Returns `{trace_id, span_id, hits: SpanMatchRecord[], total_matches, has_more}` \u2014 iterate `result.hits`, NOT `result.matches`."
  ).namespace(NAMESPACE).arg("trace_id", f.string("Real trace id")).arg("span_id", f.string("Real span id within trace")).arg("regex_pattern", f.string("JS-compatible regex, multiline")).arg("max_matches", f.number("Max records, 1..500; default 50").optional()).returns(f.json("SearchSpanResult")).handler(
    async ({ trace_id, span_id, regex_pattern, max_matches }) => store.searchSpan({
      trace_id: assertString(trace_id, "trace_id"),
      span_id: assertString(span_id, "span_id"),
      regex_pattern: assertRegex(regex_pattern),
      max_matches: assertMaxMatches(max_matches)
    })
  ).build();
  return [
    getDatasetOverview,
    queryTraces,
    countTraces,
    viewTrace,
    viewSpans,
    searchTrace,
    searchSpan
  ];
}
function traceAnalystFunctionGroup(opts) {
  return {
    namespace: NAMESPACE,
    title: "Trace Analysis",
    selectionCriteria: "Use for any inspection of OTLP-shaped trace data.",
    description: "Discovery \u2192 narrow \u2192 deep-read tools over a JSONL trace dataset. Always call getDatasetOverview first.",
    functions: buildTraceAnalystTools(opts)
  };
}
function parseFilters(input) {
  if (input == null) return void 0;
  if (typeof input !== "object" || Array.isArray(input)) {
    throw new TypeError(`filters must be an object, got ${typeof input}`);
  }
  const f2 = input;
  const out = {};
  if (typeof f2.has_errors === "boolean") out.has_errors = f2.has_errors;
  out.service_names = stringArrayOrUndefined(f2.service_names, "service_names");
  out.agent_names = stringArrayOrUndefined(f2.agent_names, "agent_names");
  out.model_names = stringArrayOrUndefined(f2.model_names, "model_names");
  out.tool_names = stringArrayOrUndefined(f2.tool_names, "tool_names");
  if (typeof f2.start_time_after === "string") out.start_time_after = f2.start_time_after;
  if (typeof f2.start_time_before === "string") out.start_time_before = f2.start_time_before;
  if (typeof f2.regex_pattern === "string") {
    if (f2.regex_pattern.length === 0) {
      throw new TypeError("filters.regex_pattern cannot be empty");
    }
    out.regex_pattern = f2.regex_pattern;
  }
  return out;
}
function stringArrayOrUndefined(v, label) {
  if (v === void 0 || v === null) return void 0;
  if (!Array.isArray(v)) throw new TypeError(`${label} must be an array of strings`);
  if (v.some((x) => typeof x !== "string")) {
    throw new TypeError(`${label} entries must be strings`);
  }
  return v;
}
function assertPageLimit(limit) {
  if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > 200) {
    throw new RangeError(`limit must be an integer 1..200`);
  }
  return limit;
}
function assertOffset(offset) {
  if (offset === void 0) return void 0;
  if (typeof offset !== "number" || !Number.isInteger(offset) || offset < 0) {
    throw new RangeError(`offset must be a non-negative integer`);
  }
  return offset;
}
function assertRegex(pattern) {
  if (typeof pattern !== "string" || pattern.length === 0) {
    throw new TypeError(`regex_pattern must be a non-empty string`);
  }
  new RegExp(pattern, "m");
  return pattern;
}
function assertMaxMatches(n) {
  if (n === void 0) return void 0;
  if (typeof n !== "number" || !Number.isInteger(n) || n < 1 || n > 500) {
    throw new RangeError(`max_matches must be an integer 1..500`);
  }
  return n;
}
function assertString(v, label) {
  if (typeof v !== "string" || v.length === 0) {
    throw new TypeError(`${label} must be a non-empty string`);
  }
  return v;
}
function assertStringArray(v, label) {
  if (!Array.isArray(v)) throw new TypeError(`${label} must be an array of strings`);
  if (v.some((x) => typeof x !== "string")) {
    throw new TypeError(`${label} entries must be strings`);
  }
  return v;
}

// src/trace-analyst/analyst.ts
import { AxJSRuntime, agent } from "@ax-llm/ax";
async function analyzeTraces(input, options) {
  if (!input.question || typeof input.question !== "string") {
    throw new TypeError("analyzeTraces: input.question must be a non-empty string");
  }
  const store = typeof options.source === "string" ? new OtlpFileTraceStore({ path: options.source }) : options.source;
  if (store instanceof OtlpFileTraceStore) {
    await store.ensureIndexed();
  }
  const tools = buildTraceAnalystTools({ store });
  const turns = [];
  let progressFs;
  if (options.progressLogPath) {
    const { createWriteStream } = await import("fs");
    const { mkdir } = await import("fs/promises");
    const { dirname } = await import("path");
    await mkdir(dirname(options.progressLogPath), { recursive: true });
    progressFs = createWriteStream(options.progressLogPath, { flags: "a" });
  }
  const actorTurnCallback = async (turn) => {
    const snap = {
      turn: turn.turn,
      isError: turn.isError,
      code: turn.code,
      output: turn.output,
      thought: turn.thought
    };
    turns.push(snap);
    if (progressFs) {
      try {
        progressFs.write(`${JSON.stringify({ ...snap, ts: Date.now() })}
`);
      } catch {
      }
    }
    if (options.onTurn) await options.onTurn(snap);
  };
  const maxDepth = options.maxDepth ?? 1;
  const maxTurns = options.maxTurns ?? 12;
  const maxParallelSubagents = options.maxParallelSubagents ?? 2;
  const maxRuntimeChars = options.maxRuntimeChars ?? 6e3;
  const analyst = agent(
    "question:string -> answer:string, findings:string[]",
    {
      agentIdentity: {
        name: "TraceAnalyst",
        description: "Analyzes OTLP-shaped JSONL traces using bounded discovery tools to identify systemic failure modes."
      },
      contextFields: ["question"],
      runtime: new AxJSRuntime({
        permissions: [],
        blockDynamicImport: true,
        allowedModules: [],
        freezeIntrinsics: true,
        blockShadowRealm: true,
        // RLM stdout mode relies on runtime bindings persisting across turns.
        preventGlobalThisExtensions: false
      }),
      mode: maxDepth > 0 ? "advanced" : "simple",
      recursionOptions: maxDepth > 0 ? { maxDepth } : void 0,
      maxTurns,
      maxRuntimeChars,
      maxBatchedLlmQueryConcurrency: maxParallelSubagents,
      promptLevel: "detailed",
      // Trace analysis depends on exact prior tool results and runtime variables.
      contextPolicy: { preset: "full", budget: "balanced" },
      functions: { local: tools },
      actorOptions: {
        description: options.actorDescription ?? TRACE_ANALYST_ACTOR_DESCRIPTION,
        ...options.model ? { model: options.model } : {},
        // Keep actor messages tool-call/content shaped across reasoning models.
        showThoughts: false,
        thinkingTokenBudget: "none"
      },
      responderOptions: {
        ...options.model ? { model: options.model } : {},
        description: options.subagentDescription ?? TRACE_ANALYST_SUBAGENT_DESCRIPTION,
        showThoughts: false
      },
      actorTurnCallback,
      bubbleErrors: [TraceFileMissingError]
    }
  );
  let result;
  try {
    result = await analyst.forward(options.ai, { question: input.question });
  } finally {
    if (progressFs) {
      await new Promise((resolve) => progressFs.end(() => resolve()));
    }
  }
  return {
    answer: typeof result.answer === "string" ? result.answer : String(result.answer ?? ""),
    findings: Array.isArray(result.findings) ? result.findings.filter((s) => typeof s === "string") : [],
    turns,
    turnCount: turns.length,
    usage: normalizeRoleArrays(analyst.getUsage()),
    chatLog: normalizeRoleArrays(analyst.getChatLog()),
    actorPromptVersion: TRACE_ANALYST_ACTOR_DESCRIPTION_VERSION
  };
}
function normalizeRoleArrays(value) {
  const record = value && typeof value === "object" ? value : {};
  return {
    actor: normalizeRecordArray(record.actor),
    responder: normalizeRecordArray(record.responder)
  };
}
function normalizeRecordArray(value) {
  if (!Array.isArray(value)) return [];
  return value.map(
    (item) => item && typeof item === "object" ? { ...item } : { value: item }
  );
}

// src/trace-analyst/hook.ts
var DEFAULT_QUESTION = "Summarise what happened in this run. Surface any failure modes, surprising findings, or evidence that the run's verdict is wrong.";
function traceAnalystOnRunComplete(opts) {
  return async (ctx) => {
    if (opts.shouldRun && !opts.shouldRun(ctx)) return;
    const source = opts.analyze.source;
    if (source === void 0) {
      await ctx.store.appendEvent({
        eventId: `analyst-skip-${ctx.runId}`,
        runId: ctx.runId,
        kind: "log",
        timestamp: Date.now(),
        payload: { source: "trace_analyst_hook", reason: "no source configured" }
      });
      return;
    }
    const result = await analyzeTraces({ question: opts.question ?? DEFAULT_QUESTION }, {
      ...opts.analyze,
      source
    });
    if (opts.save) await opts.save(result, ctx);
    if (opts.gateOn && !opts.gateOn(result, ctx)) {
      await ctx.store.appendEvent({
        eventId: `analyst-gate-${ctx.runId}`,
        runId: ctx.runId,
        kind: "log",
        timestamp: Date.now(),
        payload: {
          source: "trace_analyst_hook",
          reason: "analyst_gate_failed",
          findings: result.findings
        }
      });
    }
  };
}

// src/trace-analyst/insights.ts
var DOMAIN_STOP_WORDS = /* @__PURE__ */ new Set([
  "and",
  "advanced",
  "app",
  "build",
  "create",
  "easy",
  "expert",
  "extreme",
  "for",
  "from",
  "hard",
  "implementation",
  "integrate",
  "medium",
  "project",
  "task",
  "the",
  "this",
  "with",
  "workflow"
]);
function tokenizeDomainWords(value) {
  return [...value.matchAll(/[A-Za-z][A-Za-z0-9.+#-]{2,}/g)].map((match) => match[0].toLowerCase()).filter((word) => !DOMAIN_STOP_WORDS.has(word));
}
function inferDomainKeywords(suite) {
  const suiteWords = new Set(tokenizeDomainWords(`${suite.name} ${suite.collectionId ?? ""}`));
  const source = [
    suite.name,
    suite.collectionId ?? "",
    ...suite.tasks.flatMap((task) => [
      task.id,
      task.name,
      task.prompt ?? "",
      task.difficulty ?? "",
      ...task.tags ?? [],
      ...task.gaps ?? []
    ])
  ].join(" ");
  const counts = /* @__PURE__ */ new Map();
  for (const word of tokenizeDomainWords(source)) counts.set(word, (counts.get(word) ?? 0) + 1);
  return [...counts.entries()].filter(([word, count]) => count >= 2 || suiteWords.has(word)).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).map(([word]) => word).slice(0, 18);
}
function domainEvidencePattern(keywords) {
  const escaped = keywords.filter((keyword) => keyword.length >= 3).map((keyword) => keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
  return escaped.length > 0 ? new RegExp(`(?<![A-Za-z0-9])(?:${escaped.join("|")})(?![A-Za-z0-9])`, "i") : /(?<![A-Za-z0-9])(?:sdk|api|css|dns|xml|provider|client|service|integration|webhook|transaction|auth|oauth|graphql|rest)(?![A-Za-z0-9])/i;
}
function describeTraceInsightScope(suite) {
  const taskLabel = suite.tasks.length === 1 ? "1 implementation task" : `${suite.tasks.length} implementation tasks`;
  const tags = /* @__PURE__ */ new Map();
  for (const task of suite.tasks) {
    for (const tag of task.tags ?? []) tags.set(tag, (tags.get(tag) ?? 0) + 1);
  }
  const topTags = [...tags.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).slice(0, 8).map(([tag]) => tag);
  if (topTags.length > 0) return `${taskLabel} across ${topTags.join(", ")}.`;
  const difficulties = [
    ...new Set(
      suite.tasks.map((task) => task.difficulty).filter((value) => Boolean(value))
    )
  ].join(", ");
  return `${taskLabel} across ${difficulties || "the selected benchmark scope"}.`;
}
function planTraceInsightQuestions(input) {
  const hasFailures = input.suite.tasks.some((task) => task.outcome && task.outcome !== "satisfied");
  const hasMultipleShots = input.suite.tasks.some(
    (task) => (task.gaps ?? []).some((gap) => /shot|review|retry|continue/i.test(gap))
  );
  const questions = [
    {
      id: "execution-path",
      question: "What did the worker actually do before the first meaningful implementation edit?",
      why: "Separates grounded execution from polished but shallow output."
    },
    {
      id: "research-grounding",
      question: "Did the worker inspect docs, source, examples, or package references before committing to an implementation path?",
      why: "Identifies whether failures came from weak retrieval, weak examples, or premature coding."
    },
    {
      id: "domain-proof",
      question: "Which tasks produced executable domain proof versus UI copy, placeholders, or inferred behavior?",
      why: "Keeps product-quality claims tied to concrete evidence."
    },
    {
      id: "root-cause",
      question: "For each major failure cluster, is the likely root cause prompt/scaffold, docs/examples, SDK/API ergonomics, evaluator, runtime, or model behavior?",
      why: "Turns trace observations into actionable ownership."
    },
    {
      id: "evidence-quality",
      question: "Which external-facing claims are directly supported by trace ids, span ids, verifier findings, reviewer notes, or generated code?",
      why: "Prevents unsupported customer-report conclusions."
    }
  ];
  if (hasMultipleShots) {
    questions.push({
      id: "reviewer-lift",
      question: "Where did reviewer feedback improve score, stall, or regress across shots?",
      why: "Shows whether the driver loop is learning or merely repeating work."
    });
  }
  if (hasFailures) {
    questions.push({
      id: "optimization-targets",
      question: "Which prompt, evaluator, scaffold, or workflow changes should feed the next GEPA/autoresearch optimization run?",
      why: "Connects benchmark evidence to the optimization loop."
    });
  }
  return questions;
}
function buildTraceInsightContext(input) {
  return {
    suite: input.suite,
    scope: describeTraceInsightScope(input.suite),
    keywords: inferDomainKeywords(input.suite),
    questions: planTraceInsightQuestions(input),
    panel: defaultTraceInsightPanel(),
    findings: input.findings ?? [],
    agent: input.agent ?? null,
    totals: input.totals ?? null
  };
}
function scoreTraceInsightReadiness(context) {
  const failedTasks = context.suite.tasks.filter(
    (task) => task.outcome && task.outcome !== "satisfied"
  );
  const findingTaskIds = new Set(context.findings.flatMap((finding) => finding.taskIds));
  const failedTasksWithFindings = failedTasks.filter((task) => findingTaskIds.has(task.id));
  const tasksWithGaps = context.suite.tasks.filter((task) => (task.gaps ?? []).length > 0);
  const gates = [
    {
      id: "domain-context",
      label: "Domain context inferred",
      passed: context.keywords.length > 0,
      severity: "high",
      detail: context.keywords.length > 0 ? `${context.keywords.length} domain terms inferred: ${context.keywords.slice(0, 8).join(", ")}` : "No domain terms were inferred from suite, tasks, prompts, tags, or gaps."
    },
    {
      id: "panel-coverage",
      label: "Analyst panel planned",
      passed: context.panel.length >= 4 && context.questions.length >= 5,
      severity: "high",
      detail: `${context.panel.length} panel roles and ${context.questions.length} investigation questions planned.`
    },
    {
      id: "failure-coverage",
      label: "Failures mapped to findings",
      passed: failedTasks.length === 0 || failedTasksWithFindings.length / failedTasks.length >= 0.5,
      severity: "critical",
      detail: failedTasks.length === 0 ? "No failed tasks in suite." : `${failedTasksWithFindings.length}/${failedTasks.length} failed tasks appear in finding clusters.`
    },
    {
      id: "gap-evidence",
      label: "Task gaps captured",
      passed: failedTasks.length === 0 || tasksWithGaps.length / failedTasks.length >= 0.5,
      severity: "medium",
      detail: `${tasksWithGaps.length} tasks include explicit evaluator or analyst gaps.`
    }
  ];
  const penalty = gates.reduce((sum, gate) => {
    if (gate.passed) return sum;
    if (gate.severity === "critical") return sum + 35;
    if (gate.severity === "high") return sum + 20;
    if (gate.severity === "medium") return sum + 10;
    return sum + 5;
  }, 0);
  const score = Math.max(0, Math.min(1, 1 - penalty / 100));
  return {
    score,
    grade: score >= 0.9 ? "external-ready" : score >= 0.7 ? "internal-review" : "raw-analysis",
    gates
  };
}
function defaultTraceInsightPanel() {
  return [
    {
      id: "trace-forensics",
      name: "Trace Forensics",
      responsibility: "Reconstruct what the worker did in order, including research, edits, reviewer interventions, verifier feedback, and stop reason."
    },
    {
      id: "root-cause",
      name: "Root Cause",
      responsibility: "Map failures to prompt/scaffold, docs/examples, SDK/API/product ergonomics, evaluator, runtime, or model behavior."
    },
    {
      id: "optimization",
      name: "Optimization",
      responsibility: "Identify prompt, reviewer, evaluator, scaffold, and GEPA/autoresearch changes that should be tested next."
    },
    {
      id: "external-evidence",
      name: "External Evidence",
      responsibility: "Separate customer-safe claims from internal harness findings and reject conclusions without task, trace, span, code, reviewer, or verifier evidence."
    }
  ];
}
function buildTraceInsightPrompt(input) {
  const context = buildTraceInsightContext(input);
  const maxRepresentativeTraces = input.maxRepresentativeTraces ?? 6;
  return `Analyze this benchmark run and produce evidence-backed trace intelligence.

Audience:
- internal AI/product leadership
- possible customer-facing report for ${input.suite.name}

Investigation plan:
${context.questions.map((item, index) => `${index + 1}. ${item.question} (${item.why})`).join("\n")}

Analyst panel:
${context.panel.map((role) => `- ${role.name}: ${role.responsibility}`).join("\n")}

If the task branches are independent, use subagents for the panel roles above and aggregate their findings. Do not run a panel role unless its answer will change the final report.

Required output:
1. Executive verdict: what this run proves and does not prove.
2. The investigation questions you answered and the evidence used.
3. Failure taxonomy: agent prompting, evaluator/harness, docs/examples, SDK/API/product integration, infra.
4. Evidence-backed examples with trace ids/task ids and concrete verifier findings.
5. Highest-ROI fixes for the benchmark harness, prompt/GEPA optimization, and customer-facing product/docs surface.
6. What is safe for an external report versus what must stay internal.
7. One rerun plan that would validate lift after optimization.

Budget:
- Inspect the dataset overview, the failure summary, and at most ${maxRepresentativeTraces} representative traces.
- Prefer traces named in the failure summary over broad exploration.
- Do not do exhaustive trace sweeps.
- Return the final report as soon as the taxonomy and examples are supported.

Run summary:
${JSON.stringify(
    {
      suite: input.suite.name,
      scope: context.scope,
      inferredKeywords: context.keywords,
      agent: context.agent,
      totals: context.totals,
      findings: context.findings.map((finding) => ({
        kind: finding.kind,
        severity: finding.severity,
        taskCount: finding.taskIds.length,
        proposedFixClass: finding.proposedFixClass
      })),
      failures: input.suite.tasks.filter((task) => task.outcome && task.outcome !== "satisfied").map((task) => ({
        task: task.id,
        difficulty: task.difficulty,
        outcome: task.outcome,
        score: task.score,
        gaps: task.gaps ?? []
      }))
    },
    null,
    2
  )}

Use the trace tools. Do not invent facts. Cite task ids. Separate customer-facing claims from internal harness/model findings.`;
}

// src/trace/store.ts
var InMemoryTraceStore = class {
  runs = /* @__PURE__ */ new Map();
  allSpans = [];
  allEvents = [];
  allArtifacts = [];
  allBudget = [];
  async appendRun(run) {
    if (this.runs.has(run.runId)) throw new Error(`run ${run.runId} already exists`);
    this.runs.set(run.runId, { ...run });
  }
  async updateRun(runId, patch) {
    const existing = this.runs.get(runId);
    if (!existing) throw new Error(`run ${runId} not found`);
    this.runs.set(runId, { ...existing, ...patch });
  }
  async appendSpan(span) {
    this.allSpans.push({ ...span });
  }
  async updateSpan(spanId, patch) {
    const idx = this.allSpans.findIndex((s) => s.spanId === spanId);
    if (idx < 0) throw new Error(`span ${spanId} not found`);
    this.allSpans[idx] = { ...this.allSpans[idx], ...patch };
  }
  async appendEvent(event) {
    this.allEvents.push({ ...event });
  }
  async appendArtifact(artifact) {
    this.allArtifacts.push({ ...artifact });
  }
  async appendBudgetEntry(entry) {
    this.allBudget.push({ ...entry });
  }
  async getRun(runId) {
    const r = this.runs.get(runId);
    return r ? { ...r } : void 0;
  }
  async listRuns(filter = {}) {
    return [...this.runs.values()].filter((r) => matchesRun(r, filter));
  }
  async spans(filter = {}) {
    return this.allSpans.filter((s) => matchesSpan(s, filter)).map((s) => ({ ...s }));
  }
  async events(filter = {}) {
    return this.allEvents.filter((e) => matchesEvent(e, filter)).map((e) => ({ ...e }));
  }
  async budget(runId) {
    return this.allBudget.filter((b) => b.runId === runId).map((b) => ({ ...b }));
  }
  async artifacts(runId) {
    return this.allArtifacts.filter((a) => a.runId === runId).map((a) => ({ ...a }));
  }
};
function matchesRun(r, f2) {
  if (f2.scenarioId && r.scenarioId !== f2.scenarioId) return false;
  if (f2.variantId && r.variantId !== f2.variantId) return false;
  if (f2.status && r.status !== f2.status) return false;
  if (f2.since !== void 0 && r.startedAt < f2.since) return false;
  if (f2.until !== void 0 && r.startedAt > f2.until) return false;
  if (f2.tag && r.tags?.[f2.tag.key] !== f2.tag.value) return false;
  if (f2.parentRunId && r.parentRunId !== f2.parentRunId) return false;
  if (f2.projectId && r.projectId !== f2.projectId) return false;
  if (f2.chatId && r.chatId !== f2.chatId) return false;
  if (f2.layer && r.layer !== f2.layer) return false;
  return true;
}
function matchesSpan(s, f2) {
  if (f2.runId && s.runId !== f2.runId) return false;
  if (f2.parentSpanId && s.parentSpanId !== f2.parentSpanId) return false;
  if (f2.kind && s.kind !== f2.kind) return false;
  if (f2.name && s.name !== f2.name) return false;
  if (f2.toolName && (s.kind !== "tool" || s.toolName !== f2.toolName)) return false;
  if (f2.judgeId && (s.kind !== "judge" || s.judgeId !== f2.judgeId)) return false;
  if (f2.since !== void 0 && s.startedAt < f2.since) return false;
  if (f2.until !== void 0 && s.startedAt > f2.until) return false;
  return true;
}
function matchesEvent(e, f2) {
  if (f2.runId && e.runId !== f2.runId) return false;
  if (f2.spanId && e.spanId !== f2.spanId) return false;
  if (f2.kind && e.kind !== f2.kind) return false;
  if (f2.since !== void 0 && e.timestamp < f2.since) return false;
  if (f2.until !== void 0 && e.timestamp > f2.until) return false;
  return true;
}
var FileSystemTraceStore = class {
  dir;
  maxBytes;
  /** Lazy in-memory index for queries — populated on first read. */
  index;
  loaded = false;
  constructor(options) {
    this.dir = options.dir;
    this.maxBytes = options.maxBytes ?? 32 * 1024 * 1024;
  }
  async ensureDir() {
    const fs = await import("fs/promises");
    await fs.mkdir(this.dir, { recursive: true });
  }
  async append(name, record) {
    await this.ensureDir();
    const fs = await import("fs/promises");
    const path = await import("path");
    const active = path.join(this.dir, `${name}.ndjson`);
    try {
      const stat2 = await fs.stat(active);
      if (stat2.size >= this.maxBytes) {
        const rolled = path.join(this.dir, `${name}.${Date.now()}.ndjson`);
        await fs.rename(active, rolled);
      }
    } catch {
    }
    await fs.appendFile(active, `${JSON.stringify(record)}
`, "utf8");
    if (this.index && !record?._update) {
      void this.insertInto(name, record);
    }
  }
  async insertInto(name, record) {
    if (!this.index) return;
    switch (name) {
      case "runs":
        await this.index.appendRun(record);
        break;
      case "spans":
        await this.index.appendSpan(record);
        break;
      case "events":
        await this.index.appendEvent(record);
        break;
      case "artifacts":
        await this.index.appendArtifact(record);
        break;
      case "budget":
        await this.index.appendBudgetEntry(record);
        break;
    }
  }
  async load() {
    if (this.loaded && this.index) return this.index;
    const fs = await import("fs/promises");
    const path = await import("path");
    const store = new InMemoryTraceStore();
    try {
      const entries = await fs.readdir(this.dir);
      for (const file of entries) {
        if (!file.endsWith(".ndjson")) continue;
        const full = path.join(this.dir, file);
        const content = await fs.readFile(full, "utf8");
        const base = file.split(".")[0];
        for (const line of content.split("\n")) {
          if (!line.trim()) continue;
          const record = JSON.parse(line);
          if (base === "runs") {
            try {
              await store.appendRun(record);
            } catch {
              await store.updateRun(record.runId, record);
            }
          } else if (base === "spans") {
            if (record?._update) {
              try {
                await store.updateSpan(record.spanId, record);
              } catch {
                await store.appendSpan(record);
              }
            } else {
              await store.appendSpan(record);
            }
          } else if (base === "events") {
            await store.appendEvent(record);
          } else if (base === "artifacts") {
            await store.appendArtifact(record);
          } else if (base === "budget") {
            await store.appendBudgetEntry(record);
          }
        }
      }
    } catch {
    }
    this.index = store;
    this.loaded = true;
    return store;
  }
  async appendRun(run) {
    await this.append("runs", run);
  }
  async updateRun(runId, patch) {
    await this.append("runs", { runId, ...patch, _update: true });
    if (this.index) await this.index.updateRun(runId, patch);
  }
  async appendSpan(span) {
    await this.append("spans", span);
  }
  async updateSpan(spanId, patch) {
    await this.append("spans", { spanId, ...patch, _update: true });
    if (this.index) await this.index.updateSpan(spanId, patch);
  }
  async appendEvent(event) {
    await this.append("events", event);
  }
  async appendArtifact(artifact) {
    await this.append("artifacts", artifact);
  }
  async appendBudgetEntry(entry) {
    await this.append("budget", entry);
  }
  async getRun(runId) {
    return (await this.load()).getRun(runId);
  }
  async listRuns(filter) {
    return (await this.load()).listRuns(filter);
  }
  async spans(filter) {
    return (await this.load()).spans(filter);
  }
  async events(filter) {
    return (await this.load()).events(filter);
  }
  async budget(runId) {
    return (await this.load()).budget(runId);
  }
  async artifacts(runId) {
    return (await this.load()).artifacts(runId);
  }
};

// src/trace/otel.ts
var OTEL_AGENT_EVAL_SCOPE = { name: "@tangle-network/agent-eval", version: "0.3.0" };
async function exportRunAsOtlp(store, runId, resourceAttrs = {}) {
  const run = await store.getRun(runId);
  if (!run) throw new Error(`run ${runId} not found`);
  const spans = await store.spans({ runId });
  const events = await store.events({ runId });
  const eventsBySpan = /* @__PURE__ */ new Map();
  for (const e of events) {
    if (!e.spanId) continue;
    const arr = eventsBySpan.get(e.spanId) ?? [];
    arr.push(e);
    eventsBySpan.set(e.spanId, arr);
  }
  const traceId = runToTraceId(run);
  const otlpSpans = spans.map(
    (s) => spanToOtlp(s, traceId, eventsBySpan.get(s.spanId) ?? [])
  );
  return {
    resourceSpans: [
      {
        resource: {
          attributes: toAttributes({
            "service.name": "agent-eval",
            "run.id": run.runId,
            "run.scenario_id": run.scenarioId,
            "run.variant_id": run.variantId ?? "",
            "run.dataset_version": run.datasetVersion ?? "",
            "run.code_sha": run.codeSha ?? "",
            "run.model_fingerprint": run.modelFingerprint ?? "",
            ...resourceAttrs
          })
        },
        scopeSpans: [{ scope: OTEL_AGENT_EVAL_SCOPE, spans: otlpSpans }]
      }
    ]
  };
}
function spanToOtlp(span, traceId, events) {
  const endedAt = span.endedAt ?? span.startedAt;
  return {
    traceId,
    spanId: padSpanId(span.spanId),
    parentSpanId: span.parentSpanId ? padSpanId(span.parentSpanId) : void 0,
    name: span.name,
    kind: 1,
    // SPAN_KIND_INTERNAL
    startTimeUnixNano: msToNs(span.startedAt),
    endTimeUnixNano: msToNs(endedAt),
    attributes: toAttributes(flattenSpanAttributes(span)),
    events: events.map((e) => ({
      timeUnixNano: msToNs(e.timestamp),
      name: e.kind,
      attributes: toAttributes(flattenPayload(e.payload))
    })),
    status: span.status === "error" ? { code: 2, message: span.error } : { code: 1 }
  };
}
function flattenSpanAttributes(span) {
  const base = {
    "span.kind": span.kind
  };
  if (span.kind === "llm") {
    base["llm.model"] = span.model;
    if (span.inputTokens !== void 0) base["llm.input_tokens"] = span.inputTokens;
    if (span.outputTokens !== void 0) base["llm.output_tokens"] = span.outputTokens;
    if (span.costUsd !== void 0) base["llm.cost_usd"] = span.costUsd;
    if (span.finishReason) base["llm.finish_reason"] = span.finishReason;
  } else if (span.kind === "tool") {
    base["tool.name"] = span.toolName;
    if (span.latencyMs !== void 0) base["tool.latency_ms"] = span.latencyMs;
  } else if (span.kind === "retrieval") {
    base["retrieval.query"] = span.query;
    base["retrieval.hits"] = span.hits.length;
  } else if (span.kind === "judge") {
    base["judge.id"] = span.judgeId;
    base["judge.dimension"] = span.dimension;
    base["judge.score"] = span.score;
    base["judge.target_span_id"] = span.targetSpanId;
  } else if (span.kind === "sandbox") {
    if (span.image) base["sandbox.image"] = span.image;
    if (span.exitCode !== void 0) base["sandbox.exit_code"] = span.exitCode;
    if (span.testsPassed !== void 0) base["sandbox.tests_passed"] = span.testsPassed;
    if (span.testsTotal !== void 0) base["sandbox.tests_total"] = span.testsTotal;
  }
  if (span.attributes) {
    for (const [k, v] of Object.entries(span.attributes)) {
      if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") base[k] = v;
    }
  }
  return base;
}
function flattenPayload(payload) {
  const out = {};
  for (const [k, v] of Object.entries(payload)) {
    if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") out[k] = v;
    else out[k] = JSON.stringify(v);
  }
  return out;
}
function toAttributes(record) {
  return Object.entries(record).map(([key, value]) => ({
    key,
    value: typeof value === "number" ? Number.isInteger(value) ? { intValue: value.toString() } : { doubleValue: value } : typeof value === "boolean" ? { boolValue: value } : { stringValue: value }
  }));
}
function msToNs(ms) {
  return (BigInt(Math.floor(ms)) * 1000000n).toString();
}
function padSpanId(id) {
  const cleaned = id.replace(/-/g, "");
  return cleaned.slice(0, 16).padEnd(16, "0");
}
function runToTraceId(run) {
  const cleaned = run.runId.replace(/-/g, "");
  return cleaned.slice(0, 32).padEnd(32, "0");
}

// src/trace/otel-bridge.ts
function otelRunCompleteHook(exporter) {
  return async (ctx) => {
    const spans = await ctx.store.spans({ runId: ctx.runId });
    for (const span of spans) {
      if (span.endedAt) {
        exporter.exportSpan(storeSpanToExportable(span, ctx.runId));
      }
    }
    await exporter.flush();
  };
}
function createOtelTracingStore(inner, exporter, traceId) {
  return {
    async appendRun(run) {
      return inner.appendRun(run);
    },
    async updateRun(runId, patch) {
      return inner.updateRun(runId, patch);
    },
    async appendSpan(span) {
      if (span.endedAt) {
        exporter.exportSpan(storeSpanToExportable(span, traceId));
      }
      return inner.appendSpan(span);
    },
    async updateSpan(spanId, patch) {
      await inner.updateSpan(spanId, patch);
      if (patch.endedAt) {
        const spans = await inner.spans({ runId: traceId });
        const found = spans.find((s) => s.spanId === spanId);
        if (found) {
          exporter.exportSpan(storeSpanToExportable(found, traceId));
        }
      }
    },
    async appendEvent(event) {
      return inner.appendEvent(event);
    },
    async appendBudgetEntry(entry) {
      return inner.appendBudgetEntry(entry);
    },
    async appendArtifact(artifact) {
      return inner.appendArtifact(artifact);
    },
    getRun: inner.getRun.bind(inner),
    listRuns: inner.listRuns.bind(inner),
    spans: inner.spans.bind(inner),
    events: inner.events.bind(inner),
    budget: inner.budget.bind(inner),
    artifacts: inner.artifacts.bind(inner)
  };
}
function storeSpanToExportable(span, traceId) {
  const llm = span.kind === "llm" ? span : void 0;
  return {
    traceId,
    spanId: span.spanId,
    parentSpanId: span.parentSpanId,
    name: span.name,
    kind: span.kind,
    startedAt: span.startedAt,
    endedAt: span.endedAt,
    status: span.status,
    error: span.error,
    model: llm?.model,
    inputTokens: llm?.inputTokens,
    outputTokens: llm?.outputTokens,
    costUsd: llm?.costUsd,
    attributes: span.attributes
  };
}

// src/trace/otel-export.ts
function createOtelExporter(config) {
  const resolvedEndpoint = config?.endpoint ?? (typeof process !== "undefined" ? process.env.OTEL_EXPORTER_OTLP_ENDPOINT : void 0);
  if (!resolvedEndpoint) return void 0;
  const endpoint = resolvedEndpoint;
  const headers = config?.headers ?? parseHeadersFromEnv();
  const batchSize = config?.batchSize ?? 64;
  const flushIntervalMs = config?.flushIntervalMs ?? 5e3;
  const serviceName = config?.serviceName ?? "agent-eval";
  const resourceAttrs = config?.resourceAttributes ?? {};
  const pending = [];
  let timer;
  let stopped = false;
  const exporter = {
    exportSpan(span) {
      if (stopped) return;
      pending.push(toOtlpSpan(span));
      if (pending.length >= batchSize) {
        void doFlush();
      }
    },
    async flush() {
      await doFlush();
    },
    async shutdown() {
      stopped = true;
      if (timer !== void 0) {
        clearInterval(timer);
        timer = void 0;
      }
      await doFlush();
    }
  };
  timer = setInterval(() => {
    if (pending.length > 0) void doFlush();
  }, flushIntervalMs);
  if (typeof timer === "object" && "unref" in timer) {
    ;
    timer.unref();
  }
  async function doFlush() {
    if (pending.length === 0) return;
    const batch = pending.splice(0);
    const body = {
      resourceSpans: [
        {
          resource: {
            attributes: toAttributes2({
              "service.name": serviceName,
              ...resourceAttrs
            })
          },
          scopeSpans: [{ scope: OTEL_AGENT_EVAL_SCOPE, spans: batch }]
        }
      ]
    };
    const url = endpoint.replace(/\/+$/, "") + "/v1/traces";
    try {
      await fetch(url, {
        method: "POST",
        headers: {
          "content-type": "application/json",
          ...headers
        },
        body: JSON.stringify(body)
      });
    } catch {
    }
  }
  return exporter;
}
function parseHeadersFromEnv() {
  if (typeof process === "undefined") return {};
  const raw = process.env.OTEL_EXPORTER_OTLP_HEADERS;
  if (!raw) return {};
  const out = {};
  for (const pair of raw.split(",")) {
    const eq = pair.indexOf("=");
    if (eq < 0) continue;
    const key = pair.slice(0, eq).trim();
    const value = pair.slice(eq + 1).trim();
    if (key) out[key] = value;
  }
  return out;
}
function toOtlpSpan(span) {
  const endedAt = span.endedAt ?? span.startedAt;
  const attrs = {
    "span.kind": span.kind
  };
  if (span.model) attrs["llm.model"] = span.model;
  if (span.inputTokens !== void 0) attrs["llm.input_tokens"] = span.inputTokens;
  if (span.outputTokens !== void 0) attrs["llm.output_tokens"] = span.outputTokens;
  if (span.costUsd !== void 0) attrs["llm.cost_usd"] = span.costUsd;
  if (span.attributes) {
    for (const [k, v] of Object.entries(span.attributes)) {
      if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") attrs[k] = v;
    }
  }
  return {
    traceId: padTraceId(span.traceId),
    spanId: padSpanId2(span.spanId),
    parentSpanId: span.parentSpanId ? padSpanId2(span.parentSpanId) : void 0,
    name: span.name,
    kind: 1,
    // SPAN_KIND_INTERNAL
    startTimeUnixNano: msToNs2(span.startedAt),
    endTimeUnixNano: msToNs2(endedAt),
    attributes: toAttributes2(attrs),
    status: span.status === "error" ? { code: 2, message: span.error } : { code: 1 }
  };
}
function toAttributes2(record) {
  return Object.entries(record).map(([key, value]) => ({
    key,
    value: typeof value === "number" ? Number.isInteger(value) ? { intValue: value.toString() } : { doubleValue: value } : typeof value === "boolean" ? { boolValue: value } : { stringValue: value }
  }));
}
function msToNs2(ms) {
  return (BigInt(Math.floor(ms)) * 1000000n).toString();
}
function padSpanId2(id) {
  const cleaned = id.replace(/-/g, "");
  return cleaned.slice(0, 16).padEnd(16, "0");
}
function padTraceId(id) {
  const cleaned = id.replace(/-/g, "");
  return cleaned.slice(0, 32).padEnd(32, "0");
}

// src/trace/redact.ts
var DEFAULT_REDACTION_RULES = [
  { id: "email", pattern: /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi },
  { id: "ssn", pattern: /\b\d{3}-\d{2}-\d{4}\b/g },
  { id: "credit-card", pattern: /\b(?:\d[ -]*?){13,16}\b/g },
  { id: "phone-us", pattern: /\b(?:\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/g },
  { id: "ipv4", pattern: /\b(?:\d{1,3}\.){3}\d{1,3}\b/g },
  { id: "aws-access-key", pattern: /\bAKIA[0-9A-Z]{16}\b/g },
  { id: "bearer", pattern: /\bBearer\s+[A-Za-z0-9._~+/=-]{10,}/gi },
  { id: "sk-key", pattern: /\bsk-[A-Za-z0-9_-]{10,}\b/g },
  {
    id: "private-key-block",
    pattern: /-----BEGIN (?:RSA |EC |OPENSSH |DSA )?PRIVATE KEY-----[\s\S]*?-----END[^-]*-----/g
  }
];
var REDACTION_VERSION = "1.0.0";
function redactString(input, rules = DEFAULT_REDACTION_RULES) {
  const byRule = {};
  let redactionCount = 0;
  let output = input;
  for (const rule of rules) {
    let hits = 0;
    output = output.replace(rule.pattern, () => {
      hits++;
      return rule.replacement ?? `[redacted:${rule.id}]`;
    });
    if (hits > 0) {
      byRule[rule.id] = hits;
      redactionCount += hits;
    }
  }
  return { output, report: { redactionCount, byRule } };
}
function redactValue(value, rules = DEFAULT_REDACTION_RULES, report = { redactionCount: 0, byRule: {} }) {
  if (typeof value === "string") {
    const { output, report: r } = redactString(value, rules);
    report.redactionCount += r.redactionCount;
    for (const [k, v] of Object.entries(r.byRule)) {
      report.byRule[k] = (report.byRule[k] ?? 0) + v;
    }
    return { value: output, report };
  }
  if (Array.isArray(value)) {
    return {
      value: value.map((v) => redactValue(v, rules, report).value),
      report
    };
  }
  if (value !== null && typeof value === "object") {
    const next = {};
    for (const [k, v] of Object.entries(value)) {
      next[k] = redactValue(v, rules, report).value;
    }
    return { value: next, report };
  }
  return { value, report };
}

// src/replay.ts
var ReplayCacheMissError = class extends ReplayError {
  constructor(url, requestKey2, message) {
    super(message ?? `replay cache miss for ${url} (key=${requestKey2})`);
    this.url = url;
    this.requestKey = requestKey2;
  }
  url;
  requestKey;
};
var ReplayCache = class _ReplayCache {
  byKey = /* @__PURE__ */ new Map();
  orphans = 0;
  byProvider = {};
  byModel = {};
  /**
   * Build a cache from a sink's events. The sink must implement `list()`.
   * Filter by `runId` / `spanId` to scope to a specific replay.
   */
  static async fromSink(sink, filter = {}) {
    if (!sink.list) {
      throw new ReplayError("ReplayCache.fromSink: sink must implement list() to be replayable.");
    }
    const events = await sink.list(filter);
    return _ReplayCache.fromEvents(events);
  }
  /** Build a cache from an in-memory event list. */
  static async fromEvents(events) {
    const cache = new _ReplayCache();
    const groups = /* @__PURE__ */ new Map();
    for (const e of events) {
      const k = `${e.runId ?? ""}::${e.spanId ?? ""}::${e.attemptIndex}`;
      const g = groups.get(k) ?? {};
      if (e.direction === "request") g.req = e;
      else g.res = e;
      groups.set(k, g);
    }
    for (const g of groups.values()) {
      if (!g.req) continue;
      if (!g.res) {
        cache.orphans += 1;
        continue;
      }
      const key = await requestKey(g.req);
      cache.byKey.set(key, { request: g.req, response: g.res });
      cache.byProvider[g.req.provider] = (cache.byProvider[g.req.provider] ?? 0) + 1;
      cache.byModel[g.req.model] = (cache.byModel[g.req.model] ?? 0) + 1;
    }
    return cache;
  }
  /** Number of cacheable (request, response) pairs in the cache. */
  size() {
    return this.byKey.size;
  }
  stats() {
    return {
      total: this.byKey.size,
      byProvider: { ...this.byProvider },
      byModel: { ...this.byModel },
      orphanRequests: this.orphans
    };
  }
  /** Iterate every cached `(request, response)` pair in insertion order. */
  *entries() {
    for (const entry of this.byKey.values()) yield entry;
  }
  /**
   * Look up a cached response by hashing the (model, messages, temperature,
   * maxTokens, response_format) shape. Returns `undefined` on miss; the
   * caller decides whether to throw, fall back to the network, or skip.
   */
  async lookup(requestBody) {
    const key = await keyFromBody(requestBody);
    return this.byKey.get(key);
  }
};
function createReplayFetch(cache, opts = {}) {
  const onMiss = opts.onMiss ?? "throw";
  const fallback = opts.fallbackFetch ?? globalThis.fetch?.bind(globalThis);
  return (async (input, init) => {
    const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
    if (!/\/chat\/completions(?:[?#].*)?$/.test(url)) {
      if (!fallback)
        throw new ReplayError(
          `replay fetch: non-completions URL ${url} but no fallbackFetch configured`
        );
      return fallback(input, init);
    }
    let bodyParsed;
    if (init?.body && typeof init.body === "string") {
      try {
        bodyParsed = JSON.parse(init.body);
      } catch {
      }
    }
    const hit = bodyParsed === void 0 ? void 0 : await cache.lookup(bodyParsed);
    if (hit) {
      opts.onHit?.({ url, provider: hit.request.provider, model: hit.request.model });
      const status = hit.response.statusCode ?? 200;
      const headers = new Headers(
        Object.entries(hit.response.responseHeaders ?? { "Content-Type": "application/json" })
      );
      const bodyText = typeof hit.response.responseBody === "string" ? hit.response.responseBody : JSON.stringify(hit.response.responseBody ?? {});
      return new Response(bodyText, { status, headers });
    }
    opts.onMissNotify?.({ url, requestBody: bodyParsed });
    if (onMiss === "throw") {
      const key = bodyParsed === void 0 ? "<unparseable>" : await keyFromBody(bodyParsed);
      throw new ReplayCacheMissError(url, key);
    }
    if (onMiss === "fail-closed") {
      return new Response(JSON.stringify({ error: "replay_cache_miss" }), { status: 599 });
    }
    if (!fallback)
      throw new ReplayError("replay fetch: onMiss=fallback but no fallbackFetch configured");
    return fallback(input, init);
  });
}
async function* iterateRawCalls(sink, filter = {}) {
  if (!sink.list) {
    throw new ReplayError("iterateRawCalls: sink must implement list().");
  }
  const events = await sink.list(filter);
  const cache = await ReplayCache.fromEvents(events);
  for (const entry of cache.entries()) yield entry;
}
async function requestKey(event) {
  return keyFromBody(event.requestBody);
}
async function keyFromBody(body) {
  if (body == null || typeof body !== "object") return hashJson({ raw: String(body) });
  const b = body;
  const reduced = canonicalize({
    model: b.model ?? null,
    messages: b.messages ?? null,
    temperature: b.temperature ?? null,
    max_tokens: b.max_tokens ?? null,
    max_completion_tokens: b.max_completion_tokens ?? null,
    response_format: b.response_format ?? null
  });
  return hashJson(reduced);
}

export {
  TRACE_ANALYST_ACTOR_DESCRIPTION,
  TRACE_ANALYST_ACTOR_DESCRIPTION_VERSION,
  TRACE_ANALYST_SUBAGENT_DESCRIPTION,
  DEFAULT_TRACE_ANALYST_BUDGETS,
  TRACE_ANALYST_TRUNCATION_MARKER_PREFIX,
  OtlpFileTraceStore,
  TraceFileMissingError,
  TraceNotFoundError,
  SpanNotFoundError,
  buildTraceAnalystTools,
  traceAnalystFunctionGroup,
  analyzeTraces,
  traceAnalystOnRunComplete,
  tokenizeDomainWords,
  inferDomainKeywords,
  domainEvidencePattern,
  describeTraceInsightScope,
  planTraceInsightQuestions,
  buildTraceInsightContext,
  scoreTraceInsightReadiness,
  defaultTraceInsightPanel,
  buildTraceInsightPrompt,
  InMemoryTraceStore,
  FileSystemTraceStore,
  OTEL_AGENT_EVAL_SCOPE,
  exportRunAsOtlp,
  otelRunCompleteHook,
  createOtelTracingStore,
  createOtelExporter,
  DEFAULT_REDACTION_RULES,
  REDACTION_VERSION,
  redactString,
  redactValue,
  ReplayCacheMissError,
  ReplayCache,
  createReplayFetch,
  iterateRawCalls
};
//# sourceMappingURL=chunk-L5UNCDAJ.js.map