fix(binary): keep an imported snapshot when the first open fails

A failed first open whose client cleanup also failed cleared a staged
import even though no target had been retired. Clearing now waits until
this open has retired a previous target. A later open still retries that
cleanup before another profile can start.
This commit is contained in:
Adolanium authored and Ysopwnable committed 2026-11-21 20:06:57 +01:00
1 parent 667a0054d6
commit a963184282
2 files changed
+171 -2

No files matched your search

+4 -2
View File
@@ -1224,7 +1224,9 @@ export class BinarySession
onCleanupFailure === undefined ? {} : { onCleanupFailure },
);
if (!closed.ok) {
this.#clearUnrestoredSnapshot();
// A fresh session can hold an imported snapshot and no retired target.
// Clear only a binding left by a target this open already retired.
if (previous !== undefined) this.#clearUnrestoredSnapshot();
return analysisErrorWithCloseRejection(
primary,
closed.error,
@@ -1726,7 +1728,7 @@ export class BinarySession
}
}
/** No active target remains, so the snapshot cache must not keep the retired binding. */
/** The retired target did not return, so its snapshot binding must not block the next open. */
#clearUnrestoredSnapshot(): void {
if (this.#active !== undefined) return;
this.clearSnapshot();
@@ -10,6 +10,13 @@ import {
} from "../../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
import { createAnalysisProfile } from "../../domain/analysisProfile.js";
import {
analysisSnapshotSchema,
snapshotBinding,
snapshotTarget,
} from "../../domain/analysisSnapshot.js";
import { createEvidenceBundle } from "../../domain/evidenceBundle.js";
import { AnalysisSnapshotMismatchError } from "../../domain/evidenceErrors.js";
import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
import { err, ok } from "../../domain/result.js";
import { createAnalysisExecution } from "../../ports/AnalysisProvider.js";
@@ -378,3 +385,163 @@ it("opens another target after a failed switch cannot restore the previous one",
await session.close();
}
});
it("keeps an imported snapshot when the first open fails and its cleanup fails", async () => {
const session = new BinarySession(
SessionProviderRouter.selectable(new AnalysisProviderRegistry([]), []),
);
const identity = {
id: "restoration-test",
name: "Restoration test",
version: "1",
};
const profile = createAnalysisProfile(identity, { fixture: "imported" });
const cleanupFailure = new ProviderCleanupError(
identity.id,
["first-client"],
{ reason: "close failed" },
);
let closes = 0;
const route: SessionProviderRoute = {
identity,
capabilities: new Map(),
profile,
binding: null,
selection: undefined,
createClient: () => ({
execute: async () => err(new AnalysisOutputError("health", "Open failed")),
close: async () => {
closes += 1;
if (closes === 1) return err(cleanupFailure);
releaseOwnedCleanup(cleanupFailure);
return ok(null);
},
}),
};
try {
const target = await session.resolveTarget(process.execPath);
expect(target.ok).toBe(true);
if (!target.ok) return;
expect(
session.importAnalysisSnapshot(
analysisSnapshotSchema.parse({
target: snapshotTarget(target.value),
binding: snapshotBinding(profile),
entries: [],
workflow_entries: [],
evidence_bundle: createEvidenceBundle([]),
}),
).ok,
).toBe(true);
const failed = await session.openResolvedTarget({
target: target.value,
route,
});
expect(failed.ok).toBe(false);
if (failed.ok) return;
expect(session.activeTarget()).toBeUndefined();
const other = await session.openResolvedTarget({
target: target.value,
route: {
...route,
profile: createAnalysisProfile(identity, { fixture: "other" }),
},
});
expect(other.ok).toBe(false);
if (other.ok) return;
expect(other.error).toBeInstanceOf(AnalysisSnapshotMismatchError);
} finally {
await session.close();
}
});
it("opens another profile after a failed switch whose cleanup later succeeds", async () => {
const session = new BinarySession(
SessionProviderRouter.selectable(new AnalysisProviderRegistry([]), []),
);
const identity = {
id: "restoration-test",
name: "Restoration test",
version: "1",
};
const switchFailure = new AnalysisOutputError(
"health",
"Selected target could not start",
);
const cleanupFailure = new ProviderCleanupError(
identity.id,
["incoming-client"],
{ reason: "close failed" },
);
let incomingCloses = 0;
const route: SessionProviderRoute = {
identity,
capabilities: new Map(),
profile: createAnalysisProfile(identity, { fixture: "original" }),
binding: null,
selection: undefined,
createClient: () => ({
execute: async () => ok(createAnalysisExecution(null, identity)),
close: async () => ok(null),
}),
};
try {
const target = await session.resolveTarget(process.execPath);
expect(target.ok).toBe(true);
if (!target.ok) return;
expect(
(await session.openResolvedTarget({ target: target.value, route })).ok,
).toBe(true);
const failed = await session.openResolvedTarget({
target: target.value,
route: {
...route,
profile: createAnalysisProfile(identity, { fixture: "selected" }),
createClient: () => ({
execute: async () => err(switchFailure),
close: async () => {
incomingCloses += 1;
if (incomingCloses < 3) return err(cleanupFailure);
releaseOwnedCleanup(cleanupFailure);
return ok(null);
},
}),
},
});
expect(failed.ok).toBe(false);
if (failed.ok) return;
expect(session.activeTarget()).toBeUndefined();
const blocked = await session.openResolvedTarget({
target: target.value,
route: {
...route,
profile: createAnalysisProfile(identity, { fixture: "recovered" }),
createClient: () => ({
execute: async () => ok(createAnalysisExecution(null, identity)),
close: async () => ok(null),
}),
},
});
expect(blocked.ok).toBe(false);
if (blocked.ok) return;
expect(projectAnalysisError(blocked.error).code).toBe("cleanup_incomplete");
expect(session.activeTarget()).toBeUndefined();
const recovered = await session.openResolvedTarget({
target: target.value,
route: {
...route,
profile: createAnalysisProfile(identity, { fixture: "recovered" }),
createClient: () => ({
execute: async () => ok(createAnalysisExecution(null, identity)),
close: async () => ok(null),
}),
},
});
expect(
recovered.ok ? "opened" : projectAnalysisError(recovered.error).code,
).toBe("opened");
expect(session.analysisProfile()?.parameters.fixture).toBe("recovered");
} finally {
await session.close();
}
});