Preserve native diagnostic evidence

This commit is contained in:
2026-11-23 04:39:00 +00:00
parent c167a5e5ab
commit c477bcc388
9 changed files with 172 additions and 105 deletions

View File

@@ -7,7 +7,9 @@ import (
"testing"
"time"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/contracts"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/runner"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/structuredoutput"
pk "go.maximumdirect.net/promptkit"
)
@@ -19,14 +21,14 @@ func TestExecutionReportingKeepsSafeMilliseconds(t *testing.T) {
{CallID: 1, Context: structuredoutput.DiagnosticContext{Role: "proposal", ModuleInstance: "grammar-1"}, Prompt: structuredoutput.PromptMetadata{BackendID: "primary", ProfileID: "draft", Model: "m"}, Started: start, Finished: start.Add(1500 * time.Millisecond), WallDuration: 1500 * time.Millisecond},
{CallID: 2, Context: structuredoutput.DiagnosticContext{Role: "validation"}, Prompt: structuredoutput.PromptMetadata{BackendID: "secondary"}, Started: start, Finished: start.Add(5 * time.Millisecond), WallDuration: 5 * time.Millisecond, Failure: structuredoutput.FailureRuntime},
},
Events: []llm.ExecutionObservation{{CallID: 1, RunID: "run-1", Kind: string(pk.ExecutionFinished), BackendID: "primary", Attempts: 2, TransientRetries: 1, CapacityWait: 20 * time.Millisecond, GenerationDuration: 500 * time.Millisecond, RetryDelay: 100 * time.Millisecond}},
Events: []llm.ExecutionObservation{{CallID: 1, PromptKit: pk.ExecutionEvent{RunID: "run-1", Kind: pk.ExecutionFinished, BackendID: "primary", Attempts: 2, TransientRetries: 1, CapacityWait: 20 * time.Millisecond, GenerationDuration: 500 * time.Millisecond, RetryDelay: 100 * time.Millisecond}}},
}
raw, err := json.Marshal(BuildLLMExecution(snapshot))
if err != nil {
t.Fatal(err)
}
serialized := string(raw)
if !strings.Contains(serialized, `"elapsed_ms":1500`) || !strings.Contains(serialized, `"generation_ms":500`) || strings.Contains(serialized, "_ns") || strings.Contains(serialized, `"attempt":0`) || strings.Contains(serialized, `"section_index":0`) {
if !strings.Contains(serialized, `"elapsed_ms":1500`) || !strings.Contains(serialized, `"promptkit"`) || !strings.Contains(serialized, `"run-1"`) {
t.Fatalf("execution serialization: %s", serialized)
}
if !strings.Contains(serialized, `"calls"`) || !strings.Contains(serialized, `"events"`) {
@@ -34,9 +36,37 @@ func TestExecutionReportingKeepsSafeMilliseconds(t *testing.T) {
}
}
func TestIndexedExecutionRetainsNativeEvidence(t *testing.T) {
section, proposal := 2, 1
ctx := structuredoutput.DiagnosticContext{ModuleInstance: "grammar", SectionIndex: &section, ProposalIndex: &proposal}
event := pk.ExecutionEvent{RunID: "native", Kind: pk.ExecutionFinished,
Usage: pk.ObservedTokenUsage{ReasoningTokens: pk.ObservedTokenCount{Value: 3, Known: true, Complete: true}, CachedTokens: pk.ObservedTokenCount{Known: true, Complete: true}},
CostUSD: pk.ObservedCost{Value: 0.25, Known: true, Complete: true, Source: pk.CostSourceProvider}}
snapshot := llm.ObservationSnapshot{Calls: []llm.LogicalCall{{CallID: 1, Context: ctx}}, Events: []llm.ExecutionObservation{{CallID: 1, Context: ctx, PromptKit: event}}}
indexed := IndexLLMObservations(snapshot, []runner.WorkUnitOutcome{{ModuleInstance: "grammar", Section: contracts.SectionMetadata{Index: section}, ProposalIndexOffset: 4}})
if *indexed.Calls[0].Context.ProposalIndex != 5 || *indexed.Events[0].Context.ProposalIndex != 5 || indexed.Events[0].PromptKit != event {
t.Fatalf("indexed evidence = %+v", indexed)
}
raw, err := json.Marshal(BuildLLMExecution(indexed))
if err != nil {
t.Fatal(err)
}
var decoded struct {
Summary llm.ExecutionSummary `json:"summary"`
Events []llm.ExecutionObservation `json:"events"`
}
if err := json.Unmarshal(raw, &decoded); err != nil {
t.Fatal(err)
}
usage := decoded.Summary.PromptKit.Usage
if usage.ReasoningTokens.Value != 3 || !usage.ReasoningTokens.Known || !usage.CachedTokens.Known || usage.CachedTokens.Value != 0 || decoded.Summary.PromptKit.CostUSD != event.CostUSD || decoded.Events[0].PromptKit.RunID != "native" {
t.Fatalf("serialized native evidence = %+v", decoded)
}
}
func TestReportSummaryAvailability(t *testing.T) {
got := mapLLMSummary(llm.Summarize(llm.ObservationSnapshot{}))
if got.LogicalCalls != 0 || !got.Usage.Total.Known || !got.Usage.Total.Complete {
got := llm.Summarize(llm.ObservationSnapshot{})
if got.LogicalCalls != 0 || !got.PromptKit.Usage.TotalTokens.Known || !got.PromptKit.Usage.TotalTokens.Complete {
t.Fatalf("initialized empty summary: %+v", got)
}
}
@@ -46,7 +76,7 @@ func BenchmarkExecutionReports(b *testing.B) {
snapshot := llm.ObservationSnapshot{}
for i := 0; i < n; i++ {
snapshot.Calls = append(snapshot.Calls, llm.LogicalCall{CallID: uint64(i + 1), Prompt: structuredoutput.PromptMetadata{BackendID: "local"}, Context: structuredoutput.DiagnosticContext{ModuleInstance: "grammar", Role: "proposal"}})
snapshot.Events = append(snapshot.Events, llm.ExecutionObservation{RunID: fmt.Sprint(i), CallID: uint64(i + 1), Kind: string(pk.ExecutionFinished), Attempts: 1})
snapshot.Events = append(snapshot.Events, llm.ExecutionObservation{CallID: uint64(i + 1), PromptKit: pk.ExecutionEvent{RunID: fmt.Sprint(i), Kind: pk.ExecutionFinished, Attempts: 1}})
}
b.Run(fmt.Sprintf("execution/%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
@@ -64,9 +94,9 @@ func TestIndexedReportsPreserveOrderAndPartialUsage(t *testing.T) {
{CallID: 2, Context: structuredoutput.DiagnosticContext{Role: "validation", ModuleInstance: "grammar_1"}, Prompt: structuredoutput.PromptMetadata{BackendID: "a"}},
},
Events: []llm.ExecutionObservation{
{CallID: 1, RunID: "run-1", Kind: string(pk.ExecutionFinished), Attempts: 1, Usage: llm.Usage{Total: llm.TokenCount{Value: 7, Known: true, Complete: true}}},
{CallID: 3, RunID: "run-3", Kind: string(pk.ExecutionFinished), Attempts: 1},
{CallID: 2, RunID: "run-2", Kind: string(pk.ExecutionFinished), Attempts: 1},
{CallID: 1, PromptKit: pk.ExecutionEvent{RunID: "run-1", Kind: pk.ExecutionFinished, Attempts: 1, Usage: pk.ObservedTokenUsage{TotalTokens: pk.ObservedTokenCount{Value: 7, Known: true, Complete: true}}}},
{CallID: 3, PromptKit: pk.ExecutionEvent{RunID: "run-3", Kind: pk.ExecutionFinished, Attempts: 1}},
{CallID: 2, PromptKit: pk.ExecutionEvent{RunID: "run-2", Kind: pk.ExecutionFinished, Attempts: 1}},
},
}
raw, err := json.Marshal(BuildLLMExecution(snapshot))
@@ -74,8 +104,8 @@ func TestIndexedReportsPreserveOrderAndPartialUsage(t *testing.T) {
t.Fatal(err)
}
var execution struct {
Calls []executionCall `json:"calls"`
Events []executionEvent `json:"events"`
Calls []executionCall `json:"calls"`
Events []llm.ExecutionObservation `json:"events"`
}
if err := json.Unmarshal(raw, &execution); err != nil {
t.Fatal(err)
@@ -90,7 +120,7 @@ func TestIndexedReportsPreserveOrderAndPartialUsage(t *testing.T) {
t.Fatal("event order changed")
}
}
if execution.Calls[1].Summary.Usage.Total.Value != 7 || !execution.Calls[1].Summary.Usage.Total.Complete || execution.Calls[0].Summary.Usage.Total.Known {
if execution.Calls[1].PromptKit.Usage.TotalTokens.Value != 7 || !execution.Calls[1].PromptKit.Usage.TotalTokens.Complete || execution.Calls[0].PromptKit.Usage.TotalTokens.Known {
t.Fatalf("call usage: %+v", execution.Calls)
}
}