From 03556539d93376bf8c29e0ea68c031fa6df4c47a Mon Sep 17 00:00:00 2001 From: sid597 Date: Fri, 13 Nov 2026 16:20:56 +0530 Subject: [PATCH 1/7] FEE-890 Add the CrossApp upsert steps and their validators --- .../src/__tests__/crossAppValidators.test.ts | 11 + packages/database/src/crossAppContracts.ts | 9 + packages/database/src/crossAppValidators.ts | 15 +- .../src/lib/__tests__/crossAppUpsert.test.ts | 388 ++++++++++++++++++ .../database/src/lib/crossAppConverters.ts | 3 +- packages/database/src/lib/crossAppUpsert.ts | 297 ++++++++++++++ 6 files changed, 719 insertions(+), 4 deletions(-) create mode 100644 packages/database/src/lib/__tests__/crossAppUpsert.test.ts create mode 100644 packages/database/src/lib/crossAppUpsert.ts diff --git a/packages/database/src/__tests__/crossAppValidators.test.ts b/packages/database/src/__tests__/crossAppValidators.test.ts index c543214ee..a16b5dcf3 100644 --- a/packages/database/src/__tests__/crossAppValidators.test.ts +++ b/packages/database/src/__tests__/crossAppValidators.test.ts @@ -6,6 +6,7 @@ import type { CrossAppRelation, CrossAppRelationTripleSchema, CrossAppRelationTypeSchema, + CrossAppUpsertData, } from "../crossAppContracts"; import { obsidianOriginNodeExample, @@ -17,6 +18,7 @@ import { crossAppRelationTripleSchemaValidator, crossAppRelationTypeSchemaValidator, crossAppRelationValidator, + crossAppUpsertDataValidator, } from "../crossAppValidators"; type JsonObject = Record; @@ -109,6 +111,9 @@ describe("cross-app validators", () => { expectTypeOf< z.output >().branded.toEqualTypeOf(); + expectTypeOf< + z.output + >().branded.toEqualTypeOf(); }); it.each([ @@ -304,6 +309,12 @@ describe("cross-app validators", () => { value: { ...toJson(relation), source: 42 }, path: ["source"], }, + { + validator: crossAppRelationValidator, + when: "relation local id is empty", + value: { ...toJson(relation), localId: "" }, + path: ["localId"], + }, { validator: crossAppRelationValidator, when: "relation rid is not a RID", diff --git a/packages/database/src/crossAppContracts.ts b/packages/database/src/crossAppContracts.ts index 47483c408..0d76a52a8 100644 --- a/packages/database/src/crossAppContracts.ts +++ b/packages/database/src/crossAppContracts.ts @@ -121,3 +121,12 @@ export type CrossAppRelation = CrossAppBase & { destination: LocalId | Rid; /* eslint-enable @typescript-eslint/no-duplicate-type-constituents */ }; + +// The root types written to one space in a single upsert +export type CrossAppUpsertData = { + nodeSchemas?: CrossAppNodeSchema[]; + relationTypeSchemas?: CrossAppRelationTypeSchema[]; + relationTripleSchemas?: CrossAppRelationTripleSchema[]; + nodes?: CrossAppNode[]; + relations?: CrossAppRelation[]; +}; diff --git a/packages/database/src/crossAppValidators.ts b/packages/database/src/crossAppValidators.ts index 0a0e7ff70..ad22cca50 100644 --- a/packages/database/src/crossAppValidators.ts +++ b/packages/database/src/crossAppValidators.ts @@ -26,8 +26,8 @@ const jsonValidator: z.ZodType = z.lazy(() => // `partial` keep the setting. const crossAppBaseValidator = z .object({ - localId: z.string(), - rid: z.string().refine(isRid).optional(), + localId: z.string().min(1), + rid: z.string().refine(isRid, "Not an https:// or orn: RID").optional(), createdAt: dateValidator, modifiedAt: dateValidator.optional(), authorId: z.string(), @@ -115,3 +115,14 @@ export const crossAppRelationValidator = crossAppBaseValidator.extend({ source: z.string(), destination: z.string(), }); + +export const crossAppUpsertDataValidator = z + .object({ + nodeSchemas: z.array(crossAppNodeSchemaValidator), + relationTypeSchemas: z.array(crossAppRelationTypeSchemaValidator), + relationTripleSchemas: z.array(crossAppRelationTripleSchemaValidator), + nodes: z.array(crossAppNodeValidator), + relations: z.array(crossAppRelationValidator), + }) + .partial() + .strict(); diff --git a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts new file mode 100644 index 000000000..73470320d --- /dev/null +++ b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts @@ -0,0 +1,388 @@ +import { describe, expect, it } from "vitest"; +import type { + CrossAppBase, + CrossAppNode, + CrossAppNodeSchema, + CrossAppRelation, + CrossAppRelationTripleSchema, + CrossAppRelationTypeSchema, + CrossAppUpsertData, +} from "../../crossAppContracts"; +import { + findMissingSchemas, + mapUpsertResults, + parseUpsertData, + schemaReferences, + toOrderedEntries, + type StoredSchema, +} from "../crossAppUpsert"; + +type JsonObject = Record; + +const toJson = (value: object): JsonObject => + JSON.parse(JSON.stringify(value)) as JsonObject; + +const without = (value: object, key: string): JsonObject => { + const json = toJson(value); + delete json[key]; + return json; +}; + +const base = (localId: string): CrossAppBase => ({ + localId, + createdAt: new Date("2026-06-14T11:00:00Z"), + authorId: "account-local-1", +}); + +const claimType: CrossAppNodeSchema = { ...base("claim-type"), label: "Claim" }; +const sourceType: CrossAppNodeSchema = { + ...base("source-type"), + label: "Source", +}; +const evidenceType: CrossAppNodeSchema = { + ...base("evidence-type"), + label: "Evidence", + slotDefinitions: { source: "source-type" }, +}; +const supports: CrossAppRelationTypeSchema = { + ...base("supports"), + label: "supports", + complement: "is supported by", +}; +const evidenceSupportsClaim: CrossAppRelationTripleSchema = { + ...base("evidence-supports-claim"), + relation: "supports", + sourceType: "evidence-type", + destinationType: "claim-type", +}; + +const node = ( + localId: string, + nodeType: string, + slots?: Record, +): CrossAppNode => ({ + ...base(localId), + nodeType, + coreTitle: localId, + content: { direct: { value: localId } }, + ...(slots ? { slots } : {}), +}); + +const relation: CrossAppRelation = { + ...base("relation-1"), + relationType: "evidence-supports-claim", + source: "evidence-1", + destination: "claim-1", +}; + +// The citing node comes before its Source, so ordering has to move it. +const graph: CrossAppUpsertData = { + nodeSchemas: [evidenceType, claimType, sourceType], + relationTypeSchemas: [supports], + relationTripleSchemas: [evidenceSupportsClaim], + nodes: [ + node("evidence-1", "evidence-type", { source: "source-1" }), + node("claim-1", "claim-type"), + node("source-1", "source-type"), + ], + relations: [relation], +}; + +const storedSupports: StoredSchema = { + source_local_id: "supports", + literal_content: { + roles: ["source", "destination"], + label: "supports", + complement: "is supported by", + }, +}; + +const SPACE_URL = "https://roamresearch.com/#/app/graph-a"; + +const parse = (body: unknown): ReturnType => + parseUpsertData({ body, spaceUrl: SPACE_URL }); + +const deeplyNested = (depth: number): unknown => + JSON.parse( + `{"nodeSchemas":[{"localId":"deep","createdAt":"2026-06-14T11:00:00Z","authorId":"a","label":"Deep","metadata":${'{"a":'.repeat(depth)}1${"}".repeat(depth)}}]}`, + ); + +describe("parseUpsertData", () => { + it("parses a body sent as JSON back to the contract values", () => { + expect(parse(toJson(graph))).toStrictEqual({ success: true, data: graph }); + }); + + it.each([ + { when: "is empty", body: {} }, + { + when: "has an item whose rid names the path's space", + body: { + nodeSchemas: [toJson({ ...claimType, rid: `${SPACE_URL}/claim-type` })], + }, + }, + { + when: "has a relation whose destination is a RID in another space", + body: { + relations: [ + toJson({ + ...relation, + destination: "https://roamresearch.com/#/app/graph-b/claim-9", + }), + ], + }, + }, + ])("accepts a body that $when", ({ body }) => { + expect(parse(body).success).toBe(true); + }); + + it.each([ + { + when: "reuses a local id in another array", + body: { + nodes: [toJson(node("x", "claim-type"))], + relations: [toJson({ ...relation, localId: "x" })], + }, + issues: [ + { + path: ["relations", 0, "localId"], + message: "Another item in the body has this local id", + }, + ], + }, + { + when: "has a triple schema without a source type", + body: { + relationTripleSchemas: [without(evidenceSupportsClaim, "sourceType")], + }, + issues: [ + { + path: ["relationTripleSchemas", 0, "sourceType"], + message: "Required", + }, + ], + }, + { + when: "has a rid that is not a RID", + body: { relations: [toJson({ ...relation, rid: "relation-1" })] }, + issues: [ + { + path: ["relations", 0, "rid"], + message: "Not an https:// or orn: RID", + }, + ], + }, + ...["https://roamresearch.com/#/app/graph-b/relation-1", "orn:"].map( + (rid) => ({ + when: `has an item whose rid is ${rid}`, + body: { relations: [toJson({ ...relation, rid })] }, + issues: [ + { + path: ["relations", 0, "rid"], + message: "The rid names a space other than the one in the path", + }, + ], + }), + ), + { + when: "has a node with assets", + body: { + nodes: [ + toJson({ + ...node("claim-1", "claim-type"), + assets: [{ sourceRef: "a.png", contentHash: "ab" }], + }), + ], + }, + issues: [ + { + path: ["nodes", 0, "assets"], + message: "This route does not write this field yet", + }, + ], + }, + { + when: "has a schema with metadata", + body: { relationTypeSchemas: [toJson({ ...supports, metadata: {} })] }, + issues: [ + { + path: ["relationTypeSchemas", 0, "metadata"], + message: "This route does not write this field yet", + }, + ], + }, + { + when: "has metadata nested 100,000 levels deep", + body: deeplyNested(100_000), + issues: [{ path: [], message: "The body is nested too deeply" }], + }, + ])("refuses a body that $when", ({ body, issues }) => { + expect(parse(body)).toEqual({ success: false, issues }); + }); + + it("refuses a key outside the five root types", () => { + expect(parse({ edges: [] })).toMatchObject({ + success: false, + issues: [{ path: [] }], + }); + }); +}); + +describe("schemaReferences", () => { + it("lists schema ids that are neither RIDs nor in the body", () => { + const references = schemaReferences({ + nodeSchemas: [evidenceType], + relationTypeSchemas: [supports], + relationTripleSchemas: [ + { + ...evidenceSupportsClaim, + destinationType: "orn:roam.schema:graph-b/claim-type", + }, + { + ...evidenceSupportsClaim, + localId: "evidence-contradicts-claim", + relation: "contradicts", + }, + ], + nodes: [ + node("claim-1", "claim-type"), + node("other-1", "https://example.com/space-b/other-type"), + ], + relations: [ + relation, + { ...relation, localId: "relation-2", relationType: "claim-on-claim" }, + ], + }); + expect(references.sort()).toEqual([ + "claim-on-claim", + "claim-type", + "contradicts", + "source-type", + ]); + }); + + it("keeps a triple's relation type when it is a RID", () => { + const rid = "orn:roam.schema:graph-b/supports"; + expect( + schemaReferences({ + nodeSchemas: [claimType, evidenceType, sourceType], + relationTripleSchemas: [{ ...evidenceSupportsClaim, relation: rid }], + }), + ).toEqual([rid]); + }); +}); + +describe("findMissingSchemas", () => { + const nodesOnly: CrossAppUpsertData = { + relationTripleSchemas: [evidenceSupportsClaim], + nodes: [node("claim-1", "claim-type")], + }; + + it("returns the references the database does not hold", () => { + expect( + findMissingSchemas({ + data: nodesOnly, + storedSchemas: [ + storedSupports, + { + source_local_id: "claim-type", + literal_content: { label: "Claim" }, + }, + ], + }).sort(), + ).toEqual(["evidence-type"]); + }); + + it("counts a stored relation type without label and complement as missing", () => { + expect( + findMissingSchemas({ + data: { relationTripleSchemas: [evidenceSupportsClaim] }, + storedSchemas: [ + { source_local_id: "supports", literal_content: { label: "x" } }, + { source_local_id: "evidence-type", literal_content: {} }, + { source_local_id: "claim-type", literal_content: {} }, + ], + }), + ).toEqual(["supports"]); + }); +}); + +describe("toOrderedEntries", () => { + it("puts every concept after the concepts it references", () => { + const ids = toOrderedEntries({ data: graph, storedSchemas: [] }).map( + (e) => e.localId, + ); + expect([...ids].sort()).toEqual( + [ + "claim-1", + "claim-type", + "evidence-1", + "evidence-supports-claim", + "evidence-type", + "relation-1", + "source-1", + "source-type", + "supports", + ].sort(), + ); + const before = (first: string, second: string): void => + expect(ids.indexOf(first)).toBeLessThan(ids.indexOf(second)); + before("source-type", "evidence-type"); + before("evidence-type", "evidence-1"); + before("source-type", "source-1"); + before("source-1", "evidence-1"); + before("supports", "evidence-supports-claim"); + before("evidence-supports-claim", "relation-1"); + before("evidence-1", "relation-1"); + before("claim-1", "relation-1"); + }); + + it.each([ + { from: "the body", data: graph, storedSchemas: [] }, + { + from: "the database", + data: { relationTripleSchemas: [evidenceSupportsClaim] }, + storedSchemas: [storedSupports], + }, + ])( + "gives a triple its relation type's label and complement from $from", + ({ data, storedSchemas }) => { + const triple = toOrderedEntries({ data, storedSchemas }).find( + (e) => e.localId === "evidence-supports-claim", + ); + expect(triple?.concept.literal_content).toEqual({ + roles: ["source", "destination"], + label: "supports", + complement: "is supported by", + }); + }, + ); + + it("throws for a triple whose relation type was not found", () => { + expect(() => + toOrderedEntries({ + data: { relationTripleSchemas: [evidenceSupportsClaim] }, + storedSchemas: [], + }), + ).toThrow("evidence-supports-claim"); + }); +}); + +describe("mapUpsertResults", () => { + it("maps each result to the local id at its position after ordering", () => { + const entries = toOrderedEntries({ data: graph, storedSchemas: [] }); + const failures: Record = { + "evidence-1": -1, + "relation-1": -2, + }; + const ids = entries.map((e, index) => failures[e.localId] ?? 100 + index); + const { upserted, failed } = mapUpsertResults({ entries, ids }); + expect(failed).toEqual([ + { localId: "evidence-1", kind: "nodes", reason: "name_conflict" }, + { localId: "relation-1", kind: "relations", reason: "error" }, + ]); + expect(upserted["source-1"]).toBe( + 100 + entries.findIndex((e) => e.localId === "source-1"), + ); + expect(Object.keys(upserted)).toHaveLength(entries.length - 2); + }); +}); diff --git a/packages/database/src/lib/crossAppConverters.ts b/packages/database/src/lib/crossAppConverters.ts index 88416c5c1..778f7c609 100644 --- a/packages/database/src/lib/crossAppConverters.ts +++ b/packages/database/src/lib/crossAppConverters.ts @@ -153,8 +153,7 @@ export const crossAppRelationTypeSchemaToDbConcept = ( export const crossAppRelationTripleSchemaToDbConcept = ( node: CrossAppRelationTripleSchema, - // used in Obsidian, not yet in Roam. - relationType?: CrossAppRelationTypeSchema, + relationType?: Pick, ): LocalConceptDataInput | undefined => { if (!("label" in node) && relationType === undefined) return undefined; const label = "label" in node ? node.label : relationType!.label; diff --git a/packages/database/src/lib/crossAppUpsert.ts b/packages/database/src/lib/crossAppUpsert.ts new file mode 100644 index 000000000..2ebb730b7 --- /dev/null +++ b/packages/database/src/lib/crossAppUpsert.ts @@ -0,0 +1,297 @@ +import type { ZodIssue } from "zod"; +import type { + CrossAppBase, + CrossAppRelationTypeSchema, + CrossAppUpsertData, + LocalId, +} from "../crossAppContracts"; +import { crossAppUpsertDataValidator } from "../crossAppValidators"; +import type { Json, Tables } from "../dbTypes"; +import type { LocalConceptDataInput } from "../inputTypes"; +import { + crossAppNodeSchemaToDbConcept, + crossAppNodeToDbConcept, + crossAppRelationToDbConcept, + crossAppRelationTripleSchemaToDbConcept, + crossAppRelationTypeSchemaToDbConcept, +} from "./crossAppConverters"; +import { isRid, ridToSpaceUriAndLocalId } from "./rid"; + +const UPSERT_KINDS = crossAppUpsertDataValidator.keyof().options; + +type UpsertKind = (typeof UPSERT_KINDS)[number]; + +type ItemOf = NonNullable[number]; + +// Fields the contract allows but no converter writes yet. They are refused, not +// dropped. Remove an entry once the route writes that field. +const UNWRITTEN_FIELDS: Partial> = { + nodeSchemas: "metadata", + relationTypeSchemas: "metadata", + relationTripleSchemas: "metadata", + nodes: "assets", +}; + +type UpsertIssue = { path: (string | number)[]; message: string }; + +type UpsertEntry = { + localId: LocalId; + kind: UpsertKind; + concept: LocalConceptDataInput; +}; + +type UpsertFailure = Omit & { + reason: "name_conflict" | "error"; +}; + +export type StoredSchema = Pick< + Tables<"my_concepts">, + "source_local_id" | "literal_content" +>; + +type RelationLabels = Pick; + +const upsertItems = ( + data: CrossAppUpsertData, +): { kind: UpsertKind; index: number; item: CrossAppBase }[] => + UPSERT_KINDS.flatMap((kind) => { + const items: CrossAppBase[] = data[kind] ?? []; + return items.map((item, index) => ({ kind, index, item })); + }); + +const toUpsertIssues = (issue: ZodIssue): UpsertIssue[] => { + if (issue.code === "invalid_union") { + // A union reports its failure at its own path; the branch with the fewest + // issues is the one the caller meant, and its issues name the field. + const closest = issue.unionErrors.reduce((best, error) => + error.issues.length < best.issues.length ? error : best, + ); + return closest.issues.flatMap(toUpsertIssues); + } + return [{ path: issue.path, message: issue.message }]; +}; + +const findItemIssues = ({ + data, + spaceUrl, +}: { + data: CrossAppUpsertData; + spaceUrl: string; +}): UpsertIssue[] => { + const seen = new Set(); + return upsertItems(data).flatMap(({ kind, index, item }) => { + const issues: UpsertIssue[] = []; + const unwritten = UNWRITTEN_FIELDS[kind]; + if (seen.has(item.localId)) + issues.push({ + path: [kind, index, "localId"], + message: "Another item in the body has this local id", + }); + seen.add(item.localId); + // An item's own rid sets the space its row is written to, so it must name + // the space in the path. A RID used as a reference may name any space. + if ( + item.rid !== undefined && + ridToSpaceUriAndLocalId(item.rid).spaceUri !== spaceUrl + ) + issues.push({ + path: [kind, index, "rid"], + message: "The rid names a space other than the one in the path", + }); + if (unwritten !== undefined && unwritten in item) + issues.push({ + path: [kind, index, unwritten], + message: "This route does not write this field yet", + }); + return issues; + }); +}; + +type ParseResult = + | { success: true; data: CrossAppUpsertData } + | { success: false; issues: UpsertIssue[] }; + +const refuse = (issues: UpsertIssue[]): ParseResult => ({ + success: false, + issues, +}); + +export const parseUpsertData = ({ + body, + spaceUrl, +}: { + body: unknown; + spaceUrl: string; +}): ParseResult => { + let result: ReturnType; + try { + result = crossAppUpsertDataValidator.safeParse(body); + } catch (error) { + // Zod recurses once per nesting level of `metadata` and throws near 2,000 + // levels instead of returning a failure. + if (error instanceof RangeError) + return refuse([{ path: [], message: "The body is nested too deeply" }]); + throw error; + } + if (!result.success) + return refuse(result.error.issues.flatMap(toUpsertIssues)); + const issues = findItemIssues({ data: result.data, spaceUrl }); + if (issues.length > 0) return refuse(issues); + return { success: true, data: result.data }; +}; + +// upsert_concepts stores a node or relation whose schema is unknown with a NULL +// schema_id and no error. A triple naming its relation type takes that type's +// label and complement, so it must be in the body or this space, not a RID. +export const schemaReferences = (data: CrossAppUpsertData): LocalId[] => { + const relationTypeIds = new Set( + (data.relationTypeSchemas ?? []).map((s) => s.localId), + ); + const schemaIds = new Set([ + ...relationTypeIds, + ...(data.nodeSchemas ?? []).map((s) => s.localId), + ...(data.relationTripleSchemas ?? []).map((s) => s.localId), + ]); + const references = [ + ...(data.nodeSchemas ?? []).flatMap((s) => + Object.values(s.slotDefinitions ?? {}), + ), + ...(data.relationTripleSchemas ?? []).flatMap((s) => [ + s.sourceType, + s.destinationType, + ]), + ...(data.nodes ?? []).map((n) => n.nodeType), + ...(data.relations ?? []).map((r) => r.relationType), + ].filter( + (id): id is LocalId => id !== undefined && !isRid(id) && !schemaIds.has(id), + ); + const relationTypes = (data.relationTripleSchemas ?? []).flatMap((s) => + s.relation === undefined || relationTypeIds.has(s.relation) + ? [] + : [s.relation], + ); + return [...new Set([...references, ...relationTypes])]; +}; + +const relationLabelsById = ({ + data, + storedSchemas, +}: { + data: CrossAppUpsertData; + storedSchemas: StoredSchema[]; +}): Map => + new Map([ + ...storedSchemas.flatMap(({ source_local_id, literal_content }) => { + const { label, complement } = literal_content as Record; + return source_local_id !== null && + typeof label === "string" && + typeof complement === "string" + ? [[source_local_id, { label, complement }] as const] + : []; + }), + ...(data.relationTypeSchemas ?? []).map((s) => [s.localId, s] as const), + ]); + +export const findMissingSchemas = ({ + data, + storedSchemas, +}: { + data: CrossAppUpsertData; + storedSchemas: StoredSchema[]; +}): LocalId[] => { + const storedIds = new Set(storedSchemas.map((s) => s.source_local_id)); + const relationLabels = relationLabelsById({ data, storedSchemas }); + const tripleRelations = new Set( + (data.relationTripleSchemas ?? []).map((s) => s.relation), + ); + return schemaReferences(data).filter((id) => + tripleRelations.has(id) ? !relationLabels.has(id) : !storedIds.has(id), + ); +}; + +const relatedConcepts = (concept: LocalConceptDataInput): string[] => + [ + ...(Object.values( + concept.local_reference_content ?? {}, + ).flat() as unknown[]), + concept.schema_represented_by_local_id, + ].filter((id): id is string => typeof id === "string"); + +// upsert_concepts resolves a reference only to a row already written. Local ids +// are caller input and may be "constructor", so they key a Map, not an object. +const orderByDependency = (entries: UpsertEntry[]): UpsertEntry[] => { + const remainder = new Map(entries.map((e) => [e.localId, e])); + const processed = new Set(); + const ordered: UpsertEntry[] = []; + const visit = (entry: UpsertEntry): void => { + processed.add(entry.localId); + for (const id of relatedConcepts(entry.concept)) { + const related = remainder.get(id); + if (related !== undefined && !processed.has(id)) visit(related); + } + ordered.push(entry); + remainder.delete(entry.localId); + }; + for (const entry of remainder.values()) visit(entry); + return ordered; +}; + +export const toOrderedEntries = ({ + data, + storedSchemas, +}: { + data: CrossAppUpsertData; + storedSchemas: StoredSchema[]; +}): UpsertEntry[] => { + const relationLabels = relationLabelsById({ data, storedSchemas }); + const converters: { + [K in UpsertKind]: (item: ItemOf) => LocalConceptDataInput; + } = { + nodeSchemas: crossAppNodeSchemaToDbConcept, + relationTypeSchemas: crossAppRelationTypeSchemaToDbConcept, + relationTripleSchemas: (triple) => { + const concept = crossAppRelationTripleSchemaToDbConcept( + triple, + triple.relation === undefined + ? undefined + : relationLabels.get(triple.relation), + ); + if (concept === undefined) + throw new Error(`No relation type found for triple ${triple.localId}`); + return concept; + }, + nodes: crossAppNodeToDbConcept, + relations: crossAppRelationToDbConcept, + }; + const toEntries = (kind: K): UpsertEntry[] => + (data[kind] ?? []).map((item) => ({ + localId: item.localId, + kind, + concept: converters[kind](item), + })); + return orderByDependency(UPSERT_KINDS.flatMap((kind) => toEntries(kind))); +}; + +// upsert_concepts returns one id per input row, in input order: -1 for a name +// conflict and -2 for any other error. +export const mapUpsertResults = ({ + entries, + ids, +}: { + entries: UpsertEntry[]; + ids: number[]; +}): { upserted: Record; failed: UpsertFailure[] } => { + const upserted: [LocalId, number][] = []; + const failed: UpsertFailure[] = []; + entries.forEach(({ localId, kind }, index) => { + const id = ids[index]; + if (id !== undefined && id >= 0) upserted.push([localId, id]); + else + failed.push({ + localId, + kind, + reason: id === -1 ? "name_conflict" : "error", + }); + }); + return { upserted: Object.fromEntries(upserted), failed }; +}; From 432c63fd8d76e29647f66c6f8d7b135063994d7e Mon Sep 17 00:00:00 2001 From: sid597 Date: Fri, 13 Nov 2026 22:06:36 +0530 Subject: [PATCH 2/7] FEE-890 Type the unwritten-field table by item keys and test every entry --- .../src/lib/__tests__/crossAppUpsert.test.ts | 62 +++++++++++-------- packages/database/src/lib/crossAppUpsert.ts | 2 +- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts index 73470320d..1d423f400 100644 --- a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts +++ b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts @@ -183,33 +183,6 @@ describe("parseUpsertData", () => { ], }), ), - { - when: "has a node with assets", - body: { - nodes: [ - toJson({ - ...node("claim-1", "claim-type"), - assets: [{ sourceRef: "a.png", contentHash: "ab" }], - }), - ], - }, - issues: [ - { - path: ["nodes", 0, "assets"], - message: "This route does not write this field yet", - }, - ], - }, - { - when: "has a schema with metadata", - body: { relationTypeSchemas: [toJson({ ...supports, metadata: {} })] }, - issues: [ - { - path: ["relationTypeSchemas", 0, "metadata"], - message: "This route does not write this field yet", - }, - ], - }, { when: "has metadata nested 100,000 levels deep", body: deeplyNested(100_000), @@ -219,6 +192,41 @@ describe("parseUpsertData", () => { expect(parse(body)).toEqual({ success: false, issues }); }); + it.each([ + { kind: "nodeSchemas", item: claimType, field: "metadata", value: {} }, + { + kind: "relationTypeSchemas", + item: supports, + field: "metadata", + value: {}, + }, + { + kind: "relationTripleSchemas", + item: evidenceSupportsClaim, + field: "metadata", + value: {}, + }, + { + kind: "nodes", + item: node("claim-1", "claim-type"), + field: "assets", + value: [{ sourceRef: "a.png", contentHash: "ab" }], + }, + ])( + "refuses $kind with $field, which no converter writes yet", + ({ kind, item, field, value }) => { + expect(parse({ [kind]: [toJson({ ...item, [field]: value })] })).toEqual({ + success: false, + issues: [ + { + path: [kind, 0, field], + message: "This route does not write this field yet", + }, + ], + }); + }, + ); + it("refuses a key outside the five root types", () => { expect(parse({ edges: [] })).toMatchObject({ success: false, diff --git a/packages/database/src/lib/crossAppUpsert.ts b/packages/database/src/lib/crossAppUpsert.ts index 2ebb730b7..93a1ccda1 100644 --- a/packages/database/src/lib/crossAppUpsert.ts +++ b/packages/database/src/lib/crossAppUpsert.ts @@ -25,7 +25,7 @@ type ItemOf = NonNullable[number]; // Fields the contract allows but no converter writes yet. They are refused, not // dropped. Remove an entry once the route writes that field. -const UNWRITTEN_FIELDS: Partial> = { +const UNWRITTEN_FIELDS: { [K in UpsertKind]?: keyof ItemOf } = { nodeSchemas: "metadata", relationTypeSchemas: "metadata", relationTripleSchemas: "metadata", From 1e378add37bb8ff9918839ff450ccd7254ae32d9 Mon Sep 17 00:00:00 2001 From: sid597 Date: Fri, 13 Nov 2026 22:32:15 +0530 Subject: [PATCH 3/7] FEE-890 Write createdAt as last_modified for an item without modifiedAt Concept.last_modified and Content.last_modified are NOT NULL, so an item the contract accepts without modifiedAt failed in upsert_concepts. An item never modified was last modified when it was created. --- .../lib/__tests__/crossAppConverters.test.ts | 87 ++++++++++++++++++- .../src/lib/__tests__/crossAppUpsert.test.ts | 13 +++ .../database/src/lib/crossAppConverters.ts | 12 +-- 3 files changed, 105 insertions(+), 7 deletions(-) diff --git a/packages/database/src/lib/__tests__/crossAppConverters.test.ts b/packages/database/src/lib/__tests__/crossAppConverters.test.ts index 7481b56ba..5490e94c1 100644 --- a/packages/database/src/lib/__tests__/crossAppConverters.test.ts +++ b/packages/database/src/lib/__tests__/crossAppConverters.test.ts @@ -1,8 +1,18 @@ import { describe, expect, it } from "vitest"; -import type { CrossAppNode, CrossAppNodeSchema } from "../../crossAppContracts"; +import type { + CrossAppNode, + CrossAppNodeSchema, + CrossAppRelation, + CrossAppRelationTripleSchema, + CrossAppRelationTypeSchema, +} from "../../crossAppContracts"; import { crossAppNodeSchemaToDbConcept, crossAppNodeToDbConcept, + crossAppNodeToDbContent, + crossAppRelationToDbConcept, + crossAppRelationTripleSchemaToDbConcept, + crossAppRelationTypeSchemaToDbConcept, } from "../crossAppConverters"; const baseSchema: CrossAppNodeSchema = { @@ -139,3 +149,78 @@ describe("crossAppNodeToDbConcept", () => { ).toEqual(["direct"]); }); }); + +describe("an item without modifiedAt", () => { + const CREATED = "2026-06-14T11:00:00.000Z"; + const relationType: CrossAppRelationTypeSchema = { + localId: "supports", + createdAt: new Date(CREATED), + authorId: "account-local-1", + label: "supports", + complement: "is supported by", + }; + const triple: CrossAppRelationTripleSchema = { + localId: "node-supports-concept", + createdAt: new Date(CREATED), + authorId: "account-local-1", + label: "supports", + complement: "is supported by", + sourceType: "concept-1", + destinationType: "concept-1", + }; + const relation: CrossAppRelation = { + localId: "relation-1", + createdAt: new Date(CREATED), + authorId: "account-local-1", + relationType: "node-supports-concept", + source: "node-2", + destination: "node-1", + }; + + it.each([ + { + converter: "crossAppNodeSchemaToDbConcept", + convert: () => crossAppNodeSchemaToDbConcept(baseSchema), + }, + { + converter: "crossAppNodeToDbConcept", + convert: () => crossAppNodeToDbConcept(baseNode), + }, + { + converter: "crossAppRelationTypeSchemaToDbConcept", + convert: () => crossAppRelationTypeSchemaToDbConcept(relationType), + }, + { + converter: "crossAppRelationTripleSchemaToDbConcept", + convert: () => crossAppRelationTripleSchemaToDbConcept(triple), + }, + { + converter: "crossAppRelationToDbConcept", + convert: () => crossAppRelationToDbConcept(relation), + }, + ])("$converter writes its createdAt as last_modified", ({ convert }) => { + expect(convert()).toMatchObject({ + created: CREATED, + last_modified: CREATED, + }); + }); + + it("crossAppNodeToDbContent writes the content's own createdAt as last_modified", () => { + const content = crossAppNodeToDbContent( + { + ...baseNode, + content: { + direct: { + value: "EVD - REM sleep and recall", + createdAt: new Date("2026-06-15T09:00:00Z"), + }, + }, + }, + "direct", + ); + expect(content).toMatchObject({ + created: "2026-06-15T09:00:00.000Z", + last_modified: "2026-06-15T09:00:00.000Z", + }); + }); +}); diff --git a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts index 1d423f400..1f9e1efba 100644 --- a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts +++ b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts @@ -365,6 +365,19 @@ describe("toOrderedEntries", () => { }, ); + it("writes a last_modified on every row of a body whose items omit modifiedAt", () => { + expect(JSON.stringify(graph)).not.toContain("modifiedAt"); + const parsed = parse(toJson(graph)); + if (!parsed.success) throw new Error("The graph did not parse"); + const rows = toOrderedEntries({ + data: parsed.data, + storedSchemas: [], + }).flatMap(({ concept }) => [concept, ...(concept.contents_inline ?? [])]); + expect(rows.map((row) => row.last_modified)).toEqual( + new Array(12).fill("2026-06-14T11:00:00.000Z"), + ); + }); + it("throws for a triple whose relation type was not found", () => { expect(() => toOrderedEntries({ diff --git a/packages/database/src/lib/crossAppConverters.ts b/packages/database/src/lib/crossAppConverters.ts index 778f7c609..d9fb7afbb 100644 --- a/packages/database/src/lib/crossAppConverters.ts +++ b/packages/database/src/lib/crossAppConverters.ts @@ -44,7 +44,7 @@ const inlineCrossAppContentToDbContent = ( content_type: content.contentType || "text/plain", variant, created: content.createdAt?.toISOString(), - last_modified: content.modifiedAt?.toISOString(), + last_modified: (content.modifiedAt ?? content.createdAt)?.toISOString(), author_local_id: content.authorId, embedding_inline: crossAppEmbeddingToDbEmbedding(content.embedding), }); @@ -95,7 +95,7 @@ export const crossAppNodeToDbConcept = ( crossAppNodeToDbContent(node, "full"), ]), created: node.createdAt?.toISOString(), - last_modified: node.modifiedAt?.toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), local_reference_content: node.slots, }); }; @@ -125,7 +125,7 @@ export const crossAppNodeSchemaToDbConcept = ( Object.keys(literalInfo).length > 0 ? literalInfo : undefined, local_reference_content: referenceContent, created: node.createdAt?.toISOString(), - last_modified: node.modifiedAt?.toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), }); }; @@ -147,7 +147,7 @@ export const crossAppRelationTypeSchemaToDbConcept = ( complement: node.complement, }, created: node.createdAt?.toISOString(), - last_modified: node.modifiedAt?.toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), }); }; @@ -179,7 +179,7 @@ export const crossAppRelationTripleSchemaToDbConcept = ( destination: node.destinationType, }, created: node.createdAt?.toISOString(), - last_modified: node.modifiedAt?.toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), }); }; @@ -201,6 +201,6 @@ export const crossAppRelationToDbConcept = ( destination: node.destination, }, created: node.createdAt?.toISOString(), - last_modified: node.modifiedAt?.toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), }); }; From 691e8511f223744966667a28fa7dc124d002e8e7 Mon Sep 17 00:00:00 2001 From: sid597 Date: Fri, 13 Nov 2026 22:46:40 +0530 Subject: [PATCH 4/7] FEE-890 Leave last_modified unset, not a throw, for an item without createdAt Only an item with createdAt and no modifiedAt should change. Roam's publish tests mock relations and triples with neither date, and those threw. --- packages/database/src/lib/crossAppConverters.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/database/src/lib/crossAppConverters.ts b/packages/database/src/lib/crossAppConverters.ts index d9fb7afbb..597ce0558 100644 --- a/packages/database/src/lib/crossAppConverters.ts +++ b/packages/database/src/lib/crossAppConverters.ts @@ -95,7 +95,7 @@ export const crossAppNodeToDbConcept = ( crossAppNodeToDbContent(node, "full"), ]), created: node.createdAt?.toISOString(), - last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt)?.toISOString(), local_reference_content: node.slots, }); }; @@ -125,7 +125,7 @@ export const crossAppNodeSchemaToDbConcept = ( Object.keys(literalInfo).length > 0 ? literalInfo : undefined, local_reference_content: referenceContent, created: node.createdAt?.toISOString(), - last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt)?.toISOString(), }); }; @@ -147,7 +147,7 @@ export const crossAppRelationTypeSchemaToDbConcept = ( complement: node.complement, }, created: node.createdAt?.toISOString(), - last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt)?.toISOString(), }); }; @@ -179,7 +179,7 @@ export const crossAppRelationTripleSchemaToDbConcept = ( destination: node.destinationType, }, created: node.createdAt?.toISOString(), - last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt)?.toISOString(), }); }; @@ -201,6 +201,6 @@ export const crossAppRelationToDbConcept = ( destination: node.destination, }, created: node.createdAt?.toISOString(), - last_modified: (node.modifiedAt ?? node.createdAt).toISOString(), + last_modified: (node.modifiedAt ?? node.createdAt)?.toISOString(), }); }; From 890e3ba49c6d259027c2ce02d1e9b4fa1e925ba4 Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 16 Nov 2026 10:04:36 +0530 Subject: [PATCH 5/7] FEE-890 Refuse inline content with a scale other than document --- .../src/lib/__tests__/crossAppUpsert.test.ts | 20 ++++++++++++++++++ packages/database/src/lib/crossAppUpsert.ts | 21 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts index 1f9e1efba..72ed95bcb 100644 --- a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts +++ b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts @@ -227,6 +227,26 @@ describe("parseUpsertData", () => { }, ); + it("refuses inline content with a scale other than document", () => { + const claim = node("claim-1", "claim-type"); + const withScales = { + ...claim, + content: { + direct: { ...claim.content.direct, scale: "document" }, + full: { value: "Body", contentType: "text/plain", scale: "block" }, + }, + }; + expect(parse({ nodes: [toJson(withScales)] })).toEqual({ + success: false, + issues: [ + { + path: ["nodes", 0, "content", "full", "scale"], + message: "This route stores inline content as a document", + }, + ], + }); + }); + it("refuses a key outside the five root types", () => { expect(parse({ edges: [] })).toMatchObject({ success: false, diff --git a/packages/database/src/lib/crossAppUpsert.ts b/packages/database/src/lib/crossAppUpsert.ts index 93a1ccda1..3f87f9346 100644 --- a/packages/database/src/lib/crossAppUpsert.ts +++ b/packages/database/src/lib/crossAppUpsert.ts @@ -107,6 +107,22 @@ const findItemIssues = ({ }); }; +// upsert_concepts stores every inline content with scale "document", so another +// scale is refused, not rewritten. +const findContentScaleIssues = (data: CrossAppUpsertData): UpsertIssue[] => + (data.nodes ?? []).flatMap((node, index) => + Object.entries(node.content).flatMap(([variant, content]) => + content.scale === undefined || content.scale === "document" + ? [] + : [ + { + path: ["nodes", index, "content", variant, "scale"], + message: "This route stores inline content as a document", + }, + ], + ), + ); + type ParseResult = | { success: true; data: CrossAppUpsertData } | { success: false; issues: UpsertIssue[] }; @@ -135,7 +151,10 @@ export const parseUpsertData = ({ } if (!result.success) return refuse(result.error.issues.flatMap(toUpsertIssues)); - const issues = findItemIssues({ data: result.data, spaceUrl }); + const issues = [ + ...findItemIssues({ data: result.data, spaceUrl }), + ...findContentScaleIssues(result.data), + ]; if (issues.length > 0) return refuse(issues); return { success: true, data: result.data }; }; From f2d20764c85354ecb9cb003021689620e19100d6 Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 16 Nov 2026 11:04:47 +0530 Subject: [PATCH 6/7] FEE-890 Order entries without recursion, refuse RID types, and rename the -1 reason --- .../src/lib/__tests__/crossAppUpsert.test.ts | 41 ++++++++++++- packages/database/src/lib/crossAppUpsert.ts | 58 ++++++++++++++----- 2 files changed, 85 insertions(+), 14 deletions(-) diff --git a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts index 72ed95bcb..2f3fe7c55 100644 --- a/packages/database/src/lib/__tests__/crossAppUpsert.test.ts +++ b/packages/database/src/lib/__tests__/crossAppUpsert.test.ts @@ -183,6 +183,32 @@ describe("parseUpsertData", () => { ], }), ), + { + when: "has a node whose type is a RID", + body: { + nodes: [toJson(node("claim-1", `${SPACE_URL}/no-such-type`))], + }, + issues: [ + { + path: ["nodes", 0, "nodeType"], + message: "This route does not take a RID type yet", + }, + ], + }, + { + when: "has a relation whose type is a RID", + body: { + relations: [ + toJson({ ...relation, relationType: "orn:roam.schema:graph-b/x" }), + ], + }, + issues: [ + { + path: ["relations", 0, "relationType"], + message: "This route does not take a RID type yet", + }, + ], + }, { when: "has metadata nested 100,000 levels deep", body: deeplyNested(100_000), @@ -364,6 +390,19 @@ describe("toOrderedEntries", () => { before("claim-1", "relation-1"); }); + it("orders a chain of 10,000 nodes, each citing the next as its Source", () => { + const length = 10_000; + const nodes = Array.from({ length }, (_, i) => + i + 1 < length + ? node(`n-${i}`, "source-type", { source: `n-${i + 1}` }) + : node(`n-${i}`, "source-type"), + ); + const ids = toOrderedEntries({ data: { nodes }, storedSchemas: [] }).map( + (e) => e.localId, + ); + expect(ids).toEqual(nodes.map((n) => n.localId).reverse()); + }); + it.each([ { from: "the body", data: graph, storedSchemas: [] }, { @@ -418,7 +457,7 @@ describe("mapUpsertResults", () => { const ids = entries.map((e, index) => failures[e.localId] ?? 100 + index); const { upserted, failed } = mapUpsertResults({ entries, ids }); expect(failed).toEqual([ - { localId: "evidence-1", kind: "nodes", reason: "name_conflict" }, + { localId: "evidence-1", kind: "nodes", reason: "unique_conflict" }, { localId: "relation-1", kind: "relations", reason: "error" }, ]); expect(upserted["source-1"]).toBe( diff --git a/packages/database/src/lib/crossAppUpsert.ts b/packages/database/src/lib/crossAppUpsert.ts index 3f87f9346..51a6f103a 100644 --- a/packages/database/src/lib/crossAppUpsert.ts +++ b/packages/database/src/lib/crossAppUpsert.ts @@ -41,7 +41,7 @@ type UpsertEntry = { }; type UpsertFailure = Omit & { - reason: "name_conflict" | "error"; + reason: "unique_conflict" | "error"; }; export type StoredSchema = Pick< @@ -123,6 +123,25 @@ const findContentScaleIssues = (data: CrossAppUpsertData): UpsertIssue[] => ), ); +// upsert_concepts stores a node or relation whose RID type names no concept with +// a NULL schema_id and no error, and the schema check looks up local ids only, so +// a RID type is refused until a caller needs one. +const findRidTypeIssues = (data: CrossAppUpsertData): UpsertIssue[] => + [ + ...(data.nodes ?? []).map((node, index) => ({ + path: ["nodes", index, "nodeType"], + type: node.nodeType, + })), + ...(data.relations ?? []).map((relation, index) => ({ + path: ["relations", index, "relationType"], + type: relation.relationType, + })), + ].flatMap(({ path, type }) => + isRid(type) + ? [{ path, message: "This route does not take a RID type yet" }] + : [], + ); + type ParseResult = | { success: true; data: CrossAppUpsertData } | { success: false; issues: UpsertIssue[] }; @@ -154,6 +173,7 @@ export const parseUpsertData = ({ const issues = [ ...findItemIssues({ data: result.data, spaceUrl }), ...findContentScaleIssues(result.data), + ...findRidTypeIssues(result.data), ]; if (issues.length > 0) return refuse(issues); return { success: true, data: result.data }; @@ -238,20 +258,32 @@ const relatedConcepts = (concept: LocalConceptDataInput): string[] => // upsert_concepts resolves a reference only to a row already written. Local ids // are caller input and may be "constructor", so they key a Map, not an object. +// The walk keeps its own stack: a chain of thousands of references would overflow +// the call stack. const orderByDependency = (entries: UpsertEntry[]): UpsertEntry[] => { - const remainder = new Map(entries.map((e) => [e.localId, e])); + const byId = new Map(entries.map((e) => [e.localId, e])); const processed = new Set(); const ordered: UpsertEntry[] = []; - const visit = (entry: UpsertEntry): void => { + const enter = (entry: UpsertEntry) => { processed.add(entry.localId); - for (const id of relatedConcepts(entry.concept)) { - const related = remainder.get(id); - if (related !== undefined && !processed.has(id)) visit(related); - } - ordered.push(entry); - remainder.delete(entry.localId); + return { entry, pending: relatedConcepts(entry.concept).reverse() }; }; - for (const entry of remainder.values()) visit(entry); + for (const root of entries) { + if (processed.has(root.localId)) continue; + const stack = [enter(root)]; + while (stack.length > 0) { + const top = stack[stack.length - 1]!; + const id = top.pending.pop(); + if (id === undefined) { + ordered.push(top.entry); + stack.pop(); + continue; + } + const related = byId.get(id); + if (related !== undefined && !processed.has(id)) + stack.push(enter(related)); + } + } return ordered; }; @@ -291,8 +323,8 @@ export const toOrderedEntries = ({ return orderByDependency(UPSERT_KINDS.flatMap((kind) => toEntries(kind))); }; -// upsert_concepts returns one id per input row, in input order: -1 for a name -// conflict and -2 for any other error. +// upsert_concepts returns one id per input row, in input order: -1 for any +// unique violation, such as a name or a Content clash, and -2 for any other error. export const mapUpsertResults = ({ entries, ids, @@ -309,7 +341,7 @@ export const mapUpsertResults = ({ failed.push({ localId, kind, - reason: id === -1 ? "name_conflict" : "error", + reason: id === -1 ? "unique_conflict" : "error", }); }); return { upserted: Object.fromEntries(upserted), failed }; From 85223e99e8c8a84df1922049bad8aaa5a7f0863d Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 16 Nov 2026 11:16:20 +0530 Subject: [PATCH 7/7] FEE-890 Walk the ordering stack without a non-null assertion --- packages/database/src/lib/crossAppUpsert.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/database/src/lib/crossAppUpsert.ts b/packages/database/src/lib/crossAppUpsert.ts index 51a6f103a..d5a7c43ec 100644 --- a/packages/database/src/lib/crossAppUpsert.ts +++ b/packages/database/src/lib/crossAppUpsert.ts @@ -271,8 +271,7 @@ const orderByDependency = (entries: UpsertEntry[]): UpsertEntry[] => { for (const root of entries) { if (processed.has(root.localId)) continue; const stack = [enter(root)]; - while (stack.length > 0) { - const top = stack[stack.length - 1]!; + for (let top = stack.at(-1); top !== undefined; top = stack.at(-1)) { const id = top.pending.pop(); if (id === undefined) { ordered.push(top.entry);