outbox

package
v0.16.521 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package outbox is a conversation's way to its agent: every message — a prompt, a "/model", a "!cmd" — enters the queue, and the head of the queue is typed by the typist when the agent can take it. A message's state moves only here, and every move is an event: queued{reason} → typing → in-input → accepted → in-turn → done, or failed{code} or cancelled. Every wait has a term (wire.Wait): nothing waits silently forever.

A Box is owned by the conversation's actor and is not safe for concurrent use. It does not type and does not keep time: Pump hands the actor a Job to type under the pane lock, the actor answers with Typed, and the actor calls Tick at Wake. The tests drive it on a fake clock with no goroutines.

Index

Constants

View Source
const (
	// QueuedTTL: a message waiting on the launch, a resume, a menu or a pause this long has failed{expired}.
	QueuedTTL = 15 * time.Minute
	// OccupiedTTL: a box that holds someone else's text this long fails the message waiting behind it.
	OccupiedTTL = 10 * time.Minute
	// FinalKept: a finished message stays in snapshots this long; on disk it stays FinalStored.
	FinalKept   = 10 * time.Minute
	FinalStored = 24 * time.Hour
	// GoneKept: a forgotten message's id is remembered this long (at most GoneMax of them), so the status of a message
	// sent long ago is «expired», not «unknown», and a page that sends it again does not have it typed a second time.
	GoneKept = 7 * 24 * time.Hour
	GoneMax  = 500
	// CancelKept: the cancel of a message the box has not seen (its send is still on the road, or waits in a hub) is
	// remembered this long, at most CancelMax of them, so the send that comes after it is not queued.
	CancelKept = 24 * time.Hour
	CancelMax  = 200
	// MaxTries: a message whose typing failed this many times in a row has failed{type-failed}; the tries back off
	// from retryBase, doubling up to retryMax (1.5 → 3 → 6 → 12 → 24 s).
	MaxTries = 10

	// A message carried by the launch of the agent (its first prompt) is waited for this long: a start can be slow.
	CarryDeadline = 90 * time.Second
	// CarryLongest: a carried or steered message past its term whose agent's transcript is not read to its end yet
	// waits for it this long at most — on a loaded machine a new session's transcript is found and read minutes after
	// the agent took the prompt, and failing it before is a red «not confirmed» over a turn that runs.
	CarryLongest = 10 * time.Minute
	// GatherMaxText is the longest message the line's gathering makes (UTF-16 units): a long paste is refused by Claude
	// Code without a word, so messages that would make more are typed one after another, each whole.
	GatherMaxText = 8_000

	// MaxCarriedText is the longest text a launch carries (the agent launcher caps a first prompt there).
	MaxCarriedText = 20_000

	// SteerDeadline: a message typed into a working agent waits in Claude's own queue until the agent's next step — a
	// Bash call or a long answer may take minutes — and the transcript says when it was read. Past this it is
	// failed{unconfirmed}, which the person can retry (the retry looks in the transcript first).
	SteerDeadline = 30 * time.Minute
)
View Source
const ConfirmWindow = wire.ConfirmDeadline * time.Millisecond

ConfirmWindow is how long a typed message waits for the agent's word — its hook or its transcript entry — before the box may type it again. The hook is a program the agent starts at the Enter and that delivers within a second or leaves its event in a spool read every 5 s; on a machine loaded several times over its cores both take seconds, and a retype sooner than they can come is a second copy of the message.

View Source
const LegacyLifetime = 24 * time.Hour

LegacyLifetime: a Legacy message older than this is imported as failed{expired}.

Variables

This section is empty.

Functions

func ConfirmsByHook

func ConfirmsByHook(p wire.Provider) bool

ConfirmsByHook says whether a provider's prompt-submit hook names the prompt the agent took. Claude's does; Codex has no such hook, and its messages go the way they went before.

Types

type Agent

type Agent struct {
	State wire.AgentState
	// Foreground: the agent, not a shell, is in the pane's foreground (an unknown agent is typed into only then).
	Foreground bool
	// HandOver: the running tool can be handed to the background (Claude: Bash, an MCP call).
	HandOver bool
	Provider wire.Provider
	// Screen is a look at the pane just now, nil when none was taken: a try the screen would refuse is not started,
	// so the pages do not see the message leave the queue and come back on every look.
	Screen *typist.Screen
}

Agent is what the head of the queue needs to know of the agent.

type Box

