Skip to content

GSD-style fresh-context-per-task: worktree isolation for autonomous execution #284

Description

@azalio

Summary

map-framework has context compression/compaction but lacks per-task context isolation. On long autonomous sessions, accumulated context degrades LLM output quality. GSD solves this by giving every executor agent a completely fresh 200K-token Claude instance via isolation="worktree".

This is the second-largest structural gap after spec-driven pipeline.

What GSD does

Core mechanism: isolation="worktree"

  • Orchestrator stays thin (~10-15% context for 200K models)
  • Each plan spawns a fresh executor agent in an isolated git worktree:
    Agent(
      subagent_type="gsd-executor",
      isolation="worktree",
      prompt="...minimal instruction + file list to read from disk..."
    )
    
  • Agent gets a clean 200K window (up to 1M for models supporting it)
  • Files are READ from disk, NOT inlined into the prompt
  • After wave completes: git merge --no-ff merges all worktree branches back

Wave-based parallelism with DAG

  • Plans declare dependencies: depends_on: ["01"]
  • Orchestrator groups into dependency-resolved waves:
    Wave 1: Plan 01, Plan 02  (no deps, parallel)
    Wave 2: Plan 03           (depends: 01,02)
    
  • Intra-wave files_modified overlap detection → forces sequential if conflict
  • Sequential Agent() dispatch with run_in_background: true avoids .git/config.lock

Safety guards

  • Worktree path safety: Guards against HEAD on protected refs, cwd-drift, absolute paths outside worktree
  • Post-wave merge gate: Runs full build+test suite after merge to catch cross-plan issues
  • Deletion guard: Refuses bulk deletions outside .planning/
  • Stall detection: Configurable surveillance intervals, heartbeat checkpoints

Context budget awareness

  • Statusline hook: live context usage display
  • Context monitor hook: agent-facing warnings at 35%/25% remaining
  • Read depth scales with window: <500K = frontmatter only, >=500K = full body
  • Adaptive prompt enrichment for 1M+ models vs thinning for sub-200K

Current map-framework state

  • Compression/compaction: Exists but operates on a SINGLE session — all subtasks share accumulated context
  • .map/ state: Branch-scoped checkpoint, but no isolation BETWEEN subtasks within a session
  • No wave parallelism: Subtasks execute sequentially or at actor discretion, not as independent waves
  • No per-subtask worktree: Everything runs in the same git working tree
  • No context budget awareness in runtime: No statusline or warning hooks

Proposed design

Phase 1: Per-subtask worktree isolation (opt-in via config)

  • .map/config.yaml key: worktree_isolation: true
  • Actor spawns each independent subtask in its own worktree:
    Agent(
      subagent_type="map-actor",
      isolation="worktree",
      prompt="...subtask spec + file paths to read..."
    )
    
  • Monitor + Evaluator run in orchestrator context after subtask completes
  • Post-wave merge + full test suite run

Phase 2: Wave-based parallelism

  • map-plan assigns dependency metadata to subtasks
  • map-efficient groups independent subtasks into waves
  • Parallel Agent() dispatch within waves, sequential across waves
  • files_modified overlap detection between subtasks in same wave
  • Post-wave merge gate: build + test

Phase 3: Context budget hooks

  • hooks/context-monitor: Inject warnings at configurable thresholds
  • Statusline: Show current context usage %
  • Adaptive detail: less context → less detail in agent prompts

Safety invariants

  • Worktree path safety (protected refs, cwd-drift, absolute paths)
  • Bulk deletion detection on merge
  • Post-merge verification gate
  • Heartbeat checkpoints to prevent SSE timeouts
  • Configurable stall detection

References

  • GSD: workflows/execute-phase.md (lines 504-647), references/worktree-path-safety.md, references/context-budget.md, references/gates.md
  • GSD Architecture: docs/ARCHITECTURE.md (lines 73-75, 317-357, 389-394)

Acceptance criteria

  • .map/config.yaml key worktree.isolation
  • Per-subtask worktree creation with isolation="worktree"
  • Post-subtask merge with safety guards
  • Wave-based parallelism for independent subtasks
  • Context budget hook (warnings at thresholds)
  • Heartbeat checkpoint on orchestrator SSE stream

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions