BID · Console
Baseline · Intelligence · Decision
src/repository.ts 3,606 bytes · typescript
/**
 * Standard 10: Repository write-back. Agents declare WHAT to persist;
 * this in-memory repository is what the orchestrator actually writes
 * into. Agents never call this class directly.
 *
 * The snapshot() method serializes the full audit surface for the
 * output/run-*.json file produced by the demo script.
 */

import type { HITLEscalation } from './standards.js';
import type { ConfidenceScore, FailureObject, Handoff, Lineage } from './types.js';

export interface PersistedHandoffRecord {
  readonly id: string;
  readonly agent: string;
  readonly agentVersion: string;
  readonly payload: unknown;
  readonly metadata: Record<string, unknown>;
  readonly lineage: Lineage;
  readonly confidence: ConfidenceScore;
  readonly validationStatus: string;
  readonly writtenAt: string;
}

export interface ExceptionLogEntry {
  readonly agent: string;
  readonly category: string;
  readonly detail: string;
  readonly blocking: boolean;
  readonly occurredAt: string;
}

export interface LearnedRule {
  readonly agent: string;
  readonly ruleKey: string;
  readonly ruleValue: unknown;
  readonly learnedAt: string;
}

export interface HumanOverride {
  readonly agent: string;
  readonly field: string;
  readonly value: unknown;
  readonly overriddenBy: string;
  readonly overriddenAt: string;
}

export interface PersistedFailure {
  readonly at: string;
  readonly failure: FailureObject;
}

export class Repository {
  private records: PersistedHandoffRecord[] = [];
  private exceptions: ExceptionLogEntry[] = [];
  private learnedRules: LearnedRule[] = [];
  private overrides: HumanOverride[] = [];
  private escalations: HITLEscalation[] = [];
  private failures: PersistedFailure[] = [];

  /** Persist a completed handoff and its unresolved-issue tail. */
  writeHandoff(handoff: Handoff<unknown>): void {
    this.records.push({
      id: `${handoff.fromAgent}#${this.records.length + 1}`,
      agent: handoff.fromAgent,
      agentVersion: handoff.fromAgentVersion,
      payload: handoff.payload,
      metadata: handoff.metadata,
      lineage: handoff.lineage,
      confidence: handoff.confidence,
      validationStatus: handoff.validation.status,
      writtenAt: new Date().toISOString(),
    });
    for (const issue of handoff.unresolvedIssues) {
      this.exceptions.push({
        agent: handoff.fromAgent,
        category: issue.category,
        detail: issue.detail,
        blocking: issue.blocking,
        occurredAt: new Date().toISOString(),
      });
    }
  }

  writeException(agent: string, category: string, detail: string, blocking: boolean): void {
    this.exceptions.push({
      agent,
      category,
      detail,
      blocking,
      occurredAt: new Date().toISOString(),
    });
  }

  writeLearnedRule(agent: string, ruleKey: string, ruleValue: unknown): void {
    this.learnedRules.push({ agent, ruleKey, ruleValue, learnedAt: new Date().toISOString() });
  }

  writeHumanOverride(agent: string, field: string, value: unknown, by: string): void {
    this.overrides.push({ agent, field, value, overriddenBy: by, overriddenAt: new Date().toISOString() });
  }

  writeEscalation(esc: HITLEscalation): void {
    this.escalations.push(esc);
  }

  writeFailure(failure: FailureObject): void {
    this.failures.push({ at: new Date().toISOString(), failure });
  }

  snapshot() {
    return {
      records: [...this.records],
      exceptions: [...this.exceptions],
      learnedRules: [...this.learnedRules],
      overrides: [...this.overrides],
      escalations: [...this.escalations],
      failures: [...this.failures],
    };
  }
}