castrum 0.9.8 → 0.9.9

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 (55) hide show
  1. package/Cargo.lock +1 -1
  2. package/Cargo.toml +1 -1
  3. package/castrum.darwin-arm64.node +0 -0
  4. package/castrum.darwin-x64.node +0 -0
  5. package/castrum.linux-arm64-gnu.node +0 -0
  6. package/castrum.linux-arm64-musl.node +0 -0
  7. package/castrum.linux-x64-gnu.node +0 -0
  8. package/castrum.linux-x64-musl.node +0 -0
  9. package/castrum.linux-x64-v3-gnu.node +0 -0
  10. package/castrum.win32-x64-msvc.node +0 -0
  11. package/dist/index.d.ts +164 -4
  12. package/dist/index.js +434 -70
  13. package/docs/ENVIRONMENT.md +7 -0
  14. package/docs/RND-CONCURRENCY.md +20 -4
  15. package/docs/superpowers/plans/2026-09-18-elysia-learnings-adoption.md +634 -0
  16. package/index.ts +5 -0
  17. package/package.json +2 -1
  18. package/rust/ffi/task.rs +19 -18
  19. package/rust/ingress/api.rs +21 -5
  20. package/rust/ingress/mod.rs +7 -0
  21. package/rust/ingress/native_route.rs +4 -2
  22. package/rust/ingress/schema_cache.rs +266 -0
  23. package/rust/task/mod.rs +1 -0
  24. package/rust/task/runtime.rs +55 -7
  25. package/rust/task/tests.rs +98 -1
  26. package/src/ingress/abort.ts +48 -0
  27. package/src/ingress/body.ts +104 -8
  28. package/src/ingress/handlers.ts +1 -0
  29. package/src/ingress/health.ts +22 -0
  30. package/src/ingress/index.ts +3 -1
  31. package/src/ingress/router.ts +36 -1
  32. package/src/ingress/routes/common.ts +9 -1
  33. package/src/ingress/routes/echo.ts +10 -0
  34. package/src/ingress/routes/json-write.ts +11 -0
  35. package/src/ingress/routes/native.ts +24 -5
  36. package/src/ingress/routes/options.ts +6 -2
  37. package/src/ingress/routes/responder.ts +25 -5
  38. package/src/ingress/routes/safe-record.ts +57 -0
  39. package/src/ingress/server-error.ts +111 -0
  40. package/src/ingress/server-node.ts +102 -23
  41. package/src/ingress/server.ts +140 -32
  42. package/src/integration/pipeline.ts +6 -0
  43. package/src/native/ffi/build/task.ts +2 -1
  44. package/src/native/ffi/types.ts +6 -1
  45. package/src/native/types.ts +6 -0
  46. package/src/runtime/detect.ts +14 -0
  47. package/src/rust-ffi/context.ts +46 -3
  48. package/src/rust-ffi/scalar/factories.ts +6 -0
  49. package/src/rust-ffi/scalar/interface.ts +14 -0
  50. package/src/rust-ffi/text.ts +5 -4
  51. package/src/shared/buffer-pool.ts +10 -0
  52. package/src/shared/memory.ts +68 -0
  53. package/src/shared/metrics.ts +86 -3
  54. package/src/shared/runtime.ts +7 -0
  55. package/src/task/runtime.ts +52 -9
@@ -38,8 +38,15 @@ for false; anything else is warned about and falls back to the default.
38
38
  | Variable | Type | Default | Description |
39
39
  |----------|------|---------|-------------|
40
40
  | `CASTRUM_TASK_THREADS` | int | `max(1, cores-1)` | Worker threads for the off-thread task runtime (castrum Tasks). Read when the pool first starts — the pool is process-wide and cannot be resized afterwards (`tasks.stats().threads` reports the live count). |
41
+ | `CASTRUM_TASK_QUEUE_MAX` | int | `4096` | Admission bound on the off-thread task work queue. Read when the pool first starts (minimum 1). A submit that would exceed it is rejected rather than growing memory: the native submit returns `2` and the task promise rejects with a typed `OVERLOADED` error. |
41
42
  | `CASTRUM_TASK_PIN_CORES` | (presence) | — | When set (Linux), pin each task worker to its own core (core 0 is skipped so the JS event loop keeps it). `CASTRUM_PIN_CORES` enables it too. Off by default: pinning competes with the JS/rayon threads for the same box. |
42
43
 
44
+ ## Compiled-schema cache (`rust/ingress/schema_cache.rs`)
45
+
46
+ | Variable | Type | Default | Description |
47
+ |----------|------|---------|-------------|
48
+ | `CASTRUM_SCHEMA_CACHE_MAX` | int | `128` | Max distinct request-body schemas retained process-wide. `Ingress`/`NativeRoute` share one compiled `IngressSchema` per distinct schema byte string; this bounds that LRU. Read at first schema compile (the cache is process-wide and cannot be resized afterwards). Values below 1 (unset, unparseable, `0`) fall back to the default; values are clamped to `4096`. Call `flushMemory()` to drop the cache explicitly. |
49
+
43
50
  ## Native transport selection (`src/native/ffi.ts`)
44
51
 
