package llm import ( "context" "encoding/json" "fmt" "strings" "sync" "testing" "time" "gitea.maximumdirect.net/eric/audita/v4/internal/framework/structuredoutput" pk "go.maximumdirect.net/promptkit" ) func TestObservationSummarySeparatesLogicalCallsAttemptsAndUsage(t *testing.T) { collector := NewObservationCollector() hooks := collector.Hooks() now := time.Now().UTC() call := func(id uint64) { hooks.OnCall(structuredoutput.CallRecord{CallID: id, Context: structuredoutput.DiagnosticContext{Role: "proposal"}, Started: now}) hooks.OnCall(structuredoutput.CallRecord{CallID: id, Context: structuredoutput.DiagnosticContext{Role: "proposal"}, Started: now, Finished: now.Add(time.Second)}) } call(1) // preparation failed before PromptKit emitted events call(2) // capacity rejected without a dispatched attempt hooks.OnEventWithCall(2, structuredoutput.DiagnosticContext{Role: "proposal"}, pk.ExecutionEvent{RunID: "run-2", Kind: pk.ExecutionAttempt, Dispatched: false}) hooks.OnEventWithCall(2, structuredoutput.DiagnosticContext{Role: "proposal"}, pk.ExecutionEvent{RunID: "run-2", Kind: pk.ExecutionFinished, Attempts: 0, Usage: pk.ObservedTokenUsage{PromptTokens: pk.ObservedTokenCount{Known: true, Complete: true}, CompletionTokens: pk.ObservedTokenCount{Known: true, Complete: true}, TotalTokens: pk.ObservedTokenCount{Known: true, Complete: true}, CachedTokens: pk.ObservedTokenCount{Known: true, Complete: true}, CacheWriteTokens: pk.ObservedTokenCount{Known: true, Complete: true}}}) summary := Summarize(collector.Snapshot()) if summary.LogicalCalls != 2 || summary.PromptKit.Attempts != 0 || summary.PromptKit.Usage.TotalTokens != (pk.ObservedTokenCount{Known: true, Complete: true}) { t.Fatalf("zero-attempt accounting: %+v", summary) } call(3) context := structuredoutput.DiagnosticContext{Role: "validation", ModuleInstance: "grammar", ValidatorName: "editorial_review"} hooks.OnEventWithCall(3, context, pk.ExecutionEvent{RunID: "run-3", Kind: pk.ExecutionAttempt, Dispatched: true, Usage: pk.ObservedTokenUsage{TotalTokens: pk.ObservedTokenCount{Value: 100, Known: true, Complete: true}}}) hooks.OnEventWithCall(3, context, pk.ExecutionEvent{RunID: "run-3", Kind: pk.ExecutionFinished, Attempts: 3, TransientRetries: 1, RepairCalls: 1, Usage: pk.ObservedTokenUsage{ PromptTokens: pk.ObservedTokenCount{Value: 5, Known: true, Complete: false}, TotalTokens: pk.ObservedTokenCount{Value: 8, Known: true, Complete: false}, }}) call(4) hooks.OnEventWithCall(4, context, pk.ExecutionEvent{RunID: "run-4", Kind: pk.ExecutionFinished, Attempts: 1, Usage: pk.ObservedTokenUsage{PromptTokens: pk.ObservedTokenCount{Known: false, Complete: false}, TotalTokens: pk.ObservedTokenCount{Known: false, Complete: false}}}) snapshot := collector.Snapshot() summary = Summarize(snapshot) if summary.LogicalCalls != 4 || summary.PromptKit.Attempts != 4 || summary.PromptKit.TransientRetries != 1 || summary.PromptKit.RepairCalls != 1 || summary.PromptKit.Usage.TotalTokens.Value != 8 || !summary.PromptKit.Usage.TotalTokens.Known || summary.PromptKit.Usage.TotalTokens.Complete || summary.PromptKit.Usage.PromptTokens.Value != 5 || !summary.PromptKit.Usage.PromptTokens.Known || summary.PromptKit.Usage.PromptTokens.Complete || !summary.PromptKit.Usage.CachedTokens.Known || summary.PromptKit.Usage.CachedTokens.Complete { t.Fatalf("partial usage or double count: %+v", summary) } raw, err := json.Marshal(snapshot) if err != nil { t.Fatal(err) } for _, forbidden := range []string{"https://", "Bearer ", "raw_output", "messages", "api_key"} { if strings.Contains(string(raw), forbidden) { t.Fatalf("observation leaked payload field %q", forbidden) } } } func TestObservationCollectorConcurrentAttributionAndSnapshotCopy(t *testing.T) { collector := NewObservationCollector() hooks := collector.Hooks() const calls = 64 var wg sync.WaitGroup for i := 1; i <= calls; i++ { wg.Add(1) go func(id int) { defer wg.Done() index := id context := structuredoutput.DiagnosticContext{Role: "proposal", ModuleInstance: "grammar", SectionIndex: &index} started := time.Now().UTC() hooks.OnCall(structuredoutput.CallRecord{CallID: uint64(id), Context: context, Started: started}) hooks.OnEventWithCall(uint64(id), context, pk.ExecutionEvent{Kind: pk.ExecutionFinished, RunID: fmt.Sprint(id), Attempts: 1, Usage: pk.ObservedTokenUsage{TotalTokens: pk.ObservedTokenCount{Value: 1, Known: true, Complete: true}}}) hooks.OnCall(structuredoutput.CallRecord{CallID: uint64(id), Context: context, Started: started, Finished: time.Now().UTC()}) index = -1 }(i) } wg.Wait() snapshot := collector.Snapshot() if len(snapshot.Calls) != calls || len(snapshot.Events) != calls || Summarize(snapshot).PromptKit.Attempts != calls { t.Fatalf("parallel records: calls=%d events=%d", len(snapshot.Calls), len(snapshot.Events)) } for _, call := range snapshot.Calls { if call.Context.SectionIndex == nil || *call.Context.SectionIndex != int(call.CallID) { t.Fatalf("attribution lost: %+v", call) } } *snapshot.Calls[0].Context.SectionIndex = -2 again := collector.Snapshot() if *again.Calls[0].Context.SectionIndex == -2 { t.Fatal("snapshot aliases collector context") } } func TestRoleClientFeedsCollectorAcrossPreparationFailure(t *testing.T) { runtime, count, _ := adapterFixture(t, []reply{{status: 200, content: `{"corrections":[]}`}}, 0, 0, nil) collector := NewObservationCollector() client, err := runtime.NewRoleClient("proposal", "base", runtime.Config.LLM, collector.Hooks()) if err != nil { t.Fatal(err) } request := adapterRequest("proposal") request.Context.ModuleInstance = "glossary_1" index := 3 request.Context.SectionIndex = &index missing := request missing.PromptID = "missing.prompt" var output map[string]any if _, err := client.CompleteStructured(context.Background(), missing, &output); err == nil { t.Fatal("preparation error missing") } if _, err := client.CompleteStructured(context.Background(), request, &output); err != nil { t.Fatal(err) } snapshot := collector.Snapshot() summary := Summarize(snapshot) if count.Load() != 1 || summary.LogicalCalls != 2 || summary.PromptKit.Attempts != 1 || len(snapshot.Events) == 0 { t.Fatalf("collector accounting: %+v", summary) } for _, event := range snapshot.Events { if event.Context.ModuleInstance != "glossary_1" || event.Context.SectionIndex == nil || *event.Context.SectionIndex != 3 || event.Context.Role != "proposal" { t.Fatalf("event attribution: %+v", event) } } if snapshot.Calls[0].Failure != structuredoutput.FailureRuntime || snapshot.Calls[1].Failure != "" { t.Fatalf("logical failures: %+v", snapshot.Calls) } } func TestObservationCoverageIsIndependentOfUsage(t *testing.T) { collector := NewObservationCollector() hooks := collector.Hooks() hooks.OnEventWithCall(1, structuredoutput.DiagnosticContext{}, pk.ExecutionEvent{RunID: "pending", Kind: pk.ExecutionAttempt}) got := Summarize(collector.Snapshot()) if got.PromptKit.TerminalCoverageComplete || got.PromptKit.Usage.TotalTokens.Known || got.PromptKit.Usage.TotalTokens.Complete { t.Fatalf("incomplete group=%+v", got) } hooks.OnEventWithCall(2, structuredoutput.DiagnosticContext{}, pk.ExecutionEvent{RunID: "finished", Kind: pk.ExecutionFinished, Attempts: 1, Usage: pk.ObservedTokenUsage{TotalTokens: pk.ObservedTokenCount{Value: 7, Known: true, Complete: true}}}) got = Summarize(collector.Snapshot()) if got.PromptKit.TerminalCoverageComplete || !got.PromptKit.Usage.TotalTokens.Known || !got.PromptKit.Usage.TotalTokens.Complete || got.PromptKit.Usage.TotalTokens.Value != 7 { t.Fatalf("partial coverage=%+v", got) } } func TestObservationRetainsNativeEvidence(t *testing.T) { collector := NewObservationCollector() event := pk.ExecutionEvent{ Kind: pk.ExecutionFinished, RunID: "native-run", ParentRunID: "parent", Usage: pk.ObservedTokenUsage{ ReasoningTokens: pk.ObservedTokenCount{Value: 3, Known: true, Complete: true}, CachedTokens: pk.ObservedTokenCount{Value: 0, Known: true, Complete: true}, }, CostUSD: pk.ObservedCost{Value: 0.25, Known: true, Complete: true, Source: pk.CostSourceProvider}, } collector.Hooks().OnEventWithCall(7, structuredoutput.DiagnosticContext{Role: "repair"}, event) snapshot := collector.Snapshot() if len(snapshot.Events) != 1 || snapshot.Events[0].CallID != 7 || snapshot.Events[0].PromptKit != event { t.Fatalf("events = %+v", snapshot.Events) } summary := Summarize(snapshot).PromptKit if !summary.TerminalCoverageComplete || summary.Usage.ReasoningTokens.Value != 3 || !summary.Usage.CachedTokens.Known || summary.CostUSD != event.CostUSD { t.Fatalf("native summary = %+v", summary) } replayed := Summarize(ObservationSnapshot{Events: snapshot.Events}).PromptKit if replayed.CostUSD != summary.CostUSD || replayed.Usage.ReasoningTokens != summary.Usage.ReasoningTokens { t.Fatalf("replayed = %+v", replayed) } }