Class FakeRunLifecycle

java.lang.Object
com.agentforge4j.llm.fake.FakeRunLifecycle

public final class FakeRunLifecycle extends Object
The single per-run store for the fake provider: each run id maps to one entry holding the run's FakeScript and its ordinal counters, so registering, deregistering, or evicting a run clears both in one place — there is no separate counter map to keep in sync, and the FakeLlmClient stays stateless.

Lifecycle:

  • register(String, FakeScript) installs (or atomically replaces) a run's script, resetting that run's counters — a fresh script implies a fresh sequence.
  • deregister(String) removes the run's script and counters together.
  • A TTL leak guard evicts runs whose last access is older than the configured TTL; it runs opportunistically when a new run is first seen (never on a hot read path) so an active long-running run — kept fresh by its own calls — is not evicted (no LRU on the leak guard).
  • An optional max-run cap (dev/demo only) evicts the least-recently-accessed run when the cap is exceeded, always logging the eviction — never a silent drop.

Thread-safe: a ConcurrentHashMap of runs, per-run ConcurrentHashMap counters, and AtomicInteger.getAndIncrement() ordinals. A single instance serves concurrent runs.

  • Constructor Details

    • FakeRunLifecycle

      public FakeRunLifecycle()
      Creates a lifecycle with no TTL eviction and no run cap (system UTC clock).
    • FakeRunLifecycle

      public FakeRunLifecycle(Clock clock, Duration ttl, int maxRuns)
      Creates a lifecycle with the given leak guards.
      Parameters:
      clock - clock for last-access timestamps and TTL; must not be null
      ttl - stale-run time-to-live, or null/zero/negative to disable TTL eviction
      maxRuns - optional max concurrent runs (0 disables the cap); dev/demo only
  • Method Details

    • register

      public void register(String runId, FakeScript script)
      Installs (or atomically replaces) the script for a run, resetting that run's ordinal counters.
      Parameters:
      runId - run id; must not be blank
      script - script to register; must not be null
    • deregister

      public void deregister(String runId)
      Removes a run's script and ordinal counters together. No-op when the run is unknown.
      Parameters:
      runId - run id; must not be blank
    • sweep

      public void sweep()
      Runs the TTL leak guard immediately, evicting stale runs.
    • isRegistered

      public boolean isRegistered(String runId)
      Returns whether a run currently has state (script and/or counters) tracked.
      Parameters:
      runId - run id; must not be blank
      Returns:
      true if the run is tracked