Skip to content

ADR 0010: The pygo surface

Status: Final Type: Feature Created: 2026-07-28 Consolidated: 2026-08-07; absorbs 0012 (handles, task arguments, closures) and 0013 (select), both retired Amended-By: 0011 (statuses became POST exceptions and the early over-restrictions fell with it), 0016 (pygo.net), 0019 (tasks and CPython), 0076 (a name Python shares between the two functions is a cell) Related-ADRs: 0006 (the boundary pattern this repeats). Provenance: arbitrations A26–A41.

Context

pygo is the concurrency profile of POST Python: structured task groups, Go-semantics channels, first-class select, scope cancellation. The dev-mode kernel runs the whole surface on threads under stock CPython (every pygo program is valid Python: rule D1). Scheduler A is the kernel's C99 port behind the ABI in pygo_runtime.h. The compiler's job is to lower the surface onto that ABI so that the compiled program and the interpreted kernel are two implementations of one specification, compared at program level.

Decision

The import, and the wrapper unit

from pygo import ... is a distinct import kind: it differs from POST modules, compile-time intrinsics, declined boundary imports, and from python import. These names produce no CPython value and make the binary link pygo's runtime. The lowered surface is a closed set. Everything else pygo exports is refused at the import: a name the reader can see imported must not compile and then fail a few lines later.

The ABI is reached through one wrapper unit. It is the only translation unit in a build that includes pygo_runtime.h: the Python.h rule applied to a second foreign header. The C backend emits prototypes only and links the same extern unit QBE and LLVM link. The ABI forces the wrapper. The header speaks size_t/bool/out-parameters where the IR speaks int64_t/double/one pointer width. Elements move through their address, which the IR cannot take, so helpers are per-element-type and take the element by value. Every blocking entry returns a status that needs one place to be handled. The runtime is compiled from the pygo tree, because the binary must contain the runtime the checkout says it is. All of it is gated on a predicate over the IR, so a program that never touches pygo links what it always linked, on a machine with no pygo at all. Feature-test macros apply only to the runtime unit.

Channels

Chan[T]'s element type is resolved at the construction site and tracked on the slot. postpile has no generics. A parameterised dtype would be a far larger decision. The element types are the four that cross a C boundary (int, bool, float, str); a narrower one is refused, because widening it would change the program's memory layout. Elements cross at their wire width from a single table read by the IR check, the C declarations, and the glue definitions, because an ABI-width mismatch is the class of bug all the backends get wrong in the same direction.

SendChan[T] and RecvChan[T] are compile-time restrictions with no representation. pygo spells them as structural Protocols over one runtime object, so a Chan satisfies both. The compiler enforces what the interpreter cannot: recv on a send end and send/close on a receive end are refused; only senders close. The compiler is thereby stricter than CPython at run time and as strict as a conforming interpreted program (pygo's lint gate enforces the direction). Every call site is checked for both the element type and the direction, since neither lives in the bare dtype.

Scopes

with is a general statement form with a known set of context managers. POST has no object model, so there is no __enter__/__exit__ to call on an arbitrary value. An unrecognised manager gets a diagnostic. The exit is emitted on the unwind path too, so an exception in the body still joins the scope. TaskGroup() outside a with is refused (the scope's exit is the join, so a group built elsewhere never joins). A return/break/continue that would leave the body is refused (that path would skip the join: the leaked-scope hazard one frame earlier).

Tasks and closures

The spawn glue is generated C. It is the only place in a build that knows both the callee's C signature and the ABI's, so only it can bridge them; the frontend emits one call to the generated entry point and nothing else. A task's arguments live in a box that is a move. The spawner allocates and writes it, the trampoline copies it out and frees it before entering the body, so no instant exists at which both sides may touch it. The spawn entry returns whether it spawned, so the allocator frees what the runtime declined. The trampoline is needed because calling void worker(void) through void (*)(void *) is undefined behaviour. It is also the single generated site that reports task failure.

Closures are lambda-lifted: captures become leading parameters and the def statement lowers to nothing, with no environment and no indirect call. Python captures by cell and this captures by value; the sendability rule makes the two identical, since a spawned function may capture only immutable values and handles, for which cell and value are indistinguishable. The shapes where they would differ are refused: a captured name rebound, bound after the def, or bound in a loop the def shares (Python's one shared cell versus a per-closure value).

A lifted nested def is also called directly. An ordinary call passes the captures ahead of its arguments, reading them in the calling frame; since a capture the enclosing function rebinds is refused, the value read is the one Python's cell holds. A nested def that calls a sibling captures the sibling's captures too. The sendability rule (D15) is checked at a task's spawn site, so a function called directly may capture a list, which it reads by reference, as Python does; spawning it is still refused.

Crossings are enforced under pygo's PG numbers, so the compiler and the advisory checker speak one number. Three crossings are refused: a CPython object (runtime-classified, 0019), anything that is neither value nor handle, and a list or array. The rule defines a list or array crossing as a move; the compiler has no move tracking, so it refuses, since tasks sharing one buffer is what the rule exists to stop. Task-rebinds-enclosing-variable needs no check at all: it requires nonlocal/global, which POST refuses one phase earlier.

select

select's result is a sum tagged by a case key: one value whose type depends on which case fired. POST has no such value. 0029's unions are tagged by a type, and two receive cases of one element type have the same one. So select is recognised in the statement that consumes it: the (key, value) pair is never built. Three consuming forms lower. The first is match select(...) with literal-key arms (each arm statically knows its case, hence its type, so the bound value has a type without the pair ever having one). The second is positional i, v = select(...) where the pair would have a type (all receives, one element type, no timeout=). The third is bare-statement select(...). Everything else (binding, passing, testing, returning the result) is a diagnostic naming these forms. A key-tagged sum would be a value-domain decision made once and visible everywhere, so a lowering does not introduce one.

The shape has these consequences:

  • match over a select lowers its two pattern shapes and nothing else. A match over a union is 0029's eliminator, a separate parser: the subject decides which one applies.
  • One source select lowers to begin … per-case setters … wait … per-arm reads, with the case array as per-task state in the wrapper unit and no handle threaded through; "one select in flight per task" is a property of the unit. Every channel expression and send value is evaluated into a local before begin, in source order. Only one helper blocks, so only one can raise.
  • Commit atomicity is the runtime's; the lowering's whole job is to leave it intact: one select(...) in source is one wait in the IR, because any retry turns one committed communication into zero or two. The bug is invisible whenever the retry commits the same case, which is why it is pinned structurally.
  • timeout= is relative seconds over an absolute-instant ABI, clamped at zero, because a negative instant is the ABI's no-deadline sentinel. timeout=0.0 is Go's default: through the same mechanism (an instant already past).
  • The closed-channel status serves two rules, disambiguated from the case array: the runtime's scan visits a send case's closed test before any readiness test, so "no live case" implies no enabled send case. The status therefore means send-on-closed iff the array holds an enabled send case, and drained-out otherwise. That guarantee follows from the algorithm and is written into pygo_runtime.h as an ABI promise, since the correctness discipline forbids a silent dependency on an undocumented property.
  • An arm naming a case the call does not have is refused. This is the one place the compiler is stricter than the interpreter, where the branch would merely never fire. Named cases exist so that editing a case list cannot silently disarm a branch; a mistyped name is that failure, and only the compiler can see it.
  • Fairness is checked as a property: the interpreted kernel makes random choices, so the runs must agree on the property, while the split may differ.

Consequences

  • The pygo corpus compiles against the interpreted kernel as its oracle: the frontend is clean, interpreted output equals the corpus goldens, and compiled stdout is byte-equal to interpreted.
  • __name__ is "__main__" in script mode (an executable built from a file is what CPython calls running it as a script) and a module name otherwise; print(..., sep=, end=) lowers with no new formatting rule.
  • The contracts a header cannot enforce (the spawn box's move rule, the nothing-between-begin-and-read rule, once-per-task failure reporting) are written where the claim is. Enforcement belongs to each consumer; a second compiler consuming this ABI inherits the obligations by reading them.

Alternatives considered

  • Materialising select's pair (pick a representation and get on): rejected. A key-tagged sum would be decided once for the whole value domain, which a lowering must not pre-empt. The design's idiom is match, which needs no pair.
  • Folding pygo imports into the CPython boundary: rejected. These names produce no CPython value and link a different runtime; conflating them would make --pure meaningless.
  • A parameterised Chan dtype: rejected. That is generics; the element-on-the-slot rule serves every container in the language and stays consistent here.
  • Widening narrow channel elements: rejected. Layout belongs to the channel, and promotion would silently change semantics.