Skip to content

fix(core): avoid shell job collisions and preserve Gemini tool call IDs - #43961

Closed
major wants to merge 3 commits into
anomalyco:v2from
major:gemini-tool-collision
Closed

major wants to merge 3 commits into
anomalyco:v2from
major:gemini-tool-collision

Conversation

@major

@major major commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #43940

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Foreground shell tool calls can hang forever when Gemini models are used. The command runs to completion and its output is fully written, but the tool never settles because:

  • gemini.ts resets its tool call ID counter per request, assigning tool_0 to the first tool call of every request and ignoring part.functionCall.id when provided by the API.
  • packages/core/src/tool/plugin/shell.ts keyed process-global jobs by the raw tool call ID (context.id), causing job.start to treat subsequent calls with identical IDs as job joins and skip forking the new command handler if an unsettled job with that ID was running.
  • shell.ts then awaited runtime.job.block, which deadlocked on the stale job's deferreds.

This PR fixes both layers:

  • In packages/ai/src/protocols/gemini.ts, uses part.functionCall.id when present and generates a unique identifier fallback using @opencode-ai/schema/identifier when omitted, removing the per-request nextToolCallId counter state.
  • In packages/core/src/tool/plugin/shell.ts, decouples process-global jobs from raw model-provided tool call IDs by omitting id in job.start (allowing job.ts to allocate a unique ID) and passing job.id to notifyWhenDone while keeping context.id for model-facing synthetic session XML and metadata.

How did you verify your code works?

  • Added and updated tests in packages/ai/test/provider/gemini.test.ts verifying that functionCall.id is preserved, fallback IDs are unique across multiple calls and separate requests, and tool continuation requests match.
  • Added a test in packages/core/test/tool-shell.test.ts verifying that multiple shell tool calls sharing the same tool call ID execute without collision.
  • Ran test suites across packages/ai and packages/core.

Screenshots / recordings

N/A (backend execution engine fix)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

- use part.functionCall.id when provided by Gemini models

- generate unique identifier fallbacks when tool call IDs are omitted

- remove per-request counter state that caused duplicate tool_0 IDs

Signed-off-by: Major Hayden <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

- avoid keying process-global jobs with model-supplied tool call IDs

- pass unique job ID to notifyWhenDone while retaining model-visible ID in XML

- test that shell calls do not collide with running background jobs sharing the same tool call ID

Signed-off-by: Major Hayden <[email protected]>
# Conflicts:
#	packages/ai/src/protocols/gemini.ts
#	packages/ai/test/provider/gemini.test.ts
#	packages/core/src/tool/plugin/shell.ts
@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant