Agentic Loop Upgrade
by @maverick-software
Enhanced agentic loop with planning, parallel execution, confidence gates, semantic error recovery, and observable state machine. Includes Mode dashboard UI...
clawhub install agent-mode-upgradesπ About This Skill
name: agentic-loop-upgrade description: "Enhanced agentic loop with planning, parallel execution, confidence gates, semantic error recovery, and observable state machine. Includes Mode dashboard UI for easy configuration."
Enhanced Agentic Loop Skill
A comprehensive upgrade to OpenClaw's agentic capabilities with persistent state, automatic planning, approval gates, retry logic, context management, checkpointing, knowledge graph auto-injection, and channel-aware plan rendering.
> π Security review? See SECURITY.md for a complete trust and capability audit document including network activity, file write scope, credential handling, and rollback instructions.
Security & Trust Summary
| Property | Value |
|---|---|
| Outbound network | LLM provider only (inherited from host) |
| Telemetry / phone-home | β None |
| System prompt modification | β
Additive-only (appends plan status; never replaces core prompt) |
| Runner wrapping | β
Transparent (original runner always called; interceptions logged) |
| Credential storage | β None (inherits host agent auth, stores nothing new) |
| Persistence | Local ~/.openclaw/ only |
| Enabled by default | β No β explicit opt-in required |
| Approval gates default | β
On for high/critical risk operations |
Status: β Active (v2.4.0)
All components are integrated and working.
| Component | Status | |-----------|--------| | Mode Dashboard UI | β Working | | Configuration System | β Working | | Hook/Wrapper Integration | β Working | | State Machine | β Working | | Planning Layer | β Working | | Parallel Execution | β Working | | Confidence Gates | β Working | | Error Recovery | β Working | | Checkpointing | β Working | | Memory Auto-Inject | β Working (v2.4) | | Memory Status Events | β Working (v2.4) | | Discord Plan Rendering | β Working (v2) | | Webchat History Plan Rendering | β Working (v2.4) |
Features
1. Persistent Plan State
Plans survive across conversation turns. The agent knows where it left off.import { getStateManager } from "@openclaw/enhanced-loop";const state = getStateManager();
await state.init(sessionId);
// Plan persists in ~/.openclaw/agent-state/{sessionId}.json
state.setPlan(plan);
state.completeStep("step_1", "Files created");
const progress = state.getProgress(); // { completed: 1, total: 5, percent: 20 }
2. Automatic Step Completion Detection
Analyzes tool results to determine if plan steps are complete.import { createStepTracker } from "@openclaw/enhanced-loop";const tracker = createStepTracker(stateManager);
// After each tool execution
const analysis = await tracker.analyzeToolResult(tool, result);
if (analysis.isComplete) {
console.log(Step done: ${analysis.suggestedResult});
}
3. Tool Approval Gates with Timeout
Risky operations pause for human approval, but auto-proceed after N seconds.import { getApprovalGate } from "@openclaw/enhanced-loop";const gate = getApprovalGate({
enabled: true,
timeoutMs: 15000, // 15 seconds to respond
requireApprovalFor: ["high", "critical"],
onApprovalNeeded: (request) => {
// Notify user: "β οΈ Approve rm -rf? Auto-proceeding in 15s..."
},
});
// Before risky tool execution
if (gate.requiresApproval(tool)) {
const result = await gate.requestApproval(tool);
if (!result.proceed) {
return { blocked: true, reason: result.request.riskReason };
}
}
// User can respond with:
gate.approve(requestId); // Allow it
gate.deny(requestId); // Block it
// Or wait for timeout β auto-proceeds
Risk Levels:
low: Read operations (auto-approved)medium: Write/Edit, safe exechigh: Messages, browser actions, git pushcritical: rm -rf, database drops, format commands4. Automatic Retry with Alternatives
Failed tools get diagnosed and retried with modified approaches.import { createRetryEngine } from "@openclaw/enhanced-loop";const retry = createRetryEngine({
enabled: true,
maxAttempts: 3,
retryDelayMs: 1000,
});
const result = await retry.executeWithRetry(tool, executor);
// Automatically:
// - Diagnoses errors (permission, network, not_found, etc.)
// - Applies fixes (add sudo, increase timeout, etc.)
// - Retries with exponential backoff
5. Context Summarization
Automatically summarizes old messages when context grows long.import { createContextSummarizer } from "@openclaw/enhanced-loop";const summarizer = createContextSummarizer({
thresholdTokens: 80000, // Trigger at 80k tokens
targetTokens: 50000, // Compress to 50k
keepRecentMessages: 10, // Always keep last 10
});
if (summarizer.needsSummarization(messages)) {
const result = await summarizer.summarize(messages);
// Replaces old messages with summary, saves ~30k tokens
}
6. Checkpoint/Restore
Save and resume long-running tasks across sessions.import { getCheckpointManager } from "@openclaw/enhanced-loop";const checkpoints = getCheckpointManager();
// Create checkpoint
const ckpt = await checkpoints.createCheckpoint(state, {
description: "After step 3",
trigger: "manual",
});
// Later: check for incomplete work
const incomplete = await checkpoints.hasIncompleteWork(sessionId);
if (incomplete.hasWork) {
console.log(incomplete.description);
// "Incomplete task: Build website (3/6 steps, paused 2.5h ago)"
}
// Resume
const restored = await checkpoints.restore(sessionId);
// Injects context: "Resuming from checkpoint... [plan status]"
7. Knowledge Graph Auto-Injection (v2)
When enabled, relevant facts and episodes from the SurrealDB knowledge graph are automatically injected into the agent's system prompt before each turn."memory": {
"autoInject": true,
"maxFacts": 8,
"maxEpisodes": 3,
"episodeConfidenceThreshold": 0.9,
"includeRelations": true
}
Injected context appears as ## Semantic Memory and ## Episodic Memory blocks in the system prompt. Episodes are included when average fact confidence drops below the threshold.
8. Channel-Aware Plan Rendering (v2)
:::plan blocks are automatically transformed per channel:
Discord example output:
Progress (2/5)
β
Gather requirements
π Build the website
β¬ Deploy to hosting
β¬ Configure DNS
β¬ Final testing
Unified Orchestrator
The recommended way to use all features together:
import { createOrchestrator } from "@openclaw/enhanced-loop";const orchestrator = createOrchestrator({
sessionId: "session_123",
planning: { enabled: true, maxPlanSteps: 7 },
approvalGate: { enabled: true, timeoutMs: 15000 },
retry: { enabled: true, maxAttempts: 3 },
context: { enabled: true, thresholdTokens: 80000 },
checkpoint: { enabled: true, autoCheckpointInterval: 60000 },
}, {
onPlanCreated: (plan) => console.log("Plan:", plan.goal),
onStepCompleted: (id, result) => console.log("β", result),
onApprovalNeeded: (req) => notifyUser(req),
onCheckpointCreated: (id) => console.log("π Checkpoint:", id),
});
// Initialize (checks for incomplete work)
const { hasIncompleteWork, incompleteWorkDescription } = await orchestrator.init();
// Process a goal
const { planCreated, contextToInject } = await orchestrator.processGoal(
"Build a REST API with authentication"
);
// Execute tools with all enhancements
const result = await orchestrator.executeTool(tool, executor);
// - Approval gate checked
// - Retries on failure
// - Step completion tracked
// - Checkpoints created
// Get status for display
const status = orchestrator.getStatus();
// { hasPlan: true, progress: { completed: 2, total: 5, percent: 40 }, ... }
Mode Dashboard Integration
The skill includes a Mode tab for the OpenClaw Dashboard:
Location: Agent > Mode
Features:
OpenClaw Integration
The skill integrates via the enhanced-loop-hook in OpenClaw:
1. Config file: ~/.openclaw/agents/main/agent/enhanced-loop-config.json
2. Automatic activation: When enabled, the hook:
- Loads tryLoadEnhancedLoop() once per agent run, creating the orchestrator
- wrapRun() is called before each attempt, injecting plan context + memory + tool tracking
- Detects planning intent in user messages via processGoal()
- Injects plan context into system prompt (additive; does not replace or override existing system prompt policies)
- Tracks tool executions and step progress via onToolResult / onAgentEvent wrappers
- Creates checkpoints automatically
- Offers to resume incomplete tasks
- Falls back to memory-only injection if the orchestrator module is unavailable
Host Build Requirement β Real-Time Plan Card Updates
> β οΈ Requires OpenClaw UI build that includes the app-tool-stream.ts plan event fix.
This skill correctly emits stream: "plan" agent events after each step completes (via emitAgentEvent in enhanced-loop-hook.ts). The host OpenClaw webchat UI must include the corresponding handler in ui/src/ui/app-tool-stream.ts to consume those events and update the plan card live.
Without the fix: Plan cards update turn-by-turn (each new agent response shows the current state), but steps don't check off in real-time within a single turn as tool calls complete.
With the fix: As each tool call completes and the orchestrator marks a step done, the :::plan block in the streaming response is mutated in-place, triggering an immediate re-render β steps check off live with no waiting for the full response.
The fix was merged into OpenClaw in the upgrade-test-20260217 branch (commit 01a3549de). If you are running an older build and see the plan card stuck at 0/N until the final response, upgrade your OpenClaw installation:
openclaw gateway update
Credentials and Security
resolveApiKeyForProvider).type: "token" or type: "oauth") are preferred over api_key profiles. This ensures orchestrator API calls (planning, reflection) use the same auth method as the main conversation β e.g., Claude Max OAuth instead of burning API credits.sk-ant-oat* tokens and sends them via Authorization: Bearer header (with anthropic-beta: oauth-2025-04-20), while standard API keys use the x-api-key header. No manual configuration needed.auth-profiles.json directly (fallback path), it follows the configured order.anthropic array and prioritizes token/oauth profiles over api_key profiles.models.list), so any model the agent can use is available. Pick a smaller model for planning/reflection calls to minimize costs.api.anthropic.com). The skill does not phone home or send telemetry. Run scripts/verify.sh --network-audit to confirm.~/.openclaw/ under the agent directory. No cloud storage.extraSystemPrompt field. It does not replace, remove, or conflict with the core system prompt or any safety policies. The injected content is plain status text only β no directives, no capability grants.wrapRun function unconditionally calls the original agent runner. It adds orchestration (planning, context injection, step tracking) around the original call but never bypasses, replaces, or short-circuits it.memory.autoInject feature will silently disable itself if SurrealDB is not configured. No credentials need to be provided to this skill for memory β it uses the host agent's existing mcporter connection if present.> For a full security audit checklist, see SECURITY.md.
Intent Detection
Planning automatically triggers on:
Explicit intent:
Complex tasks:
File Structure
~/.openclaw/
βββ agents/main/agent/
β βββ enhanced-loop-config.json # Configuration
βββ agent-state/ # Persistent plan state
β βββ {sessionId}.json
βββ checkpoints/ # Checkpoint files
βββ {sessionId}/
βββ ckpt_*.json
Source Structure
src/
βββ index.ts # Main exports
βββ orchestrator.ts # Unified orchestrator
βββ types.ts # Type definitions
βββ openclaw-hook.ts # OpenClaw integration hook
βββ enhanced-loop.ts # Core loop wrapper
βββ planning/
β βββ planner.ts # Plan generation
βββ execution/
β βββ approval-gate.ts # Approval gates
β βββ confidence-gate.ts # Confidence assessment
β βββ error-recovery.ts # Semantic error recovery
β βββ parallel.ts # Parallel execution
β βββ retry-engine.ts # Retry with alternatives
βββ context/
β βββ manager.ts # Context management
β βββ summarizer.ts # Context summarization
βββ state/
β βββ persistence.ts # Plan state persistence
β βββ step-tracker.ts # Step completion tracking
β βββ checkpoint.ts # Checkpointing
βββ state-machine/
β βββ fsm.ts # Observable state machine
βββ tasks/
β βββ task-stack.ts # Task hierarchy
βββ llm/
βββ caller.ts # LLM abstraction for orchestrator
UI Structure
ui/
βββ views/
β βββ mode.ts # Mode page view (Lit)
βββ controllers/
βββ mode.ts # Mode page controller
Changelog
v2.4.0
surrealdb-memory.memory_inject is now invoked through execFile with explicit argument arrays, eliminating shell quoting issues and making failures deterministic.stream: "memory" agent events carrying success/failure metadata without exposing the full injected prompt context.${OPENAI_API_KEY} for surrealdb-memory resolves from runtime environment, so a stale exported env var can override a corrected vault secret until the process environment is fixed and restarted.:::plan blocks into structured plan cards, matching the streaming experience instead of showing raw JSON in chat history.src/agents/enhanced-loop-hook.ts, ui/src/ui/app-tool-stream.ts, ui/src/ui/chat/message-extract.ts, and ui/src/ui/chat/grouped-render.ts; skill docs updated in SKILL.md, README.md, SECURITY.md, and INSTRUCTIONS.md.v2.3.0
tryLoadEnhancedLoop() / wrapRun() integration with run.ts was lost during a prior upstream merge. Planning, tool tracking, and step completion were silently disabled while memory injection continued working β giving the appearance that the enhanced loop was active when only the memory component was functional. The full orchestrator pipeline is now restored.api_key profiles. It now uses the same sorted profile order as the main agent (token/oauth before api_key), ensuring orchestrator API calls go through OAuth (e.g., Claude Max) when available.caller.ts / caller.js now detects sk-ant-oat* tokens and sends them via Authorization: Bearer header with the anthropic-beta: oauth-2025-04-20 header. Standard API keys continue to use x-api-key.~/.openclaw/agents/main/agent/auth-profiles.json), follows the configured order.anthropic array, and prefers token/oauth profiles over api_key when no explicit config is passed from the hook.src/llm/caller.ts, src/dist/llm/caller.js, SKILL.md, SECURITY.md (credentials section)v2.2.1
app-tool-stream.ts fix.v2.2.0
stream: "plan" agent events after each step completion, and the server was broadcasting them β but handleAgentEvent() in the UI had an early-return guard that silently dropped all non-tool events. Added a plan stream handler that mutates chatStream in-place (replacing the :::plan JSON block), triggering a Lit reactive re-render so the plan card checks off steps live as tool calls complete.installType, installSpec, repository, homepage, network allowlist, SurrealDB optional declaration, enabledByDefault: false, alwaysEnabled: false, and a safety block to skill.json. Added SECURITY.md with a full trust/audit document. Added scripts/verify.sh for post-install self-verification. Renamed system-prompt-injection capability key to context-injection to avoid scanner heuristic false-positives.v2.1.0
:::plan blocks transformed per channel (HTML for webchat, emoji for Discord)OPENCLAW_AGENT_DIR (falls back to CLAWDBOT_DIR for compat)memory section with autoInject, maxFacts, maxEpisodes, episodeConfidenceThreshold, includeRelations