| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // designRenders are a Design attempt's render records: a rendered work |
| 13 | // call and a compare that ran out of time. |
| 14 | func designRenders() []wire.RenderAudit { |
| 15 | return []wire.RenderAudit{ |
| 16 | {Call: 1, Attempt: 1, Tree: wire.RenderTreeWork, Pages: 2, Captures: 2, Files: 2, DurationMS: 4100, Outcome: wire.RenderRendered}, |
| 17 | {Call: 2, Attempt: 1, Tree: wire.RenderTreeCompare, Pages: 1, Captures: 2, DurationMS: 60001, Outcome: "timeout"}, |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | // stepFrames numbers applyStep's frames: each frame id once. |
| 22 | var stepFrames int64 |
| 23 | |
| 24 | // applyStep sends a step report through the runner frame's own path. |
| 25 | func (r *hkRig) applyStep(l Lease, s stepRecord, v wire.StepReport) { |
| 26 | r.t.Helper() |
| 27 | v.TaskID, v.StepID = s.TaskID, s.ID |
| 28 | if v.Attempt == 0 { |
| 29 | v.Attempt = s.Attempt |
| 30 | } |
| 31 | if v.Findings == nil { |
| 32 | v.Findings = []wire.Finding{} |
| 33 | } |
| 34 | stepFrames++ |
| 35 | f, err := wire.NewFrame("sr-"+itoa64(stepFrames), wire.FrameStep, v) |
| 36 | r.must(err) |
| 37 | r.must(r.s.Apply(r.ctx, l, f, &v)) |
| 38 | } |
| 39 | |
| 40 | // TestDesignRenderRecordsKept (branch review finding 8): a Design |
| 41 | // attempt's report keeps its render records on the step as plain data, |
| 42 | // for its attempt, from the runner it is placed on, when the wire takes |
| 43 | // them; a later report of the attempt replaces them, and a report |
| 44 | // without any leaves them. Anything else keeps nothing, and the report |
| 45 | // itself still applies. |
| 46 | func TestDesignRenderRecordsKept(t *testing.T) { |
| 47 | r, l, _, _, s := designRunning(t) |
| 48 | r.applyStep(l, s, wire.StepReport{State: wire.StepRunning, BaseSHA: strings.Repeat("b", 40), CandidateSHA: candidateSHA, Renders: designRenders()[:1]}) |
| 49 | if got := r.stepRenders(s.ID, s.Attempt); !slices.Equal(got, designRenders()[:1]) { |
| 50 | t.Fatalf("kept %+v", got) |
| 51 | } |
| 52 | // Refused: records the wire refuses, another attempt's, another |
| 53 | // runner's. |
| 54 | bad := designRenders() |
| 55 | bad[1].Files = 3 |
| 56 | r.applyStep(l, s, wire.StepReport{State: wire.StepRunning, BaseSHA: strings.Repeat("b", 40), CandidateSHA: candidateSHA, Renders: bad}) |
| 57 | other := l |
| 58 | other.Runner.RunnerID = "RUNNER2" |
| 59 | r.must(r.tx(func(tx *txn) error { |
| 60 | return tx.keepRenders(other, &wire.StepReport{TaskID: s.TaskID, StepID: s.ID, Attempt: s.Attempt, Renders: designRenders()}) |
| 61 | })) |
| 62 | r.must(r.tx(func(tx *txn) error { |
| 63 | return tx.keepRenders(l, &wire.StepReport{TaskID: s.TaskID, StepID: s.ID, Attempt: s.Attempt + 2, Renders: designRenders()}) |
| 64 | })) |
| 65 | r.must(r.tx(func(tx *txn) error { |
| 66 | return tx.keepRenders(l, &wire.StepReport{TaskID: "tk-other", StepID: s.ID, Attempt: s.Attempt, Renders: designRenders()}) |
| 67 | })) |
| 68 | if got := r.stepRenders(s.ID, s.Attempt); !slices.Equal(got, designRenders()[:1]) { |
| 69 | t.Fatalf("after refused records %+v", got) |
| 70 | } |
| 71 | if n := r.count("SELECT count(*) FROM chat_step_renders"); n != 1 { |
| 72 | t.Fatalf("%d rows", n) |
| 73 | } |
| 74 | // The final report, failed, replaces them with all of the attempt's. |
| 75 | r.applyStep(l, s, wire.StepReport{State: wire.StepFailed, Reason: "model_failed", Termination: true, Renders: designRenders()}) |
| 76 | if got := r.stepRenders(s.ID, s.Attempt); !slices.Equal(got, designRenders()) { |
| 77 | t.Fatalf("after the final report %+v", got) |
| 78 | } |
| 79 | if st := r.stepNow(s.ID); st.State != wire.StepFailed { |
| 80 | t.Fatalf("the report did not apply: %s", st.State) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // stepRenders are the render records kept for a step's attempt. |
| 85 | func (r *hkRig) stepRenders(step string, attempt int64) []wire.RenderAudit { |
| 86 | r.t.Helper() |
| 87 | got, err := r.s.stepRenders(r.ctx, step, attempt) |
| 88 | r.must(err) |
| 89 | return got |
| 90 | } |
| 91 | |
| 92 | // TestDesignRenderRecordsAcrossFailovers (branch review round 2, finding |
| 93 | // 8): records are kept by the attempt they belong to. A failover under |
| 94 | // the limit carries the ending attempt's records, kept for it while the |
| 95 | // step runs on; its final report then carries both attempts', each kept |
| 96 | // for its own. A failover at the autonomy limit carries the third |
| 97 | // attempt's records: they are kept for it, though the instance then |
| 98 | // holds the replacement off the runner, and the failover attempt's |
| 99 | // cancelled end, which the instance no longer takes from that runner, |
| 100 | // changes nothing. |
| 101 | func TestDesignRenderRecordsAcrossFailovers(t *testing.T) { |
| 102 | r, l, _ := grantRig(t) |
| 103 | sp := r.active(l, "Patrix", "p_patrix") |
| 104 | task, s := r.runningDesign(l, sp, wire.KindChange) |
| 105 | first := wire.RenderAudit{Call: 1, Attempt: s.Attempt, Tree: wire.RenderTreeWork, Pages: 1, Captures: 1, Files: 1, DurationMS: 800, Outcome: wire.RenderRendered} |
| 106 | // A record of the attempt the advance starts is of one that has not |
| 107 | // run: not kept. |
| 108 | early := wire.RenderAudit{Call: 2, Attempt: s.Attempt + 1, Tree: wire.RenderTreeWork, Pages: 1, Captures: 1, Outcome: "busy"} |
| 109 | r.applyStep(l, s, wire.StepReport{Attempt: s.Attempt + 1, State: wire.StepRunning, Renders: []wire.RenderAudit{first, early}}) |
| 110 | moved := r.stepNow(s.ID) |
| 111 | if moved.Attempt != s.Attempt+1 || moved.State != wire.StepRunning { |
| 112 | t.Fatalf("the failover %+v", moved) |
| 113 | } |
| 114 | if got := r.stepRenders(s.ID, s.Attempt); !slices.Equal(got, []wire.RenderAudit{first}) { |
| 115 | t.Fatalf("the ending attempt's records %+v", got) |
| 116 | } |
| 117 | if got := r.stepRenders(s.ID, moved.Attempt); got != nil { |
| 118 | t.Fatalf("the starting attempt's records %+v", got) |
| 119 | } |
| 120 | second := wire.RenderAudit{Call: 2, Attempt: moved.Attempt, Tree: wire.RenderTreeBase, Pages: 1, Captures: 1, DurationMS: 30, Outcome: "no_site"} |
| 121 | r.applyStep(l, moved, wire.StepReport{State: wire.StepFailed, Reason: "model_failed", Termination: true, Renders: []wire.RenderAudit{first, second}}) |
| 122 | if got := r.stepRenders(s.ID, s.Attempt); !slices.Equal(got, []wire.RenderAudit{first}) { |
| 123 | t.Fatalf("the first attempt's after the end %+v", got) |
| 124 | } |
| 125 | if got := r.stepRenders(s.ID, moved.Attempt); !slices.Equal(got, []wire.RenderAudit{second}) { |
| 126 | t.Fatalf("the second attempt's %+v", got) |
| 127 | } |
| 128 | // At the limit. |
| 129 | third := r.failAndRetry(l, task) |
| 130 | r.ackDispatches(third.ID) |
| 131 | if refused := r.authorize(l, third, ""); refused != "" { |
| 132 | t.Fatalf("third start %q", refused) |
| 133 | } |
| 134 | third = r.stepNow(third.ID) |
| 135 | r.exec("UPDATE chat_tasks SET design_runs = ? WHERE id = ?", wire.DesignRunLimit, task) |
| 136 | rendered := wire.RenderAudit{Call: 1, Attempt: third.Attempt, Tree: wire.RenderTreeCompare, Pages: 1, Captures: 2, Files: 3, DurationMS: 5000, Outcome: wire.RenderRendered} |
| 137 | r.applyStep(l, third, wire.StepReport{Attempt: third.Attempt + 1, State: wire.StepRunning, Renders: []wire.RenderAudit{rendered}}) |
| 138 | held := r.stepNow(third.ID) |
| 139 | if held.State != wire.StepQueued || held.Placement != wire.PlacementDesignLimit || held.RunnerID != "" { |
| 140 | t.Fatalf("not held at the limit %+v", held) |
| 141 | } |
| 142 | if got := r.stepRenders(third.ID, third.Attempt); !slices.Equal(got, []wire.RenderAudit{rendered}) { |
| 143 | t.Fatalf("the held step's ending attempt's records %+v", got) |
| 144 | } |
| 145 | end := wire.StepReport{TaskID: task, StepID: third.ID, Attempt: third.Attempt + 1, State: wire.StepCancelled, Reason: "withdrawn", Termination: true, Findings: []wire.Finding{}, Renders: []wire.RenderAudit{{Call: 1, Attempt: third.Attempt + 1, Tree: wire.RenderTreeWork, Pages: 1, Captures: 1, Outcome: "busy"}}} |
| 146 | f, err := wire.NewFrame("sr-end", wire.FrameStep, end) |
| 147 | r.must(err) |
| 148 | if err := r.s.Apply(r.ctx, l, f, &end); err != nil && !errors.Is(err, ErrRefused) { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | if got := r.stepRenders(third.ID, third.Attempt+1); got != nil { |
| 152 | t.Fatalf("the voided attempt's records %+v", got) |
| 153 | } |
| 154 | if got := r.stepRenders(third.ID, third.Attempt); !slices.Equal(got, []wire.RenderAudit{rendered}) { |
| 155 | t.Fatalf("after the voided attempt's end %+v", got) |
| 156 | } |
| 157 | } |
| 158 | |