type Box struct {

	// Transcript looks for a message's own entry (its text) in the transcript; nil: none to look in. A message that was
	// typed once is looked for there before it is typed again, and a retry or a restart asks it rather than taking the
	// first user entry for the message: the transcript, not a timeout, says whether the agent has it.
	Transcript FindText
	// TranscriptRead says the transcript is found and read to its end, so Transcript's «no entry» means the agent
	// wrote none. The one retype of an unconfirmed message (HookConfirms) waits for it; nil: always read (no follower).
	TranscriptRead func() bool
	// HookConfirms: the agent's prompt-submit hook names the prompt it took (ConfirmsByHook). A typed message is then
	// delivered only by that hook or by its entry in the transcript; a box that no longer shows it proves nothing, and
	// the box types it again by itself once, and only when neither came within ConfirmWindow. False is the path of an
	// agent without such a hook, as it was: the screen decides, and a text stuck in the box is typed again.
	HookConfirms bool
	// Starting: the agent's launch is still under way. The message the launch carries is not failed meanwhile (a
	// loaded machine starts Claude in minutes, and the agent takes the prompt when it is up); it has CarryDeadline
	// from the end of the start.
	Starting bool
	// contains filtered or unexported fields
}

Box is one conversation's queue and messages.

func New

func New(conv string, limits wire.Limits, log Logger) *Box

New is an empty box of conversation conv.

func (*Box) Cancel

func (b *Box) Cancel(clientID string, now time.Time) (Change, wire.RefuseCode)

Cancel takes a queued message out; one already typed is too late, one that is over is only shown as it is. A message the box does not know is not-found, and the cancel is remembered: its send, when it comes, is not queued.

func (*Box) Carry

func (b *Box) Carry(clientID string, now time.Time) (Change, bool)

Carry takes a queued message out of the line to go with the launch of the agent, as its first prompt: it waits in-input for the agent to take it, and is never typed. False when it cannot (not queued, another on its way, images, a text the launch would cut).

func (*Box) Delivered

func (b *Box) Delivered(clientID string, turn *string, now time.Time) Change

Delivered: the agent's structured channel says it has this very message, named by its client id. It is accepted without an entry (the transcript gives it one when it is read) and joins the running turn when it was steered. No hook is waited for, and none that comes later confirms this send a second time.

func (*Box) HasAttachment

func (b *Box) HasAttachment(id string) bool

HasAttachment reports uploads on messages the person left in this conversation.

func (*Box) Head

func (b *Box) Head() string

func (*Box) Import

func (b *Box) Import(items []LegacyItem, now time.Time) Change

Import takes Legacy's queue of the tab: each message becomes one with client id "legacy-<id>", in its order; one that is a day old, or that Legacy gave up on, is failed{expired} and waits for the person.

func (*Box) InFlight

func (b *Box) InFlight() string

InFlight is the message typed or waiting to be accepted, "" none.

func (*Box) Interrupted

func (b *Box) Interrupted(now time.Time) Change

Interrupted: the person stopped the turn (Escape in the terminal, stop from the page). Claude puts what waited in its own queue back into the input box for editing: a steered message not read yet is there now, pulled back on purpose, and is never Entered again by itself — it fails as returned-to-input, its retry (which finds the text in the box and presses only its Enter) and its removal the person's to choose.

func (*Box) LostFirst

func (b *Box) LostFirst() string

Head is the client id of the message first in the line, "" for an empty line. LostFirst is the first prompt of a launch that ended before the agent took it (failed, never typed): the message a launch cut short by the machine owes the next one. Empty when there is none.

func (*Box) Message

func (b *Box) Message(clientID string) (wire.Message, bool)

Message is one message by its client id.

func (*Box) Messages

func (b *Box) Messages(now time.Time) []wire.Message

Messages are the messages not final, and the final ones of the last FinalKept, oldest first.

func (*Box) Move

func (b *Box) Move(clientID, before string, now time.Time) (Change, wire.RefuseCode)

Move puts a queued message right before another queued one (before "": at the end of the line). Only a message of the line moves: one typed, waiting in the agent's input or past that is too-late, an unknown one not-found. The line keeps its order of urgency (steered first, then «now», then the rest, as pushUrgent made it): a place that would put the message across a message of another urgency, or before one that has left the line, is a conflict. A move that changes nothing changes nothing and is accepted.

func (*Box) NeedsLook

func (b *Box) NeedsLook(now time.Time) bool

NeedsLook says the box wants a look at the pane even with nothing queued: a message in-input is due its recheck.

func (*Box) Pause

func (b *Box) Pause(reason wire.PauseReason, now time.Time) Change

