Skip to content

#722 #668 The cluster visibility summary latched permanently, and its watermark had to be published before the bit - #795

Merged
nockawa merged 2 commits into
mainfrom
feature/722-visibility-watermark
Aug 13, 2026
Merged

nockawa merged 2 commits into
mainfrom
feature/722-visibility-watermark

Conversation

@nockawa

@nockawa nockawa commented Aug 13, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #668. #722 stays open — its remaining half (the O(changed) refresh and the test that pins it) lives on the membership branch, and the [Ignore] in SystemInputViewLivenessTests still cites it, correctly.

Second of three splits off feature/feat-790, after #794. This is the visibility-gate work; the occupancy-delta membership path is deliberately not here (see the last section).

The sticky bit never recovered

The per-cluster H1 summary lets a scan skip the per-entity EntityMap probe for a whole cluster. Its death side was a sticky flag — "has anything ever died here" — and that question can only be un-asked by a full rescan, so the bit was never cleared and a churning archetype latched onto the probe forever.

The question a reader actually needs answered is not whether something died but whether every death here is already visible to it — a maximum, not a boolean. ClusterMaxDiedTsn replaces ClusterAnyDied: a maximum only rises, so it never needs a rescan to fall, and the gate recovers on its own as snapshots pass the last death. Count()'s fast path inherits the same recovery.

…which made something load-bearing that wasn't

A sticky bit denies forever, which is safe by being useless. A recovering watermark can grant, and it is only correct because ReleaseSlot clears the occupancy bit at destroy commit, so a reader past the last death sees a word that already reflects it.

Two callers of NoteClusterDied never held that. WAL replay (ApplyDestroyToExisting, cleanup deferred to the orphan sweep) and cluster migration (sets a dst bit, releases only the src slot) both leave a set bit for a dead entity. Under the sticky flag the gate stayed shut and the per-entity probe caught it; under a plain watermark every reader past that TSN is granted, and TryCountViaOccupancy popcounts a tombstone with nothing to catch it — a permanent over-count after any recovery replaying a below-frontier destroy. Both now fold VisibilityUnknown.

Fold before publish, and the two directions need opposite treatment

bornTsn is threaded into both ClaimSlot overloads, both ClaimSlotInCell overloads and TryClaimSlotInCluster, so NoteClusterBorn precedes the publishing CAS and NoteClusterDied precedes ReleaseSlot's clear.

hazard fix
existing cluster an older reader sees the bit paired with a maximum predating the entity raise before publishing
fresh cluster a newer reader sees a bit whose EntityId tail and EntityMap record don't exist yet leave unestablished until the caller writes the slot's contents

Doing the same thing to both is a bug either way round. Establishing from the sentinel is sound only on a fresh cluster, which holds exactly one entity.

Chunk ids are recycled through a free list, so ResetClusterVisibility runs at every chunk-free site — without it a "fresh" cluster inherits the previous occupant's watermarks and the rule above silently does nothing.

