ADR 0142: The Class Process Only Owns Class State — Stateless Class Methods Run in the Caller
Status
Implemented (2026-10-30); accepted 2026-10-29. Epic BT-4297.
Amended by ADR 0153 (2026-11-25), the rule that decides where a class method runs. A class has no state, so "the class or any ancestor declares class variables" is no longer a condition. A class method runs in the caller unless its selector is process-bound.
performLocally:withArguments:is removed;perform:withArguments:has its behaviour.
Superseded in part by ADR 0152 (2026-11-24), the BT-4307 resolution. The resolution that an ancestor's class-side extension counts as that ancestor's method, with the owner memo declining when one answers first, no longer applies: class-side extensions are removed, so the owner memo has no extension verdict. A runtime class-method fun installed through the class still counts as the class's method.
Implemented note (2026-10-30). What shipped differs from the text below in these ways:
- Rule 2 is chain-wide (deviation from §1). A class keeps its class process when it or any class in its superclass chain declares class variables, not only when the resolved method's defining class does. A subclass that declares none of its own holds its ancestors' class variables in its own process, and a method it defines itself reaches them through a class-side
selfsend, which would raiseclass_state_unreachablein the caller. The runtime rule isbeamtalk_class_metadata:chain_has_class_variables/1, the compile-time ruleClassHierarchy::runs_class_method_in_caller(Rust), and the deriveddirect_call_ok/2flag folds the class-variable facts over the whole chain (BT-4300, BT-4301).- The guarded static call also checks
erlang:function_exported/3, so a method removed byremoveSelector:or a reload takes theclass_sendfallback instead of raisingundef, and gets the class pid byerlang:whereis/1of the compile-time registered name. The direct call stays in tail position. Class-sideself fooand explicit own-class sends go throughclass_self_direct_ok/4, sealed or not; no unguarded class-side direct call remains. Theclass_var_abiis 3 (the class-method calling convention changed); modules compiled for 2 are refused at load.- Flag erasure is as amended in §2 below: a class's own derived inputs are raised before its change is visible, its descendants' flags are erased just after.
- Benchmarks (
benchmarks/vs-elixir/results/bench-bt4300.md,bench-bt4301.md). Rows 3b and 3c went from about 200 reductions through the class process to about 24 with the in-caller dynamic path (BT-4300), then to 7.03 with the guarded static call (BT-4301), meeting the ≤ 10 gate. Row 3a misses its ≤ 3 target: it went from 2 to 7.03, the cost of the guard, thefunction_exported/3check and the real class reference that the unguardednilcall skipped. The miss was accepted for merge; BT-4316 brought it back to the target with onepersistent_termread per static call target (§2, amended).performLocally:withArguments:now resolves through the class-method chain from the receiving class (an inherited selector is found, a subclass override wins), but applies compiled methods only: a selector whose nearest definition is an ADR 0084 runtime fun raisesdoes_not_understand.class-process-ffinow also fires in classes that declareclassState:(they used to be exempt), with a hint to move the call into an instance method or an Actor.- Class-literal signal sends (BT-4270). BT-4115's unguarded caller-side raise for
Error signal: mwas the last unguarded class-side verdict. It now sits behind a static call key of its own,{caller_safe_raise, Primitive, DefiningModule, Fun, ReceiverTag}, installed bystatic_call_target/4under the same conditions as a direct-call key (extended to an inherited owner without a runtime fun) plus the defining module's metadata mapping the selector to that primitive, and erased by the same writers. Row 8b stays at about 89 reductions (86 before), where the plain dynamic path cost 155 (results/bench-bt4270.md).- Constant class methods (BT-4363, BT-4504). A class-literal send of a class-side method whose whole body is one scalar literal (
Errors protocolErrorwithclass protocolError => 1; since BT-4504 also a float, string, symbol, character,nil,trueorfalse, carried as the term it compiles to and certified with=:=) answers the literal in place behind a static call key of its own,{constant_value, Value, Module, Fun, ReceiverTag}.static_call_target/4installs it under the same conditions as a direct-call key, plus the class's published metadata row mapping the selector to that value (constant_class_methods, generated from the method source and published on registration and reload, not read from the module, whose old code is still current during a reload's-on_load); the same writers erase it, as does a change to that row, so an extension, a runtime fun,removeSelector:or a reload that changes the body is honoured. Sealing plays no part. The send costs 1 reduction instead of 3 for the guarded direct call.- Resolved (BT-4307): an ancestor's class-side extension counts as that ancestor's method on every class-side path; the owner memo declines when one answers first, so the class process runs it.
- Explicit own-class sends (BT-4317, amended 2026-11-04): see the amendment at the end of §2.
Context
Problem
Every class has a gen_server, the class process. Today a class method runs inside that process unless three conditions all hold:
- the class is
sealed; - it has no class variables;
- the method is declared
class sealed.
These are ADR 0129 Phase 0b's rules.
Everything else, open classes and non-class sealed methods alike, is a
gen_server:call. The benchmarks/vs-elixir suite measures it:
| Row | Send | Beamtalk | Elixir |
|---|---|---|---|
| 3a | class sealed method of a sealed class | 2 red / 0 w, 0.9 ns | 2 red, 0.8 ns |
| 3b | class method, sealed class, method not class sealed | 145 red / 444 w, 2.3 µs | 2.9 ns |
| 3c | class method on an open class | 146 red / 480 w, 2.6 µs | 2.8 ns |
The cost is not only speed. The class process is a node-wide serialisation point: every caller of every class method of that class queues on one process. It also makes class methods behave unlike the functions they look like:
- Caller-sensitive FFI breaks.
gen_tcp:acceptblocks every other caller of the class.controlling_process,self(),link,monitorand the process dictionary act on the class process, not the caller. Socket code getsnot_owner.- The
class-process-ffilint exists only to warn about this.
- Re-entrancy fails. A block passed into a class method runs in the class
process (Passing Blocks Through Class Methods, BT-3022). If it sends
back to the same class, the result is
dispatch_error. ADR 0109 had to special-caseFile open:do:to escape this. - Where code runs depends on a subclassing decision. Sealing a class for
API reasons moves its
class sealedmethods out of the process; unsealing it moves them back.
What the class process actually protects is class variables. ADR 0130 made the class process their home for the duration of an invocation. A class with no class variables has nothing for the process to own.
For Catalina, class-side sends are about 1% of per-message CPU (the
echo_c10_64B profile recorded in epic BT-4291), so this ADR is mostly about
correctness and contention, not throughput.
Current state
Two mechanisms put class methods in the caller today:
- ADR 0129 Phase 0b direct calls, plus BT-4099 constructors.
- Rust computes eligibility (
ClassInfo::is_direct_call_eligible: sealed + no class variables +class sealed+ not a spawn constructor) and bakes it into__beamtalk_meta/0asdirect_class_methods. - Static call sites call
Mod:class_sel(nil, …)unguarded. Dynamic class sends consult the baked table (lookup_direct_call/3). - Trusting a compile-time verdict without a guard is the source of four
known caveats:
- BT-4126: stale descendants after a hot patch;
- BT-4133: class-side extensions are bypassed;
- BT-4134: a reparent leaves a stale verdict;
- BT-4037: callers pass
nilasClassSelf.
- Rust computes eligibility (
- Open class-side self-sends (BT-3700).
class_self_direct_ok/4reads onepersistent_termflag per send, maintained bybeamtalk_class_shadow_flags.- The flag is erased under a per-tag lock before an extension or ADR 0084 runtime fun becomes visible.
- This is a guarded direct call (ADR 0025 amendment, 2026-10-29), but it is used only for self-sends inside an open class.
Constraints
- The guard is the contract (ADR 0025 amendment, 2026-10-29). A direct call needs a run-time guard proving it reaches what dynamic dispatch would reach, with a byte-identical fallback.
- ADR 0130:
- Class variables live in the class process during an invocation.
- They are not inherited: a method may touch only its own class's variables.
- §1 forbids nesting one class-variable home inside another.
- Escape hatches are guaranteed (ADR 0100):
- No image mechanics (principles §4). Behaviour must not depend on
whether code runs in the REPL,
beamtalk run,beamtalk testor a release. - One rule implementation. The process-bound selector list and the eligibility rule are computed once, in Rust, and reach the runtime as generated metadata (ADR 0129).
Decision
1. A class method runs in the caller unless the class needs its process
A class method runs in the caller when all of these hold:
-
The receiving class has no class variables, and is not shadowed. This is checked at run time by the per-class flag
direct_call_ok(C). It is the existingbeamtalk_class_shadow_flagsderived flag, which gains one input. It is true exactly when:Chas no class variables (classState:), which is the new input;- no ADR 0084 runtime class-method fun is installed on
C; - no class-side extension shadows a compiled method of
C; - the class has finished bootstrap (
mark_ready).
-
The class defining the resolved method also has no class variables. (As implemented, this is taken over the receiving class's whole superclass chain; see the Implemented note under Status.)
- A class method touches only its own class's variables, through the receiver's class-process home (ADR 0130).
- Take a method defined on a stateful
Baseand sent to a statelessSub. Today it runs inSub's process and raisesundeclared_class_variable; run in the caller, it would raiseclass_state_unreachableinstead. So it stays in the class process, and its error does not change. - Methods that run in the caller install no class-variable home, so ADR 0130 §1's no-nesting rule is untouched.
-
The selector is not process-bound. These keep their current routing:
new,new:,startLinkandstartLink:(ADR 0129). Value constructors keep a direct path (BT-4099), behind the same guard (amended, BT-4409: see §2).spawnandspawnWith:, which have dedicated clauses inbeamtalk_class_dispatch.Supervisorsupervise/spawn*, which are routed beforeclass_send_plain.TestCaserun selectors, which answertest_spawnand need the gen_server'snoreply.
Rust owns this list, in
class_info.rsnext tois_direct_call_eligible. It reaches the runtime as generated metadata. Theclass-state-abroadlint and the migration lint (§4) read the same list.
sealed and class sealed play no part. A class that needs a serialising
home declares class state (classState:) or delegates to an Actor, the
idiomatic BEAM owner. No new keyword is added.
2. How sends reach it
Dynamic class sends. These are sends through a variable, perform:,
classNamed:, a class passed as an argument, or an Erlang caller of
class_send. They all go through beamtalk_class_dispatch:class_send_plain/3.
- When
direct_call_ok(C)holds, resolve the defining class and module in the caller. A memo,{class_method_owner, C, Sel}inbeamtalk_class_derived_cache, caches the result (the BT-4109resolve_ownerpattern). Apply the method in the caller if rule 2 holds. Otherwise use the class process, as today. - Deriving the memo walks the chain exactly as the class process does today. A class-side extension or ADR 0084 fun at each ancestor counts as that ancestor's method.
- A shadow on an ancestor never clears the receiver's flag, so the memo is
what keeps it correct. Every change that can alter the answer must
invalidate the derived cache:
beamtalk_extensions:registerandremovealready do.beamtalk_class_metadata:set_runtime_class_methods/2does not today. Phase 1 adds it.
Static class sends with a literal receiver (Point origin, or class-side
self foo), where the selector is one that C itself defines, compile to:
case call 'beamtalk_class_shadow_flags':'direct_call_ok'('Point class', 'Point') of
<'true'> -> call 'bt@app@point':'class_origin'(PointRef)
<_> -> <today's class_send>
end
-
PointRefis the class reference built from the compile-time registered name (BT-4295), passed as the realClassSelf. This retires thenilconvention and its per-call prologue lookup (BT-4037). -
An inherited class method sent to a literal receiver goes through the dynamic path, which itself now runs in the caller through the memo. No call site names a superclass module, so nothing goes stale when the hierarchy changes.
-
When the flag is erased.
- Today it is erased only when an extension is registered or removed
(
beamtalk_extensions:register/remove), or when an ADR 0084 runtime fun is installed (beamtalk_class_metadata:set_runtime_class_methods/2). Seebeamtalk_class_shadow_flags:set/2(:102) andclear/2(:139). - This ADR adds erasure on every other metadata write for
C: reload, a change to class variables,removeSelector:, andput_class_method.
Amended (BT-4299, BT-4301, 2026-10-30). For a fact the flag is derived from (an extension, a runtime fun, the class's own class variables), the raw input is raised and the class's own flag erased under the per-tag lock before the change becomes visible, so no send sees the old
true. The other metadata writes (reload,removeSelector:,put_class_method) only erase the derived flag: that forces the next send to re-derive it from the raw inputs, but does not hold the fast path off during the write. A method those writes remove is caught by the guarded call'sfunction_exported/3check, and the in-caller owner memo is kept consistent by the derived cache's generation counter, not by the erase. - Today it is erased only when an extension is registered or removed
(
-
Amended (BT-4301, 2026-10-30): descendants' flags. Because the flag folds the class-variable facts of the whole chain, a change to a class (gaining or losing class variables, a reload that re-parents it) also erases every descendant's flag. That erasure happens after the ancestor's row write, under each descendant's tag lock, which is always taken, so it also removes a value an in-flight install derived from the old row. Between the write and the erase a descendant's static send can still see its old
trueand run in the caller: the same short window a reload has for its own new code. The window is accepted; no stale value outlives the erase. See thebeamtalk_class_shadow_flagsmoduledoc andbeamtalk_class_metadata:put_class_variables/2. -
This guard replaces the unguarded ADR 0129 static calls, which closes BT-4126, BT-4133 and BT-4134 for class methods.
-
Amended (BT-4316, 2026-11-04): one read per static call target. The shipped guard read the class's flag, called
function_exported/3anderlang:whereis/1per send (row 3a: 7.03 reductions). A static send now reads onepersistent_termkey per call target,{Module, FunName, Tag}(Tagis the metaclass tag of the class the call site names), whose value is the class reference the callee receives:let ClassSelf = case call 'persistent_term':'get'( {'bt@app@point', 'class_origin', 'Point class'}, 'false') of <'false'> -> call 'beamtalk_class_shadow_flags':'static_call_target'( {'bt@app@point', 'class_origin', 'Point class'}, 'Point', 'origin', 1) <Ref> -> Ref end in case ClassSelf of <'false'> -> <today's class_send> <_> -> call 'bt@app@point':'class_origin'(ClassSelf) endstatic_call_target/4installs the key, under the class's tag lock, only whiledirect_call_ok/2holds, the dynamic path's in-caller owner (beamtalk_class_dispatch:in_caller_owner/2) names this class, module and function, the function is exported and the registered class process is alive. Every erase of the class's derived flag (and so every ancestor write) erases its static call keys, and so does the class monitor's'DOWN'.- The tag is part of the key. A module is not unique to one class over
time:
renameTo:keeps the module and process, and a removed class's module name can be reused. A call site still naming the old class misses the new class's key and asks for its own class, so it raisesclass_not_foundas before, instead of running with the other class's reference. - Reload has no
undefwindow. A compiled module's-on_loadhook registers the class synchronously, and the VM keeps the old code current until the hook returns. Both of the reload's erases therefore land while the old code still exports every function a key names. Any install after the second erase reads the new metadata, in which a dropped method is no longer the owner, so it declines. No key names a dropped function once the new code is current, so a static send of it takes the fallback, as the BT-4301 per-sendfunction_exported/3check ensured. A method the reload adds is installed only once the new code exports it. - A declining call site costs more per send. A site that never gets a
key (shadowed, class variables in the chain, another owner, a dead class)
runs the unlocked check on every send and then
class_send. That costs more than the old one-read decline, but less than theclass_sendit leads to. A negative answer is not cached, because every change that lets a site qualify would need to erase it.
See the
beamtalk_class_shadow_flagsmoduledoc andbenchmarks/vs-elixir/results/bench-bt4316.md. - The tag is part of the key. A module is not unique to one class over
time:
-
Amended (BT-4409, 2026-11-05): constructors are guarded too. The BT-4099 constructor fast path was the last unguarded compile-time verdict on the class side. It now runs behind the same guard:
- Static
X newon an eligible Value uses the static call key{Module, new, Tag}, with arity 0. The hit path callsModule:new(), which takes noClassSelf, so the key's class reference only marks it as installed. Sincenewis process-bound,in_caller_owner/2never names it.static_call_target/4asks the dynamic path's question instead:beamtalk_class_dispatch:lookup_direct_constructor(Pid, new, [])must answer{ok, Module}. The class's bakeddirect_constructorsset is published after a reload's erases, so a change to it erases the class's keys too. - Dynamic
new/new:requiresdirect_call_ok/2. A class-side extension or ADR 0084 runtime fun ofnew/new:on the class itself then takes the fallback, and the class process's{new, _}handler runs it. An ancestor's class-sidenewextension does not shadow the class's own synthesizednew, as on the class-process lookup. - The
new:chain fact is decided at run time. Rust bakes a per-class fact into__beamtalk_meta/0,defines_user_class_new1: "this class defines a class-sidenew:that is user code".Valueis exempt, because itsnew:is thebasicNewWithrouter. One Rust helper,ClassHierarchy::defines_user_class_new1, decides the fact for the bake and for the compile-time chain walk. A dynamicnew:builds in the caller only whilebeamtalk_class_metadata:chain_has_user_new1/1finds no ancestor that publishes a usernew:or has a runtime-funnew:. The walk fails closed on a missing row, link or fact, and on a cycle. It is memoised inbeamtalk_class_derived_cache. So an ancestor reloaded with a usernew:is honoured by already-loaded descendants without recompiling them. BT-4126's reload warning becomes informational. - Static
new:stays onclass_send; its fast path is the dynamic one.
- Static
-
Amended (BT-4442, 2026-11-05): one static call key per class. Each
persistent_termupdate (a put, or an erase of a present key) waits for thread progress on every scheduler, about 0.1 to 0.3 ms, and OTP has no batch erase. With one key per call target, a metadata write on a base class paid one erase per warm target in the class and every descendant: 122 ms for 50 subclasses × 10 targets (results/bench-bt4431.md). The targets of one class now share one key, the atom'<Name> class static calls', whose value maps each installed call-site key (the literals above, unchanged) to its class reference:let ClassSelf = case call 'persistent_term':'get'('Point class static calls', ~{}~) of <~{{'bt@app@point', 'class_origin', 'Point class'} := Ref}~> -> Ref <_> -> call 'beamtalk_class_shadow_flags':'static_call_target'( {'bt@app@point', 'class_origin', 'Point class'}, 'Point', 'origin', 1) end in ...as above...- Cost model. An erase of a class's keys is at most two updates (the
derived flag and the map), however many targets are warm, so
invalidate/1over N descendants is at most 2(N+1) updates. Installing a target is one update (the class's map is copied with the target added), as before; a target already present is never rewritten. But every install after a class's first replaces a present value, which, like an erase, releases a literal area that each process's heap is checked against; a put of a new key did not. The cost moves from the writer's synchronous erase to the lazy re-warm, and grows with the node's process count and heap sizes. The 50 × 10 case falls to about 20 ms, the cost of the 50 × 1 case (results/bench-bt4442.md). - The hit path stays one read. The map match is a BEAM instruction, not
a call, and rows 3a-3c stay at 3 reductions. The key is an atom because
hashing a tuple
persistent_termkey is the dearer part of the read: a{Name, Tag}tuple key with the map match measured ~36 ns per hit, the atom key ~21 ns, against ~26 ns for the per-target tuple key it replaces. The spaces keep it apart from any module's key and from every class tag. - Targets are erased together. Any write that could change one target erases the class's whole map; the next send of each call site re-installs its own target. Erasing only the descendants or targets a write can affect was rejected: every eraser would have to classify its write, and one missed case would run a stale function.
- Cost model. An erase of a class's keys is at most two updates (the
derived flag and the map), however many targets are warm, so
-
Amended (BT-4464, 2026-11-05): explicit FFI-forward
new. A class whose own class-sidenewbody is one plain FFI forward ((Erlang mod) fnwith literal arguments) orself delegateon anative:class bakesnewintodirect_constructorstoo (ClassHierarchy:: explicit_class_new_builds_in_caller), on a concrete non-actor class with no class variables in its chain. An FFI call listed in the curated class-method FFI table (ets new:,spawn_link,gen_udp open:, ...) keeps the class process: neither lint inspectsnew, and the class process used to own what it creates. Itsnew/0isclass_new('undefined'), which is all the{new, []}handler would run, so the guarded direct call is the same call minus the hop.Reference,Random,QueueandAnnouncertake it; a body that sends toself(self basicNew) does not. -
Amended (BT-4475, 2026-11-06): explicit forwarding
new:. The same rule (ClassHierarchy::explicit_class_constructor_builds_in_caller) covers a class's own class-sidenew:whose body is one plain FFI forward (literal or parameter arguments, not a table row) or a send toselfof one of the class's own in-caller class methods whose own body is such an FFI forward or its selector primitive (class new: elements => self withAll: elements,class withAll: list => @primitive): the target's body is checked, one hop only. A non-mapnew:has no direct constructor, and the class process's{new, [Arg]}handler only called that class method, so codegen leavesnew:out of the class's published process-bound selectors and the send runs the method in the caller throughin_caller_owner/2, behind the samedirect_call_ok/2guard. The list speaks only for the receiving class: a subclass inheriting thenew:keeps its full list.List,Set,ArrayandTupletake it;AtomicCounter new:(self delegatecreating a named ETS table owned by the running process) does not, sonew:never acceptsself delegate.
Amended (BT-4317, 2026-11-04): a class literal always denotes that
class. Inside a class method of Base, an explicit own-class send Base foo binds self to Base, as the same send does anywhere else and as
Smalltalk does; only self foo late-binds to the receiver. It used to run
with the receiver as self when the method was inherited by a subclass C
(self name answered #C, class variables were C's and Base new built a
C), a leftover of the own-class deadlock workaround. The send now cases on
the running receiver's tag: when it is Base, the own-class direct path
above runs it in the current process (for a stateful chain that is Base's
own class process, which a class_send would deadlock on); otherwise it is
the ordinary literal class send to Base (the guarded static call or
class_send), which runs in the caller for a stateless chain and in Base's
class process for a stateful one, never in the process already running.
Rejected: keeping "receiver as self" (it contradicts what a class literal
evaluates to), and passing Base's reference to a direct call in the
current process (for a stateful chain Base's class variables live in
Base's process, not C's). Consequence: if a Base class method running
in Base's process sends to C, and C's inherited method then sends
Base bar, that is a cross-process cycle between two stateful classes, like
any other.
3. What moves into the caller
- Blocks. A block passed into such a class method runs in the caller.
- Re-entrant sends to the same class stop raising
dispatch_error. - Inside an actor, the block runs at home in the ADR 0137 sense.
- Re-entrant sends to the same class stop raising
- Process-local effects belong to the caller:
self()and the process dictionary;- links, monitors and timeouts;
- any resource created through FFI, such as ETS tables, ports and timers.
- Class-process services become no-ops. The class process adds these
around a call; the caller already is the context they recreate:
- seeding the caller's session context;
- mirroring
beamtalk_dispatch_caller_pid; - adopting the entry group leader;
- the 60 s / 300 s class-call timeouts.
- One reader needs checking.
File open:mode:'s handle-ownership rule reads the mirrored caller pid. Phase 1 must make its no-mirror path resolve toself(), with a test in the REPL, inbeamtalk runand inbeamtalk test.
4. Migration lint
The class-process-ffi lint is inverted into class-method-process-resource.
It warns on FFI in a class method of a stateless class that does either of
these:
- creates a process-owned resource (
ets:new,open_port,spawn_link,link,monitor,register, timers); - relies on
self()or the process dictionary.
The message names the fix: an Actor, or classState:. The lint reuses the
existing validator and its curated table.
Example
Object subclass: Paths
class join: a :: String with: b :: String -> String => a ++ "/" ++ b
class each: items :: List do: aBlock -> Nil =>
items do: [:x | aBlock value: x]
nil
Paths join: "a" with: "b" // => "a/b", computed in the caller: no class-process hop
Paths each: #(1, 2) do: [:x | Paths join: "x" with: x printString]
// => nil; the block runs in the caller, so re-entering Paths is fine
// (today: dispatch_error)
A class with class state keeps its process:
Object subclass: Counter
classState: count :: Integer = 0
class bump -> Integer => self.count := self.count + 1
Counter bump // => 1, in Counter's class process, as today
Error behaviour
There are no new errors. Every error a class method raises today is raised
with the same value. The one difference is where: an exit inside a
stateless class method now exits the caller, as an Erlang function call
would, instead of being relayed back from the class process.
Prior Art
- Pharo / Squeak / Newspeak. Class methods are ordinary methods on the metaclass and run in the caller. There is no class process. Adopted.
- Erlang/OTP. Module functions run in the caller. You get serialisation by owning a process (a gen_server), never as a side effect of where code is defined. Adopted: the class process exists only for class state, and an Actor is the serialising home.
- Elixir. Module functions, including struct constructors and
defdelegate, run in the caller; anAgentorGenServeris the explicit owner of shared state. Adopted, as for Erlang. - Akka / Pony. Shared mutable state lives in an actor; everything else is a plain call. Consistent with this ADR.
User Impact
- Newcomer. Class methods behave like functions in a module, which is what someone from Python, JS or Elixir expects. There is no hidden process to learn about until they declare class state.
- Smalltalk developer. This is how Pharo has always worked. Sends keep
their semantics: DNU,
perform:, extensions and live patching all behave as before. - Erlang/Elixir developer. Caller-sensitive FFI such as
gen_tcp:accept,controlling_processandself()does what it says. A serialising process is something they write, as on OTP. - Production operator.
- The per-class node-wide queue, and the latency spikes and timeouts behind it, go away for stateless classes.
- Per-class load no longer shows as a class-process mailbox in
observerorTracing. - Resources created in stateless class methods belong to the caller; the migration lint flags them.
- Tooling developer. One rule, read from one place. The lints and codegen
share
is_direct_call_eligibleand the process-bound selector list.
Steelman Analysis
A. Only sealed, stateless classes run class methods in the caller
- 🏭 Operator: "A smaller blast radius: only classes someone sealed move."
- ⚙️ BEAM veteran: "A sealed class has no subclasses, so there is no subclass-with-class-variables case to guard."
- 🎨 Language designer: "It extends ADR 0129 instead of replacing it."
Why not:
- Open-class class methods stay at ~145 red, behind the node-wide queue.
sealedkeeps deciding which process code runs in.- Framework base classes are open by design.
B. Status quo (ADR 0129's rule)
- 🎩 Smalltalk purist: "The class object is an object, and objects in Beamtalk are processes."
- 🏭 Operator: "Nothing changes, nothing to migrate."
Why not:
- The class process is already a source of surprises: the
class-process-ffilint,dispatch_erroron re-entry, and ADR 0109's special case. - The unguarded verdicts behind BT-4126, BT-4133, BT-4134 and BT-4037 stay.
C. Unguarded trust in compile-time verdicts, extended to every stateless class
- ⚙️ BEAM veteran: "No flag read at all."
Why not: it is the source of the four staleness caveats. A
persistent_term read costs about one reduction.
Tension points
- Purists against pragmatists. Purists may want class methods to stay "inside the class object". Pharo, Erlang and newcomer intuition all agree they run in the caller.
- Operators gain and lose. They gain no node-wide queue and no timeouts. They lose implicit ownership of resources created in class methods; the migration lint and the CHANGELOG cover that.
Alternatives Considered
Only sealed, stateless classes run in the caller
Considered with the project owner and rejected: process placement should follow class state, not subclassing (steelman A).
Status quo
Rejected (steelman B).
Unguarded compile-time verdicts
Rejected (steelman C).
Consequences
Positive
- Class methods of stateless classes cost a flag read plus a remote call,
instead of ~145 red / ~450 w and a node-wide queue (rows 3b, 3c).
Exception signal:andResult ok:follow from the general rule. - Caller-sensitive FFI in class methods works, so the original complaint
behind the
class-process-ffilint disappears. - Re-entrant class methods stop raising
dispatch_error. ADR 0109'sopen:do:special case becomes an instance of the general rule. - One class-side rule replaces two, and the unguarded verdicts behind BT-4126, BT-4133, BT-4134 and BT-4037 go away.
- Method-level
class sealedno longer matters for dispatch. That removes the last blocker on ADR 0049 (removing method-levelsealed).
Negative
- A semantic change for stateless class methods.
- Resources created through FFI (ETS tables, ports, timers, links, monitors) now belong to the caller and die with it.
- Implicit serialisation through the class process is gone.
- A hung class method blocks its caller, instead of timing out a
gen_server:call. - An
exitinside the method exits the caller.
- Blocks passed from an actor into a stateless class method now run at
home (ADR 0137). They see and write live state, and the
actor-state-abroadlint's findings change. - Every class-side direct call pays a guard. It costs one
persistent_termread, where ADR 0129's static calls paid nothing. Row 3a goes from 2 red to ~3.
Neutral
- Classes with class variables keep their process unchanged.
- Catalina's throughput barely moves (~1% of per-message CPU). The win is correctness and the removal of a node-wide queue.
Implementation
All phases sit under epic BT-4297, after BT-4292, the differential harness of epic BT-4291.
Phase 0 — differential rows (S). Add these class-side rows to BT-4292's differential fixture:
- an inherited class method touching a stateful parent's class variables (its error must not change);
- an extension or runtime fun on an ancestor of the receiving class;
- each process-bound selector;
- a re-entrant block.
Results and error shapes must be identical. Add vs-elixir rows for an open-class class method.
Phase 1 — runtime (M).
- Add "has class variables" to the inputs of
beamtalk_class_shadow_flags:direct_call_ok/2, erased on class-variable changes under the same per-tag lock. class_send_plain/3runs the method in the caller when the flag holds, using the owner memo, which also checks rule 2.set_runtime_class_methods/2invalidates the derived cache.- The process-bound selector list arrives as metadata.
- Pass the real class reference as
ClassSelf. File open:mode:resolves ownership toself()without the mirror.- Files:
beamtalk_class_dispatch.erl,beamtalk_class_shadow_flags.erl,beamtalk_class_derived_cache.erl,beamtalk_class_metadata.erl,beamtalk_object_class.erl,beamtalk_file.erl. - Subsumes BT-4073 and BT-4114.
- Gate: rows 3b and 3c ≤ 10 red, with no
'$gen_call'in the profile.
Phase 2 — codegen and lint (M).
- Literal-receiver and class-side self-sends to own selectors emit the guarded direct call, and ADR 0129 static calls get the same guard.
- Retire the
nilClassSelfconvention. - Add the
class-method-process-resourcelint (§4). - Update
is_direct_call_eligible(class_info.rs, shared with theclass-state-abroadlint) to the new rule, and use it in REPL codegen too (crates/beamtalk-repl/src/codegen.rs,set_direct_call_eligible_from_hierarchy).
Phase 3 — docs (S).
- Amend these ADRs:
- 0129 (constraint 1);
- 0013 §4;
- 0010 (stale "zero-overhead class sends");
- 0137 (blocks in class methods);
- 0130 (§5's "class sealed, stateless" wording; the in-caller rule adds no class-variable home);
- 0109 (its
open:do:exception is now the general rule); - 0084 (runtime funs apply in the caller when the class has no class variables).
- Update
docs/beamtalk-language-features.md: Class-Side Methods, Passing Blocks Through Class Methods and Self-Sends and Overrides. - Update CLAUDE.md's "Blocks into class methods" rule.
- Add a CHANGELOG migration note.
Implementation Tracking
Epic: BT-4297
Issues:
- Phase 0: BT-4298, class-side differential rows and vs-elixir rows. Blocked by BT-4292.
- Phase 1a: BT-4299, the flag's class-variable input and erasure points, plus process-bound selector metadata.
- Phase 1b: BT-4300, dynamic class sends run in the caller. This subsumes BT-4114.
- Phase 2a: BT-4301, guarded static class sends, the
nilClassSelfretired, and one eligibility rule. - Phase 2b: BT-4302, the
class-method-process-resourcelint. - Phase 3: BT-4303, REPL-protocol e2e test, docs, ADR amendments, CLAUDE.md and CHANGELOG.
Status: Done (2026-10-30). Follow-ups: BT-4316 (row 3a), BT-4317 (done, amendment in §2), BT-4307.
Migration Path
This ADR changes behaviour only for class methods of classes without class variables.
-
Resources owned by the class process. Code that creates an ETS table, port, timer, link or monitor in such a class method, and expects it to outlive the caller, must move ownership:
- into an Actor (preferred: an
Actor subclass: TableOwnerthat creates the resource ininitialize), or - into a class with
classState:, which keeps its process.
The
class-method-process-resourcelint flags direct FFI cases. - into an Actor (preferred: an
-
Implicit serialisation. Read-modify-write sequences in class methods that relied on queuing must go through an Actor. No lint can detect this, so the CHANGELOG calls it out.
-
Suppressions.
@expect class_process_ffisuppressions in stateless classes become unnecessary. The lint reports them as unused, per the existing@expectrules. -
Erlang callers of
class_sendsee the same results, now computed in their own process.
References
- Related issues: BT-4118, BT-4112 (epic), BT-4291 (guarded static dispatch epic), BT-4292, BT-4295, BT-4073, BT-4114, BT-4115, BT-4126, BT-4133, BT-4134, BT-4037, BT-3700, BT-3022.
- Related ADRs: 0025 (its 2026-10-29 amendment is the guard contract this ADR applies), 0010, 0013, 0049, 0066, 0084, 0100, 0109, 0112, 0129, 0130, 0137.
- Documentation:
benchmarks/vs-elixir/README.md,docs/development/benchmarks.md(BT-3676/3690/3700),docs/beamtalk-principles.md§4.