Skip to content

Commit 7333fdf

Browse files
authored
Merge pull request #10 from radroid/t3x/fix-azure-devops-probe-timeout
fix(server): treat slow `az --version` as present, not missing (#4)
2 parents 237ca66 + d8987fa commit 7333fdf

2 files changed

Lines changed: 236 additions & 8 deletions

File tree

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Option from "effect/Option";
4+
import { ChildProcessSpawner } from "effect/unstable/process";
5+
import {
6+
type VcsError,
7+
VcsProcessExitError,
8+
VcsProcessSpawnError,
9+
VcsProcessTimeoutError,
10+
} from "@t3tools/contracts";
11+
12+
import * as VcsProcess from "../vcs/VcsProcess.ts";
13+
import * as AzureDevOps from "./AzureDevOpsSourceControlProvider.ts";
14+
import { probeSourceControlProvider } from "./SourceControlProviderDiscovery.ts";
15+
16+
const processOutput = (
17+
stdout: string,
18+
options?: {
19+
readonly stderr?: string;
20+
readonly exitCode?: ChildProcessSpawner.ExitCode;
21+
},
22+
): VcsProcess.VcsProcessOutput => ({
23+
exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
24+
stdout,
25+
stderr: options?.stderr ?? "",
26+
stdoutTruncated: false,
27+
stderrTruncated: false,
28+
});
29+
30+
interface CapturingProcess {
31+
readonly process: VcsProcess.VcsProcess["Service"];
32+
readonly calls: ReadonlyArray<VcsProcess.VcsProcessInput>;
33+
}
34+
35+
const makeProcess = (
36+
run: (input: VcsProcess.VcsProcessInput) => Effect.Effect<VcsProcess.VcsProcessOutput, VcsError>,
37+
): CapturingProcess => {
38+
const calls: VcsProcess.VcsProcessInput[] = [];
39+
return {
40+
calls,
41+
process: {
42+
run: (input) => {
43+
calls.push(input);
44+
return run(input);
45+
},
46+
},
47+
};
48+
};
49+
50+
const isVersionCall = (input: VcsProcess.VcsProcessInput): boolean => input.args[0] === "--version";
51+
const isAuthCall = (input: VcsProcess.VcsProcessInput): boolean => input.args[0] === "account";
52+
53+
it.effect(
54+
"reports Azure CLI as available and still runs the auth probe when `--version` times out",
55+
() =>
56+
Effect.gen(function* () {
57+
const { process, calls } = makeProcess((input) => {
58+
if (isVersionCall(input)) {
59+
return Effect.fail(
60+
new VcsProcessTimeoutError({
61+
operation: input.operation,
62+
command: input.command,
63+
cwd: input.cwd,
64+
timeoutMs: input.timeoutMs ?? 0,
65+
}),
66+
);
67+
}
68+
return Effect.succeed(processOutput("[email protected]\n"));
69+
});
70+
71+
const item = yield* probeSourceControlProvider({
72+
spec: AzureDevOps.discovery,
73+
process,
74+
cwd: "/repo",
75+
});
76+
77+
assert.strictEqual(item.status, "available");
78+
assert.strictEqual(item.auth.status, "authenticated");
79+
assert.deepStrictEqual(item.auth.account, Option.some("[email protected]"));
80+
assert.ok(
81+
calls.some(isAuthCall),
82+
"expected an `account show` auth probe to run after a slow `--version`",
83+
);
84+
}),
85+
);
86+
87+
it.effect(
88+
"reports Azure CLI as missing and skips the auth probe when `--version` cannot spawn",
89+
() =>
90+
Effect.gen(function* () {
91+
const { process, calls } = makeProcess((input) => {
92+
if (isVersionCall(input)) {
93+
return Effect.fail(
94+
new VcsProcessSpawnError({
95+
operation: input.operation,
96+
command: input.command,
97+
cwd: input.cwd,
98+
cause: Object.assign(new Error("spawn az ENOENT"), { code: "ENOENT" }),
99+
}),
100+
);
101+
}
102+
return Effect.succeed(processOutput("[email protected]\n"));
103+
});
104+
105+
const item = yield* probeSourceControlProvider({
106+
spec: AzureDevOps.discovery,
107+
process,
108+
cwd: "/repo",
109+
});
110+
111+
assert.strictEqual(item.status, "missing");
112+
assert.strictEqual(item.auth.status, "unknown");
113+
assert.strictEqual(calls.length, 1);
114+
assert.strictEqual(calls[0]?.args[0], "--version");
115+
}),
116+
);
117+
118+
it.effect(
119+
"raises the `--version` probe timeout above 15s and reports the version on the happy path",
120+
() =>
121+
Effect.gen(function* () {
122+
const { process, calls } = makeProcess((input) => {
123+
if (isVersionCall(input)) {
124+
return Effect.succeed(processOutput("azure-cli 2.87.0\n"));
125+
}
126+
return Effect.succeed(processOutput("[email protected]\n"));
127+
});
128+
129+
const item = yield* probeSourceControlProvider({
130+
spec: AzureDevOps.discovery,
131+
process,
132+
cwd: "/repo",
133+
});
134+
135+
const versionCall = calls.find(isVersionCall);
136+
assert.ok(versionCall, "expected a `--version` probe to run");
137+
assert.ok(
138+
(versionCall.timeoutMs ?? 0) >= 15_000,
139+
`expected the \`--version\` probe timeout to be >= 15000ms, got ${String(versionCall.timeoutMs)}`,
140+
);
141+
assert.deepStrictEqual(item.version, Option.some("azure-cli 2.87.0"));
142+
assert.strictEqual(item.status, "available");
143+
assert.strictEqual(item.auth.status, "authenticated");
144+
}),
145+
);
146+
147+
it.effect(
148+
"reports available but unauthenticated when `--version` times out and `account show` exits non-zero",
149+
() =>
150+
Effect.gen(function* () {
151+
const { process } = makeProcess((input) => {
152+
if (isVersionCall(input)) {
153+
return Effect.fail(
154+
new VcsProcessTimeoutError({
155+
operation: input.operation,
156+
command: input.command,
157+
cwd: input.cwd,
158+
timeoutMs: input.timeoutMs ?? 0,
159+
}),
160+
);
161+
}
162+
return Effect.succeed(
163+
processOutput("", {
164+
stderr: "ERROR: Please run 'az login' to setup account.",
165+
exitCode: ChildProcessSpawner.ExitCode(1),
166+
}),
167+
);
168+
});
169+
170+
const item = yield* probeSourceControlProvider({
171+
spec: AzureDevOps.discovery,
172+
process,
173+
cwd: "/repo",
174+
});
175+
176+
assert.strictEqual(item.status, "available");
177+
assert.strictEqual(item.auth.status, "unauthenticated");
178+
}),
179+
);
180+
181+
it.effect(
182+
"treats a non-spawn `--version` failure (e.g. a non-zero exit) as present and still runs the auth probe",
183+
() =>
184+
Effect.gen(function* () {
185+
const { process, calls } = makeProcess((input) => {
186+
if (isVersionCall(input)) {
187+
return Effect.fail(
188+
new VcsProcessExitError({
189+
operation: input.operation,
190+
command: input.command,
191+
cwd: input.cwd,
192+
exitCode: 1,
193+
detail: "Process exited with a non-zero status.",
194+
}),
195+
);
196+
}
197+
return Effect.succeed(processOutput("[email protected]\n"));
198+
});
199+
200+
const item = yield* probeSourceControlProvider({
201+
spec: AzureDevOps.discovery,
202+
process,
203+
cwd: "/repo",
204+
});
205+
206+
// Only a genuine spawn failure means "missing"; any other runtime failure of
207+
// `--version` (here a non-zero exit) means the CLI is present, so it stays
208+
// available and the auth probe still runs. (issue #4)
209+
assert.strictEqual(item.status, "available");
210+
assert.strictEqual(item.auth.status, "authenticated");
211+
assert.ok(
212+
calls.some(isAuthCall),
213+
"expected the auth probe to run after a non-spawn `--version` failure",
214+
);
215+
}),
216+
);

‎apps/server/src/sourceControl/SourceControlProviderDiscovery.ts‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import * as Option from "effect/Option";
1010
import type * as SourceControlProvider from "./SourceControlProvider.ts";
1111
import type * as VcsProcess from "../vcs/VcsProcess.ts";
1212

13+
// `az --version` on Windows routinely takes 6-8s (Python startup + Azure CLI's
14+
// "updates available" check), so the old 5s cap misclassified a present-but-slow
15+
// CLI as missing. Raise the discovery probe budget well above that. (issue #4)
16+
const CLI_PROBE_TIMEOUT_MS = 15_000;
17+
1318
export interface SourceControlAuthProbeInput {
1419
readonly stdout: string;
1520
readonly stderr: string;
@@ -167,7 +172,7 @@ function probeCli(input: {
167172
command: input.spec.executable,
168173
args: input.spec.versionArgs,
169174
cwd: input.cwd,
170-
timeoutMs: 5_000,
175+
timeoutMs: CLI_PROBE_TIMEOUT_MS,
171176
maxOutputBytes: 8_000,
172177
appendTruncationMarker: true,
173178
})
@@ -186,17 +191,24 @@ function probeCli(input: {
186191
detail: Option.none<string>(),
187192
}) satisfies DiscoveryProbeResult,
188193
),
189-
Effect.catch((cause) =>
190-
Effect.succeed({
194+
Effect.catch((cause) => {
195+
// Only a genuine spawn failure (e.g. ENOENT) means the CLI is absent, so
196+
// that is the sole case that reports "missing" and shows the install hint.
197+
// Any other failure (timeout, non-zero exit, etc.) means the CLI is present
198+
// but the version probe was slow/noisy; keep it "available" so
199+
// `probeSourceControlProvider` still runs the fast `account show` auth
200+
// probe instead of short-circuiting to the install hint. (issue #4)
201+
const status = cause._tag === "VcsProcessSpawnError" ? "missing" : "available";
202+
return Effect.succeed({
191203
kind: input.spec.kind,
192204
label: input.spec.label,
193205
executable: input.spec.executable,
194-
status: "missing" as const,
206+
status,
195207
version: Option.none<string>(),
196208
installHint: input.spec.installHint,
197209
detail: detailFromCause(cause),
198-
} satisfies DiscoveryProbeResult),
199-
),
210+
} satisfies DiscoveryProbeResult);
211+
}),
200212
);
201213
}
202214

@@ -244,7 +256,7 @@ export function probeSourceControlProvider(input: {
244256
args: spec.authArgs,
245257
cwd: input.cwd,
246258
allowNonZeroExit: true,
247-
timeoutMs: 5_000,
259+
timeoutMs: CLI_PROBE_TIMEOUT_MS,
248260
maxOutputBytes: 8_000,
249261
appendTruncationMarker: true,
250262
})
@@ -287,7 +299,7 @@ export const refineUnknownRemoteProvider = Effect.fn("refineUnknownRemoteProvide
287299
args: spec.authArgs,
288300
cwd: input.cwd,
289301
allowNonZeroExit: true,
290-
timeoutMs: 5_000,
302+
timeoutMs: CLI_PROBE_TIMEOUT_MS,
291303
maxOutputBytes: 8_000,
292304
appendTruncationMarker: true,
293305
})

0 commit comments

Comments
 (0)