Skip to content

feat(cli): --webhook-receiver-auto-tunnel - #681

Merged
bokelley merged 7 commits into
mainfrom
bokelley/webhook-reachability-doc
Apr 21, 2026
Merged

bokelley merged 7 commits into
mainfrom
bokelley/webhook-reachability-doc

Conversation

@bokelley

@bokelley bokelley commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Ships --webhook-receiver-auto-tunnel — autodetects ngrok or cloudflared on PATH (or $ADCP_WEBHOOK_TUNNEL="<cmd> {port}" override), spawns the tunnel pointed at the receiver, captures its public URL, plumbs it into proxy mode, and tears the tunnel down on exit / SIGINT / SIGTERM.

adcp storyboard run <remote-agent> webhook-emission --webhook-receiver-auto-tunnel

Follow-up to #679 (now merged), which exposed --webhook-receiver on the CLI but required operators to stand up their own tunnel manually.

Design rationale

Why not Socket Mode (a client asked for a Slack-style outbound-WebSocket delivery path). The webhook_receiver_runner test-kit parity invariant says "an agent that passes under loopback_mock MUST also pass under proxy_url — any divergence is a conformance bug." Both modes drive the agent's production HTTP emitter end-to-end: TLS, RFC 9421 signature math over the wire, idempotency_key across retries, body preservation, retry back-off. Swap HTTP for a WebSocket push and none of that gets exercised. Socket Mode is a delivery mechanism, not a conformance harness.

Why not bundle ngrok. webhook-receiver-runner.yaml says explicitly: "Operators MAY configure a tunnel as the proxy_url provider, but the runner MUST NOT require a specific tunnel vendor." Pinning ngrok violates that; also, free-tier caps + authtoken requirements make first-run compliance grading depend on a SaaS account. cloudflared, frp, bore, localtunnel are all legitimate alternatives.

Why auto-tunnel. HTTP on the wire is unchanged — parity invariant holds. Detection is PATH-based + env-var overridable, so no vendor lock. One flag subsumes manual tunnel setup; works offline if the binary is installed. The tradeoff is the binary prerequisite, which an actionable "install ngrok or cloudflared" error message covers. A hosted rendezvous service for graders that can't install a tunnel is tracked at adcontextprotocol/adcp#2618 (milestone 3.1.0) — separate because it requires hosting commitment and operational ownership that don't belong in a client-library PR.

Implementation notes (post-review)

  • URL capture is scoped precisely. ngrok anchors on its stable msg="started tunnel" logfmt line (vendor-domain-agnostic — catches .ngrok-free.dev, .ngrok-free.app, .ngrok.app, paid-tier and custom domains alike). Cloudflared matches *.trycloudflare.com. Custom templates must emit ADCP_TUNNEL_URL=<https://…> — "first https:// URL" would wire the tunnel to any docs URL the binary logs at startup.
  • Chunk-boundary-safe. Rolling 16 KiB buffer across stdout/stderr data events, so an anchor/URL straddling chunk boundaries still matches.
  • Cleanup survives double-signal. Module-level child registry + process.on (not once) + tunnelEscalating latch. SIGINT/SIGTERM: TERM → 250 ms grace → SIGKILL → process.exit. Second signal during grace: force-KILL immediately.
  • Conflict validation is unified. Any --webhook-receiver [mode] alongside auto-tunnel is rejected with a single message; auto-tunnel always implies proxy.
  • Dry-run still validates. Conflict checks fire in --dry-run; the tunnel spawn does not.

Verification

  • node --test test/lib/cli-webhook-receiver-flag.test.js → 20/20 pass (11 original + 9 new: conflicts, dry-run skip, missing binary, URL capture, bystander-URL rejection, startup timeout, exit-before-URL)
  • Full webhook-adjacent suite → 55/55 pass, no regressions
  • tsc --noEmit clean, prettier --check clean
  • Real-binary E2E: ngrok 3.37.3 (captured .ngrok-free.dev URL, storyboard ran, no zombie processes)
  • Real-binary E2E: cloudflared 2026.3.0 (captured trycloudflare.com URL, storyboard ran, no zombie processes)
  • Three expert-review rounds (code-reviewer + security-reviewer + ad-tech-protocol-expert). All blockers and mediums addressed; security review gave "ship it" on the tip.
  • CI (now that base is main): Test & Build, Code Quality, CodeQL, Security Audit, GitGuardian, commitlint, Publish Dry Run — all pass.

