Skip to content

fix(agent-manager): speed up long-session forks - #13666

Merged
marius-kilocode merged 1 commit into
mainfrom
optimize-tab-agent-forking-performance
Sep 1, 2026
Merged

fix(agent-manager): speed up long-session forks#13666
marius-kilocode merged 1 commit into
mainfrom
optimize-tab-agent-forking-performance

Conversation

@marius-kilocode

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Forking a long Agent Manager transcript waits for the backend to copy the entire history. A synthetic session with 2,000 messages and 6,000 parts took approximately 7.10 seconds before the fork's first transcript page arrived. Most of that time was outside the webview: copying used thousands of individual durable-event transactions, and every copied message/part was then sent twice over SSE.

Why This Change Was Made

Batch copied history into one transaction per flush while retaining the existing projectors, durable event rows, per-session sequence ordering, and notifications after commit. This also applies to copied child sessions and reference remapping. Keep the implementation and publishing adapter in Kilo-owned files, with small integration hooks in shared code.

VS Code now opts out of only the duplicate sync envelopes for copied message/part events, before those envelopes enter the SSE queue. Normal message/part updates, live sync events, in-process cloud ingestion, and default SSE clients remain unchanged. Workspace replication still receives the complete durable stream.

User Impact

The matched long-session scenario improved from approximately 7.10 seconds to 2.8617 seconds for first-page arrival, approximately 4.24 seconds less waiting (59.7% lower latency, 2.48x faster). The final fork was ready after two animation frames at 2.9081 seconds.

History, parent-message references, and independent child sessions remain intact. This is not a constant-time or instant fork: the full history is still read, copied, serialized, and persisted, so larger sessions can take longer.

Evidence

Recorded performance

  • Same warmed source session, forked from the Agent Manager tab context menu with the diff panel closed.
  • Isolated VS Code development host on macOS arm64, using the CLI built from this worktree. No LLM generation was used.
  • Fixture: 2,000 messages, 6,000 parts, with 590,000 bytes of message JSON + 37,229,566 bytes of part JSON = 37,819,566 bytes (37.82 MB).
  • Chromium trace and CPU captures were taken with vscode-self-test. All retained captures report no trace data loss.
Measurement from fork click Original Batched writes Batched writes + duplicate filtering
Fork response received approximately 6,940 ms 3,064.2 ms 2,696.8 ms
First transcript page received approximately 7,100 ms approximately 3,240 ms 2,861.7 ms
Two animation frames after page receipt Not captured 3,298.2 ms 2,908.1 ms

The duplicate-transfer change alone reduced response latency by 367.4 ms (12.0%) and the two-frame readiness measurement by 390.1 ms (11.8%) relative to batching alone. The original capture recorded only 80.457 ms of webview JavaScript time, which ruled out tab rendering as the main source of the multi-second wait.

Precision and scope: baseline and intermediate page-arrival values above are retained rounded measurements; final timings and the later semantic marks are reported to 0.1 ms. These are single matched captures, not medians or p95 estimates. Capture windows differ, so aggregate renderer CPU/DOM counters are not presented as before/after improvements. Captures were recorded against base ff9d04b0ef during implementation, before the behavior-preserving helper extraction/test reduction and fast-forward to current main.

Correctness and validation

  • Verified the real UI fork, loading earlier history, and expanded copied tool output in isolated VS Code.
  • The original stayed at 2,000 messages / 6,000 parts. The fork had 2,001 messages / 6,001 parts, including the existing synthetic handoff reminder, with zero invalid assistant-parent references.
  • Kept only two new batch-safety tests: cross-chunk sequence/commit-before-notify behavior and whole-batch rollback without notifications. Existing fork and SSE tests cover isolation and transport compatibility.
  • Core and CLI focused suites: 130 passing tests after test reduction. Earlier extension fork/SSE suites: 29 passing tests. Package/workspace typechecks, extension build, lint (no errors), annotations, facade guard, and extension unused-export checks passed during implementation.

Forked large synthetic session with copied tool output intact

@kilo-code-bot

kilo-code-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (14 files)
  • .changeset/fast-session-forks.md
  • packages/core/src/event.ts
  • packages/core/src/kilocode/event-batch.ts
  • packages/core/test/kilocode/event-batch.test.ts
  • packages/core/test/session-runner-tool-events.test.ts
  • packages/kilo-vscode/src/services/cli-backend/sdk-sse-adapter.ts
  • packages/kilo-vscode/tests/unit/sdk-sse-adapter.test.ts
  • packages/opencode/src/event-v2-bridge.ts
  • packages/opencode/src/kilocode/event-v2-bridge.ts
  • packages/opencode/src/kilocode/event-wire.ts
  • packages/opencode/src/kilocode/session/fork.ts
  • packages/opencode/src/server/routes/instance/httpapi/handlers/global.ts
  • packages/opencode/src/session/session.ts
  • packages/opencode/test/kilocode/server/httpapi-global-sse.test.ts

Reviewed by grok-4.6 · Input: 329K · Output: 21.1K · Cached: 895.1K

Review guidance: REVIEW.md from base branch main

@marius-kilocode
marius-kilocode merged commit 7af9ee5 into main Sep 1, 2026
34 checks passed
@marius-kilocode
marius-kilocode deleted the optimize-tab-agent-forking-performance branch September 1, 2026 12:38
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