@cosmicdrift/kumiko-framework 0.208.0 → 0.208.2
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/package.json +3 -3
- package/src/db/__tests__/event-store-executor-list.integration.test.ts +75 -0
- package/src/db/event-store-executor-read.ts +2 -2
- package/src/engine/define-handler.ts +66 -0
- package/src/engine/entity-handlers.ts +7 -0
- package/src/engine/feature-entity-handlers.ts +9 -4
- package/src/engine/index.ts +7 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-framework",
|
|
3
|
-
"version": "0.208.
|
|
3
|
+
"version": "0.208.2",
|
|
4
4
|
"description": "Framework core — engine, pipeline, API, DB, and every other bit that makes Kumiko go.",
|
|
5
5
|
"license": "BUSL-1.1",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -190,7 +190,7 @@
|
|
|
190
190
|
"./package.json": "./package.json"
|
|
191
191
|
},
|
|
192
192
|
"dependencies": {
|
|
193
|
-
"@cosmicdrift/kumiko-types": "0.208.
|
|
193
|
+
"@cosmicdrift/kumiko-types": "0.208.2",
|
|
194
194
|
"bullmq": "^5.76.7",
|
|
195
195
|
"bun-types": "^1.3.13",
|
|
196
196
|
"hono": "^4.13.1",
|
|
@@ -206,7 +206,7 @@
|
|
|
206
206
|
"zod": "^4.4.3"
|
|
207
207
|
},
|
|
208
208
|
"devDependencies": {
|
|
209
|
-
"@cosmicdrift/kumiko-dispatcher-live": "0.208.
|
|
209
|
+
"@cosmicdrift/kumiko-dispatcher-live": "0.208.2",
|
|
210
210
|
"bun-types": "^1.3.13",
|
|
211
211
|
"pino-pretty": "^13.1.3"
|
|
212
212
|
},
|
|
@@ -126,6 +126,81 @@ describe("event-store-executor.list — offset + totalCount (Tier 2.6d)", () =>
|
|
|
126
126
|
});
|
|
127
127
|
});
|
|
128
128
|
|
|
129
|
+
describe("event-store-executor.list — stable order (#2198)", () => {
|
|
130
|
+
const exec = createEventStoreExecutor(table, entity, { entityName: "pagerItem" });
|
|
131
|
+
|
|
132
|
+
test("offset paging mit identischem sort-Wert: keine Row auf beiden Seiten, Union = alle Rows", async () => {
|
|
133
|
+
// All rows share the same rank value. Postgres picks a Top-N heapsort
|
|
134
|
+
// for LIMIT queries, which is not stable across ties — and different
|
|
135
|
+
// offsets sort differently-sized heaps (LIMIT 3 OFFSET 0 sorts the
|
|
136
|
+
// top 3, LIMIT 3 OFFSET 3 sorts the top 6), so without an id
|
|
137
|
+
// tie-breaker the same row can surface on multiple pages while
|
|
138
|
+
// another is skipped entirely. Needs enough rows + small enough
|
|
139
|
+
// pages to make the heap sizes diverge; 10 rows / page 5 (the
|
|
140
|
+
// previous setup) was too forgiving and stayed green without the fix.
|
|
141
|
+
const total = 25;
|
|
142
|
+
const pageSize = 3;
|
|
143
|
+
for (let i = 0; i < total; i++) {
|
|
144
|
+
await exec.create({ title: `item-${String(i).padStart(3, "0")}`, rank: 1 }, admin, tdb);
|
|
145
|
+
}
|
|
146
|
+
const page1 = await exec.list(
|
|
147
|
+
{ limit: pageSize, offset: 0, sort: "rank", sortDirection: "asc" },
|
|
148
|
+
admin,
|
|
149
|
+
tdb,
|
|
150
|
+
);
|
|
151
|
+
const page2 = await exec.list(
|
|
152
|
+
{ limit: pageSize, offset: pageSize, sort: "rank", sortDirection: "asc" },
|
|
153
|
+
admin,
|
|
154
|
+
tdb,
|
|
155
|
+
);
|
|
156
|
+
const ids1 = page1.rows.map((r) => r["id"]);
|
|
157
|
+
const ids2 = page2.rows.map((r) => r["id"]);
|
|
158
|
+
expect(ids1.filter((id) => ids2.includes(id))).toHaveLength(0);
|
|
159
|
+
|
|
160
|
+
const allIds = new Set<unknown>();
|
|
161
|
+
for (let offset = 0; offset < total; offset += pageSize) {
|
|
162
|
+
const page = await exec.list(
|
|
163
|
+
{ limit: pageSize, offset, sort: "rank", sortDirection: "asc" },
|
|
164
|
+
admin,
|
|
165
|
+
tdb,
|
|
166
|
+
);
|
|
167
|
+
for (const id of page.rows.map((r) => r["id"])) allIds.add(id);
|
|
168
|
+
}
|
|
169
|
+
expect(allIds.size).toBe(total);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("cursor paging ohne sort: jede Row genau einmal", async () => {
|
|
173
|
+
// Plain sequential inserts keep heap physical order == uuidv7 id order,
|
|
174
|
+
// so a Seq Scan without ORDER BY happens to come out sorted anyway and
|
|
175
|
+
// the missing-ORDER-BY bug stays invisible. Delete half the rows and
|
|
176
|
+
// VACUUM to free their heap space, then insert more rows so their
|
|
177
|
+
// (higher) uuidv7 ids get placed into the reused (earlier) pages —
|
|
178
|
+
// now physical scan order diverges from id order.
|
|
179
|
+
const client = asRawClient(testDb.db);
|
|
180
|
+
for (let i = 0; i < 100; i++) {
|
|
181
|
+
await exec.create({ title: `item-${String(i).padStart(3, "0")}`, rank: i }, admin, tdb);
|
|
182
|
+
}
|
|
183
|
+
await client.unsafe(`DELETE FROM read_pager_items WHERE rank::int % 2 = 1`);
|
|
184
|
+
await client.unsafe(`VACUUM read_pager_items`);
|
|
185
|
+
for (let i = 100; i < 150; i++) {
|
|
186
|
+
await exec.create({ title: `item-${String(i).padStart(3, "0")}`, rank: i }, admin, tdb);
|
|
187
|
+
}
|
|
188
|
+
const countRow = await client.unsafe(`SELECT count(*)::int AS c FROM read_pager_items`);
|
|
189
|
+
const total = (countRow as unknown as Array<{ c: number }>)[0]?.c ?? 0;
|
|
190
|
+
|
|
191
|
+
const seenIds: string[] = [];
|
|
192
|
+
let cursor: string | undefined;
|
|
193
|
+
for (let page = 0; page < total + 5; page++) {
|
|
194
|
+
const res = await exec.list({ limit: 3, cursor }, admin, tdb);
|
|
195
|
+
seenIds.push(...res.rows.map((r) => r["id"] as string));
|
|
196
|
+
if (res.nextCursor === null) break;
|
|
197
|
+
cursor = res.nextCursor;
|
|
198
|
+
}
|
|
199
|
+
expect(seenIds).toHaveLength(total);
|
|
200
|
+
expect(new Set(seenIds).size).toBe(total);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
129
204
|
describe("event-store-executor.list — filter (Tier 2.7c)", () => {
|
|
130
205
|
const exec = createEventStoreExecutor(table, entity, { entityName: "pagerItem" });
|
|
131
206
|
|
|
@@ -200,8 +200,8 @@ export function createReadVerbs(ctx: ExecutorContext): Pick<EventStoreExecutor,
|
|
|
200
200
|
|
|
201
201
|
const orderByClause =
|
|
202
202
|
payload.sort && table[payload.sort]
|
|
203
|
-
? ` ORDER BY ${colSql(payload.sort)} ${payload.sortDirection === "desc" ? "DESC" : "ASC"}`
|
|
204
|
-
: ""
|
|
203
|
+
? ` ORDER BY ${colSql(payload.sort)} ${payload.sortDirection === "desc" ? "DESC" : "ASC"}, ${colSql("id")} ASC`
|
|
204
|
+
: ` ORDER BY ${colSql("id")} ASC`;
|
|
205
205
|
const useOffset = !payload.cursor && offset > 0;
|
|
206
206
|
const offsetClause = useOffset ? ` OFFSET ${offset}` : "";
|
|
207
207
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CursorResult } from "@cosmicdrift/kumiko-types/cursor-types";
|
|
1
2
|
import type { ZodType, z } from "zod";
|
|
2
3
|
import type { ContainsSecret } from "../secrets/types";
|
|
3
4
|
import { runPipeline } from "./run-pipeline";
|
|
@@ -116,3 +117,68 @@ export function defineQueryHandler<
|
|
|
116
117
|
): QueryHandlerDefinition<TName, TSchema, TResult, TMap> {
|
|
117
118
|
return def;
|
|
118
119
|
}
|
|
120
|
+
|
|
121
|
+
// Runtime marker set only by definePagedQueryHandler. A plain
|
|
122
|
+
// defineQueryHandler-built object never carries it. Kept as a type-level
|
|
123
|
+
// signal (fw#2216) even though no validator gates on it — QueryHandlerDef
|
|
124
|
+
// has no output schema, so a boot check can't distinguish "returns
|
|
125
|
+
// PagedRows" from "doesn't" without running the handler; the actual
|
|
126
|
+
// enforcement is a runtime shape guard in the renderer (kumiko-screen.tsx).
|
|
127
|
+
//
|
|
128
|
+
// A string key, not a Symbol: bundled-features imports this module through
|
|
129
|
+
// the package's "@cosmicdrift/kumiko-framework/engine" subpath (symlinked
|
|
130
|
+
// node_modules entry) via a different resolution route than a same-package
|
|
131
|
+
// relative import — two routes to the same file that can end up as two
|
|
132
|
+
// separate module instances under bundler symlink handling. A Symbol()
|
|
133
|
+
// evaluated twice would produce two unequal brands; a string literal doesn't
|
|
134
|
+
// have that failure mode.
|
|
135
|
+
// Exported (not just the predicate) so feature-entity-handlers.ts's
|
|
136
|
+
// queryHandler() registrar — which rebuilds a fresh QueryHandlerDef from an
|
|
137
|
+
// explicit field whitelist rather than spreading `def` — can carry the
|
|
138
|
+
// brand through into the stored registry entry.
|
|
139
|
+
export const PAGED_QUERY_HANDLER_BRAND = "__kumikoPagedQueryHandler";
|
|
140
|
+
|
|
141
|
+
export function isPagedQueryHandler(def: object): boolean {
|
|
142
|
+
// @cast-boundary brand-probe — reading an internal marker key off an
|
|
143
|
+
// otherwise-typed handler definition; the property may legitimately be
|
|
144
|
+
// absent, which is exactly the case this function distinguishes.
|
|
145
|
+
return (def as Record<string, unknown>)[PAGED_QUERY_HANDLER_BRAND] === true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export type PagedQueryHandlerDefinition<
|
|
149
|
+
TName extends string = string,
|
|
150
|
+
TSchema extends ZodType = ZodType,
|
|
151
|
+
TRow = unknown,
|
|
152
|
+
TMap extends object = KumikoEventTypeMap,
|
|
153
|
+
> = QueryHandlerDefinition<TName, TSchema, CursorResult<TRow>, TMap> & {
|
|
154
|
+
readonly [PAGED_QUERY_HANDLER_BRAND]: true;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// A projectionList screen's query must resolve to { rows, nextCursor, total? }
|
|
158
|
+
// (CursorResult<T>) — the renderer used to read rowsQuery.data?.rows and
|
|
159
|
+
// silently show an empty list otherwise (fw#2216: session-list); the
|
|
160
|
+
// renderer now guards against a malformed shape at runtime instead. Use this
|
|
161
|
+
// instead of defineQueryHandler for any query wired to a projectionList
|
|
162
|
+
// screen's `query` so the contract is documented at the definition site.
|
|
163
|
+
//
|
|
164
|
+
// Deliberately does NOT merge cursor/limit/sort/search into the input
|
|
165
|
+
// schema — that would change sortable/paginated derivation for existing
|
|
166
|
+
// screens (a behavior change, not a wrapper change). Callers that want
|
|
167
|
+
// those params add them to `schema` themselves.
|
|
168
|
+
export function definePagedQueryHandler<
|
|
169
|
+
const TName extends string,
|
|
170
|
+
TSchema extends ZodType,
|
|
171
|
+
TRow = unknown,
|
|
172
|
+
TMap extends object = KumikoEventTypeMap,
|
|
173
|
+
>(
|
|
174
|
+
def: QueryHandlerDefinition<TName, TSchema, CursorResult<TRow>, TMap>,
|
|
175
|
+
// R6: phantom rest-param — see defineWriteHandler. CursorResult<TRow> wraps
|
|
176
|
+
// TRow in `rows`, so ContainsSecret recurses through the array element.
|
|
177
|
+
..._noSecretInResponse: true extends ContainsSecret<CursorResult<TRow>>
|
|
178
|
+
? [
|
|
179
|
+
secretLeak: "A handler response must not contain a Secret<> — call .reveal() and return the plaintext, or drop the field.",
|
|
180
|
+
]
|
|
181
|
+
: []
|
|
182
|
+
): PagedQueryHandlerDefinition<TName, TSchema, TRow, TMap> {
|
|
183
|
+
return { ...def, [PAGED_QUERY_HANDLER_BRAND]: true };
|
|
184
|
+
}
|
|
@@ -9,6 +9,7 @@ import { createEventStoreExecutor, type EventStoreExecutor } from "../db/event-s
|
|
|
9
9
|
import { buildEntityTable, type EntityTable } from "../db/table-builder";
|
|
10
10
|
import { createTenantDb, type TenantDb } from "../db/tenant-db";
|
|
11
11
|
import { assertUnreachable } from "../utils";
|
|
12
|
+
import { PAGED_QUERY_HANDLER_BRAND } from "./define-handler";
|
|
12
13
|
import { buildInsertSchema, buildUpdateSchema } from "./schema-builder";
|
|
13
14
|
import type {
|
|
14
15
|
AccessRule,
|
|
@@ -366,6 +367,12 @@ export function defineEntityQueryHandler(
|
|
|
366
367
|
schema,
|
|
367
368
|
handler,
|
|
368
369
|
...(options?.access && { access: options.access }),
|
|
370
|
+
// The "list" verb's executor.list() always returns { rows, nextCursor,
|
|
371
|
+
// total? } (see the handler body above) — brand it so the definition
|
|
372
|
+
// site documents the PagedRows contract without needing
|
|
373
|
+
// definePagedQueryHandler on top. The "detail" verb returns a single
|
|
374
|
+
// row/null, never this shape.
|
|
375
|
+
...(verb === "list" && { [PAGED_QUERY_HANDLER_BRAND]: true }),
|
|
369
376
|
};
|
|
370
377
|
}
|
|
371
378
|
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { ZodType, z } from "zod";
|
|
2
2
|
import { toTableName } from "../db/table-builder";
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import {
|
|
4
|
+
isPagedQueryHandler,
|
|
5
|
+
PAGED_QUERY_HANDLER_BRAND,
|
|
6
|
+
type QueryHandlerDefinition,
|
|
7
|
+
type StreamHandlerDefinition,
|
|
8
|
+
type WriteHandlerDefinition,
|
|
7
9
|
} from "./define-handler";
|
|
8
10
|
import type { RegisterEntityCrudOptions } from "./entity-handlers";
|
|
9
11
|
import { registerEntityCrud } from "./entity-handlers";
|
|
@@ -153,6 +155,9 @@ export function buildEntityHandlerMethods<TName extends string>(
|
|
|
153
155
|
handler: def.handler as QueryHandlerFn, // @cast-boundary engine-bridge
|
|
154
156
|
...(def.access && { access: def.access }),
|
|
155
157
|
...(def.rateLimit && { rateLimit: def.rateLimit }),
|
|
158
|
+
// Carry the definePagedQueryHandler brand through — this rebuild
|
|
159
|
+
// drops any field not explicitly listed.
|
|
160
|
+
...(isPagedQueryHandler(def) && { [PAGED_QUERY_HANDLER_BRAND]: true }),
|
|
156
161
|
};
|
|
157
162
|
tryMapEntity(state, name, def.name);
|
|
158
163
|
return { name: def.name };
|
package/src/engine/index.ts
CHANGED
|
@@ -41,12 +41,18 @@ export { createApp } from "./create-app";
|
|
|
41
41
|
export { crossTenantOverrideDenied } from "./cross-tenant";
|
|
42
42
|
export { defineFeature } from "./define-feature";
|
|
43
43
|
export type {
|
|
44
|
+
PagedQueryHandlerDefinition,
|
|
44
45
|
QueryHandlerDefinition,
|
|
45
46
|
StreamHandlerDefinition,
|
|
46
47
|
WriteHandlerDefinition,
|
|
47
48
|
WriteHandlerInput,
|
|
48
49
|
} from "./define-handler";
|
|
49
|
-
export {
|
|
50
|
+
export {
|
|
51
|
+
definePagedQueryHandler,
|
|
52
|
+
defineQueryHandler,
|
|
53
|
+
defineWriteHandler,
|
|
54
|
+
isPagedQueryHandler,
|
|
55
|
+
} from "./define-handler";
|
|
50
56
|
export { defineRoles } from "./define-roles";
|
|
51
57
|
export { defineStep, getStep, listStepKinds } from "./define-step";
|
|
52
58
|
export type { WorkflowDefinition, WorkflowInput, WorkflowTrigger } from "./define-workflow";
|