@tier0/sdk-v2 0.9.6 → 0.9.8

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 (66) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +7 -5
  3. package/dist/cjs/capabilities.js +7 -4
  4. package/dist/cjs/capabilities.js.map +1 -1
  5. package/dist/cjs/client.d.ts +4 -6
  6. package/dist/cjs/data/bootstrap.js +6 -1
  7. package/dist/cjs/data/bootstrap.js.map +1 -1
  8. package/dist/cjs/data/define.d.ts +7 -0
  9. package/dist/cjs/data/define.js +21 -1
  10. package/dist/cjs/data/define.js.map +1 -1
  11. package/dist/cjs/data/schema.d.ts +1 -0
  12. package/dist/cjs/data/schema.js +9 -0
  13. package/dist/cjs/data/schema.js.map +1 -1
  14. package/dist/cjs/openapi/api.generated.d.ts +1 -0
  15. package/dist/cjs/openapi/codecs.generated.d.ts +54 -28
  16. package/dist/cjs/openapi/codecs.generated.js +1 -1
  17. package/dist/cjs/openapi/codecs.generated.js.map +1 -1
  18. package/dist/cjs/openapi/types.generated.d.ts +19 -2
  19. package/dist/cjs/openapi/types.generated.js.map +1 -1
  20. package/dist/cjs/source.d.ts +0 -3
  21. package/dist/cjs/source.js +0 -1
  22. package/dist/cjs/source.js.map +1 -1
  23. package/dist/cjs/uns-v2/generated.d.ts +29 -88
  24. package/dist/cjs/uns-v2/generated.js +21 -23
  25. package/dist/cjs/uns-v2/generated.js.map +1 -1
  26. package/dist/esm/capabilities.js +7 -4
  27. package/dist/esm/capabilities.js.map +1 -1
  28. package/dist/esm/client.d.ts +4 -6
  29. package/dist/esm/data/bootstrap.js +6 -1
  30. package/dist/esm/data/bootstrap.js.map +1 -1
  31. package/dist/esm/data/define.d.ts +7 -0
  32. package/dist/esm/data/define.js +19 -0
  33. package/dist/esm/data/define.js.map +1 -1
  34. package/dist/esm/data/schema.d.ts +1 -0
  35. package/dist/esm/data/schema.js +10 -1
  36. package/dist/esm/data/schema.js.map +1 -1
  37. package/dist/esm/openapi/api.generated.d.ts +1 -0
  38. package/dist/esm/openapi/codecs.generated.d.ts +54 -28
  39. package/dist/esm/openapi/codecs.generated.js +1 -1
  40. package/dist/esm/openapi/codecs.generated.js.map +1 -1
  41. package/dist/esm/openapi/types.generated.d.ts +19 -2
  42. package/dist/esm/openapi/types.generated.js.map +1 -1
  43. package/dist/esm/source.d.ts +0 -3
  44. package/dist/esm/source.js +0 -1
  45. package/dist/esm/source.js.map +1 -1
  46. package/dist/esm/uns-v2/generated.d.ts +29 -88
  47. package/dist/esm/uns-v2/generated.js +21 -23
  48. package/dist/esm/uns-v2/generated.js.map +1 -1
  49. package/package.json +2 -1
  50. package/skills/tier0-sdk-v2/tier0-sdk-v2-data/SKILL.md +4 -1
  51. package/skills/tier0-sdk-v2/tier0-sdk-v2-data/references/consistency.md +3 -3
  52. package/skills/tier0-sdk-v2/tier0-sdk-v2-data/references/errors.md +4 -0
  53. package/skills/tier0-sdk-v2/tier0-sdk-v2-data/references/modeling.md +32 -1
  54. package/skills/tier0-sdk-v2/tier0-sdk-v2-data/references/read-write.md +3 -3
  55. package/skills/tier0-sdk-v2/tier0-sdk-v2-data/references/schema-evolution.md +3 -2
  56. package/skills/tier0-sdk-v2/tier0-sdk-v2-source/SKILL.md +5 -6
  57. package/skills/tier0-sdk-v2/tier0-sdk-v2-source/references/changes.md +2 -2
  58. package/skills/tier0-sdk-v2/tier0-sdk-v2-source/references/create.md +6 -6
  59. package/skills/tier0-sdk-v2/tier0-sdk-v2-source/references/delete.md +1 -1
  60. package/skills/tier0-sdk-v2/tier0-sdk-v2-source/references/read.md +1 -1
  61. package/skills/tier0-sdk-v2/tier0-sdk-v2-source/references/update.md +8 -8
  62. package/skills/tier0-sdk-v2/tier0-sdk-v2-stream/SKILL.md +1 -1
  63. package/skills/tier0-sdk-v2/tier0-sdk-v2-stream/references/create.md +1 -1
  64. package/skills/tier0-sdk-v2/tier0-sdk-v2-stream/references/delete.md +1 -1
  65. package/skills/tier0-sdk-v2/tier0-sdk-v2-uns/references/namespace.md +1 -1
  66. package/skills/tier0-sdk-v2/tier0-sdk-v2-source/references/state.md +0 -36
package/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ ## 0.9.8 (unreleased candidate)
4
+
5
+ - Add `directoryPath` to App collection declarations and preserve it through compilation and first-use ensure. New collections use actual business directories; existing aliases retain their location.
6
+ - Validate and freeze paths, and document known ISA-95 scopes, shared ancestors, and App/module directories within those scopes without forcing five levels.
7
+ - Preserve the Source lifecycle changes from 0.9.7 and pin their combined contract with App collection directories to Backend commit `5c77d6f21e6332cb09e3212df9203eb01bf7f129`.
8
+
9
+ This candidate is not published to npm; install the verified tarball for integration testing against that backend contract.
10
+
11
+ ## 0.9.7 (unreleased candidate)
12
+
13
+ - Remove Cloud Source `state.update`, enable/disable capabilities, `desiredState`, and enable-related MQTT authorization confirmation. A Source can be used after creation; deleting it stops subsequent use.
14
+ - Keep actual Source runtime and health observations, revision checks, credential authorization, and confirmation for referenced connection changes or deletion. Name/description-only edits do not need impact confirmation.
15
+ - Use Unicode character limits of 63 for Source names and 512 for descriptions.
16
+ - Document direct deletion of active Streams and update Source/Stream examples to the shorter lifecycle.
17
+
18
+ The Source lifecycle contract is pinned to Backend commit `87a2bc316fbae69448e58dd66e38450c744793b5`. This candidate is not published to npm; install the verified tarball for integration testing against that backend contract.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Tier0 云端统一 TypeScript / JavaScript SDK,数据管理与 App Stream 交互均使用 `@tier0/sdk-v2`。旧 SDK 不再作为依赖。
4
4
 
5
+ `0.9.8` 候选版新增 App 集合业务目录声明 `directoryPath`,按已知业务范围创建或复用目录,已有集合保持位置。保留 `0.9.7` 的 Source 生命周期简化:创建后可直接使用,删除后停止后续使用;移除 `client.source.state.update`、启停能力与 `desiredState`。名称和描述修改无需影响确认,连接配置变更或删除存在引用时仍须确认;运行中的 Stream 可直接删除。该版本尚未发布 npm,验收使用下方安装包。
6
+
5
7
  `0.9.3` 同步集合授权合同:授予既有集合访问权限时无需提供别名,并更新生成类型与使用说明。集合声明的 `name` / `alias` 要求保持不变。
6
8
 
7
9
  `0.9.2` 包含接口合同变更,升级时需核对下方使用方式并确认目标后端已部署对应合同。平台接口统一使用 OpenAPI v2,能力发现使用 `state + reasonCode + operations`。Stream 请求与消息交互统一到本包的 `client.streams`;`client.stream` 保留 Stream 管理。文件上传支持并发分片与断点续传。
