Skip to content

feat(kit): extract the apps' JSON-RPC runtime into kit/jsonrpc and kit/rpctest - #4

Merged
bketelsen merged 8 commits into
kit/newfrom
kit/extract
Oct 19, 2026
Merged

bketelsen merged 8 commits into
kit/newfrom
kit/extract

Conversation

@bketelsen

@bketelsen bketelsen commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #1 (kit/new). This PR is the rest of roadmap Phase 1: the RPC machinery frostnotes and frostmail share moves into the kit. I compared frostnotes at m4/tabswitcher (4babfb7) with frostmail at lodge/desktop-app (6b85307).

What's in the kit now

  • kit/jsonrpc, the runtime, generic over each app's own ErrorCode (C) and caller kind (K):

    • Server[C]: line framing (16 MiB), gating on the hello (rpc.hello, run inline so a pipelined call sees the connection ready), concurrent dispatch (32 in flight). A panic answers internal ("request panicked" is logged with the method and stack), and a reply over the limit answers internal. Serve takes a per-connection context hook, and ServeConn takes connections that lodge's /api or a gateway already authenticated.
    • Conn, reached with ConnFrom(ctx): SetReady/Client, Notify, AfterReply, Go, OnClose and Close. This covers what each app's rpcserver did by hand for rpc.hello and events.subscribe.
    • Router[K, C]: callers per method, plus an Admit hook (notes refuses writes under a read scope with it). An unknown method, a caller the method doesn't admit, and a refusal all answer methodNotFound alike.
    • Client[C], DecodeParams[C], Error[C], Frame[C], the event envelope, and Listen. Listen makes the private socket (dir 0700, socket 0600, stale socket replaced, same uid only), and it is Linux only.
  • kit/rpctest: Serve on a temporary private socket, Dial, Pipe (a connection served as another caller) and Stop. Options[S] holds frostmail's Services func(*S) hook. Building the engine stays in each app's own rpctest.

  • ADR-0012 (Proposed): tools/rpcgen and frostmail's Windows pipe stay in the apps. The two generators differ in what a schema means:

    • a method's callers narrow its domain's (notes) or replace them (mail);
    • a domain without callers is required to list them (notes) or defaults to local and session (mail);
    • the caller kinds come from Go (notes) or from a schema enum (mail);
    • only notes has writes and the vaultId rule;
    • the initialisms and the TypeScript output differ.

    Because the runtime is generic, neither generator changes.

  • The test app's /api now runs on jsonrpc, behind lodgeapp, and is called with the kit's client through kittest.

Proof that adoption is mostly deletions

I rebuilt each app's hand-written api runtime and internal/rpcserver on the kit in a scratch module. Each sat next to that app's real, unmodified zz_generated.go and internal/events, and both compile and vet. frostmail's scope tests pass. The files are in the coordination notes folder (kit-extract-adoption/) for the app sessions to start from.

before after
notes api/api.go + client.go 345 lines 121
notes internal/rpcserver/server.go 334 141
mail api/api.go + client.go 332 116
mail internal/rpcserver/server.go 326 171

Migration note (what an app changes)

  1. go get github.com/frostyard/lodge/kit@<this SHA>.

  2. api/api.go:

    • keep type ErrorCode int; the generated constants and String still name it;
    • type Error = jsonrpc.Error[ErrorCode], and Errorf calls jsonrpc.Errorf;
    • type Event = jsonrpc.Event, type EventEnvelope = jsonrpc.EventEnvelope, and NewEnvelope calls the kit's;
    • type Router struct{ *jsonrpc.Router[Caller, ErrorCode] }, where notes' K is Caller and mail's is CallerKind;
    • NewRouter builds it with RouterOptions{Caller: …}. Notes also passes Admit: !rt.Writes || mayWrite(ctx);
    • keep the unexported handle(…), which calls r.Handle(method, jsonrpc.Route{…}). Mail passes Callers: methodCallers[method];
    • decodeParams calls jsonrpc.DecodeParams[ErrorCode];
    • delete the rest.
  3. api/client.go:

    • type Client struct{ *jsonrpc.Client[ErrorCode] }, and NewClient wraps jsonrpc.NewClient[ErrorCode];
    • Frame, MaxMessageSize, EventBuffer, EventMethod and ErrEventsOverflow alias the kit's;
    • keep Dial;
    • delete the read loop and Call.
  4. api/conn.go: keep Conn, the callers, scope and identity. ConnFrom falls back to jsonrpc.ConnFrom(ctx) wrapped as a Conn, whose Notify(Event) sends an envelope.

  5. internal/rpcserver:

    • the connection loop, write, call and reply are deleted;
    • New makes jsonrpc.NewServer[api.ErrorCode](ServerOptions{Hello: "rpc.hello"});
    • Serve and ServeConn put the caller and a small per-connection state in the context;
    • Hello calls jsonrpc.ConnFrom(ctx).SetReady(p.Client);
    • Subscribe registers OnClose(sub.Close) and AfterReply(→ Conn.Go(sub.Run → Conn.Notify));
    • Listen and checkPeer become jsonrpc.Listen(path, log), and frostmail keeps listen_windows.go.
  6. internal/rpctest:

    • use rpctest.Serve for the socket, the goroutine and the stop with its 5 s bound;
    • use run.Pipe for DialAs;
    • embed rpctest.Options[api.Services] and call o.Edit(&services) before NewRouter. That is frostmail's form. Notes replaces its per-domain fields with it.
  7. Not changed:

    • zz_generated.go, tools/rpcgen, the schemas and the frontends;
    • every api.X used outside api/.

    Two client error strings no longer name the daemon: write and malformed-message. The connect error, in Dial, keeps its name.

Gates

make check and make race cross pass in both modules. The repocheck import rule still holds: kit/ imports neither tailscale nor lodge internals.

🤖 Generated with Claude Code

bketelsen and others added 5 commits October 19, 2026 00:49
The private socket needs it too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Extracted from frostnotes' and frostmail's api runtime and rpcserver,
generic over each app's generated ErrorCode and caller kind, so the
apps' generated code sits on it unchanged:

- Server: line framing, gating on the hello, concurrent dispatch, a
  panic answering internal, a reply over the message limit answering
  internal, and Conn for hello, notifications, AfterReply, Go and
  OnClose;
- Router: caller kinds per method, an Admit hook for writes under a read
  scope, methodNotFound alike for all three;
- Client, DecodeParams, the event envelope, and Listen for the private
  socket (same uid only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Serve on a temporary private socket, Dial, Pipe a connection served as
another caller, Stop; Options[S] carries frostmail's Services hook.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tools/rpcgen and frostmail's Windows pipe stay in the apps; the roadmap,
overview and map say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bketelsen and others added 2 commits October 19, 2026 01:06
Brings in the Linux-only app socket fix (845a109).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ListenWith(path, ListenOptions{Name: "notesd"}) says "notesd is already
listening on PATH", and NewClientWith(conn, ClientOptions{Name}) says
"notesd: connection closed", "write to notesd: …" and so on, as the
apps' own copies did. Listen and NewClient keep today's text. Additive
only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bketelsen
bketelsen marked this pull request as ready for review October 19, 2026 13:19
@bketelsen
bketelsen merged commit 81f9af5 into kit/new Oct 19, 2026
@bketelsen
bketelsen deleted the kit/extract branch October 19, 2026 13: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