@ultimat3/core 31.20.0 → 31.22.0

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.
package/CLAUDE.md CHANGED
@@ -72,6 +72,7 @@ top-level `UltimateError` use in `error-codes.ts`.
72
72
  | loading `.env` | **Bun**, not us | `envFileCandidates()` documents the measured order; there is no `.env.staging` |
73
73
  | how long to wait, and whether to wait at all | `backoff.ts` + `retry-loop.ts` + `retry.ts` | one curve, one loop, one executor that meets the caller's signal and deadline; `jitter` REQUIRED on a retry policy; `attempt` is 1-based † |
74
74
  | N callers on one key | `single-flight.ts` | identity-checked eviction, optional injected deadline, optional injected `signal`: a live joiner re-leads after its leader's abort. Reads no context |
75
+ | a lazy boot or load, once per process | `retry-memo.ts` (`retryMemo`) | a rejection is never the memo's answer; never `x ??= start()` for a promise |
75
76
  | how many at once | `flight-gate.ts` | hand-over on release, refusal past `maxQueued`, injectable `overflow:` refusal |
76
77
  | a fan-out with a bound | `map-bounded.ts` (`mapBounded`) | `width` REQUIRED, no default; input order; first rejection aborts the rest via the signal. Never a hand-rolled worker pool |
77
78
  | whether an answer still applies | `generation-fence.ts` | `X_SUPERSEDED` / `isSuperseded` |
@@ -121,6 +122,8 @@ top-level `UltimateError` use in `error-codes.ts`.
121
122
  source of non-determinism. †
122
123
  - **`finiteOption` / `finiteCount` (`finite-option.ts`) are the framework's ONE screen for a numeric option.** Full rule: `docs/history/core.md`.
123
124
  - **`mcp-exposure.ts`'s `isMcpExposed` is the ONE answer to "did this opt into MCP?"** †
125
+ - **`service.ts` has no unregister for an app.** `registeredServices` / `restoreServices` are
126
+ test-only — `@ultimat3/testing`'s rerun seam; `resetServices` drops every module's for good.
124
127
 
125
128
  ## Metrics, tracing, reporting
126
129
 
package/README.md CHANGED
@@ -32,6 +32,7 @@ JSON.stringify({ dsn }); // the value never prints: a Secret redacts by value, a
32
32
  | the retry loop and the pure decision behind it, no context | `retry-loop.ts` |
33
33
  | which HTTP statuses are worth repeating | `retryable-status.ts` |
34
34
  | N callers on one key are ONE run | `single-flight.ts` |
35
+ | one lazy attempt, shared, and a failed one not kept | `retry-memo.ts` |
35
36
  | how many may run at once, and how many may wait | `flight-gate.ts` |
36
37
  | a concurrent map with a stated width — input order kept, the first rejection aborts the rest | `map-bounded.ts` |
37
38
  | whether an answer still applies — `X_SUPERSEDED` | `generation-fence.ts` |
@@ -322,6 +323,8 @@ A service that reads the actor (`ctx.posts`, scoped to `ctx.actor.orgId`) regist
322
323
  for — importing the module that calls `defineService` is the registration, the same convention
323
324
  `registerActions` uses. Passing `services: { posts: ... }` to `ctxOf` still works and
324
325
  wins over a registered factory of the same name, for a test that wants to hand in a mock.
326
+ Test-only: `registeredServices()` reads `{ name, factory }[]` and `restoreServices(entries)` makes
327
+ the registry exactly that — `@ultimat3/testing`'s rerun seam forgets one file's names with them.
325
328
 
326
329
  A factory runs again on **every** `ctxOf` / `withChildContext` call and is never cached,
327
330
  because it closes over the ctx (actor, clock, tz) it was built for. `withChildContext` drops a
@@ -852,6 +855,7 @@ nothing consulted it before deciding to try again. `As of 2026-08-23`.
852
855
  |---|---|---|
853
856
  | `backoffDelay({ attempt, base, max, factor?, curve?, jitter?, random? })` | one curve — `exponential \| linear \| fixed`, `full \| equal \| none` — 1-based `attempt`, clamped to `max` **before** jitter, rounded, and `0` rather than `NaN` | how long to wait. `random` is injectable, so a schedule is a unit test rather than a range |
854
857
  | `singleFlight({ deadlineMs?, schedule?, signal?, released? })` → `run(key, work, join?)`, `size`, `keys()`, `evict(key)` | N callers on one key are ONE run | who pays for a miss. With `signal`, a live joiner re-leads when its leader was aborted, rather than sharing that rejection. `released(key)` fires each time a key stops being held. Eviction is identity-checked, so a load that settles late never drops the load that replaced it; `deadlineMs` frees the KEY a wedged load would hold forever — it never cancels the work and never rejects a joiner. `evict(key)` does the same on demand, for a caller that learns the load in flight began before a write its result must reflect |
858
+ | `retryMemo(start)` → `get()`, `started()` | one lazy attempt, shared — a REJECTED one not kept | a boot or a load made once per process. `x ??= start()` keeps a rejection as the answer forever; here the clearing handler is attached at creation, so it runs ahead of every caller's `await` and the next `get()` really starts again. `started()` answers the attempt without making one, so a `close()` never boots what it stops. `@ultimat3/cli`'s `x mcp serve` and the docs site's corpus take it |
855
859
  | `flightGate({ maxConcurrent, maxQueued }, { subject?, overflow? })` | one bound, one queue, one refusal | how many at once. Past the queue the answer is `X_FLIGHT_GATE_OVERLOADED` (503) and never a longer queue; the slot is HANDED to a waiter, never released and re-acquired. Both limits are screened at construction (`finiteCount`, 0 allowed); a width of 0 refuses every caller rather than queueing for a slot that never frees. `run(work, signal?)`: an abort while QUEUED removes the waiter and rejects with the abort's reason |
