Files
archie/internal/container/logs.go
T
sam acec5d392d feat(archied): read-only run-scoped sandbox view over the harness contract (#1662)
* feat(archied): read-only run-scoped sandbox view over the harness contract

Serve RunTarget: the daemon resolves a running task and attempt to its Kit
container and follows its output, refusing a finished or unknown task with a
stated reason. The dashboard adds a Sandbox tab behind the task log's read
authority. Takeover stays out of scope until the sod1 authority is wired.

* fix(archied): scope and close sandbox views correctly

Forward harness activity into container output, bind containers and dashboard views to their attempt, enforce the asserted org, and report unavailable dependencies and interrupted streams accurately.

Refs: archie-core-qi91, archie-core-252tj
2026-10-14 02:57:32 +11:00

51 lines
1.4 KiB
Go

package container
import (
"context"
"errors"
"io"
"strconv"
"github.com/moby/moby/api/pkg/stdcopy"
"github.com/moby/moby/client"
)
// ErrNotRunning reports a container that exists but has stopped.
var ErrNotRunning = errors.New("container is not running")
// FollowLogs streams a running container's stdout and stderr, merged, from
// its recent past onward. It is read-only: nothing the caller does reaches
// the container. Closing the reader ends the follow.
func FollowLogs(ctx context.Context, cli *client.Client, name string, tail int) (io.ReadCloser, error) {
inspected, err := cli.ContainerInspect(ctx, name, client.ContainerInspectOptions{})
if err != nil {
return nil, err
}
if inspected.Container.State == nil || !inspected.Container.State.Running {
return nil, ErrNotRunning
}
logs, err := cli.ContainerLogs(ctx, inspected.Container.ID, client.ContainerLogsOptions{
ShowStdout: true, ShowStderr: true, Follow: true, Tail: strconv.Itoa(tail),
})
if err != nil {
return nil, err
}
pr, pw := io.Pipe()
go func() {
// A Kit container has no TTY, so the stream is multiplexed.
_, err := stdcopy.StdCopy(pw, pw, logs)
_ = logs.Close()
_ = pw.CloseWithError(err)
}()
return &logStream{PipeReader: pr, logs: logs}, nil
}
type logStream struct {
*io.PipeReader
logs io.ReadCloser
}
func (s *logStream) Close() error {
return errors.Join(s.PipeReader.Close(), s.logs.Close())
}