Skip to content
Open
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
53 changes: 53 additions & 0 deletions typescript/packages/core/src/core/__tests__/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,59 @@ describe('TaskManager', () => {
});
});

// ── TTL cleanup ──────────────────────────────────────────────────────────────
describe('TTL cleanup', () => {
it('keeps an active task alive when progress resets its TTL', () => {
jest.useFakeTimers();

const ttlManager = new TaskManager({
logger: createMockLogger(),
defaultTtl: 60000,
defaultPollInterval: 1000,
});

try {
const task = ttlManager.createTask({ ttl: 60000 });
const ctx = new TaskContext(ttlManager, task.taskId);

// Report progress 50 seconds after creation.
jest.advanceTimersByTime(50000);
ctx.updateProgress('Still working');

// Reach the cleanup sweep at 90 seconds.
// Task is 90s old, but only 40s idle.
jest.advanceTimersByTime(40000);

expect(ttlManager.hasTask(task.taskId)).toBe(true);
} finally {
ttlManager.destroy();
jest.useRealTimers();
}
});
it('removes a task after its TTL when there is no activity', () => {
jest.useFakeTimers();

const ttlManager = new TaskManager({
logger: createMockLogger(),
defaultTtl: 60000,
defaultPollInterval: 1000,
});

try {
const task = ttlManager.createTask({ ttl: 60000 });

// Cleanup runs every 30 seconds.
// At 90 seconds the task has been idle longer than its 60s TTL.
jest.advanceTimersByTime(90000);

expect(ttlManager.hasTask(task.taskId)).toBe(false);
} finally {
ttlManager.destroy();
jest.useRealTimers();
}
});
});

// ── getAbortSignal ──────────────────────────────────────────────────────────
describe('getAbortSignal', () => {
it('returns an AbortSignal that is not yet aborted', () => {
Expand Down
4 changes: 2 additions & 2 deletions typescript/packages/core/src/core/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ export class TaskManager {
for (const [taskId, entry] of this.tasks.entries()) {
if (entry.data.ttl === null) continue; // Unlimited TTL

const createdTime = new Date(entry.data.createdAt).getTime();
if (now - createdTime > entry.data.ttl) {
const lastActivityTime = new Date(entry.data.lastUpdatedAt).getTime();
if (now - lastActivityTime > entry.data.ttl) {
this.tasks.delete(taskId);
this.logger.debug(`Expired task cleaned up: ${taskId}`);
}
Expand Down