Skip to content
Source
Sign inSign up

Pass typed outputs between jobs

Software factory / Agent workflows

Pass typed outputs between jobs

One job of your workflow finds the work, and the next job does it. You want the second job to get the result of the first as typed data, not as text to parse.

3 min read

Give the first job an output schema, and read it in the next job from ctx.needs. When the first job finds ten things and you want ten jobs, add forEach: it runs one cell for each element of an array in that output.

Start with a working workflow: see Write your first agent workflow.

1. Give a job an output

Set output to a zod schema. Then one step must set the output:

  • an agent step with output: 'job': its answer is the job's output, or
  • a do step that calls ctx.setOutput(value). The last value counts.
import { agent, defineAgentWorkflow, manual, z } from '@tanstack-source/ci'

const model = { provider: 'anthropic', model: 'claude-opus-5-5' } as const

export default defineAgentWorkflow('flaky-tests', { on: [manual()] })
  .job('find', {
    output: z.object({ tests: z.array(z.object({ file: z.string(), name: z.string() })) }),
    steps: [
      agent({
        name: 'Find',
        model,
        prompt: 'Find the tests in this repository that use a fixed sleep. List each file and test name.',
        tools: ['read_file', 'list_files', 'search'],
        output: 'job',
      }),
    ],
  })

The forge checks the output against the schema when the job ends. An output that does not pass fails the job.

2. Read it in the next job

Add a job that needs the first one. ctx.needs.find.output is typed from the schema:

  .job('report', {
    needs: ['find'],
    steps: [
      {
        name: 'Count',
        do: (ctx) => console.log(`${ctx.needs.find.output.tests.length} tests use a fixed sleep`),
      },
    ],
  })

ctx.needs.<job>.result is success, failure, skipped or cancelled.

3. Run one cell for each element

Add forEach with the job and the dot path of an array in its output. The job runs one cell for each element, and ctx.item is the element:

  .job('fix', {
    needs: ['find'],
    forEach: { job: 'find', path: 'tests' },
    permissions: { contents: 'write' },
    steps: [
      agent({
        name: 'Fix',
        model,
        prompt: (ctx) => `Replace the fixed sleep in this test with a wait for the real condition: ${JSON.stringify(ctx.item)}`,
        tools: ['read_file', 'search', 'run_command', 'write_file', 'push_fix'],
      }),
    ],
  })

In a do step, ctx.item and ctx.needs have the types of the schema, so ctx.item.file is a string. In the prompt function of an agent step, they are unknown. Give them to the model as JSON, as above.

4. Run it

  1. Merge the file, then click Run next to flaky-tests on the Workflows page.
  2. On the run page, click a fix cell, for example fix [0], to read its element, its steps and its log.

Done

The find job lists the tests once. Each test gets its own fix cell, which pushes its fix to its own branch under agent/flaky-tests/.

Outputs of steps

An agent step can also have its own schema as output. Later steps of the same job read it as ctx.steps.<name>.output:

agent({ name: 'Plan', model, prompt: 'Plan the change.', output: z.object({ files: z.array(z.string()) }) })

Matrix and forEach together

A job with matrix and forEach gets one cell for each matrix cell and each element. A job that needs a job with a matrix or forEach reads ctx.needs.<job>.cells, each with its matrix, result and output.

Limits

Output sizes

  • An output has at most 1 MiB of JSON. A larger step output fails its step. A larger job output fails the job.
  • The outputs of the jobs that one job needs have at most 8 MiB of JSON together. With more, that job fails before it runs, and the run page says why.
  • An output schema has at most 1 MiB of JSON. The planner refuses a larger one.
  • The run page and src factory agent-run view <run id> show the size of each output. The outputs of a job cell load when you open that cell. From a terminal, add --cell <key>.

Cells

  • A job has at most 256 cells. When forEach would make more, the job fails.
  • forEach reads the output of a job with one cell: no matrix and no forEach of its own.
  • An empty array skips the job.
  • When the path is not in the output, or is not an array, the job fails with the reason.

Next