Files

  • bin/adcp.js — flag + detection/spawn/capture/cleanup helpers
  • test/lib/cli-webhook-receiver-flag.test.js — 9 new auto-tunnel cases with stubbed tunnel (CI doesn't depend on ngrok/cloudflared)
  • .changeset/webhook-receiver-auto-tunnel.md — minor bump

Follow-ups

🤖 Generated with Claude Code

@bokelley
bokelley force-pushed the bokelley/webhook-reachability-doc branch from c421912 to d7f5205 Compare April 21, 2026 01:43
@bokelley
bokelley changed the base branch from main to bokelley/webhook-receiver-cli April 21, 2026 01:43
@bokelley bokelley changed the title docs(proposals): webhook-grading reachability — tunnel vs sockets vs rendezvous feat(cli): --webhook-receiver-auto-tunnel + RFC 0001 rendezvous Apr 21, 2026
@bokelley
bokelley force-pushed the bokelley/webhook-reachability-doc branch from d7f5205 to 4ee82b4 Compare April 21, 2026 01:47
@bokelley bokelley changed the title feat(cli): --webhook-receiver-auto-tunnel + RFC 0001 rendezvous feat(cli): --webhook-receiver-auto-tunnel Apr 21, 2026
Base automatically changed from bokelley/webhook-receiver-cli to main April 21, 2026 09:34
bokelley and others added 7 commits April 21, 2026 05:35
…rendezvous

Design proposal responding to client feedback that webhook grading requires
a public IP. Rejects socket mode (breaks conformance parity with production
HTTP emitter) and ngrok-bundling (vendor lock + test-kit forbids it).
Proposes a --webhook-receiver-auto-tunnel flag as a short-term fix and an
AdCP-hosted rendezvous service as the long-term answer.

Follow-up to PR #679.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Autodetect ngrok/cloudflared on PATH, spawn the tunnel pointed at the
webhook receiver port, extract its public URL, and plumb it into proxy
mode. Cleans up on exit (including SIGINT/SIGTERM). HTTP on the wire —
the webhook_receiver_runner parity invariant (loopback_mock ≡ proxy_url)
is preserved.

ADCP_WEBHOOK_TUNNEL="<cmd> {port}" overrides detection with a custom
tunnel, which keeps the CLI vendor-agnostic per the test-kit's
"MUST NOT require a specific tunnel vendor" rule and doubles as the
hook for the test suite to stub a fake tunnel binary.

Conflict validation (public-url, loopback, multi-pass strategy) runs
before the dry-run gate so flag misuse fails fast; actual tunnel spawn
is deferred past dry-run to avoid wasting startup cost on previews.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…issue

Option A (auto-tunnel helper) is shipping in the companion commit on this
branch. Option B (hosted rendezvous) is a spec-stewardship initiative
with hosting, operational, and abuse-prevention commitments that belong
in the adcp spec repo, not as an in-repo RFC — now tracked at
adcontextprotocol/adcp#2618 (milestone 3.1.0).

Keeps the Socket Mode and ngrok-bundling rejection arguments in place
since they're the load-bearing reason the CLI is shaped the way it is.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…stricter conflict

Three substantive fixes from the expert reviews:

1. URL-capture scoping. ngrok regex now pins to known vendor domains
   (*.ngrok-free.app, *.ngrok.io, *.ngrok.dev, *.ngrok.app) instead of
   matching any `url=<https://…>` field, so a stray diagnostics URL in
   startup logs can't be mistaken for the forwarding URL. Custom templates
   now require an explicit `ADCP_TUNNEL_URL=<https://…>` marker on
   stdout/stderr — the "first https:// URL" heuristic wired tunnel
   destinations to docs links whenever the binary logged one at startup.

2. Persistent signal handlers. `process.once` on SIGINT/SIGTERM meant a
   second Ctrl-C bypassed tunnel cleanup and leaked listeners. Replaced
   with module-level handlers installed lazily on first tunnel spawn,
   backed by a shared child set, with a SIGTERM → SIGKILL grace period
   so tunnels that ignore TERM still get reaped.

3. Unified --webhook-receiver conflict. validateAutoTunnelArgs previously
   only rejected explicit `--webhook-receiver loopback`; a bare
   `--webhook-receiver` silently resolved to loopback_mock and was then
   overwritten by proxy_url, hiding operator intent. Now rejects any
   `--webhook-receiver` form alongside auto-tunnel with a single clear
   message (auto-tunnel always implies proxy mode).

Smaller fixes:
- ADCP_WEBHOOK_TUNNEL_TIMEOUT_MS validates via Number.isFinite + positive
  check; invalid/zero values warn and fall back to 15000 instead of
  silently swallowing garbage.
- runCli gains a timeout + SIGKILL on spawnSync so a regression where
  URL capture succeeds but the run hangs fails the test instead of
  stalling CI.
- New test: custom template ignores a bystander URL printed before the
  marker (regression for the "first https:// wins" class of bugs).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Same reasoning as the rendezvous RFC: design docs drift and don't
belong in the repo. The PR description captures the "why" durably
(Socket Mode rejection, ngrok-bundling rejection, auto-tunnel
selection) and the changeset now links straight to the rendezvous
issue instead of routing through a deleted file.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Found two issues when running against a real ngrok binary:

1. ngrok regex missed the live `.ngrok-free.dev` domain. The previous
   fix-commit vendor-domain allowlist (.ngrok-free.app, .ngrok.io,
   .ngrok.dev, .ngrok.app) excluded `.ngrok-free.dev`, which is ngrok's
   current free-tier subdomain after a mid-2024 migration. Rather than
   chase the vendor-domain list, anchor on the stable `msg="started
   tunnel"` log line ngrok emits exactly once per tunnel creation — the
   URL in its `url=` field is authoritative by definition, and the
   scoping still blocks stray `url=` fields elsewhere in the log stream
   from being mistaken for the forwarding URL.

2. SIGKILL escalation was dead code. `reapTunnelChildren` scheduled a
   `setTimeout` for the force-kill, but Node doesn't run timers after
   the `'exit'` event fires, and the SIGINT/SIGTERM handlers called
   `process.exit` synchronously right after `reapTunnelChildren` —
   cancelling the timer before it could fire. A tunnel that ignored
   TERM would never see KILL. Split the paths: `'exit'` sends TERM only
   (best-effort, OS reaps strays); SIGINT/SIGTERM send TERM, schedule
   a 250ms `setTimeout` that SIGKILLs survivors and calls
   `process.exit`. Also guard against a second signal during the
   escalation window by SIGKILLing immediately on the repeat.

Verified end-to-end against real ngrok 3.37.3: URL captured, storyboard
ran, no zombie tunnel processes after exit.

Plus small fixes: help-text clarifies the custom-template marker
contract ("first match wins").

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Code-review finding: the per-chunk regex scan could miss the ngrok URL if
ngrok's "started tunnel" logfmt line split across two `data` events.
Anchoring on the head of the line (`msg="started tunnel"` … `url=…`) made
line fragmentation a real failure mode where the prior vendor-domain-anchor
version was immune. Scan a rolling 16 KiB buffer across chunks instead of
each chunk in isolation.

Security-review finding (low): the SIGKILL escalation only signals the
direct child, so `ADCP_WEBHOOK_TUNNEL="sh -c 'ngrok …'"` would leave a
grandchild behind. Documented the constraint in the --help text rather
than adopting detached/PGID spawn semantics — the reviewer's pragmatic
call, and ngrok/cloudflared direct paths are unaffected.

Two nit-level cleanups: explicit `return` after `process.exit` in the
second-signal branch (defensive against refactors that swap exit for
something softer) and split the "why we don't unref the timer" comment
into its two separate mechanisms.

Verified end-to-end against real ngrok (ngrok-free.dev URL captured,
storyboard ran, no zombie processes).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley force-pushed the bokelley/webhook-reachability-doc branch from 4219aec to 5f73bf4 Compare April 21, 2026 09:35
@bokelley
bokelley merged commit c9977e5 into main Apr 21, 2026
13 checks passed
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