BID · Console
Baseline · Intelligence · Decision
src/agents/decision/output-ingestion/prompt.ts 4,243 bytes · typescript
/**
 * Output Ingestion & Interpretation — system prompt builder.
 *
 * Same scoped-prompt machinery as Pillar 2 agents (Std 5 — cost-
 * appropriate prompts inherited from prior pillars per spec §Std 5).
 * Callers pass an optional PromptScope listing the standards their
 * step engages; when given, the prompt is trimmed.
 */

import { STANDARDS_SUMMARY, type PromptScope } from '../../../standards.js';
import {
  AGENT_NAME,
  AGENT_VERSION,
  outputIngestionContract,
  outputIngestionMatrix,
} from './matrix.js';

interface OpPrinciple { readonly n: number; readonly text: string; }

const OPERATING_PRINCIPLES: readonly OpPrinciple[] = [
  { n: 1, text: 'interpret only. Do NOT visualize, render, or deliver — those are downstream agents\' jobs.' },
  { n: 3, text: 'every recommendation traces to a declared rule from the library; record which rule and which conditions matched.' },
  { n: 4, text: 'never invent recommendations beyond what a rule supports. If no rule applies, escalate per Std 9 — do not improvise.' },
  { n: 5, text: 'query find_rules(type="interpretation", agent="output_ingestion") with the appropriate triggers; then get_rule for the full conditions / action / confidence_framework.' },
  { n: 6, text: 'cost-appropriate execution (inherited from Pillar 2): when only one rule matches deterministically, apply it and proceed — no LLM rule-discovery step needed.' },
  { n: 7, text: 'per-recommendation confidence inherits from the source insight and applies the rule\'s confidence_framework adjustments. Reject recommendations below the action threshold.' },
  { n: 8, text: 'flag material-impact findings, rule conflicts, sub-threshold confidence, and audience-policy concerns explicitly.' },
  { n: 9, text: 'escalations route to: domain-expert (rule gaps), authorizing-decision-maker (material findings), compliance-reviewer (disclosure concerns).' },
  { n: 0, text: 'Decision-forbidden: do not re-analyze findings (Pillar 2 output is truth), do not invent rules, do not bypass disclosure policies, do not produce visualizations or dispatches.' },
];

function renderUniversal(engaged?: readonly number[]): string {
  const set = engaged && engaged.length > 0 ? new Set(engaged) : null;
  return STANDARDS_SUMMARY
    .filter(s => !set || set.has(s.n))
    .map(s => `${s.n}. ${s.name} — ${s.gist}`)
    .join('\n');
}

function renderMatrix(): string {
  return Object.entries(outputIngestionMatrix)
    .map(([key, value]) => `${key.split('_')[0]}. ${value}`)
    .join('\n');
}

function renderRunbook(): string {
  return outputIngestionContract.runbook
    .map(s => `  ${s.n}. ${s.name} — ${s.description}`)
    .join('\n');
}

function renderPrinciples(engaged?: readonly number[]): string {
  const set = engaged && engaged.length > 0 ? new Set(engaged) : null;
  return OPERATING_PRINCIPLES
    .filter(p => p.n === 0 || !set || set.has(p.n))
    .map(p => (p.n === 0 ? `- ${p.text}` : `- Std ${p.n}: ${p.text}`))
    .join('\n');
}

export function buildSystemPrompt(scope?: PromptScope): string {
  const engaged = scope?.engagedStandards;
  const isScoped = !!(engaged && engaged.length > 0);
  const omitMatrix = scope?.omitMatrix ?? isScoped;
  const omitRunbook = scope?.omitRunbook ?? isScoped;
  const lines: string[] = [
    `You are the ${AGENT_NAME} agent (v${AGENT_VERSION}) — the first agent of the BID Decision pillar.`,
  ];
  if (isScoped) {
    lines.push(
      `All 12 universal operational standards govern your behavior. ` +
        (scope?.stepLabel ? `Current step: "${scope.stepLabel}". ` : '') +
        `Standards directly engaged by this step:`,
      renderUniversal(engaged),
    );
  } else {
    lines.push(
      `Your operation is bounded by the 12 universal operational standards plus your per-agent matrix row.`,
      ``,
      `## Universal standards (canonical, apply to every agent)`,
      renderUniversal(),
    );
  }
  if (!omitMatrix) {
    lines.push(``, `## Your matrix row (your concrete fill-ins for the 12 standards)`, renderMatrix());
  }
  if (!omitRunbook) {
    lines.push(``, `## Your runbook (Std 6)`, renderRunbook());
  }
  lines.push(``, `## Operating principles`, renderPrinciples(engaged));
  return lines.join('\n');
}