package thin import ( "context" "encoding/json" "errors" "io/fs" "os" "path/filepath" "slices" "strings" "time" "amadan.net/oficina/equipo/internal/copydesk" "amadan.net/oficina/equipo/internal/runner/workspace" ) type copyWork struct { root string contract *copydesk.Contract before map[string][]byte compared string } func (e *Engineer) copyRead(ctx context.Context, tree workspace.TaskTree, sha, path string) ([]byte, error) { if !shaPattern.MatchString(sha) || !copydesk.SafePath(path) { return nil, errors.New("copy: invalid revision or path") } return gitRead(ctx, tree.Git, tree.GitDir, 2<<20, "show", sha+":"+path) } func copyFiles(root string) (map[string][]byte, error) { out := map[string][]byte{} size := 0 err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error { if err != nil { return err } rel, err := filepath.Rel(root, p) if err != nil { return err } if d.IsDir() { if d.Name() == ".git" || d.Name() == "vendor" { return filepath.SkipDir } return nil } if d.Type()&os.ModeSymlink != 0 { return errors.New("copy: symlink in command output") } raw, err := os.ReadFile(p) if err != nil { return err } size += len(raw) if size > 32<<20 { return errors.New("copy: input too large") } out[filepath.ToSlash(rel)] = raw return nil }) return out, err } func putCopyFile(root, path string, raw []byte) error { if !copydesk.SafePath(path) { return errors.New("copy: invalid path") } full := filepath.Join(root, filepath.FromSlash(path)) if err := os.MkdirAll(filepath.Dir(full), 0700); err != nil { return err } return os.WriteFile(full, raw, 0600) } func (e *Engineer) prepareCopyContract(ctx context.Context, r stepRow, tree workspace.TaskTree) (copyWork, error) { root, err := os.MkdirTemp(e.O.Runner.WorkspaceRoot, "copy-") w := copyWork{root: root, compared: r.Copy.Compared, before: map[string][]byte{}} if err != nil { return w, err } raw, err := e.copyRead(ctx, tree, r.BaseSHA, ".equipo/copy.json") if err == nil { c, err := copydesk.ParseContract(raw) if err != nil { return w, err } w.contract = &c } if c := w.contract; c != nil { commands := [][]string{c.Extract, c.Apply, c.Accept, c.Regenerate, c.Classify} if len(c.Reference) > 0 { commands = append(commands, c.Reference) } for _, a := range commands { raw, err := e.copyRead(ctx, tree, r.BaseSHA, a[1]) if err != nil { return w, err } if err := putCopyFile(filepath.Join(root, "trusted"), a[1], raw); err != nil { return w, err } } } return w, nil } func (e *Engineer) prepareCopy(ctx context.Context, r stepRow, tree workspace.TaskTree) (copyWork, error) { w, err := e.prepareCopyContract(ctx, r, tree) if err != nil { return w, err } root := w.root changed := map[string]bool{} if w.contract == nil { diff, err := gitRead(ctx, tree.Git, tree.GitDir, 2<<20, "diff", "--name-only", "-z", w.compared, r.CandidateSHA) if err != nil { return w, err } for _, path := range strings.Split(string(diff), "\x00") { changed[path] = true } } // git ls-tree supplies the exact tracked paths, including spaces, without a shell. listing, err := gitRead(ctx, tree.Git, tree.GitDir, 2<<20, "ls-files", "--stage", "-z") if err != nil { return w, err } for _, line := range strings.Split(string(listing), "\x00") { if line == "" { continue } meta, path, ok := strings.Cut(line, "\t") if !ok || !copydesk.SafePath(path) { return w, errors.New("copy: invalid tree") } selected := w.contract == nil && changed[path] && copydesk.GenericSupported(path) if c := w.contract; c != nil { selected = slices.Contains(c.Paths, path) || slices.Contains(c.Bookkeeping, path) || strings.HasSuffix(path, "_test.go") && !copydesk.Excluded(strings.TrimSuffix(path, "_test.go")+".go") } for _, ref := range r.Copy.References { selected = selected || ref.Path == path } if !selected { continue } if !strings.HasPrefix(meta, "100644 ") && !strings.HasPrefix(meta, "100755 ") { return w, errors.New("copy: nonregular source") } value, err := os.ReadFile(filepath.Join(tree.Dir, filepath.FromSlash(path))) if err != nil { return w, err } w.before[path] = value if err := putCopyFile(filepath.Join(root, "candidate"), path, value); err != nil { return w, err } old, err := e.copyRead(ctx, tree, w.compared, path) if err == nil { if err := putCopyFile(filepath.Join(root, "base"), path, old); err != nil { return w, err } } } return w, nil } func (e *Engineer) copyCommand(ctx context.Context, r stepRow, w copyWork, argv []string, input any, out any) error { raw, err := json.Marshal(input) if err != nil || len(raw) > 2<<20 { return errors.New("copy: command input") } args := slices.Clone(argv) args[0] = "/usr/bin/python3" args[1] = "/workspace/trusted/" + args[1] result := e.runGate(ctx, r, w.root, gate{jsonOutput: true, name: "copy", argv: args, input: raw, limit: 30 * time.Second}) if !result.terminated || result.reason != "" || result.code != 0 { return errors.New("copy: command refused or failed") } if out != nil { return json.Unmarshal([]byte(result.output), out) } return nil } func (e *Engineer) copyExtract(ctx context.Context, r stepRow, w copyWork) (copydesk.Extraction, error) { ex := copydesk.Extraction{Version: 1, Strings: []copydesk.Literal{}} if w.contract != nil { err := e.copyCommand(ctx, r, w, w.contract.Extract, map[string]string{"base": "/workspace/base", "candidate": "/workspace/candidate"}, &ex) return ex, err } keys := make([]string, 0, len(w.before)) for k := range w.before { keys = append(keys, k) } slices.Sort(keys) for _, path := range keys { old, _ := os.ReadFile(filepath.Join(w.root, "base", path)) ss, err := copydesk.Generic(path, old, w.before[path]) if err != nil { continue } ex.Strings = append(ex.Strings, ss...) } return ex, nil }