@mailwoman/core 7.2.0 → 7.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/api/APIClient.ts +175 -0
  2. package/api/headless.ts +23 -0
  3. package/api/index.ts +11 -0
  4. package/api/responses.ts +135 -0
  5. package/coarse-placer/coarse-placer.ts +300 -0
  6. package/coarse-placer/featurize.ts +171 -0
  7. package/coarse-placer/tools/build-dataset.ts +223 -0
  8. package/coarse-placer/tools/build-outlier-exposure.ts +172 -0
  9. package/coarse-placer/tools/build-outlier-latin.ts +200 -0
  10. package/coarse-placer/tools/build-outlier-oa.ts +231 -0
  11. package/coarse-placer/tools/eval-latin-offmap.ts +140 -0
  12. package/coarse-placer/tools/eval-openset.ts +519 -0
  13. package/coarse-placer/tools/eval-quant-compare.ts +151 -0
  14. package/coarse-placer/tools/eval.ts +187 -0
  15. package/coarse-placer/tools/fnv-hash.ts +24 -0
  16. package/coarse-placer/tools/index.ts +23 -0
  17. package/coarse-placer/tools/probe-frontier.ts +216 -0
  18. package/coarse-placer/tools/quantize.ts +116 -0
  19. package/coarse-placer/tools/train.ts +244 -0
  20. package/collections.ts +134 -0
  21. package/db/schema.ts +21 -0
  22. package/decoder/build-tree.ts +228 -0
  23. package/decoder/calibration.ts +101 -0
  24. package/decoder/containment.ts +87 -0
  25. package/decoder/index.ts +17 -0
  26. package/decoder/proposals-to-tree.ts +83 -0
  27. package/decoder/resolve-proposal-overlaps.ts +79 -0
  28. package/decoder/serialize-json.ts +70 -0
  29. package/decoder/serialize-tuples.ts +61 -0
  30. package/decoder/serialize-xml.ts +209 -0
  31. package/decoder/types.ts +159 -0
  32. package/decoder/unknown-spans.ts +103 -0
  33. package/decoder/validate-tree.ts +100 -0
  34. package/env/index.ts +42 -0
  35. package/env/load.ts +14 -0
  36. package/env/paths.ts +40 -0
  37. package/env/schema.ts +125 -0
  38. package/errors/index.ts +9 -0
  39. package/errors/schema.ts +155 -0
  40. package/fs/node.ts +38 -0
  41. package/identifiers.ts +58 -0
  42. package/index.ts +13 -0
  43. package/kysley/adapter.ts +29 -0
  44. package/kysley/client.ts +30 -0
  45. package/kysley/dialect-config.ts +30 -0
  46. package/kysley/dialect.ts +67 -0
  47. package/kysley/driver.ts +175 -0
  48. package/layers/index.ts +12 -0
  49. package/layers/manifest.ts +154 -0
  50. package/layers/schema.ts +105 -0
  51. package/lifecycle/ServiceSymbol.ts +95 -0
  52. package/lifecycle/index.ts +11 -0
  53. package/lifecycle/lru-cache.ts +44 -0
  54. package/lifecycle/services.ts +322 -0
  55. package/locale/index.ts +9 -0
  56. package/locale/locale.ts +49 -0
  57. package/locale/profiles/en-us.ts +69 -0
  58. package/locale/profiles/fr-fr.ts +75 -0
  59. package/locale/profiles/index.ts +9 -0
  60. package/locale/profiles/ja-jp.ts +33 -0
  61. package/locale/registry.ts +68 -0
  62. package/logging/index.ts +8 -0
  63. package/logging/shared.ts +145 -0
  64. package/logging/utils.ts +46 -0
  65. package/objects.ts +211 -0
  66. package/package.json +195 -104
  67. package/pipeline/index.ts +46 -0
  68. package/pipeline/reconcile-lookups.ts +108 -0
  69. package/pipeline/reconcile.ts +542 -0
  70. package/pipeline/runtime-pipeline.ts +732 -0
  71. package/pipeline/span-logit-aggregation.ts +151 -0
  72. package/pipeline/span-proposer.ts +587 -0
  73. package/pipeline/types.ts +348 -0
  74. package/policy/defaults.ts +35 -0
  75. package/policy/index.ts +10 -0
  76. package/policy/policy.ts +60 -0
  77. package/policy/proposal-collection.ts +64 -0
  78. package/policy/registry.ts +149 -0
  79. package/resolver/index.ts +29 -0
  80. package/resolver/types.ts +584 -0
  81. package/resources/LocaleIndex.ts +124 -0
  82. package/resources/ResourceMapCache.ts +126 -0
  83. package/resources/collections.ts +93 -0
  84. package/resources/debugging.ts +19 -0
  85. package/resources/git.ts +54 -0
  86. package/resources/index.ts +17 -0
  87. package/resources/languages/index.ts +49 -0
  88. package/resources/languages/types.gen.ts +1368 -0
  89. package/resources/libaddressinput.ts +105 -0
  90. package/resources/libpostal.ts +182 -0
  91. package/resources/locale.ts +23 -0
  92. package/resources/set.ts +179 -0
  93. package/resources/whosonfirst/DataSourceCache.ts +72 -0
  94. package/resources/whosonfirst/PlacetypeDataSource.ts +199 -0
  95. package/resources/whosonfirst/index.ts +10 -0
  96. package/resources/whosonfirst/loader.ts +157 -0
  97. package/resources/whosonfirst/placetypes/Placetype.ts +438 -0
  98. package/resources/whosonfirst/placetypes/admin.ts +173 -0
  99. package/resources/whosonfirst/placetypes/definition.ts +135 -0
  100. package/resources/whosonfirst/placetypes/graph.ts +0 -0
  101. package/resources/whosonfirst/placetypes/index.ts +12 -0
  102. package/resources/whosonfirst/placetypes/mermaid.ts +168 -0
  103. package/resources/whosonfirst/placetypes/tree.ts +46 -0
  104. package/routing/index.ts +208 -0
  105. package/scripting/index.ts +40 -0
  106. package/scripting/utils/index.ts +106 -0
  107. package/tokenization/Graph.ts +50 -0
  108. package/tokenization/Span.ts +297 -0
  109. package/tokenization/index.ts +9 -0
  110. package/tokenization/normalizer.ts +123 -0
  111. package/tools/download-libpostal-resources.ts +98 -0
  112. package/tools/download-ssl-address.ts +99 -0
  113. package/tools/generate-language-types.ts +194 -0
  114. package/tools/index.ts +13 -0
  115. package/types/Classification.ts +211 -0
  116. package/types/classifier.ts +133 -0
  117. package/types/component.ts +75 -0
  118. package/types/index.ts +10 -0
  119. package/types/mapping.ts +81 -0
  120. package/utils/data-root.ts +56 -0
  121. package/utils/hash.ts +27 -0
  122. package/utils/index.ts +14 -0
  123. package/utils/jsonl.ts +43 -0
  124. package/utils/python-json.ts +105 -0
  125. package/utils/python-random.ts +90 -0
  126. package/utils/repo.ts +118 -0
  127. package/utils/sealed-db.ts +85 -0
  128. package/utils/stats.ts +32 -0
  129. package/vitest.config.ts +53 -0
