import fs from "fs"; import os from "os"; import path from "path"; import { afterEach, describe, expect, it } from "vitest"; import { findLatestAgentSemanticAcceptanceSummary, readAgentSemanticSpecSummaryFromFile } from "../src/services/agentSemanticRunRegistry"; const tempRoots: string[] = []; function makeTempRoot(): string { const root = fs.mkdtempSync(path.join(os.tmpdir(), "agent-semantic-registry-")); tempRoots.push(root); return root; } function writeJson(filePath: string, payload: unknown): void { fs.mkdirSync(path.dirname(filePath), { recursive: true }); fs.writeFileSync(filePath, JSON.stringify(payload, null, 2), "utf-8"); } afterEach(() => { while (tempRoots.length > 0) { const root = tempRoots.pop(); if (root && fs.existsSync(root)) { fs.rmSync(root, { recursive: true, force: true }); } } }); describe("agentSemanticRunRegistry", () => { it("reads scenario id and deduplicated semantic tags from the source spec", () => { const root = makeTempRoot(); const specPath = path.resolve(root, "docs", "orchestration", "sample_agent_spec.json"); writeJson(specPath, { scenario_id: "address_truth_harness_demo", domain: "address_demo", title: "Demo AGENT spec", steps: [ { step_id: "step_01", question: "привет", semantic_tags: ["meta_smalltalk", "meta_scope"] }, { step_id: "step_02", question: "остатки", semantic_tags: ["inventory_root", "meta_scope"] } ] }); const summary = readAgentSemanticSpecSummaryFromFile(specPath); expect(summary).toEqual({ scenario_id: "address_truth_harness_demo", domain: "address_demo", title: "Demo AGENT spec", semantic_tags: ["inventory_root", "meta_scope", "meta_smalltalk"], steps_total: 2 }); }); it("picks the newest pack_state for the scenario and exposes acceptance summary", () => { const root = makeTempRoot(); const artifactsRoot = path.resolve(root, "artifacts", "domain_runs"); const olderDir = path.resolve(artifactsRoot, "address_truth_harness_demo_live_20260417"); const olderPackStatePath = path.resolve(olderDir, "pack_state.json"); writeJson(olderPackStatePath, { final_status: "partial", final_status_reason: "review_failures_remaining", review_overall_status: "fail", acceptance_gate_passed: false, critical_path_green: false, unresolved_p0_count: 1, unresolved_p1_count: 0, unresolved_p2_count: 0, steps_total: 4, steps_passed: 2, steps_with_warning: 1, steps_failed: 1, updated_at: "2026-04-17T09:00:00+00:00", invariants: { direct_answer_ok: false, temporal_honesty_ok: true, selected_object_continuity_ok: false, truth_gate_ok: true, human_answer_quality_ok: true, meta_context_integrity_ok: true } }); fs.utimesSync(olderPackStatePath, new Date("2026-04-17T09:00:00.000Z"), new Date("2026-04-17T09:00:00.000Z")); const newerDir = path.resolve(artifactsRoot, "address_truth_harness_demo_live_20260417_rerun2"); const newerPackStatePath = path.resolve(newerDir, "pack_state.json"); writeJson(newerPackStatePath, { final_status: "accepted", final_status_reason: "scenario_acceptance_gate_passed", review_overall_status: "pass", acceptance_gate_passed: true, critical_path_green: true, unresolved_p0_count: 0, unresolved_p1_count: 0, unresolved_p2_count: 0, steps_total: 4, steps_passed: 4, steps_with_warning: 0, steps_failed: 0, updated_at: "2026-04-17T09:10:00+00:00", invariants: { direct_answer_ok: true, temporal_honesty_ok: true, selected_object_continuity_ok: true, truth_gate_ok: true, human_answer_quality_ok: true, meta_context_integrity_ok: true } }); fs.utimesSync(newerPackStatePath, new Date("2026-04-17T09:10:00.000Z"), new Date("2026-04-17T09:10:00.000Z")); const summary = findLatestAgentSemanticAcceptanceSummary({ artifactsRootDir: artifactsRoot, repoRootDir: root, scenarioId: "address_truth_harness_demo" }); expect(summary).not.toBeNull(); expect(summary?.final_status).toBe("accepted"); expect(summary?.relative_output_dir).toBe("artifacts/domain_runs/address_truth_harness_demo_live_20260417_rerun2"); expect(summary?.steps_passed).toBe(4); expect(summary?.unresolved_p0_count).toBe(0); expect(summary?.invariants.meta_context_integrity_ok).toBe(true); }); });