{"version":3,"sources":["../src/schema/deserializer.ts","../src/schema/graph-templates.ts"],"names":["hasOwnKey","buildValidatedKindRegistry","ConfigurationError","resolveGraphVectorSlots","serializeSchema","computeSchemaHash"],"mappings":";;;;;;;;AAqFO,SAAS,kBACd,MAAA,EACoB;AACpB,EAAA,MAAM,SAAA,GAAY,MAAA,CAAO,IAAA,CAAK,MAAA,CAAO,KAAK,CAAA;AAC1C,EAAA,MAAM,SAAA,GAAY,MAAA,CAAO,IAAA,CAAK,MAAA,CAAO,KAAK,CAAA;AAC1C,EAAA,MAAM,aAAA,GAAgB,MAAA,CAAO,IAAA,CAAK,MAAA,CAAO,SAAS,SAAS,CAAA;AAE3D,EAAA,OAAO;AAAA,IACL,SAAS,MAAA,CAAO,OAAA;AAAA,IAChB,SAAS,MAAA,CAAO,OAAA;AAAA,IAChB,aAAa,MAAA,CAAO,WAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMpB,OAAA,EAAS,CAAC,IAAA,KACRA,2BAAA,CAAU,MAAA,CAAO,KAAA,EAAO,IAAI,CAAA,GAAI,MAAA,CAAO,KAAA,CAAM,IAAI,CAAA,GAAI,MAAA;AAAA,IACvD,cAAc,MAAM,SAAA;AAAA,IAEpB,OAAA,EAAS,CAAC,IAAA,KACRA,2BAAA,CAAU,MAAA,CAAO,KAAA,EAAO,IAAI,CAAA,GAAI,MAAA,CAAO,KAAA,CAAM,IAAI,CAAA,GAAI,MAAA;AAAA,IACvD,cAAc,MAAM,SAAA;AAAA,IAEpB,WAAA,EAAa,CAAC,IAAA,KACZA,2BAAA,CAAU,MAAA,CAAO,QAAA,CAAS,SAAA,EAAW,IAAI,CAAA,GACvC,MAAA,CAAO,QAAA,CAAS,SAAA,CAAU,IAAI,CAAA,GAC9B,MAAA;AAAA,IACJ,kBAAkB,MAAM,aAAA;AAAA,IAExB,YAAA,EAAc,MAAM,MAAA,CAAO,QAAA,CAAS,SAAA;AAAA,IACpC,WAAA,EAAa,MAAM,MAAA,CAAO,QAAA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,EAAiC,OAAA,EAAS,OAAO,OAAA;AAAQ,KACnE;AAAA,EACF;AACA,EAAA,OAAO,OAAO,MAAA,CAAO;AAAA,IACnB,MAAA,EAAQ,OAAA;AAAA,IACR,UAAA,EAAY,OAAO,MAAA,CAAO;AAAA,MACxB,KAAA,EAAO,WAAA;AAAA,MACP,OAAA,EAAS,OAAO,GAAA,CAAI,OAAA;AAAA,MACpB,IAAA,EAAM,OAAO,GAAA,CAAI;AAAA,KAClB,CAAA;AAAA,IACD,QAAQ,MAAA,CAAO;AAAA,GAChB,CAAA;AACH;AAEA,SAAS,iCACP,UAAA,EACoB;AACpB,EAAA,OAAO,IAAIA,oCAAA;AAAA,IACT,0BAA0B,UAAU,CAAA,gDAAA,CAAA;AAAA,IACpC,EAAE,IAAA,EAAM,gCAAA,EAAkC,UAAA;AAAW,GACvD;AACF;AAGO,SAAS,gBAAA,CACd,SACA,MAAA,EAC4C;AAC5C,EAAA,OAAO,wBAAA,CAAyB,SAAS,MAAM,CAAA;AACjD;AAEA,SAAS,+BACP,OAAA,EACoB;AACpB,EAAA,OAAO,IAAIA,oCAAA;AAAA,IACT,CAAA,IAAA,EAAO,QAAQ,OAAO,CAAA,kDAAA,CAAA;AAAA,IACtB,EAAE,IAAA,EAAM,6BAAA,EAA+B,OAAA,EAAS,QAAQ,OAAA;AAAQ,GAClE;AACF","file":"chunk-GXHEGNI6.cjs","sourcesContent":["/**\n * Schema deserializer for loading stored schemas.\n *\n * Reconstructs runtime objects from SerializedSchema.\n * Note: Zod schemas cannot be fully reconstructed from JSON Schema,\n * so this provides access to the serialized data for introspection.\n */\nimport { type AnyEdgeType, type NodeType } from \"../core/types\";\nimport { type NamedOntologyRelation } from \"../ontology/validation\";\nimport { buildValidatedKindRegistry } from \"../registry/build-validated\";\nimport type { KindRegistry } from \"../registry/kind-registry\";\nimport { type EdgeEndpointKinds } from \"../registry/validate-implies\";\nimport { hasOwnKey } from \"../utils/object\";\nimport {\n  type SerializedClosures,\n  type SerializedEdgeDef,\n  type SerializedMetaEdge,\n  type SerializedNodeDef,\n  type SerializedOntologyRelation,\n  type SerializedSchema,\n} from \"./types\";\n\n// ============================================================\n// Deserialized Schema\n// ============================================================\n\n/**\n * A deserialized schema provides read-only access to schema metadata.\n *\n * Note: Unlike the original GraphDef, this does not include Zod schemas\n * since those cannot be reconstructed from JSON Schema. Use this for\n * introspection and metadata access only.\n */\nexport type DeserializedSchema = Readonly<{\n  graphId: string;\n  version: number;\n  generatedAt: string;\n\n  /** Get node definition by name */\n  getNode: (name: string) => SerializedNodeDef | undefined;\n\n  /** Get all node names */\n  getNodeNames: () => readonly string[];\n\n  /** Get edge definition by name */\n  getEdge: (name: string) => SerializedEdgeDef | undefined;\n\n  /** Get all edge names */\n  getEdgeNames: () => readonly string[];\n\n  /** Get meta-edge definition by name */\n  getMetaEdge: (name: string) => SerializedMetaEdge | undefined;\n\n  /** Get all meta-edge names */\n  getMetaEdgeNames: () => readonly string[];\n\n  /** Get all ontology relations */\n  getRelations: () => readonly SerializedOntologyRelation[];\n\n  /** Get precomputed closures */\n  getClosures: () => SerializedClosures;\n\n  /** Get graph defaults */\n  getDefaults: () => SerializedSchema[\"defaults\"];\n\n  /** Get the durable TypeGraph Identity Profile configuration. */\n  getIdentity: () => SerializedSchema[\"identity\"];\n\n  /** Get the raw serialized schema */\n  getRaw: () => SerializedSchema;\n\n  /** Build a validated KindRegistry by recomputing closures from relations */\n  buildRegistry: () => KindRegistry;\n}>;\n\n// ============================================================\n// Deserialization\n// ============================================================\n\n/**\n * Deserializes a SerializedSchema into a DeserializedSchema.\n *\n * @param schema - The serialized schema to deserialize\n * @returns A deserialized schema with accessor methods\n */\nexport function deserializeSchema(\n  schema: SerializedSchema,\n): DeserializedSchema {\n  const nodeNames = Object.keys(schema.nodes);\n  const edgeNames = Object.keys(schema.edges);\n  const metaEdgeNames = Object.keys(schema.ontology.metaEdges);\n\n  return {\n    graphId: schema.graphId,\n    version: schema.version,\n    generatedAt: schema.generatedAt,\n\n    // Own-key reads, for the same reason {@link buildEdgeEndpointKinds} below\n    // uses a `Map`: `schema` is parsed out of a stored JSON document and the\n    // caller supplies the name, so a raw read hands back an `Object.prototype`\n    // member typed as a definition for any name no kind is registered under.\n    getNode: (name) =>\n      hasOwnKey(schema.nodes, name) ? schema.nodes[name] : undefined,\n    getNodeNames: () => nodeNames,\n\n    getEdge: (name) =>\n      hasOwnKey(schema.edges, name) ? schema.edges[name] : undefined,\n    getEdgeNames: () => edgeNames,\n\n    getMetaEdge: (name) =>\n      hasOwnKey(schema.ontology.metaEdges, name) ?\n        schema.ontology.metaEdges[name]\n      : undefined,\n    getMetaEdgeNames: () => metaEdgeNames,\n\n    getRelations: () => schema.ontology.relations,\n    getClosures: () => schema.ontology.closures,\n\n    getDefaults: () => schema.defaults,\n    getIdentity: () => schema.identity,\n    getRaw: () => schema,\n\n    buildRegistry: () => buildRegistryFromRelations(schema),\n  };\n}\n\n// ============================================================\n// Registry Building\n// ============================================================\n\n/**\n * Builds a KindRegistry from serialized relations.\n *\n * Persisted closures are a legacy inspection artifact. Relations are validated\n * and closures are recomputed so old schemas gain current hardening rules.\n */\nfunction buildRegistryFromRelations(schema: SerializedSchema): KindRegistry {\n  // Build empty node/edge kind maps (we don't have the actual Zod schemas)\n  const nodeKinds = new Map<string, NodeType>();\n  const edgeKinds = new Map<string, AnyEdgeType>();\n  return buildValidatedKindRegistry({\n    nodeKinds,\n    edgeKinds,\n    ontology: schema.ontology.relations.map(\n      (relation): NamedOntologyRelation => ({\n        metaEdge: relation.metaEdge,\n        from: relation.from,\n        to: relation.to,\n      }),\n    ),\n    edgeEndpoints: buildEdgeEndpointKinds(schema.edges),\n    ...(schema.identity === undefined ? {} : { identity: schema.identity }),\n  });\n}\n\n/**\n * Maps each edge kind's serialized definition to its domain/range kind\n * names, for `validateImpliesEndpointCompatibility`. A `Map` (rather than\n * the plain `schema.edges` object) so a lookup for an edge kind literally\n * named \"toString\" or another `Object.prototype` member can't resolve to\n * an inherited member instead of `undefined`.\n */\nfunction buildEdgeEndpointKinds(\n  edges: Record<string, SerializedEdgeDef>,\n): ReadonlyMap<string, EdgeEndpointKinds> {\n  const result = new Map<string, EdgeEndpointKinds>();\n  for (const [kind, def] of Object.entries(edges)) {\n    const pairs: { from: string; to: string }[] = [];\n    if (def.targetKindsBySource === undefined) {\n      for (const fromKind of def.fromKinds) {\n        for (const toKind of def.toKinds) {\n          pairs.push({ from: fromKind, to: toKind });\n        }\n      }\n    } else {\n      for (const [sourceKind, targetKinds] of Object.entries(\n        def.targetKindsBySource,\n      )) {\n        for (const targetKind of targetKinds) {\n          pairs.push({ from: sourceKind, to: targetKind });\n        }\n      }\n    }\n    result.set(kind, {\n      from: def.fromKinds,\n      to: def.toKinds,\n      pairs,\n    });\n  }\n  return result;\n}\n","import type { GraphBackend, SchemaVersionRow } from \"../backend/types\";\nimport { type GraphDef } from \"../core/define-graph\";\nimport { resolveGraphVectorSlots } from \"../core/embedding\";\nimport { ConfigurationError } from \"../errors\";\nimport { type ReconciledSchema } from \"../store/store\";\nimport { computeSchemaHash, serializeSchema } from \"./serializer\";\n\n/** A locally held handle for a durable server-side graph template. */\nexport type GraphTemplate<G extends GraphDef> = Readonly<{\n  id: string;\n  reconciled: ReconciledSchema<G>;\n  schemaHash: string;\n}>;\n\nexport type InstantiateGraphTemplateResult<G extends GraphDef> = Readonly<{\n  status: \"ready\";\n  reconciled: ReconciledSchema<G>;\n  schema: SchemaVersionRow;\n}>;\n\n/**\n * Registers the fully materialized shape of a verified store as a durable\n * template. The original schema version is retained only as snapshot\n * provenance: every instantiated graph starts at v1 with that final shape.\n */\nexport async function registerGraphTemplate<G extends GraphDef>(\n  backend: GraphBackend,\n  params: Readonly<{ templateId: string; reconciled: ReconciledSchema<G> }>,\n): Promise<GraphTemplate<G>> {\n  const register = backend.registerGraphTemplate;\n  if (register === undefined) throw graphTemplatesUnsupportedError(backend);\n  await backend.assertBaseSchemaCurrent?.();\n  const { reconciled } = params;\n  if (reconciled.version === undefined || reconciled.hash === undefined) {\n    throw new ConfigurationError(\n      \"A graph template requires a reconciled schema from a managed or verified Store.\",\n      { code: \"GRAPH_TEMPLATE_REQUIRES_RECONCILED_SCHEMA\" },\n    );\n  }\n  if (\n    resolveGraphVectorSlots(reconciled.graph).length > 0 &&\n    backend.capabilities.vector?.supported === true\n  ) {\n    throw new ConfigurationError(\n      \"Graph templates do not support embedding fields because vector storage is graph-scoped and cannot be instantiated schema-only.\",\n      {\n        code: \"GRAPH_TEMPLATE_VECTOR_UNSUPPORTED\",\n        graphId: reconciled.graph.id,\n      },\n      {\n        suggestion:\n          \"Provision the graph through createStoreWithSchema until template instantiation can atomically materialize vector contributions.\",\n      },\n    );\n  }\n  const schemaDocument = serializeSchema(reconciled.graph, reconciled.version);\n  const schemaHash = await computeSchemaHash(schemaDocument);\n  if (schemaHash !== reconciled.hash) {\n    throw new ConfigurationError(\n      \"Reconciled schema hash does not match its graph snapshot.\",\n      { code: \"GRAPH_TEMPLATE_RECONCILED_HASH_MISMATCH\" },\n    );\n  }\n  await register({\n    templateId: params.templateId,\n    schemaHash,\n    schemaDoc: schemaDocument,\n  });\n  return Object.freeze({\n    id: params.templateId,\n    reconciled,\n    schemaHash,\n  });\n}\n\n/**\n * Instantiates the template's final graph shape as target schema v1. Only the\n * target hash crosses the wire; the durable source document stays in the\n * backend template registry and is rebound to `graphId` by one SQL statement.\n */\nexport async function instantiateGraphTemplate<G extends GraphDef>(\n  backend: GraphBackend,\n  params: Readonly<{ template: GraphTemplate<G>; graphId: string }>,\n): Promise<InstantiateGraphTemplateResult<G>> {\n  const instantiate = backend.instantiateGraphTemplate;\n  if (instantiate === undefined) throw graphTemplatesUnsupportedError(backend);\n  await backend.assertBaseSchemaCurrent?.();\n  const sourceVersion = params.template.reconciled.version;\n  const reconciledHash = params.template.reconciled.hash;\n  if (sourceVersion === undefined || reconciledHash === undefined) {\n    throw graphTemplateHandleMismatchError(params.template.id);\n  }\n  const sourceHash = await computeSchemaHash(\n    serializeSchema(params.template.reconciled.graph, sourceVersion),\n  );\n  if (\n    sourceHash !== params.template.schemaHash ||\n    reconciledHash !== params.template.schemaHash\n  ) {\n    throw graphTemplateHandleMismatchError(params.template.id);\n  }\n  const targetGraph: G = Object.freeze({\n    ...params.template.reconciled.graph,\n    id: params.graphId,\n  });\n  const targetHash = await computeSchemaHash(serializeSchema(targetGraph, 1));\n  const result = await instantiate({\n    templateId: params.template.id,\n    templateSchemaHash: params.template.schemaHash,\n    graphId: params.graphId,\n    schemaHash: targetHash,\n  });\n  if (result.status === \"refused\") {\n    throw new ConfigurationError(\n      `Graph template instantiation was refused for graph \"${params.graphId}\". The template may be absent, or the graph already has a different schema.`,\n      {\n        code: \"GRAPH_TEMPLATE_INSTANTIATION_REFUSED\",\n        graphId: params.graphId,\n        templateId: params.template.id,\n      },\n    );\n  }\n  if (\n    result.row.graph_id !== params.graphId ||\n    result.row.version !== 1 ||\n    result.row.schema_hash !== targetHash\n  ) {\n    throw new ConfigurationError(\n      \"Graph template backend returned a schema row that does not match the requested target.\",\n      { code: \"GRAPH_TEMPLATE_INVALID_RESULT\", graphId: params.graphId },\n    );\n  }\n  return Object.freeze({\n    status: \"ready\",\n    reconciled: Object.freeze({\n      graph: targetGraph,\n      version: result.row.version,\n      hash: result.row.schema_hash,\n    }),\n    schema: result.row,\n  });\n}\n\nfunction graphTemplateHandleMismatchError(\n  templateId: string,\n): ConfigurationError {\n  return new ConfigurationError(\n    `Graph template handle \"${templateId}\" does not match its reconciled schema snapshot.`,\n    { code: \"GRAPH_TEMPLATE_HANDLE_MISMATCH\", templateId },\n  );\n}\n\n/** Short public name for {@link instantiateGraphTemplate}. */\nexport function instantiateGraph<G extends GraphDef>(\n  backend: GraphBackend,\n  params: Readonly<{ template: GraphTemplate<G>; graphId: string }>,\n): Promise<InstantiateGraphTemplateResult<G>> {\n  return instantiateGraphTemplate(backend, params);\n}\n\nfunction graphTemplatesUnsupportedError(\n  backend: GraphBackend,\n): ConfigurationError {\n  return new ConfigurationError(\n    `The ${backend.dialect} backend does not support durable graph templates.`,\n    { code: \"GRAPH_TEMPLATES_UNSUPPORTED\", dialect: backend.dialect },\n  );\n}\n"]}