Skip to content

Purging a page removes the secret store originals only it held (GST-3074) - #9796

Merged
capy-ai[bot] merged 1049 commits into
masterfrom
secret-store-cascade
Nov 19, 2026
Merged

capy-ai[bot] merged 1049 commits into
masterfrom
secret-store-cascade

Conversation

@garrytan

@garrytan garrytan commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Purging a page now removes the sealed secret store originals only that page held, so gbrain delete --purge leaves no encrypted copy of a secret the page alone referenced.
This finishes the secret store's delete side: until now, a purge left those originals in the store.
Nothing to do: the cascade runs inside every hard delete. Backups already uploaded to a storage bucket still hold their copy.

What changed
  • The purge cascade (src/core/secret-store/store.ts purgeSecretsForSlugs, called from scrubPurgedSlugStores). Every hard delete removes the page's own occurrences, which are the ones recorded on the page and not on a publish commit. It then removes each entry those occurrences pointed at, as long as no other page or commit still references it. Between the two deletes it takes SELECT … FOR UPDATE on those entries. Under Postgres READ COMMITTED, that makes a redaction of the same value on another page, committing at the same moment, show up before the entry is deleted.
  • Occurrences under an old slug go with their page. Deleting a page row sets its occurrences' page_id to NULL, so an occurrence recorded under a slug the page had before a rename can only be found earlier. deletePages, the batch tombstone purge and the per-row expiry now read the page's occurrences by page id before the delete, and pass those of the pages actually deleted. delete --purge sweeps before the page row goes, so it passes the page id.
  • Renames move occurrences. moveSlugBindings moves the renamed page's own page-level occurrences to the new slug inside the rename transaction, never another page's still under that slug. A sync rename that falls back to a new row also moves the stale row's occurrences to that row by page id (movePageIdReferences), so expiring the stale row keeps the original.
  • Restore keeps what a purge removed out, and keeps what other pages hold.
    • secrets backup --restore now adds the missing occurrences of an entry the store already holds under the same id and value, without duplicating any. So purging one page can't remove an original another page holds when the local store had lost that page's occurrence.
    • An entry whose every recorded place is on a page this brain doesn't hold is skipped (skipped_no_page).
    • A restore that would skip everything refuses with invalid_params. A value the store holds under another id counts as held, not as a missing page.
    • A restore keeps every distinct recorded occurrence (it compares every field, so two records at one place with different origins both come back).
    • A backup names each place on a page by that page's current slug, so a page renamed before occurrences moved restores. A rename makes the next backup due.
  • Doctor and errors. The secret_store check counts originals no page or commit references. The secret_store_key_missing summary now also covers a backup on a host with no key.
  • Raw SQL counts. store.ts goes to 22 and purge-slug-stores.ts to 6, both counted in the baseline, with reasons. They move behind typed engine methods with the rest of the store later.
Tests

test/secret-store-purge.test.ts runs on PGLite and on Postgres. Each new case below failed before its fix:

  • A restore adds an existing entry's missing occurrences, then purging one page keeps an original another page holds.
  • An occurrence under an old slug goes with its page under delete --purge, deletePages and the tombstone expiry.
  • On Postgres only, a purge racing an uncommitted redaction of the same value on another connection keeps the original and the other page's occurrence. Without the lock, the entry was deleted.

Two more cases pass today and guard against regressions:

  • A rename that fails inside the occurrence move leaves the page and its occurrences where they were.
  • Renaming then purging, in both orders.

The SQL-text and connection-gauge goldens change only by the added pre-delete read. The doctor goldens add unreferenced. After review, these cases were added, each failing first: the sync fallback rename, two records at one place, a value held under another id, a rename making a backup due, a page renamed before occurrences moved, and a rename leaving another page's occurrence alone. The purge suite now passes 18 on PGLite and 50 with Postgres. Local runs:

  • the secret-store suites: 51 pass on PGLite and 80 with Postgres;
  • 62 purge, rename and delete-pages files: 469 pass on PGLite and 481 with Postgres;
  • census shards 886 and 1111;
  • verify 103/103, typecheck, regen with no drift, and gitleaks clean.

Open in Capy

garrytan and others added 30 commits November 18, 2026 10:15
…lias, a redirect, a generated page, the page marker reads and every ranking stage, equal to PGLite
…ct call

