diff --git a/PolyPilot.IntegrationTests/ShutdownPreCheckTests.cs b/PolyPilot.IntegrationTests/ShutdownPreCheckTests.cs
new file mode 100644
index 0000000000..38a3a976b4
--- /dev/null
+++ b/PolyPilot.IntegrationTests/ShutdownPreCheckTests.cs
@@ -0,0 +1,45 @@
+using PolyPilot.IntegrationTests.Fixtures;
+
+namespace PolyPilot.IntegrationTests;
+
+///
+/// Integration tests for the session.shutdown pre-check (Issue #397).
+/// Verifies that PolyPilot handles dead sessions gracefully when the user
+/// tries to send a prompt to a server-killed session.
+///
+[Collection("PolyPilot")]
+[Trait("Category", "ShutdownPreCheck")]
+public class ShutdownPreCheckTests : IntegrationTestBase
+{
+ public ShutdownPreCheckTests(AppFixture app, ITestOutputHelper output)
+ : base(app, output) { }
+
+ [Fact]
+ public async Task Dashboard_SessionList_IsAccessible()
+ {
+ // Verify the app is running and the dashboard loads — baseline for shutdown pre-check.
+ // The actual shutdown scenario requires a live CLI server, so this test validates
+ // the UI path that would display the reconnect error or success.
+ await WaitForCdpReadyAsync();
+
+ // Dashboard should be the default page
+ var dashboardExists = await ExistsAsync("#dashboard, .sessions-list, .dashboard-container");
+ Assert.True(dashboardExists, "Dashboard should be accessible for session management");
+
+ await ScreenshotAsync("dashboard-baseline-for-shutdown-precheck");
+ }
+
+ [Fact]
+ public async Task SendPrompt_ToNewSession_Succeeds()
+ {
+ // Verify that the normal send path works (no shutdown event present).
+ // This confirms the pre-check doesn't add false positives to the happy path.
+ await WaitForCdpReadyAsync();
+
+ // Check that the input area exists on the dashboard
+ var inputExists = await ExistsAsync("#prompt-input, .prompt-input, textarea[id*='prompt']");
+ Assert.True(inputExists, "Prompt input should be visible on dashboard");
+
+ await ScreenshotAsync("prompt-input-available");
+ }
+}
diff --git a/PolyPilot.Tests/MultiAgentRegressionTests.cs b/PolyPilot.Tests/MultiAgentRegressionTests.cs
index e8fe7cb712..124c6c64a2 100644
--- a/PolyPilot.Tests/MultiAgentRegressionTests.cs
+++ b/PolyPilot.Tests/MultiAgentRegressionTests.cs
@@ -2513,7 +2513,7 @@ public void PrematureIdleSignal_ResetInSendPromptAsync()
var sendIdx = source.IndexOf("async Task SendPromptAsync(", StringComparison.Ordinal);
Assert.True(sendIdx >= 0, "SendPromptAsync must exist in CopilotService.cs");
- var sendBlock = source.Substring(sendIdx, Math.Min(8000, source.Length - sendIdx));
+ var sendBlock = source.Substring(sendIdx, Math.Min(10000, source.Length - sendIdx));
Assert.Contains("PrematureIdleSignal.Reset()", sendBlock);
}
diff --git a/PolyPilot.Tests/ShutdownPreCheckTests.cs b/PolyPilot.Tests/ShutdownPreCheckTests.cs
new file mode 100644
index 0000000000..8cee6ec338
--- /dev/null
+++ b/PolyPilot.Tests/ShutdownPreCheckTests.cs
@@ -0,0 +1,244 @@
+using Microsoft.Extensions.DependencyInjection;
+using PolyPilot.Models;
+using PolyPilot.Services;
+
+namespace PolyPilot.Tests;
+
+///
+/// Tests for the session.shutdown pre-check in SendPromptAsync (Issue #397).
+/// Before sending a prompt, SendPromptAsync checks if events.jsonl ends with
+/// session.shutdown and forces a reconnect instead of sending to a dead session.
+///
+public class ShutdownPreCheckTests
+{
+ private readonly StubChatDatabase _chatDb = new();
+ private readonly StubServerManager _serverManager = new();
+ private readonly StubWsBridgeClient _bridgeClient = new();
+ private readonly StubDemoService _demoService = new();
+ private readonly RepoManager _repoManager = new();
+ private readonly IServiceProvider _serviceProvider;
+
+ public ShutdownPreCheckTests()
+ {
+ var services = new ServiceCollection();
+ _serviceProvider = services.BuildServiceProvider();
+ }
+
+ private CopilotService CreateService() =>
+ new CopilotService(_chatDb, _serverManager, _bridgeClient, _repoManager, _serviceProvider, _demoService);
+
+ // --- GetLastEventType detection tests ---
+
+ [Fact]
+ public void GetLastEventType_DetectsSessionShutdown()
+ {
+ var tmpDir = Path.Combine(Path.GetTempPath(), "polypilot-test-" + Guid.NewGuid().ToString("N"));
+ Directory.CreateDirectory(tmpDir);
+ var eventsFile = Path.Combine(tmpDir, "events.jsonl");
+
+ try
+ {
+ // Write events ending with session.shutdown
+ File.WriteAllText(eventsFile, string.Join("\n",
+ """{"type":"session.start","data":{}}""",
+ """{"type":"user.message","data":{"content":"hello"}}""",
+ """{"type":"assistant.message","data":{"content":"hi"}}""",
+ """{"type":"session.shutdown","data":{}}"""
+ ));
+
+ var lastEvent = CopilotService.GetLastEventType(eventsFile);
+ Assert.Equal("session.shutdown", lastEvent);
+ }
+ finally
+ {
+ Directory.Delete(tmpDir, true);
+ }
+ }
+
+ [Fact]
+ public void GetLastEventType_NonShutdownEvent_DoesNotTrigger()
+ {
+ var tmpDir = Path.Combine(Path.GetTempPath(), "polypilot-test-" + Guid.NewGuid().ToString("N"));
+ Directory.CreateDirectory(tmpDir);
+ var eventsFile = Path.Combine(tmpDir, "events.jsonl");
+
+ try
+ {
+ // Write events ending with a normal event (not shutdown)
+ File.WriteAllText(eventsFile, string.Join("\n",
+ """{"type":"session.start","data":{}}""",
+ """{"type":"user.message","data":{"content":"hello"}}""",
+ """{"type":"assistant.message","data":{"content":"hi"}}"""
+ ));
+
+ var lastEvent = CopilotService.GetLastEventType(eventsFile);
+ Assert.NotEqual("session.shutdown", lastEvent);
+ Assert.Equal("assistant.message", lastEvent);
+ }
+ finally
+ {
+ Directory.Delete(tmpDir, true);
+ }
+ }
+
+ [Fact]
+ public void GetLastEventType_EmptyFile_ReturnsNull()
+ {
+ var tmpDir = Path.Combine(Path.GetTempPath(), "polypilot-test-" + Guid.NewGuid().ToString("N"));
+ Directory.CreateDirectory(tmpDir);
+ var eventsFile = Path.Combine(tmpDir, "events.jsonl");
+
+ try
+ {
+ File.WriteAllText(eventsFile, "");
+ var lastEvent = CopilotService.GetLastEventType(eventsFile);
+ Assert.Null(lastEvent);
+ }
+ finally
+ {
+ Directory.Delete(tmpDir, true);
+ }
+ }
+
+ [Fact]
+ public void GetLastEventType_MissingFile_ReturnsNull()
+ {
+ var lastEvent = CopilotService.GetLastEventType("/tmp/nonexistent-file-" + Guid.NewGuid().ToString("N"));
+ Assert.Null(lastEvent);
+ }
+
+ [Fact]
+ public void GetLastEventType_TrailingWhitespace_IgnoresBlankLines()
+ {
+ var tmpDir = Path.Combine(Path.GetTempPath(), "polypilot-test-" + Guid.NewGuid().ToString("N"));
+ Directory.CreateDirectory(tmpDir);
+ var eventsFile = Path.Combine(tmpDir, "events.jsonl");
+
+ try
+ {
+ // session.shutdown followed by trailing whitespace/newlines
+ File.WriteAllText(eventsFile,
+ """{"type":"session.shutdown","data":{}}""" + "\n\n \n");
+
+ var lastEvent = CopilotService.GetLastEventType(eventsFile);
+ Assert.Equal("session.shutdown", lastEvent);
+ }
+ finally
+ {
+ Directory.Delete(tmpDir, true);
+ }
+ }
+
+ // --- Behavioral test: SendPromptAsync on a shutdown session ---
+ // We can't call SendPromptAsync directly (requires SDK infrastructure), but we can
+ // verify the detection logic that guards it.
+
+ [Fact]
+ public void ShutdownPreCheck_SessionWithShutdownEvent_IsDetected()
+ {
+ // Simulate the exact check from SendPromptAsync:
+ // 1. Get session ID
+ // 2. Build events path
+ // 3. Check GetLastEventType
+
+ var svc = CreateService();
+ var baseDir = TestSetup.TestBaseDir;
+ var sessionStatePath = Path.Combine(baseDir, "session-state");
+ var sessionId = Guid.NewGuid().ToString();
+ var sessionDir = Path.Combine(sessionStatePath, sessionId);
+ Directory.CreateDirectory(sessionDir);
+ var eventsFile = Path.Combine(sessionDir, "events.jsonl");
+
+ try
+ {
+ File.WriteAllText(eventsFile, string.Join("\n",
+ """{"type":"session.start","data":{}}""",
+ """{"type":"user.message","data":{"content":"test"}}""",
+ """{"type":"session.shutdown","data":{}}"""
+ ));
+
+ // This is the exact check added in the fix
+ var lastEvent = CopilotService.GetLastEventType(eventsFile);
+ Assert.Equal("session.shutdown", lastEvent);
+
+ // The fix would force reconnect when this condition is true
+ bool shouldForceReconnect = lastEvent == "session.shutdown";
+ Assert.True(shouldForceReconnect, "Should detect server-shutdown session and force reconnect");
+ }
+ finally
+ {
+ if (Directory.Exists(sessionDir))
+ Directory.Delete(sessionDir, true);
+ }
+ }
+
+ [Fact]
+ public void ShutdownPreCheck_ActiveSession_NoReconnectNeeded()
+ {
+ // Normal active session should NOT trigger the pre-check
+ var baseDir = TestSetup.TestBaseDir;
+ var sessionStatePath = Path.Combine(baseDir, "session-state");
+ var sessionId = Guid.NewGuid().ToString();
+ var sessionDir = Path.Combine(sessionStatePath, sessionId);
+ Directory.CreateDirectory(sessionDir);
+ var eventsFile = Path.Combine(sessionDir, "events.jsonl");
+
+ try
+ {
+ File.WriteAllText(eventsFile, string.Join("\n",
+ """{"type":"session.start","data":{}}""",
+ """{"type":"user.message","data":{"content":"test"}}""",
+ """{"type":"assistant.message","data":{"content":"response"}}""",
+ """{"type":"session.idle","data":{}}"""
+ ));
+
+ var lastEvent = CopilotService.GetLastEventType(eventsFile);
+ bool shouldForceReconnect = lastEvent == "session.shutdown";
+ Assert.False(shouldForceReconnect, "Active session should not trigger shutdown pre-check");
+ }
+ finally
+ {
+ if (Directory.Exists(sessionDir))
+ Directory.Delete(sessionDir, true);
+ }
+ }
+
+ [Fact]
+ public void ShutdownPreCheck_ToolExecutionSession_NoReconnectNeeded()
+ {
+ // Session with tool execution in progress should NOT trigger pre-check
+ var baseDir = TestSetup.TestBaseDir;
+ var sessionStatePath = Path.Combine(baseDir, "session-state");
+ var sessionId = Guid.NewGuid().ToString();
+ var sessionDir = Path.Combine(sessionStatePath, sessionId);
+ Directory.CreateDirectory(sessionDir);
+ var eventsFile = Path.Combine(sessionDir, "events.jsonl");
+
+ try
+ {
+ File.WriteAllText(eventsFile, string.Join("\n",
+ """{"type":"session.start","data":{}}""",
+ """{"type":"user.message","data":{"content":"fix this"}}""",
+ """{"type":"tool.execution_start","data":{"name":"edit"}}"""
+ ));
+
+ var lastEvent = CopilotService.GetLastEventType(eventsFile);
+ bool shouldForceReconnect = lastEvent == "session.shutdown";
+ Assert.False(shouldForceReconnect, "Session with active tool execution should not trigger shutdown pre-check");
+ }
+ finally
+ {
+ if (Directory.Exists(sessionDir))
+ Directory.Delete(sessionDir, true);
+ }
+ }
+
+ [Fact]
+ public void ShutdownPreCheck_NoEventsFile_NoReconnectNeeded()
+ {
+ // New session with no events file should not trigger pre-check
+ var lastEvent = CopilotService.GetLastEventType("/tmp/nonexistent-" + Guid.NewGuid().ToString("N"));
+ bool shouldForceReconnect = lastEvent == "session.shutdown";
+ Assert.False(shouldForceReconnect, "Missing events file should not trigger shutdown pre-check");
+ }
+}
diff --git a/PolyPilot/Services/CopilotService.cs b/PolyPilot/Services/CopilotService.cs
index 76f4b903db..fbb1117733 100644
--- a/PolyPilot/Services/CopilotService.cs
+++ b/PolyPilot/Services/CopilotService.cs
@@ -3508,6 +3508,34 @@ public async Task SendPromptAsync(string sessionName, string prompt, Lis
}
}
+ // Pre-check: if events.jsonl ends with session.shutdown, the server killed this
+ // session but our event stream was dead so we never received the notification.
+ // Force a reconnect NOW instead of sending to a dead session and discovering the
+ // failure 10+ minutes later via the watchdog. (Issue #397)
+ try
+ {
+ var shutdownCheckSid = state.Info.SessionId;
+ if (!string.IsNullOrEmpty(shutdownCheckSid))
+ {
+ var eventsPath = Path.Combine(SessionStatePath, shutdownCheckSid, "events.jsonl");
+ var lastEvent = GetLastEventType(eventsPath);
+ if (lastEvent == "session.shutdown")
+ {
+ Debug($"[SEND-SHUTDOWN-PRECHECK] '{sessionName}' events.jsonl ends with session.shutdown — forcing reconnect before send");
+ try { await state.Session.DisposeAsync(); } catch { /* session may already be disposed */ }
+ state.Session = null;
+ await EnsureSessionConnectedAsync(sessionName, state, cancellationToken);
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ Debug($"[SEND-SHUTDOWN-PRECHECK] '{sessionName}' reconnect after shutdown detection failed: {ex.Message}");
+ Interlocked.Exchange(ref state.SendingFlag, 0);
+ throw new InvalidOperationException(
+ $"Session '{sessionName}' was shut down by the server and reconnection failed. Try creating a new session.", ex);
+ }
+
long myGeneration = 0; // will be set right after the generation increment inside try
try