📝 Bug Description
Autosync's pull phase halts permanently when a remote observation or user prompt refers to a session_id that does not exist locally. The resulting FOREIGN KEY constraint failed error aborts the entire pull cycle, leaves last_pulled_seq frozen, and blocks all subsequent remote mutations from being applied.
🔄 Steps to Reproduce
- On the cloud instance, have an
observation or user_prompt row whose session_id never had its corresponding session mutation synchronized to the local client.
- Enable autosync on the client (
ENGRAM_CLOUD_AUTOSYNC=1).
- Wait for a pull cycle to attempt to apply the orphaned observation/prompt.
- Observe the FK error and the
last_pulled_seq cursor remaining stuck at its previous value.
Minimal real-world scenario observed:
last_pulled_seq frozen at 16284.
- ~1200 remote mutations pending behind the failing one.
- Error repeats deterministically every cycle.
✅ Expected Behavior
Pull should defer or quarantine any FK miss—not only FK misses for relations—advance the cursor, and log the incident. This would mirror the safety net that push already provides for irreparable pending mutations.
❌ Actual Behavior
[autosync] phase=degraded last_error="pull: apply pulled mutation seq=16311: constraint failed: FOREIGN KEY constraint failed (787)"
ApplyPulledMutation does defer FK misses via sync_apply_deferred, but that mechanism currently covers only relations FK misses. For observation and prompt entity types the error propagates and aborts the whole pull cycle. The cursor does not advance, no quarantine occurs, and the client never recovers automatically.
On the same host, push behavior contrasts sharply:
[autosync] quarantined 141 irreparable pending mutation(s)
Pull has no equivalent quarantine path.
🖥️ Environment
- Operating System: Linux (Ubuntu client) / LXC (cloud server)
- Engram Version: v2.0.0-rc.11 (both client and cloud)
- Agent / Client: OpenCode
- Cloud topology: Engram Cloud self-hosted in LXC, binary v2.0.0-rc.11, behind a TLS reverse proxy with hostname
engram-cloud.lan.
- Real scenario: Multiple machines (Windows + Linux) syncing to the same cloud; content in Spanish with accents; years of history; ~1500 mutations in queue.
📋 Relevant Logs
[autosync] phase=degraded last_error="pull: apply pulled mutation seq=16311: constraint failed: FOREIGN KEY constraint failed (787)"
💡 Additional Context
Workaround applied and verified:
Manually insert the missing sessions into the local sessions table (id, project, directory, started_at, ownership_mode) so the FK is satisfied. After doing this, pull advanced from 16284 to 17798 and push completed 3565/3565.
Root cause:
The local schema has FK constraints such as observations.session_id -> sessions.id and user_prompts.session_id -> sessions.id. A remote mutation for an observation/prompt can arrive before (or without) its parent session mutation, creating an orphaned row. The pull applier treats this as a fatal error instead of a deferrable/quarantinable condition.
📝 Bug Description
Autosync's pull phase halts permanently when a remote observation or user prompt refers to a
session_idthat does not exist locally. The resultingFOREIGN KEY constraint failederror aborts the entire pull cycle, leaveslast_pulled_seqfrozen, and blocks all subsequent remote mutations from being applied.🔄 Steps to Reproduce
observationoruser_promptrow whosesession_idnever had its correspondingsessionmutation synchronized to the local client.ENGRAM_CLOUD_AUTOSYNC=1).last_pulled_seqcursor remaining stuck at its previous value.Minimal real-world scenario observed:
last_pulled_seqfrozen at16284.✅ Expected Behavior
Pull should defer or quarantine any FK miss—not only FK misses for
relations—advance the cursor, and log the incident. This would mirror the safety net that push already provides for irreparable pending mutations.❌ Actual Behavior
ApplyPulledMutationdoes defer FK misses viasync_apply_deferred, but that mechanism currently covers onlyrelationsFK misses. Forobservationandpromptentity types the error propagates and aborts the whole pull cycle. The cursor does not advance, no quarantine occurs, and the client never recovers automatically.On the same host, push behavior contrasts sharply:
Pull has no equivalent quarantine path.
🖥️ Environment
engram-cloud.lan.📋 Relevant Logs
💡 Additional Context
Workaround applied and verified:
Manually insert the missing sessions into the local
sessionstable (id,project,directory,started_at,ownership_mode) so the FK is satisfied. After doing this, pull advanced from16284to17798and push completed3565/3565.Root cause:
The local schema has FK constraints such as
observations.session_id -> sessions.idanduser_prompts.session_id -> sessions.id. A remote mutation for an observation/prompt can arrive before (or without) its parent session mutation, creating an orphaned row. The pull applier treats this as a fatal error instead of a deferrable/quarantinable condition.