OpenCode Background Agents Plugin

repository·main·Indexed 43 days ago

https://github.com/kdcokenny/opencode-background-agents

A retired plugin for OpenCode V1 that enables asynchronous background research tasks. It allows users to delegate heavy research to read-only sub-agents using tools like delegate, delegation_read, and delegation_list, persisting results as markdown files to prevent context loss during compaction. Note: OpenCode V2 users should use native background subagents.

Tokens
2.5K
Snippets
12
Records
18
Agent score
57%

What's inside OpenCode Background Agents

  1. How background agents persist data and survive compaction

    main

    Background agents solve the problem of context window exhaustion and compaction.

    Persistence Mechanism:

    • Results are saved to disk as markdown files in ~/.local/share/opencode/delegations/.
    • Each delegation is automatically tagged with a title and summary.
    • When context is compacted, the AI can use delegation_list() to scan past research and delegation_read(id) to retrieve specific results from disk.
    • Data survives session restarts and process crashes.
  2. Constraints on using delegate: Read-Only Sub-Agents Only

    main

    The delegate tool can only be used by read-only sub-agents.

    Required Permissions:

    • edit=deny
    • write=deny
    • bash={"...":"deny"}

    Why? Background delegations run in isolated sessions outside OpenCode's session tree. The undo/branching system cannot track changes made in background sessions, so write-capable agents must use the native task tool to avoid data loss.

  3. Manual Installation of background-agents

    main

    If you prefer not to use OCX, you can manually install the plugin by copying the source files from the src/ directory to .opencode/plugin/background-agents.ts.

    Important Caveats:

    • You must manually install dependencies, specifically unique-names-generator.
    • Updates must be handled manually by re-copying files.
  4. Install opencode-background-agents via OCX

    main

    To install the background agents plugin, use the ocx add command pointing to the KDCO registry.

    Note: This project is retired and intended for OpenCode V1 only. For OpenCode V2, use native background subagents. If you are using OpenCode V1, use OCX 2.0.15.

    ocx add kdco/background-agents --from https://registry.kdco.dev
  5. Use background agent tools: delegate, delegation_read, and delegation_list

    main

    The plugin provides three primary tools for managing asynchronous research tasks:

    ToolPurpose
    delegate(prompt, agent)Launch a background task
    delegation_read(id)Retrieve a specific result
    delegation_list()List all delegations with titles and summaries

    Workflow:

    1. Delegate: Call delegate with a prompt and an agent to start research.
    2. Continue: Continue your main conversation/coding while the task runs.
    3. Notified: A <task-notification> arrives in the terminal state when finished.
    4. Retrieve: Call delegation_read(id) to get the results.
  6. Background agent limitations: Timeout and Parity

    main

    Users should be aware of the following operational constraints:

    • Timeout: All delegations timeout after 15 minutes.
    • Upstream Parity: This plugin provides lifecycle parity within OpenCode plugin boundaries, but it does not replicate Claude/OpenCode internal AppState, task queue internals, or runtime notification priority controls.
    • Write Capabilities: Write-capable sub-agents should continue to use the native task tool instead of delegate.
  7. Execute operations with a timeout

    main

    Use withTimeout to wrap an asynchronous operation with a time limit. If the operation exceeds the limit, it will throw a TimeoutError.

    import { withTimeout, TimeoutError } from 'kdco-primitives';
    
    try {
      await withTimeout(async () => {
        // Your long-running task here
      }, 5000); // 5 second timeout
    } catch (e) {
      if (e instanceof TimeoutError) {
        console.error('Operation timed out');
      }
    }
  8. Escape shell commands for different environments

    main

    Use these utilities to ensure shell commands are safely escaped before execution, preventing injection attacks or syntax errors across different operating systems and shells.

    • escapeBash: For Bash environments.
    • escapeAppleScript: For AppleScript.
    • escapeBatch: For Windows Batch environments.
    • assertShellSafe: Validates if a string is safe for shell execution.
    import { escapeBash, escapeBatch, assertShellSafe } from 'kdco-primitives';
    
    const safeBash = escapeBash('my argument');
    const safeBatch = escapeBatch('my argument');
    assertShellSafe('simple-command');