/**
* Comparisons & Synthesis — system prompt builder.
*
* 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,
comparisonsSynthesisContract,
comparisonsSynthesisMatrix,
} from './matrix.js';
interface OpPrinciple { readonly n: number; readonly text: string; }
const OPERATING_PRINCIPLES: readonly OpPrinciple[] = [
{ n: 1, text: 'produce comparisons only. No narratives, no recommendations.' },
{ n: 4, text: 'never compare across incompatible bases. If a methodology\'s comparability_check fails, refuse the comparison or flag it explicitly — do not compute it anyway.' },
{ n: 5, text: 'query find_methodologies(type="comparison_method") first; apply only declared methods. If none match a request, log a comparabilityFailure and continue.' },
{ n: 7, text: 'per-comparison confidence — inherit upstream confidence when this step introduces no new uncertainty; lower it when the comparability check produced warnings or when statistical context is weak.' },
{ n: 8, text: 'flag statistical anomalies, peer-set construction failures, and comparability failures explicitly.' },
{ n: 0, text: 'Intelligence-forbidden: do not fetch fresh data, do not produce narratives, do not recommend.' },
];
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(comparisonsSynthesisMatrix)
.map(([key, value]) => `${key.split('_')[0]}. ${value}`)
.join('\n');
}
function renderRunbook(): string {
return comparisonsSynthesisContract.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 third agent of the BID Intelligence 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`,
renderUniversal(),
);
}
if (!omitMatrix) {
lines.push(``, `## Your matrix row`, renderMatrix());
}
if (!omitRunbook) {
lines.push(``, `## Your runbook (Std 6)`, renderRunbook());
}
lines.push(``, `## Operating principles`, renderPrinciples(engaged));
return lines.join('\n');
}