Skip to content

[OpenCode] Active task prompts can be truncated during context reclaim #250

Description

@excessivechaos

Summary

Magic Context can mutate an active OpenCode task invocation and truncate its prompt before the child session receives it. The child then receives only the first five characters followed by ...[truncated].

Observed child prompt:

Fix...[truncated]

This is prompt corruption, not ordinary historical context compaction.

Environment

  • OpenCode 1.18.4
  • @cortexkit/opencode-magic-context 0.33.0
  • Windows
  • AFT disabled during a reproducing run
  • Magic Context is managing context with OpenCode compaction disabled

Reproduction

  1. Use a long-running OpenCode session with Magic Context enabled.
  2. Dispatch a subagent with a prompt whose serialized task input is over 500 characters.
  3. Let Magic Context run a reclaim/transform pass while the task part is still pending or running.
  4. The child session can receive a prompt reduced to the first five characters plus ...[truncated].

The failure is intermittent because it depends on reclaim eligibility and timing. Large prompts that do not coincide with an eligible active task part are unaffected.

Root Cause

In packages/plugin/src/hooks/magic-context/tool-drop-target.ts, extractToolCallObservation() currently classifies every OpenCode part shaped like:

{ type: "tool", callID, state: { status: "running", input, output } }

as a result. Therefore the tool-call index sets hasResult = true while the invocation is still active. truncateToolPart() then calls truncateInputValues() on the live state.input, which changes long strings to:

first...[truncated]

Proposed Fix

  • Classify OpenCode pending and running tool parts as invocations.
  • Treat any tool-call entry containing an active OpenCode part as incomplete and block drop(), truncate(), and editMarker() for that entry.
  • Block setContent() from rewriting active entries or reporting a successful dropped-content mutation.
  • Add defensive guards so the individual truncation and edit-marker helpers never mutate active OpenCode tool parts.
  • Preserve the existing behavior for completed and unspecified-status parts and for other wire formats.

I have a local patch with focused regression tests covering active classification, the defensive guard, and completed-tool truncation.

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