Skip to content

File pipeline: effects with a contract

This program reads a file, counts its bytes and copies nonempty content to another file.

Echo separated computation from the world. Now the world contains files you could overwrite. How do you make the permitted operation, its limits and its refusals visible before the flow runs?

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/file-pipeline.rkg --compare examples/traces/file-pipeline
PASS files.copy
PASS files.empty
PASS files.utf8
PASS files.directory
PASS files.single_byte
5 scenario(s), 0 failed

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

Find the two File Connectors: one reads, one replaces. Their modes describe different operations under the same Contract. Between them, the Operator decides whether the bytes should continue. Follow the exit routes too: a refused read has a destination, rather than disappearing into a log. The Lab replaces both file operations, using the same resolved port types.

In a port declaration, the name after : is its payload's type: the values it may accept or emit. output failed: FileExit declares an output port named failed, not a new type. See the type reference for the notation. FileExit comes from the standard library, without an import or copied declaration; its cases are the ones on File's exit port.

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/file-pipeline.rkg

// Bounded File read, byte count and empty-file validation, atomic replacement.
// The deployment root is supplied by --file-root; the Lab never opens a file.
graph file_pipeline : App {
input in: string
output bytes_read: int
output empty: bool
output failed: FileExit // Standard-library type; no import is needed.
connector read_file : File {
mode = read
path = data as string
max_read = "64KiB"
timeout = duration("2s")
}
operator inspect {
input in: array<byte>
output accepted: array<byte>
output bytes_read: int
output empty: bool
on in match {
size(data) > 0 => { bytes_read = size(data); accepted = data }
_ => empty = true
}
}
connector write_file : File {
mode = replace
path = "copy.txt"
timeout = duration("2s")
}
in -> read_file -> inspect
inspect.accepted -> write_file
inspect.bytes_read -> bytes_read
inspect.empty -> empty
write_file.out -> nothing
read_file.exit -> match {
Completed -> nothing
AtomicReplaceUnavailable | InvalidPath | IoFailed | NotAFile | NotFound
| OutcomeUnknown | PermissionDenied | ReadLimitExceeded | TimeoutExpired -> failed
}
write_file.exit -> match {
Completed -> nothing
AtomicReplaceUnavailable | InvalidPath | IoFailed | NotAFile | NotFound
| OutcomeUnknown | PermissionDenied | ReadLimitExceeded | TimeoutExpired -> failed
}
}
lab files {
mock read_file {
on in { out = base64! { YWxwaGEKYmV0YQo= }; exit = Completed {} }
}
mock write_file {
on in { exit = Completed {} }
}
scenario copy {
app.in = "input.txt"
expect app.bytes_read == 11
expect write_file.in where size(data) == 11
expect app.failed == nothing
}
scenario empty {
mock read_file { on in { out = base64! {}; exit = Completed {} } }
app.in = "empty.txt"
expect app.empty
expect write_file.in == nothing
}
scenario utf8 {
mock read_file { on in { out = base64! { w6kK }; exit = Completed {} } }
app.in = "utf8.txt"
expect app.bytes_read == 3
expect write_file.in where size(data) == 3
expect app.failed == nothing
}
scenario directory {
mock read_file { on in { exit = NotAFile {} } }
app.in = "directory"
expect app.failed where data is NotAFile
expect write_file.in == nothing
}
scenario single_byte {
mock read_file { on in { out = base64! { eA== }; exit = Completed {} } }
app.in = "single-byte.txt"
expect app.bytes_read == 1
expect write_file.in where size(data) == 1
expect app.empty == nothing
expect app.failed == nothing
}
}

In files.copy, both File Calls use Lab responses; the count is emitted before the accepted bytes reach write_file.

In files.copy, both File Calls use Lab responses; the count is emitted before the accepted bytes reach write_file.write_file: Fileinspectread_file: Filefile_pipelineLab: files.copywrite_file: Fileinspectread_file: Filefile_pipelineLab: files.copyCompleted exits are disposed to nothingin = input.txtin = input.txtScripted read request11 bytes, then Completedout to in = alpha\nbeta\nbytes_read = 11accepted to in = the same bytesScripted replace request for copy.txtCompleted
In files.copy, both File Calls use Lab responses; the count is emitted before the accepted bytes reach write_file.write_file: Fileinspectread_file: Filefile_pipelineLab: files.copywrite_file: Fileinspectread_file: Filefile_pipelineLab: files.copyCompleted exits are disposed to nothingin = input.txtin = input.txtScripted read request11 bytes, then Completedout to in = alpha\nbeta\nbytes_read = 11accepted to in = the same bytesScripted replace request for copy.txtCompleted

