switchroom 0.12.27 → 0.12.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/cli/switchroom.js +4 -2
  2. package/package.json +2 -1
  3. package/telegram-plugin/dist/gateway/gateway.js +49 -5
  4. package/telegram-plugin/gateway/gateway.ts +5 -0
  5. package/telegram-plugin/stderr-timestamps.ts +106 -0
  6. package/telegram-plugin/tests/stderr-timestamps.test.ts +113 -0
  7. package/vendor/hindsight-memory/.claude-plugin/plugin.json +8 -0
  8. package/vendor/hindsight-memory/CHANGELOG.md +32 -0
  9. package/vendor/hindsight-memory/LICENSE +21 -0
  10. package/vendor/hindsight-memory/README.md +329 -0
  11. package/vendor/hindsight-memory/hooks/hooks.json +49 -0
  12. package/vendor/hindsight-memory/scripts/drain_pending.py +190 -0
  13. package/vendor/hindsight-memory/scripts/lib/__init__.py +0 -0
  14. package/vendor/hindsight-memory/scripts/lib/bank.py +122 -0
  15. package/vendor/hindsight-memory/scripts/lib/client.py +204 -0
  16. package/vendor/hindsight-memory/scripts/lib/config.py +180 -0
  17. package/vendor/hindsight-memory/scripts/lib/content.py +493 -0
  18. package/vendor/hindsight-memory/scripts/lib/daemon.py +334 -0
  19. package/vendor/hindsight-memory/scripts/lib/directives.py +119 -0
  20. package/vendor/hindsight-memory/scripts/lib/gateway_ipc.py +126 -0
  21. package/vendor/hindsight-memory/scripts/lib/llm.py +146 -0
  22. package/vendor/hindsight-memory/scripts/lib/pending.py +218 -0
  23. package/vendor/hindsight-memory/scripts/lib/state.py +196 -0
  24. package/vendor/hindsight-memory/scripts/recall.py +873 -0
  25. package/vendor/hindsight-memory/scripts/retain.py +286 -0
  26. package/vendor/hindsight-memory/scripts/session_end.py +122 -0
  27. package/vendor/hindsight-memory/scripts/session_start.py +76 -0
  28. package/vendor/hindsight-memory/scripts/setup_hooks.py +115 -0
  29. package/vendor/hindsight-memory/scripts/tests/__init__.py +0 -0
  30. package/vendor/hindsight-memory/scripts/tests/test_directives.py +211 -0
  31. package/vendor/hindsight-memory/scripts/tests/test_gateway_ipc.py +205 -0
  32. package/vendor/hindsight-memory/scripts/tests/test_recall_integration.py +621 -0
  33. package/vendor/hindsight-memory/settings.json +37 -0
  34. package/vendor/hindsight-memory/skills/setup.md +24 -0
  35. package/vendor/hindsight-memory/tests/conftest.py +94 -0
  36. package/vendor/hindsight-memory/tests/test_bank.py +142 -0
  37. package/vendor/hindsight-memory/tests/test_client.py +232 -0
  38. package/vendor/hindsight-memory/tests/test_config.py +128 -0
  39. package/vendor/hindsight-memory/tests/test_content.py +471 -0
  40. package/vendor/hindsight-memory/tests/test_drain_pending.py +192 -0
  41. package/vendor/hindsight-memory/tests/test_hooks.py +808 -0
  42. package/vendor/hindsight-memory/tests/test_manifest.py +14 -0
  43. package/vendor/hindsight-memory/tests/test_pending.py +152 -0
  44. package/vendor/hindsight-memory/tests/test_recall_exit_codes.py +325 -0
  45. package/vendor/hindsight-memory/tests/test_session_end_pending.py +205 -0
  46. package/vendor/hindsight-memory/tests/test_state.py +125 -0
