Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/developer/build.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Build

> Last updated: 2026-09-10 · commit `4f29957d2`
> Last updated: 2026-09-11 · commit `d353f8cb0`

How to build every component of Darkbloom from a fresh clone: the Go
coordinator, the Rust prompt-contract sidecar, the Swift provider CLI (with its
Expand Down Expand Up @@ -31,6 +31,8 @@ Go/Swift fixture and focused checks are described in [test.md](test.md) and

- **macOS on Apple Silicon** for anything under `provider-swift/` (MLX + Metal).
The coordinator, sidecar, e2e harness, and UIs build on macOS or Linux.
The [local process cleanup fixtures](test.md#local-process-cleanup-fixtures)
need only Go, Python and harmless child stubs; they do not need a provider build or database.
- **Xcode Command Line Tools + `cmake`** (`brew install cmake`) — the metallib
helper compiles MLX's Metal kernels with cmake.
- **Git submodules** checked out: `libs/mlx-swift`, `libs/mlx-swift-lm`,
Expand Down
22 changes: 21 additions & 1 deletion docs/developer/test.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Test

> Last updated: 2026-09-11 · commit `d22ad0cf3`
> Last updated: 2026-09-11 · commit `d353f8cb0`

How to run the unit tests for each component, the end-to-end suite that boots a
real coordinator + Swift provider against ephemeral Postgres, and the docs
Expand Down Expand Up @@ -111,6 +111,26 @@ Store tests that need Postgres skip themselves when `DATABASE_URL` is unset
`go test $(go list ./... | grep -v /internal/api)` from `coordinator/` to skip
the slow WebSocket integration tests; run the full set before merging.

#### Local process cleanup fixtures

The testbed's local-provider and native-Postgres lifecycle checks use harmless
child executables and PATH stubs. They verify output draining before provider
completion, retained child ownership, graceful shutdown and forced termination,
temporary-data cleanup after initialization/readiness failures, and rejected
helper-stream cleanup before waiting for its process. They start
no Swift provider, database, Docker daemon or remote connection.

```bash
go test -race ./e2e/testbed ./e2e/testbed/deps -short -run 'TestLocalProviderWait|TestNativePostgres|TestPostgres|TestOwnedMalformedStream' -count=1
```

`e2e/testbed/provider_local.go` waits through `exec.Cmd.Wait` so command output
and context cleanup finish before completion. `e2e/testbed/deps/postgres.go`
retains its native child command, waits for it to be reaped and only then removes
its owned data directory. Unconfirmed termination retains that directory for
inspection. The [owned-host helper](../../e2e/testbed/OWNED_HOSTS.md) keeps its
separate process-group, control-lease and receipt contract.

#### Provider config cleanup

The CPU-only `e2e/testbed/provider_config_cleanup_test.go` tests retain a fixed
Expand Down
4 changes: 3 additions & 1 deletion e2e/testbed/OWNED_HOSTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ and writes `terminal.json` with the group identity, signals, exit code, cleanup
status, and failure. A cleanup error retains an incomplete receipt. The Go
adapter requires terminal and host-cleanup evidence; it reports an unconfirmed
shutdown as an error. Failed startup and fixture assertions still run cleanup and
retain cleanup errors. Runtime, state, logs, cache, and terminal evidence remain
retain cleanup errors. A malformed helper event closes both control pipes before
waiting for the helper, so a rejected output stream cannot block teardown on a
full pipe. Runtime, state, logs, cache, and terminal evidence remain
under the owned root; the private fixture token is removed after confirmed
shutdown.

Expand Down
117 changes: 67 additions & 50 deletions e2e/testbed/deps/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type PostgresLifecycle struct {

dataDir string
native bool
command *exec.Cmd
done chan struct{}
}

func NewPostgresLifecycle(logger *slog.Logger, port int) *PostgresLifecycle {
Expand All @@ -38,14 +40,22 @@ func (p *PostgresLifecycle) Start(ctx context.Context) error {
return p.startNative(ctx)
}

func (p *PostgresLifecycle) resolvePort() error {
if p.Port != 0 {
return nil
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return fmt.Errorf("testbed/deps: find free port: %w", err)
}
defer listener.Close()
p.Port = listener.Addr().(*net.TCPAddr).Port
return nil
}

func (p *PostgresLifecycle) startDocker(ctx context.Context) error {
if p.Port == 0 {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return fmt.Errorf("testbed/deps: find free port: %w", err)
}
p.Port = listener.Addr().(*net.TCPAddr).Port
listener.Close()
if err := p.resolvePort(); err != nil {
return err
}

p.DatabaseURL = fmt.Sprintf("postgres://testbed:testbed@127.0.0.1:%d/testbed?sslmode=disable", p.Port)
Expand Down Expand Up @@ -85,19 +95,21 @@ func (p *PostgresLifecycle) startNative(ctx context.Context) error {
return fmt.Errorf("testbed/deps: neither docker nor postgres found in PATH (need one for ephemeral Postgres)")
}

if p.Port == 0 {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return fmt.Errorf("testbed/deps: find free port: %w", err)
}
p.Port = listener.Addr().(*net.TCPAddr).Port
listener.Close()
if err := p.resolvePort(); err != nil {
return err
}

p.dataDir = filepath.Join(os.TempDir(), fmt.Sprintf("testbed-pg-%d-%04d", time.Now().UnixMilli(), rand.Intn(10000)))
if err := os.MkdirAll(p.dataDir, 0755); err != nil {
p.dataDir, err = os.MkdirTemp("", "testbed-pg-")
if err != nil {
return fmt.Errorf("testbed/deps: create data dir: %w", err)
}
p.native = true
started := false
defer func() {
if !started {
p.stopNative()
}
}()

initdb, _ := exec.LookPath("initdb")
if initdb == "" {
Expand All @@ -124,10 +136,14 @@ func (p *PostgresLifecycle) startNative(ctx context.Context) error {
}

p.ContainerID = fmt.Sprintf("native:%d", cmd.Process.Pid)
p.native = true
p.command = cmd
p.done = make(chan struct{})
go func(command *exec.Cmd, done chan struct{}) {
defer close(done)
_ = command.Wait()
}(cmd, p.done)

if err := p.waitForReadyNative(ctx); err != nil {
p.Stop()
return fmt.Errorf("testbed/deps: postgres readiness: %w", err)
}

Expand All @@ -145,24 +161,17 @@ func (p *PostgresLifecycle) startNative(ctx context.Context) error {
p.Logger.Warn("createdb failed (may already exist)", "error", string(out))
}

started = true
p.Logger.Info("ephemeral postgres started (native)", "port", p.Port, "dataDir", p.dataDir)
return nil
}

func (p *PostgresLifecycle) waitForReadyDocker(ctx context.Context) error {
for i := 0; i < 30; i++ {
return waitForPostgres(ctx, func() bool {
cmd := exec.CommandContext(ctx, "docker", "exec", p.ContainerID,
"pg_isready", "-U", "testbed", "-d", "testbed")
if err := cmd.Run(); err == nil && p.hostDatabaseReady(ctx) {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(500 * time.Millisecond):
}
}
return fmt.Errorf("testbed/deps: postgres did not become ready within 15s")
return cmd.Run() == nil && p.hostDatabaseReady(ctx)
})
}

func (p *PostgresLifecycle) hostDatabaseReady(ctx context.Context) bool {
Expand All @@ -177,13 +186,16 @@ func (p *PostgresLifecycle) hostDatabaseReady(ctx context.Context) bool {
}

func (p *PostgresLifecycle) waitForReadyNative(ctx context.Context) error {
for i := 0; i < 30; i++ {
return waitForPostgres(ctx, func() bool {
cmd := exec.CommandContext(ctx, "pg_isready",
"-h", "127.0.0.1",
"-p", fmt.Sprintf("%d", p.Port),
"-U", "testbed",
)
if err := cmd.Run(); err == nil {
"-h", "127.0.0.1", "-p", fmt.Sprintf("%d", p.Port), "-U", "testbed")
return cmd.Run() == nil
})
}

func waitForPostgres(ctx context.Context, ready func() bool) error {
for i := 0; i < 30; i++ {
if ready() {
return nil
}
select {
Expand Down Expand Up @@ -217,25 +229,30 @@ func (p *PostgresLifecycle) stopDocker() {
}

func (p *PostgresLifecycle) stopNative() {
if p.ContainerID == "" {
return
}
var pid int
fmt.Sscanf(p.ContainerID, "native:%d", &pid)
if pid > 0 {
proc, err := os.FindProcess(pid)
if err == nil {
proc.Signal(os.Interrupt)
time.Sleep(500 * time.Millisecond)
proc.Kill()
if p.command != nil {
// Retain the actual child handle: a display PID must never authorize a kill.
_ = p.command.Process.Signal(os.Interrupt)
select {
case <-p.done:
case <-time.After(500 * time.Millisecond):
_ = p.command.Process.Kill()
select {
case <-p.done:
case <-time.After(5 * time.Second):
p.Logger.Error("postgres termination unconfirmed; retaining owned data", "dataDir", p.dataDir)
return
}
}
p.command, p.done = nil, nil
}
if p.dataDir != "" {
os.RemoveAll(p.dataDir)
if err := os.RemoveAll(p.dataDir); err != nil {
p.Logger.Error("failed to remove postgres data", "dataDir", p.dataDir, "error", err)
return
}
p.Logger.Info("ephemeral postgres removed (native)", "dataDir", p.dataDir)
}
p.Logger.Info("ephemeral postgres removed (native)", "dataDir", p.dataDir)
p.ContainerID = ""
p.dataDir = ""
p.ContainerID, p.dataDir = "", ""
}

func (p *PostgresLifecycle) SetEnv() {
Expand Down
Loading
Loading