feat(native): add inspection primitives, dispatch traces and approved UI observation (#500)
* fix(hopper): normalize string-backed addresses and update verification * feat(native): add inspection primitives, dispatch traces and approved UI observation * test: prioritize real artifact E2E and captured golden regressions * fix(process): queue observed output before reactive rendering actions * test: prune redundant cases and strengthen boundary assertions * fix(cli): route native value tracing through shared workflows * test: replace synthetic success with real workflows and captured data * fix(catalog): separate portable metadata from native host availability
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
import { z } from "zod";
|
||||
import { jsonValueSchema, type JsonValue } from "./jsonValue.js";
|
||||
|
||||
/** Select one archive within the active bundle and optionally one named root. */
|
||||
export const keyedArchiveInputSchema = z.strictObject({
|
||||
path: z.string().min(1).default("."),
|
||||
root: z.string().optional(),
|
||||
offset: z.number().int().min(0).default(0),
|
||||
limit: z.number().int().min(1).max(20_000).default(20_000),
|
||||
});
|
||||
const linkSchema = z.strictObject({
|
||||
source: z.number().int().nonnegative().nullable(),
|
||||
path: z.array(z.union([z.string(), z.number().int().nonnegative()])),
|
||||
target: z.number().int().nonnegative().nullable(),
|
||||
status: z.enum(["resolved", "nil", "unresolved", "malformed"]),
|
||||
raw: jsonValueSchema,
|
||||
});
|
||||
/** Archive object identity is its original $objects index, never a recursive copy. */
|
||||
export const keyedArchiveResultSchema = z.strictObject({
|
||||
target_sha256: z.string(),
|
||||
archive_path: z.string(),
|
||||
archive_sha256: z.string(),
|
||||
archive_format: z.enum(["binary-plist", "xml-plist"]),
|
||||
roots: z.record(z.string(), jsonValueSchema),
|
||||
objects: z.array(
|
||||
z.strictObject({
|
||||
id: z.number().int().nonnegative(),
|
||||
kind: z.enum([
|
||||
"nil",
|
||||
"scalar",
|
||||
"array",
|
||||
"dictionary",
|
||||
"class-descriptor",
|
||||
"archived-object",
|
||||
]),
|
||||
class_id: z.number().int().nonnegative().nullable(),
|
||||
class_name: z.string().nullable(),
|
||||
status: z.enum(["observed", "unknown-class", "unresolved-class", "nil"]),
|
||||
value: jsonValueSchema,
|
||||
}),
|
||||
),
|
||||
references: z.array(linkSchema),
|
||||
total_objects: z.number().int().nonnegative(),
|
||||
total_references: z.number().int().nonnegative(),
|
||||
offset: z.number().int().nonnegative(),
|
||||
next_offset: z.number().int().nonnegative().nullable(),
|
||||
truncated: z.boolean(),
|
||||
limitations: z.array(z.string()),
|
||||
});
|
||||
|
||||
const record = (
|
||||
value: JsonValue | undefined,
|
||||
): Record<string, JsonValue> | undefined =>
|
||||
value !== null && typeof value === "object" && !Array.isArray(value)
|
||||
? value
|
||||
: undefined;
|
||||
const uid = (value: JsonValue | undefined): JsonValue | undefined => {
|
||||
const object = record(value);
|
||||
if (object === undefined || Object.keys(object).length !== 1)
|
||||
return undefined;
|
||||
return Object.hasOwn(object, "UID") ? object.UID : object["CF$UID"];
|
||||
};
|
||||
|
||||
/** Interpret serialized Foundation references without instantiating archive classes. */
|
||||
export const projectKeyedArchive = (
|
||||
value: unknown,
|
||||
selection: { root?: string | undefined; offset: number; limit: number },
|
||||
) => {
|
||||
const archive = record(jsonValueSchema.parse(value));
|
||||
if (
|
||||
archive?.$archiver !== "NSKeyedArchiver" ||
|
||||
!Array.isArray(archive.$objects)
|
||||
)
|
||||
throw new TypeError("Expected NSKeyedArchiver with an $objects array");
|
||||
const table = archive.$objects;
|
||||
if (table.length > 200_000)
|
||||
throw new RangeError("Archive exceeds 200000 object-table entries");
|
||||
const top = record(archive.$top);
|
||||
if (top === undefined || Object.keys(top).length === 0)
|
||||
throw new TypeError("Keyed archive has no $top roots");
|
||||
if (selection.root !== undefined && !Object.hasOwn(top, selection.root))
|
||||
throw new TypeError(`Archive root does not exist: ${selection.root}`);
|
||||
const roots =
|
||||
selection.root === undefined
|
||||
? top
|
||||
: { [selection.root]: top[selection.root] ?? null };
|
||||
const references: z.infer<typeof linkSchema>[] = [];
|
||||
const scan = (source: number | null, initial: JsonValue) => {
|
||||
const pending: { value: JsonValue; path: (string | number)[] }[] = [
|
||||
{ value: initial, path: [] },
|
||||
];
|
||||
let visited = 0;
|
||||
while (pending.length > 0) {
|
||||
const item = pending.pop();
|
||||
if (item === undefined) break;
|
||||
if (++visited > 200_000 || item.path.length > 128)
|
||||
throw new RangeError(
|
||||
"Archive exceeds 200000 values per object or 128 nesting levels",
|
||||
);
|
||||
const target = uid(item.value);
|
||||
if (target !== undefined) {
|
||||
const valid =
|
||||
typeof target === "number" &&
|
||||
Number.isSafeInteger(target) &&
|
||||
target >= 0;
|
||||
const index = valid ? target : null;
|
||||
references.push({
|
||||
source,
|
||||
path: item.path,
|
||||
target: index,
|
||||
raw: item.value,
|
||||
status:
|
||||
index === null
|
||||
? "malformed"
|
||||
: index >= table.length
|
||||
? "unresolved"
|
||||
: index === 0 && table[0] === "$null"
|
||||
? "nil"
|
||||
: "resolved",
|
||||
});
|
||||
} else if (Array.isArray(item.value)) {
|
||||
for (let index = item.value.length - 1; index >= 0; index--) {
|
||||
const child = item.value[index];
|
||||
if (child !== undefined)
|
||||
pending.push({ value: child, path: [...item.path, index] });
|
||||
}
|
||||
} else {
|
||||
const fields = record(item.value);
|
||||
if (fields !== undefined)
|
||||
for (const key of Object.keys(fields).sort().reverse()) {
|
||||
const child = fields[key];
|
||||
if (child !== undefined)
|
||||
pending.push({ value: child, path: [...item.path, key] });
|
||||
}
|
||||
}
|
||||
if (references.length > 200_000)
|
||||
throw new RangeError("Archive exceeds 200000 reference edges");
|
||||
}
|
||||
};
|
||||
scan(null, roots);
|
||||
const objects = table.map((value, id) => {
|
||||
scan(id, value);
|
||||
const fields = record(value);
|
||||
const classUid = uid(fields?.$class);
|
||||
const classId =
|
||||
typeof classUid === "number" &&
|
||||
Number.isSafeInteger(classUid) &&
|
||||
classUid >= 0
|
||||
? classUid
|
||||
: null;
|
||||
const descriptor = classId === null ? undefined : record(table[classId]);
|
||||
const className =
|
||||
typeof descriptor?.$classname === "string" ? descriptor.$classname : null;
|
||||
const isNil = id === 0 && value === "$null";
|
||||
return {
|
||||
id,
|
||||
kind: isNil
|
||||
? ("nil" as const)
|
||||
: Array.isArray(value)
|
||||
? ("array" as const)
|
||||
: fields === undefined
|
||||
? ("scalar" as const)
|
||||
: typeof fields.$classname === "string"
|
||||
? ("class-descriptor" as const)
|
||||
: Object.hasOwn(fields, "$class")
|
||||
? ("archived-object" as const)
|
||||
: ("dictionary" as const),
|
||||
class_id: classId,
|
||||
class_name: className,
|
||||
status: isNil
|
||||
? ("nil" as const)
|
||||
: fields !== undefined && Object.hasOwn(fields, "$class")
|
||||
? className === null
|
||||
? ("unresolved-class" as const)
|
||||
: ("unknown-class" as const)
|
||||
: ("observed" as const),
|
||||
value,
|
||||
};
|
||||
});
|
||||
const end = Math.min(objects.length, selection.offset + selection.limit);
|
||||
const page = objects.slice(selection.offset, end);
|
||||
const ids = new Set(page.map(({ id }) => id));
|
||||
return {
|
||||
roots,
|
||||
objects: page,
|
||||
references: references.filter(
|
||||
({ source }) => source === null || ids.has(source),
|
||||
),
|
||||
total_objects: objects.length,
|
||||
total_references: references.length,
|
||||
offset: selection.offset,
|
||||
next_offset: end < objects.length ? end : null,
|
||||
truncated: page.length !== objects.length,
|
||||
limitations: [
|
||||
"Archive classes are never instantiated. Class names and serialized fields are observed; class-specific semantics are unknown.",
|
||||
"Conditional references serialized as UID 0 cannot be distinguished from ordinary nil references. Missing fields remain absent in the raw serialized value.",
|
||||
"Root selection selects the named $top entry; object pagination retains original indices and does not recursively expand references.",
|
||||
],
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user