Class AdHocAgentState

java.lang.Object
org.cibseven.connect.ai.agent.agentic.AdHocAgentState

public final class AdHocAgentState extends Object
Holds state belonging to a client that drives an ad hoc sub process — an agent's pending activations, its turn count, its conversation — where the scope's own children cannot reach it.

setVariable walks strictly up the parent chain, and the scope execution is an ancestor of every ad hoc child — and in an agentic scope the children are the tools. The execution returned here is their sibling instead, so a child writing the same name creates its own variable at the process instance. Event scope keeps it out of child iteration and completion checks, and has it removed with the scope like any event scope -- except when the scope is kept for compensation (CIB7-2154).

Deliberately separate from the state execution AdHocSubProcessActivityBehavior keeps for its adHocActivated marker: sharing would tie the engine's "has this scope ever started anything" answer to whether an agent happened to store something. The cost is one further execution per instance.

What only a step writes is one map, held by the marker variable itself, so a step writes one variable rather than one per counter (CIB7-2200). It is a TreeMap of JDK types, serialized as Java whatever the engine's default format (see javaSerialized(java.lang.Object)): every engine can read it, Cockpit shows it as a map, and its bytes depend only on what it holds. That matters because the engine serializes an object it read again when the command ends and writes it back if the bytes differ; a hash map can come back with another table size, which would turn a read into a write. For the same reason the map read is never changed in place: updateState(org.cibseven.bpm.engine.impl.pvm.delegate.ActivityExecution, java.lang.String, java.lang.Object) builds a new one.