856
860
  | `mapBounded(items, fn, { width, signal? })` | `Promise.all(items.map(fn))` with at most `width` in flight | a fan-out with a bound. Results in INPUT order. `width` is required with no default — the caller knows the vendor's limit — and screened by `finiteCount` (≥ 1, else `X_INVARIANT`). `fn(item, index, signal)`: the signal aborts on the first rejection (its reason IS that rejection) or when `signal` aborts; nothing new starts after either. Rejects at once with the first failure, as `Promise.all` does; the caller's abort is `X_ABORTED` naming how many finished |
857
861
  | `generationFence(subject)` → `generation()`, `bump()`, `guard(issued)` | whether an answer still applies | `X_SUPERSEDED` (499) and `isSuperseded(error)` — the piece nothing in the tree had. `guard` compares `!==`, never `<` |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/core",
3
- "version": "31.20.0",
3
+ "version": "31.22.0",
4
4
  "description": "Ultimate's foundation: errors, context, env, config, clock, ids, logging, telemetry, lifecycle",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -54,6 +54,6 @@
54
54
  "test": "bun test"
55
55
  },
56
56
  "dependencies": {
57
- "@ultimat3/schema": "31.20.0"
57
+ "@ultimat3/schema": "31.22.0"
58
58
  }
59
59
  }
package/src/index.ts CHANGED
@@ -911,6 +911,8 @@ export type { RetryDeps } from './retry';
911
911
  export { retry } from './retry';
912
912
  export type { RetryDecision, RetryPolicy, RetryStopReason } from './retry-loop';
913
913
  export { retryDecision } from './retry-loop';
914
+ export type { RetryMemo } from './retry-memo';
915
+ export { retryMemo } from './retry-memo';
914
916
  export { isRetryableStatus, RETRYABLE_STATUSES } from './retryable-status';
915
917
  export type { ResolveRoleOptions, Role, RoleInfo, ScalingSignal } from './roles';
916
918
  export { isRole, ROLE_INFO, ROLES, resolveRole } from './roles';
@@ -946,8 +948,11 @@ export {
946
948
  defineService,
947
949
  installedServices,
948
950
  registeredServiceNames,
951
+ registeredServices,
949
952
  resetServices,
953
+ restoreServices,
950
954
  type ServiceFactory,
955
+ type ServiceRegistration,
951
956
  } from './service';
952
957
  export { onSessionLost, sessionIsLost } from './session-lost';
953
958
  export { isSessionRefusal, isSessionRefusalCode } from './session-refusal';
@@ -0,0 +1,37 @@
1
+ // One in-flight attempt, shared — and a FAILED one not kept. `started ??= boot()` caches the
2
+ // rejection with the value: a Postgres that refused once answered every later call in an
3
+ // `x mcp serve` session with that error, and a docs corpus that failed to read stayed failed.
4
+
5
+ /**
6
+ * A lazily made attempt that may be made again.
7
+ *
8
+ * `started()` is the half `??=` has no spelling for: it answers the attempt already in flight or
9
+ * already made, and `undefined` when nobody has asked yet — so a `close()` can stop what was
10
+ * booted without BOOTING one in order to stop it.
11
+ */
12
+ export interface RetryMemo<T> {
13
+ get(): Promise<T>;
14
+ started(): Promise<T> | undefined;
15
+ }
16
+
17
+ /**
18
+ * `start` runs at most once per SUCCESS. `@ultimat3/db`'s `pgliteClient` states the rule this
19
+ * generalises — "a failed boot must not be cached" — and the clearing handler is attached at
20
+ * creation for the reason that matters: it therefore runs ahead of every caller's own `await`
21
+ * continuation, so by the time anyone sees the rejection the slot is already empty and the next
22
+ * call really does start a new attempt. Cleared inside the caller's `catch` instead, there is a
23
+ * window in which a second call is handed the dead promise.
24
+ */
25
+ export function retryMemo<T>(start: () => Promise<T>): RetryMemo<T> {
26
+ let attempt: Promise<T> | undefined;
27
+ return {
28
+ get(): Promise<T> {
29
+ attempt ??= start().catch((error: unknown) => {
30
+ attempt = undefined;
31
+ throw error;
32
+ });
33
+ return attempt;
34
+ },
35
+ started: (): Promise<T> | undefined => attempt,
36
+ };
37
+ }
package/src/service.ts CHANGED
@@ -73,3 +73,24 @@ export function resetServices(): void {
73
73
  export function registeredServiceNames(): readonly string[] {
74
74
  return [...factories.keys()];
75
75
  }
76
+
77
+ /** One registration, as `registeredServices` reads it and `restoreServices` writes it back. */
78
+ export interface ServiceRegistration {
79
+ readonly name: string;
80
+ readonly factory: ServiceFactory;
81
+ }
82
+
83
+ /** Test-only: every registered factory, in registration order — a copy, never the live map. */
84
+ export function registeredServices(): readonly ServiceRegistration[] {
85
+ return [...factories].map(([name, factory]) => ({ name, factory }));
86
+ }
87
+
88
+ /**
89
+ * Test-only: the registry becomes exactly `registrations`. The writer `resetServices` is not: a
90
+ * reset drops what every module imported earlier declared, and a module evaluates once per
91
+ * process — `@ultimat3/testing`'s rerun seam forgets one file's names with this and puts them back.
92
+ */
93
+ export function restoreServices(registrations: readonly ServiceRegistration[]): void {
94
+ factories.clear();
95
+ for (const { name, factory } of registrations) factories.set(name, factory);
96
+ }