Repository navigation
mcpx: per-call 'call' frames in the exec stream (#1927 Approach B item 2) - #2615
Conversation
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.
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| for _, t := range s.reg.Tools([]string{server}) { | ||
| if t.Tool == tool { | ||
| d := destructiveHint(t.Annotations) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
💡 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".
| r.mu.Lock() | ||
| sink, ok := r.sinks[runID] | ||
| r.mu.Unlock() |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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
Implements Approach B item 2 of #1927 (queue task 3222): a per-call
callframe kind in the exec stream.Docs first:
docs/exec.mdframe order now placescall{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 —
CallRegistrymaps run id → the executing stream's sink;RunWithregisters its already-serialisedemitFramefor the run's duration and unregisters on return.ReportCallbuilds 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
ExecServiceand both/v1/callsurfaces (handleCalland the path-per-tool handler). AfterRegistry.Call, the handler reports the call with the run id from the call's context (x-mcpx-run), resolvingdestructiveHintfrom 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.
Need help on this PR? Tag
@codesmith-botwith what you need.