Documentation
¶
Overview ¶
Package msgqueue is the in-memory, daemon-resident inbound backlog for phone-originated send_message turns. It is the inbound counterpart of internal/eventring (the outbound per-conversation event store): where eventring buffers what the daemon pushes OUT to phones, msgqueue buffers what phones send IN while claude is busy.
Today send_message delivers synchronously — the handler blocks the per-conn goroutine inside WriteUserTurn's idle gate while claude is mid-turn, and the turn fails if claude stays busy past a 30s cap (#594). A phone that types while claude is working therefore either blocks or loses the message, and concurrent messages race across handler goroutines with no defined order.
Queue fixes that. Enqueue appends a message to its conversation's FIFO and returns immediately (non-blocking), and a single drain goroutine per conversation delivers the backlog one at a time, in enqueue order, through an injected reliable-delivery seam (DeliverFunc, the shape of Supervisor.WriteUserTurn — the #594 ready-gate → commit-confirm → recovery path). The drain is paced entirely by that seam: DeliverFunc blocks while claude is busy and returns only on a confirmed commit, so a message enqueued mid-turn is held until the turn ends, one enqueued while claude is idle drains promptly, and there is never more than one in-flight delivery per conversation. No separate turn-state detector is needed.
The backlog lives above the claude child's lifecycle: an undelivered message is retried at the FIFO head until it is confirmed delivered, so it survives a claude *child* respawn and drains into the new child. It deliberately does NOT survive a full daemon-process restart — the queue is purely in-memory, the same loss boundary as eventring; reconnect/resync covers it. There is no on-disk persistence in this slice.
This slice ships the engine unwired (#704): no package depends on it yet. The live wiring into the send_message handler and the cmd/pyry constructor, plus the queue_state / dequeue_message reporting types and any inbound-bound policy, are separate slices (#705 / the wiring slice). The single insertion point for a future bound is Enqueue.
SECURITY: the queued text is untrusted, phone-originated content bound for claude's stdin verbatim. It is treated as opaque transit bytes — stored, never inspected or used in a control decision, and converted to []byte only at the DeliverFunc call. It is NEVER logged at any level (mirrors internal/relay/handlers/send_message.go's discipline); the drain's warn-on-error logs only conversation_id, the queued message id, and the enqueue timestamp. convID is used solely as a map key; validating/resolving it to a real session is the caller's job (upstream of Enqueue), so a hostile convID can at worst create an isolated FIFO that never drains — never reach another conversation's session.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChangeFunc ¶
type ChangeFunc func(convID string)
ChangeFunc is the injected change-notification seam — it mirrors the DeliverFunc seam style. It is invoked, NEVER while holding q.mu, with the id of the conversation whose backlog changed (on enqueue, on delivery-advance, and on a successful Remove). It carries only convID; the consumer re-reads the current backlog via Snapshot (the seam is edge-triggered — coalescing is the consumer's choice). nil disables notification.
It MUST NOT block (a blocking ChangeFunc on the drain path stalls that conversation's drain) and MUST be safe for concurrent invocation: it fires from the Enqueue caller's goroutine, from each drain goroutine, and from the Remove caller's goroutine. The consumer owns its own synchronization.
type Config ¶
type Config struct {
// Deliver is the reliable-delivery seam; required. New errors if it is nil.
Deliver DeliverFunc
// RetryInterval is the poll cadence while delivery keeps failing.
// <= 0 ⇒ defaultRetryInterval.
RetryInterval time.Duration
// OnChange is the optional change-notification seam; nil ⇒ disabled.
OnChange ChangeFunc
// Logger; nil ⇒ slog.Default().
Logger *slog.Logger
}
Config configures a Queue.
type DeliverFunc ¶
DeliverFunc is the injected reliable-delivery seam — the shape of supervisor.WriteUserTurn. It MUST block while claude is busy (the WaitReady idle gate) and return nil ONLY on a confirmed commit; that blocking IS the drain's turn-end pacing. A non-nil return means the turn was not delivered and the drain retries the same message at the FIFO head.
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue is a per-conversation, in-memory inbound message backlog with one serial drain goroutine per active conversation. The zero value is not usable — construct with New. Enqueue is safe for concurrent use; Run is called once.
func New ¶
New constructs a Queue. It returns an error if cfg.Deliver is nil — the seam is caller-supplied, so a missing one is a wiring error to surface, not a programmer-constant to panic on (contrast eventring.New's bound). RetryInterval and Logger fall back to their defaults.
func (*Queue) Enqueue ¶
Enqueue appends text to convID's FIFO and returns the stable id assigned to it (>= 1, monotonic per conversation, constant until the message is delivered). It never blocks on delivery: if the lifecycle is running and no drain is already servicing the conversation, it spawns one and returns. Conversations' queues are independent — appending to one never blocks or reorders another.
func (*Queue) Remove ¶
Remove drops a queued, not-in-flight message by id from convID's FIFO, preserving the surviving order, and returns true iff it removed one. An unknown conversation, an unknown or already-delivered id, or the in-flight (draining) head is a safe no-op that returns false — no panic, no reorder. This is the engine op behind dequeue_message; the in-flight-head no-op is what guarantees dequeue_message cannot cancel an in-flight delivery.
func (*Queue) Run ¶
Run binds the lifecycle ctx, starts a drain for any conversation that already holds a backlog (covering Enqueue-before-Run, with no lost wakeup), then blocks until ctx is done and joins every drain goroutine before returning ctx.Err(). It is called once; the daemon adds it to its errgroup in the wiring slice.
func (*Queue) Snapshot ¶
func (q *Queue) Snapshot(convID string) []QueuedMessage
Snapshot returns convID's not-yet-confirmed-delivered backlog as an ordered copy (the data queue_state reports). An unknown conversation yields an empty snapshot, not an error. The returned slice is freshly allocated and the elements are value copies, so a caller cannot mutate engine state through it.
The snapshot includes the head even while it is mid-delivery: an item is in the backlog until advanceLocked drops it on a confirmed commit, so "current items" is exactly "not-yet-delivered". The snapshot does not flag which entry is in-flight — Remove returning false is how a consumer learns the head is non-removable.
type QueuedMessage ¶
QueuedMessage is the engine-side projection of ADR 025's {queued_msg_id, text, ts} record (the producer maps ID -> queued_msg_id). It is the ordered element Snapshot returns. Text is untrusted, phone-originated transit content: the consumer must never log it and must only surface it to the authorized conversation it belongs to.