Repository Public
1// Package executor runs the typed commands an agent accepts. There is no2// free-form command: every type has a fixed implementation, parameters are3// validated against the local policy before anything runs, programs are4// started without a shell, with a minimal environment, a deadline and an5// output cap, and their output is redacted before it leaves the node.6package executor78import (9 "bufio"10 "context"11 "errors"12 "io"13 "os"14 "os/exec"15 "strings"16 "sync"17 "syscall"18 "time"1920 "clustr.spboucher.ai/mcc/internal/agent/runcmd"21 "clustr.spboucher.ai/mcc/internal/protocol"22 "clustr.spboucher.ai/mcc/internal/redact"23)2425// Output bounds for one command.26const (27 DefaultMaxLines = 200028 DefaultMaxBytes = 512 << 1029 TailMaxLines = 50030 TailMaxBytes = 256 << 1031 maxLineLen = 4 << 1032 // KillGrace is the delay between SIGTERM and SIGKILL of a process group.33 KillGrace = 10 * time.Second34)3536// Output receives the lines produced by a command, redacts them, enforces37// the cap and forwards them. Safe for concurrent use.38type Output struct {39 mu sync.Mutex40 send func(protocol.CommandOutput)41 red map[string]*redact.Stream42 lines int43 bytes int44 maxLines int45 maxBytes int46 truncated bool47}4849// NewOutput forwards redacted lines to send.50func NewOutput(send func(protocol.CommandOutput)) *Output {51 return &Output{send: send, red: map[string]*redact.Stream{}, maxLines: DefaultMaxLines, maxBytes: DefaultMaxBytes}52}5354// Limit lowers the cap (never raises it above the defaults).55func (o *Output) Limit(lines, bytes int) {56 o.mu.Lock()57 o.maxLines, o.maxBytes = min(lines, DefaultMaxLines), min(bytes, DefaultMaxBytes)58 o.mu.Unlock()59}6061// Truncated reports whether the cap was hit.62func (o *Output) Truncated() bool {63 o.mu.Lock()64 defer o.mu.Unlock()65 return o.truncated66}6768// Line emits one line on a stream (stdout | stderr | info).69func (o *Output) Line(stream, text string) {70 o.mu.Lock()71 defer o.mu.Unlock()72 if o.truncated {73 return74 }75 text = strings.ToValidUTF8(strings.TrimRight(text, "\r\n"), "�")76 if len(text) > maxLineLen {77 text = strings.ToValidUTF8(text[:maxLineLen], "") + "…"78 }79 st := o.red[stream]80 if st == nil {81 st = &redact.Stream{}82 o.red[stream] = st83 }84 text = st.Line(text)85 if o.lines+1 > o.maxLines || o.bytes+len(text) > o.maxBytes {86 o.truncated = true87 o.send(protocol.CommandOutput{Stream: "info", Data: "[output truncated by the agent: limit reached]"})88 return89 }90 o.lines++91 o.bytes += len(text)92 o.send(protocol.CommandOutput{Stream: stream, Data: text})93}9495// Info emits an agent-generated line.96func (o *Output) Info(text string) { o.Line("info", text) }9798// procResult is how a child process ended.99type procResult struct {100 ExitCode int101 Status string // succeeded | failed | timed_out | cancelled102 Err string103}104105// cancelCause distinguishes a requested cancellation from a deadline.106var errCancelled = errors.New("cancelled")107108// runStreaming starts argv (no shell) in its own process group and streams109// its output. When ctx ends, the group receives SIGTERM, then SIGKILL after110// the grace period. Extra environment variables are the declared ones only.111func runStreaming(ctx context.Context, out *Output, grace time.Duration, cwd string, extraEnv []string, argv ...string) procResult {112 if len(argv) == 0 {113 return procResult{ExitCode: -1, Status: "failed", Err: "empty command"}114 }115 cmd := exec.Command(argv[0], argv[1:]...)116 cmd.Env = runcmd.Env(extraEnv...)117 cmd.Dir = cwd118 cmd.Stdin = nil119 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}120 stdout, err := cmd.StdoutPipe()121 if err != nil {122 return procResult{ExitCode: -1, Status: "failed", Err: err.Error()}123 }124 stderr, err := cmd.StderrPipe()125 if err != nil {126 return procResult{ExitCode: -1, Status: "failed", Err: err.Error()}127 }128 if err := cmd.Start(); err != nil {129 return procResult{ExitCode: -1, Status: "failed", Err: "cannot start: " + err.Error()}130 }131 var wg sync.WaitGroup132 pump := func(stream string, r io.Reader) {133 defer wg.Done()134 br := bufio.NewReaderSize(r, 64<<10)135 for {136 line, err := br.ReadString('\n')137 if line != "" {138 out.Line(stream, line)139 }140 if err != nil {141 // Keep draining nothing: the pipe is closed or broken.142 return143 }144 }145 }146 wg.Add(2)147 go pump("stdout", stdout)148 go pump("stderr", stderr)149150 pumped := make(chan struct{})151 go func() { wg.Wait(); close(pumped) }()152 done := make(chan error, 1)153 go func() {154 // Wait closes the pipes; read everything first, but do not hang on a155 // grandchild that inherited them after the group was killed.156 <-pumped157 done <- cmd.Wait()158 }()159160 pgid := cmd.Process.Pid161 var werr error162 interrupted := false163 select {164 case werr = <-done:165 case <-ctx.Done():166 interrupted = true167 syscall.Kill(-pgid, syscall.SIGTERM)168 select {169 case werr = <-done:170 case <-time.After(grace):171 syscall.Kill(-pgid, syscall.SIGKILL)172 select {173 case werr = <-done:174 case <-time.After(5 * time.Second):175 stdout.Close()176 stderr.Close()177 werr = <-done178 }179 }180 }181 res := procResult{}182 var ee *exec.ExitError183 switch {184 case errors.As(werr, &ee):185 res.ExitCode = ee.ExitCode()186 case werr != nil:187 res.ExitCode = -1188 res.Err = werr.Error()189 }190 switch {191 case interrupted && errors.Is(context.Cause(ctx), errCancelled):192 res.Status = "cancelled"193 case interrupted:194 res.Status = "timed_out"195 case res.ExitCode == 0 && werr == nil:196 res.Status = "succeeded"197 default:198 res.Status = "failed"199 if res.Err == "" {200 res.Err = "exit status " + itoa(res.ExitCode)201 }202 }203 return res204}205206func itoa(n int) string {207 if n < 0 {208 return "-" + itoa(-n)209 }210 if n < 10 {211 return string(rune('0' + n))212 }213 return itoa(n/10) + string(rune('0'+n%10))214}215216// tailFile emits the last maxLines lines of a file, reading at most217// maxBytes from its end. The file is opened without following a final218// symlink to a device or directory: only regular files are read.219func tailFile(out *Output, stream, path string, maxLines, maxBytes int) error {220 st, err := os.Stat(path)221 if err != nil {222 return err223 }224 if !st.Mode().IsRegular() {225 return errors.New("not a regular file")226 }227 f, err := os.Open(path)228 if err != nil {229 return err230 }231 defer f.Close()232 size := st.Size()233 start := int64(0)234 if size > int64(maxBytes) {235 start = size - int64(maxBytes)236 }237 buf := make([]byte, size-start)238 n, err := f.ReadAt(buf, start)239 if err != nil && err != io.EOF {240 return err241 }242 text := string(buf[:n])243 if start > 0 {244 if i := strings.IndexByte(text, '\n'); i >= 0 {245 text = text[i+1:] // drop the partial first line246 }247 }248 lines := strings.Split(strings.TrimRight(text, "\n"), "\n")249 if len(lines) > maxLines {250 lines = lines[len(lines)-maxLines:]251 }252 for _, l := range lines {253 out.Line(stream, l)254 }255 return nil256}257