Pause stops the queue for reason; Resume lifts the pause. Neither touches a message already on its way, but one the launch was to carry goes back to the line when the launch failed or the agent is gone.

func (*Box) Paused

func (b *Box) Paused() *wire.QueuePause

Paused is the queue's pause, nil none.

func (*Box) Promote

func (b *Box) Promote(clientID string, to wire.PromoteTo, agent wire.AgentState, now time.Time) (Change, wire.RefuseCode)

Promote moves a queued message to now (the head) or to steer (typed into the running turn). Steer needs a working agent: otherwise not-working, and the page sends now.

func (*Box) PromptSubmitted

func (b *Box) PromptSubmitted(turn *string, working bool, now time.Time) Change

PromptSubmitted is the agent's prompt-submit hook: the message in input is accepted (its entry not read yet). turn is the running turn, if any: a steered message joins it. A steered message is not: the hook fires when the Enter reaches Claude's queue, and the agent reads the message only at its next step. working says the agent was already in a turn when the hook came: a normal message typed just as the agent began a turn of its own (a background task's notice, a timer) is in Claude's queue the same way, not read, and is told so — see handOver.

func (*Box) PromptSubmittedAs

func (b *Box) PromptSubmittedAs(hash string, turn *string, working bool, now time.Time) Change

PromptSubmittedAs is PromptSubmitted for a hook that names the prompt the agent took (hash, screen.MessageHash of it). It confirms one send: the oldest typed message with these words that no hook confirmed yet — so two messages with the same words take a hook each, and a prompt the person typed in the terminal confirms nothing. A message that went back to the line because its try seemed lost is accepted where it stands and is not typed again. A hook that names no prompt (hash "": Codex, a Claude started before its hook carried one) is the message on its way's, as before.

func (*Box) Pump

func (b *Box) Pump(a Agent, size int64, now time.Time) (*Job, Change)

func (*Box) Queue

func (b *Box) Queue() wire.Queue

Queue is the conversation's queue as the wire carries it. An empty queue goes out as [], never null: the page takes null for a frame outside the protocol.

func (*Box) Restarted

func (b *Box) Restarted(find Find, inBox func(text string) bool, now time.Time) Change

Restarted settles the messages a restart caught on their way, by the transcript and the box, never blindly: the entry is there → accepted; the text is in the box → in-input, and the next try presses only its Enter; neither → queued again at the head. find looks for the entry past the typed offset; inBox says whether the box holds a text.

func (*Box) Restore

func (b *Box) Restore(st State)

Restore loads a box written before; its messages caught on their way wait for Restarted.

func (*Box) Resume

func (b *Box) Resume(now time.Time) (Change, bool)

Resume lifts the pause; false when the queue was not paused.

func (*Box) Retry

func (b *Box) Retry(clientID string, find Find, now time.Time) (Change, wire.RefuseCode)

Retry queues a failed message again, attempts+1. One the agent took after all (the transcript has its entry past its typed offset) is accepted instead, not typed again.

func (*Box) ReturnLostFirst

func (b *Box) ReturnLostFirst(since wire.Ms, now time.Time) Change

ReturnLostFirst puts the first prompt of a launch back at the head of the line when it failed at or after since without ever being typed: its term ran out before the launch was taken for failed, so Uncarry found it failed already, and the next launch started the agent with an empty box. since is when that launch began; a first prompt that failed before it is the person's to retry.

func (*Box) Send

func (b *Box) Send(d Draft, agent wire.AgentState, now time.Time) (Change, wire.RefuseCode)

Send queues a message. The same client id again gives back the message it queued (its answer, or its events, were lost on the way): accepted, nothing new, and its whole record goes out again so the page that sent it takes its true state. The id of a message forgotten long ago is too-late: it was delivered, and is never typed a second time. A message cancelled before it came is kept as cancelled and never queued. A refusal names its code.

A message of the line takes its place by when the page made it (place), not by when it arrived.

func (*Box) Soft

func (b *Box) Soft(clientID string) bool

Soft says whether the message, another agent's, is read at the agent's next step rather than at once.

func (*Box) State

func (b *Box) State() State

State is the box to write.

func (*Box) Status added in v0.15.46

func (b *Box) Status(ids []string) []wire.MessageStatus

Status is the box's word on each client id, in order: the record, and where a typed message went; a forgotten id is expired; any other unknown.

func (*Box) Steered

func (b *Box) Steered(clientID string) bool

Steered says whether the message was typed into a turn that ran (it is, or was, read inside that turn).

func (*Box) Summary

func (b *Box) Summary() wire.QueueSummary

Summary is the queue as the sidebar row needs it.

func (*Box) TakenBack

func (b *Box) TakenBack(d Draft, now time.Time) (Change, bool)

TakenBack answers the send of a message that was cancelled before the box saw it: the message is kept as cancelled, never queued, and its record goes out so every page shows it as taken back. False when no such cancel is remembered.

func (*Box) Tick

func (b *Box) Tick(now time.Time) Change

Tick ends the waits past their terms: in-input past its term goes back to the line stalled (a steered or carried message, which may still be in Claude's queue or its launch, fails unconfirmed), a message left typing that long with no try on its way too; a queued message past the TTL of its reason is failed{expired}; final messages older than FinalStored are forgotten, their ids kept GoneKept.

func (*Box) TurnEnded

func (b *Box) TurnEnded(turnID string, now time.Time) Change

TurnEnded ends the messages of a turn that finished, failed or was interrupted.

func (*Box) TurnStarted

func (b *Box) TurnStarted(turnID string, now time.Time) Change

TurnStarted moves the accepted messages into the turn that opened.

func (*Box) Typed

func (b *Box) Typed(clientID string, res typist.Result, now time.Time) Change

Typed takes the outcome of a try. Sent waits in-input for the agent to accept it; a screen that said wait or blocked puts the message back at the head to be looked at again (a block backs off); a key that did not go in counts a failed try, MaxTries in a row fail the message; text stuck in the box fails it, retryable.

func (*Box) Uncarry

func (b *Box) Uncarry(ch *Change, now time.Time)

Uncarry puts a message the launch was to carry back at the head of the line: the launch did not happen.

func (*Box) UserEntry

func (b *Box) UserEntry(entryID string, offset int64, turn *string, now time.Time) (string, Change)

UserEntry is UserEntryText for an entry whose text is not known: only the message on its way can take it.

func (*Box) UserEntryText

func (b *Box) UserEntryText(entryID, text string, offset int64, turn *string, now time.Time) (string, Change)

UserEntryText is a user entry read off the transcript at offset: the first one at or after the typed offset of the message on its way is that message's — it is accepted with the entry's id, and its client id is the answer, for the entry to carry. A message the hook accepted first gets its entry now. An entry no message on its way takes is looked at by its text: a message typed before that went back to the line or failed (its Enter seemed lost, and came later — Claude took the text after all) is accepted, not left to be typed a second time.

func (*Box) Wake

func (b *Box) Wake() time.Time

Wake is when the box next needs a look of its own: a retry's time or the end of a wait; zero when nothing waits on the clock.

type Change

type Change struct {
	Events []wire.Event
	// Resume asks the actor to start the stopped agent again on its session.
	Resume bool
}

Change is what a call did: the events to publish on the conversation's channel, in order.

type Draft

type Draft struct {
	ClientID    string
	Text        string
	Attachments []wire.AttachmentRef
	Images      []string
	// Refs are the places the message points at; Text already ends with their block.
	Refs []wire.MessageRef
	Mode wire.MessageMode
	Kind wire.MessageKind
	// Auto: the runtime wrote it itself to go on (wire.Message.Auto).
	Auto wire.AutoContinueWhy
	// Agent: another agent wrote it (Record.Agent). Soft: steered, it waits for the agent's next step (Record.Soft).
	Agent bool
	Soft  bool
}

Draft is a message to queue, checked and resolved by the caller (its attachments uploaded and found).

type Find

type Find func(offset int64, typedAt wire.Ms) (entryID string, ok bool)

Find looks for a message's user entry in the transcript at or after offset, or (a line read without its offset, after a restart) written at or after typedAt: its id when there is one.

type FindText

type FindText func(offset int64, typedAt wire.Ms, text string) (entryID string, ok bool)

FindText looks for the user entry of a message with this text at or after offset (or, read without its offset, written at or after typedAt).

type Job

type Job struct {
	ClientID string
	Message  typist.Message
	Attempt  int
}

Job is one try to type a message, for the actor to run under the pane lock.

type LegacyItem

type LegacyItem struct {
	ID        string
	Text      string
	Images    []string
	CreatedAt wire.Ms
	// Failed: Legacy delivery gave up on it already.
	Failed bool
}

LegacyItem is a message of Legacy's prompt-queue.json.

type Logger

type Logger interface {
	Info(msg string, kv ...any)
	Warn(msg string, kv ...any)
}

Logger takes a line of the runtime's journal.

type Record

type Record struct {
	Message wire.Message `json:"message"`
	// Images are the attachments' paths on the machine, pasted before the text.
	Images      []string `json:"images"`
	TypedAt     wire.Ms  `json:"typedAt"`
	TypedOffset int64    `json:"typedOffset"`
	// Tries counts the failed tries in a row; NextTry is when the next may start; WaitSince is since when it waits on
	// its current reason (the TTL counts from there).
	Tries     int              `json:"tries"`
	NextTry   wire.Ms          `json:"nextTry"`
	WaitSince wire.Ms          `json:"waitSince"`
	Reason    wire.QueueReason `json:"reason"`
	Blocked   int              `json:"blocked"`
	// BlockedSince: the first look that found someone's text in the box, for the term of that block (OccupiedTTL);
	// 0 when the box is not known to be taken.
	BlockedSince wire.Ms `json:"blockedSince,omitempty"`
	// Submitted: the agent's prompt-submit came while the message was being typed.
	Submitted bool `json:"submitted"`
	// EnterOnly: a restart found the message's text in the box; the next try presses only its Enter.
	EnterOnly bool    `json:"enterOnly"`
	EndedAt   wire.Ms `json:"endedAt"`
	// Steered: the message was typed into the running turn. Claude holds it in its own queue until the agent's next
	// step, so the prompt-submit hook (fired by the Enter) does not say it was read: its transcript entry does.
	Steered bool `json:"steered"`
	// Rechecks counts the Enters pressed again for a message waiting in-input; RecheckAt is when the box is looked at
	// for it next. recheck marks the try on its way as such.
	Rechecks int `json:"rechecks"`
	// Waits counts the looks in a row at which the screen said "wait" (waitQuick, then waitAgain).
	Waits     int     `json:"-"`
	RecheckAt wire.Ms `json:"recheckAt"`

	// Agent: another agent wrote the message, not the person. It is never typed into a question the agent asks the
	// person (that would answer it in the person's name) nor into a compaction, and it is gathered only with other
	// agents' messages. Soft: steered, it is read at the agent's next step — the running tool is not handed to the
	// background for it.
	Agent bool `json:"agent,omitempty"`
	Soft  bool `json:"soft,omitempty"`
	// Carried: the message goes with the launch of the agent as its first prompt, never typed.
	Carried bool `json:"carried"`
	// First: the launch chose this message as its first prompt. If that launch fails, it stays ahead of later urgent
	// messages until a retry carries it again; its public mode remains the mode the person sent (normally queue).
	First bool `json:"first,omitempty"`
	// Stalled: typed, and the agent did not take it within wire.ConfirmDeadline. It waits at the head of the line for
	// the person (retry, promote, cancel) and is not typed again by itself. ClearBox: its text may still be in the
	// agent's input box and is to be taken out of it at the next look, so no late Enter sends it behind our back.
	Stalled  bool `json:"stalled,omitempty"`
	ClearBox bool `json:"clearBox,omitempty"`
	// ClearAt is when the box is looked at for it next; ClearLooks counts the times no look came.
	ClearAt    wire.Ms `json:"clearAt,omitempty"`
	ClearLooks int     `json:"clearLooks,omitempty"`
	// EntryID is the transcript entry the message was accepted as, "" while none is known.
	EntryID string `json:"entryId,omitempty"`
	// Send numbers the try that typed the message, in the order the box typed (0: never typed). Two messages with the
	// same words are two sends: each prompt-submit hook confirms one of them, the oldest not yet confirmed. HookSend is
	// the send a hook confirmed.
	Send     int64 `json:"send,omitempty"`
	HookSend int64 `json:"hookSend,omitempty"`
	// Retyped: the one retype the box makes by itself for a send nobody confirmed is spent (HookConfirms).
	Retyped bool `json:"retyped,omitempty"`
	// contains filtered or unexported fields
}

Record is a message with what only the runtime keeps of it.

type State

type State struct {
	Records []Record         `json:"records"`
	Items   []string         `json:"items"`
	Paused  *wire.QueuePause `json:"paused"`
	Rev     int64            `json:"rev"`
	Flight  string           `json:"flight"`
	// Sends is the number of the last send (Record.Send).
	Sends int64 `json:"sends,omitempty"`
	// Gone are the ids of the messages let go and when they ended (GoneKept).
	Gone map[string]wire.Ms `json:"gone,omitempty"`
	// Cancelled are the ids of the messages cancelled before the box saw them, and when (CancelKept).
	Cancelled map[string]wire.Ms `json:"cancelled,omitempty"`
}

State is a box as it is written to disk: the messages not final and the final ones of the last FinalStored, the queue's order and pause.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL