File size: 12,161 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | import { feature } from 'bun:bundle'
import { z } from 'zod/v4'
import { getFeatureValue_CACHED_MAY_BE_STALE } from '../../services/analytics/growthbook.js'
import { buildTool, type ToolDef } from '../../Tool.js'
import { isAgentSwarmsEnabled } from '../../utils/agentSwarmsEnabled.js'
import {
executeTaskCompletedHooks,
getTaskCompletedHookMessage,
} from '../../utils/hooks.js'
import { lazySchema } from '../../utils/lazySchema.js'
import {
blockTask,
deleteTask,
getTask,
getTaskListId,
isTodoV2Enabled,
listTasks,
type TaskStatus,
TaskStatusSchema,
updateTask,
} from '../../utils/tasks.js'
import {
getAgentId,
getAgentName,
getTeammateColor,
getTeamName,
} from '../../utils/teammate.js'
import { writeToMailbox } from '../../utils/teammateMailbox.js'
import { VERIFICATION_AGENT_TYPE } from '../AgentTool/constants.js'
import { TASK_UPDATE_TOOL_NAME } from './constants.js'
import { DESCRIPTION, PROMPT } from './prompt.js'
const inputSchema = lazySchema(() => {
// Extended status schema that includes 'deleted' as a special action
const TaskUpdateStatusSchema = TaskStatusSchema().or(z.literal('deleted'))
return z.strictObject({
taskId: z.string().describe('The ID of the task to update'),
subject: z.string().optional().describe('New subject for the task'),
description: z.string().optional().describe('New description for the task'),
activeForm: z
.string()
.optional()
.describe(
'Present continuous form shown in spinner when in_progress (e.g., "Running tests")',
),
status: TaskUpdateStatusSchema.optional().describe(
'New status for the task',
),
addBlocks: z
.array(z.string())
.optional()
.describe('Task IDs that this task blocks'),
addBlockedBy: z
.array(z.string())
.optional()
.describe('Task IDs that block this task'),
owner: z.string().optional().describe('New owner for the task'),
metadata: z
.record(z.string(), z.unknown())
.optional()
.describe(
'Metadata keys to merge into the task. Set a key to null to delete it.',
),
})
})
type InputSchema = ReturnType<typeof inputSchema>
const outputSchema = lazySchema(() =>
z.object({
success: z.boolean(),
taskId: z.string(),
updatedFields: z.array(z.string()),
error: z.string().optional(),
statusChange: z
.object({
from: z.string(),
to: z.string(),
})
.optional(),
verificationNudgeNeeded: z.boolean().optional(),
}),
)
type OutputSchema = ReturnType<typeof outputSchema>
export type Output = z.infer<OutputSchema>
export const TaskUpdateTool = buildTool({
name: TASK_UPDATE_TOOL_NAME,
searchHint: 'update a task',
maxResultSizeChars: 100_000,
async description() {
return DESCRIPTION
},
async prompt() {
return PROMPT
},
get inputSchema(): InputSchema {
return inputSchema()
},
get outputSchema(): OutputSchema {
return outputSchema()
},
userFacingName() {
return 'TaskUpdate'
},
shouldDefer: true,
isEnabled() {
return isTodoV2Enabled()
},
isConcurrencySafe() {
return true
},
toAutoClassifierInput(input) {
const parts = [input.taskId]
if (input.status) parts.push(input.status)
if (input.subject) parts.push(input.subject)
return parts.join(' ')
},
renderToolUseMessage() {
return null
},
async call(
{
taskId,
subject,
description,
activeForm,
status,
owner,
addBlocks,
addBlockedBy,
metadata,
},
context,
) {
const taskListId = getTaskListId()
// Auto-expand task list when updating tasks
context.setAppState(prev => {
if (prev.expandedView === 'tasks') return prev
return { ...prev, expandedView: 'tasks' as const }
})
// Check if task exists
const existingTask = await getTask(taskListId, taskId)
if (!existingTask) {
return {
data: {
success: false,
taskId,
updatedFields: [],
error: 'Task not found',
},
}
}
const updatedFields: string[] = []
// Update basic fields if provided and different from current value
const updates: {
subject?: string
description?: string
activeForm?: string
status?: TaskStatus
owner?: string
metadata?: Record<string, unknown>
} = {}
if (subject !== undefined && subject !== existingTask.subject) {
updates.subject = subject
updatedFields.push('subject')
}
if (description !== undefined && description !== existingTask.description) {
updates.description = description
updatedFields.push('description')
}
if (activeForm !== undefined && activeForm !== existingTask.activeForm) {
updates.activeForm = activeForm
updatedFields.push('activeForm')
}
if (owner !== undefined && owner !== existingTask.owner) {
updates.owner = owner
updatedFields.push('owner')
}
// Auto-set owner when a teammate marks a task as in_progress without
// explicitly providing an owner. This ensures the task list can match
// todo items to teammates for showing activity status.
if (
isAgentSwarmsEnabled() &&
status === 'in_progress' &&
owner === undefined &&
!existingTask.owner
) {
const agentName = getAgentName()
if (agentName) {
updates.owner = agentName
updatedFields.push('owner')
}
}
if (metadata !== undefined) {
const merged = { ...(existingTask.metadata ?? {}) }
for (const [key, value] of Object.entries(metadata)) {
if (value === null) {
delete merged[key]
} else {
merged[key] = value
}
}
updates.metadata = merged
updatedFields.push('metadata')
}
if (status !== undefined) {
// Handle deletion - delete the task file and return early
if (status === 'deleted') {
const deleted = await deleteTask(taskListId, taskId)
return {
data: {
success: deleted,
taskId,
updatedFields: deleted ? ['deleted'] : [],
error: deleted ? undefined : 'Failed to delete task',
statusChange: deleted
? { from: existingTask.status, to: 'deleted' }
: undefined,
},
}
}
// For regular status updates, validate and apply if different
if (status !== existingTask.status) {
// Run TaskCompleted hooks when marking a task as completed
if (status === 'completed') {
const blockingErrors: string[] = []
const generator = executeTaskCompletedHooks(
taskId,
existingTask.subject,
existingTask.description,
getAgentName(),
getTeamName(),
undefined,
context?.abortController?.signal,
undefined,
context,
)
for await (const result of generator) {
if (result.blockingError) {
blockingErrors.push(
getTaskCompletedHookMessage(result.blockingError),
)
}
}
if (blockingErrors.length > 0) {
return {
data: {
success: false,
taskId,
updatedFields: [],
error: blockingErrors.join('\n'),
},
}
}
}
updates.status = status
updatedFields.push('status')
}
}
if (Object.keys(updates).length > 0) {
await updateTask(taskListId, taskId, updates)
}
// Notify new owner via mailbox when ownership changes
if (updates.owner && isAgentSwarmsEnabled()) {
const senderName = getAgentName() || 'team-lead'
const senderColor = getTeammateColor()
const assignmentMessage = JSON.stringify({
type: 'task_assignment',
taskId,
subject: existingTask.subject,
description: existingTask.description,
assignedBy: senderName,
timestamp: new Date().toISOString(),
})
await writeToMailbox(
updates.owner,
{
from: senderName,
text: assignmentMessage,
timestamp: new Date().toISOString(),
color: senderColor,
},
taskListId,
)
}
// Add blocks if provided and not already present
if (addBlocks && addBlocks.length > 0) {
const newBlocks = addBlocks.filter(
id => !existingTask.blocks.includes(id),
)
for (const blockId of newBlocks) {
await blockTask(taskListId, taskId, blockId)
}
if (newBlocks.length > 0) {
updatedFields.push('blocks')
}
}
// Add blockedBy if provided and not already present (reverse: the blocker blocks this task)
if (addBlockedBy && addBlockedBy.length > 0) {
const newBlockedBy = addBlockedBy.filter(
id => !existingTask.blockedBy.includes(id),
)
for (const blockerId of newBlockedBy) {
await blockTask(taskListId, blockerId, taskId)
}
if (newBlockedBy.length > 0) {
updatedFields.push('blockedBy')
}
}
// Structural verification nudge: if the main-thread agent just closed
// out a 3+ task list and none of those tasks was a verification step,
// append a reminder to the tool result. Fires at the loop-exit moment
// where skips happen ("when the last task closed, the loop exited").
// Mirrors the TodoWriteTool nudge for V1 sessions; this covers V2
// (interactive CLI). TaskUpdateToolOutput is @internal so this field
// does not touch the public SDK surface.
let verificationNudgeNeeded = false
if (
feature('VERIFICATION_AGENT') &&
getFeatureValue_CACHED_MAY_BE_STALE('tengu_hive_evidence', false) &&
!context.agentId &&
updates.status === 'completed'
) {
const allTasks = await listTasks(taskListId)
const allDone = allTasks.every(t => t.status === 'completed')
if (
allDone &&
allTasks.length >= 3 &&
!allTasks.some(t => /verif/i.test(t.subject))
) {
verificationNudgeNeeded = true
}
}
return {
data: {
success: true,
taskId,
updatedFields,
statusChange:
updates.status !== undefined
? { from: existingTask.status, to: updates.status }
: undefined,
verificationNudgeNeeded,
},
}
},
mapToolResultToToolResultBlockParam(content, toolUseID) {
const {
success,
taskId,
updatedFields,
error,
statusChange,
verificationNudgeNeeded,
} = content as Output
if (!success) {
// Return as non-error so it doesn't trigger sibling tool cancellation
// in StreamingToolExecutor. "Task not found" is a benign condition
// (e.g., task list already cleaned up) that the model can handle.
return {
tool_use_id: toolUseID,
type: 'tool_result',
content: error || `Task #${taskId} not found`,
}
}
let resultContent = `Updated task #${taskId} ${updatedFields.join(', ')}`
// Add reminder for teammates when they complete a task (supports in-process teammates)
if (
statusChange?.to === 'completed' &&
getAgentId() &&
isAgentSwarmsEnabled()
) {
resultContent +=
'\n\nTask completed. Call TaskList now to find your next available task or see if your work unblocked others.'
}
if (verificationNudgeNeeded) {
resultContent += `\n\nNOTE: You just closed out 3+ tasks and none of them was a verification step. Before writing your final summary, spawn the verification agent (subagent_type="${VERIFICATION_AGENT_TYPE}"). You cannot self-assign PARTIAL by listing caveats in your summary — only the verifier issues a verdict.`
}
return {
tool_use_id: toolUseID,
type: 'tool_result',
content: resultContent,
}
},
} satisfies ToolDef<InputSchema, Output>)
|