@hyperscale0/udl 4.8.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +4 -13
  3. package/dist/diagnostics.d.ts +1 -15
  4. package/dist/diagnostics.d.ts.map +1 -1
  5. package/dist/diagnostics.js +0 -12
  6. package/dist/diagnostics.js.map +1 -1
  7. package/dist/index.d.ts +1 -4
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +1 -4
  10. package/dist/index.js.map +1 -1
  11. package/dist/object-contract.d.ts +0 -52
  12. package/dist/object-contract.d.ts.map +1 -1
  13. package/dist/object-contract.js +0 -19
  14. package/dist/object-contract.js.map +1 -1
  15. package/dist/object-semantics-schema.d.ts +1 -0
  16. package/dist/object-semantics-schema.d.ts.map +1 -1
  17. package/dist/schema.d.ts +4 -0
  18. package/dist/schema.d.ts.map +1 -1
  19. package/dist/schema.js +2 -0
  20. package/dist/schema.js.map +1 -1
  21. package/package.json +4 -8
  22. package/spec/README.md +12 -0
  23. package/spec/udl.schema.json +12 -0
  24. package/src/diagnostics.ts +1 -18
  25. package/src/index.ts +1 -13
  26. package/src/object-contract.ts +0 -47
  27. package/src/schema.ts +2 -0
  28. package/dist/cli.d.ts +0 -3
  29. package/dist/cli.d.ts.map +0 -1
  30. package/dist/cli.js +0 -184
  31. package/dist/cli.js.map +0 -1
  32. package/dist/evolution.d.ts +0 -5
  33. package/dist/evolution.d.ts.map +0 -1
  34. package/dist/evolution.js +0 -27
  35. package/dist/evolution.js.map +0 -1
  36. package/dist/instrument-references.d.ts +0 -5
  37. package/dist/instrument-references.d.ts.map +0 -1
  38. package/dist/instrument-references.js +0 -90
  39. package/dist/instrument-references.js.map +0 -1
  40. package/dist/parser.d.ts +0 -4
  41. package/dist/parser.d.ts.map +0 -1
  42. package/dist/parser.js +0 -67
  43. package/dist/parser.js.map +0 -1
  44. package/docs/README.md +0 -282
  45. package/src/cli.ts +0 -196
  46. package/src/evolution.ts +0 -46
  47. package/src/instrument-references.ts +0 -123
  48. package/src/parser.ts +0 -80
