Applies a CalculateRunTokenStats result to a run entity's six denormalized token/cost columns.
Calculates cumulative token and cost statistics from an agent run's steps.
Sums Prompt, Compaction, and Decision steps from their attached PromptRun
rollup columns and Sub-Agent steps from their SubAgentRun totals.
Evaluates per-run execution guardrails (absoluteMaxIterations, MaxCostPerRun,
MaxTokensPerRun, MaxIterationsPerRun, MaxTimePerRun, validation retries)
followed by the shared cumulative root-tree budget.
Evaluates whether any cumulative tree budget ceiling (maxTreeSteps, maxTreeTokens,
maxTreeCost, maxTreeDurationMs) on the shared AgentTreeBudget has been reached.
Stamps budget.breachedReason on breach so ancestor and sibling agents in the call tree
can terminate immediately.
Ensures that a shared AgentTreeBudget instance exists for the current execution tree
and is attached to params.treeBudget and fallbackParams.treeBudget.
params.treeBudget is already provided (for example, propagated by a parent agent
in ExecuteSubAgent or supplied directly by a caller), that exact object reference
is reused so all root, sequential child, and parallel child runs mutate the same shared
counters (totalSteps, totalTokens, totalCost, startedAtMs, breachedReason).params.treeBudget is absent (root run), a new AgentTreeBudget is created
and seeded with any top-level convenience ceilings (maxTreeSteps, maxTreeTokens,
maxTreeCost, maxTreeDurationMs) declared on params.Optionalparams: ExecuteAgentParamsOptionalcurrentBudget: AgentTreeBudgetOptionalfallbackParams: ExecuteAgentParamsConverts a legacy camelCase LegacyAgentGuardrailVerdict returned by a
BaseAgent.checkTreeBudgetLimit override into an AgentGuardrailCheckResult.
Returns the current execution count for an action or sub-agent ID (0 if never executed).
Increments the execution counter for an action or sub-agent ID within the run's map.
Accumulates token and cost usage from a completed prompt execution into the shared AgentTreeBudget for the execution tree.
Prefers rollup figures on promptRun (TokensUsedRollup / TokensPromptRollup +
TokensCompletionRollup and TotalCost) so hierarchical child prompts are counted,
falling back to direct promptRun columns (TokensUsed / TokensPrompt + TokensCompletion
and Cost) or the AIPromptRunResult summary fields when promptRun is not populated.
OptionalpromptRun: MJAIPromptRunEntityOptionalpromptResult: AIPromptRunResult<unknown>Synchronizes the shared tree budget floor with the current agentRun's metrics so
direct guardrail calls with pre-populated agentRun entities are reflected in the tree budget.
OptionalagentRun: MJAIAgentRunEntityExtendedConverts an AgentGuardrailCheckResult into the legacy camelCase shape
returned by BaseAgent's protected guardrail hooks.
Tracks hierarchical token, cost, wall-clock duration, and step/attempt budgets across parent -> sub-agent execution trees.