@pplethai/cli 0.8.0 → 0.10.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.
Files changed (41) hide show
  1. package/dist/bin.js +126 -124
  2. package/package.json +1 -1
  3. package/templates/golden-path/.dev.vars.example +4 -3
  4. package/templates/golden-path/CLAUDE.md +10 -5
  5. package/templates/golden-path/README.md +5 -4
  6. package/templates/golden-path/package-lock.json +24 -8
  7. package/templates/golden-path/package.json +1 -1
  8. package/templates/golden-path/src/call-service.ts +17 -10
  9. package/templates/golden-path/src/dev-call-service.ts +21 -20
  10. package/templates/golden-path/src/dev-read-dataset.ts +23 -34
  11. package/templates/golden-path/src/dev-registry.ts +25 -7
  12. package/templates/golden-path/src/entry-name.ts +114 -0
  13. package/templates/golden-path/src/env.ts +2 -2
  14. package/templates/golden-path/src/read-dataset.ts +6 -4
  15. package/templates/golden-path/src/registry.ts +1 -1
  16. package/templates/golden-path/test/call-service.test.ts +118 -8
  17. package/templates/golden-path/test/dev-call-service.test.ts +201 -25
  18. package/templates/golden-path/test/dev-read-dataset.test.ts +52 -13
  19. package/templates/golden-path/test/dev-registry.test.ts +35 -6
  20. package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +8 -0
  21. package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +11 -0
  22. package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +7 -0
  23. package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +24 -0
  24. package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +1 -0
  25. package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +10 -0
  26. package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +7 -0
  27. package/templates/golden-path/test/fixtures/overlay/score/entry.md +10 -0
  28. package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +7 -0
  29. package/templates/golden-path/test/harness.ts +1 -1
  30. package/templates/golden-path/test/read-dataset.test.ts +45 -12
  31. package/templates/golden-path/vite.config.ts +0 -18
  32. package/templates/golden-path/test/fixtures/overlay/shop/catalog/entry.md +0 -8
  33. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/entry.md +0 -0
  34. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/audience.json +0 -0
  35. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/named.json +0 -0
  36. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/offices.json +0 -0
  37. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/roster.json +0 -0
  38. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/summary.json +0 -0
  39. /package/templates/golden-path/test/fixtures/overlay/{shop/catalog → catalog}/mocks/responses.json +0 -0
  40. /package/templates/golden-path/test/fixtures/overlay/{shop/orders → orders}/entry.md +0 -0
  41. /package/templates/golden-path/test/fixtures/overlay/{shop/orders → orders}/mocks/responses.json +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pplethai/cli",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,16 +14,17 @@
14
14
  # DEV_PERSONA="province-coordinator"
15
15
 
16
16
  # Make offline callService return a platform refusal instead of a mock.
17
- # A code applies to every entry. provider/name=code applies to one entry
17
+ # A code applies to every entry. name=code applies to one entry
18
18
  # and wins over the global code. Separate several with commas.
19
19
  # invalid_app_key, not_listed, not_granted, needs_beta, user_token_invalid,
20
20
  # identity_unavailable, provider_unavailable, provider_timeout.
21
21
  # An unknown code is ignored.
22
22
  # DEV_REGISTRY_ERROR="needs_beta"
23
- # DEV_REGISTRY_ERROR="not_granted,shop/orders=provider_timeout"
23
+ # DEV_REGISTRY_ERROR="not_granted,orders=provider_timeout"
24
24
 
25
25
  # Consumer slug reported on the emulated registry path. Defaults to
26
- # "dev-consumer". The entry id is dev-provider/<name>.
26
+ # "dev-consumer". `x-pple-entry` is the bare name;
27
+ # the dev Provider is dev-provider.
27
28
  # DEV_CONSUMER="brave-purple-lion"
28
29
 
29
30
  # Exercise the notification helper locally. These are normally injected by the
@@ -56,17 +56,22 @@ Where the token comes from:
56
56
  - A Service handler lives on `registry` in `src/index.ts`. Locally, curl
57
57
  `/.pple/registry/<name>/…`. The dev wrapper sets `x-pple-consumer`
58
58
  (`DEV_CONSUMER`, default `dev-consumer`), `x-pple-entry`
59
- (`dev-provider/<name>`), and `x-pple-identity` from the active Persona.
59
+ (the bare `<name>`; the dev Provider is `dev-provider`), and `x-pple-identity` from the active Persona.
60
60
  `DEV_PERSONA=none` or an unknown persona name omits the Identity (as
61
61
  `userToken: none` does) and does not fall back to `hq-staff` on this path.
62
62
  Read the caller with `serviceCall(request)`. Tests call `callRegistry` from
63
63
  `test/harness.ts`. An entry with no handler throws `registry handler missing`.
64
64
  The wrapper is compiled out of the deployed Worker.
65
- - `callService` locally answers from `.pple/registry/<provider>/<name>/mocks/responses.json`
66
- as the active persona (`x-pple-mock` names the entry). A `userToken: required`
65
+ - `callService` takes the entry's full name: a bare name such as `branch-list`,
66
+ a Namespace (กลุ่มชื่อ) name such as `election/2027/score`, or one of the
67
+ platform's own entries, which all sit under `platform`: `platform/sms`,
68
+ `platform/legal-identity`. A bare `sms` names nothing, and `platform` on its
69
+ own is refused. Locally it answers from
70
+ `.pple/registry/<full name>/mocks/responses.json` (nested folders for a
71
+ Namespace) as the active persona (`x-pple-mock` names the entry). A `userToken: required`
67
72
  entry called with no token is `user_token_invalid`. Nothing matched is
68
73
  `no_mock` (404), dev-only, with a Thai message that names `pple registry`.
69
- `DEV_REGISTRY_ERROR` (`<code>` or `<provider>/<name>=<code>`, commas) returns
74
+ `DEV_REGISTRY_ERROR` (`<code>` or `<full name>=<code>`, commas) returns
70
75
  that platform refusal instead. A persona added in `dev-personas.ts` only
71
76
  hits cases that name no persona. `DEV_PERSONA=none` calls the real service.
72
77
  Vitest uses the injected fetch and does not read the overlay. The overlay
@@ -80,7 +85,7 @@ Where the token comes from:
80
85
  `next`. The user token is `options.userToken` and is sent only when set.
81
86
  Platform refusals are `x-pple-error` (`not_found`, `invalid_query`,
82
87
  `level_not_met`, and the same codes as `callService`). Locally it reads
83
- `.pple/registry/<provider>/<name>/mocks/<objectType>.json` through the same
88
+ `.pple/registry/<full name>/mocks/<objectType>.json` through the same
84
89
  query rules and `passesLevel` against the active Persona. `sweptAt` is the
85
90
  mock file's write time. A level whose `subs` list is missing, including
86
91
  `subs: swept`, admits nobody offline. `DEV_REGISTRY_ERROR` and `no_mock`
@@ -38,14 +38,15 @@ someone else, set `DEV_PERSONA` in `.dev.vars` (see `.dev.vars.example`) to
38
38
  or add your own in `src/dev-personas.ts`.
39
39
 
40
40
  `callService` on `npm run dev` answers from
41
- `.pple/registry/<provider>/<name>/mocks/responses.json` as that persona. A
41
+ `.pple/registry/<full name>/mocks/responses.json` (nested for a Namespace such as `election/2027/score`) as that persona. A
42
42
  missing entry or route is `no_mock` (404) and names `pple registry`. Set
43
43
  `DEV_REGISTRY_ERROR` to rehearse a platform refusal such as `not_granted` or
44
- `provider_timeout`. `DEV_PERSONA=none` calls the real service. This path is
44
+ `provider_timeout`: `<code>` for every entry, or `<full name>=<code>` for one
45
+ (`orders=provider_timeout`). `DEV_PERSONA=none` calls the real service. This path is
45
46
  compiled out of the deployed Worker.
46
47
 
47
48
  `readDataset` on `npm run dev` reads
48
- `.pple/registry/<provider>/<name>/mocks/<objectType>.json` the same way:
49
+ `.pple/registry/<full name>/mocks/<objectType>.json` the same way:
49
50
  filters, sort and pages run locally, and the active persona has to pass the
50
51
  Object Type's level. `sweptAt` is the time that mock file was written. A
51
52
  level that only names people, or a swept list, admits nobody offline — those
@@ -56,7 +57,7 @@ compiled out of the deployed Worker.
56
57
  To exercise a Service this app provides, curl
57
58
  `/.pple/registry/<name>/…` on the dev server. That path is delivered to
58
59
  `registry[name]` with the active Persona's Identity and a fake Consumer
59
- (`dev-consumer`, or `DEV_CONSUMER`). The entry id is `dev-provider/<name>`.
60
+ (`dev-consumer`, or `DEV_CONSUMER`). `x-pple-entry` is the bare name; the dev Provider is `dev-provider`.
60
61
  `DEV_PERSONA=none`, or a persona name that is not defined, sends no
61
62
  Identity — the same as an entry that declares `userToken: none`. An unknown
62
63
  name does not fall back to `hq-staff` on this path. The wrapper is compiled
@@ -7,7 +7,7 @@
7
7
  "name": "pple-app",
8
8
  "dependencies": {
9
9
  "@fontsource/ibm-plex-sans-thai-looped": "^5.3.0",
10
- "@pplethai/components": "^1.4.3",
10
+ "@pplethai/components": "^2.0.0",
11
11
  "@pplethai/pple-today-miniapp-sdk": "^0.1.4",
12
12
  "hono": "^4.12.30",
13
13
  "lucide-react": "^0.469.0",
@@ -1327,9 +1327,9 @@
1327
1327
  "license": "MIT"
1328
1328
  },
