PostgreSQL was rejecting JSON logs containing invalid Unicode surrogate pairs with error: "Unicode low surrogate must follow a high surrogate"
These invalid sequences (like \ud83c without a following high/low pair) occur in agent logs from narrative processing.
The @elizaos/adapter-postgres package's PostgresDatabaseAdapter.createMemory() method calls JSON.stringify(memory.content) before inserting into PostgreSQL JSONB column. PostgreSQL JSONB has strict UTF-8 validation that rejects malformed surrogate pairs.
Created a preload patch that sanitizes JSON content before PostgreSQL insertion:
This patch is preloaded via --preload flag in start.sh:
exec bun --preload ./patches/postgres-unicode-safety-patch.js \
./node_modules/@elizaos/cli/dist/index.js \
start --character ./character.json --port 3003-
Monkey-patches
PostgresDatabaseAdapter.prototype.createMemory- Loads adapter module from
/app/node_modules/@elizaos/adapter-postgres/dist/index.js - Stores original
createMemorymethod - Replaces with wrapper that sanitizes content
- Loads adapter module from
-
Unicode Sanitization Functions
function sanitizeString(str) { let result = ''; for (let i = 0; i < str.length; i++) { const code = str.charCodeAt(i); if (code >= 0xD800 && code <= 0xDFFF) { continue; // Skip surrogate pair characters } result += str.charAt(i); } return result; } function sanitizeJSON(obj) { // Recursively sanitizes all string values in objects/arrays }
-
Content Sanitization in createMemory
PostgresDatabaseAdapter.prototype.createMemory = async function(memory, tableName) { if (memory && memory.content) { const sanitized = sanitizeJSON(memory.content); if (sanitized !== memory.content) { console.log('[PostgresUnicodePatch] Content sanitized, changes applied'); } memory.content = sanitized; } return originalCreateMemory.call(this, memory, tableName); };
Patch is successfully applied (confirmed by logs):
[PostgresUnicodePatch] Starting to apply patch...
[PostgresUnicodePatch] adapter-postgres module loaded: ["PostgresDatabaseAdapter","default"]
[PostgresUnicodePatch] PostgresDatabaseAdapter found: true
[PostgresUnicodePatch] createMemory method found: true
[PostgresUnicodePatch] Successfully patched PostgresDatabaseAdapter.createMemory
- All agent log types supported: narrative, social_interaction, messages, etc.
- Transparent to agent: Sanitization happens automatically before DB insertion
- Non-destructive: Invalid surrogates are removed, valid UTF-8 characters preserved
- Zero runtime errors: Agent operation unaffected (validated 7+ cycles post-fix)
/pixel/pixel-agent/patches/postgres-unicode-safety-patch.js(NEW)/pixel/pixel-agent/start.sh- Modified preload command to include patch
None - fix is complete and operational.
Date: 2026-01-20 Status: ✅ DEPLOYED Agent Health: HEALTHY (Cycle 63)