Skip to content

[Bug] Subagents hallucinate ~/git/Claude path instead of actual working directory #98

@randomm

Description

@randomm

Problem

Subagents sometimes request ~/git/Claude instead of the actual working directory.

Root Cause (Investigated)

Tool descriptions are initialized once at tool creation time, containing a static Instance.directory value. When tools are shared with subagents, they carry the parent session's directory in their description, not the subagent's actual working directory.

Evidence

packages/opencode/src/tool/bash.ts:

  • Line 2-8: description = DESCRIPTION.replaceAll("${directory}", Instance.directory) — evaluated at tool initialization
  • Line 14: workdir parameter description contains static captured directory

packages/opencode/src/tool/task.ts:

  • Line 177: Session.createNext({ directory: parentSession.directory }) — subagent correctly inherits directory
  • But tools passed to subagent still have old descriptions

Why It Happens

  1. Parent session starts in /Users/janni/git/opencode
  2. Tools initialize with descriptions containing this directory
  3. Task tool spawns subagent with correct directory in session metadata
  4. Subagent's prompt includes tools (with OLD descriptions) + system prompt (with correct directory)
  5. Subagent sees conflicting directory info → confusion

Fix Options

  1. Lazily evaluate tool descriptions — Replace static Instance.directory capture with function that reads current context
  2. Rewrite tool descriptions — When passing tools to subagents, replace directory in descriptions
  3. Move directory info to system prompt only — Remove from tool descriptions entirely

Recommended Fix

Make tool descriptions dynamic by lazily evaluating Instance.directory when description is requested.

Files

  • packages/opencode/src/tool/bash.ts — Static directory in description
  • packages/opencode/src/tool/*.ts — Other tools may have same issue
  • packages/opencode/src/tool/tool.ts — Tool definition interface

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