Skip to content

feat(daemon,proxy): serve checked RPC that refuses protocol skew before dispatch - #5144

Merged
thymikee merged 5 commits into
mainfrom
perf/checked-rpc-route
Nov 27, 2026
Merged

thymikee merged 5 commits into
mainfrom
perf/checked-rpc-route

Conversation

@thymikee

@thymikee thymikee commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds POST /rpc/checked (also /agent-device/rpc/checked) on the daemon and proxy so the server refuses protocol skew before dispatch, in place of the client's /health probe. Replaces closed #5134. The client opt-in is #5145.

  • Same auth, body and answer as /rpc. The x-agent-device-rpc-protocol header is mandatory.
  • Skew gets 409 plus x-agent-device-refusal (protocol-missing, protocol-mismatch, or upstream-protocol-mismatch through a proxy).
  • The proxy admits both routes through one path and forwards to the upstream checked route.
  • /health and DAEMON_CAPABILITIES list checked-rpc. Additive ledger entries, no protocol bump. ADR 0006 amended.

Security

  • The daemon verifies the transport credential (headers or JSON params.token) before it reads the protocol or method: a wrong credential always gets 401.
  • The 409 comes only after the auth hook, tenant and token gates pass; any earlier failure gets the ordinary /rpc rejection. A refused call reserves no request id.
  • Refusals echo only a bounded id and are built from local values; the proxy never copies an upstream refusal's body or headers, and never follows a redirect on the checked route.
  • No state on disk, no resend.

Before / after

No observable change until a client calls the route. Released servers answer it with 404 before dispatch.

Validation

Head 1633e10. Ordering, id, reservation, rebuild and redirect tests fail without their fixes (checked by mutation). Proxy and daemon checked tests, test/wire-compat, eager-closure budgets, the layering scan and fallow audit pass locally. CI pending.

🤖 Generated with Claude Code

…re dispatch

POST /rpc/checked takes the same auth, body and answer as /rpc. After
authentication, the daemon and the proxy compare the mandatory
x-agent-device-rpc-protocol header with their own protocol before they
decode anything that protocol defines, and refuse with 409 plus a typed
x-agent-device-refusal header. The proxy admits both routes through one
path, checks its own protocol, and forwards to the daemon's checked
route. /health advertises checked-rpc. Additive: no protocol bump.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee
thymikee added this pull request to stack #5146 November 27, 2026 07:34
@thymikee thymikee changed the title perf/checked rpc route feat(daemon,proxy): serve checked RPC that refuses protocol skew before dispatch Nov 27, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.65 MB 6.66 MB +4.0 kB
Package (unpacked) 6.65 MB 6.65 MB +4.0 kB
Package (download) 2.03 MB 2.03 MB +1.5 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 18.5 ms 19.1 ms +0.6 ms
CLI --help 40.6 ms 43.9 ms +3.3 ms

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 10 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread packages/contracts/src/daemon-http.ts Resolved
Comment thread packages/proxy/src/daemon-proxy.ts Resolved
Comment thread packages/proxy/src/daemon-proxy.ts Resolved
Comment thread website/docs/docs/error-reasons.md Resolved
Comment thread packages/contracts/src/daemon-http.ts Resolved
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging 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.

Apex Sentinel: 2 new findings on 861ac01. The verdict and the full list are in the summary comment.

thymikee and others added 2 commits November 27, 2026 08:48
…protocol refusal

The daemon now runs a checked call through the /rpc flow and answers
the protocol refusal after the auth hook, tenant-trust and token gates,
with the same inputs. A call this build cannot decode is refused with
no version. The proxy authenticates, then checks the protocol, then the
envelope. Refusal errors are built by their owners; contracts keep data,
so daemon-http.ts gains no eager kernel edge. The unreachable
script-artifacts refusal is gone: every checked-rpc daemon serves
script artifacts. checked-rpc joins DAEMON_CAPABILITIES.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 8efd3fb, the new checked route has documentation and request handling problems to fix before merge.

The public docs now give a wrong closed list of proxy routes. README.md:10 and remote-proxy.md:331 omit POST /rpc/checked and its prefixed form /agent-device/rpc/checked, but the proxy admits and forwards it. A gateway allowlist built from these lists rejects checked requests while proxy /health advertises checked-rpc. The method rule for tenants (README.md:185, remote-proxy.md:290) also names only /rpc. Every sentence that lists the proxy routes or the RPC admission rules should match what isRpcRoute and isSupportedDaemonRoute admit, including the protocol header requirement.

