Skip to content

Commit eeec0d4

Browse files
authored
fix(cli): keep setup and agent help startup lazy (#133272)
* fix(cli): keep command actions out of help registration Defer onboarding rejection and agent finalization imports until actions execute. Split agent-exec input/config preparation and result projection into actual production owners while preserving helper bodies and test contracts. Production LOC stays neutral. * refactor(cli): complete agent exec owner migration
1 parent 9ae7a2f commit eeec0d4

13 files changed

Lines changed: 468 additions & 460 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2442,7 +2442,7 @@ src/cli/update-cli/update-command-config.ts 6
24422442
src/cli/update-cli/update-command-plugins.ts 1
24432443
src/cli/update-cli/update-command-post-update.ts 2
24442444
src/cli/users-cli.ts 1
2445-
src/commands/agent-exec.ts 6
2445+
src/commands/agent-exec.ts 1
24462446
src/commands/agent-via-gateway.ts 7
24472447
src/commands/agents.commands.bind.ts 5
24482448
src/commands/agents.commands.identity.ts 1

‎scripts/code-mode-model-matrix.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
type QaEvidenceStatus,
1616
type QaEvidenceSummaryJson,
1717
} from "../extensions/qa-lab/api.js";
18-
import type { AgentExecEnvelope } from "../src/commands/agent-exec.ts";
18+
import type { AgentExecEnvelope } from "../src/commands/agent-exec-result.ts";
1919
import { requireOptionArgument } from "./lib/arg-utils.mts";
2020
import { previewForDevToolLog, redactJsonValueForDevToolLog } from "./lib/dev-tooling-safety.ts";
2121

‎scripts/lib/config-boundary-guard.mts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const PROCESS_BOUNDARY_DIRECT_CONFIG_LOAD_FILES = new Set([
4040
// reads the ambient location and resolves from an already published runtime
4141
// snapshot, so it cannot express a pinned run; the file-scoped loader is the
4242
// point. Ambient resolution in the same command does use `getRuntimeConfig()`.
43-
"src/commands/agent-exec.ts",
43+
"src/commands/agent-exec-input.ts",
4444
]);
4545

