Documentation
¶
Overview ¶
Package substrate is the declare-then-capture contract for world-side effects: a Substrate applies already-recorded declaration records and returns capture drafts - it never appends them itself; the materialize orchestrator owns the append. Substrates register in a fail-closed registry and are selected by the substrate_ref stamped in the declarations' own witnesses. The echo substrate is the test fixture, the KV substrate is the reference world-side store, and the workspace substrate bridges the Sandbox interface so recorded file-write and exec intents reach a real workspace.
Index ¶
- Constants
- Variables
- type EchoSubstrate
- func (e *EchoSubstrate) CaptureSchemas() []string
- func (e *EchoSubstrate) Containment() trace.Containment
- func (e *EchoSubstrate) DeclarationSchemas() []string
- func (e *EchoSubstrate) Materialize(_ context.Context, records []trace.Record) (MaterializationResult, error)
- func (e *EchoSubstrate) SubstrateRef() string
- type KVSubstrate
- func (k *KVSubstrate) CaptureSchemas() []string
- func (k *KVSubstrate) Close() error
- func (k *KVSubstrate) Containment() trace.Containment
- func (k *KVSubstrate) DeclarationSchemas() []string
- func (k *KVSubstrate) Get(key string) (any, error)
- func (k *KVSubstrate) Materialize(_ context.Context, records []trace.Record) (MaterializationResult, error)
- func (k *KVSubstrate) Path() string
- func (k *KVSubstrate) SubstrateRef() string
- type MaterializationOutcome
- type MaterializationResult
- type Substrate
- type SubstrateError
- type SubstrateRegistry
- type UnknownSubstrateError
- type WorkspaceSubstrate
- func (w *WorkspaceSubstrate) CaptureSchemas() []string
- func (w *WorkspaceSubstrate) Containment() trace.Containment
- func (w *WorkspaceSubstrate) DeclarationSchemas() []string
- func (w *WorkspaceSubstrate) Materialize(ctx context.Context, records []trace.Record) (MaterializationResult, error)
- func (w *WorkspaceSubstrate) Sandbox() sandbox.Sandbox
- func (w *WorkspaceSubstrate) SubstrateRef() string
Constants ¶
const ( // KVSQLiteSubstrateRef mirrors KV_SQLITE_SUBSTRATE_REF. KVSQLiteSubstrateRef = "kv.sqlite.local.v1" // KVPutDeclarationSchema mirrors KV_PUT_DECLARATION_SCHEMA. KVPutDeclarationSchema = "shepherd2.kv.put.v1" // KVPutCaptureSchema mirrors KV_PUT_CAPTURE_SCHEMA. KVPutCaptureSchema = "shepherd2.kv.put.applied.v1" )
const ( // WorkspaceSubstrateRef identifies the workspace substrate kind. WorkspaceSubstrateRef = "workspace.sandbox.v1" // SchemaWorkspaceFileWrite declares an intent to write a file. SchemaWorkspaceFileWrite = "workspace.file.write.v1" // SchemaWorkspaceExec declares an intent to run a command. SchemaWorkspaceExec = "workspace.exec.v1" // SchemaWorkspaceFileWriteApplied is emitted when a write landed. SchemaWorkspaceFileWriteApplied = "workspace.file.write.applied.v1" // SchemaWorkspaceExecApplied is emitted when an exec completed. SchemaWorkspaceExecApplied = "workspace.exec.applied.v1" )
Variables ¶
var ErrUnknownSubstrate = errors.New("goatherd: unknown substrate")
ErrUnknownSubstrate is the sentinel behind UnknownSubstrateError, so callers can errors.Is(err, ErrUnknownSubstrate) without matching strings.
Functions ¶
This section is empty.
Types ¶
type EchoSubstrate ¶
type EchoSubstrate struct {
Ref string
Declarations []string
SubstrateCont trace.Containment
}
EchoSubstrate echoes declaration payloads into capture drafts.
func NewEchoSubstrate ¶
func NewEchoSubstrate(ref string, declarationSchemas ...string) *EchoSubstrate
NewEchoSubstrate builds an echo substrate accepting the given declaration schemas, contained by default (matching Python's dataclass default).
func (*EchoSubstrate) CaptureSchemas ¶
func (e *EchoSubstrate) CaptureSchemas() []string
func (*EchoSubstrate) Containment ¶
func (e *EchoSubstrate) Containment() trace.Containment
func (*EchoSubstrate) DeclarationSchemas ¶
func (e *EchoSubstrate) DeclarationSchemas() []string
func (*EchoSubstrate) Materialize ¶
func (e *EchoSubstrate) Materialize(_ context.Context, records []trace.Record) (MaterializationResult, error)
Materialize copies each declaration into a same-schema capture draft, caused by the declaration's own record id. The kind label mirrors the declaration's fact kind; records read through the store always carry one.
func (*EchoSubstrate) SubstrateRef ¶
func (e *EchoSubstrate) SubstrateRef() string
type KVSubstrate ¶
type KVSubstrate struct {
// contains filtered or unexported fields
}
KVSubstrate is the SQLite-backed key/value substrate.
func NewKVSubstrate ¶
func NewKVSubstrate(path string) (*KVSubstrate, error)
NewKVSubstrate opens (creating if needed) the world-side KV database at path. ":memory:" gives an in-memory store.
func (*KVSubstrate) CaptureSchemas ¶
func (k *KVSubstrate) CaptureSchemas() []string
func (*KVSubstrate) Close ¶
func (k *KVSubstrate) Close() error
Close closes the underlying database.
func (*KVSubstrate) Containment ¶
func (k *KVSubstrate) Containment() trace.Containment
func (*KVSubstrate) DeclarationSchemas ¶
func (k *KVSubstrate) DeclarationSchemas() []string
func (*KVSubstrate) Get ¶
func (k *KVSubstrate) Get(key string) (any, error)
Get returns the decoded value stored under key, or nil when absent.
The value is decoded with UseNumber for the same reason retained bodies are: a stored 42 must read back as 42, not 42.0, or a later digest over the value would not match the capture record that describes it.
func (*KVSubstrate) Materialize ¶
func (k *KVSubstrate) Materialize(_ context.Context, records []trace.Record) (MaterializationResult, error)
Materialize applies each kv put declaration to the world-side store and returns one applied capture per put. The whole batch is one transaction: either every put lands (and every capture describes a landed put) or none does.
func (*KVSubstrate) Path ¶
func (k *KVSubstrate) Path() string
Path returns the world-side database path.
func (*KVSubstrate) SubstrateRef ¶
func (k *KVSubstrate) SubstrateRef() string
type MaterializationOutcome ¶ added in v0.23.0
type MaterializationOutcome string
MaterializationOutcome is the result vocabulary a substrate reports.
const ( // MaterializationSuccess means every declaration was applied and every // intended capture is present in the result. MaterializationSuccess MaterializationOutcome = "success" // MaterializationCleanFailure means nothing was applied and the failure // reason explains why. No captures are emitted — a substrate claiming // clean_failure while emitting capture drafts is rejected by dispatch, // because the two claims contradict each other. A substrate may only // claim clean_failure when it did not attempt a world-side mutation: // a failure after a mutation was attempted is split_state, because the // world may have changed without a capture. MaterializationCleanFailure MaterializationOutcome = "clean_failure" // MaterializationSplitState means the batch did not apply cleanly: the // captures and anchors describe only the records that verifiably // landed, and the world-side state of the failed record(s) is uncertain // — the failure reason names them. MaterializationSplitState MaterializationOutcome = "split_state" )
type MaterializationResult ¶ added in v0.23.0
type MaterializationResult struct {
Outcome MaterializationOutcome
CaptureDrafts []trace.RecordDraft
FailureReason string
WorldSideAnchors []map[string]any
}
MaterializationResult is the substrate-produced result before the orchestrator appends captures. Substrates never append themselves: dispatch owns the append (the materialize orchestrator), which keeps substrates testable without a store.
type Substrate ¶
type Substrate interface {
// SubstrateRef identifies the KIND of substrate ("kv.sqlite.local.v1",
// "workspace.sandbox.v1"). Backend details belong in capture payloads.
SubstrateRef() string
// DeclarationSchemas lists the intent schemas this substrate accepts.
DeclarationSchemas() []string
// CaptureSchemas lists the receipt schemas it emits.
CaptureSchemas() []string
// Containment is the witness-level containment classification stamped
// into the retained context of the materialization captures.
Containment() trace.Containment
// Materialize applies the declarations and returns capture drafts.
Materialize(ctx context.Context, records []trace.Record) (MaterializationResult, error)
}
Substrate is a registered unit of declaration materialization.
DeclarationSchemas and CaptureSchemas are treated as sets; a duplicate entry is harmless. Materialize receives the selected declaration records (already retained in the store, addressed by the request) and returns capture drafts; it never appends them itself.
type SubstrateError ¶
type SubstrateError struct {
// contains filtered or unexported fields
}
SubstrateError is raised for substrate registration or dispatch failures. It mirrors Python's SubstrateError (RuntimeError subclass).
func (*SubstrateError) Error ¶
func (e *SubstrateError) Error() string
type SubstrateRegistry ¶
type SubstrateRegistry struct {
// contains filtered or unexported fields
}
SubstrateRegistry is a fail-closed process-local substrate registry. Registering a second, different substrate under an existing ref is an error; registering the same substrate twice is a no-op (Python compares identity, Go compares interface equality, which is identity for pointers). A non-comparable implementation (a value whose struct embeds a map, say) cannot be identity-compared without panicking, so it fails closed on re-registration instead — see sameSubstrate.
func NewSubstrateRegistry ¶
func NewSubstrateRegistry() *SubstrateRegistry
NewSubstrateRegistry returns an empty registry.
func (*SubstrateRegistry) Get ¶
func (r *SubstrateRegistry) Get(substrateRef string) (Substrate, error)
Get returns the substrate registered under ref.
func (*SubstrateRegistry) Register ¶
func (r *SubstrateRegistry) Register(s Substrate) error
Register adds a substrate under its ref.
type UnknownSubstrateError ¶
type UnknownSubstrateError struct {
// contains filtered or unexported fields
}
UnknownSubstrateError is raised when materialize dispatch names a substrate that is not registered.
func (*UnknownSubstrateError) Error ¶
func (e *UnknownSubstrateError) Error() string
func (*UnknownSubstrateError) Is ¶
func (e *UnknownSubstrateError) Is(target error) bool
type WorkspaceSubstrate ¶
type WorkspaceSubstrate struct {
// contains filtered or unexported fields
}
WorkspaceSubstrate materializes file and exec intents into a sandbox.Sandbox.
func NewWorkspaceSubstrate ¶
func NewWorkspaceSubstrate(sb sandbox.Sandbox) *WorkspaceSubstrate
NewWorkspaceSubstrate bridges a sandbox.Sandbox into the substrate contract.
func (*WorkspaceSubstrate) CaptureSchemas ¶
func (w *WorkspaceSubstrate) CaptureSchemas() []string
func (*WorkspaceSubstrate) Containment ¶
func (w *WorkspaceSubstrate) Containment() trace.Containment
Containment reports what the bridged sandbox claims, so the witness tells the truth about git-in-place (uncontained) versus isolated backends.
func (*WorkspaceSubstrate) DeclarationSchemas ¶
func (w *WorkspaceSubstrate) DeclarationSchemas() []string
func (*WorkspaceSubstrate) Materialize ¶
func (w *WorkspaceSubstrate) Materialize(ctx context.Context, records []trace.Record) (MaterializationResult, error)
Materialize applies each declaration to the sandbox and emits one applied capture per declaration.
Capability gating happens before the first mutation: a batch that needs a capability the backend does not have fails cleanly with nothing applied — the git backend's honest-failure story, never a partial append.
Once a sandbox call has been made for a record, a failure of that call is reported as split_state even when no capture was emitted and nothing else landed: backends write files in chunks and run commands to completion before teardown, so a failed call may still have changed the world, and claiming clean_failure would assert a world state that was not verified. clean_failure is therefore reserved for failures that provably touched nothing — malformed declarations, unknown schemas, capability gates. The captures and anchors in a split_state receipt cover only the records that verifiably landed, and the failure reason names the record whose world-side state is uncertain.
An exec that runs to completion is a success even with a nonzero exit code: the exit code is the observed result, recorded in the capture. Only infrastructure failures (the command could not run, a write failed) are substrate failures.
func (*WorkspaceSubstrate) Sandbox ¶
func (w *WorkspaceSubstrate) Sandbox() sandbox.Sandbox
Sandbox returns the bridged sandbox.
func (*WorkspaceSubstrate) SubstrateRef ¶
func (w *WorkspaceSubstrate) SubstrateRef() string