Skip to content

fix(cli): remap child session references on fork to prevent subagent state leaks - #8956

Merged
marius-kilocode merged 3 commits into
mainfrom
handy-lace
Apr 15, 2026
Merged

fix(cli): remap child session references on fork to prevent subagent state leaks#8956
marius-kilocode merged 3 commits into
mainfrom
handy-lace

Conversation

@marius-kilocode

Copy link
Copy Markdown
Collaborator

Summary

  • When forking a session, task tool parts referencing child (subagent) sessions were copied verbatim, causing both original and forked sessions to share the same child session IDs
  • This caused SSE events, permission prompts, and subagent state to leak across forked sessions in the Agent Manager when sessions shared the same worktree
  • After fork, remapChildren now recursively forks each referenced child session and updates the part metadata to point at the new copies, ensuring complete session isolation

…state leaks

When forking a session, task tool parts referencing child sessions were
copied verbatim, causing both original and forked sessions to share the
same subagent references. This led to SSE events and permission prompts
leaking across sessions in the Agent Manager.
Comment thread packages/opencode/src/kilocode/session/fork.ts Outdated
Comment thread packages/opencode/src/kilocode/session/fork.ts
@kilo-code-bot

kilo-code-bot Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: 1 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
packages/opencode/src/kilocode/session/fork.ts 60 state.output still contains the original task_id, so resumed tasks can reconnect to the source child session

Fix these issues in Kilo Cloud

Other Observations (not in diff)

Issues found in unchanged code that cannot receive inline comments:

None.

Files Reviewed (4 files)
  • packages/opencode/src/kilocode/session/fork.ts - 1 carried-forward unresolved issue
  • packages/opencode/src/kilocode/session/index.ts - 0 issues
  • packages/opencode/src/session/index.ts - 0 issues
  • packages/opencode/test/kilocode/session-fork-remap.test.ts - 0 issues (carried forward)

Reviewed by gpt-5.4-20260305 · 400,312 tokens

Session.fork() already calls remapChildren via the hook, so the explicit
recursive call inside remapChildren was double-forking nested descendants.
@marius-kilocode
marius-kilocode enabled auto-merge (squash) April 15, 2026 08:59
Comment thread packages/opencode/src/session/index.ts Outdated
runPromise((svc) => svc.fork(input)),
)
// kilocode_change start - remap child session references after fork to prevent subagent state leaks
export const fork = fn(z.object({ sessionID: SessionID.zod, messageID: MessageID.zod.optional() }), async (input) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this custom fork function to packages/opencode/src/kilocode/session/index.ts
and do a single line replace like:

export const fork = kiloSessionFork; // kilocode_change

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — moved the custom fork wrapper into packages/opencode/src/kilocode/session/index.ts as kiloSessionFork and changed the shared file to a one-line export const fork = kiloSessionFork // kilocode_change.

@marius-kilocode
marius-kilocode merged commit c584eec into main Apr 15, 2026
16 checks passed
@marius-kilocode
marius-kilocode deleted the handy-lace branch April 15, 2026 14:12
jliounis pushed a commit to jliounis/kilocode that referenced this pull request May 18, 2026
…state leaks (Kilo-Org#8956)

* fix(cli): remap child session references on fork to prevent subagent state leaks

When forking a session, task tool parts referencing child sessions were
copied verbatim, causing both original and forked sessions to share the
same subagent references. This led to SSE events and permission prompts
leaking across sessions in the Agent Manager.

* fix: remove redundant recursive remapChildren call

Session.fork() already calls remapChildren via the hook, so the explicit
recursive call inside remapChildren was double-forking nested descendants.
t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
…state leaks (Kilo-Org#8956)

* fix(cli): remap child session references on fork to prevent subagent state leaks

When forking a session, task tool parts referencing child sessions were
copied verbatim, causing both original and forked sessions to share the
same subagent references. This led to SSE events and permission prompts
leaking across sessions in the Agent Manager.

* fix: remove redundant recursive remapChildren call

Session.fork() already calls remapChildren via the hook, so the explicit
recursive call inside remapChildren was double-forking nested descendants.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants