Skip to content

Two tenants: whose state is this?

In examples/keyed-retry.rkg, Bob's request completes while Alice's waits for a retry. The reference traces for this example are filed under two-tenants.

A file pipeline is easy to follow one request at a time. What happens when Alice's partner stalls while Bob's request is ready? This chapter adds instance ownership and finite retry to the same graph-and-Lab model.

The versioned goldens cover native Retry: the failed attempt remains visible in the journal, while only the final outcome reaches the graph.

The file formats are .rkg for the program and Lab, .rkc for a reviewed scenario reference and .rkr for a real execution recording.

From the repository root, with rastok installed:

Terminal window
rastok simulate examples/keyed-retry.rkg --compare examples/traces/two-tenants
PASS ui_tour.two_tenants
PASS ui_tour.lifetime
2 scenario(s), 0 failed

Add --ui to follow the same checked scenario in the debugger.

Terminal window
rastok simulate examples/keyed-retry.rkg --compare examples/traces/two-tenants --ui

This requires hosted-ui. Open the private loopback URL printed by the CLI.

For the controls and source view, see the debugger tour.

The file starts with type Request = struct { ... }. This names a payload shape with two string fields, tenant and prompt; it does not create a request. input in: Request then uses that type on a port. The type reference explains structs and other type forms.

Start with instance = { key = data.tenant }: it selects independent state for Alice and Bob. Retry.attempts = 2 bounds Calls for one retained request, including the first; lifetime bounds how long an idle instance lives. They solve different problems.

An instance is not a thread or a process. The current runtime executes Operator transactions one at a time, but an outstanding external Call does not prevent another instance from progressing. Bob can finish while Alice waits; this does not demonstrate parallel Operator execution. See Instances, state and time for ownership and lifetime, and Execution and concurrency for the current runtime and the planned worker pool.

Then read model : Process + Retry, using the Process contract: Retry.on = [TimeoutExpired] selects the terminal that permits another attempt. Retry retains the original request across attempts. answer decodes the final Process frame after the Retry manager has selected it. The nested audit Graph composes another branch.

The Lab reference explains mock responses, expectations and observation windows. advance supplies virtual time; a mock's terminal response ends its simulated exchange, not the whole scenario.

Source: examples/keyed-retry.rkg

// Two tenants send requests: Bob finishes while Alice waits to retry.
// The Lab checks that Alice's retry never starts a second request for Bob.
type Request = struct {
tenant: string
prompt: string
}
type Answer = struct {
tenant: string
prompt: string
}
graph agent_retry : App {
input in: Request
output done: Answer
output failed: ProcessExit
instance = { key = data.tenant }
lifetime = { kind = idle; max = duration("10s") }
closing = { drain_for = duration("1s"); settle_for = duration("1s") }
graph audit {
input in: Request
operator observe {
input in: Request
output out: string
on in { out = data.tenant }
}
in -> observe
observe.out -> nothing
}
connector model : Process + Retry {
mode = job
command = "cat"
codec = jsonl
idempotent = true
behavior = probabilistic
timeout = duration("1s")
max_output = "1 MiB"
Retry.attempts = 2
Retry.on = [TimeoutExpired]
Retry.delay = { kind = fixed; after = duration("1s") }
}
operator answer {
input in: variant
output out: Answer
on in {
val decoded = data as? Answer
match {
decoded is Answer => out = decoded
_ => nothing
}
}
}
in -> { audit; model }
model.out -> answer -> done
model.err -> nothing
model.exit -> match {
Exited -> nothing
CaptureFailed | DecodeFailed | OutcomeUnknown | OutputOverflow
| SpawnFailed | TimeoutExpired -> failed
}
}
lab ui_tour {
mock model {
on in where data is Request && data.tenant == "alice" && now() == timestamp("1970-01-01T00:00:00Z") {
out = nothing
}
on in {
out = data
exit = Exited { exit_code = 0; signal = null }
}
}
scenario two_tenants {
app.in = { tenant = "alice"; prompt = "Explain causal replay" }
expect app.done == nothing
app.in = { tenant = "bob"; prompt = "Explain typed envelopes" }
expect app.done where data.tenant == "bob"
advance duration("1s")
expect app.done == nothing
expect model.in == nothing
advance duration("1s")
expect app.done where data.tenant == "alice"
expect answer.in == { "tenant": "alice", "prompt": "Explain causal replay" }
expect 0 model.in where data is Request && data.tenant == "bob"
expect audit.observe.in == nothing
expect 0 app.done where data.tenant == "bob"
expect app.failed == nothing
}
scenario lifetime {
app.in = { tenant = "bob"; prompt = "First generation" }
expect app.done where data.prompt == "First generation"
advance duration("10s")
expect model.in == nothing
app.in = { tenant = "bob"; prompt = "Next generation" }
expect app.done where data.prompt == "Next generation"
expect app.failed == nothing
}
}

