Skip to content

Commit be784f2

Browse files
acn-ericlawclaude
andcommitted
feat(llm-helper): attachments on a single llm.chat call - images and PDF pages named by reference and read by the helper (RFC-0016, step 2)
llm.chat took text only, so a bounded AI node that had to read a page could not show it to the model. A request may now name files inside llm.attachments.root: the helper reads them in a worker thread and inside the call's deadline, decides the kind by the first bytes and sends base64 image and document blocks before the last turn's text; pages cuts a PDF to the pages named. One root with the agent tools' workspace rules, the provider's limits checked before any model call, never on llm.stream or a loop turn. With the audit of a single call, the record keeps a reference with the source file's size and SHA-256, never its bytes. The Node twin of Accenture/mercury-python#82. The reading is a module of its own, attachments.mjs, run in a worker thread: a hash, base64 and a PDF's cut are CPU work, and the helper's event loop carries every other call and stream. A new optional package, @cantoo/pdf-lib (a devDependency here), cuts pages and tells an encrypted PDF. The shared file test/vectors/llm-attachments-vectors.json (48 cases) over eight fixture files, all byte for byte the Python pack's, and seven tests pin it; thirty-three mutations of the code, the file and a fixture were each caught by a test. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent bfe7c20 commit be784f2

19 files changed

Lines changed: 2717 additions & 22 deletions

‎.gitattributes‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ memory/archive/*.md merge=union
55
*.sh text eol=lf
66
.githooks/* text eol=lf
77
.githooks/*.d/* text eol=lf
8+
9+
# the attachment fixtures are pinned byte for byte (test/llm-attachments.test.mjs) and shared with the Python
10+
# pack: never convert a line ending in them, whatever a checkout's autocrlf says
11+
test/fixtures/attachments/* -text

‎CHANGELOG.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@
201201
file were each caught by a test. **Upgrade note:** none without the switch; it is off by default. With it on,
202202
`llm.transcript.store` must be set, or the application stops at start by name.
203203

204+
11. **Attachments on a single `llm.chat` call: images and PDF pages named by reference and read by the helper
205+
(RFC-0016 in mercury-composable, build plan step 2), in lockstep with the Python pack (mercury-python #82).** A
206+
request may carry `attachments`, a non-empty list whose entries are a path inside `llm.attachments.root` or
207+
`{path, pages}`. The request names a file and never carries one: the helper reads the file in its folder and
208+
sends its bytes to the model as an `image` block (PNG, JPEG, GIF, WebP) or a `document` block (PDF) before the
209+
text of the last turn, the kind decided by the file's first bytes. `pages` sends a new PDF with only those
210+
pages, in the order named. One folder, resolved once at start, with the agent tools' workspace rules; the
211+
provider's limits refused by name before any model call (the count, the bytes of files, an image's size by
212+
backend, an encrypted or unreadable PDF, a page the document does not have); never on `llm.stream` or a loop
213+
turn. The reading is `extensions/llm-helper/attachments.mjs`, run in a **worker thread** of its own, because a
214+
hash, base64 and a PDF's cut are CPU work and the helper's event loop carries every other call and stream; the
215+
call's deadline (`params.timeout_ms`) covers the reading, and the model gets what is left of it. With the audit
216+
of a single call, the turn's `request` keeps each file as a reference with the size and the SHA-256 of the
217+
source file, never its bytes. **A new optional package, `@cantoo/pdf-lib`** (MIT, the maintained fork of
218+
pdf-lib; a devDependency here, `npm install @cantoo/pdf-lib` where pages are used): it cuts pages and tells an
219+
encrypted PDF; without it a whole document goes as it is and `pages` is a 503 that names it. Pinned by the
220+
shared file `test/vectors/llm-attachments-vectors.json` and the eight fixture files of
221+
`test/fixtures/attachments`, byte for byte the Python pack's (48 cases), and seven tests; thirty-three mutations
222+
of the code, the file and a fixture were each caught by a test. **Read:** the folder is readable through the
223+
model by every caller that can reach the helper, and its files are sent to the provider. **Upgrade note:** none
224+
without `attachments`.
225+
204226
### Fixed
205227

206228
1. **The agent-question app keeps the first accepted decision when two devices decide one card at the same moment**

‎extensions/llm-helper/README.md‎

Lines changed: 121 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ token batches as they are produced.
9999
| `prompt` or `messages` | A single user turn, or conversation turns `[{role: user\|assistant, content}]`. `messages` wins when both are given. Content is plain text. |
100100
| `system` | Optional system prompt (text). |
101101
| `schema` | `llm.chat` only. A JSON schema; the reply is then structured output, returned parsed as `data`. `additionalProperties` defaults to `false`. A schema on `llm.stream` is a 400. |
102+
| `attachments` | `llm.chat` only, and never a loop turn. Files for the model to look at, named by reference: a non-empty list whose entries are a path inside `llm.attachments.root`, or `{path, pages}` for pages of a PDF, such as `13` or `1-3,7`. See Attachments. |
102103
| `params.model` | The model. Default `claude-opus-5-5` (`llm.model`). On Bedrock's default surface a bare id is sent with the `anthropic.` prefix and an id already spelled for Bedrock as written; on the classic runtime surface every id is sent as written, an inference profile id as the account lists it. |
103104
| `params.max_tokens` | Output cap. Default 16000 (`llm.max.tokens`). |
104105
| `params.timeout_ms` | `llm.chat`: a deadline for the whole call, SDK retries included. `llm.stream`: the idle allowance between events. Default 60000 (`llm.timeout.ms`). |
@@ -158,6 +159,103 @@ the model refused. A stream that ends before its first token fails with a 422 in
158159
is too small for the question can end with no text at all (seen live with a 120-token budget): that is
159160
the 422 above, not an empty stream. Raise `params.max_tokens` or lower `params.effort`.
160161

162+
## Attachments
163+
164+
A bounded AI node sometimes has to read a page, not a text: a scanned form, a table printed as an image, a chart, a
165+
screenshot, chosen pages of a long PDF. A single `llm.chat` call may name such files in `attachments` (RFC-0016 in
166+
mercury-composable). **The request names a file and never carries one,** so the event stays small and a file's bytes
167+
reach no log, trace or stored record.
168+
169+
```text
170+
graph node ── a path ──> llm-helper ── reads the file in its folder ── the file's bytes ──> the model
171+
```
172+
173+
**Who reads the file.** The helper does. The model is a remote service and reads nothing on this host: the node
174+
passes a path to the helper, the helper reads the file and sends its bytes to the model inside the request, as an
175+
image or a document block before the text of the last turn. The path never reaches the model, and the file's content
176+
leaves for the provider, as a prompt's text does.
177+
178+
**The shared folder.** `llm.attachments.root` names the one folder a request may read in; unset, every attachment is
179+
refused. Whatever puts the files there does so before the graph runs: an upload, a copy, a mounted volume, or a
180+
script that downloads them from an object store (the helper downloading from one itself is a later iteration). A
181+
node names a file by its path inside the folder, sub-folders included:
182+
183+
```
184+
create node read-page
185+
with type Module
186+
with properties
187+
skill=graph.task
188+
task=llm.chat
189+
input[]=text(Return the blocks of the attached page.) -> prompt
190+
input[]=model.page_schema -> schema
191+
input[]=model.page_ref -> attachments[0].path
192+
input[]=model.page_number -> attachments[0].pages
193+
output[]=result.data -> model.page_reading
194+
```
195+
196+
A mapping whose source is null supplies no parameter, so a node with no `model.page_ref` sends a text-only call that
197+
still answers: guard the node with a decision on the reference when the page is required.
198+
199+
**What the helper does.**
200+
201+
- **Stays inside the folder.** A path is relative; the folder is resolved once at start, and a file is read only when
202+
its resolved path lies inside it, so a path that climbs out and a link that leads out are refused. Hidden entries
203+
(a name that starts with a dot) are refused, and only a regular file is read. These are the rules of the agent
204+
tools' workspace (`tools.file.root`).
205+
- **Decides the kind by the first bytes, never by the name.** PNG, JPEG, GIF or WebP becomes an `image` block; a PDF
206+
a `document` block, from which the model reads both the text and the page image.
207+
- **Cuts a PDF to the pages asked for.** With `pages`, the helper writes a new PDF that holds only those pages, in the
208+
order named, each named once, so a long document costs the pages asked for. This needs the `@cantoo/pdf-lib`
209+
package (`npm install @cantoo/pdf-lib`). With the package installed a whole document is also checked; without it
210+
a whole document goes as it is and the provider judges it.
211+
- **Refuses before any model call** what the provider would refuse, with a status that names the rule, so a caller
212+
learns its mistake without spending a token.
213+
- **Reads in a worker thread and inside the call's deadline:** the files, the hashes and a PDF's cut never hold the
214+
event loop, which carries every other call and stream (`attachments.mjs`, next to the helper), and
215+
`params.timeout_ms` covers the files and the model.
216+
217+
| Case | Status |
218+
|---|---|
219+
| `attachments` on `llm.stream`, or on a loop turn; a last turn that is not the user's | 400 |
220+
| Not a non-empty list; an entry that is neither a path nor `{path, pages}`; `pages` that are malformed, run backward or name a page twice | 400 |
221+
| More files than `llm.attachments.max.count`, or more bytes of files than `llm.attachments.max.bytes` (taken from each file's size before it is read) | 400 |
222+
| An absolute path, a path or a link that leads out of the folder, a hidden entry, a missing file, a folder | 400 |
223+
| A file that is not PNG, JPEG, GIF, WebP or PDF; `pages` on an image; a page the document does not have; an encrypted or unreadable PDF | 400 |
224+
| An image larger than the backend takes, base64-encoded: 10 MB on the Claude API, 5 MB on Amazon Bedrock | 400 |
225+
| No `llm.attachments.root` configured | 503 |
226+
| `pages` without the `@cantoo/pdf-lib` package | 503, naming the package |
227+
| A configured `llm.attachments.root` that is not a folder | the application stops at start (a request that meets it later is a 500) |
228+
229+
**Limits that are the provider's.** The helper sends no more than 20 files by default because a request with more
230+
than 20 image and document blocks is held to a tighter limit on each image's dimensions. A request takes at most 600
231+
PDF pages, 100 on a model whose context window is under 1M tokens, and each page costs its text and its image in
232+
tokens: name `pages`. The helper sets no page limit of its own; a document over the provider's is the provider's 400,
233+
passed through. Both Bedrock surfaces take these blocks.
234+
235+
**What it costs.** The file's content travels in every call that names it - nothing is kept at the provider between
236+
calls - and it counts as input tokens, by what the model sees and not by the file's size in bytes. An image costs one
237+
token per 28 by 28 pixel patch, capped by the model's resolution tier: at most 1,568 tokens on the standard tier and
238+
4,784 on the high-resolution tier of the newer models, so a full-page scan is a few thousand tokens at most whatever
239+
its megabytes. A PDF page costs its text, typically 1,500 to 3,000 tokens, plus its image, at most 1,568. `pages` is
240+
therefore the cost control: ten pages named cost ten pages, however long the document.
241+
242+
**The folder is readable through the model.** Every caller that can reach this app can have a file of the folder
243+
sent to the provider and read back in the reply. Choose the folder as deployment configuration, give it only what a
244+
graph may show a model, and keep it out of reach of anything that is not trusted to write there.
245+
246+
**In the audit store.** With the audit of a single call switched on, the turn's `request` keeps each attachment as a
247+
reference in place of its bytes - `{type: attachment, path, pages, media_type, bytes, sha256}`, the size and the
248+
SHA-256 of the source file as the helper read it - followed by the turn's text. It is the one block of the audit
249+
that is not the provider's format: the exchange is reconstructed from the record and the files, by hash. The hash
250+
tells whether a file is still the one the model saw; it cannot bring back a file that was changed or removed. Where
251+
an exchange must be reconstructable, keep the files unchanged for as long as the audit's retention
252+
(`llm.transcript.audit.retention`): write-once names in the folder, or versioned objects once an object store is the
253+
source. The prompt, the system prompt and the model's reply are in the audit in full, so what the model wrote about a
254+
file is kept even when the file is not.
255+
256+
Not on `llm.stream`, where a reply carries no schema, and not on a loop turn: a transcript keeps every turn, so it
257+
would have to keep the files, or references whose files may change between turns.
258+
161259
## The loop contract
162260

163261
`llm.chat` also serves one model turn of an agent loop. The loop is `llm.run`, a function of the Java and Rust
@@ -332,6 +430,9 @@ of the box). The audit does not redact; a redaction policy is the installation's
332430
| `llm.effort` | unset | The default effort. |
333431
| `llm.log.batches` | `false` | Log each streamed batch's number, size and arrival time (never its text). |
334432
| `llm.provider` | `anthropic` | Only `anthropic`; any other value is a 400. |
433+
| `llm.attachments.root` | unset | The one folder whose files a request may name in `attachments` (see Attachments). Unset, every attachment is a 503. |
434+
| `llm.attachments.max.count` | `20` | Files per call. |
435+
| `llm.attachments.max.bytes` | `20971520` | Bytes of files per call (20 MB), counted from the files' sizes before they are read. |
335436
| `llm.transcript.store` | unset | `memory` or `redis`; a loop turn needs one (see The transcript store). |
336437
| `llm.transcript.ttl` | `7d` | A loop turn's `ttl` when the request carries none. |
337438
| `llm.transcript.retention` | `0` | A loop turn's `retention` when the request carries none. |
@@ -367,9 +468,10 @@ section above gives each model's cadence.
367468
A call logs its model, `stop_reason`, token usage, `request_id` and elapsed time. A prompt, a
368469
system prompt or a completion never reaches a log. Usage also rides the call's trace record as
369470
annotations (`llm_model`, `llm_stop_reason`, `llm_input_tokens`, `llm_output_tokens`,
370-
`llm_request_id`), so telemetry shows what a call cost. With an audit store, the `transcript.audit` executions a turn
371-
triggers show in the same trace as the function's own; their log lines carry the conversation reference, the turn
372-
numbers written and the failure's type, never the content.
471+
`llm_request_id`), so telemetry shows what a call cost. A call with attachments also logs and annotates their count
472+
and bytes (`llm_attachments`, `llm_attachment_bytes`), never a path or a file's content. With an audit store, the
473+
`transcript.audit` executions a turn triggers show in the same trace as the function's own; their log lines carry
474+
the conversation reference, the turn numbers written and the failure's type, never the content.
373475

374476
## Backends
375477

@@ -409,11 +511,13 @@ for Opus 4.6 and earlier rejects it with a 400.
409511

410512
## What it does not do
411513

412-
No tool ever runs here: the engines' agent loop dispatches each call to a route. No images or
413-
documents, no `system` turns inside `messages`, no sampling parameters, no prompt caching controls
414-
for a caller (a loop turn is cached by the helper), and no provider other than Claude. Those are
415-
deliberate: the helper is the bounded AI node of a graph and the model turn of an agent loop, not
416-
an agent runtime.
514+
No tool ever runs here: the engines' agent loop dispatches each call to a route. No file's bytes in
515+
a request (a single call names images and PDF pages by reference, see Attachments; other kinds of
516+
file, a loop turn and a stream take none), no `system` turns inside `messages`, no sampling
517+
parameters, no prompt caching controls for a caller (a loop turn is cached by the helper), and no
518+
provider other than Claude. Those are deliberate: the helper is the bounded AI node of a graph and
519+
the model turn of an agent loop, not an agent runtime. A bounded node may look at files it is given
520+
by reference; it still decides nothing about control flow.
417521

418522
## Tests
419523

@@ -436,9 +540,17 @@ either route, and when it leaves none), each run through the save, the check eve
436540
mailbox; the tests beside it pin that the reply does not wait for the save, that a failed write is repaired by the
437541
sweep, and that a loop turn is untouched by the switch.
438542

543+
`test/llm-attachments.test.mjs` pins the attachments with a third shared file,
544+
`test/vectors/llm-attachments-vectors.json` (48 cases: the shape of the field, where it does not belong, the folder a
545+
request stays inside, the files refused before any model call, the blocks the model gets, the reference the audit
546+
keeps), over the fixture files of `test/fixtures/attachments`, which both packs hold byte for byte. A cut PDF is
547+
checked page by page and never byte for byte, since two PDF libraries do not write the same bytes. The tests beside it
548+
pin an image's limit by backend, the helper without the PDF library, the deadline over the read, the worker thread,
549+
the log and the start with a folder that is not one.
550+
439551
```bash
440552
npm run build
441-
node --test test/llm-helper.test.mjs test/transcript-audit.test.mjs
553+
node --test test/llm-helper.test.mjs test/transcript-audit.test.mjs test/llm-attachments.test.mjs
442554
LLM_TRANSCRIPT_REDIS_URL=redis://127.0.0.1:6379/15 node --test --test-name-pattern='live redis' test/llm-helper.test.mjs
443555
LLM_TRANSCRIPT_AUDIT_TEST_URL='postgres://mercury:...@127.0.0.1:5432/mercury?sslmode=disable' \
444556
node --test --test-name-pattern='live postgres' test/transcript-audit.test.mjs

0 commit comments

Comments
 (0)