Merge pull request #843 from morluto/fix/exact-empty-graph-identities
fix(javascript): keep exact empty values in graph identities
This commit is contained in:
@@ -26,4 +26,27 @@ describe("input issue projection", () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("reports union branch issues at the union's path once", () => {
|
||||
const schema = z.object({
|
||||
seed: z.union([
|
||||
z.object({ value: z.string().min(1) }),
|
||||
z.object({ value: z.literal(""), match: z.literal("exact") }),
|
||||
]),
|
||||
});
|
||||
const missing = schema.safeParse({});
|
||||
if (missing.success) throw new Error("expected invalid input");
|
||||
expect(projectInputIssues(missing.error.issues, {})).toEqual([
|
||||
{ path: ["seed"], reason: "missing_argument", expected: "object" },
|
||||
]);
|
||||
|
||||
const input = { seed: { value: 1 } };
|
||||
const nested = schema.safeParse(input);
|
||||
if (nested.success) throw new Error("expected invalid input");
|
||||
expect(projectInputIssues(nested.error.issues, input)).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ path: ["seed", "value"], reason: "invalid_type", expected: "string" },
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,9 +15,17 @@ const projectIssue = (
|
||||
const path = issue.path.flatMap((part) =>
|
||||
typeof part === "string" || typeof part === "number" ? [part] : [],
|
||||
);
|
||||
// Branch issue paths are relative to the union; branches often agree.
|
||||
if (issue.code === "invalid_union")
|
||||
return issue.errors.flatMap((branch) =>
|
||||
branch.flatMap((branchIssue) => projectIssue(branchIssue, input)),
|
||||
return uniqueIssues(
|
||||
issue.errors.flatMap((branch) =>
|
||||
branch.flatMap((branchIssue) =>
|
||||
projectIssue(
|
||||
{ ...branchIssue, path: [...issue.path, ...branchIssue.path] },
|
||||
input,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (issue.code === "unrecognized_keys")
|
||||
return issue.keys.map((key) => ({
|
||||
@@ -58,6 +66,12 @@ const projectIssue = (
|
||||
];
|
||||
};
|
||||
|
||||
const uniqueIssues = (
|
||||
issues: readonly AnalysisInputIssue[],
|
||||
): AnalysisInputIssue[] => [
|
||||
...new Map(issues.map((issue) => [JSON.stringify(issue), issue])).values(),
|
||||
];
|
||||
|
||||
const valueAtPath = (
|
||||
input: unknown,
|
||||
path: readonly (string | number)[],
|
||||
|
||||
@@ -419,7 +419,8 @@ export const applicationNodeIdentitySchema = z.discriminatedUnion("strategy", [
|
||||
strategy: z.literal("source-map-original"),
|
||||
stability: z.literal("source-map-exact"),
|
||||
source_map_sha256: digestSchema,
|
||||
original_source: boundedTextSchema,
|
||||
// The exact `sources` entry; `""` is a legal relative reference.
|
||||
original_source: z.string(),
|
||||
source_sha256: digestSchema.nullable(),
|
||||
}),
|
||||
z.strictObject({
|
||||
@@ -451,7 +452,8 @@ export const applicationNodeIdentitySchema = z.discriminatedUnion("strategy", [
|
||||
stability: z.literal("artifact-version"),
|
||||
artifact_sha256: digestSchema,
|
||||
namespace: boundedKeySchema,
|
||||
key: boundedTextSchema,
|
||||
// The exact artifact-local value, such as `""` from `fetch("")`.
|
||||
key: z.string(),
|
||||
}),
|
||||
z.strictObject({
|
||||
strategy: z.literal("runtime-instance"),
|
||||
|
||||
@@ -229,8 +229,11 @@ const semanticValue = (
|
||||
node: ApplicationNode,
|
||||
properties: Readonly<Record<string, JsonValue>>,
|
||||
): string | null => {
|
||||
if (node.kind === "endpoint")
|
||||
return joined(properties.endpoint_kind, properties.value);
|
||||
if (node.kind === "endpoint") {
|
||||
const kind = nonEmptyString(properties.endpoint_kind);
|
||||
const value = exactString(properties.value);
|
||||
return kind === null || value === null ? null : `${kind}\0${value}`;
|
||||
}
|
||||
if (node.kind === "storage")
|
||||
return joined(
|
||||
properties.storage_kind,
|
||||
@@ -239,9 +242,13 @@ const semanticValue = (
|
||||
if (node.kind === "ipc-channel")
|
||||
return properties.resolution === "dynamic"
|
||||
? null
|
||||
: (nonEmptyString(properties.channel) ?? firstLabel(node));
|
||||
: (exactString(properties.channel) ?? firstLabel(node));
|
||||
if (node.kind === "context-bridge-api")
|
||||
return firstLabel(node) ?? nonEmptyString(properties.api_name);
|
||||
return (
|
||||
firstLabel(node) ??
|
||||
exactString(properties.api_key) ??
|
||||
nonEmptyString(properties.api_name)
|
||||
);
|
||||
if (node.kind === "native-export")
|
||||
return (
|
||||
joined(
|
||||
@@ -293,6 +300,10 @@ const joined = (...values: readonly unknown[]): string | null => {
|
||||
const nonEmptyString = (value: unknown): string | null =>
|
||||
typeof value === "string" && value !== "" ? value : null;
|
||||
|
||||
/** A literal value such as `ipcMain.handle("")` keeps its exact empty text. */
|
||||
const exactString = (value: unknown): string | null =>
|
||||
typeof value === "string" ? value : null;
|
||||
|
||||
const stringList = (value: unknown): string[] =>
|
||||
Array.isArray(value)
|
||||
? value
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { compareCodePoints } from "./canonicalOrdering.js";
|
||||
import type { ApplicationNode } from "./javascriptApplicationGraph.js";
|
||||
import type { ApplicationFeatureSeed } from "./javascriptFeatureTraceSchemas.js";
|
||||
import {
|
||||
featureSeedMatchMode,
|
||||
type ApplicationFeatureSeed,
|
||||
} from "./javascriptFeatureTraceSchemas.js";
|
||||
import type { JsonValue } from "./jsonValue.js";
|
||||
|
||||
/** One explicit field that matched a feature seed. */
|
||||
@@ -49,7 +52,7 @@ const seedMatch = (
|
||||
]
|
||||
: [];
|
||||
if (!kindMatches(node, seed.kind)) return [];
|
||||
const mode = seed.match ?? (seed.kind === "string" ? "contains" : "exact");
|
||||
const mode = featureSeedMatchMode(seed);
|
||||
const query = normalized(seed.value, seed.case_sensitive);
|
||||
const match = candidateFields(node, seed.kind).find((candidate) => {
|
||||
const value = normalized(candidate.value, seed.case_sensitive);
|
||||
@@ -129,7 +132,7 @@ const memberFieldSuffixes = new Set([
|
||||
|
||||
const relevantFields = {
|
||||
route: [".label", ".value", ".path"],
|
||||
api: [".label", ".api_name", ".key", ".methods", ".members"],
|
||||
api: [".label", ".api_name", ".api_key", ".key", ".methods", ".members"],
|
||||
channel: [".label", ".channel", ".key"],
|
||||
module: [
|
||||
".label",
|
||||
|
||||
@@ -10,21 +10,41 @@ const nodeIdSchema = prefixedDigestSchema("jag_node");
|
||||
const edgeIdSchema = prefixedDigestSchema("jag_edge");
|
||||
const boundedTextSchema = z.string().min(1);
|
||||
|
||||
/** Literal starting point for one application feature trace. */
|
||||
const applicationFeatureSeedSchema = z.strictObject({
|
||||
kind: z.enum([
|
||||
"node-id",
|
||||
"route",
|
||||
"string",
|
||||
"api",
|
||||
"channel",
|
||||
"module",
|
||||
"native-export",
|
||||
]),
|
||||
value: boundedTextSchema,
|
||||
match: z.enum(["exact", "contains"]).nullable().default(null),
|
||||
case_sensitive: z.boolean().default(false),
|
||||
});
|
||||
/** Matching mode for a literal seed: string seeds default to containment. */
|
||||
export const featureSeedMatchMode = (seed: {
|
||||
readonly kind: string;
|
||||
readonly match: "exact" | "contains" | null;
|
||||
}): "exact" | "contains" =>
|
||||
seed.match ?? (seed.kind === "string" ? "contains" : "exact");
|
||||
|
||||
const literalSeedKinds = [
|
||||
"route",
|
||||
"string",
|
||||
"api",
|
||||
"channel",
|
||||
"module",
|
||||
"native-export",
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Literal starting point for one application feature trace. An empty value,
|
||||
* such as the channel in `ipcMain.handle("")`, must be an explicit exact
|
||||
* literal seed: empty containment would select every node.
|
||||
*/
|
||||
const applicationFeatureSeedSchema = z.union([
|
||||
z.strictObject({
|
||||
kind: z.enum(["node-id", ...literalSeedKinds]),
|
||||
value: boundedTextSchema,
|
||||
match: z.enum(["exact", "contains"]).nullable().default(null),
|
||||
case_sensitive: z.boolean().default(false),
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.enum(literalSeedKinds),
|
||||
value: z.literal(""),
|
||||
match: z.literal("exact"),
|
||||
case_sensitive: z.boolean().default(false),
|
||||
}),
|
||||
]);
|
||||
|
||||
/** Evidence-backed application graph and feature seed. */
|
||||
export const traceApplicationFeatureInputSchema = z
|
||||
|
||||
Reference in New Issue
Block a user