diff --git a/Launcher.Next.App/PilotCli.cs b/Launcher.Next.App/PilotCli.cs index a2d2bd4..bb068a7 100644 --- a/Launcher.Next.App/PilotCli.cs +++ b/Launcher.Next.App/PilotCli.cs @@ -130,14 +130,23 @@ internal static class PilotCli if (!config.ForceAfterEvidence) { await exit; gracefulExit = true; } else { - var timeout = Task.Delay(TimeSpan.FromSeconds(Math.Clamp(config.GracefulExitTimeoutSeconds, 1, 120))); - if (await Task.WhenAny(exit, timeout) == exit) gracefulExit = true; + // The graceful-exit timeout is deliberately measured only after + // terminal evidence is durable. Killing an active AutoCAD + // command before that boundary would turn ForceAfterEvidence + // into an unsafe general process killer. + while (!exit.IsCompleted && !HasCompletedEvidence(evidencePath)) + await Task.Delay(TimeSpan.FromSeconds(1)); + if (exit.IsCompleted) gracefulExit = true; else { - if (!HasCompletedEvidence(evidencePath)) return new(true, false, false, false, false); - using var process = Process.GetProcessById(execution.ProcessId.Value); - if (!process.HasExited) { process.Kill(entireProcessTree: true); forcedCleanup = true; } - await exit; + var timeout = Task.Delay(TimeSpan.FromSeconds(Math.Clamp(config.GracefulExitTimeoutSeconds, 1, 120))); + if (await Task.WhenAny(exit, timeout) == exit) gracefulExit = true; + else + { + using var process = Process.GetProcessById(execution.ProcessId.Value); + if (!process.HasExited) { process.Kill(entireProcessTree: true); forcedCleanup = true; } + await exit; + } } } }