Files
audita/internal/framework/processreport/llm.go

80 lines
3.2 KiB
Go
Raw Normal View History

package processreport
import (
"time"
2026-11-17 19:57:22 +00:00
"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"
2026-11-23 04:39:00 +00:00
pk "go.maximumdirect.net/promptkit"
)
type executionCall struct {
2026-11-23 04:39:00 +00:00
RequestedPromptID string `json:"requested_prompt_id,omitempty"`
CallID uint64 `json:"call_id"`
Context structuredoutput.DiagnosticContext `json:"context"`
2026-11-23 04:39:00 +00:00
Prompt structuredoutput.PromptMetadata `json:"prompt"`
Started time.Time `json:"started"`
Finished *time.Time `json:"finished,omitempty"`
ElapsedMS int64 `json:"elapsed_ms"`
Failure structuredoutput.FailureKind `json:"failure,omitempty"`
PromptKit pk.ExecutionGroupSummary `json:"promptkit"`
}
2026-11-23 04:39:00 +00:00
// BuildLLMExecution preserves native evidence with Audita's logical-call context.
func BuildLLMExecution(snapshot llm.ObservationSnapshot) any {
calls := make([]executionCall, 0, len(snapshot.Calls))
2026-11-23 04:39:00 +00:00
events := make([]llm.ExecutionObservation, len(snapshot.Events))
copy(events, snapshot.Events)
eventsByCall := make(map[uint64][]llm.ExecutionObservation, len(snapshot.Calls))
2026-11-23 04:39:00 +00:00
for _, event := range events {
eventsByCall[event.CallID] = append(eventsByCall[event.CallID], event)
}
for _, call := range snapshot.Calls {
group := llm.ObservationSnapshot{Calls: []llm.LogicalCall{call}, Events: eventsByCall[call.CallID]}
var finished *time.Time
if !call.Finished.IsZero() {
value := call.Finished
finished = &value
}
2026-11-23 04:39:00 +00:00
calls = append(calls, executionCall{
RequestedPromptID: call.RequestedPromptID, CallID: call.CallID, Context: call.Context,
Prompt: call.Prompt, Started: call.Started, Finished: finished,
ElapsedMS: call.WallDuration.Milliseconds(), Failure: call.Failure,
PromptKit: llm.Summarize(group).PromptKit,
})
}
return struct {
2026-11-23 04:39:00 +00:00
Summary llm.ExecutionSummary `json:"summary"`
Calls []executionCall `json:"calls"`
Events []llm.ExecutionObservation `json:"events"`
}{llm.Summarize(snapshot), calls, events}
}
// IndexLLMObservations assigns the same proposal identities used by the ledger.
func IndexLLMObservations(snapshot llm.ObservationSnapshot, units []runner.WorkUnitOutcome) llm.ObservationSnapshot {
type sectionKey struct {
module string
section int
}
offsets := make(map[sectionKey]int)
for _, unit := range units {
if unit.ProposalIndexOffset != 0 {
offsets[sectionKey{unit.ModuleInstance, unit.Section.Index}] = unit.ProposalIndexOffset
}
}
index := func(context *structuredoutput.DiagnosticContext) {
if context.SectionIndex != nil && context.ProposalIndex != nil {
offset := offsets[sectionKey{context.ModuleInstance, *context.SectionIndex}]
context.ProposalIndex = structuredoutput.OffsetProposalIndex(context.ProposalIndex, offset)
}
}
for i := range snapshot.Calls {
index(&snapshot.Calls[i].Context)
}
for i := range snapshot.Events {
index(&snapshot.Events[i].Context)
}
return snapshot
}