From cc2acd64025ba02492aae800d4ee97e73df0e67c Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 01:04:38 -0600 Subject: [PATCH] fix(mcpx/tasks): maxAge ceiling cannot override deadline-task retention floor (#6249) --- pkgs/mcpx/internal/tasks/tasks.go | 38 ++++++++++++++++-- pkgs/mcpx/internal/tasks/tasks_test.go | 54 ++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/pkgs/mcpx/internal/tasks/tasks.go b/pkgs/mcpx/internal/tasks/tasks.go index 863e9c5f1..86cee963c 100644 --- a/pkgs/mcpx/internal/tasks/tasks.go +++ b/pkgs/mcpx/internal/tasks/tasks.go @@ -65,6 +65,13 @@ type Task struct { // per-requestor limits (protoTasks.maxPerRequestor). Never on the wire. requestor Requestor + // retentionFloor is the minimum terminal retention for a deadline + // (internal backing) task: two poll intervals, set by + // StartWithDeadline. deadline()'s maxAge ceiling must not cut + // retention below it (#6249). Zero for ordinary tasks, whose + // retention the maxAge ceiling bounds exactly. Never on the wire. + retentionFloor time.Duration + result any fault *Fault cancel context.CancelFunc @@ -91,6 +98,24 @@ func deadline(terminalAt, lastTouched time.Time, ttl, maxAge time.Duration) time return expires } +// retentionDeadline is deadline() with t's retention floor applied. +// The maxAge ceiling still bounds retention above the floor, but a +// deadline task's result always survives its two-poll-interval +// collection window: #6249 is the maxAge clamp (tasks.maxAge=100ms +// against a 1s poll) overriding at arming time the very floor +// StartWithDeadline sets on the TTL, deleting a finished backing +// result before the ask wrapper's next poll, whose 404 then records +// a successful call as failed. +func (t *Task) retentionDeadline(terminalAt, lastTouched time.Time, ttl, maxAge time.Duration) time.Time { + expires := deadline(terminalAt, lastTouched, ttl, maxAge) + if t.retentionFloor > 0 { + if floor := terminalAt.Add(t.retentionFloor); expires.Before(floor) { + expires = floor + } + } + return expires +} + func (t *Task) refreshTTLRemaining(now time.Time) { if t.ExpiresAt == nil { t.TTLRemainingMs = 0 @@ -791,7 +816,7 @@ func (s *Store) armExpiryLocked(t *Task, d time.Duration) { func (s *Store) armRetentionLocked(t *Task) { now := t.LastUpdatedAt t.LastTouched = &now - expires := deadline(t.LastUpdatedAt, now, time.Duration(t.TTL)*time.Millisecond, s.maxAge) + expires := t.retentionDeadline(t.LastUpdatedAt, now, time.Duration(t.TTL)*time.Millisecond, s.maxAge) t.ExpiresAt = &expires t.refreshTTLRemaining(time.Now()) s.armExpiryLocked(t, time.Until(expires)) @@ -827,7 +852,11 @@ func (s *Store) Start(requestor Requestor, ttl int64, fn func(ctx context.Contex // would delete a finished backing result before the wrapper's next // look, and its poll would meet a 404 and record a successful call as // failed (Codex on #2869, round 6). The floor is on the returned task's -// TTL, so what a client is told is what the store does. +// TTL, so what a client is told is what the store does. The floor +// also holds against the store's maxAge ceiling when retention is +// armed at the terminal transition (Task.retentionFloor, #6249): +// flooring the TTL alone left armRetentionLocked free to recompute +// a shorter expiry through deadline(). func (s *Store) StartWithDeadline(requestor Requestor, ttl int64, deadline time.Duration, fn func(ctx context.Context) (any, *Fault)) (Task, error) { if ttl <= 0 { s.mu.Lock() @@ -893,6 +922,9 @@ func (s *Store) StartWithDeadline(requestor Requestor, ttl int64, deadline time. requestor: requestor, cancel: cancel, done: make(chan struct{}), } + if deadline > 0 { + t.retentionFloor = 2 * poll + } ctx = context.WithValue(ctx, idKey{}, t.TaskID) s.tasks[t.TaskID] = t s.saveTaskLocked(t) @@ -1018,7 +1050,7 @@ func (s *Store) touchLocked(t *Task, now time.Time) bool { s.deleteFromDBLocked(t.TaskID) return false } - expires := deadline(t.LastUpdatedAt, now, time.Duration(t.TTL)*time.Millisecond, s.maxAge) + expires := t.retentionDeadline(t.LastUpdatedAt, now, time.Duration(t.TTL)*time.Millisecond, s.maxAge) if expires.Before(*t.ExpiresAt) { expires = *t.ExpiresAt } diff --git a/pkgs/mcpx/internal/tasks/tasks_test.go b/pkgs/mcpx/internal/tasks/tasks_test.go index 7d36c323d..8f06cdda1 100644 --- a/pkgs/mcpx/internal/tasks/tasks_test.go +++ b/pkgs/mcpx/internal/tasks/tasks_test.go @@ -1025,3 +1025,57 @@ func TestOpenWithRetentionPreservesLegacyRowUntilStoredTTLDeadline(t *testing.T) t.Errorf("control: legacy-keep survived plain Open with the built-in 1h maxAge; the seeded row does not reproduce the pre-fix loss") } } + +// #6249: the maxAge ceiling must not override a deadline task's +// two-poll-interval retention floor when retention is armed. Oracle: +// with PollInterval 400ms and maxAge 100ms (the issue's shape -- +// maxAge below one poll interval), a finished backing task's expiry +// is at least terminal+800ms and its result is still collectable +// past the 100ms ceiling, while a plain task under the same maxAge +// keeps the exact 100ms ceiling (maxAge remains a hard bound for +// ordinary tasks, per TestTaskMaxAgeStartsAtTerminalTransition...). +func TestAMaxAgeClampCannotOverrideDeadlineRetentionFloor(t *testing.T) { + st := tasks.New() + st.PollInterval = 400 * time.Millisecond + st.Configure(time.Minute, 100*time.Millisecond) + task, err := st.StartWithDeadline(tasks.Caller("asker"), 60000, time.Minute, func(ctx context.Context) (any, *tasks.Fault) { + return "the answer", nil + }) + if err != nil { + t.Fatalf("start: %v", err) + } + res, fault, err := st.Result(context.Background(), task.TaskID) + if err != nil || fault != nil || res != "the answer" { + t.Fatalf("result: %v %v %v", res, fault, err) + } + snap, ok := st.Get(task.TaskID) + if !ok || snap.ExpiresAt == nil { + t.Fatalf("finished deadline task missing expiry: %+v", snap) + } + if floor := snap.LastUpdatedAt.Add(800 * time.Millisecond); snap.ExpiresAt.Before(floor) { + t.Fatalf("maxAge clamped a deadline task below its retention floor: expiry %v, floor %v", snap.ExpiresAt, floor) + } + // Past the 100ms maxAge ceiling, inside the 800ms floor: the + // wrapper's next poll still collects the result. + time.Sleep(200 * time.Millisecond) + if _, ok := st.Get(task.TaskID); !ok { + t.Fatal("the finished deadline task expired at the maxAge ceiling inside its collection window") + } + + plain, err := st.Start(tasks.Caller("asker"), 60000, func(ctx context.Context) (any, *tasks.Fault) { + return "ok", nil + }) + if err != nil { + t.Fatalf("start plain: %v", err) + } + if _, _, err := st.Result(context.Background(), plain.TaskID); err != nil { + t.Fatalf("plain result: %v", err) + } + psnap, ok := st.Get(plain.TaskID) + if !ok || psnap.ExpiresAt == nil { + t.Fatalf("plain task missing expiry: %+v", psnap) + } + if ceiling := psnap.LastUpdatedAt.Add(100 * time.Millisecond); psnap.ExpiresAt.After(ceiling) { + t.Fatalf("a plain task's retention exceeded the maxAge ceiling: expiry %v, ceiling %v", psnap.ExpiresAt, ceiling) + } +}