fix(io): retain cleanup ownership and honor cancellation
Keep failed JSON read handles in the existing server resource scope and fail admission until cleanup succeeds. Thread cancellation through every process CLI input read and replace timer-driven cancellation checks with a shared first-read gate. Classify authenticated standalone Evidence as wrong-kind input while preserving tampered-record integrity failures and actionable messages across ledger, CLI, and MCP consumers. Record unrelated CS_RESTRICT process neighbors as unverified without weakening related-process ownership checks. Update stale ADB fixtures for the actual shell protocol. Validated focused CLI, MCP, filesystem, ownership, and error projection tests; static, docs, and duplication gates pass. The host-level CS_RESTRICT load reproduction was not run. Refs #2050, #2035, #2064.
This commit is contained in:
@@ -45,13 +45,15 @@ export class AnalysisProtocolError extends AnalysisError {
|
||||
/** Caller input failed provider-neutral application parsing. */
|
||||
export class AnalysisInputError extends AnalysisError {
|
||||
readonly _tag = "AnalysisInputError";
|
||||
override readonly userMessage: string | undefined;
|
||||
|
||||
constructor(
|
||||
readonly operation: string,
|
||||
options?: AnalysisErrorOptions,
|
||||
options?: AnalysisErrorOptions & { readonly userMessage?: string },
|
||||
readonly issues: readonly AnalysisInputIssue[] = [],
|
||||
) {
|
||||
super(`Invalid analysis input for ${operation}`, options);
|
||||
this.userMessage = options?.userMessage;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -148,6 +148,7 @@ export const analysisErrorCategory = (
|
||||
error instanceof EvidenceFileError &&
|
||||
(error.reason === "missing" ||
|
||||
error.reason === "not-file" ||
|
||||
error.reason === "wrong-kind" ||
|
||||
error.reason === "exists")
|
||||
)
|
||||
return "invalid_input";
|
||||
@@ -210,7 +211,10 @@ export const analysisErrorUserMessage = (error: AnalysisError): string => {
|
||||
if (error instanceof AnalysisArtifactChangedError)
|
||||
return "The selected artifact changed during acquisition; no stable snapshot was decoded.";
|
||||
if (error instanceof AnalysisInputError)
|
||||
return "Analysis input is invalid. Check the arguments and try again.";
|
||||
return (
|
||||
error.userMessage ??
|
||||
"Analysis input is invalid. Check the arguments and try again."
|
||||
);
|
||||
const hopperMessage = hopperErrorUserMessage(error);
|
||||
if (hopperMessage !== undefined) return hopperMessage;
|
||||
if (error.userMessage !== undefined) return error.userMessage;
|
||||
|
||||
@@ -5,6 +5,7 @@ import { ANALYSIS_SNAPSHOT_TARGET } from "./analysisSnapshot.fixture.js";
|
||||
import {
|
||||
createEvidenceBundle,
|
||||
createImmutableEvidenceBundle,
|
||||
describeEvidenceBundleFailure,
|
||||
evidenceBundleForTarget,
|
||||
parseEvidenceBundle,
|
||||
} from "./evidenceBundle.js";
|
||||
@@ -18,6 +19,24 @@ const provider = { id: "fixture", name: "Fixture", version: "1" };
|
||||
const targetDigest = "a".repeat(64);
|
||||
const foreignDigest = "b".repeat(64);
|
||||
|
||||
describe("evidence bundle failure classification", () => {
|
||||
it("treats only authenticated standalone Evidence as the wrong input kind", () => {
|
||||
const record = createEvidence(undefined, provider, {
|
||||
operation: "observe",
|
||||
parameters: {},
|
||||
result: { observed: true },
|
||||
});
|
||||
const single = describeEvidenceBundleFailure(record, new Error("bundle"));
|
||||
expect(single).toMatchObject({ kind: "single-record" });
|
||||
|
||||
const tampered = describeEvidenceBundleFailure(
|
||||
{ ...record, normalized_result: { observed: false } },
|
||||
new Error("bundle"),
|
||||
);
|
||||
expect(tampered).toMatchObject({ kind: "invalid-bundle" });
|
||||
});
|
||||
});
|
||||
|
||||
const makeUnknown = (
|
||||
question: string,
|
||||
scopeDigest: string,
|
||||
|
||||
@@ -208,16 +208,29 @@ const referencedEvidenceIds = (unknown: ResidualUnknown): readonly string[] => [
|
||||
* Name the bundle constraint that failed and how to recover. A single
|
||||
* Evidence record, such as one command result, is not a bundle.
|
||||
*/
|
||||
export type EvidenceBundleFailureDescription =
|
||||
| { readonly kind: "single-record"; readonly message: string }
|
||||
| { readonly kind: "invalid-bundle"; readonly message: string };
|
||||
|
||||
export const describeEvidenceBundleFailure = (
|
||||
input: unknown,
|
||||
cause: unknown,
|
||||
): string => {
|
||||
const record = evidenceSchema.safeParse(input);
|
||||
if (record.success)
|
||||
return `Expected an Evidence bundle with records and manifest arrays, but this JSON is one Evidence record (${record.data.evidence_id}). Export a session bundle with the MCP export_evidence_bundle tool and supply that file.`;
|
||||
return cause instanceof z.ZodError
|
||||
? `Evidence bundle does not match the bundle schema ${describeValidationFailure(cause)}. Supply an unmodified bundle written by export_evidence_bundle or rea evidence-export.`
|
||||
: `Evidence bundle validation failed: ${describeValidationFailure(cause)}. Recreate or re-export the bundle, then try again.`;
|
||||
): EvidenceBundleFailureDescription => {
|
||||
try {
|
||||
const record = parseEvidence(input);
|
||||
return {
|
||||
kind: "single-record",
|
||||
message: `Expected an Evidence bundle with records and manifest arrays, but this JSON is one Evidence record (${record.evidence_id}). Export a session bundle with the MCP export_evidence_bundle tool and supply that file.`,
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
kind: "invalid-bundle",
|
||||
message:
|
||||
cause instanceof z.ZodError
|
||||
? `Evidence bundle does not match the bundle schema ${describeValidationFailure(cause)}. Supply an unmodified bundle written by export_evidence_bundle or rea evidence-export.`
|
||||
: `Evidence bundle validation failed: ${describeValidationFailure(cause)}. Recreate or re-export the bundle, then try again.`,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/** Locate the first schema issue, or keep a thrown constraint message. */
|
||||
|
||||
@@ -45,6 +45,7 @@ export class EvidenceReferenceError extends EvidenceIntegrityError {
|
||||
/** Evidence bundle filesystem access failed within the configured policy. */
|
||||
export class EvidenceFileError extends AnalysisError {
|
||||
readonly _tag = "EvidenceFileError";
|
||||
override readonly userMessage: string | undefined;
|
||||
|
||||
/** Caller-selected path that failed, resolved against the working directory. */
|
||||
readonly path: string | undefined;
|
||||
@@ -53,14 +54,22 @@ export class EvidenceFileError extends AnalysisError {
|
||||
|
||||
constructor(
|
||||
readonly operation: "read" | "write",
|
||||
readonly reason: "not-file" | "exists" | "invalid-json" | "missing" | "io",
|
||||
readonly reason:
|
||||
| "not-file"
|
||||
| "exists"
|
||||
| "wrong-kind"
|
||||
| "invalid-json"
|
||||
| "missing"
|
||||
| "io",
|
||||
options?: AnalysisErrorOptions & {
|
||||
readonly path?: string;
|
||||
readonly published?: PublishedEvidenceFile;
|
||||
readonly userMessage?: string;
|
||||
},
|
||||
) {
|
||||
super(`Evidence bundle ${operation} failed: ${reason}`, options);
|
||||
this.path = options?.path;
|
||||
this.published = options?.published;
|
||||
this.userMessage = options?.userMessage;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user