Repository navigation
refactor(rpc): one typed contract for every RPC call - #917
Merged
Merged
Conversation
…CostTotals in the api contract
…nd providers services in the api contract
Every event is an api.Event[T]; the backend emits it with events.Send and the window reads it with backend.Decode, so both compile against one payload type. The golden lists the events and shows RevertResult's wire shape.
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The RPC had no contract a compiler could check. A client named a method by string and passed
...any, the backend matched the arguments by position through reflection, and the window's types were hand-written mirrors of the backend's. Two strings swapped in a call compiled, and a JSON tag renamed on one side broke the other at run time.Every method is now declared once, in a new public package,
api:rpc.Handle(d, api.ThemesSelect, fn)only compiles whenfntakes and returns those types. The routes live ininternal/routes, one function per service. Reflection,Registerand thedenyInternallist are gone: a method nothing routes cannot be reached, so the methods kept out for Go callers stay out by construction (the package doc names them).lich,lich mcpand the window callcallMethod(ctx, c, api.X, opts). It is the only way either client posts, so a call by name no longer exists to get wrong.[options]or[]), so alichof another release still talks to this backend, and a route reads an empty list as the zero options./eventsevent is anapi.Event[T](api/events.go, payloads inapi/eventpayloads.go). The backend emits withevents.Send(emit, api.SessionTitleEvent, payload), which only compiles with that payload type, and the window reads withbackend.Decode(ev, api.SessionTitleEvent), which refuses an event of another name. Names and payloads on the wire are unchanged.native/backendaliasesapi's types for both the RPC and the events. The one deliberate exception is the footer'ssession-usagereader, which stays field by field on purpose: an incomplete report is not a report, and an unreadable cost reads as absent.api/testdata/contract.goldenrecords every method's name, options and result, and every event's name and payload, with each JSON field. A type that encodes itself shows the struct itsMarshalJSONwrites (RevertResult:patch,indexPatchas base64), checked against what it really writes. A change to the contract shows up there in review (go test ./api -update), andTestEveryRequestIsOneObjectkeeps positional arguments out.ci.ymlgets anAPI contractstep. A PR that moves the contract without the golden fails there by name, with the golden's diff in the log.native/lichclientis renamed tonative/backend.Test plan
api: the golden, one struct per request, the envelopeArgswrites, a duplicate declaration refused.api: an event family names one event per session; a duplicate event refused; the wire shape of a type with its ownMarshalJSONis what it writes.internal/events:Sendnames the event and carries its payload,Noneas no data.native/: every event the window takes is read throughbackend.Decode; its tests and fakes useapi's payloads.API contractstep: run locally against a golden with one field renamed (fails, prints the diff) and against the real one (passes).internal/rpc: options decoded by name, an empty list as zero options,Noneanswered asnull, anything but one object refused with 400, the service's error as 500, the request's context handed over, an unknown method as 404.internal/routes: every methodapideclares has a route, per service, and everyxdgprofileprovider has its profile methods declared.internal/cli/wire_test.goposts as a client of another release through the production routes.native/: the window's tests and fakes read the options objects.lich version,sessions,theme list,theme switch,open,worktreesandlich mcp(list_sessions,list_worktrees) answer; the backend log shows no refused call.gofmt,go vet ./...,go test ./...on the backend and innative/; backend coverage 90.5%.