Repository navigation
perf(browser): clear cookies in one bulk call that excludes the Google family (STA-4065) - #14131
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughCookie cleanup now receives the target Electron session. When no cookies are excluded, it attempts bulk clearing with protected origins excluded. If bulk clearing fails, it uses per-cookie removal and rollback. Tests cover preservation, failures, rollback, empty jars, unaddressable cookies, concurrency, and removal ordering. Partition fixtures record unfiltered cookie names and verify partitioned, ordinary, and excluded cookies before cleanup. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/browser/browser-cookie-import-policy.ts (1)
204-222: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick winUse
session.clearDatawithexcludeOriginsfor the bulk path.The only call site excludes
google.com; useexcludeOrigins: ['https://google.com']withdataTypes: ['cookies']. This covers the registrable domain and its subdomains without relying oncookies.get({})to expose every partitioned cookie.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 84dc691d-fbc4-4b73-89ea-dd348143f3be
📒 Files selected for processing (4)
src/main/browser/browser-cookie-import-partition.electron.test.tssrc/main/browser/browser-cookie-import-policy.test.tssrc/main/browser/browser-cookie-import-policy.tssrc/main/browser/browser-cookie-import.ts
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e4667756-83b0-41d6-86be-a3e77a512ffc
📒 Files selected for processing (2)
src/main/browser/browser-cookie-import-policy.test.tssrc/main/browser/browser-cookie-import-policy.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/main/browser/browser-cookie-import-policy.test.ts
…A-4065) PR #14086 replaced the bulk clear with one Electron cookies.remove() per removable cookie so partitioned excluded cookies survive an import. That is required whenever something must be preserved, but it also runs when nothing is excluded, where a single clearStorageData is equivalent and far cheaper: 421ms -> 7.3ms at 1k cookies and 1764ms -> 20.1ms at 3k on Electron 43. The fast path is gated on the same unfiltered cookies.get() the exclusion predicate already reads, which an Electron fixture now proves returns partitioned cookies, so "no exclusions in the snapshot" means "nothing in the jar to preserve".
…rData switch The mocked session still exposed clearStorageData, so asserting it was never called could no longer fail once the fast path moved to clearData.
The per-cookie path's rollback is being removed by STA-4061; the comment only needs to say the fallback re-removes what survived.
The bulk fast path clears a jar with no excluded cookie in one call, which would have made every assertion in the partition-rollback fixture vacuous. Give it a live Google cookie so the partial-failure clear still runs, and assert the bulk call count is zero so a future widening fails loudly.
e1be2e2 to
9cb2bac
Compare
…cluded The fast path only fired when the jar held no Google cookie, but this import exists for Google, so a Google cookie is normally present and the common case still paid one native remove per cookie. Measured on Electron 43, clearData excludeOrigins preserves the whole google.com registrable family -- host, leading-dot, subdomain and partitioned cookies -- so the bulk call is safe with cookies to keep, and the gate was never load-bearing for preservation. Drop the caller-supplied exclusion predicate so the preserved set has exactly one source of truth: a predicate that disagreed with NON_TRANSPLANTABLE_DOMAINS would have deleted cookies the bulk call is meant to keep. The per-cookie loop stays as the fallback for a rejected bulk clear, carrying the same exclusion.
clearData can empty part of the jar before rejecting, so the per-cookie fallback was working from a snapshot taken before the attempt: it retried removals for cookies that were already gone and could not see cookies that arrived during the clear. Re-read on rejection only. Also repairs three fixtures that mocked the session with clearStorageData after the call moved to clearData. The missing method threw, the catch swallowed it, and every one of those tests silently asserted per-cookie behaviour while believing it covered exclusion. Records the registrable-domain invariant at NON_TRANSPLANTABLE_DOMAINS, since excludeOrigins derives one origin per entry, and extends the real-Electron fixture to prove http-only and domain-scoped Google cookies survive alongside the partitioned one.
Clean-provenance QA — Import Cookies Chrome submenu (no import)Fresh CDP validation of this exact worktree on No cookie import was triggered. The native file picker was not opened. Identity via
What was exercised (Playwright/CDP only):
Renderer console: 0 errors; isolated profile logs had no cookie-import / file-picker activity. 1. Settings > Browser > Session & Cookies2. Import Cookies menu open3. Chrome submenu hovered (import not clicked) |
…e family (STA-4065) (stablyai#14131) * perf(browser): bulk clear cookies when an import excludes nothing (STA-4065) PR stablyai#14086 replaced the bulk clear with one Electron cookies.remove() per removable cookie so partitioned excluded cookies survive an import. That is required whenever something must be preserved, but it also runs when nothing is excluded, where a single clearStorageData is equivalent and far cheaper: 421ms -> 7.3ms at 1k cookies and 1764ms -> 20.1ms at 3k on Electron 43. The fast path is gated on the same unfiltered cookies.get() the exclusion predicate already reads, which an Electron fixture now proves returns partitioned cookies, so "no exclusions in the snapshot" means "nothing in the jar to preserve". * fix(browser): protect cookie clear fast-path race (STA-4065) * test(browser): keep the no-bulk-clear assertion honest after the clearData switch The mocked session still exposed clearStorageData, so asserting it was never called could no longer fail once the fast path moved to clearData. * docs(browser): stop the fallback comment from asserting a rollback The per-cookie path's rollback is being removed by STA-4061; the comment only needs to say the fallback re-removes what survived. * test(browser): keep the STA-4061 rollback fixture on the per-cookie path The bulk fast path clears a jar with no excluded cookie in one call, which would have made every assertion in the partition-rollback fixture vacuous. Give it a live Google cookie so the partial-failure clear still runs, and assert the bulk call count is zero so a future widening fails loudly. * perf(browser): always bulk clear cookies, not only when nothing is excluded The fast path only fired when the jar held no Google cookie, but this import exists for Google, so a Google cookie is normally present and the common case still paid one native remove per cookie. Measured on Electron 43, clearData excludeOrigins preserves the whole google.com registrable family -- host, leading-dot, subdomain and partitioned cookies -- so the bulk call is safe with cookies to keep, and the gate was never load-bearing for preservation. Drop the caller-supplied exclusion predicate so the preserved set has exactly one source of truth: a predicate that disagreed with NON_TRANSPLANTABLE_DOMAINS would have deleted cookies the bulk call is meant to keep. The per-cookie loop stays as the fallback for a rejected bulk clear, carrying the same exclusion. * fix(browser): re-read the cookie jar after a rejected bulk clear clearData can empty part of the jar before rejecting, so the per-cookie fallback was working from a snapshot taken before the attempt: it retried removals for cookies that were already gone and could not see cookies that arrived during the clear. Re-read on rejection only. Also repairs three fixtures that mocked the session with clearStorageData after the call moved to clearData. The missing method threw, the catch swallowed it, and every one of those tests silently asserted per-cookie behaviour while believing it covered exclusion. Records the registrable-domain invariant at NON_TRANSPLANTABLE_DOMAINS, since excludeOrigins derives one origin per entry, and extends the real-Electron fixture to prove http-only and domain-scoped Google cookies survive alongside the partitioned one. (cherry picked from commit e590683)
…e family (STA-4065) (stablyai#14131) * perf(browser): bulk clear cookies when an import excludes nothing (STA-4065) PR stablyai#14086 replaced the bulk clear with one Electron cookies.remove() per removable cookie so partitioned excluded cookies survive an import. That is required whenever something must be preserved, but it also runs when nothing is excluded, where a single clearStorageData is equivalent and far cheaper: 421ms -> 7.3ms at 1k cookies and 1764ms -> 20.1ms at 3k on Electron 43. The fast path is gated on the same unfiltered cookies.get() the exclusion predicate already reads, which an Electron fixture now proves returns partitioned cookies, so "no exclusions in the snapshot" means "nothing in the jar to preserve". * fix(browser): protect cookie clear fast-path race (STA-4065) * test(browser): keep the no-bulk-clear assertion honest after the clearData switch The mocked session still exposed clearStorageData, so asserting it was never called could no longer fail once the fast path moved to clearData. * docs(browser): stop the fallback comment from asserting a rollback The per-cookie path's rollback is being removed by STA-4061; the comment only needs to say the fallback re-removes what survived. * test(browser): keep the STA-4061 rollback fixture on the per-cookie path The bulk fast path clears a jar with no excluded cookie in one call, which would have made every assertion in the partition-rollback fixture vacuous. Give it a live Google cookie so the partial-failure clear still runs, and assert the bulk call count is zero so a future widening fails loudly. * perf(browser): always bulk clear cookies, not only when nothing is excluded The fast path only fired when the jar held no Google cookie, but this import exists for Google, so a Google cookie is normally present and the common case still paid one native remove per cookie. Measured on Electron 43, clearData excludeOrigins preserves the whole google.com registrable family -- host, leading-dot, subdomain and partitioned cookies -- so the bulk call is safe with cookies to keep, and the gate was never load-bearing for preservation. Drop the caller-supplied exclusion predicate so the preserved set has exactly one source of truth: a predicate that disagreed with NON_TRANSPLANTABLE_DOMAINS would have deleted cookies the bulk call is meant to keep. The per-cookie loop stays as the fallback for a rejected bulk clear, carrying the same exclusion. * fix(browser): re-read the cookie jar after a rejected bulk clear clearData can empty part of the jar before rejecting, so the per-cookie fallback was working from a snapshot taken before the attempt: it retried removals for cookies that were already gone and could not see cookies that arrived during the clear. Re-read on rejection only. Also repairs three fixtures that mocked the session with clearStorageData after the call moved to clearData. The missing method threw, the catch swallowed it, and every one of those tests silently asserted per-cookie behaviour while believing it covered exclusion. Records the registrable-domain invariant at NON_TRANSPLANTABLE_DOMAINS, since excludeOrigins derives one origin per entry, and extends the real-Electron fixture to prove http-only and domain-scoped Google cookies survive alongside the partitioned one.



ELI5
Wiping the browser jar before a cookie import used to be one phone call. Since #14086 it is one phone call per cookie — 3,000 calls for a 3,000-cookie jar — because a few Google cookies must be left alone and it looked like Electron could only skip them one at a time. It turns out the single call can be told which cookies to spare. This PR makes it one phone call again, always, and hands it the list to leave alone.
What Changed
removeAllCookiesExcept(store, isExcluded)becomesremoveTransplantableCookies(session):clearData({ dataTypes: ['cookies'], excludeOrigins }). There is no longer a gate that disables the bulk call when the jar holds cookies to preserve.NON_TRANSPLANTABLE_DOMAINS— which bothexcludeOriginsand the fallback filter derive from. A predicate that disagreed with that constant would have deleted the very cookies the bulk call is meant to keep, and the type system now makes that unrepresentable rather than merely unlikely.cookies.setremains unreachable from this path viaPick<Cookies, 'get' | 'remove'>.One line at the call site in
browser-cookie-import.tsnow passestargetSessionand no predicate.Why
PR #14086 (
fc7c2a1) replaced a bulk clear with a per-cookie clear so that partitioned Google cookies survive an import — Electron cannot round-trip partition identity, so a removed-and-restored cookie comes back without its partition key. That is correct and must stay whenever something is excluded. But it applies unconditionally, including when the exclusion predicate matches nothing, where a jar-wide clear removes exactly the same set.Measured cost (STA-4065 notes latency was never measured, so it was)
Standalone Electron 43.1.0 / Chromium 150 main-process benchmark on macOS. Fresh
persist:partition per trial, 3 trials each, 200 distinct eTLD+1 hosts, mixed secure/host-only/paths, jar size confirmed viacookies.get({})before each timed run. Removal URLs and grouping replicatecookieRemovalUrl+mapSettledWithConcurrency(…, 8)exactly.remove× N, conc=8 (current)clearData+excludeOrigins(shipped)The exclusion costs essentially nothing: plain
clearData({dataTypes:['cookies']})measured 9.3 ms / 22.7 ms andclearStorageData({storages:['cookies']})7.9 ms / 21.3 ms over the same jars, so all three bulk calls are within noise of each other and all are two orders of magnitude below the per-cookie path.The snapshot
get({})itself is noise (1.5 ms at 1k, 4.8 ms at 3k), so the fast path costs nothing extra to decide. The per-cookie path degrades worse than linearly — 3× the cookies costs 4.2× the time (throughput falls from 2,375 to 1,700 removals/s) — so the gap widens on larger jars. The bulk clear left the jar at 0 immediately afterawaitin every one of 30 timed trials (and still 0 after 50 ms), so it is empty-when-resolved on this build.Why the bulk call is safe with cookies to keep
The gate this PR removes was never what preserved Google cookies —
excludeOriginswas. That was re-verified on real Electron 43.1.0 (Chrome 150) against a seededpersist:jar, reading the result back through CDPNetwork.getAllCookies:excludeOrigins: ['https://google.com']google.comaccounts.google.com.google.com(leading dot)mail.google.comaccounts.google.comexample.comnotgoogle.com(lookalike)A real-Electron fixture additionally proves an
http-only, non-securemail.google.comcookie and a domain-scoped.google.comcookie both survive a real import, so the exclusion is not limited to secure host-only cookies. Listing every subdomain origin explicitly produced an identical result, so origin matching is applied at the registrable-domain level — exactlyisNonTransplantableCookieDomain's rule. The control run withexcludeOrigins: []wiped the entire jar including both Google and CHIPS cookies, which proves the exclusion is what did the work rather than the jar happening to survive.This also closes the TOCTOU that review found in the first revision, and closes it better than the gate did. The target partition can be live during an import, so a Google/CHIPS cookie set by an in-flight response after the snapshot but before the clear was invisible to any gate — no snapshot can see it.
excludeOriginsis evaluated by Chromium at clear time, so a cookie that arrives in that window is still protected.Scope note: not a partition-identity or rollback fix. STA-4061 (#14132) owns those and is already on
main; this PR is rebased on it and preserves its invariants.Honest limitations
NON_TRANSPLANTABLE_DOMAINSentries must be lowercase-ASCII registrable domains, never bare subdomains or public suffixes:excludeOriginsderives onehttps://<entry>origin per entry and Chromium applies the exclusion at the registrable-domain boundary. That invariant is now recorded at the constant, because it is not obvious from the call site.clearDataon a 3,000-cookie jar still costs ~1.8 s. That is a degraded mode, not the normal one.Linked Issue
Fixes STA-4065 — https://linear.app/stably/issue/STA-4065
Rebased onto #14132 (STA-4061), which is now on
main.Visual Proof
No UI change — this is a main-process clear-path speedup. Proof is from a live Orca Electron dev build of this branch (
Orca: brennanb2025/sta-4065-cookie-bulk-clear-fastpath, CDP 9336), driven through the real Settings and browser-toolbar import surfaces.Import surface (Settings → Browser → Session & Cookies)
Chrome was detected with two profiles. Import was triggered from stably.ai.
Import completed
The Default profile flipped from “No cookies imported” to Google Chrome (stably.ai). Store summary: 949 source cookies → 878 imported, 71 skipped (Google / non-transplantable). 809 loaded in-memory; 69 need a restart (pre-existing fallback, not this PR).
Fast-path evidence (honest: no HEAD~ before/after)
I did not disable the fast path or re-run HEAD~ in a second product build, so this is not a paired per-cookie vs
clearDatascreenshot. What the running app did show:A second import against the now-populated jar takes the
session.clearData({ dataTypes: ['cookies'], excludeOrigins: ['https://google.com'] })path. (These screenshots predate the change that made the bulk call unconditional; that widening only makes this path fire more often, never less, so the timing below remains representative.) In-appcookie-import-diag.logtimestamps for the clear step only (staging complete → “cleared existing session cookies”):01:38:24.967Z→01:38:24.969Z01:40:23.768Z→01:40:23.777Z9 ms to clear ~809 cookies matches the PR’s standalone
clearDatanumbers (~7.6 ms median at 1,000) and is not the per-cookie path (~421 ms median at 1,000). The in-app log does not printclearDatavsremove, so this is timing + predicate evidence, not a logged opcode.Browser still works after import
https://example.com/loads in the in-app browser. The toolbar Import hint is the other user-facing import surface.Imported cookies are live:
https://github.com/opened as the signed-in brennanb2025 dashboard (github.com was in the imported domain list).Testing
macOS. Windows / Linux / SSH are unaffected: this is Electron main-process session API usage with no platform-conditional code, no filesystem paths, and no wire format.
npx vitest run --config config/vitest.config.ts src/main/browser/— 36 files, 614 tests passedpnpm run typecheck:node— cleannpx oxlint src/main/browser/— clean;npx oxfmt --check— cleannode config/scripts/check-changed-code-quality.mjs— 0 new findings across 5 changed files (native, type-aware, React Doctor)Also fixed a test that my own change had quietly made vacuous:
browser-cookie-import.test.tsmocked the session withclearStorageData, so itsexpect(...).not.toHaveBeenCalled()guard could no longer fail once the fast path moved toclearData. The mock now exposesclearData.New/updated unit coverage in
browser-cookie-import-policy.test.ts:clears a jar holding Google cookies in one call that excludes them — the widening's core claim; pins the exact
{ dataTypes: ['cookies'], excludeOrigins: ['https://google.com'] }argument and assertsremoveis never calledbulk clears in one call when the jar holds nothing to preserve
touches nothing when the jar is already empty
bulk clears cookies the per-cookie path could never address (undeliverable removal URL)
falls back to per-cookie removal when the bulk clear rejects
still preserves Google cookies on the per-cookie fallback — a rejected
clearDatamust not become the path that finally deletes a live Google sessionconcurrency-bound, coordinate-serialization, and no-reconstruct tests retained, now driven through a rejecting bulk clear so they still reach the per-cookie path
re-reads the jar after a rejected bulk clear —
clearDatacan empty part of the jar before rejecting, so the fallback must act on survivors rather than pre-attempt coordinatesI verified the central new test is not vacuous by mutation: reinstating the old gate makes exactly
clears a jar holding Google cookies in one call that excludes themfail, and nothing else.Two real bugs review caught in this revision
clearDatamay already have emptied part of the jar. The per-cookie fallback was iterating the snapshot taken before the attempt — retrying removals for cookies that were already gone, and blind to any cookie that arrived during the clear. It now re-reads, on rejection only.browser-cookie-import-google-exclusion.test.tsandbrowser-cookie-import-replacement.test.tsmocked the session withclearStorageDataafter the implementation moved toclearData. The missing method threw aTypeError, the fallback'scatchswallowed it, and those tests silently exercised the per-cookie path while asserting they covered exclusion. This predates the widening — it arrived with theclearDataswitch — and would have hidden a genuine regression in the bulk path.Real-Electron coverage:
browser-cookie-import-partition.electron.test.ts— unfilteredcookies.get({})returns a partitioned Google cookiebrowser-cookie-import-partition-rollback.electron.test.ts(STA-4061's fixture, carried forward) — now forcesclearDatato reject so the fixture still reaches the per-cookie partial-failure path, seeds a live Google cookie, and asserts the bulk call was attempted exactly once. Without this it would have passed vacuously under the new bulk path while testing nothing.I manually tested these changes locally
Automated tests added/updated, or explained why not below
AI Disclosure
N/A — internal.
Review
Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance
excludeOrigins, evaluated by Chromium at clear time, measured on real Electron across host / leading-dot / subdomain / partitioned Google cookies plus a lookalike control. Removing the caller-supplied predicate leaves one source of truth for the preserved set.removeAllCookiesExcept(store, isExcluded)is renamed toremoveTransplantableCookies(session); it has exactly one non-test caller.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)