Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3792dcb889 | ||
|
|
1218eff099 | ||
|
|
72c3478719 | ||
|
|
9b3394c1d6 | ||
|
|
c5472479b6 |
No files matched your search
@@ -31,7 +31,8 @@ import {
|
||||
resolveCodexModelForAccount,
|
||||
type CodexAccountSnapshot,
|
||||
} from "./provider/codexAccount";
|
||||
import { buildCodexInitializeParams, killCodexChildProcess } from "./provider/codexAppServer";
|
||||
import { buildCodexInitializeParams } from "./provider/codexAppServer";
|
||||
import { killChildProcessTree } from "./process/killTree";
|
||||
|
||||
export { buildCodexInitializeParams } from "./provider/codexAppServer";
|
||||
export { readCodexAccountSnapshot, resolveCodexModelForAccount } from "./provider/codexAccount";
|
||||
@@ -306,15 +307,6 @@ function mapCodexRuntimeMode(runtimeMode: RuntimeMode): {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* On Windows with `shell: true`, `child.kill()` only terminates the `cmd.exe`
|
||||
* wrapper, leaving the actual command running. Use `taskkill /T` to kill the
|
||||
* entire process tree instead.
|
||||
*/
|
||||
function killChildTree(child: ChildProcessWithoutNullStreams): void {
|
||||
killCodexChildProcess(child);
|
||||
}
|
||||
|
||||
export function normalizeCodexModelSlug(
|
||||
model: string | undefined | null,
|
||||
preferredId?: string,
|
||||
@@ -908,7 +900,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
|
||||
context.output.close();
|
||||
|
||||
if (!context.child.killed) {
|
||||
killChildTree(context.child);
|
||||
killChildProcessTree(context.child);
|
||||
}
|
||||
|
||||
this.updateSession(context, {
|
||||
|
||||
@@ -28,6 +28,7 @@ interface SpawnCall {
|
||||
readonly args: ReadonlyArray<string>;
|
||||
readonly detached?: boolean | undefined;
|
||||
readonly shell?: boolean | string | undefined;
|
||||
readonly windowsVerbatimArguments?: boolean | undefined;
|
||||
readonly stdin?: unknown;
|
||||
readonly stdout?: unknown;
|
||||
readonly stderr?: unknown;
|
||||
@@ -66,6 +67,7 @@ function spawnerLayer(
|
||||
readonly options: {
|
||||
readonly detached?: boolean;
|
||||
readonly shell?: boolean | string;
|
||||
readonly windowsVerbatimArguments?: boolean;
|
||||
readonly stdin?: unknown;
|
||||
readonly stdout?: unknown;
|
||||
readonly stderr?: unknown;
|
||||
@@ -77,6 +79,9 @@ function spawnerLayer(
|
||||
args: [...standardCommand.args],
|
||||
detached: standardCommand.options.detached,
|
||||
shell: standardCommand.options.shell,
|
||||
...(standardCommand.options.windowsVerbatimArguments !== undefined
|
||||
? { windowsVerbatimArguments: standardCommand.options.windowsVerbatimArguments }
|
||||
: {}),
|
||||
stdin: standardCommand.options.stdin,
|
||||
stdout: standardCommand.options.stdout,
|
||||
stderr: standardCommand.options.stderr,
|
||||
@@ -152,6 +157,9 @@ function spawnHarness(
|
||||
args: [...input.args],
|
||||
detached: input.detached,
|
||||
shell: input.shell,
|
||||
...(input.windowsVerbatimArguments !== undefined
|
||||
? { windowsVerbatimArguments: input.windowsVerbatimArguments }
|
||||
: {}),
|
||||
stdin: input.stdin,
|
||||
stdout: input.stdout,
|
||||
stderr: input.stderr,
|
||||
@@ -336,12 +344,23 @@ it.effect("openInEditor launches Windows batch shims through cmd.exe without she
|
||||
});
|
||||
|
||||
assert.equal(calls.length, 1);
|
||||
assert.equal(calls[0]?.command, "cmd.exe");
|
||||
assert.deepEqual(calls[0]?.args.slice(0, 4), ["/d", "/v:off", "/s", "/c"]);
|
||||
assert.equal(calls[0]?.args[4]?.toLowerCase().includes(`${dir}/code.cmd`.toLowerCase()), true);
|
||||
assert.equal(calls[0]?.args[4]?.includes('"--goto"'), true);
|
||||
assert.equal(calls[0]?.args[4]?.includes("100%% real"), true);
|
||||
assert.equal(calls[0]?.command.toLowerCase().includes("powershell"), true);
|
||||
assert.deepEqual(calls[0]?.args.slice(0, 5), [
|
||||
"-NoProfile",
|
||||
"-NonInteractive",
|
||||
"-ExecutionPolicy",
|
||||
"Bypass",
|
||||
"-EncodedCommand",
|
||||
]);
|
||||
const encoded = calls[0]?.args[5];
|
||||
assert.isString(encoded);
|
||||
const decoded = decodePowerShellCommand(encoded!);
|
||||
assert.equal(decoded.includes("Start-Process"), true);
|
||||
assert.equal(decoded.toLowerCase().includes(`${dir}/code.cmd`.toLowerCase()), true);
|
||||
assert.equal(decoded.includes("--goto"), true);
|
||||
assert.equal(decoded.includes("100% real"), true);
|
||||
assert.equal(calls[0]?.detached, true);
|
||||
assert.equal(calls[0]?.windowsVerbatimArguments, undefined);
|
||||
assert.equal(calls[0]?.shell, false);
|
||||
assert.equal(calls[0]?.stdin, "ignore");
|
||||
assert.equal(calls[0]?.stdout, "ignore");
|
||||
|
||||
@@ -37,6 +37,7 @@ export interface DetachedSpawnInput {
|
||||
readonly args: ReadonlyArray<string>;
|
||||
readonly detached?: boolean;
|
||||
readonly shell?: boolean;
|
||||
readonly windowsVerbatimArguments?: boolean;
|
||||
readonly stdin?: "ignore";
|
||||
readonly stdout?: "ignore";
|
||||
readonly stderr?: "ignore";
|
||||
@@ -206,7 +207,7 @@ function normalizeAppCandidates(
|
||||
}
|
||||
}
|
||||
|
||||
return candidates;
|
||||
return candidates.length > 0 ? candidates : [undefined];
|
||||
}
|
||||
|
||||
function isUriLikeTarget(target: string): boolean {
|
||||
@@ -303,6 +304,23 @@ function makePowerShellPlan(input: OpenExternalInput, powerShellCommand: string)
|
||||
);
|
||||
}
|
||||
|
||||
function makePowerShellStartProcessArgs(
|
||||
commandPath: string,
|
||||
args: ReadonlyArray<string>,
|
||||
): ReadonlyArray<string> {
|
||||
const argumentList =
|
||||
args.length > 0 ? ` -ArgumentList ${args.map(quotePowerShellValue).join(", ")}` : "";
|
||||
const command = `Start-Process -FilePath ${quotePowerShellValue(commandPath)}${argumentList} -WindowStyle Hidden`;
|
||||
return [
|
||||
"-NoProfile",
|
||||
"-NonInteractive",
|
||||
"-ExecutionPolicy",
|
||||
"Bypass",
|
||||
"-EncodedCommand",
|
||||
encodePowerShellCommand(command),
|
||||
];
|
||||
}
|
||||
|
||||
function makeLinuxDefaultPlan(input: OpenExternalInput): LaunchPlan {
|
||||
const wait = input.wait ?? false;
|
||||
return makeLaunchPlan("xdg-open", [input.target], {
|
||||
@@ -382,7 +400,23 @@ function resolveSpawnInput(
|
||||
runtime: LaunchRuntime,
|
||||
plan: LaunchPlan,
|
||||
resolvedCommand: ResolvedCommand,
|
||||
startProcessLauncher: string | undefined,
|
||||
): DetachedSpawnInput {
|
||||
if (plan.detached && resolvedCommand.usesCmdWrapper && startProcessLauncher !== undefined) {
|
||||
return {
|
||||
command: startProcessLauncher,
|
||||
args: makePowerShellStartProcessArgs(resolvedCommand.path, plan.args),
|
||||
...(plan.detached !== undefined ? { detached: plan.detached } : {}),
|
||||
...(plan.shell !== undefined ? { shell: plan.shell } : {}),
|
||||
...(plan.stdio === "ignore"
|
||||
? {
|
||||
stdin: "ignore" as const,
|
||||
stdout: "ignore" as const,
|
||||
stderr: "ignore" as const,
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
return {
|
||||
command: resolvedCommand.usesCmdWrapper
|
||||
? resolveWindowsCommandShell(runtime.env)
|
||||
@@ -392,6 +426,7 @@ function resolveSpawnInput(
|
||||
: [...plan.args],
|
||||
...(plan.detached !== undefined ? { detached: plan.detached } : {}),
|
||||
...(plan.shell !== undefined ? { shell: plan.shell } : {}),
|
||||
...(resolvedCommand.usesCmdWrapper ? { windowsVerbatimArguments: true } : {}),
|
||||
...(plan.stdio === "ignore"
|
||||
? {
|
||||
stdin: "ignore" as const,
|
||||
@@ -479,6 +514,8 @@ function toLaunchAttemptFailure(error: LaunchAttemptError): LaunchAttemptFailure
|
||||
* not expose `unref()`, which means it cannot provide true fire-and-forget
|
||||
* behavior for editor / file-manager launches.
|
||||
*/
|
||||
const DETACHED_SPAWN_GRACE_MS = 500;
|
||||
|
||||
function defaultSpawnDetached(
|
||||
input: DetachedSpawnInput,
|
||||
context: LaunchContext,
|
||||
@@ -488,6 +525,9 @@ function defaultSpawnDetached(
|
||||
spawnNodeChildProcess(input.command, [...input.args], {
|
||||
...(input.detached !== undefined ? { detached: input.detached } : {}),
|
||||
...(input.shell !== undefined ? { shell: input.shell } : {}),
|
||||
...(input.windowsVerbatimArguments !== undefined
|
||||
? { windowsVerbatimArguments: input.windowsVerbatimArguments }
|
||||
: {}),
|
||||
...(input.stdin === "ignore" && input.stdout === "ignore" && input.stderr === "ignore"
|
||||
? { stdio: "ignore" as const }
|
||||
: {}),
|
||||
@@ -496,22 +536,50 @@ function defaultSpawnDetached(
|
||||
}).pipe(
|
||||
Effect.flatMap((childProcess) =>
|
||||
Effect.callback<void, DesktopLauncherSpawnError>((resume) => {
|
||||
let graceTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
const onEarlyExit = (code: number | null) => {
|
||||
if (graceTimer !== undefined) clearTimeout(graceTimer);
|
||||
if (code !== null && code !== 0) {
|
||||
resume(
|
||||
Effect.fail(
|
||||
makeSpawnError(
|
||||
context,
|
||||
input.command,
|
||||
input.args,
|
||||
new Error(`Process exited immediately with code ${code}`),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
childProcess.unref();
|
||||
resume(Effect.void);
|
||||
}
|
||||
};
|
||||
|
||||
const onError = (error: Error) => {
|
||||
childProcess.off("spawn", onSpawn);
|
||||
resume(Effect.fail(makeSpawnError(context, input.command, input.args, error)));
|
||||
};
|
||||
|
||||
const onSpawn = () => {
|
||||
childProcess.off("error", onError);
|
||||
childProcess.unref();
|
||||
resume(Effect.void);
|
||||
childProcess.once("exit", onEarlyExit);
|
||||
graceTimer = setTimeout(() => {
|
||||
childProcess.off("exit", onEarlyExit);
|
||||
childProcess.unref();
|
||||
resume(Effect.void);
|
||||
}, DETACHED_SPAWN_GRACE_MS);
|
||||
};
|
||||
|
||||
childProcess.once("error", onError);
|
||||
childProcess.once("spawn", onSpawn);
|
||||
|
||||
return Effect.sync(() => {
|
||||
if (graceTimer !== undefined) clearTimeout(graceTimer);
|
||||
childProcess.off("error", onError);
|
||||
childProcess.off("spawn", onSpawn);
|
||||
childProcess.off("exit", onEarlyExit);
|
||||
});
|
||||
}),
|
||||
),
|
||||
@@ -615,6 +683,16 @@ export const make = Effect.fn("makeDesktopLauncher")(function* (
|
||||
const commandAvailable = (command: string) =>
|
||||
resolveCommand(command).pipe(Effect.map(Option.isSome));
|
||||
|
||||
const startProcessLauncher = yield* Effect.gen(function* () {
|
||||
for (const candidate of runtime.powerShellCandidates) {
|
||||
const resolved = yield* resolveCommand(candidate);
|
||||
if (Option.isSome(resolved)) {
|
||||
return resolved.value.path;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
const fileManagerAvailable = Effect.gen(function* () {
|
||||
const candidates =
|
||||
runtime.platform === "darwin"
|
||||
@@ -665,12 +743,15 @@ export const make = Effect.fn("makeDesktopLauncher")(function* (
|
||||
resolvedCommand: ResolvedCommand,
|
||||
context: LaunchContext,
|
||||
) => {
|
||||
const input = resolveSpawnInput(runtime, plan, resolvedCommand);
|
||||
const input = resolveSpawnInput(runtime, plan, resolvedCommand, startProcessLauncher);
|
||||
return spawner
|
||||
.spawn(
|
||||
ChildProcess.make(input.command, [...input.args], {
|
||||
detached: plan.detached,
|
||||
shell: plan.shell,
|
||||
...(input.windowsVerbatimArguments !== undefined
|
||||
? { windowsVerbatimArguments: input.windowsVerbatimArguments }
|
||||
: {}),
|
||||
...(plan.stdio === "ignore"
|
||||
? {
|
||||
stdin: "ignore",
|
||||
@@ -713,7 +794,12 @@ export const make = Effect.fn("makeDesktopLauncher")(function* (
|
||||
Scope.make("sequential"),
|
||||
(scope) =>
|
||||
Effect.gen(function* () {
|
||||
const spawnInput = resolveSpawnInput(runtime, plan, resolvedCommand);
|
||||
const spawnInput = resolveSpawnInput(
|
||||
runtime,
|
||||
plan,
|
||||
resolvedCommand,
|
||||
startProcessLauncher,
|
||||
);
|
||||
const handle = yield* spawnPlan(plan, resolvedCommand, context).pipe(
|
||||
Scope.provide(scope),
|
||||
);
|
||||
@@ -726,7 +812,8 @@ export const make = Effect.fn("makeDesktopLauncher")(function* (
|
||||
plan: LaunchPlan,
|
||||
resolvedCommand: ResolvedCommand,
|
||||
context: LaunchContext,
|
||||
) => spawnDetached(resolveSpawnInput(runtime, plan, resolvedCommand), context);
|
||||
) =>
|
||||
spawnDetached(resolveSpawnInput(runtime, plan, resolvedCommand, startProcessLauncher), context);
|
||||
|
||||
const runPlan = (plan: LaunchPlan, resolvedCommand: ResolvedCommand, context: LaunchContext) =>
|
||||
plan.wait
|
||||
|
||||
@@ -127,16 +127,6 @@ export class DesktopLauncherLaunchAttemptsExhaustedError extends Schema.TaggedEr
|
||||
}
|
||||
}
|
||||
|
||||
export const DesktopLauncherError = Schema.Union([
|
||||
DesktopLauncherCommandNotFoundError,
|
||||
DesktopLauncherDiscoveryError,
|
||||
DesktopLauncherLaunchAttemptsExhaustedError,
|
||||
DesktopLauncherNonZeroExitError,
|
||||
DesktopLauncherSpawnError,
|
||||
DesktopLauncherUnknownEditorError,
|
||||
DesktopLauncherValidationError,
|
||||
]);
|
||||
|
||||
export interface OpenInEditorInput {
|
||||
readonly cwd: string;
|
||||
readonly editor: EditorId;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
|
||||
import { spawn } from "node:child_process";
|
||||
import readline from "node:readline";
|
||||
import { readCodexAccountSnapshot, type CodexAccountSnapshot } from "./codexAccount";
|
||||
import { killChildProcessTree } from "../process/killTree";
|
||||
@@ -28,10 +28,6 @@ export function buildCodexInitializeParams() {
|
||||
} as const;
|
||||
}
|
||||
|
||||
export function killCodexChildProcess(child: ChildProcessWithoutNullStreams): void {
|
||||
killChildProcessTree(child);
|
||||
}
|
||||
|
||||
export async function probeCodexAccount(input: {
|
||||
readonly binaryPath: string;
|
||||
readonly homePath?: string;
|
||||
@@ -55,7 +51,7 @@ export async function probeCodexAccount(input: {
|
||||
output.close();
|
||||
child.removeAllListeners();
|
||||
if (!child.killed) {
|
||||
killCodexChildProcess(child);
|
||||
killChildProcessTree(child);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -214,8 +214,8 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
logWebSocketEvents,
|
||||
autoBootstrapProjectFromCwd,
|
||||
} = serverConfig;
|
||||
const open = yield* DesktopLauncher;
|
||||
const availableEditors = yield* open.getAvailableEditors.pipe(
|
||||
const desktopLauncher = yield* DesktopLauncher;
|
||||
const availableEditors = yield* desktopLauncher.getAvailableEditors.pipe(
|
||||
Effect.mapError((cause) => new ServerLifecycleError({ operation: "availableEditors", cause })),
|
||||
);
|
||||
|
||||
@@ -747,7 +747,7 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
|
||||
|
||||
case WS_METHODS.shellOpenInEditor: {
|
||||
const body = stripRequestTag(request.body);
|
||||
return yield* open.openInEditor(body);
|
||||
return yield* desktopLauncher.openInEditor(body);
|
||||
}
|
||||
|
||||
case WS_METHODS.gitStatus: {
|
||||
|
||||
Reference in new issue
Block a user