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: <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
- func LegacyToken(record string) string
- type Entry
- type Refusal
- type Spool
- func (s Spool) Acked(seq uint64) error
- func (s Spool) Alive() error
- func (s Spool) AliveSince(t time.Time) bool
- func (s Spool) Bind(stream string) error
- func (s Spool) ClearWaiter(pid int) error
- func (s Spool) ClearWaiting() error
- func (s Spool) Drained(seq uint64) error
- func (s Spool) Has(seq uint64) bool
- func (s Spool) HookedSince(t time.Time) bool
- func (s Spool) Init() error
- func (s Spool) IsDrained(seq uint64) bool
- func (s Spool) IsListed(seq uint64) bool
- func (s Spool) LastHook() (time.Time, bool)
- func (s Spool) Listed(seq uint64) error
- func (s Spool) Lock() (unlock func(), err error)
- func (s Spool) MarkShown(seqs ...uint64) error
- func (s Spool) NoteRefused(r Refusal) error
- func (s Spool) NoteTold(key string) error
- func (s Spool) NoteWaiter(r WaiterRecord) error
- func (s Spool) Own(legacy string) error
- func (s Spool) OwnRecord(legacy, 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) Waiters() []WaiterRecord
- func (s Spool) Waiting() ([]Entry, error)
- 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 ¶
func LegacyToken ¶
LegacyToken is the "pid:N" text an older hook understands, for a record: N is the record's pid, else this process's parent (serve's parent, the Claude process). The one place that text is made.
Types ¶
type Entry ¶
type Entry struct {
Seq uint64 `json:"seq"`
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
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 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 to stream, the identity of the inbox stream. Sequences restart at 1 when the stream is recreated (deprovision, then provision), so a tombstone or waiting file left by the old stream would match new mail: Bind drops them all when stream differs from the one recorded, and records it. Equal, it changes nothing (codex H1 on #21, round 2). Call it before anything is spooled.
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) 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) 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) 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) 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) 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) 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 token of the serve that holds the lease, so a hook of another session with the same endpoint name leaves it alone (codex H1 on #21, round 3). It writes the legacy .session text and removes any .owner record; "" removes the binding. Atomic: a hook never reads a half-written id.
func (Spool) OwnRecord ¶
OwnRecord is Own with the identity record (JSON {pid, session, launch}, ADR 0040) in .owner. .session keeps the legacy "pid:N" text, so a hook built before the record existed still enforces ancestry instead of reading an unknown owner as "accept all". The record is written first, then .session; each write is atomic and the caller holds the spool lock. An empty record removes .owner.
func (Spool) Owner ¶
Owner is what a hook matches against: the JSON record in .owner when there is one, else the legacy .session text, "" when neither exists (an older serve, or 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) 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 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.