web: Live and the wrapper share one rule for the open preview
Task 12 review fix round 1. directionView marked a round live, and set the direction's preview link, from an older deployed preview when a newer one of the candidate was running, failed, unconfirmed for another reason or expired; the wrapper frames only the newest preview step, so those links were 404s. wire.OpenPreview is now the one rule for "the preview the wrapper frames": the task's newest preview step, done, deployed, with a URL, of the task's candidate (or its merged commit once done), not expired, a devbox link not taken down with its ended task; never an older one. The route's previewDeployment and validatePreview use it (their errors are unchanged: none is ErrPreviewAccess, changed ErrPreviewChanged, expired ErrPreviewExpired; the binding and host checks stay theirs); directionView uses it through openPreview for PreviewURL and Live. The route now also needs the step done (deployed outcomes are only recorded on done). devboxAfterDone delegates to wire.DevboxAfterDone. TDD: go test ./internal/equipo/chat/ -run TestDirectionNoFallbackPreview failed in all four cases (running, failed, unconfirmed, expired): the older round stayed live with the old URL. PASS now. TestOpenPreviewNewestOnly (wire) and TestPreviewOptionNewerPreview (route, cross-check) added. Run on HEAD plus these paths only (exported tree): wire, chat and equipo targeted runs pass; vet clean. Mutations (break, run, restore), all killed: F1 selection skips non-deployed newer steps: TestOpenPreviewNewestOnly, TestDirectionNoFallbackPreview F2 state done dropped: TestOpenPreviewNewestOnly F3 outcome deployed dropped: TestOpenPreviewNewestOnly, TestDirectionNoFallbackPreview F4 URL present dropped: TestOpenPreviewNewestOnly F5 candidate check off: TestOpenPreviewNewestOnly, TestPreviewPassCapsAndDeploymentEligibility F6 merged exception off: TestOpenPreviewNewestOnly F7 expiry off: TestOpenPreviewNewestOnly, TestDirectionNoFallbackPreview, TestPreviewOptionNewerPreview F8 devbox final-stage rule off: TestOpenPreviewNewestOnly, TestReviewStateChangesBetweenWrapperReads F9 directionView falls back to any done deployed step: TestDirectionNoFallbackPreview F10 directionView passes no candidate: TestDirectionLiveRound F11 route expired not mapped: TestPreviewOptionNewerPreview, TestReviewStateChangesBetweenWrapperReads, TestPreviewPassCaps... F12 route changed not mapped: TestPreviewPassCapsAndDeploymentEligibility F13 route refuses only a missing preview: TestPreviewPassCaps...
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+224
−32
internal/equipo/chat/direction_live.go+13 −0internal/equipo/chat/direction_live_test.go+43 −1internal/equipo/chat/explore.go+6 −11internal/equipo/preview_option_test.go+29 −0internal/equipo/preview_pass_store.go+12 −17internal/equipo/work_views.go+1 −3internal/wire/preview_open.go+66 −0internal/wire/preview_open_test.go+54 −0
diff --git a/internal/equipo/chat/direction_live.go b/internal/equipo/chat/direction_live.go| index 76e75dba..27d7ac71 100644 |
| --- a/internal/equipo/chat/direction_live.go |
| +++ b/internal/equipo/chat/direction_live.go |
| @@ -21,6 +21,19 @@ func previewRefused(steps []stepRecord, candidate string) bool { |
| return false |
| } |
| +// openPreview is the preview step the wrapper (/tasks/{id}/preview) |
| +// frames for k now: wire.OpenPreview, the route's own rule, over k's |
| +// steps and its candidate as the task view has them. ok is false when |
| +// none opens, though an older preview deployed. |
| +func (t *txn) openPreview(k taskRecord, steps []stepRecord, p progress) (wire.Step, bool) { |
| + v := wire.Task{Stage: k.Stage, CandidateSHA: p.candidate, Merged: mergedView(k), DoneAt: k.DoneAt} |
| + for _, s := range steps { |
| + v.Steps = append(v.Steps, stepView(s)) |
| + } |
| + s, why := wire.OpenPreview(v, t.now()) |
| + return s, why == "" |
| +} |
| + |
| // markLive marks the kept round of candidate, whose preview is |
| // deployed, live (directionView calls it only then). |
| func markLive(d *wire.TaskDirection, candidate string) { |
diff --git a/internal/equipo/chat/direction_live_test.go b/internal/equipo/chat/direction_live_test.go| index 6a184ff0..1fbcf181 100644 |
| --- a/internal/equipo/chat/direction_live_test.go |
| +++ b/internal/equipo/chat/direction_live_test.go |
| @@ -15,10 +15,15 @@ import ( |
| // previewOf records a preview step of task on sha, newest, ended in |
| // state with reason and outcome. |
| func (r *hkRig) previewOf(task, sha, state, reason, outcome string) { |
| + r.t.Helper() |
| + r.previewUntil(task, sha, state, reason, outcome, r.s.now().Add(time.Hour)) |
| +} |
| + |
| +// previewUntil is previewOf with the link expiring at expires. |
| +func (r *hkRig) previewUntil(task, sha, state, reason, outcome string, expires time.Time) { |
| r.t.Helper() |
| var last stepRecord |
| r.must(r.s.db.G.Where("task_id = ?", task).Order("idx DESC").First(&last).Error) |
| - expires := time.Now().Add(time.Hour) |
| raw, _ := json.Marshal(wire.PreviewResult{Phase: wire.PreviewDone, Outcome: outcome, URL: "https://eq-" + task + ".dev.example/", App: "eq-" + task, Digest: digestA, ExpiresAt: &expires}) |
| s := stepRecord{ID: newID("s_"), TaskID: task, Idx: last.Idx + 1, Specialist: wire.SpecialistPreview, State: state, Reason: reason, Attempt: 1, CandidateSHA: sha, Terminated: true, Preview: string(raw), Revision: 1} |
| r.must(r.s.db.G.Create(&s).Error) |
| @@ -104,3 +109,40 @@ func TestDirectionPreviewRefused(t *testing.T) { |
| t.Fatalf("another reason: %+v", v.Direction) |
| } |
| } |
| + |
| +// TestDirectionNoFallbackPreview (review fix round 1): an older deployed |
| +// preview of the candidate followed by a newer one that is running, |
| +// failed, unconfirmed for another reason than headers, or expired opens |
| +// nothing: the wrapper frames only the newest preview step |
| +// (wire.OpenPreview), so no round is live and there is no preview link. |
| +func TestDirectionNoFallbackPreview(t *testing.T) { |
| + for _, newer := range []struct { |
| + name, state, reason, outcome string |
| + expired bool |
| + }{ |
| + {"running", wire.StepRunning, "", "", false}, |
| + {"failed", wire.StepFailed, "preview_failed", "", false}, |
| + {"unconfirmed", wire.StepDone, "preview_host", wire.PreviewUnconfirmed, false}, |
| + {"expired", wire.StepDone, "", wire.PreviewDeployed, true}, |
| + } { |
| + t.Run(newer.name, func(t *testing.T) { |
| + r, _, task, first, _ := roundOfThree(t) |
| + r.previewOf(task, candidateSHA, wire.StepDone, "", wire.PreviewDeployed) |
| + v, err := r.s.Task(r.ctx, paul, task) |
| + r.must(err) |
| + if live := liveRounds(v.Direction); !live[first.ID] || v.Direction.PreviewURL == "" { |
| + t.Fatalf("baseline: %v %+v", live, v.Direction) |
| + } |
| + expires := r.s.now().Add(time.Hour) |
| + if newer.expired { |
| + expires = r.s.now().Add(-time.Minute) |
| + } |
| + r.previewUntil(task, candidateSHA, newer.state, newer.reason, newer.outcome, expires) |
| + v, err = r.s.Task(r.ctx, paul, task) |
| + r.must(err) |
| + if d := v.Direction; len(liveRounds(d)) != 0 || d.PreviewURL != "" || d.PreviewRefused { |
| + t.Fatalf("an older deployment still opens: %v %+v", liveRounds(d), d) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/equipo/chat/explore.go b/internal/equipo/chat/explore.go| index c56d5789..2ea573a4 100644 |
| --- a/internal/equipo/chat/explore.go |
| +++ b/internal/equipo/chat/explore.go |
| @@ -603,17 +603,12 @@ func (t *txn) directionView(k taskRecord, repo string, steps []stepRecord, p pro |
| // The host refused the newest preview for its headers: no link. |
| return d, nil |
| } |
| - now := t.now() |
| - for i := len(steps) - 1; i >= 0; i-- { |
| - s := steps[i] |
| - if s.Specialist != wire.SpecialistPreview || s.CandidateSHA != p.candidate || s.State != wire.StepDone { |
| - continue |
| - } |
| - if r := decodePreview(s.Preview); r != nil && r.Outcome == wire.PreviewDeployed && r.URL != "" && (r.ExpiresAt == nil || r.ExpiresAt.After(now)) { |
| - d.PreviewURL = r.URL |
| - markLive(d, p.candidate) |
| - return d, nil |
| - } |
| + // Only the preview the wrapper frames (wire.OpenPreview, the newest |
| + // preview step): never an older deployment behind a newer one. |
| + if s, ok := t.openPreview(k, steps, p); ok { |
| + d.PreviewURL = s.Preview.URL |
| + markLive(d, s.CandidateSHA) |
| + return d, nil |
| } |
| off, err := t.previewsOff(k.Creator) |
| d.PreviewsOff = off |
diff --git a/internal/equipo/preview_option_test.go b/internal/equipo/preview_option_test.go| index 6381d612..6b494e2b 100644 |
| --- a/internal/equipo/preview_option_test.go |
| +++ b/internal/equipo/preview_option_test.go |
| @@ -6,6 +6,7 @@ import ( |
| "net/http/httptest" |
| "strings" |
| "testing" |
| + "time" |
| "amadan.net/oficina/equipo/internal/equipo/identity" |
| "amadan.net/oficina/equipo/internal/wire" |
| @@ -187,6 +188,34 @@ func TestPreviewOptionStepNotKept(t *testing.T) { |
| } |
| } |
| +// TestPreviewOptionNewerPreview (review fix round 1): behind a newer |
| +// preview of the same candidate that is running, failed, unconfirmed or |
| +// expired, the older deployment frames nothing, root or option, the |
| +// same rule (wire.OpenPreview) the direction's Live follows. |
| +func TestPreviewOptionNewerPreview(t *testing.T) { |
| + for _, c := range []struct{ name, state, reason, outcome, expires string }{ |
| + {"running", "running", "", "", "+1h"}, |
| + {"failed", "failed", "preview_failed", "", "+1h"}, |
| + {"unconfirmed", "done", "preview_host", wire.PreviewUnconfirmed, "+1h"}, |
| + {"expired", "done", "", wire.PreviewDeployed, "-1m"}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + r := optionRig(t) |
| + at, _ := time.ParseDuration(strings.Replace(c.expires, "+", "", 1)) |
| + expires := r.clock.Now().Add(at) |
| + raw, _ := json.Marshal(wire.PreviewResult{Phase: wire.PreviewDone, Outcome: c.outcome, URL: "https://eq-preview.example.test/", App: "eq-preview", Digest: strings.Repeat("a", 64), ExpiresAt: &expires}) |
| + if _, e := r.s.db.Writer().Exec("INSERT INTO chat_steps(id,task_id,idx,specialist,state,reason,attempt,candidate_sha,preview,revision) VALUES('preview_newer','tk_preview',2,'preview',?,?,1,?,?,1)", c.state, c.reason, passCandidate, string(raw)); e != nil { |
| + t.Fatal(e) |
| + } |
| + for _, path := range []string{"/tasks/tk_preview/preview", "/tasks/tk_preview/preview?step=eng_preview&option=2"} { |
| + if w := browserGet(t, r, path); w.Code == 200 || strings.Contains(w.Body.String(), "<iframe") { |
| + t.Errorf("%s: %d %s", path, w.Code, w.Body) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| // TestPreviewOldLinkAfterRedeploy: once round 2's candidate is the |
| // deployed preview, round 1's link is 404 and round 2's frames. |
| func TestPreviewOldLinkAfterRedeploy(t *testing.T) { |
diff --git a/internal/equipo/preview_pass_store.go b/internal/equipo/preview_pass_store.go| index 27051617..40bd6567 100644 |
| --- a/internal/equipo/preview_pass_store.go |
| +++ b/internal/equipo/preview_pass_store.go |
| @@ -86,17 +86,14 @@ func (s *Store) previewTask(tx *gorm.DB, b previewBinding) (wire.Task, error) { |
| } |
| return task, nil |
| } |
| -func selectedPreview(task wire.Task) (wire.Step, bool) { |
| - for i := len(task.Steps) - 1; i >= 0; i-- { |
| - if task.Steps[i].Specialist == wire.SpecialistPreview { |
| - return task.Steps[i], true |
| - } |
| - } |
| - return wire.Step{}, false |
| -} |
| + |
| +// previewDeployment is the task's newest preview step when it is a |
| +// done, deployed preview the wrapper may frame (wire.OpenPreview, the |
| +// rule the direction's Live link follows too), with its fingerprint. |
| +// Its candidate and expiry are validatePreview's. |
| func (s *Store) previewDeployment(task wire.Task) (wire.Step, string, error) { |
| - step, ok := selectedPreview(task) |
| - if !ok || step.Preview == nil || step.Preview.Outcome != wire.PreviewDeployed || step.Preview.URL == "" { |
| + step, why := wire.OpenPreview(task, s.runnerClock.Now()) |
| + if why == wire.PreviewOpenNone { |
| return step, "", ErrPreviewAccess |
| } |
| p := step.Preview |
| @@ -114,15 +111,13 @@ func (s *Store) previewDeployment(task wire.Task) (wire.Step, string, error) { |
| } |
| func (s *Store) validatePreview(task wire.Task, b previewBinding) error { |
| step, fingerprint, err := s.previewDeployment(task) |
| - // Of the task's candidate, or, once it is done, of its merged commit |
| - // (a preview run again after the merge). |
| - merged := task.Stage == wire.StageDone && task.Merged != nil && task.Merged.SHA != "" && step.CandidateSHA == task.Merged.SHA |
| - if task.CandidateSHA != b.Candidate || err != nil || step.CandidateSHA != b.Candidate && !merged || step.ID != b.StepID || step.Attempt != b.Attempt || fingerprint != b.Fingerprint { |
| + // Of the task's candidate (the binding's), or, once it is done, of |
| + // its merged commit; then not expired (wire.OpenPreview). |
| + _, why := wire.OpenPreview(task, s.runnerClock.Now()) |
| + if task.CandidateSHA != b.Candidate || err != nil || why == wire.PreviewOpenChanged || step.ID != b.StepID || step.Attempt != b.Attempt || fingerprint != b.Fingerprint { |
| return ErrPreviewChanged |
| } |
| - // A devbox link comes down with its task, unless it came up after the |
| - // task was done (a preview run again on a merged task). |
| - if (step.Preview.ExpiresAt != nil && !s.runnerClock.Now().Before(*step.Preview.ExpiresAt)) || (strings.HasPrefix(step.Preview.App, "eq-") && wire.FinalStage(task.Stage) && !devboxAfterDone(task, step)) { |
| + if why == wire.PreviewOpenExpired { |
| return ErrPreviewExpired |
| } |
| return nil |
diff --git a/internal/equipo/work_views.go b/internal/equipo/work_views.go| index 2e987d57..c46784e5 100644 |
| --- a/internal/equipo/work_views.go |
| +++ b/internal/equipo/work_views.go |
| @@ -1393,9 +1393,7 @@ func previewShown(t wire.Task, s wire.Step, now time.Time) bool { |
| // devboxAfterDone: a devbox link that came up after its task was done (a |
| // preview run again on a merged task); any other comes down with the |
| // task. It stays up until the task's next preview replaces it. |
| -func devboxAfterDone(t wire.Task, s wire.Step) bool { |
| - return s.Preview != nil && strings.HasPrefix(s.Preview.App, "eq-") && t.Stage == wire.StageDone && t.DoneAt != nil && s.FinishedAt != nil && s.FinishedAt.After(*t.DoneAt) |
| -} |
| +func devboxAfterDone(t wire.Task, s wire.Step) bool { return wire.DevboxAfterDone(t, s) } |
| // sentenceCase capitalises a reason fragment for a line of its own. |
| func sentenceCase(s string) string { |
diff --git a/internal/wire/preview_open.go b/internal/wire/preview_open.go| new file mode 100644 |
| index 00000000..e67d1d8e |
| --- /dev/null |
| +++ b/internal/wire/preview_open.go |
| @@ -0,0 +1,66 @@ |
| +package wire |
| + |
| +import ( |
| + "strings" |
| + "time" |
| +) |
| + |
| +// The preview the instance's wrapper (/tasks/{id}/preview) frames for a |
| +// task, one rule for the route and for every view that links it (the |
| +// direction's preview link and its live round, spec 8.5): the task's |
| +// newest preview step, whatever its state, and never an older one. A |
| +// newer preview that is running, failed, unconfirmed or expired means |
| +// none opens, though an older one deployed. |
| + |
| +// Why a task's preview does not open (OpenPreview). |
| +const ( |
| + // PreviewOpenNone: no preview step, or the newest is not a done, |
| + // deployed preview with a URL. |
| + PreviewOpenNone = "none" |
| + // PreviewOpenChanged: it is of another commit than the task's |
| + // candidate (or, once the task is done, its merged commit). |
| + PreviewOpenChanged = "changed" |
| + // PreviewOpenExpired: its link expired, or a devbox link came down |
| + // with its ended task. |
| + PreviewOpenExpired = "expired" |
| +) |
| + |
| +// SelectedPreview is the task's newest preview step, whatever its state. |
| +func SelectedPreview(steps []Step) (Step, bool) { |
| + for i := len(steps) - 1; i >= 0; i-- { |
| + if steps[i].Specialist == SpecialistPreview { |
| + return steps[i], true |
| + } |
| + } |
| + return Step{}, false |
| +} |
| + |
| +// OpenPreview is the preview step the wrapper frames for t at now, with |
| +// "" as the reason; or the newest preview step (if any) and why it does |
| +// not open, in that order: PreviewOpenNone, PreviewOpenChanged, |
| +// PreviewOpenExpired. It reads t's Steps, CandidateSHA, Stage, Merged and |
| +// DoneAt only. |
| +func OpenPreview(t Task, now time.Time) (Step, string) { |
| + s, ok := SelectedPreview(t.Steps) |
| + if !ok || s.State != StepDone || s.Preview == nil || s.Preview.Outcome != PreviewDeployed || s.Preview.URL == "" { |
| + return s, PreviewOpenNone |
| + } |
| + // Of the task's candidate, or, once it is done, of its merged commit |
| + // (a preview run again after the merge). |
| + merged := t.Stage == StageDone && t.Merged != nil && t.Merged.SHA != "" && s.CandidateSHA == t.Merged.SHA |
| + if s.CandidateSHA != t.CandidateSHA && !merged { |
| + return s, PreviewOpenChanged |
| + } |
| + // A devbox link comes down with its task, unless it came up after the |
| + // task was done (a preview run again on a merged task). |
| + if s.Preview.ExpiresAt != nil && !now.Before(*s.Preview.ExpiresAt) || strings.HasPrefix(s.Preview.App, "eq-") && FinalStage(t.Stage) && !DevboxAfterDone(t, s) { |
| + return s, PreviewOpenExpired |
| + } |
| + return s, "" |
| +} |
| + |
| +// DevboxAfterDone: s is a devbox preview that came up after its task was |
| +// done (a preview run again on a merged task). |
| +func DevboxAfterDone(t Task, s Step) bool { |
| + return s.Preview != nil && strings.HasPrefix(s.Preview.App, "eq-") && t.Stage == StageDone && t.DoneAt != nil && s.FinishedAt != nil && s.FinishedAt.After(*t.DoneAt) |
| +} |
diff --git a/internal/wire/preview_open_test.go b/internal/wire/preview_open_test.go| new file mode 100644 |
| index 00000000..4a7760a2 |
| --- /dev/null |
| +++ b/internal/wire/preview_open_test.go |
| @@ -0,0 +1,54 @@ |
| +package wire |
| + |
| +import ( |
| + "testing" |
| + "time" |
| +) |
| + |
| +// TestOpenPreviewNewestOnly: the wrapper's preview is the newest preview |
| +// step, never an older deployment behind a newer one that does not open. |
| +func TestOpenPreviewNewestOnly(t *testing.T) { |
| + now := time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC) |
| + later, earlier := now.Add(time.Hour), now.Add(-time.Minute) |
| + cand, other := "1111111111111111111111111111111111111111", "2222222222222222222222222222222222222222" |
| + deployed := func(id, sha string, expires *time.Time) Step { |
| + return Step{ID: id, Specialist: SpecialistPreview, State: StepDone, CandidateSHA: sha, Preview: &PreviewResult{Outcome: PreviewDeployed, URL: "https://tmp-x.example/", App: "tmp-x", ExpiresAt: expires}} |
| + } |
| + maker := Step{ID: "eng", Specialist: SpecialistEngineering, State: StepDone, CandidateSHA: cand} |
| + old := deployed("old", cand, &later) |
| + for _, c := range []struct { |
| + name string |
| + steps []Step |
| + id string |
| + why string |
| + }{ |
| + {"none", []Step{maker}, "", PreviewOpenNone}, |
| + {"deployed", []Step{maker, old}, "old", ""}, |
| + {"newer running", []Step{maker, old, {ID: "new", Specialist: SpecialistPreview, State: StepRunning, CandidateSHA: cand}}, "new", PreviewOpenNone}, |
| + {"newer failed", []Step{maker, old, {ID: "new", Specialist: SpecialistPreview, State: StepFailed, Reason: "preview_failed", CandidateSHA: cand}}, "new", PreviewOpenNone}, |
| + {"newer unconfirmed", []Step{maker, old, {ID: "new", Specialist: SpecialistPreview, State: StepDone, Reason: "preview_host", CandidateSHA: cand, Preview: &PreviewResult{Outcome: PreviewUnconfirmed, URL: "https://tmp-x.example/"}}}, "new", PreviewOpenNone}, |
| + {"deployed, not done", []Step{maker, {ID: "new", Specialist: SpecialistPreview, State: StepRunning, CandidateSHA: cand, Preview: old.Preview}}, "new", PreviewOpenNone}, |
| + {"no URL", []Step{maker, {ID: "new", Specialist: SpecialistPreview, State: StepDone, CandidateSHA: cand, Preview: &PreviewResult{Outcome: PreviewDeployed}}}, "new", PreviewOpenNone}, |
| + {"newer expired", []Step{maker, old, deployed("new", cand, &earlier)}, "new", PreviewOpenExpired}, |
| + {"another candidate", []Step{maker, deployed("new", other, &later)}, "new", PreviewOpenChanged}, |
| + {"no expiry", []Step{maker, deployed("new", cand, nil)}, "new", ""}, |
| + } { |
| + s, why := OpenPreview(Task{Stage: StageInReview, CandidateSHA: cand, Steps: c.steps}, now) |
| + if s.ID != c.id || why != c.why { |
| + t.Errorf("%s: %q %q, want %q %q", c.name, s.ID, why, c.id, c.why) |
| + } |
| + } |
| + // A devbox link comes down with its ended task, unless it came up |
| + // after the task was done on its merged commit. |
| + done := now.Add(-time.Hour) |
| + devbox := deployed("dev", cand, nil) |
| + devbox.Preview.App = "eq-x" |
| + if _, why := OpenPreview(Task{Stage: StageDone, CandidateSHA: cand, DoneAt: &done, Steps: []Step{devbox}}, now); why != PreviewOpenExpired { |
| + t.Errorf("devbox on a done task: %q", why) |
| + } |
| + after := now.Add(-time.Minute) |
| + devbox.CandidateSHA, devbox.FinishedAt = other, &after |
| + if _, why := OpenPreview(Task{Stage: StageDone, CandidateSHA: cand, DoneAt: &done, Merged: &Merged{SHA: other}, Steps: []Step{devbox}}, now); why != "" { |
| + t.Errorf("devbox run again on the merged commit: %q", why) |
| + } |
| +} |