spool

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 12 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 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

View Source
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 Dir

func Dir(stateDir, owner, name string) string

Dir is the spool of endpoint name of owner under stateDir (config.Config.StateDir).

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.

func (Entry) Ref added in v0.4.0

func (e Entry) Ref() Ref

Ref is the entry's identity.

type Ref added in v0.4.0

type Ref struct {
	Stream string
	Seq    uint64
}

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.

func (Ref) Less added in v0.4.0

func (r Ref) Less(o Ref) bool

Less orders refs: primary stream first, then by stream name, then by sequence.

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

func (s Spool) Acked(seq Ref) error

Acked forgets seq once serve has acked it: its tombstone, its shown mark and any waiting file (a redelivery spooled again).

func (Spool) Alive

func (s Spool) Alive() error

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

func (s Spool) AliveSince(t time.Time) bool

AliveSince reports whether a subagent's hook has run at or after t.

func (Spool) Bind

func (s Spool) Bind(stream string) error

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

func (s Spool) BindStream(name, ident string) error

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

func (s Spool) ClearStaleWaitState() error

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

func (s Spool) ClearWaitState(servePID int, instance string) error

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

func (s Spool) ClearWaiter(pid int) error

ClearWaiter removes the waiting record of pid, and every other .waiting.* that names it. Call it under Lock.

func (Spool) ClearWaiting

func (s Spool) ClearWaiting() error

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

func (s Spool) Discard(seq Ref) error

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

func (s Spool) Drained(seq Ref) error

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

func (s Spool) Has(seq Ref) bool

Has reports whether seq's waiting file is present and parses (what Waiting would list).

func (Spool) HookState added in v0.5.6

func (s Spool) HookState() ([]byte, bool)

HookState is what NoteHookState recorded, and whether there is one.

func (Spool) HookedSince

func (s Spool) HookedSince(t time.Time) bool

HookedSince reports whether a hook has run at or after t.

func (Spool) Init

func (s Spool) Init() error

Init makes the spool's directories, 0700, and tightens them if they exist.

func (Spool) IsDrained

func (s Spool) IsDrained(seq Ref) bool

IsDrained reports whether a hook has shown seq and serve has not yet acked it.

func (Spool) IsListed added in v0.3.2

func (s Spool) IsListed(seq Ref) bool

IsListed reports whether a hook has listed seq in a digest and nothing has drained it since.

func (Spool) LastHook

func (s Spool) LastHook() (time.Time, bool)

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

func (s Spool) Listed(seq Ref) error

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

func (s Spool) LiveWaitState() (WaitState, bool)

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

func (s Spool) Lock() (unlock func(), err error)

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

func (s Spool) LockContext(ctx context.Context) (unlock func(), err error)

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

func (s Spool) MarkShown(refs ...Ref) error

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

func (s Spool) NoteHookState(b []byte) error

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

func (s Spool) NoteRefused(r Refusal) error

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

func (s Spool) NoteTold(key string) error

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

func (s Spool) NoteWaitNotice(now time.Time) error

NoteWaitNotice records that the notice was printed at now. Call it under Lock.

func (Spool) NoteWaitState added in v0.4.0

func (s Spool) NoteWaitState(w WaitState) error

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

func (s Spool) Own(record string) error

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

func (s Spool) Owner() string

Owner is what a hook matches against: the JSON record in .owner, "" when there is none.

func (Spool) Put

func (s Spool) Put(e Entry) error

Put writes e atomically (a 0600 temporary file, synced, renamed into place). A message put again (a redelivery) replaces its file.

func (Spool) Refused

func (s Spool) Refused() (at time.Time, why string, ok bool)

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

func (s Spool) Told(key string) bool

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) Touch

func (s Spool) Touch() error

Touch records a hook run (the marker's mtime).

func (Spool) WaitNoticeDue added in v0.4.0

func (s Spool) WaitNoticeDue(now time.Time, every time.Duration) bool

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.

func (Spool) Waiting

func (s Spool) Waiting() ([]Entry, error)

Waiting lists the spooled messages, oldest first (the primary stream, then each other stream in name order, each by sequence). An unreadable file is skipped, not fatal: one bad entry must not stop the rest.

func (Spool) Writable added in v0.5.6

func (s Spool) Writable() bool

Writable says whether the folder takes a file now, by trying it: it creates a file of its own name in the folder and removes that exact path. Nothing is recorded about the result; whoever asks tries for itself.

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

type WaiterRecord struct {
	PID     int    `json:"pid"`
	Session string `json:"session,omitempty"`
}

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.

Jump to

Keyboard shortcuts

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