Skip to content

bug(cloud): replay acknowledges chunk when session indexing fails #1459

Description

@dnlrsls

📝 Bug Description

CloudStore.WriteChunk reports success when replaying an identical existing cloud chunk even if rebuilding its session index fails. The matching-payload branch discards the result of indexChunkSessions and returns nil (internal/cloud/cloudstore/cloudstore.go). This can acknowledge an incomplete replay while cloud_project_sessions remains unavailable or out of date; the index is consumed by KnownSessionIDs. This report does not claim observed production data loss.

🔄 Steps to Reproduce

  1. In a disposable Go test for package internal/cloud/cloudstore, use a fake database/sql/driver whose QueryContext returns the exact stored payload for SELECT payload::text FROM cloud_chunks.
  2. Use a payload with one session ID and a matching chunkIDFromPayload(payload). Make the driver's ExecContext return an injected error for INSERT INTO cloud_project_sessions, recording that the insert was attempted.
  3. Call CloudStore.WriteChunk with the matching project, chunk ID, payload and valid timestamp. Observe one failed index attempt while the method returns nil.

This was executed through a temporary Go overlay mapped to a package-local _test.go, without touching a real database or modifying the repository. The overlay was removed afterward. The focused command was go test -overlay <temporary-overlay.json> ./internal/cloud/cloudstore -run '^TestWriteChunkReplayIgnoresSessionIndexFailure$' -count=1 -v; <temporary-overlay.json> denotes the ephemeral fixture, not a checked-in file.

✅ Expected Behavior

If session-index rebuilding fails during an identical chunk replay, WriteChunk returns that error so the caller can distinguish an incomplete replay from a completed one and retry safely. A successful identical replay remains idempotent and does not duplicate mutations.

❌ Actual Behavior

The index insert returned an injected error, but WriteChunk returned nil. The current code uses _ = cs.indexChunkSessions(ctx, project, payload) before returning success. A temporary test asserting the defective behavior passed; its output is below.

Operating System

Windows

Engram Version

2.2.1 (installed CLI and source baseline used for the isolated test)

Agent / Client

Other

📋 Relevant Logs

=== RUN   TestWriteChunkReplayIgnoresSessionIndexFailure
    repro_replay_test.go:56: REPRODUCED: session index write returned injected session index failure but WriteChunk replay returned nil
--- PASS: TestWriteChunkReplayIgnoresSessionIndexFailure (0.00s)
PASS

💡 Additional Context

indexChunkSessionsWith wraps the failed insert error, but the existing-payload replay branch discards it. Existing TestWriteChunkMaterializesMutationsAndIsReplayIdempotent covers healthy replay, not index failure. First add a permanent package test that expects an error from this path (RED on current code), then propagate it and verify healthy replay/idempotence remains unchanged. This is separate from changing the chunk payload or broadening sync policy. No real PostgreSQL instance or user store was used in the reproduction.

Activity

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions