Skip to content

M4 Phase 2: callers, the agent's scope, attribution and relocation - #45

Merged
bketelsen merged 10 commits into
mainfrom
m4/phase2
Oct 19, 2026
Merged

bketelsen merged 10 commits into
mainfrom
m4/phase2

Conversation

@bketelsen

@bketelsen bketelsen commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

M4 plan 0006 Phase 2: the contracts, the agent's scope, attribution by connection, and relocation. Needs nothing from lodge. Built overnight at your request, for review. The first of three stacked PRs (Phase 3 and Phase 5 follow on top).

#44 is merged, so this PR's base is main.

What changes

  • The generator (tools/rpcgen):
    • A method may list its own callers, which must narrow its domain's.
    • A method may be marked writes.
    • Both reach the Go router, the TypeScript tables (METHOD_CALLERS, WRITES, CallerKind) and rpc-api.md.
    • agent joins the caller kinds. A method an agent may call must name its vault in a required int vaultId, and the generated handler checks it against the connection's scope.
  • The schema follows lodge-app.md's table:
    • note admits local, session, agent, with 10 methods marked writes.
    • vault and hub admit session. vault.add, vault.remove, hub.serve, hub.approve and hub.setURL stay local.
    • New methods: rpc.ping, and rpc.hello's caller, login, displayName and node.
    • New methods: hub.setURL and hub.status.url, log.hub, and sync.relocate.
  • Scope (api): Identity, Scope and ParseScope.
    • Router.Dispatch answers methodNotFound to a method marked writes under a read scope.
    • A scoped call that names another vault answers notFound, in the same words as for a vault that doesn't exist.
    • An agent with no scope reaches nothing (fails closed).
  • Attribution (engine): a session's writes are editor / <Lodge-Node-Name> (web), and an agent's are agent / its token's name, whatever the params say. local connections keep the params.
  • rpcserver.ServeConn takes the connection's api.Identity, and rpc.hello reports it.
  • Relocation:
    • The hub records a URL (migration 0004_hub_url.sql).
    • log.hub answers {hub, url?}. A device asks at each connection and moves its row to a URL that differs, with the same token and sync state. An M2 hub answers methodNotFound, and the device stays.
    • POST /sync/hub answers {hub, url, proof}: an HMAC-SHA256 keyed with the token's SHA-256, over frostnotes relocate\n + hub ID + \n + url + \n + nonce.
    • sync.relocate keeps the new URL only when the hub ID, the recorded URL and the proof all check out. The token is never sent.
  • The mock answers as caller session (MockOptions.caller), for the web app's tests.
  • Docs: sync-protocol.md and lodge-app.md changed to match the code, and plan 0006's status line.

Tests

  • rpcserver: every method sent as each caller kind gets methodNotFound exactly where the spec's table says, with the answer for a method that doesn't exist.
  • engine, scope:
    • every note method on another vault, under read and write scopes;
    • every method marked writes under a read scope;
    • an agent with no scope.
  • engine, attribution: every note method that records a version, as session, agent and local (moves through the links they rewrite).
  • hub:
    • setURL and log.hub;
    • the proof, which depends on the token and the URL;
    • 404, 409, 400 and 413, with the 1 KiB limit tested at and past it;
    • a browser's Origin;
    • SameURL.
  • syncer:
    • a device follows log.hub to the new URL and syncs there;
    • sync.relocate refuses a relay that forwards the nonce, the same hub at a URL it doesn't record, another hub ID, a forged proof, a hub with no URL and an unpaired hub, then accepts the real one and syncs.
  • rpcgen: narrowing, repeats, an agent method without a vault, and the generated output.
  • api: ParseScope at and past its edges.

Gates: make check and make ui-check (87 files, 730 tests) pass locally.

The card

  • T-0084: notesctl hub url URL and notesctl hub relocate URL NEW-URL. hub status now shows the recorded URL. Codex ran the card through make task and it passed verification on the first attempt. I checked the given test against a reference first, then deleted the reference, and reviewed the diff.

Not in this PR

  • notesd --lodge and the socket are Phase 3 (the next stacked PR).

Security review (overnight, an independent agent)

These were fixed on this branch after the review:

  • log.hub was followed without a proof. A device moved to whatever URL its hub recorded and sent its token there on its next connection. A typo in notesctl hub url, or a hub database someone changed, could therefore collect every device's token.
    • follow now checks the host at the new URL with the relocation proof first, through the same proven check sync.relocate uses, and stays otherwise.
    • Test: TestADeviceFollowsOnlyAProvenURL. lodge-app.md and sync-protocol.md now say so.
  • URL case: hub URLs keep their host in lower case, so URLs SameURL calls the same are equal strings.
  • Noise: POST /sync/hub logs at Debug.

Accepted as design and written into lodge-app.md:

  • the endpoint reveals which device IDs exist, the hub's ID and its recorded URL;
  • the proof's key is the stored token hash, so someone who can read the hub's database can make a valid proof.

The master session's security review

  • N5: POST /sync/hub no longer reveals which devices exist. An unknown or revoked device gets the same 200 answer as a paired one, with a proof under an HMAC of its ID and a random key of the hub's, which no token makes. Requests log at debug.

🤖 Generated with Claude Code

bketelsen and others added 8 commits October 18, 2026 22:58
A method may list callers that narrow its domain's, and be marked
writes. Both reach the Go router and the TypeScript tables
(METHOD_CALLERS, WRITES), and the reference shows them. agent joins
the caller kinds; a method an agent may call names its vault in a
required int vaultId, which the generated handler checks against the
connection's scope (ADR-0026, plan 0006 Phase 2).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- schema: callers and writes marks as specs/lodge-app.md lists them;
  rpc.ping, rpc.hello's caller and identity, hub.setURL and
  hub.status's url, log.hub and sync.relocate.
- api: CallerAgent, Identity and Scope. Router.Dispatch refuses a
  method marked writes under a read scope, and a scoped call that
  names another vault answers notFound, as for no vault.
- rpcserver: ServeConn takes the connection's identity; rpc.hello
  reports it. The engine records a session's writes as editor,
  "<node> (web)", and an agent's as agent, its token's name,
  whatever the params say.
- hub: the recorded URL (migration 0004), log.hub, and POST
  /sync/hub's relocation proof, an HMAC keyed with the token's hash.
- syncer: a device follows log.hub at each connection, and
  sync.relocate keeps a new URL only when the hub there proves itself.
- tests: every method as each caller kind, every note method under
  another vault and a read scope, every writing method's attribution,
  and relocation against a relay, a wrong hub, a URL that differs and a
  forged proof.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
MockOptions.caller makes the mock refuse what notesd refuses a browser
inside lodge (METHOD_CALLERS), report the session's login and node from
rpc.hello, answer rpc.ping, and name a session's writes "<node> (web)",
for the web app's tests (plan 0006 Phase 2).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sync-protocol.md gains POST /sync/hub, log.hub, sync.relocate,
hub.setURL and hub.status's url, and the rule that a device follows
log.hub at each connection; lodge-app.md says what hub.setURL accepts
and POST /sync/hub's 400 and 413; plan 0006 records the overnight
build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Task card T-0084, implemented by the executor model and verified by taskrun.
The loop can be syncing again by the time the test reads sync.status
after its quiet point.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bketelsen and others added 2 commits October 18, 2026 23:56
From the security review of the overnight build: a device moved to the
URL its hub recorded and sent its token there on the next connection, so
a mistyped notesctl hub url, or a hub database someone changed, could
collect every device's token. follow now checks the host at that URL
with the relocation proof first, as sync.relocate does (shared as
proven), and stays otherwise. Also: hub URLs keep their host in lower
case, so URLs SameURL calls the same are equal strings; POST /sync/hub
logs at debug; lodge-app.md says what the endpoint reveals and what the
proof's key allows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ired one

From the master's security review (N5): the endpoint answered 404 for a
device ID that does not exist, so any peer reaching /device/ could count
the paired devices. An unknown or revoked device now gets the same 200
answer, with a proof under an HMAC of its ID with a random key of the
hub's, which no token makes; the device's own check refuses it. Requests
are logged at debug.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bketelsen
bketelsen merged commit 044e51c into main Oct 19, 2026
4 checks passed
bketelsen added a commit that referenced this pull request Oct 19, 2026
@frostyard-machines
frostyard-machines deleted the m4/phase2 branch October 26, 2026 00:19
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