Skip to content

[WS1] Refactor async task tool to minimal-invasion pattern #159

@randomm

Description

@randomm

Parent: #158

Objective

Reduce our async task tool's invasion of upstream files from +603 lines in session/index.ts and +356 lines in session/prompt.ts to approximately +10 lines and +50 lines respectively, following the pattern established by upstream PR anomalyco#7206.

Why This Matters

session/index.ts and session/prompt.ts are the most actively developed files in upstream OpenCode:

  • prompt.ts: 78 commits in 3 months (~every other day)
  • index.ts: 18 commits in 3 months (~twice/week)

Our current 603-line diff in index.ts means every upstream sync will conflict. Reducing to ~10 lines makes conflicts rare and trivially resolvable.

Reference: PR anomalyco#7206 Pattern

Upstream PR anomalyco#7206 (IgorWarzocha/async-subagents) achieves async subagent execution with:

  • session/index.ts: +9 lines (thin hooks only)
  • session/prompt.ts: +108 lines
  • tool/task.ts: +238 lines (bulk of implementation)
  • New additive files for TUI + config

Implementation Plan

Step 1: Analyze current implementation

Step 2: Extract into additive files

  • Create session/async-tasks.ts — all async state management (task slots, completion tracking, auto-wakeup logic)
  • Keep session/tools.ts — already extracted tool resolution (good)
  • Keep tool/task.ts and tool/check_task.ts — already separate (good)
  • Keep util/tasks.ts — already separate (good)

Step 3: Minimize session/index.ts to thin hooks

Target: ~10 lines total, consisting of:

  • Import async-tasks module
  • Hook into session start/end lifecycle
  • Hook into message processing for task completion notifications

Step 4: Minimize session/prompt.ts

Target: ~50 lines total:

  • System prompt injection for async task context
  • Tool registration (task + check_task)

Step 5: Verify functionality

  • All async task features work: background execution, slot concurrency (max 5), check_task polling, auto-wakeup on completion
  • Run full test suite
  • Manual testing with multi-agent workflows

Acceptance Criteria

  • session/index.ts diff ≤20 lines vs upstream
  • session/prompt.ts diff ≤50 lines vs upstream
  • All async task functionality preserved
  • New additive files contain bulk of implementation
  • Tests pass
  • Manual verification with real subagent workflows

Files Involved

  • packages/opencode/src/session/index.ts — REDUCE from +603 to ~+10
  • packages/opencode/src/session/prompt.ts — REDUCE from +356 to ~+50
  • packages/opencode/src/session/async-tasks.ts — NEW (extracted state management)
  • packages/opencode/src/session/tools.ts — KEEP (already extracted)
  • packages/opencode/src/tool/task.ts — KEEP (already separate)
  • packages/opencode/src/tool/check_task.ts — KEEP (already separate)
  • packages/opencode/src/util/tasks.ts — KEEP (already separate)

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions