Skip to content

Denormalize state.isArchived into SQLite metadata to eliminate per-state JSONL reads in getStates #219

Description

@ProfSynapse

Motivation

MemoryService.getStates (post-B1 read-path fix in #218 / v5.9.7) calls adapter.getState for every state in the workspace to surface state.metadata.isArchived. This is O(N) JSONL reads per UI/LLM listStates call.

  • Fine for small workspaces (<50 states).
  • Will degrade for power-user workspaces hitting 100–500+ states.

The "tagged-state shortcut" that previously caused archive-visibility bugs (#218) existed precisely because the original author tried to skip these reads — but the shortcut traded correctness for perf. Denormalizing isArchived into SQLite metadata restores the perf win without sacrificing correctness.

Scope sketch (~80–120 LoC)

  1. v12→v13 schema migration: add is_archived INTEGER DEFAULT 0 column to the states metadata table.
  2. WorkspaceEventApplier.applyStateSaved: write isArchived from content.state.metadata.isArchived (default 0/false).
  3. WorkspaceEventApplier.applyStateUpdated: handle isArchived field updates (parse from data.stateJson when present).
  4. IStorageAdapter.getStates: expose isArchived in StateMetadata result rows.
  5. MemoryService.getStates: restore the conditional await adapter.getState — now safe to skip when metadata is complete. New shortcut shape:
    const fullState = (stateMeta.tags && stateMeta.isArchived !== undefined)
      ? null
      : await adapter.getState(stateMeta.id);
    Or simpler: pass isArchived through stateMetadataToWorkspaceState directly when fullState is null.
  6. Regression tests: MemoryServiceGetStates.test.ts (from fix(memoryManager): surface isArchived from getStates for tagged states #218) should be updated to verify the SQLite path returns isArchived without calling adapter.getState.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions