Compositor

The AIPCCompositor orchestrates the full enforcement pipeline. It renders deterministic blocks, validates LLM narrative, enforces compliance, assembles the final response, and produces an audit trail.

The 5-stage pipeline

The Compositor runs five stages in sequence:

  1. RENDER — Produces deterministic content blocks from validated output using the Renderer.
  2. VALIDATE — Checks the LLM narrative against contract rules using the Validator.
  3. ENFORCE — Applies enforcement actions based on violation severity. Critical violations suppress narrative. Major violations trigger redaction or retry.
  4. ASSEMBLE — Combines deterministic blocks and (possibly modified) narrative into the final response in a fixed compliance order.
  5. AUDIT — Produces a complete audit trail recording every decision, violation, retry, and enforcement action.

Assembly order

The Compositor assembles the final response in a fixed order. This order is not configurable — it ensures compliance-critical elements always appear in the expected positions:

  1. Attribution (header position)
  2. Freshness warning (if applicable)
  3. Before-data disclosures
  4. Narrative text (LLM-generated, validated)
  5. Data blocks (data points and data tables)
  6. After-data disclosures
  7. Global footer disclosures
  8. Suppression notices
  9. Attribution (footer position)

Retry logic

When the Validator detects major violations, the Compositor can retry the LLM generation with feedback. The retry flow:

  1. Build a modified prompt that includes the original narrative context plus violation feedback (e.g., "Your previous response contained the prohibited phrase 'guaranteed returns'. Regenerate without it.").
  2. Call the retryCallback with the modified prompt.
  3. Re-validate the new narrative.
  4. Repeat up to maxRetries times (default: 2).

If all retries fail, the Compositor falls back to suppressing the narrative and presenting only deterministic blocks.

Output

The Compositor returns a ComplianceEnforcedResponse containing:

FieldDescription
successWhether the final response passed compliance checks.
assembledThe assembled response in multiple formats: plaintext, html, markdown, and structured (array of typed segments).
auditFull compliance audit trail: stages executed, violations found, retries attempted, enforcement actions taken, timestamps.
enforcementLevelThe consumer enforcement level that was applied (L1, L2, or L3).
narrativeSuppressedWhether the LLM narrative was suppressed due to critical violations.

Usage example

import { AIPCCompositor } from '@aipc/runtime';

const response = await AIPCCompositor.enforce({
  validatedOutput: result,
  narrativeText: llmGeneratedText,
  renderConfig: { modality: 'visual', visualFormat: 'html' },
  retryCallback: async (modifiedPrompt) => {
    return await callLLM(modifiedPrompt);
  },
  maxRetries: 2
});

if (response.success) {
  // Display the assembled response
  console.log(response.assembled.html);

  // Inspect the audit trail
  for (const entry of response.audit.entries) {
    console.log(`[${entry.stage}] ${entry.action}: ${entry.detail}`);
  }
} else {
  // Compliance could not be achieved even after retries
  // response.assembled contains only deterministic blocks
  console.log(response.assembled.html);
  console.warn('Narrative suppressed:', response.audit.suppressionReason);
}