watch

package
v0.167.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package watch implements the --watch filesystem-trigger mode.

A Watcher observes one or more directories via fsnotify, matches new / modified files against a set of rules (glob pattern -> prompt template), and hands the result to a caller-supplied handler. Rapid write bursts against the same path are debounced so an atomic save sequence (truncate + rewrite + rename) doesn't dispatch the handler three times.

The Watcher is intentionally agnostic of the agent loop — callers plug the handler into whatever dispatch mechanism they prefer. promptzero wires it to ai.Run so FS events become natural-language turns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidatePattern added in v0.27.0

func ValidatePattern(pattern string) error

ValidatePattern returns nil when the pattern is well-formed for filepath.Match, or the underlying ErrBadPattern when not. Useful for config-load-time validation so a typo (e.g. `*[a.sub` with unmatched bracket) fails visibly at startup instead of silently suppressing every event the operator expected.

Types

type Event

type Event struct {
	At    time.Time
	Path  string
	Rule  Rule
	Error error
}

Event is a record of one dispatched rule firing. Kept in-memory on the Watcher for the /watch slash command's "last 5 events" view.

type Handler

type Handler func(rule Rule, path string) error

Handler is the callback invoked when a file matches a rule. Returning an error is non-fatal; the Watcher logs and continues processing events.

type Rule

type Rule struct {
	Pattern string
	Prompt  string
	Persona string
}

Rule is a single pattern -> prompt mapping with an optional persona override. Pattern uses filepath.Match semantics against the file basename. Prompt is templated with {{path}}, {{dir}}, {{name}}, {{ext}} when the handler fires.

type Watcher

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

Watcher debounces and dispatches fsnotify events through the configured rule set. Zero value is not usable — construct via New.

func New

func New(paths []string, rules []Rule) *Watcher

New constructs a Watcher from a list of paths and rules. The slices are copied so the caller can mutate the originals without racing the watcher.

func (*Watcher) Paths

func (w *Watcher) Paths() []string

Paths returns the list of watched paths. Copy — callers must not mutate.

func (*Watcher) Pause

func (w *Watcher) Pause()

Pause silences the watcher without stopping it; queued and subsequent events are still observed but handlers are not invoked until Resume.

func (*Watcher) Paused

func (w *Watcher) Paused() bool

Paused reports whether dispatch is currently suppressed.

func (*Watcher) Recent

func (w *Watcher) Recent(n int) []Event

Recent returns up to n most-recent events, newest first. Pass a small number (5-20) — history is bounded by eventHistory anyway.

func (*Watcher) Resume

func (w *Watcher) Resume()

Resume re-enables dispatch after a Pause.

func (*Watcher) Rules

func (w *Watcher) Rules() []Rule

Rules returns the configured rule set. Copy — callers must not mutate.

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context, handler Handler) error

Run blocks until ctx is cancelled, dispatching matching events to handler. Errors opening the underlying fsnotify watcher are returned immediately; runtime errors are logged and the loop continues.

Jump to

Keyboard shortcuts

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