Skip to content

Commit fdb70cd

Browse files
committed
fix(tui): strip inspector flags from auth child
Punchcard-Session: ember-workshop-lantern-bs
1 parent 3622daa commit fdb70cd

4 files changed

Lines changed: 90 additions & 40 deletions

File tree

‎src/tui/tui-exec-argv.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export function filterTuiExecArgv(execArgv: readonly string[]): string[] {
2+
const filtered: string[] = [];
3+
for (let index = 0; index < execArgv.length; index += 1) {
4+
const arg = execArgv[index] ?? "";
5+
// Strip inspector flags so TUI-owned children cannot contend with or pause beneath
6+
// the parent debugger.
7+
if (
8+
arg === "--inspect" ||
9+
arg.startsWith("--inspect=") ||
10+
arg === "--inspect-brk" ||
11+
arg.startsWith("--inspect-brk=") ||
12+
arg === "--inspect-wait" ||
13+
arg.startsWith("--inspect-wait=")
14+
) {
15+
const next = execArgv[index + 1];
16+
if (!arg.includes("=") && typeof next === "string" && !next.startsWith("-")) {
17+
index += 1;
18+
}
19+
continue;
20+
}
21+
if (arg === "--inspect-port") {
22+
const next = execArgv[index + 1];
23+
if (typeof next === "string" && !next.startsWith("-")) {
24+
index += 1;
25+
}
26+
continue;
27+
}
28+
if (arg.startsWith("--inspect-port=")) {
29+
continue;
30+
}
31+
filtered.push(arg);
32+
}
33+
return filtered;
34+
}

‎src/tui/tui-launch.ts‎

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "node:path";
44
import { formatErrorMessage } from "../infra/errors.js";
55
import { attachChildProcessBridge } from "../process/child-process-bridge.js";
66
import { TUI_SETUP_AUTH_SOURCE_CONFIG, TUI_SETUP_AUTH_SOURCE_ENV } from "./setup-launch-env.js";
7+
import { filterTuiExecArgv } from "./tui-exec-argv.js";
78
import type { TuiOptions } from "./tui.js";
89

910
// Relaunch helper used when setup wants to hand control to an inherited-stdio TUI process.
@@ -19,40 +20,6 @@ function appendOption(args: string[], flag: string, value: string | number | und
1920
args.push(flag, String(value));
2021
}
2122