@@ -20,13 +22,13 @@ Collection 声明必须同时提供业务名称 `name`(例如「生产工单
20
22
 
21
23
  ## 安装与初始化
22
24
 
23
- 维护者发布前在本仓库运行 `npm ci`、`npm run check`,并验证生成的 `.tgz`。本分支的功能应使用检查通过的 `tier0-sdk-v2-0.9.6.tgz` 在业务项目中验证:
25
+ 维护者发布前在本仓库运行 `npm ci`、`npm run check`,并验证生成的 `.tgz`。本分支的功能应使用检查通过的 `tier0-sdk-v2-0.9.8.tgz` 在业务项目中验证:
24
26
 
25
27
  ```sh
26
- npm install /path/to/tier0-sdk-v2-0.9.6.tgz
28
+ npm install /path/to/tier0-sdk-v2-0.9.8.tgz
27
29
  ```
28
30
 
29
- `0.9.6` 正式发布后再使用 `npm install @tier0/sdk-v2@0.9.6`。发布前,npm 的既有版本和 `next` 标签不代表本分支的完整功能。
31
+ `0.9.8` 正式发布后再使用 `npm install @tier0/sdk-v2@0.9.8`。发布前,npm 的既有版本和 `next` 标签不代表本分支的完整功能。
30
32
 
31
33
  ```ts
32
34
  import { createClient } from '@tier0/sdk-v2';
@@ -108,7 +110,7 @@ Source/Stream 仍保留 `{ code, msg, requestId, data }` 响应以及 `UnsV2Erro
108
110
 
109
111
  ## UNS 数据访问
110
112
 
111
- 创建本 App 集合使用 `defineCollection` / `appCollectionsEnsure`,`ownerAppId` 表示唯一所属 App。使用已有或其他 App 共享的集合,可用 `data.collection({ collectionId })` 获取数据层句柄,也可向 `tier0.uns.recordFind`、`recordInsertOne` 等接口传入实际 `collectionId`;无需新增声明或配置服务端 alias,也不改变集合归属。引用已有集合跳过 ensure、schema 修改和种子写入;可选的 `schema` 仅用于本地写入校验。App Key 仍须已有覆盖该集合及操作的授权,知道 ID 不代表获得权限。
113
+ 创建本 App 集合使用 `defineCollection` / `appCollectionsEnsure`,新建必须声明 `directoryPath`(从 Workspace 根开始的实际业务名称数组)。工业数据按业务已知的 ISA-95 层级逐级复用或创建目录,不强行补满五层、不统一放入缺少业务范围的根级 App 目录;允许在已知工厂、产线下声明 MES 等 App 或业务模块目录,例如“工厂 / 产线 / MES / 工单”。共享数据放在适合其范围的共同祖先。已有 alias 的位置不会因声明变化自动移动。`ownerAppId` 表示唯一所属 App。使用已有或其他 App 共享的集合,可用 `data.collection({ collectionId })` 获取数据层句柄,也可向 `tier0.uns.recordFind`、`recordInsertOne` 等接口传入实际 `collectionId`;无需新增声明或配置服务端 alias,也不改变集合归属。引用已有集合跳过 ensure、schema 修改和种子写入;可选的 `schema` 仅用于本地写入校验。App Key 仍须已有覆盖该集合及操作的授权,知道 ID 不代表获得权限。
112
114
 
113
115
  新建 Schema 的数值字段使用明确格式:整数为 `type: 'integer', format: 'int32'`,小数为 `type: 'number', format: 'double'`,明确需要单精度时指定 `format: 'float'`。App 数据层编译 Zod 声明时,会递归为未指定格式的 integer/number 补齐 int32/double,并保留显式格式;直接调用 UNS/MCP 时由调用方提供 format。这些是类型注解,不新增数值范围或舍入规则;ensure 不改写已有字段注解,已有 legacy 字段需通过 schema:replace 单独迁移。
114
116
 
@@ -241,7 +243,7 @@ node scripts/check-live-workflows.mjs <consumer> <workspaceId> .tmp/live-workflo
241
243
  node scripts/check-live-transports.mjs <consumer> <workspaceId> .tmp/live-transports.json --write-test-fixtures
242
244
  ```
243
245
 
244
- 写入验收不发布 Stream、不启用 Source、不发送通知。归档资源保留正常产品恢复能力;报告区分通过、服务端拒绝和未验证范围。分片上传失败会保留错误原因,确认尚未尝试 complete 时清理本次会话;完成结果未知时先对账。
246
+ 写入验收不发布 Stream、不发送通知。归档资源保留正常产品恢复能力;报告区分通过、服务端拒绝和未验证范围。分片上传失败会保留错误原因,确认尚未尝试 complete 时清理本次会话;完成结果未知时先对账。
245
247
 
246
248
  Stream 发布和实际运行使用单独的验收脚本,同样只在明确授权的测试 Workspace 执行;按脚本报告区分已测和未测的生命周期阶段:
247
249
 
@@ -51,25 +51,28 @@ function diagnostics(value, fail) {
51
51
  return fail();
52
52
  return result;
53
53
  }
54
- const sourceOperations = ['read', 'configure', 'test', 'discover', 'enable', 'disable'];
54
+ const sourceOperations = ['read', 'configure', 'test', 'discover'];
55
+ const enterpriseSourceOperations = [...sourceOperations, 'enable', 'disable'];
55
56
  const streamOperations = ['read', 'configure', 'validate', 'deploy', 'execute', 'disable'];
56
57
  const resourceOperations = {
57
58
  source: ['test', 'discover', 'enable'],
58
59
  stream: ['deploy', 'execute'],
59
60
  'stream.requests.invoke.v1': ['invoke'],
60
61
  };
61
- function operationFields(value, code, fail) {
62
+ function operationFields(value, code, product, fail) {
62
63
  if (value.operations === undefined)
63
64
  return {};
64
65
  if (!Array.isArray(value.operations))
65
66
  return fail();
66
67
  const operations = [];
67
- const supported = code === 'source' ? sourceOperations : code === 'stream' ? streamOperations :
68
+ const supported = code === 'source' ? (product === 'cloud' ? sourceOperations : enterpriseSourceOperations) : code === 'stream' ? streamOperations :
68
69
  code === 'stream.requests.invoke.v1' ? ['invoke'] : [];
69
70
  const seen = new Set();
70
71
  for (const item of value.operations) {
71
72
  if (!record(item) || !text(item.operation))
72
73
  return fail();
74
+ if (product === 'cloud' && code === 'source' && ['enable', 'disable'].includes(item.operation))
75
+ return fail();
73
76
  if (!supported.includes(item.operation))
74
77
  continue;
75
78
  if (seen.has(item.operation))
@@ -124,7 +127,7 @@ function discovery(value, status, accepted) {
124
127
  code, state: item.state, reasonCode: item.reasonCode,
125
128
  contractVersions: [...item.contractVersions],
126
129
  ...diagnostics(item, fail),
127
- ...operationFields(item, code, fail),
130
+ ...operationFields(item, code, value.product, fail),
128
131
  });
129
132
  }
130
133
  const deprecations = [];
@@ -1 +1 @@
1
- {"version":3,"file":"capabilities.js","sourceRoot":"","sources":["../../src/capabilities.ts"],"names":[],"mappings":";;;AAAA,6DAAsG;AACtG,mDAAiE;AAoDjE,MAAa,eAAgB,SAAQ,KAAK;IAC/B,MAAM,CAAU;IAChB,SAAS,CAAS;IAClB,SAAS,GAAG,KAAK,CAAC;IAE3B,YAAY,OAAe,EAAE,IAAyB;QACpD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;IAClC,CAAC;CACF;AAXD,0CAWC;AAED,MAAM,SAAS,GAA8C;IAC3D,cAAc,EAAE,CAAC,YAAY,CAAC;IAC9B,QAAQ,EAAE,CAAC,YAAY,CAAC;IACxB,QAAQ,EAAE,CAAC,YAAY,CAAC;CACzB,CAAC;AAEF,SAAS,MAAM,CAAC,KAAc;IAC5B,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AACD,SAAS,IAAI,CAAC,KAAc;IAC1B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AACvD,CAAC;AACD,SAAS,OAAO,CAAC,KAAc;IAC7B,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,KAAK,KAAK,CAAC,MAAM,CAAC;AAC3F,CAAC;AAGD,SAAS,WAAW,CAAC,KAA8B,EAAE,IAAiB;IACpE,MAAM,MAAM,GAA0B,EAAE,CAAC;IACz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;YACH,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,mCAAmC,EAAE,EAAE,CAAC,CAAC;QAChF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,CAAC,KAAK,YAAY,wCAAmB,CAAC;gBAAE,MAAM,KAAK,CAAC;YACzD,MAAM,IAAI,eAAe,CAAC,4CAA4C,EAAE;gBACtE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,cAAc,KAAK,CAAC,IAAI,EAAE;aAC5D,CAAC,CAAC;QACL,CAAC;QACD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,QAAQ,CAAC;QACvC,IAAI,CAAC,EAAE,EAAE,CAAC;YACR,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;gBAClF,MAAM,IAAI,eAAe,CAAC,oCAAoC,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,2BAA2B,EAAE,CAAC,CAAC;YACtH,CAAC;YACD,MAAM,IAAI,eAAe,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC;QACnF,CAAC;QACD,OAAO,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IAClD,CAAC;CACF;AA7BD,gDA6BC;AAED,sGAAsG;AACzF,QAAA,YAAY,GAAG,IAAI,kBAAkB,EAAE,CAAC","sourcesContent":["import { CloudTransport, CloudTransportError, type CloudTransportConfig } from './cloud-transport.js';\nimport { getClient, type HttpClient } from './openapi/client.js';\n\nexport type CapabilityCode = 'namespace.v2' | 'source' | 'stream' | 'stream.requests.v1' | 'stream.requests.invoke.v1';\nexport type CapabilityState = 'disabled' | 'not_supported' | 'unavailable' | 'readiness_unknown' | 'ready';\n\nexport interface CapabilityDiagnostics {\n reasonCode?: string;\n checkedAt?: string;\n validUntil?: string;\n retryAfterMs?: number;\n}\n\nexport interface CapabilityOperation extends CapabilityDiagnostics {\n operation: string;\n state: CapabilityState;\n reasonCode: string;\n requiresResourceCheck: boolean;\n}\n\nexport interface PublicCapability extends CapabilityDiagnostics {\n code: CapabilityCode;\n /** Discovery does not grant access to a resource or confirm a successful run. */\n state: CapabilityState;\n reasonCode: string;\n contractVersions: string[];\n operations?: CapabilityOperation[];\n}\n\nexport interface CapabilityDeprecation {\n contract: string;\n replacement?: string;\n sunsetAt?: string;\n}\n\nexport interface CapabilityDiscoverResponse {\n product: 'cloud' | 'enterprise';\n /** Trusted server configuration only; absent when unconfigured. */\n productVersion?: string;\n contractVersion: '2.0';\n apiContractVersions: string[];\n /** Public SDK contract labels, not npm package versions. */\n sdkContractVersions: string[];\n capabilities: PublicCapability[];\n deprecations: CapabilityDeprecation[];\n}\n\nexport interface CapabilityErrorInfo {\n status?: number;\n /** Gateway string code (including unknown future codes), or an SDK-local CAPABILITY_* code. */\n errorCode: string;\n}\n\nexport class CapabilityError extends Error {\n readonly status?: number;\n readonly errorCode: string;\n readonly retryable = false;\n\n constructor(message: string, info: CapabilityErrorInfo) {\n super(message);\n this.name = 'CapabilityError';\n this.status = info.status;\n this.errorCode = info.errorCode;\n }\n}\n\nconst contracts: Partial<Record<CapabilityCode, string[]>> = {\n 'namespace.v2': ['openapi/v2'],\n 'source': ['openapi/v2'],\n 'stream': ['openapi/v2'],\n};\n\nfunction record(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value);\n}\nfunction text(value: unknown): value is string {\n return typeof value === 'string' && value.length > 0;\n}\nfunction strings(value: unknown): value is string[] {\n return Array.isArray(value) && value.every(text) && new Set(value).size === value.length;\n}\n\n\nfunction diagnostics(value: Record<string, unknown>, fail: () => never): CapabilityDiagnostics {\n const result: CapabilityDiagnostics = {};\n for (const field of ['reasonCode', 'checkedAt', 'validUntil'] as const) {\n if (value[field] === undefined) continue;\n if (!text(value[field])) return fail();\n if (field !== 'reasonCode' && (!/^\\d{4}-\\d\\d-\\d\\d[Tt]\\d\\d:\\d\\d:\\d\\d(?:\\.\\d+)?(?:[Zz]|[+-]\\d\\d:\\d\\d)$/.test(value[field]) ||\n !Number.isFinite(Date.parse(value[field])))) return fail();\n result[field] = value[field];\n }\n if (value.retryAfterMs !== undefined) {\n if (typeof value.retryAfterMs !== 'number' || !Number.isSafeInteger(value.retryAfterMs) || value.retryAfterMs < 0) return fail();\n result.retryAfterMs = value.retryAfterMs;\n }\n if (result.validUntil !== undefined && (value.state !== 'ready' || result.checkedAt === undefined ||\n Date.parse(result.validUntil) <= Date.parse(result.checkedAt))) return fail();\n return result;\n}\n\nconst sourceOperations = ['read', 'configure', 'test', 'discover', 'enable', 'disable'];\nconst streamOperations = ['read', 'configure', 'validate', 'deploy', 'execute', 'disable'];\nconst resourceOperations: Partial<Record<CapabilityCode, readonly string[]>> = {\n source: ['test', 'discover', 'enable'],\n stream: ['deploy', 'execute'],\n 'stream.requests.invoke.v1': ['invoke'],\n};\n\nfunction operationFields(value: Record<string, unknown>, code: CapabilityCode, fail: () => never): { operations?: CapabilityOperation[] } {\n if (value.operations === undefined) return {};\n if (!Array.isArray(value.operations)) return fail();\n const operations: CapabilityOperation[] = [];\n const supported = code === 'source' ? sourceOperations : code === 'stream' ? streamOperations :\n code === 'stream.requests.invoke.v1' ? ['invoke'] : [];\n const seen = new Set<string>();\n for (const item of value.operations) {\n if (!record(item) || !text(item.operation)) return fail();\n if (!supported.includes(item.operation)) continue;\n if (seen.has(item.operation)) return fail();\n seen.add(item.operation);\n if (!text(item.reasonCode) || typeof item.requiresResourceCheck !== 'boolean' ||\n (value.state === 'disabled' && item.state !== 'disabled') ||\n (resourceOperations[code]?.includes(item.operation) && !item.requiresResourceCheck) ||\n !['disabled', 'not_supported', 'unavailable', 'readiness_unknown', 'ready'].includes(item.state as string)) return fail();\n operations.push({operation: item.operation, state: item.state as CapabilityState, reasonCode: item.reasonCode,\n requiresResourceCheck: item.requiresResourceCheck, ...diagnostics(item, fail)});\n }\n return { operations };\n}\n\n/** Validate and explicitly project the frozen contract; unknown capabilities/extension fields are ignored. */\nfunction discovery(value: unknown, status: number, accepted: Partial<Record<CapabilityCode, string[]>>): CapabilityDiscoverResponse {\n const fail = (version = false): never => {\n throw new CapabilityError(version ? 'Unsupported capability contract version.' : 'Invalid capability discovery response.', {\n status, errorCode: version ? 'CAPABILITY_UNSUPPORTED_VERSION' : 'CAPABILITY_PROTOCOL_ERROR',\n });\n };\n if (!record(value)) return fail();\n if (!text(value.product) || !text(value.contractVersion) ||\n !strings(value.apiContractVersions) || !strings(value.sdkContractVersions) ||\n !Array.isArray(value.capabilities) || !Array.isArray(value.deprecations) ||\n (value.productVersion !== undefined && !text(value.productVersion))) return fail();\n if (!['cloud', 'enterprise'].includes(value.product) || value.contractVersion !== '2.0' ||\n !value.apiContractVersions.includes('openapi/v2')) return fail(true);\n\n const capabilities: PublicCapability[] = [];\n const seen = new Set<string>();\n for (const item of value.capabilities) {\n if (!record(item) || !text(item.code)) return fail();\n if (!Object.prototype.hasOwnProperty.call(accepted, item.code)) continue;\n const code = item.code as CapabilityCode;\n if (seen.has(code)) return fail();\n seen.add(code);\n if (!text(item.reasonCode) ||\n !['disabled', 'not_supported', 'unavailable', 'readiness_unknown', 'ready'].includes(item.state as string) ||\n !strings(item.contractVersions)) return fail();\n if (accepted[code]!.some(version =>\n !(item.contractVersions as string[]).includes(version) ||\n (version.startsWith('openapi/') && !(value.apiContractVersions as string[]).includes(version)))) return fail(true);\n capabilities.push({\n code, state: item.state as CapabilityState, reasonCode: item.reasonCode,\n contractVersions: [...item.contractVersions],\n ...diagnostics(item, fail),\n ...operationFields(item, code, fail),\n });\n }\n const deprecations: CapabilityDeprecation[] = [];\n for (const item of value.deprecations) {\n if (!record(item) || !text(item.contract) ||\n (item.replacement !== undefined && !text(item.replacement)) ||\n (item.sunsetAt !== undefined && !text(item.sunsetAt))) return fail();\n deprecations.push({ contract: item.contract,\n ...(item.replacement === undefined ? {} : { replacement: item.replacement as string }),\n ...(item.sunsetAt === undefined ? {} : { sunsetAt: item.sunsetAt as string }),\n });\n }\n return {\n product: value.product as CapabilityDiscoverResponse['product'], ...(value.productVersion === undefined ? {} : { productVersion: value.productVersion as string }),\n contractVersion: '2.0', apiContractVersions: [...value.apiContractVersions],\n sdkContractVersions: [...value.sdkContractVersions],\n capabilities, deprecations,\n };\n}\n\nexport class CapabilitiesClient {\n protected transport: CloudTransport;\n protected readonly contracts: Partial<Record<CapabilityCode, string[]>> = contracts;\n\n constructor(resolveClient: () => HttpClient = getClient, config: CloudTransportConfig = {}) {\n this.transport = new CloudTransport(resolveClient, config);\n }\n\n /** Workspace comes exclusively from the authenticated key. One POST, no automatic retries or caching. */\n async discover(): Promise<CapabilityDiscoverResponse> {\n if (arguments.length !== 0) throw new TypeError('Tier0 SDK: capability discovery accepts no arguments.');\n let response: Awaited<ReturnType<CloudTransport['post']>>;\n try {\n response = await this.transport.post('/openapi/v2/capabilities/discover', {});\n } catch (error) {\n if (!(error instanceof CloudTransportError)) throw error;\n throw new CapabilityError('Capability discovery HTTP exchange failed.', {\n status: error.status, errorCode: `CAPABILITY_${error.kind}`,\n });\n }\n const { value, status, ok } = response;\n if (!ok) {\n if (!record(value) || !text(value.errorCode) || typeof value.message !== 'string') {\n throw new CapabilityError('Invalid capability error response.', { status, errorCode: 'CAPABILITY_PROTOCOL_ERROR' });\n }\n throw new CapabilityError(value.message, { status, errorCode: value.errorCode });\n }\n return discovery(value, status, this.contracts);\n }\n}\n\n/** Uses configureClient()/runtime environment, resolving the host and workspace key on every call. */\nexport const capabilities = new CapabilitiesClient();\n"]}
1
+ {"version":3,"file":"capabilities.js","sourceRoot":"","sources":["../../src/capabilities.ts"],"names":[],"mappings":";;;AAAA,6DAAsG;AACtG,mDAAiE;AAoDjE,MA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CloudTransport, CloudTransportError, type CloudTransportConfig } from './cloud-transport.js';\nimport { getClient, type HttpClient } from './openapi/client.js';\n\nexport type CapabilityCode = 'namespace.v2' | 'source' | 'stream' | 'stream.requests.v1' | 'stream.requests.invoke.v1';\nexport type CapabilityState = 'disabled' | 'not_supported' | 'unavailable' | 'readiness_unknown' | 'ready';\n\nexport interface CapabilityDiagnostics {\n reasonCode?: string;\n checkedAt?: string;\n validUntil?: string;\n retryAfterMs?: number;\n}\n\nexport interface CapabilityOperation extends CapabilityDiagnostics {\n operation: string;\n state: CapabilityState;\n reasonCode: string;\n requiresResourceCheck: boolean;\n}\n\nexport interface PublicCapability extends CapabilityDiagnostics {\n code: CapabilityCode;\n /** Discovery does not grant access to a resource or confirm a successful run. */\n state: CapabilityState;\n reasonCode: string;\n contractVersions: string[];\n operations?: CapabilityOperation[];\n}\n\nexport interface CapabilityDeprecation {\n contract: string;\n replacement?: string;\n sunsetAt?: string;\n}\n\nexport interface CapabilityDiscoverResponse {\n product: 'cloud' | 'enterprise';\n /** Trusted server configuration only; absent when unconfigured. */\n productVersion?: string;\n contractVersion: '2.0';\n apiContractVersions: string[];\n /** Public SDK contract labels, not npm package versions. */\n sdkContractVersions: string[];\n capabilities: PublicCapability[];\n deprecations: CapabilityDeprecation[];\n}\n\nexport interface CapabilityErrorInfo {\n status?: number;\n /** Gateway string code (including unknown future codes), or an SDK-local CAPABILITY_* code. */\n errorCode: string;\n}\n\nexport class CapabilityError extends Error {\n readonly status?: number;\n readonly errorCode: string;\n readonly retryable = false;\n\n constructor(message: string, info: CapabilityErrorInfo) {\n super(message);\n this.name = 'CapabilityError';\n this.status = info.status;\n this.errorCode = info.errorCode;\n }\n}\n\nconst contracts: Partial<Record<CapabilityCode, string[]>> = {\n 'namespace.v2': ['openapi/v2'],\n 'source': ['openapi/v2'],\n 'stream': ['openapi/v2'],\n};\n\nfunction record(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value);\n}\nfunction text(value: unknown): value is string {\n return typeof value === 'string' && value.length > 0;\n}\nfunction strings(value: unknown): value is string[] {\n return Array.isArray(value) && value.every(text) && new Set(value).size === value.length;\n}\n\n\nfunction diagnostics(value: Record<string, unknown>, fail: () => never): CapabilityDiagnostics {\n const result: CapabilityDiagnostics = {};\n for (const field of ['reasonCode', 'checkedAt', 'validUntil'] as const) {\n if (value[field] === undefined) continue;\n if (!text(value[field])) return fail();\n if (field !== 'reasonCode' && (!/^\\d{4}-\\d\\d-\\d\\d[Tt]\\d\\d:\\d\\d:\\d\\d(?:\\.\\d+)?(?:[Zz]|[+-]\\d\\d:\\d\\d)$/.test(value[field]) ||\n !Number.isFinite(Date.parse(value[field])))) return fail();\n result[field] = value[field];\n }\n if (value.retryAfterMs !== undefined) {\n if (typeof value.retryAfterMs !== 'number' || !Number.isSafeInteger(value.retryAfterMs) || value.retryAfterMs < 0) return fail();\n result.retryAfterMs = value.retryAfterMs;\n }\n if (result.validUntil !== undefined && (value.state !== 'ready' || result.checkedAt === undefined ||\n Date.parse(result.validUntil) <= Date.parse(result.checkedAt))) return fail();\n return result;\n}\n\nconst sourceOperations = ['read', 'configure', 'test', 'discover'];\nconst enterpriseSourceOperations = [...sourceOperations, 'enable', 'disable'];\nconst streamOperations = ['read', 'configure', 'validate', 'deploy', 'execute', 'disable'];\nconst resourceOperations: Partial<Record<CapabilityCode, readonly string[]>> = {\n source: ['test', 'discover', 'enable'],\n stream: ['deploy', 'execute'],\n 'stream.requests.invoke.v1': ['invoke'],\n};\n\nfunction operationFields(value: Record<string, unknown>, code: CapabilityCode, product: string, fail: () => never): { operations?: CapabilityOperation[] } {\n if (value.operations === undefined) return {};\n if (!Array.isArray(value.operations)) return fail();\n const operations: CapabilityOperation[] = [];\n const supported = code === 'source' ? (product === 'cloud' ? sourceOperations : enterpriseSourceOperations) : code === 'stream' ? streamOperations :\n code === 'stream.requests.invoke.v1' ? ['invoke'] : [];\n const seen = new Set<string>();\n for (const item of value.operations) {\n if (!record(item) || !text(item.operation)) return fail();\n if (product === 'cloud' && code === 'source' && ['enable', 'disable'].includes(item.operation)) return fail();\n if (!supported.includes(item.operation)) continue;\n if (seen.has(item.operation)) return fail();\n seen.add(item.operation);\n if (!text(item.reasonCode) || typeof item.requiresResourceCheck !== 'boolean' ||\n (value.state === 'disabled' && item.state !== 'disabled') ||\n (resourceOperations[code]?.includes(item.operation) && !item.requiresResourceCheck) ||\n !['disabled', 'not_supported', 'unavailable', 'readiness_unknown', 'ready'].includes(item.state as string)) return fail();\n operations.push({operation: item.operation, state: item.state as CapabilityState, reasonCode: item.reasonCode,\n requiresResourceCheck: item.requiresResourceCheck, ...diagnostics(item, fail)});\n }\n return { operations };\n}\n\n/** Validate and explicitly project the frozen contract; unknown capabilities/extension fields are ignored. */\nfunction discovery(value: unknown, status: number, accepted: Partial<Record<CapabilityCode, string[]>>): CapabilityDiscoverResponse {\n const fail = (version = false): never => {\n throw new CapabilityError(version ? 'Unsupported capability contract version.' : 'Invalid capability discovery response.', {\n status, errorCode: version ? 'CAPABILITY_UNSUPPORTED_VERSION' : 'CAPABILITY_PROTOCOL_ERROR',\n });\n };\n if (!record(value)) return fail();\n if (!text(value.product) || !text(value.contractVersion) ||\n !strings(value.apiContractVersions) || !strings(value.sdkContractVersions) ||\n !Array.isArray(value.capabilities) || !Array.isArray(value.deprecations) ||\n (value.productVersion !== undefined && !text(value.productVersion))) return fail();\n if (!['cloud', 'enterprise'].includes(value.product) || value.contractVersion !== '2.0' ||\n !value.apiContractVersions.includes('openapi/v2')) return fail(true);\n\n const capabilities: PublicCapability[] = [];\n const seen = new Set<string>();\n for (const item of value.capabilities) {\n if (!record(item) || !text(item.code)) return fail();\n if (!Object.prototype.hasOwnProperty.call(accepted, item.code)) continue;\n const code = item.code as CapabilityCode;\n if (seen.has(code)) return fail();\n seen.add(code);\n if (!text(item.reasonCode) ||\n !['disabled', 'not_supported', 'unavailable', 'readiness_unknown', 'ready'].includes(item.state as string) ||\n !strings(item.contractVersions)) return fail();\n if (accepted[code]!.some(version =>\n !(item.contractVersions as string[]).includes(version) ||\n (version.startsWith('openapi/') && !(value.apiContractVersions as string[]).includes(version)))) return fail(true);\n capabilities.push({\n code, state: item.state as CapabilityState, reasonCode: item.reasonCode,\n contractVersions: [...item.contractVersions],\n ...diagnostics(item, fail),\n ...operationFields(item, code, value.product, fail),\n });\n }\n const deprecations: CapabilityDeprecation[] = [];\n for (const item of value.deprecations) {\n if (!record(item) || !text(item.contract) ||\n (item.replacement !== undefined && !text(item.replacement)) ||\n (item.sunsetAt !== undefined && !text(item.sunsetAt))) return fail();\n deprecations.push({ contract: item.contract,\n ...(item.replacement === undefined ? {} : { replacement: item.replacement as string }),\n ...(item.sunsetAt === undefined ? {} : { sunsetAt: item.sunsetAt as string }),\n });\n }\n return {\n product: value.product as CapabilityDiscoverResponse['product'], ...(value.productVersion === undefined ? {} : { productVersion: value.productVersion as string }),\n contractVersion: '2.0', apiContractVersions: [...value.apiContractVersions],\n sdkContractVersions: [...value.sdkContractVersions],\n capabilities, deprecations,\n };\n}\n\nexport class CapabilitiesClient {\n protected transport: CloudTransport;\n protected readonly contracts: Partial<Record<CapabilityCode, string[]>> = contracts;\n\n constructor(resolveClient: () => HttpClient = getClient, config: CloudTransportConfig = {}) {\n this.transport = new CloudTransport(resolveClient, config);\n }\n\n /** Workspace comes exclusively from the authenticated key. One POST, no automatic retries or caching. */\n async discover(): Promise<CapabilityDiscoverResponse> {\n if (arguments.length !== 0) throw new TypeError('Tier0 SDK: capability discovery accepts no arguments.');\n let response: Awaited<ReturnType<CloudTransport['post']>>;\n try {\n response = await this.transport.post('/openapi/v2/capabilities/discover', {});\n } catch (error) {\n if (!(error instanceof CloudTransportError)) throw error;\n throw new CapabilityError('Capability discovery HTTP exchange failed.', {\n status: error.status, errorCode: `CAPABILITY_${error.kind}`,\n });\n }\n const { value, status, ok } = response;\n if (!ok) {\n if (!record(value) || !text(value.errorCode) || typeof value.message !== 'string') {\n throw new CapabilityError('Invalid capability error response.', { status, errorCode: 'CAPABILITY_PROTOCOL_ERROR' });\n }\n throw new CapabilityError(value.message, { status, errorCode: value.errorCode });\n }\n return discovery(value, status, this.contracts);\n }\n}\n\n/** Uses configureClient()/runtime environment, resolving the host and workspace key on every call. */\nexport const capabilities = new CapabilitiesClient();\n"]}
@@ -62,6 +62,7 @@ export declare function createClient(config?: Tier0ClientConfig): Readonly<{
62
62
  status: string;
63
63
  }>;
64
64
  get: (body: import("./index.js").components["schemas"]["SdknotificationsGetRequest"], options?: import("./openapi/client.js").RequestOptions) => Promise<{
65
+ channels: import("./index.js").components["schemas"]["NotificationChannelResult"][];
65
66
  completedAt?: string;
66
67
  createdAt: string;
67
68
  errorCode?: string;
@@ -218,9 +219,6 @@ export declare function createClient(config?: Tier0ClientConfig): Readonly<{
218
219
  protocols: Readonly<{
219
220
  list: (request: import("./index.js").UnsV2SourceProtocolsListReq, options?: import("./openapi/client.js").RequestOptions) => Promise<import("./index.js").UnsV2SourceProtocolsListResponse>;
220
221
  }>;
221
- state: Readonly<{
222
- update: (request: import("./index.js").UnsV2SourceStateUpdateReq, options?: import("./openapi/client.js").RequestOptions) => Promise<import("./index.js").UnsV2SourceStateUpdateResponse>;
223
- }>;
224
222
  runtime: Readonly<{
225
223
  get: (request: import("./index.js").UnsV2SourceRuntimeGetReq, options?: import("./openapi/client.js").RequestOptions) => Promise<import("./index.js").UnsV2SourceRuntimeGetResponse>;
226
224
  }>;
@@ -340,6 +338,7 @@ export declare function createClient(config?: Tier0ClientConfig): Readonly<{
340
338
  status: string;
341
339
  }>;
342
340
  get: (body: import("./index.js").components["schemas"]["SdknotificationsGetRequest"], options?: import("./openapi/client.js").RequestOptions) => Promise<{
341
+ channels: import("./index.js").components["schemas"]["NotificationChannelResult"][];
343
342
  completedAt?: string;
344
343
  createdAt: string;
345
344
  errorCode?: string;
@@ -574,6 +573,7 @@ export declare const client: Readonly<{
574
573
  status: string;
575
574
  }>;
576
575
  get: (body: import("./index.js").components["schemas"]["SdknotificationsGetRequest"], options?: import("./openapi/client.js").RequestOptions) => Promise<{
576
+ channels: import("./index.js").components["schemas"]["NotificationChannelResult"][];
577
577
  completedAt?: string;
578
578
  createdAt: string;
579
579
  errorCode?: string;
@@ -730,9 +730,6 @@ export declare const client: Readonly<{
730
730
  protocols: Readonly<{
731
731
  list: (request: import("./index.js").UnsV2SourceProtocolsListReq, options?: import("./openapi/client.js").RequestOptions) => Promise<import("./index.js").UnsV2SourceProtocolsListResponse>;
732
732
  }>;
733
- state: Readonly<{
734
- update: (request: import("./index.js").UnsV2SourceStateUpdateReq, options?: import("./openapi/client.js").RequestOptions) => Promise<import("./index.js").UnsV2SourceStateUpdateResponse>;
735
- }>;
736
733
  runtime: Readonly<{
737
734
  get: (request: import("./index.js").UnsV2SourceRuntimeGetReq, options?: import("./openapi/client.js").RequestOptions) => Promise<import("./index.js").UnsV2SourceRuntimeGetResponse>;
738
735
  }>;
@@ -852,6 +849,7 @@ export declare const client: Readonly<{
852
849
  status: string;
853
850
  }>;
854
851
  get: (body: import("./index.js").components["schemas"]["SdknotificationsGetRequest"], options?: import("./openapi/client.js").RequestOptions) => Promise<{
852
+ channels: import("./index.js").components["schemas"]["NotificationChannelResult"][];
855
853
  completedAt?: string;
856
854
  createdAt: string;
857
855
  errorCode?: string;
@@ -195,7 +195,12 @@ function createBootstrap(config) {
195
195
  collectionIds.set(result.alias.toLowerCase(), result.collectionId);
196
196
  const notes = result.ignored ?? [];
197
197
  const annotations = notes.filter((note) => note.reason === 'ANNOTATION_NOT_APPLIED');
198
- const wider = notes.filter((note) => note.reason !== 'ANNOTATION_NOT_APPLIED');
198
+ const directories = notes.filter((note) => note.reason === 'DIRECTORY_PATH_NOT_APPLIED');
199
+ const wider = notes.filter((note) => note.reason !== 'ANNOTATION_NOT_APPLIED' && note.reason !== 'DIRECTORY_PATH_NOT_APPLIED');
200
+ if (directories.length > 0) {
201
+ logger.warn(`[data] ensure keeps existing collection locations; not applied: ${directories.map((note) => notePath(result.alias, note)).join(', ')}. ` +
202
+ 'A directoryPath declaration only places a new collection. Move existing collections explicitly through metadata operations when authorized.');
203
+ }
199
204
  if (wider.length > 0) {
200
205
  logger.info(`[data] "${result.alias}" storage is wider than this declaration (or has retired parts); ignored: ` +
201
206
  wider.map((note) => `${note.path || '(root)'} ${note.reason ?? ''}`.trim()).join(', '));
@@ -1 +1 @@
1
- {"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../../../src/data/bootstrap.ts"],"names":[],"mappings":";;;AAwGA,wDAKC;AAsDD,0CA0MC;AA7WD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,6CAAyC;AAEzC,mDAAwE;AAExE,yCAAuD;AACvD,2CAA2F;AAyD3F,MAAM,aAAa,GAAG,CAAC,CAAC;AACxB,8DAA8D;AACjD,QAAA,aAAa,GAAG,KAAK,CAAC;AAEnC,kEAAkE;AACrD,QAAA,mBAAmB,GAAG,MAAM,CAAC;AAE1C;;;;;;GAMG;AACH,SAAgB,sBAAsB,CAAC,KAAc;IACnD,OAAO,CACL,KAAK,YAAY,oBAAS;QAC1B,CAAC,KAAK,CAAC,MAAM,KAAK,yBAAc,CAAC,UAAU,IAAI,KAAK,CAAC,MAAM,KAAK,yBAAc,CAAC,QAAQ,CAAC,CACzF,CAAC;AACJ,CAAC;AAED,SAAS,iBAAiB,CAAC,CAAc,EAAE,CAAc;IACvD,IAAI,CAAC,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,CAAC,aAAa,CAAC,IAAI;QAAE,OAAO,KAAK,CAAC;IAChE,KAAK,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,CAAC;QAC1C,IAAI,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE;YAAE,OAAO,KAAK,CAAC;IACtD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,8EAA8E;AAC9E,SAAS,YAAY,CAAC,YAAoB,EAAE,KA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,CAAC,MAAM,EAAE,KAAkB,CAAC,CAAC;YAC1D,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK;gBAAE,MAAM,KAAK,CAAC;YACrD,OAAO,EAAE,EAAE,CAAC;QACd,CAAC;IACH,CAAC;IAED,KAAK,UAAU,OAAO,CAAC,UAAgC;QACrD,MAAM,KAAK,GAAG,MAAM,MAAM,EAAE,CAAC;QAC7B,MAAM,EAAE,GAAG,KAAK,CAAC,aAAa,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC;QACnE,IAAI,CAAC,EAAE,EAAE,CAAC;YACR,MAAM,IAAI,oBAAS,CAAC;gBAClB,OAAO,EAAE,eAAe,UAAU,CAAC,KAAK,kEAAkE;gBAC1G,MAAM,EAAE,GAAG;gBACX,IAAI,EAAE,wBAAwB;aAC/B,CAAC,CAAC;QACL,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC/C,CAAC","sourcesContent":["/**\n * First-access bootstrap: one batched `ensure` of every declared collection.\n *\n * - ensure only creates or widens; storage never narrows from here, so old\n * and new code may cold-start in any order.\n * - The result is memoized per client, i.e. per server process; a client\n * built from edited declarations ensures again.\n * Failures are not memoized: a later call retries (an imported App may\n * answer \"hosted App is not ready\").\n * - Seed documents are inserted only for collections ensure reports as\n * `created`: fixed `_id`s, atomic chunks of 200 with a stable idempotency\n * key per chunk, so a failed chunk (outcome unknown, or marked safe to\n * resend) is replayed after a short wait instead of duplicated. Seed writes\n * never carry a data subject.\n * - Preview throws on SCHEMA_CONFLICT and warnings so the worker sees them;\n * a deployment logs them and keeps serving with the IDs it has.\n * - Self-healing: a Preview reset hard-deletes collections, and a worker or\n * admin may archive one, while this process still holds the old IDs. A\n * record call that fails with `data.reason` COLLECTION_NOT_FOUND or\n * COLLECTION_NOT_GRANTED drops the memo, ensures again (seeding what is created)\n * and retries the call once, but only when a collection ID actually\n * changed. Rebuilds are limited to one per REBUILD_INTERVAL_MS per client;\n * APP_GRANT_REVOKED is never retried (ensure itself reports it).\n * - `ignored` notes never fail a request. ensure never writes annotations, so\n * an edited name, `.describe()` or collection description comes back as\n * ANNOTATION_NOT_APPLIED; it is logged as a warning pointing at\n * metadata:updateOne for names, or schema:replace for schema annotations.\n */\nimport { createHash } from 'node:crypto';\nimport type { DataCall } from './call.js';\nimport { assertNoWriteErrors, MAX_ATOMIC_ITEMS } from './collection.js';\nimport type { CollectionDefinition } from './define.js';\nimport { ACCESS_REASONS, DataError } from './error.js';\nimport { compileCollection, validateDocument, type CompiledCollection } from './schema.js';\n\nexport type DataEnvironment = 'preview' | 'deployed';\n\nexport interface EnsureWarning {\n code: string;\n path?: string;\n message?: string;\n}\n\nexport interface EnsureNote {\n path: string;\n reason?: string;\n message?: string;\n}\n\nexport interface EnsureResult {\n alias: string;\n collectionId: string;\n revision: number;\n status: 'created' | 'unchanged' | 'widened';\n ignored?: EnsureNote[];\n warnings?: EnsureWarning[];\n indexStatus?: unknown[];\n}\n\nexport interface EnsureConflict {\n alias: string;\n collectionId?: string;\n path?: string;\n reason?: string;\n current?: string;\n declared?: string;\n}\n\nexport interface EnsureState {\n appId?: string;\n results: EnsureResult[];\n /** Lower-cased alias → collection ID. */\n collectionIds: Map<string, string>;\n /** Declarations that conflicted in a deployment (served with their existing ID, if any). */\n conflicts: EnsureConflict[];\n}\n\nexport interface Logger {\n info(...args: unknown[]): void;\n warn(...args: unknown[]): void;\n error(...args: unknown[]): void;\n}\n\nexport interface BootstrapConfig {\n call: DataCall;\n collections: readonly CollectionDefinition[];\n environment: DataEnvironment;\n logger: Logger;\n}\n\nconst SEED_ATTEMPTS = 3;\n/** Wait before replaying seed chunk attempt n+1: n × this. */\nexport const SEED_RETRY_MS = 1_000;\n\n/** At most one self-healing rebuild per client in this window. */\nexport const REBUILD_INTERVAL_MS = 30_000;\n\n/**\n * Whether a record call failed because the collection ID it used is gone or\n * no longer granted, by UNS's stable `data.reason` code. Archive and Preview\n * purge also remove the App grant, so most stale IDs answer\n * COLLECTION_NOT_GRANTED or COLLECTION_NOT_FOUND. APP_PERMISSION_MISSING (the\n * key lacks e.g. `uns.delete`) and APP_GRANT_REVOKED are not stale IDs.\n */\nexport function isStaleCollectionError(error: unknown): boolean {\n return (\n error instanceof DataError &&\n (error.reason === ACCESS_REASONS.notGranted || error.reason === ACCESS_REASONS.notFound)\n );\n}\n\nfunction sameCollectionIds(a: EnsureState, b: EnsureState): boolean {\n if (a.collectionIds.size !== b.collectionIds.size) return false;\n for (const [alias, id] of a.collectionIds) {\n if (b.collectionIds.get(alias) !== id) return false;\n }\n return true;\n}\n\n/** Idempotency key of one seed chunk (UNS keys are at most 96 characters). */\nfunction seedChunkKey(collectionId: string, chunk: number): string {\n return `seed-${createHash('sha256').update(collectionId).digest('hex').slice(0, 40)}-${chunk}`;\n}\n\n/** What to do about each SCHEMA_CONFLICT reason (CloudAppCollectionConflict.reason). */\nconst CONFLICT_HINTS: Record<string, string> = {\n TYPE_CONFLICT: \"a field's type cannot change; use a new field name and backfill with updateMany\",\n LIMIT_EXCEEDED: 'the joined schema exceeds a UNS limit (depth, properties, enum values or indexes); split the structure',\n OPEN_OBJECT: 'every object must be closed; declare it with z.object',\n ALIAS_IN_USE: 'the alias belongs to another collection; pick another alias',\n NAME_IN_USE: 'the business name belongs to another collection in this folder; pick another name and keep the stable alias',\n INVALID_INDEX: 'fix the index declaration',\n JOIN_UNSUPPORTED:\n 'the stored schema holds a node the declaration cannot widen (e.g. enum values outside its range); repair it with schema:replace via the tier0 MCP',\n DATA_RULE_FIELD:\n 'an employee data rule (ownerField / stateField) uses this field; keep it required with the same type, or ask a workspace admin to change the rule',\n};\n\nfunction describeConflicts(conflicts: EnsureConflict[]): string {\n return conflicts\n .map((conflict) => {\n const where = `${conflict.alias}${conflict.path ? `.${conflict.path}` : ''}`;\n const types = conflict.current || conflict.declared ? ` (stored ${conflict.current ?? '?'}, declared ${conflict.declared ?? '?'})` : '';\n const hint = conflict.reason && CONFLICT_HINTS[conflict.reason] ? ` — ${CONFLICT_HINTS[conflict.reason]}` : '';\n return ` - ${where}: ${conflict.reason ?? 'conflict'}${types}${hint}`;\n })\n .join('\\n');\n}\n\nfunction notePath(alias: string, note: EnsureNote): string {\n return note.path ? `${alias}.${note.path}` : alias;\n}\n\nfunction describeWarnings(results: EnsureResult[]): string {\n return results\n .flatMap((result) =>\n (result.warnings ?? []).map(\n (warning) => ` - ${result.alias}${warning.path ? `.${warning.path}` : ''}: ${warning.code}${warning.message ? ` — ${warning.message}` : ''}`,\n ),\n )\n .join('\\n');\n}\n\nexport function createBootstrap(config: BootstrapConfig) {\n const { call, collections, environment, logger } = config;\n let pending: Promise<EnsureState> | null = null;\n let lastRebuild = Number.NEGATIVE_INFINITY;\n\n async function requestEnsure(items: CompiledCollection[]) {\n // ensure is App-level work: it never carries the employee data subject.\n return call<{ appId?: string; results: EnsureResult[] }>('appCollectionsEnsure', { collections: items });\n }\n\n async function insertSeedChunk(collectionId: string, chunk: number, documents: { id?: string; data: Record<string, unknown> }[]) {\n for (let attempt = 1; ; attempt += 1) {\n try {\n const result = await call<{ writeErrors?: Record<string, unknown>[] }>('recordBulkWrite', {\n collectionId,\n operations: documents.map(document => ({ insertOne: document })),\n ordered: true,\n atomic: true,\n idempotencyKey: seedChunkKey(collectionId, chunk),\n });\n assertNoWriteErrors(result, 'seed');\n return;\n } catch (error) {\n // The chunk key is stable, so replaying is safe even when the outcome is unknown: a chunk\n // that did commit replays its receipt. Wait first, so a write still running can finish.\n if (!(error instanceof DataError) || !(error.outcomeUnknown || error.retry.safe) || attempt >= SEED_ATTEMPTS) throw error;\n await new Promise((resolve) => setTimeout(resolve, SEED_RETRY_MS * attempt));\n }\n }\n }\n\n async function writeSeeds(results: EnsureResult[]) {\n for (const result of results) {\n if (result.status !== 'created') continue;\n const definition = collections.find((candidate) => candidate.alias.toLowerCase() === result.alias.toLowerCase());\n const seed = definition?.seed;\n if (!definition || !seed || seed.length === 0) continue;\n try {\n const documents = seed.map((document) => {\n const { id, data } = validateDocument(definition, document as Record<string, unknown>);\n return { id, data };\n });\n for (let start = 0; start < documents.length; start += MAX_ATOMIC_ITEMS) {\n await insertSeedChunk(result.collectionId, start / MAX_ATOMIC_ITEMS, documents.slice(start, start + MAX_ATOMIC_ITEMS));\n }\n logger.info(`[data] seeded ${documents.length} record(s) into \"${result.alias}\"`);\n } catch (error) {\n // The collection exists now; a later ensure reports \"unchanged\" and\n // never re-seeds, so the failure must be visible in the server log.\n logger.error(`[data] seed for new collection \"${result.alias}\" failed; it keeps the chunks written so far:`, error);\n }\n }\n }\n\n async function run(payload: CompiledCollection[]): Promise<EnsureState> {\n const conflicts: EnsureConflict[] = [];\n let remaining = payload;\n let response: { appId?: string; results: EnsureResult[] } = { results: [] };\n while (remaining.length > 0) {\n try {\n response = await requestEnsure(remaining);\n break;\n } catch (error) {\n if (!(error instanceof DataError) || error.code !== 'SCHEMA_CONFLICT') throw error;\n const reported = Array.isArray(error.details.conflicts) ? (error.details.conflicts as EnsureConflict[]) : [];\n const summary = describeConflicts(reported);\n if (environment === 'preview') {\n throw new DataError({\n message:\n `The collection declarations conflict with the stored collection schemas; nothing was changed:\\n${summary}`,\n status: 409,\n code: 'SCHEMA_CONFLICT',\n upstreamStatus: error.upstreamStatus,\n requestId: error.requestId,\n execution: error.execution,\n attempts: error.attempts,\n details: error.details,\n });\n }\n // A deployment keeps serving: drop the conflicting declarations (the\n // whole batch was rejected) and ensure the rest again.\n const conflicting = new Set(reported.map((conflict) => conflict.alias.toLowerCase()));\n const next = remaining.filter((item) => !conflicting.has(item.alias.toLowerCase()));\n if (next.length === remaining.length) throw error;\n logger.error(`[data] ensure: declarations conflict with stored schemas; serving them unchanged:\\n${summary}`);\n conflicts.push(...reported);\n remaining = next;\n }\n }\n\n const results = response.results ?? [];\n const collectionIds = new Map<string, string>();\n for (const conflict of conflicts) {\n if (conflict.collectionId) collectionIds.set(conflict.alias.toLowerCase(), conflict.collectionId);\n }\n for (const result of results) {\n collectionIds.set(result.alias.toLowerCase(), result.collectionId);\n const notes = result.ignored ?? [];\n const annotations = notes.filter((note) => note.reason === 'ANNOTATION_NOT_APPLIED');\n const wider = notes.filter((note) => note.reason !== 'ANNOTATION_NOT_APPLIED');\n if (wider.length > 0) {\n logger.info(\n `[data] \"${result.alias}\" storage is wider than this declaration (or has retired parts); ignored: ` +\n wider.map((note) => `${note.path || '(root)'} ${note.reason ?? ''}`.trim()).join(', '),\n );\n }\n if (annotations.length > 0) {\n // A hint, not an error: storage keeps its annotations and serves as before.\n logger.warn(\n `[data] ensure does not change stored names/titles/descriptions; not applied: ${annotations.map((note) => notePath(result.alias, note)).join(', ')}. ` +\n `Rename with metadata:updateOne via the tier0 MCP; apply schema annotations with schema:replace, selecting alias, recordSchema, indexes and description from compileCollection(...).`,\n );\n }\n }\n\n await writeSeeds(results);\n\n const warnings = describeWarnings(results);\n if (warnings) {\n if (environment === 'preview') {\n throw new DataError({\n message:\n `Collections were ensured with warnings:\\n${warnings}\\n` +\n 'REQUIRED_NOT_ENFORCED: make the field optional again or narrow explicitly with schema:replace via the tier0 MCP. ' +\n 'INDEX_BUILD_FAILED: fix the duplicate or invalid data, then the next ensure retries the index.',\n status: 500,\n code: 'SCHEMA_WARNINGS',\n // ensure did apply; only the warnings fail the Preview request.\n execution: 'committed',\n details: { results },\n });\n }\n logger.warn(`[data] ensure warnings:\\n${warnings}`);\n }\n\n return { appId: response.appId, results, collectionIds, conflicts };\n }\n\n /** Ensures every declaration once per client; a failure is forgotten so the next call retries. */\n function ensure(): Promise<EnsureState> {\n pending ??= Promise.resolve()\n .then(() => run(collections.map(compileCollection)))\n .catch((error) => {\n pending = null;\n throw error;\n });\n return pending;\n }\n\n /**\n * Re-ensures after `before` turned out stale. Joins a rebuild already in\n * flight; otherwise rebuilds at most once per REBUILD_INTERVAL_MS. Resolves\n * whether any collection ID changed; ensure errors (APP_GRANT_REVOKED, …)\n * propagate.\n */\n async function rebuild(before: EnsureState, cause: DataError): Promise<boolean> {\n // Only owned IDs can be recovered by ensure; shared references must keep their original error.\n const failedId = cause.details.collectionId;\n if (typeof failedId === 'string' && ![...before.collectionIds.values()].includes(failedId)) return false;\n const current = ensure();\n const latest = await current;\n if (latest !== before) return !sameCollectionIds(before, latest);\n if (pending !== current) return !sameCollectionIds(before, await ensure());\n const now = Date.now();\n if (now - lastRebuild < REBUILD_INTERVAL_MS) return false;\n lastRebuild = now;\n logger.warn(`[data] a collection ID is no longer usable (${cause.code}: ${cause.message}); ensuring collections again`);\n pending = null;\n return !sameCollectionIds(before, await ensure());\n }\n\n /**\n * Runs one data call; when it fails on a stale collection ID, rebuilds and\n * runs it once more if an ID changed. `retry: false` (calls continuing a\n * cursor or resumeAfter token of the old collection) only rebuilds.\n */\n async function recover<R>(fn: () => Promise<R>, options: { retry?: boolean } = {}): Promise<R> {\n const before = await ensure();\n try {\n return await fn();\n } catch (error) {\n if (!isStaleCollectionError(error)) throw error;\n const changed = await rebuild(before, error as DataError);\n if (!changed || options.retry === false) throw error;\n return fn();\n }\n }\n\n async function resolve(definition: CollectionDefinition): Promise<string> {\n const state = await ensure();\n const id = state.collectionIds.get(definition.alias.toLowerCase());\n if (!id) {\n throw new DataError({\n message: `Collection \"${definition.alias}\" is unavailable: ensure did not return it (see the server log).`,\n status: 503,\n code: 'COLLECTION_UNAVAILABLE',\n });\n }\n return id;\n }\n\n return { ensure, resolve, recover, rebuild };\n}\n"]}
1
+ {"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../../../src/data/bootstrap.ts"],"names":[],"mappings":";;;AAwGA,wDAKC;AAsDD,0CAiNC;AApXD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,6CAAyC;AAEzC,mDAAwE;AAExE,yCAAuD;AACvD,2CAA2F;AAyD3F,MAAM,aAAa,GAAG,CAAC,CAAC;AACxB,8DAA8D;AACjD,Q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,OAAO,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK;gBAAE,MAAM,KAAK,CAAC;YACrD,OAAO,EAAE,EAAE,CAAC;QACd,CAAC;IACH,CAAC;IAED,KAAK,UAAU,OAAO,CAAC,UAAgC;QACrD,MAAM,KAAK,GAAG,MAAM,MAAM,EAAE,CAAC;QAC7B,MAAM,EAAE,GAAG,KAAK,CAAC,aAAa,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC;QACnE,IAAI,CAAC,EAAE,EAAE,CAAC;YACR,MAAM,IAAI,oBAAS,CAAC;gBAClB,OAAO,EAAE,eAAe,UAAU,CAAC,KAAK,kEAAkE;gBAC1G,MAAM,EAAE,GAAG;gBACX,IAAI,EAAE,wBAAwB;aAC/B,CAAC,CAAC;QACL,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC/C,CAAC","sourcesContent":["/**\n * First-access bootstrap: one batched `ensure` of every declared collection.\n *\n * - ensure only creates or widens; storage never narrows from here, so old\n * and new code may cold-start in any order.\n * - The result is memoized per client, i.e. per server process; a client\n * built from edited declarations ensures again.\n * Failures are not memoized: a later call retries (an imported App may\n * answer \"hosted App is not ready\").\n * - Seed documents are inserted only for collections ensure reports as\n * `created`: fixed `_id`s, atomic chunks of 200 with a stable idempotency\n * key per chunk, so a failed chunk (outcome unknown, or marked safe to\n * resend) is replayed after a short wait instead of duplicated. Seed writes\n * never carry a data subject.\n * - Preview throws on SCHEMA_CONFLICT and warnings so the worker sees them;\n * a deployment logs them and keeps serving with the IDs it has.\n * - Self-healing: a Preview reset hard-deletes collections, and a worker or\n * admin may archive one, while this process still holds the old IDs. A\n * record call that fails with `data.reason` COLLECTION_NOT_FOUND or\n * COLLECTION_NOT_GRANTED drops the memo, ensures again (seeding what is created)\n * and retries the call once, but only when a collection ID actually\n * changed. Rebuilds are limited to one per REBUILD_INTERVAL_MS per client;\n * APP_GRANT_REVOKED is never retried (ensure itself reports it).\n * - `ignored` notes never fail a request. ensure never writes annotations, so\n * an edited name, `.describe()` or collection description comes back as\n * ANNOTATION_NOT_APPLIED; it is logged as a warning pointing at\n * metadata:updateOne for names, or schema:replace for schema annotations.\n */\nimport { createHash } from 'node:crypto';\nimport type { DataCall } from './call.js';\nimport { assertNoWriteErrors, MAX_ATOMIC_ITEMS } from './collection.js';\nimport type { CollectionDefinition } from './define.js';\nimport { ACCESS_REASONS, DataError } from './error.js';\nimport { compileCollection, validateDocument, type CompiledCollection } from './schema.js';\n\nexport type DataEnvironment = 'preview' | 'deployed';\n\nexport interface EnsureWarning {\n code: string;\n path?: string;\n message?: string;\n}\n\nexport interface EnsureNote {\n path: string;\n reason?: string;\n message?: string;\n}\n\nexport interface EnsureResult {\n alias: string;\n collectionId: string;\n revision: number;\n status: 'created' | 'unchanged' | 'widened';\n ignored?: EnsureNote[];\n warnings?: EnsureWarning[];\n indexStatus?: unknown[];\n}\n\nexport interface EnsureConflict {\n alias: string;\n collectionId?: string;\n path?: string;\n reason?: string;\n current?: string;\n declared?: string;\n}\n\nexport interface EnsureState {\n appId?: string;\n results: EnsureResult[];\n /** Lower-cased alias → collection ID. */\n collectionIds: Map<string, string>;\n /** Declarations that conflicted in a deployment (served with their existing ID, if any). */\n conflicts: EnsureConflict[];\n}\n\nexport interface Logger {\n info(...args: unknown[]): void;\n warn(...args: unknown[]): void;\n error(...args: unknown[]): void;\n}\n\nexport interface BootstrapConfig {\n call: DataCall;\n collections: readonly CollectionDefinition[];\n environment: DataEnvironment;\n logger: Logger;\n}\n\nconst SEED_ATTEMPTS = 3;\n/** Wait before replaying seed chunk attempt n+1: n × this. */\nexport const SEED_RETRY_MS = 1_000;\n\n/** At most one self-healing rebuild per client in this window. */\nexport const REBUILD_INTERVAL_MS = 30_000;\n\n/**\n * Whether a record call failed because the collection ID it used is gone or\n * no longer granted, by UNS's stable `data.reason` code. Archive and Preview\n * purge also remove the App grant, so most stale IDs answer\n * COLLECTION_NOT_GRANTED or COLLECTION_NOT_FOUND. APP_PERMISSION_MISSING (the\n * key lacks e.g. `uns.delete`) and APP_GRANT_REVOKED are not stale IDs.\n */\nexport function isStaleCollectionError(error: unknown): boolean {\n return (\n error instanceof DataError &&\n (error.reason === ACCESS_REASONS.notGranted || error.reason === ACCESS_REASONS.notFound)\n );\n}\n\nfunction sameCollectionIds(a: EnsureState, b: EnsureState): boolean {\n if (a.collectionIds.size !== b.collectionIds.size) return false;\n for (const [alias, id] of a.collectionIds) {\n if (b.collectionIds.get(alias) !== id) return false;\n }\n return true;\n}\n\n/** Idempotency key of one seed chunk (UNS keys are at most 96 characters). */\nfunction seedChunkKey(collectionId: string, chunk: number): string {\n return `seed-${createHash('sha256').update(collectionId).digest('hex').slice(0, 40)}-${chunk}`;\n}\n\n/** What to do about each SCHEMA_CONFLICT reason (CloudAppCollectionConflict.reason). */\nconst CONFLICT_HINTS: Record<string, string> = {\n TYPE_CONFLICT: \"a field's type cannot change; use a new field name and backfill with updateMany\",\n LIMIT_EXCEEDED: 'the joined schema exceeds a UNS limit (depth, properties, enum values or indexes); split the structure',\n OPEN_OBJECT: 'every object must be closed; declare it with z.object',\n ALIAS_IN_USE: 'the alias belongs to another collection; pick another alias',\n NAME_IN_USE: 'the business name belongs to another collection in this folder; pick another name and keep the stable alias',\n INVALID_INDEX: 'fix the index declaration',\n JOIN_UNSUPPORTED:\n 'the stored schema holds a node the declaration cannot widen (e.g. enum values outside its range); repair it with schema:replace via the tier0 MCP',\n DATA_RULE_FIELD:\n 'an employee data rule (ownerField / stateField) uses this field; keep it required with the same type, or ask a workspace admin to change the rule',\n};\n\nfunction describeConflicts(conflicts: EnsureConflict[]): string {\n return conflicts\n .map((conflict) => {\n const where = `${conflict.alias}${conflict.path ? `.${conflict.path}` : ''}`;\n const types = conflict.current || conflict.declared ? ` (stored ${conflict.current ?? '?'}, declared ${conflict.declared ?? '?'})` : '';\n const hint = conflict.reason && CONFLICT_HINTS[conflict.reason] ? ` — ${CONFLICT_HINTS[conflict.reason]}` : '';\n return ` - ${where}: ${conflict.reason ?? 'conflict'}${types}${hint}`;\n })\n .join('\\n');\n}\n\nfunction notePath(alias: string, note: EnsureNote): string {\n return note.path ? `${alias}.${note.path}` : alias;\n}\n\nfunction describeWarnings(results: EnsureResult[]): string {\n return results\n .flatMap((result) =>\n (result.warnings ?? []).map(\n (warning) => ` - ${result.alias}${warning.path ? `.${warning.path}` : ''}: ${warning.code}${warning.message ? ` — ${warning.message}` : ''}`,\n ),\n )\n .join('\\n');\n}\n\nexport function createBootstrap(config: BootstrapConfig) {\n const { call, collections, environment, logger } = config;\n let pending: Promise<EnsureState> | null = null;\n let lastRebuild = Number.NEGATIVE_INFINITY;\n\n async function requestEnsure(items: CompiledCollection[]) {\n // ensure is App-level work: it never carries the employee data subject.\n return call<{ appId?: string; results: EnsureResult[] }>('appCollectionsEnsure', { collections: items });\n }\n\n async function insertSeedChunk(collectionId: string, chunk: number, documents: { id?: string; data: Record<string, unknown> }[]) {\n for (let attempt = 1; ; attempt += 1) {\n try {\n const result = await call<{ writeErrors?: Record<string, unknown>[] }>('recordBulkWrite', {\n collectionId,\n operations: documents.map(document => ({ insertOne: document })),\n ordered: true,\n atomic: true,\n idempotencyKey: seedChunkKey(collectionId, chunk),\n });\n assertNoWriteErrors(result, 'seed');\n return;\n } catch (error) {\n // The chunk key is stable, so replaying is safe even when the outcome is unknown: a chunk\n // that did commit replays its receipt. Wait first, so a write still running can finish.\n if (!(error instanceof DataError) || !(error.outcomeUnknown || error.retry.safe) || attempt >= SEED_ATTEMPTS) throw error;\n await new Promise((resolve) => setTimeout(resolve, SEED_RETRY_MS * attempt));\n }\n }\n }\n\n async function writeSeeds(results: EnsureResult[]) {\n for (const result of results) {\n if (result.status !== 'created') continue;\n const definition = collections.find((candidate) => candidate.alias.toLowerCase() === result.alias.toLowerCase());\n const seed = definition?.seed;\n if (!definition || !seed || seed.length === 0) continue;\n try {\n const documents = seed.map((document) => {\n const { id, data } = validateDocument(definition, document as Record<string, unknown>);\n return { id, data };\n });\n for (let start = 0; start < documents.length; start += MAX_ATOMIC_ITEMS) {\n await insertSeedChunk(result.collectionId, start / MAX_ATOMIC_ITEMS, documents.slice(start, start + MAX_ATOMIC_ITEMS));\n }\n logger.info(`[data] seeded ${documents.length} record(s) into \"${result.alias}\"`);\n } catch (error) {\n // The collection exists now; a later ensure reports \"unchanged\" and\n // never re-seeds, so the failure must be visible in the server log.\n logger.error(`[data] seed for new collection \"${result.alias}\" failed; it keeps the chunks written so far:`, error);\n }\n }\n }\n\n async function run(payload: CompiledCollection[]): Promise<EnsureState> {\n const conflicts: EnsureConflict[] = [];\n let remaining = payload;\n let response: { appId?: string; results: EnsureResult[] } = { results: [] };\n while (remaining.length > 0) {\n try {\n response = await requestEnsure(remaining);\n break;\n } catch (error) {\n if (!(error instanceof DataError) || error.code !== 'SCHEMA_CONFLICT') throw error;\n const reported = Array.isArray(error.details.conflicts) ? (error.details.conflicts as EnsureConflict[]) : [];\n const summary = describeConflicts(reported);\n if (environment === 'preview') {\n throw new DataError({\n message:\n `The collection declarations conflict with the stored collection schemas; nothing was changed:\\n${summary}`,\n status: 409,\n code: 'SCHEMA_CONFLICT',\n upstreamStatus: error.upstreamStatus,\n requestId: error.requestId,\n execution: error.execution,\n attempts: error.attempts,\n details: error.details,\n });\n }\n // A deployment keeps serving: drop the conflicting declarations (the\n // whole batch was rejected) and ensure the rest again.\n const conflicting = new Set(reported.map((conflict) => conflict.alias.toLowerCase()));\n const next = remaining.filter((item) => !conflicting.has(item.alias.toLowerCase()));\n if (next.length === remaining.length) throw error;\n logger.error(`[data] ensure: declarations conflict with stored schemas; serving them unchanged:\\n${summary}`);\n conflicts.push(...reported);\n remaining = next;\n }\n }\n\n const results = response.results ?? [];\n const collectionIds = new Map<string, string>();\n for (const conflict of conflicts) {\n if (conflict.collectionId) collectionIds.set(conflict.alias.toLowerCase(), conflict.collectionId);\n }\n for (const result of results) {\n collectionIds.set(result.alias.toLowerCase(), result.collectionId);\n const notes = result.ignored ?? [];\n const annotations = notes.filter((note) => note.reason === 'ANNOTATION_NOT_APPLIED');\n const directories = notes.filter((note) => note.reason === 'DIRECTORY_PATH_NOT_APPLIED');\n const wider = notes.filter((note) => note.reason !== 'ANNOTATION_NOT_APPLIED' && note.reason !== 'DIRECTORY_PATH_NOT_APPLIED');\n if (directories.length > 0) {\n logger.warn(\n `[data] ensure keeps existing collection locations; not applied: ${directories.map((note) => notePath(result.alias, note)).join(', ')}. ` +\n 'A directoryPath declaration only places a new collection. Move existing collections explicitly through metadata operations when authorized.',\n );\n }\n if (wider.length > 0) {\n logger.info(\n `[data] \"${result.alias}\" storage is wider than this declaration (or has retired parts); ignored: ` +\n wider.map((note) => `${note.path || '(root)'} ${note.reason ?? ''}`.trim()).join(', '),\n );\n }\n if (annotations.length > 0) {\n // A hint, not an error: storage keeps its annotations and serves as before.\n logger.warn(\n `[data] ensure does not change stored names/titles/descriptions; not applied: ${annotations.map((note) => notePath(result.alias, note)).join(', ')}. ` +\n `Rename with metadata:updateOne via the tier0 MCP; apply schema annotations with schema:replace, selecting alias, recordSchema, indexes and description from compileCollection(...).`,\n );\n }\n }\n\n await writeSeeds(results);\n\n const warnings = describeWarnings(results);\n if (warnings) {\n if (environment === 'preview') {\n throw new DataError({\n message:\n `Collections were ensured with warnings:\\n${warnings}\\n` +\n 'REQUIRED_NOT_ENFORCED: make the field optional again or narrow explicitly with schema:replace via the tier0 MCP. ' +\n 'INDEX_BUILD_FAILED: fix the duplicate or invalid data, then the next ensure retries the index.',\n status: 500,\n code: 'SCHEMA_WARNINGS',\n // ensure did apply; only the warnings fail the Preview request.\n execution: 'committed',\n details: { results },\n });\n }\n logger.warn(`[data] ensure warnings:\\n${warnings}`);\n }\n\n return { appId: response.appId, results, collectionIds, conflicts };\n }\n\n /** Ensures every declaration once per client; a failure is forgotten so the next call retries. */\n function ensure(): Promise<EnsureState> {\n pending ??= Promise.resolve()\n .then(() => run(collections.map(compileCollection)))\n .catch((error) => {\n pending = null;\n throw error;\n });\n return pending;\n }\n\n /**\n * Re-ensures after `before` turned out stale. Joins a rebuild already in\n * flight; otherwise rebuilds at most once per REBUILD_INTERVAL_MS. Resolves\n * whether any collection ID changed; ensure errors (APP_GRANT_REVOKED, …)\n * propagate.\n */\n async function rebuild(before: EnsureState, cause: DataError): Promise<boolean> {\n // Only owned IDs can be recovered by ensure; shared references must keep their original error.\n const failedId = cause.details.collectionId;\n if (typeof failedId === 'string' && ![...before.collectionIds.values()].includes(failedId)) return false;\n const current = ensure();\n const latest = await current;\n if (latest !== before) return !sameCollectionIds(before, latest);\n if (pending !== current) return !sameCollectionIds(before, await ensure());\n const now = Date.now();\n if (now - lastRebuild < REBUILD_INTERVAL_MS) return false;\n lastRebuild = now;\n logger.warn(`[data] a collection ID is no longer usable (${cause.code}: ${cause.message}); ensuring collections again`);\n pending = null;\n return !sameCollectionIds(before, await ensure());\n }\n\n /**\n * Runs one data call; when it fails on a stale collection ID, rebuilds and\n * runs it once more if an ID changed. `retry: false` (calls continuing a\n * cursor or resumeAfter token of the old collection) only rebuilds.\n */\n async function recover<R>(fn: () => Promise<R>, options: { retry?: boolean } = {}): Promise<R> {\n const before = await ensure();\n try {\n return await fn();\n } catch (error) {\n if (!isStaleCollectionError(error)) throw error;\n const changed = await rebuild(before, error as DataError);\n if (!changed || options.retry === false) throw error;\n return fn();\n }\n }\n\n async function resolve(definition: CollectionDefinition): Promise<string> {\n const state = await ensure();\n const id = state.collectionIds.get(definition.alias.toLowerCase());\n if (!id) {\n throw new DataError({\n message: `Collection \"${definition.alias}\" is unavailable: ensure did not return it (see the server log).`,\n status: 503,\n code: 'COLLECTION_UNAVAILABLE',\n });\n }\n return id;\n }\n\n return { ensure, resolve, recover, rebuild };\n}\n"]}
@@ -18,6 +18,8 @@ export declare const MAX_COLLECTIONS = 64;
18
18
  export declare const MAX_DESCRIPTION_LENGTH = 512;
