agent: share_file tool + containment-checked download endpoint

The agent can hand the user a same-origin download link for a file on its
filesystem (outbound complement to uploads). share_file(path, name?) returns a
Markdown link the GUI renders as a download chip.

Safety is containment, not trust: a shared path must canonicalize inside the
session personal folder or the shared uploads/ dir (.. and symlink escapes
refused), regular files only, 64 MiB cap. GET /api/sessions/{id}/download
re-validates independently (author != verifier), forces attachment +
application/octet-stream + nosniff, bare 400 out-of-bounds, 404 unknown session.

TOOL_USE seed migration appends the share_file manual without clobbering
user-edited rows. README documents the tool surface, endpoint and boundary;
Status refreshed to the verified 191 passed / 3 ignored.
This commit is contained in:
adm committed 2026-11-29 01:23:18 +01:00
1 parent 28568f0e89
commit 2f5b263d4e
11 files changed
+668 -15

No files matched your search

+26 -5
View File
@@ -112,6 +112,11 @@ offline, same-origin so no CORS). It is a thin client over a **session-aware API
- `GET/PUT /api/settings/defaults` — the global default settings template.
- `POST /api/uploads` — store an uploaded file (base64) in the shared upload dir;
returns metadata (kind, token count, path). Attach the returned id to a chat turn.
- `GET /api/sessions/{id}/download?path=…&name=…` — serve a file the agent shared
via `share_file` as an attachment (`application/octet-stream`,
`Content-Disposition: attachment`, `X-Content-Type-Options: nosniff`). The path is
re-validated server-side against the session's allowed roots (see **Agent mode**);
an out-of-bounds path is a 400, an unknown session a 404 — no bytes leak.
- `PATCH /api/sessions/{id}` — rename (only before the first interaction) and/or
replace the session's own settings instance.
@@ -202,9 +207,11 @@ copied into `data/` once on first run (the original is left in place).
## Agent mode
The GUI model can **act**, not just chat. Tools are **always on** — there is no
toggle, because the framework could not function without them. The agent has a single
universal tool, **`shell_exec`** (a shell is universal: file ops, programs, system
inspection, web via curl, git, …). A bounded server-side loop drives
toggle, because the framework could not function without them. The agent's tool
surface is small and deliberate: **`shell_exec`** (a shell is universal: file ops,
programs, system inspection, web via curl, git, …), **`notepad`** (agent-curated
persistent memory, see below) and **`share_file`** (hand the user a download link
for a file on its filesystem, see below). A bounded server-side loop drives
`model → shell_exec → result → model → … → final answer`, persisting the whole
trajectory and streaming progress events — `delta` (the model's thinking and final
answer, token by token), `tool_call` / `tool_result` (every command and its output
@@ -220,6 +227,19 @@ isolated machine; the agent then only ever sees that environment. Execution sits
behind a `ShellRunner` trait (useful for tests and alternate runners), with no
per-call confirmation gate and no separate "isolated" tool backend.
**Sharing a file (`share_file`).** When the agent produces a file the user should
be able to download, it calls `share_file(path, name?)`; the tool returns a
same-origin Markdown link (`[name](/api/sessions/{id}/download?path=…)`) the model
pastes into its reply, and the GUI renders it as a download chip. This is the
outbound complement to uploads. The safety boundary is **containment, not trust**:
a shared path must canonicalize inside one of two allowed roots — the session's
**personal folder** or the shared **`uploads/`** dir — so `..` traversal and symlink
escapes are refused, only regular files are served, and a **64 MiB** cap applies.
The download endpoint re-validates independently (author ≠ verifier): it never
trusts the tool's earlier check, forces `Content-Disposition: attachment` +
`application/octet-stream` + `nosniff` (no inline rendering of model-chosen bytes),
and returns a bare 400 on any out-of-bounds path without leaking existence or bytes.
## Architecture
```
@@ -298,9 +318,10 @@ later adaptation round.
## Status
Working and tested (162 tests, plus 3 real-file tests run on demand). Text, images,
Working and tested (191 tests, plus 3 real-file tests run on demand). Text, images,
the full client-side tool loop, the Anthropic endpoint, the web GUI (session
context, streaming, stats, **file/image upload**, assistant **Markdown/LaTeX
context, streaming, stats, **file/image upload**, **file download** (`share_file`
→ containment-checked attachment endpoint), assistant **Markdown/LaTeX
rendering** with native MathML) and **agent mode** (the model using `shell_exec`
on the host) have been validated live against the real Inkling engine. The
**context compositor** (named sections stored in SQLite — `SYSTEM_PROMPT` /