Skip to content

prd-7 phase 3: materialize _audit sheet — constraint_score, legal_result, stage_trace per transaction row #57

@promptexecutionerr

Description

@promptexecutionerr

Context

PipelineState<Reconciled> carries confidence, issues: Vec<Issue>, and meta: MetaCtx { accumulated_confidence, stage_trace, flags }. After PRD-7 Phase 0, it also carries legal verification results in issues and constraint evaluations in meta.flags. None of this reaches the workbook — the _audit sheet in crates/ledger-core/src/workbook.rs is a placeholder.

This is the CPA-visible payoff. Without it, the accountant sees no evidence of the verification layer.

Audit Row Schema

One row per committed transaction. Columns:

Column Source Type
entry_id Blake3 of document_id + source_ref String
constraint_score ConstraintEvaluation::to_confidence().0 f32
legal_result first legal_violation issue code or "ok" String
disposition worst Disposition across all issues String
accumulated_confidence meta.accumulated_confidence f32
stage_trace_json serde_json::to_string(&meta.stage_trace) String
flags comma-joined MetaFlag display strings String
invoice_arithmetic_ok InvoiceVerification::arithmetic_ok if present bool
commit_gate CommitGate variant name String

Implementation Notes

  • Add AuditRow struct to crates/ledger-core/src/workbook.rs mirroring the schema above.
  • workbook.rs already uses rust_xlsxwriter (0.94.0). The _audit sheet is written after the transactions sheet in the same Workbook write pass.
  • stage_trace_json column should be narrow (hidden by default via column width = 0 or a note in the write call) so it doesn't clutter the CPA view.
  • flags display: implement std::fmt::Display for MetaFlag in validation.rs.
  • commit_gate column: pass the gate into the row builder; derive the variant name via match (no strum needed since the column is write-only).

Acceptance Criteria

  • workbook.rs writes an _audit sheet with the nine columns above.
  • For a committed transaction with one Z3Result::Violated issue, legal_result column = "legal_violation".
  • For a transaction with CommitGate::Approved { confidence: 0.95 }, commit_gate = "Approved".
  • stage_trace_json is valid JSON parseable by serde_json.
  • Existing workbook tests still pass.

Files

  • crates/ledger-core/src/workbook.rs — primary change site
  • crates/ledger-core/src/validation.rs — add Display for MetaFlag

Dependencies

Logically follows #55 (TransactionFacts population) so legal_result column has real data. Can be implemented against test-fabricated states first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestprd-7Legal Intelligence Layer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions