HARNESS · AGENT LOOP
packages/agent-core · implementation flow
task-scoped
streaming events
max 30 turns
RUN START
Create task scope and runtime state
system prompt · transcript · ToolRegistry + shared result cache · reset task status
INITIALIZE
beforeAgentStart + typed extensions
augment messages/system prompt · inject autonomous acceptance contract · emit agent_start
QUEUE INTAKE
Drain pending messages at the turn boundary
steering first · follow-ups when idle · append as user messages · emit message events
TURN N
Transform active context
config → internal hook → user hook · replace messages when a transform returns
PROVIDER PIPELINE
beforeProviderRequest
headers · timeout · retries · metadata
beforeProviderPayload
transport-ready payload transform
backend.generate()
thinking · text · tool-call SSE deltas
Normalize response
text-tool fallback · sanitize call arguments · afterProviderResponse
output guard: valid content or tool calls?
tool calls
TOOL BATCH · RESULTS PRESERVE MODEL ORDER
Registry prepare
resolve + validate args
Permission
evaluate per call
beforeToolCall
rewrite · block · snapshot
Execute
parallel stages + barriers
afterToolCall
rewrite · error · terminate
stream updates · emit execution events · create paired tool-result messages
TURN END
Account, adapt, and prepare the next turn
loop diagnostics · token/context update · emergency compaction at budget exhaustion
emit turn_end · refresh mutable config + registry · prepareNextTurn / proactive compaction
Continue?
tools · steering · follow-up · stop policy · acceptance · reflection
continue → pending messages
finish
STRUCTURED RUN OUTCOME
completed
needs_input
blocked
failed
cancelled
run_outcome → typed agent_end → agent_end
Two delivery queues
steering: afterTurn()
interrupts the next model boundary
follow-up: onIdle()
reopens the outer loop after a stop
turn_end also creates the session save point
Hook surfaces
beforeAgentStart
transformContext · prepareNextTurn
beforeProviderRequest
beforeProviderPayload
afterProviderResponse
beforeToolCall · afterToolCall
shouldStopAfterTurn
getSteeringMessages · getFollowUpMessages
internal safeguards run before user hooks;
typed extension events mirror lifecycle events
Output guard + recovery
retryable provider error
→ bounded delay → retry
context_full
→ compactToFit(75%) → retry
empty / degenerate response
→ fail closed
turn-loop diagnostic
→ inject course-correction nudge
abort signal can cancel every wait and tool
Autonomous completion policies
structured task_status termination
queued follow-ups beat termination
continuation nudge (bounded ×3)
needs-input detection
custom stop policies
acceptance verification + correction
optional reflection + corrective turn
max-iteration safety failure
minimal profile bypasses embedded policies
no tool calls
core turn stage
queue / decision
tool batch
completion path
aligned to agent-loop-runner.ts · tool-batch-controller.ts · harness-queue-hooks.ts · builtin-hooks.ts
github.com/lseman/logician