A second independent pass found that the daemon can answer a checked request before it verifies the configured transport credential (http-server.ts:334). With a wrong bearer credential, a protocol mismatch gets a 409, an unknown method gets a 404, and invalid params or a missing command name also return early. Can every checked request verify the transport credential before it compares the protocol or decodes method params, so that a wrong credential always gets 401? Credential extraction should not depend on the method decoder, and legacy /rpc ordering should stay as is.

A second independent pass found that a checked request with skew can get the protocol 409 when the auth hook never completes successfully (http-server.ts:383). This covers a hook that throws or rejects its promise, and requests that never reach the hook because of the envelope, params, command name, or tracking registration. Can the protocol refusal be sent only after the hook and tenant checks succeed, with a safe non-protocol rejection in every other case?

A second independent pass found that the daemon echoes an unchecked request id in protocol refusals (http-checked-rpc.ts:66). An object, a private URL, or a very large string comes back in the 409, including on the unauthenticated path. Can every protocol refusal use null unless the id matches a bounded, safe correlation format?

A second independent pass found the same echo in the proxy (checked-rpc-admission.ts:28): the raw id reaches the 409 before envelope validation, limited only by the general request size. Can the proxy apply the same bounded id rule to the checked 409, without changing legacy /rpc responses?

A second independent pass found that a recognized upstream protocol 409 is forwarded with its whole body and copied header values (daemon-proxy.ts:393). An internal URL in the error body or in x-request-id then reaches the caller. Can the proxy build this 409 from the recognized reason, a small set of validated and bounded diagnostic values, and locally generated headers?

A second independent pass found that the outbound checked request follows redirects by default (daemon-proxy.ts:385). A same-origin 307 or 308 to /rpc resends the POST with the daemon credentials on the unchecked route, so a daemon with a different protocol can run the command. Can checked requests disable redirect following, and can custom transports be required to keep that rule? A test should show that such a redirect never reaches /rpc.

A second independent pass found that a checked call that will be refused still reserves the caller's meta.requestId while the async auth hook runs (http-server.ts:632). A valid /rpc call with the same ID then fails with duplicate_request_id. Can the daemon build the hook context for a protocol refusal without reserving that ID, and register only checked calls that can continue to execution?

Not blocking: take or leave a shared refusal builder in contracts for the near-identical daemon and proxy code, a fix to the ADR claim that proxy authentication does not decode the body (the default reads params.token, and README.md:168 could note that authenticate may get a non-JSON body on the checked route), and a single exit for the daemon's pre-auth refusals so a new one cannot skip the protocol 409.

None of the earlier review threads still apply, so they can be resolved: capability is now kept; the hook now sees the real request; the ADR section is now placed correctly; the envelope check now follows auth and protocol; the refusal now follows the /rpc auth order; the comment was rewritten, with the rest covered by the ADR note above; the error reason wording is fixed; the parameter type is now narrowed.

Every CI job except Body rules passed at this commit. Body rules was cancelled during toolchain setup, before the body check ran, so it has no result. That check reads only the PR body, so the cancellation is likely unrelated to the code. It failed two rules on an earlier commit, so the current body has not been shown to pass.

Not verified here: local tests and gates were not run. No mutation run confirmed that each ordering test fails if the protocol refusal moves before the hook, although the new route tests should fail on the base with a 404. A #5145 client against a proxy in front of an older daemon was not checked end to end; that behavior belongs to the client PR.

Before merge, fix the route lists and tenant method rule, address the questions above, and get a completed Body rules run.

…usals from local values

- The daemon checks a checked call's transport credential, read from the
  headers or the JSON frame, before it compares the protocol or decodes
  the method, so a wrong credential always gets 401.
- The protocol refusal comes only after the auth hook, tenant and token
  gates pass; every earlier failure, a throwing hook included, gets the
  ordinary /rpc rejection. A refused call never reserves its request id.
- Refusals echo only a bounded id, and are built by one data-only
  builder in contracts. The proxy rebuilds an upstream protocol refusal
  from validated, bounded values and never copies its body or headers.
- The proxy never follows a redirect on the checked route; custom
  transports must honor request.redirect.
- Proxy README and remote-proxy docs name /rpc/checked, its prefixed
  form and the protocol header; the ADR states the new ordering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Thanks. Fixed in 7be2a52; each behavioural fix has a test that fails without it (checked by mutation).

  1. Proxy README and remote-proxy.md list /rpc/checked, its /agent-device/ form and the protocol header, and apply the tenant method rule to both routes.
  2. A checked call's transport credential is verified first, from the headers or the JSON frame's params.token (no method decoder). A wrong credential gets 401 even with skew, an unknown method or a missing command. /rpc ordering is unchanged.
  3. The protocol 409 comes only after the hook, tenant and token gates pass. A throwing hook, and envelope, params, command-name or tracking failures, get the ordinary /rpc rejection.
  4. Daemon and proxy refusals echo the id only when it is a short token ([A-Za-z0-9._:-]{1,128}) or a safe integer, else null. The rule is in contracts. /rpc is unchanged.
  5. The proxy rebuilds a recognized upstream protocol 409 from the reason, the validated and bounded service/version/protocol, and local headers. The upstream body, id and x-request-id never pass.
  6. Checked upstream requests use redirect: 'error'. The transport contract requires custom upstreamFetch to honor it, and the proxy refuses a 3xx or redirected answer. A real upstream answering 307 to /rpc is never followed.
  7. A call that will get the protocol refusal is shown to the hook with its tracking id stamped but not registered. A concurrent /rpc call with the same id succeeds.

