Repository navigation
fix(memory): stop the topic-page sweep from blanking a fresh entity page - #236
Merged
Merged
Conversation
philbert440
force-pushed
the
fix/issue-230
branch
from
November 25, 2026 15:56
3f753e2 to
6cab864
Compare
philbert440
force-pushed
the
fix/issue-230
branch
from
November 25, 2026 17:39
18fd801 to
0cfb5bb
Compare
The sweep checked page_taken without a lock and then wrote an empty page with save_topic_page. When the entity builder wrote and bound the page between the two calls, the sweep wrote the empty page over the entity's file and row. Under a scoped key such as coco-box, the sweep still wrote the empty page to the same file and then failed on rh_wiki_topics_scope_local. TopicPageStore gains create_topic_page, which writes only to a vacant key. Postgres checks for vacancy after it takes the repository lock and the slug lock. The key is vacant when no row or redirect holds it, no row in the same scope uses the same local file, and the topic is not bound to a page. SQLite runs the same check inside its immediate transaction. The sweep creates entity and work pages with this method. If the write is refused, the sweep uses the page the topic is now bound to. Fixes #230
The sweep picked a free slug with page_taken and then called create_topic_page once. page_taken checks only the page key. In Postgres, create_topic_page also refuses when a row in the same wiki scope uses the slug as its file name, such as coco-mesh with the local name mesh. The sweep then failed with SlugConflict, and every retry picked the same slug, so the work topic never got a page. The sweep now calls create_topic_page inside the slug loop. When the create is refused and the topic is still unbound, the sweep tries the next numbered slug.
A work topic whose slug is already 80 characters got the numbered slug <slug>-2 when its first slug was taken. The wiki repository normalizes every slug it reads or writes to 80 characters, so <slug>-2 resolved to the file of the page that already held <slug>. The sweep then wrote an empty page over that file. SQLite also wrote the empty page over that page's row. Numbered slugs now shorten the base to leave room for the suffix, the same way the normalizer shortens a slug, so a numbered slug normalizes to itself. create_topic_page also refuses when the page file it resolves to already exists, whether or not a row points at it. The sweep then moves on to the next numbered slug.
The topic-page sweep runs under the topic owner's scope, and row-level security hides other owners' pages from that scope. When another owner already held the key, the vacancy check in create_topic_page passed. The sweep wrote the page file and then failed on the row upsert with a row-level security error. The vacancy query now runs in a savepoint that turns on maintenance scope, so it sees every owner's keys. The query returns only a boolean. Rolling back the savepoint restores the caller's scope before the page is written. A refused create moves the sweep on to the next numbered slug.
create_topic_page checked that the page file did not exist and then called fold, which runs git pull --rebase before it writes. When the pull brought in a teammate's file at that path, the create wrote the empty page over it and committed. LockedRepository gains create. It pulls, checks for the file after the pull, and writes and commits only when the file is still absent. The Postgres and SQLite creates use it and refuse when it finds the file, so the sweep moves on to the next numbered slug.
A Postgres person login is pinned to one person, and rh_scope_maintenance ignores the maintenance setting there, so the vacancy probe cannot see another owner's page. On such a login, for example a gateway topic page edit that creates the page, a hidden key looked vacant. save_locked committed the wiki file and then failed on the row upsert, leaving a committed file with no row. save_locked now writes the row before it writes the file. A create inserts the row with ON CONFLICT DO NOTHING, which sees conflicts on every unique key whatever the login can read, and refuses when the insert finds one, so the sweep moves on to the next numbered slug. An overwrite upserts the row first. The topic binding updates also run before the file write. After the commit, the row is stored again with the folded page and its git SHA.
save_locked upserts the page row before the writer folds the page, so that a refused row fails before the git commit. The overwrite path used topics::store for that upsert, which writes the article as the caller passed it, before the writer bounds it and archives the rest. The generated content_tsv column is computed from search_text, so a sweep that retitled a page with a large article could fail PostgreSQL's 1 MB tsvector limit before the archive step ran. The overwrite reservation now writes only the row metadata: key, title, scope, local slug, owner and visibility. On a conflict it updates the title and updated_at and leaves the stored article and search text alone. The create claim already wrote metadata only, and both now share one insert. The bounded page is stored after the write, as before.
wiki_replays_every_recorded_summary_shape deleted the page rows between replayed summaries but kept the wiki directory. Each later summary started with the previous summary's acmekit-deploys page file and no row for it. The topic-page sweep now refuses to create a page over a file that no row owns, so the topic took acmekit-deploys-2. Each replayed summary now gets a fresh wiki directory, so the files match the emptied page rows. The assertions are unchanged.
Two hub sweep tests made a topic's sync fail by having another person's row hold its page slug. The sweep now checks vacancy across every owner and moves a refused create on to the next numbered slug, so that topic gets a page and no longer fails. The tests then found no sync job of its own for the topic. The fixture now has the topic owner's own rows hold the base slug and every numbered slug up to SLUG_ATTEMPTS, which the kernel now exports. The sweep skips each held slug and fails the topic with SlugConflict, which is the failure the tests need. The assertions are unchanged.
philbert440
force-pushed
the
fix/issue-230
branch
from
November 25, 2026 17:54
0cfb5bb to
24692fe
Compare
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.
Why
The 0.8.19 stable release must not lose page prose to the topic-page sweep. The sweep checked whether a page slug was taken without holding a lock, and then wrote an empty page. If the entity builder wrote and bound the page between those two steps, the sweep wrote the empty page over the entity's file and row. The prose stayed lost until the next rewrite. With #228's scoped keys, such as
coco-cocobox, the sweep still blanked the same file and then failed onrh_wiki_topics_scope_local. Review then found more ways the create could land on a page it should leave alone: a numbered slug, a hidden owner, a page brought in bygit pull, and a pinned Postgres login.Fixes #230
What changed
One commit per fix.
4be99f7d).TopicPageStoregainscreate_topic_page. It writes only when the key is vacant and returns whether it wrote. Postgres checks for vacancy insidesave_locked, after it takes the repository lock andlock_slug, which are the locks the entity builder holds while it writes. A key is vacant when no row or redirect holds it, no row in the same scope uses the same local file, and the topic is not bound to a page. SQLite runs the same check inside its immediate transaction. The sweep (ensureandensure_entity) creates pages throughcreate_topic_page. When a create is refused, the sweep uses the page the topic is now bound to.save_topic_pagekeeps its behaviour.cc4f3e2e).page_takenchecks only the page key, but a create also refuses a slug that a scoped row uses as its file name, such ascoco-meshwith the local namemesh. The sweep now callscreate_topic_pageinside the slug loop and tries the next numbered slug when a create is refused.6889e470). For an 80-character slug,<slug>-2normalized back to<slug>, so the sweep wrote over that page's file. Numbered slugs now shorten the base to leave room for the suffix. A create also refuses when the page file already exists, whether or not a row points at it.6cab864d). Row-level security hid other owners' pages from the topic owner's scope, so the vacancy check passed on a key another owner held. The vacancy query now runs in a savepoint that turns on maintenance scope and then rolls back.e6083a77). The create checked for the page file and then calledfold, which runsgit pull --rebasebefore it writes. A pull that brought in a teammate's file at that path was overwritten with the empty page.LockedRepository::createpulls, checks for the file after the pull, and writes and commits only when the file is still absent. The Postgres and SQLite creates use it and refuse when the file is there.7a297d80). On a pinned Postgres person login,rh_scope_maintenance()ignores the maintenance setting (migration 0053), so the probe from commit 4 cannot see a hidden key. This path is reachable: a person node's gateway runsPUT /api/topics/{topic}/pageon its own pinnedRIVETHUB_PG_URL(crates/rivethub/src/composition.rson_postgres→StorePorts::of), andedit_topic_page→root_page→PageSync::ensure→create_topic_pagecreates the page when the topic has none.save_lockednow writes the row first. A create inserts it withON CONFLICT DO NOTHING, which detects conflicts on every unique key whatever the login can read, and refuses when it finds one. An overwrite upserts the row first. The topic binding updates also run before the file write. After the git commit, the row is stored again with the folded page and its SHA.Tests
a_late_sweep_keeps_the_entity_page(Postgres, SQLite) anda_late_sweep_keeps_another_scopes_entity_page(Postgres with fix(memory): keep entity and topic-page backfill running on a shared hub #228 scopes).testkit::wiki_files::RacingEntityruns a real entity-builderlease.applyinside the sweep'spage_takencall, so the entity is written and bound between the check and the write on every run.a_work_page_steps_past_a_scoped_page_with_its_name(Postgres) anda_numbered_long_slug_keeps_the_page_it_shortens_to,a_sweep_skips_a_page_file_without_a_row(Postgres, SQLite).a_work_page_steps_past_a_key_its_owner_cannot_see(Postgres, viewer scope).a_sweep_keeps_a_page_its_pull_brings_in(Postgres, SQLite). The wiki is a clone of a bare origin. A second clone pushestopics/pulled.mdafter the wiki clone was made, so only the create's own pull brings the file in. The sweep must leave the file alone and bind the topic topulled-2.a_pinned_login_steps_past_a_key_it_cannot_seeanda_pinned_login_refused_a_page_row_commits_no_file(Postgres). Both pincommon::PROBEtophilinrh_role_principals. The first expects the sweep to step past coco's hiddenmeshtomesh-2. The second expects an overwrite of the hidden key to fail and leave notopics/mesh.md.Each test fails on the sources before its fix. Before commit 5, both pull tests bound the topic to
pulledand overwrote the file. Before commit 6, the pinned sweep failed withnew row violates row-level security policyonrh_wiki_topicsafter writing the file, and the pinned overwrite lefttopics/mesh.mdcommitted.Proved with:
SQLX_OFFLINE=true CARGO_BUILD_JOBS=5 cargo xtask check --locked --no-nextest-list(GATE OK),cargo nextest run -j5 -p memory-postgres -p memory-sqlite -p workers -p kernel -p testkit -p wiki-files --no-fail-fast(1261 of 1261 passed),cargo xtask commits origin/release/0.8.19-rc..HEAD(GATE OK), and the new tests red on the sources before their fix and green after it.