TestEditMessageDebugCleanupPreservesRecentRuns verifies that the clock-skew buffer in the edit-cleanup cutoff prevents the fast retry from deleting debug runs that started within the buffer window. The stale sweep handles those leftovers later.
(t *testing.T)
| 3528 | // retry from deleting debug runs that started within the buffer |
| 3529 | // window. The stale sweep handles those leftovers later. |
| 3530 | func TestEditMessageDebugCleanupPreservesRecentRuns(t *testing.T) { |
| 3531 | t.Parallel() |
| 3532 | |
| 3533 | db, ps := dbtestutil.NewDB(t) |
| 3534 | replica := newDebugEnabledTestServer(t, db, ps, uuid.New()) |
| 3535 | |
| 3536 | ctx := testutil.Context(t, testutil.WaitLong) |
| 3537 | user, org, model := seedChatDependencies(t, db) |
| 3538 | |
| 3539 | chat, err := replica.CreateChat(ctx, chatd.CreateOptions{ |
| 3540 | OrganizationID: org.ID, |
| 3541 | OwnerID: user.ID, |
| 3542 | Title: "debug-edit-buffer", |
| 3543 | ModelConfigID: model.ID, |
| 3544 | InitialUserContent: []codersdk.ChatMessagePart{codersdk.ChatMessageText("first")}, |
| 3545 | }) |
| 3546 | require.NoError(t, err) |
| 3547 | |
| 3548 | msgs, err := db.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{ |
| 3549 | ChatID: chat.ID, AfterID: 0, |
| 3550 | }) |
| 3551 | require.NoError(t, err) |
| 3552 | require.Len(t, msgs, 1) |
| 3553 | editedMsgID := msgs[0].ID |
| 3554 | |
| 3555 | // Within the 30s skew buffer, so the fast retry must leave it |
| 3556 | // alone even though its message ID matches the delete filter. |
| 3557 | recentStart := time.Now().Add(-time.Second).UTC().Truncate(time.Microsecond) |
| 3558 | recentRun, err := db.InsertChatDebugRun(ctx, database.InsertChatDebugRunParams{ |
| 3559 | ChatID: chat.ID, |
| 3560 | ModelConfigID: uuid.NullUUID{UUID: model.ID, Valid: true}, |
| 3561 | TriggerMessageID: sql.NullInt64{Int64: editedMsgID, Valid: true}, |
| 3562 | HistoryTipMessageID: sql.NullInt64{Int64: editedMsgID, Valid: true}, |
| 3563 | Kind: "chat_turn", |
| 3564 | Status: "in_progress", |
| 3565 | Provider: sql.NullString{String: "openai", Valid: true}, |
| 3566 | Model: sql.NullString{String: model.Model, Valid: true}, |
| 3567 | StartedAt: sql.NullTime{Time: recentStart, Valid: true}, |
| 3568 | UpdatedAt: sql.NullTime{Time: recentStart, Valid: true}, |
| 3569 | }) |
| 3570 | require.NoError(t, err) |
| 3571 | |
| 3572 | _, err = replica.EditMessage(ctx, chatd.EditMessageOptions{ |
| 3573 | ChatID: chat.ID, |
| 3574 | EditedMessageID: editedMsgID, |
| 3575 | Content: []codersdk.ChatMessagePart{codersdk.ChatMessageText("edited")}, |
| 3576 | }) |
| 3577 | require.NoError(t, err) |
| 3578 | |
| 3579 | chatd.WaitUntilIdleForTest(replica) |
| 3580 | |
| 3581 | remaining, err := db.GetChatDebugRunByID(ctx, recentRun.ID) |
| 3582 | require.NoError(t, err, |
| 3583 | "runs inside the clock-skew buffer must survive the fast retry") |
| 3584 | require.Equal(t, recentRun.ID, remaining.ID) |
| 3585 | |
| 3586 | // If the clock-skew buffer were removed the fast retry would |
| 3587 | // have deleted recentRun. Verify the count of seeded survivors |
nothing calls this directly
no test coverage detected