@mwillbanks/ast-mcp 0.6.6 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +6 -0
- package/dist/ast-mcp.js +358 -337
- package/dist/ast-mcp.js.map +45 -32
- package/dist/workers/sql-postgres-worker.js +5 -0
- package/dist/workers/sql-postgres-worker.js.map +10 -0
- package/package.json +3 -2
- package/src/cli/doctor.ts +103 -0
- package/src/cli/index.ts +9 -1
- package/src/config/index.ts +61 -6
- package/src/config/registry.ts +209 -67
- package/src/config/v2-schema.ts +2 -0
- package/src/helpers/installer.ts +10 -1
- package/src/http/index.ts +12 -5
- package/src/installer/codex.ts +237 -0
- package/src/installer/diagnostics.ts +387 -0
- package/src/installer/directory-ancestry.ts +55 -0
- package/src/installer/index.ts +386 -119
- package/src/installer/ownership.ts +165 -0
- package/src/installer/service-state.ts +151 -0
- package/src/installer/service.ts +131 -7
- package/src/installer/transaction.ts +342 -0
- package/src/installer/transport.ts +22 -31
- package/src/intelligence/mutation/locks.ts +4 -1
- package/src/patch/engine.ts +157 -11
- package/src/runtime/approval.ts +427 -66
- package/src/runtime/config-write.ts +27 -1
- package/src/runtime/diagnostics.ts +115 -0
- package/src/runtime/file-capabilities.ts +2 -0
- package/src/runtime/hosts/codex.ts +16 -0
- package/src/runtime/path-policy.ts +2 -0
- package/src/runtime/release.ts +220 -0
- package/src/runtime/sql-assistance.ts +439 -0
- package/src/runtime/sql-options-schema.ts +31 -0
- package/src/runtime/sql-postgres-worker.ts +72 -0
- package/src/runtime/sql-postgres.ts +245 -0
- package/src/server/index.ts +35 -5
- package/src/server/lifecycle.ts +1 -1
- package/src/tools/configuration.ts +2 -0
- package/src/tools/diagnostics.ts +85 -0
- package/src/tools/files.ts +2 -0
- package/src/tools/index.ts +9 -0
- package/templates/skills/ast-mcp/SKILL.md +20 -1
- package/templates/skills/ast-mcp/evals/evals.json +274 -0
- package/templates/skills/ast-mcp/evals/semantic.ts +2 -0
- package/templates/skills/ast-mcp/evals/trigger-queries.json +4 -1
- package/templates/skills/ast-mcp/references/cli.md +97 -0
- package/templates/skills/ast-mcp/references/configuration.md +11 -3
- package/templates/skills/ast-mcp/references/diagnostics.md +106 -0
- package/templates/skills/ast-mcp/references/host-context.md +85 -0
- package/templates/skills/ast-mcp/references/installation.md +37 -0
- package/templates/skills/ast-mcp/references/issue-reporting.md +80 -0
- package/templates/skills/ast-mcp/references/patch-state-machine.md +10 -0
- package/templates/skills/ast-mcp/references/tool-catalog.md +120 -50
- package/templates/skills/ast-mcp/scripts/check-install.ts +223 -24
- package/templates/skills/ast-mcp/scripts/release.ts +220 -0
|
@@ -12,6 +12,12 @@ import {
|
|
|
12
12
|
isExecutable,
|
|
13
13
|
} from "./binary-resolution";
|
|
14
14
|
import { managedAstMcpHookEntry } from "./managed-hook";
|
|
15
|
+
import {
|
|
16
|
+
checkLatestVersion,
|
|
17
|
+
compareVersions,
|
|
18
|
+
isLatestVersionCheck,
|
|
19
|
+
type LatestVersionCheck,
|
|
20
|
+
} from "./release";
|
|
15
21
|
|
|
16
22
|
type Scope = "local" | "global";
|
|
17
23
|
type Target = "codex" | "claude" | "copilot";
|
|
@@ -117,9 +123,11 @@ const expectedIntelligenceTools = [
|
|
|
117
123
|
] as const;
|
|
118
124
|
|
|
119
125
|
export interface McpSmokeResult {
|
|
126
|
+
diagnostics?: { serverVersion: string; versionCheck: LatestVersionCheck };
|
|
120
127
|
exposedTools: string[];
|
|
121
128
|
initialized: boolean;
|
|
122
129
|
selectedWorkspace: boolean;
|
|
130
|
+
serverVersion?: string;
|
|
123
131
|
}
|
|
124
132
|
|
|
125
133
|
export function mcpStdioCommand(
|
|
@@ -130,6 +138,63 @@ export function mcpStdioCommand(
|
|
|
130
138
|
return [invocation.command, ...invocation.args];
|
|
131
139
|
}
|
|
132
140
|
|
|
141
|
+
type McpSmokeFailureKind = "timeout" | "transport";
|
|
142
|
+
|
|
143
|
+
class McpSmokeFailure extends Error {
|
|
144
|
+
constructor(
|
|
145
|
+
readonly kind: McpSmokeFailureKind,
|
|
146
|
+
message: string,
|
|
147
|
+
) {
|
|
148
|
+
super(message);
|
|
149
|
+
this.name = "McpSmokeFailure";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function mcpResponse(line: string): Record<string, unknown> | undefined {
|
|
154
|
+
let value: unknown;
|
|
155
|
+
try {
|
|
156
|
+
value = JSON.parse(line);
|
|
157
|
+
} catch {
|
|
158
|
+
throw new McpSmokeFailure("transport", "MCP smoke received malformed JSON");
|
|
159
|
+
}
|
|
160
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
161
|
+
throw new McpSmokeFailure(
|
|
162
|
+
"transport",
|
|
163
|
+
"MCP smoke received an invalid protocol message",
|
|
164
|
+
);
|
|
165
|
+
const message = value as Record<string, unknown>;
|
|
166
|
+
if (message.jsonrpc !== "2.0")
|
|
167
|
+
throw new McpSmokeFailure(
|
|
168
|
+
"transport",
|
|
169
|
+
"MCP smoke received an invalid protocol version",
|
|
170
|
+
);
|
|
171
|
+
if (
|
|
172
|
+
typeof message.method === "string" &&
|
|
173
|
+
message.id === undefined &&
|
|
174
|
+
!("result" in message) &&
|
|
175
|
+
!("error" in message)
|
|
176
|
+
)
|
|
177
|
+
return undefined;
|
|
178
|
+
const error = message.error;
|
|
179
|
+
const validError =
|
|
180
|
+
error &&
|
|
181
|
+
typeof error === "object" &&
|
|
182
|
+
Number.isInteger((error as Record<string, unknown>).code) &&
|
|
183
|
+
typeof (error as Record<string, unknown>).message === "string";
|
|
184
|
+
if (
|
|
185
|
+
typeof message.id !== "number" ||
|
|
186
|
+
!Number.isInteger(message.id) ||
|
|
187
|
+
"method" in message ||
|
|
188
|
+
"result" in message === "error" in message ||
|
|
189
|
+
("error" in message && !validError)
|
|
190
|
+
)
|
|
191
|
+
throw new McpSmokeFailure(
|
|
192
|
+
"transport",
|
|
193
|
+
"MCP smoke received an invalid protocol response",
|
|
194
|
+
);
|
|
195
|
+
return message;
|
|
196
|
+
}
|
|
197
|
+
|
|
133
198
|
async function mcpDeadline<T>(
|
|
134
199
|
pending: Promise<T>,
|
|
135
200
|
timeoutMs: number,
|
|
@@ -157,21 +222,26 @@ async function mcpDeadline<T>(
|
|
|
157
222
|
async function terminateMcpProcess(
|
|
158
223
|
processHandle: ReturnType<typeof Bun.spawn>,
|
|
159
224
|
) {
|
|
160
|
-
if (processHandle.exitCode !== null) return;
|
|
225
|
+
if (processHandle.exitCode !== null) return { exited: true, forced: false };
|
|
226
|
+
let forcedOnWindows = false;
|
|
161
227
|
if (process.platform === "win32") {
|
|
162
228
|
try {
|
|
163
229
|
const taskkill = Bun.spawn(
|
|
164
230
|
["taskkill.exe", "/PID", String(processHandle.pid), "/T", "/F"],
|
|
165
231
|
{ stderr: "ignore", stdin: "ignore", stdout: "ignore" },
|
|
166
232
|
);
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
233
|
+
forcedOnWindows =
|
|
234
|
+
(await mcpDeadline<number | undefined>(
|
|
235
|
+
taskkill.exited,
|
|
236
|
+
500,
|
|
237
|
+
() => undefined,
|
|
238
|
+
)) === 0;
|
|
172
239
|
if (taskkill.exitCode === null) taskkill.kill();
|
|
173
240
|
} catch {
|
|
174
|
-
processHandle.
|
|
241
|
+
if (processHandle.exitCode === null) {
|
|
242
|
+
processHandle.kill();
|
|
243
|
+
forcedOnWindows = true;
|
|
244
|
+
}
|
|
175
245
|
}
|
|
176
246
|
} else {
|
|
177
247
|
try {
|
|
@@ -194,11 +264,18 @@ async function terminateMcpProcess(
|
|
|
194
264
|
}
|
|
195
265
|
} else processHandle.kill("SIGKILL");
|
|
196
266
|
}
|
|
197
|
-
await mcpDeadline
|
|
198
|
-
processHandle.exited,
|
|
267
|
+
const exited = await mcpDeadline(
|
|
268
|
+
processHandle.exited.then(() => true),
|
|
199
269
|
500,
|
|
200
|
-
() =>
|
|
270
|
+
() => false,
|
|
201
271
|
);
|
|
272
|
+
return {
|
|
273
|
+
exited,
|
|
274
|
+
forced:
|
|
275
|
+
forcedOnWindows ||
|
|
276
|
+
processHandle.signalCode === "SIGTERM" ||
|
|
277
|
+
processHandle.signalCode === "SIGKILL",
|
|
278
|
+
};
|
|
202
279
|
}
|
|
203
280
|
|
|
204
281
|
export async function smokeMcpStdio(
|
|
@@ -239,33 +316,66 @@ export async function smokeMcpStdio(
|
|
|
239
316
|
})().catch(() => "");
|
|
240
317
|
const deadline = Date.now() + timeoutMs;
|
|
241
318
|
const responses = new Map<number, Record<string, unknown>>();
|
|
319
|
+
const requestIds = new Set<number>();
|
|
242
320
|
const reader = process.stdout.getReader();
|
|
243
321
|
const decoder = new TextDecoder();
|
|
244
322
|
let buffered = "";
|
|
245
323
|
const send = async (message: Record<string, unknown>) => {
|
|
246
|
-
|
|
247
|
-
|
|
324
|
+
try {
|
|
325
|
+
if (typeof message.id === "number") requestIds.add(message.id);
|
|
326
|
+
process.stdin.write(`${JSON.stringify(message)}\n`);
|
|
327
|
+
await mcpDeadline(
|
|
328
|
+
Promise.resolve(process.stdin.flush()),
|
|
329
|
+
Math.max(1, deadline - Date.now()),
|
|
330
|
+
() => {
|
|
331
|
+
throw new McpSmokeFailure("timeout", "MCP smoke timed out");
|
|
332
|
+
},
|
|
333
|
+
);
|
|
334
|
+
} catch (error) {
|
|
335
|
+
if (error instanceof McpSmokeFailure) throw error;
|
|
336
|
+
throw new McpSmokeFailure("transport", "MCP smoke write failed");
|
|
337
|
+
}
|
|
248
338
|
};
|
|
249
339
|
|
|
250
340
|
const readUntil = async (id: number): Promise<Record<string, unknown>> => {
|
|
251
341
|
while (!responses.has(id)) {
|
|
252
342
|
const remaining = deadline - Date.now();
|
|
253
|
-
if (remaining <= 0)
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
343
|
+
if (remaining <= 0)
|
|
344
|
+
throw new McpSmokeFailure("timeout", "MCP smoke timed out");
|
|
345
|
+
let chunk: Awaited<ReturnType<typeof reader.read>>;
|
|
346
|
+
try {
|
|
347
|
+
chunk = await mcpDeadline(reader.read(), remaining, () => {
|
|
348
|
+
throw new McpSmokeFailure("timeout", "MCP smoke timed out");
|
|
349
|
+
});
|
|
350
|
+
} catch (error) {
|
|
351
|
+
if (error instanceof McpSmokeFailure) throw error;
|
|
352
|
+
throw new McpSmokeFailure("transport", "MCP smoke read failed");
|
|
353
|
+
}
|
|
354
|
+
if (chunk.done)
|
|
355
|
+
throw new McpSmokeFailure(
|
|
356
|
+
"transport",
|
|
357
|
+
"MCP smoke ended before responding",
|
|
358
|
+
);
|
|
258
359
|
buffered += decoder.decode(chunk.value, { stream: true });
|
|
259
360
|
const lines = buffered.split("\n");
|
|
260
361
|
buffered = lines.pop() ?? "";
|
|
261
362
|
for (const line of lines) {
|
|
262
363
|
if (!line.trim()) continue;
|
|
263
|
-
const value =
|
|
264
|
-
if (
|
|
364
|
+
const value = mcpResponse(line);
|
|
365
|
+
if (value) {
|
|
366
|
+
const responseId = value.id as number;
|
|
367
|
+
if (!requestIds.has(responseId) || responses.has(responseId))
|
|
368
|
+
throw new McpSmokeFailure(
|
|
369
|
+
"transport",
|
|
370
|
+
"MCP smoke received an uncorrelated protocol response",
|
|
371
|
+
);
|
|
372
|
+
responses.set(responseId, value);
|
|
373
|
+
}
|
|
265
374
|
}
|
|
266
375
|
}
|
|
267
376
|
return responses.get(id) as Record<string, unknown>;
|
|
268
377
|
};
|
|
378
|
+
let optionalDiagnosticsTimedOut = false;
|
|
269
379
|
let protocolFailed = false;
|
|
270
380
|
let protocolError: unknown;
|
|
271
381
|
let smokeResult!: McpSmokeResult;
|
|
@@ -336,7 +446,64 @@ export async function smokeMcpStdio(
|
|
|
336
446
|
sameNativePath(openedWorkspace.canonicalRootAnchor, expectedRoot);
|
|
337
447
|
if (!selectedWorkspace)
|
|
338
448
|
throw new Error("MCP smoke did not select the requested workspace");
|
|
339
|
-
|
|
449
|
+
const serverVersion = (
|
|
450
|
+
initialize.result as { serverInfo?: { version?: unknown } }
|
|
451
|
+
).serverInfo?.version;
|
|
452
|
+
smokeResult = {
|
|
453
|
+
exposedTools,
|
|
454
|
+
initialized: true,
|
|
455
|
+
selectedWorkspace,
|
|
456
|
+
serverVersion:
|
|
457
|
+
typeof serverVersion === "string" &&
|
|
458
|
+
compareVersions(serverVersion, serverVersion) !== undefined
|
|
459
|
+
? serverVersion
|
|
460
|
+
: undefined,
|
|
461
|
+
};
|
|
462
|
+
if (exposedTools.includes("diagnostics")) {
|
|
463
|
+
try {
|
|
464
|
+
await send({
|
|
465
|
+
id: 4,
|
|
466
|
+
jsonrpc: "2.0",
|
|
467
|
+
method: "tools/call",
|
|
468
|
+
params: { name: "diagnostics", arguments: {} },
|
|
469
|
+
});
|
|
470
|
+
const diagnostic = await readUntil(4);
|
|
471
|
+
const content = diagnostic.result as
|
|
472
|
+
| {
|
|
473
|
+
isError?: boolean;
|
|
474
|
+
structuredContent?: {
|
|
475
|
+
ok?: boolean;
|
|
476
|
+
data?: {
|
|
477
|
+
server?: { version?: unknown };
|
|
478
|
+
versionCheck?: unknown;
|
|
479
|
+
};
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
| undefined;
|
|
483
|
+
const data = content?.structuredContent?.data;
|
|
484
|
+
if (
|
|
485
|
+
!diagnostic.error &&
|
|
486
|
+
!content?.isError &&
|
|
487
|
+
content?.structuredContent?.ok === true &&
|
|
488
|
+
isLatestVersionCheck(data?.versionCheck) &&
|
|
489
|
+
typeof data?.server?.version === "string" &&
|
|
490
|
+
compareVersions(data.server.version, data.server.version) !==
|
|
491
|
+
undefined &&
|
|
492
|
+
(smokeResult.serverVersion === undefined ||
|
|
493
|
+
smokeResult.serverVersion === data.server.version) &&
|
|
494
|
+
data.versionCheck.runningVersion === data.server.version
|
|
495
|
+
)
|
|
496
|
+
smokeResult.diagnostics = {
|
|
497
|
+
serverVersion: data.server.version,
|
|
498
|
+
versionCheck: data.versionCheck,
|
|
499
|
+
};
|
|
500
|
+
} catch (error) {
|
|
501
|
+
if (!(error instanceof McpSmokeFailure) || error.kind !== "timeout")
|
|
502
|
+
throw error;
|
|
503
|
+
// A bounded optional timeout does not invalidate the completed core smoke.
|
|
504
|
+
optionalDiagnosticsTimedOut = true;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
340
507
|
} catch (error) {
|
|
341
508
|
protocolFailed = true;
|
|
342
509
|
protocolError = error;
|
|
@@ -349,8 +516,17 @@ export async function smokeMcpStdio(
|
|
|
349
516
|
() => null,
|
|
350
517
|
);
|
|
351
518
|
if (exitCode === null) {
|
|
352
|
-
|
|
353
|
-
|
|
519
|
+
const termination = await terminateMcpProcess(process);
|
|
520
|
+
if (!termination.exited)
|
|
521
|
+
shutdownError = new Error("MCP smoke process did not terminate");
|
|
522
|
+
else if (!(optionalDiagnosticsTimedOut && termination.forced)) {
|
|
523
|
+
const stoppedCode = await process.exited;
|
|
524
|
+
shutdownError = new Error(
|
|
525
|
+
termination.forced || stoppedCode === 0
|
|
526
|
+
? "MCP smoke shutdown exceeded five seconds"
|
|
527
|
+
: `MCP smoke exited with code ${stoppedCode}`,
|
|
528
|
+
);
|
|
529
|
+
}
|
|
354
530
|
} else if (exitCode !== 0) {
|
|
355
531
|
const diagnostic = (await mcpDeadline(stderrDrain, 500, () => ""))
|
|
356
532
|
.slice(-8_192)
|
|
@@ -770,7 +946,7 @@ async function codexChecks(
|
|
|
770
946
|
global: boolean,
|
|
771
947
|
): Promise<InstallChecks> {
|
|
772
948
|
const base = global
|
|
773
|
-
? path.join(home, ".codex")
|
|
949
|
+
? path.resolve(process.env.CODEX_HOME || path.join(home, ".codex"))
|
|
774
950
|
: path.join(options.root, ".codex");
|
|
775
951
|
const [mcp, hook, instructions] = await Promise.all([
|
|
776
952
|
codexMcpCurrent(
|
|
@@ -887,7 +1063,7 @@ async function configuredStdioCommand(
|
|
|
887
1063
|
let entry: McpEntry | undefined;
|
|
888
1064
|
if (options.target === "codex") {
|
|
889
1065
|
const base = global
|
|
890
|
-
? path.join(home, ".codex")
|
|
1066
|
+
? path.resolve(process.env.CODEX_HOME || path.join(home, ".codex"))
|
|
891
1067
|
: path.join(options.root, ".codex");
|
|
892
1068
|
const content = await readFile(
|
|
893
1069
|
path.join(base, "config.toml"),
|
|
@@ -1049,6 +1225,7 @@ function installCommands(options: CheckOptions, global: boolean) {
|
|
|
1049
1225
|
export async function checkInstall(
|
|
1050
1226
|
args = process.argv.slice(2),
|
|
1051
1227
|
home = os.homedir(),
|
|
1228
|
+
context: { fetch?: typeof fetch } = {},
|
|
1052
1229
|
) {
|
|
1053
1230
|
const options = parse(args);
|
|
1054
1231
|
const global = options.scope === "global";
|
|
@@ -1082,8 +1259,30 @@ export async function checkInstall(
|
|
|
1082
1259
|
const installed = Object.values(checks).every(Boolean);
|
|
1083
1260
|
const operation = installOperation(checks);
|
|
1084
1261
|
const commands = installCommands(options, global);
|
|
1262
|
+
const versionCheck =
|
|
1263
|
+
smoke?.diagnostics?.versionCheck ??
|
|
1264
|
+
(await checkLatestVersion(smoke?.serverVersion ?? "unknown", {
|
|
1265
|
+
fetch: context.fetch,
|
|
1266
|
+
}));
|
|
1085
1267
|
return {
|
|
1086
1268
|
checks,
|
|
1269
|
+
diagnostics: {
|
|
1270
|
+
versionCheck,
|
|
1271
|
+
connectedServerVersion:
|
|
1272
|
+
smoke?.diagnostics?.serverVersion ?? smoke?.serverVersion ?? "unknown",
|
|
1273
|
+
activeHostPolicy: "unknown",
|
|
1274
|
+
guidance:
|
|
1275
|
+
options.target === "codex"
|
|
1276
|
+
? [
|
|
1277
|
+
"Disk MCP settings do not establish the live session approval policy.",
|
|
1278
|
+
"Codex Desktop Full access can override granular disk settings with approval_policy=never.",
|
|
1279
|
+
"Per-tool approval does not authorize the separate AST-MCP filesystem challenge.",
|
|
1280
|
+
"Run ast-mcp doctor --target codex and select an elicitation-capable session policy.",
|
|
1281
|
+
]
|
|
1282
|
+
: [
|
|
1283
|
+
"Run live MCP diagnostics to inspect the connected client and approval capabilities.",
|
|
1284
|
+
],
|
|
1285
|
+
},
|
|
1087
1286
|
intelligence: {
|
|
1088
1287
|
engine: "lancedb",
|
|
1089
1288
|
federationDefault: false,
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
const LATEST_URL = "https://registry.npmjs.org/@mwillbanks%2Fast-mcp/latest";
|
|
2
|
+
const VERSION_PATTERN =
|
|
3
|
+
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
4
|
+
|
|
5
|
+
function parsedVersion(value: string) {
|
|
6
|
+
if (value.length > 128) return undefined;
|
|
7
|
+
const matched = VERSION_PATTERN.exec(value);
|
|
8
|
+
if (!matched) return undefined;
|
|
9
|
+
const pre = matched[4]?.split(".");
|
|
10
|
+
if (
|
|
11
|
+
pre?.some(
|
|
12
|
+
(part) => /^\d+$/.test(part) && part.length > 1 && part.startsWith("0"),
|
|
13
|
+
)
|
|
14
|
+
)
|
|
15
|
+
return undefined;
|
|
16
|
+
return { core: matched.slice(1, 4).map(BigInt), pre };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function compareVersions(
|
|
20
|
+
first: string,
|
|
21
|
+
second: string,
|
|
22
|
+
): number | undefined {
|
|
23
|
+
const a = parsedVersion(first);
|
|
24
|
+
const b = parsedVersion(second);
|
|
25
|
+
if (!a || !b) return undefined;
|
|
26
|
+
for (let index = 0; index < 3; index++) {
|
|
27
|
+
if (a.core[index]! < b.core[index]!) return -1;
|
|
28
|
+
if (a.core[index]! > b.core[index]!) return 1;
|
|
29
|
+
}
|
|
30
|
+
if (!a.pre && !b.pre) return 0;
|
|
31
|
+
if (!a.pre) return 1;
|
|
32
|
+
if (!b.pre) return -1;
|
|
33
|
+
for (let index = 0; index < Math.max(a.pre.length, b.pre.length); index++) {
|
|
34
|
+
const left = a.pre[index];
|
|
35
|
+
const right = b.pre[index];
|
|
36
|
+
if (left === undefined) return -1;
|
|
37
|
+
if (right === undefined) return 1;
|
|
38
|
+
if (left === right) continue;
|
|
39
|
+
const leftNumber = /^\d+$/.test(left);
|
|
40
|
+
const rightNumber = /^\d+$/.test(right);
|
|
41
|
+
if (leftNumber && rightNumber) return BigInt(left) < BigInt(right) ? -1 : 1;
|
|
42
|
+
if (leftNumber !== rightNumber) return leftNumber ? -1 : 1;
|
|
43
|
+
return left < right ? -1 : 1;
|
|
44
|
+
}
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface LatestVersionCheck {
|
|
49
|
+
checkedAt: string;
|
|
50
|
+
latestVersion: string | null;
|
|
51
|
+
runningVersion: string;
|
|
52
|
+
status: "current" | "outdated" | "newer" | "prerelease" | "unavailable";
|
|
53
|
+
comparison?: "older" | "equal" | "newer";
|
|
54
|
+
reason?:
|
|
55
|
+
| "timeout"
|
|
56
|
+
| "registry_unavailable"
|
|
57
|
+
| "invalid_registry_response"
|
|
58
|
+
| "invalid_running_version";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Validates a complete release report, including status-specific invariants. */
|
|
62
|
+
export function isLatestVersionCheck(
|
|
63
|
+
value: unknown,
|
|
64
|
+
): value is LatestVersionCheck {
|
|
65
|
+
if (!value || typeof value !== "object") return false;
|
|
66
|
+
const check = value as Record<string, unknown>;
|
|
67
|
+
if (
|
|
68
|
+
typeof check.checkedAt !== "string" ||
|
|
69
|
+
!Number.isFinite(Date.parse(check.checkedAt)) ||
|
|
70
|
+
typeof check.runningVersion !== "string"
|
|
71
|
+
)
|
|
72
|
+
return false;
|
|
73
|
+
const running = parsedVersion(check.runningVersion);
|
|
74
|
+
const latest =
|
|
75
|
+
typeof check.latestVersion === "string"
|
|
76
|
+
? parsedVersion(check.latestVersion)
|
|
77
|
+
: undefined;
|
|
78
|
+
if (check.status === "unavailable") {
|
|
79
|
+
if (check.comparison !== undefined) return false;
|
|
80
|
+
if (check.reason === "invalid_running_version")
|
|
81
|
+
return !running && latest !== undefined && !latest.pre;
|
|
82
|
+
return (
|
|
83
|
+
typeof check.reason === "string" &&
|
|
84
|
+
["timeout", "registry_unavailable", "invalid_registry_response"].includes(
|
|
85
|
+
check.reason,
|
|
86
|
+
) &&
|
|
87
|
+
check.latestVersion === null
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (!running || !latest || latest.pre || check.reason !== undefined)
|
|
91
|
+
return false;
|
|
92
|
+
const compared = compareVersions(
|
|
93
|
+
check.runningVersion,
|
|
94
|
+
check.latestVersion as string,
|
|
95
|
+
)!;
|
|
96
|
+
const comparison = compared < 0 ? "older" : compared > 0 ? "newer" : "equal";
|
|
97
|
+
const status = running.pre
|
|
98
|
+
? "prerelease"
|
|
99
|
+
: compared < 0
|
|
100
|
+
? "outdated"
|
|
101
|
+
: compared > 0
|
|
102
|
+
? "newer"
|
|
103
|
+
: "current";
|
|
104
|
+
return check.comparison === comparison && check.status === status;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function boundedRegistryVersion(
|
|
108
|
+
response: Response,
|
|
109
|
+
signal: AbortSignal,
|
|
110
|
+
): Promise<string> {
|
|
111
|
+
if (!response.ok) throw new Error("registry_unavailable");
|
|
112
|
+
if (Number(response.headers.get("content-length")) > 16_384 || !response.body)
|
|
113
|
+
throw new Error("invalid_registry_response");
|
|
114
|
+
const reader = response.body.getReader();
|
|
115
|
+
const abort = () => {
|
|
116
|
+
void reader.cancel().catch(() => {});
|
|
117
|
+
};
|
|
118
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
119
|
+
if (signal.aborted) abort();
|
|
120
|
+
const chunks: Uint8Array[] = [];
|
|
121
|
+
let size = 0;
|
|
122
|
+
try {
|
|
123
|
+
while (true) {
|
|
124
|
+
const chunk = await reader.read();
|
|
125
|
+
if (chunk.done) break;
|
|
126
|
+
size += chunk.value.byteLength;
|
|
127
|
+
if (size > 16_384) throw new Error("invalid_registry_response");
|
|
128
|
+
chunks.push(chunk.value);
|
|
129
|
+
}
|
|
130
|
+
} finally {
|
|
131
|
+
signal.removeEventListener("abort", abort);
|
|
132
|
+
void reader.cancel().catch(() => {});
|
|
133
|
+
reader.releaseLock();
|
|
134
|
+
}
|
|
135
|
+
const bytes = new Uint8Array(size);
|
|
136
|
+
let offset = 0;
|
|
137
|
+
for (const chunk of chunks) {
|
|
138
|
+
bytes.set(chunk, offset);
|
|
139
|
+
offset += chunk.byteLength;
|
|
140
|
+
}
|
|
141
|
+
let value: unknown;
|
|
142
|
+
try {
|
|
143
|
+
value = JSON.parse(new TextDecoder().decode(bytes));
|
|
144
|
+
} catch {
|
|
145
|
+
throw new Error("invalid_registry_response");
|
|
146
|
+
}
|
|
147
|
+
const version = (value as { version?: unknown } | null)?.version;
|
|
148
|
+
if (
|
|
149
|
+
typeof version !== "string" ||
|
|
150
|
+
!parsedVersion(version) ||
|
|
151
|
+
parsedVersion(version)!.pre
|
|
152
|
+
)
|
|
153
|
+
throw new Error("invalid_registry_response");
|
|
154
|
+
return version;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Always performs a fresh unauthenticated public-registry check. */
|
|
158
|
+
export async function checkLatestVersion(
|
|
159
|
+
runningVersion: string,
|
|
160
|
+
options: { fetch?: typeof fetch; timeoutMs?: number } = {},
|
|
161
|
+
): Promise<LatestVersionCheck> {
|
|
162
|
+
const result: LatestVersionCheck = {
|
|
163
|
+
checkedAt: new Date().toISOString(),
|
|
164
|
+
latestVersion: null,
|
|
165
|
+
runningVersion,
|
|
166
|
+
status: "unavailable",
|
|
167
|
+
};
|
|
168
|
+
const controller = new AbortController();
|
|
169
|
+
const timeoutMs = Math.max(1, Math.min(options.timeoutMs ?? 5000, 5000));
|
|
170
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
171
|
+
let timedOut = false;
|
|
172
|
+
try {
|
|
173
|
+
const latest = await Promise.race([
|
|
174
|
+
Promise.resolve().then(async () =>
|
|
175
|
+
boundedRegistryVersion(
|
|
176
|
+
await (options.fetch ?? globalThis.fetch)(LATEST_URL, {
|
|
177
|
+
credentials: "omit",
|
|
178
|
+
headers: { accept: "application/json" },
|
|
179
|
+
redirect: "error",
|
|
180
|
+
signal: controller.signal,
|
|
181
|
+
}),
|
|
182
|
+
controller.signal,
|
|
183
|
+
),
|
|
184
|
+
),
|
|
185
|
+
new Promise<never>((_, reject) => {
|
|
186
|
+
timer = setTimeout(() => {
|
|
187
|
+
timedOut = true;
|
|
188
|
+
controller.abort();
|
|
189
|
+
reject(new Error("timeout"));
|
|
190
|
+
}, timeoutMs);
|
|
191
|
+
}),
|
|
192
|
+
]);
|
|
193
|
+
result.latestVersion = latest;
|
|
194
|
+
const comparison = compareVersions(runningVersion, latest);
|
|
195
|
+
if (comparison === undefined) {
|
|
196
|
+
result.reason = "invalid_running_version";
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
result.comparison =
|
|
200
|
+
comparison < 0 ? "older" : comparison > 0 ? "newer" : "equal";
|
|
201
|
+
result.status = parsedVersion(runningVersion)?.pre
|
|
202
|
+
? "prerelease"
|
|
203
|
+
: comparison < 0
|
|
204
|
+
? "outdated"
|
|
205
|
+
: comparison > 0
|
|
206
|
+
? "newer"
|
|
207
|
+
: "current";
|
|
208
|
+
return result;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
result.reason = timedOut
|
|
211
|
+
? "timeout"
|
|
212
|
+
: error instanceof Error && error.message === "invalid_registry_response"
|
|
213
|
+
? "invalid_registry_response"
|
|
214
|
+
: "registry_unavailable";
|
|
215
|
+
return result;
|
|
216
|
+
} finally {
|
|
217
|
+
clearTimeout(timer);
|
|
218
|
+
controller.abort();
|
|
219
|
+
}
|
|
220
|
+
}
|