dsh-plugin-fork-inbox-guard
Stops a forked DeepSeek Harness session from inheriting the prompts its
parent had queued but not yet delivered.
A DeepSeek Harness (dsh) plugin. Symptom search terms: fork, branch session,
child session sends a prompt I never typed, duplicated prompt after fork,
queued message fires twice,next-turnleaked into fork, 分叉会话, 衍生会话,
新会话自动发送了排队消息, 排队消息被复制.
Symptom
You queue a prompt while an answer is still streaming — dsh holds it as
next-turn until the current turn ends. Before it is delivered, you fork the
session (or pick it from the fork control). The new child opens with that
pending prompt already in its inbox, and the child executes it on its first
turn. Fork twice and the same prompt appears in more children.
Root cause
Fork copies a completed-turn prefix of the parent's log:
// packages/api/session-controller/src/commands.ts:243
let cut = SessionLogOffset(boundary.seq + 1)
while (cut < source.events.length && source.events[cut]?.type !== 'turn/start') {
cut = SessionLogOffset(cut + 1)
}
The cut advances to the next turn/start, so every event between the
completed boundary and that turn is copied — including the
agent/inbox/spliced events that queued input. The child is then created
with that prefix as its seed and meta.isSeeded: true
(commands.ts:263).
The inbox projection folds every splice in the log and has no notion of
the inherited prefix:
// packages/core/agent-loop/src/inbox.ts:27
export const inboxProjectionDefinition = {
key: 'inbox',
stateSchema: inboxProjectionSchema,
init: (): InboxState => ({ 'next-turn': [], 'next-step': [] }),
apply(state, event) { /* no inheritedEventCount check */ },
}
So the child's inbox.nextTurn is non-empty the moment it is created, and
its first claim('next-turn') consumes the parent's prompt. The session
format even appends a session/end-seed { inherited: true } marker at the
cut (packages/core/session/src/index.ts:607), but the inbox projection does
not consult it.
What this plugin does
On agent/created, for a session whose header marks it isSeeded:
- Replay only the events before
session.inheritedEventCountthrough the
inbox splice rules. That yields exactly the identities that were pending at
the fork cut. - Remove each of those identities from the child's inbox with
agent.inbox.remove(id)— a durableagent/inbox/splicedwith
removedCount: 1andoutcome: "canceled", not a memory-only edit.
Input the child queued for itself after the cut is never touched, and a
non-seeded session is never touched, so a resumed session keeps its own queue.
Install
dsh plugin --profile <your-profile> add dsh-plugin-fork-inbox-guard
Then mount it in that profile's patch layer
(~/.dsh/profiles/<your-profile>/cordis.patch.yml):
- insert:
- id: fork-inbox-guard
name: 'dsh-plugin-fork-inbox-guard'
Config
| Field | Default | Meaning |
|---|---|---|
drop |
true |
Remove fork-inherited pending input. false reports what would be removed and leaves the session untouched. |
- id: fork-inbox-guard
config:
drop: false
Verification
Reproduced end to end against the real fork command, then fixed, then
re-run — see docs/verification.md for the raw
transcripts. Summary:
| Run | Child pendingNextTurn |
Child own events | Parent queue |
|---|---|---|---|
| Guard disabled | contains the parent's queued id | [] |
intact |
| Guard mounted | [] |
one durable spliced { removedCount: 1, outcome: "canceled" } |
intact |
npm test adds 12 contract tests (all passing).
Limits
- This is a plugin, not an upstream patch. It clears the inherited input when
the child agent is created; it does not change the fork cut, so the child's
log still carries the inherited prefix. - Sessions forked by a deployment that never mounts the plugin keep the
inherited input. - The real fix belongs in the cut condition above, or in an
inheritedEventCountcheck insideinboxProjectionDefinition. This
package exists because a bundle patch cannot reach either.
Credits
Diagnosis and reproduction by the dsh community. This package only packages
the resolution as a mountable guard.
License
MIT