19
19
  /** UNS: business-facing collection name length in Unicode characters. */
20
20
  export declare const MAX_NAME_LENGTH = 63;
21
+ /** Business directory paths may omit unknown ISA-95 levels. */
22
+ export declare const MAX_DIRECTORY_DEPTH = 16;
21
23
  /** Seeds are inserted in atomic chunks of 200 during the first request; keep them a baseline, not a data import. */
22
24
  export declare const MAX_SEED_DOCUMENTS = 1000;
23
25
  /** Same shape as collection metadata indexes, limited to record indexes. */
@@ -53,6 +55,8 @@ export interface CollectionDefinition<S extends CollectionSchema = CollectionSch
53
55
  readonly alias: string;
54
56
  /** Business-facing name, independent of the stable App alias. */
55
57
  readonly name: string;
58
+ /** Workspace-root business path. Required when creating a new collection; omitted only for existing aliases. */
59
+ readonly directoryPath?: readonly string[];
56
60
  /** Zod 4 object schema of the record body (no `_id` / `_meta`). */
57
61
  readonly schema: S;
58
62
  readonly indexes: readonly IndexSpec[];
@@ -82,9 +86,12 @@ export type NewDoc<C extends CollectionDefinition> = z.input<C['schema']> & {
82
86
  export declare function defineCollection<S extends CollectionSchema>(definition: {
83
87
  alias: string;
84
88
  name: string;
89
+ directoryPath?: readonly string[];
85
90
  schema: S;
86
91
  indexes?: IndexSpec[];
87
92
  description?: string;
88
93
  seed?: SeedDoc<S>[];
89
94
  }): CollectionDefinition<S>;
90
95
  export declare function assertCollectionName(name: unknown): asserts name is string;
96
+ /** Validate each business name as one segment; no normalization or fabricated levels. */
97
+ export declare function assertDirectoryPath(path: unknown): asserts path is readonly string[];
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MAX_SEED_DOCUMENTS = exports.MAX_NAME_LENGTH = exports.MAX_DESCRIPTION_LENGTH = exports.MAX_COLLECTIONS = exports.ALIAS_PATTERN = void 0;
3
+ exports.MAX_SEED_DOCUMENTS = exports.MAX_DIRECTORY_DEPTH = exports.MAX_NAME_LENGTH = exports.MAX_DESCRIPTION_LENGTH = exports.MAX_COLLECTIONS = exports.ALIAS_PATTERN = void 0;
4
4
  exports.collectionLabel = collectionLabel;
5
5
  exports.defineCollection = defineCollection;
6
6
  exports.assertCollectionName = assertCollectionName;
7
+ exports.assertDirectoryPath = assertDirectoryPath;
7
8
  /** Collection alias: unique per App, case-insensitive. */
8
9
  exports.ALIAS_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{0,62}$/;
9
10
  /** UNS: one ensure takes 1–64 declarations, and an App owns at most 64 collections. */
@@ -12,6 +13,8 @@ exports.MAX_COLLECTIONS = 64;
12
13
  exports.MAX_DESCRIPTION_LENGTH = 512;
13
14
  /** UNS: business-facing collection name length in Unicode characters. */
14
15
  exports.MAX_NAME_LENGTH = 63;
16
+ /** Business directory paths may omit unknown ISA-95 levels. */
17
+ exports.MAX_DIRECTORY_DEPTH = 16;
15
18
  /** Seeds are inserted in atomic chunks of 200 during the first request; keep them a baseline, not a data import. */
16
19
  exports.MAX_SEED_DOCUMENTS = 1000;
17
20
  function collectionLabel(target) {
@@ -22,6 +25,8 @@ function defineCollection(definition) {
22
25
  throw new Error(`Collection alias "${definition.alias}" must match ${exports.ALIAS_PATTERN} (letters, digits, "_" or "-", starting with a letter).`);
23
26
  }
24
27
  assertCollectionName(definition.name);
28
+ if (definition.directoryPath !== undefined)
29
+ assertDirectoryPath(definition.directoryPath);
25
30
  if (definition.description && [...definition.description].length > exports.MAX_DESCRIPTION_LENGTH) {
26
31
  throw new Error(`Collection "${definition.alias}": description is longer than ${exports.MAX_DESCRIPTION_LENGTH} characters.`);
27
32
  }
@@ -31,6 +36,7 @@ function defineCollection(definition) {
31
36
  return Object.freeze({
32
37
  alias: definition.alias,
33
38
  name: definition.name,
39
+ ...(definition.directoryPath !== undefined ? { directoryPath: Object.freeze([...definition.directoryPath]) } : {}),
34
40
  schema: definition.schema,
35
41
  indexes: Object.freeze([...(definition.indexes ?? [])]),
36
42
  ...(definition.description ? { description: definition.description } : {}),
@@ -42,4 +48,18 @@ function assertCollectionName(name) {
42
48
  throw new Error(`Collection name must be nonempty, at most ${exports.MAX_NAME_LENGTH} Unicode characters, without surrounding whitespace, "/" or NUL.`);
43
49
  }
44
50
  }
51
+ /** Validate each business name as one segment; no normalization or fabricated levels. */
52
+ function assertDirectoryPath(path) {
53
+ if (!Array.isArray(path) || path.length < 1 || path.length > exports.MAX_DIRECTORY_DEPTH) {
54
+ throw new Error(`directoryPath must contain 1–${exports.MAX_DIRECTORY_DEPTH} business directory names from the workspace root.`);
55
+ }
56
+ for (let index = 0; index < path.length; index += 1) {
57
+ try {
58
+ assertCollectionName(path[index]);
59
+ }
60
+ catch {
61
+ throw new Error(`directoryPath[${index}] must be a nonempty business name of at most ${exports.MAX_NAME_LENGTH} Unicode characters, without surrounding whitespace, "/" or NUL.`);
62
+ }
63
+ }
64
+ }
45
65
  //# sourceMappingURL=define.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"define.js","sourceRoot":"","sources":["../../../src/data/define.ts"],"names":[],"mappings":";;;AA8EA,0CAEC;AAaD,4CA8BC;AAED,oDAIC;AApHD,0DAA0D;AAC7C,QAAA,aAAa,GAAG,+BAA+B,CAAC;AAC7D,uFAAuF;AAC1E,QAAA,eAAe,GAAG,EAAE,CAAC;AAClC,wDAAwD;AAC3C,QAAA,sBAAsB,GAAG,GAAG,CAAC;AAC1C,yEAAyE;AAC5D,QAAA,eAAe,GAAG,EAAE,CAAC;AAClC,oHAAoH;AACvG,QAAA,kBAAkB,GAAG,IAAI,CAAC;AAwDvC,SAAgB,eAAe,CAAC,MAAwB;IACtD,OAAO,cAAc,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;AACvE,CAAC;AAaD,SAAgB,gBAAgB,CAA6B,UAO5D;IACC,IAAI,CAAC,qBAAa,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CACb,qBAAqB,UAAU,CAAC,KAAK,gBAAgB,qBAAa,yDAAyD,CA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AE,KAAK,IAAI,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,MAAM,GAAG,uBAAe,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACvI,MAAM,IAAI,KAAK,CAAC,6CAA6C,uBAAe,kEAAkE,CAAC,CAAC;IAClJ,CAAC;AACH,CAAC","sourcesContent":["/**\n * Collection declarations — the single source of truth for App data.\n *\n * An App declares every collection with `defineCollection(...)` and passes the\n * list to `createDataClient({ collections })`. The client ensures that list in\n * one batch on first data access: a missing collection is created, a\n * declaration wider than storage widens it, and nothing is ever narrowed.\n *\n * This module is pure (Zod types only) so declarations, API input schemas and\n * build-time checks can import it without pulling in the runtime client.\n */\nimport type { z } from 'zod';\n\n/** Collection alias: unique per App, case-insensitive. */\nexport const ALIAS_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{0,62}$/;\n/** UNS: one ensure takes 1–64 declarations, and an App owns at most 64 collections. */\nexport const MAX_COLLECTIONS = 64;\n/** UNS: collection description length in characters. */\nexport const MAX_DESCRIPTION_LENGTH = 512;\n/** UNS: business-facing collection name length in Unicode characters. */\nexport const MAX_NAME_LENGTH = 63;\n/** Seeds are inserted in atomic chunks of 200 during the first request; keep them a baseline, not a data import. */\nexport const MAX_SEED_DOCUMENTS = 1000;\n\n/** Same shape as collection metadata indexes, limited to record indexes. */\nexport interface IndexSpec {\n /** Business field path without `data.`, e.g. `code` or `customer.id`. */\n path?: string;\n /** Compound index, at most 3 paths. Mutually exclusive with `path`. */\n paths?: string[];\n unique?: boolean;\n /** `btree` (default) or `gin` for one object/array path; GIN cannot be unique. */\n kind?: 'btree' | 'gin';\n /**\n * TTL index: UNS physically deletes a record this many seconds\n * (60–31536000) after the instant in `path`, an integer field of Unix\n * milliseconds. At most one per collection, on a single non-unique `path`. Expired\n * records cannot be restored. ensure may add a TTL or lengthen it;\n * shortening or removing it takes schema:replace.\n */\n expireAfterSeconds?: number;\n}\n\n/** System metadata of a stored record. Times are Unix milliseconds. */\nexport interface RecordMeta {\n revision: number;\n createdAt: number;\n updatedAt: number;\n}\n\nexport type CollectionSchema = z.ZodObject;\n\n/** A seed document: a full record plus the fixed `_id` it is inserted with. */\nexport type SeedDoc<S extends CollectionSchema> = z.input<S> & { _id: string };\n\nexport interface CollectionDefinition<S extends CollectionSchema = CollectionSchema> {\n readonly alias: string;\n /** Business-facing name, independent of the stable App alias. */\n readonly name: string;\n /** Zod 4 object schema of the record body (no `_id` / `_meta`). */\n readonly schema: S;\n readonly indexes: readonly IndexSpec[];\n readonly description?: string;\n /**\n * Baseline records, written only when ensure reports this collection as\n * `created`. Never re-applied to an existing collection.\n */\n readonly seed?: readonly SeedDoc<S>[];\n}\n\n/** An existing collection. The optional schema validates writes locally; it never changes storage. */\nexport interface CollectionReference<S extends CollectionSchema = CollectionSchema> {\n readonly collectionId: string;\n readonly schema?: S;\n}\n\nexport type CollectionTarget = CollectionDefinition | CollectionReference;\n\nexport function collectionLabel(target: CollectionTarget): string {\n return 'collectionId' in target ? target.collectionId : target.alias;\n}\n\n/** A stored record as services read it: `{ _id, ...fields, _meta }`. */\nexport type Doc<C extends CollectionDefinition> = z.output<C['schema']> & {\n _id: string;\n _meta: RecordMeta;\n};\n\n/** A record to insert: the schema input plus an optional caller-chosen `_id`. */\nexport type NewDoc<C extends CollectionDefinition> = z.input<C['schema']> & {\n _id?: string;\n};\n\nexport function defineCollection<S extends CollectionSchema>(definition: {\n alias: string;\n name: string;\n schema: S;\n indexes?: IndexSpec[];\n description?: string;\n seed?: SeedDoc<S>[];\n}): CollectionDefinition<S> {\n if (!ALIAS_PATTERN.test(definition.alias)) {\n throw new Error(\n `Collection alias \"${definition.alias}\" must match ${ALIAS_PATTERN} (letters, digits, \"_\" or \"-\", starting with a letter).`,\n );\n }\n assertCollectionName(definition.name);\n if (definition.description && [...definition.description].length > MAX_DESCRIPTION_LENGTH) {\n throw new Error(`Collection \"${definition.alias}\": description is longer than ${MAX_DESCRIPTION_LENGTH} characters.`);\n }\n if (definition.seed && definition.seed.length > MAX_SEED_DOCUMENTS) {\n throw new Error(\n `Collection \"${definition.alias}\": at most ${MAX_SEED_DOCUMENTS} seed documents (got ${definition.seed.length}). Seeds are a baseline written on creation, not a bulk import.`,\n );\n }\n return Object.freeze({\n alias: definition.alias,\n name: definition.name,\n schema: definition.schema,\n indexes: Object.freeze([...(definition.indexes ?? [])]),\n ...(definition.description ? { description: definition.description } : {}),\n ...(definition.seed ? { seed: Object.freeze([...definition.seed]) } : {}),\n });\n}\n\nexport function assertCollectionName(name: unknown): asserts name is string {\n if (typeof name !== 'string' || name.length === 0 || name.trim() !== name || [...name].length > MAX_NAME_LENGTH || /[\\/\\0]/.test(name)) {\n throw new Error(`Collection name must be nonempty, at most ${MAX_NAME_LENGTH} Unicode characters, without surrounding whitespace, \"/\" or NUL.`);\n }\n}\n"]}
1
+ {"version":3,"file":"define.js","sourceRoot":"","sources":["../../../src/data/define.ts"],"names":[],"mappings":";;;AAkFA,0CAEC;AAaD,4CAiCC;AAED,oDAIC;AAGD,kDAWC;AAzID,0DAA0D;AAC7C,QAAA,aAAa,GAAG,+BAA+B,CAAC;AAC7D,uFAAuF;AAC1E,QAAA,eAAe,GAAG,EAAE,CAAC;AAClC,wDAAwD;AAC3C,QAAA,sBAAsB,GAAG,GAAG,CAAC;AAC1C,yEAAyE;AAC5D,QAAA,eAAe,GAAG,EAAE,CAAC;AAClC,+DAA+D;AAClD,QAAA,mBAAmB,GAAG,EAAE,CAAC;AACtC,oHAAoH;AACvG,QAAA,kBAAkB,GAAG,IAAI,CAAC;AA0DvC,SAAgB,eAAe,CAAC,MAAwB;IACtD,OAAO,cAAc,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;AACvE,CAAC;AAaD,SAAgB,gBAAgB,CAA6B,UAQ5D;IACC,IAAI,CAAC,qBAAa,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAA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collection with `defineCollection(...)` and passes the\n * list to `createDataClient({ collections })`. The client ensures that list in\n * one batch on first data access: a missing collection is created, a\n * declaration wider than storage widens it, and nothing is ever narrowed.\n *\n * This module is pure (Zod types only) so declarations, API input schemas and\n * build-time checks can import it without pulling in the runtime client.\n */\nimport type { z } from 'zod';\n\n/** Collection alias: unique per App, case-insensitive. */\nexport const ALIAS_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{0,62}$/;\n/** UNS: one ensure takes 1–64 declarations, and an App owns at most 64 collections. */\nexport const MAX_COLLECTIONS = 64;\n/** UNS: collection description length in characters. */\nexport const MAX_DESCRIPTION_LENGTH = 512;\n/** UNS: business-facing collection name length in Unicode characters. */\nexport const MAX_NAME_LENGTH = 63;\n/** Business directory paths may omit unknown ISA-95 levels. */\nexport const MAX_DIRECTORY_DEPTH = 16;\n/** Seeds are inserted in atomic chunks of 200 during the first request; keep them a baseline, not a data import. */\nexport const MAX_SEED_DOCUMENTS = 1000;\n\n/** Same shape as collection metadata indexes, limited to record indexes. */\nexport interface IndexSpec {\n /** Business field path without `data.`, e.g. `code` or `customer.id`. */\n path?: string;\n /** Compound index, at most 3 paths. Mutually exclusive with `path`. */\n paths?: string[];\n unique?: boolean;\n /** `btree` (default) or `gin` for one object/array path; GIN cannot be unique. */\n kind?: 'btree' | 'gin';\n /**\n * TTL index: UNS physically deletes a record this many seconds\n * (60–31536000) after the instant in `path`, an integer field of Unix\n * milliseconds. At most one per collection, on a single non-unique `path`. Expired\n * records cannot be restored. ensure may add a TTL or lengthen it;\n * shortening or removing it takes schema:replace.\n */\n expireAfterSeconds?: number;\n}\n\n/** System metadata of a stored record. Times are Unix milliseconds. */\nexport interface RecordMeta {\n revision: number;\n createdAt: number;\n updatedAt: number;\n}\n\nexport type CollectionSchema = z.ZodObject;\n\n/** A seed document: a full record plus the fixed `_id` it is inserted with. */\nexport type SeedDoc<S extends CollectionSchema> = z.input<S> & { _id: string };\n\nexport interface CollectionDefinition<S extends CollectionSchema = CollectionSchema> {\n readonly alias: string;\n /** Business-facing name, independent of the stable App alias. */\n readonly name: string;\n /** Workspace-root business path. Required when creating a new collection; omitted only for existing aliases. */\n readonly directoryPath?: readonly string[];\n /** Zod 4 object schema of the record body (no `_id` / `_meta`). */\n readonly schema: S;\n readonly indexes: readonly IndexSpec[];\n readonly description?: string;\n /**\n * Baseline records, written only when ensure reports this collection as\n * `created`. Never re-applied to an existing collection.\n */\n readonly seed?: readonly SeedDoc<S>[];\n}\n\n/** An existing collection. The optional schema validates writes locally; it never changes storage. */\nexport interface CollectionReference<S extends CollectionSchema = CollectionSchema> {\n readonly collectionId: string;\n readonly schema?: S;\n}\n\nexport type CollectionTarget = CollectionDefinition | CollectionReference;\n\nexport function collectionLabel(target: CollectionTarget): string {\n return 'collectionId' in target ? target.collectionId : target.alias;\n}\n\n/** A stored record as services read it: `{ _id, ...fields, _meta }`. */\nexport type Doc<C extends CollectionDefinition> = z.output<C['schema']> & {\n _id: string;\n _meta: RecordMeta;\n};\n\n/** A record to insert: the schema input plus an optional caller-chosen `_id`. */\nexport type NewDoc<C extends CollectionDefinition> = z.input<C['schema']> & {\n _id?: string;\n};\n\nexport function defineCollection<S extends CollectionSchema>(definition: {\n alias: string;\n name: string;\n directoryPath?: readonly string[];\n schema: S;\n indexes?: IndexSpec[];\n description?: string;\n seed?: SeedDoc<S>[];\n}): CollectionDefinition<S> {\n if (!ALIAS_PATTERN.test(definition.alias)) {\n throw new Error(\n `Collection alias \"${definition.alias}\" must match ${ALIAS_PATTERN} (letters, digits, \"_\" or \"-\", starting with a letter).`,\n );\n }\n assertCollectionName(definition.name);\n if (definition.directoryPath !== undefined) assertDirectoryPath(definition.directoryPath);\n if (definition.description && [...definition.description].length > MAX_DESCRIPTION_LENGTH) {\n throw new Error(`Collection \"${definition.alias}\": description is longer than ${MAX_DESCRIPTION_LENGTH} characters.`);\n }\n if (definition.seed && definition.seed.length > MAX_SEED_DOCUMENTS) {\n throw new Error(\n `Collection \"${definition.alias}\": at most ${MAX_SEED_DOCUMENTS} seed documents (got ${definition.seed.length}). Seeds are a baseline written on creation, not a bulk import.`,\n );\n }\n return Object.freeze({\n alias: definition.alias,\n name: definition.name,\n ...(definition.directoryPath !== undefined ? { directoryPath: Object.freeze([...definition.directoryPath]) } : {}),\n schema: definition.schema,\n indexes: Object.freeze([...(definition.indexes ?? [])]),\n ...(definition.description ? { description: definition.description } : {}),\n ...(definition.seed ? { seed: Object.freeze([...definition.seed]) } : {}),\n });\n}\n\nexport function assertCollectionName(name: unknown): asserts name is string {\n if (typeof name !== 'string' || name.length === 0 || name.trim() !== name || [...name].length > MAX_NAME_LENGTH || /[\\/\\0]/.test(name)) {\n throw new Error(`Collection name must be nonempty, at most ${MAX_NAME_LENGTH} Unicode characters, without surrounding whitespace, \"/\" or NUL.`);\n }\n}\n\n/** Validate each business name as one segment; no normalization or fabricated levels. */\nexport function assertDirectoryPath(path: unknown): asserts path is readonly string[] {\n if (!Array.isArray(path) || path.length < 1 || path.length > MAX_DIRECTORY_DEPTH) {\n throw new Error(`directoryPath must contain 1–${MAX_DIRECTORY_DEPTH} business directory names from the workspace root.`);\n }\n for (let index = 0; index < path.length; index += 1) {\n try {\n assertCollectionName(path[index]);\n } catch {\n throw new Error(`directoryPath[${index}] must be a nonempty business name of at most ${MAX_NAME_LENGTH} Unicode characters, without surrounding whitespace, \"/\" or NUL.`);\n }\n }\n}\n"]}
@@ -38,6 +38,7 @@ export interface NormalizedIndex {
38
38
  export interface CompiledCollection {
39
39
  alias: string;
40
40
  name: string;
41
+ directoryPath?: readonly string[];
41
42
  recordSchema: RecordSchema;
42
43
  indexes: NormalizedIndex[];
43
44
  description?: string;
@@ -477,6 +477,14 @@ function compileCollection(definition) {
477
477
  catch (error) {
478
478
  throw new CollectionSchemaError(definition.alias, [{ path: 'name', message: error instanceof Error ? error.message : String(error) }]);
479
479
  }
480
+ if (definition.directoryPath !== undefined) {
481
+ try {
482
+ (0, define_js_1.assertDirectoryPath)(definition.directoryPath);
483
+ }
484
+ catch (error) {
485
+ throw new CollectionSchemaError(definition.alias, [{ path: 'directoryPath', message: error instanceof Error ? error.message : String(error) }]);
486
+ }
487
+ }
480
488
  const recordSchema = toRecordSchema(definition.alias, definition.schema);
481
489
  const issues = [];
482
490
  const indexes = normalizeIndexes(definition.alias, definition.indexes, recordSchema, issues);
@@ -485,6 +493,7 @@ function compileCollection(definition) {
485
493
  const result = {
486
494
  alias: definition.alias,
487
495
  name: definition.name,
496
+ ...(definition.directoryPath !== undefined ? { directoryPath: Object.freeze([...definition.directoryPath]) } : {}),
488
497
  recordSchema,
489
498
  indexes,
490
499
  ...(definition.description ? { description: definition.description } : {}),