Skip to content

Commit 0bdc128

Browse files
jrusso1020claude
andauthored
fix(cli): never prompt or auto-install when nobody is at the terminal (#6617)
* fix(cli): never prompt or auto-install when nobody is at the terminal publish and upgrade called the confirmation prompt with no terminal check. With stdin from /dev/null, publish exited 13; with an open pipe it hung. Unattended and without --yes, both now exit 2 before doing any work, and stderr names --yes (upgrade also names --check). All prompts and spinners now go through ui/prompts.ts, the only module allowed to import @clack/prompts (a lint rule and a contract test enforce it). Its confirm, select, text and password refuse to prompt in an unattended run, and its spinner prints only the final line, on stderr. init, feedback, catalog and cloud delete decide whether to ask with isUnattended() instead of a TTY check of their own. The background auto-install no longer starts in an unattended run, and the update notice ("Run: npm install -g ...") is not shown while an install for that version is pending. CI now counts as set for any value but "false" or "0". Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * test(cli): neutralize agent detection in the catalog offer tests through isAgentDriven Attendance now reads isAgentDriven(), so that is what the catalog tests stub to stay independent of the agent running the suite. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 6cf4b56 commit 0bdc128

31 files changed

Lines changed: 300 additions & 37 deletions

‎.oxlintrc.json‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@
9090
},
9191
{
9292
"files": ["packages/cli/src/**/*.ts"],
93-
"excludeFiles": ["**/*.test.ts", "packages/cli/src/utils/defineCommand.ts"],
93+
"excludeFiles": [
94+
"**/*.test.ts",
95+
"packages/cli/src/utils/defineCommand.ts",
96+
"packages/cli/src/ui/prompts.ts"
97+
],
9498
"rules": {
9599
"no-restricted-imports": [
96100
"error",
@@ -100,6 +104,10 @@
100104
"name": "citty",
101105
"importNames": ["defineCommand"],
102106
"message": "Define commands with defineCommand from packages/cli/src/utils/defineCommand.ts so the shared guards and the agent-mode contract test cover them."
107+
},
108+
{
109+
"name": "@clack/prompts",
110+
"message": "Import prompts and spinners from packages/cli/src/ui/prompts.ts: it refuses to prompt when nobody can answer."
103111
}
104112
]
105113
}

‎docs/packages/cli.mdx‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ example rather than waiting for input. Two commands are interactive on a TTY:
7171
`--non-interactive`, and `catalog --human-friendly` opens a picker. Nothing else
7272
prompts, apart from confirmations you can skip with `--yes` or `--no-confirm`.
7373

74+
**Unattended runs never prompt.** A run is unattended when stdin or stdout is not
75+
a terminal, under CI, inside a coding agent, or with `HYPERFRAMES_AGENT=1`.
76+
There, a command that would ask for confirmation, such as `publish` or `upgrade`,
77+
exits 2 instead and names the flag to pass (`--yes`), and spinners print only
78+
their final line, on stderr.
79+
7480
**Exit codes.** `0` means the command succeeded, `1` means it ran and failed, and
7581
`2` means the command line itself is wrong: an unknown command or flag, or a
7682
missing or invalid value.
@@ -85,8 +91,10 @@ it cannot install — `npx`, an unknown installer — you get a notice instead:
8591
Run: npx hyperframes@latest
8692
```
8793

88-
Both stay silent in CI and non-TTY shells. `HYPERFRAMES_NO_UPDATE_CHECK=1` turns
89-
off the check, the install, and the notice.
94+
Both stay silent in unattended runs, and the background install never starts
95+
there. While an install is underway, the notice is not shown.
96+
`HYPERFRAMES_NO_UPDATE_CHECK=1` turns off the check, the install, and the
97+
notice; `HYPERFRAMES_NO_AUTO_INSTALL=1` turns off only the install.
9098

9199
## Create a project
92100

@@ -1027,7 +1035,7 @@ npx hyperframes publish --space <space-id>
10271035

10281036
| Flag | Description |
10291037
| ------------------------ | ------------------------------------------------------------------ |
1030-
| `--yes, -y` | Skip the confirmation prompt |
1038+
| `--yes, -y` | Skip the confirmation prompt. Required in an unattended run |
10311039
| `--public` | Make the claimed project visible to anyone |
10321040
| `--update <url-or-id>` | Update an existing project in place. Requires sign-in. |
10331041
| `--space <space-id>` | Publish into a shared team space. Requires sign-in. |
@@ -1887,7 +1895,8 @@ npx hyperframes upgrade --project # bump pinned CLI scripts in package.jso
18871895

18881896
`upgrade` compares your installed version against npm. `--check` exits without
18891897
prompting, `--yes, -y` upgrades a detected global install (otherwise it prints
1890-
the right `npx` command), and `--project [dir]` rewrites
1898+
the right `npx` command; an unattended run without `--yes` or `--check` exits 2),
1899+
and `--project [dir]` rewrites
18911900
`hyperframes@<version>` script pins in a project's `package.json`.
18921901
`--check --json` returns:
18931902

‎packages/cli/src/cli.agentMode.test.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawn } from "node:child_process";
2-
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
2+
import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { dirname, join } from "node:path";
55
import { fileURLToPath } from "node:url";
@@ -152,4 +152,26 @@ describe("agent-mode contract", () => {
152152
expectQuiet(run);
153153
expect(run.code, run.stderr).toBe(2);
154154
});
155+
156+
it.each([["publish"], ["upgrade"]])(
157+
"%s without --yes refuses to prompt, names --yes, and exits 2",
158+
async (name) => {
159+
const run = await runAsAgent([name]);
160+
expectQuiet(run);
161+
expect(run.code, run.stderr).toBe(2);
162+
expect(run.stdout).toBe("");
163+
expect(run.stderr).toContain("--yes");
164+
},
165+
);
166+
167+
it("only the prompt wrapper loads @clack/prompts, statically or dynamically", () => {
168+
const offenders = (readdirSync(srcDir, { recursive: true }) as string[]).filter(
169+
(file) =>
170+
file.endsWith(".ts") &&
171+
!file.endsWith(".test.ts") &&
172+
file !== join("ui", "prompts.ts") &&
173+
readFileSync(join(srcDir, file), "utf8").includes('"@clack/prompts"'),
174+
);
175+
expect(offenders).toEqual([]);
176+
});
155177
});

‎packages/cli/src/commands/auth/login.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ async function readAllWithTimeout(
468468
}
469469

470470
async function promptForKey(): Promise<string> {
471-
const clack = await import("@clack/prompts");
471+
const clack = await import("../../ui/prompts.js");
472472
const value = await clack.password({
473473
message: "Enter HeyGen API key",
474474
validate: (v) => {

‎packages/cli/src/commands/benchmark.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { resolveProject } from "../utils/project.js";
1414
import { loadProducer } from "../utils/producer.js";
1515
import { c } from "../ui/colors.js";
1616
import { formatBytes, formatDuration, errorBox } from "../ui/format.js";
17-
import * as clack from "@clack/prompts";
17+
import * as clack from "../ui/prompts.js";
1818
import { withMeta } from "../utils/updateCheck.js";
1919
import { fpsToFfmpegArg } from "@hyperframes/core";
2020

‎packages/cli/src/commands/browser.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
22

33
const spinners: Array<{ running: boolean }> = [];
44
const logError = vi.fn();
5+
// A person at a terminal: the prompts and spinners these tests drive are refused otherwise.
6+
vi.mock("../utils/attendedTerminal.js", async (importOriginal) => ({
7+
...(await importOriginal<typeof import("../utils/attendedTerminal.js")>()),
8+
isUnattended: () => false,
9+
}));
510
vi.mock("@clack/prompts", () => ({
611
intro: vi.fn(),
712
outro: vi.fn(),

‎packages/cli/src/commands/browser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { failCommand } from "../utils/commandResult.js";
22
import { defineCommand } from "../utils/defineCommand.js";
33
import type { Example } from "./_examples.js";
4-
import * as clack from "@clack/prompts";
4+
import * as clack from "../ui/prompts.js";
55
import { c } from "../ui/colors.js";
66

77
export const examples: Example[] = [

‎packages/cli/src/commands/catalog.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ vi.mock("../registry/remote.js", () => ({
114114
}),
115115
}));
116116

117+
// The terminal and CI switches below decide attendance; the agent running the suite must not.
118+
vi.mock("../telemetry/agent_runtime.js", async (importOriginal) => ({
119+
...(await importOriginal<typeof import("../telemetry/agent_runtime.js")>()),
120+
isAgentDriven: () => false,
121+
}));
117122
vi.mock("@clack/prompts", () => ({
118123
confirm: async () => state.confirmAnswer,
119124
isCancel: (value: unknown) => value === null,

‎packages/cli/src/commands/catalog.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ export const examples: Example[] = [
1010
["Search (positional, same as --query)", 'hyperframes catalog "crossfade"'],
1111
];
1212

13-
import * as clack from "@clack/prompts";
13+
import * as clack from "../ui/prompts.js";
1414
import { realpath, type ItemType, type RegistryItem } from "@hyperframes/core";
1515
import { c } from "../ui/colors.js";
1616
import { loadAllItems } from "../registry/resolver.js";
1717
import { fetchRegistryManifest } from "../registry/remote.js";
1818
import { loadProjectConfig, DEFAULT_PROJECT_CONFIG } from "../utils/projectConfig.js";
1919
import { relative, resolve } from "node:path";
2020
import { finishCommand } from "../utils/commandResult.js";
21-
import { isAttendedTerminal } from "../utils/attendedTerminal.js";
21+
import { isUnattended } from "../utils/attendedTerminal.js";
2222
import { runAdd } from "./add.js";
2323
import { hasNoSearchableTokens, searchByWords } from "../registry/localSearch.js";
2424
import {
@@ -271,7 +271,7 @@ export default defineCommand({
271271
warnings = await prepareOnDeviceTier({
272272
assumedYes: args.yes === true,
273273
artifactRevision,
274-
canPrompt: isAttendedTerminal() && !json,
274+
canPrompt: !isUnattended() && !json,
275275
installRuntime: args["on-device"] === true,
276276
registry: config.registry,
277277
registryNames,
@@ -725,7 +725,7 @@ type LocalMode = "local-model" | "words";
725725
* person to prompt instead, or when that person has already answered.
726726
*/
727727
function localModelHint(json: boolean, status: LocalModelStatus | undefined): string | null {
728-
if (!json && isAttendedTerminal()) return null;
728+
if (!json && !isUnattended()) return null;
729729
if (status?.status !== "not-asked") return null;
730730
return nonInteractiveConsentMessage();
731731
}
@@ -743,7 +743,7 @@ async function offerLocalModel(
743743
registryBaseUrl: string,
744744
artifactRevision?: string,
745745
): Promise<void> {
746-
if (json || !isAttendedTerminal()) return;
746+
if (json || isUnattended()) return;
747747
if (localModelStatus().status !== "not-asked") return;
748748
// Deliberately not gated on the number of results. That gate was set when the
749749
// catalog was small; against 411 moves a word match nearly always returns

‎packages/cli/src/commands/cloud/delete.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { createCloudClient } from "../../cloud/index.js";
1111
import { reportApiError } from "../../cloud/errors.js";
1212
import { withMeta } from "../../utils/updateCheck.js";
1313
import { c } from "../../ui/colors.js";
14+
import { isUnattended } from "../../utils/attendedTerminal.js";
1415
import { errorBox } from "../../ui/format.js";
1516

1617
export default defineCommand({
@@ -48,10 +49,10 @@ export default defineCommand({
4849
// safety check. Force the caller to opt in via `--no-confirm`
4950
// so cron jobs, CI shells, and JSON consumers can't soft-delete
5051
// by accident.
51-
if (args.json || !process.stdin.isTTY) {
52+
if (args.json || isUnattended()) {
5253
errorBox(
5354
"Confirmation required",
54-
"delete cannot prompt for confirmation here — stdin isn't a TTY or --json was passed.",
55+
"delete cannot prompt for confirmation here: this run cannot answer a prompt, or --json was passed.",
5556
"Re-run with --no-confirm to acknowledge the irreversible delete.",
5657
);
5758
failCommand();
@@ -87,7 +88,7 @@ export default defineCommand({
8788
});
8889

8990
async function confirmDelete(id: string): Promise<boolean> {
90-
const clack = await import("@clack/prompts");
91+
const clack = await import("../../ui/prompts.js");
9192
const answer = await clack.confirm({
9293
message: `Delete render ${id}? This is irreversible.`,
9394
initialValue: false,

0 commit comments

Comments
 (0)