@ultimat3/core 31.19.0 → 31.21.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 +1 -0
- package/README.md +3 -0
- package/package.json +2 -2
- package/src/index.ts +3 -0
- package/src/retry-memo.ts +37 -0
- package/src/session-lost.ts +2 -4
- package/src/session-refusal.ts +31 -0
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` |
|
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` |
|
|
@@ -43,6 +44,7 @@ JSON.stringify({ dsn }); // the value never prints: a Secret redacts by value, a
|
|
|
43
44
|
| the per-tab page handle (`globalThis[Symbol.for('ultimate.client')]`) records land in | `record-sink.ts` |
|
|
44
45
|
| which principal the page acts for, and the epoch that moves when it changes | `client-scope.ts` |
|
|
45
46
|
| whether the page's session is lost — the first `X_UNAUTHENTICATED` / `X_SESSION_EXPIRED` on a page rendered for a principal, said once (`onSessionLost`, `sessionIsLost`), cleared by `rescope()`; on `./page` | `session-lost.ts` |
|
|
47
|
+
| whether a thrown value or a wire code says "this credential names no session" — `X_UNAUTHENTICATED` / `X_SESSION_EXPIRED`, the one list (`isSessionRefusal`, `isSessionRefusalCode`). The http `auth` stage reads such a throw from an authenticator as anonymous, the realtime sweep as a revocation | `session-refusal.ts` |
|
|
46
48
|
| a poll a hidden tab and a lost session stop — `visibleInterval(run, ms, { maxMs? })`: no call while hidden, one when shown, a failed run doubling the wait up to `maxMs` (jittered) and a success resetting it, an idempotent disposer; on `./page` (6,045 B alone, `page-bundle.test.ts`) | `visible-interval.ts` |
|
|
47
49
|
| which row survives a conflict — `server-wins`, `last-write-wins`, `custom` | `conflict-policy.ts` |
|
|
48
50
|
| the four shapes an async region can be in | `async-state.ts` |
|
|
@@ -851,6 +853,7 @@ nothing consulted it before deciding to try again. `As of 2026-08-23`.
|
|
|
851
853
|
|---|---|---|
|
|
852
854
|
| `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 |
|
|
853
855
|
| `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 |
|
|
856
|
+
| `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 |
|
|
854
857
|
| `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 |
|
|
855
858
|
| `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 |
|
|
856
859
|
| `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.
|
|
3
|
+
"version": "31.21.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.
|
|
57
|
+
"@ultimat3/schema": "31.21.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';
|
|
@@ -950,6 +952,7 @@ export {
|
|
|
950
952
|
type ServiceFactory,
|
|
951
953
|
} from './service';
|
|
952
954
|
export { onSessionLost, sessionIsLost } from './session-lost';
|
|
955
|
+
export { isSessionRefusal, isSessionRefusalCode } from './session-refusal';
|
|
953
956
|
export type { FlightJoin, Scheduler, SingleFlight, SingleFlightOptions } from './single-flight';
|
|
954
957
|
export { singleFlight } from './single-flight';
|
|
955
958
|
export { ambientClock, noWaitClock } from './sleep-clock';
|
|
@@ -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/session-lost.ts
CHANGED
|
@@ -6,9 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { onRescope } from './client-scope';
|
|
8
8
|
import { sessionLostCell } from './record-sink';
|
|
9
|
-
|
|
10
|
-
/** The two codes that mean the credential this page was rendered with no longer resolves. */
|
|
11
|
-
const LOSS_CODES: ReadonlySet<string> = new Set(['X_UNAUTHENTICATED', 'X_SESSION_EXPIRED']);
|
|
9
|
+
import { SESSION_REFUSAL_CODES } from './session-refusal';
|
|
12
10
|
|
|
13
11
|
/** Whether a refusal has said this page's session is gone. `rescope()` clears it. */
|
|
14
12
|
export function sessionIsLost(): boolean {
|
|
@@ -81,7 +79,7 @@ export interface Refusal {
|
|
|
81
79
|
*/
|
|
82
80
|
export function noteRefusal(refusal: Refusal, principal: string | null | undefined): void {
|
|
83
81
|
if (typeof principal !== 'string' || refusal.status !== 401) return;
|
|
84
|
-
if (!
|
|
82
|
+
if (!SESSION_REFUSAL_CODES.has(refusal.code) || !isPageOrigin(refusal.url)) return;
|
|
85
83
|
const cell = sessionLostCell();
|
|
86
84
|
if (cell.lost) return;
|
|
87
85
|
cell.lost = true;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "This credential names no session", decided once. An authenticator answers it with `null` or by
|
|
3
|
+
* throwing one of these codes; every host — the http `auth` stage, the realtime re-auth sweep, the
|
|
4
|
+
* page's session-lost flag — reads both as the same answer, never as a backend failure.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { isUltimateError } from './errors';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `@ultimat3/auth`'s two session refusals: `X_SESSION_EXPIRED` past either clock,
|
|
11
|
+
* `X_UNAUTHENTICATED` for a session unknown, revoked, forged or whose user is disabled. Exported
|
|
12
|
+
* inside core only, never from `index.ts`: `session-lost.ts` reads the set directly, because a
|
|
13
|
+
* function wrapper around it cost every page bundle 28 B and bought no behaviour.
|
|
14
|
+
*/
|
|
15
|
+
export const SESSION_REFUSAL_CODES: ReadonlySet<string> = new Set([
|
|
16
|
+
'X_UNAUTHENTICATED',
|
|
17
|
+
'X_SESSION_EXPIRED',
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
/** Whether a wire code — a 401's `code` — says the credential names no session. */
|
|
21
|
+
export function isSessionRefusalCode(code: string): boolean {
|
|
22
|
+
return SESSION_REFUSAL_CODES.has(code);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Whether a thrown value is an authenticator's "no session" rather than a failure. Only a coded
|
|
27
|
+
* `UltimateError` counts: a plain object with a `code` field is not one this framework raised.
|
|
28
|
+
*/
|
|
29
|
+
export function isSessionRefusal(thrown: unknown): boolean {
|
|
30
|
+
return isUltimateError(thrown) && SESSION_REFUSAL_CODES.has(thrown.code);
|
|
31
|
+
}
|