package thin import ( "context" "errors" "sync" "time" "amadan.net/oficina/equipo/internal/runner/containment" "amadan.net/oficina/equipo/internal/runner/pause" "amadan.net/oficina/equipo/internal/runner/provider" ) // The workspace snapshot (`tree: work`, spec ยง3 "What it renders", plan // task 9): the runner pauses every process of the step's sandbox and, // inside the pause, reads the workspace's release/preview.json and copies // its output directory into a runner-private stage, by exportStatic's // rules. No git index is used and nothing is written to the workspace or // the branch. // errWorkBusy: the step's sandbox could not be paused for the copy within // the bound (or is gone, or this system cannot pause one). render_page // answers `busy`. var errWorkBusy = errors.New("workspace snapshot refused: busy") // snapshotBound bounds the pause, stopping and copying together (a seam // for the tests). var snapshotBound = pause.Bound // Bounds of a workspace walk beyond exportStatic's (a git listing has no // directories of its own): how deep below the output a path may go, and // how many directories it may hold. const ( previewMaxDepth = 32 previewMaxDirs = 2000 ) // sandboxHandle carries the step sandbox's first process, as containment // recorded it once it ran, from the step's start to its tool calls. type sandboxHandle struct { mu sync.Mutex p pause.Process ok bool // snap runs one snapshot at a time: two pauses of one sandbox would // resume each other's. snap sync.Mutex } // keep holds r's first process; a record without one (the record written // before the launch) is ignored. func (h *sandboxHandle) keep(r containment.Record) { if h == nil || r.HostPID <= 0 || r.StartTicks == 0 { return } h.mu.Lock() defer h.mu.Unlock() h.p, h.ok = pause.Process{PID: r.HostPID, StartTicks: r.StartTicks, Namespace: r.NamespaceID}, true } // drop forgets the process once the step's run is over. func (h *sandboxHandle) drop() { if h == nil { return } h.mu.Lock() defer h.mu.Unlock() h.p, h.ok = pause.Process{}, false } func (h *sandboxHandle) current() (pause.Process, bool) { if h == nil { return pause.Process{}, false } h.mu.Lock() defer h.mu.Unlock() return h.p, h.ok } // snapshotWithin is the pause's bound for a snapshot under ctx (the // render_page call's one deadline): snapshotBound, or the time left // before ctx's deadline when that is less; a ctx already ended is its // error, and nothing is paused. func snapshotWithin(ctx context.Context) (time.Duration, error) { if err := ctx.Err(); err != nil { return 0, err } bound := snapshotBound if d, ok := ctx.Deadline(); ok { if left := time.Until(d); left < bound { bound = left } } if bound <= 0 { return 0, context.DeadlineExceeded } return bound, nil } // snapshot copies the running step's workspace site into stage (fresh, // 0700) while its sandbox is paused, within ctx's deadline // (snapshotWithin). On any error the stage's content is not a snapshot // and must not be used. func (h *sandboxHandle) snapshot(ctx context.Context, workspace, stage, headers string) (previewExport, error) { p, ok := h.current() if !ok { return previewExport{}, errWorkBusy } h.snap.Lock() defer h.snap.Unlock() bound, err := snapshotWithin(ctx) if err != nil { return previewExport{}, err } return snapshotWork(p, workspace, stage, headers, bound) } // recordedStartKept is recordedStart that also keeps the sandbox's first // process on h, from the record containment writes once it runs (before // the harness is released, so before any tool call). func recordedStartKept(ctx context.Context, m starter, l provider.Launch, argv []string, record func(context.Context, string) error, h *sandboxHandle) (provider.Process, error) { return m.StartRecorded(ctx, l, argv, func(_ context.Context, r containment.Record) error { if err := record(context.WithoutCancel(ctx), r.ID); err != nil { return err } h.keep(r) return nil }) }