Map processing outcomes to versioned YAML reports
This commit is contained in:
@@ -1,138 +1,180 @@
|
||||
// Package reporting owns the public report schema, separate from processing.
|
||||
package reporting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/internal/core/redaction"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/proposals"
|
||||
stagewarnings "gitea.maximumdirect.net/eric/audita/internal/framework/warnings"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type ProcessReport struct {
|
||||
ReportMetadata ReportMetadata `json:"report_metadata"`
|
||||
Phase string `json:"phase"`
|
||||
Status string `json:"status"`
|
||||
Operation string `json:"operation"`
|
||||
TranscriptPath string `json:"transcript_path"`
|
||||
GlossaryPath string `json:"glossary_path"`
|
||||
OutputPath string `json:"output_path,omitempty"`
|
||||
Modules []string `json:"modules"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
CompletedAt *time.Time `json:"completed_at,omitempty"`
|
||||
ErrorMessage string `json:"error_message,omitempty"`
|
||||
ErrorPhase string `json:"error_phase,omitempty"`
|
||||
InputSegmentCount *int `json:"input_segment_count,omitempty"`
|
||||
NormalizedSegmentCount *int `json:"normalized_segment_count,omitempty"`
|
||||
NormalizationMerges *int `json:"normalization_merges,omitempty"`
|
||||
NormalizationIDReassignments *int `json:"normalization_id_reassignments,omitempty"`
|
||||
NormalizationSkipped NormalizationSkipped `json:"normalization_skipped,omitempty"`
|
||||
Chunking *ChunkingSummary `json:"chunking,omitempty"`
|
||||
Diagnostics *DiagnosticsMetadata `json:"diagnostics,omitempty"`
|
||||
LLMSummary *LLMSummary `json:"llm_summary,omitempty"`
|
||||
ModulesSummary *ModulesSummary `json:"modules_summary,omitempty"`
|
||||
ModuleResults []ModuleReport `json:"module_results,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
DefaultProcessReportSchemaName = "audita-process-report"
|
||||
DefaultProcessReportSchemaVersion = "v2"
|
||||
StatusInProgress = "in_progress"
|
||||
StatusCompleted = "completed"
|
||||
StatusFailed = "failed"
|
||||
)
|
||||
|
||||
type ReportMetadata struct {
|
||||
ReportSchemaName string `json:"report_schema_name"`
|
||||
ReportSchemaVersion string `json:"report_schema_version"`
|
||||
OutputSchema string `json:"output_schema"`
|
||||
ConfigVersion *int `json:"config_version,omitempty"`
|
||||
// Nil counts mean unavailable; a pointer to zero means a known empty count.
|
||||
type Summary struct {
|
||||
WorkUnitsTotal *int `yaml:"work_units_total"`
|
||||
WorkUnitsStarted *int `yaml:"work_units_started"`
|
||||
WorkUnitsCompleted *int `yaml:"work_units_completed"`
|
||||
WorkUnitsInterrupted *int `yaml:"work_units_interrupted"`
|
||||
WorkUnitsNotStarted *int `yaml:"work_units_not_started"`
|
||||
WorkUnitsWithErrors *int `yaml:"work_units_with_errors"`
|
||||
RunErrors *int `yaml:"run_errors"`
|
||||
CorrectionsApplied *int `yaml:"corrections_applied"`
|
||||
LLMOperations *int `yaml:"llm_operations"`
|
||||
ProposalResponses *int `yaml:"proposal_responses"`
|
||||
Recoveries *int `yaml:"recoveries"`
|
||||
ProposalResponsesWithFiltering *int `yaml:"proposal_responses_with_filtering"`
|
||||
}
|
||||
|
||||
type ModuleReport struct {
|
||||
ModuleKey string `json:"module_key"`
|
||||
ModuleInstance string `json:"module_instance"`
|
||||
ReplacementPolicy string `json:"replacement_policy,omitempty"`
|
||||
Status string `json:"status"`
|
||||
ProposalCount int `json:"proposal_count"`
|
||||
Warnings []stagewarnings.StageWarning `json:"warnings,omitempty"`
|
||||
ValidatorDecisions []ValidatorDecisionReport `json:"validator_decisions,omitempty"`
|
||||
ValidatorRejected []ValidatorRejectedReport `json:"validator_rejected,omitempty"`
|
||||
AppliedChanges []proposals.AppliedChange `json:"applied_changes,omitempty"`
|
||||
SkippedChanges []proposals.SkippedChange `json:"skipped_changes,omitempty"`
|
||||
ErrorMessage string `json:"error_message,omitempty"`
|
||||
StartedAt *time.Time `json:"started_at,omitempty"`
|
||||
CompletedAt *time.Time `json:"completed_at,omitempty"`
|
||||
type ExecutionCounts struct {
|
||||
ProviderAttempts *int `yaml:"provider_attempts,omitempty"`
|
||||
TransientRetries *int `yaml:"transient_retries,omitempty"`
|
||||
StructuralRepairs *int `yaml:"structural_repairs,omitempty"`
|
||||
RetriesExhausted *bool `yaml:"retries_exhausted,omitempty"`
|
||||
HTTPStatus *int `yaml:"http_status,omitempty"`
|
||||
}
|
||||
|
||||
type ValidatorDecisionReport struct {
|
||||
ValidatorName string `json:"validator_name"`
|
||||
ProposalIndex int `json:"proposal_index"`
|
||||
Approved bool `json:"approved"`
|
||||
ReasonCode string `json:"reason_code"`
|
||||
Message string `json:"message"`
|
||||
DiagnosticArtifactPath string `json:"diagnostic_artifact_path,omitempty"`
|
||||
type Failure struct {
|
||||
Operation string `yaml:"operation"`
|
||||
Validator string `yaml:"validator,omitempty"`
|
||||
BatchIndex *int `yaml:"batch_index,omitempty"`
|
||||
Reason string `yaml:"reason"`
|
||||
Message string `yaml:"message"`
|
||||
ProposalIndices []int `yaml:"proposal_indices,omitempty"`
|
||||
ExecutionCounts `yaml:",inline"`
|
||||
}
|
||||
|
||||
type ValidatorRejectedReport struct {
|
||||
ValidatorName string `json:"validator_name"`
|
||||
ProposalIndex int `json:"proposal_index"`
|
||||
ModuleKey string `json:"module_key"`
|
||||
ModuleInstance string `json:"module_instance"`
|
||||
TargetSegmentID int `json:"target_segment_id"`
|
||||
OriginalText string `json:"original_text"`
|
||||
CorrectedText string `json:"corrected_text"`
|
||||
ReasonCode string `json:"reason_code"`
|
||||
Message string `json:"message"`
|
||||
type ErrorRecord struct {
|
||||
Scope string `yaml:"scope"`
|
||||
StageIndex *int `yaml:"stage_index,omitempty"`
|
||||
ModuleKey string `yaml:"module_key,omitempty"`
|
||||
ModuleInstance string `yaml:"module_instance,omitempty"`
|
||||
ChunkIndex *int `yaml:"chunk_index,omitempty"`
|
||||
StartSegmentID *int `yaml:"start_segment_id,omitempty"`
|
||||
EndSegmentID *int `yaml:"end_segment_id,omitempty"`
|
||||
CorrectionsApplied *int `yaml:"corrections_applied,omitempty"`
|
||||
Failures []Failure `yaml:"failures,omitempty"`
|
||||
Phase string `yaml:"phase,omitempty"`
|
||||
Reason string `yaml:"reason,omitempty"`
|
||||
Message string `yaml:"message,omitempty"`
|
||||
Operation string `yaml:"operation,omitempty"`
|
||||
Validator string `yaml:"validator,omitempty"`
|
||||
BatchIndex *int `yaml:"batch_index,omitempty"`
|
||||
OutputPath string `yaml:"output_path,omitempty"`
|
||||
}
|
||||
|
||||
type ModulesSummary struct {
|
||||
ModuleCount int `json:"module_count"`
|
||||
TotalAppliedChanges int `json:"total_applied_changes"`
|
||||
TotalSkippedChanges int `json:"total_skipped_changes"`
|
||||
FailedModuleInstance string `json:"failed_module_instance,omitempty"`
|
||||
type FilteredProposal struct {
|
||||
ProposalIndex int `yaml:"proposal_index"`
|
||||
Reason string `yaml:"reason"`
|
||||
Count int `yaml:"count"`
|
||||
}
|
||||
|
||||
type DiagnosticsMetadata struct {
|
||||
DirectoryPath string `json:"directory_path,omitempty"`
|
||||
SourceTranscriptPath string `json:"source_transcript_path,omitempty"`
|
||||
ParsedSourceTranscriptPath string `json:"parsed_source_transcript_path,omitempty"`
|
||||
NormalizedTranscriptPath string `json:"normalized_transcript_path,omitempty"`
|
||||
NormalizationSummaryPath string `json:"normalization_summary_path,omitempty"`
|
||||
ChunkingSummaryPath string `json:"chunking_summary_path,omitempty"`
|
||||
UtilizationSummaryPath string `json:"utilization_summary_path,omitempty"`
|
||||
LLMExecutionPath string `json:"llm_execution_path,omitempty"`
|
||||
SelectedProfilesPath string `json:"selected_profiles_path,omitempty"`
|
||||
CorrectionLedgerPath string `json:"correction_ledger_path,omitempty"`
|
||||
InvocationMetadataPath string `json:"invocation_metadata_path,omitempty"`
|
||||
RedactedEffectiveConfigPath string `json:"redacted_effective_config_path,omitempty"`
|
||||
ErrorLogPath string `json:"error_log_path,omitempty"`
|
||||
type RecoveryRecord struct {
|
||||
StageIndex int `yaml:"stage_index"`
|
||||
ModuleKey string `yaml:"module_key"`
|
||||
ModuleInstance string `yaml:"module_instance"`
|
||||
ChunkIndex int `yaml:"chunk_index"`
|
||||
StartSegmentID int `yaml:"start_segment_id"`
|
||||
EndSegmentID int `yaml:"end_segment_id"`
|
||||
Operation string `yaml:"operation"`
|
||||
Validator string `yaml:"validator,omitempty"`
|
||||
BatchIndex *int `yaml:"batch_index,omitempty"`
|
||||
Reasons []string `yaml:"reasons"`
|
||||
Message string `yaml:"message"`
|
||||
ExecutionCounts `yaml:",inline"`
|
||||
FilteredProposals []FilteredProposal `yaml:"filtered_proposals,omitempty"`
|
||||
}
|
||||
|
||||
type NormalizationSkipped struct {
|
||||
DifferentSpeakers *int `json:"different_speakers,omitempty"`
|
||||
GapTooLarge *int `json:"gap_too_large,omitempty"`
|
||||
DurationExceeded *int `json:"duration_exceeded,omitempty"`
|
||||
TokenLimitExceeded *int `json:"token_limit_exceeded,omitempty"`
|
||||
type ErrorsDocument struct {
|
||||
Schema string `yaml:"schema"`
|
||||
Version int `yaml:"version"`
|
||||
RunID string `yaml:"run_id"`
|
||||
Status string `yaml:"status"`
|
||||
StartedAt string `yaml:"started_at"`
|
||||
CompletedAt *string `yaml:"completed_at"`
|
||||
Summary Summary `yaml:"summary"`
|
||||
Errors []ErrorRecord `yaml:"errors"`
|
||||
}
|
||||
|
||||
type ChunkingSummary struct {
|
||||
ChunkCount int `json:"chunk_count"`
|
||||
MinEstimatedTokens int `json:"min_estimated_chunk_tokens"`
|
||||
MaxEstimatedTokens int `json:"max_estimated_chunk_tokens"`
|
||||
TotalEstimatedTokens int `json:"total_estimated_transcript_tokens"`
|
||||
TargetSections *int `json:"target_sections,omitempty"`
|
||||
MaxSectionTokens int `json:"max_section_tokens"`
|
||||
MinSectionTokens int `json:"min_section_tokens"`
|
||||
type RecoveriesDocument struct {
|
||||
Schema string `yaml:"schema"`
|
||||
Version int `yaml:"version"`
|
||||
RunID string `yaml:"run_id"`
|
||||
Status string `yaml:"status"`
|
||||
StartedAt string `yaml:"started_at"`
|
||||
CompletedAt *string `yaml:"completed_at"`
|
||||
Summary Summary `yaml:"summary"`
|
||||
Recoveries []RecoveryRecord `yaml:"recoveries"`
|
||||
}
|
||||
|
||||
func WriteProcessReport(path string, report ProcessReport, secrets ...string) error {
|
||||
payload, err := redaction.New(secrets).JSON(report)
|
||||
// WorkUnit is payload-free matrix input for report.md.
|
||||
type WorkUnit struct {
|
||||
StageIndex int
|
||||
ModuleKey string
|
||||
ModuleInstance string
|
||||
ChunkIndex int
|
||||
StartSegmentID int
|
||||
EndSegmentID int
|
||||
State string
|
||||
CorrectionsApplied int
|
||||
ErrorCount int
|
||||
RecoveryCount int
|
||||
}
|
||||
|
||||
// Snapshot is built once from processing state and shared by presentations.
|
||||
type Snapshot struct {
|
||||
RunID string
|
||||
Status string
|
||||
StartedAt time.Time
|
||||
CompletedAt *time.Time
|
||||
TranscriptPath string
|
||||
GlossaryPath string
|
||||
OutputPath string
|
||||
StageNames []string
|
||||
WorkUnits []WorkUnit
|
||||
Summary Summary
|
||||
Errors []ErrorRecord
|
||||
Recoveries []RecoveryRecord
|
||||
}
|
||||
|
||||
func (s Snapshot) ErrorsDocument() ErrorsDocument {
|
||||
errors := s.Errors
|
||||
if errors == nil {
|
||||
errors = []ErrorRecord{}
|
||||
}
|
||||
return ErrorsDocument{Schema: "audita-errors", Version: 1, RunID: s.RunID, Status: s.Status, StartedAt: s.StartedAt.UTC().Format(time.RFC3339), CompletedAt: completionTime(s.CompletedAt), Summary: s.Summary, Errors: errors}
|
||||
}
|
||||
|
||||
func (s Snapshot) RecoveriesDocument() RecoveriesDocument {
|
||||
recoveries := s.Recoveries
|
||||
if recoveries == nil {
|
||||
recoveries = []RecoveryRecord{}
|
||||
}
|
||||
return RecoveriesDocument{Schema: "audita-recoveries", Version: 1, RunID: s.RunID, Status: s.Status, StartedAt: s.StartedAt.UTC().Format(time.RFC3339), CompletedAt: completionTime(s.CompletedAt), Summary: s.Summary, Recoveries: recoveries}
|
||||
}
|
||||
|
||||
func completionTime(t *time.Time) *string {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
value := t.UTC().Format(time.RFC3339)
|
||||
return &value
|
||||
}
|
||||
|
||||
// YAML redacts only string nodes after typed serialization.
|
||||
func YAML(document any, secrets ...string) ([]byte, error) {
|
||||
raw, err := yaml.Marshal(document)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal process report JSON: %w", err)
|
||||
return nil, fmt.Errorf("encode report YAML: %w", err)
|
||||
}
|
||||
payload = append(payload, '\n')
|
||||
|
||||
if err := os.WriteFile(path, payload, 0o644); err != nil {
|
||||
return fmt.Errorf("failed to write report JSON file %q: %w", path, err)
|
||||
safe, err := redaction.New(secrets).YAML(raw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("redact report YAML: %w", err)
|
||||
}
|
||||
return nil
|
||||
return safe, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user