The Lab supplies alpha\nbeta\n: eleven bytes. The Operator counts them and forwards a nonempty value to the replacement File Connector. Empty input does not overwrite the target. A directory produces NotAFile, routed to failed. The utf8 scenario counts é\n as three bytes, not two characters: the reader's array<byte> port determines how the value is read. single_byte supplies x, exactly one byte, and checks that it reaches the writer rather than the empty output. This is the smallest input accepted by size(data) > 0.

To exercise real disk I/O, use a macOS or Linux shell with mktemp, printf and cmp on PATH. This creates a new temporary directory; both file operations are confined to that directory, and the recording uses an unused path:

Terminal window
demo_root=$(mktemp -d)
printf 'alpha\nbeta\n' > "$demo_root/input.txt"
printf 'input.txt\n' | rastok run examples/file-pipeline.rkg --file-root "$demo_root" --record "$demo_root/copy.rkr"
cmp "$demo_root/input.txt" "$demo_root/copy.txt"
printf 'changed after recording\n' > "$demo_root/input.txt"
rastok replay "$demo_root/copy.rkr"

This run printed the following values. Compare the seal from your run with the seal from its replay:

bytes_read: 11
recorded: 12 causal records · seal bfa1df6bc5ab7eaeb42419aa0203af9b5350ee0e4252f96ed4fcb2c4159f7977

cmp exited successfully without output. After changing the input file, replay printed:

replay: 12 causal records · seal bfa1df6bc5ab7eaeb42419aa0203af9b5350ee0e4252f96ed4fcb2c4159f7977 · completed recording · no Driver I/O
bytes_read: 11

Replay prints the same recorded output and seal without reading or replacing either file. It does not restore the disk to its earlier contents. Use rastok replay "$demo_root/copy.rkr" --ui to inspect the recorded history.

Change size(data) > 0 to size(data) > 1. The one-byte input is now rejected as empty, but the other four scenarios still pass. Run the comparison:

Terminal window
rastok simulate examples/file-pipeline.rkg --compare examples/traces/file-pipeline
PASS files.copy
PASS files.empty
PASS files.utf8
PASS files.directory
FAIL files.single_byte
expect app.bytes_read == 1 (file-pipeline.rkg:84)
nothing observed
expected 1 matching delivery on app.bytes_read, but observed 0
expect write_file.in where size(data) == 1 (file-pipeline.rkg:85)
nothing observed
where size(data) == 1 is false for 0 of 0 deliveries; count 0, expected 1
expect app.empty == nothing (file-pipeline.rkg:86)
observed app.empty: true
expected no deliveries on app.empty, but observed 1
golden divergence: record 7: emissions(23)[0][0]; expected "bytes_read"; got "empty"; cause: TRANSACTION "inspect" · trigger "p:in" · cause_id absent
5 scenario(s), 1 failed

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

Terminal window
git checkout -- examples/file-pipeline.rkg

The first changed Operator transaction names its changed emission and causal input. In single_byte, the byte count and writer assertions fail, and the empty output appears where the scenario expects none. The assertions fail even without --compare; the golden also locates the first changed record.

A passing scenario tests the inputs and responses it supplies, not every possible value. The earlier empty, three-byte and eleven-byte cases could not distinguish > 0 from > 1. The one-byte case adds that boundary to the reviewed examples. A golden comparison does not prove two programs equivalent for inputs absent from the Lab. Restore the threshold and compare again.

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/file-pipeline/files.copy.rkc