Both folds are now Interlocked on the element and re-read the array reference after the CAS. Concurrent claimants fold into the same cluster (#708: Transient spawns commit concurrently), so a plain read-modify-write of a maximum loses updates; and a fold taken against an array a grower has already copied lands where nobody will read it. Serialising growers against growers does not cover the second.

A rule this was violating

TryCountViaOccupancy read the active-cluster (count, array) pair with plain loads. CLUSTERWALK-02 forbids that, for a fault needing no reordering: read the length-16 array, let a concurrent spawn resize and bump the count to 17, read 17, index 16. The rule requires every site to come through one reader rather than get the order right independently — "the two sites that already loaded count-first were right by ACCIDENT".

So ReadActiveClusterList moved from a private static on TyphonRuntime onto ArchetypeClusterState, where it reads the fields it belongs to; TyphonRuntime now delegates and EcsQuery goes through it. The SoA and Path-A scans additionally acquire the occupancy word before consulting the gate, since an acquire inside the gate doesn't stop an earlier plain load sinking past it.

Dead code removed

A destroy-path fold guard claimed to serve the legacy non-cluster shape. It was unreachable: destroyClusterState is non-null only when the archetype is cluster-eligible, and every ArchetypeClusterState carries at least one segment (a non-pure-Transient archetype throws if its cluster segment can't be allocated; a pure-Transient one has transientSlotMask != 0 by construction), so an accessor flag is always set. In the genuine legacy shape destroyClusterState is null and the branch was excluded by its own first conjunct. Had it run, it would have decoded a cluster chunk id out of a legacy record's bytes.

#668: two of RefreshFull's three branches were unreachable and are gone, with the EcsView constructor producing the only shape one of them handled — no callers. The survivor is asserted rather than selected. One of the dead branches was the correct one for cluster-backed archetypes, which is how #663 read as "correct or broken depending on whether a plan exists" when it was always broken.

Rules

Two new: CLUSTERVIS-01 (publication ordering) and CLUSTERVIS-02 (a tombstone keeping its bit must deny outright). Neither was written down anywhere, and both defects were found by review, not by tests — 5 300 tests pass with the fold on either side of the publish, because both states are momentary and both settle correct. CLUSTERWALK-02's scope updated for the moved reader.

Gate

All 6 policy jobs pass · Workbench 605/605 · engine 5289/1 in Release, the failure being a TickFenceSingleVersion_…_Crash parameterisation from the #720 flake band (different parameterisation each run, green in isolation).

What is deliberately not here

The occupancy-delta membership refresh (#790). Three review rounds found ~22 correctness defects concentrated in that path, and successive fixes kept producing new ones, because it rests on two engine facts that don't hold: the active-cluster list isn't safely walkable (CLUSTERWALK-01, open), and a set occupancy bit doesn't imply a live, committed, fully-written entity. It needs a design change — running the refresh only under the tick fence's exclusion guarantee — rather than more patching.

… watermark had to be published before the bit

The per-cluster H1 summary lets a scan skip the per-entity EntityMap probe for a whole cluster. Its death side was a sticky bit — "has anything ever died here" — and that question can only be un-asked by a full rescan, so the bit was never cleared and a churning archetype latched onto the probe forever. The question a reader actually needs answered is not whether something died but whether every death here is already visible to it, which is a maximum rather than a boolean. ClusterMaxDiedTsn replaces ClusterAnyDied: a maximum only ever rises, so it needs no rescan to fall, and the gate recovers on its own as snapshots advance past the last death. Count()'s fast path inherits the same recovery.

That change makes something load-bearing that was not before. The sticky bit denied the gate forever, which is safe by being useless; a recovering watermark can grant, and it is only correct because ReleaseSlot clears the occupancy bit at destroy commit, so a reader past the last death sees a word that already reflects it. Two callers of NoteClusterDied do not clear the bit — WAL replay, whose cleanup is deferred to the orphan sweep, and cluster migration, which sets a destination bit and releases only the source slot. Both now fold VisibilityUnknown, restoring the permanent deny the sticky flag gave for free, for exactly those two cases and nothing else. Without that, every post-recovery reader satisfies the watermark and TryCountViaOccupancy popcounts a tombstone with no per-entity probe to catch it.

The summary is also now folded before the store that publishes the slot rather than after the claim returns. bornTsn is threaded into both ClaimSlot overloads, both ClaimSlotInCell overloads and TryClaimSlotInCluster, so NoteClusterBorn precedes the publishing CAS; NoteClusterDied precedes ReleaseSlot's clear. Published the other way round, a reader that sees the bit can still see a maximum predating the entity, and the gate vouches for a cluster holding an entity born after that reader's snapshot. The two directions need opposite treatment and doing the same thing to both is a bug either way: for an existing cluster the hazard is an older reader, fixed by raising first; for a freshly allocated one it is a newer reader seeing a bit whose EntityId tail and EntityMap record do not exist yet, fixed by leaving the cluster unestablished until the caller has written the slot's contents. Establishing from the sentinel is sound only on a fresh cluster, which holds exactly one entity.

Chunk ids are recycled through a free list, so ResetClusterVisibility now runs at every site that frees a cluster chunk. Without it a fresh cluster inherits the previous occupant's watermarks and the fresh-cluster rule above silently does nothing.

Both folds became Interlocked on the element and re-read the array reference after the CAS. Concurrent claimants fold into the same cluster — #708 records that Transient spawns commit concurrently from independent transactions — and a plain read-modify-write of a maximum loses updates, while a fold taken against an array a grower has already copied lands somewhere nobody will read again. Serialising growers against each other does not cover the second one. The growth path itself is now double-checked; no caller holds the finalize latch across it.

TryCountViaOccupancy read the active-cluster list's (count, array) pair with plain loads, which CLUSTERWALK-02 forbids for a reason that needs no instruction reordering to bite: read the length-16 array, let a concurrent spawn resize and bump the count to 17, read 17, index 16. The rule requires every site to come through one reader rather than get the order right independently, so ReadActiveClusterList moved from a private static on TyphonRuntime onto ArchetypeClusterState, where it reads the fields it belongs to, and both TyphonRuntime and EcsQuery now go through it. The SoA and Path-A scans additionally acquire the occupancy word before consulting the gate, since an acquire inside the gate does not stop an earlier plain load from sinking past it.

A fold guard on the destroy path is deleted rather than kept. It claimed to serve the legacy non-cluster-backed shape, but destroyClusterState is non-null only when the archetype is cluster-eligible, and every ArchetypeClusterState carries at least one segment, so one of the two accessor flags is always set and the branch was unreachable. In the genuine legacy shape destroyClusterState is null and the branch was excluded by its own first conjunct. Had it ever run it would have decoded a cluster chunk id out of a legacy record's bytes.

Two new rules, CLUSTERVIS-01 and CLUSTERVIS-02, state the publication ordering and the tombstone-keeps-its-bit case. Neither was written down anywhere; both defects were found by review rather than by tests, because 5 300 tests pass with the fold on either side of the publish — both states are momentary and both settle correct.

#668: two of RefreshFull's three branches were unreachable and are gone, along with the EcsView constructor that produced the only shape one of them handled, which had no callers. The surviving branch is asserted rather than selected. Dead code shaped like a fallback hides how narrow the live path is, and one of the dead branches was the correct one for cluster-backed archetypes, which is how #663 read as "correct or broken depending on whether a plan exists" when it was always broken.

Closes #668. #722 keeps its remaining half — the O(changed) refresh and the test that pins it — on the membership branch.
…list [no-ut]

The banner over the finalize latch enumerates every acquisition and calls itself exhaustive. It is what the next person reasons about lock order from, so a new acquisition without a line here is worse than one with a wrong line — and this change added one, behind a double-checked length compare on the visibility-array growth path.

Also states what that lock does NOT buy, since serializing growers against each other is only half the fix: a fold can still land in an array a grower has already copied and is about to replace, which is why both folds re-read the array reference after their CAS.

[no-ut] because this commit is comment-only — the identical logic already passed the full gate on the parent, and the run this push triggers is needed for the closing-keywords job, which reads the PR description from the event payload and so could not pick up a description fix without a new push.
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.

Two of RefreshFull's three branches are unreachable, and the EcsView ctor that would reach one has no callers

1 participant