langgraph icon indicating copy to clipboard operation
langgraph copied to clipboard

SqliteStore and AsyncSqliteStore reset created_at when updating an existing item

Open cty-ut opened this issue 1 month ago • 2 comments

Description

Updating an existing item with SqliteStore.put() or AsyncSqliteStore.aput() resets created_at to the update time. For a persisted user profile whose preferences change, this loses the original creation time exposed by get() and search().

Expected: preserve created_at when updating the same namespace/key, and advance updated_at. Deleting and recreating an item should start a new creation time. This matches the documented meaning of SearchItem.created_at and the Postgres store's upsert behavior.

Reproduction Steps / Example Code (Python)

import time
from langgraph.store.sqlite import SqliteStore

with SqliteStore.from_conn_string(":memory:") as store:
    namespace = ("users", "alice")
    store.put(namespace, "preferences", {"drink": "tea"})
    before = store.get(namespace, "preferences")
    assert before is not None

    time.sleep(1.1)  # SQLite CURRENT_TIMESTAMP has second precision.

    store.put(namespace, "preferences", {"drink": "coffee"})
    after = store.get(namespace, "preferences")
    assert after is not None
    assert after.value == {"drink": "coffee"}
    assert after.updated_at > before.updated_at
    assert after.created_at == before.created_at  # AssertionError

I also reproduced this through the async public API locally.

Cause and proposed fix

The shared BaseSqliteStore._prepare_batch_PUT_queries() builds INSERT OR REPLACE with CURRENT_TIMESTAMP for both timestamps. Replacing a row therefore discards its original creation time.

I have a small SQLite-only fix that reads the previous created_at by (prefix, key) within the same write statement, falling back to CURRENT_TIMESTAMP for new items. Retaining REPLACE preserves existing vector cleanup when foreign keys are enabled. No schema or dependency changes are needed.

The new regression cases fail before the fix (8 failures) and pass afterward. The package suite passes with 193 passed and 2 skipped; make format and make lint also pass. Tests cover sync/async stores, single/batched writes, inserts, TTL removal, deletion/recreation, and vector cleanup.

I'd like to contribute this fix. Could a maintainer confirm the approach and assign this issue to @cty-ut?

Related Issues / PRs

#7411 and #8340 describe the same symptom in InMemoryStore. Their related patches target the memory backend; SQLite uses a separate query builder. I did not find a public SQLite-specific report or fix in the searches performed before filing this issue.

System Info

  • Reproduced against repository commit e14f4277dc43e1d12981a36c02757fbec6ede217; the affected query builder is unchanged on current upstream main.
  • langgraph-checkpoint-sqlite: 3.1.2 (local checkout)
  • langgraph-checkpoint: 4.3.0 (local checkout)
  • langchain-core: 1.5.2
  • Python: 3.13.14; macOS arm64; SQLite: 3.54.0; aiosqlite: 0.22.1
  • Dependencies installed from the package's uv.lock.

Prepared with AI assistance; the public-API reproduction and tests were executed locally.

cty-ut avatar Oct 16 '26 16:10 cty-ut

The proposed fix and regression tests are in #9332. The assignment check automatically closed it because this issue is not assigned to me yet. Could a maintainer review the proposed approach and, if it is in scope, assign this issue to @cty-ut? The workflow indicates that assignment will reopen the PR automatically.

cty-ut avatar Oct 16 '26 16:10 cty-ut

Reproduced on current main: put()/aput() on an existing (namespace, key) resets created_at — a regression test fails on main across the sync/async and memory/file variants, and passes with the fix.

The fix carries the original creation time through REPLACE via a COALESCE subquery (so the vector-cascade behavior of REPLACE is preserved), and adds regression tests covering: update preserves created_at and advances updated_at, delete-then-recreate starts a new creation time, and list ordering stays correct. Red/green verified: 152 tests pass in libs/checkpoint-sqlite with the fix.

Branch is ready on my fork — happy to open the PR once assigned.

Wilson0511 avatar Oct 20 '26 13:10 Wilson0511