Skip to content

fix: Parse Claude Code hook JSON and cap prompt length in user-prompt-submit hook - #17

Open
HellFireAE wants to merge 2 commits into
JasonDocton:mainfrom
HellFireAE:fix/user-prompt-hook-json-input
Open

HellFireAE wants to merge 2 commits into
JasonDocton:mainfrom
HellFireAE:fix/user-prompt-hook-json-input

Conversation

@HellFireAE

Copy link
Copy Markdown

Problem

user-prompt-submit.sh treats stdin as the raw prompt, but Claude Code passes hook input as a JSON object (session_id, transcript_path, cwd, prompt, ...). Result:

  • Memories are stored as the hook-input JSON, not the prompt. In my DB 96 of 98 memories were this metadata, and lucid context printed mostly [conversation] ... entries.
  • Long prompts (>512 tokens) fail to embed: ONNX Runtime error ... Attempting to broadcast an axis by a dimension other than 1. 512 by 893, so those memories never get embeddings.

Fix

  • Extract .prompt and .cwd with jq; fall back to raw stdin if jq is missing or input is not JSON (old behavior).
  • Use hook cwd as the project path when CLAUDE_PROJECT_PATH is unset.
  • Truncate the prompt to 1200 chars to stay under the embedding model's 512-token limit.

Testing

Piped {"cwd":"...","prompt":"..."} into the hook: stored memory is clean prompt text and lucid context returns it. bash -n passes.

Notes

Adds an optional jq dependency. The PowerShell hook (user-prompt-submit.ps1) is untouched and may need the same check.

🤖 Generated with Claude Code

HellFireAE and others added 2 commits December 4, 2026 07:54
…-submit hook

The hook treated stdin as the raw prompt, but Claude Code sends a JSON
object. Memories were stored as session metadata JSON instead of prompt
text, and retrieval output degraded to empty entries.

Extract .prompt and .cwd with jq (falls back to raw stdin if jq is
missing or input is not JSON). Truncate the prompt to 1200 chars so long
inputs stay under the 512-token embedding limit instead of failing with
an ONNX broadcast error.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…HARS

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@HellFireAE

Copy link
Copy Markdown
Author

Follow-up pushed (ec89682): the prompt length cap is now configurable via LUCID_PROMPT_MAX_CHARS (default 1200, 0 disables).

Why: the hard-coded 1200 chars only fits the bundled bge-base-en-v1.5 (512 tokens). The limit belongs to the embedding model, so a custom model (LUCID_EMBEDDING_URL / LUCID_EMBEDDING_MODEL, or a different modelPath) can have a larger or smaller context, and a fixed cap would either discard usable text or still overflow.

Notes:

  • The OpenAI-style /v1/embeddings API has no field for the context max. Some servers expose it elsewhere (Ollama /api/show, LM Studio /api/v0/models, vLLM /v1/models), but not uniformly, so an env var is the simplest portable knob for the hook.
  • This is still a character count, not tokens, and the hook is the wrong layer for the long-term fix. Truncating by tokenizer in the native path (embedNative) would be more accurate; for HTTP endpoints the existing RejectedEmbeddingInput handling already covers over-limit input. Happy to follow up on that separately if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant