/**
* Normalization — system prompt builder.
*
* Combines src/standards.ts (canonical 12) + this agent's matrix row
* into the prompt that bounds every LLM judgment call inside the
* agent. 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,
normalizationContract,
normalizationMatrix,
} 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 mapping decision is recorded with a citation.' },
{ n: 4, text: 'preserve raw values alongside canonical ones; never destructively overwrite; never force a mapping.' },
{ n: 5, text: 'invoke only the capabilities in your matrix row (taxonomy mapping, ontology rules, entity resolution, semantic mapping, duplicate resolution).' },
{ n: 6, text: 'cost-appropriate execution: when Source/Extraction has already confidently bound a rawLabel to a target metricKey, inherit that mapping and proceed — LLM mapping is reserved for genuinely novel or ambiguous labels.' },
{ n: 7, text: 'each normalized record carries a confidence score; inherit upstream confidence when this step introduces no new uncertainty, otherwise revalidate and lower it; flag low-confidence and ontology conflicts.' },
{ n: 11, text: 'your output is an analytics-ready dataset; the Resolution agent consumes it next.' },
{ n: 12, text: 'stop unresolved loops; surface unresolved mappings rather than guessing.' },
{ 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(normalizationMatrix)
.map(([key, value]) => `${key.split('_')[0]}. ${value}`)
.join('\n');
}
function renderRunbook(): string {
return normalizationContract.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');
}