Documentation
¶
Overview ¶
Package filesync keeps one machine's folder in step with a collection of the store (internal/filestore): local edits go up with the revision they were made on, other machines' edits come down and land atomically, and an edit made on a stale copy is kept (revision.go) instead of being lost. It is the agent of design-sync, carried into harnessly: a runtime runs one for its Sinopia library.
Index ¶
- Constants
- Variables
- func BoardOf(rel string) (project, board string, ok bool)
- func ConflictName(rel, machine string, at time.Time) string
- func SecretEnv(name string, value any) bool
- func SmallEdit(a, b []byte) bool
- func VariantID(project, machine string, at time.Time) string
- type Agent
- func (a *Agent) Apply(ctx context.Context, e filestore.Entry) error
- func (a *Agent) Close()
- func (a *Agent) Pull(ctx context.Context, wait time.Duration) (bool, error)
- func (a *Agent) Quarantined() ([]Quarantined, error)
- func (a *Agent) RetryQuarantine(ctx context.Context) error
- func (a *Agent) Run(ctx context.Context) error
- func (a *Agent) Scan(ctx context.Context, rel string) error
- func (a *Agent) ScanAll(ctx context.Context) error
- func (a *Agent) SyncOnce(ctx context.Context) error
- type Client
- func (c *Client) Changes(ctx context.Context, since int64, wait time.Duration) (filestore.Changes, error)
- func (c *Client) Collection() string
- func (c *Client) Commit(ctx context.Context, req filestore.CommitRequest) (filestore.CommitResponse, error)
- func (c *Client) Current(ctx context.Context, path string) (*filestore.Entry, error)
- func (c *Client) GetBlob(ctx context.Context, sha string, w io.Writer) error
- func (c *Client) HasBlob(ctx context.Context, sha string) (bool, error)
- func (c *Client) History(ctx context.Context, path string) ([]filestore.Entry, error)
- func (c *Client) HistoryUnder(ctx context.Context, prefix string, limit int) ([]filestore.Entry, error)
- func (c *Client) Keys(ctx context.Context) ([]filestore.WrappedKey, error)
- func (c *Client) MissingBlobs(ctx context.Context, shas []string) (map[string]bool, error)
- func (c *Client) Put(ctx context.Context, path string, data []byte, author string) (filestore.Entry, error)
- func (c *Client) PutBlob(ctx context.Context, sha string, data []byte) error
- func (c *Client) PutLarge(ctx context.Context, sha string, size int64, r io.ReaderAt) error
- func (c *Client) Snapshot(ctx context.Context, prefix string, at int64) (filestore.Snapshot, error)
- func (c *Client) TakeLease(ctx context.Context, req filestore.LeaseRequest) (filestore.LeaseResponse, error)
- func (c *Client) UploadLimits(ctx context.Context) (filestore.UploadLimits, error)
- type Codec
- type Config
- type Notice
- type Plain
- func (Plain) Canonical(_ string, b []byte) []byte
- func (Plain) Local(_ string, c, _ []byte) []byte
- func (Plain) Merge(string, []byte, []byte, []byte, bool) ([]byte, bool)
- func (Plain) Open(_ string, b []byte) ([]byte, error)
- func (Plain) Ready() bool
- func (Plain) Seal(_ string, c []byte) ([]byte, error)
- func (Plain) Tag(c []byte) string
- type ProfileCodec
- func (p *ProfileCodec) Canonical(rel string, local []byte) []byte
- func (p *ProfileCodec) Local(rel string, canonical, current []byte) []byte
- func (p *ProfileCodec) Merge(rel string, base, local, remote []byte, localNewer bool) ([]byte, bool)
- func (p *ProfileCodec) Mergeable(rel string) bool
- func (p *ProfileCodec) Open(rel string, b []byte) ([]byte, error)
- func (p *ProfileCodec) Ready() bool
- func (p *ProfileCodec) Seal(rel string, c []byte) ([]byte, error)
- func (p *ProfileCodec) SetKey(k []byte)
- func (p *ProfileCodec) Tag(c []byte) string
- type Quarantined
- type Token
- type VariantLink
Constants ¶
const ( KindKept = "kept" // a file that is not a board: the losing contents are kept KindReapply = "reapply" // a small edit of a board: re-apply it on the fresh version KindVariant = "variant" // a bigger edit of a board: saved as the project Variant )
Notice kinds.
const KeptDir = ".conflicts"
KeptDir is where a losing edit is kept, relative to the folder's root; hidden, so it is never synced.
const RoomKeySize = chacha20poly1305.KeySize
RoomKeySize is the size of a profile room's key.
const VariantFile = "variant.json"
VariantFile is the link record of a variant project, at its root.
Variables ¶
var DefaultLocalKeys = []string{"hooks", "statusLine", "apiKeyHelper", "awsAuthRefresh", "awsCredentialExport",
"env.CCGUI_CLI_LOGIN_AUTHORIZED"}
DefaultLocalKeys: hooks and the status line call this machine's integrations, the API helpers are this machine's logins. A deployment adds the env variables that hold its own paths (RUNTIME_PROFILE_LOCAL_KEYS).
var ErrNoBlob = errors.New("the store does not hold these contents")
ErrNoBlob is contents the collection does not hold.
var ErrRefused = errors.New("the store refuses this machine's write")
ErrRefused is a write the store does not take from this machine: the path is in a project none of the machine's people may edit.
var ErrTooBig = errors.New("the store does not take contents this big")
ErrTooBig is contents bigger than the store takes: sending them again does not help.
ErrUnauthorized is a store that refused this machine even with a fresh token.
Functions ¶
func ConflictName ¶
ConflictName is where a conflicting local edit of rel is kept: `Main (конфликт vm-b 2026-10-18 2141).dc.html`, a board of its own in the same project, so the person and the agents see it.
func SecretEnv ¶
SecretEnv tells whether a settings env variable stays on the machine as a secret, by its name or its value.
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent syncs one folder.
func (*Agent) Pull ¶
Pull asks the hub for everything after the agent's cursor and applies it; with wait, it holds until something changes or the wait passes. It returns whether the hub had more.
func (*Agent) Quarantined ¶
func (a *Agent) Quarantined() ([]Quarantined, error)
Quarantined lists the files set aside because the agent had no rights on them.
func (*Agent) RetryQuarantine ¶
RetryQuarantine tries the files in quarantine again once their rights may be back: it reads the hub's history from the oldest revision set aside and applies the latest one of each such file. A file still without rights stays.
func (*Agent) Run ¶
Run syncs until ctx ends: a first full sync, then the watcher, the long poll and a periodic rescan.
The watcher is on the folder before the first sync reads it: an edit made while the first sync runs is then either read by it or told by the watcher, and not left for the rescan two minutes later. A system that gives no watcher (every inotify instance of the user is taken: 128 by default, and a machine runs many runtimes) does not stop the sync: Run used to return the error, the caller started it again ten seconds later with one more full first sync, and nothing arrived from the store in between. The folder is walked every watchlessScan instead until a watcher can be made.
func (*Agent) Scan ¶
Scan looks at one path (a file, a folder or something gone) and sends what changed to the hub.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client speaks the store's API for one collection: its base is the store's root (`https://hub.example.org/store`, or anything an in-process transport answers), the collection one of filestore.Kinds.
func NewClient ¶
NewClient makes a client of a collection. hc nil is a plain client with a timeout long enough for a long poll.
func (*Client) Changes ¶
func (c *Client) Changes(ctx context.Context, since int64, wait time.Duration) (filestore.Changes, error)
Changes is every file changed after since; with wait, the store holds the request until something changes.
func (*Client) Collection ¶
Collection is the client's collection kind.
func (*Client) Commit ¶
func (c *Client) Commit(ctx context.Context, req filestore.CommitRequest) (filestore.CommitResponse, error)
Commit asks for a new revision; a stale base answers Conflict with the current revision.
func (*Client) GetBlob ¶
GetBlob writes the contents with this sha256 to w; ErrNoBlob when the collection does not hold them.
func (*Client) History ¶
History is the revisions of a path, newest first; empty when the path never existed or is not this machine's to see.
func (*Client) HistoryUnder ¶
func (c *Client) HistoryUnder(ctx context.Context, prefix string, limit int) ([]filestore.Entry, error)
HistoryUnder is the newest revisions of the paths under a folder (prefix ends in "/"; "" for the whole collection).
func (*Client) Keys ¶
Keys is the room key of a profile room as wrapped for each member that has it.
func (*Client) MissingBlobs ¶
MissingBlobs tells which of the named contents the collection does not hold: one request for every filestore.MaxBlobCheck of them. A store that does not know the batch look is asked about each on its own, from then on without trying the batch again.
func (*Client) Put ¶
func (c *Client) Put(ctx context.Context, path string, data []byte, author string) (filestore.Entry, error)
Put stores data as the newest revision of path whatever was there (a file of a collection that nobody edits in place: an upload, a file an agent sent) and returns the revision; the contents are then read by Entry.Sha.
func (*Client) PutLarge ¶
PutLarge uploads size bytes of r, which hash to sha, in parts. The store keeps the parts it got: a call after a broken one sends only the missing ones. ErrTooBig when the store does not take contents this big (or takes no uploads in parts). A store that takes a while to finish is asked until it has.
func (*Client) TakeLease ¶
func (c *Client) TakeLease(ctx context.Context, req filestore.LeaseRequest) (filestore.LeaseResponse, error)
TakeLease takes the lease on req.Key for this machine's req.Chat, or refreshes it; Held says someone else's live lease is there (then Lease is theirs).
func (*Client) UploadLimits ¶
UploadLimits asks what the store takes in parts: a zero PartSize from a store without uploads in parts (an older one), a zero MaxSize from a store without a limit.
type Codec ¶
type Codec interface {
// Canonical is the shareable form of a local file.
Canonical(rel string, local []byte) []byte
// Local is the file this machine writes for canonical contents; current is what the file holds now (nil when
// absent), so machine-only parts survive.
Local(rel string, canonical, current []byte) []byte
// Tag fingerprints canonical contents; equal tags mean equal contents.
Tag(canonical []byte) string
// Seal makes the blob the hub stores; Open reverses it.
Seal(rel string, canonical []byte) ([]byte, error)
Open(rel string, blob []byte) ([]byte, error)
// Merge combines two edits of the same base, true when it could (settings merge key by key).
Merge(rel string, base, local, remote []byte, localNewer bool) ([]byte, bool)
// Ready tells whether the codec can work (a sealed collection waits for its key).
Ready() bool
}
Codec turns a local file into the collection's canonical form and back. The canonical form is what machines compare and what the store keeps. A Sinopia library is Plain: files as they are. The seam stays so that a collection that carries machine paths or is sealed before it leaves the machine can be added without touching the sync.
type Config ¶
type Config struct {
Root string // the folder: a Sinopia library
StateDir string // where the sync keeps what it knows, outside the folder's synced paths
// Store is the collection the folder syncs with.
Store *Client
Machine string // shown in what a lost write leaves behind
Policy filestore.Policy
Codec Codec
// TmpDir is where downloads are staged before the rename (relative to Root, same filesystem).
TmpDir string
// ConflictDir, when set, takes a losing local edit (relative to Root, never synced) instead of the handling by
// revision (revision.go).
ConflictDir string
// AdoptRemote: on the very first sync, a local file the collection does not have goes to ConflictDir instead of
// up. That is joining a collection whose truth lives on another machine: what only this machine had is kept
// aside, not spread.
AdoptRemote bool
// KeepUnseen: a file the store stops showing this machine stays in the folder (a folder several runtimes share,
// where another runtime's person may still have a role in it); otherwise an unedited one is removed.
KeepUnseen bool
// RefusalGroup, when set, names what a refused write says about other paths: the store refuses every path of the
// same non-empty group (a project none of the machine's people may edit) until what it shows the machine changes,
// so after one refusal the others are not asked about. "" is a path refused on its own.
RefusalGroup func(rel string) string
// OnApplied is called after another machine's revision lands (outside the agent's lock).
OnApplied func(rel string)
// Log is where the sync says what it did (nil: slog's default).
Log *slog.Logger
}
Config is one folder.
type Notice ¶
type Notice struct {
V int `json:"v"`
Kind string `json:"kind"`
// Path is the file whose write lost, Kept where its contents are (relative to the root).
Path string `json:"path"`
Kept string `json:"kept"`
// Project and Board name a board (<project>/project/<board>.dc.html); empty for another file.
Project string `json:"project,omitempty"`
Board string `json:"board,omitempty"`
// Variant is the project the edit was saved as (kind variant).
Variant string `json:"variant,omitempty"`
// CurrentRev, By and ByMachine tell whose version took the place.
CurrentRev int64 `json:"current_rev"`
By string `json:"by,omitempty"`
ByMachine string `json:"by_machine,omitempty"`
Machine string `json:"machine"`
At string `json:"at"`
Bytes int `json:"bytes"`
}
Notice is what the machine keeps about a losing edit, next to the contents.
type ProfileCodec ¶
type ProfileCodec struct {
Home string
Workspace string
// LocalKeys are settings.json keys (dotted for nested ones, "env.MY_ROOT") that stay on the machine.
LocalKeys []string
// contains filtered or unexported fields
}
ProfileCodec is a person's Claude profile (filestore.Profile), the codec design-sync's profile rooms were written with: machine paths become placeholders, the settings that describe the machine stay on it, and the contents are sealed with the room key before they leave the machine. The settings' environment variables that carry a secret (a token, a key, a password) stay on the machine too: secrets go through the hub's safe only.
func (*ProfileCodec) Local ¶
func (p *ProfileCodec) Local(rel string, canonical, current []byte) []byte
func (*ProfileCodec) Merge ¶
func (p *ProfileCodec) Merge(rel string, base, local, remote []byte, localNewer bool) ([]byte, bool)
Merge merges settings.json key by key (recursively for objects): a key changed on one side only takes that side, a key changed on both takes the newer edit. Two machines editing different settings never conflict.
func (*ProfileCodec) Mergeable ¶
func (p *ProfileCodec) Mergeable(rel string) bool
Mergeable: settings.json merges key by key.
func (*ProfileCodec) Ready ¶
func (p *ProfileCodec) Ready() bool
func (*ProfileCodec) SetKey ¶
func (p *ProfileCodec) SetKey(k []byte)
SetKey installs the room key (32 bytes).
func (*ProfileCodec) Tag ¶
func (p *ProfileCodec) Tag(c []byte) string
Tag is a keyed fingerprint: the hub cannot test a guess of a file's contents against it.
type Quarantined ¶
Quarantined is a file the agent cannot apply (no rights on the local copy): it is set aside so the rest of the queue goes on, and tried again on every rescan and when a newer revision of it arrives.
type Token ¶
Token gives the bearer a request to the store carries. refresh asks for a new one: the store refused the last.
type VariantLink ¶
type VariantLink struct {
V int `json:"v"`
Of string `json:"of"`
Board string `json:"board,omitempty"`
Rev int64 `json:"rev"`
Machine string `json:"machine"`
At string `json:"at"`
Reason string `json:"reason"`
}
VariantLink is the record `variant.json` of a variant project: whose variant it is, of which board and revision.