Skip to content

fix(bash-bg): stop overriding the built-in bash tool#47

Closed
mgabor3141 wants to merge 1 commit into
mainfrom
fix-bash-bg-resume-cwd
Closed

fix(bash-bg): stop overriding the built-in bash tool#47
mgabor3141 wants to merge 1 commit into
mainfrom
fix-bash-bg-resume-cwd

Conversation

@mgabor3141
Copy link
Copy Markdown
Owner

Summary

Fixes a cwd-mismatch bug in resumed pi sessions caused by pi-bash-bg overriding the built-in bash tool with one whose cwd was captured at extension-load time.

The previous version did:

const bashTool = createBashTool(process.cwd());   // snapshot at load
pi.registerTool({ ...bashTool, description: ... }); // overrides built-in

process.cwd() is the launcher's cwd at extension-load time, not the resumed session's cwd. The cwd was baked into the spawn closure, so after pi --resume from a different directory, bash commands kept running in the launcher's directory while the rest of pi (including the footer) tracked the session's cwd.

The extension only ever needed to rewrite background commands; the registerTool call existed to attach a description appendix. Dropping it lets pi's built-in bash tool (which is wired to AgentSession._cwd) execute the command, and the tool_call handler still mutates event.input.command in place to perform the rewrite.

Changes

  • packages/bash-bg/src/index.ts: remove registerTool and the createBashTool import; keep only the tool_call rewrite.
  • packages/bash-bg/test/index.test.ts: replace the description-appendix test with regression tests that assert the extension does not call registerTool, that it registers a tool_call handler, and that the handler rewrites bg commands while leaving non-bg / non-bash calls alone.
  • packages/bash-bg/README.md: drop the description-appendix mentions.
  • .changeset/bash-bg-fix-resume-cwd.md: patch entry.
  • AGENTS.md: small guidance update asking for one-line changeset summaries.

Trade-off

The bash tool description no longer carries a "background jobs continue running…" hint to the model. The rewritten command's [bg] pid=... log=... output is self-explanatory in practice, and is well-aligned with how bash backgrounding is conventionally communicated.

Context

Related upstream issue: earendil-works/pi#4006

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 2, 2026

Greptile Summary

Fixes a cwd-mismatch bug in resumed pi sessions by removing the registerTool call that was snapshotting process.cwd() at extension-load time and baking it into the bash-tool's spawn closure. The extension now only registers a tool_call handler that mutates event.input.command in place, letting the built-in bash tool (which tracks the live session cwd) execute the command.

Confidence Score: 5/5

Safe to merge — targeted removal of the cwd-snapshotting registerTool call with good regression test coverage.

The change is minimal and well-scoped: one import removed, one block deleted, tests updated to match. The root cause (process.cwd() captured at load time) and fix (delegate cwd handling to the built-in tool) are clearly correct. New tests directly verify the regression-prone behavior.

No files require special attention.

Important Files Changed

Filename Overview
packages/bash-bg/src/index.ts Removes registerTool/createBashTool call that baked in a fixed cwd; retains only the tool_call handler for command rewriting. Clean, focused fix.
packages/bash-bg/test/index.test.ts Replaces the description-appendix test with 4 targeted regression tests covering: no registerTool call, handler registration, bg-command rewriting, and pass-through for non-bg/non-bash events.
packages/bash-bg/README.md Removes mentions of the description-appendix feature to align with the new implementation.
.changeset/bash-bg-fix-resume-cwd.md Adds patch-level changeset entry for the cwd bug fix in pi-bash-bg.
AGENTS.md Minor doc update asking for single-line changeset summaries; no code impact.

Sequence Diagram

sequenceDiagram
    participant Agent
    participant pi as pi (built-in bash tool)
    participant ext as bash-bg extension
    participant Shell

    Note over ext: extension load — no registerTool call,<br/>no cwd snapshot taken

    Agent->>pi: tool_call { name: "bash", command: "sleep 300 &" }
    pi->>ext: emit tool_call event
    ext->>ext: detectBackground(command)
    ext->>ext: rewriteCommand() → mutate event.input.command in place
    ext-->>pi: (returns, no result override)
    pi->>Shell: exec rewritten command (using session's live cwd)
    Shell-->>pi: "[bg] pid=... log=..."
    pi-->>Agent: tool result
Loading

Reviews (1): Last reviewed commit: "fix(bash-bg): stop overriding the built-..." | Re-trigger Greptile

@mgabor3141
Copy link
Copy Markdown
Owner Author

Closing in favor of #43, which already fixes the same root cause (pi.registerTool(...) override clobbering the built-in bash tool), plus preserves the background-jobs guidance via a before_agent_start system-prompt hook. I missed #43 when opening this; my apologies for the duplicate.

@mgabor3141 mgabor3141 closed this May 2, 2026
@mgabor3141 mgabor3141 deleted the fix-bash-bg-resume-cwd branch May 2, 2026 10:13
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.

1 participant