Skip to content

From code to cells

This page follows real declarations through to running cells, twice. The first example has blocks that share dependencies; the second has blocks that need incompatible versions of the same library. Each shows the source, the closure each block resolves to, and the cells the kernel runs.

Three rules shape both examples:

  1. A block is one source module. It resolves to one closure and runs in one cell.
  2. A block imports what its own @xratu declares. The foundation and runtime.xratu supply bytes to reuse; the declaration grants the import.
  3. Native libraries reach every cell from two shared places: the foundation's exported baseline and runtime.xratu apt. A block's own apt reaches only that block.

The shared setup

Both examples run in one project, on one foundation.

The foundation exports libmagic1 in its system baseline, and its component store carries NumPy 1.26.4 and requests 2.31.0. The project adds SQLite for every block:

# runtime.xratu
kind: RuntimeSpec
metadata:
  id: runtime.documents
  version: "1.0.0"
document:
  strategy: "standard"
  build:
    build_instance: "standard"
    kernel_version: "stable"
  foundation:
    reference: "registry.example/xratu/foundation:stable"
    profile: "standard"
  identity_mode: "onprem_mtls"
  dependencies:
    apt:
      - sqlite3

Example 1: blocks that share dependencies

Two blocks, each its own module. A workflow passes extract's output to classify.

# extract.py
from sdk import xratu

@xratu(["numpy>=1.26,<2", "requests>=2.31"])
class Extract:
    def run(self, document: bytes) -> list:
        ...
# classify.py
from sdk import xratu

@xratu(["numpy>=1.26,<2", "requests>=2.32"])
class Classify:
    def run(self, features: list) -> str:
        ...
flowchart TB
  X["extract.py<br/>@xratu numpy>=1.26,<2, requests>=2.31"]
  SH["Shared<br/>foundation: libmagic baseline,<br/>numpy 1.26.4, requests 2.31.0<br/>runtime.xratu apt: sqlite3"]
  Y["classify.py<br/>@xratu numpy>=1.26,<2, requests>=2.32"]
  X --> CA["Closure A<br/>numpy 1.26.4 from the foundation<br/>requests 2.31.0 from the foundation<br/>sqlite3 project-wide"]
  SH --> CA
  SH --> CB
  Y --> CB["Closure B<br/>numpy 1.26.4 from the foundation<br/>requests 2.32.3 built for this closure<br/>foundation requests excluded<br/>sqlite3 project-wide"]
  CA --> KA["Cell A, in the kernel<br/>imports numpy, requests 2.31.0<br/>loads libmagic, libsqlite3"]
  CB --> KB["Cell B, in the kernel<br/>imports numpy, requests 2.32.3<br/>loads libmagic, libsqlite3"]
  KA -.->|"typed value through the kernel"| KB
Component Source Scope Cell A Cell B
libmagic1 Foundation baseline system_baseline Loads it Loads it
sqlite3 runtime.xratu apt project_wide Loads it Loads it
NumPy 1.26.4 Foundation component store reusable_block Imports it Imports it
requests 2.31.0 Foundation component store reusable_block Imports it Excluded
requests 2.32.3 Built for closure B reusable_block Unavailable Imports it

Both blocks declared NumPy, and the foundation's NumPy satisfies both, so both closures bind the same component digest and both cells map the same immutable bytes. Each cell still runs its own provider process with its own NumPy module state.

requests resolves differently for each block. The foundation's 2.31.0 satisfies extract, so closure A uses it. It fails classify's requests>=2.32, so the resolver installs 2.32.3 for closure B alone, records the foundation candidate as version_mismatch, and excludes it from closure B. Each cell imports its own requests, and each version stays in its own cell.

The two cells' import views, trimmed to module_ownership, show the split:

{
  "cell_a": [
    {"module_name": "numpy", "owner_kind": "component", "owner_digest": "sha256:f00d…"},
    {"module_name": "requests", "owner_kind": "component", "owner_digest": "sha256:1111…"}
  ],
  "cell_b": [
    {"module_name": "numpy", "owner_kind": "component", "owner_digest": "sha256:f00d…"},
    {"module_name": "requests", "owner_kind": "component", "owner_digest": "sha256:2222…"}
  ]
}

Example 2: blocks that need incompatible versions

score needs NumPy 2 and an OpenMP runtime that neither the foundation nor the project provides. The workflow passes extract's output to score.

# score.py
from sdk import xratu

@xratu(["numpy>=2.1"], apt=["libgomp1"])
class Score:
    def run(self, features: list) -> float:
        ...
flowchart TB
  X["extract.py<br/>@xratu numpy>=1.26,<2"]
  SH["Shared<br/>foundation: libmagic baseline, numpy 1.26.4<br/>runtime.xratu apt: sqlite3"]
  Z["score.py<br/>@xratu numpy>=2.1, apt libgomp1"]
  X --> CA["Closure A<br/>numpy 1.26.4 from the foundation<br/>sqlite3 project-wide"]
  SH --> CA
  SH --> CC
  Z --> CC["Closure C<br/>numpy 2.1.3 built for this closure<br/>foundation numpy 1.26.4 excluded<br/>libgomp1 for this block<br/>sqlite3 project-wide"]
  CA --> KA["Cell A, in the kernel<br/>imports numpy 1.26.4<br/>loads libmagic, libsqlite3"]
  CC --> KC["Cell C, in the kernel<br/>imports numpy 2.1.3<br/>loads libmagic, libsqlite3, libgomp"]
  KA -.->|"typed value through the kernel"| KC
Component Source Scope Cell A Cell C
libmagic1 Foundation baseline system_baseline Loads it Loads it
sqlite3 runtime.xratu apt project_wide Loads it Loads it
NumPy 1.26.4 Foundation component store reusable_block Imports it Excluded
NumPy 2.1.3 Built for closure C reusable_block Unavailable Imports it
libgomp1 Built for closure C reusable_block Unavailable Loads it

The foundation's NumPy does not satisfy numpy>=2.1, so the resolver records it as version_mismatch, builds NumPy 2.1.3 for closure C, and lists the foundation component in excluded_foundation_components. Cell C's search path holds NumPy 2.1.3 alone, and cell A's holds NumPy 1.26.4 alone.

libgomp1 is a block-only system package, so its loader root carries root_scope: reusable_block and appears in cell C only. Cell A, and every other block in the project, loads the foundation baseline and sqlite3 as before.

extract hands score a typed value defined by score's interface, such as a list of floats. NumPy 1.26 produces it in cell A and NumPy 2.1 reads it in cell C; each array object stays in the cell that created it.

What each example changes, and what stays

  1. Raising classify's requests constraint reaches closure B and cell B only.
  2. Adding libgomp1 to score's apt reaches closure C and cell C only.
  3. Adding a package to runtime.xratu apt reaches every closure and cell in the project.
  4. Changing the foundation reaches every closure and cell in the project.

Each change produces new closure digests for the closures it reaches, and new cells to run them. Cells for the other closures keep running unchanged. See Closure reconciliation and upgrade.

Related: Three declaration scopes, Dependency declaration for how one block's declaration shapes its cell, and Version coexistence.