🤝
Part 5고급⏱ 약 55분 · 2개 섹션
에이전트 생성과 격리
재귀적 에이전트 아키텍처

✦이 챕터에서 배울 것
전체 아키텍처에서 현재 위치
⚙️코어 엔진
→🔧도구 시스템
→🛡️보안
→🧠컨텍스트
→🤝멀티 에이전트
→🌐생태계
→⚡인프라
1
AgentTool 구현
하위 에이전트 생성과 실행 모드
에이전트 생성실행 모드스키마 합성도구 풀 필터링|
tools/AgentTool/AgentTool.tsxtools/AgentTool/runAgent.tsAgentTool은 14개 파일로 구성된 하위 에이전트 생성 도구입니다. 동기/비동기/포크/원격/Worktree 5가지 모드를 지원합니다.
src/tools/AgentTool/AgentTool.tsx:82-120
1// src/tools/AgentTool/AgentTool.tsx — 입력 스키마2 3// 기본 스키마4const baseInputSchema = lazySchema(() => z.object({💡5 description: z.string()6 .describe('A short (3-5 word) description of the task'),7 prompt: z.string()8 .describe('The task for the agent to perform'),9 subagent_type: z.string().optional()10 .describe('The type of specialized agent to use'),11 model: z.enum(['sonnet', 'opus', 'haiku']).optional()12 .describe("Optional model override"),13 run_in_background: z.boolean().optional()14 .describe('Set to true to run in the background'),15}))16 17// 멀티 에이전트 확장 스키마18const fullInputSchema = lazySchema(() => {💡19 const multiAgentSchema = z.object({20 name: z.string().optional()21 .describe('Makes it addressable via SendMessage'),22 team_name: z.string().optional(),23 mode: permissionModeSchema().optional(),24 })25 return baseInputSchema()💡26 .merge(multiAgentSchema) // 스키마 합성!27 .extend({28 isolation: z.enum(['worktree']).optional()29 .describe('Isolation mode'),30 })31})32 33// 피처 게이트로 불필요한 필드 제거34export const inputSchema = lazySchema(() => {💡35 return feature('KAIROS')36 ? fullInputSchema()37 : fullInputSchema().omit({ cwd: true })38})2
에이전트 격리 수준
기본, Worktree, 원격, AsyncLocalStorage 격리
WorktreeCCR 원격AsyncLocalStorage도구 풀 제한|
tools/EnterWorktreeTool/EnterWorktreeTool.tstools/ExitWorktreeTool/ExitWorktreeTool.ts에이전트 격리에는 4가지 수준이 있습니다. 각 수준은 서로 다른 리소스 격리를 제공합니다.
에이전트 격리 수준과 도구 제한
1// 에이전트 격리 4단계2 3// 1. 기본 격리 (같은 프로세스)4// - 도구 풀 필터링 (AgentTool 제외 → 재귀 방지)5// - 격리된 ToolUseContext6// - 같은 파일 시스템 접근7 8// 2. Worktree 격리 (별도 git worktree)9// - createAgentWorktree() → 새 git worktree 생성10// - 파일 시스템 충돌 방지11// - 작업 완료 후 변경 사항 병합 또는 브랜치 반환12 13// 3. AsyncLocalStorage 격리 (인프로세스 팀원)14// - Node.js AsyncLocalStorage로 컨텍스트 격리15// - 팀 기반 아이덴티티 (agentId@teamName)16// - 비동기 메시지 큐17 18// 4. 원격 격리 (CCR 환경) — Ant 전용19// - 완전히 별도의 환경에서 실행20// - JWT + 디바이스 토큰 인증21// - 독립적인 파일 시스템/네트워크22 23// 도구 풀 필터링 (src/constants/tools.ts)24const ALL_AGENT_DISALLOWED_TOOLS = new Set([💡25 'Agent', // 재귀 방지26 'TaskOutput', // 부모만 사용27 'ExitPlanMode', // 부모만 사용28 'TaskStop', // 부모만 사용29])30 31const ASYNC_AGENT_ALLOWED_TOOLS = new Set([💡32 'Bash', 'FileRead', 'FileEdit', 'FileWrite',33 'Glob', 'Grep', 'WebFetch', 'WebSearch',34 'NotebookEdit', 'TodoWrite', 'AskUserQuestion',35 // Agent 제외 → 비동기 에이전트는 하위 에이전트 생성 불가36])