Skip to content

SqliteSessions.post() deletes and rewrites all messages on every incremental save — O(n) writes per tool call #19

Closed

Description

@FernandoCelmer

Problem

SqliteSessions.post() unconditionally deletes every MessageRecord for a session and inserts them all again on each call:

db.query(MessageRecord).filter(
    MessageRecord.session_key == key
).delete()
for position, message in enumerate(session.messages):
    db.add(MessageRecord(...))

The agent calls on_message (which triggers storage.post()) after every message appended to the session — including every tool result. For a 20-tool-call turn on a session with 200 messages, this performs 20 × (200 DELETE + 200 INSERT) = 8 000 row writes. On a long session this becomes the dominant cost and causes visible lag in the terminal after each tool call.

Steps to reproduce / context

Profile a long session (100+ messages, 10+ tool calls per turn) with SQLite WAL mode. The post() call time grows linearly with session length.

Expected behavior

Incremental saves should only write new messages since the last save, not rewrite the entire history. The position column already exists and could be used as a monotonic cursor.

Suggested fix

Track the last persisted message count and only insert messages beyond that watermark. The full rewrite should only happen when a message is mutated (e.g., after _compact() rewrites session.messages). Alternatively, add a dirty flag to Session that distinguishes append-only updates from full rewrites.

Activity

  1. added 2 commits that reference this issue on Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions