Connect processing to shared PromptKit backend capacity
This commit is contained in:
@@ -1,350 +0,0 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/contracts"
|
||||
validatormetadata "gitea.maximumdirect.net/eric/audita/internal/validators/metadata"
|
||||
)
|
||||
|
||||
type UtilizationDiagnostics struct {
|
||||
EffectiveConcurrency EffectiveConcurrencyLimits `json:"effective_concurrency"`
|
||||
RunTiming RunTimingSummary `json:"run_timing"`
|
||||
LLMCalls LLMCallSummary `json:"llm_calls"`
|
||||
Modules []ModuleTimingSummary `json:"modules,omitempty"`
|
||||
Validators []ValidatorTimingSummary `json:"validators,omitempty"`
|
||||
}
|
||||
|
||||
type EffectiveConcurrencyLimits struct {
|
||||
TotalLLM int `json:"total_llm"`
|
||||
ProposalLLM int `json:"proposal_llm"`
|
||||
ValidationLLM int `json:"validation_llm"`
|
||||
}
|
||||
|
||||
type RunTimingSummary struct {
|
||||
RunWallTimeMS int64 `json:"run_wall_time_ms"`
|
||||
SchedulerQueueWaitMS int64 `json:"scheduler_queue_wait_ms"`
|
||||
LLMExecutionTimeMS int64 `json:"llm_execution_time_ms"`
|
||||
DeterministicValidationMS int64 `json:"deterministic_validation_time_ms"`
|
||||
MaxInFlightLLMCalls int `json:"max_in_flight_llm_calls"`
|
||||
AverageInFlightLLMCalls int `json:"average_in_flight_llm_calls"`
|
||||
}
|
||||
|
||||
type LLMCallSummary struct {
|
||||
TotalProposalCalls int `json:"total_proposal_calls"`
|
||||
TotalValidationCalls int `json:"total_validation_calls"`
|
||||
}
|
||||
|
||||
type ModuleTimingSummary struct {
|
||||
ModuleKey string `json:"module_key"`
|
||||
ModuleInstance string `json:"module_instance"`
|
||||
ProposalLLMCalls int `json:"proposal_llm_calls"`
|
||||
ValidationLLMCalls int `json:"validation_llm_calls"`
|
||||
SchedulerQueueWaitMS int64 `json:"scheduler_queue_wait_ms"`
|
||||
LLMExecutionTimeMS int64 `json:"llm_execution_time_ms"`
|
||||
DeterministicValidationMS int64 `json:"deterministic_validation_time_ms"`
|
||||
ModuleWallTimeMS int64 `json:"module_wall_time_ms"`
|
||||
}
|
||||
|
||||
type ValidatorTimingSummary struct {
|
||||
ModuleKey string `json:"module_key"`
|
||||
ModuleInstance string `json:"module_instance"`
|
||||
ValidatorKey string `json:"validator_key"`
|
||||
LLMBacked bool `json:"llm_backed"`
|
||||
Calls int `json:"calls"`
|
||||
ElapsedMS int64 `json:"elapsed_ms"`
|
||||
}
|
||||
|
||||
type runInstrumentation struct {
|
||||
startedAt time.Time
|
||||
|
||||
mu sync.Mutex
|
||||
totalProposalCalls int
|
||||
totalValidationCalls int
|
||||
schedulerQueueWait time.Duration
|
||||
llmExecutionTime time.Duration
|
||||
deterministicValidation time.Duration
|
||||
perModuleProposalCalls map[string]int
|
||||
perModuleValidationCalls map[string]int
|
||||
perModuleSchedulerQueueWait map[string]time.Duration
|
||||
perModuleLLMExecutionTime map[string]time.Duration
|
||||
perModuleDeterministicValidation map[string]time.Duration
|
||||
validatorSummaries map[string]*ValidatorTimingSummary
|
||||
|
||||
inflight int
|
||||
maxInflight int
|
||||
inflightArea float64
|
||||
lastInflightChange time.Time
|
||||
averageInflightComputed bool
|
||||
}
|
||||
|
||||
func newRunInstrumentation(startedAt time.Time) *runInstrumentation {
|
||||
return &runInstrumentation{
|
||||
startedAt: startedAt,
|
||||
perModuleProposalCalls: make(map[string]int),
|
||||
perModuleValidationCalls: make(map[string]int),
|
||||
perModuleSchedulerQueueWait: make(map[string]time.Duration),
|
||||
perModuleLLMExecutionTime: make(map[string]time.Duration),
|
||||
perModuleDeterministicValidation: make(map[string]time.Duration),
|
||||
validatorSummaries: make(map[string]*ValidatorTimingSummary),
|
||||
lastInflightChange: startedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *runInstrumentation) wrapProposalClient(client contracts.StructuredLLMClient) contracts.StructuredLLMClient {
|
||||
if client == nil {
|
||||
return nil
|
||||
}
|
||||
return measuredClient{
|
||||
inner: client,
|
||||
collector: i,
|
||||
isProposal: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *runInstrumentation) wrapValidationClient(client contracts.StructuredLLMClient) contracts.StructuredLLMClient {
|
||||
if client == nil {
|
||||
return nil
|
||||
}
|
||||
return measuredClient{
|
||||
inner: client,
|
||||
collector: i,
|
||||
isProposal: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *runInstrumentation) wrapScheduler(s contracts.LLMScheduler, isProposal bool) contracts.LLMScheduler {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
return measuredScheduler{
|
||||
inner: s,
|
||||
collector: i,
|
||||
isProposal: isProposal,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *runInstrumentation) recordValidatorTiming(moduleKey, moduleInstance, validatorKey string, llmBacked bool, elapsed time.Duration) {
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
if !llmBacked {
|
||||
i.deterministicValidation += elapsed
|
||||
i.perModuleDeterministicValidation[moduleInstance] += elapsed
|
||||
}
|
||||
k := moduleInstance + "::" + validatorKey
|
||||
summary, ok := i.validatorSummaries[k]
|
||||
if !ok {
|
||||
summary = &ValidatorTimingSummary{
|
||||
ModuleKey: moduleKey,
|
||||
ModuleInstance: moduleInstance,
|
||||
ValidatorKey: validatorKey,
|
||||
LLMBacked: llmBacked,
|
||||
}
|
||||
i.validatorSummaries[k] = summary
|
||||
}
|
||||
summary.Calls++
|
||||
summary.ElapsedMS += elapsed.Milliseconds()
|
||||
}
|
||||
|
||||
func (i *runInstrumentation) recordModuleWallTime(moduleInstance string, elapsed time.Duration) {
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
// Derived later from started/completed, but we keep a slot for sanity.
|
||||
_ = moduleInstance
|
||||
_ = elapsed
|
||||
}
|
||||
|
||||
func (i *runInstrumentation) beginInFlight() {
|
||||
now := time.Now().UTC()
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
i.inflightArea += float64(i.inflight) * now.Sub(i.lastInflightChange).Seconds()
|
||||
i.lastInflightChange = now
|
||||
i.inflight++
|
||||
if i.inflight > i.maxInflight {
|
||||
i.maxInflight = i.inflight
|
||||
}
|
||||
}
|
||||
|
||||
func (i *runInstrumentation) endInFlight() {
|
||||
now := time.Now().UTC()
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
i.inflightArea += float64(i.inflight) * now.Sub(i.lastInflightChange).Seconds()
|
||||
i.lastInflightChange = now
|
||||
if i.inflight > 0 {
|
||||
i.inflight--
|
||||
}
|
||||
}
|
||||
|
||||
func (i *runInstrumentation) finalize(runOutput *RunOutput, completedAt time.Time, limits EffectiveConcurrencyLimits) *UtilizationDiagnostics {
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
|
||||
if !i.averageInflightComputed {
|
||||
i.inflightArea += float64(i.inflight) * completedAt.Sub(i.lastInflightChange).Seconds()
|
||||
i.lastInflightChange = completedAt
|
||||
i.averageInflightComputed = true
|
||||
}
|
||||
|
||||
runSeconds := completedAt.Sub(i.startedAt).Seconds()
|
||||
avgInflight := 0
|
||||
if runSeconds > 0 {
|
||||
avgInflight = int(i.inflightArea / runSeconds)
|
||||
}
|
||||
|
||||
moduleSummaries := make([]ModuleTimingSummary, 0)
|
||||
if runOutput != nil {
|
||||
moduleSummaries = make([]ModuleTimingSummary, 0, len(runOutput.ModuleResults))
|
||||
for _, moduleResult := range runOutput.ModuleResults {
|
||||
moduleSummaries = append(moduleSummaries, ModuleTimingSummary{
|
||||
ModuleKey: moduleResult.ModuleKey,
|
||||
ModuleInstance: moduleResult.ModuleInstance,
|
||||
ProposalLLMCalls: i.perModuleProposalCalls[moduleResult.ModuleInstance],
|
||||
ValidationLLMCalls: i.perModuleValidationCalls[moduleResult.ModuleInstance],
|
||||
SchedulerQueueWaitMS: i.perModuleSchedulerQueueWait[moduleResult.ModuleInstance].Milliseconds(),
|
||||
LLMExecutionTimeMS: i.perModuleLLMExecutionTime[moduleResult.ModuleInstance].Milliseconds(),
|
||||
DeterministicValidationMS: i.perModuleDeterministicValidation[moduleResult.ModuleInstance].Milliseconds(),
|
||||
ModuleWallTimeMS: moduleResult.CompletedAt.Sub(moduleResult.StartedAt).Milliseconds(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
validatorSummaries := make([]ValidatorTimingSummary, 0, len(i.validatorSummaries))
|
||||
for _, summary := range i.validatorSummaries {
|
||||
validatorSummaries = append(validatorSummaries, *summary)
|
||||
}
|
||||
sortValidatorSummaries(validatorSummaries)
|
||||
|
||||
return &UtilizationDiagnostics{
|
||||
EffectiveConcurrency: limits,
|
||||
RunTiming: RunTimingSummary{
|
||||
RunWallTimeMS: completedAt.Sub(i.startedAt).Milliseconds(),
|
||||
SchedulerQueueWaitMS: i.schedulerQueueWait.Milliseconds(),
|
||||
LLMExecutionTimeMS: i.llmExecutionTime.Milliseconds(),
|
||||
DeterministicValidationMS: i.deterministicValidation.Milliseconds(),
|
||||
MaxInFlightLLMCalls: i.maxInflight,
|
||||
AverageInFlightLLMCalls: avgInflight,
|
||||
},
|
||||
LLMCalls: LLMCallSummary{
|
||||
TotalProposalCalls: i.totalProposalCalls,
|
||||
TotalValidationCalls: i.totalValidationCalls,
|
||||
},
|
||||
Modules: moduleSummaries,
|
||||
Validators: validatorSummaries,
|
||||
}
|
||||
}
|
||||
|
||||
type measuredClient struct {
|
||||
inner contracts.StructuredLLMClient
|
||||
collector *runInstrumentation
|
||||
isProposal bool
|
||||
}
|
||||
|
||||
func (c measuredClient) CompleteStructured(ctx context.Context, req contracts.StructuredCompletionRequest, out any) (contracts.StructuredCompletionResponse, error) {
|
||||
if c.collector != nil {
|
||||
c.collector.beginInFlight()
|
||||
}
|
||||
started := time.Now().UTC()
|
||||
resp, err := c.inner.CompleteStructured(ctx, req, out)
|
||||
elapsed := time.Since(started)
|
||||
if c.collector != nil {
|
||||
c.collector.endInFlight()
|
||||
c.collector.mu.Lock()
|
||||
moduleInstance := moduleInstanceFromStage(req.StageName)
|
||||
c.collector.llmExecutionTime += elapsed
|
||||
c.collector.perModuleLLMExecutionTime[moduleInstance] += elapsed
|
||||
if c.isProposal {
|
||||
c.collector.totalProposalCalls++
|
||||
c.collector.perModuleProposalCalls[moduleInstance]++
|
||||
} else {
|
||||
c.collector.totalValidationCalls++
|
||||
c.collector.perModuleValidationCalls[moduleInstance]++
|
||||
}
|
||||
c.collector.mu.Unlock()
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
type measuredScheduler struct {
|
||||
inner contracts.LLMScheduler
|
||||
collector *runInstrumentation
|
||||
isProposal bool
|
||||
}
|
||||
|
||||
func (s measuredScheduler) Run(ctx context.Context, fn func(context.Context) error) error {
|
||||
started := time.Now().UTC()
|
||||
var execDuration time.Duration
|
||||
err := s.inner.Run(ctx, func(runCtx context.Context) error {
|
||||
execStart := time.Now().UTC()
|
||||
callErr := fn(runCtx)
|
||||
execDuration += time.Since(execStart)
|
||||
return callErr
|
||||
})
|
||||
totalDuration := time.Since(started)
|
||||
waitDuration := totalDuration - execDuration
|
||||
if waitDuration < 0 {
|
||||
waitDuration = 0
|
||||
}
|
||||
if s.collector != nil {
|
||||
stageModule := moduleInstanceFromContext(ctx)
|
||||
s.collector.mu.Lock()
|
||||
s.collector.schedulerQueueWait += waitDuration
|
||||
if stageModule != "" {
|
||||
s.collector.perModuleSchedulerQueueWait[stageModule] += waitDuration
|
||||
}
|
||||
s.collector.mu.Unlock()
|
||||
}
|
||||
_ = s.isProposal
|
||||
return err
|
||||
}
|
||||
|
||||
func moduleInstanceFromStage(stage string) string {
|
||||
stage = strings.TrimSpace(stage)
|
||||
if stage == "" {
|
||||
return ""
|
||||
}
|
||||
parts := strings.Split(stage, ":")
|
||||
if len(parts) == 0 {
|
||||
return stage
|
||||
}
|
||||
return strings.TrimSpace(parts[0])
|
||||
}
|
||||
|
||||
func moduleInstanceFromContext(ctx context.Context) string {
|
||||
if ctx == nil {
|
||||
return ""
|
||||
}
|
||||
if v := ctx.Value(moduleInstanceContextKey{}); v != nil {
|
||||
if s, ok := v.(string); ok {
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type moduleInstanceContextKey struct{}
|
||||
|
||||
func withModuleInstanceContext(ctx context.Context, moduleInstance string) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
return context.WithValue(ctx, moduleInstanceContextKey{}, strings.TrimSpace(moduleInstance))
|
||||
}
|
||||
|
||||
func isLLMBackedValidator(v contracts.Validator) bool {
|
||||
return validatormetadata.ClassOf(v) == validatormetadata.ExecutionClassLLMBacked
|
||||
}
|
||||
|
||||
func sortValidatorSummaries(in []ValidatorTimingSummary) {
|
||||
sort.SliceStable(in, func(i, j int) bool {
|
||||
if in[i].ModuleInstance != in[j].ModuleInstance {
|
||||
return in[i].ModuleInstance < in[j].ModuleInstance
|
||||
}
|
||||
return in[i].ValidatorKey < in[j].ValidatorKey
|
||||
})
|
||||
}
|
||||
@@ -34,8 +34,6 @@ type Runner struct {
|
||||
factory ModuleFactory
|
||||
}
|
||||
|
||||
type ValidationScheduler = contracts.LLMScheduler
|
||||
|
||||
// ModuleResult captures deterministic per-module execution output.
|
||||
type ModuleResult struct {
|
||||
ModuleKey string `json:"module_key"`
|
||||
@@ -80,12 +78,9 @@ type RunInput struct {
|
||||
Transcript *schema.Transcript
|
||||
Glossary *schema.Glossary
|
||||
ModuleSpecs []contracts.ModuleRunSpec
|
||||
EffectiveConcurrency EffectiveConcurrencyLimits
|
||||
ProposalLLMClient contracts.StructuredLLMClient
|
||||
ProposalLLMScheduler contracts.LLMScheduler
|
||||
ProposalDiagnosticsDir string
|
||||
ValidationLLMClient contracts.StructuredLLMClient
|
||||
ValidationLLMScheduler ValidationScheduler
|
||||
ValidationDiagnosticsDir string
|
||||
}
|
||||
|
||||
@@ -93,7 +88,6 @@ type RunInput struct {
|
||||
type RunOutput struct {
|
||||
FinalTranscript *schema.Transcript `json:"-"`
|
||||
ModuleResults []ModuleResult `json:"module_results"`
|
||||
Utilization *UtilizationDiagnostics
|
||||
}
|
||||
|
||||
func New(factory ModuleFactory) *Runner {
|
||||
@@ -105,14 +99,6 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
||||
return RunOutput{}, fmt.Errorf("runner module factory is required")
|
||||
}
|
||||
|
||||
startedAt := time.Now().UTC()
|
||||
instrumentation := newRunInstrumentation(startedAt)
|
||||
|
||||
input.ProposalLLMClient = instrumentation.wrapProposalClient(input.ProposalLLMClient)
|
||||
input.ValidationLLMClient = instrumentation.wrapValidationClient(input.ValidationLLMClient)
|
||||
input.ProposalLLMScheduler = instrumentation.wrapScheduler(input.ProposalLLMScheduler, true)
|
||||
input.ValidationLLMScheduler = instrumentation.wrapScheduler(input.ValidationLLMScheduler, false)
|
||||
|
||||
working := cloneTranscript(input.Transcript)
|
||||
results := make([]ModuleResult, 0, len(input.ModuleSpecs))
|
||||
|
||||
@@ -132,7 +118,6 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
||||
return RunOutput{
|
||||
FinalTranscript: working,
|
||||
ModuleResults: results,
|
||||
Utilization: instrumentation.finalize(&RunOutput{ModuleResults: results}, time.Now().UTC(), input.EffectiveConcurrency),
|
||||
}, fmt.Errorf("module %q setup failed: %w", spec.InstanceName, err)
|
||||
}
|
||||
|
||||
@@ -152,7 +137,6 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
||||
return RunOutput{
|
||||
FinalTranscript: working,
|
||||
ModuleResults: results,
|
||||
Utilization: instrumentation.finalize(&RunOutput{ModuleResults: results}, time.Now().UTC(), input.EffectiveConcurrency),
|
||||
}, fmt.Errorf("module %q chunking failed: %w", spec.InstanceName, err)
|
||||
}
|
||||
|
||||
@@ -164,12 +148,9 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
||||
Glossary: input.Glossary,
|
||||
Sections: sections,
|
||||
ProposalClient: input.ProposalLLMClient,
|
||||
ProposalScheduler: input.ProposalLLMScheduler,
|
||||
DiagnosticsDir: input.ProposalDiagnosticsDir,
|
||||
ValidationClient: input.ValidationLLMClient,
|
||||
ValidationScheduler: input.ValidationLLMScheduler,
|
||||
ValidationDiagnosticsDir: input.ValidationDiagnosticsDir,
|
||||
Instrumentation: instrumentation,
|
||||
})
|
||||
if pipelineErr != nil {
|
||||
failed := ModuleResult{
|
||||
@@ -189,7 +170,6 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
||||
return RunOutput{
|
||||
FinalTranscript: working,
|
||||
ModuleResults: results,
|
||||
Utilization: instrumentation.finalize(&RunOutput{ModuleResults: results}, time.Now().UTC(), input.EffectiveConcurrency),
|
||||
}, fmt.Errorf("module %q failed: %w", spec.InstanceName, pipelineErr)
|
||||
}
|
||||
|
||||
@@ -213,7 +193,6 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
||||
}
|
||||
|
||||
output := RunOutput{FinalTranscript: working, ModuleResults: results}
|
||||
output.Utilization = instrumentation.finalize(&output, time.Now().UTC(), input.EffectiveConcurrency)
|
||||
return output, nil
|
||||
}
|
||||
|
||||
@@ -225,12 +204,9 @@ type collectSectionProposalsInput struct {
|
||||
Glossary *schema.Glossary
|
||||
Sections []chunking.Section
|
||||
ProposalClient contracts.StructuredLLMClient
|
||||
ProposalScheduler contracts.LLMScheduler
|
||||
DiagnosticsDir string
|
||||
ValidationClient contracts.StructuredLLMClient
|
||||
ValidationScheduler ValidationScheduler
|
||||
ValidationDiagnosticsDir string
|
||||
Instrumentation *runInstrumentation
|
||||
}
|
||||
|
||||
type sectionProposals struct {
|
||||
@@ -279,7 +255,7 @@ func collectSectionProposals(ctx context.Context, input collectSectionProposalsI
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
meta := contracts.SectionMetadataFromSection(section)
|
||||
proposalResult, err := input.Module.Propose(withModuleInstanceContext(runCtx, input.Spec.InstanceName), contracts.ProposalRequest{
|
||||
proposalResult, err := input.Module.Propose(runCtx, contracts.ProposalRequest{
|
||||
ExecutionContext: contracts.ExecutionContext{
|
||||
Config: input.Config,
|
||||
WorkingTranscript: transcriptFromSection(section),
|
||||
@@ -292,8 +268,7 @@ func collectSectionProposals(ctx context.Context, input collectSectionProposalsI
|
||||
InstanceName: input.Spec.InstanceName,
|
||||
ReplacementPolicy: input.Policy,
|
||||
},
|
||||
LLMClient: input.ProposalClient,
|
||||
LLMScheduler: input.ProposalScheduler,
|
||||
LLMClient: input.ProposalClient,
|
||||
})
|
||||
select {
|
||||
case results <- sectionProposalResult{
|
||||
@@ -402,9 +377,7 @@ func runModulePipeline(ctx context.Context, input collectSectionProposalsInput)
|
||||
Validators: validatorsOrdered,
|
||||
SectionEnriched: enriched,
|
||||
ValidationLLMClient: input.ValidationClient,
|
||||
ValidationScheduler: input.ValidationScheduler,
|
||||
DiagnosticsDir: input.ValidationDiagnosticsDir,
|
||||
Instrumentation: input.Instrumentation,
|
||||
})
|
||||
validationResults <- sectionValidationResult{
|
||||
sectionPos: sectionPos,
|
||||
@@ -503,9 +476,7 @@ type validateSectionCandidatesInput struct {
|
||||
Validators []contracts.Validator
|
||||
SectionEnriched []proposals.EnrichedCorrectionProposal
|
||||
ValidationLLMClient contracts.StructuredLLMClient
|
||||
ValidationScheduler ValidationScheduler
|
||||
DiagnosticsDir string
|
||||
Instrumentation *runInstrumentation
|
||||
}
|
||||
|
||||
type validateSectionCandidatesResult struct {
|
||||
@@ -532,8 +503,7 @@ func validateSectionCandidates(ctx context.Context, input validateSectionCandida
|
||||
}
|
||||
}
|
||||
|
||||
validatorStartedAt := time.Now().UTC()
|
||||
vResult, err := validator.Validate(withModuleInstanceContext(ctx, input.Spec.InstanceName), contracts.ValidationRequest{
|
||||
vResult, err := validator.Validate(ctx, contracts.ValidationRequest{
|
||||
WorkingTranscript: input.WorkingTranscript,
|
||||
CandidateProposal: eligible,
|
||||
ModuleKey: input.Spec.ModuleKey,
|
||||
@@ -541,19 +511,10 @@ func validateSectionCandidates(ctx context.Context, input validateSectionCandida
|
||||
ReplacementPolicy: input.Policy,
|
||||
Glossary: input.Glossary,
|
||||
Config: input.Config,
|
||||
LLMClient: validationLLMClientAdapter{client: input.ValidationLLMClient},
|
||||
Scheduler: input.ValidationScheduler,
|
||||
LLMClient: input.ValidationLLMClient,
|
||||
DiagnosticsWriter: diagnosticsWriter,
|
||||
})
|
||||
if input.Instrumentation != nil {
|
||||
input.Instrumentation.recordValidatorTiming(
|
||||
input.Spec.ModuleKey,
|
||||
input.Spec.InstanceName,
|
||||
validator.Name(),
|
||||
isLLMBackedValidator(validator),
|
||||
time.Since(validatorStartedAt),
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return validateSectionCandidatesResult{
|
||||
approved: eligible,
|
||||
@@ -650,9 +611,9 @@ func chunkWorkingTranscript(cfg *config.Config, transcript *schema.Transcript) (
|
||||
}, nil
|
||||
}
|
||||
chunker := chunking.NewChunker(chunking.ChunkingConfig{
|
||||
MaxSectionTokens: cfg.MaxSectionTokens,
|
||||
MinSectionTokens: cfg.MinSectionTokens,
|
||||
TargetSections: cfg.TargetSections,
|
||||
MaxSectionTokens: cfg.Chunking.MaxSectionTokens,
|
||||
MinSectionTokens: cfg.Chunking.MinSectionTokens,
|
||||
TargetSections: cfg.Chunking.TargetSections,
|
||||
})
|
||||
return chunker.ChunkTranscript(transcript)
|
||||
}
|
||||
@@ -697,34 +658,6 @@ func cloneTranscript(t *schema.Transcript) *schema.Transcript {
|
||||
return &schema.Transcript{Segments: segments}
|
||||
}
|
||||
|
||||
type validationLLMClientAdapter struct {
|
||||
client contracts.StructuredLLMClient
|
||||
}
|
||||
|
||||
func (a validationLLMClientAdapter) CompleteStructured(ctx context.Context, req validators.StructuredCompletionRequest, out any) (validators.StructuredCompletionResponse, error) {
|
||||
if a.client == nil {
|
||||
return validators.StructuredCompletionResponse{}, fmt.Errorf("validation structured LLM client is not configured")
|
||||
}
|
||||
messages := make([]contracts.LLMMessage, len(req.Messages))
|
||||
for i, m := range req.Messages {
|
||||
messages[i] = contracts.LLMMessage{Role: m.Role, Content: m.Content}
|
||||
}
|
||||
resp, err := a.client.CompleteStructured(ctx, contracts.StructuredCompletionRequest{
|
||||
StageName: req.StageName,
|
||||
Messages: messages,
|
||||
Model: req.Model,
|
||||
ResponseSchema: req.ResponseSchema,
|
||||
}, out)
|
||||
if err != nil {
|
||||
return validators.StructuredCompletionResponse{}, err
|
||||
}
|
||||
return validators.StructuredCompletionResponse{
|
||||
PromptTokens: resp.PromptTokens,
|
||||
CompletionTokens: resp.CompletionTokens,
|
||||
TotalTokens: resp.TotalTokens,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type llmDiagnosticsWriterAdapter struct {
|
||||
writer *llm.DiagnosticsWriter
|
||||
}
|
||||
|
||||
@@ -5,26 +5,27 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
ck "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/core/schema"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/llm"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/modules"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/proposal_generation"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/proposals"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/responseschema"
|
||||
"gitea.maximumdirect.net/eric/audita/internal/framework/validators"
|
||||
validatormetadata "gitea.maximumdirect.net/eric/audita/internal/validators/metadata"
|
||||
)
|
||||
|
||||
func runnerTestConfig() config.Config {
|
||||
return config.Config{Chunking: config.Chunking{MaxSectionTokens: 8192, MinSectionTokens: 2048}, Validation: config.Validation{MaxPromptTokens: 2048}}
|
||||
}
|
||||
|
||||
type fakeFactory struct {
|
||||
modules map[string]contracts.TranscriptModule
|
||||
}
|
||||
@@ -162,9 +163,9 @@ func TestRunnerModulesRunSequentially(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunnerProposalsExecutePerChunkSection(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.MaxSectionTokens = 3
|
||||
cfg.MinSectionTokens = 0
|
||||
cfg := runnerTestConfig()
|
||||
cfg.Chunking.MaxSectionTokens = 3
|
||||
cfg.Chunking.MinSectionTokens = 0
|
||||
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{
|
||||
{ID: 1, Text: "one two"},
|
||||
@@ -220,84 +221,10 @@ func TestRunnerProposalsExecutePerChunkSection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerProposalSectionConcurrencyBoundedByProposalLLMConcurrency(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.MaxSectionTokens = 3
|
||||
cfg.MinSectionTokens = 0
|
||||
cfg.TotalLLMConcurrency = 2
|
||||
cfg.ProposalLLMConcurrency = 2
|
||||
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{
|
||||
{ID: 1, Text: "one two"},
|
||||
{ID: 2, Text: "three four"},
|
||||
{ID: 3, Text: "five six"},
|
||||
{ID: 4, Text: "seven eight"},
|
||||
}}
|
||||
|
||||
var inFlight int32
|
||||
var maxInFlight int32
|
||||
entered := make(chan struct{}, len(transcript.Segments))
|
||||
release := make(chan struct{})
|
||||
scheduler, err := llm.NewScheduler(2)
|
||||
if err != nil {
|
||||
t.Fatalf("NewScheduler: %v", err)
|
||||
}
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{key: "m", policy: proposals.ReplacementPolicyRequireUnique, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
err := req.LLMScheduler.Run(context.Background(), func(context.Context) error {
|
||||
current := atomic.AddInt32(&inFlight, 1)
|
||||
for {
|
||||
prior := atomic.LoadInt32(&maxInFlight)
|
||||
if current <= prior || atomic.CompareAndSwapInt32(&maxInFlight, prior, current) {
|
||||
break
|
||||
}
|
||||
}
|
||||
entered <- struct{}{}
|
||||
<-release
|
||||
atomic.AddInt32(&inFlight, -1)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
}},
|
||||
}})
|
||||
|
||||
resultCh := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: transcript,
|
||||
ProposalLLMScheduler: scheduler,
|
||||
ModuleSpecs: []contracts.ModuleRunSpec{
|
||||
{ModuleKey: "m", InstanceName: "m"},
|
||||
},
|
||||
})
|
||||
resultCh <- err
|
||||
}()
|
||||
|
||||
waitForRunnerEntries(t, entered, 2, "proposal workers to enter")
|
||||
close(release)
|
||||
|
||||
err = <-resultCh
|
||||
if err != nil {
|
||||
t.Fatalf("Run error: %v", err)
|
||||
}
|
||||
if maxInFlight < 2 {
|
||||
t.Fatalf("expected proposal execution to run concurrently, got max in-flight %d", maxInFlight)
|
||||
}
|
||||
if maxInFlight > 2 {
|
||||
t.Fatalf("expected proposal concurrency <= 2, got %d", maxInFlight)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerProposalIndexOrderingIsDeterministicAcrossParallelSections(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.MaxSectionTokens = 3
|
||||
cfg.MinSectionTokens = 0
|
||||
cfg.TotalLLMConcurrency = 3
|
||||
cfg.ProposalLLMConcurrency = 3
|
||||
cfg := runnerTestConfig()
|
||||
cfg.Chunking.MaxSectionTokens = 3
|
||||
cfg.Chunking.MinSectionTokens = 0
|
||||
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{
|
||||
{ID: 1, Text: "alpha one"},
|
||||
@@ -411,11 +338,9 @@ func mapKeys(values map[int]struct{}) []int {
|
||||
}
|
||||
|
||||
func TestRunnerValidationStartsBeforeAllSectionProposalsComplete(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.MaxSectionTokens = 3
|
||||
cfg.MinSectionTokens = 0
|
||||
cfg.TotalLLMConcurrency = 2
|
||||
cfg.ProposalLLMConcurrency = 2
|
||||
cfg := runnerTestConfig()
|
||||
cfg.Chunking.MaxSectionTokens = 3
|
||||
cfg.Chunking.MinSectionTokens = 0
|
||||
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{
|
||||
{ID: 1, Text: "alpha one"},
|
||||
@@ -488,298 +413,6 @@ func TestRunnerValidationStartsBeforeAllSectionProposalsComplete(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerProposalAndValidationLLMCanOverlapWithinModule(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.MaxSectionTokens = 3
|
||||
cfg.MinSectionTokens = 0
|
||||
cfg.TotalLLMConcurrency = 2
|
||||
cfg.ProposalLLMConcurrency = 2
|
||||
validationCap := 2
|
||||
cfg.ValidationLLMConcurrency = &validationCap
|
||||
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{
|
||||
{ID: 1, Text: "alpha one"},
|
||||
{ID: 2, Text: "bravo two"},
|
||||
}}
|
||||
|
||||
global, err := llm.NewScheduler(2)
|
||||
if err != nil {
|
||||
t.Fatalf("NewScheduler: %v", err)
|
||||
}
|
||||
scheduler := &trackingScheduler{inner: global}
|
||||
|
||||
proposalSectionOneStarted := make(chan struct{}, 1)
|
||||
releaseProposalSectionOne := make(chan struct{})
|
||||
|
||||
releaseValidation := make(chan struct{})
|
||||
client := &stageAwareStructuredClient{
|
||||
startedSection: make(chan int, 8),
|
||||
releaseValidation: releaseValidation,
|
||||
}
|
||||
llmValidator, err := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
if err != nil {
|
||||
t.Fatalf("NewLLMBackedValidator: %v", err)
|
||||
}
|
||||
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{
|
||||
key: "m",
|
||||
policy: proposals.ReplacementPolicyRequireUnique,
|
||||
validators: []contracts.Validator{llmValidator},
|
||||
proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
seg := req.WorkingTranscript.Segments[0]
|
||||
err := req.LLMScheduler.Run(context.Background(), func(context.Context) error {
|
||||
if req.Section != nil && req.Section.Index == 1 {
|
||||
select {
|
||||
case proposalSectionOneStarted <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
<-releaseProposalSectionOne
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []proposals.CorrectionProposal{
|
||||
{TargetSegmentID: seg.ID, OriginalText: seg.Text, CorrectedText: strings.ToUpper(seg.Text), Confidence: 1},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
}})
|
||||
|
||||
resultCh := make(chan struct {
|
||||
out RunOutput
|
||||
err error
|
||||
}, 1)
|
||||
go func() {
|
||||
out, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: transcript,
|
||||
ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m", InstanceName: "m"}},
|
||||
ProposalLLMScheduler: scheduler,
|
||||
ValidationLLMScheduler: scheduler,
|
||||
ValidationLLMClient: client,
|
||||
})
|
||||
resultCh <- struct {
|
||||
out RunOutput
|
||||
err error
|
||||
}{out: out, err: err}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-proposalSectionOneStarted:
|
||||
case <-time.After(350 * time.Millisecond):
|
||||
t.Fatal("expected section-1 proposal to start")
|
||||
}
|
||||
|
||||
select {
|
||||
case section := <-client.startedSection:
|
||||
if section != 0 {
|
||||
t.Fatalf("expected first validation to start for section 0, got section %d", section)
|
||||
}
|
||||
case <-time.After(350 * time.Millisecond):
|
||||
t.Fatal("expected validation call for section 0 while later section proposal still running")
|
||||
}
|
||||
|
||||
close(releaseProposalSectionOne)
|
||||
close(releaseValidation)
|
||||
|
||||
result := <-resultCh
|
||||
if result.err != nil {
|
||||
t.Fatalf("Run error: %v", result.err)
|
||||
}
|
||||
if got := atomic.LoadInt32(&scheduler.maxInFlight); got > 2 {
|
||||
t.Fatalf("expected combined proposal+validation in-flight <= 2, got %d", got)
|
||||
}
|
||||
if got := atomic.LoadInt32(&scheduler.maxInFlight); got < 2 {
|
||||
t.Fatalf("expected observed overlap/in-flight utilization of at least 2, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerValidationLLMConcurrencyRespected(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.MaxSectionTokens = 3
|
||||
cfg.MinSectionTokens = 0
|
||||
cfg.TotalLLMConcurrency = 4
|
||||
cfg.ProposalLLMConcurrency = 4
|
||||
validationCap := 1
|
||||
cfg.ValidationLLMConcurrency = &validationCap
|
||||
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{
|
||||
{ID: 1, Text: "one one"},
|
||||
{ID: 2, Text: "two two"},
|
||||
{ID: 3, Text: "three three"},
|
||||
}}
|
||||
|
||||
validationSchedulerInner, err := llm.NewScheduler(1)
|
||||
if err != nil {
|
||||
t.Fatalf("NewScheduler(validation): %v", err)
|
||||
}
|
||||
validationScheduler := &trackingScheduler{inner: validationSchedulerInner}
|
||||
|
||||
releaseValidation := make(chan struct{})
|
||||
client := &stageAwareStructuredClient{
|
||||
startedSection: make(chan int, 16),
|
||||
releaseValidation: releaseValidation,
|
||||
}
|
||||
llmValidator, err := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
if err != nil {
|
||||
t.Fatalf("NewLLMBackedValidator: %v", err)
|
||||
}
|
||||
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{
|
||||
key: "m",
|
||||
policy: proposals.ReplacementPolicyRequireUnique,
|
||||
validators: []contracts.Validator{llmValidator},
|
||||
proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
seg := req.WorkingTranscript.Segments[0]
|
||||
return []proposals.CorrectionProposal{
|
||||
{TargetSegmentID: seg.ID, OriginalText: seg.Text, CorrectedText: strings.ToUpper(seg.Text), Confidence: 1},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
}})
|
||||
|
||||
resultCh := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: transcript,
|
||||
ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m", InstanceName: "m"}},
|
||||
ValidationLLMScheduler: validationScheduler,
|
||||
ValidationLLMClient: client,
|
||||
})
|
||||
resultCh <- err
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-client.startedSection:
|
||||
case <-time.After(350 * time.Millisecond):
|
||||
t.Fatal("expected validation to start")
|
||||
}
|
||||
close(releaseValidation)
|
||||
|
||||
if err := <-resultCh; err != nil {
|
||||
t.Fatalf("Run error: %v", err)
|
||||
}
|
||||
if got := atomic.LoadInt32(&validationScheduler.maxInFlight); got > 1 {
|
||||
t.Fatalf("expected validation in-flight <= 1, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerMixedProposalValidationFIFOOrder(t *testing.T) {
|
||||
cfg := config.Default()
|
||||
cfg.MaxSectionTokens = 3
|
||||
cfg.MinSectionTokens = 0
|
||||
cfg.TotalLLMConcurrency = 1
|
||||
cfg.ProposalLLMConcurrency = 1
|
||||
validationCap := 1
|
||||
cfg.ValidationLLMConcurrency = &validationCap
|
||||
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{
|
||||
{ID: 1, Text: "alpha one"},
|
||||
{ID: 2, Text: "bravo two"},
|
||||
}}
|
||||
|
||||
global, err := llm.NewScheduler(1)
|
||||
if err != nil {
|
||||
t.Fatalf("NewScheduler: %v", err)
|
||||
}
|
||||
proposalScheduler := global
|
||||
validationScheduler := global
|
||||
|
||||
events := make(chan string, 8)
|
||||
releaseProposalSectionOne := make(chan struct{})
|
||||
releaseValidation := make(chan struct{})
|
||||
sectionZeroEntered := make(chan struct{})
|
||||
sectionOneAttempted := make(chan struct{}, 1)
|
||||
|
||||
client := &stageAwareStructuredClient{
|
||||
startedSection: make(chan int, 8),
|
||||
releaseValidation: releaseValidation,
|
||||
eventSink: events,
|
||||
}
|
||||
llmValidator, err := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
if err != nil {
|
||||
t.Fatalf("NewLLMBackedValidator: %v", err)
|
||||
}
|
||||
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{
|
||||
key: "m",
|
||||
policy: proposals.ReplacementPolicyRequireUnique,
|
||||
validators: []contracts.Validator{llmValidator},
|
||||
proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
seg := req.WorkingTranscript.Segments[0]
|
||||
if req.Section != nil && req.Section.Index == 1 {
|
||||
<-sectionZeroEntered
|
||||
select {
|
||||
case sectionOneAttempted <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
err := req.LLMScheduler.Run(context.Background(), func(context.Context) error {
|
||||
if req.Section != nil {
|
||||
events <- "p" + strconv.Itoa(req.Section.Index)
|
||||
if req.Section.Index == 0 {
|
||||
select {
|
||||
case sectionZeroEntered <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
<-sectionOneAttempted
|
||||
}
|
||||
if req.Section.Index == 1 {
|
||||
<-releaseProposalSectionOne
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []proposals.CorrectionProposal{
|
||||
{TargetSegmentID: seg.ID, OriginalText: seg.Text, CorrectedText: strings.ToUpper(seg.Text), Confidence: 1},
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
}})
|
||||
|
||||
resultCh := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: transcript,
|
||||
ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m", InstanceName: "m"}},
|
||||
ProposalLLMScheduler: proposalScheduler,
|
||||
ValidationLLMScheduler: validationScheduler,
|
||||
ValidationLLMClient: client,
|
||||
})
|
||||
resultCh <- err
|
||||
}()
|
||||
|
||||
if got := <-events; got != "p0" {
|
||||
t.Fatalf("expected first event p0, got %q", got)
|
||||
}
|
||||
if got := <-events; got != "p1" {
|
||||
t.Fatalf("expected second event p1 (FIFO queued proposal), got %q", got)
|
||||
}
|
||||
|
||||
close(releaseProposalSectionOne)
|
||||
close(releaseValidation)
|
||||
|
||||
if got := <-events; !strings.HasPrefix(got, "v") {
|
||||
t.Fatalf("expected validator event after queued p1, got %q", got)
|
||||
}
|
||||
|
||||
if err := <-resultCh; err != nil {
|
||||
t.Fatalf("Run error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var sectionStagePattern = regexp.MustCompile(`section-(\d+)`)
|
||||
|
||||
type stageAwareStructuredClient struct {
|
||||
startedSection chan int
|
||||
releaseValidation <-chan struct{}
|
||||
@@ -804,7 +437,10 @@ func (c *captureContractStructuredClient) CompleteStructured(ctx context.Context
|
||||
}
|
||||
|
||||
func (c *stageAwareStructuredClient) CompleteStructured(ctx context.Context, req contracts.StructuredCompletionRequest, out any) (contracts.StructuredCompletionResponse, error) {
|
||||
section := parseSectionFromStage(req.StageName)
|
||||
section := 0
|
||||
if req.Context.BatchIndex != nil {
|
||||
section = *req.Context.BatchIndex
|
||||
}
|
||||
if c.startedSection != nil {
|
||||
select {
|
||||
case c.startedSection <- section:
|
||||
@@ -831,67 +467,6 @@ func (c *stageAwareStructuredClient) CompleteStructured(ctx context.Context, req
|
||||
return contracts.StructuredCompletionResponse{}, nil
|
||||
}
|
||||
|
||||
func parseSectionFromStage(stage string) int {
|
||||
match := sectionStagePattern.FindStringSubmatch(stage)
|
||||
if len(match) != 2 {
|
||||
return 0
|
||||
}
|
||||
n, err := strconv.Atoi(match[1])
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func TestValidationLLMClientAdapterPassesResponseSchema(t *testing.T) {
|
||||
capture := &captureContractStructuredClient{}
|
||||
adapter := validationLLMClientAdapter{client: capture}
|
||||
schema := responseschema.MustLookup(responseschema.ValidatorDecisionSetKey)
|
||||
|
||||
_, err := adapter.CompleteStructured(context.Background(), validators.StructuredCompletionRequest{
|
||||
StageName: "module:validator:batch-0000",
|
||||
Messages: []validators.LLMMessage{
|
||||
{Role: "system", Content: "system"},
|
||||
{Role: "user", Content: "user"},
|
||||
},
|
||||
Model: "test-model",
|
||||
ResponseSchema: &schema,
|
||||
}, &validators.LLMValidationResponse{})
|
||||
if err != nil {
|
||||
t.Fatalf("CompleteStructured error: %v", err)
|
||||
}
|
||||
|
||||
if capture.lastRequest.ResponseSchema == nil {
|
||||
t.Fatalf("expected response schema to be forwarded")
|
||||
}
|
||||
if capture.lastRequest.ResponseSchema.ID != schema.ID ||
|
||||
capture.lastRequest.ResponseSchema.Version != schema.Version ||
|
||||
capture.lastRequest.ResponseSchema.Name != schema.Name ||
|
||||
capture.lastRequest.ResponseSchema.SHA256 != schema.SHA256 {
|
||||
t.Fatalf("unexpected forwarded schema metadata: got=%+v want=%+v", *capture.lastRequest.ResponseSchema, schema)
|
||||
}
|
||||
}
|
||||
|
||||
type trackingScheduler struct {
|
||||
inner contracts.LLMScheduler
|
||||
inFlight int32
|
||||
maxInFlight int32
|
||||
}
|
||||
|
||||
func (s *trackingScheduler) Run(ctx context.Context, fn func(context.Context) error) error {
|
||||
return s.inner.Run(ctx, func(callCtx context.Context) error {
|
||||
current := atomic.AddInt32(&s.inFlight, 1)
|
||||
for {
|
||||
prior := atomic.LoadInt32(&s.maxInFlight)
|
||||
if current <= prior || atomic.CompareAndSwapInt32(&s.maxInFlight, prior, current) {
|
||||
break
|
||||
}
|
||||
}
|
||||
defer atomic.AddInt32(&s.inFlight, -1)
|
||||
return fn(callCtx)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunnerSkippedRecorded(t *testing.T) {
|
||||
transcript := &schema.Transcript{Segments: []schema.Segment{{ID: 1, Speaker: "A", Start: 0, End: 1, Text: "word word"}}}
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
@@ -940,7 +515,7 @@ func TestRunnerDoesNotMutateInputTranscript(t *testing.T) {
|
||||
}},
|
||||
}})
|
||||
|
||||
_, err := r.Run(context.Background(), RunInput{Config: ptrConfig(config.Default()), Transcript: transcript, ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m", InstanceName: "m"}}})
|
||||
_, err := r.Run(context.Background(), RunInput{Config: ptrConfig(runnerTestConfig()), Transcript: transcript, ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m", InstanceName: "m"}}})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error: %v", err)
|
||||
}
|
||||
@@ -1128,20 +703,9 @@ func (f *fakeStructuredClient) CompleteStructured(ctx context.Context, req contr
|
||||
return contracts.StructuredCompletionResponse{}, nil
|
||||
}
|
||||
|
||||
type countingScheduler struct{ runs int }
|
||||
|
||||
func (s *countingScheduler) Run(ctx context.Context, fn func(context.Context) error) error {
|
||||
s.runs++
|
||||
return fn(ctx)
|
||||
}
|
||||
|
||||
type lenEstimator struct{}
|
||||
|
||||
func (lenEstimator) EstimateTokens(text string) int { return len(text) }
|
||||
|
||||
func TestRunnerLLMValidatorApprovalApplied(t *testing.T) {
|
||||
client := &fakeStructuredClient{responses: []validators.LLMValidationResponse{{Validations: []validators.LLMValidationDecision{{CorrectionIndex: 0, Approved: true, Confidence: 0.9, Reason: "ok"}}}}}
|
||||
llmValidator, err := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
llmValidator, err := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility)
|
||||
if err != nil {
|
||||
t.Fatalf("NewLLMBackedValidator: %v", err)
|
||||
}
|
||||
@@ -1151,7 +715,7 @@ func TestRunnerLLMValidatorApprovalApplied(t *testing.T) {
|
||||
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: "gestures", CorrectedText: "Jesters", Confidence: 1}}, nil
|
||||
}},
|
||||
}})
|
||||
cfg := config.Default()
|
||||
cfg := runnerTestConfig()
|
||||
out, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "There were gestures at the temple."}}},
|
||||
@@ -1168,13 +732,13 @@ func TestRunnerLLMValidatorApprovalApplied(t *testing.T) {
|
||||
|
||||
func TestRunnerLLMValidatorRejectionPreventsApplication(t *testing.T) {
|
||||
client := &fakeStructuredClient{responses: []validators.LLMValidationResponse{{Validations: []validators.LLMValidationDecision{{CorrectionIndex: 0, Approved: false, Confidence: 0.95, Reason: "reject"}}}}}
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility)
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{key: "m", policy: proposals.ReplacementPolicyRequireUnique, validators: []contracts.Validator{llmValidator}, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: "gestures", CorrectedText: "Jesters", Confidence: 1}}, nil
|
||||
}},
|
||||
}})
|
||||
cfg := config.Default()
|
||||
cfg := runnerTestConfig()
|
||||
out, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "There were gestures at the temple."}}},
|
||||
@@ -1194,7 +758,7 @@ func TestRunnerLLMValidatorRejectionPreventsApplication(t *testing.T) {
|
||||
|
||||
func TestRunnerLLMValidatorMalformedResponseRejectsBatchAndKeepsPartialProgress(t *testing.T) {
|
||||
client := &fakeStructuredClient{responses: []validators.LLMValidationResponse{{Validations: []validators.LLMValidationDecision{{CorrectionIndex: 99, Approved: true, Confidence: 0.9, Reason: "bad index"}}}}}
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility)
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m1": fakeModule{key: "m1", policy: proposals.ReplacementPolicyRequireUnique, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: "teh", CorrectedText: "the", Confidence: 1}}, nil
|
||||
@@ -1203,7 +767,7 @@ func TestRunnerLLMValidatorMalformedResponseRejectsBatchAndKeepsPartialProgress(
|
||||
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: "cat", CorrectedText: "dog", Confidence: 1}}, nil
|
||||
}},
|
||||
}})
|
||||
cfg := config.Default()
|
||||
cfg := runnerTestConfig()
|
||||
out, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "teh cat"}}},
|
||||
@@ -1226,13 +790,13 @@ func TestRunnerLLMValidatorMalformedResponseRejectsBatchAndKeepsPartialProgress(
|
||||
|
||||
func TestRunnerLLMValidatorMissingDecisionRejectsBatch(t *testing.T) {
|
||||
client := &fakeStructuredClient{responses: []validators.LLMValidationResponse{{Validations: []validators.LLMValidationDecision{}}}}
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility)
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{key: "m", policy: proposals.ReplacementPolicyRequireUnique, validators: []contracts.Validator{llmValidator}, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: "gestures", CorrectedText: "Jesters", Confidence: 1}}, nil
|
||||
}},
|
||||
}})
|
||||
cfg := config.Default()
|
||||
cfg := runnerTestConfig()
|
||||
_, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "There were gestures at the temple."}}},
|
||||
@@ -1249,13 +813,13 @@ func TestRunnerLLMValidatorDuplicateDecisionRejectsBatch(t *testing.T) {
|
||||
{CorrectionIndex: 0, Approved: true, Confidence: 0.9, Reason: "ok"},
|
||||
{CorrectionIndex: 0, Approved: false, Confidence: 0.9, Reason: "dup"},
|
||||
}}}}
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility)
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{key: "m", policy: proposals.ReplacementPolicyRequireUnique, validators: []contracts.Validator{llmValidator}, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
return []proposals.CorrectionProposal{{TargetSegmentID: 1, OriginalText: "gestures", CorrectedText: "Jesters", Confidence: 1}}, nil
|
||||
}},
|
||||
}})
|
||||
cfg := config.Default()
|
||||
cfg := runnerTestConfig()
|
||||
_, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "There were gestures at the temple."}}},
|
||||
@@ -1267,16 +831,20 @@ func TestRunnerLLMValidatorDuplicateDecisionRejectsBatch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerLLMValidatorBatchingAndSchedulerUsage(t *testing.T) {
|
||||
type lenEstimator struct{}
|
||||
|
||||
func (lenEstimator) EstimateTokens(text string) int { return len(text) }
|
||||
|
||||
func TestRunnerLLMValidatorBatching(t *testing.T) {
|
||||
client := &fakeStructuredClient{responses: []validators.LLMValidationResponse{
|
||||
{Validations: []validators.LLMValidationDecision{{CorrectionIndex: 0, Approved: true, Confidence: 0.9, Reason: "ok"}}},
|
||||
{Validations: []validators.LLMValidationDecision{{CorrectionIndex: 1, Approved: true, Confidence: 0.9, Reason: "ok"}}},
|
||||
}}
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility)
|
||||
llmValidator.SetTokenEstimator(lenEstimator{})
|
||||
scheduler := &countingScheduler{}
|
||||
cfg := config.Default()
|
||||
cfg.ValidationMaxPromptTokens = 260
|
||||
|
||||
cfg := runnerTestConfig()
|
||||
cfg.Validation.MaxPromptTokens = 260
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{key: "m", policy: proposals.ReplacementPolicyReplaceAll, validators: []contracts.Validator{llmValidator}, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
return []proposals.CorrectionProposal{
|
||||
@@ -1286,18 +854,15 @@ func TestRunnerLLMValidatorBatchingAndSchedulerUsage(t *testing.T) {
|
||||
}},
|
||||
}})
|
||||
out, err := r.Run(context.Background(), RunInput{
|
||||
Config: &cfg,
|
||||
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "alpha gamma"}}},
|
||||
ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m", InstanceName: "m"}},
|
||||
ValidationLLMClient: client,
|
||||
ValidationLLMScheduler: scheduler,
|
||||
Config: &cfg,
|
||||
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "alpha gamma"}}},
|
||||
ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m", InstanceName: "m"}},
|
||||
ValidationLLMClient: client,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected run error: %v", err)
|
||||
}
|
||||
if scheduler.runs < 2 {
|
||||
t.Fatalf("expected scheduler to run per batch, got %d", scheduler.runs)
|
||||
}
|
||||
|
||||
if client.calls < 2 {
|
||||
t.Fatalf("expected multiple llm calls for batching, got %d", client.calls)
|
||||
}
|
||||
@@ -1310,10 +875,14 @@ func TestRunnerLLMValidatorDiagnosticsWrittenAndRedacted(t *testing.T) {
|
||||
primarySecret := "runner-primary-secret"
|
||||
validationSecret := "runner-validation-secret"
|
||||
client := &fakeStructuredClient{responses: []validators.LLMValidationResponse{{Validations: []validators.LLMValidationDecision{{CorrectionIndex: 0, Approved: true, Confidence: 0.9, Reason: validationSecret}}}}}
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility, "")
|
||||
cfg := config.Default()
|
||||
cfg.PrimaryLLM.APIKey = primarySecret
|
||||
cfg.ValidationLLM.APIKey = validationSecret
|
||||
llmValidator, _ := validators.NewLLMBackedValidator("spoken_form_plausibility_review", validators.LLMValidatorTypeSpokenFormPlausibility)
|
||||
t.Setenv("RUNNER_PRIMARY_KEY", primarySecret)
|
||||
t.Setenv("RUNNER_VALIDATION_KEY", validationSecret)
|
||||
loaded, loadErr := config.Load(ck.Bytes("config.yml", []byte("version: 2\nbackends:\n primary:\n endpoint: https://primary.invalid/v1\n credential: RUNNER_PRIMARY_KEY\n validation:\n endpoint: https://validation.invalid/v1\n credential: RUNNER_VALIDATION_KEY\nllm:\n proposal_profile: base\n")), "")
|
||||
if loadErr != nil {
|
||||
t.Fatal(loadErr)
|
||||
}
|
||||
cfg := loaded.Config
|
||||
diagDir := t.TempDir()
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
"m": fakeModule{key: "m", policy: proposals.ReplacementPolicyRequireUnique, validators: []contracts.Validator{llmValidator}, proposeF: func(req contracts.ProposalRequest) ([]proposals.CorrectionProposal, error) {
|
||||
@@ -1359,16 +928,6 @@ func TestRunnerLLMValidatorDiagnosticsWrittenAndRedacted(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerAcceptsLLMSchedulerType(t *testing.T) {
|
||||
s, err := llm.NewScheduler(1)
|
||||
if err != nil {
|
||||
t.Fatalf("NewScheduler: %v", err)
|
||||
}
|
||||
if s == nil {
|
||||
t.Fatal("expected scheduler instance")
|
||||
}
|
||||
}
|
||||
|
||||
type proposalGenerationModule struct {
|
||||
key string
|
||||
policy proposals.ReplacementPolicy
|
||||
@@ -1385,13 +944,11 @@ func (m proposalGenerationModule) Propose(ctx context.Context, req contracts.Pro
|
||||
WorkingTranscript: req.WorkingTranscript,
|
||||
Config: req.Config,
|
||||
Glossary: req.Glossary,
|
||||
Messages: []contracts.LLMMessage{
|
||||
{Role: "system", Content: "return transcript corrections"},
|
||||
{Role: "user", Content: "produce one safe correction"},
|
||||
},
|
||||
LLMClient: req.LLMClient,
|
||||
Scheduler: req.LLMScheduler,
|
||||
DiagnosticsDir: req.DiagnosticsDir,
|
||||
PromptID: "modules.grammar.proposal",
|
||||
PromptVersion: "v1",
|
||||
Variables: map[string]string{"TranscriptDescriptionBlock": "", "GlossaryJSON": "[]", "SectionJSON": "[]"},
|
||||
LLMClient: req.LLMClient,
|
||||
DiagnosticsDir: req.DiagnosticsDir,
|
||||
})
|
||||
if err != nil {
|
||||
return contracts.ProposalResult{}, err
|
||||
@@ -1428,8 +985,8 @@ func TestRunnerProposalGenerationHelperFlowsThroughPipeline(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
scheduler := &countingScheduler{}
|
||||
cfg := config.Default()
|
||||
|
||||
cfg := runnerTestConfig()
|
||||
diagDir := t.TempDir()
|
||||
|
||||
r := New(fakeFactory{modules: map[string]contracts.TranscriptModule{
|
||||
@@ -1441,7 +998,6 @@ func TestRunnerProposalGenerationHelperFlowsThroughPipeline(t *testing.T) {
|
||||
Transcript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "teh cat"}}},
|
||||
ModuleSpecs: []contracts.ModuleRunSpec{{ModuleKey: "m", InstanceName: "m"}},
|
||||
ProposalLLMClient: client,
|
||||
ProposalLLMScheduler: scheduler,
|
||||
ProposalDiagnosticsDir: diagDir,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -1450,9 +1006,6 @@ func TestRunnerProposalGenerationHelperFlowsThroughPipeline(t *testing.T) {
|
||||
if out.FinalTranscript.Segments[0].Text != "the cat" {
|
||||
t.Fatalf("expected proposal-generated correction to apply, got %q", out.FinalTranscript.Segments[0].Text)
|
||||
}
|
||||
if scheduler.runs != 1 {
|
||||
t.Fatalf("expected proposal scheduler use, got %d runs", scheduler.runs)
|
||||
}
|
||||
if len(out.ModuleResults) != 1 || len(out.ModuleResults[0].AppliedChanges) != 1 {
|
||||
t.Fatalf("expected one applied change, got %+v", out.ModuleResults)
|
||||
}
|
||||
@@ -1475,7 +1028,7 @@ func TestRunnerGrammarModuleUsesConfidenceThreshold(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
cfg := config.Default()
|
||||
cfg := runnerTestConfig()
|
||||
cfg.Thresholds.Grammar = 0.9
|
||||
factory := modules.NewFactory(modules.Dependencies{})
|
||||
out, err := New(factory).Run(context.Background(), RunInput{
|
||||
@@ -1509,7 +1062,7 @@ func TestRunnerGlossaryModuleUsesConfidenceThreshold(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
cfg := config.Default()
|
||||
cfg := runnerTestConfig()
|
||||
cfg.Thresholds.Glossary = 0.9
|
||||
factory := modules.NewFactory(modules.Dependencies{})
|
||||
out, err := New(factory).Run(context.Background(), RunInput{
|
||||
|
||||
Reference in New Issue
Block a user