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 從公開儲存庫中收錄這些內容。

檢舉或申請下架