This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
clawprobe is a CLI observability tool for OpenClaw agents. It monitors OpenClaw's files in the background and provides real-time visibility into token usage, API costs, context health, and smart alerts.
# Install dependencies
npm install
# Build (TypeScript compilation)
npm run build
# Development with auto-reload
npm run dev
# Run all tests
npm test
# Run a single test file
npx tsx --test test/cost-and-db.test.ts
# Type check without emitting
npm run typecheck
# Lint
npm run lint-
Daemon (
src/daemon.ts): Background process that watches OpenClaw files usingchokidar- Watches
sessions.json,*.jsonltranscript files, workspace.mdfiles, andopenclaw.json - On file changes, processes data and writes to SQLite (
~/.clawprobe/probe.db) - Runs rules engine periodically to detect issues
- Watches
-
File Watcher (
src/core/watcher.ts): Debounced file change events (300ms), categorizes changes by type -
JSONL Parser (
src/core/jsonl-parser.ts): Parses OpenClaw transcript files to extract:- Per-turn token usage, model info, and compaction events
- Session name (
slug/custom-titlefield) - Per-tool call statistics (
toolStats: name, callCount, errorCount) - Todo list state (
latestTodos) fromTodoWrite/TaskCreate/TaskUpdatetool calls - Sub-agent invocations (
agentStats) fromTasktool calls
-
Session Store (
src/core/session-store.ts): Readssessions.jsonand maps UUID-named.jsonlfiles to human-readable session keys -
Database (
src/core/db.ts): SQLite with tables:session_snapshots: Periodic snapshots of session state from sessions.jsonturn_records: Per-turn cost data (the single source of truth for billing)compact_events: Context compaction events with archived messagesfile_snapshots: Workspace file analysis (truncation detection)suggestions: Rule-based optimization alertstool_stats: Per-session tool usage counts and error ratestodo_snapshots: Latest todo list state per sessionagent_stats: Sub-agent invocation records per session
-
Cost Engine (
src/engines/cost.ts): Calculates API costs with provider-specific pricing including cache discounts. Built-in prices for 30+ models (Anthropic, OpenAI, Google, DeepSeek, Moonshot, etc.) -
Rule Engine (
src/engines/rule-engine.ts): Built-in rules detect:tools-truncation: TOOLS.md truncated by bootstrapMaxCharshigh-compact-freq: Compacting too frequently (< 30 min avg)context-headroom: Context window > 90% fullcost-spike: Today's spend > 2× weekly averagememory-bloat: MEMORY.md too large
Commands are in src/cli/commands/:
status.ts: Current session snapshottop.ts: Live dashboard with auto-refresh (SIGWINCH-aware, shows cache read/write)cost.ts: API cost tracking (day/week/month/all)session.ts: Per-session breakdown with turn timeline, tool usage, todos, sub-agentscontext.ts: Context window composition analysiscompacts.ts: Compaction event history (--savesupports--fileoverride)suggest.ts: Optimization suggestionsschema.ts: JSON output schemas for agent integration
- User config:
~/.clawprobe/config.json(auto-created on first run) - OpenClaw config:
~/.openclaw/openclaw.json(auto-detected, override withOPENCLAW_DIR) - Default agent:
main(configurable)
- Zero side effects: Only reads OpenClaw files, writes only to
~/.clawprobe/ - Per-turn cost recording:
turn_recordstable stores raw token counts with cache breakdown; cost recomputed at query time using live price table - JSON mode: All commands support
--jsonfor agent/tool integration - Daemon logs:
~/.clawprobe/daemon.logfor debugging
Tests use a fixture pattern (test/helpers.ts) that creates isolated temp directories:
createFixture(): Sets up mock OpenClaw directory structurerunCli(): Spawns CLI process withHOMEenv var pointed at temp dircleanupFixture(): Removes temp dir and resets DB handle
- Node.js >= 22 (uses
node:sqlitewhich requires Node 22+) - ES modules only (
"type": "module"in package.json)