ADR 0132: Callee-Side Threaded Entry Points for Higher-Order Methods

Status

Superseded by ADR 0133 (2026-10-18); Accepted 2026-10-17; Proposed 2026-10-16

ADR 0133 decides that a block which escapes (stored, returned, passed to a user-defined method or sent to another process) may read captured variables but not assign them, so no callee-side threaded entry is needed. Phases 0a to 1c shipped (BT-3866 to BT-3871); nothing in Beamtalk called a threaded entry before this ADR was superseded, and ADR 0133 Phase 2 removes them.

Implementation Tracking

Epic (Phases 0 and 1): BT-3865 Status: Not built. ADR 0133 superseded this ADR and the epic was cancelled (2026-10-18).

PhaseIssueDescriptionSizePR
0aBT-3866Pin the §6 inventory (I1–I4, I6–I8) as PIN-BUG BUnit and repl-protocol casesS
0bBT-3867I5: a stored Tier 2 block sent value in a class or value-type method as an inline producerM
0cBT-3868Runtime floor (send_threaded/4, call_threaded/3), metadata keys, MethodInfo::threading_params, wire check, baselinesM
1aBT-3869VersionPrefix::Acc, ScopeKind::Threaded, the MethodBody builder, four verifier checksM
1bBT-3870Generalize callee_threads_block_param (greatest fixpoint); populate threading_paramsM
1cBT-3871Threaded entry for class methodsM#4361
1dBT-3872Threaded entry for value-type methodsM
1eBT-3873Caller-side callee_threaded_send producer; §6 narrowed for exact receivers; actor locals-only ruleM
1fBT-3874Corpus with oracle, pin flips, repl-protocol case, measurement, docsM

Dependency order: {3866, 3867, 3868, 3869} → 3870 (after 3868) → {3871, 3872} → 3873 → 3874.

Two file references in the text below are corrected by this tracking entry: call_threaded/3 lives in beamtalk_dispatch.erl (there is no beamtalk_block.erl), and the generated builtins file is crates/beamtalk-core/src/semantic_analysis/class_hierarchy/generated_builtins.rs.

