---
title: "Echo: one graph, two worlds"
description: Run one graph with a real cat process or scripted Lab responses,
  then inspect a recording without starting the process again.
url: https://docs.rastok.com/examples/01-echo
source_revision: v0.2.0-alpha
---

# Echo: one graph, two worlds

A line of text enters the program, and a real `cat` program, which repeats
what it reads, sends it back. The line leaves the App through its output.

There are two ways to run this program: the real world (App) starts `cat`;
the virtual world (Lab) supplies its responses from a mock. The graph stays
the same. You can test its success, filtering and timeout before starting
an external process.

The three [file formats](/guide/09-command-line.md#file-formats) have different
jobs: `.rkg` is the program and its Lab, `.rkc` is a scenario's reference
trace, and `.rkr` is a recording of a real execution.

## Run

From the repository root, with `rastok` installed:

```sh
rastok simulate examples/echo.rkg --compare examples/traces/echo
```

```text
PASS smoke.roundtrip
PASS smoke.filtered
PASS smoke.silence_expires
3 scenario(s), 0 failed
```

This reads `examples/echo.rkg` and compares each scenario with its reviewed
file in `examples/traces/echo/`. The command starts no external process and
writes no reference trace.

To inspect the same scenario, use a binary built with `hosted-ui`:

```sh
rastok simulate examples/echo.rkg --compare examples/traces/echo --ui
```

Open the complete URL printed by the CLI and keep it private. In Terminal
on macOS, hold Command and double-click the URL to open it. If your terminal
does not open links, copy the complete URL into your browser instead.
The [debugger tour](/debugger.md) explains its controls and source view.

## Read the program

Open `examples/echo.rkg` and read from top to bottom.

`graph echo : App` declares the application. It contains a Connector and
an Operator, with wiring between their ports.

The Connector `shell : Process` talks to an external program under the
[Process contract](/reference/connectors/process.md).
Here `mode = job` starts a child for each Call, `command = "cat"` selects the
program, and `codec = lines` exchanges lines of text. `timeout = duration("5s")`
bounds the wait. The declarations of idempotence and deterministic behavior
describe the repetition guarantee and the expected behavior of this command.

The Operator `relay` does not start a process. Its `on in where data != ""`
handler accepts a nonempty input and emits that data on `out`. An empty line
produces no output.

The wiring `in -> relay -> shell -> out` sends the App input to `relay`,
then to `shell`, then to the App output. The two remaining outputs,
`shell.err` and `shell.exit`, go to `nothing`: the program explicitly
discards them. They still exist in the journal and can be checked by the Lab.

Below the App, `lab smoke` describes the virtual world. It contains the
shared mock and scenarios directly; no intermediate simulation block is needed.

`mock shell` supplies the process response. In `on in { ... }`, `out = .`
means “emit the data this mock received”: the dot is the received data, so the
mock echoes it. `exit = Exited { exit_code = 0, signal = null }` then ends
the simulated process successfully. See [mock responses](/reference/lab.md#mock-responses)
for the other forms.

The scenarios are independent:

- `roundtrip` sends `"hello"`; `expect app.out == "hello"`
  requires exactly one matching App output, then `expect shell.err == nothing`
  requires no error output.
- `filtered` sends `""`; `expect shell.in == nothing` and `expect app.out == nothing`
  check that the empty line reaches neither the process nor the App output.
- `silence_expires` replaces the shared mock with `out = nothing`, providing no
  terminal response; `advance duration("5s")` moves virtual time and the final
  expectation checks `TimeoutExpired` on `shell.exit`.

An [expectation](/reference/lab.md#expectations-and-windows) examines the
observations since the preceding input or time advance. It does not consume
them, so successive expectations can inspect the same window.

<a id="graph"></a>

Source: `examples/echo.rkg`

```rkg
// A nonempty line goes to cat and comes back on the App output.
// The Lab tests the echo, an empty line and a child that never answers.


graph echo : App {
  connector shell : Process {
    mode = job ; command = "cat"
    codec = lines
    idempotent = true; behavior = deterministic
    timeout = duration("5s")
  }

  operator relay {
    on in where data != "" {
      out = data
    }
  }

  in -> relay -> shell -> out
  shell.err -> nothing
  shell.exit -> nothing
}

lab smoke {
  mock shell {
    on in {
      out = .
      exit = Exited { exit_code = 0, signal = null }
    }
  }

  scenario roundtrip {
    app.in = "hello"
    expect app.out == "hello"
    expect shell.err == nothing
  }

  scenario filtered {
    app.in = ""
    expect shell.in == nothing
    expect app.out == nothing
  }

  scenario silence_expires {
    mock shell {
      on in { out = nothing }
    }
    app.in = "hello"
    advance duration("5s")
    expect shell.exit == TimeoutExpired {}
  }
}
```

## Expected result

The roundtrip scenario follows this sequence, with the Lab supplying the
response instead of starting `cat`.

```mermaid
sequenceDiagram
    participant lab as Lab: smoke.roundtrip
    participant app as echo
    participant relay as relay
    participant shell as shell: Process
    lab->>app: in = hello
    app->>relay: in = hello
    relay->>shell: out to in = hello
    shell->>lab: Scripted Call request
    lab-->>shell: out = hello, exit = Exited(0)
    shell-->>app: out = hello
    Note over shell: Exited is disposed to nothing
```

To run in the real world, use a macOS or Linux shell with `cat` on PATH.
The following command starts `cat` and writes `echo.rkr` in your current
directory. A recording captures the real execution so it can be inspected
without starting that process again. Use an unused filename: existing
recordings are never overwritten.

```sh
printf 'hello\n' | rastok run examples/echo.rkg --record echo.rkr
```

```text
hello
recorded: 8 causal records · seal 80f1263274f3b1b7480fdb13c007459e18e1e4cf10cc9bf08b5d12b63a4b7e0e
```

Echo has one App output, so stdout contains the value alone. The `recorded:`
summary goes to stderr. Now inspect the recording:

```sh
rastok replay echo.rkr
```

```text
replay: 8 causal records · seal 80f1263274f3b1b7480fdb13c007459e18e1e4cf10cc9bf08b5d12b63a4b7e0e · completed recording · no Driver I/O
hello
```

Replay needs neither the original source nor `cat`. Its summary goes to
stderr; the output value goes to stdout. The matching seals describe the
same recorded execution, not a new call to the outside world. The seal above
is from one run; compare your replay with the seal printed by your own run.

A Lab execution and a real run do not have equal raw seals by default:
their input provenance differs, and real response timing can differ from
virtual time. Here the proof is equality between the real run and its
recorded replay.

## Break it

In `mock shell`, change exactly `out = .` to `out = "changed"`.
Leave the expected value `"hello"` unchanged. Run:

```sh
rastok simulate examples/echo.rkg --compare examples/traces/echo
```

```text
FAIL smoke.roundtrip
     expect app.out == "hello"  (echo.rkg:34)
     observed app.out: "changed"
     expected 1 matching delivery on app.out, but observed 0
     golden divergence: record 6: closure(32)[1][0][1][1]; expected 82066568656c6c6f; got 8206676368616e67…(10 bytes); cause: EGRESS "shell" · emitting_id 801cfc175b8f2151…(16 bytes) · call_key absent
PASS smoke.filtered
PASS smoke.silence_expires
3 scenario(s), 1 failed
```

Restore the checked-in source:

```sh
git checkout -- examples/echo.rkg
```

That command discards your edits to this example. Then run the complete
comparison again:

```sh
rastok simulate examples/echo.rkg --compare examples/traces/echo
```

It prints the same three PASS lines and zero-failed total shown under Run.

## Read the golden difference

The expectation names the source line, the observed `"changed"` value and
the failed count. The golden difference names the first differing record,
its field, the expected and actual encoded values, and the Call that produced
it. Those are two checks: the assertion checks the authored expectation;
the trace comparison checks the reviewed causal history. Do not save a new
reference merely to make the comparison pass.

<details id="golden" class="raw-trace">
<summary>Inspect the raw sealed trace (.rkc)</summary>

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/echo/smoke.roundtrip.rkc`

```rastok-trace
a6070108a40150f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c804010582024882066568656c6c6f09636c61620a000b000d81826c2e696e3e72656c61792e696e824982816572656c61798000
a7070211601282438280800013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c8
a80702116572656c617912824982816572656c6179800013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c81822824382808000
a80702116572656c617912824982816572656c617980001364703a696e1481836c2e696e3e72656c61792e696e82438280800001178183636f757400a50150801cfc175b8f2151fe076d50a05c2b580250f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c804010582024882066568656c6c6f1818818201502d4efb509bf4e072a22ea0314c1c7ee11819818350801cfc175b8f2151fe076d50a05c2b587272656c61792e6f75743e7368656c6c2e696e82498281657368656c6c8000
a8070211657368656c6c1282498281657368656c6c800013676c3a737461727414801780181a50801cfc175b8f2151fe076d50a05c2b581822824382808000
a60703181b50801cfc175b8f2151fe076d50a05c2b58181c657368656c6c181e58208e4c7c1b99dbfd50e7a95185fead5ee1448fa904a2fdd778eaf5f2dbfd629a99181f0118217272656c61792e6f75743e7368656c6c2e696e
a70703181b50801cfc175b8f2151fe076d50a05c2b58181c657368656c6c181e58208e4c7c1b99dbfd50e7a95185fead5ee1448fa904a2fdd778eaf5f2dbfd629a99181f02182082018282636f757482024882066568656c6c6f8264657869748202582a82138266457869746564820f828269657869745f636f646582020082667369676e616c821382008207f618217272656c61792e6f75743e7368656c6c2e696e
a6070211657368656c6c1282498281657368656c6c80001364703a696e1481837272656c61792e6f75743e7368656c6c2e696e824982816572656c6179800001178283636f757400a50150233ee7a209d98e59c5f7b3aa16a9345a0250801cfc175b8f2151fe076d50a05c2b580350f3df1105112054d397a04ed0666ca4c804010582024882066568656c6c6f83646578697400a501509403fe6e10f58d02bd0e61df7a9d3e760250801cfc175b8f2151fe076d50a05c2b580350f3df1105112054d397a04ed0666ca4c80401058202582a82138266457869746564820f828269657869745f636f646582020082667369676e616c821382008207f6
seal 7e20cb99d172b4d2816136dfab6e60fadc99e592aa754a520059e2d4bfea5ea9
```

</details>

## Carry forward

For the concepts behind this example, read [A program](/guide/01-a-program.md)
and [The Lab](/guide/04-the-lab.md).

An injected fault belongs to the virtual world (Lab); the application stays
the same as in the real world (App). Keep that distinction when a partner
environment is not available yet.

Next: [File pipeline](/examples/02-file-pipeline.md). Replace the external program with
two file operations and ask what each effect is allowed to do.

## Limits

Reference traces contain causal evidence. Recordings are bounded local
diagnostics, not durable deployment journals or share-safe exports. The
recorder currently refuses personal-data plans and boot-dependent deployment
profiles. Diagnostic replay reconstructs recorded facts; it does not execute
a Lab scenario.