4646
const BROAD_CONFIG_RUNTIME_COMPAT_FILES = new Set([

‎src/agents/embedded-agent-runner/run/terminal-resolution.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import { SILENT_REPLY_TOKEN } from "../../../auto-reply/tokens.js";
3-
import { classifyAgentExecResult } from "../../../commands/agent-exec.js";
3+
import { classifyAgentExecResult } from "../../../commands/agent-exec-result.js";
44
import {
55
buildEmbeddedRunnerAssistant,
66
makeEmbeddedRunnerAttempt,

‎src/cli/help-cold-imports.test.ts‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ vi.mock("../commands/agent-via-gateway.js", () => {
148148
return { agentCliCommand: vi.fn(async () => {}) };
149149
});
150150

151+
vi.mock("../commands/agent-exec.js", () => {
152+
loaded.mark("agent-exec-command");
153+
return { agentExecCommand: vi.fn(async () => {}) };
154+
});
155+
151156
vi.mock("../commands/agents.commands.add.js", () => {
152157
loaded.mark("agents-add-command");
153158
return { agentsAddCommand: vi.fn(async () => {}) };
@@ -311,23 +316,27 @@ describe("subcommand help cold imports", () => {
311316
expect(loaded.modules).not.toContain("default-runtime");
312317
});
313318

314-
it("keeps agents help out of agent action modules", async () => {
315-
const { registerAgentsCommands } = await import("./program/register.agent.js");
316-
const { registerAgentTurnCommand } = await import("./program/register.agent-turn.js");
317-
const program = makeProgram();
318-
319-
registerAgentTurnCommand(program, { agentChannelOptions: "last|telegram|discord" });
320-
registerAgentsCommands(program);
321-
await expectHelpExit(program, ["agents", "--help"]);
322-
323-
expect(loaded.modules).not.toContain("agent-via-gateway-command");
324-
expect(loaded.modules).not.toContain("agents-add-command");
325-
expect(loaded.modules).not.toContain("agents-bind-command");
326-
expect(loaded.modules).not.toContain("agents-delete-command");
327-
expect(loaded.modules).not.toContain("agents-identity-command");
328-
expect(loaded.modules).not.toContain("agents-list-command");
329-
expect(loaded.modules).not.toContain("default-runtime");
330-
});
319+
it.each(["agent", "agent exec", "agents"])(
320+
"keeps %s help out of agent action modules",
321+
async (command) => {
322+
const { registerAgentsCommands } = await import("./program/register.agent.js");
323+
const { registerAgentTurnCommand } = await import("./program/register.agent-turn.js");
324+
const program = makeProgram();
325+
326+
registerAgentTurnCommand(program, { agentChannelOptions: "last|telegram|discord" });
327+
registerAgentsCommands(program);
328+
await expectHelpExit(program, [...command.split(" "), "--help"]);
329+
330+
expect(loaded.modules).not.toContain("agent-via-gateway-command");
331+
expect(loaded.modules).not.toContain("agent-exec-command");
332+
expect(loaded.modules).not.toContain("agents-add-command");
333+
expect(loaded.modules).not.toContain("agents-bind-command");
334+
expect(loaded.modules).not.toContain("agents-delete-command");
335+
expect(loaded.modules).not.toContain("agents-identity-command");
336+
expect(loaded.modules).not.toContain("agents-list-command");
337+
expect(loaded.modules).not.toContain("default-runtime");
338+
},
339+
);
331340

332341
it("keeps secrets help out of secrets action modules", async () => {
333342
const { registerSecretsCli } = await import("./secrets-cli.js");

‎src/cli/program/register.agent-turn.ts‎

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,11 @@ import { THINKING_LEVELS_HELP } from "../../auto-reply/thinking.shared.js";
77
import { inheritOptionFromParent } from "../command-options.js";
88
import { measureCliCommandStartup } from "../command-startup-timing.js";
99
import { formatHelpExamples } from "../help-format.js";
10-
import { requestExitAfterOneShotOutput } from "../one-shot-exit.js";
11-
12-
type AgentViaGatewayModule = typeof import("../../commands/agent-via-gateway.js");
13-
type AgentExecModule = typeof import("../../commands/agent-exec.js");
14-
type CliUtilsModule = typeof import("../cli-utils.js");
15-
type GlobalStateModule = typeof import("../../global-state.js");
16-
type RuntimeModule = typeof import("../../runtime.js");
17-
18-
async function loadAgentCliCommand(): Promise<AgentViaGatewayModule["agentCliCommand"]> {
19-
return (await import("../../commands/agent-via-gateway.js")).agentCliCommand;
20-
}
21-
22-
async function loadAgentExecCommand(): Promise<AgentExecModule["agentExecCommand"]> {
23-
return (await import("../../commands/agent-exec.js")).agentExecCommand;
24-
}
2510

2611
function collectFallback(value: string, previous: string[]): string[] {
2712
return [...previous, value];
2813
}
2914

30-
async function loadDefaultRuntime(): Promise<RuntimeModule["defaultRuntime"]> {
31-
return (await import("../../runtime.js")).defaultRuntime;
32-
}
33-
34-
async function loadRunCommandWithRuntime(): Promise<CliUtilsModule["runCommandWithRuntime"]> {
35-
return (await import("../cli-utils.js")).runCommandWithRuntime;
36-
}
37-
38-
async function loadSetVerbose(): Promise<GlobalStateModule["setVerbose"]> {
39-
return (await import("../../global-state.js")).setVerbose;
40-
}
41-
4215
/** Register `openclaw agent` for one Gateway-backed agent turn. */
4316
export function registerAgentTurnCommand(
4417
program: Command,
@@ -110,15 +83,21 @@ ${theme.muted("Docs:")} ${formatDocsLink("/cli/agent", "docs.openclaw.ai/cli/age
11083
.action(async (opts): Promise<void> => {
11184
const verboseLevel =
11285
typeof opts.verbose === "string" ? normalizeLowercaseStringOrEmpty(opts.verbose) : "";
113-
const [defaultRuntime, runCommandWithRuntime, setVerbose, agentCliCommand] =
114-
await measureCliCommandStartup("agent-action-imports", () =>
115-
Promise.all([
116-
loadDefaultRuntime(),
117-
loadRunCommandWithRuntime(),
118-
loadSetVerbose(),
119-
loadAgentCliCommand(),
120-
]),
121-
);
86+
const [
87+
{ defaultRuntime },
88+
{ runCommandWithRuntime },
89+
{ setVerbose },
90+
{ agentCliCommand },
91+
{ requestExitAfterOneShotOutput },
92+
] = await measureCliCommandStartup("agent-action-imports", () =>
93+
Promise.all([
94+
import("../../runtime.js"),
95+
import("../cli-utils.js"),
96+
import("../../global-state.js"),
97+
import("../../commands/agent-via-gateway.js"),
98+
import("../one-shot-exit.js"),
99+
]),
100+
);
122101
await runCommandWithRuntime(defaultRuntime, async () => {
123102
setVerbose(verboseLevel === "on");
124103
await agentCliCommand(opts, defaultRuntime);
@@ -193,10 +172,16 @@ ${theme.muted("Docs:")} ${formatDocsLink("/cli/agent", "docs.openclaw.ai/cli/age
193172
timeout: inheritOptionFromParent<string>(command, "timeout") ?? opts.timeout,
194173
json: opts.json === true || parentOpts?.json === true,
195174
};
196-
const [defaultRuntime, runCommandWithRuntime, agentExecCommand] = await Promise.all([
197-
loadDefaultRuntime(),
198-
loadRunCommandWithRuntime(),
199-
loadAgentExecCommand(),
175+
const [
176+
{ defaultRuntime },
177+
{ runCommandWithRuntime },
178+
{ agentExecCommand },
179+
{ requestExitAfterOneShotOutput },
180+
] = await Promise.all([
181+
import("../../runtime.js"),
182+
import("../cli-utils.js"),
183+
import("../../commands/agent-exec.js"),
184+
import("../one-shot-exit.js"),
200185
]);
201186
await runCommandWithRuntime(defaultRuntime, async () => {
202187
const result = await agentExecCommand(message, execOpts, defaultRuntime);

‎src/cli/program/register.onboard.ts‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { theme } from "../../../packages/terminal-core/src/theme.js";
66
import { formatAuthChoiceChoicesForCli } from "../../commands/auth-choice-options.js";
77
import type { GatewayDaemonRuntime } from "../../commands/daemon-runtime.js";
88
import { CORE_ONBOARD_AUTH_FLAGS } from "../../commands/onboard-core-auth-flags.js";
9-
import { rejectOnboardingOption } from "../../commands/onboard-options.js";
109
import type {
1110
AuthChoice,
1211
GatewayAuthChoice,
@@ -45,18 +44,19 @@ const MODERN_ONBOARD_OPTION_KEYS = new Set([
4544
"json",
4645
]);
4746

48-
function validateRecommendationParentOptions(
47+
async function validateRecommendationParentOptions(
4948
command: Command,
5049
runtime: RuntimeEnv,
5150
json = false,
52-
): boolean {
51+
): Promise<boolean> {
5352
const unsupported = listExplicitOptionFlagsExcept(
5453
command,
5554
json ? RECOMMENDATION_READ_PARENT_OPTIONS : NO_RECOMMENDATION_PARENT_OPTIONS,
5655
);
5756
if (unsupported.length === 0) {
5857
return true;
5958
}
59+
const { rejectOnboardingOption } = await import("../../commands/onboard-options.js");
6060
return rejectOnboardingOption(
6161
{ json },
6262
runtime,
@@ -227,12 +227,13 @@ function pickOnboardAuthOptionValues(opts: Record<string, unknown>): Partial<Onb
227227
};
228228
}
229229

230-
export function resolveOnboardCommandOptions(
230+
export async function resolveOnboardCommandOptions(
231231
opts: Record<string, unknown>,
232232
command: Command,
233233
runtime: RuntimeEnv,
234-
): OnboardOptions | false {
234+
): Promise<OnboardOptions | false> {
235235
if (opts.customImageInput === true && opts.customTextInput === true) {
236+
const { rejectOnboardingOption } = await import("../../commands/onboard-options.js");
236237
const message = "Use either --custom-image-input or --custom-text-input, not both.";
237238
return rejectOnboardingOption({ json: opts.json === true }, runtime, message);
238239
}
@@ -321,7 +322,7 @@ export function registerOnboardCommand(program: Command): void {
321322
const { defaultRuntime } = await import("../../runtime.js");
322323
await runCommandWithRuntime(defaultRuntime, async () => {
323324
const json = Boolean(opts.json || recommendationsCommand.parent?.opts().json);
324-
if (!validateRecommendationParentOptions(command, defaultRuntime, json)) {
325+
if (!(await validateRecommendationParentOptions(command, defaultRuntime, json))) {
325326
return;
326327
}
327328
const { onboardRecommendationsCommand } =
@@ -338,8 +339,8 @@ export function registerOnboardCommand(program: Command): void {
338339
const { defaultRuntime } = await import("../../runtime.js");
339340
await runCommandWithRuntime(defaultRuntime, async () => {
340341
if (
341-
!validateRecommendationParentOptions(command, defaultRuntime) ||
342-
!validateRecommendationParentOptions(recommendations, defaultRuntime)
342+
!(await validateRecommendationParentOptions(command, defaultRuntime)) ||
343+
!(await validateRecommendationParentOptions(recommendations, defaultRuntime))
343344
) {
344345
return;
345346
}
@@ -356,8 +357,8 @@ export function registerOnboardCommand(program: Command): void {
356357
const { defaultRuntime } = await import("../../runtime.js");
357358
await runCommandWithRuntime(defaultRuntime, async () => {
358359
if (
359-
!validateRecommendationParentOptions(command, defaultRuntime) ||
360-
!validateRecommendationParentOptions(recommendations, defaultRuntime)
360+
!(await validateRecommendationParentOptions(command, defaultRuntime)) ||
361+
!(await validateRecommendationParentOptions(recommendations, defaultRuntime))
361362
) {
362363
return;
363364
}
@@ -370,6 +371,7 @@ export function registerOnboardCommand(program: Command): void {
370371
command.action(async (opts, commandRuntime: Command) => {
371372
const { defaultRuntime } = await import("../../runtime.js");
372373
await runCommandWithRuntime(defaultRuntime, async () => {
374+
const { rejectOnboardingOption } = await import("../../commands/onboard-options.js");
373375
const rejectOption = (message: string) =>
374376
rejectOnboardingOption({ json: Boolean(opts.json) }, defaultRuntime, message);
375377
if (opts.modern) {
@@ -416,7 +418,7 @@ export function registerOnboardCommand(program: Command): void {
416418
);
417419
return;
418420
}
419-
const onboardingOptions = resolveOnboardCommandOptions(
421+
const onboardingOptions = await resolveOnboardCommandOptions(
420422
opts as Record<string, unknown>,
421423
commandRuntime,
422424
defaultRuntime,

‎src/cli/program/register.setup.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { readStringValue } from "@openclaw/normalization-core/string-coerce";
33
import type { Command } from "commander";
44
import { formatDocsLink } from "../../../packages/terminal-core/src/links.js";
55
import { theme } from "../../../packages/terminal-core/src/theme.js";
6-
import { rejectOnboardingOption } from "../../commands/onboard-options.js";
76
import type { RuntimeEnv } from "../../runtime.js";
87
import { runCommandWithRuntime } from "../cli-utils.js";
98
import { hasExplicitOptions, listExplicitOptionFlagsExcept } from "../command-options.js";
@@ -92,6 +91,7 @@ async function runOnboardingEntry(
9291
if (options.baseline) {
9392
const unsupportedOptions = listExplicitOptionFlagsExcept(commandRuntime, BASELINE_OPTION_NAMES);
9493
if (unsupportedOptions.length > 0) {
94+
const { rejectOnboardingOption } = await import("../../commands/onboard-options.js");
9595
const message = `--baseline cannot be combined with: ${unsupportedOptions.join(", ")}.`;
9696
rejectOnboardingOption({ json: options.json === true }, runtime, message);
9797
return;
@@ -103,7 +103,7 @@ async function runOnboardingEntry(
103103
);
104104
return;
105105
}
106-
const onboardingOptions = resolveOnboardCommandOptions(options, commandRuntime, runtime);
106+
const onboardingOptions = await resolveOnboardCommandOptions(options, commandRuntime, runtime);
107107
if (!onboardingOptions) {
108108
return;
109109
}

0 commit comments

Comments
 (0)