Non-blocking: I took the shared builder as a data-only buildCheckedRpcProtocolRefusal in contracts; it adds no eager import and the budget passes. The ADR and README now say the default token check reads params.token and that authenticate may get a non-JSON body. The pre-auth refusal branches are gone, so no separate exit is needed.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 7be2a52, one diagnostic field in the proxy's rebuilt protocol 409 is still unbounded. Seven of the eight fixes requested earlier are in place. The request to bound the upstream refusal's diagnostic values is only partly met.

A second independent pass found that the version check limits the suffix but not the three numeric parts. On either checked proxy route, an authenticated, protocol-matched request can reach an upstream that returns a protocol-mismatch or upstream-protocol-mismatch 409. If that body names agent-device-daemon, protocol 99, and a version of 60,000 digits followed by .0.0, it fits under the 64 KiB read cap. The proxy then sends all 60,004 characters in its rebuilt 409. Every upstream string copied into that response should have a small explicit maximum, applied to the whole version string before the syntax test. When the check fails, the proxy should omit the diagnostic fields and still return the protocol 409. A test with an oversized numeric version should show that.

None of these review threads still apply, so they can be resolved: capability is listed; hook gets the decoded request; ADR text is placed correctly; envelope check follows auth and protocol; early token check exposes no version; comment and docs are updated; error reason wording is fixed; refusal parameter is typed narrowly; the named ADR text was rewritten; the code behind it had already changed.

All 22 CI checks pass at this commit, including Body rules.

Not verified here: local tests and mutation runs were not run. That each new test fails without its fix was judged from the test bodies. Rejection of redirects by the default fetch rests on Fetch semantics and the PR's real-server 307 test. A #5145 client against a proxy in front of an older daemon was not checked end to end; that belongs to the client PR.

Before merge, bound the version field and add its test.

…x check

A checked refusal's version is capped at the 64 characters the /health
reader allows before the syntax test, so a version of many digits never
reaches the proxy's rebuilt 409; the refusal keeps its reason without
the diagnostic fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Fixed in 1633e10. readCheckedRpcRefusalSide now rejects a version longer than 64 characters, the limit the /health reader uses, before the syntax test. In that case it returns nothing, so the proxy keeps the protocol 409 and its reason and drops the diagnostic fields. New test on the proxy checked route: an upstream 409 with a version of 60,000 digits plus .0.0. The rebuilt 409 carries only { reason, dispatched: 'no' } and stays under 2 KB. The test fails without the cap (checked by mutation).

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 1633e10

Since the review of 7be2a52: 0 new · 0 still open · 0 no longer raised

No open findings.

1 new finding away from what changed since 7be2a52 not shown. Comment @apex-sentinel-staging review for a full review.

Reviewed with this repository's own Sentinel configuration (7d392e62143f).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee

Copy link
Copy Markdown
Member Author

No blocking problems were found in 1633e10. The version length problem from the review of 7be2a52 is fixed: the whole version string is now capped at 64 characters before the syntax check. A proxy test covers this through the proxy route: an oversized version keeps the protocol 409 and drops the diagnostic fields.

These earlier threads no longer apply and can be resolved, since the changes since 7be2a52 leave their fixes in place: capability listed, hook gets decoded request, ADR text placed, envelope check ordered after auth and protocol, early token check exposes no version, comment and docs updated, error reason wording fixed, refusal parameter typed narrowly, ADR text rewritten, and code already changed at 7be2a52.

CI is green: all 22 checks pass at 1633e10, including the wire compatibility ledger and the new proxy test.

Not verified here: the new test and the mutation run reported for it were not executed locally. That the test fails without the cap is judged from the version pattern, the 64 KiB read limit and the test body. The thread texts were not re-read; their status rests on the check at 7be2a52 and on the first four commits being unchanged.

Nothing further is needed from this review before merge.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 27, 2026
@thymikee
thymikee merged commit baf4633 into main Nov 27, 2026
22 checks passed
@thymikee
thymikee deleted the perf/checked-rpc-route branch November 27, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant