Documentation
¶
Overview ¶
Package spool is the hand-over between serve and a Claude Code session's hooks (hook delivery, mash v0.2.0). serve writes each message it has received to the spool and holds it unacked; a hook (`mash hook`) shows the spooled messages to the session and marks them drained; serve then acks them, so a message is acked only once it has been seen.
Delivery is at-least-once: a message is never lost, but one may be shown twice. A hook prints its batch and then marks each entry drained; a kill between the two leaves the entries waiting, and the next run shows them again. To make that visible, a hook first records the seqs it is about to show in shown/<seq>; an entry read back with its marker still present has Redelivered set, and the host adapter tells the model it may already have been seen.
Layout, under Dir (a message of a second stream is named <stream>@<seq> where <seq> is written; the endpoint's own stream has no prefix): <seq>.json for each message waiting (0600), shown/<seq> for each one a hook is about to show and has not yet drained, drained/<seq> for each one a hook has shown and serve has not yet acked, and .hooked, whose mtime is the last hook run, listed/<seq> for each one a hook has only listed in a digest (still waiting, not drained: it is drained when the model reads it or a later hook shows it in full, ADR 0046), and .alive, whose mtime is the last run of a subagent's hook. The directories are 0700.
Index ¶
- Constants
- func Dir(stateDir, owner, name string) string
- type Entry
- type Ref
- type Refusal
- type Spool
- func (s Spool) Acked(seq Ref) error
- func (s Spool) Alive() error
- func (s Spool) AliveSince(t time.Time) bool
- func (s Spool) Bind(stream string) error
- func (s Spool) BindStream(name, ident string) error
- func (s Spool) ClearStaleWaitState() error
- func (s Spool) ClearWaitState(servePID int, instance string) error
- func (s Spool) ClearWaiter(pid int) error
- func (s Spool) ClearWaiting() error
- func (s Spool) Discard(seq Ref) error
- func (s Spool) Drained(seq Ref) error
- func (s Spool) Has(seq Ref) bool
- func (s Spool) HookState() ([]byte, bool)
- func (s Spool) HookedSince(t time.Time) bool
- func (s Spool) Init() error
- func (s Spool) IsDrained(seq Ref) bool
- func (s Spool) IsListed(seq Ref) bool
- func (s Spool) LastHook() (time.Time, bool)
- func (s Spool) Listed(seq Ref) error
- func (s Spool) LiveWaitState() (WaitState, bool)
- func (s Spool) Lock() (unlock func(), err error)
- func (s Spool) LockContext(ctx context.Context) (unlock func(), err error)
- func (s Spool) MarkShown(refs ...Ref) error
- func (s Spool) NoteHookState(b []byte) error
- func (s Spool) NoteRefused(r Refusal) error
- func (s Spool) NoteTold(key string) error
- func (s Spool) NoteWaitNotice(now time.Time) error
- func (s Spool) NoteWaitState(w WaitState) error
- func (s Spool) NoteWaiter(r WaiterRecord) error
- func (s Spool) Own(record string) error
- func (s Spool) Owner() string
- func (s Spool) Put(e Entry) error
- func (s Spool) Refused() (at time.Time, why string, ok bool)
- func (s Spool) Told(key string) bool
- func (s Spool) Touch() error
- func (s Spool) WaitNoticeDue(now time.Time, every time.Duration) bool
- func (s Spool) Waiters() []WaiterRecord
- func (s Spool) Waiting() ([]Entry, error)
- func (s Spool) Writable() bool
- type WaitState
- type WaiterRecord
Constants ¶
const OwnerUnreadable = "{owner record unreadable"
OwnerUnreadable is what Owner returns for an .owner that is present but unreadable, empty or not JSON. It starts with "{" and is not valid JSON, so the hook's Match refuses it: a damaged record is never read as "no owner".
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Entry ¶
type Entry struct {
Seq uint64 `json:"seq"`
// Stream names the stream Seq is a sequence of; empty for the endpoint's own (primary) stream, and then absent
// from the file, so a single-stream entry is byte-identical to one written before streams were keyed.
Stream string `json:"stream,omitempty"`
From string `json:"from"`
Trust string `json:"trust"`
Instance string `json:"instance,omitempty"`
Owner string `json:"owner,omitempty"` // the sender's owner
Urgent bool `json:"urgent,omitempty"`
Kind string `json:"kind,omitempty"` // blocker, question, task, result or fyi; empty (an older serve) reads as task
ReplyTo uint64 `json:"reply_to,omitempty"` // the verified #seq the sender says this answers
Via string `json:"via,omitempty"` // the verified relay declaration, "<origin> #<seq>"
At time.Time `json:"at"`
Body []byte `json:"body"`
// Redelivered is set by Waiting, never persisted: a hook had begun to show this entry and did not finish
// draining it, so the session may already have seen it.
Redelivered bool `json:"-"`
// Listed is never persisted. Waiting sets it for an entry a hook has listed in a digest (its listed/<seq> mark is
// there); the host adapter's Present sets it on the entries it rendered as digest lines, which the hook then marks
// listed instead of drained.
Listed bool `json:"-"`
}
Entry is one spooled message: what render needs to show it.
type Ref ¶ added in v0.4.0
Ref identifies one message: the stream it is in and its sequence there. Two streams of one endpoint number from 1 each, so the bare sequence is not an identity once an endpoint reads more than one. The endpoint's own (primary) stream is Stream == "", and its refs behave exactly as the bare sequence did.
type Refusal ¶
type Refusal struct {
Time time.Time `json:"time"`
Why string `json:"why"`
Session string `json:"session,omitempty"` // the hook's session id
Ancestors []int `json:"ancestors,omitempty"`
}
Refusal is what a hook that matched none of the ownership rules leaves in .refused.
type Spool ¶
type Spool struct{ Dir string }
Spool is one endpoint's spool directory.
func (Spool) Acked ¶
Acked forgets seq once serve has acked it: its tombstone, its shown mark and any waiting file (a redelivery spooled again).
func (Spool) Alive ¶
Alive records a hook run by one of this session's own subagents (never a drain): hooks work in this session, though the main session is busy in a subagent. The marker's mtime is the run.
func (Spool) AliveSince ¶
AliveSince reports whether a subagent's hook has run at or after t.
func (Spool) Bind ¶
Bind scopes the spool's primary stream to stream, the identity of the inbox stream. It is BindStream("", stream).
func (Spool) BindStream ¶ added in v0.4.0
BindStream scopes the spool's messages of the stream named name ("" is the endpoint's own inbox stream) to ident, that stream's identity. Sequences restart at 1 when a stream is recreated (deprovision, then provision), so a tombstone or waiting file left by the old stream would match new mail: BindStream drops that stream's waiting files, tombstones and marks when ident differs from the one recorded, and records it. Another stream's files and marks are not touched: each stream is bound on its own. Equal, it changes nothing (codex H1 on #21, round 2). Call it before anything of that stream is spooled. The primary stream's marker is .stream (as it always was), another's .stream.<name>.
func (Spool) ClearStaleWaitState ¶ added in v0.4.0
ClearStaleWaitState removes the state when its serve process is gone (or it is unreadable) and the rate-limit record with it; a state of a live serve is left alone. Call it under Lock.
func (Spool) ClearWaitState ¶ added in v0.4.0
ClearWaitState removes the state and the notice's rate-limit record, but only the state that still names this serve (its pid and instance): the files are shared by every serve of the endpoint that waits, so a serve whose state a later one overwrote must not clear the later one's. With no state there is none to protect and the rate-limit record goes. Call it under Lock.
func (Spool) ClearWaiter ¶ added in v0.3.1
ClearWaiter removes the waiting record of pid, and every other .waiting.* that names it. Call it under Lock.
func (Spool) ClearWaiting ¶
ClearWaiting removes every waiting <seq>.json and leftover .put-* temporary file, keeping the drained/ tombstones and .hooked, and the shown/ marks like the tombstones: a message shown and not drained is redelivered, and may have been seen. The listed/ marks go with the files: a message only listed in a digest was not seen, and its redelivery is listed again, once (ADR 0046). A restarted serve calls it: NATS redelivers what was never acked (codex M1 on #21).
func (Spool) Discard ¶ added in v0.4.0
Discard forgets a message that was spooled and has not been drained: its waiting file, shown mark and listed mark go, and nothing is acknowledged. A drained message (its tombstone is there) is left alone, as a hook has shown it. Call it under Lock, so it cannot interleave with a hook's read and drained marks.
func (Spool) Drained ¶
Drained marks seq shown: its tombstone is written, then its file removed, then its shown mark. Tombstone first, so a crash in between leaves the message both waiting and drained, which shows it again (flagged Redelivered, as the mark is still there) rather than losing it.
func (Spool) Has ¶
Has reports whether seq's waiting file is present and parses (what Waiting would list).
func (Spool) HookState ¶ added in v0.5.6
HookState is what NoteHookState recorded, and whether there is one.
func (Spool) HookedSince ¶
HookedSince reports whether a hook has run at or after t.
func (Spool) IsDrained ¶
IsDrained reports whether a hook has shown seq and serve has not yet acked it.
func (Spool) IsListed ¶ added in v0.3.2
IsListed reports whether a hook has listed seq in a digest and nothing has drained it since.
func (Spool) LastHook ¶
LastHook is the time of the last hook run recorded in the spool, and whether there is one.
func (Spool) Listed ¶ added in v0.3.2
Listed marks seq as listed in a digest and not seen: its listed mark is written (atomically: a temporary file, renamed) and its shown mark removed, so a later run does not flag it re-delivered. Its file stays, so a later hook can show it in full; it is not drained, so serve does not ack it (ADR 0046).
func (Spool) LiveWaitState ¶ added in v0.4.0
LiveWaitState is the recorded state when there is one and its serve process is still alive; a missing, unreadable or stale state (the serve is gone) is not one. A stale state's files are left for the next ClearWaitState.
func (Spool) Lock ¶
Lock takes the spool's exclusive advisory lock (the file .lock, blocking) and returns the function that releases it. Serve holds it while it rebinds and re-owns the spool, and a hook holds it across its owner check, read and drained marks, so a hook that passed the owner check can never drain mail a new lease holder spooled after it (codex H1 on #21, round 7).
func (Spool) LockContext ¶ added in v0.5.10
LockContext is Lock that gives up when ctx ends, returning ctx's error: it tries the lock without blocking and waits a short interval between attempts, so a holder that never lets go cannot hold the caller past its context.
func (Spool) MarkShown ¶
MarkShown records that a hook is about to show seqs, before it writes them out. If the hook is then killed, the entries stay waiting and the next Waiting flags them Redelivered. A seq that cannot be marked stops the rest; the caller shows anyway (at-least-once wins) and reports.
func (Spool) NoteHookState ¶ added in v0.5.6
NoteHookState records the state of the hook detectors that the serve holding this spool keeps (JSON, atomic write). It holds no mail and no secret. Own removes it, so it is always the current holder's.
func (Spool) NoteRefused ¶
NoteRefused records a refused hook in .refused, only if there is none or it is older than 60 s. Call it under Lock.
func (Spool) NoteTold ¶ added in v0.3.1
NoteTold records that the notice for key was printed, replacing any earlier key. Call it under Lock.
func (Spool) NoteWaitNotice ¶ added in v0.4.0
NoteWaitNotice records that the notice was printed at now. Call it under Lock.
func (Spool) NoteWaitState ¶ added in v0.4.0
NoteWaitState records the state, atomically. Call it under Lock.
func (Spool) NoteWaiter ¶ added in v0.3.1
func (s Spool) NoteWaiter(r WaiterRecord) error
NoteWaiter records a serve waiting for the lease, atomically, in .waiting.<pid>. Call it under Lock.
func (Spool) Own ¶
Own binds the spool to the owner record of the serve that holds the lease (JSON {pid, session, launch}, ADR 0040), so a hook of another session with the same endpoint name leaves it alone (codex H1 on #21, round 3). "" removes the binding. The caller holds the spool lock; the write is atomic, so a hook never reads a half-written record.
func (Spool) Owner ¶
Owner is what a hook matches against: the JSON record in .owner, "" when there is none.
func (Spool) Put ¶
Put writes e atomically (a 0600 temporary file, synced, renamed into place). A message put again (a redelivery) replaces its file.
func (Spool) Refused ¶
Refused is the last recorded refusal: when (the file's mtime), why, and whether there is one.
func (Spool) Told ¶ added in v0.3.1
Told reports whether the in-session notice for key (a short hash of one copy of a session) was already printed. Call it under Lock.
func (Spool) WaitNoticeDue ¶ added in v0.4.0
WaitNoticeDue reports whether the hook's notice is due: none was recorded since the state appeared, or the last was at least every ago. Call it under Lock.
func (Spool) Waiters ¶ added in v0.3.1
func (s Spool) Waiters() []WaiterRecord
Waiters lists the recorded waiting serves whose process is still alive. A junk file or a dead pid is skipped.
type WaitState ¶ added in v0.4.0
type WaitState struct {
HolderPID string `json:"holder_pid"`
HolderHost string `json:"holder_host"`
HolderSince string `json:"holder_since,omitempty"`
ServePID int `json:"serve_pid"`
Instance string `json:"instance,omitempty"` // the serve's own token: told from another serve whose pid is reused
ParentPID int `json:"parent_pid,omitempty"` // the serve's parent: its hooks descend from it
Session string `json:"session,omitempty"` // the serve's CLAUDE_CODE_SESSION_ID
Updated time.Time `json:"updated"` // rewritten on every retry
}
WaitState is a waiting serve's record of the lease it waits for.
type WaiterRecord ¶ added in v0.3.1
WaiterRecord is what a serve that waits for the lease leaves in .waiting.<pid> (ADR 0043): its parent Claude process and that process's session id.