Files
archie/internal/domain/messaging/inbound.go
T
sam c70b144782 fix(messaging): carry an inbound attachment's bytes to the gateway turn
A Telegram photo or document was dropped between the process that receives
it and the one that runs the turn. Telegram runs in archie-messaging, which
downloads the attachment; the turn runs in archie-gateway/archied, which
never holds the bot token. gatewayrpc stripped MediaAttachment.Data on the
inbound request, so by the time the Gateway's model seam ran, the
attachment had no bytes and no URL, and mediaParts dropped it -- the model
read only the "[photo]" placeholder.

- proto/gateway/v1: Media gains a `data` field, populated only on the
  inbound request. Stored-history and outbound-event directions keep it
  empty: a persisted record still strips the bytes, and a streamed media
  event still carries metadata only.
- gatewayrpc: the inbound conversion maps Data; the stored and outbound
  conversions deliberately do not.
- archied: the Gateway server raises its gRPC receive limit from the 4 MB
  default to the messaging contract's inbound attachment ceiling plus
  framing headroom, so a maximal (20 MB) attachment is accepted rather than
  failing the RPC.
- messaging: MaxInboundAttachmentBytes is the single ceiling the Telegram
  download gate and the Gateway receive limit both derive from.

Red first: a new TestInboundMediaBytesReachTheTurn asserted the bytes crossed
the wire and failed at that assertion before the mapping changed; it was then
consolidated into TestInboundMediaRoundTrip, which now pins metadata plus bytes.
2026-10-02 10:18:01 +10:00

103 lines
4.7 KiB
Go

package messaging
import (
"context"
"time"
)
// Inbound is a message arriving from a channel together with the
// transport context that is not part of the canonical record.
//
// Message is the record itself, exactly as it will be persisted. Channels
// construct it directly: ConversationID addresses the chat (a Telegram
// chat ID and topic thread, an email recipient, a webhook path), Sender is
// the channel-native attribution, and Role is always RoleUser --
// a channel only ever carries what a person said. ID is left empty for a
// newly received message so the store derives one from SourceID, and
// honoured when set, which is what lets a read-modify-write of a history
// keep its identities rather than minting new ones.
type Inbound struct {
Message Message
// Page is the dashboard route the operator is on when they sent
// this message. Transport-only: it reaches the system prompt and
// is never persisted. Empty for non-web channels.
Page string
// BudgetKey is what the Gateway charges this message against in its
// inbound rate limiter (internal/ratelimit), for a channel whose
// Message.SenderID is empty because it carries no per-person
// identity but still has a stable, operator-controlled source to
// budget -- a webhook's configured route path, today. It is
// deliberately not SenderID: SenderID means "this is who sent it",
// and several consumers (internal/app/archied/chat_identity.go,
// sessioncurator) read it as a person, so a route path in that field
// becomes a user identity. BudgetKey is transport-only and never
// persisted, exactly like Page. Empty means the message is not
// rate limited, which is correct for a source with no stable key of
// its own (the dashboard).
BudgetKey string
// Platform is the channel that carried this message ("telegram",
// "email", "webhook", "web"), named by the frontend that owns that
// channel. It is how the Gateway learns which channel it is serving:
// one Router serves all of them, so its own name is "web" even for a
// Telegram turn, which made SessionSource.Platform -- the first
// component of the session natural key -- a constant, and left the
// per-user identity policy unreachable with a real channel name
// (archie-core-c1qx). Transport-only and never persisted: the session
// record keeps the platform. Empty means the sender did not name its
// channel, and the Gateway falls back to its own name.
Platform string
// Media carries files the sender attached to this message (photos,
// documents, voice, video) with their in-process bytes already
// downloaded. Like Page and BudgetKey it is transport-only: it never
// crosses a persistence boundary, and the stored Message records the
// attachment as a short textual note in its Text instead, because
// download URLs expire and raw media bytes must not silently inflate
// the transcript store. It does cross the inbound request to the
// Gateway process that runs the turn -- that process has no platform
// credential, so the bytes have no other way to reach the model. nil
// for a text-only message.
Media []MediaAttachment
}
// SpawnRequest is a chat-originated task creation request. Repo and
// Workflow are optional -- empty means "the daemon's configured
// default for this identity".
type SpawnRequest struct {
Title string
Body string // operator instructions carried into the admitted task
Repo string // "owner/name"; empty = identity's default repo
// Workflow may name a workflow whose interface declares inputs
// (pr-review's pr_number, for instance). Inputs assigns them; a
// workflow that declares none ignores it.
Workflow string // empty = daemon's default workflow routing
Inputs map[string]any
Identity string // the identity spawning this task; propagated from Router.Identity
}
// TaskCreator creates a native (non-forge-backed) task from a chat
// command. The daemon supplies an implementation backed by the store.
// When nil on a Router, /spawn returns "not configured". CreateTask
// must return the task's real, durable database ID -- never a
// synthetic or fabricated value.
type TaskCreator interface {
CreateTask(ctx context.Context, req SpawnRequest) (taskID int64, err error)
}
// DangerousCommandAuthority is the sandbox-owned authority for operations
// that can destroy process or filesystem state. Adapters only request and
// present approval; they never implement these operations themselves.
type DangerousCommandAuthority interface {
StopProcess(context.Context, string) error
Rollback(context.Context, int) (string, error)
ListCheckpoints(context.Context) ([]CheckpointInfo, error)
}
// CheckpointInfo describes a saved sandbox filesystem checkpoint.
type CheckpointInfo struct {
Number int
Timestamp time.Time
Label string
Size string
}