Files
kandev/apps/backend/internal/office/repository/sqlite/runs_test.go
T
Carlos Florêncio 2d73a3d1db fix: keep completed office comments editable (#1548)
* fix: keep completed office comments editable

Keep the office task detail composer editable after completed sessions load,

including projectless office tasks.

Update verification tests for SHA-pinned GitHub actions. Extract the

dockview main area to satisfy the existing max-lines lint rule.

* fix: preserve upload artifact version guard

* fix: clarify upload artifact pin check

* fix: wake completed office sessions on comments

* fix: queue completed office comment runs

* fix: preserve dispatcher session lookup errors

* fix: resolve office comment wake review issues

* fix: stabilize comment wake idempotency

* fix: salt custom comment wake keys

* fix: address comment wake review cleanup

* fix: avoid comment payload allocation overflow pattern

* fix: salt comment wake action variants

* fix: resolve cross-task primary by target step

* fix: resolve cross-task participant roles

* fix: surface cross-task comment run status

* fix: use target steps for cross-task queue runs

* fix: avoid comment run lookup scan regression

* fix: cover cross-task queue run edge cases

* fix: index comment payload run lookup

* fix: support postgres comment run index

* fix: tighten queue run target test coverage

* fix: route source comment run updates

* fix: preserve cross-task comment wakes

* fix: tighten comment wake identity

* fix: align comment wake routing

* fix: remove stale primary resolver

* fix: dedupe comment run status

* fix: tighten legacy comment wake dedupe

* fix: remove unreachable comment wake guard

* fix: skip duplicate comment reactivity wakes

* fix: route comment wakes synchronously

* fix: share comment engine dispatch contracts

* fix: preserve comment wake fallbacks

* fix: preserve done comment runner

* fix: guard empty task runner fallback

* fix: handle empty task runner fallback

* fix: stabilize completed comment wakes

* fix: index comment runner fallback

---------

Co-authored-by: root <root@kandev.ts.net>
2026-06-30 13:10:40 +01:00

574 lines
15 KiB
Go

package sqlite_test
import (
"context"
"database/sql"
"errors"
"testing"
"time"
"github.com/kandev/kandev/internal/office/models"
)
func strPtr(s string) *string { return &s }
func TestClaimNextEligibleRun_Basic(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
req := &models.Run{
AgentProfileID: "a1",
Reason: "task_assigned",
Payload: `{"task_id":"t1"}`,
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
claimed, err := repo.ClaimNextEligibleRun(ctx)
if err != nil {
t.Fatalf("claim: %v", err)
}
if claimed.ID != req.ID {
t.Errorf("claimed ID = %q, want %q", claimed.ID, req.ID)
}
if claimed.Status != "claimed" {
t.Errorf("status = %q, want claimed", claimed.Status)
}
}
func TestClaimNextEligibleRun_SkipsBusyAgent(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
// Create a claimed run (agent at capacity).
claimed := &models.Run{
AgentProfileID: "a1",
Reason: "task_assigned",
Payload: "{}",
Status: "claimed",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, claimed); err != nil {
t.Fatalf("create claimed: %v", err)
}
// Create a queued run for same agent.
queued := &models.Run{
AgentProfileID: "a1",
Reason: "task_comment",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, queued); err != nil {
t.Fatalf("create queued: %v", err)
}
// Claim should return nothing since agent is at capacity.
_, err := repo.ClaimNextEligibleRun(ctx)
if !errors.Is(err, sql.ErrNoRows) {
t.Errorf("expected sql.ErrNoRows for busy agent, got %v", err)
}
}
func TestClaimNextEligibleRun_PicksNextEligible(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
// a1 is at capacity (has a claimed run).
atCap := &models.Run{
AgentProfileID: "a1",
Reason: "task_assigned",
Payload: "{}",
Status: "claimed",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, atCap); err != nil {
t.Fatalf("create: %v", err)
}
// a1 has a queued run.
a1Queued := &models.Run{
AgentProfileID: "a1",
Reason: "task_comment",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, a1Queued); err != nil {
t.Fatalf("create: %v", err)
}
// a2 has a queued run.
a2Queued := &models.Run{
AgentProfileID: "a2",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, a2Queued); err != nil {
t.Fatalf("create: %v", err)
}
// Should claim a2's run since a1 is busy.
result, err := repo.ClaimNextEligibleRun(ctx)
if err != nil {
t.Fatalf("claim: %v", err)
}
if result.AgentProfileID != "a2" {
t.Errorf("claimed agent = %q, want a2", result.AgentProfileID)
}
}
func TestClaimNextEligibleRun_ClaimsPausedAgent(t *testing.T) {
// Agent status filtering is now done at the service layer.
// The DB-level claim only checks capacity (no active claims).
repo := newTestRepo(t)
ctx := context.Background()
req := &models.Run{
AgentProfileID: "paused-agent-1",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
claimed, err := repo.ClaimNextEligibleRun(ctx)
if err != nil {
t.Fatalf("claim: %v", err)
}
if claimed.AgentProfileID != "paused-agent-1" {
t.Errorf("agent = %q, want paused-agent-1", claimed.AgentProfileID)
}
}
func TestCoalesceRun(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
// Create agent instance so the workspace-filtered list query can find runs.
agent := &models.AgentInstance{
ID: "a1",
WorkspaceID: "ws1",
Name: "coalesce-agent",
Role: models.AgentRoleWorker,
Status: models.AgentStatusIdle,
}
if err := repo.CreateAgentInstance(ctx, agent); err != nil {
t.Fatalf("create agent: %v", err)
}
req := &models.Run{
AgentProfileID: "a1",
Reason: "task_comment",
Payload: `{"task_id":"t1"}`,
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
coalesced, err := repo.CoalesceRun(ctx, "a1", "task_comment", 10, `{"task_id":"t2"}`)
if err != nil {
t.Fatalf("coalesce: %v", err)
}
if !coalesced {
t.Error("expected coalesce to succeed")
}
reqs, _ := repo.ListRuns(ctx, "ws1")
if len(reqs) != 1 {
t.Fatalf("want 1, got %d", len(reqs))
}
if reqs[0].CoalescedCount != 2 {
t.Errorf("coalesced_count = %d, want 2", reqs[0].CoalescedCount)
}
}
func TestCheckIdempotencyKey(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
req := &models.Run{
AgentProfileID: "a1",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
IdempotencyKey: strPtr("unique-key-1"),
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
dup, err := repo.CheckIdempotencyKey(ctx, "unique-key-1", 24)
if err != nil {
t.Fatalf("check: %v", err)
}
if !dup {
t.Error("expected duplicate=true for existing key")
}
dup, err = repo.CheckIdempotencyKey(ctx, "nonexistent-key", 24)
if err != nil {
t.Fatalf("check: %v", err)
}
if dup {
t.Error("expected duplicate=false for nonexistent key")
}
}
func TestCleanExpired(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
req := &models.Run{
AgentProfileID: "a1",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
// Finish it.
if err := repo.FinishRun(ctx, req.ID, "finished"); err != nil {
t.Fatalf("finish: %v", err)
}
// Clean with a future cutoff should remove it.
n, err := repo.CleanExpired(ctx, time.Now().Add(time.Hour))
if err != nil {
t.Fatalf("clean: %v", err)
}
if n != 1 {
t.Errorf("cleaned %d, want 1", n)
}
reqs, _ := repo.ListRuns(ctx, "ws1")
if len(reqs) != 0 {
t.Errorf("want 0 after clean, got %d", len(reqs))
}
}
func TestClaimNextEligibleRun_ClaimsWithoutCooldownCheck(t *testing.T) {
// Cooldown enforcement is now done at the service layer, not the DB query.
repo := newTestRepo(t)
ctx := context.Background()
req := &models.Run{
AgentProfileID: "cooldown-agent-1",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
claimed, err := repo.ClaimNextEligibleRun(ctx)
if err != nil {
t.Fatalf("claim: %v", err)
}
if claimed.AgentProfileID != "cooldown-agent-1" {
t.Errorf("agent = %q, want cooldown-agent-1", claimed.AgentProfileID)
}
}
func TestClaimNextEligibleRun_AllowsAgentPastCooldown(t *testing.T) {
// Cooldown enforcement is done at the service layer, not the DB query.
// The DB claim only checks capacity. This test verifies the run is
// claimed regardless of runtime state stored in office_agent_runtime.
repo := newTestRepo(t)
ctx := context.Background()
// Store runtime state with a past run timestamp.
past := time.Now().UTC().Add(-10 * time.Second)
if err := repo.UpsertAgentRuntime(ctx, "a-cd2", "idle", ""); err != nil {
t.Fatalf("upsert runtime: %v", err)
}
if err := repo.UpdateRuntimeLastRunFinished(ctx, "a-cd2", past); err != nil {
t.Fatalf("update finished: %v", err)
}
req := &models.Run{
AgentProfileID: "a-cd2",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
claimed, err := repo.ClaimNextEligibleRun(ctx)
if err != nil {
t.Fatalf("claim: %v", err)
}
if claimed.AgentProfileID != "a-cd2" {
t.Errorf("claimed agent = %q, want a-cd2", claimed.AgentProfileID)
}
}
func TestClaimNextEligibleRun_SkipsScheduledRetryInFuture(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
future := time.Now().UTC().Add(10 * time.Minute)
req := &models.Run{
AgentProfileID: "a-retry",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
RetryCount: 1,
ScheduledRetryAt: &future,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
_, err := repo.ClaimNextEligibleRun(ctx)
if !errors.Is(err, sql.ErrNoRows) {
t.Errorf("expected sql.ErrNoRows for future retry, got %v", err)
}
}
func TestScheduleRetry_ResetsToQueued(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
req := &models.Run{
AgentProfileID: "a-sr",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
// Claim it.
claimed, err := repo.ClaimNextEligibleRun(ctx)
if err != nil {
t.Fatalf("claim: %v", err)
}
// Schedule retry.
retryAt := time.Now().UTC().Add(2 * time.Minute)
if err := repo.ScheduleRetry(ctx, claimed.ID, retryAt, 1); err != nil {
t.Fatalf("schedule retry: %v", err)
}
// Should not be claimable yet (retry in future).
_, err = repo.ClaimNextEligibleRun(ctx)
if !errors.Is(err, sql.ErrNoRows) {
t.Errorf("expected sql.ErrNoRows (retry in future), got %v", err)
}
}
func TestScheduleRetry_PreservesRunPayloadForResume(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
payload := `{"task_id":"task-1","session_id":"session-1"}`
req := &models.Run{
AgentProfileID: "a-resume",
Reason: "task_assigned",
Payload: payload,
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
claimed, err := repo.ClaimNextEligibleRun(ctx)
if err != nil {
t.Fatalf("claim: %v", err)
}
retryAt := time.Now().UTC().Add(time.Minute)
if err := repo.ScheduleRetry(ctx, claimed.ID, retryAt, 1); err != nil {
t.Fatalf("schedule retry: %v", err)
}
requeued, err := repo.GetRun(ctx, claimed.ID)
if err != nil {
t.Fatalf("get run: %v", err)
}
if requeued.ID != claimed.ID {
t.Fatalf("run ID = %q, want %q", requeued.ID, claimed.ID)
}
if requeued.Payload != payload {
t.Fatalf("payload = %q, want %q", requeued.Payload, payload)
}
if requeued.Status != "queued" {
t.Fatalf("status = %q, want queued", requeued.Status)
}
if requeued.RetryCount != 1 {
t.Fatalf("retry_count = %d, want 1", requeued.RetryCount)
}
}
func TestRecoverStale(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
req := &models.Run{
AgentProfileID: "a1",
Reason: "task_assigned",
Payload: "{}",
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, req); err != nil {
t.Fatalf("create: %v", err)
}
// Claim it.
_, err := repo.ClaimRun(ctx, "a1")
if err != nil {
t.Fatalf("claim: %v", err)
}
// Recover with a future cutoff should reset it.
n, err := repo.RecoverStale(ctx, time.Now().Add(time.Hour))
if err != nil {
t.Fatalf("recover: %v", err)
}
if n != 1 {
t.Errorf("recovered %d, want 1", n)
}
}
// TestGetRunsByCommentIDs_MapsLatestRunPerComment pins that the
// per-comment lookup returns one row per comment, picks the most
// recently requested row when duplicates exist, and surfaces the
// error_message field for failed runs so the dashboard can render the
// red badge tooltip without a second query.
func TestGetRunsByCommentIDs_MapsLatestRunPerComment(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
// Comment cm-A: a single finished run.
runA := &models.Run{
AgentProfileID: "a1",
Reason: "task_comment",
Payload: `{"task_id":"t1","comment_id":"cm-A"}`,
Status: "finished",
CoalescedCount: 1,
IdempotencyKey: strPtr("task_comment:cm-A"),
}
if err := repo.CreateRun(ctx, runA); err != nil {
t.Fatalf("create runA: %v", err)
}
// Comment cm-B: a failed run with an error message. CreateRun
// doesn't write error_message (the column is populated by the
// failure-handler path); use the test-only setter to land the
// fixture value.
runB := &models.Run{
AgentProfileID: "a1",
Reason: "task_comment",
Payload: `{"task_id":"t1","comment_id":"cm-B"}`,
Status: "failed",
CoalescedCount: 1,
IdempotencyKey: strPtr("task_comment:cm-B"),
}
if err := repo.CreateRun(ctx, runB); err != nil {
t.Fatalf("create runB: %v", err)
}
if err := repo.SetRunErrorMessageForTest(ctx, runB.ID, "boom"); err != nil {
t.Fatalf("set error message: %v", err)
}
// Comment cm-C is queried but has no run row.
got, err := repo.GetRunsByCommentIDs(ctx, []string{"cm-A", "cm-B", "cm-C"})
if err != nil {
t.Fatalf("get: %v", err)
}
if len(got) != 2 {
t.Fatalf("got %d entries, want 2 (cm-C should be absent): %+v", len(got), got)
}
if got["cm-A"].RunID != runA.ID || got["cm-A"].Status != "finished" {
t.Errorf("cm-A = %+v, want runA finished", got["cm-A"])
}
if got["cm-B"].RunID != runB.ID || got["cm-B"].Status != "failed" || got["cm-B"].ErrorMessage != "boom" {
t.Errorf("cm-B = %+v, want runB failed with error", got["cm-B"])
}
if _, present := got["cm-C"]; present {
t.Errorf("cm-C should be absent from map: %+v", got["cm-C"])
}
}
func TestGetRunsByCommentIDs_MapsSaltedCommentKeysByPayload(t *testing.T) {
repo := newTestRepo(t)
ctx := context.Background()
run := &models.Run{
AgentProfileID: "a1",
Reason: "task_comment",
Payload: `{"task_id":"target-task","source_task_id":"source-task","comment_id":"cm-X"}`,
Status: "queued",
CoalescedCount: 1,
IdempotencyKey: strPtr("task_comment:cm-X:work:target-task:a1:abcd1234"),
}
if err := repo.CreateRun(ctx, run); err != nil {
t.Fatalf("create run: %v", err)
}
nilKeyRun := &models.Run{
AgentProfileID: "a1",
Reason: "task_comment",
Payload: `{"task_id":"target-task","comment_id":"cm-nil-key"}`,
Status: "queued",
CoalescedCount: 1,
}
if err := repo.CreateRun(ctx, nilKeyRun); err != nil {
t.Fatalf("create nil-key run: %v", err)
}
got, err := repo.GetRunsByCommentIDs(ctx, []string{"cm-X", "cm-nil-key"})
if err != nil {
t.Fatalf("get: %v", err)
}
status, ok := got["cm-X"]
if !ok {
t.Fatalf("missing cm-X status: %+v", got)
}
if status.RunID != run.ID || status.Status != "queued" {
t.Fatalf("cm-X = %+v, want run %s queued", status, run.ID)
}
nilKeyStatus, ok := got["cm-nil-key"]
if !ok {
t.Fatalf("missing cm-nil-key status: %+v", got)
}
if nilKeyStatus.RunID != nilKeyRun.ID || nilKeyStatus.Status != "queued" {
t.Fatalf("cm-nil-key = %+v, want run %s queued", nilKeyStatus, nilKeyRun.ID)
}
}
// TestGetRunsByCommentIDs_EmptyInput_ReturnsEmptyMap pins the
// degenerate case so callers can hand the result straight into a
// range loop without nil-checking.
func TestGetRunsByCommentIDs_EmptyInput_ReturnsEmptyMap(t *testing.T) {
repo := newTestRepo(t)
got, err := repo.GetRunsByCommentIDs(context.Background(), nil)
if err != nil {
t.Fatalf("get: %v", err)
}
if len(got) != 0 {
t.Errorf("expected empty map, got %+v", got)
}
}