refactor: return complete tool results inline

This commit is contained in:
morluto
2026-09-29 07:08:46 +08:00
parent 2160d4fae5
commit 0d96275b68
535 changed files with 4084 additions and 13537 deletions
@@ -5,10 +5,7 @@ import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
paginateArtifactInventory,
scanArtifactInventory,
} from "../../../src/application/ArtifactInventory.js";
import { scanArtifactInventory } from "../../../src/application/ArtifactInventory.js";
const LIMITS = {
maxEntries: 10,
@@ -20,37 +17,18 @@ const LIMITS = {
} as const;
describe("artifact inventory snapshot", () => {
it("projects every page from the same completed scan", async () => {
it("retains the complete scan after the source directory changes", async () => {
const root = await createTestTempDirectory("rea-inventory-snapshot-");
await writeFile(join(root, "a.txt"), "a");
await writeFile(join(root, "b.txt"), "b");
const snapshot = await scanArtifactInventory(root, LIMITS);
await rm(join(root, "b.txt"));
const first = paginateArtifactInventory(snapshot, {
nodeOffset: 0,
nodeLimit: 1,
occurrenceOffset: 0,
occurrenceLimit: 2,
edgeOffset: 0,
edgeLimit: 1,
});
const second = paginateArtifactInventory(snapshot, {
nodeOffset: 1,
nodeLimit: 10,
occurrenceOffset: 2,
occurrenceLimit: 2,
edgeOffset: 1,
edgeLimit: 10,
});
expect(first.manifest).toEqual(second.manifest);
expect(
[...first.occurrences.items, ...second.occurrences.items].map(
({ logical_path: path }) => path,
),
).toEqual([".", "a.txt", "b.txt"]);
expect(first.occurrences.total).toBe(3);
expect(second.occurrences.total).toBe(3);
expect(snapshot.occurrences.map(({ logical_path: path }) => path)).toEqual([
".",
"a.txt",
"b.txt",
]);
expect(snapshot.manifest.occurrence_count).toBe(3);
});
});
@@ -17,15 +17,11 @@ const LIMITS = {
} as const;
describe("authorized artifact inventory", () => {
it("uses a valid input root when another configured root is missing", async () => {
it("scans a local artifact without configured input roots", async () => {
const root = await createTestTempDirectory("rea-artifact-roots-");
const path = join(root, "artifact.js");
await writeFile(path, "export const value = 1;\n");
const inventory = await scanAuthorizedArtifactInventory(
path,
[join(root, "missing"), root],
LIMITS,
);
const inventory = await scanAuthorizedArtifactInventory(path, LIMITS);
expect(inventory.manifest.root_format).toBe("javascript-bundle");
});
});
@@ -96,7 +96,7 @@ describe("persistent cross-version investigation workspace", () => {
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{ inputRoots: [directory] },
{},
);
if (!completed.ok) throw completed.error;
@@ -116,7 +116,7 @@ describe("persistent cross-version investigation workspace", () => {
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{ inputRoots: [directory] },
{},
);
if (!completed.ok) throw completed.error;
const run = completed.value.workspace.runs[0];
@@ -162,7 +162,7 @@ describe("persistent cross-version investigation workspace", () => {
replay_run_id: partialRun.run_id,
},
policy(directory),
{ inputRoots: [], authorizeInputRead: replayAuthorization },
{},
),
).resolves.toMatchObject({
ok: false,
@@ -176,7 +176,7 @@ describe("persistent cross-version investigation workspace", () => {
const resumed = await runCrossVersionInvestigation(
{ ...input, workspace_path: resumePath },
policy(directory),
{ inputRoots: [directory] },
{},
);
expect(resumed).toMatchObject({
ok: true,
@@ -192,9 +192,11 @@ describe("persistent cross-version investigation workspace", () => {
describe("persistent investigation workspace history", () => {
it("appends a changed-content run without replacing earlier Evidence", async () => {
const { directory, right, input } = await fixture();
const first = await runCrossVersionInvestigation(input, policy(directory), {
inputRoots: [directory],
});
const first = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
if (!first.ok) throw first.error;
const firstRun = first.value.workspace.runs[0];
if (firstRun === undefined || firstRun.result_evidence_id === null)
@@ -204,7 +206,7 @@ describe("persistent investigation workspace history", () => {
const second = await runCrossVersionInvestigation(
input,
policy(directory),
{ inputRoots: [directory] },
{},
);
expect(second).toMatchObject({
ok: true,
@@ -251,7 +253,7 @@ describe("persistent investigation workspace history", () => {
const strict = await runCrossVersionInvestigation(
crossVersionInvestigationInputSchema.parse(base),
policy(directory),
{ inputRoots: [directory] },
{},
);
expect(strict).toMatchObject({
ok: false,
@@ -268,7 +270,7 @@ describe("persistent investigation workspace history", () => {
const continued = await runCrossVersionInvestigation(
continuedInput,
policy(directory),
{ inputRoots: [directory], integrityContinueEnabled: true },
{ integrityContinueEnabled: true },
);
expect(continued).toMatchObject({
ok: true,
@@ -289,7 +291,6 @@ describe("persistent investigation workspace history", () => {
).toMatchObject({ status: "contradiction" });
await expect(
runCrossVersionInvestigation(continuedInput, policy(directory), {
inputRoots: [directory],
integrityContinueEnabled: true,
}),
).resolves.toMatchObject({ ok: true, value: { reused: true } });
@@ -299,9 +300,11 @@ describe("persistent investigation workspace history", () => {
workspace_path: continuedInput.workspace_path,
});
await expect(
runCrossVersionInvestigation(strictAfterContinuation, policy(directory), {
inputRoots: [directory],
}),
runCrossVersionInvestigation(
strictAfterContinuation,
policy(directory),
{},
),
).resolves.toMatchObject({
ok: false,
error: { _tag: "ArtifactOperationError", reason: "integrity" },
@@ -55,9 +55,7 @@ describe("persistent cross-version investigation workspace", () => {
mode: 0o600,
});
expect(
await runCrossVersionInvestigation(input, policy(directory), {
inputRoots: [directory],
}),
await runCrossVersionInvestigation(input, policy(directory), {}),
).toMatchObject({
ok: false,
error: { _tag: "InvestigationWorkspaceError", reason: "locked" },
@@ -65,9 +63,7 @@ describe("persistent cross-version investigation workspace", () => {
await rm(`${path}.lock`);
await writeFile(`${path}.lock`, "occupied\n", { mode: 0o600 });
expect(
await runCrossVersionInvestigation(input, policy(directory), {
inputRoots: [directory],
}),
await runCrossVersionInvestigation(input, policy(directory), {}),
).toMatchObject({
ok: false,
error: { _tag: "InvestigationWorkspaceError", reason: "locked" },
@@ -77,7 +73,7 @@ describe("persistent cross-version investigation workspace", () => {
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{ inputRoots: [directory] },
{},
);
if (!completed.ok) throw completed.error;
await expect(stat(`${path}.lock`)).rejects.toMatchObject({
@@ -87,7 +83,7 @@ describe("persistent cross-version investigation workspace", () => {
await runCrossVersionInvestigation(
{ ...input, expected_workspace_revision: 2 },
policy(directory),
{ inputRoots: [directory] },
{},
),
).toMatchObject({
ok: false,
@@ -116,7 +112,7 @@ describe("persistent cross-version investigation workspace", () => {
await runCrossVersionInvestigation(
{ ...input, workspace_path: cancelledPath },
policy(directory),
{ inputRoots: [directory], signal: controller.signal },
{ signal: controller.signal },
),
).toMatchObject({
ok: false,
@@ -129,9 +125,7 @@ describe("persistent cross-version investigation workspace", () => {
const approved = join(directory, "approved");
await mkdir(approved);
expect(
await runCrossVersionInvestigation(input, policy(approved), {
inputRoots: [directory],
}),
await runCrossVersionInvestigation(input, policy(approved), {}),
).toMatchObject({
ok: false,
error: {
@@ -148,26 +142,11 @@ describe("persistent cross-version investigation workspace", () => {
await runCrossVersionInvestigation(
{ ...input, workspace_path: escaped },
policy(approved),
{ inputRoots: [directory] },
{},
),
).toMatchObject({
ok: false,
error: { _tag: "InvestigationWorkspaceError", reason: "not-file" },
});
});
it("rejects artifact inputs outside independently approved roots", async () => {
const { directory, input, path } = await fixture();
const approvedInputs = join(directory, "approved-inputs");
await mkdir(approvedInputs);
expect(
await runCrossVersionInvestigation(input, policy(directory), {
inputRoots: [approvedInputs],
}),
).toMatchObject({
ok: false,
error: { _tag: "ArtifactOperationError", reason: "path" },
});
await expect(stat(path)).rejects.toMatchObject({ code: "ENOENT" });
});
});
@@ -2,7 +2,7 @@ import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { join } from "node:path";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import canonicalize from "canonicalize";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
@@ -21,7 +21,6 @@ import {
parseInvestigationWorkspace,
serializeInvestigationWorkspace,
} from "../../../src/domain/investigationWorkspace.js";
import { ok } from "../../../src/domain/result.js";
const digestCanonical = (value: unknown): string => {
const encoded = canonicalize(value);
@@ -65,7 +64,7 @@ describe("persistent cross-version investigation workspace", () => {
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{ inputRoots: [directory] },
{},
);
if (!completed.ok) throw completed.error;
const current = completed.value.workspace;
@@ -159,7 +158,7 @@ describe("persistent cross-version investigation workspace", () => {
runCrossVersionInvestigation(
{ ...input, expected_workspace_revision: current.revision },
policy(directory),
{ inputRoots: [directory] },
{},
),
).resolves.toMatchObject({ ok: true, value: { reused: true } });
await expect(
@@ -172,7 +171,7 @@ describe("persistent cross-version investigation workspace", () => {
},
},
policy(directory),
{ inputRoots: [directory] },
{},
),
).resolves.toMatchObject({ ok: true, value: { reused: false } });
});
@@ -181,9 +180,11 @@ describe("persistent cross-version investigation workspace", () => {
describe("persistent investigation workspace reuse", () => {
it("checkpoints, validates, and reuses a completed deterministic run", async () => {
const { directory, path, input } = await fixture();
const first = await runCrossVersionInvestigation(input, policy(directory), {
inputRoots: [directory],
});
const first = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
expect(first).toMatchObject({
ok: true,
value: { reused: false, workspace: { revision: 3 } },
@@ -212,7 +213,7 @@ describe("persistent investigation workspace reuse", () => {
const second = await runCrossVersionInvestigation(
input,
policy(directory),
{ inputRoots: [directory] },
{},
);
expect(second).toMatchObject({
ok: true,
@@ -235,12 +236,8 @@ describe("persistent investigation workspace reuse", () => {
workspace_path: join(directory, "repeated-workspace.json"),
});
const [first, repeated] = await Promise.all([
runCrossVersionInvestigation(input, policy(directory), {
inputRoots: [directory],
}),
runCrossVersionInvestigation(repeatedInput, policy(directory), {
inputRoots: [directory],
}),
runCrossVersionInvestigation(input, policy(directory), {}),
runCrossVersionInvestigation(repeatedInput, policy(directory), {}),
]);
if (!first.ok) throw first.error;
if (!repeated.ok) throw repeated.error;
@@ -293,21 +290,16 @@ describe("persistent investigation workspace replay", () => {
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{ inputRoots: [directory] },
{},
);
if (!completed.ok) throw completed.error;
const run = completed.value.workspace.runs[0];
if (run === undefined || run.comparison_evidence_id === null)
throw new Error("missing completed run");
const defaultAuthorization = vi.fn(() => Promise.resolve(ok(null)));
await expect(
runCrossVersionInvestigation(input, policy(directory), {
inputRoots: [directory],
authorizeInputRead: defaultAuthorization,
}),
runCrossVersionInvestigation(input, policy(directory), {}),
).resolves.toMatchObject({ ok: true, value: { reused: true } });
expect(defaultAuthorization).toHaveBeenCalledOnce();
const inconsistentRun = investigationRunSchema.parse({
...run,
@@ -335,7 +327,7 @@ describe("persistent investigation workspace replay", () => {
replay_run_id: run.run_id,
},
policy(directory),
{ inputRoots: [] },
{},
),
).resolves.toMatchObject({
ok: false,
@@ -346,7 +338,6 @@ describe("persistent investigation workspace replay", () => {
rm(left, { recursive: true, force: true }),
rm(right, { recursive: true, force: true }),
]);
const replayAuthorization = vi.fn(() => Promise.resolve(ok(null)));
await expect(
runCrossVersionInvestigation(
{
@@ -355,7 +346,7 @@ describe("persistent investigation workspace replay", () => {
replay_run_id: run.run_id,
},
policy(directory),
{ inputRoots: [], authorizeInputRead: replayAuthorization },
{},
),
).resolves.toMatchObject({
ok: true,
@@ -365,7 +356,6 @@ describe("persistent investigation workspace replay", () => {
workspace: { revision: completed.value.workspace.revision },
},
});
expect(replayAuthorization).not.toHaveBeenCalled();
for (const replayInput of [
{ ...input, replay_run_id: `run_${"f".repeat(64)}` },
@@ -377,10 +367,7 @@ describe("persistent investigation workspace replay", () => {
},
])
await expect(
runCrossVersionInvestigation(replayInput, policy(directory), {
inputRoots: [],
authorizeInputRead: replayAuthorization,
}),
runCrossVersionInvestigation(replayInput, policy(directory), {}),
).resolves.toMatchObject({
ok: false,
error: {
@@ -388,6 +375,5 @@ describe("persistent investigation workspace replay", () => {
reason: "revision-conflict",
},
});
expect(replayAuthorization).not.toHaveBeenCalled();
});
});
@@ -19,54 +19,22 @@ import {
} from "../../../src/artifacts/ArtifactReader.js";
import { writeJavaScriptArtifactFixture } from "../../fixtures/javascriptArtifactApplication.js";
it("discovers source maps without reading them until separately approved", async () => {
it("reads local source maps as part of static artifact analysis", async () => {
const root = await fixtureDirectory();
const unapproved = await reconstructJavaScriptArtifact({
input_path: root,
source_map_read_approved: false,
});
const approved = await reconstructJavaScriptArtifact({
input_path: root,
source_map_read_approved: true,
});
const result = await reconstructJavaScriptArtifact({ input_path: root });
expect(unapproved.graph.nodes.some(({ kind }) => kind === "source-map")).toBe(
expect(result.graph.nodes.some(({ kind }) => kind === "source-map")).toBe(
true,
);
expect(
unapproved.graph.nodes.some(({ kind }) => kind === "source-module"),
).toBe(false);
expect(unapproved.graph.coverage).toMatchObject({
status: "partial",
truncated: false,
omitted_count: null,
});
expect(unapproved.statistics.policy_filtered_text_files).toBe(1);
expect(unapproved.graph.nodes).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: "unknown",
observations: expect.arrayContaining([
expect.objectContaining({
properties: expect.objectContaining({
operation: "parse-local-source-map",
}),
evidence: expect.objectContaining({ state: "unavailable" }),
}),
]),
}),
]),
expect(result.graph.nodes.some(({ kind }) => kind === "source-module")).toBe(
true,
);
expect(
approved.graph.nodes.some(({ kind }) => kind === "source-module"),
).toBe(true);
});
it("reports text and AST limits as truncation instead of absence", async () => {
const root = await fixtureDirectory();
const result = await reconstructJavaScriptArtifact({
input_path: root,
source_map_read_approved: true,
limits: {
max_text_file_bytes: 128,
max_total_text_bytes: 1_024,
@@ -163,7 +131,6 @@ it("keeps malformed JavaScript, package metadata, and source maps as explicit un
const result = await reconstructJavaScriptArtifact({
input_path: root,
source_map_read_approved: true,
});
const unknownOperations = result.graph.nodes
.filter(({ kind }) => kind === "unknown")
@@ -213,7 +180,6 @@ it("applies one source-map source budget across every local map", async () => {
const result = await reconstructJavaScriptArtifact({
input_path: root,
source_map_read_approved: true,
limits: { max_source_map_sources: 3 },
});
@@ -297,12 +263,12 @@ it("preserves cancellation and explicit format diagnostics", async () => {
reason: "format",
message: expect.stringContaining(root),
});
expect(() =>
expect(
javascriptArtifactReconstructionInputSchema.parse({
input_path: root,
limits: { max_entries: 100_000 },
}),
).toThrow(/graph contract/iu);
}).limits.max_entries,
).toBe(100_000);
});
class TraversalReader implements ArtifactReader {
@@ -13,7 +13,6 @@ import {
createJavaScriptApplicationGraph,
createJavaScriptApplicationNode,
} from "../../../src/domain/javascriptApplicationGraph.js";
import { permissionAuthorityForRoot } from "../../fixtures/permissionAuthority.js";
describe("JavaScript export return-shape comparison", () => {
it("reports exactly the heading depth addition from source-owned parser fixtures", async () => {
@@ -194,7 +193,6 @@ describe("JavaScript export return-shape selection", () => {
});
const ambiguousGraph = createJavaScriptApplicationGraph({
schema: "JavaScriptApplicationGraph",
schema_version: 1,
root_node_ids: left.graph.root_node_ids,
nodes: [...left.graph.nodes, duplicate],
edges: left.graph.edges,
@@ -271,14 +269,8 @@ const compare = (
});
const analyzeGraph = async (root: string) => {
const authority = await permissionAuthorityForRoot(
root,
["investigation_input"],
["investigation_input"],
);
const result = await analyzeJavaScriptApplication(authority, {
const result = await analyzeJavaScriptApplication({
input_path: root,
approved: true,
});
if (!result.ok) throw result.error;
return parseApplicationGraphEvidence(result.value);
@@ -234,7 +234,7 @@ const inspect = (
) => {
const target = managedPeFixtureTarget(bytes, path);
const result = inspectManagedMembersBytes(bytes, target, limits);
const method = result.methods.items[0];
const method = result.methods[0];
if (method === undefined) throw new Error("fixture has no method");
return {
result,
@@ -235,7 +235,6 @@ const dependenciesFor = (root: string, onExecute: () => void) => {
execute: async (prepared) => {
onExecute();
return {
schema_version: 1,
plan_digest: prepared.publicPlan.plan_digest,
outcomes: [
{
@@ -53,11 +53,7 @@ describe("runtime identification", () => {
await writeFile(path, await writer.close());
const inventory = parseEvidence(
await runProviderAnalysis(path, "inventory_artifact", {
node_limit: 100,
occurrence_limit: 100,
edge_limit: 100,
}),
await runProviderAnalysis(path, "inventory_artifact", {}),
);
const first = identifyRuntimes({ inventory_evidence: [inventory] });
const second = identifyRuntimes({ inventory_evidence: [inventory] });