Skip to content

mcpx: per-call 'call' frames in the exec stream (#1927 Approach B item 2) - #2615

Merged
dezren39 merged 4 commits into
mainfrom
queue/3222-1927-call-frames
Oct 30, 2026
Merged

dezren39 merged 4 commits into
mainfrom
queue/3222-1927-call-frames

Conversation

@dezren39

@dezren39 dezren39 commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Implements Approach B item 2 of #1927 (queue task 3222): a per-call call frame kind in the exec stream.

Docs first: docs/exec.md frame order now places call{server,tool,argsHash,durationMs,ok,error?,destructiveHint?} in the interleaved group with log/emit/stdout, plus a paragraph defining the frame and when it can appear (runs the daemon itself executes).

Emission plumbing (new): execsvc gains a run-scoped sink registry — CallRegistry maps run id → the executing stream's sink; RunWith registers its already-serialised emitFrame for the run's duration and unregisters on return. ReportCall builds the frame; arguments travel as a sha256 hash (ArgsHash), never in clear. Calls for runs this process is not executing are dropped by design — there is no stream to join.

Daemon wiring: one registry is shared (lazily, like the task store) between ExecService and both /v1/call surfaces (handleCall and the path-per-tool handler). After Registry.Call, the handler reports the call with the run id from the call's context (x-mcpx-run), resolving destructiveHint from the tool's annotations under the same defaulting the confirmation path uses.

Tests: registry delivery/unregister/failure frames at unit level, and an end-to-end run that reports a call mid-run and checks the emitted frame sequence against the groups parsed out of docs/exec.md (documented vs emitted order). Targeted suites green on the laptop: execsvc, daemon (go test -count=1).

The rest of the #1927 ladder stays tracked on #1927.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

docs/exec.md first: the frame order now places call{server,tool,
argsHash,durationMs,ok,error?,destructiveHint?} in the interleaved
group with log/emit/stdout, with a paragraph on what a call frame is
and when one can appear.

execsvc gains the run-scoped sink registry the emission needed: a
CallRegistry maps run id to the sink of the stream executing that
run; RunWith registers its (already serialised) emitFrame for the
runs duration. ReportCall builds the frame -- arguments as a sha256
hash, never in clear -- and drops calls for runs nobody here is
executing.

The daemon shares one registry between ExecService and its call
handlers: both /v1/call surfaces report after Registry.Call, with
the destructiveHint resolved from the tools annotations under the
same defaulting the confirmation path uses.

Tests: registry delivery/unregister/failure frames, and an
end-to-end run whose emitted order is checked against the groups
parsed out of docs/exec.md.
@dezren39
dezren39 requested a review from a team as a code owner October 30, 2026 07:22
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 31 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b1870c52-9679-4d2c-89cb-3854782623e0
Commits

Reviewing files that changed from the base of the PR and between c8ee2a6 and adf346a.

Files selected for processing: 9
  • pkgs/mcpx/docs/exec.md
  • pkgs/mcpx/internal/codegen/emit.go
  • pkgs/mcpx/internal/daemon/call_frames_test.go
  • pkgs/mcpx/internal/daemon/input_required.go
  • pkgs/mcpx/internal/daemon/routes_exec.go
  • pkgs/mcpx/internal/daemon/routes_proto.go
  • pkgs/mcpx/internal/daemon/server.go
  • pkgs/mcpx/internal/execsvc/call_frames_test.go
  • pkgs/mcpx/internal/execsvc/execsvc.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T11:40:54.212452Z adf346a New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e505aa2c5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pkgs/mcpx/internal/daemon/server.go Outdated
start := time.Now()
res, err := s.reg.Call(ctx, req.Server, req.Tool, cc, args)
dur := time.Since(start).Truncate(time.Millisecond)
s.reportCallFrame(ctx, req.Server, req.Tool, args, start, err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Treat MCP isError results as failed call frames

When an upstream tool returns isError: true, Registry.Call returns its result envelope with a nil Go error, so this reporting call produces ok: true with no error even though the generated client throws ToolError. This makes failed tool invocations appear successful to stream consumers. Inspect the result's failure state when building the frame, as the path-per-tool response already does with resultFailed(res), and apply the same correction to that handler's reporting call.

AGENTS.md reference: pkgs/mcpx/AGENTS.md:L80-L80

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in aa5a242: reportCallFrame now takes the call result and, when the Go error is nil but resultFailed(res) reports isError, marks the frame failed with an isError error string — applied at both call sites (/v1/call and /v1/call/{server}/{tool}). Covered by TestReportCallFrameMarksIsErrorResultsFailed in internal/daemon/call_frames_test.go.

Comment thread pkgs/mcpx/internal/daemon/server.go Outdated
start := time.Now()
res, err := s.reg.Call(ctx, req.Server, req.Tool, cc, args)
dur := time.Since(start).Truncate(time.Millisecond)
s.reportCallFrame(ctx, req.Server, req.Tool, args, start, err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Emit call frames from the input-report execution path

For remote CLI runs with an elicitation broker configured, internal/cli/exec.go sets MCPX_INPUT=report, which the generated client sends as x-mcpx-input. These requests return through handleCallReporting at lines 1062–1064 before reaching this new reporting call. That handler executes Registry.CallReporting without emitting a call frame, so even ordinary successful tool calls from these daemon-executed runs are entirely absent from the stream. Report completed calls from that branch as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in aa5a242: handleCallReporting now calls reportCallFrame with the run context when CallReporting completes, so calls on the input-report path emit call frames too. Covered by TestHandleCallReportingEmitsCallFrame in internal/daemon/call_frames_test.go.

Comment thread pkgs/mcpx/internal/daemon/server.go Outdated
Comment on lines +1612 to +1614
for _, t := range s.reg.Tools([]string{server}) {
if t.Tool == tool {
d := destructiveHint(t.Annotations)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Resolve the exact server before selecting call annotations

When one server's namespace matches another server's name, Registry.Tools([]string{server}) can return tools from both servers because its filter accepts either name or namespace. For example, with server a using namespace b and server b using namespace c, a call to server b executes that server because Registry.Pool prioritizes exact names, but this loop can select server a's identically named tool first. Consequently, a destructive call can be reported with destructiveHint: false from an unrelated tool. Resolve the server identity using the same precedence as call dispatch and match that identity when selecting annotations.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in aa5a242: reportCallFrame now selects annotations with dispatch precedence — exact server name first (ToolInfo.Server), namespace only as fallback (ToolInfo.Namespace) — matching Registry.Pool. Covered by TestReportCallFrameResolvesExactServerBeforeNamespace, including the a-namespace-b / b-namespace-c collision.

- isError tool results now produce failed call frames (resultFailed),
  on both /v1/call and /v1/call/{server}/{tool}
- handleCallReporting reports completed calls, so exec runs whose
  calls ask questions still emit call frames
- destructiveHint annotations are resolved with dispatch precedence:
  exact server name before namespace, so a namespace that equals
  another server's name cannot lend it wrong annotations
- daemon tests for all three

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aa5a24297b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +276 to +278
r.mu.Lock()
sink, ok := r.sinks[runID]
r.mu.Unlock()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Drain in-flight call reports before closing the stream

When a script times out or is cancelled while a tool request is finishing, ReportCall can obtain this sink just before RunWith unregisters it and returns. Deleting the map entry does not invalidate the captured closure, so the reporter can subsequently emit a call after the terminal frames—or write through execStream's HTTP response writer after its handler has returned. The sink mutex only serializes writes; it does not prevent this lifetime race. Coordinate unregistering with in-flight reports and close call-frame delivery before emitting the terminal frames.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 78cf55a. CallRegistry sinks are now lifetime-tracked entries (callSink{closed, wg}): ReportCall counts itself in-flight under the registry lock, and unregister — now idempotent — closes the entry to new reports and waits for in-flight deliveries to finish. RunWith calls unregister explicitly after the run returns and before emitting any terminal (result/error/end) frames, with the defer kept only as a safety net. So a report already being delivered lands in order ahead of the terminal frames, a report that starts after the close is dropped, and nothing can write to the stream after end. Regression test: TestCallRegistryUnregisterDrainsInFlightReports (blocks a sink mid-delivery and asserts unregister waits, the frame lands, and later reports drop).

start := time.Now()
res, err := s.reg.Call(ctx, req.Server, req.Tool, cc, args)
dur := time.Since(start).Truncate(time.Millisecond)
s.reportCallFrame(ctx, req.Server, req.Tool, args, start, res, err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Include generated batch calls in per-call reporting

When a daemon-executed script uses the supported global batch(...) helper, its tool invocations produce no call frames. The generated helper in internal/codegen/emit.go:1330–1339 posts to /v1/batch without forwarding X-Mcpx-Run, and handleBatch invokes Registry.Call directly at server.go:1190, bypassing the newly instrumented single-call handlers. Consequently, stream consumers miss all calls made through either sequential or parallel batches. Forward the run ID from the helper and report each completed batch item as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 78cf55a. Two parts: (1) the generated batch() helper in internal/codegen/emit.go now forwards "x-mcpx-run": env("MCPX_RUN"), matching __call — handleBatch already places that header into the call context via withRun; (2) handleBatch's runOne now calls s.reportCallFrame(ctx, server, tool, args, start, res, err) for every completed item, so sequential and parallel batch items each produce a call frame (including the isError→failed mapping, via the shared reporting path). Pre-dispatch validation failures stay unreported, matching the single-call handler's behaviour. Regression test: TestHandleBatchEmitsCallFrames.

…(Codex round 2 on #2615)

CallRegistry sinks are now lifetime-tracked entries: unregister closes an entry to new reports and waits for in-flight deliveries, and RunWith closes call delivery explicitly before its terminal frames, so a call frame can never land after result/end. handleBatch reports each completed item via reportCallFrame and the generated batch() helper forwards x-mcpx-run, so batch calls appear in the exec stream like single calls.
…frames

# Conflicts:
#	pkgs/mcpx/docs/exec.md
#	pkgs/mcpx/internal/daemon/routes_exec.go
#	pkgs/mcpx/internal/execsvc/execsvc.go
@dezren39
dezren39 merged commit d37f225 into main Oct 30, 2026
5 checks passed
@dezren39
dezren39 deleted the queue/3222-1927-call-frames branch October 30, 2026 11:39
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.

1 participant