output writes to files and stdout. file/files project one or many files. log writes to stderr. append adds to a file, creating it if needed. stream forwards a command's output as it runs.

Word order: projection vs routing

The write directives split into two word-order families, and the order encodes what the directive does. There are no cross-aliases between them.

  • file / files declare a target's contents — target-first assignment (projection). The target appears first, on the left of =, and the value is what that file should contain: file "task.md" = @task, files "src/" = [...]. This is projection — the file re-renders from the assigned value. See File Directive and Files Directive.

  • output / append route a value to a destination — source-first to-clause (routing). The value (source) comes first, followed by a to clause naming where it goes: output @content to "out.txt". This is routing — an existing value is sent somewhere. log and stream are source-first too, with a fixed destination and no to clause (log @x, stream @f()). See Output Directive.

Output Directive

const @content = "hello"
const @data = { name: "Ada" }
const @message = "done"
const @error = "something failed"

output @content to "out.txt"
output @data to "config.json"
output @message to stdout
output @error to stderr

Objects and arrays are written as JSON. For .json files that is the file format; for other file types (.txt, .md, …) they are written as indented JSON text. Strings and numbers are written as plain text.

Selecting a field (output @value.email to …) keeps that field's labels, so policy still sees them. What gets written is plain text (or JSON for .json targets).

A target can be a variable holding the path: output @content to @path writes where @path points, exactly as output @content to `${@path}` would, and a path that carries labels brings them to the same checks. A variable that does not hold a string refuses. @root/… and other names followed by / are resolvers, not variables.

To write into the state an SDK host passed to the run, declare it with state external and call its set operation; output does not write state. See sdk-state.

A relative target is resolved from the running script's directory, not the current working directory — so the path means the same thing no matter where you launch the script from. Any missing parent directories in the target path are created automatically (like mkdir -p).

Inside a box, output writes into the box's workspace, not the project folder. The path must be relative and cannot contain ... Commands in the box can read the file, and after the box <@ws/path> reads it from the workspace (see box-blocks):

const @ws = box [
  output "hello" to "note.txt"
  run cmd { cat note.txt }
]
show <@ws/note.txt>

Security: output writes run under the .op:output operation label, so a guard bound to .op:output in a policy, or a policy label-flow rule, can inspect or block them. The guard sees the written value via @input. The source of output is a value, not a call: assign a call's result first (const @s = @readSecret(), then output @s to "out.txt"). The content is evaluated before the guard runs, so the guard sees the real value — which also means a denied write does not undo side effects the content had. To stop the content from running at all, guard the operation that produces it (.op:run, .op:llm).

Environment targets

