Skip to content

fix: make room ownership atomic at birth - #179

Open
e-lutris wants to merge 1 commit into
flop-labs:mainfrom
e-lutris:codex/fix-claim-first-message-race
Open

e-lutris wants to merge 1 commit into
flop-labs:mainfrom
e-lutris:codex/fix-claim-first-message-race

Conversation

@e-lutris

Copy link
Copy Markdown

Summary

  • serialize a first owner claim with the room creation and room locks, then re-check that the room still has no messages before creating the owner note
  • mark writes authorised while a room was open and re-check owner absence under the append lock before committing
  • force create-if-absent for first claims inside the shared boundary, which also prevents the owner-owner overwrite described in TOCTOU race in room ownership claim allows ownership theft #173
  • keep the core size ratchet flat while consolidating repeated expiry checks

Why this is separate from #174

#174 protects two owner-note writes from each other. It does not serialize the owner note with the room JSONL commit, so both cross-resource interleavings in #176 still return 200: a conversation can be claimed after its first message, or an unsigned message can land after ownership commits.

This PR makes the first owner note and first room record share the existing room creation/room lock domain.

Tests

Two deterministic regressions cover both orderings:

  • first message lands after the claim preflight: claim returns 403 and the room stays open
  • claim lands after unsigned-write preflight: write returns 403 and no message commits

Validation:

  • uv run --no-sync coverage run -m pytest tests -q — 381 passed
  • uv run --no-sync ty check
  • uv run --no-sync ruff check .
  • uv run --no-sync ruff format --check .
  • uv run --no-sync python sz.py --check — 2010 core code-lines vs 2012 baseline
  • git diff --check

Closes #176

@bdunn77

bdunn77 commented Aug 25, 2026

Copy link
Copy Markdown

I ran a branch-neutral ownership/concurrency comparison against exact PR head bcdfdf2d08d27a1c4d5f0afe9d32d529cf12da02, replayed onto main at e934e093752da48c7778ef918981d0ca839b90c7. (main has since advanced by one release-only commit, e6114b38e58889c71d1f7338e3b58fa2db530194, touching changelog/package/MCP metadata but not the ownership implementation or tests.)

What passed

  • Ruff, formatting, ty, and core-size caps
  • full Linux suite: 383 passed, 97.66% coverage
  • deterministic claim-versus-first-message checks in both forced orderings
  • independent-process birth stress: 200/200 rounds preserved the invariant that a first ownership claim and a pre-ownership first message cannot both commit
    • claim won 51 rounds
    • message won 149 rounds
    • forbidden owner-plus-preexisting-message state: 0 rounds

This is materially stronger than #174's first-owner-note CAS: under the same 200-round process harness, #174 admitted both the claim and first message in all 200 rounds.

Two authorization windows remain

The same neutral harness found two commit-time fencing gaps:

  1. Stale transfer: Alice→Bob passes _note_write_gate; before its final owner-note write, another valid Alice-authorized transfer commits Alice→Carol. The stale outer request still returns 200 and overwrites Carol with Bob.
  2. Stale signed message: Alice's signed message passes the room write gate; ownership transfers to Bob before store.append. Alice's stale-authorized message still returns 200 and lands afterward.

So my read is: this is the stronger consolidation target for atomic room birth, and the birth race itself is well covered. Before merge, it would be valuable either to fence transfer/message authorization through commit under the relevant room lock, or to define the weaker semantics explicitly and pin them with regression tests.

I did not push a branch or open a competing PR. I can provide the small branch-neutral reproducer if useful.

@djd39448 djd39448 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.

Rebased #179 onto current main and tested it independently. It fixes both #173 and #176, stays in scope, and is green on the checks that gate this area. Reviewing as a contributor — this is a review, not a merge.

Rebase is a one-line CHANGELOG conflict, resolved trivially:

$ gh pr checkout 179 && git rebase origin/main   # only CHANGELOG.md [Unreleased] conflicts

The race is fixed. I applied #174's three race regression tests against this branch — all pass:

test_two_concurrent_first_claims_do_not_both_succeed PASSED
test_claim_vs_first_message_do_not_both_succeed PASSED
test_first_message_vs_claim_do_not_both_succeed PASSED

Same shape as the approach in #174: an unowned / require_unowned_room flag re-checked under the append lock, with the first claim serialized against room creation.

The checks that gate this area pass on current main:

$ uv run ruff check .                                   # clean
$ uv run sz.py --check                                  # ok
$ uv run python -m pytest tests/unit/test_store_doc.py  # 2 passed

That last one matters here: the change adds unowned: bool = False to store.append, and this PR regenerates docs/store.md to match (the generated-doc test enforces it). CHANGELOG updated, baselines in sync, full suite green. Complete from where I sit.

