Files
reverse-engineering-anything/src/application/CallRelationshipTraversal.test.ts
T
Kaoru0822andClaude Opus 5.5 35ae7232bf fix(enhanced): stop call graph traversals at unconfirmed provider cleanup (#8516)
* fix(enhanced): stop call graph traversals at unconfirmed provider cleanup

A relationship read whose provider could not confirm cleanup of resources it
owns was projected into a graph node or call-path failure, the traversal kept
dispatching queued reads, and get_call_graph / trace_call_path returned
success with the cleanup_incomplete status buried inside the result.

Both traversals now read relationships through one shared owner,
readCallRelationships, which classifies a cancelled read or a read with
unconfirmed owned cleanup as terminal. On such a cleanup stop the workflow
returns the provider's own error as the outcome, so its cleanup owner, retry
binding and live resolution stay intact, and keeps the work admitted before
the stop as the workflow's normal result shape with a caveat naming the
stopped procedure and the unexpanded count. The composed-workflow layer
turns that admitted result into partial workflow Evidence on the failure,
and the MCP adapter retains it through the shared partial-error delivery.
Ordinary read failures still produce successful partial results.

Fixes #8369

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(enhanced): retain every carried partial Evidence on enhanced failures

Enhanced-tool failures used the shared partial-error delivery only when this
PR's workflow partial was attached. A failing read that already carried its
own partial Evidence, and analyze_function dossier failures, were projected
inline and never recorded with the session writer, so readback and bundle
export could not recover them. Every enhanced execution failure now goes
through one delivery owner that uses toPartialErrorToolResult with the
session writer; it still falls back to inline when no writer or no valid
Evidence exists.

Tests compare the traversal caveat against its owning formatter instead of
copying the sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 08:52:53 -08:00

126 lines
4.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
import {
AnalysisCancelledError,
AnalysisCapabilityUnavailableError,
} from "../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { err, ok } from "../domain/result.js";
import {
createAnalysisExecution,
type AnalysisOperationPort,
} from "./AnalysisProvider.js";
import { EnhancedTools } from "./EnhancedTools.js";
const provider = { id: "fixture", name: "Fixture", version: "1" };
const tools = ["get_call_graph", "trace_call_path"] as const;
const input = (tool: (typeof tools)[number]) =>
tool === "get_call_graph"
? { address: "0x1", direction: "forward" }
: { start: "0x1", goal: "0x4", direction: "forward" };
const cleanupFailure = () =>
new AnalysisCapabilityUnavailableError(
provider.id,
"procedure_callees",
"child failed",
{ cleanup: { reason: "worker cleanup failed", resources: ["worker"] } },
);
/** 0x1 calls 0x2 and 0x3; 0x2's read is supplied by the test. */
const port = (
reads: string[],
second: (signal?: AbortSignal) => AnalysisError,
): AnalysisOperationPort => ({
execute: async (operation, parameters, options) => {
const procedure = String(parameters.procedure);
if (operation === "procedure_address")
return ok(createAnalysisExecution(procedure, provider));
reads.push(procedure);
if (procedure === "0x2") return err(second(options?.signal));
return ok(
createAnalysisExecution(
procedure === "0x1" ? ["0x2", "0x3"] : ["0x4"],
provider,
{ relationshipCoverage: { status: "complete" } },
),
);
},
});
describe.each(tools)("%s cleanup ownership", (tool) => {
it("returns the provider's cleanup failure so its owner resolves the outcome", async () => {
const failure = cleanupFailure();
let released = false;
const lease = new AnalysisCleanupLease(async () => {
if (!released) throw new Error("still owned");
}, ["worker"]);
lease.bindCleanupError(failure);
failure.bindCleanupRetry({ operation: "close_binary" });
const reads: string[] = [];
const result = await new EnhancedTools(port(reads, () => failure)).execute(
tool,
input(tool),
);
if (result.ok) throw new Error("expected incomplete cleanup");
expect(result.error).toBe(failure);
expect("partialResult" in result).toBe(true);
expect(reads).toEqual(["0x1", "0x2"]);
expect(projectAnalysisError(result.error)).toMatchObject({
code: "cleanup_incomplete",
recovery: { kind: "retry_cleanup", operation: "close_binary" },
});
await expect(lease.release()).rejects.toThrow("still owned");
expect(projectAnalysisError(result.error).code).toBe("cleanup_incomplete");
released = true;
await lease.release();
expect(projectAnalysisError(result.error).code).toBe(
"capability_unavailable",
);
});
it("keeps a cleanup failure primary when cancellation arrives with it", async () => {
const controller = new AbortController();
const failure = cleanupFailure();
const reads: string[] = [];
const result = await new EnhancedTools(
port(reads, () => {
controller.abort({ requested: "stop" });
return failure;
}),
).execute(tool, input(tool), controller.signal);
if (result.ok) throw new Error("expected incomplete cleanup");
expect(result.error).toBe(failure);
expect(projectAnalysisError(result.error).code).toBe("cleanup_incomplete");
expect(reads).toEqual(["0x1", "0x2"]);
});
it("stops at a provider cancellation whose own cleanup is unconfirmed", async () => {
const controller = new AbortController();
const reads: string[] = [];
let cancellation: AnalysisCancelledError | undefined;
const result = await new EnhancedTools(
port(reads, (signal) => {
controller.abort();
cancellation = new AnalysisCancelledError("procedure_callees", {
cause: signal?.reason,
cleanup: { reason: "cancel cleanup failed", resources: ["worker"] },
});
return cancellation;
}),
).execute(tool, input(tool), controller.signal);
if (result.ok) throw new Error("expected cancellation");
expect(result.error).toBe(cancellation);
expect(projectAnalysisError(result.error).code).toBe("cleanup_incomplete");
expect(reads).toEqual(["0x1", "0x2"]);
});
});