Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Start Console again when it is launched while quitting (#990)

Abel Dantas99edb101af91
diff --git a/console/apps/switch-console-desktop/src/main/app/deeplinks.ts b/console/apps/switch-console-desktop/src/main/app/deeplinks.ts
index 4ef158484..a5f1955f4 100644
--- a/console/apps/switch-console-desktop/src/main/app/deeplinks.ts
+++ b/console/apps/switch-console-desktop/src/main/app/deeplinks.ts
@@ -140,7 +140,7 @@ export function flushPendingDeeplink(): void {
/** Pull a `switchdash://…` arg out of a process argv list (Windows/Linux pass
* the deeplink as a launch argument rather than via the open-url event). */
-function deeplinkFromArgv(argv: string[]): string | undefined {
+export function deeplinkFromArgv(argv: string[]): string | undefined {
return argv.find((arg) => arg.startsWith(`${DEEPLINK_SCHEME}://`));
}
diff --git a/console/apps/switch-console-desktop/src/main/app/relaunch-on-quit.test.ts b/console/apps/switch-console-desktop/src/main/app/relaunch-on-quit.test.ts
new file mode 100644
index 000000000..c923e1b26
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/main/app/relaunch-on-quit.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, it } from 'vitest';
+import { LaunchesWhileQuitting } from './relaunch-on-quit';
+
+const findDeeplink = (argv: string[]) => argv.find((arg) => arg.startsWith('switchdash://'));
+
+describe('LaunchesWhileQuitting', () => {
+ it('does not relaunch when no launch arrived', () => {
+ expect(new LaunchesWhileQuitting(findDeeplink).relaunchArgs()).toBeNull();
+ });
+
+ it('relaunches with a plain launch', () => {
+ const launches = new LaunchesWhileQuitting(findDeeplink);
+ launches.note([]);
+ expect(launches.relaunchArgs()).toEqual([]);
+ expect(launches.deeplink).toBeNull();
+ });
+
+ it('keeps a deep link when a plain launch follows it', () => {
+ const launches = new LaunchesWhileQuitting(findDeeplink);
+ launches.note(['switchdash://session?room=a']);
+ launches.note(['--flag']);
+ expect(launches.relaunchArgs()).toEqual(['--flag', 'switchdash://session?room=a']);
+ expect(launches.deeplink).toBe('switchdash://session?room=a');
+ });
+
+ it('replaces an earlier deep link with a later one', () => {
+ const launches = new LaunchesWhileQuitting(findDeeplink);
+ launches.note(['switchdash://session?room=a']);
+ launches.note(['switchdash://session?room=b']);
+ expect(launches.relaunchArgs()).toEqual(['switchdash://session?room=b']);
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/main/app/relaunch-on-quit.ts b/console/apps/switch-console-desktop/src/main/app/relaunch-on-quit.ts
new file mode 100644
index 000000000..131ec4d90
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/main/app/relaunch-on-quit.ts
@@ -0,0 +1,32 @@
+/**
+ * Launches that arrive while Console is quitting, kept so Console can start
+ * again with them once it has quit.
+ *
+ * The deep link is kept apart from the rest of the arguments: a plain launch
+ * after a deep link must not lose the link, and a later deep link replaces an
+ * earlier one, since that is the one the person clicked last.
+ */
+export class LaunchesWhileQuitting {
+ private args: string[] | null = null;
+ private link: string | null = null;
+
+ constructor(private readonly findDeeplink: (argv: string[]) => string | undefined) {}
+
+ /** Record a launch, given its arguments without the executable. */
+ note(args: string[]): void {
+ const link = this.findDeeplink(args);
+ if (link) this.link = link;
+ this.args = args.filter((arg) => arg !== link);
+ }
+
+ /** The deep link to open after the restart, if any launch carried one. */
+ get deeplink(): string | null {
+ return this.link;
+ }
+
+ /** Arguments to start Console again with, or null when no launch arrived. */
+ relaunchArgs(): string[] | null {
+ if (this.args === null) return null;
+ return this.link ? [...this.args, this.link] : [...this.args];
+ }
+}
diff --git a/console/apps/switch-console-desktop/src/main/index.ts b/console/apps/switch-console-desktop/src/main/index.ts
index 6d54f6e4c..a588ebb6e 100644
--- a/console/apps/switch-console-desktop/src/main/index.ts
+++ b/console/apps/switch-console-desktop/src/main/index.ts
@@ -8,9 +8,10 @@ import { app, BrowserWindow, dialog, ipcMain, powerMonitor } from 'electron';
import dockIcon from '@/assets/images/switch-console/icon-dock.png?asset';
import { PRODUCT_NAME } from '@shared/app-identity';
import { registerRPCRouter } from '@shared/lib/ipc/rpc';
-import { flushPendingDeeplink, setupDeeplinks } from './app/deeplinks';
+import { deeplinkFromArgv, flushPendingDeeplink, setupDeeplinks } from './app/deeplinks';
import { setupApplicationMenu } from './app/menu';
import { APP_ORIGIN, registerAppScheme, setupAppProtocol } from './app/protocol';
+import { LaunchesWhileQuitting } from './app/relaunch-on-quit';
import { createMainWindow, getMainWindow } from './app/window';
import { startAutoMigration, stopAutoMigration } from './core/agent-migration/agent-migration';
import { bridgeAgentEventsToRenderer } from './core/agents/agent-events-renderer-bridge';
@@ -84,7 +85,16 @@ registerProcessErrorLogging(log);
registerRendererLogHandler(ipcMain);
logAppStart();
-app.on('second-instance', () => {
+// A launch while Console is quitting finds no window to show, and one opened
+// now would close with the exit, so Console starts again once it has quit.
+let quitting = false;
+const launchesWhileQuitting = new LaunchesWhileQuitting(deeplinkFromArgv);
+
+app.on('second-instance', (_event, commandLine) => {
+ if (quitting) {
+ launchesWhileQuitting.note(commandLine.slice(1));
+ return;
+ }
const win = BrowserWindow.getAllWindows()[0];
if (win?.isMinimized()) win.restore();
win?.focus();
@@ -310,6 +320,12 @@ void app.whenReady().then(async () => {
app.on('before-quit', (event) => {
event.preventDefault();
+ if (quitting) return;
+ quitting = true;
+ // Installing an update quits through here too, and the installer starts the
+ // new version itself. Starting Console as well could run the old executable
+ // while it is being replaced.
+ const updaterRestarts = updateService.getState().status === 'installing';
logAppExit('before-quit');
controlService.dispose();
stopAutoMigration();
@@ -338,6 +354,16 @@ app.on('before-quit', (event) => {
await locationManager.dispose().catch((e) => {
log.error('Failed to shutdown location manager:', e);
});
+ const relaunchArgs = launchesWhileQuitting.relaunchArgs();
+ if (relaunchArgs && !updaterRestarts) {
+ app.relaunch({ args: relaunchArgs });
+ } else if (relaunchArgs && launchesWhileQuitting.deeplink) {
+ // Nothing carries a deep link across a restart, so the updated Console
+ // starts without it.
+ log.warn('Dropped a deep link that arrived while Console quit to install an update', {
+ url: launchesWhileQuitting.deeplink,
+ });
+ }
app.exit(0);
})();
});