BID · Console
Baseline · Intelligence · Decision
src/agents/baseline/resolution/prompt.ts 3,579 bytes · typescript
/**
 * Resolution — system prompt builder.
 *
 * Combines canonical 12 universal standards (src/standards.ts) with
 * the agent's matrix row. Std 5 — cost-appropriate prompts: 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,
  resolutionContract,
  resolutionMatrix,
} from './matrix.js';

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

const OPERATING_PRINCIPLES: readonly OpPrinciple[] = [
  { n: 3, text: 'explicit, deterministic reasoning where possible; every remediation decision is recorded with a citation.' },
  { n: 4, text: 'preserve parent outputs, preserve the audit trail, never destructively overwrite; prevent recursive loops.' },
  { n: 5, text: 'invoke only the capabilities in your matrix row (exact lookups, rule lookup, fuzzy matchers, AI with citation).' },
  { n: 7, text: 'revalidate after each remediation; flag issues you cannot fix rather than guessing.' },
  { n: 9, text: 'escalate unresolved issues, critical conflicts, sub-threshold confidence, or anything with no approved rule.' },
  { n: 12, text: 'stop recursive remediation, preserve unresolved failure context, escalate critical issues safely.' },
  { n: 0, text: 'Baseline-specific: NO strategic insight, NO benchmarking, NO maturity scoring, NO recommendations.' },
];

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(resolutionMatrix)
    .map(([key, value]) => `${key.split('_')[0]}. ${value}`)
    .join('\n');
}
function renderRunbook(): string {
  return resolutionContract.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}) in the BID Baseline 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');
}