@@ -0,0 +1,329 @@
1
+ # Hindsight Memory Plugin for Claude Code
2
+
3
+ Biomimetic long-term memory for [Claude Code](https://docs.anthropic.com/en/docs/claude-code) using [Hindsight](https://vectorize.io/hindsight). Automatically captures conversations and intelligently recalls relevant context — a complete port of [`hindsight-openclaw`](../openclaw/) adapted to Claude Code's hook-based plugin architecture.
4
+
5
+ ## Quick Start
6
+
7
+ ```bash
8
+ # 1. Add the Hindsight marketplace and install the plugin
9
+ claude plugin marketplace add vectorize-io/hindsight
10
+ claude plugin install hindsight-memory
11
+
12
+ # 2. Configure your LLM provider for memory extraction
13
+ # Option A: OpenAI (auto-detected)
14
+ export OPENAI_API_KEY="sk-your-key"
15
+
16
+ # Option B: Anthropic (auto-detected)
17
+ export ANTHROPIC_API_KEY="your-key"
18
+
19
+ # Option C: No API key needed (uses Claude Code's own model — personal/local use only)
20
+ # See: https://vectorize.io/hindsight/developer/models#claude-code-setup-claude-promax
21
+ export HINDSIGHT_LLM_PROVIDER=claude-code
22
+
23
+ # Option D: Connect to an external Hindsight server instead of running locally
24
+ mkdir -p ~/.hindsight
25
+ echo '{"hindsightApiUrl": "https://your-hindsight-server.com"}' > ~/.hindsight/claude-code.json
26
+
27
+ # 3. Start Claude Code — the plugin activates automatically
28
+ claude
29
+ ```
30
+
31
+ That's it! The plugin will automatically start capturing and recalling memories.
32
+
33
+ ## Features
34
+
35
+ - **Auto-recall** — on every user prompt, queries Hindsight for relevant memories and injects them as context (invisible to the chat transcript, visible to Claude)
36
+ - **Auto-retain** — after every response (or every N turns), extracts and retains conversation content to Hindsight for long-term storage
37
+ - **Daemon management** — can auto-start/stop `hindsight-embed` locally or connect to an external Hindsight server
38
+ - **Dynamic bank IDs** — supports per-agent, per-project, or per-session memory isolation
39
+ - **Channel-agnostic** — works with Claude Code Channels (Telegram, Discord, Slack) or interactive sessions
40
+ - **Zero dependencies** — pure Python stdlib, no pip install required
41
+
42
+ ## Architecture
43
+
44
+ The plugin uses all four Claude Code hook events:
45
+
46
+ | Hook | Event | Purpose |
47
+ |------|-------|---------|
48
+ | `session_start.py` | `SessionStart` | Health check — verify Hindsight is reachable |
49
+ | `recall.py` | `UserPromptSubmit` | **Auto-recall** — query memories, inject as `additionalContext` |
50
+ | `retain.py` | `Stop` | **Auto-retain** — extract transcript, POST to Hindsight (async) |
51
+ | `session_end.py` | `SessionEnd` | Cleanup — stop auto-managed daemon if started |
52
+
53
+ ### Library Modules
54
+
55
+ | Module | Purpose |
56
+ |--------|---------|
57
+ | `lib/client.py` | Hindsight REST API client (stdlib `urllib`) |
58
+ | `lib/config.py` | Configuration loader (settings.json + env overrides) |
59
+ | `lib/daemon.py` | `hindsight-embed` daemon lifecycle (start/stop/health) |
60
+ | `lib/bank.py` | Bank ID derivation + mission management |
61
+ | `lib/content.py` | Content processing (transcript parsing, memory formatting, tag stripping) |
62
+ | `lib/state.py` | File-based state persistence with `fcntl` locking |
63
+ | `lib/llm.py` | LLM provider auto-detection for daemon mode |
64
+
65
+ ### How Recall Works
66
+
67
+ 1. User sends a prompt → `UserPromptSubmit` hook fires
68
+ 2. Plugin resolves Hindsight API URL (external, local, or auto-start daemon)
69
+ 3. Derives bank ID (static or dynamic from project context)
70
+ 4. Composes query from current prompt + optional prior turns
71
+ 5. Calls Hindsight recall API
72
+ 6. Formats memories into `<hindsight_memories>` block
73
+ 7. Outputs via `hookSpecificOutput.additionalContext` — Claude sees it, user doesn't
74
+
75
+ ### How Retain Works
76
+
77
+ 1. Claude responds → `Stop` hook fires (async, non-blocking)
78
+ 2. Reads conversation transcript from Claude Code's JSONL file
79
+ 3. Applies chunked retention logic (every N turns with sliding window)
80
+ 4. Strips `<hindsight_memories>` tags to prevent feedback loops
81
+ 5. Extracts text from channel messages (Telegram reply tool calls, etc.)
82
+ 6. POSTs formatted transcript to Hindsight retain API
83
+
84
+ ## Connection Modes
85
+
86
+ The plugin supports three connection modes, matching the Openclaw plugin:
87
+
88
+ ### 1. External API (recommended for production)
89
+
90
+ Connect to a running Hindsight server (cloud or self-hosted). No local LLM needed — the server handles fact extraction.
91
+
92
+ ```json
93
+ {
94
+ "hindsightApiUrl": "https://your-hindsight-server.com",
95
+ "hindsightApiToken": "your-token"
96
+ }
97
+ ```
98
+
99
+ ### 2. Local Daemon (auto-managed)
100
+
101
+ The plugin automatically starts and stops `hindsight-embed` via `uvx`. Requires an LLM provider API key for local fact extraction.
102
+
103
+ ```json
104
+ {
105
+ "hindsightApiUrl": "",
106
+ "apiPort": 9077
107
+ }
108
+ ```
109
+
110
+ Set an LLM provider:
111
+ ```bash
112
+ export OPENAI_API_KEY="sk-your-key" # Auto-detected, uses gpt-4o-mini
113
+ # or
114
+ export ANTHROPIC_API_KEY="your-key" # Auto-detected, uses claude-3-5-haiku
115
+ ```
116
+
117
+ ### 3. Existing Local Server
118
+
119
+ If you already have `hindsight-embed` running, leave `hindsightApiUrl` empty and set `apiPort` to match your server's port. The plugin will detect it automatically.
120
+
121
+ ## Configuration
122
+
123
+ All settings live in `~/.hindsight/claude-code.json`. Every setting can also be overridden via environment variables. The plugin ships with sensible defaults — you only need to configure what you want to change.
124
+
125
+ **Loading order** (later entries win):
126
+ 1. Built-in defaults (hardcoded in the plugin)
127
+ 2. Plugin `settings.json` (ships with the plugin, at `CLAUDE_PLUGIN_ROOT/settings.json`)
128
+ 3. User config (`~/.hindsight/claude-code.json` — recommended for your overrides)
129
+ 4. Environment variables
130
+
131
+ ---
132
+
133
+ ### Connection & Daemon
134
+
135
+ These settings control how the plugin connects to the Hindsight API.
136
+
137
+ | Setting | Env Var | Default | Description |
138
+ |---------|---------|---------|-------------|
139
+ | `hindsightApiUrl` | `HINDSIGHT_API_URL` | `""` (empty) | URL of an external Hindsight API server. When empty, the plugin uses a local daemon instead. |
140
+ | `hindsightApiToken` | `HINDSIGHT_API_TOKEN` | `null` | Authentication token for the external API. Only needed when `hindsightApiUrl` is set. |
141
+ | `apiPort` | `HINDSIGHT_API_PORT` | `9077` | Port used by the local `hindsight-embed` daemon. Change this if you run multiple instances or have a port conflict. |
142
+ | `daemonIdleTimeout` | `HINDSIGHT_DAEMON_IDLE_TIMEOUT` | `0` | Seconds of inactivity before the local daemon shuts itself down. `0` means the daemon stays running until the session ends. |
143
+ | `embedVersion` | `HINDSIGHT_EMBED_VERSION` | `"latest"` | Which version of `hindsight-embed` to install via `uvx`. Pin to a specific version (e.g. `"0.5.2"`) for reproducibility. |
144
+ | `embedPackagePath` | `HINDSIGHT_EMBED_PACKAGE_PATH` | `null` | Local filesystem path to a `hindsight-embed` checkout. When set, the plugin runs from this path instead of installing via `uvx`. Useful for development. |
145
+
146
+ ---
147
+
148
+ ### LLM Provider (local daemon only)
149
+
150
+ These settings configure which LLM the local daemon uses for fact extraction. They are **ignored** when connecting to an external API (the server uses its own LLM configuration).
151
+
152
+ | Setting | Env Var | Default | Description |
153
+ |---------|---------|---------|-------------|
154
+ | `llmProvider` | `HINDSIGHT_LLM_PROVIDER` | auto-detect | Which LLM provider to use. Supported values: `openai`, `anthropic`, `gemini`, `groq`, `ollama`, `openai-codex`, `claude-code`. When omitted, the plugin auto-detects by checking for API key env vars in order: `OPENAI_API_KEY` → `ANTHROPIC_API_KEY` → `GEMINI_API_KEY` → `GROQ_API_KEY`. |
155
+ | `llmModel` | `HINDSIGHT_LLM_MODEL` | provider default | Override the default model for the chosen provider (e.g. `"gpt-4o"`, `"claude-sonnet-4-20250514"`). When omitted, the Hindsight API picks a sensible default for each provider. |
156
+ | `llmApiKeyEnv` | — | provider standard | Name of the environment variable that holds the API key. Normally auto-detected (e.g. `OPENAI_API_KEY` for the `openai` provider). Set this only if your key is in a non-standard env var. |
157
+
158
+ ---
159
+
160
+ ### Memory Bank
161
+
162
+ A **bank** is an isolated memory store — like a separate "brain." These settings control which bank the plugin reads from and writes to.
163
+
164
+ | Setting | Env Var | Default | Description |
165
+ |---------|---------|---------|-------------|
166
+ | `bankId` | `HINDSIGHT_BANK_ID` | `"claude_code"` | The bank ID to use when `dynamicBankId` is `false`. All sessions share this single bank. |
167
+ | `bankMission` | `HINDSIGHT_BANK_MISSION` | generic assistant prompt | A short description of the agent's identity and purpose. Sent to Hindsight when creating or updating the bank, and used during recall to contextualize results. |
168
+ | `retainMission` | — | extraction prompt | Instructions for the fact extraction LLM — tells it *what* to extract from conversations (e.g. "Extract technical decisions and user preferences"). |
169
+ | `dynamicBankId` | `HINDSIGHT_DYNAMIC_BANK_ID` | `false` | When `true`, the plugin derives a unique bank ID from context fields (see `dynamicBankGranularity`), giving each combination its own isolated memory. |
170
+ | `dynamicBankGranularity` | — | `["agent", "project"]` | Which context fields to combine when building a dynamic bank ID. Available fields: `agent` (agent name), `project` (working directory), `session` (session ID), `channel` (channel ID), `user` (user ID). |
171
+ | `bankIdPrefix` | — | `""` | A string prepended to all bank IDs — both static and dynamic. Useful for namespacing (e.g. `"prod"` or `"staging"`). |
172
+ | `agentName` | `HINDSIGHT_AGENT_NAME` | `"claude-code"` | Name used for the `agent` field in dynamic bank ID derivation. |
173
+
174
+ ---
175
+
176
+ ### Auto-Recall
177
+
178
+ Auto-recall runs on every user prompt. It queries Hindsight for relevant memories and injects them into Claude's context as invisible `additionalContext` (the user doesn't see them in the chat transcript).
179
+
180
+ | Setting | Env Var | Default | Description |
181
+ |---------|---------|---------|-------------|
182
+ | `autoRecall` | `HINDSIGHT_AUTO_RECALL` | `true` | Master switch for auto-recall. Set to `false` to disable memory retrieval entirely. |
183
+ | `recallBudget` | `HINDSIGHT_RECALL_BUDGET` | `"mid"` | Controls how hard Hindsight searches for memories. `"low"` = fast, fewer strategies; `"mid"` = balanced; `"high"` = thorough, slower. Affects latency directly. |
184
+ | `recallMaxTokens` | `HINDSIGHT_RECALL_MAX_TOKENS` | `1024` | Maximum number of tokens in the recalled memory block. Lower values reduce context usage but may truncate relevant memories. |
185
+ | `recallTypes` | — | `["world", "experience"]` | Which memory types to retrieve. `"world"` = general facts; `"experience"` = personal experiences; `"observation"` = raw observations. |
186
+ | `recallContextTurns` | `HINDSIGHT_RECALL_CONTEXT_TURNS` | `1` | How many prior conversation turns to include when composing the recall query. `1` = only the latest user message; higher values give more context but may dilute the query. |
187
+ | `recallMaxQueryChars` | `HINDSIGHT_RECALL_MAX_QUERY_CHARS` | `800` | Maximum character length of the query sent to Hindsight. Longer queries are truncated. |
188
+ | `recallRoles` | — | `["user", "assistant"]` | Which message roles to include when building the recall query from prior turns. |
189
+ | `recallPromptPreamble` | — | built-in string | Text placed above the recalled memories in the injected context block. Customize this to change how Claude interprets the memories. |
190
+
191
+ ---
192
+
193
+ ### Auto-Retain
194
+
195
+ Auto-retain runs after Claude responds. It extracts the conversation transcript and sends it to Hindsight for long-term storage and fact extraction.
196
+
197
+ | Setting | Env Var | Default | Description |
198
+ |---------|---------|---------|-------------|
199
+ | `autoRetain` | `HINDSIGHT_AUTO_RETAIN` | `true` | Master switch for auto-retain. Set to `false` to disable memory storage entirely. |
200
+ | `retainMode` | `HINDSIGHT_RETAIN_MODE` | `"full-session"` | Retention strategy. `"full-session"` sends the full conversation transcript (with chunking). |
201
+ | `retainEveryNTurns` | — | `10` | How often to retain. `1` = every turn; `10` = every 10th turn. Higher values reduce API calls but delay memory capture. Values > 1 enable **chunked retention** with a sliding window. |
202
+ | `retainOverlapTurns` | — | `2` | When chunked retention fires, this many extra turns from the previous chunk are included for continuity. Total window size = `retainEveryNTurns + retainOverlapTurns`. |
203
+ | `retainRoles` | — | `["user", "assistant"]` | Which message roles to include in the retained transcript. |
204
+ | `retainToolCalls` | — | `true` | Whether to include tool calls (function invocations and results) in the retained transcript. Captures structured actions like file reads, searches, and code edits. |
205
+ | `retainTags` | — | `["{session_id}"]` | Tags attached to the retained document. Supports template placeholders: `{session_id}`, `{bank_id}`, `{timestamp}`, and `{user_id}` (resolved from `HINDSIGHT_USER_ID` env var; empty string if unset). Tags whose resolved form ends in an empty namespace part (e.g. `"user:"` when `HINDSIGHT_USER_ID` is unset) are dropped from the outgoing request. See [Template variables](#template-variables-for-retaintags-and-retainmetadata) below. |
206
+ | `retainMetadata` | — | `{}` | Arbitrary key-value metadata attached to the retained document. Same template placeholders as `retainTags`. |
207
+ | `retainContext` | — | `"claude-code"` | A label attached to retained memories identifying their source. Useful when multiple integrations write to the same bank. |
208
+
209
+ #### Template variables for `retainTags` and `retainMetadata`
210
+
211
+ | Variable | Source |
212
+ |----------|--------|
213
+ | `{session_id}` | Current Claude Code session ID |
214
+ | `{bank_id}` | Resolved bank ID (per `bankGranularity`) |
215
+ | `{timestamp}` | ISO 8601 UTC at retain time |
216
+ | `{user_id}` | Value of `HINDSIGHT_USER_ID` env var (empty string if unset) |
217
+
218
+ ##### Example: per-user memory scoping
219
+
220
+ ```json
221
+ {
222
+ "retainTags": ["user:{user_id}", "session:{session_id}"]
223
+ }
224
+ ```
225
+
226
+ Set `HINDSIGHT_USER_ID=<opaque-user-id>` in your shell profile (`.zshrc`,
227
+ `.bashrc`, etc.). If the env var is unset, the `user:` tag is dropped from the
228
+ outgoing retain request and the rest of the tags are sent as-is — so the same
229
+ `settings.json` works across machines whether you've set the env var or not.
230
+
231
+ Downstream, `recall` can filter by `tags=["user:alice"]` to isolate memories
232
+ authored by a specific user from a shared bank.
233
+
234
+ ---
235
+
236
+ ### Debug
237
+
238
+ | Setting | Env Var | Default | Description |
239
+ |---------|---------|---------|-------------|
240
+ | `debug` | `HINDSIGHT_DEBUG` | `false` | Enable verbose logging to stderr. All log lines are prefixed with `[Hindsight]`. Useful for diagnosing connection issues, recall/retain behavior, and bank ID derivation. |
241
+
242
+ ## Claude Code Channels
243
+
244
+ With [Claude Code Channels](https://docs.anthropic.com/en/docs/claude-code), Claude Code can operate as a persistent background agent connected to Telegram, Discord, Slack, and other messaging platforms. This plugin gives Channel-based agents the same long-term memory that `hindsight-openclaw` provides for Openclaw agents.
245
+
246
+ For Channel agents, set these environment variables in your Channel configuration:
247
+
248
+ ```bash
249
+ # Per-channel/per-user memory isolation
250
+ export HINDSIGHT_CHANNEL_ID="telegram-group-12345"
251
+ export HINDSIGHT_USER_ID="user-67890"
252
+ ```
253
+
254
+ And enable dynamic bank IDs:
255
+
256
+ ```json
257
+ {
258
+ "dynamicBankId": true,
259
+ "dynamicBankGranularity": ["agent", "channel", "user"]
260
+ }
261
+ ```
262
+
263
+ ## Troubleshooting
264
+
265
+ ### Plugin not activating
266
+
267
+ - Verify installation: check that `.claude-plugin/plugin.json` exists in the installed plugin directory
268
+ - Check Claude Code logs for `[Hindsight]` messages (enable `"debug": true` in `~/.hindsight/claude-code.json`)
269
+
270
+ ### Recall returning no memories
271
+
272
+ - Verify the Hindsight server is reachable: `curl http://localhost:9077/health`
273
+ - Check that the bank has retained content: memories need at least one retain cycle
274
+ - Try increasing `recallBudget` to `"mid"` or `"high"`
275
+
276
+ ### Daemon not starting
277
+
278
+ - Ensure `uvx` is installed: `pip install uv` or `brew install uv`
279
+ - Check that an LLM API key is set (required for local daemon)
280
+ - Review daemon logs: `tail -f ~/.hindsight/profiles/claude-code.log`
281
+ - Try starting manually: `uvx hindsight-embed@latest daemon --profile claude-code start`
282
+
283
+ ### High latency on recall
284
+
285
+ - The recall hook has a 12-second timeout. If Hindsight is slow:
286
+ - Use `recallBudget: "low"` (fewer retrieval strategies)
287
+ - Reduce `recallMaxTokens`
288
+ - Consider using an external API with a faster server
289
+
290
+ ### State file issues
291
+
292
+ - State is stored in `$CLAUDE_PLUGIN_DATA/state/`
293
+ - To reset: delete the `state/` directory
294
+ - Turn counts, bank missions, and daemon state are tracked here
295
+
296
+ ## Development
297
+
298
+ To test local changes to `hindsight-embed`:
299
+
300
+ ```json
301
+ {
302
+ "embedPackagePath": "/path/to/hindsight-embed"
303
+ }
304
+ ```
305
+
306
+ The plugin will use `uv run --directory <path> hindsight-embed` instead of `uvx hindsight-embed@latest`.
307
+
308
+ To view daemon logs:
309
+
310
+ ```bash
311
+ # Check daemon status
312
+ uvx hindsight-embed@latest daemon --profile claude-code status
313
+
314
+ # View logs
315
+ tail -f ~/.hindsight/profiles/claude-code.log
316
+
317
+ # List profiles
318
+ uvx hindsight-embed@latest profile list
319
+ ```
320
+
321
+ ## Links
322
+
323
+ - [Hindsight Documentation](https://vectorize.io/hindsight)
324
+ - [Claude Code Documentation](https://docs.anthropic.com/en/docs/claude-code)
325
+ - [GitHub Repository](https://github.com/vectorize-io/hindsight)
326
+
327
+ ## License
328
+
329
+ MIT
@@ -0,0 +1,49 @@
1
+ {
2
+ "hooks": {
3
+ "SessionStart": [
4
+ {
5
+ "hooks": [
6
+ {
7
+ "type": "command",
8
+ "command": "python3 \"${CLAUDE_PLUGIN_ROOT}/scripts/session_start.py\"",
9
+ "timeout": 5
10
+ }
11
+ ]
12
+ }
13
+ ],
14
+ "UserPromptSubmit": [
15
+ {
16
+ "hooks": [
17
+ {
18
+ "type": "command",
19
+ "command": "python3 \"${CLAUDE_PLUGIN_ROOT}/scripts/recall.py\"",
20
+ "timeout": 12
21
+ }
22
+ ]
23
+ }
24
+ ],
25
+ "Stop": [
26
+ {
27
+ "hooks": [
28
+ {
29
+ "type": "command",
30
+ "command": "python3 \"${CLAUDE_PLUGIN_ROOT}/scripts/retain.py\"",
31
+ "timeout": 15,
32
+ "async": true
33
+ }
34
+ ]
35
+ }
36
+ ],
37
+ "SessionEnd": [
38
+ {
39
+ "hooks": [
40
+ {
41
+ "type": "command",
42
+ "command": "python3 \"${CLAUDE_PLUGIN_ROOT}/scripts/session_end.py\"",
43
+ "timeout": 10
44
+ }
45
+ ]
46
+ }
47
+ ]
48
+ }
49
+ }
@@ -0,0 +1,190 @@
1
+ #!/usr/bin/env python3
2
+ """Drain ``~/.hindsight/pending-retains/``.
3
+
4
+ SessionStart calls into ``drain()`` to retry any retain payloads that
5
+ ``session_end.py`` queued on failure (#1071). Each entry is retried up
6
+ to ``MAX_ATTEMPTS`` (5) times; after that it's renamed to ``.dead`` so
7
+ the queue no longer drains it but the operator can still inspect via
8
+ ``switchroom doctor``.
9
+
10
+ Boundaries
11
+ ----------
12
+ * Per-entry HTTP timeout: ``HINDSIGHT_DRAIN_TIMEOUT`` (default 5s).
13
+ * Stall guard: if ``STALL_THRESHOLD`` (3) consecutive entries fail with
14
+ the same error class, we stop draining for this session — that's a
15
+ systemic outage, not a transient flake, and continuing would only
16
+ burn the SessionStart timeout budget. The remaining entries stay
17
+ queued for the next session.
18
+ * Total wall-clock cap: ``HINDSIGHT_DRAIN_BUDGET_S`` (default 4s) so
19
+ drain never blocks SessionStart longer than the upstream
20
+ hook timeout permits.
21
+
22
+ Standalone usage::
23
+
24
+ python3 drain_pending.py # one-shot drain, prints summary
25
+ """
26
+
27
+ from __future__ import annotations
28
+
29
+ import os
30
+ import sys
31
+ import time
32
+
33
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
34
+
35
+ from lib.client import HindsightClient
36
+ from lib.config import debug_log, load_config
37
+ from lib.pending import (
38
+ MAX_ATTEMPTS,
39
+ delete_entry,
40
+ iter_entries,
41
+ mark_dead,
42
+ update_attempt,
43
+ )
44
+
45
+
46
+ STALL_THRESHOLD = 3
47
+
48
+
49
+ def _per_entry_timeout() -> int:
50
+ raw = os.environ.get("HINDSIGHT_DRAIN_TIMEOUT", "5")
51
+ try:
52
+ v = int(raw)
53
+ return max(1, v)
54
+ except ValueError:
55
+ return 5
56
+
57
+
58
+ def _budget_seconds() -> float:
59
+ raw = os.environ.get("HINDSIGHT_DRAIN_BUDGET_S", "4")
60
+ try:
61
+ v = float(raw)
62
+ return max(0.5, v)
63
+ except ValueError:
64
+ return 4.0
65
+
66
+
67
+ def _retry_one(entry: dict, timeout: int) -> None:
68
+ """POST a single queued retain. Raises on failure."""
69
+ client = HindsightClient(entry["api_url"], entry.get("api_token"))
70
+ client.retain(
71
+ bank_id=entry["bank_id"],
72
+ content=entry["content"],
73
+ document_id=entry.get("document_id", "conversation"),
74
+ context=entry.get("context"),
75
+ metadata=entry.get("metadata") or {},
76
+ tags=entry.get("tags"),
77
+ timeout=timeout,
78
+ )
79
+
80
+
81
+ def drain(config: dict | None = None) -> dict:
82
+ """Walk the pending-retains directory and retry each entry.
83
+
84
+ Returns a summary dict::
85
+
86
+ {"drained": int, # successful retries (entries deleted)
87
+ "retried": int, # failures kept for next session
88
+ "dead": int, # entries promoted to .dead this run
89
+ "stalled": bool, # stall guard tripped
90
+ "budget_exceeded": bool}
91
+ """
92
+ config = config or load_config()
93
+ timeout = _per_entry_timeout()
94
+ budget = _budget_seconds()
95
+ started = time.monotonic()
96
+
97
+ summary = {
98
+ "drained": 0,
99
+ "retried": 0,
100
+ "dead": 0,
101
+ "stalled": False,
102
+ "budget_exceeded": False,
103
+ }
104
+
105
+ entries = iter_entries()
106
+ if not entries:
107
+ debug_log(config, "drain_pending: queue empty")
108
+ return summary
109
+
110
+ debug_log(config, f"drain_pending: {len(entries)} entries to retry")
111
+
112
+ consecutive_failures = 0
113
+ last_error_class: str | None = None
114
+
115
+ for path, entry in entries:
116
+ if time.monotonic() - started > budget:
117
+ summary["budget_exceeded"] = True
118
+ debug_log(config, "drain_pending: total budget exceeded, stopping")
119
+ break
120
+
121
+ try:
122
+ _retry_one(entry, timeout=timeout)
123
+ except Exception as e:
124
+ err_class = type(e).__name__
125
+ if err_class == last_error_class:
126
+ consecutive_failures += 1
127
+ else:
128
+ consecutive_failures = 1
129
+ last_error_class = err_class
130
+
131
+ attempts = int(entry.get("attempt_count", 1))
132
+ if attempts >= MAX_ATTEMPTS:
133
+ marker = mark_dead(path, entry)
134
+ summary["dead"] += 1
135
+ print(
136
+ f"[Hindsight] drain_pending: entry exceeded {MAX_ATTEMPTS} "
137
+ f"attempts, marking dead at {marker} (last error: {err_class}: {e})",
138
+ file=sys.stderr,
139
+ )
140
+ else:
141
+ update_attempt(path, entry, e)
142
+ summary["retried"] += 1
143
+ debug_log(
144
+ config,
145
+ f"drain_pending: retry {attempts}/{MAX_ATTEMPTS} failed for {path} ({err_class}: {e})",
146
+ )
147
+
148
+ if consecutive_failures >= STALL_THRESHOLD:
149
+ summary["stalled"] = True
150
+ print(
151
+ f"[Hindsight] drain_pending: {consecutive_failures} consecutive "
152
+ f"failures with {err_class}, stalling drain. Remaining entries "
153
+ f"stay queued.",
154
+ file=sys.stderr,
155
+ )
156
+ break
157
+ continue
158
+
159
+ # Success — delete the entry.
160
+ delete_entry(path)
161
+ summary["drained"] += 1
162
+ consecutive_failures = 0
163
+ last_error_class = None
164
+
165
+ return summary
166
+
167
+
168
+ def main() -> int:
169
+ config = load_config()
170
+ summary = drain(config)
171
+ if summary["drained"] or summary["retried"] or summary["dead"]:
172
+ print(
173
+ f"[Hindsight] drain_pending: "
174
+ f"drained={summary['drained']} retried={summary['retried']} "
175
+ f"dead={summary['dead']} "
176
+ f"stalled={summary['stalled']} budget_exceeded={summary['budget_exceeded']}",
177
+ file=sys.stderr,
178
+ )
179
+ # Non-zero only when we promoted entries to .dead — that's the
180
+ # operator-visible signal. Plain retry-still-pending isn't an error,
181
+ # the next SessionStart picks them up.
182
+ return 1 if summary["dead"] else 0
183
+
184
+
185
+ if __name__ == "__main__":
186
+ try:
187
+ sys.exit(main())
188
+ except Exception as e:
189
+ print(f"[Hindsight] drain_pending unexpected error: {e}", file=sys.stderr)
190
+ sys.exit(2)