Agent Context Isolation

作者 parcadeid07ff4b06b62无许可证3.9K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库8个月前更新

Agent Context Isolation

仅含说明AI & Agents
AI 生成的概览

通过后台代理和基于文件的协作,避免子代理输出污染主上下文窗口的指导规则。

功能
该技能提供一组规则,用于防止代理输出进入主对话上下文。它建议以后台方式运行子代理,并让其把结果写入缓存目录下的文件,而不是用 TaskOutput 取回完整记录。内容还涉及监控代理进度、识别卡住的代理、用测试验证结果,以及通过文件输出串联多个代理。
适用场景
在编排子代理、且主上下文窗口可能被大量代理记录淹没时使用。也适用于多阶段代理流水线,其中每个阶段应读取上一阶段的文件输出,而不是原始任务输出。
运行要求
不附带脚本,仅为说明性指令。需要具备后台任务执行、文件读写权限的代理环境,以及用于验证的测试命令(如 bun test)。

Agent Context Isolation

Prevent agent output from polluting the main context window.

Rules

1. Use Background Agents with File-Based Coordination

# RIGHT - background agent writes to file, main reads fileTask(subagent_type="...", run_in_background=true, prompt="... Output to: /path/to/file.md")
# WRONG - foreground agent dumps full transcript into main contextTask(subagent_type="...", run_in_background=false)

Background agents with run_in_background=true isolate their context. Have them write results to files in .claude/cache/agents/<agent-type>/.

2. Never Use TaskOutput to Retrieve Results

# WRONG - dumps entire transcript (70k+ tokens) into contextTaskOutput(task_id="<id>")TaskOutput(task_id="<id>", block=true)
# RIGHT - check expected output filesBash("ls -la .claude/cache/agents/<agent-type>/")Bash("bun test")  # verify with tests

TaskOutput returns the full agent transcript. Always use file-based coordination instead.

3. Monitor Agent Progress via System Reminders

# System reminders come automatically:# "Agent a42a16e progress: 6 new tools used, 88914 new tokens"
# To detect completion:# - Watch for progress reminders to stop arriving# - Poll for expected output files: find .claude/cache/agents -name "*.md" -mmin -5# - Check task output file size growth: wc -c /tmp/claude/.../tasks/<id>.output

Stuck agent detection:

  1. Progress reminders stop arriving
  2. Task output file size stops growing
  3. Expected output file not created after reasonable time

4. Verify with Tests, Not Output

After agent work:

  1. Run the test suite directly: bun test
  2. Report pass/fail counts
  3. Only investigate failures if tests fail

5. File-Based Agent Pipeline Pattern

Research agent → .claude/cache/agents/oracle/output.md                          ↓Plan agent → .claude/cache/agents/plan-agent/output.md (reads research)                          ↓Validate agent → .claude/cache/agents/validate-agent/output.md (reads plan)                          ↓Implement agent → src/module.ts (reads validated plan)

Each agent reads the previous agent's file output, not TaskOutput.

Why This Matters

Agent context isolation preserves the main conversation's context budget. Reading agent outputs via TaskOutput floods context, causing:

  • Mid-conversation compaction
  • Lost context about user's original request
  • Repeated explanations needed

Source

  • Session where TaskOutput flooded 70k+ tokens into main context
  • Session 2026-01-01: Successfully used background agents with file-based coordination for SDK Phase 3

来源与署名

来源:parcadei/continuous-claude-v3位于.claude/skills/agent-context-isolation提交d07ff4b

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架