@@ -0,0 +1,85 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * The sealed-artifact invariant: every SQLite DB a build produces is a READ-ONLY asset. `sealDatabase`
7
+ * is the last step of every builder — checkpoint, freeze the journal, chmod 0444. `openBuiltDatabase`
8
+ * is how anything opens a data artifact; a write-mode open of a sealed file throws a NAMED error
9
+ * pointing at the rebuild command instead of a cryptic SQLITE_READONLY. Unsealing is deliberate and
10
+ * manual (`chmod u+w`), never programmatic — rebuild, don't mutate.
11
+ */
12
+ import { chmodSync, existsSync, statSync, unlinkSync } from "node:fs"
13
+ import { basename } from "node:path"
14
+ import type { DatabaseSync } from "node:sqlite"
15
+
16
+ /**
17
+ * `node:sqlite` via {@link process.getBuiltinModule} — invisible to bundlers. A static import here would ride the
18
+ * `@mailwoman/core/utils` barrel into every consumer, and the docs' plugin loader (which transpiles `docusaurus.config`
19
+ * imports) can't resolve `node:sqlite` (CI: "Cannot find module 'sqlite'"). The builtin accessor keeps the functions
20
+ * synchronous with zero resolve surface.
21
+ */
22
+ function sqlite(): typeof import("node:sqlite") {
23
+ return process.getBuiltinModule("node:sqlite")
24
+ }
25
+
26
+ /** A write-mode open was attempted on a sealed (0444) data artifact. */
27
+ export class SealedArtifactError extends Error {
28
+ constructor(path: string) {
29
+ super(
30
+ `${basename(path)} is a sealed read-only artifact — rebuild it via \`mailwoman gazetteer build …\`, ` +
31
+ `don't mutate it. (Deliberate unseal: chmod u+w — but prefer a rebuild.)`
32
+ )
33
+ this.name = "SealedArtifactError"
34
+ }
35
+ }
36
+
37
+ /** True when the artifact exists and carries no write bits (the sealed state {@link sealDatabase} leaves). */
38
+ export function isSealed(path: string): boolean {
39
+ return existsSync(path) && (statSync(path).mode & 0o222) === 0
40
+ }
41
+
42
+ /**
43
+ * Finalize a built DB: WAL-checkpoint → `journal_mode = DELETE` → remove `-wal`/`-shm` sidecars → `chmod 0o444`.
44
+ * Idempotent. Throws if the checkpoint cannot complete (another writer holds the DB).
45
+ */
46
+ export function sealDatabase(path: string): void {
47
+ // A previously sealed artifact needs the write bit back for the journal-mode switch.
48
+ if (isSealed(path)) {
49
+ chmodSync(path, 0o644)
50
+ }
51
+ const db = new (sqlite().DatabaseSync)(path)
52
+ const checkpoint = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get() as { busy: number }
53
+
54
+ if (checkpoint.busy !== 0) {
55
+ db.close()
56
+ throw new Error(`sealDatabase: WAL checkpoint busy on ${path} — close all writers first`)
57
+ }
58
+ const mode = db.prepare("PRAGMA journal_mode = DELETE").get() as { journal_mode: string }
59
+ db.close()
60
+
61
+ if (mode.journal_mode !== "delete") {
62
+ throw new Error(`sealDatabase: journal_mode switch failed on ${path} (still ${mode.journal_mode})`)
63
+ }
64
+
65
+ for (const sidecar of [`${path}-wal`, `${path}-shm`]) {
66
+ if (existsSync(sidecar)) {
67
+ unlinkSync(sidecar)
68
+ }
69
+ }
70
+ chmodSync(path, 0o444)
71
+ }
72
+
73
+ /**
74
+ * Open a data artifact. Read-only by default. `write: true` is for builders working on UNsealed staging — against a
75
+ * sealed artifact it throws {@link SealedArtifactError}.
76
+ */
77
+ export function openBuiltDatabase(path: string, opts: { write?: boolean } = {}): DatabaseSync {
78
+ if (opts.write) {
79
+ if (isSealed(path)) throw new SealedArtifactError(path)
80
+
81
+ return new (sqlite().DatabaseSync)(path)
82
+ }
83
+
84
+ return new (sqlite().DatabaseSync)(path, { readOnly: true })
85
+ }
package/utils/stats.ts ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Small stats helpers — the canonical home for the `percentile`/`median` copies (~15) and the
7
+ * `pct` percentage-format lambdas (~40) the 2026-07-09 dedupe survey found across eval scripts.
8
+ *
9
+ * `percentile` is byte-for-byte the gate scripts' nearest-rank implementation
10
+ * (oa-resolver-eval.ts / resolver-eval.ts) — gate parity depends on this exact semantics; do not
11
+ * "upgrade" it to linear interpolation.
12
+ */
13
+
14
+ /** Nearest-rank percentile over an unsorted sample; `null` on an empty sample. `p` in [0, 100]. */
15
+ export function percentile(xs: readonly number[], p: number): number | null {
16
+ if (xs.length === 0) return null
17
+ const s = [...xs].sort((a, b) => a - b)
18
+
19
+ return s[Math.min(s.length - 1, Math.floor((p / 100) * s.length))]!
20
+ }
21
+
22
+ /** `percentile(xs, 50)`. */
23
+ export function median(xs: readonly number[]): number | null {
24
+ return percentile(xs, 50)
25
+ }
26
+
27
+ /** Format `numerator / denominator` as a fixed-digit percentage (`"12.5%"`); `"—"` when `denominator` is 0. */
28
+ export function formatPercent(numerator: number, denominator: number, digits = 1): string {
29
+ if (denominator === 0) return "—"
30
+
31
+ return ((100 * numerator) / denominator).toFixed(digits) + "%"
32
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Per-package vitest config that resolves @mailwoman/core subpath imports to source. The
7
+ * package.json `exports` field points at `./out/.../index.js` — those files don't exist until tsc
8
+ * has run. With these aliases vitest can run from a clean checkout.
9
+ *
10
+ * A few subpaths are single files, not directories with an `index.ts` — `fs` is the node build
11
+ * (`fs/node.ts`), `objects` is a bare file (`objects.ts`), `coarse-placer`/`kysley/*` are single
12
+ * files. Those get explicit entries BEFORE the generic `<subpath> → <subpath>/index.ts` rule,
13
+ * which would otherwise resolve them to a non-existent `index.ts` (mirrors the root config).
14
+ */
15
+
16
+ import { resolve } from "node:path"
17
+
18
+ // `defineConfig` from "vitest/config" (not "vite"): vitest's overload carries the `test` field.
19
+ // vite 8 (pulled in by docs/ Storybook) no longer applies the `vitest/config` type augmentation to
20
+ // vite's own `defineConfig`, so importing from "vite" makes `test` a type error under vite 8.
21
+ import { defineConfig } from "vitest/config"
22
+
23
+ const here = import.meta.dirname
24
+
25
+ export default defineConfig({
26
+ resolve: {
27
+ alias: [
28
+ // Order matters — more specific entries first. Single-file subpaths (no directory index)
29
+ // must beat the generic `<subpath>/index.ts` rule below.
30
+ { find: /^@mailwoman\/core\/kysley\/(.+)$/, replacement: resolve(here, "kysley/$1.ts") },
31
+ { find: /^@mailwoman\/core\/coarse-placer$/, replacement: resolve(here, "coarse-placer/coarse-placer.ts") },
32
+ { find: /^@mailwoman\/core\/objects$/, replacement: resolve(here, "objects.ts") },
33
+ { find: /^@mailwoman\/core\/fs$/, replacement: resolve(here, "fs/node.ts") },
34
+ // Every other @mailwoman/core/<subpath> resolves to the index.ts of the matching subdir.
35
+ { find: /^@mailwoman\/core\/(.+)$/, replacement: resolve(here, "$1/index.ts") },
36
+ { find: /^@mailwoman\/core$/, replacement: resolve(here, "index.ts") },
37
+ // Sibling workspaces.
38
+ { find: /^@mailwoman\/corpus\/(.+)$/, replacement: resolve(here, "../corpus/src/$1.ts") },
39
+ { find: /^@mailwoman\/corpus$/, replacement: resolve(here, "../corpus/src/index.ts") },
40
+ // The root `mailwoman` package — test-kit imports it (transitively re-exports core +
41
+ // classifiers). Tests across workspaces also import `mailwoman/test-kit` directly.
42
+ { find: "mailwoman/test-kit", replacement: resolve(here, "../mailwoman/test-kit/index.ts") },
43
+ { find: /^mailwoman$/, replacement: resolve(here, "../mailwoman/index.ts") },
44
+ ],
45
+ },
46
+ test: {
47
+ // isolate: true (default) — required because @mailwoman/core/resources/libpostal has a
48
+ // top-level await that Vite's loader treats as a cycle under shared module graphs,
49
+ // breaking downstream `class extends ...` evaluations.
50
+ isolate: true,
51
+ exclude: ["**/node_modules/**", "**/out/**", "**/dist/**"],
52
+ },
53
+ })