diff --git a/internal/llm/tools/shell/comparison_test.go b/internal/llm/tools/shell/comparison_test.go index d0b615f0968cdfa00df24b62894e0b0bf8ab8bec..7fcd720b5ecdfba236ed7316dfc26b63d5ff9605 100644 --- a/internal/llm/tools/shell/comparison_test.go +++ b/internal/llm/tools/shell/comparison_test.go @@ -28,7 +28,7 @@ func TestShellPerformanceComparison(t *testing.T) { assert.Equal(t, 0, exitCode) assert.Contains(t, stdout, "hello") assert.Empty(t, stderr) - + t.Logf("Quick command took: %v", duration) } @@ -48,13 +48,13 @@ func TestShellCPUUsageComparison(t *testing.T) { start := time.Now() _, stderr, exitCode, _, err := shell.Exec(context.Background(), "sleep 0.1", 1000) duration := time.Since(start) - + runtime.ReadMemStats(&m2) require.NoError(t, err) assert.Equal(t, 0, exitCode) assert.Empty(t, stderr) - + memGrowth := m2.Alloc - m1.Alloc t.Logf("Sleep 0.1s command took: %v", duration) t.Logf("Memory growth during polling: %d bytes", memGrowth) @@ -72,7 +72,7 @@ func BenchmarkShellPolling(b *testing.B) { b.ResetTimer() b.ReportAllocs() - + for i := 0; i < b.N; i++ { // Use a short sleep to measure polling overhead _, _, exitCode, _, err := shell.Exec(context.Background(), "sleep 0.02", 500) @@ -80,4 +80,4 @@ func BenchmarkShellPolling(b *testing.B) { b.Fatalf("Command failed: %v, exit code: %d", err, exitCode) } } -} \ No newline at end of file +} diff --git a/internal/llm/tools/shell/shell.go b/internal/llm/tools/shell/shell.go index 99e197afbcc2d3c8923455567a5a288e868c9ffb..6c94904c9584c8d1500130196fef10cad202620d 100644 --- a/internal/llm/tools/shell/shell.go +++ b/internal/llm/tools/shell/shell.go @@ -192,10 +192,10 @@ echo $EXEC_EXIT_CODE > %s // Use exponential backoff polling pollInterval := 1 * time.Millisecond maxPollInterval := 100 * time.Millisecond - + ticker := time.NewTicker(pollInterval) defer ticker.Stop() - + for { select { case <-ctx.Done(): @@ -219,7 +219,7 @@ echo $EXEC_EXIT_CODE > %s return } } - + // Exponential backoff to reduce CPU usage for longer-running commands if pollInterval < maxPollInterval { pollInterval = time.Duration(float64(pollInterval) * 1.5) diff --git a/internal/llm/tools/shell/shell_test.go b/internal/llm/tools/shell/shell_test.go index f454e87398e6d2617f4d3f1210161f8f9d5b0893..327ec91db5f2cdffdbb501648df1546e4746fabb 100644 --- a/internal/llm/tools/shell/shell_test.go +++ b/internal/llm/tools/shell/shell_test.go @@ -28,7 +28,7 @@ func TestShellPerformanceImprovement(t *testing.T) { assert.Equal(t, 0, exitCode) assert.Contains(t, stdout, "hello world") assert.Empty(t, stderr) - + // Quick commands should complete very fast with our exponential backoff assert.Less(t, duration, 50*time.Millisecond, "Quick command should complete fast with exponential backoff") } @@ -44,11 +44,11 @@ func BenchmarkShellQuickCommands(b *testing.B) { b.ResetTimer() b.ReportAllocs() - + for i := 0; i < b.N; i++ { _, _, exitCode, _, err := shell.Exec(context.Background(), "echo test", 0) if err != nil || exitCode != 0 { b.Fatalf("Command failed: %v, exit code: %d", err, exitCode) } } -} \ No newline at end of file +}