Repository navigation
Commit d2418d6
authored
Add read tool output guardrails (#4598)
Adds bounded, line-numbered output to the agents runtime `read` tool so
file reads no longer dump up to 512KB of raw text into model context.
Users can page through files with `offset`/`limit`, get explicit
continuation guidance, and the edit tool no longer requires a prior read
before applying an exact string replacement.
## Root Cause
The read tool had a 512KB hard file-size cap, but successful reads under
that limit returned the entire file. That made medium-sized files
expensive in context and gave agents no first-class way to inspect
specific line ranges except by falling back to shell commands.
The edit tool also enforced a same-wake prior-read check. That added
friction without much safety: an edit is already constrained by whether
the exact `old_string` patch applies uniquely, or by `replace_all=true`
when requested.
## Approach
- Add optional `offset` and `limit` parameters to `read`.
- Render file contents with line numbers and XML-ish path/content
wrappers.
- Bound returned file content to 2000 lines and 50KB by default.
- Truncate individual lines longer than 2000 characters.
- Include continuation hints such as `Use offset=2001 to continue` when
output is partial.
- Preserve existing safeguards for sandbox containment, binary files,
and files over 512KB.
- Remove the edit tool's read-set gate while keeping exact-match and
multi-match protections.
- Update Horton prompt/tool descriptions so agents are no longer told
they must read before editing.
## Key Invariants
- A single read call should have a predictable context footprint.
- Truncation must be explicit, never silent.
- Edits should only apply when `old_string` is found exactly once,
unless `replace_all=true` is provided.
- Existing binary and oversized-file rejection behavior should remain
intact.
## Non-goals
- This does not add true streaming/ranged filesystem reads; the current
sandbox API still reads the file after the existing 512KB stat gate.
- This does not add `.hortonignore`/secret deny rules.
- This does not broaden edit semantics beyond existing exact string
replacement behavior.
## Trade-offs
The implementation keeps the existing 512KB file-size gate instead of
introducing sandbox-level streaming. That makes the change smaller and
safer, while still reducing the returned tool output from as much as
512KB to a bounded page. A future improvement could move truncation
earlier into the sandbox read path for larger-file previews.
Removing the prior-read gate relies on the patch application itself for
safety: if the exact text is absent or ambiguous, the edit is rejected.
This is simpler and avoids forcing unnecessary reads solely to satisfy
runtime state.
## Verification
```bash
cd packages/agents-runtime
pnpm exec vitest run test/read-file-tool.test.ts test/write-edit-roundtrip.test.ts test/edit-tool-read-guard.test.ts test/sandbox-tool-refactor.test.ts test/readset-isolation.test.ts test/sandbox-tool-symlink-safety.test.ts
pnpm run typecheck
cd ../agents
pnpm exec vitest run test/horton-system-prompt.test.ts
pnpm run typecheck
```
Also attempted broader agents tests; unrelated bootstrap/pull-wake tests
failed locally with undici cache `invalid onError method` errors after
building runtime dist.
## Files changed
- `packages/agents-runtime/src/tools/read-file.ts` — adds offset/limit
params, line-numbered bounded output, truncation metadata, and
empty-file/trailing-newline handling.
- `packages/agents-runtime/src/tools/edit.ts` — removes the same-session
prior-read gate while preserving exact-match replacement behavior.
- `packages/agents-runtime/test/read-file-tool.test.ts` — adds focused
coverage for default paging, offset/limit, byte/line truncation, and
empty/trailing-newline handling.
- `packages/agents-runtime/test/edit-tool-read-guard.test.ts` and
`readset-isolation.test.ts` — update coverage for edit behavior without
read-set enforcement.
- `packages/agents/src/agents/horton.ts` — removes prompt guidance
claiming edits require prior reads.
- `.changeset/horton-read-file-guardrails.md` — patch changeset for
affected agents packages.1 parent fcc5864 commit d2418d6
17 files changed
Lines changed: 389 additions & 250 deletions
File tree
- .changeset
- packages
- agents-runtime
- src/tools
- test
- agents
- src/agents
- test
- website/docs/agents/entities/agents
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
| 9 | + | |
16 | 10 | | |
17 | 11 | | |
18 | 12 | | |
19 | | - | |
| 13 | + | |
20 | 14 | | |
21 | 15 | | |
22 | 16 | | |
23 | 17 | | |
24 | 18 | | |
25 | | - | |
| 19 | + | |
26 | 20 | | |
27 | 21 | | |
28 | 22 | | |
| |||
45 | 39 | | |
46 | 40 | | |
47 | 41 | | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | 42 | | |
53 | 43 | | |
54 | 44 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | 45 | | |
63 | 46 | | |
64 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
9 | 117 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
14 | 131 | | |
15 | 132 | | |
16 | 133 | | |
17 | | - | |
| 134 | + | |
18 | 135 | | |
19 | 136 | | |
20 | 137 | | |
21 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
22 | 151 | | |
23 | 152 | | |
24 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
25 | 158 | | |
26 | 159 | | |
27 | 160 | | |
| |||
33 | 166 | | |
34 | 167 | | |
35 | 168 | | |
36 | | - | |
| 169 | + | |
37 | 170 | | |
38 | 171 | | |
39 | | - | |
| 172 | + | |
40 | 173 | | |
41 | 174 | | |
42 | 175 | | |
43 | 176 | | |
44 | 177 | | |
45 | | - | |
46 | | - | |
| 178 | + | |
| 179 | + | |
47 | 180 | | |
48 | 181 | | |
49 | 182 | | |
| |||
52 | 185 | | |
53 | 186 | | |
54 | 187 | | |
55 | | - | |
| 188 | + | |
56 | 189 | | |
57 | 190 | | |
58 | 191 | | |
59 | 192 | | |
60 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
61 | 197 | | |
62 | | - | |
63 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
64 | 209 | | |
65 | 210 | | |
66 | 211 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
| 9 | + | |
13 | 10 | | |
14 | 11 | | |
15 | 12 | | |
| |||
29 | 26 | | |
30 | 27 | | |
31 | 28 | | |
32 | | - | |
33 | | - | |
| 29 | + | |
34 | 30 | | |
35 | 31 | | |
36 | 32 | | |
| |||
43 | 39 | | |
44 | 40 | | |
45 | 41 | | |
46 | | - | |
47 | 42 | | |
48 | 43 | | |
49 | 44 | | |
| |||
0 commit comments