Skip to content

perf(server): linked calls reuse sessions while a tool waits - #26567

Merged
maria-rcks merged 2 commits into
pingdotgg:mainfrom
Adamulek123:perf/linked-mcp-session-reuse
Oct 31, 2026
Merged

maria-rcks merged 2 commits into
pingdotgg:mainfrom
Adamulek123:perf/linked-mcp-session-reuse

Conversation

@Adamulek123

Copy link
Copy Markdown
Contributor

Problem

A linked-environment command can repeat MCP initialization while an earlier call on the same peer is still waiting for its response. PeerMcpClient.call saves the initialized session only after the first tools/call POST returns response headers. A held thread wait therefore leaves a completed, usable handshake unavailable to other calls.

The real T3 MCP peer test reproduces this: once the first held tool has entered its handler, a second command sends three authenticated POSTs—initialize, initialized notification, and tool call—instead of using the session for one tool POST.

Change

Publish the session immediately after openSession finishes its initialized notification, before sending the first tool call. Session lifetime already extends beyond an individual tool request, so cancellation of that request also leaves the initialized session available.

The existing missing-session (HTTP 404) renewal remains in place. Each call still resolves its peer and carries its own authentication and mode-limit header. This is a change in the shared server MCP client; web, desktop, mobile, providers and scheduled callers keep their existing paths. No UI, wire contract, route ordering or provider adapter changes.

Scope and approval

Submitted as a small, focused fix for the existing session-reuse defect: a completed handshake was hidden until a tool response arrived. It moves cache publication earlier without adding a cache, locking or a setting. This is a proposed application of the obvious-defect exception, not a claim of prior maintainer approval or an exemption.

This is independent of #26551's failed-address route ordering. Simultaneous calls that arrive during initialization can still create separate sessions; this PR addresses calls arriving after initialization completes.

Verification

  • From apps/server: vp test run src/peer/PeerMcpClient.test.ts src/peer/PeerLinks.test.ts src/peer/PeerForwarding.test.ts — 20 tests pass across three files.
  • The new parameterized test, reuses an initialized session before its first call finishes, holds the first tool at an explicit Deferred milestone. The next call uses one authenticated POST, applies its narrower mode limits, and preserves the original caller's limits. The cancellation case verifies reuse after the first caller stops. Both cases fail on upstream main with three POSTs instead of one.
  • renews a missing peer session once and reuses its replacement checks the 404 renewal and subsequent session reuse through the real client with a strict transport fixture.
  • Existing peer-link tests cover revocation, expiry, changed identity, mode limits and disabled linked environments. Targeted format/lint, git diff --check, and server typecheck pass. Independent source review found no blocking issue.

Current-pin performance experiment: real PeerMcpClient over loopback HTTP to a strict JSON-RPC fixture with 25 ms per-response delay. PeerLinks.resolve and outcome recording are stubbed, so route probes, SQLite and secret reads are excluded. Counts are recorded before fixture response generation; every result and caller's mode limits are checked. Twelve samples per workload/version, six in each process, with main/incumbent and reversed incumbent/main order. Baseline is upstream 125b61954dbba5822a8108c8ca47a4846b4b4328; the published source preserves f13ca86b8b with that main merged.

Workload Main median [range], ms PR median [range], ms Change Requests per measured call
Second call while first tool is held 127.26 [110.33, 131.92] 36.48 [33.40, 38.34] -71.34% 3 to 1
First call 132.36 [113.65, 160.62] 130.96 [116.81, 153.05] -1.06% 3 to 3
Warm call 35.79 [33.18, 48.76] 35.82 [33.19, 46.64] +0.09% 1 to 1
Eight simultaneous cold calls, all complete 115.17 [99.71, 131.03] 115.86 [101.69, 132.26] +0.60% 24 to 24 per burst
Eight unavailable-session calls, all fail 40.07 [36.59, 42.79] 41.27 [37.99, 54.24] +3.00% (+1.20 ms) 8 to 8 per burst

In the held-first workload, JSON request/response bodies fall from 297/253 bytes to 87/123 bytes per measured call. These counts exclude HTTP/TCP/TLS and tunnel overhead. The production MCP peer test also establishes the three-to-one request reduction without the modeled response delay; its one-off timings are not used as latency evidence.

This is controlled service timing on Windows, Node 24.18.0 and Effect 4.0.5. It does not establish a 71% UI or real-tunnel speedup. Retained-heap snapshots vary with runtime/transport state, so no whole-app memory or CPU saving is claimed. An initialized session can now remain cached after first-call cancellation or transport failure where it previously remained uncached. There are no new cache keys or extra synchronization objects. Real relay/device/browser timing was not measured. This backend change has no UI screenshot.

