···422422// ── Init ────────────────────────────────────────────────────
423423424424async function init() {
425425- // Create anchor window immediately — doesn't need native messaging
426426- try {
427427- await ensureAnchorWindow();
428428- } catch (e) {
429429- console.error("Failed to create anchor window:", e);
430430- }
431431-432432- // Connect to native host for workspace integration
425425+ // Connect to native host first — we need it to save existing tabs
433426 connectNative();
434427 await new Promise((r) => setTimeout(r, 500));
435428···437430 lastWorkspace = await getCurrentWorkspace();
438431 } catch (e) {
439432 console.warn("Failed to get current workspace:", e);
433433+ }
434434+435435+ const hadExistingWindows = (await browser.windows.getAll()).length > 0;
436436+437437+ // Save all existing tabs before creating the anchor window
438438+ // (ensureAnchorWindow closes all windows, so save first)
439439+ if (hadExistingWindows) {
440440+ try {
441441+ await saveAllTabs();
442442+ } catch (e) {
443443+ console.error("Failed to save existing tabs before anchor creation:", e);
444444+ }
445445+ }
446446+447447+ // Create anchor window — closes existing windows via the for_window rule
448448+ try {
449449+ await ensureAnchorWindow();
450450+ } catch (e) {
451451+ console.error("Failed to create anchor window:", e);
440452 }
441453442454 try {
···448460 // Mark init complete — workspace changes from now on will restore tabs
449461 initialized = true;
450462451451- // Restore tabs for the initial workspace if it has saved tabs
463463+ // Restore tabs for the initial workspace if it has saved tabs from a previous session
452464 if (lastWorkspace) {
453465 const withFirefox = await getWorkspacesWithFirefox();
454466 if (!withFirefox.has(lastWorkspace)) {