| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "sync" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/containment" |
| 10 | "amadan.net/oficina/equipo/internal/runner/pause" |
| 11 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 12 | ) |
| 13 | |
| 14 | // The workspace snapshot (`tree: work`, spec §3 "What it renders", plan |
| 15 | // task 9): the runner pauses every process of the step's sandbox and, |
| 16 | // inside the pause, reads the workspace's release/preview.json and copies |
| 17 | // its output directory into a runner-private stage, by exportStatic's |
| 18 | // rules. No git index is used and nothing is written to the workspace or |
| 19 | // the branch. |
| 20 | |
| 21 | // errWorkBusy: the step's sandbox could not be paused for the copy within |
| 22 | // the bound (or is gone, or this system cannot pause one). render_page |
| 23 | // answers `busy`. |
| 24 | var errWorkBusy = errors.New("workspace snapshot refused: busy") |
| 25 | |
| 26 | // snapshotBound bounds the pause, stopping and copying together (a seam |
| 27 | // for the tests). |
| 28 | var snapshotBound = pause.Bound |
| 29 | |
| 30 | // Bounds of a workspace walk beyond exportStatic's (a git listing has no |
| 31 | // directories of its own): how deep below the output a path may go, and |
| 32 | // how many directories it may hold. |
| 33 | const ( |
| 34 | previewMaxDepth = 32 |
| 35 | previewMaxDirs = 2000 |
| 36 | ) |
| 37 | |
| 38 | // sandboxHandle carries the step sandbox's first process, as containment |
| 39 | // recorded it once it ran, from the step's start to its tool calls. |
| 40 | type sandboxHandle struct { |
| 41 | mu sync.Mutex |
| 42 | p pause.Process |
| 43 | ok bool |
| 44 | // snap runs one snapshot at a time: two pauses of one sandbox would |
| 45 | // resume each other's. |
| 46 | snap sync.Mutex |
| 47 | } |
| 48 | |
| 49 | // keep holds r's first process; a record without one (the record written |
| 50 | // before the launch) is ignored. |
| 51 | func (h *sandboxHandle) keep(r containment.Record) { |
| 52 | if h == nil || r.HostPID <= 0 || r.StartTicks == 0 { |
| 53 | return |
| 54 | } |
| 55 | h.mu.Lock() |
| 56 | defer h.mu.Unlock() |
| 57 | h.p, h.ok = pause.Process{PID: r.HostPID, StartTicks: r.StartTicks, Namespace: r.NamespaceID}, true |
| 58 | } |
| 59 | |
| 60 | // drop forgets the process once the step's run is over. |
| 61 | func (h *sandboxHandle) drop() { |
| 62 | if h == nil { |
| 63 | return |
| 64 | } |
| 65 | h.mu.Lock() |
| 66 | defer h.mu.Unlock() |
| 67 | h.p, h.ok = pause.Process{}, false |
| 68 | } |
| 69 | |
| 70 | func (h *sandboxHandle) current() (pause.Process, bool) { |
| 71 | if h == nil { |
| 72 | return pause.Process{}, false |
| 73 | } |
| 74 | h.mu.Lock() |
| 75 | defer h.mu.Unlock() |
| 76 | return h.p, h.ok |
| 77 | } |
| 78 | |
| 79 | // snapshotWithin is the pause's bound for a snapshot under ctx (the |
| 80 | // render_page call's one deadline): snapshotBound, or the time left |
| 81 | // before ctx's deadline when that is less; a ctx already ended is its |
| 82 | // error, and nothing is paused. |
| 83 | func snapshotWithin(ctx context.Context) (time.Duration, error) { |
| 84 | if err := ctx.Err(); err != nil { |
| 85 | return 0, err |
| 86 | } |
| 87 | bound := snapshotBound |
| 88 | if d, ok := ctx.Deadline(); ok { |
| 89 | if left := time.Until(d); left < bound { |
| 90 | bound = left |
| 91 | } |
| 92 | } |
| 93 | if bound <= 0 { |
| 94 | return 0, context.DeadlineExceeded |
| 95 | } |
| 96 | return bound, nil |
| 97 | } |
| 98 | |
| 99 | // snapshot copies the running step's workspace site into stage (fresh, |
| 100 | // 0700) while its sandbox is paused, within ctx's deadline |
| 101 | // (snapshotWithin). On any error the stage's content is not a snapshot |
| 102 | // and must not be used. |
| 103 | func (h *sandboxHandle) snapshot(ctx context.Context, workspace, stage, headers string) (previewExport, error) { |
| 104 | p, ok := h.current() |
| 105 | if !ok { |
| 106 | return previewExport{}, errWorkBusy |
| 107 | } |
| 108 | h.snap.Lock() |
| 109 | defer h.snap.Unlock() |
| 110 | bound, err := snapshotWithin(ctx) |
| 111 | if err != nil { |
| 112 | return previewExport{}, err |
| 113 | } |
| 114 | return snapshotWork(p, workspace, stage, headers, bound) |
| 115 | } |
| 116 | |
| 117 | // recordedStartKept is recordedStart that also keeps the sandbox's first |
| 118 | // process on h, from the record containment writes once it runs (before |
| 119 | // the harness is released, so before any tool call). |
| 120 | func recordedStartKept(ctx context.Context, m starter, l provider.Launch, argv []string, record func(context.Context, string) error, h *sandboxHandle) (provider.Process, error) { |
| 121 | return m.StartRecorded(ctx, l, argv, func(_ context.Context, r containment.Record) error { |
| 122 | if err := record(context.WithoutCancel(ctx), r.ID); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | h.keep(r) |
| 126 | return nil |
| 127 | }) |
| 128 | } |
| 129 | |