Metakit / @doeixd/metakit/artifact

Hash

namespace · packages/metakit/src/hash.ts:6

export * as Hash from './hash.js'

The one place MetaKit hashes a definition: the canonical JSON rules, the domain tags and SHA-256. The rules are written out in docs/semantics.md ("Content hashes") so a consumer outside this repository can recompute a hash.

In the guides

Members

Hash.canonical

function · packages/metakit/src/hash.ts:138

function canonical(value: Hashable): Result.Result<string, Unhashable>

The canonical JSON text of value: no whitespace, object keys sorted by UTF-16 code units, a key holding undefined left out, numbers and strings as JSON.stringify writes them (a lone surrogate as a \udxxx escape).

Hash.Digest

type · packages/metakit/src/hash.ts:57

type Digest<Kind extends string = string> = string & {
  readonly [DigestKind]: Kind
}

sha256:<64 lowercase hex>, of a payload of the kind it names.

Hash.digest

function · packages/metakit/src/hash.ts:156

function digest<Kind extends string>(
  domain: HashDomain<Kind>,
  payload: Hashable,
): Effect.Effect<Digest<Kind>, Unhashable>

Hash payload as a value of domain: SHA-256 of the canonical JSON of [domain.tag, payload], written sha256:<hex>.

Hash.domains

const · packages/metakit/src/hash.ts:26

const domains = {
  expr,
  transition,
  transitionContract,
  operation,
  operationContract,
  fn,
  fnContract,
  machine,
  machineContract,
  evidence,
  schema,
}

Every kind MetaKit hashes. A new kind is added here, never spelled at a call site.

Hash.Hashable

type · packages/metakit/src/hash.ts:74

type Hashable =
  | null
  | boolean
  | number
  | string
  | ReadonlyArray<Hashable>
  | {
      readonly [key: string]: Hashable | undefined
    }

What canonical writes: JSON, where an object's key holding undefined is absent.

Hash.HashDomain

interface · packages/metakit/src/hash.ts:13

interface HashDomain<Kind extends string = string> {
  readonly kind: Kind
  readonly version: number
  readonly tag: `metakit/${Kind}@${number}`
}

What a hash is of, and the version of its canonical form. Its tag (metakit/expr@1) is hashed with the payload, so two kinds never share a hash and a change of rules changes every hash of that kind.

Hash.Unhashable

class · packages/metakit/src/hash.ts:68

class Unhashable extends Schema.TaggedError<Unhashable>()('Unhashable', {
  reason: Schema.Literals(['opaque', 'leaf', 'unidentified', 'cycle']),
  detail: Schema.String,
}) {}

Why a value has no hash: it holds an Opaque node ('opaque'), a leaf canonical JSON cannot write ('leaf': a non-finite number, -0, a bigint, a function, a symbol, undefined in a list, a class instance), or it reads a Schema holding a check or declaration with no representation identity ('unidentified'; detail names the field), or it calls a derived operation whose definition reaches itself ('cycle'; detail names the chain). Any other failure on the way to a hash is an error of its own, never this.