Deviation from §2 (BT-3871 review, #4361): the plain class_<sel> wrapper does not hand the body an empty map. With ~{}~ a Tier 2 block reaching the plain entry from a late-bound send ran through call_threaded/3 and its writes were dropped silently, which §4 forbids. The wrapper passes a sentinel as Acc@0 (the atom plain in #4361; {plain, ClassTag, Selector} since BT-3885, below); call_threaded/3 with the sentinel applies an arity-N fun as before, raises stateful_block_dispatch for a Tier 2 fun without running it, and sends value to a non-fun callable. The sentinel is threaded through Acc unchanged and never used as a map.

Deviation from §2's tier gate (BT-3885): the gate below said is_function(B, n+1) applies a Tier 2 fun with Acc. Arity alone cannot tell a Tier 2 fun from a plain block that takes one argument more than it is called with, so with a real Acc map that gate had to run such a block (its side effects happened, with the caller's Acc bound to its last parameter) before its reply showed the mistake, and a reply that happened to be {_, Map} (an Erlang {ok, #{...}}) was taken for a Tier 2 reply, the foreign map becoming the caller's Acc. Tier 2 funs are now marked at creation: codegen mints every Tier 2 block as letrec 'BtTier2Block'/N+1 = fun (..., StateAcc) -> ... in 'BtTier2Block'/N+1 (generate_block_stateful_fun, the one place Tier 2 funs are created), the Erlang compiler names the lifted function -F/A-BtTier2Block/N+1-K-, and beamtalk_dispatch:is_tier2_block/1 reads that from erlang:fun_info(F, name). The gate decides the tier before applying anything: an arity-N+1 fun runs with Acc only if it is marked; unmarked, it raises arity_mismatch unrun, whatever Acc is. The mark and the plain-entry sentinel have one source, crates/beamtalk-codegen/src/core_erlang/threading_abi.rs, rendered into the generated runtime/apps/beamtalk_runtime/include/beamtalk_threading_abi.hrl (just check-generated-builtins, checked_in_header_matches_the_generator), and an EUnit test checks a block codegen compiled against the runtime's check end to end. The sentinel carries the class method's name so the refusal says which method refused the block ('twice:' on Twice class cannot thread a block that assigns captured variables through this send; the block was not run) instead of the structural fallback's wording about perform: and value.

Deviation from §2's collection-HOM row (review of #4361): the ADR 0128 fold over Acc runs for a list receiver only. The plain class_<sel> wrapper must answer exactly what the structural lowering answered before this ADR, and that lowering guarded the receiver with is_list and sent a non-list receiver the collection message itself; folding every receiver through beamtalk_collection:to_list changed what a Dictionary collect: answers (a list, not a Dictionary), made a Stream collect: eager and an infinite Stream detect:ifNone: hang. So the threaded body emits case is_list(Recv) of true -> <fold over Acc> ; false -> {Recv sel: ..., Acc} end: a non-list receiver takes the plain send with Acc unchanged (its callee runs the block as a plain callable, so a Tier 2 block raises its structured error there, never silently). The ifNone: handler of detect:ifNone: is invoked through call_threaded/3 like every other callable (a non-block handler gets its value send, not a raw apply). One error kind does change on the list path: a plain block of the wrong arity handed to a collection HOM of a threading class method now raises stateful_block_dispatch from call_threaded/3 (a block of arity N+1 is indistinguishable from a Tier 2 block by arity; BT-3885 tracks a dedicated message) where lists:map's badarity used to surface as argument_error; a Symbol callable raises does_not_understand where it raised internal_error. Two agreement rules were also tightened on the same review: a mention of a threading parameter in a conditional / and: / or: send's argument is not a root use (the index leaves the parameter plain, as codegen's sequencing rule, which walks only such a send's receiver, requires), and a use's own operands are root operand sites (a use of a second threading parameter inside the first's argument, a value: (b value), is sequenced ahead of it; BT-3890).

Deviation from §2's NLR row (BT-3871 review, #4361): Phase 1c ships no NLR-carrying threaded body. A ^ throws the Acc version current where its block is lowered, and a threaded callee can run the block after a later Acc step (b value: [:x | ^x], a stored block, a block a callee keeps), so the {Val, AccAtThrow} catch would drop the caller's writes. The threading index therefore gives a method whose body holds any block that does ^ no threading parameters (facts::body_has_block_nlr, over-approximate); it keeps its plain entry and compiles as before. The verifier's NlrInThreadedBody refuses an NLR node in a threaded body; NlrSlotShapeMismatch keeps the slot shape Phase 3 will need. Carrying the live Acc to the throw is Phase 3. Stdlib effect: Collection>>detect:ifNone: loses its (instance-side) threading parameter.

Context

Problem statement

ADR 0131 made an outer-local write inside a compiler-known construct (a loop, a fold, a conditional, on:do:, Result tryDo:, a Dictionary lookup handler, an evaluated literal block) a state effect the construct hands back to its frame. It deliberately stopped at the one boundary the compiler does not control: a block passed to a user-defined higher-order method (HOM). Its §6 makes that a compile error outside an actor instance method, because a class method or a value-type method has no channel through which a callee could return the block's writes:

sealed Object subclass: Twice
  class sealed twice: aBlock =>
    aBlock value
    aBlock value

Object subclass: Caller
  class run =>
    t := 0
    Twice twice: [t := t + 1]      // error: `twice:` cannot return the write
    t
error: block writes outer local `t`, but `twice:` cannot return the write
  ┌─ caller.bt:8:18
8 │     Twice twice: [t := t + 1]
  │                  ^^^^^^^^^^^^ `t` is written here
  = help: return the new value from the block and assign it: `t := Twice twice: [t + 1]`,
          or use a control-flow message (`do:`, `inject:into:`, `on:do:`) that threads locals

In Pharo this program answers 2. ADR 0041 promised the same ("local mutations work in all blocks"). Today it is an error in a class method and a value-type method, and in an actor instance method it works only for the narrow set of callees BT-3755 could prove safe: a sealed method of the same class that invokes the block directly at the top level of its body, as the callable of a collection message, or passes it on to another such method. A callee that invokes the block inside a do: body, a conditional arm, an operand, or a nested closure, an inherited callee, an overridable one, and every callee reached through any receiver but a bare self are the same error (stdlib/test/fixtures/adr0132section6_actor.bt.pending pins the four actor shapes; the class and value-type shapes are the ADR 0131 §6 error at every send).

ADR 0131 named this gap its Alternative D and kept it out of scope: "it changes the calling convention of every block-taking method, including stdlib natives and Erlang FFI, and needs a migration of compiled code (hot reload of mixed old/new modules). That is its own ADR." This is that ADR.

Current state

How a Tier 2 block reaches a callee today. ADR 0041 compiles a block that writes an outer local as a Tier 2 closure, fun (Args..., StateAcc) -> {Result, StateAcc1}, with the written locals under '__local__t' keys of the map. Three things can then happen at the callee:

CalleeWhat happens to the block's write
A §1 construct (do:, inject:into:, on:do:, …)Inlined by codegen; the write is a LocalRebind prelude (ADR 0131). Correct everywhere.
An actor instance method of the same class, sealed, reached by a bare self send, using the block only in the BT-3755 shapesgenerate_tier2_self_send_open packs the locals into the actor State before the self-dispatch, the callee is compiled with that parameter as a Tier 2 parameter (tier2_method_info, a per-class pre-scan), its b value is apply B(State) threading the gen_server state, and the caller reads the '__local__' keys back out of the returned State. Correct, verified, but the channel is the actor state map, and the caller and callee must be compiled together.
Anything elseThe callee's b value is the structural fallback (intrinsics.rs): is_function(B, 0) applies it, else is_function(B, 1) raises #beamtalk_error{kind = stateful_block_dispatch}. A callee that forwards the block to do:/collect:/select: runs the ADR 0128 fold with a frozen state and drops the write (ADR 0128 §"Explicitly narrowed"). ADR 0131 §6 turns both into the compile error above wherever it can see the shape.

Note the last row: a user HOM's plain function already pays an is_function/2 guard on every block invocation. There is no zero-overhead Tier 1 path through a method body; that path exists only for the inlined stdlib constructs, which never enter a method body at all. This is what decides §2's "one body" below.

Calling conventions and modules. Every class compiles to its own BEAM module (one class per file, ADR 0040; compiled_module_name in driver.rs), reloaded one at a time (Counter reload, ADR 0082 compile:source:, REPL redefinition). The ADR 0130 convention for a class method is class_<sel>(ClassSelf, A1, …, An) -> Result, bare, with class variables written in place in the class process. A value-type method is '<sel>'/n+1 = fun (Self, A1, …, An) -> Result, reached through 'dispatch'/3 = fun (Selector, Args, Self) (ADR 0006). A sealed actor method is '__sealed_<sel>'(A1, …, An, Self, State) -> {'reply', Result, State1}. Self-sends are late-bound in all three kinds: an actor self-send goes through safe_dispatch on the instance's own '__class_mod__' (BT-3666), a class-side self-send through class_self_send, a value-type instance send through dispatch/3; only a sealed class or method is statically bound. super always targets another module. A direct-called class method (ADR 0129 Phase 0b: a sealed class with no classState: and a class sealed method, ClassInfo::is_direct_call_eligible) is listed in the module's '__beamtalk_meta'/0 as direct_class_methods => #{Selector => SafeFn}, and beamtalk_class_dispatch:class_send/3 reads that map (cached at class registration, refreshed on reload) to call Module:SafeFn(nil, Args...) in the caller's process. Eligibility is computed once in Rust from the cross-file ClassHierarchy and shipped as generated metadata; the runtime has no second implementation. That pattern is reused below. dispatch/3 and handle_class_method_call resolve a selector by walking the hierarchy and checking the ADR 0066 extension registry at each level before the class's own methods; ADR 0084 runtime-installed funs take part in the same walk.

One threaded entry already exists. ADR 0131 §5 gave beamtalk_result:tryDo an arity-2 clause, tryDo(Block, StateAcc) when is_function(Block, 1), answering {Result, StateAcc1}, sharing protect/1and the catch sequence with the arity-1 clause, with a Rust ↔ Erlang conformance fixture. Codegen chooses the arity from the block literal. That is a per-method callee-side{Result, StateAcc}` protocol for exactly one native. This ADR generalizes it.

Size of the surface. 837 method headers in stdlib/src/*.bt, of which 130 name a block-like parameter and 40 of those are @primitive or @intrinsic (Erlang-implemented or codegen-lowered). The rest are ordinary Beamtalk HOM bodies, and most of them forward the block to a §1 construct.

The inventory

ADR 0131 §6 and BT-3742 define the inventory as "the set of call sites §6 rejects". Enumerated from the §6 validator (validators/local_threading.rs check_send), its fixtures, and ADR 0131's close-out:

#Shape (the block writes outer local t)ContextTodayThis ADR
I1X hom: [t := t + 1], X a literal class, user HOM invoking its parameter directly (b value, b value: x, a value-family cascade) as a statement, an assignment RHS or a ^ valueclass, value-type§6 errorPhase 1 (static or index-known callee), Phase 2 (unknown receiver)
I2as I1, callee invokes the parameter inside a do: body, a conditional arm, an operand, an on:do:/ensure: block, or a nested closure (f := [b value]. f value)all three (BT-3755's four actor probes)§6 errorPhase 3
I3as I1, callee inherited from a superclass, or overridable (class or method not sealed)actor (self-send)§6 errorPhase 4 (late-bound self-dispatch, threaded)
I4callee passes the parameter on, bare, to another HOM, including superallactor only, same-class chainPhase 1 (same module), Phase 2 (cross-module, super)
I5stored Tier 2 block sent value directly, no callee (b := [t := t + 1]. b value. t; ADR 0131 probe s4)class, value-type§6 errorPhase 0 (an ADR 0131 inline value producer over a fresh map; needs nothing from this ADR and ships first)
I6stored Tier 2 block as the callable of a collection message (items do: blk)class, value-type, REPL, and actor (ADR 0128 §"Explicitly narrowed")§6 errorPhase 2 (the ADR 0128 fold seeded from the pack, §3)
I7class-side self hom: [t := t + 1] in a class methodclass§6 errorPhase 1 (sealed), Phase 4 (late-bound)
I8a Tier 2 block passed to a class method of a class with classState: or not class sealed (runs in the class process, ADR 0109)any§6 errorPhase 4
I9an Erlang-implemented (native) stdlib HOM that invokes the blockany§6 error, or stateful_block_dispatchPhase 5, per native, opt-in; default a structured error. Block's own intrinsics with a non-literal receiver or argument (aBlock ensure: […], c ifTrue: aBlock, d at: k ifAbsent: aBlock, aBlock whileTrue) are codegen-lowered and are Phase 3
I10an Erlang FFI argument ((Erlang lists) map: [...] with: ...)anylossy, warnedunchanged (ADR 0041 §Erlang Interop Boundary)
I11the block escapes the method: ^[…], last expression, field or class-variable store, collection literal (BT-3756)any§6 errorunchanged, permanent
I12at:ifAbsentPut: / removeKey:ifAbsent: on a Dictionaryany§6 errorunchanged for Dictionary (immutable, no behaviour to lower); a user class defining either selector is an ordinary HOM, I1
I13cascade part, string interpolation, match: subjectany§6 error (ADR 0131 close-out)unchanged (no statement boundary for a prelude)
I14a keyword whileTrue:/whileFalse: condition block (BT-3782)class§6 errorunchanged (BT-3743)
I15a runtime-installed class method fun (ADR 0084 put_class_method/4)classn/ano entry; structured error at run time (Open Question 2)
I16an ADR 0127 trait provision routing the block through self do:value-type, actorvalue-type works (self_send_hom_sackful.bt); actor only through the BT-912 channelvalue-type unchanged; actor via Phase 4
I17a Tier 2 block passed to an asynchronous send (!, a spawns_block method, Future, Timer)any§6 error or a dropped write§6 error, permanent: the block runs after the caller's frame is gone (I11's rule at run time)

Constraints

Decision

A method that invokes or forwards a block parameter is compiled as one body with two entry points. The body is lowered as a ThreadedIr MethodBody frame over a new trailing family, Acc, the opaque map a caller hands over; every invocation of the parameter is gated per invocation on the block's tier and threads Acc through every construct the body contains. The plain entry (today's function name and arity) calls the body with no map (the plain sentinel, see the deviation note in the tracking section: an empty map would silently drop a Tier 2 block's writes) and answers the bare result; the threaded entry ('__t2_<sel>', one arity higher) takes Acc and answers {Result, Acc1}. Threaded entries are advertised in '__beamtalk_meta'/0 and resolved through the same late-bound dispatch walk a plain send uses. A send that passes a Tier 2 block value is, on the caller's side, one more ADR 0131 producer: it packs the block's locals, calls the threaded entry, and rebinds from the returned map. Which methods have a threaded entry is decided by one predicate in beamtalk-core, read by the §6 diagnostic, by codegen, by the metadata emitter and by the cross-file class index.

1. Which methods get a threaded entry

semantic_analysis::tier2_callee::callee_threads_block_param(class, selector, index) is kept as the one predicate and generalized. Parameter index of selector is a threading parameter when every use of it in the method body is one of:

A parameter with any other use (stored in a field, a class variable or a collection literal; returned; passed to an Erlang FFI call; passed to an asynchronous send, a spawns_block method or any callee that runs the block after the caller's frame is gone) is not a threading parameter, and the method has no threaded entry for it. BT-3755's four shapes (top-level direct invocation, the last := b value: e loop shape, the bare callable form, pass-on) are the subset Phase 1 lights up; the rest is Phase 3.

A method has one threaded entry, with every threading parameter accepted in either tier. There is no wrapper for a Tier 1 block passed alongside a Tier 2 one: the per-invocation gate in §2 handles any mix, and a non-fun callable (a Symbol, an object answering value:, ADR 0128's opaque callables) goes through the gate's dynamic value-family send as it does today.

The predicate's answer is delivered, never re-derived: codegen emits it into '__beamtalk_meta'/0 as

'tier2_methods'       => #{ 'each:do:' => {'__t2_each:do:', [1]} },
'tier2_class_methods' => #{ 'twice:'   => {'__t2_class_twice:', [0]} }

(selector → entry function name and the threading positions; class-side entries are named '__t2_class_<sel>' to mirror class_<sel> and keep a class with both twice: and class twice: compiling), beside direct_class_methods. The same answer is recorded as MethodInfo::threading_params in the cross-file ClassHierarchy (the precedent is spawns_block), filled for the stdlib in generated_builtins.rs and for package classes from their metadata, so the compiler can answer "does this known class's method thread" for a class in another file. The runtime reads the metadata (§4); the §6 validator reads the predicate through the index (§5); the override lint reads it (§6). There is one source.

2. One body, two entry points

For a class method:

%% The body, lowered once over Acc. ABlock is a threading parameter.
'__t2_class_twice:'/3 = fun (ClassSelf, ABlock, Acc0) ->
    case call 'beamtalk_dispatch':'call_threaded'(ABlock, [], Acc0) of
      <{_V1, Acc1}> when 'true' ->
        case call 'beamtalk_dispatch':'call_threaded'(ABlock, [], Acc1) of
          <{V2, Acc2}> when 'true' -> {V2, Acc2}
        end
    end

%% The plain entry: today's name and arity, unchanged for every caller.
'class_twice:'/2 = fun (ClassSelf, ABlock) ->
    call 'erlang':'element'(1, apply '__t2_class_twice:'/3 (ClassSelf, ABlock, ~{}~))

beamtalk_dispatch:call_threaded(B, Args, Acc) is the per-invocation tier gate: is_function(B, n) applies a Tier 1 fun and answers {Result, Acc} unchanged; is_function(B, n+1) applies a Tier 2 fun with Acc; anything else is the dynamic value-family send (beamtalk_dispatch), answering {Result, Acc}. (Amended by BT-3885, see Implementation Tracking: an arity n+1 fun is Tier 2 only when it carries the Tier 2 mark codegen gives every Tier 2 block, read before it is applied; any other fun of the wrong arity raises arity_mismatch without running.) It replaces the structural fallback's is_function(B, n) check for threading parameters, so the plain path pays what it pays today plus one tuple. This is ADR 0128's per-element is_function/2 gate, moved from the fold into the one helper every invocation uses.

Why one body and not two. A second, Tier-1-only body would keep the plain path free of the tuple. It would also lower every threading method twice (build time), roughly double its code, and leave two bodies that must agree. The plain path already pays an arity guard per invocation (Context), so the second body buys one tuple allocation per call and nothing else. Rejected (Alternatives). The .beam size and the plain-path cost are measured in Phase 1 against today's structural fallback.

For a value-type method the entry is '__t2_twice:'/3 = fun (Self, ABlock, Acc0) -> {Result, AccN} and 'dispatch'/3 keeps answering the plain function. For an actor instance method (Phase 4), the body is one function and the two entries are two dispatch/4 clauses (and two '__sealed_' functions for a sealed method): the plain clause replies {'reply', Result, State1} as today, and the threaded clause replies {'reply', {Result, AccN}, State1}, so the caller's map rides inside the reply value and the gen_server protocol, safe_dispatch and the {reply, _, _} 3-tuple are untouched. The ADR 0131 §5 tryDo: twin is the native form of the same shape: one implementation, two arities.

Acc is a trailing family, never the scratch map. ADR 0131 §2 defined ThreadedStmt::MethodBody { frame, threads, body } and noted it has no production builder. The threading body is its first: MethodBody { frame: Threaded, threads: [], families: [Acc, …] }. Acc is a new storage family (VersionPrefix::Acc, versions Acc0, Acc1, …) that travels as a trailing slot of every construct's tuple, as SelfVt does, and is read and written only by call_threaded and by forwards. It is never the slot-2 scratch StateAcc map of a loop or fold, and RebindShape::for_frame never seeds a StateAcc plan from it: the body's own locals go through ADR 0131's frames exactly as in any class or value-type method, under their own '__local__' keys in their own scratch map, and no key of the body's ever enters Acc. In scope kind ScopeKind::Threaded, eligible_families answers [Acc] for a class body, [SelfVt, Acc] for a value-type body that writes a field, and [State, Acc] for an actor body; the ADR 0131 ordering rule (State extracted first) extends to Acc last. The verifier check is VerifyError::AccAsScratchSeed (an Acc version used as a StateAcc seed or Put target) and OwnLocalKeyInAcc (a '__local__' Put whose map is an Acc version).

What this inherits. Because Acc is a trailing family, it has SelfVt's per-site capability map (Constraints): today a trailing family is carried by do:, collect:, inject:into:, the conditionals and on:do:/ensure: at the method root, and rejected elsewhere. Phase 3 is the work of lifting those rejections for Acc at every site a threading parameter can be invoked: Letrec bodies, the search and keyed folds (select:, reject:, detect:, count:, anySatisfy:, …), conditional arms that hold a fold, on:do:/ensure: arms, bare and stored blocks, and the NLR boundary. It is sized L, and its proof is a per-site capability corpus as BT-3827's was, not a list of fixed shapes. Until a site is lifted, a threading parameter invoked there makes the method non-threading (the predicate reads the same capability table, so the §6 error names the site).

The invariant for Acc, per scope kind, once a site is lifted:

Scope kind inside the bodyInvariant for Acc after it
top-level statementAcc_{n+1} is the map after every gated invocation the statement made, in source order
conditional arm (BranchArm)the arm's closer carries Acc out as a trailing slot; both arms rebind the same version
Letrec loop body (whileTrue:/timesRepeat:/to:do:)Acc is a loop parameter; the recursive call passes the body's final version; the loop's result tuple carries it
Foldl loop body (do:/collect:/inject:into:/…)Acc is a trailing slot of the fold accumulator, beside the fold's own StateAcc slot, never inside it
on:do:/ensure: armthe protected body's final Acc on the non-raising path; the Acc on entry on the raising path (the ADR 0131 protected-region rule: a raise discards the region's writes, here the caller's writes made inside the region)
bare block / evaluated literal blocka block literal that invokes a threading parameter is Tier 2 by containment and takes and returns Acc as a trailing slot of its own map (below); an evaluated [...] value is the one-arm conditional of ADR 0131 (BT-3841 PR 6)
stored closure invoked later in the body (f := [b value]. f value)f is Tier 2 by containment; f value is ADR 0131 §1's inline Tier 2 value producer, carrying Acc out as a trailing slot
NLR boundary, the body's own ^the $bt_nlr 4-tuple's state slot carries the body's family tuple with Acc last (Acc for a class body, {SelfN, AccN} for a value-type body, {StateN, AccN} for an actor body); the entry's catch unpacks that shape and answers {Val, AccAtThrow}, so the caller's writes made before the ^ are kept (ADR 0041 §State-Carrying NLR). Checked by VerifyError::NlrSlotShapeMismatch
NLR boundary, the caller's block's ^ crossing the bodythe throw carries the caller's own slot (§3) and a foreign token; the body's catch re-raises it untouched, and the caller's writes made inside the construct are discarded, as ADR 0131 §2 says for every producer

Tier 2 by containment. Today a block is Tier 2 when it writes an outer local (captured_mutations_for_block). This ADR adds: a block literal that invokes a threading parameter or a Tier 2 local, or forwards one in a threading position, is Tier 2. Its own map is its own (its outer-local writes under '__local__' keys, as today), and the Acc it carries for the parameter it forwards is nested under one reserved key, '__acc__', never merged: self each: [:x | b value: x. n := n + 1] compiles the block as fun (X, S) -> … call_threaded(B, [X], maps:get('__acc__', S)) …, putting the returned Acc back under the same key, and the body's local n under '__local__n' in S. A caller and a callee with a local of the same name therefore never share a key, and a recursive HOM nests one level per forward along the chain, as values, with nothing aliased. The rule lives beside captured_mutations_for_block in beamtalk-core's block facts, so the §6 validator and codegen agree on it. Inside such a block, and inside a body, '__local__' and '__acc__' reads use maps:get/2, not the maps:get/3 creation-time fallback of generate_block_stateful_fun, so a crossed or missing key crashes with a badkey instead of silently reading the wrong value; the corpus includes same-named caller/callee locals and recursion.

Why a new family and not State. The REPL root already threads an opaque map it does not name (the bindings), and it does so as the State family. Reusing State would be the smallest change. It is rejected because State carries actor meaning the body must not inherit: make_self, 'field__' keys, the BT-2717 stray-key hazard, and the ActorStateInClassMethod verifier rule that ADR 0131 extended to value-type methods precisely to keep State out of them.

3. The caller side is a producer

A send that passes a Tier 2 block value (a literal that writes an outer local, or a Tier 2 local) to a non-§1 selector is lowered as an ADR 0131 producer, callee_threaded_send, in local_threading_producer's case list:

prelude: [ Bind   { Acc0 ← ~{ '__local__t' => T }~ },             -- pack, one Put per local
           ConstructTuple { carrier: CT, doc: <threaded call>, threads: ["t"] },
           LocalRebind { local: "t", carrier: CT, slot: Key("__local__t") } ]
value:   element(1, CT)

The LocalRebinds are lowered by the enclosing frame under ADR 0131 §2's table, unchanged, so the send works wherever ADR 0131 lets a producer sit. ADR 0131 §1a's sibling-read rule applies: t + (Twice twice: [t := t + 1]) reads t before the send's rebind. The producer was gated by the same position check as every other producer (validators/local_threading.rs) until BT-3913 retired it: a position the check rejects for a loop is rejected for a threaded send too, so this ADR adds no position that ADR 0131's measurement has not shown right.

The pack, per caller kind:

I5 and I6, which have no callee. A stored Tier 2 block sent value in a class or value-type method (I5) is ADR 0131 §1's inline Tier 2 value producer over a fresh map and ships in Phase 0, independent of everything else here. A stored Tier 2 block passed to a collection message (I6) is the ADR 0128 opaque-callable fold with its StateAcc slot seeded from the pack above instead of from the actor State (ADR 0131 Phase 2d's "its own __local__ seeding, designed then"): in class, value-type and REPL context the slot is the fresh map or the bindings; in actor context it stays the actor State, which is where the block's keys already are. The fold's per-element is_function/2 gate becomes call_threaded.

The existing BT-912 actor channel (generate_tier2_self_send_open, tier2_method_info, packing into State) is kept: it is correct, verified, same-process, and cheaper than a pack for the sends it accepts. The producer above is used for every send it does not accept, so the two never apply to the same send (the agreement test asserts it). Whether to retire it after Phase 4 is Open Question 1, decided by self_send_bench.

4. Resolution: the plain send's binding, threaded

A threaded call binds exactly as the plain send would. There are four paths, and the choice is the one codegen already makes for the plain send:

The plain send is…The threaded call is…
Statically bound, same module (a self send in a sealed class or to a sealed method, or a send to the enclosing class's own direct-called class method)a direct call of the entry: call 'mod':'__t2_class_twice:'(ClassSelf, B, Acc0). Same process, no lookup. Only ever same-module, because ADR 0040 makes every other class another module.
Late-bound self (actor safe_dispatch on '__class_mod__'; class-side class_self_send; value-type dispatch/3)the threaded variant of the same path: safe_dispatch_threaded, class_self_send_threaded, a threaded dispatch/3 clause, each resolving the receiver's actual class and then looking for the entry there. Never send_threaded/4: a class-side self-send through the general path would hit handle_class_self_call's deadlock guard.
supersuper_threaded/5, starting the walk at the superclass as the plain super send does (mirroring class_super_dispatch/4's receiver-tag handling, BT-4001), so an override that forwards to super (twice: b => self log. ^super twice: b) never resolves back to itself.
Anything else (a literal class in another module, a variable, an argument, an inferred union, perform:)call 'beamtalk_dispatch':'send_threaded'(Receiver, 'twice:', [B], Acc0), answering {Result, Acc1}.

send_threaded/4 is the same walk as dispatch/3 with one difference at the end. It resolves the selector exactly as the plain send does (extensions at each level, ADR 0084 runtime funs, the class's own methods, then the superclass; class_send/3's rules for a class receiver, including the direct-call map and the gen_server path) and, at the implementation that walk resolves to:

An actor receiver is the same walk: a plain actor send is a synchronous gen_server:call (ADR 0006), so before Phase 4 the resolved actor module has no threaded dispatch/4 clause and send_threaded/4 raises stateful_block_dispatch; from Phase 4 it makes the same synchronous call and unwraps the {Result, Acc1} carried inside the reply. The {Result, Acc} shape is produced only by a threaded clause and consumed only by a threaded call, so it is never handed back as an ordinary result. The asynchronous forms (!, a Future-returning send) are I17: a Tier 2 block in one is a compile error, and no threaded variant of them exists.

perform:-family sends are not a special case: perform: is resolved by the walk to the performed selector as today (ADR 0006), user overrides of perform: included, and the entry is looked up at whatever that resolves to. The arity ambiguity beamtalk_error.erl records today ("wrong number of arguments, or the block captures mutable state") disappears for this error: the lookup is by metadata, not by fun arity, so the message says one thing. respondsTo: and selectors answer the plain selector only; an entry is not a selector, its name is not a valid one, and it is not part of the ADR 0087 synthetic-method cross-reference.

Compile time. The §6 error for a send whose callee cannot thread is reported only for an exact receiver: a literal class, self or super in a sealed class, or an inferred type that is exactly one known class (not a union, not a :: Base that a subclass may satisfy), looked up in MethodInfo::threading_params. For every other receiver the send compiles to the runtime path, and the compiler reports a threading-resolved-at-run-time warning at the block, suppressible with @expect threading_resolved_at_run_time, because the error such a send can still raise is one the LSP cannot show. This is the ADR 0131 Dictionary lookup rule (BT-3798) applied to callees: a compile error where the compiler can see the answer, a runtime-guarded path where it cannot, and never a silent drop. Every surface (CLI, REPL, LSP, MCP) answers the exact-receiver question from the same ClassHierarchy index (surface-parity.md gains the row).

Mixed old and new modules under hot reload. The plain entry keeps its name, arity and behaviour, so an old caller of a new callee is unchanged. A new caller of an old callee: a direct entry call is emitted only within one module, and every other call goes through a path that reads the callee's metadata and checks function_exported/3, so it finds no entry and raises the structured error at the one send that needs threading, never an undef. A newly compiled module on a runtime older than this ADR fails with undef beamtalk_dispatch:send_threaded/4 at its first threaded send: that is the compiler/runtime version floor the otp-support release gate and ADR 0131 §5's tryDo:/2 precedent already enforce, and the stdlib is built with its compiler. Load order follows ADR 0125's rule for upgrades, receivers before senders: a callee module is loaded before the callers that expect its entry. No .appup/relup change: the entry is an added export and an added metadata key, neither of which touches process state, so beamtalk_hot_reload:code_change/3 has nothing to migrate. (ADR 0125 §3.4 is the wire-shape skew contract; module skew within one node is this paragraph.)

5. What §6 becomes

ADR 0131 §6's rule, "a Tier 2 block value with no return channel is a compile error", stands; what changes is the set with no channel. After this ADR it is: I10 (FFI, warned, exempt), I11 and I17 (escapes and asynchronous sends, permanent), I12 for a Dictionary receiver, I13 and I14, a send whose exact receiver's method is known not to thread (§4), and, until the site is lifted in Phase 3, a callee that invokes the parameter at a site Acc cannot yet reach. A send to a non-exact receiver is not an error: it compiles to the runtime path with the warning. Phase 0 leaves §6 unchanged: nothing is narrowed before the path that replaces it exists. The §6 validator keeps reading callee_threads_block_param, now through the index for every exact receiver rather than the enclosing actor only, and the agreement test (every Tier 2 argument the §6 pass accepts lowers to a producer, every one it rejects does not) extends to the new producer.

6. Overrides, inheritance, sealing

A threaded entry is resolved on the receiver's actual class by the plain send's own binding, so an inherited HOM threads through its inherited entry, and an override is reached like any override. The one new hazard is an override that drops the entry: Base twice: threads, Sub twice: stores its block, and Sub twice: [t := t + 1] through a Base-typed receiver raises at run time where Base would have threaded. The compiler reports it where it can see it: a method that overrides one with an entry for position i, and has none, gets a twin-dropped-in-override warning naming the superclass, suppressible with @expect twin_dropped_in_override. It is a lint, not a guarantee, exactly as ADR 0130's class-state-abroad is. Sealing decides only how a send is bound (§4's first row), as it does for a plain send; BT-3755 required sealed because the actor channel needed the callee compiled in the same pre-scan, and a metadata-resolved entry does not.

7. Class variables, processes, and the hop

A Tier 2 block handed to a class method of a class with classState: or a method that is not class sealed runs in that class's process (ADR 0109). Its map is a value: it travels in the class_method_call message and back in the reply, so the outer-local write survives the hop. The block's class-variable rules are ADR 0130's and unchanged: a write to its home class's variables from the foreign process raises class_state_unreachable, a read answers the captured value. Both are documented together in beamtalk-language-features.md § Passing Blocks Through Class Methods. A direct-called class method's entry runs in the caller's process and needs neither hop nor copy.

REPL session

> sealed Object subclass: Twice
>   class sealed twice: aBlock =>
>     aBlock value
>     aBlock value
> t := 0
0
> Twice twice: [t := t + 1]
2
> t
2
> Object subclass: Keeper
>   classState: saved = nil
>   class keep: aBlock => self.saved := aBlock
> Keeper keep: [t := t + 1]
error: block writes outer local `t`, but `keep:` cannot return the write: `keep:` stores its block in `self.saved`
  = help: return the new value from the block and assign it, or keep the state where the block is stored
> k := Keeper
> k keep: [t := t + 1]
warning: `keep:` is resolved at run time; if the receiver's method cannot thread the block this raises
ERROR: stateful_block_dispatch: 'keep:' on Keeper does not thread its block
  hint: return the new value from the block and assign it where it is called
> t
2

The last error is the runtime path (§4's fourth row): through a variable the compiler cannot see Keeper, so the send resolves at run time, finds no entry, and raises. t is untouched either way; nothing is dropped silently. In a body that forwards the block on, the block may already have run (with whatever side effects it has) before a downstream resolution raises; what is guaranteed is the error, not that no code ran.

Error examples

Object subclass: Base
  class twice: b => b value. b value

Base subclass: Sub
  classState: saved = nil
  class twice: b => self.saved := b         // warning: twin-dropped-in-override
warning: `twice:` overrides `Base twice:`, which threads its block parameter `b`, but this method stores it
  = note: a caller passing a block that writes a local will get `stateful_block_dispatch` here where `Base` threads it
  = help: invoke `b` instead of storing it, or add `@expect twin_dropped_in_override`

(Base twice: is not class sealed: overriding a sealed method is already the hard error "Cannot override sealed method", so the lint only ever fires on an overridable one.)

Prior Art

User Impact

Steelman Analysis

A. One body, two entry points, metadata-resolved (chosen)

B. Universal caller-side protocol (every block-taking method takes StateAcc, ADR 0041 §Caller-Side Protocol as written)

C. Mutable reference cell (a make_ref()-keyed process-dictionary cell for each threaded local, cleaned up on return, NLR and exception)

D. Keep ADR 0131 §6 as the permanent answer (the issue asks for this steelman)

E. Call-site specialization (inline a statically known, sealed, same-module callee so the block becomes a §1 construct; ADR 0128 Option 4, BT-3610's recommendation)

F. A declared annotation (class sealed twice: aBlock threads: aBlock => or a @threads pragma) instead of an inferred predicate

G. Static-only entries (same-module sealed self sends and exact-receiver direct-called class methods, guarded; every other receiver stays a §6 error)

H. Two bodies (a Tier-1-only plain body beside the threaded one)

Tension points

Alternatives Considered

B, C, D, E, F, G, H

See the Steelman Analysis.

Reuse the State family for the body's carrier instead of adding Acc

Smaller diff (the REPL root already threads an opaque map as State). Rejected in §2: State carries actor-specific meaning and a verifier rule whose whole purpose is to keep it out of class and value-type methods.

Acc as the body's scratch StateAcc map (slot 2) rather than a trailing family

It would reuse every StateAcc-mode lowering unchanged. Rejected in §2: the body's own '__local__' keys and the caller's would share one map, so a caller and a callee with a local of the same name, or a recursive HOM, would read each other's values silently; and a StateAcc plan seeded from Acc would carry the BT-2717 stray-key hazard into another frame's locals. The trailing-family choice costs the per-site capability work of Phase 3 and buys a key space that cannot collide.

Extend the BT-912 actor channel to class and value-type methods by giving them a State parameter

This is B restricted to compiled Beamtalk methods: it changes the arity of every class and value-type method, direct-called ones included, and so every runtime caller (class_send/3, dispatch/3, ADR 0084 funs, the supervisor paths). Rejected for the same reason as B.

An entry per (method, parameter) rather than per method

'__t2_0_twice:', '__t2_1_each:do:', one for each threading position and one for each combination a caller might use. Rejected: the combinatorics are unbounded for a method with several block parameters, and the per-invocation gate makes one entry accept any mix.

Consequences

Positive

Negative

Neutral

Implementation

PhaseScopeProofSize
0Pins, the floor, I5 and the wire check. Move the four adr0131section6callee pending fixtures and the class/value-type shapes of I1–I4, I6–I8 into PIN-BUG BUnit files over all three method kinds and the REPL. Add tier2_methods/tier2_class_methods to '__beamtalk_meta'/0 (empty), MethodInfo::threading_params (empty), and beamtalk_dispatch:send_threaded/4 and beamtalk_dispatch:call_threaded/3 raising the precise stateful_block_dispatch. §6 unchanged. I5 as ADR 0131's inline value producer over a fresh map. Wire check: one hand-written Erlang entry ('__t2_class_twice:'/3 in a test module with a hand-written metadata entry) reached through send_threaded/4 from a compiled class method whose Tier 2 block is packed and unpacked by hand in the fixture, proving the walk, the lookup, the function_exported/3 guard, the entry call and the map round trip before any codegen exists. Baseline build time, .beam sizes, self_send_bench.Every pin red as a compile error or the structured runtime error; I5 green; the wire check green; no silent wrong answer.S
1Static entries (Alternative G as scope). VersionPrefix::Acc, ScopeKind::Threaded, eligible_families; the MethodBody builder; call_threaded/3; the predicate generalized to the §1 direct-invocation, callable-argument and same-module pass-on uses with the greatest-fixpoint rule; one body with two entries, metadata and threading_params emission (stdlib via generated_builtins.rs); the callee_threaded_send producer (§3) for statically bound same-module sends and exact-receiver direct-called class methods, guarded by function_exported/3 across modules; the §6 narrowing for exact receivers with "why" help; the actor locals-only rule for blocks bound for an entry. Verifier: AccAsScratchSeed, OwnLocalKeyInAcc, NlrSlotShapeMismatch, ActorStateInThreadedBlock.I1, I4 (same module), I7 (sealed) pins green in class and value-type; the agreement test extended; just verify-threaded-ir; the local_touch corpus gains hom_call, same-named caller/callee locals, recursion and NLR shapes, with the oracle that the explicit-accumulator spelling of each program (t := X hom: [... t + 1] via inject:into:) answers the same; plain-path cost, .beam size and build-time measured.L
2Resolution. send_threaded/4 as the dispatch/3 walk with the entry lookup at the resolved implementation (extensions, runtime funs and DNU proxies → structured error; DNU → does_not_understand); super_threaded/5; the non-exact-receiver warning; cross-module forwards and forwards to unknown receivers inside a body; I6 (the ADR 0128 fold seeded from the pack, with call_threaded as its gate, in class, value-type, REPL and actor context); the REPL bindings-map rule.I1 and I4 for unknown and cross-module receivers, I6 pins; send_threaded benchmarked against the ~65 ns budget; a module-skew test (new caller, old callee; old caller, new callee; reload mid-cache).M
3Callee frames: Acc capability per site. Lift the trailing-family rejections for Acc at Letrec bodies, the search and keyed folds, arms holding a fold, on:do:/ensure: arms, bare and stored blocks (Tier 2 by containment with the '__acc__' nesting) and the NLR boundary; Block's own intrinsics with a non-literal receiver or argument; arity/respondsTo: lowering.I2 and the I9 intrinsic row; a per-site capability corpus (site × fold kind × body × position × tier mix), as BT-3827's; .core diff harness lists the expected diffs.L
4Late binding, processes, actors. safe_dispatch_threaded, class_self_send_threaded, the threaded dispatch/3 clause; the threaded class_method_call variant through invoke_class_method/7 (I8); actor bodies as paired dispatch/4 clauses and '__sealed_' functions with the {Result, Acc} reply; I3; the override lint (§6).I3, I7 (late-bound), I8 pins; hom_composability_actor.bt, self_send_hom_pool.bt, tier2stored_block_matrix_actor.bt unchanged; self_send_bench before and after; the BT-3755 exemption in local_threading.rs reduced to the agreement test's non-overlap rule.M
5Natives, on demand. For each Erlang-implemented HOM that should thread: an arity-n+1 clause sharing the body (the tryDo/2 shape), a hand-written metadata entry in the native facade, and a .bt conformance fixture asserting both arities agree on the raising and non-raising paths. The Erlang guideline.Per native.S each
6Close-out. Docs (beamtalk-language-features.md § What Works and What Doesn't, § Passing Blocks Through Class Methods; docs/agents/expanded.md § State-Threading Codegen; debugging.md verifier table; erlang-guidelines.md; surface-parity.md); ADR 0131 §6 and ADR 0128 §"Explicitly narrowed" amended to point here; flip every pin; final measurement; Status to Implemented.CI.S

Dependency order: 0 → 1 → 2 → 3 → 4; 5 and 6 after 4. Phase 1 lands with the verifier checks and the producer together; shipping a body without the producer, or the producer without the §6 narrowing for exact receivers, would reintroduce a silent path. The expanded.md proof rule is met as follows: the invariant per scope kind is §2's table; each VerifyError is shown to fail on hand-built IR of the pending fixtures' shapes before its lowering exists (the fixtures themselves are compile errors today and have no IR, so the hand-built IR is the repro); the execution oracle is the explicit-accumulator spelling; the corpus is generated; the self-send path reports self_send_bench.

Affected components: beamtalk-core (semantic_analysis/tier2_callee.rs, block_facts.rs for containment and the capability table, validators/local_threading.rs, class_hierarchy MethodInfo, generated_builtins.rs, the two lints), beamtalk-codegen (threaded_ir/{ir,verify,build}.rs, generator/version.rs for Acc, threaded_expr.rs for the producer, gen_server/methods.rs, value_type_codegen.rs, dispatch_codegen.rs, class_meta.rs, spec_codegen.rs for the entry's -spec, control_flow/list_ops/opaque_fold.rs, intrinsics.rs), the runtime (beamtalk_dispatch.erl send_threaded/4 and super_threaded/5, call_threaded/3 beside it, beamtalk_actor.erl safe_dispatch_threaded, beamtalk_class_dispatch.erl, beamtalk_object_class.erl for the threaded class call, beamtalk_error.erl), stdlib natives per Phase 5, BUnit fixtures, tests/repl-protocol/cases/, runtime/perf/self_send_bench.

Shared-kernel check. The predicate has one home, tier2_callee::callee_threads_block_param, read by the §6 validator, codegen, the metadata emitter and the ClassHierarchy index; the runtime reads the emitted metadata and re-derives nothing (the ADR 0129 pattern). Tier 2 by containment and the per-site capability table live in block_facts. The caller-side lowering is an ADR 0131 producer and reuses threaded_locals_of; no second threaded-set walk. Acc goes through ADR 0122's families-as-data, not a per-construct slot. The tier gate has one implementation, call_threaded/3, used by the body, the opaque fold and the structural fallback. The Rust ↔ Erlang boundaries are two: the metadata shape (a Rust emission test plus an Erlang test that send_threaded/4 honours a compiled module's map and raises on a module without it, both over one .bt fixture) and each native entry's conformance fixture, as for tryDo:/2.

Migration Path

No correct program changes behaviour. Programs that are ADR 0131 §6 errors today compile and run once their callee has an entry. The one new way a program can fail is the runtime stateful_block_dispatch through a non-exact receiver whose callee does not thread; such a program was a compile error before, so nothing that compiled before this ADR regresses, and the warning at the send names it. An actor block passed to a HOM that also writes a field becomes a compile error where today it is a §6 error or a dropped write. A hand-written Erlang HOM that users pass stateful blocks to gains an entry clause in Phase 5 or raises the structured error. Modules compiled before this ADR need no change and keep working as callees of old callers; recompiling them adds their entries. On upgrade, load callees before callers.

Open Questions

  1. Retire the BT-912 actor channel. After Phase 4 every actor send has an entry path. generate_tier2_self_send_open packs into State and needs the callee in the same pre-scan; the entry does not, but costs a pack. Decided by self_send_bench after Phase 4; the default is to keep it while it is measurably cheaper.
  2. Runtime-installed class methods (ADR 0084). A put_class_method/4 fun has no entry and no metadata; send_threaded/4 raises for it. Whether ClassBuilder grows an entry form (a fun of arity n+2 registered beside the n+1 one) is left to a need.
  3. A declared annotation on top of the predicate (Alternative F) if the override lint proves insufficient in practice.
  4. Same-module inlining (Alternative E) as an optimization of a statically bound entry call, if Phase 1's measurement shows the pack and tuple on a hot same-class HOM matter.
  5. Which natives first. The candidates are the Erlang-implemented HOMs users pass stateful blocks to in practice, counted from real §6 hits before Phase 5 is scheduled; Future, Timer and every asynchronous callee are never candidates (I17).

References