@sdxc/kit 2026.10.24 → 2026.10.26

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.
Files changed (82) hide show
  1. package/README.md +28 -15
  2. package/changelog.jsonl +125 -4
  3. package/dist/architecture/graph.d.ts +16 -3
  4. package/dist/architecture/graph.js +37 -19
  5. package/dist/architecture/layers.d.ts +10 -3
  6. package/dist/architecture/layers.js +38 -9
  7. package/dist/commands/destroy.d.ts +1 -1
  8. package/dist/commands/make.d.ts +5 -5
  9. package/dist/commands/make.js +11 -8
  10. package/dist/commands/scaffold.d.ts +1 -1
  11. package/dist/contract.d.ts +3 -3
  12. package/dist/doctor/checks.d.ts +2 -2
  13. package/dist/doctor/checks.js +14 -2
  14. package/dist/doctor/constant-names.d.ts +17 -0
  15. package/dist/doctor/constant-names.js +106 -0
  16. package/dist/doctor/dependencies.d.ts +3 -3
  17. package/dist/doctor/dependencies.js +3 -3
  18. package/dist/doctor/fan-out.d.ts +3 -2
  19. package/dist/doctor/fan-out.js +4 -3
  20. package/dist/doctor/fetch-param.d.ts +15 -0
  21. package/dist/doctor/fetch-param.js +37 -0
  22. package/dist/doctor/files.d.ts +2 -0
  23. package/dist/doctor/files.js +4 -0
  24. package/dist/doctor/local-const.d.ts +15 -0
  25. package/dist/doctor/local-const.js +54 -0
  26. package/dist/doctor/logger.d.ts +15 -0
  27. package/dist/doctor/logger.js +41 -0
  28. package/dist/doctor/orphans.d.ts +3 -3
  29. package/dist/doctor/orphans.js +33 -7
  30. package/dist/doctor/process-env.d.ts +15 -0
  31. package/dist/doctor/process-env.js +39 -0
  32. package/dist/doctor/project.js +17 -13
  33. package/dist/doctor/result-throws.d.ts +5 -4
  34. package/dist/doctor/result-throws.js +6 -5
  35. package/dist/doctor/source-rule.d.ts +35 -0
  36. package/dist/doctor/source-rule.js +52 -0
  37. package/dist/doctor/stack-imports.d.ts +15 -0
  38. package/dist/doctor/stack-imports.js +60 -0
  39. package/dist/doctor/syntax.d.ts +31 -0
  40. package/dist/doctor/syntax.js +56 -0
  41. package/dist/make/binding.d.ts +13 -8
  42. package/dist/make/binding.js +96 -64
  43. package/dist/make/from-model.js +2 -7
  44. package/dist/make/index.js +2 -7
  45. package/dist/package-manager.d.ts +6 -0
  46. package/dist/package-manager.js +25 -2
  47. package/dist/scaffold/index.js +25 -19
  48. package/dist/ui/commands.js +2 -5
  49. package/dist/ui/registry.d.ts +2 -2
  50. package/dist/ui/registry.js +1 -1
  51. package/dist/upgrade/changelog.d.ts +2 -0
  52. package/dist/upgrade/changelog.js +2 -0
  53. package/dist/upgrade/project.d.ts +2 -2
  54. package/dist/upgrade/prompt.d.ts +3 -2
  55. package/dist/upgrade/prompt.js +43 -2
  56. package/package.json +5 -5
  57. package/registry/ui/youtube-embed.json +3 -3
  58. package/stubs/scaffold.test-mcp.stub +5 -1
  59. package/templates/app/AGENTS.md +3 -1
  60. package/templates/app/app/commands/db/fresh.ts +20 -13
  61. package/templates/app/app/commands/db/migrate.ts +42 -32
  62. package/templates/app/app/http/controllers/api/openapi.test.ts +29 -0
  63. package/templates/app/app/http/controllers/api/openapi.tsx +8 -2
  64. package/templates/app/app/http/controllers/health.test.ts +35 -0
  65. package/templates/app/app/http/controllers/health.ts +16 -0
  66. package/templates/app/app/http/controllers/mcp.test.ts +16 -0
  67. package/templates/app/app/http/middleware/access.ts +6 -3
  68. package/templates/app/app/http/middleware/page.ts +3 -4
  69. package/templates/app/app/http/middleware/server-timing.test.ts +1 -1
  70. package/templates/app/app/initializers/database.test.ts +6 -1
  71. package/templates/app/app/initializers/health.ts +17 -0
  72. package/templates/app/app/initializers/openapi.ts +5 -4
  73. package/templates/app/app/initializers/router.test.ts +1 -1
  74. package/templates/app/app/initializers/router.tsx +5 -6
  75. package/templates/app/app/test/cloudflare-workers.test.ts +27 -0
  76. package/templates/app/app/test/cloudflare-workers.ts +17 -4
  77. package/templates/app/app/test/database.ts +33 -0
  78. package/templates/app/app/test/mcp.ts +96 -0
  79. package/templates/app/package.json +1 -0
  80. package/templates/app/resources/layouts/document.tsx +2 -3
  81. package/templates/app/routes/api.ts +17 -7
  82. package/templates/app/app/http/controllers/api/health.tsx +0 -15
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `/mcp`, called through the app's router the way an MCP client calls it, with `callTool` from
3
+ * `app/test/mcp.ts`.
4
+ */
5
+
6
+ import { describe, expect, test } from "vitest";
7
+
8
+ import { callTool } from "~/app/test/mcp";
9
+
10
+ describe("mcp", () => {
11
+ test("answers a call to a tool the server does not offer with an error", async () => {
12
+ let call = await callTool("missing", {});
13
+
14
+ expect(call.isError).toBe(true);
15
+ });
16
+ });
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import { access } from "@sdxc/authz/middleware/router";
8
+ import * as s from "remix/data-schema";
8
9
  import { Session } from "remix/session";
