Skip to content

Commit 96260a8

Browse files
thymikeeclaude
andcommitted
refactor(commands): resolve command inputs and output from the caller host (#3753)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9a16be1 commit 96260a8

20 files changed

Lines changed: 267 additions & 162 deletions

File tree

‎src/agent-device-client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export function createAgentDeviceClient(
162162
command: DaemonCommandName,
163163
options: InternalRequestOptions = {},
164164
): Promise<T> => {
165-
const request = await prepareDaemonCommandRequest(command, options);
165+
const request = await prepareDaemonCommandRequest(command, options, host);
166166
return (await execute(
167167
request.command,
168168
request.positionals,

‎src/cli/commands/proxy.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function renderProxyStartup(
106106
startup: ProxyStartup,
107107
options: { useColor?: boolean } = {},
108108
): string {
109-
const useColor = options.useColor ?? supportsColor();
109+
const useColor = options.useColor ?? supportsColor(process.env);
110110
const checkmark = formatProxyOutputValue('✓', 'green', useColor);
111111
const proxyBaseUrl = formatProxyOutputValue(startup.proxyBaseUrl, 'cyan', useColor);
112112
const daemonBaseUrl = formatProxyOutputValue('<tunnel URL>', 'cyan', useColor);

‎src/cli/commands/screenshot.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const diffCommand: ClientCommandHandler = async ({ positionals, flags, cl
5252
flags,
5353
env: host.env,
5454
});
55-
await writeCommandOutput(flags, result, () => formatSnapshotDiffText(result));
55+
await writeCommandOutput(flags, result, () => formatSnapshotDiffText(result, host.env));
5656
return true;
5757
}
5858

@@ -104,7 +104,7 @@ export const diffCommand: ClientCommandHandler = async ({ positionals, flags, cl
104104
});
105105

106106
await writeCommandOutput(flags, result, () =>
107-
formatScreenshotDiffText(result as ScreenshotDiffResult),
107+
formatScreenshotDiffText(result as ScreenshotDiffResult, host),
108108
);
109109
return true;
110110
};

‎src/cli/replay-test/progress.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function formatReplayTestLiveProgressLine(
130130
): string {
131131
const title = event.title?.trim();
132132
const file = path.basename(event.file);
133-
const useColor = supportsColor(process.stderr);
133+
const useColor = supportsColor(process.env, process.stderr);
134134
const spinner = formatReplayTestProgressSpinner(spinnerFrame, { useColor });
135135
const shardSuffix = formatReplayTestProgressShardSuffix(event, { useColor });
136136
const stepSuffix = formatReplayTestLiveProgressStepSuffix(event, { useColor });
@@ -223,7 +223,7 @@ function replayTestProgressFailureContextLines(event: ReplayTestResult): string[
223223
}
224224

225225
function formatReplayTestCaseSummaryLine(event: ReplayTestResult): string {
226-
const useColor = supportsColor(process.stderr);
226+
const useColor = supportsColor(process.env, process.stderr);
227227
const statusLabel = formatReplayTestProgressStatusLabel(event);
228228
const name = formatReplayTestProgressName(event);
229229
const shardSuffix = formatReplayTestProgressShardSuffix(event, { useColor });

‎src/cli/replay-test/reporters/default.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function renderWarningsSection(
143143

144144
function formatReplayTestSummaryLine(data: ReplaySuiteResult, flakyCount: number): string {
145145
const durationMs = typeof data.durationMs === 'number' ? data.durationMs : undefined;
146-
const useColor = supportsColor();
146+
const useColor = supportsColor(process.env);
147147
const passed = formatReplaySummaryPassed(data.passed, { useColor });
148148
const total = formatReplaySummaryTotal(data.total, { useColor });
149149
const failedSuffix = data.failed > 0 ? `, ${data.failed} failed` : '';
@@ -169,7 +169,7 @@ function formatReplayDuration(durationMs: number, options: { useColor?: boolean
169169
}
170170

171171
function replayFlakyStatusIcon(): string {
172-
const useColor = supportsColor();
172+
const useColor = supportsColor(process.env);
173173
return useColor ? colorize('✓', 'yellow') : '✓';
174174
}
175175

‎src/commands/batch/projection.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { HostEnvironment } from '@agent-device/kernel/source-value';
12
import { PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog';
23
import {
34
STRUCTURED_BATCH_COMMAND_NAMES,
@@ -27,23 +28,32 @@ type PrepareDaemonCommandRequest = (
2728
command: string,
2829
input: CommandInput,
2930
stepNumber: number,
31+
host: HostEnvironment,
32+
) => Promise<DaemonCommandRequest>;
33+
34+
type PrepareBatchStep = (
35+
command: string,
36+
input: CommandInput,
37+
stepNumber: number,
3038
) => Promise<DaemonCommandRequest>;
3139

3240
export function createBatchDaemonWriter(
3341
prepareDaemonCommandRequest: PrepareDaemonCommandRequest,
3442
): AsyncDaemonWriter {
35-
return async (input) =>
43+
return async (input, host) =>
3644
request(PUBLIC_COMMANDS.batch, [], {
3745
...input,
38-
batchSteps: await readBatchDaemonSteps(input.steps, prepareDaemonCommandRequest),
46+
batchSteps: await readBatchDaemonSteps(input.steps, (command, input, stepNumber) =>
47+
prepareDaemonCommandRequest(command, input, stepNumber, host),
48+
),
3949
batchOnError: input.onError,
4050
batchMaxSteps: input.maxSteps,
4151
});
4252
}
4353

4454
async function readBatchDaemonSteps(
4555
steps: unknown,
46-
prepareDaemonCommandRequest: PrepareDaemonCommandRequest,
56+
prepareDaemonCommandRequest: PrepareBatchStep,
4757
): Promise<DaemonBatchStep[]> {
4858
if (!Array.isArray(steps) || steps.length === 0) {
4959
throw new AppError('INVALID_ARGS', 'batch requires a non-empty steps array.');
@@ -60,7 +70,7 @@ async function readBatchDaemonSteps(
6070
async function readBatchDaemonStep(
6171
step: unknown,
6272
stepNumber: number,
63-
prepareDaemonCommandRequest: PrepareDaemonCommandRequest,
73+
prepareDaemonCommandRequest: PrepareBatchStep,
6474
): Promise<DaemonBatchStep> {
6575
const record = readBatchStepRecord(step, stepNumber);
6676
const command = readBatchStepCommand(record, stepNumber);

‎src/commands/cli-grammar/types.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { HostEnvironment } from '@agent-device/kernel/source-value';
12
import type {
23
CliFlags,
34
CommandFlags,
@@ -97,6 +98,9 @@ export type DaemonWriter = (input: CommandInput) => DaemonCommandRequest;
9798
* `runFlow` includes on demand. The registry awaits whichever kind a command declares, so the
9899
* asynchrony stays inside the one writer that needs it.
99100
*/
100-
export type AsyncDaemonWriter = (input: CommandInput) => Promise<DaemonCommandRequest>;
101+
export type AsyncDaemonWriter = (
102+
input: CommandInput,
103+
host: HostEnvironment,
104+
) => Promise<DaemonCommandRequest>;
101105

102106
export type AnyDaemonWriter = DaemonWriter | AsyncDaemonWriter;

‎src/commands/command-projection.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { HostEnvironment } from '@agent-device/kernel/source-value';
12
import { createBatchDaemonWriter } from './batch/index.ts';
23
import type { AnyDaemonWriter, CommandInput, DaemonCommandRequest } from './cli-grammar/types.ts';
34
import { findCommandMetadata } from './command-metadata.ts';
@@ -17,6 +18,7 @@ async function prepareBatchDaemonCommandRequest(
1718
command: string,
1819
input: CommandInput,
1920
stepNumber: number,
21+
host: HostEnvironment,
2022
): Promise<DaemonCommandRequest> {
2123
const writer = (daemonWriters as Readonly<Record<string, AnyDaemonWriter>>)[command];
2224
if (!writer) {
@@ -31,6 +33,7 @@ async function prepareBatchDaemonCommandRequest(
3133
writer,
3234
metadata,
3335
metadata.readInput(input) as CommandInput,
36+
host,
3437
);
3538
} catch (error) {
3639
const message = error instanceof Error ? error.message : String(error);
@@ -46,22 +49,24 @@ async function prepareBatchDaemonCommandRequest(
4649
export async function prepareDaemonCommandRequest(
4750
command: DaemonCommandName,
4851
input: CommandInput,
52+
host: HostEnvironment,
4953
): Promise<DaemonCommandRequest> {
5054
const writer = daemonWriters[command];
5155
if (!writer) {
5256
throw new Error(`Missing daemon writer for command: ${command}`);
5357
}
5458
refuseRetiredCommonInput(input as Record<string, unknown>);
5559
const metadata = findCommandMetadata(command);
56-
return await prepareRequestWithMetadataFlags(writer, metadata, input);
60+
return await prepareRequestWithMetadataFlags(writer, metadata, input, host);
5761
}
5862

5963
async function prepareRequestWithMetadataFlags(
6064
writer: AnyDaemonWriter,
6165
metadata: ReturnType<typeof findCommandMetadata>,
6266
input: CommandInput,
67+
host: HostEnvironment,
6368
): Promise<DaemonCommandRequest> {
64-
const request = await writer(input);
69+
const request = await writer(input, host);
6570
return {
6671
...request,
6772
...(metadata ? { metadataFlags: readMetadataCommandFlags(metadata, request.options) } : {}),

‎src/commands/output/color.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ test('supportsColor uses the stream TTY state by default', () => {
99
delete process.env.FORCE_COLOR;
1010
delete process.env.NO_COLOR;
1111
try {
12-
assert.equal(supportsColor({ isTTY: true }), true);
13-
assert.equal(supportsColor({ isTTY: false }), false);
12+
assert.equal(supportsColor(process.env, { isTTY: true }), true);
13+
assert.equal(supportsColor(process.env, { isTTY: false }), false);
1414
} finally {
1515
if (typeof originalForceColor === 'string') process.env.FORCE_COLOR = originalForceColor;
1616
else delete process.env.FORCE_COLOR;
@@ -25,14 +25,14 @@ test('supportsColor honors explicit environment overrides', () => {
2525
try {
2626
process.env.FORCE_COLOR = '0';
2727
delete process.env.NO_COLOR;
28-
assert.equal(supportsColor({ isTTY: true }), false);
28+
assert.equal(supportsColor(process.env, { isTTY: true }), false);
2929

3030
process.env.FORCE_COLOR = '1';
31-
assert.equal(supportsColor({ isTTY: false }), true);
31+
assert.equal(supportsColor(process.env, { isTTY: false }), true);
3232

3333
delete process.env.FORCE_COLOR;
3434
process.env.NO_COLOR = '1';
35-
assert.equal(supportsColor({ isTTY: true }), false);
35+
assert.equal(supportsColor(process.env, { isTTY: true }), false);
3636
} finally {
3737
if (typeof originalForceColor === 'string') process.env.FORCE_COLOR = originalForceColor;
3838
else delete process.env.FORCE_COLOR;

‎src/commands/output/color.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import type { EnvMap } from '@agent-device/kernel/source-value';
12
import { styleText } from 'node:util';
23

3-
export function supportsColor(stream: { isTTY?: boolean } = process.stdout): boolean {
4-
const forceColor = process.env.FORCE_COLOR;
4+
export function supportsColor(env: EnvMap, stream: { isTTY?: boolean } = process.stdout): boolean {
5+
const forceColor = env.FORCE_COLOR;
56
if (typeof forceColor === 'string') {
67
return forceColor !== '0';
78
}
8-
if (typeof process.env.NO_COLOR === 'string') {
9+
if (typeof env.NO_COLOR === 'string') {
910
return false;
1011
}
1112
return Boolean(stream.isTTY);

0 commit comments

Comments
 (0)