Skip to content

[wildcard] Flip GUARANTEES Row 11 from 'No claim yet' to Guaranteed: Linear Atomic Pointer Swaps (atomic.Box[T]) with Exchange-Enforced Pointee Ownership #833

Open

Description

@magomedta

The idea, in a paragraph

Directly answering Active Solicitation Prompt 4 in MOONSHOT.md ("'Impossible' guarantees... pick a guarantee GUARANTEES.md explicitly does not make and argue it is reachable") and resolving the open [TBD] gap recorded in GUARANTEES.md Row 11:
Introduce atomic.Box[T] as a first-class Sendable Class 2 type that elevates hardware atomic operations into compile-time ownership transfer boundaries. In PR #52 (issue #51), atomic.Pointer[T] and atomic.Value were demoted from Sendable Class 2 to unproven HYB-E3005 fail-closed because "the atomic makes the pointer swap race-free, NOT the pointee." Today, lock-free concurrency (Treiber stacks, Michael-Scott queues, RCU epoch updates, and atomic buffer swaps) is 100% impossible in Go-Hybrid without taking the managed("...", "atomic") permit, which strips all soundness guarantees (Row 8). atomic.Box[T] solves this by enforcing Exchange Linearity: box.swap(move new_val) consumes the caller's ownership of new_val and returns old_val with exclusive, unaliased ownership transferred to the caller; box.compare_and_swap(expected, move new_val) transfers ownership strictly on success. Hardware LOCK CMPXCHG becomes the ownership handoff mechanism, allowing lock-free data structures to run at hardware speed (~10 ns vs 80 ns for channels) with zero data races on the underlying pointee.

Which assumption it breaks

  1. Breaks the assumption in GUARANTEES.md Row 11: That non-scalar atomic pointers cannot guarantee pointee race-freedom and must remain unproven (HYB-E3005 fail-closed) indefinitely.
  2. Breaks the assumption in SPEC-LANGUAGE.md §8.4 C8 and compiler/crates/hy-check/src/sendability.rs: That concurrent communication between tasks must strictly occur via channel value copies or permanent freeze-on-send. atomic.Box[T] allows a shared concurrent cell across tasks while guaranteeing that at any single point in time, exactly ONE task holds an alias to the underlying pointee.
  3. Breaks the dichotomy between "Channels only" and "Unsound managed blocks": Eliminates the performance cliff where developers must either pay the channel scheduler overhead (goroutine parking, channel locks, buffer allocations) or abandon all compiler soundness checks via managed("...", "atomic").

Why this is a breakthrough, not an increment

A standard feature bounty cannot solve this: PR #52 demonstrated that simply treating atomic.Pointer[T] as sendable causes silent data races under go run -race because multiple goroutines can call .Load() on the atomic pointer and concurrently mutate the returned *T. That is why the maintainer kicked it out.

The breakthrough is recognizing that atomic swap instructions are the exact hardware equivalent of linear ownership transfers:

  • In standard Go sync/atomic.Pointer[T], .Load() permits unbounded fan-out of shared mutable pointers.
  • In atomic.Box[T], .load() is statically forbidden on mutable types (or restricted strictly to deeply immutable/frozen types, Class 1/4).
  • For mutable types (map, slice, pointer to struct), access is permitted strictly via atomic exchange:
    // Thread A transfers exclusive ownership of task_data into the atomic box:
    old := box.swap(move task_data)
    // Thread A's task_data is marked moved_at (HYB-E3003 prevents local reuse).
    // Thread A now exclusively owns 'old' (transferred from the box).

Because swap atomically writes the new pointer while extracting the old pointer, no two tasks can ever observe or dereference the same pointee simultaneously. The hardware memory bus guarantees mutual exclusion, and Tier-0's lexical move checker guarantees single-ownership handoff.

