Skip to content

test(api): guard the response envelope, which no mock written from a type can catch - #746

Merged
thinkclay merged 1 commit into
mainfrom
claude/744-envelope
Sep 11, 2026
Merged

thinkclay merged 1 commit into
mainfrom
claude/744-envelope

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

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:

if (response.ok) {
  const data = await response.json()
  return data          // no unwrapping
}

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 to sdk-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 data key" is too loose. It classified PrerequisitesCheckResponse as wrapped — that type carries a data field among a dozen domain fields. A live probe of /v1/prerequisites/check returned { isComplete, hasName, …, data }, bare. An envelope is now data plus nothing but pagination/metadata keys.

2. 39 SDK methods across six resources unwrap the envelope themselves.

async retrieve(id: string): Promise<Job> {
  const res = await this.get<JobApiResponse>(`/v1/jobs/${id}`)
  return res.data
}

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

probe result
flip legal.getCurrent to an envelope fails: "GET /v1/legal — sdk says wrapped, route says bare"
add a baseline entry for a pair that agrees fails: "GET /v1/legal — now agrees; remove it"
both reverted 6/6 pass

Plus 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 --check and deno lint clean; the whole contract directory is 9/9.

The pointer bump

#743 should have carried #26's sdk pointer and did not. pnpm install runs prepare, which runs git 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.

…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]>
@github-actions

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


@thinkclay
thinkclay merged commit ac49dbd into main Sep 11, 2026
14 checks passed
@thinkclay
thinkclay deleted the claude/744-envelope branch September 11, 2026 16:42
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]>
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