From c167a5e5ab37337f1a2b13f48e5d62f732cd2f8d Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Mon, 23 Nov 2026 04:33:53 +0000 Subject: [PATCH] Retain native prompt execution evidence --- docs/roadmap/implementation.md | 6 ++ internal/framework/llm/coordinate_test.go | 6 +- internal/framework/llm/observations.go | 94 ++++--------------- internal/framework/llm/observations_test.go | 37 ++++++-- .../framework/runner/cancellation_test.go | 2 +- 5 files changed, 59 insertions(+), 86 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 8c6eef2..4dc4cc8 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -318,6 +318,12 @@ group behavior rather than retesting its aggregation algebra or provider decoders. Ordinary-report counts and role progress still function without debug capture. +**Complete.** The collector retains native PromptKit events and group summaries +alongside Audita's logical-call attribution. Focused LLM tests pass for +concurrent attribution, snapshot ownership, coverage, reasoning and cache +usage, cost, and replayed group evidence. The debug serializer and its tests +still consume the former projection and are migrated in the next stage. + ## Stage 6: Serialize the Native Diagnostic Envelope **Outcome:** One debug format exposes native evidence with Audita's stage, diff --git a/internal/framework/llm/coordinate_test.go b/internal/framework/llm/coordinate_test.go index 4289cec..68e2c60 100644 --- a/internal/framework/llm/coordinate_test.go +++ b/internal/framework/llm/coordinate_test.go @@ -61,7 +61,7 @@ func TestCoordinatorFinalTurnFailuresAndAccounting(t *testing.T) { } snapshot := collector.Snapshot() summary := Summarize(snapshot) - if len(snapshot.Calls) != 2 || summary.ProviderAttempts != 2 || !summary.TerminalCoverageComplete { + if len(snapshot.Calls) != 2 || summary.PromptKit.Attempts != 2 || !summary.PromptKit.TerminalCoverageComplete { t.Fatalf("summary=%+v calls=%+v", summary, snapshot.Calls) } for _, call := range snapshot.Calls { @@ -106,7 +106,7 @@ func TestCoordinatorCanceledDeliveryRetainsEvidence(t *testing.T) { if !errors.Is(err, context.Canceled) || deliveries != 1 { t.Fatalf("deliveries=%d err=%v", deliveries, err) } - if got := Summarize(collector.Snapshot()); got.ProviderAttempts != 2 || !got.TerminalCoverageComplete { + if got := Summarize(collector.Snapshot()); got.PromptKit.Attempts != 2 || !got.PromptKit.TerminalCoverageComplete { t.Fatalf("summary=%+v", got) } } @@ -134,7 +134,7 @@ func TestCoordinatorReleasesAdmissionBeforeNestedValidation(t *testing.T) { return &structuredoutput.Request{PromptID: prompts.PromptIDModuleGrammarRepair, CaptureDiagnostics: true, Variables: map[string]string{"RepairRequestJSON": "{}"}}, nil }) summary := Summarize(collector.Snapshot()) - if err != nil || count.Load() != 3 || summary.ProviderAttempts != 3 || summary.LogicalCalls != 3 || !summary.TerminalCoverageComplete { + if err != nil || count.Load() != 3 || summary.PromptKit.Attempts != 3 || summary.LogicalCalls != 3 || !summary.PromptKit.TerminalCoverageComplete { t.Fatalf("calls=%d summary=%+v err=%v", count.Load(), summary, err) } } diff --git a/internal/framework/llm/observations.go b/internal/framework/llm/observations.go index 75e77bd..1df2829 100644 --- a/internal/framework/llm/observations.go +++ b/internal/framework/llm/observations.go @@ -9,47 +9,11 @@ import ( pk "go.maximumdirect.net/promptkit" ) -// TokenCount retains the distinction between zero and unavailable usage. -type TokenCount struct { - Value int `json:"value"` - Known bool `json:"known"` - Complete bool `json:"complete"` -} - -type Usage struct { - Prompt TokenCount `json:"prompt"` - Completion TokenCount `json:"completion"` - Total TokenCount `json:"total"` - Cached TokenCount `json:"cached"` - CacheWrite TokenCount `json:"cache_write"` -} - -// ExecutionObservation contains execution metadata only. It never contains -// endpoint URLs, credentials, messages, provider errors, or generated text. +// ExecutionObservation combines an Audita logical call with native execution evidence. type ExecutionObservation struct { - CallID uint64 `json:"call_id"` - Context structuredoutput.DiagnosticContext `json:"context"` - Kind string `json:"kind"` - RunID string `json:"run_id,omitempty"` - Time time.Time `json:"time"` - BackendID string `json:"backend_id,omitempty"` - ProfileID string `json:"profile_id,omitempty"` - Model string `json:"model,omitempty"` - Generation uint64 `json:"generation,omitempty"` - Attempt uint64 `json:"attempt,omitempty"` - Dispatched bool `json:"dispatched"` - CapacityWait time.Duration `json:"capacity_wait_ns"` - GenerationDuration time.Duration `json:"generation_duration_ns"` - RetryDelay time.Duration `json:"retry_delay_ns"` - Duration time.Duration `json:"duration_ns"` - Outcome string `json:"outcome,omitempty"` - Failure string `json:"failure,omitempty"` - StatusCode int `json:"status_code,omitempty"` - RetriesExhausted bool `json:"retries_exhausted,omitempty"` - Attempts uint64 `json:"attempts"` - TransientRetries uint64 `json:"transient_retries"` - StructuralRepairs uint64 `json:"structural_repairs"` - Usage Usage `json:"usage"` + CallID uint64 `json:"call_id"` + Context structuredoutput.DiagnosticContext `json:"context"` + PromptKit pk.ExecutionEvent `json:"promptkit"` } type LogicalCall struct { @@ -70,12 +34,8 @@ type ObservationSnapshot struct { } type ExecutionSummary struct { - TerminalCoverageComplete bool `json:"terminal_coverage_complete"` - LogicalCalls uint64 `json:"logical_calls"` - ProviderAttempts uint64 `json:"provider_attempts"` - TransientRetries uint64 `json:"transient_retries"` - StructuralRepairs uint64 `json:"structural_repairs"` - Usage Usage `json:"usage"` + LogicalCalls uint64 `json:"logical_calls"` + PromptKit pk.ExecutionGroupSummary `json:"promptkit"` } // ObservationCollector accepts short synchronous callbacks from parallel calls. @@ -140,22 +100,10 @@ func (c *ObservationCollector) recordEvent(callID uint64, context structuredoutp if c == nil { return } - c.group.Observe(event) - count := func(value pk.ObservedTokenCount) TokenCount { - return TokenCount{Value: value.Value, Known: value.Known, Complete: value.Complete} - } - observation := ExecutionObservation{ - CallID: callID, Context: copyContext(context), Kind: string(event.Kind), RunID: event.RunID, - Time: event.Time, BackendID: event.BackendID, ProfileID: event.ProfileID, Model: event.Model, - Generation: event.Generation, Attempt: event.Attempt, Dispatched: event.Dispatched, - CapacityWait: event.CapacityWait, GenerationDuration: event.GenerationDuration, RetryDelay: event.RetryDelay, Duration: event.Duration, - Outcome: string(event.Outcome), Failure: string(event.Failure), StatusCode: event.StatusCode, RetriesExhausted: event.RetriesExhausted, - Attempts: event.Attempts, TransientRetries: event.TransientRetries, StructuralRepairs: event.RepairCalls, - Usage: Usage{Prompt: count(event.Usage.PromptTokens), Completion: count(event.Usage.CompletionTokens), Total: count(event.Usage.TotalTokens), Cached: count(event.Usage.CachedTokens), CacheWrite: count(event.Usage.CacheWriteTokens)}, - } c.mu.Lock() - c.events = append(c.events, observation) - c.mu.Unlock() + defer c.mu.Unlock() + c.group.Observe(event) + c.events = append(c.events, ExecutionObservation{CallID: callID, Context: copyContext(context), PromptKit: event}) } func (c *ObservationCollector) Snapshot() ObservationSnapshot { @@ -178,7 +126,7 @@ func (c *ObservationCollector) Snapshot() ObservationSnapshot { if snapshot.Events[i].CallID != snapshot.Events[j].CallID { return snapshot.Events[i].CallID < snapshot.Events[j].CallID } - return snapshot.Events[i].Time.Before(snapshot.Events[j].Time) + return snapshot.Events[i].PromptKit.Time.Before(snapshot.Events[j].PromptKit.Time) }) group := c.group.Snapshot() snapshot.Group = &group @@ -188,20 +136,14 @@ func (c *ObservationCollector) Snapshot() ObservationSnapshot { // Summarize delegates deduplication and terminal accounting to PromptKit. A // filtered diagnostic view selects its own group; overlapping views are not additive. func Summarize(snapshot ObservationSnapshot) ExecutionSummary { - summary := snapshot.Group - if summary == nil { - var group pk.ExecutionGroup - count := func(c TokenCount) pk.ObservedTokenCount { - return pk.ObservedTokenCount{Value: c.Value, Known: c.Known, Complete: c.Complete} - } - for _, event := range snapshot.Events { - group.Observe(pk.ExecutionEvent{Kind: pk.ExecutionEventKind(event.Kind), RunID: event.RunID, Attempts: event.Attempts, TransientRetries: event.TransientRetries, RepairCalls: event.StructuralRepairs, Usage: pk.ObservedTokenUsage{PromptTokens: count(event.Usage.Prompt), CompletionTokens: count(event.Usage.Completion), TotalTokens: count(event.Usage.Total), CachedTokens: count(event.Usage.Cached), CacheWriteTokens: count(event.Usage.CacheWrite)}}) - } - value := group.Snapshot() - summary = &value + if snapshot.Group != nil { + group := *snapshot.Group + group.Members = append([]pk.ExecutionGroupMember(nil), group.Members...) + return ExecutionSummary{LogicalCalls: uint64(len(snapshot.Calls)), PromptKit: group} } - count := func(c pk.ObservedTokenCount) TokenCount { - return TokenCount{Value: c.Value, Known: c.Known, Complete: c.Complete} + var group pk.ExecutionGroup + for _, event := range snapshot.Events { + group.Observe(event.PromptKit) } - return ExecutionSummary{LogicalCalls: uint64(len(snapshot.Calls)), TerminalCoverageComplete: summary.TerminalCoverageComplete, ProviderAttempts: summary.Attempts, TransientRetries: summary.TransientRetries, StructuralRepairs: summary.RepairCalls, Usage: Usage{Prompt: count(summary.Usage.PromptTokens), Completion: count(summary.Usage.CompletionTokens), Total: count(summary.Usage.TotalTokens), Cached: count(summary.Usage.CachedTokens), CacheWrite: count(summary.Usage.CacheWriteTokens)}} + return ExecutionSummary{LogicalCalls: uint64(len(snapshot.Calls)), PromptKit: group.Snapshot()} } diff --git a/internal/framework/llm/observations_test.go b/internal/framework/llm/observations_test.go index 90f2fec..77c7a9e 100644 --- a/internal/framework/llm/observations_test.go +++ b/internal/framework/llm/observations_test.go @@ -26,7 +26,7 @@ func TestObservationSummarySeparatesLogicalCallsAttemptsAndUsage(t *testing.T) { 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.ProviderAttempts != 0 || summary.Usage.Total != (TokenCount{Known: true, Complete: true}) { + 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) } @@ -41,7 +41,7 @@ func TestObservationSummarySeparatesLogicalCallsAttemptsAndUsage(t *testing.T) { 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.ProviderAttempts != 4 || summary.TransientRetries != 1 || summary.StructuralRepairs != 1 || summary.Usage.Total.Value != 8 || !summary.Usage.Total.Known || summary.Usage.Total.Complete || summary.Usage.Prompt.Value != 5 || !summary.Usage.Prompt.Known || summary.Usage.Prompt.Complete || !summary.Usage.Cached.Known || summary.Usage.Cached.Complete { + 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) @@ -75,7 +75,7 @@ func TestObservationCollectorConcurrentAttributionAndSnapshotCopy(t *testing.T) } wg.Wait() snapshot := collector.Snapshot() - if len(snapshot.Calls) != calls || len(snapshot.Events) != calls || Summarize(snapshot).ProviderAttempts != calls { + 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 { @@ -112,7 +112,7 @@ func TestRoleClientFeedsCollectorAcrossPreparationFailure(t *testing.T) { } snapshot := collector.Snapshot() summary := Summarize(snapshot) - if count.Load() != 1 || summary.LogicalCalls != 2 || summary.ProviderAttempts != 1 || len(snapshot.Events) == 0 { + 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 { @@ -130,12 +130,37 @@ func TestObservationCoverageIsIndependentOfUsage(t *testing.T) { hooks := collector.Hooks() hooks.OnEventWithCall(1, structuredoutput.DiagnosticContext{}, pk.ExecutionEvent{RunID: "pending", Kind: pk.ExecutionAttempt}) got := Summarize(collector.Snapshot()) - if got.TerminalCoverageComplete || got.Usage.Total.Known || got.Usage.Total.Complete { + 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.TerminalCoverageComplete || !got.Usage.Total.Known || !got.Usage.Total.Complete || got.Usage.Total.Value != 7 { + 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) + } +} diff --git a/internal/framework/runner/cancellation_test.go b/internal/framework/runner/cancellation_test.go index 6bde927..67f384b 100644 --- a/internal/framework/runner/cancellation_test.go +++ b/internal/framework/runner/cancellation_test.go @@ -157,7 +157,7 @@ func TestValidatorFailureCancelsActiveAndPendingProviderWork(t *testing.T) { t.Fatalf("unfinished/cause lost: %+v", call) } } - if got := llm.Summarize(snapshot); got.ProviderAttempts != 1 { + if got := llm.Summarize(snapshot); got.PromptKit.Attempts != 1 { t.Fatalf("attempt count: %+v", got) } }