All files / src/util decisionAttention.ts

100% Statements 13/13
100% Branches 19/19
100% Functions 4/4
100% Lines 9/9

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                                  120x 58x   57x     53x 120x 48x                   53x   8x     8x            
import type { ChatMessage, TraceEvent } from "@/objects/Model";
import { isRuleNodeId, ruleEvaluations } from "@/util/ruleTrace";
import { narrowedBySettledWeb } from "@/util/webSearchReport";
 
/**
 * Whether an answer is worth putting its decision record and verdict in
 * front of the person — rather than one hover away.
 *
 * Every answer is recorded, whatever this says: the record is the audit
 * trail, and a complaint arrives about a conversation nobody flagged in
 * advance. This decides only what the chat shows unasked. A greeting shows
 * nothing; an answer something happened to — a rule refused, changed or
 * withheld it, an audit rule flagged it, the quality check failed or could
 * not run — or one already judged, shows its verdict and the way to its
 * record.
 */
export function needsAttention(message: ChatMessage): boolean {
  if (message.verdict) return true;
  if (message.status === "blocked") return true;
  // An Output rule withheld it: what the person reads is the rule's message.
  if (message.meta?.withheld) return true;
  // A check the agent's cost profile skipped (`skipped`) is a choice, not
  // something that happened to the answer: it stays quiet.
  const check = message.meta?.eval_outcome;
  if (check === "failed" || check === "unavailable") return true;
  return rulesActed(message.trace);
}
 
/** A policy or audit rule blocked the turn or did something to it —
 *  redacted, filtered, flagged it for review — as its stored trace says.
 *  A Tools rule counts only when it narrowed one of the turn's web searches
 *  (its settled `detail[action].web`): one that put a source first, or that
 *  no search met, did nothing to the answer worth pointing out — and before
 *  settling it read "applied" on every turn it governed. */
export function rulesActed(trace: TraceEvent[] | null | undefined): boolean {
  return (trace ?? []).some(
    (event) =>
      event.type === "system_node_result" &&
      isRuleNodeId(event.node_id) &&
      ruleEvaluations(event.node_id, event.result).some((rule) =>
        rule.layer === "tools"
          ? narrowedBySettledWeb(rule.detail)
          : rule.outcome === "blocked" || rule.outcome === "applied",
      ),
  );
}