Hook Setup
Set up ClawVault hooks for automatic OpenClaw integration
Hook Setup
The ClawVault OpenClaw hook provides automatic context death resilience, session continuity, and memory injection. Once installed, it runs transparently on three gateway lifecycle events without requiring manual intervention.
Prerequisites
Before installing the hook, ensure:
- ClawVault is installed and available on your
PATH:
clawvault --version- A vault is initialized in your workspace:
clawvault statusIf you do not have a vault yet, initialize one:
clawvault init ~/my-vault- qmd is installed (required for search and context features):
bun install -g github:tobi/qmd- OpenClaw is running with hook support enabled.
Installation
Install ClawVault as an OpenClaw skill, which registers the hook automatically:
clawhub install clawvaultTo verify the hook is registered:
openclaw hooks listYou should see clawvault in the output.
Manual Installation
If clawhub is not available, copy the hook handler directly into your OpenClaw hooks directory:
cp -r /path/to/clawvault/hooks/clawvault ~/.openclaw/hooks/clawvaultThe hook directory must contain a handler.js file that exports a default async function.
Hook Events
The hook listens on two active lifecycle events, with forward-compatible handlers for future events. Event names are normalized and matched flexibly -- separators like ., :, and / are treated interchangeably.
gateway:startup
Fires when the OpenClaw gateway starts. The hook:
- Locates the nearest vault (via
CLAWVAULT_PATHor by walking up from the current directory) - Runs
clawvault recover --clearto check for context death - If a death is detected, injects a system message into the event with recovery details
- Suggests running
clawvault wakefor full recovery context
This ensures the agent is immediately aware if its previous session died unexpectedly.
command:new
Fires when a user issues the /new command to reset the session. The hook:
- Creates an automatic checkpoint via
clawvault checkpoint - Records the session key and command source in the checkpoint metadata
- Preserves the agent's working state before the context is cleared
This prevents information loss on manual session resets.
session:start (forward-compatible)
session:start is not yet implemented in OpenClaw. The handler is included as forward-compatible dead code — it will activate automatically when OpenClaw adds this event.
When available, this event will fire at the beginning of a new session. The hook will:
- Extract the session key from the event context
- Fetch a session recap using
clawvault session-recap(recent conversation turns) - Extract the initial user prompt and search the vault for relevant memories using
clawvault context - Inject both the recap and relevant memories as a system message
The hook caps context injection at 4 memory results and 6 recap messages to avoid consuming excessive tokens.
Configuration
The hook uses environment variables and vault auto-discovery. No separate configuration file is needed.
Vault Discovery
The hook locates the vault in this order:
CLAWVAULT_PATHenvironment variable (if set)- Walk up from
process.cwd()looking for a.clawvault.jsonfile - Check
memory/subdirectory at each level (OpenClaw convention)
If no vault is found, the hook silently skips its work and logs a message.
Environment Variables
| Variable | Purpose |
|---|---|
CLAWVAULT_PATH | Explicit vault path. Skips auto-discovery. |
OPENCLAW_SESSION_KEY | Session identifier, used by checkpoint for metadata. |
OPENCLAW_AGENT_ID | Agent identifier, used by session-recap to scope lookups. |
OPENCLAW_MODEL | Current model name, recorded in checkpoint metadata. |
OPENCLAW_TOKEN_ESTIMATE | Token usage estimate, recorded in checkpoint metadata. |
These are typically set automatically by the OpenClaw runtime. You do not need to configure them manually.
Tuning Constants
The following limits are hardcoded in the handler and control injection size:
| Constant | Value | Purpose |
|---|---|---|
MAX_CONTEXT_RESULTS | 4 | Maximum vault memory results injected |
MAX_CONTEXT_PROMPT_LENGTH | 500 | Maximum characters of the user prompt sent to search |
MAX_CONTEXT_SNIPPET_LENGTH | 220 | Maximum characters per memory snippet |
MAX_RECAP_RESULTS | 6 | Maximum session recap messages injected |
MAX_RECAP_SNIPPET_LENGTH | 220 | Maximum characters per recap message |
Verifying the Hook
Run the compatibility check to confirm your setup:
clawvault compatThis verifies:
- ClawVault CLI is functional
- A vault is discoverable
- qmd is installed and working
- The hook can execute vault commands
You can also check the gateway logs for hook activity:
[clawvault] Checking for context death
[clawvault] Clean startup - no context deathOr on session start:
[clawvault] Fetching session recap for context restoration
[clawvault] Fetching vault memories for session start prompt
[clawvault] Injected session context (3 recap, 2 memories)Security
The hook follows strict security practices:
- No shell execution. All subprocess calls use
execFileSyncwith an argument array, preventing command injection. - Input sanitization. Session keys, prompts, and display strings are validated and stripped of control characters before use.
- Timeout enforcement. Each
clawvaultsubprocess has a 15-second timeout to prevent hangs. - Vault path validation. The vault path must be absolute, exist as a directory, and contain a
.clawvault.jsonconfig file.
Troubleshooting
Hook not firing
- Confirm the hook is listed:
openclaw hooks list - Check that the event names match. The hook normalizes separators, so
gateway:startup,gateway.startup, andgateway/startupall work. - Review gateway logs for
[clawvault]prefixed messages.
"No vault found" in logs
- Set
CLAWVAULT_PATHexplicitly to your vault directory. - Ensure the vault contains a
.clawvault.jsonfile:ls /path/to/vault/.clawvault.json - If using the OpenClaw workspace convention, ensure the vault is at
memory/inside the workspace.
Context not injected on session start
- The hook requires an initial user message in the event to search against. If the event has no messages array, injection is skipped.
- Check that
clawvault contextworks manually:clawvault context "test query" -v /path/to/vault - Check that
clawvault session-recapworks:clawvault session-recap agent:myagent:main
Auto-checkpoint failing on /new
- Run
clawvault checkpoint --working-on "test"manually to verify the checkpoint command works. - Ensure the vault directory is writable.
Recovery not detecting context death
- Run
clawvault recover --clear -v /path/to/vaultmanually. - Context death detection relies on checkpoint data. If no checkpoint was ever created, there is nothing to recover.