Skip to main content

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:
  1. First install breaks silently — no auto-migrations, no setup redirect, no provider validation
  2. Engine lacks resilience patterns — no circuit breakers, no context compaction, no deferred tool loading
  3. No hook system — users can’t customize behavior without modifying core code
  4. Tool results can blow up context — no size limits, no disk spillover
  5. Streaming is rigidgenerateText() return value, not consumable async generator
Users who 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, call await 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 setup or set ANTHROPIC_API_KEY / OPENAI_API_KEY”
    • If PROFCLAW_STRICT_MODE=true, exit(1)
    • Otherwise, continue but set server.degradedMode = true
  • 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 /setup instead 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: ephemeral header 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: 60000 but it’s not used. Wrap each step’s tool execution in Promise.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 existing run():
  • Key: run() wraps stream(), 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 with hooks.run('beforeToolCall') / hooks.run('afterToolCall')
  • src/server.ts — load hooks from profclaw.hooks.yml or hooks/ 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, run compactor.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): Batch 2 — Sequential (after batch 1): Batch 3 — Final verification:

Testing Requirements

Every workstream produces tests. Minimum coverage: Total: ~44 new tests minimum

Success Criteria

After all workstreams complete:
  1. npm i -g profclaw && profclaw serve on a fresh machine → setup wizard appears (not blank screen)
  2. No AI keys configured → clear error message with instructions (not silent failure)
  3. Tool fails 3 times → circuit breaker opens, agent tries alternative approach
  4. 200KB tool result → stored on disk, summary in context
  5. 50+ turn conversation → context auto-compacts, no token overflow crash
  6. Agent executor yields typed events → CLI/SSE/SDK can all consume same stream
  7. Users can add hooks via profclaw.hooks.yml without modifying core code
  8. Agent only sees ~15 tools initially, discovers more via search tool
  9. All 95 existing tests still pass
  10. 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)