Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(managed): project call and field-access edges into the applicati…
…on graph

project_managed_application_graph dropped the call_edges and
field_accesses that member Evidence already decodes, so the graph was a
containment tree that could not answer who calls a P/Invoke wrapper or
which methods touch a field, while reporting complete-within-inputs.

In-module call/callvirt/newobj/jmp sites now become one calls edge per
caller and target, and field loads, stores and address-takes become one
accesses edge per method and field, each with site and per-opcode counts
and member Evidence links. MemberRef and MethodSpec targets, unknown
targets, ldftn/ldvirtftn loads, recursive self-calls (the graph has no
self-loops), MemberRef fields and sites whose node is absent are counted
by reason in limitations, and the graph is partial with that
omitted_count. Add the provider-neutral accesses relation.

Graph Evidence projected before these counters existed still parses,
with null calls and field_accesses summaries.

Fixes #4056

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
kaoru0822-kitauji and claude committed Oct 22, 2026
commit a6473eb431945c3499b5ca8a2012b79e8c2297be
4 changes: 2 additions & 2 deletions docs/javascript-application-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ Graph coverage describes the analyzed artifact scope and resource limits, not
runtime execution. A partial target alone does not imply omitted source.

The directed relation vocabulary is `contains`, `loads`, `imports`, `maps_to`,
`exposes`, `sends`, `invokes`, `handles`, `calls`, `persists_to`, `observed_as`,
and `changed_from`. Provider names are deliberately absent from both
`exposes`, `sends`, `invokes`, `handles`, `calls`, `accesses`, `persists_to`,
`observed_as`, and `changed_from`. Provider names are deliberately absent from both
vocabularies.

For a `maps_to` edge targeting a source-map node, the edge carries
Expand Down
26 changes: 26 additions & 0 deletions docs/managed-code-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,32 @@ Declared import inventory supports a bounded positive claim. Its absence does
not exclude dynamic resolution, generated code, protected code, native helpers,
or server-side behavior.

## Application graph relations

`project_managed_application_graph` turns member Evidence into `contains`
edges for declarations, `imports` edges for P/Invoke declarations, and two
relation kinds read from method bodies:

- `calls` links two `managed-method` nodes when a `call`, `callvirt`, `newobj`
or `jmp` site in the caller targets a MethodDef in the inspected module.
- `accesses` links a `managed-method` node to a `managed-field` node for each
field it loads, stores or takes the address of.

Each edge stands for every site between one source and one target. Its
properties carry `site_count` and per-opcode `opcodes` counts; `accesses`
edges also list `access` as `read`, `write` and/or `address`. Edge evidence
links the member Evidence.

Some sites are counted rather than projected: calls to MemberRefs (members of
other assemblies or of generic type instantiations), generic method
instantiations (`MethodSpec`), unknown targets, `ldftn`/`ldvirtftn` function
pointer loads, recursive self-calls (the graph has no self-loops), MemberRef
fields, and sites whose method or field node is absent, for example under a
type selection. Limitations name each counted class, and graph coverage is
`partial` with `omitted_count` equal to the omitted sites when nothing else is
incomplete. A missing `calls` edge is therefore not evidence that a method is
never called.

## Evidence record shape

