Skip to content

feat: shared broker-core for both brokers (0.3.0) - #2

Merged
cycle-five merged 9 commits into
masterfrom
shared-core
Nov 19, 2026
Merged

cycle-five merged 9 commits into
masterfrom
shared-core

Conversation

@cycle-five

Copy link
Copy Markdown
Member

What this is

discord-broker and github-broker were the same program written twice, and in two weeks the copies had drifted: one had hardening the other lacked. This pull request puts everything both use into one package, packages/core (broker-core), so it exists once. It is step two of docs/superpowers/specs/2026-11-19-brokers-monorepo-design.md; step one (#1) was the workspace layout.

Released as 0.3.0. It deploys over 0.2.0 with no migration: same Workers, URLs, KV namespaces and secrets, and nobody signs in again.

Design

Core holds only what both brokers use today; nothing is shaped for a third.

In broker-core Comes from
handleMcp (the MCP server), taking the tool list and a call function src/mcp.ts, byte-identical in both
tool types, validateArgs, truncate, ToolInputError top of each tools.ts
consent and error pages (CSP, no-store, escaping) both auth.ts
the sign-in flow: handleAuthorize, handleCallback, PKCE, behind a SignInProvider interface both auth.ts
createBroker: OAuth roles, routes, per-request allowlist re-check, top-level error catch both index.ts
json and auditedFeed for the internal Feed entrypoint both feed.ts
VERSION, publicUrl, required, base64url; test support (MemoryKV, the cloudflare:workers stub) both

Each broker keeps its API client, its tools and callTool, its Feed routes, and a small src/signin.ts implementing SignInProvider (authorize URL, code exchange and token revocation, allowlist, config check). A broker's index.ts is now one createBroker({...}) call plus export { Feed }. Core exports TypeScript source; wrangler bundles it from the workspace, so there is no build step.

The sign-in flow and router were taken from Discord's code, the more complete of the two, and moved rather than rewritten.

Behaviour that changes, on purpose

GitHub broker gains what Discord already had:

  • DISABLE_CIMD="true" makes clients register instead of using a client ID metadata document; an unfetchable metadata document gets an explanatory 502 page.
  • A config check before anyone is sent to GitHub: missing client ID, empty allowlist or missing client secret is explained on a 500 page.
  • An unexpected exception is answered with a logged 500 instead of crashing the Worker.
  • Log lines authorize_error, authorize_exception, sign_in_cancelled, config_error, oauth_endpoint, unhandled.

Discord broker:

  • Feed no longer echoes an exception's message in the response body. The status is kept (the upstream status for a Discord API error, otherwise 500), the body is generic, the detail goes to the log line. A missing bot token is answered the same way.
  • Feed's audit line gains method: { event, method, path, status, ms }, as GitHub's already was.
  • Feed's JSON answers send Content-Type: application/json; charset=utf-8 (was without the charset).

Both:

  • Grants issued from now on store the whole props object as display metadata ({ id, username } / { login, id }; was { username } / { login }). The props themselves are unchanged.
  • GitHub's sign_in and sign_in_refused log lines carry id as a string.
  • One VERSION in core, checked by a test against every package.json.

What does not change

  • No MCP tool: names, schemas, output and audit fields are as they were.
  • Stored sign-ins: the props shapes, scope names (discord, github), issuer, resource, endpoint paths and token lifetime are identical. The final review ran both versions against one shared KV store, for both brokers: a token and a refresh token issued by 0.2.0 work on this branch with the same props; removing the user from the allowlist refuses the old token; and tokens issued by this branch work on 0.2.0, so a rollback is safe.
  • mailroom's contract with Feed: every path, status code and non-exception answer shape.

Checked

  • npm run typecheck clean; tests: broker-core 48, discord-broker 65, github-broker 51.
  • Each broker's flow.test.ts, which drives the real Worker entry through the real OAuth library, passes with nothing changed in it except the shared MemoryKV import and tests added.
  • wrangler deploy --dry-run builds both bundles with core included.
  • Per-task reviews, then a whole-branch review; its one fix wave was tests and documentation only.

After merge

Deploy all four Workers, verify (banners, OAuth metadata against the saved 0.2.0 metadata, a tool call through the already-connected connectors, mailroom's polling), then tag v0.3.0 and release.

🤖 Generated with Claude Code

https://claude.ai/code/session_017zqkE8grD6nh1bNnb2r1X8

Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
…nd the error catch

Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
…ng-token answer

Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
… weaker assertions from final review

Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
…to broker-core

Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
@cycle-five
cycle-five merged commit 2ee3e4e into master Nov 19, 2026
1 check passed
@cycle-five
cycle-five deleted the shared-core branch November 19, 2026 22:54
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