Within a command the changes collect, and the map is written once, when the command closes, if it then differs from what was stored. A step changes the map several times (the turn, the model call, every tool call, the read position in the results), and each write of a variable is a history row of its own at history level full. The engine tells its listeners that a command closes before it flushes, so the write still goes with the step's transaction; a command that fails drops what collected, as the transaction does.

  • Field Details

    • STATE_MARKER

      public static final String STATE_MARKER
      Marks this execution, and holds the map of what only a step writes.

      A fixed marker is needed because some of the state has no fixed variable name — a conversation is keyed by a memory id — so recognition cannot work by looking for the payload.

      See Also:
    • LAYOUT_KEY

      public static final String LAYOUT_KEY
      The layout of the map, kept in it, so a later version can tell an older one.
      See Also:
    • LAYOUT

      public static final int LAYOUT
      See Also:
    • MAX_TURNS_PROPERTY

      public static final String MAX_TURNS_PROPERTY
      camunda:property on the scope overriding DEFAULT_MAX_TURNS.
      See Also:
    • MAX_MODEL_CALLS_PROPERTY

      public static final String MAX_MODEL_CALLS_PROPERTY
      camunda:property on the scope overriding DEFAULT_MAX_MODEL_CALLS.
      See Also:
    • DEFAULT_MAX_TURNS

      public static final int DEFAULT_MAX_TURNS
      Turns allowed before the agent must stop.

      A loop that invokes a language model per turn has no natural end, so an unbounded one is unbounded spend on a bad prompt. The scope's MAX_TURNS_PROPERTY overrides it.

      It stops the agent, not the work the agent already started. Over the cap no model is asked again, but a scope with a child still running is left standing and ends when that child does — ending it there would cancel a task someone may be halfway through, which is the same reason completeScope refuses and endOfTurn waits.

      See Also:
    • DEFAULT_MAX_MODEL_CALLS

      public static final int DEFAULT_MAX_MODEL_CALLS
      Model calls allowed within one turn — the second bound, and a different one: the turn cap cannot see a model that keeps calling tools and never ends its turn. Each call is a step of its own (CIB7-2148). The same number bounds the tool calls one answer may ask for.
      See Also:
  • Method Details

    • find

      public static PvmExecutionImpl find(ActivityExecution scopeExecution)
      The agent state execution of scopeExecution, or null when nothing has been stored yet. Never creates one, so it is safe on a read path.

      Looks for STATE_MARKER only. It must not fall back to the behaviour's variable names — see the class comment.

    • requestCompletion

      public static void requestCompletion(ActivityExecution scopeExecution)
      Records that the scope should end once nothing is running in it any more.
      See Also:
      • COMPLETION_REQUESTED
    • isCompletionRequested

      public static boolean isCompletionRequested(ActivityExecution scopeExecution)
    • stateValue

      public static Object stateValue(ActivityExecution scopeExecution, String key)
      One entry of the state map, or null. Never creates the state.
    • updateState

      public static void updateState(ActivityExecution scopeExecution, String key, Object value)
      Sets one entry of the state map; null removes it.
      See Also:
    • updateState

      public static void updateState(ActivityExecution scopeExecution, Map<String,?> changes)
      Sets entries of the state map, a null value removing its key, and writes the map once, only if it changed. Lists and maps in a value are stored as an ArrayList and a TreeMap, whatever the caller passed.
    • javaSerialized

      public static ObjectValue javaSerialized(Object value)
      The value an internal variable of the agent holds: Java-serialized, whatever the engine's default serialization format (CIB7-2800). Under JSON, where Spin's map type detector is registered, as a host may register it, Spin names the type of a map after its first value, so a map of mixed values cannot be read back; and a whole number comes back as an Integer. Java serialization keeps the types, and every engine can write it, Spin or not, as it writes toolCall (CIB7-2136).
    • findOrCreate

      public static PvmExecutionImpl findOrCreate(ActivityExecution scopeExecution)
      The agent state execution of scopeExecution, created on first use.

      Created lazily so a scope no agent ever drives carries neither an extra execution nor an extra variable.

    • findAdHocScope

      public static ExecutionEntity findAdHocScope(ExecutionEntity execution)
      Walks up from execution to the enclosing ad hoc sub process scope, or returns null when there is none.

      Identified by the activity's behaviour rather than by element name, the same way TriggerAdHocActivitiesCmd and the migration validator do, so the check cannot drift from what the runtime actually does. Used by callers that only have the execution of an activity inside the scope — a service task, for instance — and need the scope's own execution.

    • turns

      public static int turns(ExecutionEntity scopeExecution)
      Turns taken so far, zero before the first one is counted.
    • beginTurn

      public static int beginTurn(ExecutionEntity scopeExecution, String turnId)
      Counts the turn turnId belongs to, once, and returns the count.

      A turn and its retry are one turn, not two: the id — the turn job's, in practice — is the same for both, so the retry finds it already counted. A null id counts every call as its own turn, which is the safe direction: a cap that bites early is better than one that never bites.

    • recordCall

      public static void recordCall(ExecutionEntity scopeExecution, String tool)
      Records that the turn now running called tool, and the call was carried out.
    • calledThisTurn

      public static List<String> calledThisTurn(ExecutionEntity scopeExecution)
      The tools the turn now running called, oldest first; empty before its first call.
    • modelCalls

      public static int modelCalls(ExecutionEntity scopeExecution)
      Model calls in the turn now running, zero before the first one is counted.
    • countModelCall

      public static int countModelCall(ExecutionEntity scopeExecution, String stepId)
      Counts the model call of the step stepId, once, and returns the count for the turn. A step and its retry are one call against the cap, as a turn and its retry are one turn; a null id counts every call.
    • isTurnInProgress

      public static boolean isTurnInProgress(ExecutionEntity scopeExecution)
      Whether the next step continues a turn the model has not ended yet.
    • continueTurn

      public static void continueTurn(ExecutionEntity scopeExecution)
      The model called tools and answers in a further step of the same turn.
    • endTurn

      public static void endTurn(ExecutionEntity scopeExecution)
      The turn is over: the model answered, or a limit ended it.
    • positiveProperty

      public static int positiveProperty(ExecutionEntity scopeExecution, String property, int fallback)
      A positive-integer camunda:property of the scope, or fallback.

      A value that is not a positive number falls back rather than failing: the value is a modelling mistake, and refusing to run would turn it into an outage, while the fallback still ends the loop.