This cut of ui_tour.two_tenants follows model and answer; the separate audit.observe branch runs once per input and is omitted here.

This cut of ui_tour.two_tenants follows model and answer; the separate audit.observe branch runs once per input and is omitted here.answermodel: Process + Retryagent_retryLab: ui_tour.two_tenantsanswermodel: Process + Retryagent_retryLab: ui_tour.two_tenantsAlice: TimeoutExpired is Held, not deliveredNo second Call for Bobin = Alice's requestAlice, attempt 1Scripted Call: no responsein = Bob's requestBob, attempt 1Scripted CallBob's data, then Exited(0)Bob's datadone = Bob's answeradvance 1sadvance 1sAlice, attempt 2: the same requestAlice's data, then Exited(0)Alice's datadone = Alice's answer
This cut of ui_tour.two_tenants follows model and answer; the separate audit.observe branch runs once per input and is omitted here.answermodel: Process + Retryagent_retryLab: ui_tour.two_tenantsanswermodel: Process + Retryagent_retryLab: ui_tour.two_tenantsAlice: TimeoutExpired is Held, not deliveredNo second Call for Bobin = Alice's requestAlice, attempt 1Scripted Call: no responsein = Bob's requestBob, attempt 1Scripted CallBob's data, then Exited(0)Bob's datadone = Bob's answeradvance 1sadvance 1sAlice, attempt 2: the same requestAlice's data, then Exited(0)Alice's datadone = Alice's answer

Alice enters first and waits. Bob enters a separate instance and finishes immediately. Advancing one second expires Alice's Call; the next second admits her retry, which finishes. Bob receives no second Call. The lifetime scenario advances through ten seconds of silence, then proves that a new admission uses a fresh instance generation.

⧉ counts instances. The Instances tab's Retry table shows Bob's one attempt and Alice's two distinct Calls. Alice's timeout is recorded as Held, not delivered to the failed output; the final attempt is Published. The audit runs once per incoming request, not again for an internal retry.

Change Retry.attempts = 2 to Retry.attempts = 1. Alice can no longer be readmitted: her original TimeoutExpired reaches failed, and the assertion expecting Alice's answer fails. Bob still finishes once. There is no new RetryExpired error type.

Terminal window
rastok simulate examples/keyed-retry.rkg --compare examples/traces/two-tenants
FAIL ui_tour.two_tenants
expect app.done where data.tenant == "alice" (keyed-retry.rkg:95)
nothing observed
where data.tenant == "alice" is false for 0 of 0 deliveries; count 0, expected 1
expect answer.in == { "tenant": "alice", "prompt": "Explain causal replay" } (keyed-retry.rkg:96)
nothing observed
expected 1 matching delivery on answer.in, but observed 0
golden divergence: record 6: retry_protocol(43) . parent_id(2)[0][4]; expected 98f50438c7e3285a…(32 bytes); got 639236b28503f36b…(32 bytes); cause: TRANSACTION "model" · trigger "l:retry" · cause_id f3df1105112054d3…(16 bytes)
FAIL ui_tour.lifetime
golden divergence: record 6: retry_protocol(43) . parent_id(2)[0][4]; expected 98f50438c7e3285a…(32 bytes); got 639236b28503f36b…(32 bytes); cause: TRANSACTION "model" · trigger "l:retry" · cause_id f3df1105112054d3…(16 bytes)
2 scenario(s), 2 failed

Restore the checked-in source (this discards your edits to this example):

Terminal window
git checkout -- examples/keyed-retry.rkg

Add --compare examples/traces/two-tenants to compare every record. The unedited example matches. After your edit, the CLI names the first difference and recorded context, which can precede the final missing answer. Restore the attempt bound afterward.

Inspect the raw sealed trace (.rkc)

The versioned reference stores one hex-encoded causal record per line, with an optional final seal line. Use the chapter's comparison commands to explain a divergence.

Source: examples/traces/two-tenants/ui_tour.two_tenants.rkc