22-
function filterTuiExecArgv(execArgv: readonly string[]): string[] {
23-
const filtered: string[] = [];
24-
for (let index = 0; index < execArgv.length; index += 1) {
25-
const arg = execArgv[index] ?? "";
26-
// Strip inspector flags so a relaunched TUI does not fight the parent debug port.
27-
if (
28-
arg === "--inspect" ||
29-
arg.startsWith("--inspect=") ||
30-
arg === "--inspect-brk" ||
31-
arg.startsWith("--inspect-brk=") ||
32-
arg === "--inspect-wait" ||
33-
arg.startsWith("--inspect-wait=")
34-
) {
35-
const next = execArgv[index + 1];
36-
if (!arg.includes("=") && typeof next === "string" && !next.startsWith("-")) {
37-
index += 1;
38-
}
39-
continue;
40-
}
41-
if (arg === "--inspect-port") {
42-
const next = execArgv[index + 1];
43-
if (typeof next === "string" && !next.startsWith("-")) {
44-
index += 1;
45-
}
46-
continue;
47-
}
48-
if (arg.startsWith("--inspect-port=")) {
49-
continue;
50-
}
51-
filtered.push(arg);
52-
}
53-
return filtered;
54-
}
55-
5623
function buildCurrentCliEntryArgs(): string[] {
5724
const entry = process.argv[1]?.trim();
5825
if (!entry) {

‎src/tui/tui.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Covers core TUI state transitions and backend event rendering.
22
import { EventEmitter } from "node:events";
3+
import path from "node:path";
34
import { afterEach, describe, expect, it, vi } from "vitest";
45
import type { OpenClawConfig } from "../config/config.js";
56
import { MAX_TIMER_TIMEOUT_MS } from "../infra/parse-finite-number.js";
@@ -24,6 +25,7 @@ import {
2425
isTuiBusyActivityStatus,
2526
resolveLocalAuthSpawnInvocation,
2627
resolveTuiCtrlCAction,
28+
resolveTuiLocalAuthCliInvocation,
2729
resolveTuiShutdownHardExitMs,
2830
resolveTuiSessionKey,
2931
scheduleProcessExitAfterTuiReturn,
@@ -65,6 +67,44 @@ describe("resolveFinalAssistantText", () => {
6567
});
6668
});
6769

70+
describe("resolveTuiLocalAuthCliInvocation", () => {
71+
it("filters inspector flags while preserving the current CLI runtime context", () => {
72+
const originalArgv = [...process.argv];
73+
try {
74+
const cliEntry = path.resolve("openclaw.mjs");
75+
process.argv[1] = cliEntry;
76+
77+
expect(
78+
resolveTuiLocalAuthCliInvocation({
79+
provider: "test-provider",
80+
execArgv: [
81+
"--import",
82+
"/repo/node_modules/tsx/dist/loader.mjs",
83+
"--inspect-brk=0",
84+
"--trace-warnings",
85+
],
86+
}),
87+
).toStrictEqual({
88+
command: process.execPath,
89+
args: [
90+
"--import",
91+
"/repo/node_modules/tsx/dist/loader.mjs",
92+
"--trace-warnings",
93+
cliEntry,
94+
"models",
95+
"auth",
96+
"login",
97+
"--provider",
98+
"test-provider",
99+
],
100+
cwd: path.resolve("."),
101+
});
102+
} finally {
103+
process.argv = originalArgv;
104+
}
105+
});
106+
});
107+
68108
describe("tui slash commands", () => {
69109
it("treats /elev as an alias for /elevated", () => {
70110
expect(parseCommand("/elev on")).toEqual({ name: "elevated", args: "on" });

‎src/tui/tui.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { sanitizeAutocompleteProvider } from "./tui-autocomplete.js";
4242
import type { TuiBackend } from "./tui-backend.js";
4343
import { createCommandHandlers } from "./tui-command-handlers.js";
4444
import { createEventHandlers } from "./tui-event-handlers.js";
45+
import { filterTuiExecArgv } from "./tui-exec-argv.js";
4546
import {
4647
formatTuiErrorMessage,
4748
formatTuiFooter,
@@ -144,6 +145,19 @@ export function resolveLocalAuthSpawnInvocation(params: {
144145
};
145146
}
146147

148+
export function resolveTuiLocalAuthCliInvocation(params: {
149+
provider?: string;
150+
execArgv?: readonly string[];
151+
}) {
152+
const provider = params.provider?.trim();
153+
return resolveCurrentOpenClawCliInvocation(
154+
["models", "auth", "login", ...(provider ? ["--provider", provider] : [])],
155+
{
156+
execArgv: filterTuiExecArgv(params.execArgv ?? process.execArgv),
157+
},
158+
);
159+
}
160+
147161
export function resolveTuiSessionKey(params: {
148162
raw?: string;
149163
sessionScope: SessionScope;
@@ -1157,12 +1171,7 @@ export async function runTui(opts: RunTuiOptions): Promise<TuiResult> {
11571171
args = ["login"];
11581172
cwd = resolveUsableCwd();
11591173
} else {
1160-
const invocation = resolveCurrentOpenClawCliInvocation([
1161-
"models",
1162-
"auth",
1163-
"login",
1164-
...(provider ? ["--provider", provider] : []),
1165-
]);
1174+
const invocation = resolveTuiLocalAuthCliInvocation({ provider });
11661175
({ command, args, cwd } = invocation);
11671176
}
11681177

0 commit comments

Comments
 (0)