All files / src/util systemNodeTrace.ts

100% Statements 54/54
97.7% Branches 85/87
100% Functions 19/19
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                                          6x       112x 72x         228x           16x 8x 8x 8x 6x                   17x 11x   11x     17x             17x       17x       152x           103x               13x 13x 13x 6x   8x 8x                     13x   4x 4x         13x 13x                                 6x 6x 5x         6x 6x                           21x 21x 18x 21x                           2x                   12x 12x 5x 5x 9x 9x 8x               5x    
import {
  KnowledgeMix,
  KnowledgeMixLane,
  KnowledgePlanResult,
  LanePacking,
  LaneSearchResult,
  RetrievalReport,
  TriageChoice,
} from "@/objects/Model";
 
/**
 * Readers for what a system node's trace frame says about how it decided:
 * the route the gate's triage chose before the gate changed it, and what the
 * retrieval pipeline ran without. Both arrive as untyped JSON, and stored
 * traces from before either key have neither, so each reader returns null
 * rather than a half-filled object when there is nothing to show.
 */
 
/** A route as svc-ai's trace writes it (`graph._route_label`):
 *  `retrieve · both`, `direct`. */
export function routeLabel(route: string, retrievalType: string): string {
  return route === "retrieve" && retrievalType ? `${route} · ${retrievalType}` : route;
}
 
function lines(raw: unknown): string[] {
  return Array.isArray(raw)
    ? raw.filter((s): s is string => typeof s === "string" && s.trim() !== "")
    : [];
}
 
function text(raw: unknown): string {
  return typeof raw === "string" ? raw.trim() : "";
}
 
/** The gate frame's `triage`, or null when the frame has none (a trace stored
 *  before it, a trivial turn) or it names no route. */
export function triageChoice(raw: unknown): Required<TriageChoice> | null {
  if (!raw || typeof raw !== "object") return null;
  const t = raw as Record<string, unknown>;
  const route = text(t.route);
  if (!route) return null;
  return {
    route,
    retrieval_type: route === "retrieve" ? text(t.retrieval_type) : "",
    source: t.source === "fallback" ? "fallback" : "triage",
    adjustments: lines(t.adjustments),
  };
}
 
/** The retrieval frame's `report`, or null when nothing in it is set. */
export function retrievalReport(raw: unknown): Required<RetrievalReport> | null {
  if (!raw || typeof raw !== "object") return null;
  const r = raw as Record<string, unknown>;
  const judgedOut =
    typeof r.judged_out === "number" && Number.isFinite(r.judged_out) && r.judged_out > 0
      ? Math.floor(r.judged_out)
      : 0;
  const report = {
    planner_failure: text(r.planner_failure),
    judge_failure: text(r.judge_failure),
    degraded: lines(r.degraded),
    judged_out: judgedOut,
  };
  const anything =
    report.planner_failure !== "" ||
    report.judge_failure !== "" ||
    report.degraded.length > 0 ||
    report.judged_out > 0;
  return anything ? report : null;
}
 
function count(raw: unknown): number {
  return typeof raw === "number" && Number.isFinite(raw) && raw > 0
    ? Math.floor(raw)
    : 0;
}
 
function record(raw: unknown): Record<string, unknown> | null {
  return raw && typeof raw === "object" && !Array.isArray(raw)
    ? (raw as Record<string, unknown>)
    : null;
}
 
/** A turn's knowledge mix (the gate's `knowledge_mix`, the plan's `mix`),
 *  or null when there is none to read (a trace from before it). */
export function knowledgeMix(raw: unknown): KnowledgeMix | null {
  const m = record(raw);
  const profile = m ? text(m.profile) : "";
  if (!m || !profile) return null;
  const lanes: KnowledgeMixLane[] = (Array.isArray(m.lanes) ? m.lanes : [])
    .map(record)
    .filter((l): l is Record<string, unknown> => !!l && !!text(l.category))
    .map((l) => ({
      category: text(l.category),
      role: text(l.role),
      kb_ids: lines(l.kb_ids),
      share:
        typeof l.share === "number" && Number.isFinite(l.share) ? l.share : 0,
      candidate_k: count(l.candidate_k),
      judge_slots: count(l.judge_slots),
      max_queries: count(l.max_queries),
      reason: text(l.reason),
    }));
  const scoped = (Array.isArray(m.scoped_out) ? m.scoped_out : [])
    .map(record)
    .filter((s): s is Record<string, unknown> => !!s && !!text(s.category))
    .map((s) => ({
      category: text(s.category),
      kb_ids: lines(s.kb_ids),
      reason: text(s.reason),
    }));
  const signals = record(m.signals) ?? {};
  return {
    profile,
    modifiers: lines(m.modifiers),
    lanes,
    scoped_out: scoped,
    signals: {
      live: signals.live === true,
      complexity: text(signals.complexity) || null,
      window_days: count(signals.window_days) || null,
      web_led: signals.web_led === true,
      named: lines(signals.named),
    },
  };
}
 
/** `__knowledge_plan__`'s frame, or null when it is not one. */
export function knowledgePlanResult(raw: unknown): KnowledgePlanResult | null {
  const r = record(raw);
  if (!r || !text(r.mode)) return null;
  return {
    mode: text(r.mode),
    mix: knowledgeMix(r.mix),
    lanes: (Array.isArray(r.lanes) ? r.lanes : [])
      .map(record)
      .filter((l): l is Record<string, unknown> => !!l && !!text(l.category))
      .map((l) => ({
        category: text(l.category),
        role: text(l.role),
        kb_ids: lines(l.kb_ids),
        queries: lines(l.queries),
      })),
    planner_failure: text(r.planner_failure),
    gaps: lines(r.gaps),
    error: text(r.error),
  };
}
 
/** A lane's frame, or null when it is not one. */
export function laneSearchResult(raw: unknown): LaneSearchResult | null {
  const r = record(raw);
  if (!r || !text(r.category)) return null;
  const timings = record(r.timings_ms) ?? {};
  return {
    category: text(r.category),
    role: text(r.role),
    kb_ids: lines(r.kb_ids),
    queries: lines(r.queries),
    found: count(r.found),
    judged: count(r.judged),
    ruled_out: count(r.ruled_out),
    out_of_window: count(r.out_of_window),
    handed_on: count(r.handed_on),
    judge_failure: text(r.judge_failure),
    degraded: lines(r.degraded),
    timings_ms: Object.fromEntries(
      Object.entries(timings).filter(
        (e): e is [string, number] => typeof e[1] === "number",
      ),
    ),
    error: text(r.error),
  };
}
 
/** The join's `lanes`: how each lane fared in packing, by its kind. Empty
 *  on a trace from before the lanes, and on a join with no knowledge. */
export function lanePacking(raw: unknown): Record<string, LanePacking> {
  const r = record(raw);
  if (!r) return {};
  const out: Record<string, LanePacking> = {};
  for (const [kind, value] of Object.entries(r)) {
    const v = record(value);
    if (!v) continue;
    out[kind] = {
      role: text(v.role),
      budget: count(v.budget),
      used: count(v.used),
      packed: count(v.packed),
      spilled_in: count(v.spilled_in),
    };
  }
  return out;
}