package thintest import ( "io" "log/slog" "strings" "testing" "amadan.net/oficina/equipo/internal/equipo/chat" "amadan.net/oficina/equipo/internal/wire" "amadan.net/rastrillo/rastrillo/db" ) // slice4 strips the runners migration from a stopped instance's database, // leaving the schema and rows a slice 4 instance would have left. func slice4(t *testing.T, path string) { t.Helper() d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) if err != nil { t.Fatal(err) } defer d.Close() for _, q := range []string{ "DROP INDEX runner_command_runner", "DROP INDEX chat_step_runner", "DROP TABLE runner_transfers", "ALTER TABLE runners DROP COLUMN role", "ALTER TABLE runners DROP COLUMN removing_at", "ALTER TABLE runner_commands DROP COLUMN runner_id", "ALTER TABLE runner_sessions DROP COLUMN protocol", "ALTER TABLE chat_tasks DROP COLUMN engineering_runner", "ALTER TABLE chat_steps DROP COLUMN runner_id", "ALTER TABLE chat_steps DROP COLUMN needs", "ALTER TABLE chat_steps DROP COLUMN placement", "ALTER TABLE chat_steps DROP COLUMN waiting_since", "ALTER TABLE chat_steps DROP COLUMN refused_by", "ALTER TABLE chat_steps DROP COLUMN withdrawn", "ALTER TABLE chat_steps DROP COLUMN termination_kind", "ALTER TABLE chat_steps DROP COLUMN publication", "ALTER TABLE chat_steps DROP COLUMN publication_id", "ALTER TABLE chat_steps DROP COLUMN apple", "DELETE FROM rastrillo_migrations WHERE id LIKE 'equipo/0054_%'", } { if _, err := d.Writer().Exec(q); err != nil { t.Fatalf("%s: %v", q, err) } } } // Migration 0054 over a slice 4 database with live work (a running step, // an attempt awaiting its termination proof, a dispatch the runner never // acked, a running turn) binds it all to the primary, and the runner that // held it carries on as before: the dispatch is delivered, the reports // are accepted, Retry still waits for termination. func TestContractMigrationOverSlice4Work(t *testing.T) { r := newToolRig(t) ctx := r.ctx running := taskID(t, r.call("m1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Running when the upgrade lands", "brief": "b"})) r.call("m2", wire.ToolStartTask, map[string]string{"task": running, "brief": "go"}) _, body := r.expect(wire.FrameDispatch) d1 := body.(*wire.DispatchCommand) r.step("r-m1", wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) pending := taskID(t, r.call("m3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Awaiting its proof", "brief": "b"})) r.call("m4", wire.ToolStartTask, map[string]string{"task": pending, "brief": "go"}) _, body = r.expect(wire.FrameDispatch) d2 := body.(*wire.DispatchCommand) r.step("r-m2", wire.StepReport{TaskID: pending, StepID: d2.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", BaseSHA: strings.Repeat("1", 40)}) unacked := taskID(t, r.call("m5", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Never acked", "brief": "b"})) r.call("m6", wire.ToolStartTask, map[string]string{"task": unacked, "brief": "go"}) r.runner.Close() r.inst.Stop() slice4(t, r.inst.Path) r.inst.Restart(t) n, err := chat.UnboundSteps(r.inst.DB.G) if err != nil || n != 0 { t.Fatalf("unbound steps: %d %v", n, err) } // The same runner reconnects: the unacked dispatch is still its own. r.runner = r.dial(r.runner.Welcome.Generation) f, body, err := r.runner.Expect(ctx, wire.FrameDispatch) for err == nil && body.(*wire.DispatchCommand).TaskID != unacked { f, body, err = r.runner.Expect(ctx, wire.FrameDispatch) } if err != nil { t.Fatalf("unacked dispatch lost in the upgrade: %v", err) } shape(t, "frames/dispatch.json", r.runner.Received[len(r.runner.Received)-1]) if err := r.runner.Send(ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { t.Fatal(err) } // Its sync holds the running step: nothing is settled. if err := r.runner.Send(ctx, "r-sync-m", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{{TurnID: r.turn, Attempt: 1, State: "running"}}, Steps: []wire.StepRun{{TaskID: running, StepID: d1.StepID, Attempt: 1, State: "running"}, {TaskID: pending, StepID: d2.StepID, Attempt: 1, State: "pending"}, {TaskID: unacked, StepID: body.(*wire.DispatchCommand).StepID, Attempt: 1, State: "running"}}}); err != nil { t.Fatal(err) } // Retry still waits for the termination proof. if _, err := r.client.RetryTask(ctx, pending); Status(err) != 409 { t.Fatalf("retry before termination after the upgrade: %v", err) } r.step("r-m3", wire.StepReport{TaskID: pending, StepID: d2.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) if acc, err := r.client.RetryTask(ctx, pending); err != nil || acc.Attempt != 2 { t.Fatalf("retry after the proof: %+v %v", acc, err) } // The running step's report is accepted from the runner it was bound to. r.step("r-m4", wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) if k, err := r.client.Task(ctx, running); err != nil || k.Steps[0].State != wire.StepFailed || !k.Steps[0].Terminated { t.Fatalf("the running step's report after the upgrade: %+v %v", k, err) } // The turn that was running is still the primary's. if k, _ := r.client.Turn(ctx, r.turn); k.State != wire.TurnRunning { t.Fatalf("turn after the upgrade: %+v", k) } }