Skip to content

Historian child cleanup races late OpenCode part writes #336

Description

@coleleavitt

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:

  1. Historian child created and streamed for about five minutes.
  2. Magic Context accepted/published the compartment and deleted the child.
  3. OpenCode persisted a late step-start part for that child.
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions