| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/copydesk" |
| 11 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 12 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | ) |
| 15 | |
| 16 | func (e *Engineer) recoveryEvidence(ctx context.Context, r stepRow) (*copydesk.Signature, error) { |
| 17 | if e.CI == nil { |
| 18 | return nil, errors.New("copy: current hosted evidence unavailable") |
| 19 | } |
| 20 | st, err := e.CI.CIStatus(ctx, r.Repo, r.Branch, forge.CILogsNone) |
| 21 | if err != nil || st.SHA != r.CandidateSHA || st.State != wire.HostedFailure { |
| 22 | return nil, errors.New("copy: recovery candidate moved") |
| 23 | } |
| 24 | rep, ok := e.hostedVerdict(ctx, r, judgeReport(r), st) |
| 25 | if !ok { |
| 26 | return nil, errors.New("copy: no completed verdict") |
| 27 | } |
| 28 | rep = e.classifyChecks(ctx, r, rep) |
| 29 | if rep.Failure == nil || !rep.Failure.CopyOnly() { |
| 30 | return nil, errors.New("copy: recovery failures are not completely classified") |
| 31 | } |
| 32 | for _, f := range rep.Failure.Failures { |
| 33 | if f.Category == "bookkeeping" && len(r.Copy.References) == 0 { |
| 34 | return nil, errors.New("copy: owner must identify retained reference lines") |
| 35 | } |
| 36 | } |
| 37 | return rep.Failure, nil |
| 38 | } |
| 39 | func (e *Engineer) rebaseCopy(ctx context.Context, r stepRow, tree workspace.TaskTree) (workspace.TaskTree, error) { |
| 40 | tip, err := e.defaultTip(ctx, r.Repo) |
| 41 | if err != nil { |
| 42 | return tree, err |
| 43 | } |
| 44 | if _, err := gitRead(ctx, tree.Git, tree.GitDir, 1024, "merge-base", "--is-ancestor", r.BaseSHA, tip); err != nil { |
| 45 | return tree, errors.New("copy: tooling is not on default branch") |
| 46 | } |
| 47 | raw, err := e.copyRead(ctx, tree, r.BaseSHA, ".equipo/copy.json") |
| 48 | if err != nil { |
| 49 | return tree, err |
| 50 | } |
| 51 | c, err := copydesk.ParseContract(raw) |
| 52 | if err != nil { |
| 53 | return tree, err |
| 54 | } |
| 55 | tree, err = workspace.PrepareTaskTree(ctx, tree.Git, e.Clones.Path(r.Repo), r.BaseSHA, r.Branch, tree.Dir, tree.GitDir) |
| 56 | if err != nil { |
| 57 | return tree, err |
| 58 | } |
| 59 | conflicts, err := tree.Rebase(ctx, r.FromBase, r.CandidateSHA) |
| 60 | if err != nil { |
| 61 | return tree, err |
| 62 | } |
| 63 | for _, path := range conflicts { |
| 64 | if !slices.Contains(c.Paths, path) && !slices.Contains(c.Bookkeeping, path) { |
| 65 | return tree, errors.New("copy: recovery has product conflicts") |
| 66 | } |
| 67 | } |
| 68 | return tree, nil |
| 69 | } |
| 70 | func (e *Engineer) regenerateCopy(ctx context.Context, r stepRow, tree workspace.TaskTree, w *copyWork) error { |
| 71 | if w.contract == nil { |
| 72 | return errors.New("copy: recovery needs a contract") |
| 73 | } |
| 74 | // The adapter owns its source grammar. One source file is the v1 recovery |
| 75 | // contract; wider or ambiguous conflicts need the owner. |
| 76 | if len(w.contract.Paths) != 1 { |
| 77 | return errors.New("copy: unsupported recovery contract") |
| 78 | } |
| 79 | source := w.contract.Paths[0] |
| 80 | old, err := e.copyRead(ctx, tree, r.FromBase, source) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | tooling, err := e.copyRead(ctx, tree, r.BaseSHA, source) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | candidate, err := e.copyRead(ctx, tree, r.CandidateSHA, source) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | for _, p := range w.contract.Bookkeeping { |
| 93 | current := w.before[p] |
| 94 | if strings.Contains(string(current), "<<<<<<<") { |
| 95 | trusted, err := e.copyRead(ctx, tree, r.BaseSHA, p) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | if err := putCopyFile(filepath.Join(w.root, "candidate"), p, trusted); err != nil { |
| 100 | return err |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | for name, value := range map[string][]byte{"old": old, "tooling": tooling, "candidate": candidate} { |
| 105 | if err := putCopyFile(w.root, "recovery/"+name, value); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | } |
| 109 | err = e.copyCommand(ctx, r, *w, w.contract.Regenerate, map[string]any{"candidate": "/workspace/candidate", "recovery": map[string]string{"old_path": "/workspace/recovery/old", "tooling_path": "/workspace/recovery/tooling", "candidate_path": "/workspace/recovery/candidate"}}, nil) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | w.before, err = copyFiles(filepath.Join(w.root, "candidate")) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | for p, raw := range w.before { |
| 118 | if err := putCopyFile(tree.Dir, p, raw); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | } |
| 122 | return nil |
| 123 | } |
| 124 | |