|
| 1 | +import { Tool } from "./tool" |
| 2 | +import DESCRIPTION from "./list_tasks.txt" |
| 3 | +import z from "zod" |
| 4 | +import { listBackgroundTasks, getBackgroundTaskResult, getBackgroundTaskMetadata } from "../session/async-tasks" |
| 5 | + |
| 6 | +interface TaskInfo { |
| 7 | + task_id: string |
| 8 | + status: "running" | "completed" | "failed" |
| 9 | + agent?: string |
| 10 | + description?: string |
| 11 | + started_at?: string |
| 12 | + completed_at?: string |
| 13 | +} |
| 14 | + |
| 15 | +interface ListTasksResult { |
| 16 | + pending: TaskInfo[] |
| 17 | + completed: TaskInfo[] |
| 18 | + total_count: number |
| 19 | +} |
| 20 | + |
| 21 | +// Helper: Timestamp validation (>= 2000-01-01, sane range) |
| 22 | +function isValidTimestamp(timestamp: number): boolean { |
| 23 | + if (!timestamp || !Number.isFinite(timestamp)) { |
| 24 | + return false |
| 25 | + } |
| 26 | + const date = new Date(timestamp) |
| 27 | + return date.getFullYear() >= 2000 && date.getFullYear() <= 2100 |
| 28 | +} |
| 29 | + |
| 30 | +export const ListTasksTool = Tool.define("list_tasks", { |
| 31 | + description: DESCRIPTION, |
| 32 | + parameters: z.object({ |
| 33 | + limit: z.number().optional().describe("Maximum number of tasks to return (default: 50)"), |
| 34 | + include_completed: z.boolean().optional().describe("Include completed tasks (default: true)"), |
| 35 | + }), |
| 36 | + async execute(params, ctx) { |
| 37 | + // CRITICAL #1: Session ownership filtering - only return tasks owned by caller's session |
| 38 | + const callerSessionId = ctx.sessionID! |
| 39 | + |
| 40 | + const tasks = listBackgroundTasks() |
| 41 | + const limit = params.limit ?? 50 |
| 42 | + |
| 43 | + // Filter and map pending tasks |
| 44 | + const pending: TaskInfo[] = [] |
| 45 | + for (const id of tasks.pending.slice(0, limit)) { |
| 46 | + const metadata = getBackgroundTaskMetadata(id) |
| 47 | + const result = getBackgroundTaskResult(id) |
| 48 | + |
| 49 | + // Ownership check: filter to caller's session |
| 50 | + const ownedByCaller = |
| 51 | + metadata?.parent_session_id === callerSessionId || result?.metadata?.parent_session_id === callerSessionId |
| 52 | + |
| 53 | + if (!ownedByCaller) { |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + // CRITICAL #2: Timestamp validation before Date() calls |
| 58 | + let started_at: string | undefined |
| 59 | + if (metadata?.start_time && isValidTimestamp(metadata.start_time)) { |
| 60 | + started_at = new Date(metadata.start_time).toISOString() |
| 61 | + } else if (result?.time.started && isValidTimestamp(result.time.started)) { |
| 62 | + started_at = new Date(result.time.started).toISOString() |
| 63 | + } |
| 64 | + |
| 65 | + pending.push({ |
| 66 | + task_id: id, |
| 67 | + status: "running", |
| 68 | + agent: metadata?.agent_type, |
| 69 | + description: metadata?.description, |
| 70 | + started_at, |
| 71 | + }) |
| 72 | + |
| 73 | + if (pending.length >= limit) { |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Filter and map completed tasks |
| 79 | + const completed: TaskInfo[] = [] |
| 80 | + if (params.include_completed !== false) { |
| 81 | + // CRITICAL #4: Clamp slice end to prevent negative values (limit=0 case) |
| 82 | + const completedLimit = Math.max(0, limit - pending.length) |
| 83 | + |
| 84 | + // CRITICAL #3: Guard against undefined values from Object.entries |
| 85 | + for (const [id, result] of Object.entries(tasks.results)) { |
| 86 | + if (!result) { |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + // Ownership check: only return tasks owned by caller's session |
| 91 | + if (result.metadata?.parent_session_id !== callerSessionId) { |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + // CRITICAL #5: Timestamp validation before Date() calls |
| 96 | + const has_valid_started = isValidTimestamp(result.time.started) |
| 97 | + const has_valid_completed = result.time.completed && isValidTimestamp(result.time.completed) |
| 98 | + |
| 99 | + completed.push({ |
| 100 | + task_id: id, |
| 101 | + status: result.status, |
| 102 | + agent: result.metadata?.agent_type, |
| 103 | + description: result.metadata?.description, |
| 104 | + started_at: has_valid_started ? new Date(result.time.started).toISOString() : undefined, |
| 105 | + completed_at: has_valid_completed ? new Date(result.time.completed).toISOString() : undefined, |
| 106 | + }) |
| 107 | + |
| 108 | + if (completed.length >= completedLimit) { |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + const taskResult: ListTasksResult = { |
| 115 | + pending, |
| 116 | + completed, |
| 117 | + total_count: pending.length + completed.length, |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + title: `List tasks: ${taskResult.total_count} total`, |
| 122 | + output: JSON.stringify(taskResult, null, 2), |
| 123 | + metadata: { |
| 124 | + pending_count: pending.length, |
| 125 | + completed_count: completed.length, |
| 126 | + }, |
| 127 | + } |
| 128 | + }, |
| 129 | +}) |
0 commit comments