9
10
 
10
11
  import type { Actor } from "~/app/policies/policy";
@@ -15,14 +16,16 @@ import policy from "~/app/policies/policy";
15
16
  /** The session key a sign-in stores the visitor under, and `signedIn` reads back. */
16
17
  export const ACTOR_KEY = "actor";
17
18
 
19
+ /** The shape a signed-in visitor is stored in; any other value reads as a guest. */
20
+ const STORED_ACTOR = s.object({ id: s.string() });
21
+
18
22
  /**
19
23
  * The signed-in visitor the session holds, or `undefined` for a guest. A request without a
20
24
  * session, or one whose stored value has no string `id`, reads as a guest.
21
25
  */
22
26
  export function signedIn(session: Session | undefined): Actor | undefined {
23
- let value: unknown = session?.get(ACTOR_KEY);
24
- if (typeof value !== "object" || value === null || !("id" in value)) return undefined;
25
- return typeof value.id === "string" ? { id: value.id } : undefined;
27
+ let stored = s.parseSafe(STORED_ACTOR, session?.get(ACTOR_KEY));
28
+ return stored.success ? { id: stored.value.id } : undefined;
26
29
  }
27
30
 
28
31
  /** The roles the session's visitor holds: `member` once signed in, none for a guest. */
@@ -1,8 +1,7 @@
1
1
  /**
2
- * The middleware every page declares: the parsed form body, the cross-origin check a
3
- * cookie-backed form relies on, the visitor's session, access and language, the honeypot every
4
- * form submission must pass, the security headers, and the document's assets and renderer. A
5
- * page controller passes it as its `middleware`, so only requests that render a document pay.
2
+ * The chain every page controller passes as its `middleware`: form body, cross-origin check,
3
+ * session, access, language, honeypot, security headers, and the document's assets and
4
+ * renderer, so only requests that render a document pay for them.
6
5
  */
7
6
 
8
7
  import { assets } from "@sdxc/assets-middleware";
@@ -8,7 +8,7 @@ import { createWebRouter } from "~/app/initializers/router";
8
8
 
9
9
  describe("serverTiming", () => {
10
10
  test("reports how long the request took", async () => {
11
- let response = await createWebRouter().fetch("https://example.com/api/health");
11
+ let response = await createWebRouter().fetch("https://example.com/health");
12
12
 
13
13
  expect(response.headers.get("server-timing")).toMatch(/^app(;dur=[\d.]+)?$/);
14
14
  });
@@ -1,11 +1,12 @@
1
1
  /**
2
2
  * The database the router and the job dispatcher publish as `ctx.db`, run against the
3
- * in-memory D1 stand-in tests receive as the `DB` binding.
3
+ * in-memory D1 stand-in tests receive as the `DB` binding, and the migration chain applied to it.
4
4
  */
5
5
 
6
6
  import { describe, expect, test } from "vitest";
7
7
 
8
8
  import { openDatabase } from "~/app/initializers/database";
9
+ import { migrate } from "~/app/test/database";
9
10
 
10
11
  describe("openDatabase", () => {
11
12
  test("runs SQL through the DB binding", async () => {
@@ -15,4 +16,8 @@ describe("openDatabase", () => {
15
16
 
16
17
  expect(result.rows).toEqual([{ answer: 42 }]);
17
18
  });
19
+
20
+ test("takes every migration in database/migrations/ on an empty binding", async () => {
21
+ await expect(migrate()).resolves.toBeUndefined();
22
+ });
18
23
  });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The health check `GET /health` answers: one probe per binding or service the app depends on,
3
+ * each read-only, so a monitor polling every minute changes nothing. Add a probe here with each
4
+ * binding the app starts reading, and `critical: false` for one it can serve without.
5
+ */
6
+
7
+ import { HealthCheck } from "@sdxc/health";
8
+ import { sql } from "remix/data-table";
9
+
10
+ import type { AppContext } from "~/app/initializers/router";
11
+
12
+ import manifest from "../../package.json" with { type: "json" };
13
+
14
+ /** The report every request to `/health` runs, named after the app. */
15
+ export const health = new HealthCheck<AppContext>({ serviceId: manifest.name });
16
+
17
+ health.probe("database", (ctx) => ctx.db.exec(sql`SELECT 1`), { componentType: "datastore" });
@@ -5,17 +5,18 @@
5
5
 
6
6
  import { createDocument } from "@sdxc/openapi";
7
7
 
8
+ import { SITE_URL } from "~/config/constants";
8
9
  import problems from "~/config/problems";
9
10
  import { HEALTH } from "~/routes/api";
10
11
 
11
12
  /**
12
- * Starts the document for the origin the API is served from, which OpenAPI requires as an
13
- * absolute server URL; `build()` assembles it, failing on a contract it cannot express.
13
+ * Starts the document with `SITE_URL`, the origin the app is published at, as its server, since
14
+ * OpenAPI requires an absolute one; `build()` assembles it, failing on a contract it cannot express.
14
15
  */
15
- export function createApiDocument(origin: string) {
16
+ export function createApiDocument() {
16
17
  return createDocument({
17
18
  info: { title: "App API", version: "1.0.0" },
18
- servers: [{ url: origin }],
19
+ servers: [{ url: SITE_URL }],
19
20
  problems,
20
21
  }).add(HEALTH);
21
22
  }
@@ -23,7 +23,7 @@ async function record(request: Request) {
23
23
  describe("router", () => {
24
24
  test("continues the caller's W3C trace", async () => {
25
25
  let written = await record(
26
- new Request("https://example.com/api/health", {
26
+ new Request("https://example.com/health", {
27
27
  headers: { traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" },
28
28
  }),
29
29
  );
@@ -49,10 +49,9 @@ declare module "remix/router" {
49
49
  }
50
50
 
51
51
  /**
52
- * Builds the router the Worker forwards each request to. Its middleware is what pages and
53
- * endpoints both need, written inline so {@link AppContext} reads what each one publishes; a
54
- * controller adds its contract's chain, `app/http/middleware/page.ts` or `api.ts`, itself.
55
- * While developing, `/_emails` previews every mapped email in every supported language.
52
+ * Builds the router the Worker forwards each request to, with the middleware pages and
53
+ * endpoints share written inline so {@link AppContext} reads what each publishes; a controller
54
+ * adds `page.ts` or `api.ts` itself. While developing, `/_emails` previews every mapped email.
56
55
  */
57
56
  export function createWebRouter() {
58
57
  let router = createRouter({
@@ -101,8 +100,8 @@ export function createWebRouter() {
101
100
  );
102
101
 
103
102
  router.map(
104
- apiRoutes.api.health,
105
- lazy(() => import("~/app/http/controllers/api/health")),
103
+ apiRoutes.health,
104
+ lazy(() => import("~/app/http/controllers/health")),
106
105
  );
107
106
  router.map(
108
107
  apiRoutes.api.openapi,
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The `cloudflare:workers` stand-in Vitest resolves: a Durable Object class extends its
3
+ * `DurableObject` and runs against a mock state, as the Worker runtime would construct it.
4
+ */
5
+
6
+ import { createDurableObjectState } from "@sdxc/cloudflare-mocks";
7
+ import { DurableObject, env } from "cloudflare:workers";
8
+ import { describe, expect, test } from "vitest";
9
+
10
+ /** A counter in the shape `sdxc make do` writes, with storage of its own. */
11
+ class CounterDO extends DurableObject<Cloudflare.Env> {
12
+ /** Counts one more and answers the total. */
13
+ async increment(): Promise<number> {
14
+ let count = ((await this.ctx.storage.get<number>("count")) ?? 0) + 1;
15
+ await this.ctx.storage.put("count", count);
16
+ return count;
17
+ }
18
+ }
19
+
20
+ describe("DurableObject", () => {
21
+ test("lets a class extending it run against a mock state", async () => {
22
+ let counter = new CounterDO(createDurableObjectState(), env);
23
+
24
+ expect(await counter.increment()).toBe(1);
25
+ expect(await counter.increment()).toBe(2);
26
+ });
27
+ });
@@ -1,8 +1,7 @@
1
1
  /**
2
- * The `cloudflare:workers` module under Vitest, which imports the app's modules in Node, outside
3
- * the Worker runtime. It carries the development secrets `.env.example` lists, so a module that
4
- * reads `env` at import loads the same way it does under `bun dev`. Its bindings are in-memory
5
- * stand-ins that behave like Cloudflare's, so a test reads and writes real SQL through `DB`.
2
+ * The `cloudflare:workers` module under Vitest, which runs the app's modules in Node: the
3
+ * development secrets `.env.example` lists, so a module reading `env` at import loads as under
4
+ * `bun dev`, and in-memory bindings that behave like Cloudflare's, so `DB` runs real SQL.
6
5
  */
7
6
 
8
7
  import {
@@ -25,3 +24,17 @@ export const env = {
25
24
  TURNSTILE_SITE_KEY: "1x00000000000000000000AA",
26
25
  TURNSTILE_SECRET_KEY: "1x0000000000000000000000000000000AA",
27
26
  } as Partial<Cloudflare.Env> as Cloudflare.Env;
27
+
28
+ /**
29
+ * The base class a Durable Object extends, holding the state and `env` it is constructed with,
30
+ * so a test builds one with `createDurableObjectState()` from `@sdxc/cloudflare-mocks`.
31
+ */
32
+ export class DurableObject<Env = Cloudflare.Env> {
33
+ ctx: DurableObjectState;
34
+ env: Env;
35
+
36
+ constructor(ctx: DurableObjectState, env: Env) {
37
+ this.ctx = ctx;
38
+ this.env = env;
39
+ }
40
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The database a test reads and writes: the in-memory `DB` binding with every migration in
3
+ * `database/migrations/` applied in name order, as `sdxc run db migrate` applies them, and
4
+ * factories that write through the app's own models.
5
+ *
6
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/test-data}
7
+ */
8
+
9
+ import { createFactories } from "@sdxc/data-model/testing";
10
+ import { env } from "cloudflare:workers";
11
+
12
+ import { openDatabase } from "~/app/initializers/database";
13
+ import registry from "~/app/models";
14
+
15
+ /** Every migration's SQL by path, bundled by Vite, so a test reads them without the file system. */
16
+ const MIGRATIONS = import.meta.glob<string>("/database/migrations/*.sql", {
17
+ query: "?raw",
18
+ import: "default",
19
+ eager: true,
20
+ });
21
+
22
+ /**
23
+ * Applies every migration to the `DB` binding. Each test file starts with an empty binding, so a
24
+ * file runs this once, in `beforeAll`.
25
+ */
26
+ export async function migrate(): Promise<void> {
27
+ for (let path of Object.keys(MIGRATIONS).sort()) await env.DB.exec(MIGRATIONS[path] ?? "");
28
+ }
29
+
30
+ /** Factories writing through the app's models over the `DB` binding, with values that repeat run to run. */
31
+ export function factories() {
32
+ return createFactories(registry.bind({ db: openDatabase() }), { seed: 1 });
33
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Calls the app's MCP tools the way a client does: a JSON-RPC `tools/call` posted to `/mcp`. A
3
+ * guest's call goes through the app's router, running every middleware a real call runs; a
4
+ * member's goes through a router binding that principal, which the API grants nobody yet.
5
+ *
6
+ * @see {@link https://sdxc.dev/docs/operations-and-testing/mcp-server}
7
+ */
8
+
9
+ import { access } from "@sdxc/authz/middleware/router";
10
+ import { models } from "@sdxc/data-model/router";
11
+ import { database } from "@sdxc/database-middleware";
12
+ import { LATEST_PROTOCOL_VERSION, MetaKey } from "@sdxc/mcp";
13
+ import { createRouter } from "remix/router";
14
+
15
+ import { openDatabase } from "~/app/initializers/database";
16
+ import mcp from "~/app/initializers/mcp";
17
+ import { createWebRouter } from "~/app/initializers/router";
18
+ import registry from "~/app/models";
19
+ import abilities from "~/app/policies/abilities";
20
+ import policy from "~/app/policies/policy";
21
+ import routes from "~/routes/api";
22
+
23
+ /** Who a call is made as: a guest, as the API binds every caller, or a signed-in member. */
24
+ export type Caller = "guest" | "member";
25
+
26
+ /** What a call answered: its text, and whether the tool, or the protocol, reported a failure. */
27
+ export interface ToolCall {
28
+ text: string;
29
+ isError: boolean;
30
+ }
31
+
32
+ /** The JSON-RPC response to a `tools/call`. */
33
+ interface CallResponse {
34
+ result?: { content?: Array<{ text?: string }>; isError?: boolean };
35
+ error?: { message?: string };
36
+ }
37
+
38
+ /**
39
+ * A router answering `/mcp` over the app's database and models as a member, the principal the
40
+ * API binds once it authenticates its callers.
41
+ */
42
+ function createMemberRouter() {
43
+ let router = createRouter({
44
+ middleware: [
45
+ database(openDatabase()),
46
+ models(registry, (ctx) => ({ db: ctx.db })),
47
+ access(policy, { roles: ["member"], facts: { actor: { id: "1" } }, load: [abilities] }),
48
+ ],
49
+ });
50
+ router.map(routes.mcp, (ctx) => mcp.fetch(ctx));
51
+ return router;
52
+ }
53
+
54
+ /**
55
+ * Calls the tool `name` with `args` as `caller`. A protocol error, such as arguments the tool's
56
+ * schema refuses or a forbidden call, answers its message as a failure.
57
+ */
58
+ export async function callTool(
59
+ name: string,
60
+ args: Record<string, unknown>,
61
+ caller: Caller = "guest",
62
+ ): Promise<ToolCall> {
63
+ let router = caller === "member" ? createMemberRouter() : createWebRouter();
64
+ let response = await router.fetch(
65
+ new Request(new URL(routes.mcp.href(), "https://example.com"), {
66
+ method: "POST",
67
+ headers: {
68
+ "Content-Type": "application/json",
69
+ "MCP-Protocol-Version": LATEST_PROTOCOL_VERSION,
70
+ "Mcp-Method": "tools/call",
71
+ "Mcp-Name": name,
72
+ },
73
+ body: JSON.stringify({
74
+ jsonrpc: "2.0",
75
+ id: 1,
76
+ method: "tools/call",
77
+ params: {
78
+ name,
79
+ arguments: args,
80
+ _meta: {
81
+ [MetaKey.ProtocolVersion]: LATEST_PROTOCOL_VERSION,
82
+ [MetaKey.ClientInfo]: { name: "test", version: "1.0.0" },
83
+ [MetaKey.ClientCapabilities]: {},
84
+ },
85
+ },
86
+ }),
87
+ }),
88
+ );
89
+ return readCall((await response.json()) as CallResponse);
90
+ }
91
+
92
+ /** What a `tools/call` response says, with a protocol error read as the call's failure. */
93
+ function readCall(body: CallResponse): ToolCall {
94
+ if (body.error !== undefined) return { text: body.error.message ?? "", isError: true };
95
+ return { text: body.result?.content?.[0]?.text ?? "", isError: body.result?.isError === true };
96
+ }
@@ -21,6 +21,7 @@
21
21
  "@sdxc/data-table-d1": "0.0.1",
22
22
  "@sdxc/database-middleware": "0.0.1",
23
23
  "@sdxc/get-client-ip": "0.0.1",
24
+ "@sdxc/health": "0.0.1",
24
25
  "@sdxc/honeypot": "0.0.1",
25
26
  "@sdxc/http": "0.0.1",
26
27
  "@sdxc/i18n": "0.0.1",
@@ -1,8 +1,7 @@
1
1
  /**
2
2
  * The HTML document every page renders inside: the head tags, the page's title, description,
3
- * canonical link and link-preview tags, and the
4
- * stylesheets imported below in cascade order. The client entry is linked only by a page
5
- * that carries an island.
3
+ * canonical link and link-preview tags, and the stylesheets imported below in cascade order.
4
+ * The client entry is linked only by a page that carries an island.
6
5
  */
7
6
 
8
7
  import type { DocumentAssets } from "@sdxc/assets-middleware";
@@ -4,14 +4,17 @@
4
4
  * request read the same schemas.
5
5
  */
6
6
 
7
- import * as s from "@sdxc/json-schema";
7
+ import HEALTH_REPORT from "@sdxc/health/schema";
8
8
  import { defineOperation } from "@sdxc/openapi";
9
9
  import { get, post, route } from "remix/routes";
10
10
 
11
- /** The JSON endpoints under `/api`, and the MCP endpoint an agent connects to. */
11
+ /**
12
+ * The health check at `/health`, the path monitors look for first, the JSON endpoints under
13
+ * `/api`, and the MCP endpoint an agent connects to.
14
+ */
12
15
  const ROUTES = route({
16
+ health: get("/health"),
13
17
  api: {
14
- health: get("/api/health"),
15
18
  openapi: get("/api/openapi.json"),
16
19
  notFound: "/api/*path",
17
20
  },
@@ -20,10 +23,17 @@ const ROUTES = route({
20
23
 
21
24
  export default ROUTES;
22
25
 
23
- /** GET /api/health, the check a monitor or load balancer polls. */
24
- export const HEALTH = defineOperation("health", ROUTES.api.health, {
25
- summary: "Check the service",
26
+ /** GET /health, the check a monitor or load balancer polls, answered as `application/health+json`. */
27
+ export const HEALTH = defineOperation("health", ROUTES.health, {
28
+ summary: "Check the service and every dependency it reads",
26
29
  responses: {
27
- 200: { description: "The service is up", body: s.object({ status: s.literal("ok") }) },
30
+ 200: {
31
+ description: "Every critical dependency answers: the report's status is `pass` or `warn`",
32
+ body: { "application/health+json": HEALTH_REPORT },
33
+ },
34
+ 503: {
35
+ description: "A critical dependency fails: the report's status is `fail`",
36
+ body: { "application/health+json": HEALTH_REPORT },
37
+ },
28
38
  },
29
39
  });
@@ -1,15 +0,0 @@
1
- /**
2
- * The health check: it answers as soon as the Worker runs, so a monitor tells a deploy that
3
- * serves from one that fails to start.
4
- */
5
-
6
- import { createAction } from "remix/router";
7
-
8
- import api from "~/app/http/middleware/api";
9
- import routes from "~/routes/api";
10
-
11
- export default createAction(routes.api.health, {
12
- middleware: api,
13
- /** GET /api/health, answered as the `health` operation documents it. */
14
- handler: () => Response.json({ status: "ok" }),
15
- });