AgentWorker

org.llm4s.agent.service.AgentWorker
See theAgentWorker companion object
@Experimental
final class AgentWorker

Runs queued entries (Stage 5 W2, design 4.56): it takes entries from a store's RunQueue and runs them on the agents it was built with, whose runtimes are over the same store, at most settings.concurrency at once. Built with AgentWorker.apply, started with start, stopped with shutdown.

For each entry it takes it:

  1. holds the entry's lease, renewing it every lease.renewEvery beside the run's claim, which the run's runtime renews; a renewal the queue refuses (another worker took the entry over, or it closed) cancels the run, which then writes nothing more, rather than run on beside the entry's next attempt;
  2. defers to a live claim on the thread - a run there, queued or direct, on any machine - by giving the entry back (RunQueue.release) without recording an attempt, so deferring never spends maxAttempts;
  3. leaves an entry whose thread its agent's graph cannot take on - another graph version's run in flight, during a rolling upgrade - for a worker that can, passing over it for a while, and closes it Failed(Unrunnable) once it has waited settings.unrunnableAfter by the store's clock;
  4. records the attempt with a new RunId.random() before touching the thread (rule 4), then acts on AttemptPlan: starts the input, recovers the thread, or closes the entry from the thread's state;
  5. closes the entry from the thread's state once its run has ended at rest, or - for a run that failed - leaves it to come back when its lease expires, for an attempt that recovers it, until its attempts run out;
  6. on AttemptStart.Exhausted, settles the thread first if its input was admitted - each tool call left without a result gets a synthetic one and a Failed checkpoint is committed under the thread's claim - and closes the entry Failed(AttemptsExhausted);
  7. stops the run when a stop is asked of the entry (W3, design 4.57), which it learns of from its lease renewals: a cancel (RunQueue.cancel) at once, as AgentRun.cancel does, an interrupt (a later Interrupt entry) at the run's next superstep boundary. It waits for the run to exit, and the run settles itself as it stops - a Failed checkpoint, every tool call left without a result given AgentWorker.CancelledResult or AgentWorker.InterruptedResult, committed under the run's own claim - so the thread is at rest and the next entry starts normally. It then closes the entry from how the run ended: Cancelled or Interrupted if the stop ended it, the run's own outcome if it reached rest first. A stop asked of an entry no run is running - its worker died - is acted on by the worker that next takes it, recording no attempt: the thread is settled the same way, under a claim of its own, if the entry's run is its incomplete latest.

A crashed worker's entries come back when their leases expire, by the store's clock, and the worker that takes one recovers its thread, once the crashed run's claim - on the same clock - has expired too.

With settings.sweepEvery set it also runs the store-wide retention sweep (Checkpointer.sweep, with a guard that keeps every open entry's runs and its thread's tombstone) and reclaims closed entries past their ticket lifetime, one instance of the fleet at a time: the sweep holds a store claim on a reserved thread id while it runs.

Built with a MetricsCollector, it records the run queue's and its own metrics (org.llm4s.agent.metrics.AgentMetrics, design 4.59): lease expiries, waits, attempts and what they did, deferrals, lease lapses and losses, closes by outcome, sweep passes and drains, and every settings.queueDepthEvery its agents' queue depth. The runs, claims and commits its agents make are a MeteredCheckpointer's to record.

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

Where the worker is in its life.

Where the worker is in its life.

Attributes

The worker's readiness, for a service's probes: it is running, its loop has beaten within a bound and its last take succeeded, and the store answers a read of the checkpoints and of the queue, each within the store's bound.

The worker's readiness, for a service's probes: it is running, its loop has beaten within a bound and its last take succeeded, and the store answers a read of the checkpoints and of the queue, each within the store's bound.

Attributes

Drains and stops the worker: it stops taking entries, lets its running runs reach a suspension or their end within settings.drainGrace, then cancels the rest and waits up to settings.exitTimeout for them to exit. A cancelled run's lease is given back (or its entry closed from how it ended) only once the run has exited, and its claim is released by its runtime as it exits, so another worker recovers it at once without overlapping its side effects. A run that has not exited by then keeps its claim and lease: renewing the lease stops here, the claim's renewal stops when the process exits, and both expire after their ttl, as after a crash. Returns at once, with an empty report, on a worker never started or already stopped.

Drains and stops the worker: it stops taking entries, lets its running runs reach a suspension or their end within settings.drainGrace, then cancels the rest and waits up to settings.exitTimeout for them to exit. A cancelled run's lease is given back (or its entry closed from how it ended) only once the run has exited, and its claim is released by its runtime as it exits, so another worker recovers it at once without overlapping its side effects. A run that has not exited by then keeps its claim and lease: renewing the lease stops here, the claim's renewal stops when the process exits, and both expire after their ttl, as after a crash. Returns at once, with an empty report, on a worker never started or already stopped.

Attributes

def start(): Unit

Starts taking entries, and the retention sweep's schedule. Once only: a second call changes nothing.

Starts taking entries, and the retention sweep's schedule. Once only: a second call changes nothing.

Attributes

Concrete fields

val holder: String

The lease holder this worker takes entries as: its label, if any, and a random suffix.

The lease holder this worker takes entries as: its label, if any, and a random suffix.

Attributes