Stream run output
Save incremental task output and resume readers without losing committed chunks.
Use a named stream when readers need intermediate output before a task returns. Runlane stores the chunks with the run. Your application still owns authorization and delivery to its clients.
Write output from a handler
import { task } from '@runlane/core'
import * as z from 'zod'
export const writeReport = task({
id: 'report.write',
schema: z.object({ title: z.string() }),
output: z.string(),
streams: { report: z.string() },
async run({ title }, context) {
const output = context.stream('report')
await output.append(`# ${title}\n`)
await output.appendBatch(['First section\n', 'Second section\n'])
return 'Report saved'
},
})The declaration types each append as a string and validates it before storage. Only declared names can be opened. Each awaited append is durable before the next handler statement. The returned string is the final task result, independent of the chunks. For a provider's AsyncIterable, pass it to await output.pipe(source); the pipe limits outstanding work and flushes while a slow source waits.
Read and save a checkpoint
const page = await runlane.runs.readStream(runId, 'report')
for (const record of page.records) {
if (record.type === 'chunk') process.stdout.write(String(record.value))
}
const savedCursor = page.cursor
const next = await runlane.runs.readStream(runId, 'report', { after: savedCursor })Keep the cursor with the exact run and name. Continue while hasMore is true. An empty page does not mean an active writer has finished.
For a combined run-status and stream feed, use runs.subscribe(runId, { after, signal }). Persist the cursor after applying each event. Committed chunks arrive before terminal status. On reconnect, pass that saved composite cursor to the same subscription API.
Display later attempts
A retry or resume starts another handler attempt. Named reads include attempt_started and derived attempt_closed records. Clear the displayed answer at a new start when showing only the current attempt, or retain every attempt for an audit view. Do not concatenate answers from separate attempts by accident.
Runlane drains admitted writes before saving the attempt result. Quota or storage failure can therefore fail the attempt even when application code caught an append rejection. An appendBatch() error reports its confirmed prefix in error.meta.accepted. An ambiguous failure can leave more committed chunks; read retained records before appending again.
Inspect from the terminal
npm exec -- runlane --json runs stream <run-id> report
npm exec -- runlane --json runs stream <run-id> report --after <cursor> --followSee stream contracts for limits, cursor scope, cancellation, and retention.
Follow in a browser
Mount the realtime handler, then use useRunStream to show the parts with parts.join(''). Read the final result separately with useRun. See set up realtime and streams in React.