Skip to content

fix(memory): stop the topic-page sweep from blanking a fresh entity page - #236

Merged
philbert440 merged 9 commits into
release/0.8.19-rcfrom
fix/issue-230
Nov 25, 2026
Merged

philbert440 merged 9 commits into
release/0.8.19-rcfrom
fix/issue-230

Conversation

@philbert440

@philbert440 philbert440 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

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 on rh_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 by git pull, and a pinned Postgres login.

Fixes #230

What changed

One commit per fix.

  1. Create only on a vacant key (4be99f7d). TopicPageStore gains create_topic_page. It writes only when the key is vacant and returns whether it wrote. Postgres checks for vacancy inside save_locked, after it takes the repository lock and lock_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 (ensure and ensure_entity) creates pages through create_topic_page. When a create is refused, the sweep uses the page the topic is now bound to. save_topic_page keeps its behaviour.
  2. Try the next slug after a refusal (cc4f3e2e). page_taken checks only the page key, but a create also refuses a slug that a scoped row uses as its file name, such as coco-mesh with the local name mesh. The sweep now calls create_topic_page inside the slug loop and tries the next numbered slug when a create is refused.
  3. Keep numbered slugs within the slug limit (6889e470). For an 80-character slug, <slug>-2 normalized 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.
  4. Check vacancy across every owner (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.
  5. Check for the file after the pull (e6083a77). The create checked for the page file and then called fold, which runs git pull --rebase before it writes. A pull that brought in a teammate's file at that path was overwritten with the empty page. LockedRepository::create 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 the file is there.
  6. Write the row before the file (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 runs PUT /api/topics/{topic}/page on its own pinned RIVETHUB_PG_URL (crates/rivethub/src/composition.rs on_postgres → StorePorts::of), and edit_topic_page → root_page → PageSync::ensure → create_topic_page creates the page when the topic has none. save_locked now writes the row first. A create inserts it with ON 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) and a_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::RacingEntity runs a real entity-builder lease.apply inside the sweep's page_taken call, 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) and a_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 pushes topics/pulled.md after 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 to pulled-2.
  • a_pinned_login_steps_past_a_key_it_cannot_see and a_pinned_login_refused_a_page_row_commits_no_file (Postgres). Both pin common::PROBE to phil in rh_role_principals. The first expects the sweep to step past coco's hidden mesh to mesh-2. The second expects an overwrite of the hidden key to fail and leave no topics/mesh.md.

Each test fails on the sources before its fix. Before commit 5, both pull tests bound the topic to pulled and overwrote the file. Before commit 6, the pinned sweep failed with new row violates row-level security policy on rh_wiki_topics after writing the file, and the pinned overwrite left topics/mesh.md committed.

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.

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
philbert440 merged commit e7089b9 into release/0.8.19-rc Nov 25, 2026
8 checks passed
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