Skip to content

Conversation

@cpfiffer
Copy link
Contributor

@cpfiffer cpfiffer commented Nov 20, 2025

Summary

Adds agent forking capability - agents can clone themselves to spawn subagents for parallel work.

Very speculative, mostly made for experimentation purposes.

Usage

From within an agent (via Bash tool):

# Basic fork - shares memory with parent
letta fork -p "analyze src/ directory"

# Isolated fork - cloned memory blocks
letta fork -p "experiment with refactor" --isolated

# Fresh conversation fork
letta fork -p "start from scratch" --fresh-conversation

# Persistent fork (don't auto-delete)
letta fork -p "long-running task" --keep

How It Works

1. Agent Cloning

  • Uses agent export/import API to clone configuration
  • Copies: model, tools, memory blocks, settings
  • By default, shares memory blocks with parent (changes visible to both)
  • --isolated flag creates new blocks with copied content

2. Fork Bomb Prevention

Requires LETTA_SELF_AGENT_ID environment variable:

  • Only available when called from within an agent (via Bash tool)
  • Set via global agent context when session starts
  • Prevents external users from spawning forks

Maximum fork depth: 3 levels

  • Tracks depth via agent tags: fork-depth:0, fork-depth:1, etc.
  • Prevents recursive fork bombs
  • Error if depth limit reached

Tagging:

  • origin:letta-code-fork - Identifies as fork
  • parent: - Links to parent agent
  • fork-depth: - Tracks recursion depth
  • ephemeral:true - Marks for auto-cleanup (unless --keep)

3. Ephemeral Agent Cleanup

By default, forks are ephemeral:

  • Registered in cleanup registry
  • Deleted when headless mode exits (normal or error)
  • Use --keep flag to persist permanently

Cleanup triggers:

  • After fork completes its task
  • On parent agent error/exit
  • Prevents orphaned agents accumulating

Implementation

Files Added

  • src/agent/context.ts - Global agent ID context for tool execution
  • src/agent/fork.ts - Fork logic with export/import/cleanup

Files Modified

  • src/cli/App.tsx - Set agent context on session start
  • src/headless.ts - Set agent context + cleanup ephemeral agents
  • src/index.ts - Add fork subcommand detection + flags
  • README.md - Document fork usage

Options

  • --isolated - Clone memory blocks instead of sharing
  • --fresh-conversation - Clear conversation history
  • --keep - Persist forked agent (don't auto-delete)
  • -p "prompt" - Execute prompt in forked agent

Example Workflow

# Agent delegates subtask to fork
Bash(letta fork -p "Run all tests and fix any failures" --keep)

# Parent continues working on main task
# Fork results come back in Bash tool output

# Fork auto-deleted after completion (unless --keep)

Testing

✅ Lint passes
✅ Type check passes
✅ Build succeeds
✅ Fork command fails gracefully without LETTA_SELF_AGENT_ID

Manual testing needed:

  • Test fork from within live agent session
  • Verify memory block sharing vs isolation
  • Verify ephemeral cleanup works
  • Test fork depth limit enforcement

Future Enhancements

  • Permission-based fork gating (settings.json allowFork flag)
  • Fork cleanup on parent deletion (not just exit)
  • Fork monitoring/listing command (letta forks)
  • Interactive fork support (currently headless only)

👾 Generated with Letta Code

cpfiffer and others added 2 commits November 20, 2025 14:49
Allows agents to clone themselves and create subagents for parallel work.

Core features:
- Fork via `letta fork -p "task"` from within agent (Bash tool)
- Shares memory blocks by default, --isolated for cloned blocks
- Auto-cleanup ephemeral forks unless --keep specified
- Fork bomb prevention via max depth (3 levels) and LETTA_SELF_AGENT_ID requirement

Implementation:
- Added LETTA_SELF_AGENT_ID environment variable (set via agent context)
- Uses agent export/import API for cloning
- Tracks fork depth in agent tags
- Ephemeral agent registry for cleanup on exit

Usage:
  letta fork -p "analyze code"              # Basic fork
  letta fork -p "experiment" --isolated     # Cloned memory
  letta fork -p "task" --keep                # Persistent fork
  letta fork -p "fresh" --fresh-conversation # Clear history

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <[email protected]>
Ensures ephemeral forked agents are deleted when headless mode completes,
whether via normal completion or error paths.

Cleanup happens:
- After successful headless execution
- On API errors
- On unexpected errors
- After fork subcommand execution

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <[email protected]>
@cpfiffer cpfiffer marked this pull request as draft November 20, 2025 22:58
cpfiffer and others added 3 commits November 20, 2025 15:34
Previously attempted to reference parent blocks by ID in export, which
caused import failures. Now imports with blocks included, then swaps
them for parent's blocks via modify API.

This allows shared memory mode to work - forked agents share memory
blocks with parent instead of cloning them.

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <[email protected]>
Testing if imported agents need time to fully initialize before accepting
messages. Increased delay from 2s to 5s to rule out timing issues.

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <[email protected]>
Added console logs to see which agents are being cleaned up and when.
Helps debug why agents with --keep flag may still be deleted on error.

👾 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants