Skip to content

WCI-2309 fix(sdk-api): slim walk, concurrent batches, staged-count gate - #10197

Open
vibhavgo wants to merge 1 commit into
masterfrom
WCI-2309-sdk-slim-rotation-fetch
Open

vibhavgo wants to merge 1 commit into
masterfrom
WCI-2309-sdk-slim-rotation-fetch

Conversation

@vibhavgo

@vibhavgo vibhavgo commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Speeds up and hardens the changePassword rotation flow for heavy accounts (Rain-scale: ~1,100 keys, ~131 MB payload): slim envelopeOnly key walk with pipelined pages, per-entry payload sizing (no full stringify), 4-way concurrent batch uploads, and an expectedKeychainCount coverage gate so a partial or TTL-expired staging set can never silently flip the password.

Linear: WCI-2309

Changes

  • sdk-core keychains: ListKeychainOptions.envelopeOnly opt-in (envelopeOnly=true query param); the rotation walk uses the slim list and pipelines the next page fetch while the current page's crypto runs.
  • sdk-api computeChangePasswordPayloadSize: per-entry byte sizing replaces JSON.stringify of the whole payload (RangeError above V8's max string length on heavy accounts).
  • sdk-api processKeychainPasswordUpdatesInBatches: sequential batch PUTs → 4-way cursor-dispatched worker pool. Exactly-once dispatch; per-bin retry and error strings unchanged in isolation; fail-loud on terminal error (workers stop pulling new bins, in-flight siblings abandon their remaining retries, first error rethrown).
  • sdk-api batching-path final POST gains expectedKeychainCount (total V1+V2 keys staged); the server (BitGo/bitgo-microservices#64777) verifies it against the staged set before applying the password change.
  • Staging A/B measured (same egress IP, 131.5 MB fixture): batching 13.1 → 3.1 min (4.18×), total flow 17.9 → 7.6 min, final merge POST 200 in 28.5 s.

Test Plan

  • modules/sdk-core: npx mocha test/unit/bitgo/keychain/keychains.ts — envelopeOnly forwarding, slim-walk rotation, pipelining, old-server fallback.
  • modules/sdk-api: npx mocha test/unit/bitgoAPI.ts — payload sizing; pool: concurrency cap (peak in-flight 4), exactly-once dispatch, per-bin retry, stop-on-terminal-error; expectedKeychainCount present on the batching POST (2 / 0) and absent on the legacy POST.
  • End-to-end staging harness validation recorded in WCI-2309 comments (505c66bd, 047a259e).

@linear-code

linear-code Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

WCI-2309

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Compatibility checks are failing on Node.js 27-nightly (experimental, non-blocking). This does not block merge, but it signals an incompatibility to fix before this release becomes supported.

View run

@vibhavgo
vibhavgo force-pushed the WCI-2309-sdk-slim-rotation-fetch branch from f42c9be to 19d1f5a Compare November 18, 2026 10:49
@vibhavgo
vibhavgo marked this pull request as ready for review November 18, 2026 11:06
@vibhavgo
vibhavgo requested review from a team as code owners November 18, 2026 11:06

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@vibhavgo
vibhavgo force-pushed the WCI-2309-sdk-slim-rotation-fetch branch from 19d1f5a to cef7392 Compare November 19, 2026 04:26
@vibhavgo

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @vibhavgo's task in 20s —— View job


Review of #10197

The change looks correct overall. I found no blocking bugs. I only read the diff and did not run the tests or build.

keychains.ts (list and the pipelined walk)

  • envelopeOnly is validated like limit, prevId and safeId. false and undefined leave the query unchanged, so the change is backward compatible.
  • The prefetch ordering is sound. The next fetch starts after totalCount is read and before page N is processed. Records are still processed in page order, so progress-event order stays deterministic.
  • fetch.catch(_.noop) avoids an unhandled rejection. The original promise is still awaited on the next iteration, so the error still surfaces. If page N's processing throws, the orphaned prefetch is swallowed instead of crashing the process.
  • Nits:
    • fetch shadows the global fetch. A name like pending would avoid the lint and readability hit.
    • A failure mid-page leaves one extra request in flight. That is harmless, but worth knowing if the server counts these calls.

bitgoAPI.ts (per-entry payload sizing)

  • computeChangePasswordPayloadSize fixes the real RangeError from stringifying the whole payload. It reuses the same Buffer.byteLength idiom as packKeychainsFFD (line 2529), so there is no new browser-compat concern.
  • Minor: the estimate leaves out JSON framing (quotes, colons and commas, about 6 bytes per entry) and any string escaping. It undercounts slightly. With about 500 keys that is only a few KB. This matters only if a payload lands within a KB of the server's batching threshold. Adding a constant of 6 bytes per entry would make it a tighter upper bound.
  • The old code counted UTF-16 length. The new code counts UTF-8 bytes. This is more accurate, but it is a small behaviour change.

Scope and process

  • This PR bundles two changes: the slim-list opt-in and prefetch (WCI-2309), and the payload-sizing fix (footer Ticket: WCN-4788). The PR title and body describe only the first. Please mention the second in the description, or split it out so it can be reverted independently.
  • The test plan says "32/32 passing" for sdk-core. The sdk-api test changes (+48) are described as only "79 passing, unchanged". Please confirm the new sizing tests are included in that count.
  • The PR has the DO NOT MERGE label, which is consistent with the note that the server PR must ship first.
  • A Node 27-nightly compatibility check failed. It is non-blocking, and I did not look at whether it is related to this PR.

Comment thread modules/sdk-api/src/bitgoAPI.ts Resolved
@vibhavgo
vibhavgo force-pushed the WCI-2309-sdk-slim-rotation-fetch branch 2 times, most recently from 574ad58 to 23a3576 Compare November 25, 2026 10:46
@vibhavgo vibhavgo changed the title WCI-2309 fix(sdk-core): opt rotation walk into slim key list WCI-2309 fix(sdk-api): slim walk, concurrent batches, staged-count gate Nov 25, 2026
@vibhavgo

Copy link
Copy Markdown
Contributor Author

@claude review always

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @vibhavgo's task in 26s —— View job


Review of #10197 (commit 23a3576)

I read the diff and the surrounding code. I did not run the tests or the build. One issue should be fixed before merge. The rest are nits.

Should fix: computeChangePasswordPayloadSize undercounts (modules/sdk-api/src/bitgoAPI.ts:2584-2596)

  • The helper sums only Buffer.byteLength(id) + Buffer.byteLength(value) for each entry. It leaves out the JSON scaffolding ("id":"value",, about 6 bytes per entry). It also leaves out the backslash JSON adds for every " inside an encryptedPrv envelope. Envelopes are JSON strings, so they contain many quotes.
  • The old code sent JSON.stringify(params).length, which was exact. Now an account whose real body is just over the server's batching threshold can report a size just under it. isBatchingFlowEnabled then comes back false. The client makes the single legacy POST with the full oversized body, which can be rejected. That account was routed to batching before this change.
  • The docstring says an approximation is sufficient. That holds only if the approximation never falls below the real size. It should err high, since over-batching is harmless and under-batching is not.
  • Suggested fix: add 6 bytes per entry plus the escape count for each value, or call JSON.stringify(value).length per entry. The per-entry call keeps the cost bounded and avoids the whole-payload string. For an upper bound that is even simpler, use Buffer.byteLength(JSON.stringify(value)).
  • Please add a test where the real serialized length sits within a few KB of the threshold and the reported size is not below it.

Correctness checks that passed

  • Worker pool: nextBin++ runs on a single-threaded event loop, so each bin is dispatched exactly once. firstError is checked both before pulling a bin and in the retry loop. A bin that abandons its retries because of a sibling's failure returns silently, and the original error is still rethrown after Promise.all. Per-bin error strings are unchanged. workerCount = min(4, binCount) is safe because the empty-bins guard sits above it.
  • expectedKeychainCount: it is sent only on the batching POST, and it counts V1 plus V2 keys, which matches what is staged. The legacy POST is unchanged. The server check (bitgo-microservices#64777) has to ship first, and the DO NOT MERGE label covers that.
  • Prefetch (keychains.ts): fetch.catch(_.noop) stops an unhandled rejection. The original promise is still awaited on the next iteration, so errors still surface. prevId is only added when set, so the first request is the same as before. Page processing order is preserved.
  • envelopeOnly: it is validated like the other options, and false or undefined leaves the query unchanged. The docstring says old servers return full keychains. That relies on the rotation loop reading only fields present in both shapes. The old-server fallback test covers this.

Nits

  • const fetch in startFetch shadows the global fetch. Renaming it to page or pending avoids the lint and readability problem.
  • If page N's processing throws, one prefetched request is still in flight and its result is dropped. This is harmless but worth knowing if the server counts those requests.
  • The BATCH_CONCURRENCY = 4 constant is declared inside the method. A module-level constant would make it easier to find and test.
  • The batching POST still returns after workers finish even if the process is mid-retry on a sibling. That is fine because the error is rethrown. A comment saying so would help the next reader.

Scope and process

  • The Node 27-nightly compatibility failure is non-blocking. I did not check whether it relates to this PR.
  • The earlier review noted that the payload-sizing fix has its own ticket (WCN-4788). The PR description now lists it, but the commit footer has only Ticket: WCI-2309. Please make the footer match the description.

Not covered here: the @claude review always subscription is a repository-side setting. Nothing in this run changes it.
· branch WCI-2309-sdk-slim-rotation-fetch

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still open from earlier reviews (1):

If you have decided not to act on one of these findings, resolve its thread (a reply alone leaves it open) and the next review stops counting it. To review this commit again now, use Re-run on its "Claude Code Review" check.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🟣 modules/sdk-api/src/bitgoAPI.ts — Pre-existing: a batch upload that fails for good never reaches the caller; heavy accounts get a misleading legacy-POST failure instead of the Batch N failed error. The catch (error) {} at bitgoAPI.ts:2250 swallows what processKeychainPasswordUpdatesInBatches rethrows. useBatchingFlow then stays false, and the code falls through to the full-payload POST at 2275. Fix: catch only failures of the checkBatchingPasswordFlow GET, and rethrow errors from the batch upload, so the first-error contract at 2696-2699 holds for callers.

    Why this was flagged

    An account big enough that checkBatchingPasswordFlow returns isBatchingFlowEnabled hits a bin that fails all 3 retries, or whose PUT response lists failed keychains (bitgoAPI.ts:2660-2669). The new worker pool stores that error and rethrows it at bitgoAPI.ts:2697-2698, and its comment says this is the 'same fail-loud contract as the sequential loop'. But the call at bitgoAPI.ts:2242 sits inside the try whose catch (error) { // batching flow check failed } (2250-2252) drops every error. useBatchingFlow stays false, and changePassword sends the full updatePasswordParams body in the legacy POST at 2275. This change exists to let multi-hundred-MB accounts be sized without a full stringify (2230-2231). For those accounts, superagent's JSON serialization of that body throws the same V8 RangeError, or the server rejects the oversized body. The caller never sees the batch error. The base has the same swallow, so this is pre-existing, but the diff's new rethrow path has no effect in production.

    Verification: pre-existing. Trigger: /user/checkBatchingPasswordFlow returns isBatchingFlowEnabled, and then one bin fails terminally. The catch (error) { // batching flow check failed } at bitgoAPI.ts:2250-2252 throws away every error, including the firstError rethrown at 2696-2698. Control falls through to the full-payload legacy POST at 2275. The base commit has the same try/catch.

Slim envelopeOnly walk with pipelined pages, per-entry payload sizing, 4-way concurrent
batch uploads, and an expectedKeychainCount coverage gate on the batching final POST so a
partial or TTL-expired staging set fails loudly instead of flipping the password on
incomplete keychains.

Ticket: WCI-2309
@vibhavgo
vibhavgo force-pushed the WCI-2309-sdk-slim-rotation-fetch branch from 23a3576 to 8d73ef0 Compare November 25, 2026 13:42

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no new issues

No new issues were found in this update; 1 finding from earlier reviews is still open above.

Still open from earlier reviews (1):

If you have decided not to act on one of these findings, resolve its thread (a reply alone leaves it open) and the next review stops counting it. To review this commit again now, use Re-run on its "Claude Code Review" check.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants