Skip to content

Commit f2151b6

Browse files
donjor-agent[bot]rsi-pr-agent
authored andcommitted
fix(desktop): client-only boots drain restore and launch intent
## What Changed - **Drain module:** startup restore and launch intents move to `DesktopStartupDrain.fork.ts`. - Bounded queue: capacity 8, drops newest with a log line, never blocks. - Renderer readiness and backend readiness share one drain. - The drain opens the restore first, then each intent in arrival order, once. - It re-polls until empty, so an intent queued mid-drain still opens. - One winner claims the drain; later callers await it. - A failed open is a typed `ElectronWindowCreateError`. - `markNotReady` re-arms it after a backend restart. - **Second-instance buffer:** `DesktopSecondInstanceBuffer.fork.ts` listens from single-instance lock acquisition. - It holds launches that arrive before `DesktopClerk.configure` registers the real listener. - It replays them after the first launch's argv, in order. - **Client-only snapshot:** client-only bootstrap now runs `snapShot.initialize`. ## Why - **Client-only readiness:** client-only desktop never called `handleBackendReady`, so restore and launch intents only drained in managed mode. - An update relaunch dropped every window. - A `--project` cold start opened the hub. - **Early second launch:** a second launch arriving within about 0.1s of the first was dropped. - Electron emitted `second-instance` before any app listener existed. ## Close conditions - [x] Relaunch: client-only update restores windows. Live client-only Electron relaunch with a recorded `window-session.json` reopened exactly `zmux` and `solar`, with no hub. Fork test "opens the restore before an intent queued during it". - [x] Cold start: client-only `--project` opens it. Live: `--project <solar>` cold start produced one page, the solar project route, with no hub. - [x] Order: queued intents drain once, in order. Live: second-instance launches at 0.05s ×3, 0.1s ×3 and 0.4s ×2 each opened both windows, A then B, with no duplicate; before the buffer, 0.05–0.1s dropped B in 8 of 8 trials. Fork tests: straggler during an open, followers admitted mid-open, buffer replay once. - [x] Bound: the queue is bounded. `STARTUP_INTENT_QUEUE_CAPACITY = 8`; the second-instance buffer shares the cap (test "holds at most the startup queue capacity"). - [x] Tests: both backend modes covered. Both readiness paths call the same drain; drain and buffer fork tests are mode-agnostic, and live proof ran in client-only mode. Live proof ran at `ccde0e21a7`, off-screen on an agent workspace. ## Checklist - [x] This PR is small and focused - [x] I explained what changed and why - [ ] I included before/after screenshots for any UI changes - [ ] I included a video for animation/interaction changes No UI changes: startup window-open behavior only; no screenshots. Closes #1340 ## Fork trailers Squashes: - d9d39f7 fix(desktop): client-only boots drain restore and launch intent - faa53bd refactor(multi-window): createWindow seams become marked insertions (#1811) (#1811) Fork-Domain: multi-window Fork-Tier: core
1 parent cfa2968 commit f2151b6

5 files changed

Lines changed: 490 additions & 0 deletions

