Skip to content

feat: parallel-safe tool execution via wallet mutex#211

Merged
premiumjibles merged 4 commits intomainfrom
worktree-ss-5498
Mar 12, 2026
Merged

feat: parallel-safe tool execution via wallet mutex#211
premiumjibles merged 4 commits intomainfrom
worktree-ss-5498

Conversation

@premiumjibles
Copy link
Contributor

@premiumjibles premiumjibles commented Mar 12, 2026

Summary

  • Adds a composable withWalletLock async mutex (~5 lines) that serializes wallet operations (network switching, signing, broadcasting) to prevent concurrent tool executions from causing React "Maximum update depth exceeded" errors
  • Wraps all 10 wallet-touching executor callbacks so simultaneous LLM tool calls execute one at a time instead of colliding
  • Includes 3 unit tests covering serialization, error recovery, and return value passthrough

Details

When the LLM triggers multiple tool calls simultaneously (e.g., two sends), both UI components try to manipulate wallet state at the same time, causing a render loop. The mutex queues wallet operations so they execute sequentially while all tool cards still appear in the UI at once.

New files:

  • apps/agentic-chat/src/lib/walletMutex.ts — promise-chain mutex
  • apps/agentic-chat/src/lib/walletMutex.test.ts — unit tests

Modified files (10):

  • SendUI.tsx, SwitchNetworkUI.tsx, VaultDepositUI.tsx, VaultWithdrawUI.tsx
  • useSwapExecution.tsx, useLimitOrderExecution.tsx, useConditionalOrderExecution.tsx
  • useCancelConditionalOrderExecution.tsx, useCancelLimitOrderExecution.tsx, useVaultWithdrawAllExecution.ts

Read-only tools (balance queries, price lookups, tx history) are unaffected.

Test plan

  • bun test src/lib/walletMutex.test.ts — 3/3 pass
  • Trigger two wallet operations in quick succession (e.g., ask AI to swap then send) — verify second waits for first
  • Reject a wallet signature in MetaMask — verify next queued operation proceeds
  • Verify single tool calls work exactly as before (no behavioral change)

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Enhanced reliability of wallet-related operations including swaps, deposits, withdrawals, and order management through improved transaction sequencing and execution order guarantees.
  • Tests

    • Added test coverage for wallet operation execution sequencing.

premiumjibles and others added 3 commits March 12, 2026 12:09
Prevents concurrent tool executions from causing React render loops
by serializing wallet-touching operations (network switching, signing,
broadcasting) through a simple promise-chain mutex.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Mar 12, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
shapeshift-agentic Ready Ready Preview, Comment Mar 12, 2026 10:54pm

Request Review

@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 75289c48-6986-4e42-bb71-b8c5b50f257d

📥 Commits

Reviewing files that changed from the base of the PR and between 8bed3f7 and 25416be.

📒 Files selected for processing (12)
  • apps/agentic-chat/src/components/tools/SendUI.tsx
  • apps/agentic-chat/src/components/tools/SwitchNetworkUI.tsx
  • apps/agentic-chat/src/components/tools/VaultDepositUI.tsx
  • apps/agentic-chat/src/components/tools/VaultWithdrawUI.tsx
  • apps/agentic-chat/src/components/tools/useCancelConditionalOrderExecution.tsx
  • apps/agentic-chat/src/components/tools/useCancelLimitOrderExecution.tsx
  • apps/agentic-chat/src/components/tools/useConditionalOrderExecution.tsx
  • apps/agentic-chat/src/components/tools/useLimitOrderExecution.tsx
  • apps/agentic-chat/src/components/tools/useSwapExecution.tsx
  • apps/agentic-chat/src/components/tools/useVaultWithdrawAllExecution.ts
  • apps/agentic-chat/src/lib/walletMutex.test.ts
  • apps/agentic-chat/src/lib/walletMutex.ts

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.


📝 Walkthrough

Walkthrough

Introduces a wallet mutex mechanism (withWalletLock) to serialize asynchronous wallet operations across the agentic-chat application. This mechanism is then applied to multiple tool execution and UI components to prevent concurrent wallet-related operations, ensuring operations queue and execute sequentially rather than in parallel.

Changes

Cohort / File(s) Summary
Wallet Mutex Infrastructure
apps/agentic-chat/src/lib/walletMutex.ts, apps/agentic-chat/src/lib/walletMutex.test.ts
Adds module-scoped pending promise and exports withWalletLock<T>(fn) function that queues async callbacks for sequential execution. Includes three unit tests verifying serialization, lock release on failure, and result propagation.
Tool Execution Components with Lock
apps/agentic-chat/src/components/tools/SendUI.tsx, apps/agentic-chat/src/components/tools/SwitchNetworkUI.tsx, apps/agentic-chat/src/components/tools/VaultDepositUI.tsx, apps/agentic-chat/src/components/tools/VaultWithdrawUI.tsx, apps/agentic-chat/src/components/tools/useCancelConditionalOrderExecution.tsx, apps/agentic-chat/src/components/tools/useCancelLimitOrderExecution.tsx
Each wraps primary execution flow with withWalletLock import and encapsulation. Consolidates state updates, network switching, transaction signing/submission, and error handling within the locked section. Maintains existing validation and user feedback mechanisms.
Order & Swap Execution with Lock
apps/agentic-chat/src/components/tools/useConditionalOrderExecution.tsx, apps/agentic-chat/src/components/tools/useLimitOrderExecution.tsx, apps/agentic-chat/src/components/tools/useSwapExecution.tsx
Wraps multi-step order/swap execution in withWalletLock. Introduces structured step sequences (preparation, network switch, approvals, signing, submission) within locked sections. Adds wallet connectivity validation, signer retrieval, and centralized error handling with toast notifications.
Withdrawal Execution with Lock
apps/agentic-chat/src/components/tools/useVaultWithdrawAllExecution.ts
Wraps entire vault-withdraw-all flow with withWalletLock. Retains validation checks, per-chain batch execution, and success/failure signaling within locked context, preventing concurrent vault withdrawal operations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A lock upon the wallet's door,
No races now, no conflicts more!
Each operation waits its turn,
As pending promises gently churn.
Sequential safety, serene and sweet,
Makes wallet dances complete! ✨

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch worktree-ss-5498
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@premiumjibles premiumjibles marked this pull request as ready for review March 12, 2026 22:55
@premiumjibles premiumjibles merged commit e96a7d0 into main Mar 12, 2026
3 of 4 checks passed
@premiumjibles premiumjibles deleted the worktree-ss-5498 branch March 12, 2026 22:55
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