KITSOKI
Learn · Notebook

What are Processes and Things?

Any system, in two words: what a Thing is, what a Process is, and how a program stops being about the system and becomes it.

Kitsoki·
Read the paper and learn more
Contents
  1. Two words for any system
  2. A Thing: what a system is made of
  3. A Process: how a system moves
  4. Five systems, the same two words
  5. Watch it

Every system is two kinds of thing. A Thing is what the system is made of: a cup, a basket, a tower, a basin. A Process is how it moves: one stage at a time, by named moves. Kitlark writes both as plain classes you can read aloud, and checks them whole before anything runs. This recipe defines the two words on a coffee order, then shows the same two words on five very different systems, each as a few lines of its real model and a journey of the real application.

Two words for any system

A system is the Things it holds and the Processes that move them.

Word Says Written as Example
Thing what it is and what it is made of a Record: typed fields, parts that are Things a cup with a size and a volume
Process how it moves a Process or Story: stages, and a method for each move Ordering → Poured

A Thing with a life has a Process; a Process moves Things. Everything else in a model (its laws, its measures, its effects on the world) hangs off one of the two.

A Thing: what a system is made of

A Thing names itself, says what it is in a sentence, and types its fields. Its parts are Things, each with its own owner, and it carries the laws its parts must keep. The Villareal aquifer is water underground, owned by the state's water department; it holds well W-3 of the irrigation district and well RF-1 of Rivera Farms; and its law, one line in holds, is that a well is screened within its aquifer:

from kitlark import Record, Field, Finding
from std.data.collections import Seq


class Well(Record):
    """A well, and the depths its screen draws water from."""
    name: str = Field(min_length=1)
    owner: str = Field(min_length=1)
    screen_top_m: float = Field(ge=0)
    screen_bottom_m: float = Field(ge=0)


class Aquifer(Record):
    """Water underground, and every well that draws on it."""
    name: str = Field(min_length=1)
    owner: str = Field(min_length=1)
    top_m: float = Field(ge=0)
    bottom_m: float = Field(ge=0)
    wells: Seq[Well] = []

    def holds(self, w: Well) -> bool:
        """A well is screened within the aquifer."""
        return self.top_m <= w.screen_top_m and w.screen_bottom_m <= self.bottom_m

    def check(self) -> Seq[Finding]:
        """Each well keeps the law."""
        out = []
        for w in self.wells:
            if not self.holds(w):
                found = Finding(code="screen_outside", subject=w.name)
                out = out + [found]
        return out

The check runs on the parts. With illustrative depths, RF-1's screen reaches below the aquifer's floor, so the aquifer reports it:

aquifer = Aquifer(name="Villareal aquifer", owner="State water resources department", top_m=40, bottom_m=310, wells=[
    Well(name="W-3", owner="Villareal Irrigation District", screen_top_m=80, screen_bottom_m=220),
    Well(name="RF-1", owner="Rivera Farms", screen_top_m=60, screen_bottom_m=340)])
aquifer.check()

A Thing holds its laws the same way at every scale: the whole basin holds the river, the reservoir and the aquifer, and its water budget closes over all of them.

A Process: how a system moves

A Process is stages and the moves between them, over the data it carries. Coffee pours a Cup and starts at Ordering. A move is a method with typed inputs; each case of its match says where the Process goes next:

"""Coffee: an intent with a typed slot, and a match that handles every size."""

from kitlark import Record, Field
from kitsoki.story import Story


class Cup(Record):
    """A cup as poured."""
    size: str = Field(min_length=1)
    millilitres: int


class Coffee(Story):
    """Pours one cup of the size asked for."""
    customer: str = "guest"


class Ordering(Coffee.State):
    """Waiting for a size."""
    initial = True

    def order(self, size: str) -> Coffee:
        """Order a small or a large cup."""
        match size:
            case "small":
                return self.to(Poured, cup=Cup(size=size, millilitres=240))
            case "large":
                return self.to(Poured, cup=Cup(size=size, millilitres=470))
            case _:
                return self.stay(hint="We pour small or large.")


class Poured(Coffee.State):
    """The cup is poured."""
    terminal = True
    cup: Cup

Any other size stays where it is and says why. The checker reads the whole Process before it runs: a match that forgets a case, a stage with no way out or a stage nothing reaches is refused with a typed diagnostic. Every move of a Process record emits exactly one typed Event, so every board and measure reads the same history.

Five systems, the same two words

The same two words model a shop, a platform, a plant and a river basin:

System Its Things Its Process Model
A checkout basket, held line, charge, label, receipt ReadingCart → Reserving → Charging → Booking → … observe · stories/checkout.kl
A defect the defect, its change Open → Ready → Claimed → InProgress → Verifying → Done app-change-kl · stories/defect.kl
A refinery tower a run: lanes, spans, steps on one clock each reading a step of the run observe · obs/records.kl
A river basin basin, aquifer, well, generating station its water year Ontolo Simulations (mockup)
A study notebook, model, data, sources notebook → study → system → operations, and back to study on a broken check Ontolo Simulations (mockup)

One checkout across five services, each stage one call. The order is the key, so a resend charges nothing twice:

class Checkout(Story):
    """One order, from the cart to the receipt."""
    order: str = "order-1"
    customer: str = "ada"
    skus: Seq[str] = []
    total_cents: int = 0
    holds: Seq[str] = []
    charge_id: str = ""
    label: str = ""

    def failed(self, error: OpError) -> Checkout:
        return self.to(Abandoned)


class ReadingCart(Checkout.State):
    """Read the customer's basket."""
    initial = True

    def work(self) -> Effect[Basket]:
        return cart.read[Basket](customer=self.customer)

    def done(self, out: Basket) -> Checkout:
        if len(out.skus) == 0:
            return self.to(Empty)
        return self.to(Reserving, skus=out.skus, total_cents=out.total_cents)


class Reserving(Checkout.State):
    """Hold the next line in the warehouse: one call per line."""

    def work(self) -> Effect[Held]:
        return inventory.reserve[Held](sku=self.skus[len(self.holds)], order=self.order)

    def done(self, out: Held) -> Checkout:
        holds = self.holds + [out.hold]
        if len(holds) < len(self.skus):
            return self.stay(holds=holds)
        return self.to(Charging, holds=holds)


class Charging(Checkout.State):
    """Charge the card. The key is the order, so a resend charges nothing twice."""

    def work(self) -> Effect[Charged]:
        return payments.charge[Charged](order=self.order, amount_cents=self.total_cents, idempotency_key=self.order + ":charge")

    def done(self, out: Charged) -> Checkout:
        if out.status != "captured":
            return self.to(Declined, charge_id=out.charge_id)
        return self.to(Booking, charge_id=out.charge_id)

The platform's own work is a Process too. A defect is claimed by one driver at a time:

class Defect(Process):
    """One defect, keyed by its typed failure signature."""
    severity: str = ""
    owner_app: str = ""
    change: str = ""
    promotion: str = ""
    # The run that claimed it (goalseek:<session>): adoption takes only a dead run's claims.
    driver: str = ""
    attrs = ["severity", "owner_app"]


class Open(Defect.Stage):
    """Filed; not yet triaged."""
    role = "entry"

    def triage(self) -> Defect:
        return self.to(Ready)

    def duplicate(self) -> Defect:
        return self.to(Duplicate)

    def wont(self) -> Defect:
        return self.to(WontDo)


class Ready(Defect.Stage):
    """Triaged: a driver may claim it."""

    def claim(self, driver: str) -> Defect:
        """The driver (the Event's actor, e.g. goalseek:<session>) claims it before it opens the change."""
        return self.to(Claimed, driver=driver)

    def reopen(self) -> Defect:
        return self.to(Open, change="", driver="")

    def duplicate(self) -> Defect:
        return self.to(Duplicate)

    def wont(self) -> Defect:
        return self.to(WontDo)

Each system plays as its application, from a recording of a real passing run:

Watch it

The deck is the fast version of this page: two scenes define the words on the coffee order, then each system shows a few lines of its model and plays its journey in place, from its recording. Each scene links to the section it tells. The last scene opens the Kitlark playground, which checks, runs and plays back a story:

← All of Learn