2026-09-24 02:31:37 +00:00
|
|
|
package processreport
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2026-09-24 15:32:13 +00:00
|
|
|
"fmt"
|
2026-09-24 02:31:37 +00:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-11-23 04:39:00 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/contracts"
|
2026-11-17 19:57:22 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm"
|
2026-11-23 04:39:00 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/runner"
|
2026-11-17 19:57:22 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/structuredoutput"
|
2026-11-10 18:13:12 +00:00
|
|
|
pk "go.maximumdirect.net/promptkit"
|
2026-09-24 02:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
2026-11-10 20:26:43 +00:00
|
|
|
func TestExecutionReportingKeepsSafeMilliseconds(t *testing.T) {
|
2026-09-24 02:31:37 +00:00
|
|
|
start := time.Now().UTC()
|
|
|
|
|
snapshot := llm.ObservationSnapshot{
|
|
|
|
|
Calls: []llm.LogicalCall{
|
|
|
|
|
{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},
|
|
|
|
|
},
|
2026-11-23 04:39:00 +00:00
|
|
|
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}}},
|
2026-09-24 02:31:37 +00:00
|
|
|
}
|
|
|
|
|
raw, err := json.Marshal(BuildLLMExecution(snapshot))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
serialized := string(raw)
|
2026-11-23 04:39:00 +00:00
|
|
|
if !strings.Contains(serialized, `"elapsed_ms":1500`) || !strings.Contains(serialized, `"promptkit"`) || !strings.Contains(serialized, `"run-1"`) {
|
2026-09-24 02:31:37 +00:00
|
|
|
t.Fatalf("execution serialization: %s", serialized)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(serialized, `"calls"`) || !strings.Contains(serialized, `"events"`) {
|
|
|
|
|
t.Fatal(serialized)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 04:39:00 +00:00
|
|
|
func TestIndexedExecutionRetainsNativeEvidence(t *testing.T) {
|
|
|
|
|
section, proposal := 2, 1
|
|
|
|
|
ctx := structuredoutput.DiagnosticContext{ModuleInstance: "grammar", SectionIndex: §ion, 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 02:31:37 +00:00
|
|
|
func TestReportSummaryAvailability(t *testing.T) {
|
2026-11-23 04:39:00 +00:00
|
|
|
got := llm.Summarize(llm.ObservationSnapshot{})
|
|
|
|
|
if got.LogicalCalls != 0 || !got.PromptKit.Usage.TotalTokens.Known || !got.PromptKit.Usage.TotalTokens.Complete {
|
2026-09-24 02:31:37 +00:00
|
|
|
t.Fatalf("initialized empty summary: %+v", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-24 15:32:13 +00:00
|
|
|
|
|
|
|
|
func BenchmarkExecutionReports(b *testing.B) {
|
|
|
|
|
for _, n := range []int{1000, 10000, 20000} {
|
|
|
|
|
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"}})
|
2026-11-23 04:39:00 +00:00
|
|
|
snapshot.Events = append(snapshot.Events, llm.ExecutionObservation{CallID: uint64(i + 1), PromptKit: pk.ExecutionEvent{RunID: fmt.Sprint(i), Kind: pk.ExecutionFinished, Attempts: 1}})
|
2026-09-24 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
b.Run(fmt.Sprintf("execution/%d", n), func(b *testing.B) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
BuildLLMExecution(snapshot)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIndexedReportsPreserveOrderAndPartialUsage(t *testing.T) {
|
|
|
|
|
snapshot := llm.ObservationSnapshot{
|
|
|
|
|
Calls: []llm.LogicalCall{
|
|
|
|
|
{CallID: 3, Context: structuredoutput.DiagnosticContext{Role: "validation", ModuleInstance: "grammar_2"}, Prompt: structuredoutput.PromptMetadata{BackendID: "b"}},
|
|
|
|
|
{CallID: 1, Context: structuredoutput.DiagnosticContext{Role: "proposal", ModuleInstance: "grammar_1"}, Prompt: structuredoutput.PromptMetadata{BackendID: "a"}},
|
|
|
|
|
{CallID: 2, Context: structuredoutput.DiagnosticContext{Role: "validation", ModuleInstance: "grammar_1"}, Prompt: structuredoutput.PromptMetadata{BackendID: "a"}},
|
|
|
|
|
},
|
|
|
|
|
Events: []llm.ExecutionObservation{
|
2026-11-23 04:39:00 +00:00
|
|
|
{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}},
|
2026-09-24 15:32:13 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
raw, err := json.Marshal(BuildLLMExecution(snapshot))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var execution struct {
|
2026-11-23 04:39:00 +00:00
|
|
|
Calls []executionCall `json:"calls"`
|
|
|
|
|
Events []llm.ExecutionObservation `json:"events"`
|
2026-09-24 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
if err := json.Unmarshal(raw, &execution); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for i, call := range snapshot.Calls {
|
|
|
|
|
if execution.Calls[i].CallID != call.CallID {
|
|
|
|
|
t.Fatal("call order changed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for i, event := range snapshot.Events {
|
|
|
|
|
if execution.Events[i].CallID != event.CallID {
|
|
|
|
|
t.Fatal("event order changed")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-23 04:39:00 +00:00
|
|
|
if execution.Calls[1].PromptKit.Usage.TotalTokens.Value != 7 || !execution.Calls[1].PromptKit.Usage.TotalTokens.Complete || execution.Calls[0].PromptKit.Usage.TotalTokens.Known {
|
2026-09-24 15:32:13 +00:00
|
|
|
t.Fatalf("call usage: %+v", execution.Calls)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-23 16:56:44 +00:00
|
|
|
|
|
|
|
|
func TestPerCallGroupsRetainCoverageAndDeduplicateTerminals(t *testing.T) {
|
|
|
|
|
terminal := pk.ExecutionEvent{RunID: "complete", Kind: pk.ExecutionFinished, Attempts: 2, TransientRetries: 1}
|
|
|
|
|
snapshot := llm.ObservationSnapshot{
|
|
|
|
|
Calls: []llm.LogicalCall{{CallID: 3}, {CallID: 1}, {CallID: 2}},
|
|
|
|
|
Events: []llm.ExecutionObservation{
|
|
|
|
|
{CallID: 1, PromptKit: pk.ExecutionEvent{RunID: "pending", Kind: pk.ExecutionAttempt}},
|
|
|
|
|
{CallID: 2, PromptKit: terminal}, {CallID: 2, PromptKit: terminal},
|
|
|
|
|
{CallID: 1, PromptKit: pk.ExecutionEvent{RunID: "pending", Kind: pk.ExecutionAttempt}},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
raw, err := json.Marshal(BuildLLMExecution(snapshot))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var report struct {
|
|
|
|
|
Calls []executionCall
|
|
|
|
|
Events []llm.ExecutionObservation
|
|
|
|
|
}
|
|
|
|
|
if err := json.Unmarshal(raw, &report); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(report.Events) != 4 || len(report.Calls) != 3 {
|
|
|
|
|
t.Fatal(string(raw))
|
|
|
|
|
}
|
|
|
|
|
empty, pending, complete := report.Calls[0], report.Calls[1], report.Calls[2]
|
|
|
|
|
if empty.CallID != 3 || len(empty.PromptKit.Members) != 0 || !empty.PromptKit.TerminalCoverageComplete || !empty.PromptKit.Usage.TotalTokens.Known {
|
|
|
|
|
t.Fatalf("empty: %+v", empty)
|
|
|
|
|
}
|
|
|
|
|
if pending.CallID != 1 || pending.PromptKit.TerminalCoverageComplete || pending.PromptKit.Usage.TotalTokens.Known {
|
|
|
|
|
t.Fatalf("pending: %+v", pending)
|
|
|
|
|
}
|
|
|
|
|
if complete.CallID != 2 || complete.PromptKit.Attempts != 2 || complete.PromptKit.TransientRetries != 1 || !complete.PromptKit.TerminalCoverageComplete {
|
|
|
|
|
t.Fatalf("complete: %+v", complete)
|
|
|
|
|
}
|
|
|
|
|
}
|