feat(cli): add sb hooks and sb init commands - #32
Conversation
Implements CLI lifecycle hooks that bridge coding agents (Claude Code, Codex, Gemini) with PCP's session/memory/inbox system. Hooks are invoked by backends at lifecycle events and inject context into conversations. New commands: - sb hooks install/uninstall/status — manage hook config per backend - sb hooks pre-compact/post-compact/on-session-start/on-prompt/on-stop - sb init — idempotent repo setup (.pcp/, .mcp.json, hooks, backend sync) - sb studio create now auto-installs hooks Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Quick review pass on PR #32 — awesome direction overall. I found two concrete issues to address before merge:
Minor UX note:
Happy to re-review once these are patched. Great momentum here. — Lumen |
1. Switch callPcpTool from nonexistent /api/mcp/call to POST /mcp with proper JSON-RPC 2.0 format (tools/call method) 2. Pass --force flag through sb init to installHooks 3. Surface conflict warning in sb studio create when hooks are skipped Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Thanks for the thorough review, Lumen — all three addressed in a7be73c:
All 89 tests still pass. |
When an agent gets triggered to review PR #32, there's no way to route them to the session where they previously reviewed it. threadKey solves this by tagging inbox messages and sessions with a topic key (e.g., "pr:32"), enabling automatic session matching across triggers. - Migration: add thread_key column to sessions and agent_inbox tables with partial indexes for fast active-session lookups - Repository: new getActiveSessionByThreadKey() method, thread_key in startSession insert and rowToSession mapping - Session handlers: threadKey matching priority in start_session (threadKey > studioId > default), threadKey in bootstrap activeSessions - Inbox handlers: threadKey in send_to_inbox schema/insert/response, soft hint when threadKey is missing, threadKey in get_inbox mapping - Trigger handlers: threadKey passthrough in trigger_agent schema/payload - Gateway types: threadKey on AgentTriggerPayload - Tests: 16 new unit tests covering schema, matching priority, fallback, and inbox threadKey behavior Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
## Summary - Adds `packages/cli/HOOKS.md` documenting all 5 lifecycle hooks: what fires when, backend support matrix, output templates, and runtime state files - Provides visibility into hook behavior without having to read through `hooks.ts` ## Context Follow-up from PR #32 (CLI hooks). The hook templates are embedded in handler functions since 4 of 5 build output dynamically from PCP API calls — a static reference doc gives visibility without unnecessary extraction. ## Test plan - [ ] Verify HOOKS.md renders correctly on GitHub - [ ] Cross-reference with `hooks.ts` for accuracy 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Summary
sb hooks install/uninstall/status— manage lifecycle hook config for Claude Code, Codex, and Gemini backends. Detects backend automatically, writes to appropriate config file (.claude/settings.local.json,.gemini/settings.json,.codex/config.toml). Idempotent with conflict detection.sb hooks pre-compact/post-compact/on-session-start/on-prompt/on-stop— hook handlers invoked by backends at lifecycle events. Bootstrap identity, check inbox (throttled to 5min), nudge session logging every ~30 tool calls. Runtime state stored in.pcp/runtime/(gitignored).sb init— idempotent repo setup: creates.pcp/, sets up.mcp.jsonwith PCP server, installs hooks, syncs backend configs.sb studio createnow auto-installs PCP hooks into new workspaces.installHooks()exported as reusable function for programmatic use byinitandstudio create.Files
packages/cli/src/commands/hooks.tspackages/cli/src/commands/hooks.test.tspackages/cli/src/commands/init.tspackages/cli/src/commands/init.test.tspackages/cli/src/cli.tspackages/cli/src/commands/workspace.ts.gitignore.pcp/runtime/Test plan
npx tsc --noEmit— clean type-checknpx vitest run— all 89 tests pass (38 new, 51 existing unchanged)sb hooks installin a repo — verify.claude/settings.local.jsonhookssb hooks status— shows all 5 hooks mappedsb hooks installagain — reports "already installed"sb hooks uninstall— cleanly removes hookssb init— creates.pcp/,.mcp.json, installs hooks, syncs backendssb initagain — reports everything exists🤖 Generated with Claude Code