# env/materialize

> Package materialize publishes bounded recorded skill bundles on a local filesystem.

<!-- Generated from the Go source of github.com/barevoidco/zelk/core/env/materialize (cmd/zelk/internal/reference TestReferenceGoAPI). Don't edit: change the code, then run make reference. -->

```go
import "github.com/barevoidco/zelk/core/env/materialize"
```

Package materialize publishes bounded recorded skill bundles on a local filesystem. Hosts compose its Store with an Environment and protect its namespace through launcher.ReadOnlyLauncher before starting processes. On Windows, the Store validates the cache's ACL and the ACLs of recorded files. A receipt rename commits the Windows tree without moving its open files. The sandbox's read-only grant protects publications from workloads.


## Bounds

```go
type Bounds struct {
	Bytes        int64
	Publications int
}
```

Bounds limits live storage, including staging trees and receipt metadata. Zero uses 64 MiB and 128 publications. Larger limits are refused.


## Store

```go
type Store struct {
	// contains filtered or unexported fields
}
```

Store implements env.Materializer using a dedicated host-owned directory. Calls are concurrency safe. A nonblocking process lock excludes other Store instances. Close waits for calls and releases that lock; it does not remove publications. Suspension and host shutdown therefore preserve resume data.

**Example**

```go
ctx := context.Background()
dir, err := os.MkdirTemp("", "zelk-materialize-")
if err != nil {
	return
}
defer os.RemoveAll(dir)
s, err := materialize.New(ctx, dir, ".zelk-skills", materialize.Bounds{})
if err != nil {
	return
}
defer s.Close()
data := []byte("echo recorded\n")
p, err := s.Materialize(ctx, env.MaterializeBundle{Session: "session", Key: "effect", Files: []env.MaterializeFile{{Path: "scripts/hello.sh", Digest: blob.Digest(data), Data: data}}})
if err != nil {
	return
}
fmt.Println(p.Bytes, s.Release(ctx, p))
// Output: 14 <nil>
```

Output:

```text
14 <nil>
```


### New

```go
func New(ctx context.Context, directory, visible string, bounds Bounds) (*Store, error)
```

New opens an existing private directory reserved exclusively for this store. visible is its canonical workspace-relative name in the target Environment; the host must ensure both names refer to the same directory. The directory must not be a symlink or writable by other users. Windows validates the directory ACL, including permissions inherited by its contents. Hosts must protect the directory before starting tool processes.


### Store.Close

```go
func (s *Store) Close() error
```

Close releases directory handles and the process lock, preserving data.


### Store.MaterializationNamespace

```go
func (s *Store) MaterializationNamespace() string
```

MaterializationNamespace identifies this canonical cache and visible mapping. Recreating the directory preserves it; changing either mapping does not.

**Example**

```go
ctx := context.Background()
dir, err := os.MkdirTemp("", "publication-namespace-")
if err != nil {
	return
}
defer os.RemoveAll(dir)
s, err := materialize.New(ctx, dir, "cache", materialize.Bounds{})
if err != nil {
	return
}
namespace := s.MaterializationNamespace()
if err := s.Close(); err != nil {
	return
}
s, err = materialize.New(ctx, dir, "cache", materialize.Bounds{})
if err != nil {
	return
}
defer s.Close()
fmt.Println(namespace == s.MaterializationNamespace())
// Output: true
```

Output:

```text
true
```


### Store.Materialize

```go
func (s *Store) Materialize(ctx context.Context, b env.MaterializeBundle) (out env.Materialization, err error)
```

Materialize verifies recorded content, stages a fresh tree and publishes it when its commit succeeds. Complete existing trees are verified byte for byte. The store removes matching interrupted stages and preserves unexpected files or changed bytes, reporting them as errors.


### Store.Release

```go
func (s *Store) Release(ctx context.Context, p env.Materialization) error
```

Release verifies a receipt and every owned file before removing publication contents. Absent or uncommitted publications succeed; changed or unexpected files survive. ReleaseBundle can reclaim uncommitted stages with recorded bytes. The host must ensure no live session or process still references the tree.


### Store.ReleaseBundle

```go
func (s *Store) ReleaseBundle(ctx context.Context, bundle env.MaterializeBundle) error
```

ReleaseBundle removes matching complete publications and interrupted stages without creating new storage. Every candidate is verified before any is changed. Unknown files, hard links, symlinks and changed data are preserved.

**Example**

```go
ctx := context.Background()
dir, err := os.MkdirTemp("", "zelk-cleanup-")
if err != nil {
	return
}
defer os.RemoveAll(dir)
s, err := materialize.New(ctx, dir, ".zelk-skills", materialize.Bounds{})
if err != nil {
	return
}
defer s.Close()
data := []byte("echo recorded\n")
bundle := env.MaterializeBundle{Session: "session", Key: "effect", Files: []env.MaterializeFile{{Path: "scripts/hello.sh", Digest: blob.Digest(data), Data: data}}}
// Cleanup needs recorded bytes, even when a publication receipt was lost.
_, err = s.Materialize(ctx, bundle)
if err != nil {
	return
}
fmt.Println(s.ReleaseBundle(ctx, bundle))
fmt.Println(s.ReleaseBundle(ctx, bundle))
// Output:
// <nil>
// <nil>
```

Output:

```text
<nil>
<nil>
```
