@substrat-run/contracts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # @substrat-run/contracts
2
+
3
+ Contract schemas for the [Substrat](https://github.com/substrat-run/substrat) kernel —
4
+ the hard parts of vertical B2B SaaS (tenancy, permissions, audit, GDPR), hosted and
5
+ enforced at runtime.
6
+
7
+ This package is the **source of truth** for every data shape that crosses a Substrat
8
+ boundary. The schemas are written in [Zod](https://zod.dev), so the reviewed artifact
9
+ *is* the runtime validator: OpenAPI and JSON Schema documents are emitted from this
10
+ package, never hand-maintained beside it.
11
+
12
+ ## What's inside
13
+
14
+ - **IDs** — branded ULID types (`TenantId`, `ScopeId`, `PrincipalId`, `EventId`, …):
15
+ opaque, sortable, and unmixable at compile time.
16
+ - **Tenancy** — `Tenant` and `Scope`: two-level today, tree-ready by design
17
+ (`parentScopeId`), with per-scope `storageShape` and immutable `jurisdiction`.
18
+ - **Permissions** — roles @ nodes plus capability grants, and a `Decision` type where
19
+ an allow *always* carries the grant and inheritance path that produced it.
20
+ - **Events** — the domain-event envelope. `piiClass` is required at the type level and
21
+ a PII-classed event without a `subjectId` fails validation: crypto-shredding must
22
+ always be able to key the erasure.
23
+ - **Module manifest** — what makes an engine self-describing: permissions, emitted and
24
+ consumed events, migrations and skew window, attachment targets, entitlement key.
25
+
26
+ ## Usage
27
+
28
+ ```ts
29
+ import { scope, domainEventInput, tenantId } from '@substrat-run/contracts';
30
+
31
+ const s = scope.parse(row); // validated, branded Scope
32
+ const t: TenantId = tenantId.parse(x); // a ScopeId will not typecheck here
33
+
34
+ domainEventInput.parse({
35
+ type: 'workorder.completed',
36
+ schemaVersion: 1,
37
+ entity: { entityType: 'workorder', entityId: wo.id },
38
+ piiClass: 'none',
39
+ payload: { ... },
40
+ });
41
+ ```
42
+
43
+ ## Related packages
44
+
45
+ - [`@substrat-run/kernel`](https://npmjs.com/package/@substrat-run/kernel) — the behavioral
46
+ interfaces (`ScopeHost`, `OperationContext`) built on these shapes
47
+ - [`@substrat-run/adapter-sqlite`](https://npmjs.com/package/@substrat-run/adapter-sqlite) — the
48
+ pure-SQLite reference implementation
49
+ - [`@substrat-run/contract-tests`](https://npmjs.com/package/@substrat-run/contract-tests) — the
50
+ conformance suite every adapter must pass
51
+
52
+ ## Status
53
+
54
+ Pre-release (0.x): shapes change without notice until the first vertical ships.
@@ -0,0 +1,4 @@
1
+ import { z } from 'zod';
2
+ export declare const visibility: z.ZodEnum<["internal", "customer"]>;
3
+ export type Visibility = z.infer<typeof visibility>;
4
+ //# sourceMappingURL=attachments.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachments.d.ts","sourceRoot":"","sources":["../src/attachments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAQxB,eAAO,MAAM,UAAU,qCAAmC,CAAC;AAC3D,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,UAAU,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { z } from 'zod';
2
+ // Visibility classification for attachment items — documents, comments,
3
+ // custom-field values (K-13). Mandatory wherever data can reach the customer
4
+ // portal: like piiClass on events, a classification is only total if it was
5
+ // never optional. Engine-owned tables handle their own flags; this is the
6
+ // kernel-surface convention.
7
+ export const visibility = z.enum(['internal', 'customer']);
8
+ //# sourceMappingURL=attachments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachments.js","sourceRoot":"","sources":["../src/attachments.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,wEAAwE;AACxE,6EAA6E;AAC7E,4EAA4E;AAC5E,0EAA0E;AAC1E,6BAA6B;AAE7B,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC"}
@@ -0,0 +1,186 @@
1
+ import { z } from 'zod';
2
+ export declare const entityRef: z.ZodObject<{
3
+ entityType: z.ZodString;
4
+ entityId: z.ZodString;
5
+ }, "strip", z.ZodTypeAny, {
6
+ entityType: string;
7
+ entityId: string;
8
+ }, {
9
+ entityType: string;
10
+ entityId: string;
11
+ }>;
12
+ export type EntityRef = z.infer<typeof entityRef>;
13
+ export declare const piiClass: z.ZodEnum<["none", "pseudonymous", "direct"]>;
14
+ export type PiiClass = z.infer<typeof piiClass>;
15
+ export declare const eventType: z.ZodString;
16
+ export declare const systemActor: z.ZodObject<{
17
+ system: z.ZodBranded<z.ZodString, "ModuleId">;
18
+ }, "strip", z.ZodTypeAny, {
19
+ system: string & z.BRAND<"ModuleId">;
20
+ }, {
21
+ system: string;
22
+ }>;
23
+ export declare const actor: z.ZodUnion<[z.ZodBranded<z.ZodString, "PrincipalId">, z.ZodObject<{
24
+ system: z.ZodBranded<z.ZodString, "ModuleId">;
25
+ }, "strip", z.ZodTypeAny, {
26
+ system: string & z.BRAND<"ModuleId">;
27
+ }, {
28
+ system: string;
29
+ }>]>;
30
+ export type Actor = z.infer<typeof actor>;
31
+ export declare const domainEventInput: z.ZodEffects<z.ZodObject<{
32
+ type: z.ZodString;
33
+ schemaVersion: z.ZodNumber;
34
+ entity: z.ZodObject<{
35
+ entityType: z.ZodString;
36
+ entityId: z.ZodString;
37
+ }, "strip", z.ZodTypeAny, {
38
+ entityType: string;
39
+ entityId: string;
40
+ }, {
41
+ entityType: string;
42
+ entityId: string;
43
+ }>;
44
+ piiClass: z.ZodEnum<["none", "pseudonymous", "direct"]>;
45
+ subjectId: z.ZodOptional<z.ZodBranded<z.ZodString, "DataSubjectId">>;
46
+ payload: z.ZodUnknown;
47
+ }, "strip", z.ZodTypeAny, {
48
+ type: string;
49
+ schemaVersion: number;
50
+ entity: {
51
+ entityType: string;
52
+ entityId: string;
53
+ };
54
+ piiClass: "none" | "pseudonymous" | "direct";
55
+ subjectId?: (string & z.BRAND<"DataSubjectId">) | undefined;
56
+ payload?: unknown;
57
+ }, {
58
+ type: string;
59
+ schemaVersion: number;
60
+ entity: {
61
+ entityType: string;
62
+ entityId: string;
63
+ };
64
+ piiClass: "none" | "pseudonymous" | "direct";
65
+ subjectId?: string | undefined;
66
+ payload?: unknown;
67
+ }>, {
68
+ type: string;
69
+ schemaVersion: number;
70
+ entity: {
71
+ entityType: string;
72
+ entityId: string;
73
+ };
74
+ piiClass: "none" | "pseudonymous" | "direct";
75
+ subjectId?: (string & z.BRAND<"DataSubjectId">) | undefined;
76
+ payload?: unknown;
77
+ }, {
78
+ type: string;
79
+ schemaVersion: number;
80
+ entity: {
81
+ entityType: string;
82
+ entityId: string;
83
+ };
84
+ piiClass: "none" | "pseudonymous" | "direct";
85
+ subjectId?: string | undefined;
86
+ payload?: unknown;
87
+ }>;
88
+ export type DomainEventInput = z.infer<typeof domainEventInput>;
89
+ export declare const domainEvent: z.ZodEffects<z.ZodObject<{
90
+ id: z.ZodBranded<z.ZodString, "EventId">;
91
+ type: z.ZodString;
92
+ schemaVersion: z.ZodNumber;
93
+ occurredAt: z.ZodBranded<z.ZodString, "Instant">;
94
+ tenantId: z.ZodBranded<z.ZodString, "TenantId">;
95
+ scopeId: z.ZodBranded<z.ZodString, "ScopeId">;
96
+ actor: z.ZodUnion<[z.ZodBranded<z.ZodString, "PrincipalId">, z.ZodObject<{
97
+ system: z.ZodBranded<z.ZodString, "ModuleId">;
98
+ }, "strip", z.ZodTypeAny, {
99
+ system: string & z.BRAND<"ModuleId">;
100
+ }, {
101
+ system: string;
102
+ }>]>;
103
+ entity: z.ZodObject<{
104
+ entityType: z.ZodString;
105
+ entityId: z.ZodString;
106
+ }, "strip", z.ZodTypeAny, {
107
+ entityType: string;
108
+ entityId: string;
109
+ }, {
110
+ entityType: string;
111
+ entityId: string;
112
+ }>;
113
+ piiClass: z.ZodEnum<["none", "pseudonymous", "direct"]>;
114
+ subjectId: z.ZodOptional<z.ZodBranded<z.ZodString, "DataSubjectId">>;
115
+ payload: z.ZodUnknown;
116
+ }, "strip", z.ZodTypeAny, {
117
+ type: string;
118
+ schemaVersion: number;
119
+ entity: {
120
+ entityType: string;
121
+ entityId: string;
122
+ };
123
+ piiClass: "none" | "pseudonymous" | "direct";
124
+ id: string & z.BRAND<"EventId">;
125
+ occurredAt: string & z.BRAND<"Instant">;
126
+ tenantId: string & z.BRAND<"TenantId">;
127
+ scopeId: string & z.BRAND<"ScopeId">;
128
+ actor: (string & z.BRAND<"PrincipalId">) | {
129
+ system: string & z.BRAND<"ModuleId">;
130
+ };
131
+ subjectId?: (string & z.BRAND<"DataSubjectId">) | undefined;
132
+ payload?: unknown;
133
+ }, {
134
+ type: string;
135
+ schemaVersion: number;
136
+ entity: {
137
+ entityType: string;
138
+ entityId: string;
139
+ };
140
+ piiClass: "none" | "pseudonymous" | "direct";
141
+ id: string;
142
+ occurredAt: string;
143
+ tenantId: string;
144
+ scopeId: string;
145
+ actor: string | {
146
+ system: string;
147
+ };
148
+ subjectId?: string | undefined;
149
+ payload?: unknown;
150
+ }>, {
151
+ type: string;
152
+ schemaVersion: number;
153
+ entity: {
154
+ entityType: string;
155
+ entityId: string;
156
+ };
157
+ piiClass: "none" | "pseudonymous" | "direct";
158
+ id: string & z.BRAND<"EventId">;
159
+ occurredAt: string & z.BRAND<"Instant">;
160
+ tenantId: string & z.BRAND<"TenantId">;
161
+ scopeId: string & z.BRAND<"ScopeId">;
162
+ actor: (string & z.BRAND<"PrincipalId">) | {
163
+ system: string & z.BRAND<"ModuleId">;
164
+ };
165
+ subjectId?: (string & z.BRAND<"DataSubjectId">) | undefined;
166
+ payload?: unknown;
167
+ }, {
168
+ type: string;
169
+ schemaVersion: number;
170
+ entity: {
171
+ entityType: string;
172
+ entityId: string;
173
+ };
174
+ piiClass: "none" | "pseudonymous" | "direct";
175
+ id: string;
176
+ occurredAt: string;
177
+ tenantId: string;
178
+ scopeId: string;
179
+ actor: string | {
180
+ system: string;
181
+ };
182
+ subjectId?: string | undefined;
183
+ payload?: unknown;
184
+ }>;
185
+ export type DomainEvent = z.infer<typeof domainEvent>;
186
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAYxB,eAAO,MAAM,SAAS;;;;;;;;;EAGpB,CAAC;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,SAAS,CAAC,CAAC;AAIlD,eAAO,MAAM,QAAQ,+CAA6C,CAAC;AACnE,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,QAAQ,CAAC,CAAC;AAGhD,eAAO,MAAM,SAAS,aAA+C,CAAC;AAEtE,eAAO,MAAM,WAAW;;;;;;EAAiC,CAAC;AAC1D,eAAO,MAAM,KAAK;;;;;;IAAsC,CAAC;AACzD,MAAM,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,KAAK,CAAC,CAAC;AAiB1C,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EASD,CAAC;AAC7B,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAGhE,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAcI,CAAC;AAC7B,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,CAAC,CAAC"}
package/dist/events.js ADDED
@@ -0,0 +1,52 @@
1
+ import { z } from 'zod';
2
+ import { dataSubjectId, eventId, instant, moduleId, principalId, scopeId, tenantId, } from './ids.js';
3
+ // Opaque ref — the kernel owns no entities (D-1); attachment contracts bind here.
4
+ export const entityRef = z.object({
5
+ entityType: z.string().min(1),
6
+ entityId: z.string().min(1),
7
+ });
8
+ // Drives crypto-shredding (§5.3 of the plan). Required at the type level:
9
+ // an event that COULD carry PII cannot be declared without classification.
10
+ export const piiClass = z.enum(['none', 'pseudonymous', 'direct']);
11
+ // 'workorder.completed' — module-namespaced
12
+ export const eventType = z.string().regex(/^[a-z0-9-]+\.[a-z0-9-]+$/);
13
+ export const systemActor = z.object({ system: moduleId });
14
+ export const actor = z.union([principalId, systemActor]);
15
+ const piiInvariant = (val, ctx) => {
16
+ if (val.piiClass !== 'none' && val.subjectId === undefined) {
17
+ ctx.addIssue({
18
+ code: z.ZodIssueCode.custom,
19
+ path: ['subjectId'],
20
+ message: `subjectId is required when piiClass is '${val.piiClass}' — crypto-shredding must be able to key the erasure`,
21
+ });
22
+ }
23
+ };
24
+ // What module code passes to emit(). Everything identifying the origin is
25
+ // deliberately absent — the kernel stamps it (§6.1 of the design doc).
26
+ export const domainEventInput = z
27
+ .object({
28
+ type: eventType,
29
+ schemaVersion: z.number().int().positive(),
30
+ entity: entityRef,
31
+ piiClass,
32
+ subjectId: dataSubjectId.optional(),
33
+ payload: z.unknown(),
34
+ })
35
+ .superRefine(piiInvariant);
36
+ // The full envelope as it enters the spine.
37
+ export const domainEvent = z
38
+ .object({
39
+ id: eventId, // ULID; idempotency key downstream (consumers are required-idempotent)
40
+ type: eventType,
41
+ schemaVersion: z.number().int().positive(),
42
+ occurredAt: instant, // stamped by kernel
43
+ tenantId, // stamped by kernel — a vertical cannot mislabel an event's origin
44
+ scopeId, // stamped by kernel
45
+ actor, // stamped by kernel from the stub's ambient context
46
+ entity: entityRef,
47
+ piiClass,
48
+ subjectId: dataSubjectId.optional(),
49
+ payload: z.unknown(),
50
+ })
51
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3B,IAAI,EAAE,CAAC,WAAW,CAAC;YACnB,OAAO,EAAE,2CAA2C,GAAG,CAAC,QAAQ,sDAAsD;SACvH,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CAAC;AAEF,0EAA0E;AAC1E,uEAAuE;AACvE,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC;KAC9B,MAAM,CAAC;IACN,IAAI,EAAE,SAAS;IACf,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE;IAC1C,MAAM,EAAE,SAAS;IACjB,QAAQ;IACR,SAAS,EAAE,aAAa,CAAC,QAAQ,EAAE;IACnC,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE;CACrB,CAAC;KACD,WAAW,CAAC,YAAY,CAAC,CAAC;AAG7B,4CAA4C;AAC5C,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC;KACzB,MAAM,CAAC;IACN,EAAE,EAAE,OAAO,EAAE,uEAAuE;IACpF,IAAI,EAAE,SAAS;IACf,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE;IAC1C,UAAU,EAAE,OAAO,EAAE,oBAAoB;IACzC,QAAQ,EAAE,mEAAmE;IAC7E,OAAO,EAAE,oBAAoB;IAC7B,KAAK,EAAE,oDAAoD;IAC3D,MAAM,EAAE,SAAS;IACjB,QAAQ;IACR,SAAS,EAAE,aAAa,CAAC,QAAQ,EAAE;IACnC,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE;CACrB,CAAC;KACD,WAAW,CAAC,YAAY,CAAC,CAAC"}
package/dist/ids.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { z } from 'zod';
2
+ export declare const tenantId: z.ZodBranded<z.ZodString, "TenantId">;
3
+ export type TenantId = z.infer<typeof tenantId>;
4
+ export declare const scopeId: z.ZodBranded<z.ZodString, "ScopeId">;
5
+ export type ScopeId = z.infer<typeof scopeId>;
6
+ export declare const principalId: z.ZodBranded<z.ZodString, "PrincipalId">;
7
+ export type PrincipalId = z.infer<typeof principalId>;
8
+ export declare const eventId: z.ZodBranded<z.ZodString, "EventId">;
9
+ export type EventId = z.infer<typeof eventId>;
10
+ export declare const dataSubjectId: z.ZodBranded<z.ZodString, "DataSubjectId">;
11
+ export type DataSubjectId = z.infer<typeof dataSubjectId>;
12
+ export declare const moduleId: z.ZodBranded<z.ZodString, "ModuleId">;
13
+ export type ModuleId = z.infer<typeof moduleId>;
14
+ export declare const instant: z.ZodBranded<z.ZodString, "Instant">;
15
+ export type Instant = z.infer<typeof instant>;
16
+ export declare const permissionKey: z.ZodBranded<z.ZodString, "PermissionKey">;
17
+ export type PermissionKey = z.infer<typeof permissionKey>;
18
+ export declare const slug: z.ZodString;
19
+ //# sourceMappingURL=ids.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../src/ids.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAQxB,eAAO,MAAM,QAAQ,uCAA6C,CAAC;AACnE,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,QAAQ,CAAC,CAAC;AAEhD,eAAO,MAAM,OAAO,sCAA4C,CAAC;AACjE,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,CAAC;AAE9C,eAAO,MAAM,WAAW,0CAAgD,CAAC;AACzE,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,CAAC,CAAC;AAEtD,eAAO,MAAM,OAAO,sCAA4C,CAAC;AACjE,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,CAAC;AAE9C,eAAO,MAAM,aAAa,4CAAkD,CAAC;AAC7E,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAG1D,eAAO,MAAM,QAAQ,uCAGC,CAAC;AACvB,MAAM,MAAM,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,QAAQ,CAAC,CAAC;AAGhD,eAAO,MAAM,OAAO,sCAA2D,CAAC;AAChF,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,CAAC;AAG9C,eAAO,MAAM,aAAa,4CAGC,CAAC;AAC5B,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAE1D,eAAO,MAAM,IAAI,aAAwD,CAAC"}
package/dist/ids.js ADDED
@@ -0,0 +1,24 @@
1
+ import { z } from 'zod';
2
+ // Branded ID types: invalid states unrepresentable at the SDK boundary (§5.6/§5.8).
3
+ // ULIDs everywhere — sortable, opaque, no PII. IDs are logged outside jurisdictions
4
+ // by Cloudflare (billing/debug), so they must never encode meaning.
5
+ const ULID = /^[0-9A-HJKMNP-TV-Z]{26}$/;
6
+ export const tenantId = z.string().regex(ULID).brand();
7
+ export const scopeId = z.string().regex(ULID).brand();
8
+ export const principalId = z.string().regex(ULID).brand();
9
+ export const eventId = z.string().regex(ULID).brand();
10
+ export const dataSubjectId = z.string().regex(ULID).brand();
11
+ // npm-package-shaped, e.g. '@substrat-run/engine-workorder'
12
+ export const moduleId = z
13
+ .string()
14
+ .regex(/^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/)
15
+ .brand();
16
+ // ISO 8601 with timezone (Z or offset). Stamped kernel-side, never caller-side.
17
+ export const instant = z.string().datetime({ offset: true }).brand();
18
+ // Module-namespaced permission key, e.g. 'workorder:create'
19
+ export const permissionKey = z
20
+ .string()
21
+ .regex(/^[a-z0-9-]+:[a-z0-9-]+$/)
22
+ .brand();
23
+ export const slug = z.string().regex(/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/);
24
+ //# sourceMappingURL=ids.js.map
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AC,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,mCAAmC,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from './ids.js';
2
+ export * from './tenancy.js';
3
+ export * from './permission.js';
4
+ export * from './events.js';
5
+ export * from './manifest.js';
6
+ export * from './money.js';
7
+ export * from './attachments.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from './ids.js';
2
+ export * from './tenancy.js';
3
+ export * from './permission.js';
4
+ export * from './events.js';
5
+ export * from './manifest.js';
6
+ export * from './money.js';
7
+ export * from './attachments.js';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC"}