# env/supervisor

> Package supervisor retains bounded process I/O in an explicitly hosted local service, so interactive agent hosts can restart without losing their jobs.

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

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

Package supervisor retains bounded process I/O in an explicitly hosted local service, so interactive agent hosts can restart without losing their jobs. Hosts own the private transport, configured workspace and service lifecycle.


## Client

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

Client attaches to an explicitly owned Service over an injected local HTTP transport. Closing a client leaves jobs owned by the service. The caller owns the HTTP client's transport. Methods and returned processes are concurrency-safe.


### NewClient

```go
func NewClient(ctx context.Context, httpClient *http.Client, identity string) (*Client, error)
```

NewClient verifies configuration identity before allowing process attachment. httpClient must connect the fixed supervisor hostname to a private local socket; exposing this protocol on an untrusted network is unsupported.

**Example**

```go
package supervisor_test

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/barevoidco/zelk/core/env"
	"github.com/barevoidco/zelk/core/env/supervisor"
)

type exampleStarter struct{}

func (exampleStarter) Start(context.Context, env.Cmd) (env.Proc, error) {
	return nil, env.ErrNotDispatched
}

type exampleTransport struct{ handler http.Handler }

func (t exampleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	w := httptest.NewRecorder()
	t.handler.ServeHTTP(w, req)
	return w.Result(), nil
}

func ExampleNewClient() {
	// An in-memory transport stands in for a host's private Unix socket.
	service, err := supervisor.New(exampleStarter{}, "/workspace", "workspace-and-confinement", env.Enforcement{})
	if err != nil {
		panic(err)
	}
	defer service.Close()
	client, err := supervisor.NewClient(context.Background(), &http.Client{Transport: exampleTransport{service.Handler()}}, "workspace-and-confinement")
	if err != nil {
		panic(err)
	}
	jobs, err := client.Jobs(context.Background())
	if err != nil {
		panic(err)
	}
	fmt.Println(client.Root(), len(jobs))
	// Closing an Agent using this client would leave processes with the service.
	// Only the independent owner closes the service itself.
	// Output: /workspace 0
}

```


### Client.Enforcement

```go
func (c *Client) Enforcement() env.Enforcement
```

Enforcement reports the service's process confinement, checked at attachment.


### Client.Jobs

```go
func (c *Client) Jobs(ctx context.Context) ([]Job, error)
```

Jobs inspects retained process identity, completion and exit without reading output.


### Client.Lookup

```go
func (c *Client) Lookup(ctx context.Context, id env.ProcID) (env.Proc, error)
```

Lookup attaches stdin and retained output to a process generation, or returns env.ErrNotFound for confirmed absence or env.ErrProcessUnobservable for an unconfirmed exit. It never launches a replacement for a lost process.


### Client.Root

```go
func (c *Client) Root() string
```

Root identifies the workspace whose configured launcher owns these processes.


### Client.StartPersistent

```go
func (c *Client) StartPersistent(ctx context.Context, key string, cmd env.Cmd) (env.Proc, error)
```

StartPersistent starts an effect once per key during this service generation. Conflicting command reuse or a released key is refused without launching work.


## Job

```go
type Job struct {
	ID    env.ProcID
	Key   string
	Done  bool
	Exit  env.Exit
	Error string
}
```

Job is a retained process's stable identity and observed completion state.


## Service

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

Service owns bounded process I/O independently of any interactive Agent. The host explicitly serves Handler on a private local transport and calls Close before closing the workspace. It never starts a server implicitly. A service restart loses its processes; a new generation cannot impersonate handles from an earlier service. Methods are safe for concurrent use.


### New

```go
func New(starter Starter, root, identity string, profile env.Enforcement) (*Service, error)
```

New returns a service retaining at most 128 processes. identity describes the host's exact workspace/confinement configuration and is checked on every RPC. The caller retains ownership of starter. Output is bounded to 1 MiB per job. A starter with [env.ProcessPresence](/reference/go/env/#processpresence) supplies the absence proof for jobs from an earlier service generation.


### Service.Close

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

Close kills and joins every retained process and observation goroutine. Closing a client connection does not close this service or its processes.


### Service.Handler

```go
func (s *Service) Handler() http.Handler
```

Handler exposes the process protocol. Serve it only on a host-owned private local socket: configuration identity prevents accidental cross-workspace attachment but is not an authentication secret or a network security boundary.


### Service.Jobs

```go
func (s *Service) Jobs() []Job
```

Jobs returns retained running and finished jobs without consuming output.


## Starter

```go
type Starter interface {
	Start(context.Context, env.Cmd) (env.Proc, error)
}
```

Starter starts processes in a host-owned, confined workspace.