The owner's wider reads (Facts rows as stored on get_page and fetch_page, a
segmented page's markdown view, every take holder for a credential with no
holders list, and graph-hop text) were on wherever a context resolved to the
owner, with each exception switched off by hand; delegated agent jobs were
one nobody had switched off. They are now off by default, and dispatch
switches them on for exactly one case: a direct remote call from an
owner-bound credential speaking for itself with no room declared. Agent jobs,
group chats, stdio, on-behalf-of audiences and attested actors read as before
this change. A census test fails if a second site sets the flag.

An agent job also reads its client's stored takes holders list, read fresh
with the grant, else world takes as before.

Review-Finding: p5/fix-check#1 reader=fix-check
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…o a brain without the pages refuse

A page rename now moves the places the secret redactor recorded on the page, so purging a page that later
takes the old name no longer deletes the renamed page's originals, and purging the renamed page removes them.
The purge also keeps any place whose page id names a live page outside the purge (rows renamed before this fix).

A backup restore now reports originals whose page is not in this brain as skipped_no_page ("skipped because
their page is not in this brain"), with the next step to sync the pages and run the same restore again. When
every entry is skipped that way it refuses with invalid_params and writes nothing.

Also: the next backup is due for entries sealed while a backup ran (database high-water mark, not the host
clock); a spooled duplicate records its commit places on the existing original so a purge keeps it; a restore
groups occurrences by entry once.

Review-Finding: p1/red-team#1 reader=red-team
Review-Finding: p1/specialist-batched#1 reader=state-machines
Review-Finding: p1/specialist-batched#2 reader=data-migration
Review-Finding: p1/specialist-batched#4 reader=performance
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
A host that restores the thin-client tree without its node_modules got a launcher that passed its runtime check and then failed in Bun's module resolver ("Cannot find package 'js-yaml'"). When the CLI runs from a tree with its own bun.lock and node_modules, the launcher now checks that directory and prints the lockfile restore command (bun install --frozen-lockfile --ignore-scripts in that tree).

Reported by @Kkkkkuro; first fixed by @Masashi-Ono0611 in #9666.

Co-Authored-By: Masashi-Ono0611 <98894508+Masashi-Ono0611@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
A rebuilt brain numbers its pages in another order, so a restored occurrence could name an unrelated page; the purge cascade then kept the original when its real page was purged. Restore now looks the page up by source and slug.

Review-Finding: p2/outside-voice#1 reader=outside-voice
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…ript changes (#9648)

A oneshot child that decides correctly to write nothing but answers in prose instead of the skip JSON dead-letters under require_writes and releases its key, so every cycle paid for the same transcript again and doctor reported dream_paid_loop. A dead synthesize child that finished cleanly with no page write now counts as settled for that content key; a changed transcript has a new key and is synthesized again.

Reported by @andreineacsu; first fixed by @Masashi-Ono0611 in #9651.

Co-Authored-By: Masashi-Ono0611 <98894508+Masashi-Ono0611@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Known limits from #9707's final review: the room mark is judged with the caller's own fact grant (GST-3066), and owner tokens minted with gbrain auth create store ['world'].

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
A timestamp like 2026-02-30 or 25:61 used to be accepted by the Rust store and stored as written. The store now checks the calendar: the day must exist in its month, hours, minutes and seconds must be in range, and 24:00:00 and a :60 leap second roll over the same way Postgres does. Page dates (effective_date) use the same check.

Review-Finding: specialist-batched#1 reader=specialist-batched
…d) pages

Three writes could leave a segmented page's search text wrong on the Rust store:
- sealChunks accepted a segmented page and replaced the chunks built from its segments. It now refuses (invalid_state), and a page in an archived source is not found, as on Postgres.
- deleteChunks removed the chunks but kept the page's seal, so a later unsealed write was read as current. It now clears the seal.
- createVersion of a segmented page wrote a version with no body and no reference to one. It now refuses with a typed capability_missing instead of writing an empty version. This changes the store's contract on purpose: the old test that pinned a version of a shell page is rewritten to expect the refusal.

Also adds a test that an impossible effective date is refused and writes nothing.

Review-Finding: adversarial-native#2 reader=adversarial-native
Review-Finding: adversarial-native#3 reader=adversarial-native
Review-Finding: adversarial-native#1 reader=adversarial-native
… holds

Creating a segmented page, or moving or reviving one with a new body, skipped the check that two live pages never share one source file. Both paths now run the same check as an ordinary page write and refuse with origin_claimed, as Postgres refuses with its unique index.

Review-Finding: red-team#1 reader=red-team
…EXCLUDE names

The Rust store hid only its built-in prefixes (test/, attachments/, .raw/). Pages under prefixes the operator lists in GBRAIN_SEARCH_EXCLUDE showed up in results. The adapter now passes the same resolved list Postgres uses, and include_slug_prefixes still brings a prefix back.

Review-Finding: outside-voice#1 reader=outside-voice
…it locked

If close() ran while an open() waited in the worker queue, the open finished afterwards and held the brain's lock with no handle to release it. The open now sees the close and refuses with store_closed, so the brain can be opened again.

Review-Finding: outside-voice#3 reader=outside-voice
…d) page

