@ultimat3/action 31.45.0 → 31.47.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 -1
- package/README.md +87 -1
- package/package.json +9 -9
- package/src/error-titles.ts +2 -0
- package/src/errors-http.ts +18 -2
- package/src/http.ts +6 -2
- package/src/replay-scope.ts +21 -0
package/CLAUDE.md
CHANGED
|
@@ -22,7 +22,7 @@ Owns the `action` + `mutator` primitives and five projections. Tier 3. The MCP t
|
|
|
22
22
|
| `tool-gate.ts` | a named tool's gate: `mcp` registers, `ai` reads, first wins |
|
|
23
23
|
| `define-api.ts` | `defineApi({ actions, mutators, queries, llm, jobs, tasks })` — the app's one boot call, and the `Api` type the typed clients are shaped from |
|
|
24
24
|
| `client-scale-pins.ts` | compile-time pin: `rpc<Api['actions']>` over 300 actions in 100 modules, 100 reads in 50 |
|
|
25
|
-
| `http.ts` | route projection (`enforcedBy: 'handler'`) + OpenAPI operation |
|
|
25
|
+
| `http.ts`, `replay-scope.ts` | route projection (`enforcedBy: 'handler'`) + OpenAPI operation |
|
|
26
26
|
| `openapi.ts` | deterministic OpenAPI 3.1 document |
|
|
27
27
|
| `openapi-complete.ts` | the COMPLETE document (`defineApi({ openapi })`: info, servers, security schemes, 401/429 by the ROUTE's `meta.auth`, a declared `rateLimit` or `meter`) and a bearer mount's own cut |
|
|
28
28
|
| `http-path.ts` | where an action is served: the app's `pathStyle` + a per-action `http.path` pin, read lazily by every projection; the pin's `:param` template + `method` (`ACTION_HTTP_METHODS`), `assertPinnedHttp`, `filledRoute`, `pathParametersOf`, `actionHttpPinOf` |
|
package/README.md
CHANGED
|
@@ -20,6 +20,16 @@ An action carries its own projections, so app code never reaches through `.def`
|
|
|
20
20
|
never imports a projection function:
|
|
21
21
|
|
|
22
22
|
```ts
|
|
23
|
+
import { type Actor } from '@ultimat3/core';
|
|
24
|
+
import { type Action, t } from '@ultimat3/action';
|
|
25
|
+
|
|
26
|
+
declare const actor: Actor;
|
|
27
|
+
declare const postId: string;
|
|
28
|
+
declare const baseUrl: string;
|
|
29
|
+
const PostView = t.object({ id: t.uuid, orgId: t.uuid, title: t.string });
|
|
30
|
+
const PublishPostInput = t.object({ postId: t.uuid });
|
|
31
|
+
declare const publishPost: Action<typeof PublishPostInput, typeof PostView>;
|
|
32
|
+
|
|
23
33
|
publishPost.input // the declared input schema
|
|
24
34
|
publishPost.output // the declared output schema
|
|
25
35
|
publishPost.policy // the one policy object
|
|
@@ -44,11 +54,29 @@ imports one package for the primitive and its schemas, never two.
|
|
|
44
54
|
|
|
45
55
|
```ts
|
|
46
56
|
import { action, t } from '@ultimat3/action';
|
|
57
|
+
import { can } from '@ultimat3/policy';
|
|
58
|
+
import { tag } from '@ultimat3/cache';
|
|
59
|
+
import { type JobHandle } from '@ultimat3/jobs';
|
|
60
|
+
import { type Actor } from '@ultimat3/core';
|
|
61
|
+
|
|
62
|
+
declare module '@ultimat3/core' {
|
|
63
|
+
interface CtxServices {
|
|
64
|
+
readonly posts: {
|
|
65
|
+
publish(id: string, tx?: unknown): Promise<{ id: string; orgId: string; title: string }>;
|
|
66
|
+
like(id: string): Promise<{ likeCount: number; likedByMe: boolean }>;
|
|
67
|
+
body(id: string): Promise<string>;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare const notifySubscribers: JobHandle<{ postId: string; orgId: string }>;
|
|
73
|
+
declare function ownsPost(actor: Actor | null, postId: string): boolean;
|
|
74
|
+
const PostView = t.object({ id: t.uuid, orgId: t.uuid, title: t.string });
|
|
47
75
|
|
|
48
76
|
export const publishPost = action({
|
|
49
77
|
input: t.object({ postId: t.uuid, orgId: t.uuid, notify: t.boolean.default(true) }),
|
|
50
78
|
output: PostView,
|
|
51
|
-
policy: can('post:publish', ({ input, actor }) => ownsPost(actor, input.postId)),
|
|
79
|
+
policy: can<{ postId: string }>('post:publish', ({ input, actor }) => ownsPost(actor, input.postId)),
|
|
52
80
|
cache: { invalidates: [tag.post, tag.feed] },
|
|
53
81
|
mcp: { expose: true, description: 'Publish a draft post' },
|
|
54
82
|
idempotent: true,
|
|
@@ -376,6 +404,27 @@ projection an action has; it adds `local(tx, input)` for the optimistic write an
|
|
|
376
404
|
strategy for the rebase.
|
|
377
405
|
|
|
378
406
|
```ts
|
|
407
|
+
import { mutator, t } from '@ultimat3/action';
|
|
408
|
+
import { can } from '@ultimat3/policy';
|
|
409
|
+
|
|
410
|
+
declare module '@ultimat3/action' {
|
|
411
|
+
interface LocalTables {
|
|
412
|
+
posts: { likedByMe: boolean; likeCount: number };
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
declare module '@ultimat3/core' {
|
|
417
|
+
interface CtxServices {
|
|
418
|
+
readonly posts: {
|
|
419
|
+
publish(id: string, tx?: unknown): Promise<{ id: string; orgId: string; title: string }>;
|
|
420
|
+
like(id: string): Promise<{ likeCount: number; likedByMe: boolean }>;
|
|
421
|
+
body(id: string): Promise<string>;
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const PostLikes = t.object({ likeCount: t.number.int(), likedByMe: t.boolean });
|
|
427
|
+
|
|
379
428
|
export const likePost = mutator({
|
|
380
429
|
input: t.object({ postId: t.uuid }),
|
|
381
430
|
output: PostLikes,
|
|
@@ -401,6 +450,16 @@ The projected surface carries the same three names the declaration used, on top
|
|
|
401
450
|
every action member above:
|
|
402
451
|
|
|
403
452
|
```ts
|
|
453
|
+
import { type LocalTx, type Mutator, t } from '@ultimat3/action';
|
|
454
|
+
import { type Ctx } from '@ultimat3/core';
|
|
455
|
+
|
|
456
|
+
const LikePostInput = t.object({ postId: t.uuid });
|
|
457
|
+
const PostLikes = t.object({ likeCount: t.number.int(), likedByMe: t.boolean });
|
|
458
|
+
declare const likePost: Mutator<typeof LikePostInput, typeof PostLikes>;
|
|
459
|
+
declare const tx: LocalTx;
|
|
460
|
+
declare const ctx: Ctx;
|
|
461
|
+
declare const postId: string;
|
|
462
|
+
|
|
404
463
|
likePost.local(tx, { postId }) // the optimistic write, replayed on rebase
|
|
405
464
|
await likePost.server(ctx, { postId }) // the authoritative write
|
|
406
465
|
likePost.conflict // the declared strategy
|
|
@@ -980,6 +1039,21 @@ which fields, whose tenant, chained or not, kept how long:
|
|
|
980
1039
|
|
|
981
1040
|
```ts
|
|
982
1041
|
// apps/web/runtime.ts — every name below but `runtime` is the app's own
|
|
1042
|
+
import { type RuntimeOverrides } from '@ultimat3/cli/serve';
|
|
1043
|
+
import { type Actor, type Ctx } from '@ultimat3/core';
|
|
1044
|
+
|
|
1045
|
+
declare function chainHead(ctx: Ctx): Promise<string>;
|
|
1046
|
+
declare function orgOf(actor: Actor): string;
|
|
1047
|
+
declare function subjectOf(name: string, input: unknown): string;
|
|
1048
|
+
declare function impersonatorOf(actor: Actor): string | null;
|
|
1049
|
+
declare function sha256(prev: string, record: unknown): Promise<string>;
|
|
1050
|
+
|
|
1051
|
+
declare module '@ultimat3/core' {
|
|
1052
|
+
interface CtxServices {
|
|
1053
|
+
readonly db: { readonly auditRows: { insert(row: Record<string, unknown>): Promise<unknown> } };
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
|
|
983
1057
|
export const runtime = { auditSink: {
|
|
984
1058
|
async write(record) {
|
|
985
1059
|
const { ctx } = record; // the services a sink needs to write a row
|
|
@@ -1021,6 +1095,18 @@ asserts the denial, not `X_FORBIDDEN`: a denial carries the policy decision's ow
|
|
|
1021
1095
|
`can()` answers a null actor with `X_UNAUTHENTICATED`.
|
|
1022
1096
|
|
|
1023
1097
|
```ts
|
|
1098
|
+
import { type Actor, type Ctx } from '@ultimat3/core';
|
|
1099
|
+
import { type Action, t } from '@ultimat3/action';
|
|
1100
|
+
|
|
1101
|
+
declare const myCtx: Ctx;
|
|
1102
|
+
|
|
1103
|
+
declare const actor: Actor;
|
|
1104
|
+
const PostView = t.object({ id: t.uuid, orgId: t.uuid, title: t.string });
|
|
1105
|
+
const PublishPostInput = t.object({ postId: t.uuid });
|
|
1106
|
+
declare const publishPost: Action<typeof PublishPostInput, typeof PostView>;
|
|
1107
|
+
declare const orgId: string;
|
|
1108
|
+
declare const postId: string;
|
|
1109
|
+
|
|
1024
1110
|
publishPost.contract({
|
|
1025
1111
|
garbage: 42, // what the input schema must reject
|
|
1026
1112
|
input: { postId, orgId }, // when the synthesized one cannot fit
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/action",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.47.0",
|
|
4
4
|
"description": "The action primitive: one declaration projected to route, OpenAPI, client, MCP tool, job handle, tests",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -48,13 +48,13 @@
|
|
|
48
48
|
"test": "bun test"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@ultimat3/cache": "31.
|
|
52
|
-
"@ultimat3/core": "31.
|
|
53
|
-
"@ultimat3/db": "31.
|
|
54
|
-
"@ultimat3/entity": "31.
|
|
55
|
-
"@ultimat3/http": "31.
|
|
56
|
-
"@ultimat3/policy": "31.
|
|
57
|
-
"@ultimat3/schema": "31.
|
|
58
|
-
"@ultimat3/storage": "31.
|
|
51
|
+
"@ultimat3/cache": "31.47.0",
|
|
52
|
+
"@ultimat3/core": "31.47.0",
|
|
53
|
+
"@ultimat3/db": "31.47.0",
|
|
54
|
+
"@ultimat3/entity": "31.47.0",
|
|
55
|
+
"@ultimat3/http": "31.47.0",
|
|
56
|
+
"@ultimat3/policy": "31.47.0",
|
|
57
|
+
"@ultimat3/schema": "31.47.0",
|
|
58
|
+
"@ultimat3/storage": "31.47.0"
|
|
59
59
|
}
|
|
60
60
|
}
|
package/src/error-titles.ts
CHANGED
|
@@ -52,6 +52,8 @@ export const ACTION_ERROR_TITLES: Readonly<Record<string, string>> = {
|
|
|
52
52
|
X_ACTION_STREAM_INVALID: 'a streaming action broke its stream contract',
|
|
53
53
|
X_METER_EXCEEDED: "an action's usage meter is spent for this period",
|
|
54
54
|
X_METER_INVALID: "an action's meter cannot be counted against",
|
|
55
|
+
X_REPLAY_PRINCIPAL_MISMATCH:
|
|
56
|
+
'a queued offline write was replayed under a session that did not queue it',
|
|
55
57
|
};
|
|
56
58
|
|
|
57
59
|
// One unconditional call: a presence guard would turn "another package claims one of these codes"
|
package/src/errors-http.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The refusals of the HTTP declaration an app makes about its actions
|
|
3
|
-
* app's `pathStyle`,
|
|
2
|
+
* The refusals of the HTTP projection: the declaration an app makes about its actions (a pinned
|
|
3
|
+
* `http.path`, the app's `pathStyle`, the `openapi` block) and a replay under the wrong session. Split from `errors.ts` at its ceiling; the codes and
|
|
4
4
|
* titles are registered in `error-titles.ts`, one registry, imported bare so constructing one of
|
|
5
5
|
* these registers its title first.
|
|
6
6
|
*/
|
|
@@ -84,3 +84,19 @@ export class OpenApiConfigInvalidError extends UltimateError {
|
|
|
84
84
|
});
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A queued offline write came back under a session that is not the principal whose queue holds it.
|
|
90
|
+
* 409, which a page reads as "ask again later": the write is not refused on its merits, it is held
|
|
91
|
+
* for its owner. Neither principal is rendered: the answer goes to whoever holds the cookie now.
|
|
92
|
+
*/
|
|
93
|
+
export class ReplayPrincipalMismatchError extends UltimateError {
|
|
94
|
+
constructor(input: { name: string }) {
|
|
95
|
+
super({
|
|
96
|
+
code: 'X_REPLAY_PRINCIPAL_MISMATCH',
|
|
97
|
+
cause: `a queued write to action "${input.name}" names the principal that queued it, and this session is someone else: another tab signed out or in, or SESSION_SECRET is unset and another process rendered the page`,
|
|
98
|
+
fix: 'await pageOutbox().replay() // @ultimat3/realtime, in the tab that queued the write, once that account has signed back in: the entry is kept for it, and loading a page as another account wipes it',
|
|
99
|
+
meta: { action: input.name },
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
package/src/http.ts
CHANGED
|
@@ -44,6 +44,7 @@ import { answerNatively, isNativeFormPost } from './native-answer';
|
|
|
44
44
|
import { withDeclaredErrors } from './openapi-errors';
|
|
45
45
|
import { assertActionRateLimit, fixedRateLimit } from './rate-limit-gate';
|
|
46
46
|
import { carriesRecords, recordResponse } from './record-wire';
|
|
47
|
+
import { assertReplayPrincipal, REPLAY_REFUSAL } from './replay-scope';
|
|
47
48
|
|
|
48
49
|
/** The digest a request's idempotency key names its write by; none for a missing or blank one. */
|
|
49
50
|
async function writeOriginOf(req: UltimateRequest): Promise<string | undefined> {
|
|
@@ -121,6 +122,8 @@ function projectRoute(
|
|
|
121
122
|
// 403ing still has to learn the endpoint is going away.
|
|
122
123
|
for (const [header, value] of Object.entries(sunsetting)) req.ctx.headers.set(header, value);
|
|
123
124
|
}
|
|
125
|
+
// First, before the body is read: a replay under the wrong session is not this caller's write.
|
|
126
|
+
assertReplayPrincipal(req, name);
|
|
124
127
|
// No `catch`: an error — framework or not — takes the path every route's error takes. This
|
|
125
128
|
// used to answer `problem(error)` itself, so the `error-map` stage never saw it: no
|
|
126
129
|
// `onError`/`reportError` on a 5xx, no HTML error page or sign-in redirect for a browser, no
|
|
@@ -267,8 +270,9 @@ export function toOpenApiOperation(target: AnyAction): OpenApiOperation {
|
|
|
267
270
|
required: true,
|
|
268
271
|
content: { 'application/json': { schema: { $ref: schemaRef(inputSchemaName(name)) } } },
|
|
269
272
|
},
|
|
270
|
-
// The declared refusals join the framework's own, per status (`openapi-errors.ts`).
|
|
271
|
-
|
|
273
|
+
// The declared refusals join the framework's own, per status (`openapi-errors.ts`). The replay
|
|
274
|
+
// refusal is every action's: any of them may be a queued write replayed under another session.
|
|
275
|
+
responses: withDeclaredErrors([...new Set([...(def.errors ?? []), REPLAY_REFUSAL])], {
|
|
272
276
|
// Only an output that references an entity row changes shape: every other operation's
|
|
273
277
|
// bytes are the ones `x verify`'s contract diff already holds.
|
|
274
278
|
'200': enveloped
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server half of a queued write's binding: a request naming the principal it was queued by runs
|
|
3
|
+
* only under that principal's session. The page cannot enforce this itself, because the cookie is
|
|
4
|
+
* shared with tabs it cannot hear. A request naming nobody is every ordinary call, and passes.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { CLIENT_REPLAY_SCOPE_HEADER, clientScopeId, principalScopeName } from '@ultimat3/core';
|
|
8
|
+
import type { UltimateRequest } from '@ultimat3/http';
|
|
9
|
+
import { ReplayPrincipalMismatchError } from './errors-http';
|
|
10
|
+
|
|
11
|
+
/** What `assertReplayPrincipal` refuses with, as the OpenAPI operation publishes it. */
|
|
12
|
+
export const REPLAY_REFUSAL = 'X_REPLAY_PRINCIPAL_MISMATCH';
|
|
13
|
+
|
|
14
|
+
export function assertReplayPrincipal(req: UltimateRequest, name: string): void {
|
|
15
|
+
const named = req.header(CLIENT_REPLAY_SCOPE_HEADER);
|
|
16
|
+
if (named === null) return;
|
|
17
|
+
const scope = clientScopeId(req.ctx.actor);
|
|
18
|
+
if (named !== principalScopeName(scope === '' ? null : scope)) {
|
|
19
|
+
throw new ReplayPrincipalMismatchError({ name });
|
|
20
|
+
}
|
|
21
|
+
}
|