Repository navigation
test(api): guard the response envelope, which no mock written from a type can catch - #746
Merged
Merged
Conversation
3 tasks
…type can catch
Some routes wrap their success body in `{ data }` and some return it bare. The
http client returns the parsed body verbatim, so a method declaring
`Promise<UserProfile>` against a wrapping route actually resolves to
`{ data: UserProfile }`. TypeScript never notices, because the generic is
asserted — `this.get<UserProfile>(...)` — rather than inferred from the wire.
And the msw handlers are written from the declared types, so the suite agrees
with the type while the type disagrees with the API.
That combination has produced three wrong contracts a green suite certified:
Scaffald/sdk#21 (employers), #26 (organizations), #29 (getUser). The check has
to compare the route to the SDK, because comparing either to a fixture cannot
see it.
sdk-envelopes.test.ts parses both sides and fails on any pair that disagrees,
with a baseline enforced in both directions like its sibling — a new mismatch
fails, and so does a stale entry.
**11 genuine mismatches are baselined.** Which side moves is a public-API
decision (#744), not something to settle inside a test.
Getting that number right took three corrections, all of which are why the
guard looks the way it does:
* "has a top-level `data` key" is too loose. It classified
PrerequisitesCheckResponse as wrapped — that type has a `data` field among
a dozen domain fields — and a live probe of /v1/prerequisites/check
confirmed the body is bare. An envelope is `data` plus nothing but
pagination/metadata keys.
* 39 SDK methods across six resources unwrap the envelope themselves
(`const res = await this.get<X>(p); return res.data`). Their declared type
describes the inner payload and is correct *because* the route wraps.
Comparing their declared type to the route reported all of them as broken;
11 of my first 22 baseline entries were this false positive.
* `return c.json({ data }, 200)` is shorthand for `{ data: data }`. Requiring
a colon to read a key called two correct routes bare.
Verified by attacking it rather than trusting a green run: flipping
legal.getCurrent to an envelope fails with "sdk says wrapped, route says bare",
and adding a baseline entry for a pair that agrees fails with "now agrees;
remove it". Both pass again once reverted.
Also bumps the sdk pointer to pick up #28 and #29. #743 should have carried
`git submodule update --init --recursive` and reset the submodule back to the
recorded commit after I had checked out the merged one. So main has been
carrying the honest route schema from #743 with the SDK type that contradicts
it.
Refs #744
Co-Authored-By: Claude Opus 5 <[email protected]>
thinkclay
force-pushed
the
claude/744-envelope
branch
from
September 11, 2026 16:33
36b09e3 to
39febea
Compare
Contributor
📊 Performance Benchmark Results |
This was referenced Sep 11, 2026
thinkclay
added a commit
that referenced
this pull request
Sep 15, 2026
The guard asked `commits/{sha}/pulls` whether a commit belongs to a merged
PR and treated an empty list as proof of a direct push. That index is
eventually consistent and this workflow starts seconds after the merge, so
an empty list also means "not indexed yet".
#747 is what that looks like. PR #746 merged at 16:42:57Z; the guard ran at
16:43:00Z, three seconds later, read `[]`, and filed an issue against its own
squash commit. The same endpoint returns `#746` today. A backstop that can
accuse a properly-merged PR is a backstop people learn to ignore, which costs
more than the direct push it exists to catch.
Two checks now, ordered so the ordinary path stays instant:
1. `pulls/{n}.merge_commit_sha` — written as part of the merge, never stale.
Requiring it to equal this SHA is what makes reading the trailing `(#n)`
safe: a direct push can end its subject with any number, but only the real
merge commit of PR n matches.
2. The commit index as before, but polled (0/5/10/20/30s) instead of believed
on the first read. It still covers merges whose message carries no PR
number, and the waiting is only spent on commits that already failed (1).
Probed against real SHAs rather than trusting a green run:
real squash of #746 PASS (via referenced PR)
real squash of #721 PASS (via referenced PR)
spoofed (#746), fake sha FAIL -> would file an issue
real merge, no (#n) PASS (via commit index)
real commit, no (#n) PASS (via commit index)
The third is the one that matters: the `merge_commit_sha` equality is what
keeps check 1 from being a free pass for anyone who appends `(#746)`.
Closes #747
Co-authored-by: Claude Opus 5 <[email protected]>
18 of 23 tasks
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.
Closes the guard half of #744, and carries the sdk pointer bump #743 should have had.
The class of bug
Some routes wrap their success body in
{ data }; some return it bare. The http client returns the parsed body verbatim:So a method declaring
Promise<UserProfile>against a wrapping route actually resolves to{ data: UserProfile }. TypeScript never notices — the generic is asserted (this.get<UserProfile>(…)), not inferred from the wire. And the msw handlers are written from the declared types, so the suite agrees with the type while the type disagrees with the API.That has produced three wrong contracts a green suite certified — Scaffald/sdk#21 (employers), #26 (organizations), #29 (
getUser). The check has to compare the route to the SDK, because comparing either to a fixture cannot see it.What landed
sdk-envelopes.test.ts, sibling tosdk-routes.test.ts: parses both sides, fails on any pair that disagrees, baseline enforced in both directions.11 genuine mismatches are baselined. Which side moves is a public-API decision — that is what #744 is for, and it is not something to settle inside a test.
Getting to 11 took three corrections, and they are why the guard looks like this
1. "has a top-level
datakey" is too loose. It classifiedPrerequisitesCheckResponseas wrapped — that type carries adatafield among a dozen domain fields. A live probe of/v1/prerequisites/checkreturned{ isComplete, hasName, …, data }, bare. An envelope is nowdataplus nothing but pagination/metadata keys.2. 39 SDK methods across six resources unwrap the envelope themselves.
Their declared type describes the inner payload and is correct because the route wraps. Comparing declared type to route reported every one of them as broken — 11 of my first 22 baseline entries were this false positive.
3.
return c.json({ data }, 200)is shorthand for{ data: data }. Requiring a colon to read a key called two correct routes bare.Each of those would have shipped a guard that was confidently wrong, which is worse than no guard.
Verified by attacking it
legal.getCurrentto an envelopePlus three parser-sanity assertions, so a regression cannot turn the whole thing into a vacuous pass: route and SDK classification counts must stay plausible, and both conventions must remain represented — if the classifier ever collapsed to a constant it would otherwise report perfect agreement.
deno fmt --checkanddeno lintclean; the whole contract directory is 9/9.The pointer bump
#743 should have carried #26's sdk pointer and did not.
pnpm installrunsprepare, which runsgit submodule update --init --recursive— that reset the submodule back to the recorded commit after I had checked out the merged one, so the bump was never staged. Main has since been carrying the honest route schema from #743 alongside the SDK type that contradicts it. This picks up #28 and #29.