Summary
Magic Context deletes a successful magic-context-compartment child immediately after the prompt API returns. OpenCode can still have detached session writers persisting step/summary parts, so the late insert targets a deleted session_id/message_id and fails its foreign key.
Observed failure
Failed query: insert into "part" (...) values (...) on conflict (...) do update ...
params: ..., {"type":"step-start"}, ...
Observed sequence:
- Historian child created and streamed for about five minutes.
- Magic Context accepted/published the compartment and deleted the child.
- OpenCode persisted a late
step-start part for that child.
- The insert failed because the child session/message rows were already gone.
Root cause
runHistorianPrompt() deleted successful children in finally. Prompt completion and even session.idle are not global quiescence barriers: OpenCode can fork detached summary/session writes.
Expected behavior
Never delete historian children on the prompt-return path. Retire them only through an age-gated maintenance sweep whose stale budget covers the configured timeout, outer retries, model-suggestion retry, and fallback chain. Preserve children when keep_subagents is enabled.
Regression requirements
- Successful run does not call
session.status or session.delete inline.
- Recent historian children are never swept.
- Stale historian children are swept after the full configured attempt budget.
- Dreamer-disabled installations still run the sweep.
- Slow reasoning models receive at least a ten-minute default timeout.
Summary
Magic Context deletes a successful
magic-context-compartmentchild immediately after the prompt API returns. OpenCode can still have detached session writers persisting step/summary parts, so the late insert targets a deletedsession_id/message_idand fails its foreign key.Observed failure
Observed sequence:
step-startpart for that child.Root cause
runHistorianPrompt()deleted successful children infinally. Prompt completion and evensession.idleare not global quiescence barriers: OpenCode can fork detached summary/session writes.Expected behavior
Never delete historian children on the prompt-return path. Retire them only through an age-gated maintenance sweep whose stale budget covers the configured timeout, outer retries, model-suggestion retry, and fallback chain. Preserve children when
keep_subagentsis enabled.Regression requirements
session.statusorsession.deleteinline.