Skip to content

KeyCallers admits through an atomic count (spec 0004, entry 3) - #32

Merged
matthewjones372 merged 2 commits into
mainfrom
key-callers-atomic-count
Dec 1, 2026
Merged

matthewjones372 merged 2 commits into
mainfrom
key-callers-atomic-count

Conversation

@matthewjones372

Copy link
Copy Markdown
Contributor

Entry 3 of spec 0004, key-callers-atomic-count. Based on main; entry 4 (key-material-uses-outside-steps) stacks on it.

What changes

KeyCallers no longer admits through state.modify. It keeps two JDK atomics, each commented with spec 0004:

  • a call adds one to inFlight, then reads closed. If closed, it takes the one away and refuses.
  • close sets closed, then polls (10 ms, as before) until inFlight is zero.

Whichever side goes second sees the other, so a call is either refused or waited for. Tickets, CallerState and the close stages that existed only for them (FENCE as a commit, REAP) are gone. REVOKE, STOP, OBSERVE and the failure assembly are unchanged.

One use of a held key view commits 2 steps where it committed 3. KeyViewUseCostSuite's ceiling drops to 2; it failed at 3 before the change.

Bench

./gradlew :surefold-tenancy-crypto-bench:run, paired runs alternating this branch and main on the same machine, load average 195 to 210 on 8 cores. p50 is the comparison; p99 and max are noise at this load.

Run 1 caller p50 8 callers p50 8 callers p99
this, 1 3.25 µs 2.63 µs 443 µs
main, 1 3.58 µs 4.29 µs 908 µs
this, 2 2.96 µs 2.42 µs 862 µs
main, 2 4.63 µs 4.63 µs 770 µs

New story

a call arriving during close is refused or waited for, never neither (RetainedKeyCallersCloseSuite) races a call against close 200 times. Either the call is refused with nothing entered, or it entered and left before close returned. As the spec's question 2 expects, there is no seam between two atomic operations, so this is the stress story. The two branches are also held deterministically by pendingCaller (a held call keeps close waiting, and a later call is refused).

Caller stories: kept, rewritten, removed

Removed:

  • a committed admission observer throw preserves original and runs no body: admission commits no step, so it has no observer to fault.
  • The CallerFenceProbe self-tests: the Solo probe interrupts a real pending Shared commit without changing its value, a fresh commit after the Solo interruption survives its observer failure and owned unwind, an assertion failure while Solo is held releases every gate and joins both workers, pause-only handoffs retain late state and clean up after assertion: false/true. The probe paused the fence and admission commits. Neither commits now, so the probe and its self-tests go.
  • atomic caller fence captures late admission and cleans up after assertion: false/true: replaced by the new story above.
  • a real precommit fence interruption clears before retry and restores at close exit: the fence is now one atomic write, so there is no commit to interrupt. Restoring interruption at close exit is still covered by accepted callers keep close parked…, normal interrupted pool stop… and close refuses $mode steps… preserves interruption.

Rewritten:

  • committed terminal reap is not repeated and an empty close has nothing to reap → a second close and a close with no callers return at once. There is no reap stage now.
  • the call guard refuses ordinary nested and forced solo steps before admission: the admission-commit counter is dropped. Kept: refused inside a transaction, the body is not run, and a later call runs.
  • committed fence and reap failures retain received roots for $mode (×4) → close cleanup failures retain received roots for $mode. The two faults now come from owed cleanup actions in the drain phase. The root-assembly assertions are the same.
  • opaque Fail retains its received cleanup root before its owning attempt: same change, faults moved to drain-phase actions.
  • same-owner direct inherited and observer closes refuse before fencing → same-owner direct and inherited closes refuse before fencing. The observer variants closed from the admit and fence commits, which no longer exist.
  • KeyCachePublicCloseSuite an accepted lease reaching enrolment after close fencing has no loader → an admitted lease…. It paused on the admission commit. It now sets up the same state directly (admitted by open callers, ledger already fenced) and asserts refusal with no loader and no clock sample.

Kept unchanged:

  • The RetainedKeyDeliverySuite atDeliveryCommit stories: they hook ledger steps (registerPrepared, abortPrepared, drain), not caller admission.
  • an admitted view remains owned when admission instrumentation throws (admissionObserverFault): it hooks OwnedKeyMaterial.borrow, which stays a step.
  • Every pendingCaller story and the other public-close stories.

Verified

  • Locally: spotlessApply, detekt main and test clean. All 255 tests of :surefold-tenancy-crypto ran. Everything except the Postgres-backed suites passed.
  • Not green locally: TenantKeysSuite and KeyCacheCompatibilitySuite hit their 1-minute watchdog in container and Liquibase setup (the thread dump shows Liquibase finishing a second before the timeout) at load average 100 to 230. They pass when run alone. The same limit was noted on [stack 2/3] Spec 0004: a key view use registers once #30. CI is the judge for these and for Kover.

🤖 Generated with Claude Code

matthewjones372 and others added 2 commits December 1, 2026 10:19
A call adds itself to an in-flight count, then reads closed; close sets
closed, then waits for the count to reach zero. Admission commits no
step, so one use of a held key view commits two steps, not three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matthewjones372
matthewjones372 merged commit 425b013 into main Dec 1, 2026
3 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