Skip to content

fix(examples): derive SQL memory IDs from keys so search results are reproducible - #10

Open
Tim0085 wants to merge 2 commits into
CodeSoul-co:devfrom
Tim0085:fix/sql-memory-stable-ids
Open

Tim0085 wants to merge 2 commits into
CodeSoul-co:devfrom
Tim0085:fix/sql-memory-stable-ids

Conversation

@Tim0085

@Tim0085 Tim0085 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Problem

createSqlMemoryStore (examples/_shared/tools/storage/sql-memory.ts) gives every written memory a fresh randomUUID(), while get, query and search all order by that ID (ORDER BY id LIMIT ?). Whenever more rows match than limit, the rows that come back depend only on the random IDs. As a result:

  • writing the same memories into two databases (or rebuilding one) returns different search results;
  • the order of query pages also changes from one build to the next.

The Qdrant adapter already avoids this: it derives point IDs from the key with memoryPointId(key) (qdrant-memory.ts).

We ran into this while comparing two Worker layouts of the same agent on a benchmark. The only reason the model received different requests under the two layouts was that SQLite memory search returned different rows. With key-derived IDs, the request bodies sent to the model were byte-identical for every task we compared.

Change

  • Keyed memories now get the same key-derived UUID that qdrant-memory.ts uses. I copied the helper as memoryRecordId so that sql-memory.ts doesn't have to import qdrant-memory.ts, which pulls in @codesoul-co/ditto-retrieval. If you prefer, I can move the helper into a small shared file instead.
  • Memories without a key still get randomUUID().
  • New test in test/examples-memory.test.ts. It writes the same keyed memories into two SQLite files and checks two things: the IDs match the Qdrant point IDs, and a limited search returns the same rows in both files. The test fails on the current dev.

Compatibility

  • Both bundled schemas (SQLite and Postgres) declare memory_key TEXT UNIQUE and use idempotentKeys = true, so this change adds no new conflicts. Existing rows keep their IDs; only new writes are affected.
  • One behavior change for custom setups: with idempotentKeys = false and a table where memory_key is not unique, writing the same key twice used to create a duplicate row. It now fails on the primary key. This seems closer to the intended key semantics, but I'm happy to limit key-derived IDs to idempotentKeys = true if you prefer.
  • Ordering for memories without a key is still arbitrary. That is out of scope here.

Validation

Run with npm run check on Windows 11, Node 24.14.0, on top of dev at f0e5297:

  • Typecheck and builds pass. Tests: 518 total, 511 passed, 3 skipped, 4 failed.
  • The new test passes. It fails when write is switched back to randomUUID() with everything else unchanged.
  • The same 4 tests fail on unmodified dev on this machine, and none of them touches the files changed here. They all look Windows-specific:
    • capability-boundary.test.ts: examples/capabilities/tools/fixtures.ts: model/service transport belongs in a registered adapter
    • examples-rag.test.ts and examples-retrieval.test.ts: EPERM on symlink. Windows allows symlinks only in Developer Mode or with admin rights.
    • sandbox-executor.test.ts: the environment-key assertion. Windows adds SYSTEMROOT, TEMP and similar keys; this was also noted in fix: stop ReAct on rejected or unknown output receipts #4.

@erwinmsmith

Copy link
Copy Markdown
Collaborator

Thanks for tracking the different search results down to the SQLite IDs. Please use the key-derived ID only when idempotentKeys is enabled and the record has a key. The SQL integration example uses the default mode and explicitly allows several records to share a key. Two writes with the same key should still return distinct IDs there.

Could you add a regression test for those two writes alongside the reproducibility test? For the UUID calculation, please share one small helper between the SQL and Qdrant adapters so they stay in sync.

SQL integration example and key semantics

@Tim0085

Tim0085 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I've updated the PR:

  • Key-derived IDs are now used only when idempotentKeys is enabled and the record has a key. The default mode keeps randomUUID(), so the SQL integration example can still store several records under one key.
  • New regression test using the integration example's schema (memory_key indexed, not unique). Two writes with the same key return distinct IDs, neither ID is the key-derived one, and get({ keys }) returns both records. The test fails against this PR's previous commit.
  • The UUID calculation now lives in one helper, keyedMemoryId in examples/_shared/tools/storage/memory-id.ts. qdrant-memory.ts imports it and still exports memoryPointId, so existing imports keep working. sql-memory.ts no longer has its own copy.
  • sql-memory.ts now imports memory-id.ts, so the new file has to be copied wherever sql-memory.ts is copied. I added it to:
    • the copy lists in scripts/check-package-basics.ts and scripts/check-json-agent-package.ts;
    • the copy instructions in the package guide and the package-basics, integrations and handbook READMEs, in English and Chinese;
    • the table in the storage README.
  • These two scripts spawn npm directly, which doesn't work on Windows, so I couldn't run them here. Instead I copied exactly the files on the updated list into a separate directory and ran openSqliteMemory write and search there; both worked. Without memory-id.ts in the copy, that run fails with ERR_MODULE_NOT_FOUND.

npm run check on Windows 11, Node 24.14.0: 519 tests, 512 passed, 3 skipped, 4 failed. The 4 failures are the same Windows-only tests as before.

The "Compatibility" section of the description is now out of date: default mode no longer changes behavior.

This branch has not been deployed

No deployments
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.

2 participants