scratch

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 9 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 scratch reclaims throwaway workspaces nobody is using.

Every blank shell and every project-less session gets its own directory under the target's scratch root, and nothing ever removed one. Most are empty the day they are made and stay that way; a few hold work somebody meant to file under a project and never did. The sweep tells those apart, moves the empty ones to a trash it empties later, and leaves anything that looks like work for a person to decide about.

It fails closed: a directory is removed only when every signal could be read and all of them say nothing happened there.

Index

Constants

View Source
const KeepMarker = ".lectern-keep"

KeepMarker is the file that exempts a directory from the sweep for good.

View Source
const RootExpr = `` /* 231-byte string literal not displayed */

RootExpr resolves the scratch root on the target. A target that was set up under the old name still has its workspaces in ~/agentdeck-scratch, and a sweep that looked elsewhere would silently stop covering them; so the old directory is used until a new one exists.

Variables

This section is empty.

Functions

func HistoryMentions

func HistoryMentions(ctx context.Context, run Runner, paths []string) (map[string]bool, error)

HistoryMentions searches Codex's recorded conversations for each path. Claude files its history by directory, which Inspect already measured; Codex files by date, so finding a directory means reading them. A search that errors or times out reports a hit: not knowing is not the same as nothing being there.

func MarkKeep

func MarkKeep(ctx context.Context, run Runner, name string) error

MarkKeep exempts a directory from the sweep for good.

func Purge

func Purge(ctx context.Context, run Runner, days float64, now int64) (int, error)

Purge deletes trash entries older than days. Only names this package stamped are eligible, so nothing else that finds its way into the trash is removed.

func SafeName

func SafeName(name string) bool

SafeName reports whether a directory name may be handed to a shell command. Scratch names come from mktemp over a slug, so anything else is not ours.

func Trash

func Trash(ctx context.Context, run Runner, names []string, now int64) ([]string, error)

Trash moves directories out of the scratch root into the trash, stamped with the time so Purge knows how long each has been there. It returns the names that actually moved.

Types

type Dir

type Dir struct {
	Name        string  `json:"name"`
	Path        string  `json:"path"`
	Mtime       float64 `json:"mtime"`
	Files       int     `json:"files"`
	Commits     int     `json:"commits"`
	ClaudeBytes int64   `json:"claude_bytes"`
	SizeKB      int64   `json:"size_kb"`
	Keep        bool    `json:"keep"`
	// Unreadable is set when any signal could not be measured. Such a directory
	// is treated as holding work, whatever the other signals say.
	Unreadable bool `json:"unreadable,omitempty"`
}

Dir is what the target reported about one scratch directory.

func Inspect

func Inspect(ctx context.Context, run Runner) (root string, dirs []Dir, err error)

Inspect asks the target what is in its scratch root.

type Entry

type Entry struct {
	Dir
	TargetID   int64     `json:"target_id"`
	TargetName string    `json:"target_name"`
	Sessions   []Session `json:"sessions"`
	AgeDays    float64   `json:"age_days"`
	Verdict    Verdict   `json:"verdict"`
	Reasons    []string  `json:"reasons"`
	// NeedsHistoryCheck marks an otherwise-removable directory whose agent
	// history has not been searched yet. That search reads every recorded
	// conversation, so it is only run for directories that pass everything else.
	NeedsHistoryCheck bool `json:"-"`
}

Entry is one directory with its verdict and the evidence for it.

func Classify

func Classify(d Dir, sessions []Session, now float64, days float64, historyHit *bool) Entry

Classify decides one directory from what the target and the database say. historyHit is whether an agent's recorded conversations mention the path; pass nil when that has not been checked.

type Result

type Result struct {
	DryRun  bool           `json:"dry_run"`
	Days    float64        `json:"days"`
	Targets []TargetReport `json:"targets"`
	Trashed []string       `json:"trashed"`
	Purged  int            `json:"purged"`
}

Result is what a sweep did, or with DryRun what it would do.

type Runner

type Runner func(ctx context.Context, script string) (string, error)

Runner executes a shell script on one target and returns its stdout.

type Session

type Session struct {
	ID        int64   `json:"id"`
	Name      string  `json:"name"`
	Agent     string  `json:"agent"`
	Workdir   string  `json:"-"`
	Project   bool    `json:"-"`
	Live      bool    `json:"-"`
	LastSeen  float64 `json:"-"`
	NativeID  bool    `json:"-"` // the agent recorded a conversation identity
	CanResume bool    `json:"can_resume"`
}

Session is the part of a session row the decision needs.

type Sweeper

type Sweeper struct {
	DB        *store.DB
	Reg       *executor.Registry
	Log       *slog.Logger
	Days      float64 // idle days before an empty directory is removed
	TrashDays float64 // days a removed directory stays recoverable
}

Sweeper applies the sweep across every target that has hosted a session.

func (*Sweeper) Discard

func (w *Sweeper) Discard(ctx context.Context, targetID int64, name string) error

Discard trashes a directory a person has decided against. It is recoverable until the purge, and refused outright while a session or a project owns it.

func (*Sweeper) Keep

func (w *Sweeper) Keep(ctx context.Context, targetID int64, name string) error

Keep exempts a directory from the sweep for good.

func (*Sweeper) Sweep

func (w *Sweeper) Sweep(ctx context.Context, dryRun bool, onlyTarget int64) (*Result, error)

Sweep inspects every target that has hosted a session and, unless dryRun, trashes what is empty and old and purges what has sat in the trash too long. onlyTarget narrows it to one target; zero means all.

type TargetReport

type TargetReport struct {
	TargetID   int64   `json:"target_id"`
	TargetName string  `json:"target_name"`
	Root       string  `json:"root"`
	Entries    []Entry `json:"entries"`
	Error      string  `json:"error,omitempty"`
}

TargetReport is one target's scratch root, or why it could not be read.

type Verdict

type Verdict string

Verdict is what the sweep concluded about one directory.

const (
	Live    Verdict = "live"    // a session is running in it
	Project Verdict = "project" // a session there belongs to a project
	Kept    Verdict = "kept"    // a person marked it to keep
	Work    Verdict = "work"    // something happened there; a person decides
	Recent  Verdict = "recent"  // empty, but not yet old enough to remove
	Empty   Verdict = "empty"   // empty and old: the sweep removes it
)

Jump to

Keyboard shortcuts

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