Skip to content

fix: catch unhandled exceptions in transform hooks and avoid loading embedding BLOBs#52

Merged
BYK merged 2 commits intomainfrom
fix/transform-hook-error-handling
Mar 24, 2026
Merged

fix: catch unhandled exceptions in transform hooks and avoid loading embedding BLOBs#52
BYK merged 2 commits intomainfrom
fix/transform-hook-error-handling

Conversation

@BYK
Copy link
Owner

@BYK BYK commented Mar 24, 2026

Problem

Both transform hooks in src/index.tsexperimental.chat.system.transform and experimental.chat.messages.transform — had no try-catch wrapping. Any SQLite error (corruption, busy timeout, schema mismatch) propagated through OpenCode's Plugin.trigger mechanism and surfaced as a 500 "Internal server error", halting the user's session.

Additionally, after adding the embedding BLOB column (schema v8), all SELECT * queries in ltm.ts were unnecessarily loading 4KB of Float32Array data per knowledge entry (~200KB per forSession() call) that was immediately discarded.

Investigation: Embedding/vector search link

The embedding/vector code is not in the transform hook call pathforSession() uses only FTS5 BM25, not embeddings. The 500 errors were a latent bug (unprotected hooks) that predated the embedding feature. The temporal correlation with the Voyage AI rollout was coincidental — it coincided with the search overhaul (PRs #46-#50).

Changes

Error handling (src/index.ts, src/gradient.ts)

  • system.transform: Wrap knowledge injection in try-catch. On error: log via log.error(), reset setLtmTokens(0), push fallback note directing LLM to use recall tool. Track degraded sessions to avoid busting the provider's read-token cache on recovery — if conversation is longer than LTM content, keep fallback note.
  • messages.transform: Wrap entire transform path in try-catch. On error: log and leave output.messages unmodified (layer 0 passthrough).
  • Export getLastTransformEstimate() from gradient.ts for the cache trade-off calculation.

Performance (src/ltm.ts)

  • Define KNOWLEDGE_COLS / KNOWLEDGE_COLS_K constants listing exactly the 11 columns in KnowledgeEntry, excluding embedding.
  • Replace all 10 SELECT * / SELECT k.* queries across 8 functions.

Tests (test/index.test.ts)

4 new tests:

  1. system.transform survives DB error → fallback note + getLtmTokens() === 0
  2. messages.transform survives DB error → messages unchanged
  3. LTM recovery skipped on long session (preserves prompt cache)
  4. LTM recovery proceeds on short session (cheap cache bust)

BYK added 2 commits March 24, 2026 10:39
Both transform hooks (system.transform and messages.transform) lacked
try-catch wrapping. Any SQLite error (corruption, busy timeout, schema
mismatch) propagated through OpenCode's Plugin.trigger mechanism and
surfaced as a 500 'Internal server error', halting the user's session.

Changes:
- system.transform: wrap knowledge injection block in try-catch. On
  error, log via log.error(), reset LTM tokens to 0, and push a fixed
  fallback note directing the LLM to use the recall tool. Track
  degraded sessions to avoid busting the provider's read-token cache
  on recovery — if the conversation is longer than the LTM content,
  keep the fallback note rather than switching mid-session.
- messages.transform: wrap the entire transform path in try-catch. On
  error, log via log.error() and leave output.messages unmodified
  (equivalent to layer 0 passthrough).
- gradient.ts: export getLastTransformEstimate() for the cache
  trade-off calculation.
- Tests: 4 new tests covering DB error survival for both hooks, plus
  cache-aware LTM recovery (skip on long sessions, proceed on short).
…ng BLOBs

After adding the embedding BLOB column (schema v8), all SELECT * queries
in ltm.ts were loading 4KB of Float32Array data per knowledge entry that
was immediately discarded (KnowledgeEntry type doesn't include embedding).

This wasted ~200KB per forSession() call (2 queries × ~25 entries × 4KB)
and affected all other knowledge queries (search, searchLike, all, get,
forProject, searchScored).

Define KNOWLEDGE_COLS and KNOWLEDGE_COLS_K constants that list exactly
the columns needed for KnowledgeEntry, excluding the embedding BLOB.
The embedding column is only needed by vectorSearch() in embedding.ts,
which already selects it explicitly.
@BYK BYK enabled auto-merge (squash) March 24, 2026 10:40
@BYK BYK merged commit 3ab2696 into main Mar 24, 2026
1 check passed
@BYK BYK deleted the fix/transform-hook-error-handling branch March 24, 2026 10:40
@craft-deployer craft-deployer bot mentioned this pull request Mar 24, 2026
2 tasks
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