Skip to content

fix: prevent 414 error and error log flooding#3

Open
mengfanbo123 wants to merge 2 commits into
ourmem:mainfrom
mengfanbo123:fix/query-truncation-and-error-handling
Open

fix: prevent 414 error and error log flooding#3
mengfanbo123 wants to merge 2 commits into
ourmem:mainfrom
mengfanbo123:fix/query-truncation-and-error-handling

Conversation

@mengfanbo123
Copy link
Copy Markdown

Summary

Fixes two critical bugs that cause the omem MCP server and OpenCode plugin to fail when memory content is large:

Bug 1: 414 Request-URI Too Large

The autoRecallHook in the OpenCode plugin sends the entire user message (which can include long subagent prompts) as the q query parameter in the search URL. After URL encoding, this exceeds nginx's default large_client_header_buffers limit (8KB), causing HTTP 414 errors.

Fix: Added MAX_QUERY_LENGTH = 500 constant and truncateQuery() function in both clients. All search queries are now truncated to 500 characters before making the API request. Also added sanitizeContent() to strip XML tags from content before sending, reducing payload size.

Bug 2: Error log flooding the terminal

When an HTTP error occurs, the MCP client includes the full response body in the Error message. Combined with tools.ts catch blocks that propagate this, a single 414 error can dump hundreds of KB into the OpenCode terminal window, making it unusable.

Fix:

  • Added safeErrorMessage() in MCP client that truncates error response bodies to 200 characters
  • Added shortError() in tools.ts that truncates all catch block error messages to 200 characters

Bug 3: Large content causing server errors

Large content strings sent to the store/ingest APIs can cause embedding failures (500 errors).

Fix: Added MAX_CONTENT_CHARS = 30000 and sanitizeContent() to strip XML tags and truncate content before sending.

Files Changed

  • plugins/mcp/src/client.ts — Added query truncation, content sanitization, error message truncation
  • plugins/mcp/src/tools.ts — Added shortError() for all 14 catch blocks
  • plugins/opencode/src/client.ts — Added query truncation and content sanitization
  • plugins/opencode/src/hooks.ts — Truncated firstMessage in autoRecallHook to 500 chars
  • plugins/mcp/package.json — JSON formatting only
  • plugins/opencode/package.json — JSON formatting only

Testing

  • Verified that truncated queries (500 chars → ~4.6KB URL) successfully return HTTP 200
  • Verified that untruncated queries (>8KB URL) still cause HTTP 414 (expected, nginx limitation)
  • Verified that large content storage still works with sanitized/truncated content
  • Verified that error responses are now truncated and no longer flood the terminal

- Truncate search query to 500 chars in both MCP and OpenCode plugin clients
  to prevent URL length exceeding nginx default 8KB limit
- Add safeErrorMessage() in MCP client to truncate HTTP error response body
  to 200 chars, preventing error logs from filling the terminal
- Add shortError() in MCP tools.ts to truncate all catch block error messages
- Add sanitizeContent() to strip XML tags and truncate large content before
  sending to server (max 30000 chars)
- Truncate firstMessage in OpenCode plugin autoRecallHook to 500 chars

Root cause: OpenCode plugin's autoRecallHook sends entire user messages
(including subagent prompts) as search query parameter, which after URL
encoding exceeds nginx's large_client_header_buffers limit, causing 414.
The MCP client then includes the full HTTP error response body in the
Error message, which floods the OpenCode terminal window.
…uffer

CJK chars URL-encode ~9x: 500 chars → ~4500 bytes > nginx 4K buffer.
200 chars → ~1800 bytes, safe under nginx default 4K and 16K.
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