feat(telegram): send local files as uploads, not fetchable URLs
The agent could produce a file -- a transcript, a log dump, a screenshot --
and had no way to hand it over. Asked to send one it offered a "download"
link; nothing arrived and it had no signal that anything had failed. The
defect was not that it lied: success and silent non-delivery were
indistinguishable to it.
Three places made a local file undeliverable, and each is fixed here:
- liveReply.Media skipped any attachment with an empty URL, so a local
file was dropped before it ever reached a sender.
- telegramMediaSender built every attachment as models.InputFileString,
which tells Telegram to FETCH the string as a URL. A host path is not
fetchable, so the send did nothing while reporting success. It now
chooses by what the attachment carries: URL -> fetch, Path -> upload
via models.InputFileUpload over an open file.
- Nothing let the agent ask for a send at all. New send_file tool
(internal/tools/sendfile) validates the file and returns a
MultimodalResult naming it; the turn's channel delivers it.
Meaning changes, called out because the names did not:
- MediaAttachment gains Path, the alternative to URL. Exactly one is
meaningful for an outbound send.
- tools.MultimodalResult.URLs used to mean remote-hosted media only; it
now means media this result wants delivered, which may be local. JSON
name unchanged so existing producers keep working.
- errNoURL is now errNoSource: having neither source, not having no URL,
is what makes an attachment undeliverable.
Failures are visible at every layer, which is the actual fix. send_file
errors on an absent, irregular, unreadable, oversized or confinement-
refused path, so the model sees it and can report it. The sender checks
Telegram's ceilings (10 MB photos, 50 MB otherwise) before the request and
reports the real size rather than leaving a 400 nobody reads. A delivery
that still fails now appends "could not send <name>" to the reply instead
of an empty link, which read as a delivered file.
Exposing a public URL for local files was explicitly rejected: it trades a
missing feature for a hosting and access-control problem.
send_file resolves paths through builtin.ResolveReadable -- the read tool's
own policy, exported rather than reimplemented -- so sending cannot reach
what reading cannot, whatever [chat] unrestricted_filesystem is set to.
This commit is contained in:
1 parent
547007253f
commit
e5e0316475
13 files changed
+839
-34
No files matched your search
@@ -4,6 +4,8 @@
|
||||
{"_type":"issue","id":"archie-core-1ysw.1","title":"Bind worktree RPC operations to the dispatched task","description":"The sandboxed agent can currently choose owner, repo, issue, base, and branch on worktree RPC requests. The daemon then performs host deletion and credentialed forge operations without correlating the request to the dispatched task or enforcing WorkDir containment.","acceptance_criteria":"Reject traversal and unconfigured or task-mismatched coordinates before any filesystem or forge side effect; derive or validate the permitted branch; regression tests fail on the old behavior and pass on the fix.","status":"closed","priority":0,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-22T11:01:43Z","created_by":"Sam Catlow","updated_at":"2026-08-24T06:59:58Z","started_at":"2026-08-22T11:02:03Z","closed_at":"2026-08-24T06:59:58Z","close_reason":"Fixed on main: worktreerpc now exposes only Push, authorized by an opaque per-dispatch grant issued from the dispatched *store.Task (worktreerpc.go:71-83). Sandbox input no longer selects host path, repo or branch; ValidCoordinates rejects traversal. Package tests pass.","external_ref":"https://github.com/samcharles93/archie-core/issues/589","metadata":null,"dependencies":[{"issue_id":"archie-core-1ysw.1","depends_on_id":"archie-core-1ysw","type":"parent-child","created_at":"2026-08-22T21:01:42Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-1ysw","title":"Repair internal/worktree correctness and trust-boundary defects","description":"Track the concrete worktree-package defects proven by the 2026-08-22 evidence-gated review. Child bugs cover RPC authority/path containment, repository-key isolation, retry/base/diff correctness, push lifecycle, terminal cleanup, cancellation, and branch naming. This epic groups implementation only; each child has its own observable acceptance criteria.","acceptance_criteria":"All child bugs are closed with red-green regression tests, scoped lint passes, and the repository quality gate is clean.","status":"closed","priority":0,"issue_type":"epic","owner":"sam@catlow.tech","created_at":"2026-08-22T11:01:18Z","created_by":"Sam Catlow","updated_at":"2026-08-24T08:16:00Z","closed_at":"2026-08-24T08:16:00Z","close_reason":"All 9 children complete. The P0/P1 trust-boundary and correctness defects (.1 RPC task binding, .2 colliding directory keys, .3 untracked cleanup, .4 fail-closed base resolution, .5 three-dot merge-base semantics) were already fixed on main and reconciled 2026-08-24. The remaining four (.6 push result, .7 no-change worktree leak, .8 commit cancellation, .9 branch naming contract) were implemented this session, merged as 474c31d and its two merge commits, task check green, with an independent adversarial review reporting zero findings.","external_ref":"https://github.com/samcharles93/archie-core/issues/590","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-t2db.5","title":"Security: an unauthenticated endpoint must not be able to run an agent","description":"P0 within this epic because the feature's own pitch is the threat model: a public intake surface whose output is an agent with tool access and a repo. Get this wrong and a webhook URL is remote code execution with extra steps.\n\nNon-negotiables to design:\n\n- Per-source authentication. A source registers with a secret (HMAC, as internal/channels/webhook already does per route) before its events can trigger anything. Capture-only for unknown senders is acceptable; triggering is not.\n- Human approval before a binding goes live. An operator authoring a binding is a deliberate act; an event arriving is not. Nothing should self-arm.\n- Rate and volume limits per source, so capture cannot be used to exhaust disk or drown the inspector.\n- Payload content is untrusted input flowing into an agent prompt. Treat prompt injection as expected, not exceptional -- the sender controls that text, and the agent has tools. Bound what a playbook-triggered workflow may do.\n- Captured payloads may contain secrets from the sending system. Decide retention, redaction and who can view them; internal/memory/scrubber may already have applicable prior art.\n\nArchie's own principle applies: environmental enforcement over prompt rules. 'The agent should ignore instructions in the payload' is not a control.","acceptance_criteria":"An event from an unauthenticated source can be captured but cannot trigger a workflow; a new binding requires explicit human approval before it arms; per-source rate limits exist; the blast radius of a playbook-triggered workflow is bounded by code rather than by prompt text; retention and visibility of captured payloads is decided and documented.","notes":"2026-08-22: security design decided and documented in docs/prds/webhook-intake-security.md, covering all five non-negotiables from this bead's description:\n1. Per-source auth reuses internal/channels/webhook's existing HMAC-SHA256 scheme; capture accepts unsigned events (visible, marked unauthenticated), only authenticated events can ever match a binding.\n2. Bindings gain a draft -\u003e pending_approval -\u003e armed state machine; any edit to an armed binding drops it back to pending_approval so approval can't be bypassed by editing. Modeled on telegram's existing dangerousAction/pendingApproval flow (dangerous.go/approval.go).\n3. Per-source token-bucket rate limiting (precedent: pairing/store.go's MaxPendingPerPlatform); limit-exceeded is an observable 429, not a silent drop.\n4. Blast radius bounded by the SAME code-level controls every task already gets -- gate, diff_cap_lines, sandboxed container, worktree isolation -- no new relaxation for playbook-triggered work, and payload text is ordinary untrusted prompt input, never a trusted instruction.\n5. 7-day configurable retention on captured payloads; best-effort redaction reusing gateway/stream.go's sensitiveParameterKey heuristic before persisting; visibility gated by the same dashboard auth as every other webui surface.\n\nThis bead's code-level acceptance criteria (capture-can't-trigger, rate limits exist, blast-radius bounded, approval gate) cannot be implemented yet: they constrain t2db.1 (capture), 7d5u.3 (intake family) and t2db.4 (binding), none of which have landed. This document is the contract those beads build against -- do not build any of them without satisfying it. Leaving this bead in_progress rather than closing it; it closes when t2db.1/7d5u.3/t2db.4 land and demonstrably satisfy points 1-5, not before.","status":"in_progress","priority":0,"issue_type":"feature","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-21T13:45:46Z","created_by":"Sam Catlow","updated_at":"2026-08-22T09:25:01Z","started_at":"2026-08-21T14:51:26Z","external_ref":"https://github.com/samcharles93/archie-core/issues/536","dependencies":[{"issue_id":"archie-core-t2db.5","depends_on_id":"archie-core-t2db","type":"parent-child","created_at":"2026-08-21T23:45:45Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":1,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-440c","title":"Dashboard mutations 403 behind a TLS-terminating reverse proxy: Origin check ignores X-Forwarded-Proto","description":"Every operator action from the dashboard -- Stop, Cancel, Retry, Approve, Reject, Abandon, Archive -- fails with HTTP 403 in the production deployment. Observed 2026-08-24: `POST https://archie.catlow.cloud/api/tasks/3/action` returned 403 in 7ms with a 30-byte body. This left the operator with no way to stop a running task from the web UI at all.\n\n## Root cause\n\n`authorizeTaskMutation` (internal/webui/api_tasks.go:197-221) derives the expected Origin scheme from the daemon's OWN connection:\n\n wantScheme := \"http\"\n if r.TLS != nil {\n wantScheme = \"https\"\n }\n ...\n if ... || !strings.EqualFold(u.Scheme, wantScheme) || !strings.EqualFold(u.Host, r.Host) {\n http.Error(w, \"cross-origin mutation refused\", http.StatusForbidden)\n\narchied runs behind a reverse proxy that terminates TLS (confirmed: archie.catlow.cloud redirects to auth.catlow.cloud for forward auth, and the request was HTTP/2 to the proxy). The daemon therefore receives a PLAIN HTTP connection, so `r.TLS == nil` and `wantScheme == \"http\"`, while the browser correctly sends `Origin: https://archie.catlow.cloud`. Scheme mismatch -\u003e 403 on every mutation.\n\n`X-Forwarded-Proto` is never consulted anywhere in the codebase (`grep -rn 'X-Forwarded' internal/webui/ internal/config/` returns nothing).\n\n## Evidence the body confirms which branch fired\n\nThere are exactly two 403 paths in this handler. Their response bodies differ in length:\n- \"missing CSRF header\\n\" = 20 bytes\n- \"cross-origin mutation refused\\n\" = 30 bytes\n\nThe observed response was 30 B, so the Origin comparison is what rejected it, NOT the CSRF header. The dashboard is sending X-Archie-CSRF correctly.\n\n## Why this is P1\n\nIt is a total loss of the operator control surface in the supported deployment shape. It also removes the documented remedy for other faults: archie-core-0dic leaves a stopped task's row reading `running`, and `Parked`'s retry action is the stated recovery for a review that fails closed (docs/prds/adversarial-self-review.md section 4) -- both assume dashboard actions work.\n\nNote the deployment model is explicitly plural (CLAUDE.md \"Deployment model\": several supported shapes, systemd is not required), so \"runs behind a proxy\" is a first-class configuration, not an edge case.\n\n## Steps to Reproduce\n\n1. Serve archied behind any TLS-terminating reverse proxy (nginx, Caddy, Traefik, or a forward-auth proxy such as Authelia), with the daemon listening on plain HTTP.\n2. Load the dashboard over https and press Stop on a running task.\n3. Observe `POST /api/tasks/\u003cid\u003e/action` -\u003e 403, body \"cross-origin mutation refused\".\n4. The same build works when reached directly over http, or over https terminated by the daemon itself, which is why this does not reproduce in local development.\n\n## Acceptance Criteria\n\nDashboard mutations succeed when archied is behind a TLS-terminating reverse proxy; the effective scheme is derived from X-Forwarded-Proto (and the effective host from X-Forwarded-Host where applicable) ONLY when the request arrives from a trusted proxy, configured explicitly -- an unconditional trust of these headers would let any client forge the Origin check and defeat the CSRF protection this function exists to provide; direct (non-proxied) deployments keep their current strict behaviour; tests cover proxied-https, direct-http, direct-https, and a spoofed X-Forwarded-Proto from an untrusted source; the config surface and its default are documented in config.example.toml and the deployment profiles.\n","notes":"CONFIRMED LIVE against production 2026-08-25 via Playwright, signed in as the operator.\n\nReproduced the dashboard's own Stop request from the loaded page:\n\n fetch('/api/tasks/3/action', {method:'POST',\n headers:{'X-Archie-CSRF':'1','Content-Type':'application/json'},\n body:'{\"action\":\"stop\"}'})\n\nResult: status 403, body exactly \"cross-origin mutation refused\\n\" (30 bytes), with location.origin = https://archie.catlow.cloud and location.protocol = https:.\n\nThis settles which of the two 403 branches fires. The request carried a correct X-Archie-CSRF header and same-origin credentials, so it passed the CSRF check and was rejected by the Origin comparison in authorizeTaskMutation. Combined with the deployment shape (archie.catlow.cloud 302s to auth.catlow.cloud for forward auth, so TLS terminates at the proxy and the daemon sees plain HTTP), the scheme comparison is the failing term: browser Origin scheme 'https' vs the daemon's computed wantScheme 'http' from r.TLS == nil.\n\nThe dashboard confirms the operator impact at the same time: the header reads '1 running' and task #3 is still listed as running (that stale row is archie-core-0dic, a separate defect). So in production the Line truncated
|
||||
{"_type":"issue","id":"archie-core-0dic","title":"Stopped tasks stay 'running' forever: terminal writes ride the cancelled context","description":"/stop reports success and genuinely does stop the container, but the task row is never updated, so the dashboard, /status and task_list all show the task as running indefinitely. Observed 2026-08-24: task #3 was stopped and was still displayed as running the following day.\n\n## Root cause\n\n`Daemon.runViaAgent` (internal/daemon/daemon.go:1010-1100) writes its terminal state with the SAME context that was just cancelled, and discards the result:\n\n reply, err := d.requestTaskRun(ctx, task.ID, data) // returns ctx.Err() on /stop\n if err != nil {\n _ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, \"taskrun request failed: \"+err.Error())\n\n`store.Transition` writes through `tx.ExecContext(ctx, ...)` (internal/store/store.go:358-365), so on a cancelled context the write fails immediately and the `_ =` throws the error away. The row stays `running` with nothing left to correct it.\n\nThere are SIX such writes in runViaAgent (grant unavailable, grant failed, encode failed, request failed, decode failed, run failed). Every one is unreachable in exactly the situation it exists for -- cancellation -- because cancellation is what invalidates the context it writes with.\n\n`cleanupTerminalTaskWorktree` (daemon.go:897) has the same defect: it re-reads via `d.Store.TaskByID(ctx, ...)` and returns early on error, so a cancelled task also skips worktree cleanup.\n\n## Why the container is NOT the problem\n\n`Pool.Release` already gets this right and is the precedent for the fix: it detaches deliberately with `context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)` and documents why -- \"the most important reason a task is on its way out is that it was cancelled... Teardown is cleanup, so it gets its own deadline rather than inheriting the caller's cancellation.\" So teardown works; only state recording is broken.\n\n## Contributing factor\n\n`StoreTaskController.StopRunning` (internal/gateway/tasks.go:227) deliberately writes no terminal state, documented as \"A stopped task unwinds through its own error paths and lands wherever that leaves it, usually parked\". That assumption is now false: the unwind path cannot record anything, so nothing lands. Note `CancelChatTask` (the /cancel path) DOES write state explicitly and is unaffected.\n\n## Steps to Reproduce\n\n1. Start an autonomous task so it reaches `running` with a live archie-agent container.\n2. Issue `/stop` from chat (or Stop from the dashboard).\n3. Observe the container is stopped and removed by Pool.Release.\n4. Observe the task row is still `status = running` in the store, in `task_list`, in `/status` and on the dashboard.\n5. Wait: nothing ever corrects it. Only a daemon restart re-queues it, via the crash-recovery path that re-queues tasks left `running`.\n\n## Acceptance Criteria\n\nTerminal state writes on the cancellation path use a context that survives cancellation (`context.WithoutCancel`, with its own timeout, mirroring Pool.Release); their errors are logged rather than discarded; a stopped task reaches a terminal state observable in the store, task_list and the dashboard without a daemon restart; cleanupTerminalTaskWorktree also runs for a cancelled task; a regression test cancels a task mid-run and asserts the row is no longer `running`, and fails against the current code.\n","notes":"Timeline correction: the task was observed still 'running' roughly ONE HOUR after /stop, not a day (an earlier note said a day; that was inferred wrongly from a date rollover). An hour is if anything stronger evidence, since no plausible refresh interval covers it.\n\nChronology correction: the agent's pre-/stop task_list reading 'still marked running' is NOT evidence of this bug -- nothing had stopped the task at that point, so it was reporting correctly. The genuine evidence is (a) the row still 'running' an hour after /stop, and (b) the code path itself, which predicts exactly that and was found by reading runViaAgent rather than from the transcript.\n\nScope note: this bug explains a stale ROW only. It does not explain a still-live container, because container.Pool.Release deliberately detaches from the cancelled context and does tear down. If a container is ever found alive after /stop, that is a SEPARATE defect, not this one.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-24T15:22:19Z","created_by":"Sam Catlow","updated_at":"2026-08-24T15:42:47Z","started_at":"2026-08-24T15:31:34Z","closed_at":"2026-08-24T15:42:47Z","close_reason":"Closed","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-ite6","title":"Flush daemon task-event subscription before accepting worker events","description":"cmd/archied.subscribeAgentEvents registers a core-NATS wildcard subscription but returns before flushing the SUB frame. A task worker can publish immediately and lose its first event; TestSubscribeAgentEventsDropsUnparseableMessages exposes the race under full-suite load.","acceptance_criteria":"subscribeAgentEvents flushes the NATS connection before returning; flush failure unsubscribes the just-created subscription; a regression test proves a publisher can deliver immediately after subscription setup.","status":"closed","priority":1,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-24T04:39:22Z","created_by":"Sam Catlow","updated_at":"2026-08-24T05:19:34Z","started_at":"2026-08-24T04:39:24Z","closed_at":"2026-08-24T05:19:34Z","close_reason":"Completed; reconciled with the already-closed GitHub issue.","external_ref":"https://github.com/samcharles93/archie-core/issues/616","metadata":null,"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-q15y.3","title":"Remove legacy inprocess subprocess and single-stage agent execution","description":"The configuration and worker still expose daemon inprocess, per-stage subprocess, and single-stage NATS execution paths. Remove those production choices and keep the agent-loop runner owned only by the full-task archie-agent worker.","acceptance_criteria":"agent.mode no longer selects inprocess/subprocess/nats production behavior; cmd/archied and internal/daemon cannot construct an agent loop runner; legacy SubprocessRunner and single-stage worker paths are deleted or proven test-only; architecture tests enforce worker-only ownership.","status":"closed","priority":1,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-24T03:03:38Z","created_by":"Sam Catlow","updated_at":"2026-08-24T06:58:30Z","started_at":"2026-08-24T03:49:36Z","closed_at":"2026-08-24T06:58:30Z","close_reason":"Implemented in 157e99b (refactor(agentexec): remove legacy execution paths); [agent] is decode-only. Verified: no SubprocessRunner/inprocess symbols in non-test code.","external_ref":"https://github.com/samcharles93/archie-core/issues/617","metadata":null,"dependencies":[{"issue_id":"archie-core-q15y.3","depends_on_id":"archie-core-q15y","type":"parent-child","created_at":"2026-08-24T03:03:38Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-q15y.3","depends_on_id":"archie-core-q15y.2","type":"blocks","created_at":"2026-08-24T03:03:52Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":1,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-q15y.2","title":"Use full-task NATS handoff for every autonomous workflow","description":"daemon.process currently runs workflow.Run locally unless a container pool exists, and NATS mode without containers uses per-stage requests. Make full-task taskrun request/reply the only production workflow path for both embedded and external brokers.","acceptance_criteria":"daemon.process never calls workflow.Run; every workflow task is sent once through the taskrun subject to a dedicated managed worker; task state/RPC/event behavior remains correct; regression tests fail against the old local branch.","notes":"Implemented full-task-only autonomous dispatch: archied parks before worktree access when the managed container pool or task transport is unavailable, acquires exactly one task-scoped worker, and sends exactly one full-task request. Removed daemon-local workflow execution state and persisted daemon-side park details atomically as Task.ParkReason. task check passes; scoped lint is clean; fresh adversarial re-review has zero findings.","status":"closed","priority":1,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-24T03:03:37Z","created_by":"Sam Catlow","updated_at":"2026-08-24T06:58:29Z","started_at":"2026-08-24T03:32:18Z","closed_at":"2026-08-24T06:58:29Z","close_reason":"Implemented in 7d69001 (fix(daemon): require full-task worker handoff); daemon.process parks instead of running locally. Verified: no workflow.Run in internal/daemon non-test code, enforced by daemon/architecture_test.go:75.","external_ref":"https://github.com/samcharles93/archie-core/issues/618","metadata":null,"dependencies":[{"issue_id":"archie-core-q15y.2","depends_on_id":"archie-core-q15y","type":"parent-child","created_at":"2026-08-24T03:03:37Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-q15y.2","depends_on_id":"archie-core-q15y.1","type":"blocks","created_at":"2026-08-24T03:03:51Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":2,"comment_count":0}
|
||||
@@ -64,6 +66,8 @@
|
||||
{"_type":"issue","id":"archie-core-g7tm","title":"telegram: finalize the reply through a live context, matching the error branch","description":"The success path calls live.finalize(turnCtx, reply) with the raw cancellable turn context (telegram.go:582), while the error branch two lines above deliberately uses context.WithoutCancel(turnCtx). If /stop lands during finalize, the rich edit, the plain-text retry, and the split-send fallback all abort with context.Canceled (go-telegram/bot builds requests with NewRequestWithContext), so the authoritative reply is never delivered and the live message keeps the ▌ cursor forever. The reply is persisted to the ledger, so the loss is delivery-level — but the user sees a frozen half-answer.\n\nFailure: a long tool-using turn is /stop-ped as it completes; turnCtx is cancelled mid-finalize; every send fails on the dead context; the chat shows the last live frame with a blinking cursor and no final answer.\n\nFound by code-review of PR #476 (feat/467, merged 98e8dda).","design":"live.finalize(context.WithoutCancel(turnCtx), reply) — the one-liner the error branch already uses. Consider making finalize take a live context internally rather than trusting each caller to remember.","acceptance_criteria":"A /stop landing mid-finalize still delivers the authoritative reply and drops the cursor; a test covers the cancellable-context finalize path.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-14T14:43:08Z","created_by":"Sam Catlow","updated_at":"2026-08-14T15:07:36Z","started_at":"2026-08-14T14:53:28Z","closed_at":"2026-08-14T15:03:28Z","close_reason":"Fixed in internal/channels/telegram/live.go and telegram.go: finalize/abandon strip context cancellation before sending (mirroring each other), ToolCall escapes Markdown metacharacters via bot.EscapeMarkdown, splitLongMessage splits on rune boundaries. All new/changed tests verified red-then-green; task check clean; adversarial review pass found no blocking issues.","external_ref":"https://github.com/samcharles93/archie-core/issues/484","labels":["code-review","streaming","telegram"],"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-tb3a","title":"Fix streaming review blockers for inline tool calls","description":"Resolve the four evidence-gated findings in merged inline tool-call streaming: preserve the authoritative Telegram reply when final edits fail; keep Telegram network I/O off the model FullStream consumer and cancellable; correlate tool inputs with results and render a bounded redacted parameter summary; snapshot show_tool_calls per live reply to avoid reload races. Add regression tests for every path and run task check plus lint/race validation.","acceptance_criteria":"Regression tests fail on the merged implementation and pass after the fix. Final edit failure falls back to normal split delivery. Delta and ToolCall callbacks do not wait on Telegram HTTP and stop cancels in-flight rendering. Completed tool events include bounded redacted parameters correlated by tool-call ID. Reload cannot race an in-flight reply's show_tool_calls setting. task check passes, golangci-lint reports no new findings, and focused race tests pass.","notes":"Implemented in worktree /tmp/archie-core-tb3a on branch fix/archie-core-tb3a. Added cancellable async Telegram rendering, final edit failure fallback through normal split delivery, call-ID-correlated bounded conservative parameter summaries, atomic per-reply show_tool_calls snapshots, SSE/UI parameter rendering, rebuilt ui/dist, and regression tests. Validation: GOFLAGS=-buildvcs=false task check passed; golangci-lint --new-from-rev=HEAD ./... reported 0 issues; focused race suite passed; fresh adversarial reviewer reported PASS with zero findings. Plain task check is blocked in linked worktrees by Go VCS stamping exit 128, hence buildvcs override.\n2026-08-24 reconciliation: the close reason's 'worktree left uncommitted' is stale. The work did land on main via ba33527 (Merge pull request #477 from samcharles93/fix/archie-core-tb3a); verified on main: gateway/stream.go sensitiveParameterKey+redactToolParameters, telegram/live.go per-reply showToolCalls snapshot, webui/api_chat.go gating. The /tmp/archie-core-tb3a worktree and its branch are gone, as expected.\n","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-13T18:05:35Z","created_by":"Sam Catlow","updated_at":"2026-08-24T07:29:09Z","started_at":"2026-08-13T18:05:39Z","closed_at":"2026-08-13T18:35:26Z","close_reason":"Completed and independently reviewed; worktree left uncommitted per conservative repository policy.","external_ref":"https://github.com/samcharles93/archie-core/issues/485","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-dzx","title":"Remove duplicate archie-agent safe.directory setup","description":"## Problem\\ncmd/archie-agent/main.go configures git safe.directory inline and then calls markWorktreeSafe, so startup writes the same global git configuration twice.\\n\\n## Scope\\nKeep markWorktreeSafe as the single tested production path and remove the older inline exec.Command block/import. Add regression coverage proving startup wiring has exactly one authoritative safe-directory call.\\n\\n## Acceptance\\n- archie-agent startup invokes markWorktreeSafe exactly once\\n- no second inline safe.directory command remains in main.go\\n- cmd/archie-agent tests and repository quality gate pass","acceptance_criteria":"Single tested safe.directory setup path remains; duplicate inline command is removed; tests and task check pass","notes":"Red proof: TestMainConfiguresWorktreeSafetyOnce failed with 2 setup paths before implementation. Green proof: go test/vet/build ./cmd/archie-agent/... passed; golangci-lint --new-from-rev=HEAD reported 0 issues. Fresh adversarial review found no blockers. Full task check was not run because the working tree contains unrelated in-progress changes and task check rewrites files; scoped package gate used per repository protocol.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-13T16:10:09Z","created_by":"Sam Catlow","updated_at":"2026-08-13T16:13:48Z","started_at":"2026-08-13T16:10:24Z","closed_at":"2026-08-13T16:13:48Z","close_reason":"Removed duplicate inline safe.directory setup; markWorktreeSafe is now the single tested production path","external_ref":"https://github.com/samcharles93/archie-core/issues/470","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-8s7v","title":"[EPIC] Intercom: let the chat agent talk to running agents, the curator, and peers","description":"Today the chat agent can only observe autonomous work -- list tasks, read logs -- and act on it as an outsider through coarse verbs (spawn, stop). It cannot TALK to a running agent. An operator who can see a task going wrong has no way to say \"stop guessing, the config lives at X\" without killing the task and starting over, losing everything the agent had already worked out. Archie should behave like a living system whose parts can address each other, not a set of isolated processes with a dashboard bolted on.\n\n## Prior art: pi-intercom\n\nPi ships `pi-intercom`, which solves this shape for Pi sessions on one machine and is worth reading before designing (/home/sam/.pi/agent/npm/node_modules/pi-intercom; skills/pi-intercom/SKILL.md is the usage guide). Its model:\n\n- Sessions take names (`/name planner`, `/name worker-backend`) and address each other by name.\n- `send` -- fire-and-forget notification. Task assignment, status, notice.\n- `ask` -- BLOCKS until the target replies, with a timeout. The blocking form is the interesting one: it lets a worker stop and get a decision instead of guessing.\n- `reply` -- answers an inbound ask, threaded automatically.\n- `pending` -- lists unresolved inbound asks, i.e. who is waiting on you.\n- `list` -- connected sessions and their state (idle / thinking / tool), for discovering targets.\n- Its use cases map directly onto archie: planner-to-worker delegation, context and findings handoff, blocking clarification, subagent escalation (`need_decision`, `interview_request`), cross-project coordination.\n\n## What is genuinely different here\n\nDo NOT copy pi-intercom's transport. It ships its own local IPC broker (broker/broker.ts, a socket server with framing, spawn and runtime-claim logic) because Pi has no message bus. **Archie already has one**: NATS, with per-task subjects already defined in internal/agentexec/subjects.go (archie.agent.\u003cid\u003e.system and .events) and a request/reply pattern already used by taskrun, forgerpc, storerpc and worktreerpc. Building a second broker alongside it would be the \"two parallel abstractions\" mistake this repo already made once with gateway.BasePlatformAdapter.\n\nSo the real design questions are:\n1. Does agent-directed messaging ride the existing per-task subjects, or does it need its own contract and subject family? Note CLAUDE.md's recorded NATS traps -- work-queue retention forbids overlapping consumers, and reply inboxes are core-NATS-only and are not stream subjects.\n2. What is addressable? Running tasks have ids. The curator, channels and the daemon itself do not have a uniform identity for this purpose. An address space has to be decided before verbs are.\n3. How does a BLOCKING ask work against an agent that is mid-model-call? A worker is not sitting in a receive loop; it is inside an agentloop turn. Delivering a message it will actually notice, without corrupting the turn, is the hard part -- and is where this stops being a transport problem.\n4. Authorization: an operator messaging their own task is fine. Cross-identity messaging is not, and multi-identity deployments are supported (deployments/multi-forge-github-gitea.toml). Whatever the gateway's task tools enforce for identity scoping, this must enforce too.\n5. Does an inbound operator message become part of the agent's context permanently, or a one-shot injection? This decides whether it can be used to steer a task or only to interrupt it.\n\n## Relationship to existing work\n\n- Overlaps archie-core-absb (agent cannot send local files) only in spirit: both are about the agent being able to communicate outward.\n- The curator (internal/domain/curator) is a natural second participant after running tasks.\n- Any design here should be checked against docs/architecture/organisation.md: the domain declares the contract it needs; the transport implements it.\n\n## Success Criteria\n\nAn operator can address a running agent from chat and have it receive the message without the task being killed and restarted; a blocking ask returns the agent's answer or times out cleanly; participants are discoverable with their current state; messaging is identity-scoped so one identity cannot address another's work; the mechanism reuses the existing NATS transport rather than introducing a second broker; a design decision is recorded in docs/prds/ before implementation, per CLAUDE.md's scope discipline.\n","status":"open","priority":2,"issue_type":"epic","owner":"sam@catlow.tech","created_at":"2026-08-24T15:35:57Z","created_by":"Sam Catlow","updated_at":"2026-08-24T15:35:57Z","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-absb","title":"Agent cannot send local files: Telegram delivery is URL-fetch only","description":"The agent can produce a file on the host -- a log dump, a screenshot, a written archive, a diff -- and has no way to deliver it to the operator. Asked to send a saved transcript, it replied with a \"Download the archive\" link twice; nothing arrived either time, and it had no signal that anything had failed.\n\n## Why video works and files do not\n\n`telegramMediaSender.dispatch` (internal/channels/telegram/media.go:97) builds every attachment as:\n\n file := \u0026models.InputFileString{Data: att.URL}\n\n`InputFileString` tells Telegram to FETCH the given URL itself. That works for the existing media path because those attachments are remote-hosted -- MiniMax returns a public URL for a generated video, which is what `MultimodalResult.URLs` was added for. It cannot work for anything produced locally: a daemon-host path like /home/sam/archive.md is not a URL Telegram can fetch, so the send silently does nothing useful.\n\nThe `case \"document\"` branch (media.go:124-127) already calls `bot.SendDocument`, so the routing exists. Only the file SOURCE is wrong.\n\n## What is needed\n\nA way for the agent to send a LOCAL file as an attachment -- documents, logs, screenshots, images, any media -- not a link. The go-telegram/bot SDK supports upload via a reader (`models.InputFileUpload` with an io.Reader) as an alternative to `InputFileString`; the sender needs to choose between fetch-by-URL and upload-by-reader based on whether the attachment is remote or local.\n\nExplicitly NOT the fix: exposing an external/public URL for local files. That trades a missing feature for a hosting and access-control problem.\n\n## Constraints to respect\n\n- Telegram enforces upload size limits (50 MB for documents via the Bot API at time of writing); exceeding them must produce a clear error to the operator, not a silent drop.\n- The file tools are workspace-confined unless `[chat] unrestricted_filesystem = true`. Sending must not become a way around whatever confinement is configured -- resolve and check the path under the same policy the read tool applies.\n- A failed send must surface as a failure the agent can see and report. The current defect is not that the agent lied; it is that success and silent non-delivery are indistinguishable to it.\n\n## Acceptance Criteria\n\nThe agent can send a local file through Telegram and the operator receives it as an attachment; local and remote attachments both work, choosing upload-by-reader or fetch-by-URL as appropriate; a send that fails (too large, unreadable, outside the configured confinement) returns an error the agent surfaces rather than reporting success; existing remote-URL media delivery is unchanged and still covered by tests.\n","status":"open","priority":2,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-08-24T15:32:07Z","created_by":"Sam Catlow","updated_at":"2026-08-24T15:32:07Z","dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-1787583419901-1-85796c0b","title":"Allow parked agent jobs to be closed or archived","description":"## Problem\nParked agent jobs remain visible indefinitely, but the task-management interface currently exposes no action to close or archive them. This leaves stale jobs in the active job history and requires manual cleanup outside the normal workflow.\n\n## Observed behavior\nThe following old jobs are parked and cannot be closed through the available task-management actions:\n\n- Job #1 — `daemon: label dispatch trigger with empty label matches every open issue`\n- Job #2 — `archied: [[repos]] entries can't span more than one forge`\n\nThe interface can list and inspect parked jobs, but provides no close/archive operation.\n\n## Expected behavior\nProvide an explicit operation for a parked job, such as:\n\n- `close` when the job is definitively finished or no longer actionable;\n- `archive` when it should be removed from the active job list while retaining history;\n- optionally `reopen` if a parked job needs to be resumed.\n\nThe operation should persist the state, be reflected in task listings, and retain the job logs and audit history.\n\n## Acceptance criteria\n- A user can close or archive a parked job from the supported task-management interface/API.\n- The operation is idempotent and reports a clear error for invalid state transitions.\n- Closed/archived jobs no longer appear among active or parked jobs by default, while remaining queryable.\n- Existing logs, attempts, and failure details remain accessible.\n- Tests cover closing, archiving, reopening (if supported), and repeated operations.\n","status":"open","priority":2,"issue_type":"bug","assignee":"samcharles93","created_at":"2026-08-24T13:44:17Z","updated_at":"2026-08-24T13:48:20Z","external_ref":"https://github.com/samcharles93/archie-core/issues/624","source_system":"github:https://github.com/samcharles93/archie-core/issues/624:624","metadata":null,"labels":["agent","bug","ux"],"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-5bsf","title":"Rollback the managed agent image when a runtime update fails health checks","description":"The runtime update path rebuilds the Docker image, but scripts/archie-update-watchdog only restores the unused host archie-agent binary. A failed health check can report a rollback while the task-container image remains on the failed runtime. Design and implement image-reference preservation/restoration for the configured managed worker image, with tests for runtime-only and combined updates.","acceptance_criteria":"A failed runtime-only update restores the prior worker image before restarting archied; a combined update restores both daemon and worker image atomically; update tests prove the configured managed image, not a host worker binary, is the rollback target.","status":"closed","priority":2,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-08-24T04:37:24Z","created_by":"Sam Catlow","updated_at":"2026-08-24T05:19:34Z","closed_at":"2026-08-24T05:19:34Z","close_reason":"Completed; reconciled with the already-closed GitHub issue.","external_ref":"https://github.com/samcharles93/archie-core/issues/621","metadata":null,"dependency_count":0,"dependent_count":0,"comment_count":0}
|
||||
{"_type":"issue","id":"archie-core-q15y.4","title":"Make native archied embedded NATS and managed containers the default deployment","description":"Supported examples and install.sh currently generate agent.mode=inprocess and treat external Compose NATS as the container path. Migrate the default operator experience to native archied plus embedded NATS plus managed task containers, while keeping external NATS as the advanced broker option.","acceptance_criteria":"Fresh install produces the supported NATS-only container topology; systemd runs native archied; interactive host chat authority is unchanged; no Compose NATS service is required for embedded mode; external NATS profile remains documented and load-tested.","status":"closed","priority":2,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-08-24T03:03:38Z","created_by":"Sam Catlow","updated_at":"2026-08-24T06:58:42Z","started_at":"2026-08-24T04:21:30Z","closed_at":"2026-08-24T06:58:42Z","close_reason":"Implemented in c978f10 (feat(deployment): default to managed NATS workers). Verified: no agent.mode/inprocess remains in install.sh, deployments/*.toml or config.example.toml; deployment_contract_test.go added to enforce it.","external_ref":"https://github.com/samcharles93/archie-core/issues/622","metadata":null,"dependencies":[{"issue_id":"archie-core-q15y.4","depends_on_id":"archie-core-q15y","type":"parent-child","created_at":"2026-08-24T03:03:38Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-q15y.4","depends_on_id":"archie-core-q15y.1","type":"blocks","created_at":"2026-08-24T03:03:52Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-q15y.4","depends_on_id":"archie-core-q15y.2","type":"blocks","created_at":"2026-08-24T03:03:53Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-q15y.4","depends_on_id":"archie-core-q15y.3","type":"blocks","created_at":"2026-08-24T03:03:53Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":3,"dependent_count":0,"comment_count":0}
|
||||
|
||||
@@ -54,6 +54,7 @@ import (
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
|
||||
memorytoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/memory"
|
||||
"github.com/samcharles93/archie-core/internal/tools/sendfile"
|
||||
"github.com/samcharles93/archie-core/internal/tools/webfetch"
|
||||
"github.com/samcharles93/archie-core/internal/webhookguard"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
@@ -889,6 +890,20 @@ func (b *boot) registerStandaloneTools() {
|
||||
log.Info("web fetch disabled")
|
||||
}
|
||||
|
||||
// send_file. Rooted at the same workspace as the file tools and gated
|
||||
// by the same confinement, because it hands a host file to an outbound
|
||||
// message: a send that could reach paths the read tool refuses would
|
||||
// be a way around whatever confinement is configured.
|
||||
if entry := sendfile.Tool(cfg.Chat.Workspace); entry != nil {
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("send_file registration failed", "err", err)
|
||||
} else {
|
||||
log.Info("file sending enabled", "workspace", cfg.Chat.Workspace)
|
||||
}
|
||||
} else {
|
||||
log.Info("file sending disabled (chat.workspace is unset)")
|
||||
}
|
||||
|
||||
b.registerMinimaxTool(cfg, log)
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,9 @@ func (s *recordingStream) ToolCall(event gateway.ToolCallEvent) {
|
||||
}
|
||||
|
||||
func (s *recordingStream) Media(event gateway.MediaEvent) {
|
||||
s.events = append(s.events, "media:"+event.ToolName+":"+event.Attachment.Type)
|
||||
// Path is recorded alongside the type: a local attachment that lost
|
||||
// its path on the way through would still look like a media event.
|
||||
s.events = append(s.events, "media:"+event.ToolName+":"+event.Attachment.Type+":"+event.Attachment.Path)
|
||||
}
|
||||
|
||||
func streamOf(parts ...core.StreamPart) <-chan core.StreamPart {
|
||||
@@ -106,9 +108,41 @@ func TestDrainChatStream(t *testing.T) {
|
||||
},
|
||||
wantEvents: []string{
|
||||
`tool:generate_video::{"is_multimodal":true,"urls":[{"type":"video","url":"https://x/v"}]}`,
|
||||
"media:generate_video:video",
|
||||
"media:generate_video:video:",
|
||||
},
|
||||
},
|
||||
{
|
||||
// A local file has no URL and must not acquire one: the
|
||||
// channel uploads it. Losing Path here is exactly the
|
||||
// silent non-delivery send_file exists to end.
|
||||
name: "a tool result carrying a local path reports Media with that path",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "send_file"}},
|
||||
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "1",
|
||||
ToolName: "send_file",
|
||||
Output: `{"is_multimodal":true,"urls":[{"type":"document","path":"/tmp/a.md"}]}`,
|
||||
}},
|
||||
},
|
||||
wantEvents: []string{
|
||||
"tool:send_file::" + `{"is_multimodal":true,"urls":[{"type":"document","path":"/tmp/a.md"}]}`,
|
||||
"media:send_file:document:/tmp/a.md",
|
||||
},
|
||||
},
|
||||
{
|
||||
// A ref naming neither a URL nor a path is not deliverable,
|
||||
// and an empty attachment would render as a delivered file.
|
||||
name: "a media ref with no source reports no Media event",
|
||||
parts: []core.StreamPart{
|
||||
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "odd_tool"}},
|
||||
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
||||
ToolCallID: "1",
|
||||
ToolName: "odd_tool",
|
||||
Output: `{"is_multimodal":true,"urls":[{"type":"document"}]}`,
|
||||
}},
|
||||
},
|
||||
wantEvents: []string{"tool:odd_tool::" + `{"is_multimodal":true,"urls":[{"type":"document"}]}`},
|
||||
},
|
||||
{
|
||||
// An ordinary JSON tool result that happens to be a JSON
|
||||
// object but isn't a MultimodalResult (no is_multimodal, or
|
||||
|
||||
@@ -387,9 +387,21 @@ func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream) stri
|
||||
Err: part.ToolResult.Error,
|
||||
})
|
||||
for _, ref := range multimodalMediaRefs(part.ToolResult.Output) {
|
||||
// Path and URL are alternatives, not a pair: the channel
|
||||
// uploads one and fetches the other. A ref carrying
|
||||
// neither names nothing deliverable, so it is dropped
|
||||
// rather than sent as an empty attachment.
|
||||
if ref.URL == "" && ref.Path == "" {
|
||||
continue
|
||||
}
|
||||
turn.Media(gateway.MediaEvent{
|
||||
ToolName: name,
|
||||
Attachment: gateway.MediaAttachment{Type: ref.Type, URL: ref.URL},
|
||||
ToolName: name,
|
||||
Attachment: gateway.MediaAttachment{
|
||||
Type: ref.Type,
|
||||
URL: ref.URL,
|
||||
Path: ref.Path,
|
||||
FileName: ref.FileName,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -370,12 +370,16 @@ const mediaSendTimeout = 30 * time.Second
|
||||
// upload, and TurnStream implementations must not block generation on one.
|
||||
// It satisfies gateway.TurnStream.
|
||||
//
|
||||
// A missing URL is silently skipped rather than surfaced: an attachment
|
||||
// telegramMediaSender.SendMedia would reject as invalid_message has nothing
|
||||
// worth falling back to either, and Media has no error return to report it
|
||||
// through.
|
||||
// An attachment carrying neither a URL to fetch nor a local Path to
|
||||
// upload is silently skipped: SendMedia would reject it as
|
||||
// invalid_message, there is nothing to fall back to, and Media has no
|
||||
// error return to report it through.
|
||||
//
|
||||
// This used to skip on a missing URL alone, which dropped every local
|
||||
// file before it reached the sender -- the guard, not just the sender,
|
||||
// was part of making a local path undeliverable.
|
||||
func (l *liveReply) Media(event gateway.MediaEvent) {
|
||||
if event.Attachment.URL == "" {
|
||||
if event.Attachment.URL == "" && event.Attachment.Path == "" {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -413,7 +417,18 @@ func (l *liveReply) Media(event gateway.MediaEvent) {
|
||||
// MediaEvent that could not be (or was not attempted to be) delivered
|
||||
// inline.
|
||||
func (l *liveReply) appendFallbackLine(att gateway.MediaAttachment) {
|
||||
// A local attachment has no URL to fall back to, and printing an empty
|
||||
// one reads as a delivered file. Say plainly that it was not sent:
|
||||
// the whole defect this path exists around is that non-delivery and
|
||||
// success looked the same to everyone downstream, the agent included.
|
||||
line := fallbackMediaLinePrefix + bot.EscapeMarkdown(att.Type) + ": " + att.URL
|
||||
if att.URL == "" {
|
||||
name := att.FileName
|
||||
if name == "" {
|
||||
name = att.Path
|
||||
}
|
||||
line = fallbackMediaLinePrefix + "could not send " + bot.EscapeMarkdown(name)
|
||||
}
|
||||
|
||||
l.mu.Lock()
|
||||
l.toolLines = append(l.toolLines, line)
|
||||
|
||||
@@ -3,6 +3,7 @@ package telegram
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -1111,3 +1112,45 @@ func TestLiveReplyMediaSkipsDeliveryWhenCapabilityReportsUnsupported(t *testing.
|
||||
t.Fatalf("fallback lines = %q, want it to contain the asset URL", lines)
|
||||
}
|
||||
}
|
||||
|
||||
// failingSender reports every send as failed, so the fallback path can be
|
||||
// exercised without a network.
|
||||
type failingSender struct{}
|
||||
|
||||
func (failingSender) SendMedia(context.Context, gateway.MessageEvent) (gateway.SendResult, error) {
|
||||
return gateway.SendResult{Success: false}, errors.New("upload refused")
|
||||
}
|
||||
|
||||
func (failingSender) Capabilities() gateway.AdapterCapabilities {
|
||||
return gateway.AdapterCapabilities{Media: true}
|
||||
}
|
||||
|
||||
// A local attachment has no URL, so the URL fallback has nothing to offer.
|
||||
// It must say the file was not sent rather than render an empty link,
|
||||
// which reads as a delivered file -- success and silent non-delivery
|
||||
// looking alike is the whole defect here.
|
||||
func TestLiveReplyMediaFallbackReportsUndeliveredLocalFile(t *testing.T) {
|
||||
live, _ := newTestLiveReply(t, false)
|
||||
live.newMediaSender = func(*bot.Bot, int64, int) gateway.MediaSender { return failingSender{} }
|
||||
|
||||
live.Media(gateway.MediaEvent{
|
||||
ToolName: "send_file",
|
||||
Attachment: gateway.MediaAttachment{
|
||||
Type: "document",
|
||||
Path: "/var/log/archie/session.log",
|
||||
FileName: "session.log",
|
||||
},
|
||||
})
|
||||
live.waitMedia()
|
||||
|
||||
live.mu.Lock()
|
||||
lines := strings.Join(live.toolLines, "\n")
|
||||
live.mu.Unlock()
|
||||
|
||||
if !strings.Contains(lines, "could not send") {
|
||||
t.Errorf("fallback lines = %q, want them to report the failure", lines)
|
||||
}
|
||||
if !strings.Contains(lines, bot.EscapeMarkdown("session.log")) {
|
||||
t.Errorf("fallback lines = %q, want them to name the file", lines)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
@@ -22,10 +24,15 @@ import (
|
||||
// the new one. That is the same lifetime rule the per-launch turns
|
||||
// registry documents.
|
||||
//
|
||||
// Delivery is URL-only. A MediaAttachment.FileID identifies a file on the
|
||||
// platform it was uploaded from and is meaningless as an outbound handle
|
||||
// here, so only URL is accepted; uploading local bytes is a separate
|
||||
// concern from routing a generated asset to a chat.
|
||||
// An attachment is delivered one of two ways, chosen by what it carries:
|
||||
// a URL is handed to Telegram to FETCH, while a Path is UPLOADED from this
|
||||
// host. Delivery used to be URL-only, which meant a locally produced file
|
||||
// was passed off as a URL Telegram could not fetch and the send did
|
||||
// nothing useful while reporting success.
|
||||
//
|
||||
// A MediaAttachment.FileID is still not accepted: it identifies a file on
|
||||
// the platform it was uploaded from and is meaningless as an outbound
|
||||
// handle here.
|
||||
type telegramMediaSender struct {
|
||||
bot *bot.Bot
|
||||
chatID int64
|
||||
@@ -50,11 +57,23 @@ func (s *telegramMediaSender) Capabilities() gateway.AdapterCapabilities {
|
||||
return gateway.AdapterCapabilities{Media: true}
|
||||
}
|
||||
|
||||
// errNoMedia and errNoURL are the two ways an event can be undeliverable
|
||||
// before any request is made.
|
||||
// errNoMedia and errNoSource are the two ways an event can be
|
||||
// undeliverable before any request is made.
|
||||
var (
|
||||
errNoMedia = errors.New("message event carries no media attachment")
|
||||
errNoURL = errors.New("media attachment has no URL")
|
||||
// errNoSource replaces the former errNoURL: an attachment is now
|
||||
// deliverable with either a URL or a local Path, so having neither --
|
||||
// not having no URL -- is what makes it undeliverable.
|
||||
errNoSource = errors.New("media attachment has neither a URL nor a local path")
|
||||
)
|
||||
|
||||
// Bot API upload ceilings, in bytes. Photos are capped far lower than
|
||||
// everything else, and exceeding either is a 400 from Telegram with a
|
||||
// message the operator never sees; checking here turns that into a
|
||||
// reported failure with the actual size in it.
|
||||
const (
|
||||
maxPhotoUploadBytes int64 = 10 * 1024 * 1024
|
||||
maxFileUploadBytes int64 = 50 * 1024 * 1024
|
||||
)
|
||||
|
||||
// SendMedia delivers the event's first attachment, captioned with the
|
||||
@@ -66,11 +85,17 @@ func (s *telegramMediaSender) SendMedia(ctx context.Context, event gateway.Messa
|
||||
return invalidMedia(errNoMedia)
|
||||
}
|
||||
att := event.Media[0]
|
||||
if att.URL == "" {
|
||||
return invalidMedia(fmt.Errorf("%w (type %q)", errNoURL, att.Type))
|
||||
if att.URL == "" && att.Path == "" {
|
||||
return invalidMedia(fmt.Errorf("%w (type %q)", errNoSource, att.Type))
|
||||
}
|
||||
|
||||
msg, err := s.dispatch(ctx, att, event.Text)
|
||||
file, closeFile, err := s.source(att)
|
||||
if err != nil {
|
||||
return invalidMedia(err)
|
||||
}
|
||||
defer closeFile()
|
||||
|
||||
msg, err := s.dispatch(ctx, att, file, event.Text)
|
||||
if err != nil {
|
||||
if errors.Is(err, errUnsupportedMediaType) {
|
||||
return invalidMedia(err)
|
||||
@@ -87,15 +112,70 @@ func (s *telegramMediaSender) SendMedia(ctx context.Context, event gateway.Messa
|
||||
return gateway.SendResult{Success: true, MessageID: id}, nil
|
||||
}
|
||||
|
||||
var errUnsupportedMediaType = errors.New("unsupported media type")
|
||||
var (
|
||||
errUnsupportedMediaType = errors.New("unsupported media type")
|
||||
errNotRegularFile = errors.New("not a regular file")
|
||||
errTooLarge = errors.New("file exceeds the Telegram upload limit")
|
||||
)
|
||||
|
||||
// source builds the Bot API file value for att, and a function to release
|
||||
// whatever it holds open.
|
||||
//
|
||||
// A URL becomes an InputFileString, which tells Telegram to fetch it. A
|
||||
// local path becomes an InputFileUpload carrying an open *os.File, which
|
||||
// the library streams as multipart form data -- the only way bytes that
|
||||
// exist solely on this host can reach a chat.
|
||||
//
|
||||
// The size check happens here rather than being left to Telegram: over the
|
||||
// limit the API answers a bad request whose text nobody reads, and the
|
||||
// operator sees an attachment that never arrived. The returned error is
|
||||
// classified invalid_message by the caller, so it is not retried -- a
|
||||
// file does not get smaller on a second attempt.
|
||||
func (s *telegramMediaSender) source(att gateway.MediaAttachment) (models.InputFile, func(), error) {
|
||||
noop := func() {}
|
||||
if att.Path == "" {
|
||||
return &models.InputFileString{Data: att.URL}, noop, nil
|
||||
}
|
||||
|
||||
info, err := os.Stat(att.Path)
|
||||
if err != nil {
|
||||
return nil, noop, fmt.Errorf("attach %s: %w", att.Path, err)
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return nil, noop, fmt.Errorf("attach %s: %w", att.Path, errNotRegularFile)
|
||||
}
|
||||
if limit := uploadLimit(att.Type); info.Size() > limit {
|
||||
return nil, noop, fmt.Errorf("attach %s: %w: %d bytes exceeds %d",
|
||||
att.Path, errTooLarge, info.Size(), limit)
|
||||
}
|
||||
|
||||
f, err := os.Open(att.Path)
|
||||
if err != nil {
|
||||
return nil, noop, fmt.Errorf("attach %s: %w", att.Path, err)
|
||||
}
|
||||
|
||||
name := att.FileName
|
||||
if name == "" {
|
||||
name = filepath.Base(att.Path)
|
||||
}
|
||||
return &models.InputFileUpload{Filename: name, Data: f}, func() { _ = f.Close() }, nil
|
||||
}
|
||||
|
||||
// uploadLimit reports the Bot API ceiling for a media kind. Photos have
|
||||
// their own, much lower one; everything else shares the general file
|
||||
// limit.
|
||||
func uploadLimit(mediaType string) int64 {
|
||||
if mediaType == "image" {
|
||||
return maxPhotoUploadBytes
|
||||
}
|
||||
return maxFileUploadBytes
|
||||
}
|
||||
|
||||
// dispatch routes an attachment to the Bot API method for its kind. The
|
||||
// media type, not the MessageEvent type, decides: the event type describes
|
||||
// the message while the attachment describes the file, and Telegram
|
||||
// rejects a video sent through sendPhoto.
|
||||
func (s *telegramMediaSender) dispatch(ctx context.Context, att gateway.MediaAttachment, caption string) (*models.Message, error) {
|
||||
file := &models.InputFileString{Data: att.URL}
|
||||
|
||||
func (s *telegramMediaSender) dispatch(ctx context.Context, att gateway.MediaAttachment, file models.InputFile, caption string) (*models.Message, error) {
|
||||
switch att.Type {
|
||||
case "video":
|
||||
p := &bot.SendVideoParams{ChatID: s.chatID, Video: file, Caption: caption}
|
||||
|
||||
@@ -2,10 +2,13 @@ package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -21,11 +24,21 @@ type mediaAPI struct {
|
||||
mu sync.Mutex
|
||||
method string
|
||||
form map[string]string
|
||||
body string
|
||||
// files records the multipart file parts an upload sends, by form
|
||||
// field name. A fetch-by-URL send has none; an upload must have one,
|
||||
// which is the difference these tests exist to prove.
|
||||
files map[string]uploadedFile
|
||||
body string
|
||||
}
|
||||
|
||||
// uploadedFile is one multipart file part as the Bot API received it.
|
||||
type uploadedFile struct {
|
||||
name string
|
||||
content string
|
||||
}
|
||||
|
||||
func newMediaAPI() (*mediaAPI, *httptest.Server) {
|
||||
api := &mediaAPI{form: map[string]string{}, body: `{"ok":true,"result":{"message_id":77}}`}
|
||||
api := &mediaAPI{form: map[string]string{}, files: map[string]uploadedFile{}, body: `{"ok":true,"result":{"message_id":77}}`}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
api.mu.Lock()
|
||||
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
||||
@@ -36,6 +49,7 @@ func newMediaAPI() (*mediaAPI, *httptest.Server) {
|
||||
api.form[k] = v[0]
|
||||
}
|
||||
}
|
||||
recordFileParts(api.files, r)
|
||||
}
|
||||
body := api.body
|
||||
api.mu.Unlock()
|
||||
@@ -46,6 +60,34 @@ func newMediaAPI() (*mediaAPI, *httptest.Server) {
|
||||
return api, srv
|
||||
}
|
||||
|
||||
// recordFileParts copies the request's multipart file parts into files,
|
||||
// keyed by form field. A fetch-by-URL send contributes none.
|
||||
func recordFileParts(files map[string]uploadedFile, r *http.Request) {
|
||||
if r.MultipartForm == nil {
|
||||
return
|
||||
}
|
||||
for field, headers := range r.MultipartForm.File {
|
||||
if len(headers) == 0 {
|
||||
continue
|
||||
}
|
||||
f, err := headers[0].Open()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
content, _ := io.ReadAll(f)
|
||||
_ = f.Close()
|
||||
files[field] = uploadedFile{name: headers[0].Filename, content: string(content)}
|
||||
}
|
||||
}
|
||||
|
||||
// uploaded returns the file part sent under field, if any.
|
||||
func (m *mediaAPI) uploaded(field string) (uploadedFile, bool) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f, ok := m.files[field]
|
||||
return f, ok
|
||||
}
|
||||
|
||||
func (m *mediaAPI) called() (string, map[string]string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@@ -165,9 +207,10 @@ func TestSendMedia_NoAttachmentIsAnError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// An attachment with no URL cannot be sent: this sender is URL-only, and
|
||||
// a FileID belongs to the platform it was uploaded from.
|
||||
func TestSendMedia_MissingURLIsAnError(t *testing.T) {
|
||||
// An attachment with neither a URL nor a local path names nothing to
|
||||
// send. A FileID does not count: it belongs to the platform it was
|
||||
// uploaded from.
|
||||
func TestSendMedia_MissingSourceIsAnError(t *testing.T) {
|
||||
_, srv := newMediaAPI()
|
||||
defer srv.Close()
|
||||
|
||||
@@ -261,3 +304,166 @@ func TestMediaSender_ReportsMediaCapability(t *testing.T) {
|
||||
t.Error("telegram media sender does not report the Media capability")
|
||||
}
|
||||
}
|
||||
|
||||
// writeTempFile creates a file of the given content and returns its path.
|
||||
func writeTempFile(t *testing.T, name, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), name)
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("write %s: %v", path, err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// A local file must be UPLOADED, not handed to Telegram as a URL to
|
||||
// fetch. This is the defect the Path field exists for: a host path in the
|
||||
// URL field is not fetchable, so the send did nothing while reporting
|
||||
// success.
|
||||
func TestSendMedia_UploadsLocalPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mediaType string
|
||||
file string
|
||||
method string
|
||||
field string
|
||||
}{
|
||||
{"document", "document", "transcript.md", "sendDocument", "document"},
|
||||
{"image", "image", "shot.png", "sendPhoto", "photo"},
|
||||
{"video", "video", "clip.mp4", "sendVideo", "video"},
|
||||
{"audio", "audio", "note.mp3", "sendAudio", "audio"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
api, srv := newMediaAPI()
|
||||
defer srv.Close()
|
||||
path := writeTempFile(t, tt.file, "file bytes")
|
||||
|
||||
sender := newTestMediaSender(t, srv.URL, 555, 0)
|
||||
res, err := sender.SendMedia(context.Background(), gateway.MessageEvent{
|
||||
Text: "a caption",
|
||||
Media: []gateway.MediaAttachment{{Type: tt.mediaType, Path: path}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SendMedia: %v", err)
|
||||
}
|
||||
if !res.Success {
|
||||
t.Fatalf("expected success, got %+v", res)
|
||||
}
|
||||
|
||||
method, form := api.called()
|
||||
if method != tt.method {
|
||||
t.Errorf("called %q, want %q", method, tt.method)
|
||||
}
|
||||
file, ok := api.uploaded(tt.field)
|
||||
if !ok {
|
||||
t.Fatalf("no file part under %q; form = %v (the path was sent as a URL to fetch, not uploaded)", tt.field, form)
|
||||
}
|
||||
if file.content != "file bytes" {
|
||||
t.Errorf("uploaded content = %q, want the file's bytes", file.content)
|
||||
}
|
||||
if file.name != tt.file {
|
||||
t.Errorf("uploaded filename = %q, want %q", file.name, tt.file)
|
||||
}
|
||||
if form["caption"] != "a caption" {
|
||||
t.Errorf("caption = %q, want it preserved on an upload", form["caption"])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// FileName overrides the on-disk basename, so a file written to a
|
||||
// generated temp name still arrives under a name that means something.
|
||||
func TestSendMedia_UploadUsesAttachmentFileName(t *testing.T) {
|
||||
api, srv := newMediaAPI()
|
||||
defer srv.Close()
|
||||
path := writeTempFile(t, "tmp-8342.bin", "x")
|
||||
|
||||
sender := newTestMediaSender(t, srv.URL, 555, 0)
|
||||
if _, err := sender.SendMedia(context.Background(), gateway.MessageEvent{
|
||||
Media: []gateway.MediaAttachment{{Type: "document", Path: path, FileName: "session.log"}},
|
||||
}); err != nil {
|
||||
t.Fatalf("SendMedia: %v", err)
|
||||
}
|
||||
file, ok := api.uploaded("document")
|
||||
if !ok {
|
||||
t.Fatal("no document file part")
|
||||
}
|
||||
if file.name != "session.log" {
|
||||
t.Errorf("uploaded filename = %q, want session.log", file.name)
|
||||
}
|
||||
}
|
||||
|
||||
// A file that cannot be uploaded must fail before any request, with an
|
||||
// error naming the reason. Silent non-delivery is the defect; a 400 whose
|
||||
// text nobody reads is the same defect wearing a status code.
|
||||
func TestSendMedia_LocalPathFailures(t *testing.T) {
|
||||
oversize := writeTempFile(t, "big.bin", "")
|
||||
if err := os.Truncate(oversize, maxFileUploadBytes+1); err != nil {
|
||||
t.Fatalf("truncate: %v", err)
|
||||
}
|
||||
overPhoto := writeTempFile(t, "big.png", "")
|
||||
if err := os.Truncate(overPhoto, maxPhotoUploadBytes+1); err != nil {
|
||||
t.Fatalf("truncate: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mediaType string
|
||||
path string
|
||||
wantSub string
|
||||
}{
|
||||
{"missing file", "document", filepath.Join(t.TempDir(), "gone.txt"), "gone.txt"},
|
||||
{"directory", "document", t.TempDir(), "not a regular file"},
|
||||
{"over the document limit", "document", oversize, "exceeds"},
|
||||
{"over the photo limit", "image", overPhoto, "exceeds"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
api, srv := newMediaAPI()
|
||||
defer srv.Close()
|
||||
|
||||
sender := newTestMediaSender(t, srv.URL, 555, 0)
|
||||
res, err := sender.SendMedia(context.Background(), gateway.MessageEvent{
|
||||
Media: []gateway.MediaAttachment{{Type: tt.mediaType, Path: tt.path}},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
if res.Success {
|
||||
t.Errorf("Success = true for an undeliverable file: %+v", res)
|
||||
}
|
||||
if res.Retryable {
|
||||
t.Error("Retryable = true; none of these get better on a retry")
|
||||
}
|
||||
if res.ErrorCode != "invalid_message" {
|
||||
t.Errorf("ErrorCode = %q, want invalid_message", res.ErrorCode)
|
||||
}
|
||||
if !strings.Contains(err.Error(), tt.wantSub) {
|
||||
t.Errorf("err = %v, want it to mention %q", err, tt.wantSub)
|
||||
}
|
||||
if method, _ := api.called(); method != "" {
|
||||
t.Errorf("called the API (%q) despite an undeliverable file", method)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A photo just under the photo ceiling must still be accepted -- the
|
||||
// per-type limits must not collapse into one conservative number.
|
||||
func TestSendMedia_PhotoUnderLimitIsSent(t *testing.T) {
|
||||
api, srv := newMediaAPI()
|
||||
defer srv.Close()
|
||||
path := writeTempFile(t, "ok.png", "small")
|
||||
|
||||
sender := newTestMediaSender(t, srv.URL, 555, 0)
|
||||
if _, err := sender.SendMedia(context.Background(), gateway.MessageEvent{
|
||||
Media: []gateway.MediaAttachment{{Type: "image", Path: path}},
|
||||
}); err != nil {
|
||||
t.Fatalf("SendMedia: %v", err)
|
||||
}
|
||||
if method, _ := api.called(); method != "sendPhoto" {
|
||||
t.Errorf("called %q, want sendPhoto", method)
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,16 @@ type MediaAttachment struct {
|
||||
// platforms that require a download API call.
|
||||
URL string `json:"url,omitempty"`
|
||||
|
||||
// Path is a local filesystem path on the host that produced the
|
||||
// attachment. It is the alternative to URL for outbound delivery: a
|
||||
// URL is fetched by the platform, a Path is uploaded by us. A file the
|
||||
// agent wrote has no URL and never gets one -- publishing it would
|
||||
// trade a delivery problem for a hosting and access-control one -- so
|
||||
// senders that support upload must read this.
|
||||
//
|
||||
// Exactly one of URL and Path is meaningful for an outbound send.
|
||||
Path string `json:"path,omitempty"`
|
||||
|
||||
// MIMEType is the content type (e.g. "image/png", "audio/ogg").
|
||||
MIMEType string `json:"mime_type,omitempty"`
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// Not lifted from tau: archie-original. It exports the read tool's own
|
||||
// path policy so a tool outside this package cannot end up applying a
|
||||
// second, subtly different one.
|
||||
package builtin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ErrPathNotAllowed reports a path refused by the workspace confinement.
|
||||
var ErrPathNotAllowed = errors.New("path is outside the configured workspace")
|
||||
|
||||
// ResolveReadable resolves path the way the read tool does -- relative
|
||||
// against cwd, leading @ stripped -- and refuses anything the read tool
|
||||
// would refuse, returning the absolute path when it is allowed.
|
||||
//
|
||||
// Callers that hand a file to somewhere outside the process (an outbound
|
||||
// attachment, say) must gate on this rather than on their own check: the
|
||||
// jail is a deployment posture, and a second implementation of it is a
|
||||
// second thing to keep in step with SetPathConfinement.
|
||||
func ResolveReadable(cwd, path string) (string, error) {
|
||||
resolved := resolvePath(cwd, path)
|
||||
if !isReadConfined(cwd, resolved) {
|
||||
return "", fmt.Errorf("%w: %s", ErrPathNotAllowed, resolved)
|
||||
}
|
||||
abs, err := filepath.Abs(resolved)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve %s: %w", resolved, err)
|
||||
}
|
||||
return abs, nil
|
||||
}
|
||||
@@ -22,10 +22,15 @@ type MultimodalResult struct {
|
||||
// Files lists the full paths written under SubdirHint, in the order
|
||||
// the source content blocks appeared.
|
||||
Files []string `json:"files,omitempty"`
|
||||
// URLs lists media that lives at a remote, already-hosted URL rather
|
||||
// than local bytes -- a generation API's result, for example, which
|
||||
// has nowhere to go in Files: there is no SubdirHint to write it under
|
||||
// because nothing was downloaded.
|
||||
// URLs lists media to deliver to the user's channel. Each ref carries
|
||||
// either a remote URL -- a generation API's result, for example,
|
||||
// which has nowhere to go in Files because nothing was downloaded --
|
||||
// or a local Path the channel uploads.
|
||||
//
|
||||
// The field used to mean remote-hosted media only; it now means "media
|
||||
// this result wants delivered", because a locally produced file has no
|
||||
// URL and exposing one for it was rejected. The JSON name is unchanged
|
||||
// so existing producers keep working.
|
||||
URLs []MediaRef `json:"urls,omitempty"`
|
||||
}
|
||||
|
||||
@@ -35,6 +40,13 @@ type MediaRef struct {
|
||||
// vocabulary gateway.MediaAttachment.Type uses, so a caller can build
|
||||
// one directly from this without translation.
|
||||
Type string `json:"type"`
|
||||
// URL is where the media is hosted.
|
||||
URL string `json:"url"`
|
||||
// URL is where the media is hosted, for media the channel should
|
||||
// fetch. Empty when the ref names a local file instead.
|
||||
URL string `json:"url,omitempty"`
|
||||
// Path is an absolute local filesystem path, for media the channel
|
||||
// should upload. Empty when the ref names a hosted URL instead.
|
||||
Path string `json:"path,omitempty"`
|
||||
// FileName is the name to present the upload under. Ignored for a
|
||||
// URL ref, which carries its own name.
|
||||
FileName string `json:"file_name,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
// Package sendfile provides the send_file tool: it delivers a file that
|
||||
// exists on the daemon host to the chat the turn is running in.
|
||||
//
|
||||
// It exists because the agent could produce a file -- a log dump, a
|
||||
// transcript, a screenshot -- and had no way to hand it over. The media
|
||||
// path it did have was fetch-by-URL, which only ever worked for remotely
|
||||
// hosted assets, so a local path was accepted and silently delivered
|
||||
// nothing. Exposing a public URL for local files was rejected: it trades a
|
||||
// missing feature for a hosting and access-control problem.
|
||||
//
|
||||
// The tool does not send anything itself. It validates the file under the
|
||||
// same path policy the read tool applies and returns a
|
||||
// tools.MultimodalResult naming it, which the turn's channel delivers by
|
||||
// upload. That keeps the tool free of any channel binding -- the tool
|
||||
// registry is process-wide, while a chat is per-turn -- and makes every
|
||||
// failure it CAN detect (unreadable, absent, refused by confinement, too
|
||||
// large) a tool error the model sees, rather than a silent non-delivery.
|
||||
package sendfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/tools/builtin"
|
||||
)
|
||||
|
||||
// ToolName is the registry name of the file-delivery tool.
|
||||
const ToolName = "send_file"
|
||||
|
||||
// MaxUploadBytes bounds what may be handed to a channel for upload. It is
|
||||
// the Telegram Bot API's 50 MB document ceiling, which is the tightest
|
||||
// limit among the channels that can deliver at all; checking it here means
|
||||
// an oversized file fails as a tool error the model can report instead of
|
||||
// as a delivery failure it never sees. The channel enforces its own real
|
||||
// limits regardless -- this is an early, visible check, not the authority.
|
||||
const MaxUploadBytes int64 = 50 * 1024 * 1024
|
||||
|
||||
// Tool builds the registry entry rooted at workspace. Relative paths
|
||||
// resolve against it, and confinement (when enabled) is measured from it,
|
||||
// exactly as for the read tool.
|
||||
//
|
||||
// Returns nil when workspace is empty: with no root there is no policy to
|
||||
// apply, and a tool that reads arbitrary host files into an outbound
|
||||
// message is not something to enable by defaulting.
|
||||
func Tool(workspace string) *tools.ToolEntry {
|
||||
if workspace == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &tools.ToolEntry{
|
||||
Name: ToolName,
|
||||
Toolset: "media",
|
||||
Description: "Send a file from this host to the user as an attachment in the current chat. " +
|
||||
"Use it for files you produced or found locally -- logs, transcripts, screenshots, archives -- " +
|
||||
"instead of describing a path or offering a link. The file is uploaded, so it must exist on " +
|
||||
fmt.Sprintf("this host and be no larger than %d MB.", MaxUploadBytes/(1024*1024)),
|
||||
Classification: tools.ClassIdempotent,
|
||||
Schema: tools.JSONSchema{
|
||||
"type": "object",
|
||||
"properties": map[string]any{
|
||||
"path": map[string]any{
|
||||
"type": "string",
|
||||
"description": "Path to the file to send. Relative paths resolve against the workspace.",
|
||||
},
|
||||
"caption": map[string]any{
|
||||
"type": "string",
|
||||
"description": "Optional short caption shown with the attachment.",
|
||||
},
|
||||
},
|
||||
"required": []any{"path"},
|
||||
},
|
||||
Handler: func(_ context.Context, input map[string]any) (any, error) {
|
||||
path, _ := input["path"].(string)
|
||||
caption, _ := input["caption"].(string)
|
||||
return prepare(workspace, path, caption)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// errNotRegularFile reports a target that exists but cannot be uploaded as
|
||||
// a file -- a directory, a device, a socket.
|
||||
var errNotRegularFile = errors.New("not a regular file")
|
||||
|
||||
// prepare validates the request and returns the result describing the
|
||||
// delivery. Every return path other than the final one is an error: a
|
||||
// caller must never be able to read "sent" out of a failed preparation.
|
||||
func prepare(workspace, path, caption string) (tools.MultimodalResult, error) {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return tools.MultimodalResult{}, fmt.Errorf("%s: path is required", ToolName)
|
||||
}
|
||||
|
||||
resolved, err := builtin.ResolveReadable(workspace, path)
|
||||
if err != nil {
|
||||
return tools.MultimodalResult{}, fmt.Errorf("%s: %w", ToolName, err)
|
||||
}
|
||||
|
||||
info, err := os.Stat(resolved)
|
||||
if err != nil {
|
||||
return tools.MultimodalResult{}, fmt.Errorf("%s: %w", ToolName, err)
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return tools.MultimodalResult{}, fmt.Errorf("%s: %s: %w", ToolName, resolved, errNotRegularFile)
|
||||
}
|
||||
if info.Size() > MaxUploadBytes {
|
||||
return tools.MultimodalResult{}, fmt.Errorf("%s: %s is %d bytes, over the %d byte upload limit",
|
||||
ToolName, resolved, info.Size(), MaxUploadBytes)
|
||||
}
|
||||
// Opened, not just stat'd: an unreadable file is a failure the model
|
||||
// must see now rather than one the channel discovers after the turn
|
||||
// has already reported success.
|
||||
f, err := os.Open(resolved)
|
||||
if err != nil {
|
||||
return tools.MultimodalResult{}, fmt.Errorf("%s: %w", ToolName, err)
|
||||
}
|
||||
_ = f.Close()
|
||||
|
||||
name := filepath.Base(resolved)
|
||||
summary := fmt.Sprintf("Sending %s (%d bytes) to the chat as an attachment.", name, info.Size())
|
||||
if caption != "" {
|
||||
summary = fmt.Sprintf("%s Caption: %s", summary, caption)
|
||||
}
|
||||
return tools.MultimodalResult{
|
||||
IsMultimodal: true,
|
||||
Summary: summary,
|
||||
URLs: []tools.MediaRef{{
|
||||
Type: MediaType(resolved),
|
||||
Path: resolved,
|
||||
FileName: name,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// MediaType maps a filename onto the four-value media vocabulary
|
||||
// gateway.MediaAttachment.Type uses.
|
||||
//
|
||||
// Anything unrecognised is a document, which is the only kind that carries
|
||||
// arbitrary bytes: guessing "image" wrongly makes the platform reject the
|
||||
// send, while sending an image as a document still delivers the file.
|
||||
func MediaType(path string) string {
|
||||
mt := mime.TypeByExtension(strings.ToLower(filepath.Ext(path)))
|
||||
switch {
|
||||
case strings.HasPrefix(mt, "image/"):
|
||||
return "image"
|
||||
case strings.HasPrefix(mt, "video/"):
|
||||
return "video"
|
||||
case strings.HasPrefix(mt, "audio/"):
|
||||
return "audio"
|
||||
default:
|
||||
return "document"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package sendfile
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/tools/builtin"
|
||||
)
|
||||
|
||||
// writeFile creates a file of size bytes under dir and returns its path.
|
||||
func writeFile(t *testing.T, dir, name string, size int) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(dir, name)
|
||||
if err := os.WriteFile(path, make([]byte, size), 0o600); err != nil {
|
||||
t.Fatalf("write %s: %v", path, err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// A prepared send must name the file as a local Path, never as a URL:
|
||||
// upload is the whole point, and a path in the URL field is exactly the
|
||||
// silent non-delivery this tool exists to end.
|
||||
func TestPrepareReturnsLocalPathRef(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
file string
|
||||
wantType string
|
||||
}{
|
||||
{"markdown is a document", "transcript.md", "document"},
|
||||
{"unknown extension is a document", "dump.bin", "document"},
|
||||
{"png is an image", "shot.png", "image"},
|
||||
{"mp4 is a video", "clip.mp4", "video"},
|
||||
{"mp3 is audio", "note.mp3", "audio"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ws := t.TempDir()
|
||||
path := writeFile(t, ws, tc.file, 16)
|
||||
|
||||
// Relative, to prove resolution happens against the workspace.
|
||||
res, err := prepare(ws, tc.file, "here you go")
|
||||
if err != nil {
|
||||
t.Fatalf("prepare: %v", err)
|
||||
}
|
||||
if !res.IsMultimodal {
|
||||
t.Error("IsMultimodal = false, want true")
|
||||
}
|
||||
if len(res.URLs) != 1 {
|
||||
t.Fatalf("refs = %d, want 1", len(res.URLs))
|
||||
}
|
||||
ref := res.URLs[0]
|
||||
if ref.Path != path {
|
||||
t.Errorf("Path = %q, want %q", ref.Path, path)
|
||||
}
|
||||
if ref.URL != "" {
|
||||
t.Errorf("URL = %q, want empty: a local file has no URL", ref.URL)
|
||||
}
|
||||
if ref.FileName != tc.file {
|
||||
t.Errorf("FileName = %q, want %q", ref.FileName, tc.file)
|
||||
}
|
||||
if ref.Type != tc.wantType {
|
||||
t.Errorf("Type = %q, want %q", ref.Type, tc.wantType)
|
||||
}
|
||||
if !strings.Contains(res.Summary, "here you go") {
|
||||
t.Errorf("Summary = %q, want it to carry the caption", res.Summary)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Every failure must be an error the model sees. Reporting a
|
||||
// MultimodalResult for a file that cannot be delivered would put the
|
||||
// caller back where it started: unable to tell delivery from silence.
|
||||
func TestPrepareFailuresAreErrors(t *testing.T) {
|
||||
ws := t.TempDir()
|
||||
outside := t.TempDir()
|
||||
writeFile(t, outside, "secret.txt", 8)
|
||||
big := writeFile(t, ws, "big.bin", 0)
|
||||
if err := os.Truncate(big, MaxUploadBytes+1); err != nil {
|
||||
t.Fatalf("truncate: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
wantErr error
|
||||
wantSub string
|
||||
}{
|
||||
{name: "empty path", path: " ", wantSub: "path is required"},
|
||||
{name: "missing file", path: "nope.txt", wantErr: os.ErrNotExist},
|
||||
{name: "directory", path: ".", wantSub: "not a regular file"},
|
||||
{name: "over the upload limit", path: "big.bin", wantSub: "upload limit"},
|
||||
{
|
||||
name: "outside the workspace",
|
||||
path: filepath.Join(outside, "secret.txt"),
|
||||
wantErr: builtin.ErrPathNotAllowed,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := prepare(ws, tc.path, "")
|
||||
if err == nil {
|
||||
t.Fatalf("prepare succeeded, want an error (got %+v)", res)
|
||||
}
|
||||
if len(res.URLs) != 0 || res.IsMultimodal {
|
||||
t.Errorf("failed prepare returned deliverable media: %+v", res)
|
||||
}
|
||||
if tc.wantErr != nil && !errors.Is(err, tc.wantErr) {
|
||||
t.Errorf("err = %v, want it to wrap %v", err, tc.wantErr)
|
||||
}
|
||||
if tc.wantSub != "" && !strings.Contains(err.Error(), tc.wantSub) {
|
||||
t.Errorf("err = %v, want it to mention %q", err, tc.wantSub)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Confinement is a deployment posture: with the jail lifted, a path
|
||||
// outside the workspace must be sendable, exactly as the read tool can
|
||||
// read it.
|
||||
func TestPrepareRespectsUnrestrictedFilesystem(t *testing.T) {
|
||||
ws := t.TempDir()
|
||||
outside := t.TempDir()
|
||||
path := writeFile(t, outside, "unit.service", 8)
|
||||
|
||||
if _, err := prepare(ws, path, ""); !errors.Is(err, builtin.ErrPathNotAllowed) {
|
||||
t.Fatalf("err = %v, want ErrPathNotAllowed while confined", err)
|
||||
}
|
||||
|
||||
builtin.SetPathConfinement(false)
|
||||
t.Cleanup(func() { builtin.SetPathConfinement(true) })
|
||||
|
||||
res, err := prepare(ws, path, "")
|
||||
if err != nil {
|
||||
t.Fatalf("prepare with confinement lifted: %v", err)
|
||||
}
|
||||
if len(res.URLs) != 1 || res.URLs[0].Path != path {
|
||||
t.Fatalf("refs = %+v, want the outside path", res.URLs)
|
||||
}
|
||||
}
|
||||
|
||||
// The entry withdraws entirely without a workspace: there is no root to
|
||||
// resolve against and no policy to apply.
|
||||
func TestToolRequiresWorkspace(t *testing.T) {
|
||||
if entry := Tool(""); entry != nil {
|
||||
t.Fatalf("Tool(\"\") = %+v, want nil", entry)
|
||||
}
|
||||
entry := Tool(t.TempDir())
|
||||
if entry == nil {
|
||||
t.Fatal("Tool(workspace) = nil, want an entry")
|
||||
}
|
||||
if entry.Name != ToolName {
|
||||
t.Errorf("Name = %q, want %q", entry.Name, ToolName)
|
||||
}
|
||||
if entry.Handler == nil {
|
||||
t.Error("Handler is nil")
|
||||
}
|
||||
}
|
||||
|
||||
// The handler must return the same result shape the channel decodes, so a
|
||||
// wiring change cannot quietly stop producing media refs.
|
||||
func TestHandlerReturnsMultimodalResult(t *testing.T) {
|
||||
ws := t.TempDir()
|
||||
writeFile(t, ws, "log.txt", 4)
|
||||
entry := Tool(ws)
|
||||
|
||||
out, err := entry.Handler(t.Context(), map[string]any{"path": "log.txt"})
|
||||
if err != nil {
|
||||
t.Fatalf("handler: %v", err)
|
||||
}
|
||||
res, ok := out.(tools.MultimodalResult)
|
||||
if !ok {
|
||||
t.Fatalf("out is %T, want tools.MultimodalResult", out)
|
||||
}
|
||||
if len(res.URLs) != 1 {
|
||||
t.Fatalf("refs = %d, want 1", len(res.URLs))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user