[codex] Structure update manifest failures (#3293)

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-06-20 19:21:42 -07:00
1 parent f9cf047b6f
commit 0dd63500b2
3 files changed
+407 -55

No files matched your search

+237 -42
View File
@@ -1,10 +1,138 @@
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
export interface UpdateManifestFile {
readonly url: string;
readonly sha512: string;
readonly size: number;
}
export type UpdateManifestScalar = string | number | boolean;
export const UpdateManifestScalar = Schema.Union([Schema.String, Schema.Number, Schema.Boolean]);
export type UpdateManifestScalar = typeof UpdateManifestScalar.Type;
export const UpdateManifestScalarType = Schema.Literals(["string", "number", "boolean"]);
export type UpdateManifestScalarType = typeof UpdateManifestScalarType.Type;
export const UpdateManifestParseReason = Schema.Literals([
"incomplete file entry",
"sha512 without a file entry",
"size without a file entry",
"unsupported line",
"version must be a string",
"releaseDate must be a string",
"missing version",
"missing releaseDate",
"missing files",
]);
export type UpdateManifestParseReason = typeof UpdateManifestParseReason.Type;
export class UpdateManifestParseError extends Schema.TaggedErrorClass<UpdateManifestParseError>()(
"UpdateManifestParseError",
{
platformLabel: Schema.String,
sourcePath: Schema.String,
lineNumber: Schema.optionalKey(Schema.Number),
reason: UpdateManifestParseReason,
lineLength: Schema.optionalKey(Schema.Number),
},
) {
override get message(): string {
const location =
this.lineNumber === undefined ? this.sourcePath : `${this.sourcePath}:${this.lineNumber}`;
const input = this.lineLength === undefined ? "" : ` Input length: ${this.lineLength}.`;
return `Invalid ${this.platformLabel} update manifest at ${location}: ${this.reason}.${input}`;
}
}
export class UpdateManifestVersionConflictError extends Schema.TaggedErrorClass<UpdateManifestVersionConflictError>()(
"UpdateManifestVersionConflictError",
{
platformLabel: Schema.String,
primaryVersion: Schema.String,
secondaryVersion: Schema.String,
},
) {
override get message(): string {
return `Cannot merge ${this.platformLabel} update manifests with different versions (${this.primaryVersion} vs ${this.secondaryVersion}).`;
}
}
export class UpdateManifestExtraConflictError extends Schema.TaggedErrorClass<UpdateManifestExtraConflictError>()(
"UpdateManifestExtraConflictError",
{
platformLabel: Schema.String,
key: Schema.String,
primaryValueType: UpdateManifestScalarType,
primaryValueLength: Schema.optionalKey(Schema.Number),
secondaryValueType: UpdateManifestScalarType,
secondaryValueLength: Schema.optionalKey(Schema.Number),
},
) {
override get message(): string {
return `Cannot merge ${this.platformLabel} update manifests: conflicting '${this.key}' ${this.primaryValueType} and ${this.secondaryValueType} values.`;
}
}
export class UpdateManifestFileConflictError extends Schema.TaggedErrorClass<UpdateManifestFileConflictError>()(
"UpdateManifestFileConflictError",
{
platformLabel: Schema.String,
urlInputLength: Schema.Number,
urlProtocol: Schema.optionalKey(Schema.String),
urlHostname: Schema.optionalKey(Schema.String),
existingManifest: Schema.Literals(["primary", "secondary"]),
existingSha512Length: Schema.Number,
existingSize: Schema.Number,
conflictingManifest: Schema.Literals(["primary", "secondary"]),
conflictingSha512Length: Schema.Number,
conflictingSize: Schema.Number,
sha512Conflict: Schema.Boolean,
sizeConflict: Schema.Boolean,
},
) {
override get message(): string {
const origin =
this.urlProtocol === undefined || this.urlHostname === undefined
? ""
: ` at ${this.urlProtocol}//${this.urlHostname}`;
return `Cannot merge ${this.platformLabel} update manifests: conflicting file entry${origin} (URL input length: ${this.urlInputLength}).`;
}
}
export class UpdateManifestSerializationError extends Schema.TaggedErrorClass<UpdateManifestSerializationError>()(
"UpdateManifestSerializationError",
{
platformLabel: Schema.String,
key: Schema.String,
},
) {
override get message(): string {
return `Cannot serialize ${this.platformLabel} update manifest: missing value for '${this.key}'.`;
}
}
export const UpdateManifestError = Schema.Union([
UpdateManifestParseError,
UpdateManifestVersionConflictError,
UpdateManifestExtraConflictError,
UpdateManifestFileConflictError,
UpdateManifestSerializationError,
]);
export type UpdateManifestError = typeof UpdateManifestError.Type;
export const isUpdateManifestError = Schema.is(UpdateManifestError);
export const attemptUpdateManifest = <A>(
evaluate: () => A,
): Effect.Effect<A, UpdateManifestError> =>
Effect.suspend(() => {
try {
return Effect.succeed(evaluate());
} catch (error) {
return isUpdateManifestError(error) ? Effect.fail(error) : Effect.die(error);
}
});
export interface UpdateManifest {
readonly version: string;
@@ -40,9 +168,12 @@ function parseFileRecord(
typeof currentFile.sha512 !== "string" ||
typeof currentFile.size !== "number"
) {
throw new Error(
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: incomplete file entry.`,
);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
lineNumber,
reason: "incomplete file entry",
});
}
return {
url: currentFile.url,
@@ -94,9 +225,12 @@ export function parseUpdateManifest(
const fileShaMatch = line.match(/^ sha512:\s*(.+)$/);
if (fileShaMatch?.[1]) {
if (currentFile === null) {
throw new Error(
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: sha512 without a file entry.`,
);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
lineNumber,
reason: "sha512 without a file entry",
});
}
currentFile.sha512 = stripSingleQuotes(fileShaMatch[1].trim());
continue;
@@ -105,9 +239,12 @@ export function parseUpdateManifest(
const fileSizeMatch = line.match(/^ size:\s*(\d+)$/);
if (fileSizeMatch?.[1]) {
if (currentFile === null) {
throw new Error(
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: size without a file entry.`,
);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
lineNumber,
reason: "size without a file entry",
});
}
currentFile.size = Number(fileSizeMatch[1]);
continue;
@@ -127,9 +264,13 @@ export function parseUpdateManifest(
const topLevelMatch = line.match(/^([A-Za-z][A-Za-z0-9]*):\s*(.+)$/);
if (!topLevelMatch?.[1] || topLevelMatch[2] === undefined) {
throw new Error(
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: unsupported line '${line}'.`,
);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
lineNumber,
reason: "unsupported line",
lineLength: line.length,
});
}
const [, key, rawValue] = topLevelMatch;
@@ -137,9 +278,12 @@ export function parseUpdateManifest(
if (key === "version") {
if (typeof value !== "string") {
throw new Error(
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: version must be a string.`,
);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
lineNumber,
reason: "version must be a string",
});
}
version = value;
continue;
@@ -147,9 +291,12 @@ export function parseUpdateManifest(
if (key === "releaseDate") {
if (typeof value !== "string") {
throw new Error(
`Invalid ${platformLabel} update manifest at ${sourcePath}:${lineNumber}: releaseDate must be a string.`,
);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
lineNumber,
reason: "releaseDate must be a string",
});
}
releaseDate = value;
continue;
@@ -166,15 +313,25 @@ export function parseUpdateManifest(
if (finalized) files.push(finalized);
if (!version) {
throw new Error(`Invalid ${platformLabel} update manifest at ${sourcePath}: missing version.`);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
reason: "missing version",
});
}
if (!releaseDate) {
throw new Error(
`Invalid ${platformLabel} update manifest at ${sourcePath}: missing releaseDate.`,
);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
reason: "missing releaseDate",
});
}
if (files.length === 0) {
throw new Error(`Invalid ${platformLabel} update manifest at ${sourcePath}: missing files.`);
throw new UpdateManifestParseError({
platformLabel,
sourcePath,
reason: "missing files",
});
}
return {
@@ -185,6 +342,12 @@ export function parseUpdateManifest(
};
}
function getScalarType(value: UpdateManifestScalar): UpdateManifestScalarType {
if (typeof value === "string") return "string";
if (typeof value === "number") return "number";
return "boolean";
}
function mergeExtras(
primary: Readonly<Record<string, UpdateManifestScalar>>,
secondary: Readonly<Record<string, UpdateManifestScalar>>,
@@ -195,9 +358,14 @@ function mergeExtras(
for (const [key, value] of Object.entries(secondary)) {
const existing = merged[key];
if (existing !== undefined && existing !== value) {
throw new Error(
`Cannot merge ${platformLabel} update manifests: conflicting '${key}' values ('${existing}' vs '${value}').`,
);
throw new UpdateManifestExtraConflictError({
platformLabel,
key,
primaryValueType: getScalarType(existing),
...(typeof existing === "string" ? { primaryValueLength: existing.length } : {}),
secondaryValueType: getScalarType(value),
...(typeof value === "string" ? { secondaryValueLength: value.length } : {}),
});
}
merged[key] = value;
}
@@ -211,27 +379,53 @@ export function mergeUpdateManifests(
platformLabel: string,
): UpdateManifest {
if (primary.version !== secondary.version) {
throw new Error(
`Cannot merge ${platformLabel} update manifests with different versions (${primary.version} vs ${secondary.version}).`,
);
throw new UpdateManifestVersionConflictError({
platformLabel,
primaryVersion: primary.version,
secondaryVersion: secondary.version,
});
}
const filesByUrl = new Map<string, UpdateManifestFile>();
for (const file of [...primary.files, ...secondary.files]) {
const existing = filesByUrl.get(file.url);
if (existing && (existing.sha512 !== file.sha512 || existing.size !== file.size)) {
throw new Error(
`Cannot merge ${platformLabel} update manifests: conflicting file entry for ${file.url}.`,
);
const filesByUrl = new Map<
string,
{ readonly manifest: "primary" | "secondary"; readonly file: UpdateManifestFile }
>();
for (const [manifest, files] of [
["primary", primary.files],
["secondary", secondary.files],
] as const) {
for (const file of files) {
const existing = filesByUrl.get(file.url);
if (existing && (existing.file.sha512 !== file.sha512 || existing.file.size !== file.size)) {
const urlDiagnostics = getUrlDiagnostics(file.url);
throw new UpdateManifestFileConflictError({
platformLabel,
urlInputLength: urlDiagnostics.inputLength,
...(urlDiagnostics.protocol === undefined
? {}
: { urlProtocol: urlDiagnostics.protocol }),
...(urlDiagnostics.hostname === undefined
? {}
: { urlHostname: urlDiagnostics.hostname }),
existingManifest: existing.manifest,
existingSha512Length: existing.file.sha512.length,
existingSize: existing.file.size,
conflictingManifest: manifest,
conflictingSha512Length: file.sha512.length,
conflictingSize: file.size,
sha512Conflict: existing.file.sha512 !== file.sha512,
sizeConflict: existing.file.size !== file.size,
});
}
filesByUrl.set(file.url, { manifest, file });
}
filesByUrl.set(file.url, file);
}
return {
version: primary.version,
releaseDate:
primary.releaseDate >= secondary.releaseDate ? primary.releaseDate : secondary.releaseDate,
files: [...filesByUrl.values()],
files: [...filesByUrl.values()].map(({ file }) => file),
extras: mergeExtras(primary.extras, secondary.extras, platformLabel),
};
}
@@ -264,9 +458,10 @@ export function serializeUpdateManifest(
for (const key of Object.keys(manifest.extras).toSorted()) {
const value = manifest.extras[key];
if (value === undefined) {
throw new Error(
`Cannot serialize ${options.platformLabel} update manifest: missing value for '${key}'.`,
);
throw new UpdateManifestSerializationError({
platformLabel: options.platformLabel,
key,
});
}
lines.push(`${key}: ${serializeScalarValue(value)}`);
}
+156 -3
View File
@@ -6,14 +6,27 @@ import * as Path from "effect/Path";
import { Command, CliError } from "effect/unstable/cli";
import {
mergeUpdateManifestFiles,
mergePlatformUpdateManifests,
mergeUpdateManifestsCommand,
parsePlatformUpdateManifest,
serializePlatformUpdateManifest,
} from "./merge-update-manifests.ts";
import { isUpdateManifestError, type UpdateManifestError } from "./lib/update-manifest.ts";
const runCli = Command.runWith(mergeUpdateManifestsCommand, { version: "0.0.0" });
function captureUpdateManifestError(run: () => unknown): UpdateManifestError {
let caught: unknown;
try {
run();
} catch (error) {
caught = error;
}
assert.ok(isUpdateManifestError(caught));
return caught;
}
describe("merge-update-manifests", () => {
it("merges arm64 and x64 macOS update manifests into one multi-arch manifest", () => {
const arm64 = parsePlatformUpdateManifest(
@@ -148,10 +161,121 @@ releaseDate: '2026-03-07T10:36:07.540Z'
"latest-win-x64.yml",
);
assert.throws(
() => mergePlatformUpdateManifests("win", primary, secondary),
/different versions/,
const error = captureUpdateManifestError(() =>
mergePlatformUpdateManifests("win", primary, secondary),
);
assert.equal(error._tag, "UpdateManifestVersionConflictError");
if (error._tag === "UpdateManifestVersionConflictError") {
assert.equal(error.platformLabel, "Windows");
assert.equal(error.primaryVersion, "0.0.4");
assert.equal(error.secondaryVersion, "0.0.5");
}
});
it("reports manifest parse location without retaining the unsupported input", () => {
const unsupportedLine = "authorization Bearer secret-token without-separator";
const error = captureUpdateManifestError(() =>
parsePlatformUpdateManifest("mac", unsupportedLine, "latest-mac.yml"),
);
assert.equal(error._tag, "UpdateManifestParseError");
if (error._tag === "UpdateManifestParseError") {
assert.equal(error.platformLabel, "macOS");
assert.equal(error.sourcePath, "latest-mac.yml");
assert.equal(error.lineNumber, 1);
assert.equal(error.reason, "unsupported line");
assert.equal(error.lineLength, unsupportedLine.length);
assert.notProperty(error, "offendingLine");
assert.equal(
error.message,
`Invalid macOS update manifest at latest-mac.yml:1: unsupported line. Input length: ${unsupportedLine.length}.`,
);
assert.notInclude(error.message, "Bearer");
assert.notInclude(error.message, "secret-token");
}
});
it("identifies both sources when duplicate primary file entries conflict", () => {
const privateUrl =
"https://user:[email protected]/releases/private/app.exe?token=secret-token#fragment";
const firstSha512 = "first-private-sha";
const secondSha512 = "second-private-sha";
const manifest = {
version: "1.0.0",
releaseDate: "2026-06-20T00:00:00.000Z",
extras: {},
};
const error = captureUpdateManifestError(() =>
mergePlatformUpdateManifests(
"win",
{
...manifest,
files: [
{ url: privateUrl, sha512: firstSha512, size: 1 },
{ url: privateUrl, sha512: secondSha512, size: 2 },
],
},
{ ...manifest, files: [] },
),
);
assert.equal(error._tag, "UpdateManifestFileConflictError");
if (error._tag === "UpdateManifestFileConflictError") {
assert.equal(error.existingManifest, "primary");
assert.equal(error.conflictingManifest, "primary");
assert.equal(error.urlInputLength, privateUrl.length);
assert.equal(error.urlProtocol, "https:");
assert.equal(error.urlHostname, "example.test");
assert.equal(error.existingSha512Length, firstSha512.length);
assert.equal(error.existingSize, 1);
assert.equal(error.conflictingSha512Length, secondSha512.length);
assert.equal(error.conflictingSize, 2);
assert.isTrue(error.sha512Conflict);
assert.isTrue(error.sizeConflict);
assert.notProperty(error, "url");
assert.notProperty(error, "existingSha512");
assert.notProperty(error, "conflictingSha512");
const diagnostics = [error.message, ...Object.values(error).map(String)].join("\n");
assert.notInclude(diagnostics, "user");
assert.notInclude(diagnostics, "password");
assert.notInclude(diagnostics, "/releases/private/app.exe");
assert.notInclude(diagnostics, "secret-token");
assert.notInclude(diagnostics, firstSha512);
assert.notInclude(diagnostics, secondSha512);
}
});
it("reports extra conflicts without retaining arbitrary scalar values", () => {
const primaryValue = "primary-private-value";
const secondaryValue = "secondary-private-value";
const manifest = {
version: "1.0.0",
releaseDate: "2026-06-20T00:00:00.000Z",
files: [{ url: "app.exe", sha512: "sha", size: 1 }],
};
const error = captureUpdateManifestError(() =>
mergePlatformUpdateManifests(
"win",
{ ...manifest, extras: { releaseNotes: primaryValue } },
{ ...manifest, extras: { releaseNotes: secondaryValue } },
),
);
assert.equal(error._tag, "UpdateManifestExtraConflictError");
if (error._tag === "UpdateManifestExtraConflictError") {
assert.equal(error.key, "releaseNotes");
assert.equal(error.primaryValueType, "string");
assert.equal(error.primaryValueLength, primaryValue.length);
assert.equal(error.secondaryValueType, "string");
assert.equal(error.secondaryValueLength, secondaryValue.length);
assert.notProperty(error, "primaryValue");
assert.notProperty(error, "secondaryValue");
const diagnostics = [error.message, ...Object.values(error).map(String)].join("\n");
assert.notInclude(diagnostics, primaryValue);
assert.notInclude(diagnostics, secondaryValue);
}
});
it("preserves quoted scalars as strings", () => {
@@ -286,6 +410,35 @@ releaseDate: '2026-03-07T10:36:07.540Z'
}),
);
it.effect("surfaces manifest validation as a typed failure", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const baseDir = yield* fs.makeTempDirectoryScoped({
prefix: "merge-update-manifests-invalid-",
});
const primaryPath = path.join(baseDir, "latest-mac.yml");
const secondaryPath = path.join(baseDir, "latest-mac-x64.yml");
yield* fs.writeFileString(primaryPath, "not yaml");
yield* fs.writeFileString(secondaryPath, arm64MacManifest);
const error = yield* mergeUpdateManifestFiles(
"mac",
primaryPath,
secondaryPath,
undefined,
).pipe(Effect.flip);
assert.ok(isUpdateManifestError(error));
assert.equal(error._tag, "UpdateManifestParseError");
if (error._tag === "UpdateManifestParseError") {
assert.equal(error.sourcePath, primaryPath);
assert.equal(error.reason, "unsupported line");
}
}),
);
it.effect("rejects invalid platform values during cli parsing", () =>
Effect.gen(function* () {
const error = yield* runCli(["--platform", "linux", "a.yml", "b.yml"]).pipe(Effect.flip);
+14 -10
View File
@@ -10,6 +10,7 @@ import * as Schema from "effect/Schema";
import { Argument, Command, Flag } from "effect/unstable/cli";
import {
attemptUpdateManifest,
mergeUpdateManifests,
parseUpdateManifest,
serializeUpdateManifest,
@@ -61,19 +62,22 @@ export const mergeUpdateManifestFiles = Effect.fn("mergeUpdateManifestFiles")(fu
const secondaryPath = path.resolve(secondaryPathArg);
const outputPath = path.resolve(outputPathArg ?? primaryPathArg);
const primaryManifest = parsePlatformUpdateManifest(
platform,
yield* fs.readFileString(primaryPath),
primaryPath,
const primaryRaw = yield* fs.readFileString(primaryPath);
const primaryManifest = yield* attemptUpdateManifest(() =>
parsePlatformUpdateManifest(platform, primaryRaw, primaryPath),
);
const secondaryManifest = parsePlatformUpdateManifest(
platform,
yield* fs.readFileString(secondaryPath),
secondaryPath,
const secondaryRaw = yield* fs.readFileString(secondaryPath);
const secondaryManifest = yield* attemptUpdateManifest(() =>
parsePlatformUpdateManifest(platform, secondaryRaw, secondaryPath),
);
const merged = yield* attemptUpdateManifest(() =>
mergePlatformUpdateManifests(platform, primaryManifest, secondaryManifest),
);
const merged = mergePlatformUpdateManifests(platform, primaryManifest, secondaryManifest);
yield* fs.writeFileString(outputPath, serializePlatformUpdateManifest(platform, merged));
const serialized = yield* attemptUpdateManifest(() =>
serializePlatformUpdateManifest(platform, merged),
);
yield* fs.writeFileString(outputPath, serialized);
});
export const mergeUpdateManifestsCommand = Command.make(