1329
1329
  "node_modules/@pplethai/components": {
1330
- "version": "1.4.3",
1331
- "resolved": "https://registry.npmjs.org/@pplethai/components/-/components-1.4.3.tgz",
1332
- "integrity": "sha512-YB8y5WsGduRI0pRos6vrjqpVWYvpxK9gDPXPSAhSC24hM7Xofk1A4o1poZ7g8DFVfG91FWGx8LXtAZ8okHhqsw==",
1330
+ "version": "2.0.0",
1331
+ "resolved": "https://registry.npmjs.org/@pplethai/components/-/components-2.0.0.tgz",
1332
+ "integrity": "sha512-AWl1NRTPC+g+h1QxYKeJrtnRfO/P4RboKmhto/Lpxzsqq+MuPa+/qqq3jS+aOabIShZLaJIo5/eZbIf1XOXG+Q==",
1333
1333
  "license": "MIT",
1334
1334
  "dependencies": {
1335
1335
  "@fontsource/ibm-plex-sans-thai-looped": "^5.1.0",
@@ -1355,13 +1355,20 @@
1355
1355
  "lucide-react": "^0.469.0",
1356
1356
  "react-day-picker": "^10.0.1",
1357
1357
  "sonner": "^1.7.1",
1358
- "tailwind-merge": "^2.6.0"
1358
+ "tailwind-merge": "^3.7.0",
1359
+ "tw-animate-css": "^1.4.0"
1359
1360
  },
1360
1361
  "peerDependencies": {
1361
1362
  "react": ">=18",
1362
1363
  "react-dom": ">=18",
1363
1364
  "react-hook-form": ">=7",
1365
+ "tailwindcss": ">=4",
1364
1366
  "zod": ">=3"
1367
+ },
1368
+ "peerDependenciesMeta": {
1369
+ "tailwindcss": {
1370
+ "optional": true
1371
+ }
1365
1372
  }
1366
1373
  },
1367
1374
  "node_modules/@pplethai/pple-today-miniapp-sdk": {
@@ -3848,9 +3855,9 @@
3848
3855
  }
3849
3856
  },
3850
3857
  "node_modules/tailwind-merge": {
3851
- "version": "2.6.1",
3852
- "resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-2.6.1.tgz",
3853
- "integrity": "sha512-Oo6tHdpZsGpkKG88HJ8RR1rg/RdnEkQEfMoEk2x1XRI3F1AxeU+ijRXpiVUF4UbLfcxxRGw6TbUINKYdWVsQTQ==",
3858
+ "version": "3.7.0",
3859
+ "resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-3.7.0.tgz",
3860
+ "integrity": "sha512-XPPUyAc+cvspz3lHTcR/QgPfW2A0lv/xQNIjX3HGhLR+Nq2lHaLq5MtTesHn8GUr3W3DguT2KT5x3NVgRtYwmA==",
3854
3861
  "license": "MIT",
3855
3862
  "funding": {
3856
3863
  "type": "github",
@@ -3907,6 +3914,15 @@
3907
3914
  "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
3908
3915
  "license": "0BSD"
3909
3916
  },
3917
+ "node_modules/tw-animate-css": {
3918
+ "version": "1.4.0",
3919
+ "resolved": "https://registry.npmjs.org/tw-animate-css/-/tw-animate-css-1.4.0.tgz",
3920
+ "integrity": "sha512-7bziOlRqH0hJx80h/3mbicLW7o8qLsH5+RaLR2t+OHM3D0JlWGODQKQ4cxbK7WlvmUxpcj6Kgu6EKqjrGFe3QQ==",
3921
+ "license": "MIT",
3922
+ "funding": {
3923
+ "url": "https://github.com/sponsors/Wombosvideo"
3924
+ }
3925
+ },
3910
3926
  "node_modules/typescript": {
3911
3927
  "version": "5.9.3",
3912
3928
  "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@fontsource/ibm-plex-sans-thai-looped": "^5.3.0",
19
- "@pplethai/components": "^1.4.3",
19
+ "@pplethai/components": "^2.0.0",
20
20
  "@pplethai/pple-today-miniapp-sdk": "^0.1.4",
21
21
  "hono": "^4.12.30",
22
22
  "lucide-react": "^0.469.0",
@@ -11,13 +11,18 @@
11
11
  // CI does not have.
12
12
 
13
13
  import { answerOfflineCall } from "./dev-call-service";
14
+ import { assertEntryName } from "./entry-name";
14
15
 
15
16
  export const PLATFORM_ERROR_HEADER = "x-pple-error";
16
17
 
17
18
  /**
18
19
  * Platform refusals, in the order a caller meets them.
19
- * The first eight are ADR-0024 (a Service call). The last four are ADR-0030
20
- * (a Dataset read) and are not part of the Service check order.
20
+ * The first eight are ADR-0024 (a Service call). The next four are ADR-0030
21
+ * (a Dataset read) and are not part of the Service check order. `renamed` is
22
+ * ADR-0032: an admin renamed the entry's Namespace, so the old name is not
23
+ * served at all. It is a 410 for a Service call and a Dataset read alike, and
24
+ * the body is `{ error, renamed, renamedTo, message }` with the new name in
25
+ * `renamedTo`. It is answered before any grant or token is looked at.
21
26
  */
22
27
  export const PLATFORM_ERROR_CODES = [
23
28
  "invalid_app_key",
@@ -32,6 +37,7 @@ export const PLATFORM_ERROR_CODES = [
32
37
  "not_found",
33
38
  "invalid_query",
34
39
  "level_not_met",
40
+ "renamed",
35
41
  ] as const;
36
42
 
37
43
  export type PlatformErrorCode = (typeof PLATFORM_ERROR_CODES)[number];
@@ -46,8 +52,8 @@ export interface CallServiceEnv {
46
52
  /** `"none"` uses the real service. Anything else, under `npm run dev`, uses the overlay. */
47
53
  DEV_PERSONA?: string;
48
54
  /**
49
- * Offline only. `<code>` for every entry, or `<provider>/<name>=<code>`,
50
- * comma-separated. A per-entry code wins. Unknown codes are ignored.
55
+ * Offline only. `<code>` for every entry, or `<full name>=<code>`
56
+ * (`election/2027/score=503`), comma-separated. A per-entry code wins. Unknown codes are ignored.
51
57
  */
52
58
  DEV_REGISTRY_ERROR?: string;
53
59
  }
@@ -63,7 +69,10 @@ export interface CallServiceOptions {
63
69
  }
64
70
 
65
71
  /**
66
- * `GET`/`POST`/… `services.<domain>/<provider>/<name>/<path>`.
72
+ * `GET`/`POST`/… `services.<domain>/<full name>/<path>`. `entry` is the full
73
+ * name: a bare name such as `"branch-list"`, a Namespace and a path such as
74
+ * `"election/2027/score"`, or a platform entry such as `"platform/sms"`.
75
+ * Anything the shared validator refuses throws, the bare `"platform"` included.
67
76
  *
68
77
  * `path` `""` is the entry root. A path without a leading `/` or `?` gains
69
78
  * one `/`. The App Key is always `Authorization: Bearer`. A `userToken` is
@@ -81,9 +90,7 @@ export async function callService(
81
90
  init: RequestInit = {},
82
91
  options: CallServiceOptions = {},
83
92
  ): Promise<Response> {
84
- if (!/^[^/\s]+\/[^/\s]+$/.test(entry)) {
85
- throw new Error('callService entry must be "<provider>/<name>"');
86
- }
93
+ assertEntryName("callService", entry);
87
94
 
88
95
  // `import.meta.env.DEV` is false in the production build, so this branch
89
96
  // and `dev-call-service.ts` are absent from the deployed Worker. Vitest
@@ -108,8 +115,8 @@ export async function callService(
108
115
  /**
109
116
  * The platform refusal code on `response`, read from `x-pple-error`.
110
117
  *
111
- * Null when the header is absent or names a code outside ADR-0024 and
112
- * ADR-0030. A null from an absent header means the Provider answered.
118
+ * Null when the header is absent or names a code outside ADR-0024, ADR-0030
119
+ * and ADR-0032. A null from an absent header means the Provider answered.
113
120
  * `no_mock` is dev-only and stays null here. Callers that must not treat an
114
121
  * unknown code as a Provider response check the header itself.
115
122
  */
@@ -4,7 +4,9 @@
4
4
  // branch, so `vite build` drops it the same way it drops the Persona bypass
5
5
  // (ADR-0011).
6
6
  //
7
- // The overlay is `.pple/registry/<provider>/<name>/`, written by the CLI.
7
+ // The overlay is `.pple/registry/<full name>/`, written by the CLI (ADR-0031,
8
+ // ADR-0032: an entry is named by its full name, so `election/2027/score` is
9
+ // nested folders; the Provider is not part of the path).
8
10
  // Vite's `import.meta.glob` skips a dot-folder unless the dot sits in the
9
11
  // pattern's fixed base, so the glob below keeps `.pple/registry` in that
10
12
  // base. CI has no overlay; tests pass a fixture glob to `answerFromOverlay`
@@ -16,6 +18,7 @@
16
18
  // `callService`.
17
19
 
18
20
  import type { PlatformErrorCode } from "./call-service";
21
+ import { isEntryName, entryNameError, overlayEntryFile } from "./entry-name";
19
22
  import { DEV_PERSONAS, DEFAULT_DEV_PERSONA, resolveDevPersona } from "./dev-personas";
20
23
 
21
24
  /** Statuses for every platform code. Keyed by the union, so a new code fails typecheck here. */
@@ -32,6 +35,7 @@ const PLATFORM_ERROR_STATUS: Record<PlatformErrorCode, number> = {
32
35
  not_found: 404,
33
36
  invalid_query: 400,
34
37
  level_not_met: 403,
38
+ renamed: 410,
35
39
  };
36
40
 
37
41
  const MOCK_HEADER = "x-pple-mock";
@@ -58,6 +62,8 @@ export interface OverlayCall {
58
62
  /** Active Persona name. A name the Builder added matches only unscoped cases. */
59
63
  persona: string;
60
64
  devRegistryError?: string;
65
+ /** Where the overlay starts in `files`' keys. Omitted: `.pple/registry/`. Tests with a fixture folder set it. */
66
+ overlayRoot?: string;
61
67
  }
62
68
 
63
69
  interface IndexedEntry {
@@ -101,7 +107,7 @@ export function answerFromOverlay(
101
107
  const forced = forcedError(call.devRegistryError, call.entry);
102
108
  if (forced) return platformRefusal(forced);
103
109
 
104
- const entry = indexOverlay(files).get(call.entry);
110
+ const entry = indexOverlay(files, call.overlayRoot).get(call.entry);
105
111
  if (!entry) return noMock(missingEntryMessage(call.entry));
106
112
 
107
113
  if (entry.userToken === "required" && !hasToken(call.userToken)) {
@@ -152,27 +158,18 @@ function activePersonaName(configured: string | undefined): string {
152
158
  return DEFAULT_DEV_PERSONA;
153
159
  }
154
160
 
155
- function indexOverlay(files: Readonly<Record<string, unknown>>): Map<string, IndexedEntry> {
161
+ function indexOverlay(files: Readonly<Record<string, unknown>>, root?: string): Map<string, IndexedEntry> {
156
162
  const found = new Map<string, { entryMd?: string; responses?: string }>();
157
163
  for (const [file, value] of Object.entries(files)) {
158
164
  const text = asText(value);
159
165
  if (text === null) continue;
160
- const path = file.split("?")[0]?.replaceAll("\\", "/") ?? file;
161
- const entryMd = path.match(/(?:^|\/)([^/]+)\/([^/]+)\/entry\.md$/);
162
- if (entryMd?.[1] && entryMd[2]) {
163
- const id = `${entryMd[1]}/${entryMd[2]}`;
164
- const row = found.get(id) ?? {};
165
- row.entryMd = text;
166
- found.set(id, row);
167
- continue;
168
- }
169
- const responses = path.match(/(?:^|\/)([^/]+)\/([^/]+)\/mocks\/responses\.json$/);
170
- if (responses?.[1] && responses[2]) {
171
- const id = `${responses[1]}/${responses[2]}`;
172
- const row = found.get(id) ?? {};
173
- row.responses = text;
174
- found.set(id, row);
175
- }
166
+ const at = overlayEntryFile(file, root);
167
+ if (!at) continue;
168
+ const row = found.get(at.name) ?? {};
169
+ if (at.inside === "entry.md") row.entryMd = text;
170
+ else if (at.inside === "mocks/responses.json") row.responses = text;
171
+ else continue;
172
+ found.set(at.name, row);
176
173
  }
177
174
 
178
175
  const entries = new Map<string, IndexedEntry>();
@@ -226,7 +223,7 @@ function hasToken(userToken: string | undefined): boolean {
226
223
  }
227
224
 
228
225
  /**
229
- * A per-entry `<provider>/<name>=<code>` wins over a bare `<code>`. An
226
+ * A per-entry `<full name>=<code>` wins over a bare `<code>`. An
230
227
  * unknown code is ignored, so a bad per-entry rule falls through to the
231
228
  * global one. Checked before the missing-token refusal: `not_granted` and
232
229
  * `needs_beta` are answers the platform gives before it looks at the token.
@@ -248,6 +245,10 @@ export function forcedError(spec: string | undefined, entry: string): PlatformEr
248
245
  console.warn(`DEV_REGISTRY_ERROR ignores "${item}".`);
249
246
  continue;
250
247
  }
248
+ if (!isEntryName(id)) {
249
+ console.warn(`DEV_REGISTRY_ERROR ignores "${item}": ${entryNameError(id)}.`);
250
+ continue;
251
+ }
251
252
  rules.push({ entry: id, code });
252
253
  }
253
254
 
@@ -4,7 +4,8 @@
4
4
  // only from that branch, so `vite build` drops it the same way it drops the
5
5
  // Persona bypass (ADR-0011).
6
6
  //
7
- // The overlay is `.pple/registry/<provider>/<name>/`. Vite's `import.meta.glob`
7
+ // The overlay is `.pple/registry/<full name>/`, nested for a namespaced entry
8
+ // (`election/2027/score`, ADR-0032). Vite's `import.meta.glob`
8
9
  // skips a dot-folder unless the dot sits in the pattern's fixed base, so both
9
10
  // globs below keep `.pple/registry` in that base. CI has no overlay; tests
10
11
  // pass a fixture glob to `answerOfflineDataset` and never read `.pple/`.
@@ -27,6 +28,7 @@ import { parse as parseYaml } from "yaml";
27
28
  import { forcedError, platformRefusal } from "./dev-call-service";
28
29
  import { resolveDevPersona } from "./dev-personas";
29
30
  import { applyDatasetQuery, datasetOpaqueHash, datasetPageHash, parseDatasetQuery, type DatasetQuerySpec, type DatasetRow } from "./dataset-query";
31
+ import { overlayEntryFile, overlayRelative } from "./entry-name";
30
32
  import { passesLevel, type AccessLevel } from "./levels";
31
33
  import { isPlainObject } from "./dataset-schema";
32
34
 
@@ -43,6 +45,8 @@ export interface OfflineDatasetCall {
43
45
  /** Active Persona name. An unknown name warns and uses `hq-staff`. */
44
46
  persona: string;
45
47
  devRegistryError?: string;
48
+ /** Where the overlay starts in `files`' keys. Omitted: `.pple/registry/`. Tests with a fixture folder set it. */
49
+ overlayRoot?: string;
46
50
  }
47
51
 
48
52
  interface DeclaredType {
@@ -88,7 +92,7 @@ export function answerOfflineDataset(
88
92
  const forced = forcedError(call.devRegistryError, call.entry);
89
93
  if (forced) return platformRefusal(forced);
90
94
 
91
- const indexed = indexOverlay(files);
95
+ const indexed = indexOverlay(files, call.overlayRoot);
92
96
  const entry = indexed.get(call.entry);
93
97
  if (!entry) return noMock(missingEntryMessage(call.entry));
94
98
 
@@ -110,7 +114,7 @@ export function answerOfflineDataset(
110
114
  const mock = entry.mocks.get(call.objectType);
111
115
  if (mock === undefined) return noMock(missingMockMessage(call.entry, call.objectType));
112
116
 
113
- const sweptAt = writtenTime(writtenAt, call.entry, call.objectType);
117
+ const sweptAt = writtenTime(writtenAt, call.entry, call.objectType, call.overlayRoot);
114
118
  const snapshot = snapshotOf(mock);
115
119
  const search = call.search ?? [];
116
120
 
@@ -173,26 +177,17 @@ function overlayWrittenAt(): Record<string, unknown> {
173
177
  });
174
178
  }
175
179
 
176
- function indexOverlay(files: Readonly<Record<string, unknown>>): Map<string, IndexedEntry> {
180
+ function indexOverlay(files: Readonly<Record<string, unknown>>, root?: string): Map<string, IndexedEntry> {
177
181
  const found = new Map<string, { entryMd?: string; mocks: Map<string, string> }>();
178
- const touch = (id: string) => {
179
- const row = found.get(id) ?? { mocks: new Map<string, string>() };
180
- found.set(id, row);
181
- return row;
182
- };
183
182
  for (const [file, value] of Object.entries(files)) {
184
183
  const text = asText(value);
185
184
  if (text === null) continue;
186
- const path = normalize(file);
187
- const entryMd = path.match(/(?:^|\/)([^/]+)\/([^/]+)\/entry\.md$/);
188
- if (entryMd?.[1] && entryMd[2]) {
189
- touch(`${entryMd[1]}/${entryMd[2]}`).entryMd = text;
190
- continue;
191
- }
192
- const mock = path.match(/(?:^|\/)([^/]+)\/([^/]+)\/mocks\/([^/]+)\.json$/);
193
- if (mock?.[1] && mock[2] && mock[3]) {
194
- touch(`${mock[1]}/${mock[2]}`).mocks.set(mock[3], text);
195
- }
185
+ const at = overlayEntryFile(file, root);
186
+ if (!at) continue;
187
+ const row = found.get(at.name) ?? { mocks: new Map<string, string>() };
188
+ found.set(at.name, row);
189
+ if (at.inside === "entry.md") row.entryMd = text;
190
+ else row.mocks.set(at.inside.slice("mocks/".length, -".json".length), text);
196
191
  }
197
192
  const entries = new Map<string, IndexedEntry>();
198
193
  for (const [id, row] of found) {
@@ -347,25 +342,19 @@ function canonicalId(raw: string, idType: DatasetQuerySpec["idType"]): string |
347
342
  return String(value);
348
343
  }
349
344
 
350
- function writtenTime(writtenAt: Readonly<Record<string, unknown>>, entry: string, objectType: string): string | null {
345
+ function writtenTime(
346
+ writtenAt: Readonly<Record<string, unknown>>,
347
+ entry: string,
348
+ objectType: string,
349
+ root?: string,
350
+ ): string | null {
351
351
  const times = new Map<string, string>();
352
352
  for (const [file, value] of Object.entries(writtenAt)) {
353
353
  if (typeof value !== "string" || value === "") continue;
354
- times.set(normalize(file), value);
354
+ const relative = overlayRelative(file, root);
355
+ if (relative !== null) times.set(relative, value);
355
356
  }
356
- const mockSuffix = `${entry}/mocks/${objectType}.json`;
357
- const entrySuffix = `${entry}/entry.md`;
358
- for (const [path, iso] of times) {
359
- if (pathEnds(path, mockSuffix)) return iso;
360
- }
361
- for (const [path, iso] of times) {
362
- if (pathEnds(path, entrySuffix)) return iso;
363
- }
364
- return null;
365
- }
366
-
367
- function pathEnds(path: string, suffix: string): boolean {
368
- return path === suffix || path.endsWith(`/${suffix}`);
357
+ return times.get(`${entry}/mocks/${objectType}.json`) ?? times.get(`${entry}/entry.md`) ?? null;
369
358
  }
370
359
 
371
360
  /** Stable across pages of one file, and different after the mock text changes. */
@@ -17,9 +17,17 @@
17
17
  // `userToken: none`. A name that is not a Persona does the same, and does
18
18
  // not fall back to the default: a registry call must not invent a caller.
19
19
  // `/api` still falls back, so a typo does not lock the Builder out of the
20
- // app. The Consumer is `DEV_CONSUMER`, or `dev-consumer`. The entry id is
21
- // `dev-provider/<name>` — a local app has no App Slug until the platform
22
- // creates it.
20
+ // app. The Consumer is `DEV_CONSUMER`, or `dev-consumer`. `x-pple-entry` is
21
+ // the `registry` key. For a bare entry that is the name the platform sends
22
+ // (ADR-0031). For a namespaced entry the key is its local path (`2027/score`),
23
+ // because the Provider never sees the Namespace (ADR-0032), so the platform's
24
+ // header carries the full name while this one carries the path. The Provider is
25
+ // not part of it: the local app is the dev Provider, `dev-provider`, because it
26
+ // has no App Slug until the platform creates it.
27
+ //
28
+ // The registry path names an entry by its local path, which may run several
29
+ // folders (`/.pple/registry/2027/score/<tail>`). No key is a segment-prefix of
30
+ // another in one app (ADR-0032), so the key that leads the path is the entry.
23
31
  //
24
32
  // A Dataset value is an object of page functions. `/.pple/registry/<name>/<objectType>`
25
33
  // calls that function with `{ cursor }` and answers with its page. A value with
@@ -47,7 +55,7 @@ import {
47
55
 
48
56
  export const DEFAULT_DEV_CONSUMER = "dev-consumer";
49
57
 
50
- /** Local stand-in for the App Slug. The entry id is `${DEV_PROVIDER_SLUG}/<name>`. */
58
+ /** Local stand-in for the Provider's App Slug. It is never joined into the entry name. */
51
59
  export const DEV_PROVIDER_SLUG = "dev-provider";
52
60
 
53
61
  const REGISTRY_PATH_PREFIX = "/.pple/registry/";
@@ -111,7 +119,7 @@ export async function devWorkerFetch(
111
119
  ctx: ExecutionContext,
112
120
  ): Promise<Response> {
113
121
  const url = new URL(request.url);
114
- const parsed = parseRegistryPath(url.pathname);
122
+ const parsed = parseRegistryPath(url.pathname, user.registry);
115
123
  if (!parsed) return user.fetch(stripPlatformHeaders(request), env, ctx);
116
124
  if (!parsed.name) return emptyRegistryResponse();
117
125
 
@@ -164,7 +172,7 @@ function registryFacts(
164
172
  consumer: string,
165
173
  identity: Identity | null,
166
174
  ): { consumer: string; entry: string; identity: Identity | null } {
167
- return { consumer, entry: `${DEV_PROVIDER_SLUG}/${name}`, identity };
175
+ return { consumer, entry: name, identity };
168
176
  }
169
177
 
170
178
  /** What the handler sees. Matches pple-services' header filter, minus the secret. */
@@ -234,11 +242,21 @@ function handlerFor(registry: RegistryMap | undefined, name: string): RegistryFe
234
242
  throw new Error("registry handler missing");
235
243
  }
236
244
 
237
- function parseRegistryPath(pathname: string): { name: string; tail: string } | null {
245
+ function parseRegistryPath(
246
+ pathname: string,
247
+ registry: RegistryMap | undefined,
248
+ ): { name: string; tail: string } | null {
238
249
  if (pathname !== "/.pple/registry" && !pathname.startsWith(REGISTRY_PATH_PREFIX)) return null;
239
250
  const restPath = pathname.startsWith(REGISTRY_PATH_PREFIX)
240
251
  ? pathname.slice(REGISTRY_PATH_PREFIX.length)
241
252
  : "";
253
+ // A nested local path (`2027/score`) is a key that leads the path at a
254
+ // segment boundary. Otherwise the first segment, so a missing handler
255
+ // throws as before.
256
+ const key = Object.keys(registry ?? {}).find(
257
+ (candidate) => candidate.includes("/") && (restPath === candidate || restPath.startsWith(`${candidate}/`)),
258
+ );
259
+ if (key) return { name: key, tail: restPath.slice(key.length) || "/" };
242
260
  const slash = restPath.indexOf("/");
243
261
  const name = slash === -1 ? restPath : restPath.slice(0, slash);
244
262
  const tail = slash === -1 ? "/" : restPath.slice(slash) || "/";
@@ -0,0 +1,114 @@
1
+ // A Registry Entry is named by its full name: one bare segment (`branch-list`),
2
+ // or a Namespace (กลุ่มชื่อ) plus one to three more segments (`election/score`,
3
+ // `election/2027/score`). The Provider is not part of the name (ADR-0031,
4
+ // ADR-0032). The platform's own entries all sit under the Namespace `platform`:
5
+ // `platform/sms`, `platform/legal-identity`.
6
+ //
7
+ // A copy of the name rules in workers/src/shared/registry/validate.ts. This
8
+ // file is copied into a Builder App, which cannot import the platform
9
+ // package. cli/test/registry-entry-name.test.ts keeps the two in step.
10
+ //
11
+ // `platform` is reserved for apps: no app may use it as a bare name or a
12
+ // Namespace. That is why `platform/<x>` is the platform's name and never an
13
+ // app's. A Consumer calls it by that name, so it passes here; the bare
14
+ // `platform` does not.
15
+
16
+ /** The slug shape of one segment: lowercase words joined by single hyphens. */
17
+ const SEGMENT = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
18
+ /** The longest one segment may be. */
19
+ const SEGMENT_MAX = 63;
20
+ /** The longest a full name may be, slashes included. */
21
+ export const ENTRY_FULL_NAME_MAX = 128;
22
+ /** A Namespace plus at most three path segments. */
23
+ export const ENTRY_NAME_MAX_SEGMENTS = 4;
24
+ /** The Namespace of every platform entry (`platform/sms`). The platform holds it; no app may. */
25
+ export const PLATFORM_NAMESPACE = "platform";
26
+ /** Names the platform keeps for itself. Rejected as a bare name and as an app's Namespace. */
27
+ export const RESERVED_ENTRY_NAMES: ReadonlySet<string> = new Set(["health", "overlay", "platform"]);
28
+
29
+ /**
30
+ * Why `name` is not an entry name, or null when it is one. Reserved first
31
+ * segments are reported apart from other shape problems so a caller can say
32
+ * more about them. `platform/<path>` is an entry name: the platform's own
33
+ * Namespace, which only the platform publish holds. The bare `platform` is not.
34
+ */
35
+ export function entryNameProblem(name: string): "shape" | "reserved" | null {
36
+ if (name.length > ENTRY_FULL_NAME_MAX) return "shape";
37
+ const segments = name.split("/");
38
+ if (segments.length > ENTRY_NAME_MAX_SEGMENTS) return "shape";
39
+ if (!segments.every((segment) => segment.length <= SEGMENT_MAX && SEGMENT.test(segment))) return "shape";
40
+ const top = segments[0] ?? "";
41
+ if (top === PLATFORM_NAMESPACE && segments.length > 1) return null;
42
+ return RESERVED_ENTRY_NAMES.has(top) ? "reserved" : null;
43
+ }
44
+
45
+ /** True when `name` is one an entry may have. */
46
+ export function isEntryName(name: string): boolean {
47
+ return entryNameProblem(name) === null;
48
+ }
49
+
50
+ /** Why `name` is refused, in a sentence. Null when it is fine. */
51
+ export function entryNameError(name: string): string | null {
52
+ const problem = entryNameProblem(name);
53
+ if (problem === null) return null;
54
+ if (problem === "reserved") {
55
+ const top = name.split("/")[0] ?? name;
56
+ if (top === PLATFORM_NAMESPACE) {
57
+ return `"${name}" is not an entry name: "platform" belongs to the platform, and its entries are named under it, such as "platform/sms"`;
58
+ }
59
+ return `"${top}" is reserved by the platform and cannot start an entry name`;
60
+ }
61
+ return (
62
+ `"${name}" is not an entry name: use one bare name such as "branch-list", or a Namespace and up to ` +
63
+ `${ENTRY_NAME_MAX_SEGMENTS - 1} more names such as "election/2027/score" ` +
64
+ `(lowercase letters, digits and hyphens; at most ${ENTRY_FULL_NAME_MAX} characters)`
65
+ );
66
+ }
67
+
68
+ /**
69
+ * Throws unless `entry` is an entry name. `fn` is the public function that
70
+ * received it, so the message names the call the Builder wrote.
71
+ */
72
+ export function assertEntryName(fn: string, entry: string): void {
73
+ const error = entryNameError(entry);
74
+ if (error) throw new Error(`${fn} entry ${error}`);
75
+ }
76
+
77
+ /** Where the workspace overlay lives. The CLI writes one folder per entry under it. */
78
+ export const OVERLAY_ROOT = ".pple/registry/";
79
+
80
+ /**
81
+ * `file` (a glob key or a path) relative to the overlay root, posix, or null
82
+ * when it is not under `root`. The root must start at a path boundary, so
83
+ * `x.pple/registry/` is not a root. Tests pass their own `root` for a fixture
84
+ * folder, because CI has no `.pple/`.
85
+ */
86
+ export function overlayRelative(file: string, root: string = OVERLAY_ROOT): string | null {
87
+ const path = file.split("?")[0]?.replaceAll("\\", "/") ?? file;
88
+ let from = 0;
89
+ for (;;) {
90
+ const at = path.indexOf(root, from);
91
+ if (at === -1) return null;
92
+ if (at === 0 || path[at - 1] === "/") return path.slice(at + root.length);
93
+ from = at + 1;
94
+ }
95
+ }
96
+
97
+ /**
98
+ * The entry full name a file under the overlay belongs to, with the file's
99
+ * place inside that entry's folder (`entry.md`, `mocks/orders.json`). An entry
100
+ * folder is nested for a namespaced entry, so the name is every folder above
101
+ * `entry.md` or `mocks/`. Null for a file that is neither.
102
+ */
103
+ export function overlayEntryFile(
104
+ file: string,
105
+ root: string = OVERLAY_ROOT,
106
+ ): { name: string; inside: string } | null {
107
+ const relative = overlayRelative(file, root);
108
+ if (relative === null) return null;
109
+ const entryMd = /^(.+)\/(entry\.md)$/.exec(relative);
110
+ if (entryMd?.[1] && entryMd[2]) return { name: entryMd[1], inside: entryMd[2] };
111
+ const mock = /^(.+)\/(mocks\/[^/]+\.json)$/.exec(relative);
112
+ if (mock?.[1] && mock[2]) return { name: mock[1], inside: mock[2] };
113
+ return null;
114
+ }