2026-09-24 02:23:57 +00:00
|
|
|
package llm
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2026-11-15 01:25:55 +00:00
|
|
|
"fmt"
|
2026-09-24 02:23:57 +00:00
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
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:23:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
2026-11-15 01:25:55 +00:00
|
|
|
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}}})
|
2026-09-24 02:23:57 +00:00
|
|
|
summary := Summarize(collector.Snapshot())
|
2026-11-23 04:33:53 +00:00
|
|
|
if summary.LogicalCalls != 2 || summary.PromptKit.Attempts != 0 || summary.PromptKit.Usage.TotalTokens != (pk.ObservedTokenCount{Known: true, Complete: true}) {
|
2026-09-24 02:23:57 +00:00
|
|
|
t.Fatalf("zero-attempt accounting: %+v", summary)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
call(3)
|
|
|
|
|
context := structuredoutput.DiagnosticContext{Role: "validation", ModuleInstance: "grammar", ValidatorName: "editorial_review"}
|
2026-11-15 01:25:55 +00:00
|
|
|
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{
|
2026-09-24 02:23:57 +00:00
|
|
|
PromptTokens: pk.ObservedTokenCount{Value: 5, Known: true, Complete: false},
|
|
|
|
|
TotalTokens: pk.ObservedTokenCount{Value: 8, Known: true, Complete: false},
|
|
|
|
|
}})
|
|
|
|
|
call(4)
|
2026-11-15 01:25:55 +00:00
|
|
|
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}}})
|
2026-09-24 02:23:57 +00:00
|
|
|
snapshot := collector.Snapshot()
|
|
|
|
|
summary = Summarize(snapshot)
|
2026-11-23 04:33:53 +00:00
|
|
|
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 {
|
2026-09-24 02:23:57 +00:00
|
|
|
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})
|
2026-11-15 01:25:55 +00:00
|
|
|
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}}})
|
2026-09-24 02:23:57 +00:00
|
|
|
hooks.OnCall(structuredoutput.CallRecord{CallID: uint64(id), Context: context, Started: started, Finished: time.Now().UTC()})
|
|
|
|
|
index = -1
|
|
|
|
|
}(i)
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
snapshot := collector.Snapshot()
|
2026-11-23 04:33:53 +00:00
|
|
|
if len(snapshot.Calls) != calls || len(snapshot.Events) != calls || Summarize(snapshot).PromptKit.Attempts != calls {
|
2026-09-24 02:23:57 +00:00
|
|
|
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()
|
2026-11-10 18:13:12 +00:00
|
|
|
client, err := runtime.NewRoleClient("proposal", "base", runtime.Config.LLM, collector.Hooks())
|
2026-09-24 02:23:57 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-23 04:32:15 +00:00
|
|
|
request := adapterRequest("proposal")
|
2026-09-24 02:23:57 +00:00
|
|
|
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)
|
2026-11-23 04:33:53 +00:00
|
|
|
if count.Load() != 1 || summary.LogicalCalls != 2 || summary.PromptKit.Attempts != 1 || len(snapshot.Events) == 0 {
|
2026-09-24 02:23:57 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-15 01:25:55 +00:00
|
|
|
|
|
|
|
|
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())
|
2026-11-23 04:33:53 +00:00
|
|
|
if got.PromptKit.TerminalCoverageComplete || got.PromptKit.Usage.TotalTokens.Known || got.PromptKit.Usage.TotalTokens.Complete {
|
2026-11-15 01:25:55 +00:00
|
|
|
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())
|
2026-11-23 04:33:53 +00:00
|
|
|
if got.PromptKit.TerminalCoverageComplete || !got.PromptKit.Usage.TotalTokens.Known || !got.PromptKit.Usage.TotalTokens.Complete || got.PromptKit.Usage.TotalTokens.Value != 7 {
|
2026-11-15 01:25:55 +00:00
|
|
|
t.Fatalf("partial coverage=%+v", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-23 04:33:53 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|