filetrack

package
v0.0.310 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package filetrack records file changes made by agent tools so sessions can expose a cumulative diff (baseline = file state at first touch in a session). Bulky before/after contents live in a dedicated SQLite database as content-addressed compressed blobs, keeping the main sessions DB slim.

Index

Constants

View Source
const (
	DefaultMaxFileBytes    = 2 * 1024 * 1024           // per-file content cap
	DefaultMaxSessionBytes = 100 * 1024 * 1024         // retained-content budget per session
	DefaultMaxTotalBytes   = int64(1024 * 1024 * 1024) // whole-database size cap (across sessions)
)

Default caps, overridable via config.

View Source
const (
	KindCreate = "create"
	KindModify = "modify"
	KindDelete = "delete"
)

Change kinds.

Variables

This section is empty.

Functions

func CountAddsDels

func CountAddsDels(oldContent, newContent []byte) (adds, dels int)

CountAddsDels counts added and removed lines between two contents. Empty/nil sides are treated as a missing file (pure create/delete).

Types

type Change

type Change struct {
	Seq        int64
	Path       string
	Kind       string
	ToolName   string
	ToolCallID string
	BeforeHash string // empty when absent/unknown/not retained
	AfterHash  string
	BeforeSize int64
	AfterSize  int64
	Adds       int
	Dels       int
	Truncated  bool
	IsBinary   bool
}

Change is one recorded change row.

type ChangeRecord

type ChangeRecord struct {
	SessionID  string
	ToolName   string
	ToolCallID string
	Path       string // absolute path

	Before []byte // content before the change (ignored when BeforeMissing/BeforeUnknown)
	After  []byte // content after the change (ignored when AfterMissing/AfterUnknown)

	BeforeMissing bool // file did not exist before the change
	AfterMissing  bool // file does not exist after the change (deletion)
	BeforeUnknown bool // file existed before but its content was not captured
	AfterUnknown  bool // file exists after but its content was not captured (e.g. oversized)

	// Size hints for unknown-content sides (from stat); ignored when the
	// corresponding content is provided.
	BeforeSizeHint int64
	AfterSizeHint  int64
}

ChangeRecord describes one before→after file transition to record.

type CumulativeChange

type CumulativeChange struct {
	Path      string `json:"path"`
	Kind      string `json:"kind"`
	Adds      int    `json:"adds"`
	Dels      int    `json:"dels"`
	Truncated bool   `json:"truncated"`
	Seq       int64  `json:"seq"` // latest change sequence for this path in the session
}

CumulativeChange summarizes a file's net change relative to the session baseline.

type DiffLine

type DiffLine struct {
	T string `json:"t"` // "ctx" | "add" | "del"
	S string `json:"s"` // line text without the diff prefix
}

DiffLine is one row of a structured diff hunk.

type FileDiffContent

type FileDiffContent struct {
	Path      string
	Kind      string
	Before    []byte
	After     []byte
	Truncated bool
}

FileDiffContent holds the baseline and current contents for one file.

type Hunk

type Hunk struct {
	OldStart int        `json:"old_start"`
	NewStart int        `json:"new_start"`
	Lines    []DiffLine `json:"lines"`
}

Hunk is one contiguous block of a structured diff.

func BuildHunks

func BuildHunks(path string, oldContent, newContent []byte) []Hunk

BuildHunks computes a structured diff between two file contents. Returns nil when the contents are identical.

type Options

type Options struct {
	MaxFileBytes    int   // 0 = DefaultMaxFileBytes
	MaxSessionBytes int   // 0 = DefaultMaxSessionBytes
	MaxTotalBytes   int64 // 0 = DefaultMaxTotalBytes; whole-database size cap enforced by GC
}

Options configures a Store.

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder adapts a Store to the tools-facing FileChangeRecorder interface. Recording is best-effort: failures are reported once on stderr and never surface to the calling tool — tracking must not break editing.

func NewRecorder

func NewRecorder(store *Store) *Recorder

NewRecorder wraps a store in a Recorder.

func (*Recorder) MaxFileBytes

func (r *Recorder) MaxFileBytes() int

MaxFileBytes returns the per-file content cap.

func (*Recorder) RecordChange

func (r *Recorder) RecordChange(ctx context.Context, rec ChangeRecord) *llm.FileChange

RecordChange persists one before→after transition. Returns nil when the change is a no-op or recording fails.

func (*Recorder) SessionPaths

func (r *Recorder) SessionPaths(ctx context.Context, sessionID string) []string

SessionPaths returns paths already recorded for the session (best-effort).

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store persists file-change history in a dedicated SQLite database.

func Open

func Open(path string, opts Options) (*Store, error)

Open opens (creating if necessary) the file-change history database at path.

func (*Store) Close

func (s *Store) Close() error

Close closes the database.

func (*Store) GC

func (s *Store) GC(ctx context.Context, sessionsDBPath string, maxAgeDays int) error

GC removes change rows for sessions that no longer exist in the sessions DB (and rows older than maxAgeDays when > 0), then sweeps unreferenced blobs.

func (*Store) GetFileDiffContent

func (s *Store) GetFileDiffContent(ctx context.Context, sessionID, path string) (*FileDiffContent, error)

GetFileDiffContent returns the baseline and current contents for one path in a session, or nil when the path has no net change recorded.

func (*Store) ListSessionChanges

func (s *Store) ListSessionChanges(ctx context.Context, sessionID string) ([]CumulativeChange, error)

ListSessionChanges returns the cumulative per-file changes for a session, sorted by path. Net no-ops are omitted.

func (*Store) MaxFileBytes

func (s *Store) MaxFileBytes() int

MaxFileBytes returns the per-file content cap.

func (*Store) RecordChange

func (s *Store) RecordChange(ctx context.Context, rec ChangeRecord) (*Change, error)

RecordChange records one file transition and returns metadata for event emission. Returns (nil, nil) for no-ops (identical content, missing→missing, or empty session ID).

func (*Store) SessionPaths

func (s *Store) SessionPaths(ctx context.Context, sessionID string) ([]string, error)

SessionPaths returns the distinct absolute paths already recorded for a session.

Jump to

Keyboard shortcuts

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