Runtime Lifecycle
This page explains the full flow from runTask() to final output.
1. High-level flow
mermaid
flowchart TD
A[runTask capability+input] --> B[Guardrails.checkInput]
B --> C[createHarnessContext]
C --> D[runLoop]
D --> E[loadContext recent+semantic]
E --> F[LLM Call]
F --> G{tool calls?}
G -- yes --> H[ToolRegistry.invoke]
H --> F
G -- no --> I[Guardrails.checkOutput]
I --> J[MemoryManager.save]
J --> K[TraceSession.complete]
K --> L[return output]2. Key stages
2.1 Input guardrails
ColonyHarness.runTask() checks input first via guardrails.checkInput(...).
2.2 Context assembly
createHarnessContext() wires model, loop, memory, and trace APIs.
2.3 Loop execution
runLoop(prompt) does:
- load recent + semantic memory
- call model with tool schemas
- invoke tools and inject tool results into messages
- stop by conditions and return
LoopResult
2.4 Output checks and persistence
After task handler returns:
guardrails.checkOutput- persist episodic output memory
- finalize trace and metrics
3. Useful metrics
loopIterationstoolCallCounttoolErrorsinputTokensoutputTokens
4. Common tuning points
- Loop runs too long: lower
maxIterations, or add stop conditions. - Token usage is high: lower
workingMemoryTokenLimit. - Model calls are flaky: set
modelFailStrategytoretry, then tunemodelRetryMax/modelRetryBaseDelayMs. - Upstream is consistently unhealthy: enable
modelCircuitBreakerEnabledand tunemodelCircuitBreakerFailureThreshold/modelCircuitBreakerCooldownMs. - Tools fail often: set
toolFailStrategytoretry, then tunetoolRetryMax.