Member comparison, application graph projection, and managed/native verification
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/managed/managedWorkflowToolContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const MANAGED_WORKFLOW_TOOL_CONTRACTS = [
name: "project_managed_application_graph",
...toolContractMetadata("project_managed_application_graph"),
description:
"Project authenticated managed artifact, member, and native-boundary Evidence into the provider-neutral application graph without executing managed code, loading assemblies, or translating managed metadata tokens to native addresses. The graph preserves managed static observations as managed-specific nodes linked to source Evidence.",
"Project authenticated managed artifact, member, and native-boundary Evidence into the provider-neutral application graph without executing managed code, loading assemblies, or translating managed metadata tokens to native addresses. The graph preserves managed static observations as managed-specific nodes linked to source Evidence, with in-module calls and field-access edges; other call and field-access sites are counted in limitations.",
kind: "application",
inputSchema: managedApplicationGraphReferenceInputSchema,
outputSchema: managedApplicationGraphOutputSchema,
Expand Down
1 change: 1 addition & 0 deletions src/domain/javascript/javascriptApplicationGraphSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const JAVASCRIPT_APPLICATION_RELATIONS = [
"invokes",
"handles",
"calls",
"accesses",
"persists_to",
"observed_as",
"changed_from",
Expand Down
48 changes: 39 additions & 9 deletions src/domain/managed/managedApplicationGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ import {
assessManagedGraphOmissions,
managedGraphEvidenceCoverage,
managedGraphResultCoverage,
type ManagedGraphProjectionOmissions,
} from "./managedApplicationGraphCoverage.js";
import {
addArtifactNode,
addArtifactIdentityNodes,
addBoundaryNodes,
addMemberNodes,
} from "./managedApplicationGraphNodes.js";
import {
addMemberRelations,
relationOmissionLimitations,
} from "./managedApplicationGraphRelations.js";
import { digestSchema } from "../digests.js";
import { prefixedDigestSchema } from "../digests.js";
import { parseManagedInspectionEvidence } from "./managedInspectionEvidence.js";
Expand Down Expand Up @@ -75,6 +80,22 @@ export const managedApplicationGraphResultSchema = z.strictObject({
fields: z.int().min(0),
pinvoke_imports: z.int().min(0),
native_implementations: z.int().min(0),
// Graphs projected before call and field-access edges existed omit these
// counters; null means those relations were not projected, not zero.
calls: z
.int()
.min(0)
.nullable()
.default(null)
.describe("calls edges; null for graphs projected before calls edges"),
field_accesses: z
.int()
.min(0)
.nullable()
.default(null)
.describe(
"accesses edges; null for graphs projected before accesses edges",
),
}),
graph: javascriptApplicationGraphSchema,
coverage: z.strictObject({
Expand Down Expand Up @@ -144,7 +165,15 @@ export const projectManagedApplicationGraph = (
addArtifactIdentityNodes(state, artifactNode, parsed);
addMemberNodes(state, artifactNode, parsed);
addBoundaryNodes(state, artifactNode, parsed);
const omissions = omittedCounts(parsed);
const relations = addMemberRelations(state, parsed);
const omissions = assessManagedGraphOmissions(
{
artifact: parsed.artifact?.result ?? null,
members: parsed.members?.result ?? null,
boundaries: parsed.boundaries?.result ?? null,
},
relations,
);
const limitations = projectionLimitations(parsed, omissions);
const graph = createJavaScriptApplicationGraph({
schema: "JavaScriptApplicationGraph",
Expand Down Expand Up @@ -176,6 +205,8 @@ export const projectManagedApplicationGraph = (
graph.nodes,
"managed-native-implementation",
),
calls: countEdges(graph.edges, "calls"),
field_accesses: countEdges(graph.edges, "accesses"),
},
graph,
coverage: managedGraphResultCoverage(omissions),
Expand All @@ -196,6 +227,11 @@ const countNodes = (
kind: ApplicationNode["kind"],
): number => nodes.filter((node) => node.kind === kind).length;

const countEdges = (
edges: readonly ApplicationEdge[],
relation: ApplicationEdge["relation"],
): number => edges.filter((edge) => edge.relation === relation).length;

const parseManagedInputs = (
input: ProjectManagedApplicationGraphInput,
): ParsedManagedGraphInput => ({
Expand Down Expand Up @@ -298,16 +334,9 @@ const assertConsistentModuleIdentity = (
}
};

const omittedCounts = (parsed: ParsedManagedGraphInput) =>
assessManagedGraphOmissions({
artifact: parsed.artifact?.result ?? null,
members: parsed.members?.result ?? null,
boundaries: parsed.boundaries?.result ?? null,
});

const projectionLimitations = (
parsed: ParsedManagedGraphInput,
omissions: ReturnType<typeof omittedCounts>,
omissions: ManagedGraphProjectionOmissions,
): string[] => [
"Managed graph projection preserves static metadata observations only; it does not execute managed code, load assemblies, or map managed tokens to native addresses.",
...(parsed.members === null
Expand Down Expand Up @@ -338,4 +367,5 @@ const projectionLimitations = (
"Managed boundary tokens were not linked to member nodes because matching module MVID scope was not established.",
]
: []),
...relationOmissionLimitations(omissions.relations),
];
43 changes: 41 additions & 2 deletions src/domain/managed/managedApplicationGraphCoverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ import { managedTokenScopesMatch } from "./managedInspectionEvidence.js";

type ManagedCoverageState = "complete" | "partial" | "unavailable";

/** Why a call site did not become a `calls` edge. */
export type OmittedCallReason =
| "member-ref"
| "method-spec"
| "unknown-target"
| "method-reference"
| "absent-node"
| "self-call";

/** Why a field-access site did not become an `accesses` edge. */
export type OmittedFieldAccessReason = "member-ref" | "absent-node";

/** Member-Evidence sites the projection counted but did not turn into edges. */
export interface ManagedRelationOmissions {
readonly callSites: Readonly<Record<OmittedCallReason, number>>;
readonly fieldAccessSites: Readonly<Record<OmittedFieldAccessReason, number>>;
}

/** Total sites counted but not projected. */
export const omittedRelationSites = (
omissions: ManagedRelationOmissions | null,
): number =>
omissions === null
? 0
: [
...Object.values(omissions.callSites),
...Object.values(omissions.fieldAccessSites),
].reduce((total, count) => total + count, 0);

/** Source observations used to derive managed graph coverage. */
export interface ManagedGraphCoverageSources {
readonly artifact: ManagedArtifactInspection | null;
Expand All @@ -21,13 +50,19 @@ export interface ManagedGraphProjectionOmissions {
readonly unscopedTokenRelationships: boolean;
/** Declaring types the member Evidence selected; null when it inspected every type. */
readonly selectedTypes: readonly string[] | null;
/** Counted call and field-access sites without edges; null without member Evidence. */
readonly relations: ManagedRelationOmissions | null;
}

const omitsAny = (omissions: ManagedGraphProjectionOmissions): boolean =>
/** Omissions whose size is unknown, unlike counted relation sites. */
const omitsUncounted = (omissions: ManagedGraphProjectionOmissions): boolean =>
omissions.partialInput ||
omissions.unscopedTokenRelationships ||
omissions.selectedTypes !== null;

const omitsAny = (omissions: ManagedGraphProjectionOmissions): boolean =>
omitsUncounted(omissions) || omittedRelationSites(omissions.relations) > 0;

const completeCoverage = (): ApplicationGraphEvidence["coverage"] => ({
status: "complete",
truncated: false,
Expand All @@ -51,7 +86,9 @@ export const managedSourceCoverage = (
/** Determine whether any supplied managed inspection was partial. */
export const assessManagedGraphOmissions = (
sources: ManagedGraphCoverageSources,
relations: ManagedRelationOmissions | null,
): ManagedGraphProjectionOmissions => ({
relations,
unscopedTokenRelationships:
sources.members !== null &&
sources.boundaries !== null &&
Expand Down Expand Up @@ -79,7 +116,9 @@ export const managedGraphEvidenceCoverage = (
? {
status: "partial",
truncated: false,
omitted_count: null,
omitted_count: omitsUncounted(omissions)
? null
: omittedRelationSites(omissions.relations),
limits: [],
}
: completeCoverage();
Expand Down
2 changes: 1 addition & 1 deletion src/domain/managed/managedApplicationGraphNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ const artifactLocalIdentity = (
});

/** Build managed static-analysis evidence for a graph observation. */
const sourceManagedEvidence = (
export const sourceManagedEvidence = (
state: GraphBuildState,
source: Evidence,
coverageState: ManagedArtifactInspection["coverage"]["state"],
Expand Down
Loading
Loading