a6070108a40150f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c804010582024c820669696e7075742e74787409636c61620a000b000d8182702e696e3e726561645f66696c652e696e824d828169726561645f66696c658000
a7070211601282438280800013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c8
a807021169726561645f66696c6512824d828169726561645f66696c65800013676c3a737461727414801780181a50f3df1105112054d397a04ed0666ca4c81822824382808000
a60703181b50f3df1105112054d397a04ed0666ca4c8181c69726561645f66696c65181e58204871ffb86d08c9280e24baa4c587dad86a9970eee2bdf74bc788acdab0e5dc1f181f011821702e696e3e726561645f66696c652e696e
a70703181b50f3df1105112054d397a04ed0666ca4c8181c69726561645f66696c65181e58204871ffb86d08c9280e24baa4c587dad86a9970eee2bdf74bc788acdab0e5dc1f181f02182082018282636f757482024e82054b616c7068610a626574610a82646578697482025082138269436f6d706c65746564820f801821702e696e3e726561645f66696c652e696e
a707021169726561645f66696c6512824d828169726561645f66696c6580001364703a696e148183702e696e3e726561645f66696c652e696e82438280800001178283636f757400a5015077caecb1cc181b62e2b991b703bb4c7f0250f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c804010582024e82054b616c7068610a626574610a83646578697400a50150b80b3a765394a1c849ad18cc90b9659d0250f3df1105112054d397a04ed0666ca4c80350f3df1105112054d397a04ed0666ca4c804010582025082138269436f6d706c65746564820f80181981835077caecb1cc181b62e2b991b703bb4c7f7818726561645f66696c652e6f75743e696e73706563742e696e824b828167696e73706563748000
a807021167696e737065637412824b828167696e7370656374800013676c3a737461727414801780181a5077caecb1cc181b62e2b991b703bb4c7f1822824382808000
a807021167696e737065637412824b828167696e737065637480001364703a696e1481837818726561645f66696c652e6f75743e696e73706563742e696e824d828169726561645f66696c658000011782836a62797465735f7265616400a5015020f4693e7f501632f25c434a0371050b025077caecb1cc181b62e2b991b703bb4c7f0350f3df1105112054d397a04ed0666ca4c804010582024382020b8368616363657074656400a5015099f25f558f929e9517b51ebf3097d40a025077caecb1cc181b62e2b991b703bb4c7f0350f3df1105112054d397a04ed0666ca4c804010582024e82054b616c7068610a626574610a1818818201506da3bb6071d750c9a949a41d80aeadd2181981835099f25f558f929e9517b51ebf3097d40a781e696e73706563742e61636365707465643e77726974655f66696c652e696e824e82816a77726974655f66696c658000
a80702116a77726974655f66696c6512824e82816a77726974655f66696c65800013676c3a737461727414801780181a5099f25f558f929e9517b51ebf3097d40a1822824382808000
a60703181b5099f25f558f929e9517b51ebf3097d40a181c6a77726974655f66696c65181e5820dc84b5f65453acf1d3d8b78ee597a5f8a263f6435219c95886187dbf6d97b09b181f011821781e696e73706563742e61636365707465643e77726974655f66696c652e696e
a70703181b5099f25f558f929e9517b51ebf3097d40a181c6a77726974655f66696c65181e5820dc84b5f65453acf1d3d8b78ee597a5f8a263f6435219c95886187dbf6d97b09b181f02182082018182646578697482025082138269436f6d706c65746564820f801821781e696e73706563742e61636365707465643e77726974655f66696c652e696e
a60702116a77726974655f66696c6512824e82816a77726974655f66696c6580001364703a696e148183781e696e73706563742e61636365707465643e77726974655f66696c652e696e824b828167696e7370656374800001178183646578697400a50150e4b73bb11bac0a9dfe97fefbc0202588025099f25f558f929e9517b51ebf3097d40a0350f3df1105112054d397a04ed0666ca4c804010582025082138269436f6d706c65746564820f80
seal 50b6357a920f562ed961f967f3fd8bc92b5e91b842e420393244291b24819721

The guide explains the same source in Types and expressions and Connectors and outcomes.

The Contract makes an effect reviewable: input, operation, bounds and outcomes travel together. Your application decides what to do with each result. A recording then lets you inspect what happened without repeating those effects.

Previous: Echo. Next: Two tenants, where the new question is who owns the state when several requests share a graph.

max_read bounds the complete input: no partial bytes are published on a read refusal. File does not implement recursive directory operations, append or watch. Atomic replacement concerns one target and depends on the Driver's supported filesystem boundary; refusals have typed outcomes. The directory Lab scenario scripts that result; the real File tests qualify the actual filesystem refusal. The example validates and copies bytes.

Diagram