import {
  OtlpFileTraceStore,
  TraceFileMissingError,
  buildTraceAnalystTools
} from "./chunk-YBIGNSCZ.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/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(
    // `reasoning!` is an internal (Ax `!`) scratchpad field: generated first to
    // force reason-before-conclude, stripped from the returned output — so the
    // consumed shape stays { answer, findings }. Brings the trace-analyst to the
    // same prose-first CoT ordering the kind-factory gets from its `report` field.
    "question:string -> reasoning!: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 }
  );
}

export {
  TRACE_ANALYST_ACTOR_DESCRIPTION,
  TRACE_ANALYST_ACTOR_DESCRIPTION_VERSION,
  TRACE_ANALYST_SUBAGENT_DESCRIPTION,
  analyzeTraces
};
//# sourceMappingURL=chunk-IZCEK2HR.js.map