a6070108a40150f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c804010582025833820f82826670726f6d70748206754578706c61696e2063617573616c207265706c6179826674656e616e74820665616c69636509636c61620a000b000d8282742e696e3e61756469742e6f6273657276652e696e82581e8282656175646974676f627365727665818380018207820665616c69636500826c2e696e3e6d6f64656c2e696e82568281656d6f64656c818380018207820665616c69636500
a7070211601282508280818380018207820665616c6963650013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c8
a80702116561756469741282568281656175646974818380018207820665616c6963650013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c8182282508280818380018207820665616c69636500
a80702116d61756469742e6f6273657276651282581e8282656175646974676f627365727665818380018207820665616c6963650013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c8182282568281656175646974818380018207820665616c69636500
a8070211656d6f64656c1282568281656d6f64656c818380018207820665616c6963650013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c8182282508280818380018207820665616c69636500
a60702116d61756469742e6f6273657276651282581e8282656175646974676f627365727665818380018207820665616c696365001364703a696e148183742e696e3e61756469742e6f6273657276652e696e82508280818380018207820665616c6963650001178183636f757400a50150242f04840e2b7276f46b18be463a760e0250f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c8040105820248820665616c696365
a8070211656d6f64656c1282568281656d6f64656c818380018207820665616c6963650013676c3a726574727914801780181a50f3df1105112054d397a04ed0666ca4c8182ba10282870150961dad6a19ad6dc36464a3b337506d5c509046c8137b2b3d115dfb05d17181057962696e582098f50438c7e3285ad952d34f94123cf760a8e4f60f14f6ef9446ad9ccc373b1d6d7374642f70726f6365737340318450f3df1105112054d397a04ed0666ca4c8582001f21d03f134e24c78d89a02c40b1518042219fa5d32b5295521810e04bfc4d780f6850250961dad6a19ad6dc36464a3b337506d5c01a40150f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c804010582025833820f82826670726f6d70748206754578706c61696e2063617573616c207265706c6179826674656e616e74820665616c696365509046c8137b2b3d115dfb05d171810579
a70703181b50f3df1105112054d397a04ed0666ca4c8181c656d6f64656c181e582001f21d03f134e24c78d89a02c40b1518042219fa5d32b5295521810e04bfc4d7181f0118216c2e696e3e6d6f64656c2e696e182ba1018250961dad6a19ad6dc36464a3b337506d5c01
a6070108a40150e6c20d6146234b74a33da1c709e86efd0350e6c20d6146234b74a33da1c709e86efd04010582025833820f82826670726f6d70748206774578706c61696e20747970656420656e76656c6f706573826674656e616e74820663626f6209636c61620a010b000d8282742e696e3e61756469742e6f6273657276652e696e82581c8282656175646974676f627365727665818380018207820663626f6200826c2e696e3e6d6f64656c2e696e82548281656d6f64656c818380018207820663626f6200
a70702116012824e8280818380018207820663626f620013676c3a737461727414801780181a50e6c20d6146234b74a33da1c709e86efd
a80702116561756469741282548281656175646974818380018207820663626f620013676c3a737461727414801780181a50e6c20d6146234b74a33da1c709e86efd1822824e8280818380018207820663626f6200
a80702116d61756469742e6f6273657276651282581c8282656175646974676f627365727665818380018207820663626f620013676c3a737461727414801780181a50e6c20d6146234b74a33da1c709e86efd182282548281656175646974818380018207820663626f6200
a8070211656d6f64656c1282548281656d6f64656c818380018207820663626f620013676c3a737461727414801780181a50e6c20d6146234b74a33da1c709e86efd1822824e8280818380018207820663626f6200
a60702116d61756469742e6f6273657276651282581c8282656175646974676f627365727665818380018207820663626f62001364703a696e148183742e696e3e61756469742e6f6273657276652e696e824e8280818380018207820663626f620001178183636f757400a50150f98c7c092a53d6d1a7989ec1f6b52b630250e6c20d6146234b74a33da1c709e86efd0350e6c20d6146234b74a33da1c709e86efd040105820246820663626f62
a8070211656d6f64656c1282548281656d6f64656c818380018207820663626f620013676c3a726574727914801780181a50e6c20d6146234b74a33da1c709e86efd182ba10282870150b1c68415b421d87951425a4853e66f5650a08358e94753b1beccc9a14b211f98ca62696e582098f50438c7e3285ad952d34f94123cf760a8e4f60f14f6ef9446ad9ccc373b1d6d7374642f70726f6365737340318450e6c20d6146234b74a33da1c709e86efd5820e2c35b0b29e096536129210ebbf03f76743c3c01adec1d83f308c8d62115d3ef80f6850250b1c68415b421d87951425a4853e66f5601a40150e6c20d6146234b74a33da1c709e86efd0350e6c20d6146234b74a33da1c709e86efd04010582025833820f82826670726f6d70748206774578706c61696e20747970656420656e76656c6f706573826674656e616e74820663626f6250a08358e94753b1beccc9a14b211f98ca
a70703181b50e6c20d6146234b74a33da1c709e86efd181c656d6f64656c181e5820e2c35b0b29e096536129210ebbf03f76743c3c01adec1d83f308c8d62115d3ef181f0118216c2e696e3e6d6f64656c2e696e182ba1018250b1c68415b421d87951425a4853e66f5601
a80703181b50e6c20d6146234b74a33da1c709e86efd181c656d6f64656c181e5820e2c35b0b29e096536129210ebbf03f76743c3c01adec1d83f308c8d62115d3ef181f02182082018282636f7574820258378210828282066670726f6d70748206774578706c61696e20747970656420656e76656c6f7065738282066674656e616e74820663626f628264657869748202582a82138266457869746564820f828269657869745f636f646582020082667369676e616c821382008207f618216c2e696e3e6d6f64656c2e696e182ba1018250b1c68415b421d87951425a4853e66f5601
a8070211656d6f64656c1282548281656d6f64656c818380018207820663626f62001364703a696e1481836c2e696e3e6d6f64656c2e696e824e8280818380018207820663626f620001178283636f757400a501502f3afb52c023c8407761929204189d5b0250e6c20d6146234b74a33da1c709e86efd0350e6c20d6146234b74a33da1c709e86efd040105820258378210828282066670726f6d70748206774578706c61696e20747970656420656e76656c6f7065738282066674656e616e74820663626f6283646578697400a5015021e0d24451d2373d0490524464f3fd210250e6c20d6146234b74a33da1c709e86efd0350e6c20d6146234b74a33da1c709e86efd0401058202582a82138266457869746564820f828269657869745f636f646582020082667369676e616c821382008207f618198183502f3afb52c023c8407761929204189d5b736d6f64656c2e6f75743e616e737765722e696e8255828166616e73776572818380018207820663626f6200182ba2018250b1c68415b421d87951425a4853e66f56010282850650b1c68415b421d87951425a4853e66f56015061c7816a4b547232f22e62ea101f1c6b82502f3afb52c023c8407761929204189d5b5021e0d24451d2373d0490524464f3fd21840750b1c68415b421d87951425a4853e66f560101
a807021166616e73776572128255828166616e73776572818380018207820663626f620013676c3a737461727414801780181a502f3afb52c023c8407761929204189d5b1822824e8280818380018207820663626f6200
a607021166616e73776572128255828166616e73776572818380018207820663626f62001364703a696e148183736d6f64656c2e6f75743e616e737765722e696e82548281656d6f64656c818380018207820663626f620001178183636f757400a501506057c95cde89e87685a95298350240f702502f3afb52c023c8407761929204189d5b0350e6c20d6146234b74a33da1c709e86efd04010582025833820f82826670726f6d70748206774578706c61696e20747970656420656e76656c6f706573826674656e616e74820663626f62
a9070318188182045048a92fbc727f4d7ed69005b9f745d4ce181b50f3df1105112054d397a04ed0666ca4c8181c656d6f64656c181e582001f21d03f134e24c78d89a02c40b1518042219fa5d32b5295521810e04bfc4d7181f021820810218216c2e696e3e6d6f64656c2e696e182ba1018250961dad6a19ad6dc36464a3b337506d5c01
a7070211656d6f64656c1282568281656d6f64656c818380018207820665616c6963650013696c3a74696d656f75741481836c2e696e3e6d6f64656c2e696e82508280818380018207820665616c6963650001178183646578697400a50150d5b3406cdffd7e099c4c3694fd0293b90250f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c80401058202558213826e54696d656f757445787069726564820f80182ba2018250961dad6a19ad6dc36464a3b337506d5c010281850350961dad6a19ad6dc36464a3b337506d5c015001c6fbc51564f0a3ccf04ad9bc0e8bb08150d5b3406cdffd7e099c4c3694fd0293b9
a8070211656d6f64656c1282568281656d6f64656c818380018207820665616c6963650013676c3a726574727914801780181a50d5b3406cdffd7e099c4c3694fd0293b9182ba10282850450961dad6a19ad6dc36464a3b337506d5c015001c6fbc51564f0a3ccf04ad9bc0e8bb08150d5b3406cdffd7e099c4c3694fd0293b9850550961dad6a19ad6dc36464a3b337506d5c01025001c6fbc51564f0a3ccf04ad9bc0e8bb0
aa070211656d6f64656c1282568281656d6f64656c818380018207820665616c6963650013676c3a72657472791480178018188182015003260119a3813e2ae8426b064bdc8699181981835048a92fbc727f4d7ed69005b9f745d4ce782a4072657472793a39363164616436613139616436646333363436346133623333373530366435633a696e82568281656d6f64656c818380018207820665616c69636500181a50d5b3406cdffd7e099c4c3694fd0293b9182ba10281850250961dad6a19ad6dc36464a3b337506d5c02a5015048a92fbc727f4d7ed69005b9f745d4ce0250d5b3406cdffd7e099c4c3694fd0293b90350f3df1105112054d397a04ed0666ca4c804010582025833820f82826670726f6d70748206754578706c61696e2063617573616c207265706c6179826674656e616e74820665616c6963655003260119a3813e2ae8426b064bdc8699
a70703181b5048a92fbc727f4d7ed69005b9f745d4ce181c656d6f64656c181e582001f21d03f134e24c78d89a02c40b1518042219fa5d32b5295521810e04bfc4d7181f011821782a4072657472793a39363164616436613139616436646333363436346133623333373530366435633a696e182ba1018250961dad6a19ad6dc36464a3b337506d5c02
a80703181b5048a92fbc727f4d7ed69005b9f745d4ce181c656d6f64656c181e582001f21d03f134e24c78d89a02c40b1518042219fa5d32b5295521810e04bfc4d7181f02182082018282636f7574820258378210828282066670726f6d70748206754578706c61696e2063617573616c207265706c61798282066674656e616e74820665616c6963658264657869748202582a82138266457869746564820f828269657869745f636f646582020082667369676e616c821382008207f61821782a4072657472793a39363164616436613139616436646333363436346133623333373530366435633a696e182ba1018250961dad6a19ad6dc36464a3b337506d5c02
a8070211656d6f64656c1282568281656d6f64656c818380018207820665616c696365001364703a696e148183782a4072657472793a39363164616436613139616436646333363436346133623333373530366435633a696e82568281656d6f64656c818380018207820665616c6963650001178283636f757400a501505c3c5de80a70aba7a6912e427b7718b0025048a92fbc727f4d7ed69005b9f745d4ce0350f3df1105112054d397a04ed0666ca4c8040105820258378210828282066670726f6d70748206754578706c61696e2063617573616c207265706c61798282066674656e616e74820665616c69636583646578697400a5015031ee0198f4f892c3e741eed100a13b98025048a92fbc727f4d7ed69005b9f745d4ce0350f3df1105112054d397a04ed0666ca4c80401058202582a82138266457869746564820f828269657869745f636f646582020082667369676e616c821382008207f618198183505c3c5de80a70aba7a6912e427b7718b0736d6f64656c2e6f75743e616e737765722e696e8257828166616e73776572818380018207820665616c69636500182ba2018250961dad6a19ad6dc36464a3b337506d5c020282850650961dad6a19ad6dc36464a3b337506d5c025090dcc4a4031912c5809a3209963216e082505c3c5de80a70aba7a6912e427b7718b05031ee0198f4f892c3e741eed100a13b98840750961dad6a19ad6dc36464a3b337506d5c0201
a807021166616e73776572128257828166616e73776572818380018207820665616c6963650013676c3a737461727414801780181a505c3c5de80a70aba7a6912e427b7718b0182282508280818380018207820665616c69636500
a607021166616e73776572128257828166616e73776572818380018207820665616c696365001364703a696e148183736d6f64656c2e6f75743e616e737765722e696e82568281656d6f64656c818380018207820665616c6963650001178183636f757400a501507e8bba5f41e34f6dd8c6326184317c3f02505c3c5de80a70aba7a6912e427b7718b00350f3df1105112054d397a04ed0666ca4c804010582025833820f82826670726f6d70748206754578706c61696e2063617573616c207265706c6179826674656e616e74820665616c696365
seal 1edd9d70d157cdac9a426e4b84934e0cd1b77792f9e23eba7cad434db11d358f

Ask two separate questions in every stateful flow: "whose state?" and "for how long?" A key answers the first, a lifetime the second. Neither alone means that a duplicate request will be ignored. The next chapter will make that policy an explicit piece of state.

Previous: File pipeline. Next: Webhook to partner, combining these mechanisms into an integration you can qualify before its partner is ready.

The Process mock is stateless: the admitted request and explicit logical time select its reply. Only Alice's request at time zero is silent. No real language model or paid API is used. The instance key isolates state but does not guarantee deduplication or persistence across a deployment restart. The fixed backoff is one second.

Diagram