spool

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 11 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: <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).

func LegacyToken

func LegacyToken(record string) string

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

func (s Spool) Acked(seq uint64) 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 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

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

func (s Spool) Drained(seq uint64) 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 uint64) bool

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

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 uint64) 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 uint64) 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 uint64) 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) 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) MarkShown

func (s Spool) MarkShown(seqs ...uint64) 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) 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) 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(legacy string) error

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

func (s Spool) OwnRecord(legacy, record string) error

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

func (s Spool) Owner() string

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

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) 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. An unreadable file is skipped, not fatal: one bad entry must not stop the rest.

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