feat(managed): project static evidence into application graph

This commit is contained in:
N0zoM1z0
2026-07-16 21:36:13 +08:00
parent 4df42ab616
commit 77178bba54
189 changed files with 2264 additions and 404 deletions
@@ -1,6 +1,7 @@
import {
JAVASCRIPT_APPLICATION_PROVIDER,
JAVASCRIPT_RUNTIME_RECONCILIATION_PROVIDER,
MANAGED_WORKFLOW_PROVIDER,
} from "./InvestigationProviders.js";
import { parseEvidence, type Evidence } from "../domain/evidence.js";
import {
@@ -10,12 +11,16 @@ import {
} from "../domain/javascriptApplicationAnalysis.js";
import type { JavaScriptApplicationGraph } from "../domain/javascriptApplicationGraph.js";
import { javascriptRuntimeReconciliationResultSchema } from "../domain/javascriptRuntimeReconciliationSchemas.js";
import { managedApplicationGraphResultSchema } from "../domain/managedApplicationGraph.js";
/** Supported immutable source for an application-level graph workflow. */
export interface ApplicationGraphEvidenceSource {
readonly evidence: Evidence;
readonly graph: JavaScriptApplicationGraph;
readonly kind: "static-application" | "static-runtime-reconciliation";
readonly kind:
| "static-application"
| "static-runtime-reconciliation"
| "managed-application";
readonly rootArtifactSha256: string;
}
@@ -36,8 +41,14 @@ export const parseApplicationGraphEvidence = (
providerMatches(evidence, JAVASCRIPT_RUNTIME_RECONCILIATION_PROVIDER)
)
return staticRuntimeSource(evidence);
if (
evidence.operation === "project_managed_application_graph" &&
evidence.predicate_type === "rea.managed-application-graph/v1" &&
providerMatches(evidence, MANAGED_WORKFLOW_PROVIDER)
)
return managedApplicationSource(evidence);
throw new TypeError(
"Application workflow requires authenticated analyze_javascript_application or reconcile_javascript_runtime Evidence",
"Application workflow requires authenticated analyze_javascript_application, reconcile_javascript_runtime, or project_managed_application_graph Evidence",
);
};
@@ -101,6 +112,35 @@ const staticRuntimeSource = (
};
};
const managedApplicationSource = (
evidence: Evidence,
): ApplicationGraphEvidenceSource => {
if (
evidence.authority !== "analyst-inference" ||
evidence.confidence !== "inferred"
)
throw new TypeError(
"Managed application graph Evidence authority or confidence is invalid",
);
const result = managedApplicationGraphResultSchema.parse(
evidence.normalized_result,
);
if (
result.evidence_links.some(
(evidenceId) => !evidence.evidence_links.includes(evidenceId),
)
)
throw new TypeError(
"Managed application graph result references Evidence outside its envelope",
);
return {
evidence,
graph: result.graph,
kind: "managed-application",
rootArtifactSha256: result.root_artifact_sha256,
};
};
const assertApplicationSubject = (
evidence: Evidence,
result: JavaScriptApplicationAnalysisResult,
@@ -0,0 +1,93 @@
import { z } from "zod";
import {
AnalysisInputError,
AnalysisProtocolError,
type AnalysisError,
} from "../domain/errors.js";
import { createEvidence, type Evidence } from "../domain/evidence.js";
import { jsonValueSchema } from "../domain/jsonValue.js";
import {
managedApplicationGraphResultSchema,
projectManagedApplicationGraph,
projectManagedApplicationGraphInputSchema,
type ManagedApplicationGraphResult,
type ProjectManagedApplicationGraphInput,
} from "../domain/managedApplicationGraph.js";
import { err, ok, type Result } from "../domain/result.js";
import { MANAGED_WORKFLOW_PROVIDER } from "./InvestigationProviders.js";
const OPERATION = "project_managed_application_graph" as const;
/** Project authenticated managed Evidence into a provider-neutral graph. */
export const projectManagedApplicationGraphEvidence = (
rawInput: unknown,
): Result<Evidence, AnalysisError> => {
const parsed = projectManagedApplicationGraphInputSchema.safeParse(rawInput);
if (!parsed.success)
return err(new AnalysisInputError(OPERATION, { cause: parsed.error }));
try {
const result = projectManagedApplicationGraph(parsed.data);
return ok(createManagedApplicationGraphEvidence(parsed.data, result));
} catch (cause: unknown) {
return err(
cause instanceof TypeError || cause instanceof z.ZodError
? new AnalysisInputError(OPERATION, { cause })
: new AnalysisProtocolError(
"Managed application graph projection produced an invalid result",
{ cause },
),
);
}
};
const createManagedApplicationGraphEvidence = (
input: ProjectManagedApplicationGraphInput,
result: ManagedApplicationGraphResult,
): Evidence =>
createEvidence(subjectTarget(input), MANAGED_WORKFLOW_PROVIDER, {
predicateType: "rea.managed-application-graph/v1",
operation: OPERATION,
parameters: {
managed_artifact_evidence_id: input.managed_artifact?.evidence_id ?? null,
managed_members_evidence_id: input.managed_members?.evidence_id ?? null,
managed_native_boundaries_evidence_id:
input.managed_native_boundaries?.evidence_id ?? null,
limits: jsonValueSchema.parse(input.limits),
},
result: jsonValueSchema.parse(
managedApplicationGraphResultSchema.parse(result),
),
rawResult: null,
confidence: "inferred",
authority: "analyst-inference",
environment: null,
limitations: result.limitations,
evidenceLinks: result.evidence_links,
});
const subjectTarget = (
input: ProjectManagedApplicationGraphInput,
):
| {
readonly path: string;
readonly sha256: string;
readonly format: "pe";
readonly architecture?: "x86" | "x86_64" | "arm" | "arm64";
}
| undefined => {
const subject =
input.managed_artifact?.subject ??
input.managed_members?.subject ??
input.managed_native_boundaries?.subject;
if (subject === undefined || subject === null || subject.format !== "pe")
return undefined;
return {
path: subject.local_path,
sha256: subject.digest.sha256,
format: "pe",
...(subject.architecture === null
? {}
: { architecture: subject.architecture }),
};
};