-
Notifications
You must be signed in to change notification settings - Fork 2
feat: enable ESC cancellation for Mathematica tool long-running computations #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
- Add signal handler to send SIGINT to Wolfram kernel process - Create EvaluationCancelledError exception for clean error handling - Implement interruptible_evaluate() wrapper for synchronous cancellation - Update all evaluation functions (evaluate, apply_to_last, convert_latex) - Preserve session state and history after cancellation - Maintain full synchronous operation without threading Fixes issue where long-running Mathematica computations could not be cancelled when users pressed ESC in Claude Code. The kernel now properly responds to interrupt signals while keeping the session alive. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Add @handle_cancellation decorator to eliminate repetitive error handling - Extract _get_kernel_pid() helper function for PID discovery across API versions - Improve exception handling with specific WolframKernelException catching - Add comprehensive test coverage for new cancellation functionality - Maintain synchronous design while enabling ESC cancellation in Claude Code - Follow DRY principles and concise code style per codebase standards 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
**Code Elegance Improvements:** - Refactor _get_kernel_pid() with data-driven tuple iteration approach - Make _save_as_*() helper functions explicit about side-effects (return None) - Improve convert_latex exception handling to re-raise EvaluationCancelledError **Testing Enhancement:** - Add comprehensive integration tests for live kernel cancellation - Test session preservation after cancellation - Test direct interruptible_evaluate functionality **Code Quality:** - More concise _get_kernel_pid eliminates repetitive hasattr checks - Better separation of concerns with explicit side-effect functions - Robust cancellation handling prevents fallback on cancelled operations All changes maintain backward compatibility while improving code elegance and following "aggressive concision" principles per codebase philosophy. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
|
I did some more testing and found that it still fails in certain cases when I was using xAct package, it's likely because xAct uses CheckAbort, which will catch the abort and let the rest of the code continue. I'm looking into whether it's possible to bypass that and abort the rest of the evaluation without killing the kernel |
|
Hi Sina, Just checking in on this one -- did you make any progress on the CheckAbort issue with xAct? Happy to help brainstorm if useful. Alternatively, we could merge as-is with a documented limitation and address the edge case in a follow-up PR -- let me know your preference. Best, |
Sorry, this has been pushed to the back of my brain since I haven't been using Mathematica as often recently! |
|
Hi Will, I have been looking into this and trying different things and here are some conclusions (I have tried as suggested by Claude and Gemini):
Thoughts: It might be a good idea to use FrontEnd in Mathematica mcp, similar to Will Barker's NoMoreNotebooks, then it wil be possible to send abort signal like in a mathematica notebook. It is apparently also possible to use Jupyter kernel to connect mathematica with the mcp, and we will be able to send abort through Jupyter's interrupt_request. I'm currently investigating these options! |
Summary
Key Features
🚀 ESC Cancellation Support
Users can now press ESC in Claude Code to cancel long-running Mathematica evaluations:
%reference historywolframclientAPI versions🎯 Elegant Implementation
Testing Instructions for Mathematica Users
Prerequisites
pip install wolframclientInstallation from Branch
Test Scenarios
1. Basic Cancellation Test
Expected: Press ESC, see immediate cancellation message
2. Complex Computation Cancellation
Expected: Can interrupt and continue with new computations
3. Session Preservation Test
Expected:
%references work after cancellation4. Error Handling Test
Verification Steps
✅ Cancellation works: ESC interrupts long evaluations
✅ Session preserved:
%,%%references work after cancellation✅ New evaluations: Can continue computing after cancellation
✅ Error messages: Clear feedback when cancellation occurs
✅ No crashes: Tool remains stable after multiple cancellations
Reporting Issues
If you encounter problems:
$Versionoutputpip show wolframclientTechnical Implementation
Signal Handling Architecture
Cross-API Compatibility
Supports PID discovery across different
wolframclientversions:session.kernel.pidsession.kernel.kernel_proc.pidsession.controller.pidsession.controller.kernel_proc.pidError Recovery
Code Quality Improvements
Based on comprehensive Gemini code review:
🤖 Generated with Claude Code