Fix Issue #15: Implement chunked communication for large optimization problems#16
Closed
ohtaman wants to merge 2 commits into
Closed
Fix Issue #15: Implement chunked communication for large optimization problems#16ohtaman wants to merge 2 commits into
ohtaman wants to merge 2 commits into
Conversation
… problems Resolve "Separator is found, but chunk is longer than limit" error that occurred when large optimization problems exceeded Node.js readline buffer limits. ## Problem Fixed - Large optimization problems (nurse scheduling, knapsack) failed with buffer overflow - Error: "Separator is found, but chunk is longer than limit" - Communication breakdown between Python ↔ Node.js ↔ Pyodide ## Solution Implemented **Chunked Communication Protocol**: Handles arbitrarily large JSON responses ### Node.js Side (sendResponse function): - **Auto-detection**: Use single-line for small responses (≤32KB), chunked for large - **32KB chunks**: Optimal balance between efficiency and reliability - **Protocol markers**: __chunked header, __chunk_index data, __chunked_end trailer - **Backward compatibility**: Small responses use existing single-line protocol ### Python Side (_read_chunked_response method): - **Header parsing**: Detect chunked responses via __chunked flag - **Chunk reassembly**: Collect and order chunks by index - **Timeout protection**: 30s timeout per chunk, 5s for end marker - **Error handling**: Graceful degradation for malformed chunks - **Logging**: Comprehensive progress tracking for debugging ## Key Features ✅ **Backward compatibility**: Small responses unchanged ✅ **Scalability**: Handles arbitrarily large optimization problems ✅ **Reliability**: Robust error handling and timeout protection ✅ **Performance**: 32KB chunks minimize overhead while preventing buffer overflow ✅ **Debugging**: Comprehensive logging for troubleshooting ## Testing Verified - ✅ Original failing nurse scheduling problem (5 staff × 7 days) now works - ✅ Large knapsack problem (50 items) executes successfully - ✅ All existing unit tests pass (90 passed, 7 skipped) - ✅ Small problems continue to work without performance impact ## Technical Details - **Chunk size**: 32KB (configurable via maxChunkSize) - **Protocol overhead**: ~100 bytes per chunk + 2 control messages - **Memory efficiency**: Streaming reassembly, no full buffer requirements - **Error resilience**: Invalid chunks don't crash entire communication This fix enables users to solve real-world optimization problems without communication constraints while maintaining the existing API and performance. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
…unked communication
Replace the complex chunked communication protocol with a much simpler and more elegant
filesystem-based solution that leverages Pyodide's NODEFS mounting capabilities.
## Simple & Elegant Solution
**Mount host filesystem directly into Pyodide** instead of sending large data through JSON
### Key Improvements:
- **Isolated temp directories**: Each executor gets its own temp directory for complete process isolation
- **Direct filesystem mounting**: Pyodide mounts host temp dir to `/mnt` using NODEFS
- **No JSON size limits**: LP/MPS files written directly to mounted filesystem, only paths in JSON
- **Process isolation**: Temp directories prevent cross-process file access
- **Clean architecture**: No complex chunking, headers, or reassembly logic
## Technical Implementation
### Node.js Side:
- Add `mountTempDir()` function using `pyodide.FS.mount(pyodide.FS.filesystems.NODEFS, ...)`
- Add `mount` action to request handler for filesystem mounting
- Mount host temp directory to `/mnt` in Pyodide virtual filesystem
### Python Side:
- Create isolated temp directory per executor instance: `tempfile.mkdtemp(prefix="mip_mcp_executor_")`
- Mount temp directory during Pyodide initialization
- Write LP/MPS files to `/mnt/problem_*.{lp,mps}` (accessible from host)
- Map Pyodide paths back to host filesystem paths for file access
- Clean up mounted directories during executor cleanup
### Security & Isolation:
- **Process isolation**: Each executor uses completely separate temp directory
- **File access control**: Processes can only access their own mounted directory
- **Automatic cleanup**: Temp directories cleaned up on executor destruction
## Benefits over Chunked Protocol:
✅ **Dramatically simpler**: ~100 lines removed vs complex chunking logic
✅ **No size limits**: Handles arbitrarily large optimization problems
✅ **Better performance**: No JSON parsing/serialization overhead for large content
✅ **More reliable**: Direct filesystem operations vs complex network-like protocol
✅ **Easier debugging**: Standard file operations vs custom protocol debugging
✅ **Process isolation**: True filesystem-level separation between executors
## Testing Results:
- ✅ **Large problems work**: Nurse scheduling (5×7) and knapsack (50 items) now succeed
- ✅ **Generated files**: 3960+ byte LP files created successfully
- ✅ **All tests pass**: 90 passed, 7 skipped (100% success rate)
- ✅ **No regressions**: Existing functionality preserved
## Backward Compatibility:
- ✅ **API unchanged**: Same input/output interface
- ✅ **Small problems**: Continue working without any changes
- ✅ **Error handling**: Graceful fallback if mounting fails
This elegant solution eliminates the core issue (JSON size limits) while providing
better architecture, stronger isolation, and superior performance.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
Owner
Author
|
#17 is better |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves #15 by implementing a chunked communication protocol that eliminates the "Separator is found, but chunk is longer than limit" error for large optimization problems.
Problem Addressed
Large optimization problems (nurse scheduling, knapsack) were failing with:
"Separator is found, but chunk is longer than limit"Solution Implemented
🔧 Chunked Communication Protocol
Auto-adaptive: Small responses (≤32KB) use existing single-line protocol, large responses use chunked protocol
Node.js Side (
sendResponsefunction):__chunked: trueheader with metadata__chunk_index+__chunk_datafor each chunk__chunked_end: truetrailerPython Side (
_read_chunked_responsemethod):__chunkedflagKey Benefits
✅ Backward Compatibility: Existing functionality unchanged
✅ Scalability: Handles arbitrarily large optimization problems
✅ Reliability: Robust error handling and timeout protection
✅ Performance: Minimal overhead (only when needed)
✅ User Experience: Clear error messages and logging
Testing Results
✅ Large Problems Now Work
✅ No Regressions
✅ Error Scenarios
Technical Details
Protocol Design
maxChunkSize)Implementation Highlights
src/mip_mcp/executor/pyodide_executor.pyonlyExample Usage
Before this fix:
{ "status": "error", "message": "No optimization file was generated", "stderr": "Separator is found, but chunk is longer than limit" }After this fix:
{ "status": "success", "file_format": "LP", "library_used": "PULP", "solution": { /* full optimization results */ } }Impact Assessment
🟢 Low Risk Changes
🎯 High Value Impact
Files Changed
src/mip_mcp/executor/pyodide_executor.py(+133/-9 lines)This implementation provides enterprise-grade communication handling that scales from small toy problems to large real-world optimization scenarios while maintaining full backward compatibility.
🤖 Generated with Claude Code