Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions pkgs/mcpx/internal/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
54 changes: 54 additions & 0 deletions pkgs/mcpx/internal/tasks/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading