profClaw Engine Hardening & Enhancement Spec
Make profClaw launch-ready: reliable first-install, robust engine, Claude Code-inspired patterns.Date: 2026-04-02 Status: Draft Priority focus: CLI experience + Agent execution reliability
Problem Statement
profClaw has a powerful engine (882-line executor, 72 tools, 35 providers, 22 chat channels) but isn’t launch-ready because:- First install breaks silently — no auto-migrations, no setup redirect, no provider validation
- Engine lacks resilience patterns — no circuit breakers, no context compaction, no deferred tool loading
- No hook system — users can’t customize behavior without modifying core code
- Tool results can blow up context — no size limits, no disk spillover
- Streaming is rigid —
generateText()return value, not consumable async generator
npm i -g profclaw && profclaw serve must get a working experience in 60 seconds or they uninstall.
Workstreams
WS-1: First-Install Reliability (CRITICAL — blocks launch)
These are the “user runs profclaw for the first time” fixes. All are independent and can be parallelized.1.1 Auto-Migration on Startup
- File:
src/storage/index.ts - Change: After
initStorage()connects, callawait storage.runMigrations() - Fallback: If migration fails, log error with exact SQL that failed, suggest
profclaw db:migrate --verbose - Test:
src/storage/tests/auto-migrate.test.ts— test fresh DB gets schema, test already-migrated DB is no-op
1.2 Provider Validation on Startup
- File:
src/server.ts(after agent init block ~line 768) - Change: After agent registry populates, check
registry.getActiveAdapters().length > 0. If zero:- Log: “No AI providers configured. Run
profclaw setupor set ANTHROPIC_API_KEY / OPENAI_API_KEY” - If
PROFCLAW_STRICT_MODE=true, exit(1) - Otherwise, continue but set
server.degradedMode = true
- Log: “No AI providers configured. Run
- Test:
src/server/tests/startup-validation.test.ts
1.3 First-Run Setup Redirect
- File:
src/server.ts(GET/route) - Change: If admin user count === 0, redirect to
/setupinstead of serving blank dashboard - File:
src/cli/commands/serve.ts - Change: On first boot, print banner: “First run detected. Visit http://localhost:9100/setup to configure profClaw”
- Test:
src/e2e/first-run.test.ts
1.4 In-Memory Storage Warning
- File:
src/server.ts - Change: When storage falls back to in-memory:
- Log warning banner every 60s (not 30s — less noisy)
- Add
X-ProfClaw-Storage: ephemeralheader via Hono middleware - Show yellow banner in UI dashboard
- Test: Extend existing storage tests
1.5 Port Conflict Handling
- File:
src/cli/commands/serve.ts - Change: Replace check-then-bind with bind-or-retry:
- Try configured port
- If EADDRINUSE, try port+1, port+2 (max 3 attempts)
- Log which port was actually used
- Test:
src/cli/tests/port-conflict.test.ts
WS-2: Engine Resilience (HIGH — prevents runtime failures)
2.1 Circuit Breaker for Tools
- File:
src/agents/executor.ts - New file:
src/agents/circuit-breaker.ts - Design:
- Integration: In executor’s tool execution block (~line 494), check
circuitBreaker.canExecute(toolName)before calling tool. On failure,recordFailure(). On success,recordSuccess(). - Thresholds: 3 failures in 2 minutes → open. Half-open after cooldown. 1 success in half-open → close.
- Test:
src/agents/tests/circuit-breaker.test.ts
2.2 Per-Step Timeout Enforcement
- File:
src/agents/executor.ts - Change: The config already has
stepTimeoutMs: 60000but it’s not used. Wrap each step’s tool execution inPromise.race([toolExec, timeout(config.stepTimeoutMs)]). - Test: Extend
executor.test.ts
2.3 Tool Result Size Management
- File:
src/agents/executor.ts - New file:
src/agents/result-store.ts - Design:
- Integration: After tool returns result, pass through
resultStore.store(). Agent sees summary; tools can access full data. - Test:
src/agents/tests/result-store.test.ts
WS-3: Async Generator Streaming (HIGH — architectural upgrade)
This is the biggest change. Refactors the executor to yield events as they happen instead of returning a final result.3.1 Event Types
- New file:
src/agents/events.ts
3.2 Generator Executor
- File:
src/agents/executor.ts - Change: Add
async *stream()method alongside existingrun(): - Key:
run()wrapsstream(), so all existing callers work unchanged. New callers (CLI TUI, SSE, SDK) consume the generator directly. - Test:
src/agents/tests/streaming.test.ts
3.3 SSE Integration
- File:
src/server.ts(SSE endpoint) - Change: Replace manual event broadcasting with direct generator consumption:
WS-4: Hook System (MEDIUM-HIGH — extensibility)
4.1 Hook Registry
- New file:
src/hooks/registry.ts
4.2 Built-in Hooks
- Cost warning hook: Fires at 50%, 80%, 100% budget usage
- Dangerous tool hook: Prompts for confirmation on file writes, bash commands
- Logging hook: Records all tool calls to audit log
4.3 Integration Points
src/agents/executor.ts— wrap tool calls withhooks.run('beforeToolCall')/hooks.run('afterToolCall')src/server.ts— load hooks fromprofclaw.hooks.ymlorhooks/directory on startup- Test:
src/hooks/tests/registry.test.ts
WS-5: Deferred Tool Loading (MEDIUM — performance + context savings)
5.1 Tool Categories
- File:
src/chat/execution/tools/(existing tool definitions) - New file:
src/agents/tool-loader.ts - Design: Categorize 72 tools into groups:
5.2 Tool Search Tool
- New file:
src/chat/execution/tools/tool-search.ts - Test:
src/agents/tests/tool-loader.test.ts
WS-6: Context Compaction (MEDIUM — prevents context overflow)
6.1 History Compactor
- New file:
src/agents/context-compactor.ts
6.2 Integration
- File:
src/agents/executor.ts— before each API call, runcompactor.compact(messages) - File:
src/chat/agentic-executor.ts— same integration for chat executor - Test:
src/agents/tests/context-compactor.test.ts
Parallelization Strategy
These workstreams have minimal dependencies:Agent Dispatch Plan
Batch 1 — All parallel (no dependencies):| Agent | Workstream | Files touched |
|---|---|---|
| Agent A | WS-1.1 Auto-migration | src/storage/index.ts + test |
| Agent B | WS-1.2 + 1.3 + 1.4 Provider validation + first-run + memory warning | src/server.ts + tests |
| Agent C | WS-1.5 Port conflict | src/cli/commands/serve.ts + test |
| Agent D | WS-2.1 Circuit breaker | New src/agents/circuit-breaker.ts + test |
| Agent E | WS-2.2 Step timeout | src/agents/executor.ts (small change) |
| Agent F | WS-2.3 Result store | New src/agents/result-store.ts + test |
| Agent G | WS-4.1 + 4.2 Hook registry + built-ins | New src/hooks/ + tests |
| Agent H | WS-5.1 Tool categorization + 5.2 search tool | New src/agents/tool-loader.ts + tool |
| Agent I | WS-6.1 Context compactor | New src/agents/context-compactor.ts + test |
| Agent | Workstream | Depends on |
|---|---|---|
| Agent J | WS-3.1 + 3.2 Event types + Generator refactor | Batch 1 (D, E, F) |
| Agent K | WS-3.3 SSE integration | Agent J |
| Agent L | WS-4.3 Hook integration into executor | Agent J + G |
| Agent M | WS-6.2 Compactor integration | Agent J + I |
| Agent | Task |
|---|---|
| Agent N | Run full test suite, fix any integration issues |
| Agent O | First-install smoke test (fresh DB, no config, verify setup flow) |
Testing Requirements
Every workstream produces tests. Minimum coverage:| Workstream | Test type | Min test count |
|---|---|---|
| WS-1.x | Integration | 8 tests (2 per sub-item) |
| WS-2.1 | Unit | 6 tests (closed/open/half-open states, reset, concurrent) |
| WS-2.2 | Unit | 3 tests (timeout fires, timeout doesn’t fire, cleanup) |
| WS-2.3 | Unit | 5 tests (inline, spillover, retrieve, cleanup, size edge cases) |
| WS-3.x | Unit + Integration | 8 tests (event ordering, backpressure, error propagation, SSE) |
| WS-4.x | Unit | 6 tests (register, priority, abort, modify, lifecycle) |
| WS-5.x | Unit | 4 tests (categorize, search, dynamic add, always-loaded) |
| WS-6.x | Unit | 4 tests (below threshold no-op, compaction, preserve recent, token estimate) |
Success Criteria
After all workstreams complete:npm i -g profclaw && profclaw serveon a fresh machine → setup wizard appears (not blank screen)- No AI keys configured → clear error message with instructions (not silent failure)
- Tool fails 3 times → circuit breaker opens, agent tries alternative approach
- 200KB tool result → stored on disk, summary in context
- 50+ turn conversation → context auto-compacts, no token overflow crash
- Agent executor yields typed events → CLI/SSE/SDK can all consume same stream
- Users can add hooks via
profclaw.hooks.ymlwithout modifying core code - Agent only sees ~15 tools initially, discovers more via search tool
- All 95 existing tests still pass
- 44+ new tests added and passing
Non-Goals
- UI redesign (separate effort)
- New chat channel integrations
- New tool implementations
- Provider SDK upgrades
- Performance benchmarking (do after launch)