Skip to content

[Bug]: Resuming a subagent via task_id silently rewrites all previous task results with the latest reply #595

Description

@jensenojs

Bug Description

I often run multi-round work in a single subagent session (the task tool with task_id, experimental.allowSubAgents: true — a subagent on a long task needs DCP to prune its own context). Nothing looks wrong unless the model goes back and reads them. In a real session this made the main agent conclude the subagent was repeating itself, and it re-sent the same instruction byte-for-byte over and over.

Minimal reproduction

  1. Enable experimental.allowSubAgents: true.
  2. Dispatch a subagent with a small question, asking it to start its reply with ROUND-1-MARKER.
  3. Resume it twice via task_id with two different questions, asking for ROUND-2-MARKER / ROUND-3-MARKER.
  4. After round 3, compare the three task results in the main agent's context with what's stored.

What I get:

layer round 1 round 2 round 3
subagent's actual replies ROUND-1 + answer A ROUND-2 + answer B ROUND-3 + answer C
task results in parent DB A ✓ B ✓ C ✓
task results in the model's input C ✗ C ✗ C ✓

Root cause in brief: injectExtendedSubAgentResults replaces the whole <task_result> body with the subagent session's current last assistant text. The cache that's supposed to pin each call's correct text (subAgentResultCache) lives in DCP's singleton session state, which is reset on every session switch — and a resumed subagent runs its own model calls through the same hook between the parent's turns, so the cache is empty on every single parent call. No server restart or special environment needed; any resumed session hits this every round.

{
  "$schema": "https://raw.githubusercontent.com/Opencode-DCP/opencode-dynamic-context-pruning/master/dcp.schema.json",
  "compress": {
    "maxContextLimit": "60%",
    "minContextLimit": "35%",
    "nudgeFrequency": 3
  },
  "experimental": {
    "allowSubAgents": true
  }
}

Expected Behavior

Each task result keeps the reply of its own round — resuming a session must never rewrite the content of earlier calls' results.

Debug Context Logs

| layer                             | round 1            | round 2            | round 3            |
| --------------------------------- | ------------------ | ------------------ | ------------------ |
| subagent's actual replies         | `ROUND-1` + answer A | `ROUND-2` + answer B | `ROUND-3` + answer C |
| task results in parent DB         | A ✓                | B ✓                | C ✓                |
| task results in the model's input | C ✗                | C ✗                | C ✓                |

Tool Call Details

No response

DCP Version

3.1.14

Opencode Version

1.18.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions