---
title: Instances, state and time
description: Choose the owner of state, separate instance keys from lifetimes,
  and make closure and bounded waiting explicit.
url: https://docs.rastok.com/guide/06-instances-state-and-time
source_revision: v0.2.0-alpha
---

# Instances, state and time

After this chapter you can choose an instance key, distinguish immutable
values from private mutable state, and explain when a lifetime or a bounded
join produces work. The central question is ownership: which tenant or sensor
does this arrival belong to, and which live instance may consume it?

## Select an instance

In `examples/keyed-retry.rkg`, lines 19–21, the App declares three independent
policies:

```rkg
instance = { key = data.tenant }
lifetime = { kind = idle; max = duration("10s") }
closing = { drain_for = duration("1s"); settle_for = duration("1s") }
```

The key selects Alice's or Bob's instance before delivery. An unkeyed child
is a singleton within its parent, not across tenants. An Operator owns state
and mailboxes; a Connector owns exchanges. A nested Graph types its key from
its declared input, even before a Connector that accepts `variant`.
Keys do not create threads: the [Worker](/reference/execution.md) runs
transactions sequentially while external Calls can remain open.

Try a resident Process with `rastok run examples/agent-dialogue.rkg`
and `cat` on PATH. Type a line and read its echo on `out`. The next line
uses the same local session; Ctrl-C stops it.

The key reads the entering envelope and permitted enclosing immutable
values. Its type must be valid on every admitting input. Changing the payload (DATA) later
does not move the current instance. When an address closes and later opens
again, a new generation separates its clean state from old deliveries and
deadlines. The journal records that exact destination generation.

## Values and mutable state

A handler-local `val` lasts one transaction. A Node-level `val` can read
that owner's `instance.key`, as can its handlers (including `on stop`) and
instance-bound settings. A Graph can pass its key to children through a
named `val`; the children never inherit `instance.key`.

A `var` belongs to one Operator instance. In `examples/sensor-alarm.rkg`,
lines 19–21, each sensor keeps its own alarm state, even across a silent period:

```rkg
instance = { key = data.sensor }
lifetime = { kind = persistent }
var tripped: bool = false
```

Pump-a can be tripped while pump-b is normal. The state change and emission
commit together in the owning Operator transaction. Another Operator cannot
read that cell by name; shared mutable behavior needs an explicit Operator
with typed inputs and outputs. See [`docs/spec/instances.md`](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/instances.md) §§3–5.

## Choose a lifetime

If you declare a key, you must also declare a lifetime. The key says which
instance receives a delivery; the lifetime says when that instance closes.
Without a key, omitting the lifetime still gives a persistent singleton.
The attribute has the shape
`lifetime = { kind = ... }`, with a positive `max` duration where required.

| Kind | Meaning and additional field |
|---|---|
| `persistent` | No lifetime deadline |
| `idle` | Close after admission silence, with `max = duration("10s")` |
| `age` | Close at a fixed age from the creating admission, using `max` |
| `once` | Admit one entering envelope into a generation, then request Closing |
| `slot` | Select the epoch-aligned processing-time box at admission, with `max = duration("5m")` |

Traffic refreshes an idle deadline, not an age deadline. An open Call alone is
not admission activity. Slot boundaries are independent of boot time;
the run's recorded start timestamp maps them onto the logical timeline.

## Close with explicit budgets

Closing drains admitted work, then settles canceled Calls. Both durations
are positive. Confirmed outcomes keep their typed exits; unconfirmed ones
become `OutcomeUnknown` at the settlement deadline.

A finite owner needs this policy when it is an outbound Connector or owns
one in its subtree. The rule includes the root App. Children close
before their parent, and later arrivals wait for a successor generation.
Closure is neither a rollback nor permission to drop the old generation's
outcomes. The full sequence is in [`docs/spec/instances.md`](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/instances.md) §7.1.

## Bound a wait, then advance time

`examples/sensor-alarm.rkg`, lines 34–44, correlates within the sensor instance:

```rkg
operator watchdog {
  input probe: Sensor
  input heartbeat: Sensor
  output out: string
  output expired: MissedHeartbeat
  instance = { key = data.sensor }
  lifetime = { kind = persistent }
  on (probe, heartbeat) within duration("5m") {
    out = probe.data.sensor
  }
}
```

`within` bounds how long each candidate waits; `last N` instead bounds the
number retained per input. A bounded join must declare and wire `JoinExpired`
on its `expired` output, even if the intended scenario always supplies both
inputs. Deliberate `-> nothing` is a destination; silence is not.

After a pair fires, the watchdog has no candidate or state to keep, but its
sensor instance stays open for the next pair. Check reports this as
[`W-EMPTY-INSTANCE`](/reference/diagnostics.md#instance-warnings).
If the instance is finished instead, write `stop` on that branch.

The Lab's `advance` visits due deadlines without sleeping. A partial join
can be waiting while no work is runnable. `now()` observes the transaction's
civil timestamp, derived from origin plus logical time; it does not trigger
a handler. See [`docs/spec/execution-model.md`](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/execution-model.md) §§5, 7 and 8.

## Summary

Keys select owners, `var` stores private state, and lifetimes determine when
instances close. Joins bound retained candidates; the Lab advances their
deadlines explicitly. Sliding/event-time windows and durable recovery are
outside the [current alpha scope](/index.md#what-is-available-today).

## Next

Continue with [Loops and retries](/guide/07-loops-and-retries.md), then compare [Two tenants](/examples/03-two-tenants.md)
and [Sensor alarm](/examples/05-sensor-alarm.md). Explain which instance owns
each pending Call or unmatched probe before running the scenario.