Model: gpt-6.1-sol through Codex in T3 Code.

@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:XS 0-9 changed lines (additions + deletions). labels Oct 28, 2026
@macroscopeapp

ghost commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at ddf6924

Macroscope's review found this PR approvable — Initialized peer sessions become reusable while a tool call is still waiting or after cancellation. Existing per-request authentication, mode limits, and stale-session recovery remain unchanged.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

ghost commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 125b619 and ddf6924.

Files selected for processing: 2
  • apps/server/src/peer/PeerMcpClient.test.ts
  • apps/server/src/peer/PeerMcpClient.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

The peer MCP client now caches a newly opened session before sending a tool request. Tests cover reuse while a call is pending, mode limits, and reuse of a replacement session after a missing-session response.

Changes: Peer MCP session lifecycle

Layer / File(s) Summary
Cache sessions and validate reuse and renewal
apps/server/src/peer/PeerMcpClient.ts, apps/server/src/peer/PeerMcpClient.test.ts
The client stores a newly opened session before sending the tool request. Tests cover reuse during a pending or completed call, requested mode limits, and reuse of a replacement session after a 404 response.

Priority: Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • pingdotgg/t3code#26344: Sends linked-only requests through PeerMcpClient and includes a watch that can remain pending while another call reuses the initialized session.
  • pingdotgg/t3code#23834: Allows MCP requests over ACP while another MCP call is pending, using a different transport implementation.

Suggested labels: bugfix, size:M

Suggested reviewers: juliusmarminge, macroscopeapp

Merge Risk: Minimal · up to ddf69

The session-reuse change is mergeable on the supplied evidence; no concrete blocking failure is established.

Pre-merge checks: Passed 4
Check name Status Explanation
Description check Passed The description includes the required Problem, Change, Scope and approval, and Verification sections. It explains the defect, the implementation, scope limits, approval rationale, focused tests, obser…
Title check Passed The title clearly and concisely describes the main change: linked calls reuse MCP sessions while an earlier tool call is waiting.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description includes the required Problem, Change, Scope and approval, and Verification sections. It explains the defect, the implementation, scope limits, approval rationale, focused tests, observed results, and known limitations.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

ghost commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Note

Written by gpt-6.1-sol on behalf of Maria

Verified in a real dev environment

verified as-is at ddf6924c57f7d67b13a765a85f5e2b30d14a7659 on this linux boat vm. baseline was github/main at 6575ce32508bd7908444241f3719aa33aaf77f90, in a separate worktree. installed each checkout with vp i; ran only one dev runner at a time, plus a regular isolated source server as the real linked peer.

created a project pointing to a real git checkout through the web app. drove chrome with the repo's playwright dependency. two real codex gpt-6.1-sol turns ran terminal commands and completed on the peer. no mocked peer, tool responses, fixtures, or demo data.

sent concurrent mcp calls through the running source server's production forwarding/client path. a transparent proxy counted actual peer requests without recording credentials:

main, while the first forwarded t3_thread_wait was pending:
  concurrent read: initialize -> notifications/initialized -> tools/call
  3 posts; new session
pr, same scenario:
  concurrent read: tools/call
  1 post; same session as the pending wait
pr, after aborting the waiter:
  read: 1 post; same session

a real codex gpt-6.1-sol turn submitted in the pr's web app also called t3_thread_read against the linked environment. the peer received one post on the cached session; the client displayed the completed remote thread and its final message.

nearby behavior passed: an approval-required/plan caller could read, while its attempted send to the full-access thread was rejected with runtime_mode_escalation_denied. after restarting the real peer while retaining the source cache, the old session returned http 404; the client initialized a replacement, completed the read, and reused the replacement for the next one-post read.

blacksmith ran vp i && vp test run apps/server/src/peer/PeerMcpClient.test.ts apps/server/src/peer/PeerLinks.test.ts apps/server/src/peer/PeerForwarding.test.ts: 20 tests passed across 3 files. existing head ci showed 31 successful, none pending or failing. no source edits or pushes were needed.

short runtime transcript

real codex turn in the pr web app reading the linked thread

completed linked thread result displayed in the pr web app

Not verified

native desktop or ios/android clients, other providers, and wan/relay/tunnel links were not exercised on this linux vm. no whole-app latency, cpu, or memory improvement is claimed; the measured change is the real peer request count. there is no visual code change requiring matching before/after screenshots. the recording was trimmed to remove pairing/loading and inspected. the uploaded mp4 playback and seeking passed in t3's browser; the same clip is embedded above as a gif for github rendering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS 0-9 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants