@ultimat3/action 31.51.0 → 31.52.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 +4 -5
- package/README.md +51 -0
- package/package.json +9 -9
- package/src/action-decide-def.ts +44 -0
- package/src/action-decide-pins.ts +58 -0
- package/src/action-decide.ts +81 -0
- package/src/action.ts +25 -2
- package/src/index.ts +6 -0
- package/src/invoke.ts +8 -1
- package/src/validate-unknown-keys.ts +84 -0
- package/src/validate.ts +4 -0
package/CLAUDE.md
CHANGED
|
@@ -56,7 +56,7 @@ Owns the `action` + `mutator` primitives and five projections. Tier 3. The MCP t
|
|
|
56
56
|
| `idempotency-memory.ts` | the process default: bounded, swept, `scope: 'process'` |
|
|
57
57
|
| `idempotency-redact.ts` | `restingAnswer` — the copy of an answer a store may keep: core's `isRedactedKey` + `isSecret`, the SAME reference when nothing is redacted |
|
|
58
58
|
| `idempotency-postgres.ts` | the SHARED store — one table, one `insert … on conflict` |
|
|
59
|
-
| `policy-gate.ts` | **the
|
|
59
|
+
| `policy-gate.ts` | **the** call's authz gate (`guardPrimitive`); `action-decide.ts` is the handler's per-row one |
|
|
60
60
|
| `cache-gate.ts` | the post-COMMIT bust — **the only** file that calls `invalidateTags` |
|
|
61
61
|
| `tx-scope.ts` | the open transaction — **the only** file that imports `@ultimat3/db` (`tx-scope.test.ts`): `openCommitScope` (the bust), `liveTransaction` (the settle), `completeTransaction`, and `inRolledBackScope`, the exercise's always-rolled-back scope and the memory transaction it opens always |
|
|
62
62
|
| `rate-limit-gate.ts` | **the one place** a declared `rateLimit:` is spent (a `limit` resolved per call from `ctx`, through `toBucket`) |
|
|
@@ -70,7 +70,7 @@ Owns the `action` + `mutator` primitives and five projections. Tier 3. The MCP t
|
|
|
70
70
|
| `audit-input.ts` | what may be written DOWN: an `input` redacted through core's table and made JSON-representable on every path |
|
|
71
71
|
| `audit-gate.ts` | **the only** file that calls a sink, and where the two failure policies live |
|
|
72
72
|
| `type-pins.ts` | compile-time assertions `tsc` checks — what the erased view projects, and why `client()` is not part of it |
|
|
73
|
-
| `naming.ts`, `validate.ts`, `json-schema.ts`, `stable.ts` | pure helpers. `stable.ts` is the DOCUMENT serializer (core's `isJsonObject` is imported from core, never re-exported) — the hash form is `@ultimat3/core`'s `canonicalJson`/`fingerprint` |
|
|
73
|
+
| `naming.ts`, `validate.ts`, `json-schema.ts`, `stable.ts` | pure helpers; `validate-unknown-keys.ts` warns `X_INPUT_UNKNOWN_KEY` (#1281). `stable.ts` is the DOCUMENT serializer (core's `isJsonObject` is imported from core, never re-exported) — the hash form is `@ultimat3/core`'s `canonicalJson`/`fingerprint` |
|
|
74
74
|
|
|
75
75
|
## Invariants — execution and authz
|
|
76
76
|
|
|
@@ -92,9 +92,8 @@ Owns the `action` + `mutator` primitives and five projections. Tier 3. The MCP t
|
|
|
92
92
|
from `@ultimat3/policy` by `http.ts`; 25.0.0 dropped this package's re-export, `index.test.ts`
|
|
93
93
|
pins the absence), never the root combinator — never a copy here (`@ultimat3/query` needs the same
|
|
94
94
|
answer).
|
|
95
|
-
- Authz
|
|
96
|
-
|
|
97
|
-
`@ultimat3/policy` (`errors.ts`'s `import type { SurfaceDenial }` erases).
|
|
95
|
+
- Authz is `guardPrimitive` in `policy-gate.ts`; a denial is `ActionDeniedError` with the policy's
|
|
96
|
+
code. `decide(rows)` (`action-decide.ts`), bound by `invoke`: own policy, rows only.
|
|
98
97
|
- No policy at registration → `X_ACTION_POLICY_MISSING`. No exceptions, no flag.
|
|
99
98
|
- **`registerAction` guards the derived PATH as well as the name** (`X_ACTION_PATH_DUPLICATE`; a
|
|
100
99
|
second index cleared by `resetActions`).
|
package/README.md
CHANGED
|
@@ -407,6 +407,57 @@ aborted transaction (`X_DB_TRANSACTION_ABORTED`). Busting at the handler's retur
|
|
|
407
407
|
concurrent read refill the cache from the pre-commit rows, which then stayed for the TTL. With no
|
|
408
408
|
transaction open the bust runs when the handler returns, as it always did.
|
|
409
409
|
|
|
410
|
+
**An undeclared input key is reported, not refused** (#1281). After a good parse, a key the input
|
|
411
|
+
schema does not declare is logged once per action and path set as `input.unknown_key`
|
|
412
|
+
(`X_INPUT_UNKNOWN_KEY`, naming the nearest declared key) and added to the call's span on every
|
|
413
|
+
call. The answer is byte-identical; refusing the key waits on `next`.
|
|
414
|
+
|
|
415
|
+
### `decide(rows)` — the action's own policy, per row
|
|
416
|
+
|
|
417
|
+
A handler acting on many rows asks the rule it was declared with, once per row:
|
|
418
|
+
|
|
419
|
+
```ts
|
|
420
|
+
import { action, t } from '@ultimat3/action';
|
|
421
|
+
import { can } from '@ultimat3/policy';
|
|
422
|
+
|
|
423
|
+
interface Post {
|
|
424
|
+
readonly id: string;
|
|
425
|
+
readonly authorId: string;
|
|
426
|
+
}
|
|
427
|
+
declare function findPosts(ids: readonly string[]): Promise<Post[]>;
|
|
428
|
+
declare function archive(posts: readonly Post[]): Promise<void>;
|
|
429
|
+
|
|
430
|
+
export const archivePosts = action({
|
|
431
|
+
input: t.object({ ids: t.array(t.string) }),
|
|
432
|
+
output: t.object({ archived: t.array(t.string), refused: t.array(t.string) }),
|
|
433
|
+
// `row: null` is the CALL (may this actor archive at all); a row is that row.
|
|
434
|
+
policy: can<unknown, Post>(
|
|
435
|
+
'post:archive',
|
|
436
|
+
({ actor, row }) => row === null || row.authorId === actor?.id,
|
|
437
|
+
),
|
|
438
|
+
async handle({ input, decide }) {
|
|
439
|
+
const { allowed, denied } = decide(await findPosts(input.ids));
|
|
440
|
+
await archive(allowed);
|
|
441
|
+
return {
|
|
442
|
+
archived: allowed.map((post) => post.id),
|
|
443
|
+
refused: denied.map(({ row }) => row.id),
|
|
444
|
+
};
|
|
445
|
+
},
|
|
446
|
+
});
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
| Rule | Why |
|
|
450
|
+
|---|---|
|
|
451
|
+
| `decide(rows) → { allowed, denied: [{ row, code, reason }] }`, both in row order | the split is data; skip-denied vs abort-all is the handler's convention, never the framework's |
|
|
452
|
+
| the action's OWN policy, bound by `invoke` — rows are the only parameter | a handler has no way to name another rule (a type error, and ignored at runtime) |
|
|
453
|
+
| judged on the call's surface (`PRIMITIVE_POLICY_SURFACES`, the table `guardPrimitive` reads); one `policy.decision` event per row, through `evaluateEach` → `evaluate` | no second authz path, and an audit sees every row |
|
|
454
|
+
| only `allowed: true` allows; a predicate that throws fails the call, no list returned | fail closed: a garbage decision or a throw never moves a row into `allowed` |
|
|
455
|
+
| a `null`/`undefined` row is denied (`X_FORBIDDEN`) without asking the policy | `row: null` is the call-level question, so a lookup that found nothing would borrow the call's allow |
|
|
456
|
+
| on `handle` only — `row()` and every stage before the parse have none | the input stage has not been judged yet |
|
|
457
|
+
|
|
458
|
+
Every surface (HTTP, MCP, `.job()`, `.as()`, a direct call) reaches `handle` through `invoke`, so
|
|
459
|
+
every one gets the same `decide`. `mutator`'s `server(ctx, input)` does not receive it.
|
|
460
|
+
|
|
410
461
|
Registering an action without `policy:` throws `X_ACTION_POLICY_MISSING`; there is
|
|
411
462
|
no bypass flag. A look-alike that never came out of `action()` is `X_ACTION_FOREIGN`.
|
|
412
463
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/action",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.52.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.52.0",
|
|
52
|
+
"@ultimat3/core": "31.52.0",
|
|
53
|
+
"@ultimat3/db": "31.52.0",
|
|
54
|
+
"@ultimat3/entity": "31.52.0",
|
|
55
|
+
"@ultimat3/http": "31.52.0",
|
|
56
|
+
"@ultimat3/policy": "31.52.0",
|
|
57
|
+
"@ultimat3/schema": "31.52.0",
|
|
58
|
+
"@ultimat3/storage": "31.52.0"
|
|
59
59
|
}
|
|
60
60
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// The declaration `action()` types a handler by: `ActionDef` whose `handle` is handed
|
|
2
|
+
// `decide(rows)`. A pair beside `ActionDef`, never an edit of it: `ActionDef.handle` is also what
|
|
3
|
+
// app code CALLS on a declaration it holds, and a required `decide` there refuses a call written
|
|
4
|
+
// before it existed. `action()` reads these first; `ActionDef` stays assignable to them.
|
|
5
|
+
|
|
6
|
+
import type { InferInput, InferOutput, StandardSchemaV1 } from '@ultimat3/schema';
|
|
7
|
+
import type { ActionDef, ActionHandlerArgs, StreamActionDef } from './action';
|
|
8
|
+
import type { ActionDecision } from './action-decide';
|
|
9
|
+
|
|
10
|
+
export interface ActionDecideArgs<TInput extends StandardSchemaV1, TRow = unknown>
|
|
11
|
+
extends ActionHandlerArgs<TInput> {
|
|
12
|
+
/**
|
|
13
|
+
* This action's OWN policy, asked once per row on the surface the call came in on — one
|
|
14
|
+
* `policy.decision` event per row. Rows only: no parameter names another rule. Only in `handle`,
|
|
15
|
+
* after the input parse and the call's own guard; `row()` never gets it. What a handler does with
|
|
16
|
+
* the denied rows (skip them, abort the batch) is its own convention (`action-decide.ts`).
|
|
17
|
+
* Method syntax on purpose: its bivariant parameter is what keeps a `DecidingActionDef<…, Post>`
|
|
18
|
+
* assignable to the erased declaration every projection reads.
|
|
19
|
+
*/
|
|
20
|
+
decide(rows: readonly TRow[]): ActionDecision<TRow>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** What `action({ … })` takes: `ActionDef`, its `handle` handed `decide`. */
|
|
24
|
+
export interface DecidingActionDef<
|
|
25
|
+
TInput extends StandardSchemaV1,
|
|
26
|
+
TOutput extends StandardSchemaV1,
|
|
27
|
+
TRow = unknown,
|
|
28
|
+
> extends Omit<ActionDef<TInput, TOutput, TRow>, 'handle'> {
|
|
29
|
+
handle(
|
|
30
|
+
args: ActionDecideArgs<TInput, TRow>,
|
|
31
|
+
): Promise<InferOutput<TOutput>> | InferOutput<TOutput>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** What `action({ stream, … })` takes: `StreamActionDef`, its `handle` handed `decide`. */
|
|
35
|
+
export interface DecidingStreamActionDef<
|
|
36
|
+
TInput extends StandardSchemaV1,
|
|
37
|
+
TOutput extends StandardSchemaV1,
|
|
38
|
+
TChunk extends StandardSchemaV1,
|
|
39
|
+
TRow = unknown,
|
|
40
|
+
> extends Omit<StreamActionDef<TInput, TOutput, TChunk, TRow>, 'handle'> {
|
|
41
|
+
handle(
|
|
42
|
+
args: ActionDecideArgs<TInput, TRow>,
|
|
43
|
+
): AsyncGenerator<InferInput<TChunk>, InferOutput<TOutput>, undefined>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Compile-time pins for `decide(rows)` landing additively: `ActionDef` and `ActionHandlerArgs` keep
|
|
2
|
+
// their 31.51.0 shapes — a call `def.handle({ input, ctx })` written then still compiles — and the
|
|
3
|
+
// `Deciding*` pair `action()` reads first hands a handler `decide` typed by the policy's row. Types
|
|
4
|
+
// only, for `type-pins.ts`' reason: `tsc -b` never reads a test file.
|
|
5
|
+
|
|
6
|
+
import type { Ctx } from '@ultimat3/core';
|
|
7
|
+
import type { InferOutput, StandardSchemaV1 } from '@ultimat3/schema';
|
|
8
|
+
import type { ActionDef, ActionHandlerArgs, StreamActionDef } from './action';
|
|
9
|
+
import type { ActionDecision } from './action-decide';
|
|
10
|
+
import type {
|
|
11
|
+
ActionDecideArgs,
|
|
12
|
+
DecidingActionDef,
|
|
13
|
+
DecidingStreamActionDef,
|
|
14
|
+
} from './action-decide-def';
|
|
15
|
+
|
|
16
|
+
type Assert<T extends true> = T;
|
|
17
|
+
|
|
18
|
+
type Identical<A, B> =
|
|
19
|
+
(<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
|
|
20
|
+
|
|
21
|
+
type In = StandardSchemaV1<{ readonly ids: readonly string[] }>;
|
|
22
|
+
type Out = StandardSchemaV1<{ readonly n: number }>;
|
|
23
|
+
type Chunk = StandardSchemaV1<string>;
|
|
24
|
+
interface Post {
|
|
25
|
+
readonly id: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 31.51.0's `ActionHandlerArgs`, spelt out: what a caller of `def.handle(…)` passes. */
|
|
29
|
+
interface RecordedArgs {
|
|
30
|
+
readonly input: InferOutput<In>;
|
|
31
|
+
readonly ctx: Ctx;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type _HandlerArgsKeep3151Shape = Assert<Identical<ActionHandlerArgs<In>, RecordedArgs>>;
|
|
35
|
+
export type _ActionDefHandleTakes3151Args = Assert<
|
|
36
|
+
Identical<Parameters<ActionDef<In, Out, Post>['handle']>[0], ActionHandlerArgs<In>>
|
|
37
|
+
>;
|
|
38
|
+
export type _StreamDefHandleTakes3151Args = Assert<
|
|
39
|
+
Identical<Parameters<StreamActionDef<In, Out, Chunk, Post>['handle']>[0], ActionHandlerArgs<In>>
|
|
40
|
+
>;
|
|
41
|
+
|
|
42
|
+
/** A declaration an app typed `ActionDef` still goes into `action()`'s first overload. */
|
|
43
|
+
export type _ActionDefIsADecidingDef = Assert<
|
|
44
|
+
ActionDef<In, Out, Post> extends DecidingActionDef<In, Out, Post> ? true : false
|
|
45
|
+
>;
|
|
46
|
+
export type _StreamDefIsADecidingDef = Assert<
|
|
47
|
+
StreamActionDef<In, Out, Chunk, Post> extends DecidingStreamActionDef<In, Out, Chunk, Post>
|
|
48
|
+
? true
|
|
49
|
+
: false
|
|
50
|
+
>;
|
|
51
|
+
|
|
52
|
+
/** The handler's `decide` takes the policy's rows alone and answers them. */
|
|
53
|
+
type Decide = Parameters<DecidingActionDef<In, Out, Post>['handle']>[0]['decide'];
|
|
54
|
+
export type _DecideTakesRowsAlone = Assert<Identical<Parameters<Decide>, [rows: readonly Post[]]>>;
|
|
55
|
+
export type _DecideAnswersTheRows = Assert<Identical<ReturnType<Decide>, ActionDecision<Post>>>;
|
|
56
|
+
export type _DecideArgsAreTheRecordedOnesPlusDecide = Assert<
|
|
57
|
+
Identical<Exclude<keyof ActionDecideArgs<In, Post>, keyof RecordedArgs>, 'decide'>
|
|
58
|
+
>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `decide(rows)` for a handler: the action's OWN policy, asked once per row on the surface the
|
|
3
|
+
* call came in on. Bound here from the declaration, so the handler holds a function of rows alone
|
|
4
|
+
* and has no parameter through which to name another rule — skip-denied or abort-all stays its own.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Actor, Ctx } from '@ultimat3/core';
|
|
8
|
+
import { codeOf, evaluateEach, PRIMITIVE_POLICY_SURFACES, reasonOf } from '@ultimat3/policy';
|
|
9
|
+
import type { ActionPolicy, Surface } from './policy-gate';
|
|
10
|
+
|
|
11
|
+
/** A row the action's policy refused, with the code and reason it would have refused the call. */
|
|
12
|
+
export interface ActionRowDenial<TRow> {
|
|
13
|
+
readonly row: TRow;
|
|
14
|
+
readonly code: string;
|
|
15
|
+
readonly reason: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ActionDecision<TRow> {
|
|
19
|
+
/** In the order given. Only a row whose evaluation was exactly `allowed: true` lands here. */
|
|
20
|
+
readonly allowed: readonly TRow[];
|
|
21
|
+
/** In the order given: a missing (`null`/`undefined`) row, or one the policy refused. */
|
|
22
|
+
readonly denied: readonly ActionRowDenial<TRow>[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Rows only: the policy, the actor, the input and the surface are the call's, never the caller's. */
|
|
26
|
+
export type ActionDecide<TRow> = (rows: readonly TRow[]) => ActionDecision<TRow>;
|
|
27
|
+
|
|
28
|
+
/** Safe to log: names no row. */
|
|
29
|
+
const MISSING_ROW = 'no row: a missing row is never evidence of permission';
|
|
30
|
+
|
|
31
|
+
export interface DecideSubject {
|
|
32
|
+
readonly actor: Actor | null;
|
|
33
|
+
/** The parsed input — the same object `handle` and the call's own guard saw. */
|
|
34
|
+
readonly input: unknown;
|
|
35
|
+
readonly ctx: Ctx;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The surface is read off `PRIMITIVE_POLICY_SURFACES`, the table `guardPrimitive` judges the call
|
|
40
|
+
* itself by, so a row's decision event names the surface the call's own decision did. Every
|
|
41
|
+
* evaluation goes through `evaluateEach` → `evaluate`: one decision event per row, no second path.
|
|
42
|
+
*/
|
|
43
|
+
export function decideFor<TRow>(
|
|
44
|
+
policy: ActionPolicy<TRow>,
|
|
45
|
+
subject: DecideSubject,
|
|
46
|
+
surface: Surface,
|
|
47
|
+
): ActionDecide<TRow> {
|
|
48
|
+
const target = PRIMITIVE_POLICY_SURFACES.action[surface];
|
|
49
|
+
return (rows) => {
|
|
50
|
+
// A missing row is never asked: `evaluate()` hands the predicate `null` for it, and `null` is
|
|
51
|
+
// the CALL-level question a batch rule admits — so a lookup that found nothing would borrow
|
|
52
|
+
// the call's allow. Denied here instead, the way an action's rule must read `row: null`.
|
|
53
|
+
const present = rows.filter((row) => row !== null && row !== undefined);
|
|
54
|
+
const evaluations = evaluateEach(
|
|
55
|
+
policy,
|
|
56
|
+
{ input: subject.input, actor: subject.actor, rows: present, ctx: subject.ctx },
|
|
57
|
+
{ surface: target },
|
|
58
|
+
);
|
|
59
|
+
const allowed: TRow[] = [];
|
|
60
|
+
const denied: ActionRowDenial<TRow>[] = [];
|
|
61
|
+
let next = 0;
|
|
62
|
+
for (const row of rows) {
|
|
63
|
+
if (row === null || row === undefined) {
|
|
64
|
+
denied.push({ row, code: 'X_FORBIDDEN', reason: MISSING_ROW });
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const evaluation = evaluations[next];
|
|
68
|
+
next += 1;
|
|
69
|
+
// `=== true`, never truthiness: a hand-built policy whose `run` answers `{ allowed: 'yes' }`
|
|
70
|
+
// must not move a row into `allowed` — fail closed, the way `isDecision` does for a predicate.
|
|
71
|
+
if (evaluation?.allowed === true) allowed.push(row);
|
|
72
|
+
else
|
|
73
|
+
denied.push({
|
|
74
|
+
row,
|
|
75
|
+
code: (evaluation && codeOf(evaluation.decision)) ?? 'X_FORBIDDEN',
|
|
76
|
+
reason: (evaluation && reasonOf(evaluation.decision)) ?? 'denied',
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return Object.freeze({ allowed: Object.freeze(allowed), denied: Object.freeze(denied) });
|
|
80
|
+
};
|
|
81
|
+
}
|
package/src/action.ts
CHANGED
|
@@ -13,6 +13,8 @@ import type { RateLimitDecision } from '@ultimat3/http';
|
|
|
13
13
|
import { declaredStatusFor } from '@ultimat3/http';
|
|
14
14
|
import { policyCapability, policyPermissions } from '@ultimat3/policy';
|
|
15
15
|
import type { InferInput, InferOutput, StandardSchemaV1 } from '@ultimat3/schema';
|
|
16
|
+
import type { ActionDecision } from './action-decide';
|
|
17
|
+
import type { DecidingActionDef, DecidingStreamActionDef } from './action-decide-def';
|
|
16
18
|
import { streamingDef } from './action-stream';
|
|
17
19
|
import type { ClientMethod, ClientOptions, StreamClientMethod } from './client';
|
|
18
20
|
import type { ContractTest, ContractTestOptions } from './contract-test';
|
|
@@ -263,7 +265,11 @@ export interface AnyActionDef {
|
|
|
263
265
|
readonly audit?: boolean;
|
|
264
266
|
readonly stream?: StandardSchemaV1;
|
|
265
267
|
row?(args: { readonly input: unknown; readonly ctx: Ctx }): unknown;
|
|
266
|
-
handle(args: {
|
|
268
|
+
handle(args: {
|
|
269
|
+
readonly input: unknown;
|
|
270
|
+
readonly ctx: Ctx;
|
|
271
|
+
decide(rows: readonly unknown[]): ActionDecision<unknown>;
|
|
272
|
+
}): unknown;
|
|
267
273
|
}
|
|
268
274
|
|
|
269
275
|
export interface AnyAction {
|
|
@@ -341,6 +347,23 @@ export type ActionFacade<TInput extends StandardSchemaV1, TOutput extends Standa
|
|
|
341
347
|
'input' | 'output' | 'policy' | 'mcp' | 'as' | 'openapi' | 'client' | 'job' | 'contract'
|
|
342
348
|
>;
|
|
343
349
|
|
|
350
|
+
// The `Deciding*` pair FIRST: tsc fixes a handler's parameter types on the first overload it
|
|
351
|
+
// tries, so a `({ decide })` handler must meet them before the `ActionDef` pair. That pair resolves
|
|
352
|
+
// nothing an earlier one does not — every `ActionDef` is a `DecidingActionDef` — and stays only
|
|
353
|
+
// because the 31.x public-API lock records `action` by these two signatures (`scripts/public-api.ts`).
|
|
354
|
+
export function action<
|
|
355
|
+
TInput extends StandardSchemaV1,
|
|
356
|
+
TOutput extends StandardSchemaV1,
|
|
357
|
+
TChunk extends StandardSchemaV1 = StandardSchemaV1,
|
|
358
|
+
TRow = unknown,
|
|
359
|
+
>(
|
|
360
|
+
def: DecidingStreamActionDef<TInput, TOutput, TChunk, TRow>,
|
|
361
|
+
): StreamAction<TInput, TOutput, TChunk>;
|
|
362
|
+
export function action<
|
|
363
|
+
TInput extends StandardSchemaV1,
|
|
364
|
+
TOutput extends StandardSchemaV1,
|
|
365
|
+
TRow = unknown,
|
|
366
|
+
>(def: DecidingActionDef<TInput, TOutput, TRow>): Action<TInput, TOutput>;
|
|
344
367
|
export function action<
|
|
345
368
|
TInput extends StandardSchemaV1,
|
|
346
369
|
TOutput extends StandardSchemaV1,
|
|
@@ -356,7 +379,7 @@ export function action<
|
|
|
356
379
|
TInput extends StandardSchemaV1,
|
|
357
380
|
TOutput extends StandardSchemaV1,
|
|
358
381
|
TRow = unknown,
|
|
359
|
-
>(def:
|
|
382
|
+
>(def: DecidingActionDef<TInput, TOutput, TRow>): Action<TInput, TOutput> {
|
|
360
383
|
// Refused where it is written, and frozen before `build` stores it: the list a caller still
|
|
361
384
|
// holds is not the one the descriptor, the document and the wrap read.
|
|
362
385
|
const errors = checkDeclaredErrors(def.errors, declaredStatusFor, 'this action');
|
package/src/index.ts
CHANGED
|
@@ -42,6 +42,12 @@ export type {
|
|
|
42
42
|
export { action, describeAction, isAction } from './action';
|
|
43
43
|
export type { ActionAnswer, ActionAnswerIssue } from './action-answer';
|
|
44
44
|
export { ACTION_ANSWER_COOKIE, useActionAnswer } from './action-answer';
|
|
45
|
+
export type { ActionDecide, ActionDecision, ActionRowDenial } from './action-decide';
|
|
46
|
+
export type {
|
|
47
|
+
ActionDecideArgs,
|
|
48
|
+
DecidingActionDef,
|
|
49
|
+
DecidingStreamActionDef,
|
|
50
|
+
} from './action-decide-def';
|
|
45
51
|
export { chunksRead } from './action-stream';
|
|
46
52
|
/**
|
|
47
53
|
* The app's API as a whole — `defineApi({ http: { pathStyle, mounts }, openapi })` — read back by
|
package/src/invoke.ts
CHANGED
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
import { statusFor } from '@ultimat3/http';
|
|
32
32
|
import { maskSensitive } from '@ultimat3/schema';
|
|
33
33
|
import type { AnyAction, AnyActionDef, InvokeOptions } from './action';
|
|
34
|
+
import { decideFor } from './action-decide';
|
|
34
35
|
import { declarationOf, drainsItsHandler } from './action-stream';
|
|
35
36
|
import type { AuditRecord } from './audit';
|
|
36
37
|
import {
|
|
@@ -365,6 +366,10 @@ async function perform(
|
|
|
365
366
|
// already decided, per call, by `guardActionBeforeInput` above: a revoked `can()` is refused
|
|
366
367
|
// here too. In flight or absent, nothing has been decided yet, so the rule decides.
|
|
367
368
|
const answered = def.row !== undefined && peeked !== undefined && peeked.status !== 'in-flight';
|
|
369
|
+
// The handler's `decide(rows)`: this declaration's policy, this caller, this parsed input, this
|
|
370
|
+
// surface — bound here so a handler can name none of them. Built after the parse, so `row()`
|
|
371
|
+
// and anything before the input stage never hold one.
|
|
372
|
+
const decide = decideFor(def.policy, { actor: actorOf(ctx), input, ctx }, surface);
|
|
368
373
|
if (!answered) await judgeRow();
|
|
369
374
|
|
|
370
375
|
// Output parsing sits inside `run` so a replayed idempotent response is the parsed value too —
|
|
@@ -378,7 +383,9 @@ async function perform(
|
|
|
378
383
|
// passing the outer `ctx.signal` onward would fail its vendor call and settle a failure.
|
|
379
384
|
const override = drainsItsHandler(def) ? undefined : handlerOverrideOf(def);
|
|
380
385
|
const handle = override ?? def.handle.bind(def);
|
|
381
|
-
const produced = await judgedHandler(def, name, () =>
|
|
386
|
+
const produced = await judgedHandler(def, name, () =>
|
|
387
|
+
handle({ input, ctx: useContext(), decide }),
|
|
388
|
+
);
|
|
382
389
|
return validateOutput(def.output, produced, name);
|
|
383
390
|
};
|
|
384
391
|
// The meter is drawn LAST before the handler, on every surface `surface: 'server'` included: a
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Single responsibility: say, after a successful action input parse, which keys the parse dropped
|
|
2
|
+
// because the input schema never declared them (#1281). The answer is unchanged — refusing is a
|
|
3
|
+
// break and waits for `next` — so in 31.x this is a coded `log.record` warning plus a span event.
|
|
4
|
+
// `@ultimat3/query`'s `validate-unknown-keys.ts` is the read twin (query cannot import action).
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
currentSpan,
|
|
8
|
+
logger,
|
|
9
|
+
nearestName,
|
|
10
|
+
renderCauseValue,
|
|
11
|
+
renderFixShellArg,
|
|
12
|
+
} from '@ultimat3/core';
|
|
13
|
+
import type { StandardSchemaV1 } from '@ultimat3/schema';
|
|
14
|
+
import { tryIntrospect, unknownKeys } from '@ultimat3/schema';
|
|
15
|
+
|
|
16
|
+
/** The log message and span-event name: a subscriber matches `log.record`'s `message` on it. */
|
|
17
|
+
export const INPUT_UNKNOWN_KEY = 'input.unknown_key';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The warning's code, on `fields.code`. A refusal under the same code is `next`'s, not 31.x's.
|
|
21
|
+
* Titled in `@ultimat3/query`'s server-only `errors.ts`: this package's titles ride in `rpc`.
|
|
22
|
+
*/
|
|
23
|
+
const CODE = 'X_INPUT_UNKNOWN_KEY';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Reports already logged. Bounded — the keys are the caller's, and a flood must not grow a set —
|
|
27
|
+
* by evicting the OLDEST (insertion order), never by refusing the newest: a cap that refused went
|
|
28
|
+
* silent for the life of the process after 1,000 junk keys, real typos included.
|
|
29
|
+
*/
|
|
30
|
+
export const MAX_REPORTS = 1_000;
|
|
31
|
+
const reported = new Set<string>();
|
|
32
|
+
|
|
33
|
+
/** A path is the caller's text: bounded before it reaches a log line or a span. */
|
|
34
|
+
const MAX_PATH = 128;
|
|
35
|
+
const bounded = (path: string): string =>
|
|
36
|
+
path.length <= MAX_PATH ? path : `${path.slice(0, MAX_PATH)}…`;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Called by `parseActionInput` with the RAW input, after it parsed. Silent for a clean input and a
|
|
40
|
+
* schema the active provider cannot describe. The span event fires on every call (the dev
|
|
41
|
+
* timeline's request); the log line once per action and set of paths, so a client in the field
|
|
42
|
+
* sending one stale key every request is one line, not a line per request.
|
|
43
|
+
*/
|
|
44
|
+
export function reportUnknownInputKeys(
|
|
45
|
+
schema: StandardSchemaV1,
|
|
46
|
+
raw: unknown,
|
|
47
|
+
actionName: string,
|
|
48
|
+
): void {
|
|
49
|
+
const node = tryIntrospect(schema);
|
|
50
|
+
if (node === undefined) return;
|
|
51
|
+
const found = unknownKeys(node, raw);
|
|
52
|
+
if (found.length === 0) return;
|
|
53
|
+
const keys = found.map(({ path, key, declared }) => {
|
|
54
|
+
// A key past `MAX_PATH` is no typo of a declared name, and `nearestName`'s cost grows with it.
|
|
55
|
+
const nearest = key.length > MAX_PATH ? undefined : nearestName(key, declared);
|
|
56
|
+
return nearest === undefined ? { path: bounded(path) } : { path: bounded(path), nearest };
|
|
57
|
+
});
|
|
58
|
+
const paths = keys.map((entry) => entry.path);
|
|
59
|
+
currentSpan()?.addEvent(INPUT_UNKNOWN_KEY, { code: CODE, action: actionName, keys: paths });
|
|
60
|
+
const report = JSON.stringify([actionName, paths]);
|
|
61
|
+
if (reported.has(report)) return;
|
|
62
|
+
if (reported.size >= MAX_REPORTS) {
|
|
63
|
+
const oldest = reported.values().next();
|
|
64
|
+
if (oldest.done !== true) reported.delete(oldest.value);
|
|
65
|
+
}
|
|
66
|
+
reported.add(report);
|
|
67
|
+
const first = keys[0];
|
|
68
|
+
const rename =
|
|
69
|
+
first?.nearest === undefined
|
|
70
|
+
? 'declare the field in the input schema, or stop sending it'
|
|
71
|
+
: `rename ${renderCauseValue(first.path)} to "${first.nearest}" in the caller, or declare it in the input schema`;
|
|
72
|
+
logger.warn(INPUT_UNKNOWN_KEY, {
|
|
73
|
+
code: CODE,
|
|
74
|
+
action: actionName,
|
|
75
|
+
keys,
|
|
76
|
+
cause: `input for action "${actionName}" carried ${paths.map(renderCauseValue).join(', ')}, which its input schema does not declare; the parse dropped ${paths.length === 1 ? 'it' : 'them'} and the call ran without`,
|
|
77
|
+
fix: `x actions show ${renderFixShellArg(actionName, '<action-name>')} --json # the declared input — ${rename}`,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** TEST SEAM: forget every logged report. */
|
|
82
|
+
export function resetUnknownKeyReports(): void {
|
|
83
|
+
reported.clear();
|
|
84
|
+
}
|
package/src/validate.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import type { InferOutput, StandardSchemaV1 } from '@ultimat3/schema';
|
|
8
8
|
import { formatIssues, toValidationIssues, validateAsync } from '@ultimat3/schema';
|
|
9
9
|
import { InputInvalidError, OutputInvalidError } from './errors';
|
|
10
|
+
import { reportUnknownInputKeys } from './validate-unknown-keys';
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* The refusal carries the issue list as well as the line, and the two are ONE value rendered twice:
|
|
@@ -36,6 +37,9 @@ export async function parseActionInput<S extends StandardSchemaV1>(
|
|
|
36
37
|
result.issuesTruncated === true,
|
|
37
38
|
);
|
|
38
39
|
}
|
|
40
|
+
// After the parse, never instead of it: the answer is the parse's, and the warning only says
|
|
41
|
+
// what it dropped (#1281). `raw`, because the parsed value no longer carries the key.
|
|
42
|
+
reportUnknownInputKeys(schema, raw, actionName);
|
|
39
43
|
return result.value;
|
|
40
44
|
}
|
|
41
45
|
|