MODULO 8.4

📋 Task System

6
Topicos
~60
Minutos
Deep
Nivel
Source
Tipo
1

🏗️ What Is a Task?

Tasks represent concurrent, observable work units: bash commands, subagents, remote sessions, dream agents. Seven types: local_bash, local_agent, remote_agent, in_process_teammate, local_workflow, monitor_mcp, dream.

💡 Task IDs Encode Type

One-letter prefix: b=bash, a=agent, r=remote, t=teammate, w=workflow, m=monitor, d=dream. Remaining 8 chars cryptographically random to prevent symlink attacks.

2

🔄 The Lifecycle

pending

Task registered via registerTask().

running

Task started execution.

completed

Terminal. Exit code 0 / agent success.

failed

Terminal. Exit code != 0 / agent error.

killed

Terminal. kill() called.

💡 notified Flag

One-way latch with compare-and-set. Even if two async paths race to completion, only one notification fires.

3

🖥️ Seven Task Types

TypePrefixOutputKill Mechanism
local_bashbDirect file via OS fdSIGKILL
local_agentaSymlink to transcript JSONLabortController.abort()
remote_agentrPolled + written locallyarchiveRemoteSession() API
in_process_teammatetSymlink to transcript JSONLkillInProcessTeammate()
local_workflowwTask output fileabortController.abort()
dreamdNoneabort + lock rewind
4

🔍 Shell Task Deep Dive

Stdout/stderr go directly to file via OS fd - no JS buffering. Stall watchdog polls file size every 5s. After 45s no growth, reads last 1KB checking 7 prompt-detection patterns.

💡 Orphan Cleanup

killShellTasksForAgent() kills bash tasks matching agentId when agent exits. Prevents zombie processes lasting days.

5

🤖 Agent & Teammate Tasks

Agent tasks track input tokens (cumulative/latest) and output tokens (per-turn/summed). 50-message UI cap prevents 36GB RSS in swarm sessions. Teammates have idle/active lifecycle within running status.

💡 DreamTask Kill-Then-Rewind

On kill, rollbackConsolidationLock(priorMtime) rewinds lock's mtime. Without this, killed dream permanently prevents next session from dreaming.

6

🔔 Notification & Output

XML notifications with task_id, tool_use_id, output_file, status, summary. Shell tasks use 'later' priority; monitor tasks use 'next'. Output files use O_NOFOLLOW flag to prevent symlink-based path-traversal attacks.

💡 Stall Notification Design

Stall watchdog notification intentionally omits <status> tag. With any status value, print.ts would falsely close the task for SDK consumers.

🗺️ Diagrama de Arquitetura

stateDiagram-v2
direction LR
[*] --> pending : registerTask()
pending --> running : task starts
running --> completed : exit code 0 / agent success
running --> failed : exit code != 0 / agent error
running --> killed : kill() called
completed --> [*] : evicted after notified=true
failed --> [*] : evicted after notified=true
killed --> [*] : evicted after notified=true
                    

📋 Resumo do Modulo

One interface, seven implementations. All share TaskStateBase and Task.kill().
notified flag is atomic one-way latch ensuring exactly one notification per completion.
Bash output never touches JS during execution - direct to file via OS descriptors.
O_NOFOLLOW prevents symlink attacks on task output files in sandboxed environments.
DreamTask kills rewind the consolidation lock to prevent permanent blockage.
50-message UI cap is a memory safety valve - swarm sessions reached 36GB RSS without it.
Voltar Proximo