| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "log/slog" |
| 8 | "os" |
| 9 | "strings" |
| 10 | "unicode" |
| 11 | |
| 12 | "the-valley/flow/internal/event" |
| 13 | "the-valley/flow/internal/git" |
| 14 | "the-valley/flow/internal/run" |
| 15 | ) |
| 16 | |
| 17 | // dataRefNotInHistory is a failure's message when origin took a history |
| 18 | // that lacks the commit finalize wrote for the run. |
| 19 | const dataRefNotInHistory = "the run's commit is not in the history that was pushed" |
| 20 | |
| 21 | // recordDataRefPush pushes the data ref in root to origin when push is set, |
| 22 | // then records on state, and in one event, what happened at finalize. It |
| 23 | // records published only when the history origin took holds commit. A |
| 24 | // failed push changes nothing else: the run keeps its outcome and the local |
| 25 | // commit stays. Only the class and the remote's own lines of a failure are |
| 26 | // kept, since git's other output can echo origin's URL with its credentials. |
| 27 | func recordDataRefPush(ctx context.Context, store run.StateStore, gitClient git.Client, root, dataRef, commit string, push bool, state *run.State) { |
| 28 | rec := &run.DataRefPush{Ref: dataRef, Commit: commit, Status: run.DataRefNotAttempted} |
| 29 | if push { |
| 30 | pushed, err := gitClient.PushDataRef(ctx, root, dataRef) |
| 31 | switch { |
| 32 | case err != nil: |
| 33 | pushErr := &git.PushError{Class: git.PushOther} |
| 34 | errors.As(err, &pushErr) |
| 35 | rec.Status, rec.Class, rec.Message = run.DataRefFailed, pushErr.Class, pushErr.Message |
| 36 | case !git.IsAncestor(ctx, root, commit, pushed): |
| 37 | rec.Status, rec.Class, rec.Message = run.DataRefFailed, git.PushOther, dataRefNotInHistory |
| 38 | default: |
| 39 | rec.Status = run.DataRefPublished |
| 40 | } |
| 41 | if rec.Status == run.DataRefFailed { |
| 42 | fmt.Fprintf(os.Stderr, "warning: at finalize: %s\n %s\n", dataRefFailure(rec), dataRefHint(rec)) |
| 43 | } |
| 44 | } |
| 45 | state.DataRefPush = rec |
| 46 | if err := store.Save(state); err != nil { |
| 47 | slog.Warn("failed to save state after the data ref push", "id", state.ID, "err", err) |
| 48 | } |
| 49 | hds, ok := store.(*run.HomeDirStore) |
| 50 | if !ok { |
| 51 | return |
| 52 | } |
| 53 | data := map[string]interface{}{"ref": rec.Ref, "commit": rec.Commit, "status": rec.Status} |
| 54 | if rec.Status != run.DataRefFailed { |
| 55 | emitRunEvent(hds.BaseDir(), state, event.AgentDataRefPush, data) |
| 56 | return |
| 57 | } |
| 58 | data["class"], data["message"] = rec.Class, rec.Message |
| 59 | emitRunEvent(hds.BaseDir(), state, event.AgentDataRefPushFailed, data) |
| 60 | } |
| 61 | |
| 62 | // dataRefFailure says on one line how finalize's push of rec failed. Where |
| 63 | // nothing else says so, callers label it as what happened at finalize. |
| 64 | func dataRefFailure(rec *run.DataRefPush) string { |
| 65 | s := fmt.Sprintf("publishing %s failed (%s)", oneLine(rec.Ref), git.PushClassText(rec.Class)) |
| 66 | if m := oneLine(rec.Message); m != "" { |
| 67 | s += ": " + m |
| 68 | } |
| 69 | return s |
| 70 | } |
| 71 | |
| 72 | // dataRefHint says what is known of a conversation finalize could not |
| 73 | // publish, and how it reaches origin. The data ref is one history per clone, |
| 74 | // so any later push of it from that clone carries every earlier run's commit. |
| 75 | func dataRefHint(rec *run.DataRefPush) string { |
| 76 | ref := oneLine(rec.Ref) |
| 77 | switch { |
| 78 | case rec.Class == git.PushNotFastForward: |
| 79 | // A later run's push from the same clone also leaves origin ahead of |
| 80 | // this one, and that push carried this run's commit. |
| 81 | return fmt.Sprintf("origin's %s has moved: it may already hold this run's commit, carried by a later run's push, or hold commits this clone lacks", ref) |
| 82 | case rec.Class == git.PushUnconfirmed: |
| 83 | return fmt.Sprintf("origin did not report the push's outcome, so it may hold this run's commit; to be sure, push by hand in the run's clone: git push origin %s", ref) |
| 84 | case rec.Class == git.PushOther && rec.Message == dataRefNotInHistory: |
| 85 | return fmt.Sprintf("the clone's %s did not hold this run's commit when it was pushed, so pushing the ref does not publish the run", ref) |
| 86 | } |
| 87 | return fmt.Sprintf("the conversation stays in the run's clone; the next run there that publishes carries it, or push by hand there: git push origin %s", ref) |
| 88 | } |
| 89 | |
| 90 | // failedDataRefPushes returns the runs whose data ref push failed at |
| 91 | // finalize, in states' order. |
| 92 | func failedDataRefPushes(states []*run.State) []*run.State { |
| 93 | var failed []*run.State |
| 94 | for _, s := range states { |
| 95 | if s.DataRefPush != nil && s.DataRefPush.Status == run.DataRefFailed { |
| 96 | failed = append(failed, s) |
| 97 | } |
| 98 | } |
| 99 | return failed |
| 100 | } |
| 101 | |
| 102 | // oneLine joins the lines of a stored push message for a one-line display, |
| 103 | // dropping control characters, which a hand-edited record could carry. |
| 104 | func oneLine(s string) string { |
| 105 | return strings.Join(strings.FieldsFunc(s, func(r rune) bool { return unicode.IsControl(r) }), "; ") |
| 106 | } |