Skip to content

perf(memory): update_fidelity_tags issues N sequential SQLite UPDATEs per turn — consider batch UPDATE #4619

@bug-ops

Description

@bug-ops

Description

MessageStore::update_fidelity_tags in crates/zeph-memory/src/store/messages/mod.rs:401 executes one UPDATE messages SET fidelity_tag = ? WHERE id = ? per message in a loop inside a single transaction. For a context window with 100+ messages this is 100+ sequential execute calls.

Code

let mut tx = self.pool.begin().await?;
for &(id, tag) in updates {
    zeph_db::query(sql!("UPDATE messages SET fidelity_tag = ? WHERE id = ?"))
        .bind(i32::from(tag))
        .bind(id)
        .execute(&mut *tx)
        .await?;
}
tx.commit().await?;

Expected Behavior

A single SQL statement (e.g. UPDATE messages SET fidelity_tag = CASE id WHEN ? THEN ? ... END WHERE id IN (...)) or a temporary table join to batch all updates in one round-trip.

Actual Behavior

N round-trips to the SQLite pool per scoring pass. For a 200-message window this is 200 sequential awaits on the agent's main context-assembly path.

Impact

  • Context assembly latency grows linearly with history length.
  • persist_fidelity_tags is called unconditionally after every score_and_apply, including when most messages have unchanged tags.

Environment

Metadata

Metadata

Assignees

Labels

P3Research — medium-high complexitymemoryzeph-memory crate (SQLite)performancePerformance improvements

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions