Skip to content

Move Code Mode business logic into dispatch#72

Merged
jmagar merged 2 commits into
mainfrom
feat/code-mode-dispatch-refactor
May 25, 2026
Merged

Move Code Mode business logic into dispatch#72
jmagar merged 2 commits into
mainfrom
feat/code-mode-dispatch-refactor

Conversation

@jmagar
Copy link
Copy Markdown
Owner

@jmagar jmagar commented May 25, 2026

Summary

  • Move Code Mode execution/search/schema business logic into dispatch-owned CodeModeBroker
  • Keep MCP as adapter glue for auth/scope, catalog notifications, and MCP envelopes
  • Add native labby gateway code search|schema|exec CLI path using the same broker
  • Fix the full workspace test harness by disabling the duplicate labby bin test harness, keeping log_fmt tests in the lib harness, and stripping repo-local debug info that triggered LLVM OOM under the global Cargo config

Verification

  • cargo fmt --all -- --check
  • CARGO_BUILD_JOBS=1 cargo clippy --workspace --all-features -- -D warnings
  • CARGO_BUILD_JOBS=1 RUST_TEST_THREADS=1 cargo test --workspace --all-features -- --test-threads=1
  • cargo test --workspace --all-features -- --test-threads=1
  • Live local smoke: target/debug/labby --json gateway code exec with Promise.all over two read-only lab::gateway.gateway.servers calls through real configured upstream discovery

Harness Debugging Notes

  • The failing path was not a test failure: rustc/LLVM OOMed while compiling the labby test harness with -C debuginfo=1 from ~/.cargo/config.toml.
  • src/main.rs duplicated the app module tree, so cargo was also compiling a redundant bin test harness with nearly the same test set as the lib harness.
  • The repo-local .cargo/config.toml now overrides debug info to 0, and the bin target has test = false; the formatter tests that were only in the bin harness are covered through the lib harness.

Summary by CodeRabbit

  • New Features

    • Added native CLI support for Code Mode operations: lab gateway code search, lab gateway code schema, and lab gateway code exec for searching tools, fetching schemas, and executing sandboxed JavaScript.
    • Enhanced tool call handling to support concurrent promise-based execution within Code Mode.
    • Added explicit confirmation requirement for destructive Code Mode actions.
  • Bug Fixes

    • Fixed gateway test configuration.
  • Documentation

    • Expanded Code Mode implementation details and added CLI usage examples.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 25, 2026

Warning

Review limit reached

@jmagar, we couldn't start this review because you've used your available PR reviews for now.

Your plan includes 1 review of capacity. Refill in 10 minutes and 28 seconds.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more review capacity refills, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 96aa3cae-a6ba-488c-a1a0-b70515fd7aa7

📥 Commits

Reviewing files that changed from the base of the PR and between 3e4d479 and 4e7ca5d.

⛔ Files ignored due to path filters (10)
  • docs/generated/action-catalog.json is excluded by !**/generated/** and included by **/*
  • docs/generated/action-catalog.md is excluded by !**/generated/** and included by **/*
  • docs/generated/api-routes.json is excluded by !**/generated/** and included by **/*
  • docs/generated/api-routes.md is excluded by !**/generated/** and included by **/*
  • docs/generated/cli-help.md is excluded by !**/generated/** and included by **/*
  • docs/generated/mcp-help.json is excluded by !**/generated/** and included by **/*
  • docs/generated/mcp-help.md is excluded by !**/generated/** and included by **/*
  • docs/generated/openapi.json is excluded by !**/generated/** and included by **/*
  • docs/generated/service-catalog.json is excluded by !**/generated/** and included by **/*
  • docs/generated/service-catalog.md is excluded by !**/generated/** and included by **/*
📒 Files selected for processing (13)
  • .cargo/config.toml
  • crates/lab/Cargo.toml
  • crates/lab/src/api/services/gateway.rs
  • crates/lab/src/cli/gateway.rs
  • crates/lab/src/config.rs
  • crates/lab/src/dispatch/gateway/code_mode.rs
  • crates/lab/src/lib.rs
  • crates/lab/src/main.rs
  • crates/lab/src/mcp/CLAUDE.md
  • crates/lab/src/mcp/server.rs
  • crates/lab/src/node/ws_client.rs
  • docs/dev/DISPATCH.md
  • docs/services/GATEWAY.md
📝 Walkthrough

Walkthrough

This PR centralizes Code Mode logic into a dispatch-owned CodeModeBroker (search, schema, execute), rewrites the JS runner to use promise-based concurrent tool calls, exposes the broker via MCP and a new native CLI (lab gateway code), and updates tests and docs accordingly.

Changes

Code Mode Dispatch and Surface Integration

Layer / File(s) Summary
Broker structure and executor foundation
crates/lab/src/dispatch/gateway/code_mode.rs
Defines CodeModeCaller/CodeModeSurface, implements CodeModeBroker with search/schema/execute, adds pending_calls state and helpers for candidate sorting, process lifecycle, provenance injection, admin gating, and upstream error canonicalization.
Promise-based async code execution and runner IPC
crates/lab/src/dispatch/gateway/code_mode.rs
Runner callTool returns pending JS promises stored in pending_calls; the runner loop repeatedly runs jobs while the top-level promise is pending and settles promises from ToolResult/ToolError, supporting concurrent/out-of-order tool calls.
CLI surface for gateway code operations
crates/lab/src/cli/gateway.rs
Adds lab gateway code subcommands (search, schema, exec), argument types and parser tests, fast-path dispatch to run_gateway_code, builtin-schema upstream discovery exclusion, --file size limit (20KiB), and --code/--file mutual-exclusion validation.
MCP adapter wiring and dispatch
crates/lab/src/mcp/server.rs
Rewires code_search/code_schema/code_execute to call CodeModeBroker with auth-scoped CodeModeCaller and MCP CodeModeSurface; code_execute gains optional confirm for destructive actions; snapshots and notifies catalog changes around broker execution.
Broker, runner, and adapter test coverage
crates/lab/src/dispatch/gateway/code_mode.rs, crates/lab/src/mcp/server.rs, crates/lab/tests/code_mode_runner.rs
Adds unit tests for upstream error kind preservation, MCP destructive-action confirmation, gateway provenance overwrite; updates MCP tests to broker APIs; adds integration test that verifies Promise.all fan-out and out-of-order tool completion.
Documentation, component size refactor, and supporting updates
crates/lab/src/mcp/CLAUDE.md, docs/dev/DISPATCH.md, docs/services/GATEWAY.md, docs/superpowers/plans/2026-05-24-code-mode-dispatch-refactor.md, crates/lab/src/node/ws_client.rs, .cargo/config.toml, crates/lab/Cargo.toml, crates/lab/src/lib.rs, crates/lab/src/log_fmt.rs, crates/lab/src/api/services/gateway.rs
Clarifies Code Mode ownership and CLI examples, refactors component decoding to parameterized limits and updates tests, adjusts workspace Cargo/profile settings, adds log_fmt module and allow(dead_code), and updates a gateway test payload to include allow_stdio.

Possibly related PRs

  • jmagar/lab#71: Introduces initial Code Mode contract types and MCP code tools that the broker and dispatch wiring in this PR build upon.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

🐰 I hopped from MCP to dispatch's glade,
Where brokers hum and promise-calls are made.
Files bounded, schemas tidy, runner sings,
CLI and MCP now share the same wings.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Move Code Mode business logic into dispatch' directly and clearly summarizes the main change across multiple files: refactoring Code Mode logic from MCP into a new dispatch-owned CodeModeBroker component.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/code-mode-dispatch-refactor

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 and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/lab/src/dispatch/gateway/code_mode.rs`:
- Around line 1016-1029: The write_runner_input function currently writes JSON
to the ChildStdin but never flushes, which can leave data in user-space buffers;
after calling stdin.write_all(&line).await in write_runner_input, call
stdin.flush().await and map any flush errors to the same ToolError::Sdk shape
(same sdk_kind "internal_error" and similar message text like "failed to flush
Code Mode runner stdin: {err}") so the function returns an error on flush
failure; update the async function handling to return that mapped error.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f0b577af-2496-4182-a5b5-fadade7a9fec

📥 Commits

Reviewing files that changed from the base of the PR and between 9ace94d and abe12d0.

📒 Files selected for processing (10)
  • crates/lab/src/api/services/gateway.rs
  • crates/lab/src/cli/gateway.rs
  • crates/lab/src/dispatch/gateway/code_mode.rs
  • crates/lab/src/mcp/CLAUDE.md
  • crates/lab/src/mcp/server.rs
  • crates/lab/src/node/ws_client.rs
  • crates/lab/tests/code_mode_runner.rs
  • docs/dev/DISPATCH.md
  • docs/services/GATEWAY.md
  • docs/superpowers/plans/2026-05-24-code-mode-dispatch-refactor.md

Comment thread crates/lab/src/dispatch/gateway/code_mode.rs
Copy link
Copy Markdown

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

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: abe12d03a8

ℹ️ 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 crates/lab/src/mcp/server.rs
@jmagar jmagar force-pushed the feat/code-mode-dispatch-refactor branch 2 times, most recently from 572663c to 3e4d479 Compare May 25, 2026 02:31
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/lab/src/cli/gateway.rs`:
- Around line 760-771: The match arm for GatewayCodeCommand::Exec currently
enforces CODE_MODE_CLI_MAX_SOURCE_BYTES only for files; after you compute the
final code string (the local variable code in that arm), add the same guard:
check if code.as_bytes().len() > CODE_MODE_CLI_MAX_SOURCE_BYTES and, if so,
return an anyhow::bail!("Code Mode source file exceeds 20480 bytes") (or the
same message used for files). This ensures both the code branch and the file
branch share the same size validation.

