Skip to main content

Module pending_queue

Module pending_queue 

Source
Expand description

The offline queue of a session: pending-<target>.jsonl in its session directory, one StreamEventRequest per line, holding what a hook could not send.

Every transcript line is in exactly one place: sent, in the queue, or still unread in the agent’s transcript file. A hook that cannot send lines it read from a file queues them and moves the file’s read offset past them, so the next hook does not read them again. Earlier releases left the offset where it was and queued the lines, so every failed hook added the whole unsent end of the transcript to the queue once more, and the queue (read whole by every hook) grew to gigabytes for a transcript of a few megabytes.

A queue is bounded (MAX_QUEUE_BYTES) and read one record at a time, so a hook’s memory does not depend on how long the server has been unreachable. Lines that do not fit stay unread in the transcript file, which is as good a place: the offset stops there until the queue drains.

A session directory whose queues an earlier release wrote is brought up to this once (upgrade): there the lines of a transcript file are both in the queue, many times, and still ahead of the read offset, so the queue’s copies are dropped.

Structs§

Claimed
A queue taken out of its place to be read: the file is renamed, so a hook that queues meanwhile starts a new one, and what the reader does not send it appends there. The claimed files are removed by Claimed::release; a reader that dies first leaves them to be taken over (after [ABANDONED_AFTER]) instead of losing what it held in memory.
Position
Where a Reader is in a claim: a record’s first byte.
Reader
Reads a claim’s records in order and can tell where the last one it returned began, which is where a reader that could not send it stops: Claimed::finish keeps what is from there on.

Constants§

MAX_QUEUE_BYTES
The most a queue file may hold. Transcript lines that do not fit are left in the transcript; an event (or a record an agent handed over inline) that does not fit can be kept nowhere and is dropped, with a warning. A queue without a bound did that to the machine instead.

Functions§

append
Append one record to the queue. A full queue drops it and says so once per process.
claim
Claim the queue at pending_path, together with claims other readers abandoned (oldest first, as their records are older).
compacted
One line of a queue an earlier release wrote, as it is kept: None for a blank or unreadable line, and for a record that is only lines of a transcript file (they are still ahead of the file’s read offset); an event that carries such lines comes back without them.
lines_are_reread
Whether the transcript lines of a queued record are read again from the agent’s transcript file, going by the tool the record names. An absent tool is Claude Code (protocol v1 predates the field).
lock
Take the exclusive lock lock_path names, waiting for its holder. For the few file operations a hook must not interleave with another’s; the OS drops the lock when its holder exits, even if killed.
queue_of
The queue file a directory entry belongs to: the entry itself when it is a queue, and for a claimed queue (see claim) the file it was claimed from, which may not exist while its records wait in the claim.
requeue
Put a record that was already in the queue back into it, whatever the queue’s size: a reader that claimed a queue and could not send a record must not lose it to the bound, which a hook queueing meanwhile may have used up. It can exceed the bound by no more than what was claimed.
try_append
Append one record to the queue if it fits; false when the queue is full.
upgrade
Bring the queues of session_dir up to “every line in one place”, once: a directory without the mark may hold queues an earlier release wrote, which are [compact]ed. Called before a queue of the directory is read or written. A new directory has no queue and only gets the mark.
without_lines
part without the transcript lines a file still holds: None when it is nothing but lines.