File tree

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as DesktopAppIdentity from "./DesktopAppIdentity.ts";
2020
import * as DesktopBackendMode from "./DesktopBackendMode.ts";
2121
import * as DesktopClerk from "./DesktopClerk.ts";
2222
import { desktopLaunchArguments } from "./DesktopLaunchArguments.fork.ts"; // fork-hook: multi-window/launch-arguments
23+
import { desktopSecondInstanceBuffer } from "./DesktopSecondInstanceBuffer.fork.ts"; // fork-hook: multi-window/second-instance-buffer
2324
import * as DesktopApplicationMenu from "../window/DesktopApplicationMenu.ts";
2425
import * as DesktopWindow from "../window/DesktopWindow.ts";
2526
import * as PreviewPasskeys from "../preview/Passkeys.ts";
@@ -245,6 +246,7 @@ const bootstrap = Effect.gen(function* () {
245246

246247
yield* installDesktopIpcHandlers();
247248
yield* logBootstrapInfo("bootstrap ipc handlers registered");
249+
yield* snapShot.initialize; // fork-hook: multi-window/client-only-snapshot-init
248250
if (!(yield* Ref.get(state.quitting))) {
249251
yield* appActivation.start.pipe(
250252
Effect.tap(() => logBootstrapInfo("desktop app control socket ready")),
@@ -425,6 +427,7 @@ const startup = Effect.gen(function* () {
425427
yield* desktopLaunchArguments.install((argv) => desktopWindow.openArguments(argv)); // fork-hook: multi-window/launch-arguments
426428
yield* clerk.configure;
427429
yield* desktopWindow.openArguments(process.argv); // fork-hook: multi-window/open-arguments
430+
yield* desktopSecondInstanceBuffer.flush((argv) => desktopWindow.openArguments(argv)); // fork-hook: multi-window/second-instance-buffer
428431

429432
yield* electronApp.whenReady.pipe(
430433
Effect.withSpan("desktop.electron.whenReady"),
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Ref from "effect/Ref";
4+
import { EnvironmentId, ProjectId } from "@t3tools/contracts";
5+
6+
import type * as ElectronApp from "../electron/ElectronApp.ts";
7+
import {
8+
makeStartupDrain,
9+
STARTUP_INTENT_QUEUE_CAPACITY,
10+
type StartupDrainServices,
11+
type WindowOpeners,
12+
} from "../window/DesktopStartupDrain.fork.ts";
13+
import { projectWindowIdentity, type WindowIdentity } from "../window/WindowIdentity.ts";
14+
import { makeSecondInstanceBuffer } from "./DesktopSecondInstanceBuffer.fork.ts";
15+
16+
const fakeWindow = {} as Electron.BrowserWindow;
17+
18+
const noopServices: StartupDrainServices = {
19+
hyprlandPlacement: {
20+
isAvailable: false,
21+
claim: () => Effect.void,
22+
forget: () => Effect.void,
23+
workspaceOf: () => Effect.succeedNone,
24+
stageWorkspaceRule: () => Effect.succeed(false),
25+
clearWorkspaceRule: () => Effect.void,
26+
moveToWorkspace: () => Effect.void,
27+
},
28+
windowSession: { capture: () => Effect.void, consume: Effect.succeed([]) },
29+
};
30+
31+
const makeFakeApp = () => {
32+
const listeners: Array<(...args: ReadonlyArray<unknown>) => void> = [];
33+
const electronApp = {
34+
on: (eventName: string, listener: (...args: ReadonlyArray<unknown>) => void) =>
35+
Effect.sync(() => {
36+
if (eventName === "second-instance") listeners.push(listener);
37+
}),
38+
} as unknown as ElectronApp.ElectronApp["Service"];
39+
const emit = (argv: readonly string[]) => {
40+
for (const listener of listeners) listener({}, argv);
41+
};
42+
return { electronApp, emit } as const;
43+
};
44+
45+
// argv is ["t3code", <name>]; the resolver maps the name to a project window.
46+
const identityOf = (argv: readonly string[]): WindowIdentity | null =>
47+
argv[1] === undefined
48+
? null
49+
: projectWindowIdentity(
50+
EnvironmentId.make(`environment-${argv[1]}`),
51+
ProjectId.make(`project-${argv[1]}`),
52+
);
53+
54+
describe("DesktopSecondInstanceBuffer", () => {
55+
it.effect("opens a launch emitted before configure after the first intent, once", () =>
56+
Effect.gen(function* () {
57+
const drain = yield* makeStartupDrain;
58+
const buffer = makeSecondInstanceBuffer();
59+
const app = makeFakeApp();
60+
const opened = yield* Ref.make<ReadonlyArray<string>>([]);
61+
const record = (identity: WindowIdentity) =>
62+
Ref.update(opened, (keys) => [
63+
...keys,
64+
identity.kind === "hub" ? "hub" : identity.ref.projectId,
65+
]).pipe(Effect.as(fakeWindow));
66+
const openers: WindowOpeners = {
67+
ensureIdentity: () => record,
68+
revealOrCreateIdentity: () => record,
69+
createMainIfBackendReady: () => Effect.void,
70+
};
71+
const stage = (argv: readonly string[]) =>
72+
drain.stageArguments(noopServices, identityOf, argv).pipe(Effect.asVoid);
73+
74+
yield* buffer.listen(app.electronApp);
75+
// Emitted in the gap before configure registers the real listener.
76+
app.emit(["t3code", "second"]);
77+
yield* buffer.close;
78+
// After close the real listener owns the event; the buffer must not
79+
// replay it too.
80+
app.emit(["t3code", "late"]);
81+
yield* stage(["t3code", "first"]);
82+
yield* buffer.flush(stage);
83+
yield* buffer.flush(stage);
84+
yield* drain.drain(noopServices, () => openers);
85+
assert.deepEqual(yield* Ref.get(opened), ["project-first", "project-second"]);
86+
}),
87+
);
88+
89+
it.effect("holds at most the startup queue capacity", () =>
90+
Effect.gen(function* () {
91+
const buffer = makeSecondInstanceBuffer();
92+
const app = makeFakeApp();
93+
yield* buffer.listen(app.electronApp);
94+
for (let index = 0; index < STARTUP_INTENT_QUEUE_CAPACITY + 3; index += 1) {
95+
app.emit(["t3code", `launch-${index}`]);
96+
}
97+
const replayed: Array<string> = [];
98+
yield* buffer.flush((argv) => Effect.sync(() => void replayed.push(argv[1] ?? "")));
99+
assert.equal(replayed.length, STARTUP_INTENT_QUEUE_CAPACITY);
100+
assert.equal(replayed[0], "launch-0");
101+
}),
102+
);
103+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as Cause from "effect/Cause";
2+
import * as Effect from "effect/Effect";
3+
import type * as Scope from "effect/Scope";
4+
5+
import type * as ElectronApp from "../electron/ElectronApp.ts";
6+
import { STARTUP_INTENT_QUEUE_CAPACITY } from "../window/DesktopStartupDrain.fork.ts";
7+
import { makeComponentLogger } from "./DesktopObservability.ts";
8+
9+
const { logWarning } = makeComponentLogger("desktop-clerk");
10+
11+
// Fork-only (RSI-Software/t3code-hyprws#1340): the Clerk bridge takes the
12+
// single-instance lock when it is created, but the app's own `second-instance`
13+
// listener registers later, in `DesktopClerk.configure`. Electron emits into
14+
// that gap with no app listener, dropping a second launch. This buffer
15+
// listens from lock acquisition, stops accepting once configure's listener is
16+
// about to take over, and replays what it held after the first launch's own
17+
// argv is staged, so the drain opens the launches in arrival order, once each.
18+
export const makeSecondInstanceBuffer = () => {
19+
const held: Array<readonly string[]> = [];
20+
let accepting = true;
21+
22+
/** Registers the buffering listener; call where the lock is acquired. */
23+
const listen = (
24+
electronApp: ElectronApp.ElectronApp["Service"],
25+
): Effect.Effect<void, never, Scope.Scope> =>
26+
electronApp.on("second-instance", (_event: unknown, argv: readonly string[]) => {
27+
if (!accepting) return;
28+
if (held.length >= STARTUP_INTENT_QUEUE_CAPACITY) return;
29+
held.push([...argv]);
30+
});
31+
32+
/** Stops buffering; call right before the real listener registers. */
33+
const close = Effect.sync(() => {
34+
accepting = false;
35+
});
36+
37+
/** Replays held launches in arrival order, exactly once. */
38+
const flush = <E>(openArguments: (argv: readonly string[]) => Effect.Effect<void, E>) =>
39+
Effect.gen(function* () {
40+
accepting = false;
41+
const pending = held.splice(0);
42+
for (const argv of pending) {
43+
yield* openArguments(argv).pipe(
44+
Effect.catchCause((cause) =>
45+
logWarning("failed to open buffered second-instance arguments", {
46+
argv: [...argv],
47+
error: Cause.pretty(cause),
48+
}),
49+
),
50+
);
51+
}
52+
});
53+
54+
return { listen, close, flush } as const;
55+
};
56+
57+
/** The desktop process's one buffer: Electron's lock is process-global too. */
58+
export const desktopSecondInstanceBuffer = makeSecondInstanceBuffer();

0 commit comments

Comments
 (0)