In `@crates/lab/src/dispatch/gateway/code_mode.rs`:
- Around line 291-314: The schema handler currently only checks
caller.can_execute() which allows non-admin callers to fetch admin-only Lab
action schemas; modify schema to pass the caller into schema_for_lab_action
(call site: schema -> schema_for_lab_action(&parsed.raw, &service, &action,
surface, caller)) and update the schema_for_lab_action function to accept a
CodeModeCaller parameter and call caller.can_execute_action(entry, action_name)
(using the entry/service/action names available) before returning built-in
schemas; if can_execute_action returns false, return the same ToolError::Sdk
forbidden response as in schema, otherwise proceed to build and return the
schema.

In `@crates/lab/src/log_fmt.rs`:
- Line 1: Remove the module-level #![allow(dead_code)] in the log_fmt module and
instead apply item-level #[allow(dead_code)] to only the specific unused items
(e.g., helper structs/functions) so unused-warning suppression is precise; if
the blanket allow is temporary due to test harness restructuring, replace the
module attribute with a short comment explaining the temporary rationale and a
TODO to remove the suppression once the tests/binary linkage (e.g., formatter
helper types) are consolidated.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: d0679d6a-3560-4c3c-b3ef-ffec9e4f6b3f

📥 Commits

Reviewing files that changed from the base of the PR and between abe12d0 and 3e4d479.

⛔ Files ignored due to path filters (10)
  • docs/generated/action-catalog.json is excluded by !**/generated/** and included by **/*
  • docs/generated/action-catalog.md is excluded by !**/generated/** and included by **/*
  • docs/generated/api-routes.json is excluded by !**/generated/** and included by **/*
  • docs/generated/api-routes.md is excluded by !**/generated/** and included by **/*
  • docs/generated/cli-help.md is excluded by !**/generated/** and included by **/*
  • docs/generated/mcp-help.json is excluded by !**/generated/** and included by **/*
  • docs/generated/mcp-help.md is excluded by !**/generated/** and included by **/*
  • docs/generated/openapi.json is excluded by !**/generated/** and included by **/*
  • docs/generated/service-catalog.json is excluded by !**/generated/** and included by **/*
  • docs/generated/service-catalog.md is excluded by !**/generated/** and included by **/*
📒 Files selected for processing (14)
  • .cargo/config.toml
  • crates/lab/Cargo.toml
  • crates/lab/src/api/services/gateway.rs
  • crates/lab/src/cli/gateway.rs
  • crates/lab/src/dispatch/gateway/code_mode.rs
  • crates/lab/src/lib.rs
  • crates/lab/src/log_fmt.rs
  • crates/lab/src/mcp/CLAUDE.md
  • crates/lab/src/mcp/server.rs
  • crates/lab/src/node/ws_client.rs
  • crates/lab/tests/code_mode_runner.rs
  • docs/dev/DISPATCH.md
  • docs/services/GATEWAY.md
  • docs/superpowers/plans/2026-05-24-code-mode-dispatch-refactor.md

Comment thread crates/lab/src/cli/gateway.rs
Comment thread crates/lab/src/dispatch/gateway/code_mode.rs
Comment thread crates/lab/src/log_fmt.rs Outdated
jmagar added 2 commits May 24, 2026 23:10
Resolves review thread PRRT_kwDOR8nC1M6EcO3b

Resolves review thread PRRT_kwDOR8nC1M6EcO3e

Resolves review thread PRRT_kwDOR8nC1M6EcO3f

Resolves review thread PRRT_kwDOR8nC1M6Eb7ED

Resolves review thread PRRT_kwDOR8nC1M6Eb645
@jmagar jmagar force-pushed the feat/code-mode-dispatch-refactor branch from b56660b to 4e7ca5d Compare May 25, 2026 03:21
@jmagar jmagar merged commit d25a8af into main May 25, 2026
13 checks passed
@jmagar jmagar deleted the feat/code-mode-dispatch-refactor branch May 25, 2026 18:24
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