capture

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package capture implements the agent-reasoning-capture buffer that the Stop hook appends to after every assistant turn. The buffer is a JSONL file per Claude Code session under .contexo/raw/sessions/_pending/, kept bounded by per-turn truncation and a hard turn cap.

The buffer is read by the ctx_push MCP tool's distill handshake (Phase 1) to produce a structured "source" page. See docs/specs/2026-05-17-agent- reasoning-capture-design.md.

Index

Constants

View Source
const (
	PendingDirRel = "raw/sessions/_pending"
	ArchiveDirRel = "raw/sessions/_pending/_archive"

	MaxAssistantBytes    = 4 * 1024
	MaxUserBytes         = 2 * 1024
	MaxTurns             = 500
	DropOldestOnOverflow = 100
)

File-tree constants. All paths are relative to the .contexo directory.

Variables

This section is empty.

Functions

func PruneOlderThan

func PruneOlderThan(contexoDir string, maxAge time.Duration) (int, error)

PruneOlderThan deletes pending buffer files whose mtime is older than maxAge. Returns the count of removed files.

func TakePendingPrompt added in v0.5.0

func TakePendingPrompt(contexoDir, sessionID string) (string, error)

TakePendingPrompt reads and removes the pending prompt for a session. Returns "" (no error) when there is none.

func WritePendingPrompt added in v0.5.0

func WritePendingPrompt(contexoDir, sessionID, prompt string) error

WritePendingPrompt stores the latest user prompt for a session, overwriting any previous unpaired prompt.

Types

type Buffer

type Buffer struct {
	ContexoDir string
	SessionID  string
}

Buffer is one session's pending capture file.

func List

func List(contexoDir string) ([]*Buffer, error)

List returns all non-archived buffers in the .contexo directory, sorted by modification time descending (most-recent first). Empty list if the pending directory does not exist.

func MostRecent

func MostRecent(contexoDir string, maxAge time.Duration) (*Buffer, error)

MostRecent returns the most-recently-modified buffer whose mtime is within maxAge of now. Returns nil (no error) if none qualifies.

func Open

func Open(contexoDir, sessionID string) *Buffer

Open returns a Buffer handle for the given session. Does not create the file; AppendTurn does that on first write.

func (*Buffer) AppendTurn

func (b *Buffer) AppendTurn(rec TurnRecord) error

AppendTurn writes one record. Truncates oversized fields, dedupes by turn index against existing records, and inserts a marker line when the buffer would exceed MaxTurns. If rec.Turn is zero or negative, it is auto-assigned to one past the current last turn.

func (*Buffer) Archive

func (b *Buffer) Archive() error

Archive moves the buffer file from _pending/ to _pending/_archive/. No-op if the file does not exist.

func (*Buffer) Exists

func (b *Buffer) Exists() bool

Exists reports whether the buffer's JSONL file is present on disk.

func (*Buffer) Path

func (b *Buffer) Path() string

Path returns the absolute path to the buffer's JSONL file.

func (*Buffer) Records

func (b *Buffer) Records() ([]TurnRecord, error)

Records returns all turn records in append order. Returns nil if the buffer file does not exist.

type Exchange

type Exchange struct {
	User      string
	Assistant string
	Tools     []string
}

Exchange is one user→assistant exchange extracted from a Claude Code transcript file.

func LatestExchange

func LatestExchange(path string) (Exchange, error)

LatestExchange returns the most recent (user, assistant, tools) tuple from the JSONL transcript at path. Returns an empty Exchange (no error) if the transcript has no assistant turns yet.

type TruncationTag

type TruncationTag struct {
	Dropped int    `json:"dropped"`
	Reason  string `json:"reason"`
}

TruncationTag marks a marker line inserted when older turns are dropped to keep the buffer under MaxTurns.

type TurnRecord

type TurnRecord struct {
	Timestamp string         `json:"ts,omitempty"`
	Turn      int            `json:"turn"`
	User      string         `json:"user,omitempty"`
	Assistant string         `json:"assistant,omitempty"`
	Tools     []string       `json:"tools,omitempty"`
	Truncated *TruncationTag `json:"truncated,omitempty"`
}

TurnRecord is one JSONL line in the buffer.

Jump to

Keyboard shortcuts

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