Unify ready pipeline jobs under one worker pool and remove the global LLM scheduler

This commit is contained in:
2026-11-22 01:45:31 +00:00
parent b47208bd57
commit 9a527b77c4
24 changed files with 496 additions and 1160 deletions

View File

@@ -151,22 +151,33 @@ type laneEngineConfig struct {
states []*laneExtractState
}
// laneJob is ready to execute: a nil chunk selects the lane's merge/normalize
// continuation, otherwise it selects one extraction. Prerequisites are resolved
// by the coordinator before dispatch, never by an occupied worker.
type laneJob struct {
lane *laneExtractState
chunk *source.Chunk
}
type laneJobResult struct {
extract *extractJobResult
completion *laneCompletion
}
type laneEngine struct {
runner *Runner
laneEngineConfig
ctx context.Context
cancel context.CancelFunc
workerCount int
jobs chan extractJob
results chan extractJobResult
completions chan laneCompletion
continuations chan *laneExtractState
continuationWorkers sync.WaitGroup
pending []*laneExtractState
completedOutputs []RunOutput
runErrors []orderedRunError
launched int
completed int
ctx context.Context
cancel context.CancelFunc
workerCount int
jobs chan laneJob
results chan laneJobResult
workers sync.WaitGroup
pending []*laneExtractState
completedOutputs []RunOutput
runErrors []orderedRunError
nextExtract int
active int
}
func (r *Runner) runLaneEngine(parent context.Context, config laneEngineConfig) ([]RunOutput, []orderedRunError) {
@@ -175,31 +186,20 @@ func (r *Runner) runLaneEngine(parent context.Context, config laneEngineConfig)
}
func newLaneEngine(r *Runner, parent context.Context, config laneEngineConfig) *laneEngine {
workerCount := config.input.ExtractWorkers
if workerCount < 1 {
workerCount = 1
workerCount := config.input.Workers
if workerCount == 0 {
workerCount = DefaultWorkers
}
ctx, cancel := context.WithCancel(parent)
return &laneEngine{
runner: r, laneEngineConfig: config, ctx: ctx, cancel: cancel, workerCount: workerCount,
jobs: make(chan extractJob, workerCount), results: make(chan extractJobResult, workerCount),
completions: make(chan laneCompletion, len(config.states)), continuations: make(chan *laneExtractState, workerCount),
jobs: make(chan laneJob), results: make(chan laneJobResult, workerCount),
completedOutputs: make([]RunOutput, len(config.states)),
}
}
func (e *laneEngine) run() ([]RunOutput, []orderedRunError) {
defer e.cancel()
e.initializeCollection()
e.startExtractWorkers()
e.startContinuationWorkers()
e.collect()
close(e.continuations)
e.continuationWorkers.Wait()
return e.completedOutputs, e.runErrors
}
func (e *laneEngine) initializeCollection() {
for _, state := range e.states {
if state.terminal {
e.completedOutputs[state.index] = state.output
@@ -207,90 +207,96 @@ func (e *laneEngine) initializeCollection() {
e.pending = append(e.pending, state)
}
}
e.startWorkers()
e.collect()
close(e.jobs)
e.workers.Wait()
return e.completedOutputs, e.runErrors
}
func (e *laneEngine) startExtractWorkers() {
var workers sync.WaitGroup
func (e *laneEngine) startWorkers() {
for i := 0; i < e.workerCount; i++ {
workers.Add(1)
e.workers.Add(1)
go func() {
defer workers.Done()
defer e.workers.Done()
for job := range e.jobs {
if e.ctx.Err() != nil {
if job.chunk != nil {
result := e.runner.runExtractJob(e.ctx, e.input, e.doc, e.sourceInput, e.sessionID, extractJob{lane: job.lane, chunk: *job.chunk})
e.results <- laneJobResult{extract: &result}
continue
}
e.results <- e.runner.runExtractJob(e.ctx, e.input, e.doc, e.sourceInput, e.sessionID, job)
completion := laneCompletion{index: job.lane.index}
if err := e.ctx.Err(); err != nil {
completion.err = err
} else {
completion.output, completion.err = e.runner.continueLane(e.ctx, e.input, e.checkpoints, e.loader, e.doc, e.sourceInput, e.sessionID, e.chunks, job.lane)
}
e.results <- laneJobResult{completion: &completion}
}
}()
}
go e.dispatchExtractJobs()
go func() { workers.Wait(); close(e.results) }()
}
func (e *laneEngine) dispatchExtractJobs() {
defer close(e.jobs)
for chunkIndex := range e.chunks {
for laneIndex := range e.states {
state := e.states[laneIndex]
if state.terminal || state.decision.Reused {
continue
}
select {
case e.jobs <- extractJob{lane: state, chunk: e.chunks[chunkIndex]}:
case <-e.ctx.Done():
// nextJob gives ready continuations priority, then preserves chunk-first,
// lane-second extraction order. Dispatch remains lazy rather than creating a
// queue or goroutine for every extraction.
func (e *laneEngine) nextJob() (laneJob, bool) {
if len(e.pending) > 0 {
return laneJob{lane: e.pending[0]}, true
}
for e.nextExtract < len(e.chunks)*len(e.states) {
state := e.states[e.nextExtract%len(e.states)]
if !state.terminal && !state.decision.Reused {
return laneJob{lane: state, chunk: &e.chunks[e.nextExtract/len(e.states)]}, true
}
e.nextExtract++
}
return laneJob{}, false
}
// collect alone owns dispatch and result accounting. Cancellation disables new
// dispatch, but every dispatched job still returns one result before workers exit.
func (e *laneEngine) collect() {
for {
if e.ctx.Err() != nil {
if e.active == 0 {
return
}
e.receiveResult(<-e.results)
continue
}
}
}
func (e *laneEngine) startContinuationWorkers() {
for i := 0; i < e.workerCount; i++ {
e.continuationWorkers.Add(1)
go func() {
defer e.continuationWorkers.Done()
for state := range e.continuations {
if err := e.ctx.Err(); err != nil {
e.completions <- laneCompletion{index: state.index, err: err}
continue
}
laneOutput, err := e.runner.continueLane(e.ctx, e.input, e.checkpoints, e.loader, e.doc, e.sourceInput, e.sessionID, e.chunks, state)
e.completions <- laneCompletion{index: state.index, output: laneOutput, err: err}
}
}()
}
}
// collect is the sole owner of pending, launch, and completion accounting. It
// must drain closed extract results and one completion for every launched
// continuation, even after cancellation.
func (e *laneEngine) collect() {
resultChannel := (<-chan extractJobResult)(e.results)
for resultChannel != nil || len(e.pending) > 0 || e.completed < e.launched {
var continuationChannel chan<- *laneExtractState
var nextContinuation *laneExtractState
if len(e.pending) > 0 && e.ctx.Err() == nil {
continuationChannel = e.continuations
nextContinuation = e.pending[0]
} else if e.ctx.Err() != nil {
e.pending = nil
job, ready := e.nextJob()
if !ready && e.active == 0 {
return
}
var jobs chan<- laneJob
if ready && e.active < e.workerCount {
jobs = e.jobs
}
select {
case continuationChannel <- nextContinuation:
e.pending = e.pending[1:]
e.launched++
case result, ok := <-resultChannel:
if !ok {
resultChannel = nil
continue
case <-e.ctx.Done():
case result := <-e.results:
e.receiveResult(result)
case jobs <- job:
e.active++
if job.chunk == nil {
e.pending = e.pending[1:]
} else {
e.nextExtract++
}
e.handleExtractResult(result)
case completion := <-e.completions:
e.handleCompletion(completion)
}
}
}
func (e *laneEngine) receiveResult(result laneJobResult) {
e.active--
if result.extract != nil {
e.handleExtractResult(*result.extract)
} else {
e.handleCompletion(*result.completion)
}
}
func (e *laneEngine) handleExtractResult(result extractJobResult) {
state := e.states[result.laneIndex]
state.remaining--
@@ -317,7 +323,6 @@ func (e *laneEngine) handleExtractResult(result extractJobResult) {
}
func (e *laneEngine) handleCompletion(completion laneCompletion) {
e.completed++
e.completedOutputs[completion.index] = completion.output
if completion.err != nil {
e.runErrors = append(e.runErrors, classifyLaneError(completion.index, len(e.chunks), completion.err))