Documentation
¶
Overview ¶
Package fsdiff snapshots a directory tree and computes/applies/restores content-addressed file diffs (§36). It is the single implementation shared by the internal implementer (which writes files) and the engine (which applies and reverts the diff), so both sides agree on the representation. The diff is always computed by the orchestrator from a before/after snapshot — never trusted from an agent's self-declaration.
Index ¶
- Variables
- func Commit(root string, changes []domain.FileChange, ...) error
- func Diff(before, after Manifest) []domain.FileChange
- func Restore(root string, pre Manifest, get func(digest string) ([]byte, error)) error
- func StaleBase(root string, changes []domain.FileChange) []string
- func StalePost(root string, changes []domain.FileChange) []string
- type Entry
- type Manifest
- type Patch
- type SnapshotOption
- type SnapshotTooLargeError
Constants ¶
This section is empty.
Variables ¶
var DefaultIgnoredDirNames = map[string]bool{ ".git": true, ".hg": true, ".svn": true, ".idea-harness": true, "node_modules": true, "vendor": true, "dist": true, "build": true, "target": true, ".venv": true, "venv": true, "__pycache__": true, ".pytest_cache": true, ".mypy_cache": true, ".ruff_cache": true, ".cache": true, ".tools": true, ".next": true, ".nuxt": true, ".gradle": true, ".tox": true, ".terraform": true, }
DefaultIgnoredDirNames are well-known cache, dependency, and toolchain directories that Snapshot always excludes, regardless of .gitignore or whether the workspace is even a git repository. A downloaded Go toolchain under .tools/, a node_modules/ tree, or a Python virtualenv is never part of a change set an agent should be judged on, and walking one is exactly what let a single agent's write balloon a snapshot to ~14,300 files (review defect #2). This list is deliberately conservative in NAME (not content): a project that genuinely versions a directory with one of these names is unusual enough that documenting the exclusion here is the right tradeoff over silently walking every agent-writable tree's cache dirs by default.
var ErrControlPlanePath = errors.New("fsdiff: path is part of the harness control plane and cannot be changed by a change set")
ErrControlPlanePath is returned when a change set names a path that governs the harness itself.
Functions ¶
func Commit ¶
func Commit(root string, changes []domain.FileChange, get func(digest string) ([]byte, error)) error
Commit writes (and deletes) the changes into root, assuming the pre-image was already captured by Prepare. Every Path is containment-checked against root before any write, and writes are atomic (temp file + rename).
func Diff ¶
func Diff(before, after Manifest) []domain.FileChange
Diff computes the changes from before to after. Added/modified files carry their post-image digest; files present only in before are deletions (empty digest). The result is path-sorted.
func Restore ¶
Restore restores a pre-image manifest. Entries with an empty digest did not exist before and are removed; others are rewritten to their prior content.
func StaleBase ¶ added in v0.5.0
func StaleBase(root string, changes []domain.FileChange) []string
StaleBase reports every change whose base no longer matches root, one line per file. An empty result means the patch can be applied without overwriting anything the change was not made against.
Keyed on ChangeType, not on BaseDigest being empty: an all-adds patch has no base digests at all and must still be checked, because "add a file" over a file that now exists is exactly how an agent that never saw the workspace deleted a user's code. A change with no ChangeType predates this check and is applied as before.
func StalePost ¶ added in v0.6.0
func StalePost(root string, changes []domain.FileChange) []string
StalePost reports every change whose post-image no longer matches root, one line per file. An empty result means a revert can restore the pre-image without erasing anything written into the workspace after apply.
This is StaleBase's mirror at the opposite end of a change set's life: StaleBase protects an edit made BEFORE apply ran by comparing the workspace against each change's BaseDigest; StalePost protects an edit made AFTER apply ran by comparing the workspace against each change's Digest -- the post-image apply itself wrote. Both refuse rather than overwrite, and both write nothing when they refuse (review A3: an ICP user's comment added to src/app.js after apply was silently erased by a revert that never looked).
Keyed on ChangeType, like StaleBase: an add has nothing to compare on the "base" side but everything on the "post" side, and a delete is the reverse. A change with no ChangeType predates this check and is restored as before.
Types ¶
type Entry ¶
Entry is one file in a snapshot manifest: a workspace-relative path and the SHA-256 of its content.
type Manifest ¶
type Manifest []Entry
Manifest is a path-sorted snapshot of a directory's regular files.
func Apply ¶
func Apply(root string, changes []domain.FileChange, get func(digest string) ([]byte, error), put func(content []byte) (string, error)) (Manifest, error)
Apply writes changes into root and returns the pre-image manifest of every touched path (for revert). It is the two-phase Prepare+Commit composed for callers that do not need the crash-safety window between them.
func Prepare ¶
func Prepare(root string, changes []domain.FileChange, put func(content []byte) (string, error)) (Manifest, error)
Prepare captures the pre-image of every touched path (persisting each via put) WITHOUT mutating root. It returns the pre-image manifest so a caller can persist it BEFORE any byte is written, making an interrupted apply recoverable (review: full-platform apply atomicity).
func Snapshot ¶
func Snapshot(root string, opts ...SnapshotOption) (Manifest, error)
Snapshot walks root and returns a path-sorted manifest of its regular files (content-addressed). Directories, symlinks, and non-regular files are ignored. Symlinks are rejected via Lstat (not followed) so an untrusted executor cannot plant a symlink to a host file and induce the daemon to read it (review: v1 security boundary). A missing root yields an empty manifest.
DefaultIgnoredDirNames (node_modules, .git, .tools, vendor, and other well-known caches/toolchains) are always excluded, on top of the harness's own control-plane paths, regardless of any option passed. WithWorkspaceIgnore and WithSanityCap add the workspace's own .gitignore and a hard file/byte ceiling on top of that (review defect #2).
type Patch ¶
type Patch struct {
BaseSnapshotDigest string `json:"base_snapshot_digest,omitempty"`
Files []domain.FileChange `json:"files"`
}
Patch is the content-addressed diff manifest persisted as a change set's patch. BaseSnapshotDigest references the pre-image snapshot; Files is the ordered change list.
type SnapshotOption ¶ added in v0.6.0
type SnapshotOption func(*snapshotConfig)
SnapshotOption configures Snapshot beyond its always-on defaults (control plane paths and DefaultIgnoredDirNames are excluded unconditionally).
func WithSanityCap ¶ added in v0.6.0
func WithSanityCap(maxFiles int, maxBytes int64) SnapshotOption
WithSanityCap fails Snapshot with a *SnapshotTooLargeError once the walked tree exceeds maxFiles files or maxBytes of total content, whichever comes first (0 disables that particular bound). This is what turns "an agent wrote a downloaded toolchain into its work root" into a clear, immediate failure instead of a secret sweep with 79 false findings and a diff endpoint that never responds (review defect #2).
func WithWorkspaceIgnore ¶ added in v0.6.0
func WithWorkspaceIgnore(workspaceRoot string) SnapshotOption
WithWorkspaceIgnore additionally excludes any path the WORKSPACE's own .gitignore would ignore. root (the directory Snapshot walks) is typically a fresh work-root copy with no .git of its own, so the ignore rules are read from workspaceRoot -- the canonical checkout -- and applied to root's workspace-relative paths, which mirror the same layout.
When workspaceRoot is a git repository, `git check-ignore` is used (one batched subprocess call for every candidate path, not one per file -- review defect #2 was 14,300 files). Otherwise a best-effort matcher reads workspaceRoot's own top-level .gitignore; it covers the common case (glob patterns, negation, anchoring, directory-only patterns) but not every git gitignore rule (e.g. per-directory nested .gitignore files), which is a documented limitation rather than a silent gap -- DefaultIgnoredDirNames and WithSanityCap still bound a non-git workspace regardless.
type SnapshotTooLargeError ¶ added in v0.6.0
type SnapshotTooLargeError struct {
Root string
FileCount int
TotalBytes int64
MaxFiles int
MaxBytes int64
Examples []string
}
SnapshotTooLargeError is returned by Snapshot when WithSanityCap's bound is exceeded. It carries enough detail for a human to act on: how many files (or bytes), and a few example paths so "add them to .gitignore" is actionable rather than a guess.
func (*SnapshotTooLargeError) Error ¶ added in v0.6.0
func (e *SnapshotTooLargeError) Error() string