org.llm4s.agent.testkit
Members list
Type members
Classlikes
The contract every org.llm4s.agent.graph.Checkpointer must meet, as a ScalaTest suite. Mix it into a spec, implement newCheckpointer, and every case below runs against your store:
The contract every org.llm4s.agent.graph.Checkpointer must meet, as a ScalaTest suite. Mix it into a spec, implement newCheckpointer, and every case below runs against your store:
class SqliteCheckpointerContractSpec extends AnyFlatSpec with CheckpointerContract:
protected def newCheckpointer(clock: java.time.Clock): Checkpointer =
SqliteCheckpointer.open(freshDatabaseFile(), clock)
The cases are the ones the Checkpointer Scaladoc states, in three groups:
- '''The store''', called directly: commits applied atomically or not at all; a new checkpoint accepted only over the latest (GraphError.CheckpointConflict) and pending writes only for it (GraphError.InvalidCommit); events numbered contiguously in commit order and never reused, also after a refused commit, compaction or
deleteThread- a new thread of a deleted thread's id is numbered on past the deleted thread's events, and a read from inside the deleted log is refused (GraphError.ThreadDeleted), also while waiting; compaction and GraphError.ReplayUnavailable;deleteThread, fenced by the current claim's token like a commit (GraphError.StaleClaim otherwise, deleting nothing); checkpoints, pending writes and events read back as written; the checkpoint history - every superseded checkpoint kept, newest first, paged and read by id, ids never repeated, a blocked turn retracted (Commit.retractTurn), and pruning by age (by when the store committed, never a skewed producer's timestamps), by count and by event count, never the latest checkpoint; effect records (design §4.24) - upserted by key and fenced like any commit, released at a commit with a checkpoint exactly asCommit.releasesEffectsays, kept bypruneand across a reopen, removed bydeleteThread; child threads (design 4.41) - a checkpoint'sorigin(its trace context included, design 4.58) andoutcomeread back, a format-7 checkpoint read with neither, andchildrenlisting a parent's children oldest first, paged, without a tenant, never one deleted or one whose latest checkpoint names another parent; the thread registry (design 4.52) - metadata kept and replaced only by a fenced commit, a tenant's root threads listed newest first by commit time, paged, filtered by status (judged by the latest checkpoint and a live claim, by the store's clock), agent, metadata and age, and its incomplete threads oldest first, never another tenant's, a child or a deleted thread; the retention sweep (design 4.53) - every thread, tombstones included, visited once in pages of at mostbatchSize, each pruned aspruneprunes it, by commit time, a protected thread keeping the newest checkpoint of each protected run and its tombstone, a failing guard changing nothing, and a tombstone reclaimed only once older thantombstoneAgeby the store's clock, its log's readers still refused withThreadDeletedand a new thread of the id numbered past it, across a reopen (on storage of its own, isolatedCheckpointer); and claims and fencing - one live claim per thread, tokens strictly increasing, takeover once a claim has expired by the store's clock (naming the expired claim's holder astakenOverFrom), the tenant recorded on each claim - a holder named, in a refusal or astakenOverFrom, only to a claim of its own tenant, also across a reopen - renewal and release by the current token only, a refused claim leaving the live one as it was, and every commit refused with GraphError.StaleClaim unless it carries the current token - checked before any other refusal, and including from many threads at once; one claim granted of many raced at once from as many stores over the storage (contenders); stored values kept apart from those callers pass in and are given back - and change notification:awaitEventsAfterreturns what is there at once, returns a commit made meanwhile through this store or another over the same storage within about itstimeout- and, for a store told of commits (wakeBound), within that bound of the commit however long itstimeout- returns empty when nothing lands, and ends its wait when interrupted. - '''Two runtimes over one store''': a second GraphRuntime is refused a thread whose run is live in the first (GraphError.ThreadBusy naming the holder); a run that keeps renewing is not taken over; once a claim expires, the second runtime recovers the thread without re-running the first run's completed tasks, and the first run's later commits are refused as stale, so it ends with GraphError.ClaimLost and leaves nothing in the thread; and of several runtimes recovering one thread at once, exactly one is admitted;
updateStateandforkfrom a second runtime are refused while a run is live in the first, and fenced by their own claim; and a run a node blocks leaves nothing of its turn in the history; and a claim of another tenant on a thread never reveals its run id to the thread's own tenant - not throughThreadBusy, also while it has claimed but not yet checkpointed, not asRunRecovered.takenOverFromonce it lapses, and not throughClaimLostwhen it takes over a run of the owner. - '''Subscriptions across runtimes''', each runtime over a store of its own on the same storage (sibling): a subscription in one runtime receives the durable events of runs another runtime commits - in order, without gap or duplicate, when it joined before or mid-run - while runs alternate between the runtimes; resumes from a sequence number after its runtime restarts; never receives another runtime's live progress; stops watching the store when cancelled; and ends with DisconnectReason.TenantMismatch, delivering nothing of it, when another runtime creates the thread for another tenant.
Claim expiry is judged by the store's clock. By default the store under test must use the clock it is given for that (and may use it for nothing else), and the suite moves a ManualClock; it never waits for real time to pass a claim's ttl. A store that judges expiry by a clock it cannot be handed - a database server's now() - overrides advanceStoreClock to make claims expire as if that clock had moved (for example by moving every stored expiry back), and sets exactExpiry to false, so that the cases check expiry by its effect only.
A durable store also overrides reopen, so that the cases about claims and tokens surviving a restart - a process that closes the store and opens the same storage again - run against storage that was really closed. The default reopens nothing and hands the same instance back. It overrides sibling too, so that the cases about subscriptions across runtimes put each runtime on a store of its own - another connection, as another process would open - and change notification is checked between stores, not within one; and the cases racing many claims at once spread them over that many stores (contenders, by default built with sibling), so that a store's own storage decides the race - for SqliteCheckpointer, sixteen connections to one file.
Each case gets a new store from newCheckpointer and uses its own thread ids; the store need not be empty of other threads.
Attributes
- Companion
- object
- Source
- CheckpointerContract.scala
- Supertypes
-
trait OptionValuestrait EitherValuestrait Matcherstrait Explicitlytrait MatcherWordstrait Tolerancetrait AnyFlatSpecLiketrait Documentingtrait Alertingtrait Notifyingtrait Informingtrait CanVerbtrait MustVerbtrait ShouldVerbtrait TestRegistrationtrait TestSuitetrait Suitetrait Serializabletrait Assertionstrait TripleEqualstrait TripleEqualsSupportclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- trait
- Source
- CheckpointerContract.scala
- Supertypes
- Self type
-
CheckpointerContract.type
The delegation contract (design 4.44), as a ScalaTest suite: a parent agent delegating to a subagent on a store, through every row of rule 14's table - what a task call does, by the child thread's latest checkpoint, when the call runs again (the parent's recover):
The delegation contract (design 4.44), as a ScalaTest suite: a parent agent delegating to a subagent on a store, through every row of rule 14's table - what a task call does, by the child thread's latest checkpoint, when the call runs again (the parent's recover):
| child's latest checkpoint | the call |
|---|---|
| none | starts the child |
Running |
continues the child from its checkpoint |
Suspended |
surfaces the child's interrupts again |
Completed or Failed |
returns the outcome there, with no run |
Mix it into a spec and give it a fresh store per case:
class MyStoreDelegationSpec extends AnyFlatSpec with DelegationContract:
protected def newCheckpointer(): Checkpointer = MyStore.open(...)
Attributes
- Companion
- object
- Source
- DelegationContract.scala
- Supertypes
-
trait LoneElementtrait OptionValuestrait EitherValuestrait Matcherstrait Explicitlytrait MatcherWordstrait Tolerancetrait AnyFlatSpecLiketrait Documentingtrait Alertingtrait Notifyingtrait Informingtrait CanVerbtrait MustVerbtrait ShouldVerbtrait TestRegistrationtrait TestSuitetrait Suitetrait Serializabletrait Assertionstrait TripleEqualstrait TripleEqualsSupportclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- trait
- Source
- DelegationContract.scala
- Supertypes
- Self type
-
DelegationContract.type
The contract every org.llm4s.agent.harness.FileBackend must meet (design §4.25), as a ScalaTest suite: mix it into a spec and implement newBackend. Every case runs the backend's operations as tool calls of a real agent run, so a state backend's updates are committed by the runtime and an external backend's effects go through its replay guard, as they would in production:
The contract every org.llm4s.agent.harness.FileBackend must meet (design §4.25), as a ScalaTest suite: mix it into a spec and implement newBackend. Every case runs the backend's operations as tool calls of a real agent run, so a state backend's updates are committed by the runtime and an external backend's effects go through its replay guard, as they would in production:
class StateFileBackendContractSpec extends AnyFlatSpec with FileBackendContract:
protected def newBackend(): FileBackend = StateFileBackend(maxBytes = 200)
It checks each operation and its refusals; that two mutations of one path in one batch both apply, the second against the first's result, because their locks conflict; that locks conflict for one path and for a directory and a path beneath it, and not for siblings; and, when the backend says so, that a batch over its bound refuses exactly one write, the overflowing one (boundedBatch) and that two tenants see separate trees (tenantScoped).
Replay is ReplayContract's: an external backend also runs it, with FileBackendContract.EditSubject as the subject, once per support class it offers.
Attributes
- Companion
- object
- Source
- FileBackendContract.scala
- Supertypes
-
trait EitherValuestrait Matcherstrait Explicitlytrait MatcherWordstrait Tolerancetrait AnyFlatSpecLiketrait Documentingtrait Alertingtrait Notifyingtrait Informingtrait CanVerbtrait MustVerbtrait ShouldVerbtrait TestRegistrationtrait TestSuitetrait Suitetrait Serializabletrait Assertionstrait TripleEqualstrait TripleEqualsSupportclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- trait
- Source
- FileBackendContract.scala
- Supertypes
- Self type
-
FileBackendContract.type
The contract org.llm4s.agent.harness.FileTools meets over a org.llm4s.agent.harness.FileBackend (design §4.32), as a ScalaTest suite: mix it into a spec and implement newBackend. A backend that passes FileBackendContract should pass this too; it runs the seven tools, by their public names and arguments, as a model calls them in a real agent run:
The contract org.llm4s.agent.harness.FileTools meets over a org.llm4s.agent.harness.FileBackend (design §4.32), as a ScalaTest suite: mix it into a spec and implement newBackend. A backend that passes FileBackendContract should pass this too; it runs the seven tools, by their public names and arguments, as a model calls them in a real agent run:
class WorkspaceFileToolsSpec extends AnyFlatSpec with FileToolsContract:
protected def newBackend(): FileBackend = WorkspaceFileBackend(...)
It checks each tool and its refusals; that two edits of one file in one message both apply, in order; that a read in the same message as a write sees the file whole, before or after the write (over thread state, before: FileToolsSpec checks that); and, when the backend says so, that a batch over its bound refuses exactly one write (boundedBatch) and that two tenants' files stay apart (tenantScoped).
Attributes
- Companion
- object
- Source
- FileToolsContract.scala
- Supertypes
-
trait EitherValuestrait Matcherstrait Explicitlytrait MatcherWordstrait Tolerancetrait AnyFlatSpecLiketrait Documentingtrait Alertingtrait Notifyingtrait Informingtrait CanVerbtrait MustVerbtrait ShouldVerbtrait TestRegistrationtrait TestSuitetrait Suitetrait Serializabletrait Assertionstrait TripleEqualstrait TripleEqualsSupportclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- trait
- Source
- FileToolsContract.scala
- Supertypes
- Self type
-
FileToolsContract.type
A clock that moves only when told to, for driving claim expiry in a test: hand it to the store under test, and advance it past a claim's ttl to let another run take the thread over. Safe to read and move from any thread.
A clock that moves only when told to, for driving claim expiry in a test: hand it to the store under test, and advance it past a claim's ttl to let another run take the thread over. Safe to read and move from any thread.
Attributes
- Source
- ManualClock.scala
- Supertypes
The contract every external target of the harness must meet when recover runs its effects again (design §4.24), as a ScalaTest suite: mix it into a spec, implement newSubject with the target's own guarded code path, and every case below runs against it - once per support class the target offers, one spec each.
The contract every external target of the harness must meet when recover runs its effects again (design §4.24), as a ScalaTest suite: mix it into a spec, implement newSubject with the target's own guarded code path, and every case below runs against it - once per support class the target offers, one spec each.
class StoreFileBackendReplaySpec extends AnyFlatSpec with ReplayContract:
protected def newSubject(): ReplayContract.Subject = StoreFileSubject(InMemoryLongTermStore())
Each case runs a one-task graph on a real GraphRuntime whose task performs the subject's two effects (slots 0 and 1), then crashes the run at a point - the store goes down at a chosen effect record write, before or after writing it, or after the task's commit, and every later write fails as in a dead process - moves the store's clock past the claim's lease, and recovers on a new runtime. The task records each slot's outcome, or the guard's refusal, as a tool turns a refusal into a tool error; any other failure fails the task, as a tool's Fatal does. For every crash point and the subject's ReplaySupport it checks the target's state, the outcomes against a run that never crashed, that the two effects never collide, and that once the run completes the store holds no effect record and the target no applied key:
| Crash point | Transactional | Verifiable | Unverifiable |
|---|---|---|---|
| before the intent is written | once | once | once |
| after the intent, before the effect | once | once | refused, never run (run once if idempotent) |
| after the effect, before its outcome is written | once, the target's recorded outcome | once, the presumed outcome | refused (run again if idempotent) |
| after the outcome, before the task's commit | once, the stored outcome | same | same |
| after the task's commit | not re-run | same | same |
and, when another thread changes the target after the effect and before its outcome is written: the recorded outcome with the change left in place (Transactional), GraphError.EffectReplayRefused (Verifiable), refused (Unverifiable). It also checks that a run left recoverable for a year, its thread pruned by age meanwhile, still replays from its records.
Two cases put a stale claimant beside its successor on a real takeover. In each, a run pauses inside the store, its claim expires, and a successor recovers the thread; then the stale run carries on:
- The stale run paused after reading no record. Its successor applied the effect and crashed before recording the outcome.
- The stale run paused after writing its intent. Its successor replayed the effect and completed.
Either way the effect must apply once. That holds only if the target fences itself: a Transactional target refuses a token older than the newest it has seen for the key's scope (org.llm4s.agent.graph.AppliedKeys), and a Verifiable effect is a compare-and-set against its plan's before.
Override newCheckpointer to run the suite against another store; the default is InMemoryCheckpointer. The store must judge claims by the clock it is given.
Attributes
- Companion
- object
- Source
- ReplayContract.scala
- Supertypes
-
trait OptionValuestrait EitherValuestrait Matcherstrait Explicitlytrait MatcherWordstrait Tolerancetrait AnyFlatSpecLiketrait Documentingtrait Alertingtrait Notifyingtrait Informingtrait CanVerbtrait MustVerbtrait ShouldVerbtrait TestRegistrationtrait TestSuitetrait Suitetrait Serializabletrait Assertionstrait TripleEqualstrait TripleEqualsSupportclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- trait
- Source
- ReplayContract.scala
- Supertypes
- Self type
-
ReplayContract.type
The contract every org.llm4s.agent.service.RunQueue must meet (design 4.55), as a ScalaTest suite. Mix it into a spec and implement newStores - a store's checkpointer and the queue that lives in the same database:
The contract every org.llm4s.agent.service.RunQueue must meet (design 4.55), as a ScalaTest suite. Mix it into a spec and implement newStores - a store's checkpointer and the queue that lives in the same database:
class SqliteRunQueueContractSpec extends AnyFlatSpec with RunQueueContract:
protected def newStores(clock: java.time.Clock): RunQueueContract.Stores =
val store = SqliteCheckpointer.open(freshDatabaseFile(), clock).toOption.get
RunQueueContract.Stores(store, store.runQueue)
The cases are the ones the RunQueue Scaladoc states: submission - entries read back as submitted (the submitter's trace context included, kept through a reopen and a retried key), ids in submission order, the per-thread run order of rule 5 (an attempted entry first, then Interrupt entries, then the rest, each by id; a later Interrupt closing an earlier one that has not started), Reject refused while the thread has an open entry or a live claim, and submission keys looked up or inserted per tenant; cancel and interrupt (W3) - an entry not started closed at once, a started one asked to stop durably, the request answered with its renewals and takes, a cancel never replaced by an interrupt, per tenant, and each close reported only to the call that made it, of two cancels or two Interrupt submissions raced at once (#2368); tenant scoping and the TicketExpired / TicketNotFound answers; leases - only a thread's head taken, the oldest head first, one live lease per thread, takeover once a lease expires by the store's clock with a greater token, renewal, release and every call under a replaced lease refused; attempts - recorded with their run ids, a reused run id refused, maxAttempts bounding them, and a thread deleted since submission closing its entry Cancelled(ThreadDeleted); closes, their outcomes read back, awaitClosed and reclamation by ticket lifetime; rule 4's evidence - Checkpointer.prune keeping the newest checkpoint of an open entry's runs; one lease of many takes raced at once from as many queues over the storage; and, for metrics (design 4.59), a take's expiredLease mark and each agent's depth.
Lease expiry and close times are judged by the store's clock. By default the store must use the clock it is given, and the suite moves a ManualClock; a store that judges time by a database server's clock overrides advanceStoreClock and sets exactExpiry to false, as for CheckpointerContract. A durable store overrides reopen and sibling.
Cases may share one database: each uses thread ids and agent ids of its own, and takes only its own agents' entries.
Attributes
- Companion
- object
- Source
- RunQueueContract.scala
- Supertypes
-
trait OptionValuestrait EitherValuestrait Matcherstrait Explicitlytrait MatcherWordstrait Tolerancetrait AnyFlatSpecLiketrait Documentingtrait Alertingtrait Notifyingtrait Informingtrait CanVerbtrait MustVerbtrait ShouldVerbtrait TestRegistrationtrait TestSuitetrait Suitetrait Serializabletrait Assertionstrait TripleEqualstrait TripleEqualsSupportclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- trait
- Source
- RunQueueContract.scala
- Supertypes
- Self type
-
RunQueueContract.type
