150 lines
5.0 KiB
Go
150 lines
5.0 KiB
Go
package llm
|
|
|
|
import (
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/structuredoutput"
|
|
pk "go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
// ExecutionObservation combines an Audita logical call with native execution evidence.
|
|
type ExecutionObservation struct {
|
|
CallID uint64 `json:"call_id"`
|
|
Context structuredoutput.DiagnosticContext `json:"context"`
|
|
PromptKit pk.ExecutionEvent `json:"promptkit"`
|
|
}
|
|
|
|
type LogicalCall struct {
|
|
RequestedPromptID string `json:"requested_prompt_id,omitempty"`
|
|
CallID uint64 `json:"call_id"`
|
|
Context structuredoutput.DiagnosticContext `json:"context"`
|
|
Prompt structuredoutput.PromptMetadata `json:"prompt"`
|
|
Started time.Time `json:"started"`
|
|
Finished time.Time `json:"finished,omitempty"`
|
|
WallDuration time.Duration `json:"wall_duration_ns"`
|
|
Failure structuredoutput.FailureKind `json:"failure,omitempty"`
|
|
}
|
|
|
|
type ObservationSnapshot struct {
|
|
Group *pk.ExecutionGroupSummary `json:"-"`
|
|
Calls []LogicalCall `json:"calls"`
|
|
Events []ExecutionObservation `json:"events"`
|
|
}
|
|
|
|
type ExecutionSummary struct {
|
|
LogicalCalls uint64 `json:"logical_calls"`
|
|
PromptKit pk.ExecutionGroupSummary `json:"promptkit"`
|
|
}
|
|
|
|
// ObservationCollector accepts short synchronous callbacks from parallel calls.
|
|
// Snapshot should be taken after the runner has joined active section work.
|
|
type ObservationCollector struct {
|
|
group pk.ExecutionGroup
|
|
mu sync.Mutex
|
|
calls map[uint64]LogicalCall
|
|
events []ExecutionObservation
|
|
}
|
|
|
|
func NewObservationCollector() *ObservationCollector {
|
|
return &ObservationCollector{calls: make(map[uint64]LogicalCall)}
|
|
}
|
|
|
|
func (c *ObservationCollector) Hooks() CallHooks {
|
|
return CallHooks{OnCall: c.recordCall, OnEventWithCall: c.recordEvent}
|
|
}
|
|
|
|
func copyContext(value structuredoutput.DiagnosticContext) structuredoutput.DiagnosticContext {
|
|
if value.ProposalIndex != nil {
|
|
index := *value.ProposalIndex
|
|
value.ProposalIndex = &index
|
|
}
|
|
if value.RepairAttempt != nil {
|
|
index := *value.RepairAttempt
|
|
value.RepairAttempt = &index
|
|
}
|
|
if value.StageIndex != nil {
|
|
index := *value.StageIndex
|
|
value.StageIndex = &index
|
|
}
|
|
if value.StageID != nil {
|
|
id := *value.StageID
|
|
value.StageID = &id
|
|
}
|
|
if value.SectionIndex != nil {
|
|
index := *value.SectionIndex
|
|
value.SectionIndex = &index
|
|
}
|
|
if value.BatchIndex != nil {
|
|
index := *value.BatchIndex
|
|
value.BatchIndex = &index
|
|
}
|
|
return value
|
|
}
|
|
|
|
func (c *ObservationCollector) recordCall(record structuredoutput.CallRecord) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
call := LogicalCall{RequestedPromptID: record.RequestedPromptID, CallID: record.CallID, Context: copyContext(record.Context), Prompt: record.Prompt, Started: record.Started, Finished: record.Finished, Failure: record.Failure}
|
|
if !record.Finished.IsZero() {
|
|
call.WallDuration = record.Finished.Sub(record.Started)
|
|
}
|
|
c.calls[record.CallID] = call
|
|
}
|
|
|
|
func (c *ObservationCollector) recordEvent(callID uint64, context structuredoutput.DiagnosticContext, event pk.ExecutionEvent) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
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 {
|
|
if c == nil {
|
|
return ObservationSnapshot{}
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
snapshot := ObservationSnapshot{Calls: make([]LogicalCall, 0, len(c.calls)), Events: make([]ExecutionObservation, len(c.events))}
|
|
for _, call := range c.calls {
|
|
call.Context = copyContext(call.Context)
|
|
snapshot.Calls = append(snapshot.Calls, call)
|
|
}
|
|
copy(snapshot.Events, c.events)
|
|
for i := range snapshot.Events {
|
|
snapshot.Events[i].Context = copyContext(snapshot.Events[i].Context)
|
|
}
|
|
sort.Slice(snapshot.Calls, func(i, j int) bool { return snapshot.Calls[i].CallID < snapshot.Calls[j].CallID })
|
|
sort.SliceStable(snapshot.Events, func(i, j int) bool {
|
|
if snapshot.Events[i].CallID != snapshot.Events[j].CallID {
|
|
return snapshot.Events[i].CallID < snapshot.Events[j].CallID
|
|
}
|
|
return snapshot.Events[i].PromptKit.Time.Before(snapshot.Events[j].PromptKit.Time)
|
|
})
|
|
group := c.group.Snapshot()
|
|
snapshot.Group = &group
|
|
return snapshot
|
|
}
|
|
|
|
// 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 {
|
|
if snapshot.Group != nil {
|
|
group := *snapshot.Group
|
|
group.Members = append([]pk.ExecutionGroupMember(nil), group.Members...)
|
|
return ExecutionSummary{LogicalCalls: uint64(len(snapshot.Calls)), PromptKit: group}
|
|
}
|
|
var group pk.ExecutionGroup
|
|
for _, event := range snapshot.Events {
|
|
group.Observe(event.PromptKit)
|
|
}
|
|
return ExecutionSummary{LogicalCalls: uint64(len(snapshot.Calls)), PromptKit: group.Snapshot()}
|
|
}
|