This unlocks lock-free algorithms in Go-Hybrid:

  • Lock-free queues & stacks: Nodes are popped via atomic swap/CAS; the popping task receives exclusive ownership to process and mutate the node with zero lock contention and zero data races.
  • Double-buffering & RCU: Workers atomically swap active render/write buffers in 10 ns without channels or mutexes.

How we would know it works (or doesn't)

We pre-register two falsifiable milestones and concrete kill criteria:

Milestone 1 (Feasibility Spike — Zero-Race Lock-Free Proof, no compiler rewrite required):

  1. Implement atomic.Box[T] in pure Go (runtime/go/hyatomic/box.go) using Go's sync/atomic.Pointer with runtime exchange checks:
    • Swap(new_val *T) (old_val *T)
    • CompareAndSwap(expected *T, new_val *T) (swapped bool, acquired *T)
  2. Build an adversarial lock-free test suite:
    • Case A (Treiber Stack): 64 concurrent tasks pushing and popping 1,000,000 dynamically allocated mutable structs (type Node struct { Payload map[string]int; Next *Node }). Each popping task mutates Payload["count"]++ immediately after pop.
    • Case B (Buffer Swapper): 16 producer tasks and 16 consumer tasks swapping mutable byte buffers.
  3. Run under go test -race -count=1000 -cpu=8,16,32.
  4. Falsification Gate for M1:
    • G1 (Race Freedom): If go test -race reports a SINGLE data race on any pointee field across 1,000,000 operations, the linear exchange hypothesis is invalid → proposal dies, publish failure log to MOONSHOT.md.
    • G2 (Performance Budget): Single-thread uncontended swap latency must be < 15 ns (vs ~85 ns for an unbuffered channel ping-pong). If hardware atomic exchange overhead exceeds 30 ns, the performance justification fails → proposal dies.

Milestone 2 (Checker Typing & Corpus Flipping):

  1. Register atomic.Box[T] in hy-check as Sendable Class 2 (inherently sendable across task and chan without move).
  2. Add exchange-legality checks in compiler/crates/hy-check/src/loans.rs and sendability.rs:
    • box.swap(v) requires move v (rejects HYB-E3001 if v is not moved).
    • box.load() on a mutable T rejects with HYB-E3007 (cannot load shared mutable pointer without exchange).
    • box.compare_and_swap(exp, v) requires move v.
  3. Add 4 regression tests to the test suite:
    • atomic_box_swap_accepts_and_moves (ACCEPT)
    • atomic_box_swap_without_move_rejects_e3001 (REJECT)
    • atomic_box_load_on_mutable_rejects_e3007 (REJECT)
    • atomic_box_treiber_stack_e2e_race_clean (ACCEPT + go run -race)
  4. Deliverable: Publish the learning report to MOONSHOT.md and flip GUARANTEES.md Row 11 from [TBD] / "No claim yet" to "Guaranteed: Race-free lock-free ownership exchange via atomic.Box[T]".

Cost honesty (optional but powerful)

  • What gets added:
    • runtime/go/hyatomic/ (~150 LOC of pure Go wrapping sync/atomic.Pointer[T]).
    • Sendable Class 2 classification for atomic.Box[T] in compiler/crates/hy-check/src/sendability.rs (~40 LOC).
    • Method shape validation in compiler/crates/hy-check/src/shape.rs for swap and compare_and_swap (~60 LOC).
  • What gets deleted / avoided:
    • Deletes the need for managed("...", "atomic") in lock-free code.
    • Eliminates the [TBD] footnote in GUARANTEES.md Row 11.
  • What does NOT break:

Bounty Payout Wallet Address (if accepted for M1 / M2)

  • USDT (TRC-20 / Tron): TJtkXjcJT5MuCcSxUCU8tkJqQYJAfHVq1d

Track terms

  • I understand no prototype is required and failure-with-knowledge is a valid, paid outcome
  • I understand all outcomes (including declines and failures) are published in MOONSHOT.md

Activity

  1. dancerdan15 commented on Oct 23, 2026

    @dancerdan15

    /attempt #833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions