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

+15 -3
View File
@@ -202,7 +202,11 @@ pub struct NotepadEntry {
/// the DB rows are the source of truth afterwards).
const SYSTEM_PROMPT_SECTION: &str = "{identity}{speaker_note}\n{session_prompt}";
const TOOL_USE_SECTION_PRE_NOTEPAD: &str = "Strumenti: shell_exec(command, cwd?, timeout?) esegue un comando shell nell'ambiente host e restituisce stdout/stderr ed exit code; la cartella predefinita è la tua cartella personale. Usalo per ispezionare o modificare file e per verificare i risultati invece di inventarli. Fai riferimento alle dichiarazioni dei tool fornite per gli argomenti esatti.";
const TOOL_USE_SECTION: &str = "Strumenti: shell_exec(command, cwd?, timeout?) esegue un comando shell nell'ambiente host e restituisce stdout/stderr ed exit code; la cartella predefinita è la tua cartella personale. Usalo per ispezionare o modificare file e per verificare i risultati invece di inventarli. Fai riferimento alle dichiarazioni dei tool fornite per gli argomenti esatti. notepad(action, key?, text?) gestisce il tuo blocco appunti persistente, sempre visibile sopra il dialogo: action=set crea o aggiorna la entry etichettata `key` con `text` (upsert idempotente); action=remove cancella la entry `key`; action=clear svuota tutto. Fissa qui ciò che deve sopravvivere allo scorrere della finestra di contesto: goal, decisioni e razionale, stato/next-step, vincoli, pitfall, path e identificatori chiave. Non usarlo per log o documenti lunghi né per dati effimeri del singolo task. È bounded: quando si avvicina al limite consolida le entry vecchie; non c'è eviction automatica, decidi tu cosa tenere. Tieni le entry brevi.";
const TOOL_USE_SECTION_PRE_SHARE: &str = "Strumenti: shell_exec(command, cwd?, timeout?) esegue un comando shell nell'ambiente host e restituisce stdout/stderr ed exit code; la cartella predefinita è la tua cartella personale. Usalo per ispezionare o modificare file e per verificare i risultati invece di inventarli. Fai riferimento alle dichiarazioni dei tool fornite per gli argomenti esatti. notepad(action, key?, text?) gestisce il tuo blocco appunti persistente, sempre visibile sopra il dialogo: action=set crea o aggiorna la entry etichettata `key` con `text` (upsert idempotente); action=remove cancella la entry `key`; action=clear svuota tutto. Fissa qui ciò che deve sopravvivere allo scorrere della finestra di contesto: goal, decisioni e razionale, stato/next-step, vincoli, pitfall, path e identificatori chiave. Non usarlo per log o documenti lunghi né per dati effimeri del singolo task. È bounded: quando si avvicina al limite consolida le entry vecchie; non c'è eviction automatica, decidi tu cosa tenere. Tieni le entry brevi.";
/// Prose appended to the notepad-era TOOL_USE manual to document `share_file`.
/// Kept separate so the seed can refresh untouched older rows without duplicating
/// the long notepad text (and never clobber a user-edited section).
const SHARE_FILE_SUFFIX: &str = " share_file(path, name?) espone un file che hai su disco come link di scaricamento cliccabile nella chat web: restituisce un link Markdown già pronto da incollare testualmente nella risposta per l'utente. Usalo quando produci un file che l'utente deve scaricare (report, script, dati, immagini, archivi). Sono ammessi solo file regolari dentro la tua cartella personale o la cartella uploads condivisa, entro 64 MiB; `path` può essere assoluto o relativo alla tua cartella personale, `name` opzionalmente sostituisce il nome del file scaricato.";
/// The storage interface. Synchronous; call via `spawn_blocking` from async code.
pub trait Store: Send + Sync {
@@ -405,19 +409,27 @@ fn migrate(conn: &Connection) -> Result<()> {
/// Seed the static context sections once (idempotent); afterwards the stored
/// rows are authoritative and editable.
fn seed_context_sections(conn: &Connection) -> Result<()> {
// The current TOOL_USE manual = notepad-era prose + the share_file paragraph.
let tool_use = format!("{TOOL_USE_SECTION_PRE_SHARE}{SHARE_FILE_SUFFIX}");
conn.execute(
"INSERT OR IGNORE INTO context_sections (name, content, position) VALUES ('SYSTEM_PROMPT', ?1, 0)",
params![SYSTEM_PROMPT_SECTION],
)?;
conn.execute(
"INSERT OR IGNORE INTO context_sections (name, content, position) VALUES ('TOOL_USE', ?1, 1)",
params![TOOL_USE_SECTION],
params![tool_use],
)?;
// Refresh an untouched pre-notepad TOOL_USE row so the notepad manual reaches
// existing databases; a user-edited row (content differs) is left alone.
conn.execute(
"UPDATE context_sections SET content = ?1 WHERE name = 'TOOL_USE' AND content = ?2",
params![TOOL_USE_SECTION, TOOL_USE_SECTION_PRE_NOTEPAD],
params![tool_use, TOOL_USE_SECTION_PRE_NOTEPAD],
)?;
// Likewise refresh an untouched notepad-era row so the share_file manual
// reaches databases created before share_file existed.
conn.execute(
"UPDATE context_sections SET content = ?1 WHERE name = 'TOOL_USE' AND content = ?2",
params![tool_use, TOOL_USE_SECTION_PRE_SHARE],
)?;
Ok(())
}