package/docs/README.md DELETED
@@ -1,282 +0,0 @@
1
- # UDL 4
2
-
3
- UDL is the Universal Domain Language, the typed contract between an HSX program
4
- and its executor. The grammar
5
- lives in `src/schema.ts`. Generate `udl.schema.json` with
6
- `bun scripts/emit-spec.ts --write`. There is no migration reader for earlier UDL.
7
-
8
- ## Reading a compiled program
9
-
10
- Start with `product`, `title` and `currency`. `objects` describes business records
11
- and their attachments. Follow an attachment's instrument identity into
12
- `instruments` for its retained fields, lifecycle and actions. `actionOrder`
13
- preserves authored action order even though canonical JSON sorts object keys.
14
- An action's `publicAction` supplies its exposed name; its actor and requirements
15
- still control who may execute it and when.
16
-
17
- Object metadata describes the subject. Instrument fields retain the agreement's
18
- terms, account bindings and receipts. Party names identify authenticated or
19
- configured participants. None of these makes a caller-supplied account ID valid.
20
- A Build freezes this contract and its bindings for the executor.
21
-
22
- Business instructions are actions composed from typed requirements and effects.
23
- The four `internal_transfer` instructions are their money effects. The contract
24
- expresses valid conditions; execution can still refuse for the current state,
25
- authority, evidence or balance. Parsing and validation execute nothing.
26
-
27
- ## JSON admission and canonical bytes
28
-
29
- The parser refuses duplicate JSON member names, including escaped spellings of
30
- one name. Decoded documents must contain finite JSON data. Accessors, cycles,
31
- sparse arrays, symbol keys, hidden properties and `__proto__` members refuse
32
- before schema parsing. Shared values count once per occurrence toward the budget.
33
- Admission permits at most 100,000 visited values, 32 nesting levels, 240 code units
34
- per key and 1 MiB of UTF-8 key and string data. Source text has a separate 1 MiB
35
- byte limit. These bounds live in `src/limits.ts`.
36
-
37
- Canonical JSON sorts object keys recursively, preserves array order and ends
38
- with a newline. Schema defaults are part of the admitted document. Equivalent
39
- key insertion orders and explicit defaults have the same digest. Object field
40
- compatibility and evolution comparisons use the same key ordering.
41
-
42
- ## Objects and subjects
43
-
44
- An object kind declares identity, title, authored field names, normalized fields
45
- and display columns. Each attachment freezes its instrument identity and party
46
- parameter bindings to `owner`, `actor`, `operator`, or a declared Product party. Core resolves role bindings from the object row, session and Product, and
47
- declared parties from the Product party binding. `authoredFields` records only names declared in the HSX
48
- object block. `fields` is their union with attached action requirements. Matching
49
- names must have matching types and constraints. Account fields belong to
50
- instruments, never objects.
51
-
52
- An instrument may name its object kind with `subject`. An action's `subject`
53
- contains its requirements and any frozen adapter binding. Each requirement keeps
54
- its source field and optional `objectField` rename. Adapter snapshots contain
55
- provider, capability, operation, declaration digest and requirements. A null snapshot
56
- marks an unbound adapter. It permits discovery but blocks that action. No provider
57
- requirement is inferred from an operation name.
58
-
59
- Object creation accepts `{}`. Its optional `fields` property accepts every
60
- normalized field as optional. Requirements become mandatory only when the
61
- attached action runs. Object creation performs no financial action. Instrument
62
- creation remains internal; an attached create action may expose a public name.
63
- Attachment parties resolve from authenticated authority, never from party account
64
- IDs supplied in a caller body.
65
-
66
- `object-contract.ts` projects `ObjectDiscovery` for
67
- `GET /v1/products/{productId}/objects`. It exports the discovery, instance, list
68
- and action response types. Discovery preserves Build identity, authored field
69
- names, the full optional create schema and each exposed action's requirements.
70
- Object instances carry `objectId`, `kind`, `revision`, optional `externalId`,
71
- `fields` and `productBuildId`. Action responses carry the object and either a
72
- financial instrument outcome or null.
73
-
74
- ## Object operations
75
-
76
- The host owns transport routes for object discovery, creation, listing,
77
- retrieval, action availability, execution and agreement evidence.
78
- `src/object-contract.ts` defines the language package's object projections.
79
-
80
- Execution binds `productBuildId`, `digest`, `target` and `expectedRevision`.
81
- The target identifies an attachment or an existing instance on that attachment.
82
- The optional `fields` collect subject metadata; optional `input` carries the action's typed input.
83
- Missing requirements refuse execution. Callers reuse an idempotency key only for
84
- retries of the same request. Staff Create for may name `ownerPrincipalId` through
85
- its authorized path; ordinary ownership comes from the session.
86
-
87
- ## Accounts and money
88
-
89
- A document declares SAR once. Money is a nonnegative minor-unit decimal string
90
- of at most 18 digits. Percentages are basis points, durations are positive integer
91
- milliseconds, and dates are timestamps with explicit offsets.
92
-
93
- An account field binds a party, the instrument itself, or an adapter. Party owners
94
- are names; `owner: "self"` provisions an account per instance. An adapter owner
95
- is `{ "adapter": "insurer" }`, where `insurer` names an adapter declared in one
96
- of the instrument's action subjects. The agreement retains that declaration's
97
- provider identity. Missing or conflicting bindings refuse with
98
- `subject_adapter_unbound`; changing an existing account binding refuses with
99
- `account_binding_changed`. A null declaration can describe a draft but cannot
100
- create an adapter-owned account.
101
-
102
- Account creation and binding are executor work. Callers never supply account IDs.
103
- Accounts declare `book: "cash" | "claim"`, defaulting to cash. Moves never cross books.
104
- Only claim accounts may declare `contra: true` and permit a negative balance.
105
- Party and adapter accounts share owner, book and key within a Product; the key
106
- defaults to `balance`. Adapter aliases for the same provider share that account. Transfers between
107
- known aliases of one account refuse, including self fields with the same key.
108
- The executor scopes provider accounts to the tenant and Product, with the Product
109
- participant as ledger custodian. Providers are not parties. An account owned by
110
- self defaults its key to the field name. Named capital, premium, income, debt and
111
- loss balances use explicit keys. `party.buyer` is the buyer's default cash account.
112
-
113
- A plain move to an adapter account credits its ledger balance. It does not prove
114
- an external payout. Provider confirmation still uses a boundary reservation and
115
- instruction-bound evidence. Account ownership adds no settlement operation.
116
-
117
- Disbursement moves cash from lender to borrower and claims from borrower debt to
118
- principal and profit receivables. Repayment moves borrower cash to lender cash and
119
- the same claim amounts from receivables back to borrower debt. Unearned profit is
120
- cancelled by the claim move alone. Write-off moves the principal claim to the
121
- lender's loss account, with no cash movement. The library declares when profit
122
- is earned: on payment, by schedule or at disbursement.
123
- These are ordinary paired moves in HSX, not executor loan rules.
124
-
125
- A value is `{literal: value}` or `{field: path}`. Paths start with `self`, `input`, `subject`
126
- or `party`. Reference fields allow typed traversal. Account paths expose locked,
127
- read-only `.balance` and `.reserved` money values. `self.id`, `self.status`,
128
- `self.createdAt` and `self.now` are sealed executor values. Callers cannot set
129
- constants, calculated fields, account bindings or capture fields. Create supplies
130
- declared typed references; later actions cannot replace them. A ref declares `targetKind: "object" | "instrument"` and one target id or
131
- a list of 1 to 16 distinct target ids in that namespace. Its stored value is an
132
- identity from one listed target. Path traversal exposes only fields
133
- with compatible types on every target. Nested refs combine their target sets;
134
- accounts must agree on owner, key, book and contra flags, and enums must
135
- have the same values. A comparison or selection anchor must share a possible
136
- reference target. An invoked input must accept every possible supplied target.
137
- The executor checks the actual instance type at admission.
138
-
139
- ## Calculation and movement
140
-
141
- Calculations form a finite dependency graph. `sum`, `subtract` and `minimum`
142
- operate on money or integer fields, with operands of the same type as the target.
143
- `rate`, `multiply`, `divide` and `shift` retain their typed operands. A shift's
144
- `milliseconds` operand is a duration field or a non-negative safe integer literal.
145
- Zero leaves the date unchanged; duration fields still require positive values. Rate and
146
- division round down. Weighted shares also round down; residual minor units go
147
- to the declared residual account. Cash and loss use this one rounding rule.
148
- There is no largest-remainder allocation. Subtraction refuses a negative result. Integer results must
149
- be safe integers. No calculation evaluates source text.
150
-
151
- The only move instructions are `internal_transfer.create`,
152
- `internal_transfer.reserve`, `internal_transfer.post` and `internal_transfer.void`.
153
- Create and reserve declare an amount, from account and to account. Reserve captures
154
- its executor-produced transfer identity into a declared self text field. Post and
155
- void consume that identity. Callers cannot create or replace captured identities.
156
- Adapter-owned endpoints use the same move vocabulary.
157
-
158
- Each move may declare `economics: { purpose, sourceParty, reversalOf? }`.
159
- Purpose is `earning`, `principal`, `participant_payout`, `internal`,
160
- `prepaid_credit`, or `pass_through`. Pass-through money is excluded from earnings
161
- and costs. `sourceParty` names an authored party whose retained
162
- participant must own the debit account. For an earning realized from company-held
163
- customer money, it identifies the retained payer instead. The debit must be an
164
- instrument account bound to the same agreement or a retained referenced agreement
165
- in the same Build with the same payer; the company credit must be outside the
166
- agreement. Evidence also retains the actual debit account participant.
167
- `reversalOf` is a transfer reference path. A reversal preserves purpose, Product, Build and agreement, debits the
168
- original recipient account and credits the original source participant. The
169
- combined refunds cannot exceed the original posted amount. A post inherits its
170
- reservation's economics; it cannot change them.
171
- The terminal ledger evidence keeps these fields with the agreement, Build,
172
- account participants, and move key. A move without economics has no inferred
173
- classification.
174
-
175
- A loan, refund, payoff, write-off or distribution is library behavior built from
176
- accounts, calculations and ordered moves. None has a privileged executor clause.
177
- Outstanding principal is an account balance. A schedule consists of explicit
178
- dated child records, with positions 1 through n in declaration order. The library
179
- uses ordinary comparisons and aggregates to constrain those records. There is no
180
- recurrence process, allocation bucket, partition expander or schedule requirement
181
- in the UDL kernel.
182
-
183
- ## Admission and lifecycle
184
-
185
- Actions declare typed input lists, requirements, an actor and an event. Lifecycle
186
- edges name their source and destination states. Requirements and effects execute
187
- atomically under the same account and reference locks. `set` copies typed values
188
- to mutable fields. Action calculations read the locked snapshot and populate the
189
- action draft before requirements. Ordered moves and invocations follow admission;
190
- a zero create move refuses unless the action declares `allowZero: true`.
191
- With that permission it records no transfer or capture. A reserve amount must
192
- always be positive.
193
- Invariants check the completed transaction. A due instant
194
- is inclusive; a deadline is exclusive. Clock delays never extend deadlines.
195
-
196
- Requirements are compare, state, unique, aggregate, evidence and hours.
197
- A typed selection names one instrument or a bounded union, a reference field,
198
- anchor, accepted states and row limit. Exceeding the limit refuses rather than
199
- truncates. Optional equality filters apply to every selected type. An optional
200
- window selects date values between `self.now - milliseconds` and `self.now`.
201
- Aggregate sums use typed paths on the selected records, including account
202
- balances. An invariant holds before and after every affected transaction.
203
-
204
- `hours` converts a date path to its literal IANA timezone and accepts `[start,end)`.
205
- A start greater than end wraps midnight; equal endpoints admit no time.
206
- `evidence` declares subject, family, check, result and maxAge. The subject is an
207
- account or text id. The executor selects the newest matching subject binding,
208
- checks its retained Build, protected input digest and adapter declaration, then
209
- requires the declared result and age. A newer matching refusal or pending result
210
- prevents reuse of an older success.
211
-
212
- `invoke` supplies typed inputs to a linked action or bounded selection. Its graph
213
- is acyclic and bounded. Public names grant no authority; clock and parent actors
214
- remain executor-owned. A public action on a subject instrument requires an
215
- attachment on that object kind. State requirements refer to instrument instances;
216
- objects have no instrument lifecycle. List extraction preserves the reference
217
- namespace and target. Selection anchors and filters participate in calculation
218
- cycle detection. Literal assignments must satisfy the destination field's constraints.
219
-
220
- A captured transfer exposes its executor-owned `.status`. A boundary payout
221
- reserves before dispatch. Only matching terminal confirmation can post or void
222
- the reservation. An acknowledgement, transport failure or missing response
223
- keeps the reservation. A refund is a separately funded move. Account balances
224
- alone do not prove an external effect. The language adds no automatic
225
- provider-failure reversal or deadline settlement policy.
226
-
227
- ## Ten laws
228
-
229
- 1. Each concept has one concrete meaning.
230
- 2. Provider transport and credentials stay outside the contract.
231
- 3. Every state change emits its declared event.
232
- 4. Four transfer instructions are the only money movement vocabulary.
233
- 5. Instances have an opaque identity and money uses integer minor units.
234
- 6. Typed requirements and effects execute atomically.
235
- 7. Live additions preserve existing meaning; development estates may recreate.
236
- 8. Business names identify object kinds, attached instruments and public actions.
237
- 9. Waiting uses lifecycle states and clocks. Before admission, the executor
238
- catches up actor: clock actions with due instants on the locked instance,
239
- references and selected rows, in chronological order. Catch-up commits its
240
- own transaction and events before the caller request is evaluated. A refused
241
- caller request rolls back only its own drafts. Caller deadlines are refusal
242
- boundaries; they never execute the caller action. Separate clock actions
243
- carry expiry consequences. Requirements and selections observe the resulting
244
- clock state.
245
- 10. References close, states are reachable, captures are linear, and terminal
246
- instances have no remaining owned-account balances or reservations.
247
-
248
- ## Clause inventory
249
-
250
- The generated [JSON schema](../spec/udl.schema.json) owns the complete field
251
- inventory, defaults and constraints. `src/schema.ts` is its source.
252
-
253
- `calculate.aggregate` reads a typed selection and yields its count or a money sum. `calculate.ratio` computes floor(amount * numerator / denominator) with arbitrary-precision intermediates and refuses a zero denominator. Numerator and denominator share a numeric type. Selection order is a list of typed ascending paths, followed by identity as the final tie-break. `invoke {instrument, action: "create", input}` creates a child record in the same transaction; its inputs resolve in the caller, and the ordinary create actor and requirements still apply.
254
-
255
- `calculate.at` reads a typed list at a one-based position and refuses an out-of-range index. Its result has the list item type. Integer divide accepts integer operands and rounds down. Every move may capture its transfer identity into a declared self text field; reserve requires a capture. Captures and their status paths are executor-owned.
256
-
257
- ## Reporting definitions
258
-
259
- An instrument may declare `reports`, a bounded array of strict ReportDefinition
260
- values. Each definition contains identity/version, scope, datasets, time,
261
- selection, calculation, validation, output and authority. The JSON schema defines
262
- all fields. Expressions form an ordered graph with backward references, typed
263
- money in integer minor units, explicit ratios and date operations. Source fields
264
- must exist in every bound instrument. A scalar field cannot acquire a suffix;
265
- account balances use the declared `<account>.balance` path. Company instrument reports require
266
- cross-Build bindings and are not admitted in this version.
267
-
268
- Joins declare one-or-many cardinality, missing-record policy and aggregates;
269
- implicit row multiplication is forbidden. Compilation checks currencies, column
270
- and expression types, output columns and sort keys. Execution must check complete
271
- source populations, required facts, unique row and sort identities, reconciliation
272
- and declared limits before publishing any result. Empty-population behavior and
273
- unavailable facts are definition data. Request, read and release roles are
274
- separate. Independent activation and retained deterministic replay belong to the
275
- host; language acceptance alone does not authorize an artifact or external release.
276
-
277
- Reporting source selections and foreign join keys must be computable from
278
- PostgreSQL facts; ledger balances and ratios belong after capture. Aggregate
279
- names are unique across stages. `average` requires an explicit `rounding` policy
280
- (`floor` or `halfUp`) to retain integer and money minor-unit types. Numeric and
281
- min/max aggregates skip nulls, return null for all-null inputs, and emit per-row
282
- `_skippedNulls_<name>` counters in JSON and CSV. Required facts still refuse nulls.
package/src/cli.ts DELETED
@@ -1,196 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * The `udl` command. Actions over the same library the compiler uses:
4
- * `validate` answers whether a document is admissible, `fmt` writes the
5
- * canonical bytes, and `diff` answers whether a change is legal against a
6
- * document that already has live instances.
7
- */
8
- import { readFile, writeFile } from "node:fs/promises";
9
-
10
- import {
11
- canonicalizeUdl,
12
- canonicalDigest,
13
- diffValidatedUdlEvolution,
14
- parseUdl,
15
- UdlError,
16
- type UdlDocument,
17
- udlDiagnostic,
18
- } from "./index.js";
19
-
20
- const USAGE = `udl - the Universal Domain Language toolchain
21
-
22
- usage:
23
- udl validate <file> parse <file> and report every issue found
24
- udl fmt <file> [--write] print the canonical form, or rewrite the file
25
- udl canon <file> [--digest] print canonical bytes, or their SHA-256 digest
26
- udl diff <live> <next> check <next> against the append-only law
27
- udl explain UDL#### explain one stable diagnostic code
28
- udl help print this
29
-
30
- exit codes:
31
- 0 the document is admissible, or the change is additive
32
- 1 the document was refused, or the change breaks the append-only law
33
- 2 the command line was wrong, or a file could not be read`;
34
-
35
- async function main(argv: readonly string[]): Promise<number> {
36
- const [command, ...rest] = argv;
37
- switch (command) {
38
- case "help":
39
- case "-h":
40
- case "--help":
41
- console.log(USAGE);
42
- return 0;
43
- case "validate":
44
- return validateCommand(rest);
45
- case "fmt":
46
- return formatCommand(rest);
47
- case "canon":
48
- return canonCommand(rest);
49
- case "diff":
50
- return diffCommand(rest);
51
- case "explain":
52
- return explainCommand(rest);
53
- case undefined:
54
- return usageError("udl: no command given");
55
- default:
56
- return usageError(`udl: unknown command ${command}`);
57
- }
58
- }
59
-
60
- async function canonCommand(args: readonly string[]): Promise<number> {
61
- const digest = args.includes("--digest");
62
- const [file, ...extra] = args.filter((argument) => argument !== "--digest");
63
- if (file === undefined || extra.length > 0) {
64
- return usageError("usage: udl canon <file> [--digest]");
65
- }
66
- const bytes = await readDocument(file);
67
- if (bytes === null) return 2;
68
- let document: UdlDocument;
69
- try {
70
- document = parseUdl(bytes);
71
- } catch (error) {
72
- return reportIssues(file, error);
73
- }
74
- if (digest) console.log(await canonicalDigest(document));
75
- else process.stdout.write(canonicalizeUdl(bytes));
76
- return 0;
77
- }
78
-
79
- function explainCommand(args: readonly string[]): number {
80
- const [code, ...extra] = args;
81
- if (code === undefined || extra.length > 0) {
82
- return usageError("usage: udl explain UDL####");
83
- }
84
- const diagnostic = udlDiagnostic(code);
85
- if (!diagnostic) {
86
- return usageError(`udl: unknown diagnostic code ${code}`);
87
- }
88
- console.log(`${code} ${diagnostic.title}`);
89
- console.log(`category: ${diagnostic.category}`);
90
- console.log(`family: ${diagnostic.family}`);
91
- console.log(`fix: ${diagnostic.fix}`);
92
- return 0;
93
- }
94
-
95
- async function validateCommand(args: readonly string[]): Promise<number> {
96
- const [file, ...extra] = args;
97
- if (file === undefined || extra.length > 0) {
98
- return usageError("usage: udl validate <file>");
99
- }
100
- const bytes = await readDocument(file);
101
- if (bytes === null) return 2;
102
- if (parseDocument(file, bytes) === null) return 1;
103
- console.log(`${file}: ok`);
104
- return 0;
105
- }
106
-
107
- async function formatCommand(args: readonly string[]): Promise<number> {
108
- const write = args.includes("--write");
109
- const [file, ...extra] = args.filter((argument) => argument !== "--write");
110
- if (file === undefined || extra.length > 0) {
111
- return usageError("usage: udl fmt <file> [--write]");
112
- }
113
- const bytes = await readDocument(file);
114
- if (bytes === null) return 2;
115
-
116
- let canonical: string;
117
- try {
118
- canonical = canonicalizeUdl(bytes);
119
- } catch (error) {
120
- return reportIssues(file, error);
121
- }
122
-
123
- if (!write) {
124
- process.stdout.write(canonical);
125
- return 0;
126
- }
127
- // The bytes decoded cleanly already: canonicalizeUdl refuses anything else.
128
- if (new TextDecoder().decode(bytes) === canonical) return 0;
129
- await writeFile(file, canonical);
130
- console.log(`${file}: formatted`);
131
- return 0;
132
- }
133
-
134
- async function diffCommand(args: readonly string[]): Promise<number> {
135
- const [livePath, nextPath, ...extra] = args;
136
- if (livePath === undefined || nextPath === undefined || extra.length > 0) {
137
- return usageError("usage: udl diff <live> <next>");
138
- }
139
- const liveBytes = await readDocument(livePath);
140
- const nextBytes = await readDocument(nextPath);
141
- if (liveBytes === null || nextBytes === null) return 2;
142
-
143
- const live = parseDocument(livePath, liveBytes);
144
- const next = parseDocument(nextPath, nextBytes);
145
- if (live === null || next === null) return 1;
146
-
147
- // `parseDocument` already validated both, so take the door that does not
148
- // validate a second time.
149
- const violations = diffValidatedUdlEvolution(live, next);
150
- if (violations.length === 0) {
151
- console.log(`${nextPath}: additive`);
152
- return 0;
153
- }
154
- for (const violation of violations) {
155
- console.error(
156
- `${nextPath}: ${violation.code} ${violation.path}: ${violation.message}`,
157
- );
158
- }
159
- return 1;
160
- }
161
-
162
- async function readDocument(file: string): Promise<Uint8Array | null> {
163
- try {
164
- return await readFile(file);
165
- } catch (error) {
166
- const reason = error instanceof Error ? error.message : String(error);
167
- console.error(`udl: cannot read ${file}: ${reason}`);
168
- return null;
169
- }
170
- }
171
-
172
- /** Parses one document, printing its issues. Null means the document was refused. */
173
- function parseDocument(file: string, bytes: Uint8Array): UdlDocument | null {
174
- try {
175
- return parseUdl(bytes);
176
- } catch (error) {
177
- reportIssues(file, error);
178
- return null;
179
- }
180
- }
181
-
182
- function reportIssues(file: string, error: unknown): number {
183
- if (!(error instanceof UdlError)) throw error;
184
- for (const issue of error.issues) {
185
- console.error(`${file}: ${issue.code} ${issue.path}: ${issue.message}`);
186
- }
187
- return 1;
188
- }
189
-
190
- function usageError(message: string): number {
191
- console.error(message);
192
- console.error(USAGE);
193
- return 2;
194
- }
195
-
196
- process.exitCode = await main(process.argv.slice(2));
package/src/evolution.ts DELETED
@@ -1,46 +0,0 @@
1
- import type { UdlDocument } from "./schema.js";
2
- import { issue, type UdlIssue } from "./diagnostics.js";
3
- import { writeJson } from "./json.js";
4
-
5
- /** Existing instruments are frozen; a live document may add independent instruments. */
6
- export function diffValidatedUdlEvolution(
7
- live: UdlDocument,
8
- next: UdlDocument,
9
- ): UdlIssue[] {
10
- const issues: UdlIssue[] = [];
11
- const refuse = (path: string, message: string) =>
12
- issues.push(issue("UDL7001", path, message));
13
- if (live.product !== next.product || live.currency !== next.currency)
14
- refuse("$", "product identity and currency are immutable");
15
- for (const [name, party] of Object.entries(live.parties))
16
- if (
17
- !Object.hasOwn(next.parties, name) ||
18
- writeJson(party) !== writeJson(next.parties[name])
19
- )
20
- refuse(`$.parties.${name}`, "a live party cannot change or disappear");
21
- for (const object of live.objects) {
22
- const updated = next.objects.find((o) => o.id === object.id);
23
- if (!updated || writeJson(object) !== writeJson(updated))
24
- refuse(
25
- `$.objects.${object.id}`,
26
- "a live object kind cannot change or disappear; recreate development estates",
27
- );
28
- }
29
- for (const instrument of live.instruments) {
30
- const updated = next.instruments.find((i) => i.id === instrument.id);
31
- if (!updated || writeJson(instrument) !== writeJson(updated))
32
- refuse(
33
- `$.instruments.${instrument.id}`,
34
- "a live instrument cannot change or disappear; recreate development estates",
35
- );
36
- }
37
- if (writeJson(live) !== writeJson(next) && next.version <= live.version)
38
- issues.push(
39
- issue(
40
- "UDL7002",
41
- "$.version",
42
- "a changed document needs a higher version",
43
- ),
44
- );
45
- return issues;
46
- }
@@ -1,123 +0,0 @@
1
- import * as z from "zod";
2
- import {
3
- udlDocumentSchema,
4
- type UdlDocument,
5
- type UdlInstrument,
6
- } from "./schema.js";
7
-
8
- type Schema = Record<string, unknown>;
9
- const object = (value: unknown): value is Schema =>
10
- value !== null && typeof value === "object" && !Array.isArray(value);
11
- let documentSchema: Schema | undefined;
12
-
13
- /** Visit identities through schema annotations, never through user JSON keys. */
14
- export function mapUdlInstrumentReferences(
15
- document: UdlDocument,
16
- map: (id: string) => string,
17
- ): UdlDocument {
18
- const root = (documentSchema ??= z.toJSONSchema(udlDocumentSchema));
19
- const result = structuredClone(document);
20
- const locations = new Map<
21
- string,
22
- { parent: Schema | unknown[]; key: string | number; kind: string }
23
- >();
24
- function visit(
25
- schema: unknown,
26
- value: unknown,
27
- parent: Schema | unknown[],
28
- key: string | number,
29
- path: readonly (string | number)[],
30
- ): void {
31
- if (!object(schema)) return;
32
- if (typeof schema.$ref === "string") {
33
- if (!schema.$ref.startsWith("#/"))
34
- throw new Error("UDL reference schema must be local");
35
- let target: unknown = root;
36
- for (const part of schema.$ref.slice(2).split("/")) {
37
- target = object(target)
38
- ? target[part.replaceAll("~1", "/").replaceAll("~0", "~")]
39
- : undefined;
40
- }
41
- visit(target, value, parent, key, path);
42
- }
43
- const kind = schema["x-udl-reference"];
44
- if (typeof kind === "string" && typeof value === "string")
45
- locations.set(JSON.stringify(path), { parent, key, kind });
46
- for (const union of [schema.anyOf, schema.oneOf, schema.allOf])
47
- if (Array.isArray(union))
48
- for (const branch of union) visit(branch, value, parent, key, path);
49
- if (Array.isArray(value)) {
50
- value.forEach((entry, index) =>
51
- visit(schema.items, entry, value, index, [...path, index]),
52
- );
53
- } else if (object(value)) {
54
- const properties = object(schema.properties) ? schema.properties : {};
55
- const discriminator = object(properties.type)
56
- ? properties.type.const
57
- : undefined;
58
- if (
59
- (discriminator === "ref" || discriminator === "list") &&
60
- value.type === discriminator &&
61
- value.targetKind === "instrument"
62
- ) {
63
- if (typeof value.target === "string")
64
- locations.set(JSON.stringify([...path, "target"]), {
65
- parent: value,
66
- key: "target",
67
- kind: "instrument",
68
- });
69
- else if (Array.isArray(value.target))
70
- value.target.forEach((_, index) =>
71
- locations.set(JSON.stringify([...path, "target", index]), {
72
- parent: value.target as unknown[],
73
- key: index,
74
- kind: "instrument",
75
- }),
76
- );
77
- }
78
- for (const [name, entry] of Object.entries(value))
79
- visit(
80
- properties[name] ?? schema.additionalProperties,
81
- entry,
82
- value,
83
- name,
84
- [...path, name],
85
- );
86
- }
87
- }
88
- visit(root, result, [result], 0, []);
89
- for (const { parent, key, kind } of locations.values()) {
90
- const holder = parent as Record<string | number, unknown>;
91
- const value = holder[key] as string;
92
- if (kind === "instrument") holder[key] = map(value);
93
- else if (kind === "instrument_event") {
94
- const separator = value.indexOf(".");
95
- holder[key] = map(value.slice(0, separator)) + value.slice(separator);
96
- }
97
- }
98
- return result;
99
- }
100
-
101
- export function referencedUdlInstrumentIds(
102
- instrument: UdlInstrument,
103
- ): readonly string[] {
104
- const found = new Set<string>();
105
- mapUdlInstrumentReferences(
106
- {
107
- udl: 4,
108
- version: 1,
109
- product: "references",
110
- title: "References",
111
- currency: "SAR",
112
- parties: {},
113
- objects: [],
114
- instruments: [instrument],
115
- },
116
- (id) => {
117
- found.add(id);
118
- return id;
119
- },
120
- );
121
- found.delete(instrument.id);
122
- return [...found];
123
- }