effect

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 5 Imported by: ? 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 effect turns a workspace change set into typed, causally-linked trace records plus live bus events. It is the Go analogue of the Shepherd reference's OverlayEffectExtractor.

What it is, and what it deliberately is not

The Python reference extracts *reversible* effects: FileCreate carries the file's content, FilePatch carries old and new content, FileDelete carries what was there, and each has a reverse() so an effect can be undone. That works because Python walks the overlay upper layer itself and reads every file.

This package does not do that, and cannot from the input it is given. A containerd-backed change set is metadata — path, kind, mode, size — because the daemon has already classified the difference between two snapshots. Reading content back for every changed path would mean a round trip per file through the container boundary, and for a large change set that is a materially different operation from the one this package performs.

So these effects are **observations**, not reversible actions: a record that a path was created, patched or deleted, with enough metadata to reason about it, and no capacity to undo it. Plan 05 called this "parity"; it is not, and the distinction matters to anyone expecting to build rollback on top. Rollback in the Go stack is the workspace-state layer's job — Capture and Apply on a sandbox — not the effect layer's.

Why the input type is this package's own

FromChanges takes []Change, defined here, rather than the change type of any particular backend. That is not abstraction for its own sake: this package lives in the runtime core, whose go.mod is deliberately free of any container or OCI dependency. Taking a containerd-backed type in the signature would pull containerd v2 and the whole OCI tree into the core, destroying the reason the container backend is a nested module at all. Backends convert into Change; the sandbox/croft adapter's EffectChanges will do exactly that, once the core is released with this package and the nested module can pin it.

It also keeps the vocabulary reusable. The copy and clonefile carriers planned for later phases produce changes too, and none of them should have to route through a containerd type to emit an effect.

Index

Constants

View Source
const (
	SchemaFileCreated = "shepherd2.effect.file.created.v1"
	SchemaFilePatched = "shepherd2.effect.file.patched.v1"
	SchemaFileDeleted = "shepherd2.effect.file.deleted.v1"
)

Schema refs for the three file effects.

They use the kernel's shepherd2. prefix, matching every existing ref (shepherd2.execution.created.v1, shepherd2.runtime.published_fact.v1, ...). The kernel's ValidateSchemaRef checks only non-emptiness, so introducing these needs no kernel ABI change — but the prefix is a convention worth keeping, since a ref that does not match its siblings is a ref nobody can find by pattern.

View Source
const (
	LabelFileCreated = "file:created"
	LabelFilePatched = "file:patched"
	LabelFileDeleted = "file:deleted"
)

Kind labels, the human-readable counterpart to the schema refs. Colon-separated like the kernel's existing labels ("turn:started").

Variables

This section is empty.

Functions

func Emit

func Emit(ctx context.Context, appender FactAppender, bus *trace.EffectBus, opID string, effects []ChangeEffect) error

Emit records each effect as a trace fact and publishes it on the bus for live supervision.

The two halves answer different questions, and the kernel's split between them is deliberate: the trace store is the durable record for later analysis, the bus is transient for real-time observation. A supervisor that misses an event because its buffer filled can still read the fact from the store, so Emit appends first and publishes only once the record exists — an event must never point at a record that was not written.

opID is the operation the effects are attributed to: the run or tool call that caused them. It becomes the payload's caused_by and the bus event's sole causal parent, which is what makes "why did this file change" answerable. An empty opID is allowed and means no known cause; it is then omitted from the payload and leaves CausalParents empty rather than recording a blank parent.

A nil bus appends without publishing, which is the right mode for a batch or test run with no live supervisor attached.

The appender must be non-nil, and "non-nil" means a real value: because FactAppender is an interface, a typed nil (an unassigned *goatherd.Scope passed as the parameter) satisfies the nil check below and then panics when its method is called. Detecting that would need reflection over every possible underlying kind, which is not a trade worth making for a programming error — so callers own it, as they do for every interface parameter in Go.

Emission is not atomic: each effect is its own append, so a failure part way through leaves the earlier effects recorded and returns the error. That is preferable to the alternatives — swallowing the failure would leave a silent hole in the trace, and there is no multi-fact append on this seam. The error names the path that failed so a caller can tell what was and was not recorded.

func UnixOctal

func UnixOctal(m fs.FileMode) string

UnixOctal renders a mode the way `stat -c %a` would, including the special bits — "4755" for a setuid executable, not "0755".

It is the inverse of the parsing a sandbox backend does when it shells out to stat, and it is deliberately lossless where a host-side write is not: an effect is an observation, so recording the setuid bit is information, whereas propagating it onto a host file would be a privilege decision. Keeping the full mode here means the trace can answer "what was this file's mode" even for bits that never cross back to the host.

Types

type Change

type Change struct {
	// Path is the changed path, workspace-relative and slash-separated,
	// matching what a sandbox Diff reports.
	Path string
	// Kind classifies the change.
	Kind Kind
	// IsDir marks a directory entry.
	IsDir bool
	// Mode is the entry's mode at the comparison point, including any
	// setuid/setgid/sticky bits the backend reports.
	Mode fs.FileMode
	// Size is the entry's size in bytes. Meaningless for a delete, where the
	// backend typically reports zero.
	Size int64
}

Change is one classified difference between a workspace and a comparison point, in backend-neutral terms.

It carries metadata only. See the package doc for why that is a deliberate limit rather than an omission.

type ChangeEffect

type ChangeEffect struct {
	// SchemaRef is the record schema, one of the SchemaFile* constants.
	SchemaRef string
	// KindLabel is the human-readable label, one of the LabelFile* constants.
	KindLabel string
	// Path, Mode and Size are carried through from the Change.
	Path string
	Mode fs.FileMode
	Size int64
}

ChangeEffect is a Change resolved to the schema ref and label it will be recorded under, ready for Emit.

Splitting this from Change means the mapping is decided once, here, and a caller cannot append an effect under the wrong schema by hand.

There is deliberately no IsDir field. FromChanges omits directories, so such a field could only ever be false, and a payload key that is constant across every record advertises information the pipeline structurally cannot produce — a consumer would be entitled to read is_dir:false as positive evidence that the path is a file. If directory effects are ever added, the field comes back with a kind that can actually be true.

func FromChanges

func FromChanges(changes []Change) ([]ChangeEffect, error)

FromChanges maps a change set onto typed effects.

Directories are skipped, matching the Python reference, which notes that a directory creation is "usually implicit" and defers DirectoryCreate effects. A directory carries no content to observe, and its existence is already implied by any child effect beneath it. The sandbox Diff path list drops directories for the same reason, so the two views of a change set agree.

An unrecognised Kind is an error rather than a skipped entry. Silently dropping a change a backend reported would mean a workspace mutation goes unrecorded and nothing says so — the failure mode this package exists to prevent. It also means a backend gaining a new kind (a rename, say) surfaces as a compile-or-test failure here instead of a quiet hole in the trace, and adding the kind becomes a deliberate act that includes choosing its schema.

func (ChangeEffect) Payload

func (e ChangeEffect) Payload(causedBy string) map[string]any

Payload renders an effect as a trace record payload.

causedBy is the operation the effect is attributed to — the tool call or run that produced it — and is omitted when empty rather than recorded as an empty string, so a consumer can distinguish "no known cause" from "the cause was the empty string".

There is no is_dir key: FromChanges omits directories, so every record this produces is a file, and a key that is constant across all of them is noise at best and misleading at worst.

type FactAppender

type FactAppender interface {
	// AppendFact records one capture fact and returns the receipt.
	AppendFact(ctx context.Context, schemaRef, kindLabel string, payload map[string]any) (trace.AppendReceipt, error)
	// OwnerID is the trace owner path the fact is appended under, and the
	// TraceOwnerID published on the bus so a supervisor can attribute the event.
	OwnerID() string
}

FactAppender is the seam Emit needs: somewhere to append a capture record, and the owner path it landed on.

It is an interface rather than the concrete *goatherd.Scope for two reasons. It keeps this package from importing the runtime root, so effect stays a leaf that anything can depend on without a cycle. And it makes Emit testable against a recorder instead of a SQLite store. *goatherd.Scope satisfies it with the methods it already exports.

type Kind

type Kind string

Kind classifies a workspace change. The values are strings so they read meaningfully in a trace payload and in an error message, rather than as integers whose meaning lives in a const block nobody scrolls back to.

const (
	// KindCreated is a path that did not exist at the comparison point.
	KindCreated Kind = "created"
	// KindPatched is a path that existed and changed — content, mode, or both.
	KindPatched Kind = "patched"
	// KindDeleted is a path that existed at the comparison point and does not
	// now. A container backend resolves its own whiteouts, so a delete arrives
	// classified rather than as an opaque marker file.
	KindDeleted Kind = "deleted"
)

func (Kind) KindLabel

func (k Kind) KindLabel() (string, error)

KindLabel returns the human-readable label for a kind.

func (Kind) SchemaRef

func (k Kind) SchemaRef() (string, error)

SchemaRef returns the record schema for a kind.

Jump to

Keyboard shortcuts

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