> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mirage.strukto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Line

> Route, hold, refuse or cap a whole line with pre_execute and post_execute.

`pre_execute` runs once per line, before any of it runs, and `post_execute`
once the line ends, with its output. TypeScript spells each method and field in camelCase (`preExecute`, `ctx.exitCode`).

| Method | Runs | Can return |
| - | - | - |
| `pre_execute` | once per line, before any of it runs | Deny, Ask, Route |
| `post_execute` | once per line, after it ends | Deny, Ask, Limit |

## Context

| Field | What it is |
| - | - |
| `line` | the line the agent typed |
| `commands` | every command of the line, each the context [`pre_command`](https://docs.mirage.strukto.ai/home/policy/command#context) gets for it |
| `exit_code`, `output` | `post_execute`: how the line ended and what it printed |
| `results` | `post_execute`: the line's execution tree, a node per command with its exit code, stderr and records |

`commands` is read before the line runs, so a word only the run can expand
(`$X`) is not resolved there yet. Every `ctx` also carries the [session fields](https://docs.mirage.strukto.ai/home/policy/policies.md#context).

## pre\_execute

```python Python theme={null}
from mirage.policy import Policy, Route, RouteContext

class JobsOnTheBox(Policy):
    async def pre_execute(self, ctx: RouteContext) -> Route | None:
        paths = [p.virtual for c in ctx.commands for p in c.paths]
        if any(p.startswith("/jobs/") for p in paths):
            return Route(machine="box")
        return None
```

```typescript TypeScript theme={null}
const jobsOnTheBox: Policy = {
  preExecute(ctx) {
    const jobs = ctx.commands.some((c) => c.paths.some((p) => p.virtual.startsWith('/jobs/')))
    return jobs ? { kind: 'route', machine: 'box' } : null
  },
}
```

`python3 /jobs/train.py` runs on the workspace's `box`
[machine](https://docs.mirage.strukto.ai/home/setup/machine/overview.md). See
[Route Machine](https://docs.mirage.strukto.ai/home/policy/route-machine.md).

## post\_execute

```python Python theme={null}
from mirage import Limit
from mirage.policy import ExecuteResultContext, Policy

class FirstHundredLines(Policy):
    async def post_execute(self, ctx: ExecuteResultContext) -> Limit:
        return Limit(max_lines=100)
```

```typescript TypeScript theme={null}
const firstHundredLines: Policy = {
  postExecute() {
    return new Limit({ maxLines: 100 })
  },
}
```

A longer output stops at 100 lines, and stderr says
`output truncated at limit (100 lines); ...`. A Deny here drops the
output: exit 126 and a decision record.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.