@sv sv added bug Something isn't working help wanted Extra attention is needed labels Aug 27, 2026 — with Claude
Jr-kenny added a commit to Jr-kenny/technocore-chat that referenced this pull request Sep 9, 2026
Gate returns its own first-claim snapshot so the write lane forces
create-if-absent without re-reading after the nonce burn. Re-reading
reintroduces the split-read: B completes between the two reads, A sees
present and overwrites unconditionally.

Zero store churn: uses existing expect_absent inside the note lock.
Caller ?if preserved: stale if still 409 via normal CAS path.
Both lanes: GET note_write_signed and POST note_post.
Tests: GET + POST distinct-nonce race, forced at the owner-note lock.

Fixes flop-labs#173. Handover/allow/message fencing stays with flop-labs#501/flop-labs#499/flop-labs#179.

Caps: app 1027/1030, store untouched.
WIZARDspace added a commit to WIZARDspace/technocore-chat that referenced this pull request Sep 12, 2026
…le test

The marker carried strict=True but no raises=, so every failure inside the test
counted as the expected one. A dead hook or a moved helper reported "1 xfailed"
-- including the test's own `assert raced` guard, whose entire job was to refuse
to prove nothing. Verified on 1d8baac by breaking `_race_before_lock` two ways:
both reported XFAIL, and under --runxfail both stopped at line 63, never
reaching the ownership invariant.

That is not only a reporting fault. A harness that breaks while flop-labs#173 is open
never reaches the invariant, so it keeps reporting xfail after a fix lands: no
XPASS, no removed marker, and the acceptance bar disappears with a green build.

- raises=AssertionError scopes the expectation to the defect's own failure.
- Every check upstream of the invariant goes through _plumbing(), which calls
  pytest.fail. Failed is not an AssertionError, so it falls outside raises= and
  reports FAILED from inside an xfail-marked test. Covers the race-happened
  guard and a new refusal of a 5xx from the losing claim.
- test_a_broken_harness_fails_the_build_instead_of_reporting_the_bug runs both
  shapes under a real pytest and asserts the report separates them.

pytest.xfail() was the other option and loses the ratchet: the imperative form
has no strict mode, so once the race closes the test would simply pass.

The measurement note is re-taken at today's heads: flop-labs#629 (22c7e43) xpasses,
flop-labs#501 (3209cb8) still xfails, flop-labs#179 (bcdfdf2) still conflicts in four files.

Raised by @Minh3132.
Jr-kenny added a commit to Jr-kenny/technocore-chat that referenced this pull request Sep 21, 2026
Gate returns its own first-claim snapshot so the write lane forces
create-if-absent without re-reading after the nonce burn. Re-reading
reintroduces the split-read: B completes between the two reads, A sees
present and overwrites unconditionally.

Zero store churn: uses existing expect_absent inside the note lock.
Caller ?if preserved: stale if still 409 via normal CAS path.
Both lanes: GET note_write_signed and POST note_post.
Tests: GET + POST distinct-nonce race, forced at the owner-note lock.

Fixes flop-labs#173. Handover/allow/message fencing stays with flop-labs#501/flop-labs#499/flop-labs#179.

Caps: app 1027/1030, store untouched.
@sv sv added the material Substantive: correctness, security, data durability, capacity, or user-visible behavior label Oct 19, 2026
@sv

sv commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Carrying over from look-alike #629 (closed as this PR's duplicate):

  1. Its gate-snapshot mechanism is a cleaner primitive than re-reading: the gate returns its own first = (current is None) snapshot so the write lane forces expect_absent=True without a second read after the nonce burn — re-reading reintroduces the split-read discussed in the fix: server-side CAS for room-owners first-claim to close TOCTOU takeover #629 thread.
  2. The TOCTOU race in room ownership claim allows ownership theft #173 distinct-nonce first-claim race pin (originally test(rooms): pin the #173 distinct-nonce first-claim race #706, carried by fix: server-side CAS for room-owners first-claim to close TOCTOU takeover #629) should ride with this PR.
  3. fix: server-side CAS for room-owners first-claim to close TOCTOU takeover #629's argument for the narrowed OWNERS-only CAS (zero store churn) is worth weighing against this PR's broader serialization.

@pepedesigner

Copy link
Copy Markdown

Reviewed PR #179 (fix: make room ownership atomic at birth).

Files changed: CHANGELOG.md,docs/store.md,src/app.py,src/store.py,tests/http/test_rooms.py,

Verdict: LGTM. Reviewed code changes; no issues found.

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

Labels

bug Something isn't working help wanted Extra attention is needed material Substantive: correctness, security, data durability, capacity, or user-visible behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Room claim and first message race breaks ownership-from-birth

5 participants