You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add multi-node stream management and output modes (Phase 2 of #68) (#71)
* feat: Add multi-node stream management and output modes (Phase 2 of #68)
Implements Phase 2 of issue #68 - Independent stream management for multiple nodes with real-time output modes.
## Changes
### New Components
**Stream Manager** (`src/executor/stream_manager.rs`):
- NodeStream: Independent output buffer and state per node
- MultiNodeStreamManager: Non-blocking coordination of all streams
- ExecutionStatus tracking (Pending/Running/Completed/Failed)
- Per-node exit code and error handling
**Output Modes** (`src/executor/output_mode.rs`):
- OutputMode enum: Normal, Stream, File
- Smart TTY detection with CI environment support
- Mode selection based on CLI flags and environment
### Enhanced Executor
**Parallel Executor** (`src/executor/parallel.rs`):
- execute_streaming_multi(): Parallel execution with real-time output
- Integration with MultiNodeStreamManager
- Support for all three output modes
- Non-blocking stream polling (50ms interval)
### CLI Integration
**Command Line** (`src/cli.rs`):
- Added --stream flag for real-time output mode
- Works with existing --output-dir for file mode
- Default mode remains unchanged (normal)
**Exec Command** (`src/commands/exec.rs`):
- OutputMode detection from CLI flags
- Conditional execution based on mode
- Backward compatible with existing behavior
**Dispatcher** (`src/app/dispatcher.rs`):
- Integrated --stream flag handling
- Mode propagation to executor
### Documentation
**Architecture** (`ARCHITECTURE.md`):
- Comprehensive Phase 2 section (168 lines)
- Usage examples for all output modes
- Design rationale and implementation notes
- Performance characteristics
## Features
### Stream Mode (--stream)
Real-time output with node prefixes:
```bash
bssh -C production --stream "tail -f /var/log/app.log"
[host1] Starting process...
[host2] Starting process...
```
### File Mode (--output-dir)
Save per-node output to timestamped files:
```bash
bssh -C cluster --output-dir ./results "ps aux"
# Creates: results/host1_20251029_143022.stdout
```
### Normal Mode (default)
Traditional output after completion (unchanged).
## Testing
New test suites:
- stream_manager_tests: 7 tests for NodeStream and MultiNodeStreamManager
- output_mode_tests: 3 tests for TTY detection and mode selection
All Phase 2 tests: 10/10 passing
Existing tests: 395/396 passing (1 pre-existing failure)
Clippy: Zero warnings
Build: Success (debug + release)
## Performance
- Stream mode latency: <100ms
- Polling interval: 50ms
- Memory overhead: Minimal (buffered lines only)
- True parallel execution with independent streams
## Backward Compatibility
100% backward compatible:
- Default behavior unchanged
- Existing CLI flags work as before
- Same exit code strategies
- No breaking changes to public API
## Related
- Implements #68 (Phase 2: Tasks 2 & 3)
- Builds on PR #69 (Phase 1)
* fix(security): Add buffer size limits to prevent memory exhaustion - Priority: CRITICAL
- Implement RollingBuffer with MAX_BUFFER_SIZE (10MB per stream)
- Automatically discard old data when buffer exceeds limit
- Add overflow warnings to track dropped data
- Protect against memory DoS attacks from unbounded output
This prevents OOM crashes when nodes produce large amounts of output
(e.g., 100 nodes × 100MB = 10GB RAM exhaustion attack)
* fix(perf): Add stdout/stderr synchronization to prevent race conditions - Priority: CRITICAL
- Implement global Mutex locks for stdout/stderr using once_cell::Lazy
- Create NodeOutputWriter for atomic, prefixed output per node
- Replace all println!/eprintln! with synchronized versions
- Batch write multiple lines while holding lock to prevent interleaving
- Add error handling for write failures with logging
This prevents output corruption when multiple nodes write simultaneously,
ensuring clean, readable output even under high concurrency.
* fix(security): Add file system validation and error handling - Priority: HIGH
- Validate output directory exists and is a directory
- Check write permissions before processing
- Create test file to verify writability
- Add error handling for file write operations
- Continue processing other nodes on individual write failures
- Log clear error messages with paths and reasons
This prevents crashes from permission errors, full disks, or invalid paths,
providing graceful degradation and clear error messages to users.
* fix(perf): Fix channel cleanup and resource leaks - Priority: HIGH
- Add CleanupGuard with Drop trait for semaphore permit release
- Track all channel senders for proper cleanup
- Explicitly drop channels after task completion
- Handle task panics gracefully without affecting other nodes
- Add debug/error logging for all failure paths
- Ensure resources are freed even on panic/error paths
This prevents resource leaks from unclosed channels and unreleased permits,
improving reliability under error conditions and preventing gradual degradation.
Copy file name to clipboardExpand all lines: ARCHITECTURE.md
+171-1Lines changed: 171 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -418,10 +418,180 @@ The existing `execute()` method was refactored to use `execute_streaming()` inte
418
418
- All existing tests pass with zero modifications
419
419
420
420
**Future Phases (Issue #68):**
421
-
- Phase 2: Executor integration for parallel streaming
421
+
-~~Phase 2: Executor integration for parallel streaming~~ ✓ Completed (2025-10-29)
422
422
- Phase 3: UI components (progress bars, live updates)
423
423
- Phase 4: Advanced features (filtering, aggregation)
424
424
425
+
### 4.0.2 Multi-Node Stream Management and Output Modes (Phase 2)
426
+
427
+
**Status:** Implemented (2025-10-29) as part of Phase 2 of Issue #68
428
+
429
+
**Design Motivation:**
430
+
Building on Phase 1's streaming infrastructure, Phase 2 adds independent stream management for multiple nodes and flexible output modes. This enables real-time monitoring of parallel command execution across clusters while maintaining full backward compatibility.
431
+
432
+
**Architecture:**
433
+
434
+
The Phase 2 implementation consists of four key components:
Copy file name to clipboardExpand all lines: src/cli.rs
+6Lines changed: 6 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -112,6 +112,12 @@ pub struct Cli {
112
112
)]
113
113
pubport:Option<u16>,
114
114
115
+
#[arg(
116
+
long,
117
+
help = "Stream output in real-time with [node] prefixes\nEach line of output is prefixed with the node hostname and displayed as it arrives.\nUseful for monitoring long-running commands across multiple nodes.\nAutomatically disabled when output is piped or in CI environments."
0 commit comments