castrum 0.9.0 → 0.9.2

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 (120) hide show
  1. package/README.md +153 -630
  2. package/castrum.linux-x64-gnu.node +0 -0
  3. package/dist/index.d.ts +1653 -1817
  4. package/dist/index.js +6172 -4294
  5. package/docs/API.md +337 -0
  6. package/docs/ARCHITECTURE.md +111 -17
  7. package/docs/BENCHMARKS.md +117 -49
  8. package/docs/CASE_STUDY.md +243 -0
  9. package/docs/ENVIRONMENT.md +24 -7
  10. package/docs/FFI_BUN_GUIDE.md +335 -0
  11. package/docs/GETTING_STARTED.md +18 -34
  12. package/docs/HOW_TO_ADD_AN_OP.md +180 -0
  13. package/docs/INGRESS-ROUTER.md +82 -0
  14. package/docs/INGRESS.md +11 -7
  15. package/docs/NATIVE-ROUTE.md +77 -0
  16. package/docs/REPO_MAP.md +103 -53
  17. package/docs/adr/0001-two-wire-formats.md +1 -1
  18. package/docs/adr/0003-bun-builtins-delegation.md +6 -5
  19. package/docs/ai/LOCAL_DEV.md +81 -0
  20. package/docs/ai/TREE.md +1070 -0
  21. package/docs/bun-builtins-decision-matrix.md +25 -18
  22. package/index.ts +9 -16
  23. package/package.json +41 -28
  24. package/src/ingress/body.ts +167 -27
  25. package/src/ingress/decode/result-base.ts +10 -2
  26. package/src/ingress/handlers.ts +116 -285
  27. package/src/ingress/headers/security.ts +41 -0
  28. package/src/ingress/health.ts +1 -0
  29. package/src/ingress/index.ts +45 -169
  30. package/src/ingress/native-route.ts +182 -0
  31. package/src/ingress/packing/gather-raw-headers.ts +110 -22
  32. package/src/ingress/packing/header-packing.ts +5 -5
  33. package/src/ingress/packing/input-packer.ts +17 -3
  34. package/src/ingress/packing/route-wire.ts +197 -0
  35. package/src/ingress/packing/select-headers.ts +20 -7
  36. package/src/ingress/path-matcher.ts +115 -0
  37. package/src/ingress/response/baked-response.ts +302 -0
  38. package/src/ingress/router.ts +277 -0
  39. package/src/ingress/routes/common.ts +50 -1
  40. package/src/ingress/routes/fallback.ts +2 -2
  41. package/src/ingress/routes/head.ts +3 -13
  42. package/src/ingress/routes/index.ts +5 -4
  43. package/src/ingress/routes/json-write.ts +26 -47
  44. package/src/ingress/routes/native.ts +130 -0
  45. package/src/ingress/routes/options.ts +2 -12
  46. package/src/ingress/routes/read.ts +3 -13
  47. package/src/ingress/routes/responder.ts +159 -0
  48. package/src/ingress/server-node.ts +7 -7
  49. package/src/ingress/server.ts +83 -112
  50. package/src/ingress/sync.ts +151 -0
  51. package/src/ingress/types.ts +44 -1
  52. package/src/integration/pipeline.ts +3 -0
  53. package/src/integration/streaming.ts +2 -3
  54. package/src/integration/websocket.ts +4 -3
  55. package/src/loader/batch.ts +4 -0
  56. package/src/loader/cost.ts +1 -0
  57. package/src/loader/index.ts +43 -228
  58. package/src/loader/state.ts +84 -0
  59. package/src/loader/types.ts +145 -0
  60. package/src/native/ffi/build/codecs.ts +736 -0
  61. package/src/native/ffi/build/compress.ts +248 -0
  62. package/src/native/ffi/build/instances.ts +598 -0
  63. package/src/native/ffi/build/parse.ts +539 -0
  64. package/src/native/ffi/build/util.ts +140 -0
  65. package/src/native/ffi/build.ts +94 -0
  66. package/src/native/ffi/constants.ts +58 -0
  67. package/src/native/ffi/selftest.ts +22 -0
  68. package/src/native/ffi/types.ts +499 -0
  69. package/src/native/ffi.ts +307 -1357
  70. package/src/native/loader.ts +52 -6
  71. package/src/native/types/instances.ts +279 -0
  72. package/src/native/types.ts +41 -238
  73. package/src/runtime/builtins.ts +131 -0
  74. package/src/runtime/codec.ts +147 -0
  75. package/src/runtime/detect.ts +36 -0
  76. package/src/runtime/index.ts +28 -0
  77. package/src/runtime/native.ts +35 -0
  78. package/src/runtime/server.ts +31 -0
  79. package/src/runtime/transport.ts +64 -0
  80. package/src/runtime/types.ts +173 -0
  81. package/src/runtime/uuid.ts +45 -0
  82. package/src/runtime/websocket.ts +30 -0
  83. package/src/rust-ffi/addon.ts +1 -1
  84. package/src/rust-ffi/batch/build.ts +155 -0
  85. package/src/rust-ffi/batch/index.ts +8 -0
  86. package/src/rust-ffi/batch/types.ts +92 -0
  87. package/src/rust-ffi/client.ts +5 -6
  88. package/src/rust-ffi/context.ts +39 -10
  89. package/src/rust-ffi/index.ts +28 -17
  90. package/src/rust-ffi/into.ts +21 -0
  91. package/src/rust-ffi/options.ts +1 -1
  92. package/src/rust-ffi/packed.ts +40 -2
  93. package/src/rust-ffi/proven.ts +16 -30
  94. package/src/rust-ffi/scalar/crypto.ts +188 -107
  95. package/src/rust-ffi/scalar/factories.ts +257 -27
  96. package/src/rust-ffi/scalar/hashing.ts +43 -41
  97. package/src/rust-ffi/scalar/http.ts +208 -77
  98. package/src/rust-ffi/scalar/index.ts +3 -3
  99. package/src/rust-ffi/scalar/interface.ts +88 -213
  100. package/src/rust-ffi/scalar/json-packed.ts +175 -0
  101. package/src/rust-ffi/scalar/json.ts +28 -12
  102. package/src/rust-ffi/scalar/payload.ts +159 -42
  103. package/src/rust-ffi/text.ts +31 -5
  104. package/src/selection.ts +27 -31
  105. package/src/shared/adaptive.ts +1 -2
  106. package/src/shared/buffer-pool.ts +72 -17
  107. package/src/shared/bytes.ts +75 -6
  108. package/src/shared/codec.ts +78 -0
  109. package/src/shared/env.ts +4 -4
  110. package/src/shared/log.ts +1 -0
  111. package/src/shared/packed/index.ts +10 -0
  112. package/src/shared/packed/parsers.ts +31 -0
  113. package/src/shared/packed/schema.ts +23 -0
  114. package/src/shared/{packed.ts → packed/wire.ts} +31 -39
  115. package/src/shared/proven.ts +189 -496
  116. package/src/shared/runtime.ts +20 -25
  117. package/src/shared/uuid.ts +12 -28
  118. package/src/data/json-rows.ts +0 -33
  119. package/src/rust-ffi/batch.ts +0 -431
  120. package/src/shared/bench-classify.ts +0 -209
