Skip to content

Commit 658b676

Browse files
SarthakWadeyashranaway
authored andcommitted
fix(sdk): preserve platform launch defaults
1 parent 55a87d9 commit 658b676

4 files changed

Lines changed: 26 additions & 16 deletions

File tree

packages/headless-npm/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ that trust marker when sending page content to an agent or another system.
5050
## Supervised host
5151

5252
Use `launch()` when this process must own a new host. It invokes the installed
53-
CLI with `headless start --background --supervised`, keeps the ownership pipe
54-
open, verifies that the startup response and socket report the same host PID,
55-
and reaps only that launcher during disposal. It fails rather than claiming an
56-
already-running shared host.
53+
CLI with `headless start --supervised`, keeps the ownership pipe open, verifies
54+
that the startup response and socket report the same host PID, and reaps only
55+
that launcher during disposal. Omit `presentation` to preserve the platform
56+
default, or explicitly select `background` or `foreground` on macOS. Launch
57+
fails rather than claiming an already-running shared host.
5758

5859
```ts
5960
import { launch } from "@lockintime/headless";

packages/headless-npm/src/generated.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type Untrusted<T> = Readonly<{ readonly untrustedContent: true; readonly
77
export const PROTOCOL_VERSION = "0.5" as const;
88
export const PROTOCOL_SCHEMA_VERSION = 1 as const;
99
export const MAXIMUM_MESSAGE_BYTES = 1048576 as const;
10-
export const PROTOCOL_SCHEMA_SHA256 = "c199f18185cfa05b5c16c9140e48e1f588c61188b5f2ea6fa58eea2ddc57dcbf" as const;
10+
export const PROTOCOL_SCHEMA_SHA256 = "882634187c7ef02ec4ed51fff0e747114eadeff308c10bb9b3274b3630fad11d" as const;
1111
export const PROTOCOL_FIXTURES_SHA256 = "0b51ffaa2d3e3aaf0c32adcfeb02c180dcbe44face0d49e1c332b69f403ae062" as const;
1212
export const RESPONSE_ADDITIONAL_PROPERTIES = true as const;
1313
export const MAXIMUM_COMMAND_TIMEOUT_MS = 125000 as const;
@@ -22,7 +22,6 @@ export const LOCAL_LIFECYCLE = {
2222
"launch": {
2323
"argv": [
2424
"start",
25-
"--background",
2625
"--supervised"
2726
],
2827
"command": "start",

packages/headless-npm/src/lifecycle.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,19 +337,23 @@ export async function launch(options: LaunchOptions = {}): Promise<HeadlessHost>
337337
: AbortSignal.any([options.signal, deadlineSignal]);
338338
const socketPath = options.socketPath ?? defaultSocketPath(options.environment ?? process.env);
339339
validateSocketLocation(socketPath);
340-
const presentation = options.presentation ?? "background";
341-
if (!(LAUNCH_PRESENTATIONS as readonly string[]).includes(presentation)) {
340+
const presentation = options.presentation;
341+
if (presentation !== undefined
342+
&& !(LAUNCH_PRESENTATIONS as readonly string[]).includes(presentation)) {
342343
throw new ValidationError(`presentation must be one of ${LAUNCH_PRESENTATIONS.join(", ")}`);
343344
}
344345
const presentationFlags = new Set(LAUNCH_PRESENTATIONS.map((value) => `--${value}`));
345346
const generatedPresentationFlags = LOCAL_LIFECYCLE.launch.argv
346347
.filter((argument) => presentationFlags.has(argument));
347-
if (generatedPresentationFlags.length !== 1) {
348+
if (generatedPresentationFlags.length !== 0) {
348349
throw new ValidationError("generated launch argv has an invalid presentation flag");
349350
}
350-
const argumentsList: string[] = LOCAL_LIFECYCLE.launch.argv.map((argument) => (
351-
presentationFlags.has(argument) ? `--${presentation}` : argument
352-
));
351+
const argumentsList: string[] = [...LOCAL_LIFECYCLE.launch.argv];
352+
const supervisedIndex = argumentsList.indexOf("--supervised");
353+
if (supervisedIndex < 0 || argumentsList.lastIndexOf("--supervised") !== supervisedIndex) {
354+
throw new ValidationError("generated launch argv has an invalid supervised flag");
355+
}
356+
if (presentation !== undefined) argumentsList.splice(supervisedIndex, 0, `--${presentation}`);
353357
const allowDefinition = LOCAL_LIFECYCLE.launch.options.find((option) => option.name === "allow");
354358
const allow = options.allow ?? [];
355359
if (!allowDefinition || allow.length > allowDefinition.maximumItems) {

packages/headless-npm/test/lifecycle.test.mjs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,15 @@ import { chmodSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
4646
import { createServer } from "node:net";
4747
import { dirname } from "node:path";
4848
49-
const expectedPresentation = process.env.HEADLESS_TEST_EXPECT_PRESENTATION ?? "background";
50-
const expected = ["start", "--" + expectedPresentation, "--supervised"];
51-
if (JSON.stringify(process.argv.slice(2, 5)) !== JSON.stringify(expected)) process.exit(64);
49+
const expectedPresentation = process.env.HEADLESS_TEST_EXPECT_PRESENTATION;
50+
const expected = [
51+
"start",
52+
...(expectedPresentation ? ["--" + expectedPresentation] : []),
53+
"--supervised",
54+
];
55+
if (JSON.stringify(process.argv.slice(2, 2 + expected.length)) !== JSON.stringify(expected)) {
56+
process.exit(64);
57+
}
5258
const mode = process.env.HEADLESS_TEST_MODE ?? "owned";
5359
const socketPath = process.env.HEADLESS_SOCKET;
5460
const commands = JSON.parse(process.env.HEADLESS_TEST_COMMANDS);
@@ -158,7 +164,7 @@ test("supervised launch uses generated argv and owns only the matching host", as
158164
socketPath,
159165
environment: launchEnvironment(),
160166
});
161-
assert.deepEqual(LOCAL_LIFECYCLE.launch.argv, ["start", "--background", "--supervised"]);
167+
assert.deepEqual(LOCAL_LIFECYCLE.launch.argv, ["start", "--supervised"]);
162168
assert.equal(host.client.hostStatus.pid > 0, true);
163169
assert.deepEqual(
164170
{ SIGINT: process.listenerCount("SIGINT"), SIGTERM: process.listenerCount("SIGTERM") },

0 commit comments

Comments
 (0)