const @v = "P"
output @v to env:FOO       >> child processes started later see FOO=P
output @v to env           >> no name: writes MLLD_V (MLLD_ + the source variable's name)
const @a = "Q" | output @input to env:BAR   >> pipeline-stage form; bare `env` writes MLLD_OUTPUT

to env:NAME writes the value's text (no trailing newline) under NAME; bare to env picks the default name from the source — MLLD_<SOURCE> on the directive form, MLLD_OUTPUT on the pipeline form. The name is a literal identifier and is never interpolated.

The write lands in an environment overlay: every child process spawned AFTER the write (run commands, sh/bash bodies, node and py subprocesses) sees the value. An in-process js body does NOT — it runs inside the mlld process, whose own environment never changes — and @input.env does not read the overlay either. A credential binding (from: "env:NAME") also reads only the real process environment — an overlay write never feeds a credential. When a call binds a credential under the same name, the credential wins.

Security: the write runs under the .op:output operation label with the same cascade, guard seam and label-flow gate as a file write, so a policy can forbid it. The gate fires once, at the write: later children inherit the value unmarked, so avoid writing names that change what a child executes (PATH, NODE_OPTIONS, LD_PRELOAD).

See Also

Log Directive

const @debugMode = true
const @message = "starting"
const @item = "report.md"
const @items = ["a.md", "b.md"]

log @message                        >> same as output @message to stderr
log `Processing: ${@item}`

>> In action contexts
for @item in @items => log @item
if @debugMode => log "Debug info"

See Also

Append Directive

const @record = { event: "start" }
const @data = { step: 1 }
const @runs = ["build", "test"]

append @record to "events.jsonl"        >> JSON object per line
append "raw line" to "events.log"

>> In pipelines: the value passes through unchanged
const @saved = @data | append "audit.jsonl"

>> In loops
for @name in @runs => append @name to "pipeline.log"

For .jsonl, each value must be JSON data (an object, array, number, …) and is written as one line of JSON; a plain string is refused. .json targets are refused: use .jsonl. Other extensions write text.

Selecting a field (append @value.email to …) keeps that field's labels, so policy still sees them. What gets written is plain text, or one line of JSON for .jsonl.

In a pipeline, | append @path writes where @path points, as output … to @path does (see output), and a variable that does not hold a string refuses. A quoted "@path" is a file of that name.

A relative target is resolved from the running script's directory, not the current working directory. A missing file is created, and any missing parent directories in the target path are created automatically (like mkdir -p).

Inside a box, append writes into the box's workspace, not the project folder, with the same path rules as output: relative only, no .. (see box-blocks).

Security: append writes run under the .op:append operation label, so a guard bound to .op:append in a policy, or a policy label-flow rule, can inspect or block them. The guard sees the appended value via @input. The source of append is a value, not a call: assign a call's result first (const @s = @readSecret(), then append @s to "out.txt"). The content is evaluated before the guard runs, so the guard sees the real value — which also means a denied write does not undo side effects the content had. To stop the content from running at all, guard the operation that produces it (.op:run, .op:llm).

Streaming

Streaming shows a command's output while it runs, instead of only when it finishes.

Stream an Executable

Use stream @f() to forward a shell or command executable's output to stderr as it arrives. The completed value is omitted from the document, so the output appears once. The examples print, pause and print again, which takes a full shell, so they use sh; sh blocks are off by default, and the policy line switches them on:

policy @allow_sh = { * { capabilities: { sh: true } } }
exe @progress() = sh { printf 'first\n'; sleep 1; printf 'second\n' }
stream @progress()

cmd bodies use the same forwarding behavior. Executables that delegate to another executable preserve streaming; an explicit stream: false on the delegated call opts out. stream @progress() with { stream: false } disables forwarding and still omits the result from the document. A body that produces no chunks as it runs (js, node, py, a template) is not streamed progressively: stream @f() shows its completed result once on stderr when the call finishes, after the same guard and policy checks. The result still stays out of the document.

A before-guard on .op:stream, a data guard, or a label-flow policy can inspect the completed result. Output is held until that check allows it; denied output is discarded. These guarded calls emit their output after completion. An unrelated operation guard, such as .op:show, leaves streaming progressive. A matching after-guard refuses before a streaming call executes because it cannot retract output already sent to the terminal; with { stream: false } disables that streaming restriction.

Stream a Command Body

run stream sh { … } and run stream node { … } forward each chunk to stderr as the command emits it, then add the complete output to the document when the command exits:

policy @allow_sh = { * { capabilities: { sh: true } } }
run stream sh { printf 'building\n'; sleep 1; printf 'done\n' }

stream is not available on exe definitions or inside a parallel group (|| …).

Format Adapters

Tools that print NDJSON events (one JSON object per line), such as the Claude CLI's stream-json output, need a format adapter to turn the events into text. Set stream_format on a run cmd { … } with stream: true:

run cmd {printf '{"type":"text","text":"hi "}\n{"type":"text","text":"there"}\n'} with { stream: true, stream_format: "ndjson" }

The decoded text (hi , there) is added to the document. The Claude CLI form:

run cmd {claude -p "Hello" --output-format stream-json --verbose --include-partial-messages} with { stream: true, stream_format: "claude-code" }
Adapter Aliases Parses
ndjson — Generic NDJSON
claude-code claude-agent-sdk Claude CLI stream-json events

Without stream_format, the raw NDJSON lines pass through unchanged. stream_format works on run cmd { … } and on a call to an executable whose body is cmd { … }; on a sh or node body it is an error.

On a call, stream_format turns the stream on without stream: true (an explicit stream: false keeps it off), and the decoded text goes wherever the call's stream goes. stream @f() with { stream_format: "ndjson" } forwards the decoded text to stderr. A call in a value position, such as show @f() with { stream_format: "ndjson" }, shows its ordinary result.

Suppress

mlld script.mld --no-stream
MLLD_STREAMING=false mlld script.mld

--no-stream, MLLD_STREAMING=false and MLLD_NO_STREAMING=true each turn off document streaming: the finished document is printed once when the script ends, instead of piece by piece as each directive produces output. They do not affect run stream or stream @f(): those still forward chunks to stderr as they arrive.

If the consumer of streamed output stops reading — a pipe nobody drains any more — the run waits up to ~30s for the output to be accepted, then fails with the output consumer stopped reading instead of hanging forever. A consumer that reads slowly (just not instantly) never trips this and receives the full stream. A closed pipe (mlld script.mld | head) remains an ordinary way to use mlld and does not fail the run.

Limitations

  • A matching after-guard refuses a streaming call before it executes: output already forwarded to stderr cannot be retracted for later validation. A before-guard works instead — the output is buffered until the guard admits it.
  • Pipeline stages buffer between stages; streaming is within each stage

File Directive

const @task = "Summarize the release notes"
const @summary = "Three fixes, one feature."
file "task.md" = @task
file "notes/today.md" = `## Notes\n${@summary}`

file writes exactly one file.

Inside a box block — writes to the box's workspace VFS:

const @task = "Summarize the release notes"
const @source = "console.log('hi')"
box [
  file "task.md" = @task
  file "src/main.js" = @source
  run cmd { cat task.md }
]

Files written this way are scoped to the box, and commands run inside the box (run cmd { … }) can read them. Inside a box, a path must be relative (no leading /), cannot contain .., and can be written only once: writing the same path again is an error.

Outside a box block — writes to the real filesystem. A relative target is resolved from the running script's directory, not the current working directory, so the path means the same thing regardless of where you launch the script. Any missing parent directories in the target path are created automatically (like mkdir -p). Outside a box, file does not restrict the path: an absolute path or a path with .. writes wherever it points, including outside the project, and writing the same path again replaces the file. To keep writes inside a workspace, write them in a box.

Security: file writes run under the .op:file operation label, so a guard bound to .op:file in a policy, or a policy label-flow rule, can inspect or block them (for example, denying a .src:file-labeled value from being written to disk). The guard sees the written value via @input. The content is evaluated before the guard runs, so the guard sees the real value — which also means a denied write does not undo side effects the content had. To stop the content from running at all, guard the operation that produces it (.op:run, .op:llm).

Files Directive

const @source = "console.log('hi')"
const @config = { debug: true }
files "src/" = [
  { "index.js": @source, desc: "Entry point" },
  { "config.json": @config }
]

Each entry is an object with one file key mapped to content. An entry may also carry desc, a short description of the file; reading descriptions back is not available in mlld 2.2.0.

Named workspace:

const @task = "Summarize the release notes"
const @code = "console.log('hi')"
files <@workspace/> = [{ "task.md": @task }]
files <@workspace/src/> = [{ "main.js": @code }]
const @content = <@workspace/task.md>
show @content

The <@name/> form creates an in-memory workspace named @name on first use; nothing is written to disk. Later files <@name/subdir/> writes add to the same workspace. Read files back with <@name/path>.

Inside a box block — writes to the box's workspace:

const @source = "console.log('hi')"
const @utils = "export {}"
box [
  files "src/" = [
    { "index.js": @source },
    { "util.js": @utils }
  ]
  run cmd { ls src/ }
]

Loading files from a git repository (files <@ws/> = git "…") is not available in mlld 2.2.0; clone the repository before the run and write its files with the entry-list form above. Inspecting a workspace's changes (.mx.edits, .mx.diff) is not available in mlld 2.2.0 either.

Security: files writes run under the .op:files operation label, so a guard bound to .op:files in a policy, or a policy label-flow rule, can inspect or block them — for example, denying a value labeled .src:file from being written out. A guard sees the written values via @input. The content is evaluated before the guard runs, so the guard sees the real value — which also means a denied write does not undo side effects the content had. To stop the content from running at all, guard the operation that produces it (.op:run, .op:llm). Enforcement is atomic: if any entry carries a denied label, the whole directive is refused and no file is written.