@@ -1,209 +0,0 @@
1
- /**
2
- * src/shared/bench-classify.ts — Pure benchmark classification for PROVEN_SURFACE.
3
- *
4
- * Given the CPU report's `comparisons` array and the static PROVEN_SURFACE
5
- * registry, compute per-function live classification: aggregate all of a
6
- * function's benchmark tasks (the canonical comparison plus its `_`-suffixed
7
- * variants that no other registry entry claims), score each task pass/fail,
8
- * and derive:
9
- *
10
- * - `live` status purely from the latest report (majority-fail rule)
11
- * - `effective` hybrid status the JSDoc annotator and the audit use:
12
- * a clear MAJORITY of task losses overrides the static
13
- * status and auto-deprecates the function, while a single
14
- * good run never auto-promotes a "not-competitive" function
15
- * (static classifications reflect the shipped release build).
16
- * - `promotable` static "not-competitive" but live majority WIN (ratio >= 1)
17
- * - `won`/`failed`/`total` — the score embedded in the generated JSDoc.
18
- *
19
- * Task bands: won = ratio >= 1 (rust at least as fast), failed = ratio <
20
- * 1/LOSS_TOLERANCE (clear loss), middle = tie (counts toward neither).
21
- *
22
- * PURE DATA — imports only the registry (no addon, no `rust` client), so it is
23
- * unit-testable and shared by scripts/annotate-performance.ts and
24
- * scripts/check-proven.ts (one source of truth for classification).
25
- */
26
-
27
- import { PROVEN_SURFACE, type PerformanceStatus, type ProvenEntry } from './proven'
28
-
29
- /** A "proven"/"parity" function whose baseline is more than this much faster is a clear loss. */
30
- export const LOSS_TOLERANCE = 1.35
31
- /** A "not-competitive" function that wins by more than this is a promotion candidate. */
32
- export const PROMOTE_TOLERANCE = 1.5
33
-
34
- /** One measured rust-vs-baseline comparison from the CPU report. */
35
- export interface Comparison {
36
- label: string
37
- nativeName: string
38
- rustName: string
39
- nativeAvgMs: number
40
- rustAvgMs: number
41
- ratio: number
42
- faster: 'rust' | 'native'
43
- }
44
-
45
- /** Pass/fail outcome for one of a function's benchmark tasks. */
46
- export interface TaskOutcome {
47
- rustName: string
48
- label: string
49
- ratio: number
50
- /** True when rust is at least as fast (ratio >= 1) — a genuine win. */
51
- won: boolean
52
- /** True when rust is a CLEAR loss (ratio < 1 / LOSS_TOLERANCE) — the deprecation signal. */
53
- failed: boolean
54
- /** False for the registry's canonical rustTask; true for aggregated variants. */
55
- isVariant: boolean
56
- }
57
-
58
- export interface ClassifiedEntry {
59
- entry: ProvenEntry
60
- tasks: TaskOutcome[]
61
- total: number
62
- /** Tasks where rust is at least as fast (ratio >= 1). */
63
- won: number
64
- /** Tasks where rust is a clear loss (ratio < 1 / LOSS_TOLERANCE). */
65
- failed: number
66
- /** True when a clear majority of the function's benchmarks lose (failed > total/2). */
67
- majorityFail: boolean
68
- /** True when a clear majority of the function's benchmarks win (won > total/2). */
69
- majorityWin: boolean
70
- /**
71
- * Ratio to display in JSDoc: the worst task when majority-failing (honest
72
- * "this loses" number), otherwise the canonical comparison, else the median.
73
- */
74
- ratio: number | undefined
75
- /** Status derived purely from the latest report. */
76
- live: PerformanceStatus
77
- /** Hybrid status the annotator/audit should act on (majority-fail overrides static). */
78
- effective: PerformanceStatus
79
- /** Live results contradict the static classification (majority fail on a proven/parity fn). */
80
- drifted: boolean
81
- /** Static "not-competitive" that now majority-wins (keep deprecated, add a note). */
82
- promotable: boolean
83
- }
84
-
85
- function median(values: number[]): number | undefined {
86
- if (values.length === 0) return undefined
87
- const sorted = [...values].sort((a, b) => a - b)
88
- const mid = Math.floor(sorted.length / 2)
89
- if (sorted.length % 2 === 1) return sorted[mid] ?? 0
90
- return ((sorted[mid - 1] ?? 0) + (sorted[mid] ?? 0)) / 2
91
- }
92
-
93
- /** Every rustTask claimed by any registry entry (prevents cross-entry variant theft). */
94
- function defaultClaimed(): Set<string> {
95
- return new Set(PROVEN_SURFACE.map((e) => e.rustTask).filter(Boolean))
96
- }
97
-
98
- /**
99
- * Classify a single registry entry against the report's comparisons.
100
- *
101
- * `claimedRustTasks` is the set of rustTasks owned by OTHER registry entries;
102
- * a variant comparison is aggregated only when no other entry claims it (e.g.
103
- * `rust:jwt_sign_bytes` belongs to `jwtSignBytes`, not `jwtSign`).
104
- */
105
- export function classifyEntry(
106
- entry: ProvenEntry,
107
- comparisons: Comparison[],
108
- claimedRustTasks: ReadonlySet<string> = defaultClaimed(),
109
- ): ClassifiedEntry {
110
- const unmeasured: ClassifiedEntry = {
111
- entry,
112
- tasks: [],
113
- total: 0,
114
- won: 0,
115
- failed: 0,
116
- majorityFail: false,
117
- majorityWin: false,
118
- ratio: undefined,
119
- live: 'unmeasured',
120
- effective: 'unmeasured',
121
- drifted: false,
122
- promotable: false,
123
- }
124
- if (!entry.rustTask) return unmeasured
125
-
126
- const tasks: TaskOutcome[] = []
127
- for (const c of comparisons) {
128
- const isCanonical = c.rustName === entry.rustTask
129
- const isVariant =
130
- !isCanonical &&
131
- !claimedRustTasks.has(c.rustName) &&
132
- c.rustName.startsWith(`${entry.rustTask}_`)
133
- if (isCanonical || isVariant) {
134
- // Three-band classification (honest, noise-resistant): a task is WON when
135
- // rust is at least as fast (ratio >= 1), FAILED when rust clearly loses
136
- // (ratio < 1 / LOSS_TOLERANCE), and a TIE in between. The middle band
137
- // counts toward neither majority, so marginal losses can never produce a
138
- // false win (e.g. a stale PROMOTABLE flag for a function that never
139
- // actually beat its baseline).
140
- tasks.push({
141
- rustName: c.rustName,
142
- label: c.label,
143
- ratio: c.ratio,
144
- won: c.ratio >= 1,
145
- failed: c.ratio < 1 / LOSS_TOLERANCE,
146
- isVariant: !isCanonical,
147
- })
148
- }
149
- }
150
-
151
- const total = tasks.length
152
- if (total === 0) return unmeasured
153
-
154
- const won = tasks.filter((t) => t.won).length
155
- const failed = tasks.filter((t) => t.failed).length
156
- const majorityFail = failed * 2 > total
157
- const majorityWin = won * 2 > total
158
-
159
- const canonical = tasks.find((t) => !t.isVariant)
160
- const ratio = majorityFail
161
- ? Math.min(...tasks.map((t) => t.ratio))
162
- : (canonical?.ratio ?? median(tasks.map((t) => t.ratio)))
163
-
164
- const live: PerformanceStatus = majorityFail
165
- ? 'not-competitive'
166
- : majorityWin
167
- ? 'proven'
168
- : 'parity'
169
-
170
- // Hybrid effective status: a clear majority of losses overrides the static
171
- // registry (auto-deprecate); otherwise the static classification stands —
172
- // classifications reflect the shipped release build, not one local run.
173
- const effective: PerformanceStatus =
174
- entry.status !== 'unmeasured' && majorityFail ? 'not-competitive' : entry.status
175
-
176
- return {
177
- entry,
178
- tasks,
179
- total,
180
- won,
181
- failed,
182
- majorityFail,
183
- majorityWin,
184
- ratio,
185
- live,
186
- effective,
187
- drifted: entry.status !== 'unmeasured' && effective !== entry.status,
188
- promotable: entry.status === 'not-competitive' && majorityWin,
189
- }
190
- }
191
-
192
- /** Classify every registry entry (single source of truth for the audit + annotator). */
193
- export function classifySurface(
194
- entries: readonly ProvenEntry[],
195
- comparisons: Comparison[],
196
- ): Map<string, ClassifiedEntry> {
197
- const claimed = new Set(entries.map((e) => e.rustTask).filter(Boolean))
198
- const out = new Map<string, ClassifiedEntry>()
199
- for (const entry of entries) {
200
- out.set(entry.name, classifyEntry(entry, comparisons, claimed))
201
- }
202
- return out
203
- }
204
-
205
- /** Human ratio summary: "3.18x rust" / "2.34x baseline". */
206
- export function formatRatio(ratio: number): string {
207
- if (ratio >= 1) return `${ratio.toFixed(2)}x rust`
208
- return `${(1 / ratio).toFixed(2)}x baseline`
209
- }