The export read the body of every page as one string, which a segmented page refuses, so it stopped with no manifest. It now skips each segmented page and counts it in the manifest (skipped_pages_segmented).

Review-Finding: outside-voice#4 reader=outside-voice
…s, search excludes, close during open, corpus export)
…er-reads-protected-sections-3058

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

# Conflicts:
#	src/mcp/http-transport.ts
The census shells out to git grep, which made the structural-suite classifier list the whole database suite as structural; on its own file only the census is.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…d the pages.ts size

verify on the master merge: the test's Postgres arm runs in the Postgres lane, bodyStripsFor (inline pages only) and protectedTakesFilter (body flags on a segmented page) are reviewed body readers, and pages.ts grows 8 lines for the owner option and the room body check.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
… login-shaped word

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
The export holds the full text of a private brain, so it refuses an output
directory inside a git working tree unless --allow-in-repo is passed. The check
read every git error as "outside a repository": an inherited invalid GIT_DIR,
a missing git or unsafe ownership let the export write into a working tree.

Now inherited GIT_* settings are dropped for the probe, only git's own
"not a git repository" answer counts as outside, and any other failure refuses
with the git error and the --allow-in-repo way out.

Red before: with GIT_DIR pointing nowhere, the unfixed exporter passed the
guard and created its output directory inside a git repository.

Review-Finding: targeted#1
…re when only its occurrences change