45
52
  `bun:ffi` is the PRIMARY transport under Bun; NAPI is the fallback (Node,
@@ -69,7 +69,8 @@ documented `JSTypedArrayBytesDeallocator` hook crashes the process (fault at
69
69
 
70
70
  ```
71
71
  rust/task/runtime.rs condvar pool, N = cores−1 (CASTRUM_TASK_THREADS), auto-start on submit,
72
- batch dequeue + bounded spin-then-park, optional core pinning
72
+ batch dequeue + bounded spin-then-park, optional core pinning,
73
+ HARD admission bound on the queue (CASTRUM_TASK_QUEUE_MAX)
73
74
  rust/task/completion.rs result ring + ARMED coalescing doorbell + needed-size drain layout
74
75
  rust/task/ops.rs op dispatch (header + payload split), cancellation set, catch_unwind
75
76
  containment, `_into` (zero-copy output) and slice (zero-copy input) forms
@@ -121,6 +122,17 @@ right one is picked per op:
121
122
  copied and the payload is read in place, so a large argument never crosses the
122
123
  boundary. This one was forced by a measurement, not symmetry (§7b).
123
124
 
125
+ **Bounded admission (backpressure).** The work queue is HARD-bounded by
126
+ `CASTRUM_TASK_QUEUE_MAX` (default 4096, minimum 1; read once when the pool is
127
+ created). A submit that would exceed the bound is rejected instead of growing
128
+ memory: `castrum_task_submit{,_out,_slice}` return `2 = overloaded`, and the JS
129
+ runtime rejects the task promise with a typed `code: 'OVERLOADED'` error. The
130
+ existing return meanings are unchanged — `1` accepted, `0` invalid args / panic.
131
+ A rejected zero-copy submit drops its `output`/`keep` reference exactly like any
132
+ other rejection, so a full queue cannot leak buffers. (The richer M2 design — a
133
+ `WouldBlock` + re-queue-behind-drain policy and priority levels — remains a
134
+ later milestone; v1 is a hard cap with a distinct overload outcome.)
135
+
124
136
  **Measured comparison** (`bun run bench:task`, Bun 1.4.2, 12 cores):
125
137
 
126
138
  | variant | time | loop stall | note |
@@ -466,9 +478,12 @@ rust/task/
466
478
  as in rayon/Go. Deliberately **separate from the rayon global pool**: rayon
467
479
  stays the batch executor for `par_iter`; the task runtime owns offloaded ops.
468
480
  `CASTRUM_TASK_THREADS` sizes it (default `cores − 1`).
469
- - **Admission/backpressure**: bounded queue (`CASTRUM_TASK_QUEUE_MAX`, default
470
- 4× threads). `submit` returns `WouldBlock` when full; JS resolves by queueing
471
- behind a `drain` promise — no unbounded growth (fixes F6).
481
+ - **Admission/backpressure** (v1 shipped): the queue is hard-bounded by
482
+ `CASTRUM_TASK_QUEUE_MAX` (default 4096, min 1, read once at pool creation).
483
+ `submit` returns `false` when full; the C ABI reports `2 = overloaded` and JS
484
+ rejects that promise with a typed `OVERLOADED` error — no unbounded growth
485
+ (fixes F6). M2 replaces this hard cap with `WouldBlock` + requeue-behind-drain
486
+ and priority levels; v1 has neither.
472
487
  - **Cancellation**: each task carries an `Arc<CancelToken>` (atomic + reason).
473
488
  Ops check it at safepoints (between chunks: gzip blocks, schema subtrees,
474
489
  Argon2 is atomic and reports "not cancellable"). Scope cancellation cascades
@@ -618,6 +633,7 @@ Each of these removes syscalls, lock traffic, or reallocations from the hot path
618
633
  | **Zero-copy INPUT** (`castrum_task_submit_slice` + the header/payload split in `ops.rs`) | Forced by a measurement: packing the payload into the args blob copied the whole input on the JS thread, making offloaded `gzipCompress` of 24 MiB **18.6 ms / 11.7 ms stall against 5.6 ms synchronous** — a net loss on both axes. Reading the payload in place: **7.3 ms / 2.1 ms**. The split also simplified every op arm (no more `args[4..]` offset arithmetic). |
619
634
  | **No BigInt, no per-completion DataView** | The drain loop reads ids and lengths as two `u32` reads at an offset — `getBigUint64` boxes a BigInt and `readU64` allocated a DataView *per completion*. Exact for any value under 2^53 (ids are a JS counter, sizes are byte counts). A zero-copy completion's body is never materialized at all. |
620
635
  | **Reusable args scratch** (copy path only) | Rust copies args on submit, so one growable buffer serves every synchronous submission. Deliberately NOT used for secret-bearing ops (pbkdf2/argon2) — a shared buffer would keep plaintext passwords alive far longer than a per-call allocation — nor for zero-copy retries, which re-submit after an `await` and must own their memory. |
636
+ | **Bounded admission** (`CASTRUM_TASK_QUEUE_MAX`, default 4096) | A hard cap turns sustained overload into an explicit rejection (`2 = overloaded` → typed `OVERLOADED`) instead of unbounded queue/`inflight` growth. The bound is a length check inside the same lock that pushes, so it is one comparison on a path that already locks. |
621
637
 
622
638
  ---
623
639