The page purge cascade (removing a purged page's sealed originals), the rename move of occurrence slugs, the
restore rule that skipped originals whose page is missing, and doctor's count of unreferenced originals move to
their own change (GST-3074). This change now never deletes a secret store row: a restore only adds rows, ties each
restored place to the page that holds its slug now, and never overwrites an existing entry.

The backup is now due when only occurrences changed, for example a new page that uses an existing secret.

Review-Finding: p4/outside-voice#3 reader=outside-voice
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…, and honour embedding_disabled on every embedding route (#9653, #9715)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
vorflux Bot and others added 7 commits November 19, 2026 06:43
Review-Finding: p1/red-team#1 reader=red-team
Review-Finding: p1/specialist-batched#1 reader=security
Review-Finding: p1/red-team#2 reader=red-team
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…send-keyed-hash

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

# Conflicts:
#	scripts/postgres-arm-weights.json
…bench-baseline-postgres-import

# Conflicts:
#	rust/bench/baseline/baseline.test.ts
…eps every recorded occurrence

Review round 1 on the purge cascade: a sync rename that falls back to a new row now moves the stale row's
occurrences to the new row, so expiring the stale row keeps the original its marker names. A restore compares every
recorded field before skipping an occurrence, reports a value held under another id as held rather than as a missing
page, and a rename makes the next backup due.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…lug, and how to check

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai

capy-ai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Open notes from review round 1. Neither blocks this PR.

  • Leftover places from an older purge. An occurrence that an older gbrain left behind after purging its page still counts as a reference. The cascade and doctor's count therefore keep that original. This errs on the side of keeping it, and docs/guides/secret-store.md says so.
  • A restore racing a purge. A restore that commits while a purge of the same page runs can leave a sealed original with no recorded place. Doctor's secret_store check counts it. This follows from how the SQL is ordered and was not reproduced.
  • A restore tying an original to a page that took an old slug. The fix is tracked separately: a restore attaches an original only where the page still holds its marker. Until then this is a Known-limits line in the changelog fragment.

garrytan and others added 3 commits November 19, 2026 07:05
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
… page renamed earlier restores

A place recorded before occurrences moved with their page can carry a slug the page had before an older rename. The
backup now writes the slug of the page its page_id names, so a restore into a rebuilt brain finds that page instead of
skipping the original.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai

capy-ai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Open notes from review round 2. None of them blocks this PR.

  • An allowed entry goes with its last page. An allowed entry is a value the owner restored as a false positive. It is deleted when its last page is purged, so a later repair redacts that value again. This is tracked with the restore follow-up.
  • The purge's lock can deadlock with a repair. The purge takes FOR UPDATE in sorted order. A repair that records occurrences in a different order can deadlock with it. Postgres then aborts one of the two, which costs a retry, not data.
  • One path that moves page ids has no test. gbrain repair rename-identity calls movePageIdReferences, which now moves occurrence page ids, but no test covers that call.
  • The race test covers one order. It covers the redaction committing first. Nothing covers the purge committing first and the redactor's foreign-key rollback.

garrytan and others added 2 commits November 19, 2026 07:27
An occurrence another page holds under an older slug (left by a rename before occurrences moved with their page) now
stays with that page when a different page that took the slug is renamed, so the new slug's readers do not pass the
reveal check for it.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai

capy-ai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Notes after the final review, plus one advisory read. None of them blocks this PR.

  • A restore can add one duplicate occurrence. This happens when a fresh backup is restored into a store that still holds a place under a slug left by an older rename. The restore skips a place that is already there by comparing stored slugs, but the backup now names each place by its page's current slug, so the two don't match. A second restore adds nothing, and the extra row only keeps an original longer.
  • One fix came after the last review. At 25401d6, a rename moves only the renamed page's own occurrences. The final review found the issue: a rename could move another page's occurrence that sat under an older slug. The fix has a test that fails without it, on PGLite and on Postgres. One scoped reader checks that predicate. That read is advisory, and it is not a review round.

vorflux Bot and others added 5 commits November 19, 2026 07:35
…bench-baseline-postgres-import

# Conflicts:
#	rust/bench/baseline/baseline.test.ts
… the shared-source tables

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai
capy-ai Bot marked this pull request as ready for review November 19, 2026 08:18
@capy-ai capy-ai Bot added the full-ci Maintainer-only: run the full Test and E2E suite on this PR head label Nov 19, 2026
@github-actions github-actions Bot removed the full-ci Maintainer-only: run the full Test and E2E suite on this PR head label Nov 19, 2026
garrytan added a commit that referenced this pull request Nov 19, 2026
@capy-ai
capy-ai Bot merged commit ded6640 into master Nov 19, 2026
229 checks passed
@garrytan garrytan mentioned this pull request Nov 19, 2026
capy-ai Bot added a commit that referenced this pull request Nov 19, 2026
Releases v0.60.744.0 with eight changes since v0.60.743.0, including a security fix: archived sources are now hidden from every API token (#9795).
- Archiving a source now hides its pages from every API token, not just from search (#9795)
- `gbrain sources refresh` no longer stops and asks for the user when a local commit lands while it publishes the checkout's earlier local commits (#9744)
- A managed `gbrain sources refresh` that brings in a few files now holds the checkout's refresh lock for about a second instead of minutes, and reports where its time went (#9746)
- The Mac helper's send log no longer keeps a recipient hash that can be reversed by trying every phone number (#9756)
- The experimental Rust engine's store now serves vector search, embeddings, link and graph reads, takes, salience, stats and the ingest log the way PGLite does, but nothing selects it yet; PGLite and Postgres brains are unchanged (#9759)
- A publish that redacts secrets no longer reports a failure that did not happen, and the secret audit logs are private to their owner (#9790)
- Purging a page now removes the secret store originals only that page held (#9796)
- The bake-off baseline benchmark can run against Postgres on a clean install again (#9758)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Co-authored-by: garrytan <garrytan@gmail.com>
garrytan pushed a commit that referenced this pull request Nov 19, 2026
…den for API tokens, purge removes secret-store originals, bench baseline driver (#9795, #9758, #9796)
capy-ai Bot added a commit that referenced this pull request Nov 19, 2026
…ds its marker (GST-3178) (#9806)

A secret store restore now ties an original only to the page at its backed-up slug, and only while that page still holds the entry's marker. A page that merely took an old slug never gets the original, so its readers never pass the `reveal_secret` read check for it.
This closes the restore gap #9796 listed as a known limit. A restore can't bring back an original that a purge removed.
Nothing to do.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Co-authored-by: garrytan <garrytan@gmail.com>
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