@mark1russell7/stack-risk 0.0.0-stage → 0.1.2

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mark Russell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,240 @@
1
- # Temporary Holding Version
1
+ # @mark1russell7/stack-risk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The risks of a system stack as queries. The package tells which failures stop a part, how many failures the part survives, and the chance that it works. It has a gate that fails when a part does not meet its goal.
4
+
5
+ It is the risk add-on of [`@mark1russell7/system-stack`](../system-stack/README.md). The core knows only parts and needs. This package puts a goal on a part.
6
+
7
+ Nobody keeps a list of risks by hand. Each risk is a result of a query over the records, so it changes when the stack changes.
8
+
9
+ Each function is a plain function with no clock and no network.
10
+
11
+ ## The rule
12
+
13
+ A goal tells how many failures a part must survive. The gate finds each set of parts whose failure stops the part. A set of the size of the goal, or smaller, is a fault.
14
+
15
+ The owner can accept a set, with a reason and a last date. Thus each risk of the system is in one of two states: a fault of the gate, or a record with a reason.
16
+
17
+ A search that passes its limit gives no result. The gate then fails, because no result is not a good result.
18
+
19
+ ## The entries
20
+
21
+ | Entry | What it has |
22
+ | --- | --- |
23
+ | `@mark1russell7/stack-risk` | The records of a layer, the states of the goals, the chance, the gate and a sample. |
24
+ | `@mark1russell7/stack-risk/schema` | `riskSchema`, the zod schema of a layer, and `parseRisk`. |
25
+ | `@mark1russell7/stack-risk/add-on` | `riskAddOn`, the add-on in the form of the layer contract of the core. |
26
+
27
+ The root entry has no schema library. Thus a page that only shows the states gets no zod.
28
+
29
+ ## Install
30
+
31
+ ```sh
32
+ npm install @mark1russell7/stack-risk @mark1russell7/system-stack
33
+ ```
34
+
35
+ ## A layer
36
+
37
+ A risk layer is one record with goals, chances and accepted risks. Its format has the name `risk/1`. A stack keeps it in `Stack.layers`, with the key `risk/1`.
38
+
39
+ ```ts
40
+ import type { RiskLayer } from "@mark1russell7/stack-risk";
41
+
42
+ const layer: RiskLayer = {
43
+ schema: "risk/1",
44
+ goals: [{ part: "alert", survives: 1, chance: 0.99 }],
45
+ chances: [
46
+ { part: "reader", works: 0.97, source: "An estimate: the hours when the reader cannot read a message" },
47
+ { part: "phone", works: 0.995, source: "An estimate: the hours with no battery or no signal" },
48
+ { part: "cloud-host", works: 0.999, source: "The service agreement of the provider" },
49
+ // The sample has a record for each part of the paths of the alert.
50
+ ],
51
+ accepted: [{ part: "alert", stop: ["reader"], reason: "One person reads the alerts. A second reader is a later step.", until: "2027-06-30" }],
52
+ };
53
+ ```
54
+
55
+ `SAMPLE_RISK` of the package is this layer with each chance record. It is for `SAMPLE_STACK` of the core.
56
+
57
+ | Field of a goal | What it is |
58
+ | --- | --- |
59
+ | `part` | The `id` of the part with the goal, for example a function of the system. |
60
+ | `survives` | The count of failures of other parts that the part survives. With 1, no failure of one part stops the part. |
61
+ | `chance` | The smallest chance that the part works, from 0 to 1. |
62
+
63
+ | Field of a chance record | What it is |
64
+ | --- | --- |
65
+ | `part` | The `id` of the part. |
66
+ | `works` | The chance that the part works at a moment, when each of its needs holds. |
67
+ | `source` | The source of the number, for example a service agreement or a measured period. |
68
+
69
+ | Field of an accepted risk | What it is |
70
+ | --- | --- |
71
+ | `part` | The `id` of the part with the goal. |
72
+ | `stop` | The parts of one set, in any sequence. Their failure together stops the part. |
73
+ | `reason` | The reason of the owner. |
74
+ | `until` | The last date of the acceptance. An acceptance without it has no end. |
75
+
76
+ ## The gate
77
+
78
+ `checkRisk(layer, stack, { today })` gives the faults of a layer. An empty list tells that the gate passes. The gate has no clock: the caller gives the date.
79
+
80
+ ```ts
81
+ import { checkRisk, SAMPLE_RISK, SAMPLE_TODAY } from "@mark1russell7/stack-risk";
82
+ import { SAMPLE_STACK } from "@mark1russell7/system-stack";
83
+
84
+ checkRisk(SAMPLE_RISK, SAMPLE_STACK, { today: SAMPLE_TODAY }).map((fault) => `${fault.code}: ${fault.message}`);
85
+ // => [
86
+ // 'stop-set: The failure of the set ["phone"] stops "alert". The goal of "alert" is to survive each set of 1 or fewer parts.',
87
+ // 'chance-low: The chance that "alert" works is 0.9649835920528091. The goal is 0.99.',
88
+ // ]
89
+ ```
90
+
91
+ The failure of the reader alone stops the alert too. A record accepts that set until 2027-06-30, so it is no fault. After that date, the gate gives the fault `accept-old` for the set.
92
+
93
+ The gate examines the design: each part of the stack is in operation. A fault of a stop set has the set in its field `parts`.
94
+
95
+ | Code | The fault |
96
+ | --- | --- |
97
+ | `goal-part` | A goal names no part of the stack. |
98
+ | `goal-duplicate` | Two goals name one part. |
99
+ | `goal-survives` | The count of a goal is not a whole number from 0. |
100
+ | `goal-chance` | The chance of a goal is not from 0 to 1. |
101
+ | `chance-part` | A chance record names no part of the stack. |
102
+ | `chance-duplicate` | Two chance records name one part. |
103
+ | `chance-range` | The chance of a part is not from 0 to 1. |
104
+ | `accept-part` | An accepted risk names a part with no goal. |
105
+ | `accept-stop` | An accepted risk names no part, or a part that is not in the stack. |
106
+ | `accept-date` | The last date of an accepted risk is not a date. |
107
+ | `stopped` | The part of a goal does not work, with each part of the stack in operation. |
108
+ | `stop-set` | The failure of a set stops a part, and the goal is to survive a set of that size. No record accepts the set. |
109
+ | `accept-old` | The acceptance of a stop set ended before the date of the check. |
110
+ | `accept-unused` | An accepted risk names a set that is no stop set of its goal. |
111
+ | `chance-none` | A goal has a chance, and a part of its paths has no chance record. |
112
+ | `chance-low` | The chance that a part works is less than the chance of its goal. |
113
+ | `no-read` | A search passed its limit, so the gate has no result for the goal. |
114
+
115
+ The gate reports only the smallest sets. When the failure of one part stops a part, each larger set with that part is not a second fault.
116
+
117
+ An accepted risk that names no stop set is a fault. Thus a record cannot stay after a change of the stack removed its risk.
118
+
119
+ ## The state at this time
120
+
121
+ The gate examines the design. `goalStates(layer, stack, { down })` gives the state of each goal when the parts of `down` do not work. The parts of `down` come from the monitors of the system.
122
+
123
+ ```ts
124
+ import { goalStates, SAMPLE_RISK } from "@mark1russell7/stack-risk";
125
+ import { SAMPLE_STACK } from "@mark1russell7/system-stack";
126
+
127
+ goalStates(SAMPLE_RISK, SAMPLE_STACK);
128
+ // => [{ part: "alert", read: true, works: true, margin: 0, stop: ["reader"], points: ["reader", "phone"] }]
129
+
130
+ goalStates(SAMPLE_RISK, SAMPLE_STACK, { down: ["program-cloud"] });
131
+ // => [{
132
+ // part: "alert", read: true, works: true, margin: 0, stop: ["reader"],
133
+ // points: ["reader", "phone", "home-power", "home-network", "home-host", "program-home"],
134
+ // }]
135
+ ```
136
+
137
+ | Field of a state | What it is |
138
+ | --- | --- |
139
+ | `works` | If the part works at this time. |
140
+ | `margin` | The count of more failures that the part always survives. It is `Infinity` when no failure of other parts stops the part. |
141
+ | `stop` | A smallest set of parts whose failure stops the part. |
142
+ | `points` | Each part whose failure alone stops the part. |
143
+
144
+ With the copy in the cloud down, the alert still works. But each part of the copy at home is then a single point of failure. A page can show this change before the second failure comes.
145
+
146
+ A state with `read: false` has no result, because the search passed its limit. Do not show it as a good state.
147
+
148
+ ## The chance
149
+
150
+ `worksChance(layer, stack, part)` gives the chance that a part works, from the chance record of each part of its paths.
151
+
152
+ ```ts
153
+ import { SAMPLE_RISK, worksChance } from "@mark1russell7/stack-risk";
154
+ import { SAMPLE_STACK } from "@mark1russell7/system-stack";
155
+
156
+ const chance = worksChance(SAMPLE_RISK, SAMPLE_STACK, "alert");
157
+ // => {
158
+ // kind: "exact",
159
+ // value: 0.9649835920528091,
160
+ // importance: [
161
+ // { part: "reader", importance: 0.9948284454152673 },
162
+ // { part: "phone", importance: 0.9698327558319689 },
163
+ // { part: "alert", importance: 0.9649835920528091 },
164
+ // { part: "card", importance: 0.02187312796942623 },
165
+ // ...
166
+ // ],
167
+ // assumed: [],
168
+ // }
169
+ ```
170
+
171
+ - Each part works or fails alone. A shared cause of failure is thus a part of the stack: a provider, an account or a power feed.
172
+ - The value is exact. A part that two paths share counts one time.
173
+ - The importance of a part is the chance when the part works, minus the chance when the part fails. The list starts with the largest importance.
174
+ - A part with no chance record has the chance 1, and `assumed` names it. The gate gives the fault `chance-none` for such a part.
175
+
176
+ The sample shows where work helps most. The two copies of the program and the two message services have a small importance. The reader and the phone have a large one.
177
+
178
+ The paths, the stop sets and the smallest stop set are functions of the core: `pathSets`, `stopSets` and `smallestStop`.
179
+
180
+ ## The read of JSON
181
+
182
+ `parseRisk` of the entry `/schema` reads the JSON of a layer. It gives the layer, or the faults of its form with the place of each fault.
183
+
184
+ ```ts
185
+ import { parseRisk } from "@mark1russell7/stack-risk/schema";
186
+
187
+ parseRisk({ schema: "risk/1", goals: [{ part: "alert", survives: "1" }] });
188
+ // => { ok: false, errors: ['The record "risk/1" has a fault at "goals.0.survives": Invalid input: expected number, received string'] }
189
+ ```
190
+
191
+ The schema examines the form of a record. `checkRisk` examines the meaning of a layer with a correct form.
192
+
193
+ ## The goals of some owners
194
+
195
+ `joinRisk(pieces)` joins the risk layers of the fragments of a stack into one layer. A piece is the layer of one fragment, with the name of the fragment.
196
+
197
+ - The goals and the chance records are in the order of the fragments. The record of a part is in one fragment.
198
+ - A part with a goal in two or more fragments is a fault. Equal goals give the fault too.
199
+ - A part with a chance record in two or more fragments is a fault. Equal records give the fault too.
200
+ - The accepted risks are in the order of the fragments. A fragment can accept a risk of a goal of a different fragment.
201
+
202
+ ```ts
203
+ import { joinRisk } from "@mark1russell7/stack-risk";
204
+
205
+ const goals = { schema: "risk/1", goals: [{ part: "alert", survives: 1 }], chances: [{ part: "phone", works: 0.99 }] } as const;
206
+ const accepted = [{ part: "alert", stop: ["reader"], reason: "One reader is enough for this year.", until: "2027-06-30" }];
207
+
208
+ joinRisk([
209
+ { from: "a.json", layer: goals },
210
+ { from: "b.json", layer: { schema: "risk/1", goals: [], accepted } },
211
+ ]);
212
+ // => { ok: true, layer: { schema: "risk/1", goals: [{ part: "alert", survives: 1 }], chances: [{ part: "phone", works: 0.99 }], accepted: [{ part: "alert", stop: ["reader"], ... }] } }
213
+
214
+ joinRisk([
215
+ { from: "a.json", layer: goals },
216
+ { from: "b.json", layer: { schema: "risk/1", goals: [], chances: [{ part: "phone", works: 0.9 }], accepted } },
217
+ ]);
218
+ // => { ok: false, errors: ['The fragments "a.json", "b.json" each have the chance record of the part "phone".'] }
219
+ ```
220
+
221
+ The add-on of the entry `/add-on` has the function as `join`. `joinStacks` of the core uses it for a stack in two or more documents, and the gate `checkRisk` examines the joined layer afterward.
222
+
223
+ The sequence of the parts of a stack does not change a chance, to its last digit. Thus the joined stack and one whole document give the same faults.
224
+
225
+ ## The add-on for one check of a stack
226
+
227
+ `riskAddOn` of the entry `/add-on` gives the add-on in the form of the layer contract. A host gives it to `checkStackLayers` of the core, with the add-ons of the other layers. A host with a stack in two or more documents gives it to `joinStacks` first.
228
+
229
+ ```ts
230
+ import { riskAddOn } from "@mark1russell7/stack-risk/add-on";
231
+ import { checkStackLayers } from "@mark1russell7/system-stack";
232
+
233
+ checkStackLayers(stack, [riskAddOn({ today: "2026-11-16" })]);
234
+ // => the faults of the stack, then the faults of `checkRisk` for the layer `risk/1`, each with the name of its layer
235
+ ```
236
+
237
+ A layer with a wrong form gives one finding `layer-form` for each fault of the form, and the gate does not start. The function refuses a date of a wrong form at once, with a `RangeError`.
238
+
239
+ ## License
240
+ MIT
@@ -0,0 +1,16 @@
1
+ import { i as RiskOptions } from "./check-CahTrDAa.js";
2
+ import { a as RiskLayer } from "./layer-zvH_ueL5.js";
3
+ import { StackJoiner } from "@mark1russell7/system-stack";
4
+ /**
5
+ * This function gives the risk add-on for a check on one date.
6
+ *
7
+ * - `schema` is `risk/1`, the key of the layer in `Stack.layers`.
8
+ * - `parse` is `parseRisk`.
9
+ * - `check` is the gate `checkRisk` with the options. It throws a RangeError when `maxSets` or `maxSteps` is not valid.
10
+ * - `join` is `joinRisk`. It joins the risk layers of the fragments of a stack.
11
+ *
12
+ * @param options - The options of the gate. The gate has no clock: the caller gives the date.
13
+ * @throws RangeError when `today` is not a date `YYYY-MM-DD`
14
+ */
15
+ export declare function riskAddOn(options: RiskOptions): StackJoiner<RiskLayer>;
16
+ //# sourceMappingURL=add-on.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"add-on.d.ts","names":[],"sources":["../src/add-on.ts"],"mappings":";;;;;;;;;;;;;;wBAoBgB,UAAU,SAAS,cAAc,YAAY"}
@@ -0,0 +1,28 @@
1
+ import { r as checkRisk, t as joinRisk } from "./join-ClFU2WDq.js";
2
+ import { t as RISK_SCHEMA } from "./layer-BLQnPkuy.js";
3
+ import { parseRisk } from "./schema.js";
4
+ import { dayNumber } from "@mark1russell7/system-stack";
5
+ // src/add-on.ts
6
+ /**
7
+ * This function gives the risk add-on for a check on one date.
8
+ *
9
+ * - `schema` is `risk/1`, the key of the layer in `Stack.layers`.
10
+ * - `parse` is `parseRisk`.
11
+ * - `check` is the gate `checkRisk` with the options. It throws a RangeError when `maxSets` or `maxSteps` is not valid.
12
+ * - `join` is `joinRisk`. It joins the risk layers of the fragments of a stack.
13
+ *
14
+ * @param options - The options of the gate. The gate has no clock: the caller gives the date.
15
+ * @throws RangeError when `today` is not a date `YYYY-MM-DD`
16
+ */
17
+ function riskAddOn(options) {
18
+ if (dayNumber(options.today) === void 0) throw new RangeError(`The date "${options.today}" is not a date of the form YYYY-MM-DD.`);
19
+ return {
20
+ schema: RISK_SCHEMA,
21
+ parse: parseRisk,
22
+ check: (layer, stack) => checkRisk(layer, stack, options),
23
+ join: joinRisk
24
+ };
25
+ }
26
+ export { riskAddOn };
27
+
28
+ //# sourceMappingURL=add-on.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"add-on.js","names":[],"sources":["../src/add-on.ts"],"sourcesContent":["import { dayNumber, type StackJoiner } from \"@mark1russell7/system-stack\";\nimport { checkRisk, type RiskOptions } from \"./check.ts\";\nimport { joinRisk } from \"./join.ts\";\nimport { RISK_SCHEMA, type RiskLayer } from \"./layer.ts\";\nimport { parseRisk } from \"./schema.ts\";\n\n// The risk add-on in the form of the layer contract. A host gives it to `checkStackLayers` and to `joinStacks` of the core.\n// This entry has the schema library, so the root entry does not import it.\n\n/**\n * This function gives the risk add-on for a check on one date.\n *\n * - `schema` is `risk/1`, the key of the layer in `Stack.layers`.\n * - `parse` is `parseRisk`.\n * - `check` is the gate `checkRisk` with the options. It throws a RangeError when `maxSets` or `maxSteps` is not valid.\n * - `join` is `joinRisk`. It joins the risk layers of the fragments of a stack.\n *\n * @param options - The options of the gate. The gate has no clock: the caller gives the date.\n * @throws RangeError when `today` is not a date `YYYY-MM-DD`\n */\nexport function riskAddOn(options: RiskOptions): StackJoiner<RiskLayer> {\n if (dayNumber(options.today) === undefined) throw new RangeError(`The date \"${options.today}\" is not a date of the form YYYY-MM-DD.`);\n return { schema: RISK_SCHEMA, parse: parseRisk, check: (layer, stack) => checkRisk(layer, stack, options), join: joinRisk };\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAoBA,SAAgB,UAAU,SAA8C;CACtE,IAAI,UAAU,QAAQ,KAAK,MAAM,KAAA,GAAW,MAAM,IAAI,WAAW,aAAa,QAAQ,MAAM,wCAAwC;CACpI,OAAO;EAAE,QAAQ;EAAa,OAAO;EAAW,QAAQ,OAAO,UAAU,UAAU,OAAO,OAAO,OAAO;EAAG,MAAM;CAAS;AAC5H"}
@@ -0,0 +1,53 @@
1
+ import { a as RiskLayer } from "./layer-zvH_ueL5.js";
2
+ import { Fault, Stack } from "@mark1russell7/system-stack";
3
+ /** These are the codes of the faults of a risk layer. */
4
+ declare const RISK_CODES: readonly ["goal-part", "goal-duplicate", "goal-survives", "goal-chance", "chance-part", "chance-duplicate", "chance-range", "accept-part", "accept-stop", "accept-date", "stopped", "stop-set", "accept-old", "accept-unused", "chance-none", "chance-low", "no-read"];
5
+ /** A code of a fault of a risk layer. */
6
+ type RiskCode = (typeof RISK_CODES)[number];
7
+ /** A fault of a risk layer. Its field `at` is the `id` of the part of the record. */
8
+ interface RiskFault extends Fault {
9
+ readonly code: RiskCode;
10
+ /** The parts of the fault: the stop set, or the parts with no chance record. */
11
+ readonly parts?: readonly string[] | undefined;
12
+ }
13
+ /** The options of the gate. */
14
+ interface RiskOptions {
15
+ /** The date of the check, as `YYYY-MM-DD`. The gate has no clock: the caller gives the date. */
16
+ readonly today: string;
17
+ /** The largest count of sets in one step of a search. The default is the default of the core, 4096. */
18
+ readonly maxSets?: number | undefined;
19
+ /** The largest count of steps of the calculation of a chance. The default is 1000000. */
20
+ readonly maxSteps?: number | undefined;
21
+ }
22
+ /**
23
+ * This function gives each fault of a risk layer for a stack. An empty list tells that the gate passes.
24
+ * The gate examines the design: each part of the stack is in operation. `goalStates` gives the state with parts that are down.
25
+ *
26
+ * The records:
27
+ * - `goal-part`: a goal names no part of the stack.
28
+ * - `goal-duplicate`: two goals name one part.
29
+ * - `goal-survives`: the count of a goal is not a whole number from 0.
30
+ * - `goal-chance`: the chance of a goal is not from 0 to 1.
31
+ * - `chance-part`: a chance record names no part of the stack.
32
+ * - `chance-duplicate`: two chance records name one part.
33
+ * - `chance-range`: the chance of a part is not from 0 to 1.
34
+ * - `accept-part`: an accepted risk names a part with no goal.
35
+ * - `accept-stop`: an accepted risk names no part, or a part that is not in the stack.
36
+ * - `accept-date`: the last date of an accepted risk is not a date.
37
+ *
38
+ * The goals:
39
+ * - `stopped`: the part of a goal does not work, with each part of the stack in operation.
40
+ * - `stop-set`: the failure of a set of parts stops a part, and the goal is to survive a set of that size. No record accepts the set.
41
+ * - `accept-old`: the acceptance of a stop set ended before `today`.
42
+ * - `accept-unused`: an accepted risk names a set that is no stop set of its goal. Remove the record.
43
+ * - `chance-none`: a goal has a chance, and a part of its paths has no chance record.
44
+ * - `chance-low`: the chance that a part works is less than the chance of its goal.
45
+ * - `no-read`: a search passed its limit. The gate has no result for the goal, so it does not pass.
46
+ *
47
+ * The faults are in the sequence of the chance records, the accepted risks and the goals. The function is pure.
48
+ *
49
+ * @throws RangeError when `today` is not a date `YYYY-MM-DD`, or `maxSets` or `maxSteps` is not valid
50
+ */
51
+ declare function checkRisk(layer: RiskLayer, stack: Stack, options: RiskOptions): RiskFault[];
52
+ export { checkRisk as a, RiskOptions as i, RiskCode as n, RiskFault as r, RISK_CODES as t };
53
+ //# sourceMappingURL=check-CahTrDAa.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"check-CahTrDAa.d.ts","names":[],"sources":["../src/check.ts"],"mappings":";;;cAQa;;KAuCD,mBAAmB;;UAGd,kBAAkB;WACxB,MAAM;;WAEN;;;UAIM;;WAEN;;WAEA;;WAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0CK,UAAU,OAAO,WAAW,OAAO,OAAO,SAAS,cAAc"}
@@ -0,0 +1,103 @@
1
+ import { a as RiskLayer, c as isChance, i as RISK_SCHEMA, n as Chance, o as chancesByPart, r as Goal, s as firstGoals, t as Accepted } from "./layer-zvH_ueL5.js";
2
+ import { a as checkRisk, i as RiskOptions, n as RiskCode, r as RiskFault, t as RISK_CODES } from "./check-CahTrDAa.js";
3
+ import { LayerParse, LayerPiece, PathOptions, Stack } from "@mark1russell7/system-stack";
4
+ /**
5
+ * The state of the goal of one part, with the parts that are down at this time.
6
+ *
7
+ * - `read: true`: the query has a result. `works` tells if the part works.
8
+ * `margin` is the count of more failures that the part always survives. It is `Infinity` when no failure of other parts stops the part.
9
+ * `stop` is a smallest set of parts whose failure stops the part. `points` has each part whose failure alone stops the part.
10
+ * - `read: false`: the search passed its limit, so the state has no result. Do not show it as a state that is good.
11
+ */
12
+ export type GoalState = {
13
+ readonly part: string;
14
+ readonly read: true;
15
+ readonly works: boolean;
16
+ readonly margin: number;
17
+ readonly stop: readonly string[];
18
+ readonly points: readonly string[];
19
+ } | {
20
+ readonly part: string;
21
+ readonly read: false;
22
+ };
23
+ /**
24
+ * This function gives the state of each goal of a layer, when the parts of `down` do not work.
25
+ *
26
+ * - A part that does not work has the margin 0, and its lists are empty.
27
+ * - A goal with `survives: 1` and one part of its paths down can have the margin 0: one more failure stops the part.
28
+ * - The states are in the sequence of the goals. The first goal of a part is used.
29
+ *
30
+ * The function is pure.
31
+ *
32
+ * @throws RangeError when `maxSets` is not a whole number from 1
33
+ */
34
+ export declare function goalStates(layer: RiskLayer, stack: Stack, options?: PathOptions): GoalState[];
35
+ /** The options of `worksChance`. */
36
+ export interface ChanceOptions extends PathOptions {
37
+ /** The largest count of steps of the calculation. The default is 1000000. */
38
+ readonly maxSteps?: number | undefined;
39
+ }
40
+ /** The importance of one part for the chance that a different part works. */
41
+ export interface PartImportance {
42
+ /** The `id` of the part. */
43
+ readonly part: string;
44
+ /** The Birnbaum importance: the chance when the part works, minus the chance when the part fails. */
45
+ readonly importance: number;
46
+ }
47
+ /**
48
+ * The result of `worksChance`.
49
+ *
50
+ * - `exact`: `value` is the chance that the part works. `importance` has the part and each part of its paths, from the
51
+ * largest importance. Parts with one importance are in the sequence of the stack, after the part itself. `assumed`
52
+ * has the parts of the paths with no chance record, in the sequence of the stack: the calculation uses the chance 1
53
+ * for each. The part itself is not in `assumed`. A part that cannot work has the value 0 and two empty lists.
54
+ * - `budget`: the search of the paths or the calculation passed its limit, so the result has no value.
55
+ */
56
+ export type WorksChance = {
57
+ readonly kind: "exact";
58
+ readonly value: number;
59
+ readonly importance: readonly PartImportance[];
60
+ readonly assumed: readonly string[];
61
+ } | {
62
+ readonly kind: "budget";
63
+ };
64
+ /**
65
+ * This function gives the chance that a part of a stack works.
66
+ *
67
+ * - The part works when each part of one of its path sets works, and the part itself works.
68
+ * - Each part works or fails alone, with the chance of its record in the layer. A part with no record has the chance 1.
69
+ * - A shared cause of failure is thus a part of the stack: a provider, an account or a power feed. Do not put it in a number.
70
+ * - The value is exact: `pathSetReliability` of `@mark1russell7/collections/graph` counts a shared part one time.
71
+ * - A part that does not work with the parts of `down` has the chance 0.
72
+ * - The sequence of the parts of the stack does not change the value or an importance, to the last digit.
73
+ *
74
+ * The function is pure.
75
+ *
76
+ * @throws RangeError when a chance of a part of the paths is not from 0 to 1, or an option is not valid
77
+ */
78
+ export declare function worksChance(layer: RiskLayer, stack: Stack, part: string, options?: ChanceOptions): WorksChance;
79
+ /**
80
+ * This function joins the risk layers of the fragments of a stack into one layer.
81
+ *
82
+ * - The goals and the chance records are in the order of the fragments. The record of a part is in one fragment.
83
+ * - A part with a goal in two or more fragments is a fault, also when the goals are equal.
84
+ * - A part with a chance record in two or more fragments is a fault, also when the records are equal.
85
+ * - The accepted risks are in the order of the fragments. A fragment can accept a risk of a goal of a different fragment.
86
+ *
87
+ * The joined layer has the field `chances` or `accepted` when a fragment has it. The function is pure, and it
88
+ * changes no layer. The gate `checkRisk` examines the joined layer afterward.
89
+ *
90
+ * @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.
91
+ * @returns The joined layer, or the faults of the join.
92
+ */
93
+ export declare function joinRisk(pieces: readonly LayerPiece<RiskLayer>[]): LayerParse<RiskLayer>;
94
+ /** The date of the check of the sample. With it, the gate gives the faults that the README shows. */
95
+ export declare const SAMPLE_TODAY = "2026-11-16";
96
+ /**
97
+ * A sample risk layer for `SAMPLE_STACK` of the core. Each name and each number is an example.
98
+ * It has two faults: the failure of the phone alone stops the alert, and the chance that the alert works is less than its goal.
99
+ * The failure of the reader alone stops the alert too, and a record accepts that risk.
100
+ */
101
+ export declare const SAMPLE_RISK: RiskLayer;
102
+ export { Accepted, Chance, Goal, RISK_CODES, RISK_SCHEMA, RiskCode, RiskFault, RiskLayer, RiskOptions, chancesByPart, checkRisk, firstGoals, isChance };
103
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/states.ts","../src/chance.ts","../src/join.ts","../src/sample.ts"],"mappings":";;;;;;;;;;;YAaY;WACG;WAAuB;WAAqB;WAAyB;WAAyB;WAAkC;;WAChI;WAAuB;;;;;;;;;;;;;wBAatB,WAAW,OAAO,WAAW,OAAO,OAAO,UAAS,cAAmB;;iBCrBtE,sBAAsB;;WAE5B;;;iBAIM;;WAEN;;WAEA;;;;;;;;;;;YAYC;WAAyB;WAAwB;WAAwB,qBAAqB;WAA2B;;WAA0C;;;;;;;;;;;;;;;;wBAqB/J,YAAY,OAAO,WAAW,OAAO,OAAO,cAAc,UAAS,gBAAqB;;;;;;;;;;;;;;;wBC/BxF,SAAS,iBAAiB,WAAW,eAAe,WAAW;;qBChBlE;;;;;;qBAOA,aAAa"}
package/build/index.js ADDED
@@ -0,0 +1,148 @@
1
+ import { i as isChance, n as chancesByPart, r as firstGoals, t as RISK_SCHEMA } from "./layer-BLQnPkuy.js";
2
+ import { i as worksChance, n as RISK_CODES, r as checkRisk, t as joinRisk } from "./join-ClFU2WDq.js";
3
+ import { smallestStop, stopSets, works } from "@mark1russell7/system-stack";
4
+ // src/states.ts
5
+ /**
6
+ * This function gives the state of each goal of a layer, when the parts of `down` do not work.
7
+ *
8
+ * - A part that does not work has the margin 0, and its lists are empty.
9
+ * - A goal with `survives: 1` and one part of its paths down can have the margin 0: one more failure stops the part.
10
+ * - The states are in the sequence of the goals. The first goal of a part is used.
11
+ *
12
+ * The function is pure.
13
+ *
14
+ * @throws RangeError when `maxSets` is not a whole number from 1
15
+ */
16
+ function goalStates(layer, stack, options = {}) {
17
+ const down = [...options.down ?? []];
18
+ return firstGoals(layer).map((goal) => {
19
+ const part = goal.part;
20
+ const least = smallestStop(stack, part, {
21
+ ...options,
22
+ down
23
+ });
24
+ const single = stopSets(stack, part, {
25
+ ...options,
26
+ down,
27
+ size: 1
28
+ });
29
+ if (least.kind === "budget" || single.kind === "budget") return {
30
+ part,
31
+ read: false
32
+ };
33
+ const up = works(stack, part, down);
34
+ if (least.kind === "none") return {
35
+ part,
36
+ read: true,
37
+ works: up,
38
+ margin: Number.POSITIVE_INFINITY,
39
+ stop: [],
40
+ points: []
41
+ };
42
+ return {
43
+ part,
44
+ read: true,
45
+ works: up,
46
+ margin: Math.max(0, least.parts.length - 1),
47
+ stop: least.parts,
48
+ points: single.sets.flat()
49
+ };
50
+ });
51
+ }
52
+ // src/sample.ts
53
+ /** The date of the check of the sample. With it, the gate gives the faults that the README shows. */
54
+ const SAMPLE_TODAY = "2026-11-16";
55
+ /**
56
+ * A sample risk layer for `SAMPLE_STACK` of the core. Each name and each number is an example.
57
+ * It has two faults: the failure of the phone alone stops the alert, and the chance that the alert works is less than its goal.
58
+ * The failure of the reader alone stops the alert too, and a record accepts that risk.
59
+ */
60
+ const SAMPLE_RISK = {
61
+ schema: RISK_SCHEMA,
62
+ goals: [{
63
+ part: "alert",
64
+ survives: 1,
65
+ chance: .99
66
+ }],
67
+ chances: [
68
+ {
69
+ part: "reader",
70
+ works: .97,
71
+ source: "An estimate: the hours when the reader cannot read a message"
72
+ },
73
+ {
74
+ part: "phone",
75
+ works: .995,
76
+ source: "An estimate: the hours with no battery or no signal"
77
+ },
78
+ {
79
+ part: "card",
80
+ works: .999,
81
+ source: "An estimate"
82
+ },
83
+ {
84
+ part: "home-power",
85
+ works: .999,
86
+ source: "An estimate: the hours without power in a year"
87
+ },
88
+ {
89
+ part: "home-network",
90
+ works: .995,
91
+ source: "An estimate: the hours without a connection in a year"
92
+ },
93
+ {
94
+ part: "home-host",
95
+ works: .99,
96
+ source: "An estimate"
97
+ },
98
+ {
99
+ part: "cloud-account",
100
+ works: .999,
101
+ source: "An estimate"
102
+ },
103
+ {
104
+ part: "cloud-host",
105
+ works: .999,
106
+ source: "The service agreement of the provider"
107
+ },
108
+ {
109
+ part: "program-home",
110
+ works: .995,
111
+ source: "An estimate"
112
+ },
113
+ {
114
+ part: "program-cloud",
115
+ works: .995,
116
+ source: "An estimate"
117
+ },
118
+ {
119
+ part: "push-account",
120
+ works: .999,
121
+ source: "An estimate"
122
+ },
123
+ {
124
+ part: "push",
125
+ works: .999,
126
+ source: "The status page of the provider"
127
+ },
128
+ {
129
+ part: "call-account",
130
+ works: .999,
131
+ source: "An estimate"
132
+ },
133
+ {
134
+ part: "call",
135
+ works: .999,
136
+ source: "The status page of the provider"
137
+ }
138
+ ],
139
+ accepted: [{
140
+ part: "alert",
141
+ stop: ["reader"],
142
+ reason: "One person reads the alerts. A second reader is a later step.",
143
+ until: "2027-06-30"
144
+ }]
145
+ };
146
+ export { RISK_CODES, RISK_SCHEMA, SAMPLE_RISK, SAMPLE_TODAY, chancesByPart, checkRisk, firstGoals, goalStates, isChance, joinRisk, worksChance };
147
+
148
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/states.ts","../src/sample.ts"],"sourcesContent":["import { smallestStop, stopSets, works, type PathOptions, type Stack } from \"@mark1russell7/system-stack\";\nimport { firstGoals, type RiskLayer } from \"./layer.ts\";\n\n// The state of each goal at this time: the live read. The parts that are down come from the monitors of the system.\n\n/**\n * The state of the goal of one part, with the parts that are down at this time.\n *\n * - `read: true`: the query has a result. `works` tells if the part works.\n * `margin` is the count of more failures that the part always survives. It is `Infinity` when no failure of other parts stops the part.\n * `stop` is a smallest set of parts whose failure stops the part. `points` has each part whose failure alone stops the part.\n * - `read: false`: the search passed its limit, so the state has no result. Do not show it as a state that is good.\n */\nexport type GoalState =\n | { readonly part: string; readonly read: true; readonly works: boolean; readonly margin: number; readonly stop: readonly string[]; readonly points: readonly string[] }\n | { readonly part: string; readonly read: false };\n\n/**\n * This function gives the state of each goal of a layer, when the parts of `down` do not work.\n *\n * - A part that does not work has the margin 0, and its lists are empty.\n * - A goal with `survives: 1` and one part of its paths down can have the margin 0: one more failure stops the part.\n * - The states are in the sequence of the goals. The first goal of a part is used.\n *\n * The function is pure.\n *\n * @throws RangeError when `maxSets` is not a whole number from 1\n */\nexport function goalStates(layer: RiskLayer, stack: Stack, options: PathOptions = {}): GoalState[] {\n // The option `down` can be a sequence that gives its items one time. Each query reads this list.\n const down = [...(options.down ?? [])];\n return firstGoals(layer).map((goal): GoalState => {\n const part = goal.part;\n const least = smallestStop(stack, part, { ...options, down });\n const single = stopSets(stack, part, { ...options, down, size: 1 });\n if (least.kind === \"budget\" || single.kind === \"budget\") return { part, read: false };\n const up = works(stack, part, down);\n if (least.kind === \"none\") return { part, read: true, works: up, margin: Number.POSITIVE_INFINITY, stop: [], points: [] };\n return { part, read: true, works: up, margin: Math.max(0, least.parts.length - 1), stop: least.parts, points: single.sets.flat() };\n });\n}\n","import { RISK_SCHEMA, type RiskLayer } from \"./layer.ts\";\n\n/** The date of the check of the sample. With it, the gate gives the faults that the README shows. */\nexport const SAMPLE_TODAY = \"2026-11-16\";\n\n/**\n * A sample risk layer for `SAMPLE_STACK` of the core. Each name and each number is an example.\n * It has two faults: the failure of the phone alone stops the alert, and the chance that the alert works is less than its goal.\n * The failure of the reader alone stops the alert too, and a record accepts that risk.\n */\nexport const SAMPLE_RISK: RiskLayer = {\n schema: RISK_SCHEMA,\n goals: [{ part: \"alert\", survives: 1, chance: 0.99 }],\n chances: [\n { part: \"reader\", works: 0.97, source: \"An estimate: the hours when the reader cannot read a message\" },\n { part: \"phone\", works: 0.995, source: \"An estimate: the hours with no battery or no signal\" },\n { part: \"card\", works: 0.999, source: \"An estimate\" },\n { part: \"home-power\", works: 0.999, source: \"An estimate: the hours without power in a year\" },\n { part: \"home-network\", works: 0.995, source: \"An estimate: the hours without a connection in a year\" },\n { part: \"home-host\", works: 0.99, source: \"An estimate\" },\n { part: \"cloud-account\", works: 0.999, source: \"An estimate\" },\n { part: \"cloud-host\", works: 0.999, source: \"The service agreement of the provider\" },\n { part: \"program-home\", works: 0.995, source: \"An estimate\" },\n { part: \"program-cloud\", works: 0.995, source: \"An estimate\" },\n { part: \"push-account\", works: 0.999, source: \"An estimate\" },\n { part: \"push\", works: 0.999, source: \"The status page of the provider\" },\n { part: \"call-account\", works: 0.999, source: \"An estimate\" },\n { part: \"call\", works: 0.999, source: \"The status page of the provider\" },\n ],\n accepted: [{ part: \"alert\", stop: [\"reader\"], reason: \"One person reads the alerts. A second reader is a later step.\", until: \"2027-06-30\" }],\n};\n"],"mappings":";;;;;;;;;;;;;;;AA4BA,SAAgB,WAAW,OAAkB,OAAc,UAAuB,CAAC,GAAgB;CAEjG,MAAM,OAAO,CAAC,GAAI,QAAQ,QAAQ,CAAC,CAAE;CACrC,OAAO,WAAW,KAAK,CAAC,CAAC,KAAK,SAAoB;EAChD,MAAM,OAAO,KAAK;EAClB,MAAM,QAAQ,aAAa,OAAO,MAAM;GAAE,GAAG;GAAS;EAAK,CAAC;EAC5D,MAAM,SAAS,SAAS,OAAO,MAAM;GAAE,GAAG;GAAS;GAAM,MAAM;EAAE,CAAC;EAClE,IAAI,MAAM,SAAS,YAAY,OAAO,SAAS,UAAU,OAAO;GAAE;GAAM,MAAM;EAAM;EACpF,MAAM,KAAK,MAAM,OAAO,MAAM,IAAI;EAClC,IAAI,MAAM,SAAS,QAAQ,OAAO;GAAE;GAAM,MAAM;GAAM,OAAO;GAAI,QAAQ,OAAO;GAAmB,MAAM,CAAC;GAAG,QAAQ,CAA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js";
2
+ import { dayNumber, joinRecords, partsById, pathSets, stopSets, works } from "@mark1russell7/system-stack";
3
+ import { NUMBER_RING, pathSetReliability } from "@mark1russell7/collections/graph";
4
+ // src/chance.ts
5
+ /** This function compares two texts by their code units. The language of the computer does not change the result. */
6
+ function compareTexts(a, b) {
7
+ return a < b ? -1 : a > b ? 1 : 0;
8
+ }
9
+ /**
10
+ * This function gives the chance that a part of a stack works.
11
+ *
12
+ * - The part works when each part of one of its path sets works, and the part itself works.
13
+ * - Each part works or fails alone, with the chance of its record in the layer. A part with no record has the chance 1.
14
+ * - A shared cause of failure is thus a part of the stack: a provider, an account or a power feed. Do not put it in a number.
15
+ * - The value is exact: `pathSetReliability` of `@mark1russell7/collections/graph` counts a shared part one time.
16
+ * - A part that does not work with the parts of `down` has the chance 0.
17
+ * - The sequence of the parts of the stack does not change the value or an importance, to the last digit.
18
+ *
19
+ * The function is pure.
20
+ *
21
+ * @throws RangeError when a chance of a part of the paths is not from 0 to 1, or an option is not valid
22
+ */
23
+ function worksChance(layer, stack, part, options = {}) {
24
+ const paths = pathSets(stack, part, options);
25
+ if (paths.kind === "budget") return paths;
26
+ if (paths.sets.length === 0) return {
27
+ kind: "exact",
28
+ value: 0,
29
+ importance: [],
30
+ assumed: []
31
+ };
32
+ const used = new Set(paths.sets.flat());
33
+ const ids = [part, ...stack.parts.map((entry) => entry.id).filter((id, at, all) => id !== part && used.has(id) && all.indexOf(id) === at)];
34
+ const fixed = [...ids].sort(compareTexts);
35
+ const index = new Map(fixed.map((id, at) => [id, at]));
36
+ const records = chancesByPart(layer);
37
+ const chances = fixed.map((id) => records.get(id)?.works ?? 1);
38
+ const sets = paths.sets.map((set) => [part, ...set].map((id) => index.get(id) ?? 0));
39
+ const result = pathSetReliability(sets, chances, NUMBER_RING, options.maxSteps === void 0 ? {} : { maxSteps: options.maxSteps });
40
+ if (result.kind === "budget") return { kind: "budget" };
41
+ const importance = ids.map((id) => ({
42
+ part: id,
43
+ importance: result.importance[index.get(id) ?? 0] ?? 0
44
+ })).sort((a, b) => b.importance - a.importance);
45
+ return {
46
+ kind: "exact",
47
+ value: result.value,
48
+ importance,
49
+ assumed: ids.filter((id) => id !== part && !records.has(id))
50
+ };
51
+ }
52
+ // src/check.ts
53
+ /** These are the codes of the faults of a risk layer. */
54
+ const RISK_CODES = [
55
+ "goal-part",
56
+ "goal-duplicate",
57
+ "goal-survives",
58
+ "goal-chance",
59
+ "chance-part",
60
+ "chance-duplicate",
61
+ "chance-range",
62
+ "accept-part",
63
+ "accept-stop",
64
+ "accept-date",
65
+ "stopped",
66
+ "stop-set",
67
+ "accept-old",
68
+ "accept-unused",
69
+ "chance-none",
70
+ "chance-low",
71
+ "no-read"
72
+ ];
73
+ /** This function gives the text of a set of parts for a message, for example `["card", "push"]`. */
74
+ function setText(parts) {
75
+ return `[${parts.map((id) => `"${id}"`).join(", ")}]`;
76
+ }
77
+ /** This function gives one key for each set of parts. The sequence of the parts does not change the key. */
78
+ function setKey(parts) {
79
+ return JSON.stringify([...new Set(parts)].sort());
80
+ }
81
+ /**
82
+ * This function gives each fault of a risk layer for a stack. An empty list tells that the gate passes.
83
+ * The gate examines the design: each part of the stack is in operation. `goalStates` gives the state with parts that are down.
84
+ *
85
+ * The records:
86
+ * - `goal-part`: a goal names no part of the stack.
87
+ * - `goal-duplicate`: two goals name one part.
88
+ * - `goal-survives`: the count of a goal is not a whole number from 0.
89
+ * - `goal-chance`: the chance of a goal is not from 0 to 1.
90
+ * - `chance-part`: a chance record names no part of the stack.
91
+ * - `chance-duplicate`: two chance records name one part.
92
+ * - `chance-range`: the chance of a part is not from 0 to 1.
93
+ * - `accept-part`: an accepted risk names a part with no goal.
94
+ * - `accept-stop`: an accepted risk names no part, or a part that is not in the stack.
95
+ * - `accept-date`: the last date of an accepted risk is not a date.
96
+ *
97
+ * The goals:
98
+ * - `stopped`: the part of a goal does not work, with each part of the stack in operation.
99
+ * - `stop-set`: the failure of a set of parts stops a part, and the goal is to survive a set of that size. No record accepts the set.
100
+ * - `accept-old`: the acceptance of a stop set ended before `today`.
101
+ * - `accept-unused`: an accepted risk names a set that is no stop set of its goal. Remove the record.
102
+ * - `chance-none`: a goal has a chance, and a part of its paths has no chance record.
103
+ * - `chance-low`: the chance that a part works is less than the chance of its goal.
104
+ * - `no-read`: a search passed its limit. The gate has no result for the goal, so it does not pass.
105
+ *
106
+ * The faults are in the sequence of the chance records, the accepted risks and the goals. The function is pure.
107
+ *
108
+ * @throws RangeError when `today` is not a date `YYYY-MM-DD`, or `maxSets` or `maxSteps` is not valid
109
+ */
110
+ function checkRisk(layer, stack, options) {
111
+ const today = dayNumber(options.today);
112
+ if (today === void 0) throw new RangeError(`The date "${options.today}" is not a date of the form YYYY-MM-DD.`);
113
+ const limits = {
114
+ ...options.maxSets === void 0 ? {} : { maxSets: options.maxSets },
115
+ ...options.maxSteps === void 0 ? {} : { maxSteps: options.maxSteps }
116
+ };
117
+ const faults = [];
118
+ const add = (code, at, message, more = {}) => {
119
+ faults.push({
120
+ level: "error",
121
+ code,
122
+ at,
123
+ message,
124
+ ...more
125
+ });
126
+ };
127
+ const parts = partsById(stack);
128
+ const chanceParts = /* @__PURE__ */ new Set();
129
+ let chancesValid = true;
130
+ for (const chance of layer.chances ?? []) {
131
+ const at = chance.part;
132
+ if (chanceParts.has(at)) {
133
+ add("chance-duplicate", at, `Two chance records name the part "${at}".`);
134
+ continue;
135
+ }
136
+ chanceParts.add(at);
137
+ if (!parts.has(at)) add("chance-part", at, `A chance record names "${at}", which is no part of the stack.`);
138
+ if (!isChance(chance.works)) {
139
+ add("chance-range", at, `The chance ${String(chance.works)} of "${at}" is not from 0 to 1.`);
140
+ chancesValid = false;
141
+ }
142
+ }
143
+ const goals = new Map(firstGoals(layer).map((goal) => [goal.part, goal]));
144
+ const accepted = [];
145
+ for (const record of layer.accepted ?? []) {
146
+ const at = record.part;
147
+ let valid = true;
148
+ if (!goals.has(at)) {
149
+ add("accept-part", at, `An accepted risk names "${at}", which has no goal.`);
150
+ valid = false;
151
+ }
152
+ if (record.stop.length === 0) {
153
+ add("accept-stop", at, `An accepted risk of "${at}" names no part.`);
154
+ valid = false;
155
+ }
156
+ for (const id of record.stop) {
157
+ if (parts.has(id)) continue;
158
+ add("accept-stop", at, `An accepted risk of "${at}" names "${id}", which is no part of the stack.`, { other: id });
159
+ valid = false;
160
+ }
161
+ const last = record.until === void 0 ? void 0 : dayNumber(record.until);
162
+ if (record.until !== void 0 && last === void 0) {
163
+ add("accept-date", at, `The date "${record.until}" of an accepted risk of "${at}" is not a date of the form YYYY-MM-DD.`);
164
+ valid = false;
165
+ }
166
+ if (valid) accepted.push({
167
+ record,
168
+ key: setKey(record.stop),
169
+ ended: last !== void 0 && last < today,
170
+ used: false
171
+ });
172
+ }
173
+ const read = /* @__PURE__ */ new Set();
174
+ const seen = /* @__PURE__ */ new Set();
175
+ for (const goal of layer.goals) {
176
+ const at = goal.part;
177
+ if (seen.has(at)) {
178
+ add("goal-duplicate", at, `Two goals name the part "${at}".`);
179
+ continue;
180
+ }
181
+ seen.add(at);
182
+ if (!parts.has(at)) {
183
+ add("goal-part", at, `A goal names "${at}", which is no part of the stack.`);
184
+ continue;
185
+ }
186
+ const counted = Number.isInteger(goal.survives) && goal.survives >= 0;
187
+ if (!counted) add("goal-survives", at, `The count ${String(goal.survives)} of the goal of "${at}" is not a whole number from 0.`);
188
+ if (goal.chance !== void 0 && !isChance(goal.chance)) add("goal-chance", at, `The chance ${String(goal.chance)} of the goal of "${at}" is not from 0 to 1.`);
189
+ if (!works(stack, at)) {
190
+ add("stopped", at, `The part "${at}" does not work, with each part of the stack in operation: one of its needs does not hold.`);
191
+ continue;
192
+ }
193
+ if (counted) {
194
+ const stops = stopSets(stack, at, {
195
+ ...limits,
196
+ size: goal.survives
197
+ });
198
+ if (stops.kind === "budget") add("no-read", at, `The search of the stop sets of "${at}" passed its limit. The gate has no result for this goal.`);
199
+ else {
200
+ read.add(at);
201
+ for (const set of stops.sets) {
202
+ const key = setKey(set);
203
+ const records = accepted.filter((entry) => entry.record.part === at && entry.key === key);
204
+ for (const entry of records) entry.used = true;
205
+ if (records.some((entry) => !entry.ended)) continue;
206
+ const old = records[0];
207
+ if (old === void 0) add("stop-set", at, `The failure of the set ${setText(set)} stops "${at}". The goal of "${at}" is to survive each set of ${String(goal.survives)} or fewer parts.`, { parts: set });
208
+ else add("accept-old", at, `The acceptance of the stop set ${setText(set)} of "${at}" ended on ${old.record.until ?? ""}. The failure of the set stops "${at}".`, { parts: set });
209
+ }
210
+ }
211
+ }
212
+ if (goal.chance === void 0 || !isChance(goal.chance) || !chancesValid) continue;
213
+ const chance = worksChance(layer, stack, at, limits);
214
+ if (chance.kind === "budget") add("no-read", at, `The calculation of the chance of "${at}" passed its limit. The gate has no result for this goal.`);
215
+ else if (chance.assumed.length > 0) add("chance-none", at, `The goal of "${at}" has a chance, and these parts of its paths have no chance record: ${setText(chance.assumed)}.`, { parts: chance.assumed });
216
+ else if (chance.value < goal.chance) add("chance-low", at, `The chance that "${at}" works is ${String(chance.value)}. The goal is ${String(goal.chance)}.`);
217
+ }
218
+ for (const entry of accepted) {
219
+ const at = entry.record.part;
220
+ if (entry.used || !read.has(at)) continue;
221
+ const survives = goals.get(at)?.survives ?? 0;
222
+ add("accept-unused", at, `An accepted risk of "${at}" names the set ${setText(entry.record.stop)}. It is no stop set of "${at}" of ${String(survives)} or fewer parts.`, { parts: entry.record.stop });
223
+ }
224
+ return faults;
225
+ }
226
+ // src/join.ts
227
+ /**
228
+ * This function joins the risk layers of the fragments of a stack into one layer.
229
+ *
230
+ * - The goals and the chance records are in the order of the fragments. The record of a part is in one fragment.
231
+ * - A part with a goal in two or more fragments is a fault, also when the goals are equal.
232
+ * - A part with a chance record in two or more fragments is a fault, also when the records are equal.
233
+ * - The accepted risks are in the order of the fragments. A fragment can accept a risk of a goal of a different fragment.
234
+ *
235
+ * The joined layer has the field `chances` or `accepted` when a fragment has it. The function is pure, and it
236
+ * changes no layer. The gate `checkRisk` examines the joined layer afterward.
237
+ *
238
+ * @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.
239
+ * @returns The joined layer, or the faults of the join.
240
+ */
241
+ function joinRisk(pieces) {
242
+ const goals = joinRecords(pieces, (layer) => layer.goals, (goal) => goal.part, "goal of the part");
243
+ const chances = joinRecords(pieces, (layer) => layer.chances, (chance) => chance.part, "chance record of the part");
244
+ const errors = [...goals.errors, ...chances.errors];
245
+ if (errors.length > 0) return {
246
+ ok: false,
247
+ errors
248
+ };
249
+ const withChances = pieces.some((piece) => piece.layer.chances !== void 0);
250
+ const withAccepted = pieces.some((piece) => piece.layer.accepted !== void 0);
251
+ return {
252
+ ok: true,
253
+ layer: {
254
+ schema: RISK_SCHEMA,
255
+ goals: goals.items,
256
+ ...withChances ? { chances: chances.items } : {},
257
+ ...withAccepted ? { accepted: pieces.flatMap((piece) => piece.layer.accepted ?? []) } : {}
258
+ }
259
+ };
260
+ }
261
+ export { worksChance as i, RISK_CODES as n, checkRisk as r, joinRisk as t };
262
+
263
+ //# sourceMappingURL=join-ClFU2WDq.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"join-ClFU2WDq.js","names":[],"sources":["../src/chance.ts","../src/check.ts","../src/join.ts"],"sourcesContent":["import { NUMBER_RING, pathSetReliability } from \"@mark1russell7/collections/graph\";\nimport { pathSets, type PathOptions, type Stack } from \"@mark1russell7/system-stack\";\nimport { chancesByPart, type RiskLayer } from \"./layer.ts\";\n\n// The chance that a part works, from the chance of each part of its paths. Each part works or fails alone.\n\n/** The options of `worksChance`. */\nexport interface ChanceOptions extends PathOptions {\n /** The largest count of steps of the calculation. The default is 1000000. */\n readonly maxSteps?: number | undefined;\n}\n\n/** The importance of one part for the chance that a different part works. */\nexport interface PartImportance {\n /** The `id` of the part. */\n readonly part: string;\n /** The Birnbaum importance: the chance when the part works, minus the chance when the part fails. */\n readonly importance: number;\n}\n\n/**\n * The result of `worksChance`.\n *\n * - `exact`: `value` is the chance that the part works. `importance` has the part and each part of its paths, from the\n * largest importance. Parts with one importance are in the sequence of the stack, after the part itself. `assumed`\n * has the parts of the paths with no chance record, in the sequence of the stack: the calculation uses the chance 1\n * for each. The part itself is not in `assumed`. A part that cannot work has the value 0 and two empty lists.\n * - `budget`: the search of the paths or the calculation passed its limit, so the result has no value.\n */\nexport type WorksChance = { readonly kind: \"exact\"; readonly value: number; readonly importance: readonly PartImportance[]; readonly assumed: readonly string[] } | { readonly kind: \"budget\" };\n\n/** This function compares two texts by their code units. The language of the computer does not change the result. */\nfunction compareTexts(a: string, b: string): number {\n return a < b ? -1 : a > b ? 1 : 0;\n}\n\n/**\n * This function gives the chance that a part of a stack works.\n *\n * - The part works when each part of one of its path sets works, and the part itself works.\n * - Each part works or fails alone, with the chance of its record in the layer. A part with no record has the chance 1.\n * - A shared cause of failure is thus a part of the stack: a provider, an account or a power feed. Do not put it in a number.\n * - The value is exact: `pathSetReliability` of `@mark1russell7/collections/graph` counts a shared part one time.\n * - A part that does not work with the parts of `down` has the chance 0.\n * - The sequence of the parts of the stack does not change the value or an importance, to the last digit.\n *\n * The function is pure.\n *\n * @throws RangeError when a chance of a part of the paths is not from 0 to 1, or an option is not valid\n */\nexport function worksChance(layer: RiskLayer, stack: Stack, part: string, options: ChanceOptions = {}): WorksChance {\n const paths = pathSets(stack, part, options);\n if (paths.kind === \"budget\") return paths;\n if (paths.sets.length === 0) return { kind: \"exact\", value: 0, importance: [], assumed: [] };\n // The part itself is first: it is a part of each path. Then each part of the paths, in the sequence of the stack.\n const used = new Set(paths.sets.flat());\n const ids = [part, ...stack.parts.map((entry) => entry.id).filter((id, at, all) => id !== part && used.has(id) && all.indexOf(id) === at)];\n // The calculation gets the parts in one fixed order, the order of the texts of the ids.\n // Thus the sequence of the records of the stack does not change the last digits of the value.\n // The order of the path sets and of the parts of a set has no effect on the result of `pathSetReliability`.\n const fixed = [...ids].sort(compareTexts);\n const index = new Map(fixed.map((id, at) => [id, at]));\n const records = chancesByPart(layer);\n const chances = fixed.map((id) => records.get(id)?.works ?? 1);\n const sets = paths.sets.map((set) => [part, ...set].map((id) => index.get(id) ?? 0));\n const result = pathSetReliability(sets, chances, NUMBER_RING, options.maxSteps === undefined ? {} : { maxSteps: options.maxSteps });\n if (result.kind === \"budget\") return { kind: \"budget\" };\n const importance = ids.map((id) => ({ part: id, importance: result.importance[index.get(id) ?? 0] ?? 0 })).sort((a, b) => b.importance - a.importance);\n return { kind: \"exact\", value: result.value, importance, assumed: ids.filter((id) => id !== part && !records.has(id)) };\n}\n","import { dayNumber, partsById, stopSets, works, type Fault, type Stack } from \"@mark1russell7/system-stack\";\nimport { worksChance } from \"./chance.ts\";\nimport { firstGoals, isChance, type Accepted, type RiskLayer } from \"./layer.ts\";\n\n// The gate of the risk layer. A gate that passes tells that each goal holds: no set of parts stops a part with fewer\n// failures than its goal permits, unless a record accepts the set, and each chance is not less than its goal.\n\n/** These are the codes of the faults of a risk layer. */\nexport const RISK_CODES: readonly [\n \"goal-part\",\n \"goal-duplicate\",\n \"goal-survives\",\n \"goal-chance\",\n \"chance-part\",\n \"chance-duplicate\",\n \"chance-range\",\n \"accept-part\",\n \"accept-stop\",\n \"accept-date\",\n \"stopped\",\n \"stop-set\",\n \"accept-old\",\n \"accept-unused\",\n \"chance-none\",\n \"chance-low\",\n \"no-read\",\n] = [\n \"goal-part\",\n \"goal-duplicate\",\n \"goal-survives\",\n \"goal-chance\",\n \"chance-part\",\n \"chance-duplicate\",\n \"chance-range\",\n \"accept-part\",\n \"accept-stop\",\n \"accept-date\",\n \"stopped\",\n \"stop-set\",\n \"accept-old\",\n \"accept-unused\",\n \"chance-none\",\n \"chance-low\",\n \"no-read\",\n];\n\n/** A code of a fault of a risk layer. */\nexport type RiskCode = (typeof RISK_CODES)[number];\n\n/** A fault of a risk layer. Its field `at` is the `id` of the part of the record. */\nexport interface RiskFault extends Fault {\n readonly code: RiskCode;\n /** The parts of the fault: the stop set, or the parts with no chance record. */\n readonly parts?: readonly string[] | undefined;\n}\n\n/** The options of the gate. */\nexport interface RiskOptions {\n /** The date of the check, as `YYYY-MM-DD`. The gate has no clock: the caller gives the date. */\n readonly today: string;\n /** The largest count of sets in one step of a search. The default is the default of the core, 4096. */\n readonly maxSets?: number | undefined;\n /** The largest count of steps of the calculation of a chance. The default is 1000000. */\n readonly maxSteps?: number | undefined;\n}\n\n/** This function gives the text of a set of parts for a message, for example `[\"card\", \"push\"]`. */\nfunction setText(parts: readonly string[]): string {\n return `[${parts.map((id) => `\"${id}\"`).join(\", \")}]`;\n}\n\n/** This function gives one key for each set of parts. The sequence of the parts does not change the key. */\nfunction setKey(parts: readonly string[]): string {\n return JSON.stringify([...new Set(parts)].sort());\n}\n\n/**\n * This function gives each fault of a risk layer for a stack. An empty list tells that the gate passes.\n * The gate examines the design: each part of the stack is in operation. `goalStates` gives the state with parts that are down.\n *\n * The records:\n * - `goal-part`: a goal names no part of the stack.\n * - `goal-duplicate`: two goals name one part.\n * - `goal-survives`: the count of a goal is not a whole number from 0.\n * - `goal-chance`: the chance of a goal is not from 0 to 1.\n * - `chance-part`: a chance record names no part of the stack.\n * - `chance-duplicate`: two chance records name one part.\n * - `chance-range`: the chance of a part is not from 0 to 1.\n * - `accept-part`: an accepted risk names a part with no goal.\n * - `accept-stop`: an accepted risk names no part, or a part that is not in the stack.\n * - `accept-date`: the last date of an accepted risk is not a date.\n *\n * The goals:\n * - `stopped`: the part of a goal does not work, with each part of the stack in operation.\n * - `stop-set`: the failure of a set of parts stops a part, and the goal is to survive a set of that size. No record accepts the set.\n * - `accept-old`: the acceptance of a stop set ended before `today`.\n * - `accept-unused`: an accepted risk names a set that is no stop set of its goal. Remove the record.\n * - `chance-none`: a goal has a chance, and a part of its paths has no chance record.\n * - `chance-low`: the chance that a part works is less than the chance of its goal.\n * - `no-read`: a search passed its limit. The gate has no result for the goal, so it does not pass.\n *\n * The faults are in the sequence of the chance records, the accepted risks and the goals. The function is pure.\n *\n * @throws RangeError when `today` is not a date `YYYY-MM-DD`, or `maxSets` or `maxSteps` is not valid\n */\nexport function checkRisk(layer: RiskLayer, stack: Stack, options: RiskOptions): RiskFault[] {\n const today = dayNumber(options.today);\n if (today === undefined) throw new RangeError(`The date \"${options.today}\" is not a date of the form YYYY-MM-DD.`);\n const limits = { ...(options.maxSets === undefined ? {} : { maxSets: options.maxSets }), ...(options.maxSteps === undefined ? {} : { maxSteps: options.maxSteps }) };\n const faults: RiskFault[] = [];\n const add = (code: RiskCode, at: string, message: string, more: { readonly other?: string; readonly parts?: readonly string[] } = {}): void => {\n faults.push({ level: \"error\", code, at, message, ...more });\n };\n const parts = partsById(stack);\n\n const chanceParts = new Set<string>();\n let chancesValid = true;\n for (const chance of layer.chances ?? []) {\n const at = chance.part;\n if (chanceParts.has(at)) {\n add(\"chance-duplicate\", at, `Two chance records name the part \"${at}\".`);\n continue;\n }\n chanceParts.add(at);\n if (!parts.has(at)) add(\"chance-part\", at, `A chance record names \"${at}\", which is no part of the stack.`);\n if (!isChance(chance.works)) {\n add(\"chance-range\", at, `The chance ${String(chance.works)} of \"${at}\" is not from 0 to 1.`);\n chancesValid = false;\n }\n }\n\n const goals = new Map(firstGoals(layer).map((goal) => [goal.part, goal]));\n // Each accepted risk with a correct form, with the key of its set. `used` tells that the set is a stop set of its goal.\n const accepted: { readonly record: Accepted; readonly key: string; readonly ended: boolean; used: boolean }[] = [];\n for (const record of layer.accepted ?? []) {\n const at = record.part;\n let valid = true;\n if (!goals.has(at)) {\n add(\"accept-part\", at, `An accepted risk names \"${at}\", which has no goal.`);\n valid = false;\n }\n if (record.stop.length === 0) {\n add(\"accept-stop\", at, `An accepted risk of \"${at}\" names no part.`);\n valid = false;\n }\n for (const id of record.stop) {\n if (parts.has(id)) continue;\n add(\"accept-stop\", at, `An accepted risk of \"${at}\" names \"${id}\", which is no part of the stack.`, { other: id });\n valid = false;\n }\n const last = record.until === undefined ? undefined : dayNumber(record.until);\n if (record.until !== undefined && last === undefined) {\n add(\"accept-date\", at, `The date \"${record.until}\" of an accepted risk of \"${at}\" is not a date of the form YYYY-MM-DD.`);\n valid = false;\n }\n if (valid) accepted.push({ record, key: setKey(record.stop), ended: last !== undefined && last < today, used: false });\n }\n\n // The goals whose stop sets the gate read. An accepted risk of a different goal gets no examination.\n const read = new Set<string>();\n const seen = new Set<string>();\n for (const goal of layer.goals) {\n const at = goal.part;\n if (seen.has(at)) {\n add(\"goal-duplicate\", at, `Two goals name the part \"${at}\".`);\n continue;\n }\n seen.add(at);\n if (!parts.has(at)) {\n add(\"goal-part\", at, `A goal names \"${at}\", which is no part of the stack.`);\n continue;\n }\n const counted = Number.isInteger(goal.survives) && goal.survives >= 0;\n if (!counted) add(\"goal-survives\", at, `The count ${String(goal.survives)} of the goal of \"${at}\" is not a whole number from 0.`);\n if (goal.chance !== undefined && !isChance(goal.chance)) add(\"goal-chance\", at, `The chance ${String(goal.chance)} of the goal of \"${at}\" is not from 0 to 1.`);\n if (!works(stack, at)) {\n add(\"stopped\", at, `The part \"${at}\" does not work, with each part of the stack in operation: one of its needs does not hold.`);\n continue;\n }\n if (counted) {\n const stops = stopSets(stack, at, { ...limits, size: goal.survives });\n if (stops.kind === \"budget\") add(\"no-read\", at, `The search of the stop sets of \"${at}\" passed its limit. The gate has no result for this goal.`);\n else {\n read.add(at);\n for (const set of stops.sets) {\n const key = setKey(set);\n const records = accepted.filter((entry) => entry.record.part === at && entry.key === key);\n for (const entry of records) entry.used = true;\n if (records.some((entry) => !entry.ended)) continue;\n const old = records[0];\n if (old === undefined) {\n add(\"stop-set\", at, `The failure of the set ${setText(set)} stops \"${at}\". The goal of \"${at}\" is to survive each set of ${String(goal.survives)} or fewer parts.`, { parts: set });\n } else {\n add(\"accept-old\", at, `The acceptance of the stop set ${setText(set)} of \"${at}\" ended on ${old.record.until ?? \"\"}. The failure of the set stops \"${at}\".`, { parts: set });\n }\n }\n }\n }\n if (goal.chance === undefined || !isChance(goal.chance) || !chancesValid) continue;\n const chance = worksChance(layer, stack, at, limits);\n if (chance.kind === \"budget\") add(\"no-read\", at, `The calculation of the chance of \"${at}\" passed its limit. The gate has no result for this goal.`);\n else if (chance.assumed.length > 0) {\n add(\"chance-none\", at, `The goal of \"${at}\" has a chance, and these parts of its paths have no chance record: ${setText(chance.assumed)}.`, { parts: chance.assumed });\n } else if (chance.value < goal.chance) {\n add(\"chance-low\", at, `The chance that \"${at}\" works is ${String(chance.value)}. The goal is ${String(goal.chance)}.`);\n }\n }\n\n for (const entry of accepted) {\n const at = entry.record.part;\n if (entry.used || !read.has(at)) continue;\n const survives = goals.get(at)?.survives ?? 0;\n add(\"accept-unused\", at, `An accepted risk of \"${at}\" names the set ${setText(entry.record.stop)}. It is no stop set of \"${at}\" of ${String(survives)} or fewer parts.`, { parts: entry.record.stop });\n }\n return faults;\n}\n","import { joinRecords, type LayerParse, type LayerPiece } from \"@mark1russell7/system-stack\";\nimport { RISK_SCHEMA, type RiskLayer } from \"./layer.ts\";\n\n// The join of the risk layers of the fragments of a stack, for example one file of goals for each owner.\n\n/**\n * This function joins the risk layers of the fragments of a stack into one layer.\n *\n * - The goals and the chance records are in the order of the fragments. The record of a part is in one fragment.\n * - A part with a goal in two or more fragments is a fault, also when the goals are equal.\n * - A part with a chance record in two or more fragments is a fault, also when the records are equal.\n * - The accepted risks are in the order of the fragments. A fragment can accept a risk of a goal of a different fragment.\n *\n * The joined layer has the field `chances` or `accepted` when a fragment has it. The function is pure, and it\n * changes no layer. The gate `checkRisk` examines the joined layer afterward.\n *\n * @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.\n * @returns The joined layer, or the faults of the join.\n */\nexport function joinRisk(pieces: readonly LayerPiece<RiskLayer>[]): LayerParse<RiskLayer> {\n const goals = joinRecords(\n pieces,\n (layer) => layer.goals,\n (goal) => goal.part,\n \"goal of the part\",\n );\n const chances = joinRecords(\n pieces,\n (layer) => layer.chances,\n (chance) => chance.part,\n \"chance record of the part\",\n );\n const errors = [...goals.errors, ...chances.errors];\n if (errors.length > 0) return { ok: false, errors };\n const withChances = pieces.some((piece) => piece.layer.chances !== undefined);\n const withAccepted = pieces.some((piece) => piece.layer.accepted !== undefined);\n return {\n ok: true,\n layer: {\n schema: RISK_SCHEMA,\n goals: goals.items,\n ...(withChances ? { chances: chances.items } : {}),\n ...(withAccepted ? { accepted: pieces.flatMap((piece) => piece.layer.accepted ?? []) } : {}),\n },\n };\n}\n"],"mappings":";;;;;AAgCA,SAAS,aAAa,GAAW,GAAmB;CAClD,OAAO,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;AAClC;;;;;;;;;;;;;;;AAgBA,SAAgB,YAAY,OAAkB,OAAc,MAAc,UAAyB,CAAC,GAAgB;CAClH,MAAM,QAAQ,SAAS,OAAO,MAAM,OAAO;CAC3C,IAAI,MAAM,SAAS,UAAU,OAAO;CACpC,IAAI,MAAM,KAAK,WAAW,GAAG,OAAO;EAAE,MAAM;EAAS,OAAO;EAAG,YAAY,CAAC;EAAG,SAAS,CAAC;CAAE;CAE3F,MAAM,OAAO,IAAI,IAAI,MAAM,KAAK,KAAK,CAAC;CACtC,MAAM,MAAM,CAAC,MAAM,GAAG,MAAM,MAAM,KAAK,UAAU,MAAM,EAAE,CAAC,CAAC,QAAQ,IAAI,IAAI,QAAQ,OAAO,QAAQ,KAAK,IAAI,EAAE,KAAK,IAAI,QAAQ,EAAE,MAAM,EAAE,CAAC;CAIzI,MAAM,QAAQ,CAAC,GAAG,GAAG,CAAC,CAAC,KAAK,YAAY;CACxC,MAAM,QAAQ,IAAI,IAAI,MAAM,KAAK,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;CACrD,MAAM,UAAU,cAAc,KAAK;CACnC,MAAM,UAAU,MAAM,KAAK,OAAO,QAAQ,IAAI,EAAE,CAAC,EAAE,SAAS,CAAC;CAC7D,MAAM,OAAO,MAAM,KAAK,KAAK,QAAQ,CAAC,MAAM,GAAG,GAAG,CAAC,CAAC,KAAK,OAAO,MAAM,IAAI,EAAE,KAAK,CAAC,CAAC;CACnF,MAAM,SAAS,mBAAmB,MAA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I,EAAE,CAAC,EAAE,YAAY;EAC5C,IAAI,iBAAiB,IAAI,wBAAwB,GAAG,kBAAkB,QAAQ,MAAM,OAAO,IAAI,EAAE,0BAA0B,GAAG,OAAO,OAAO,QAAQ,EAAE,mBAAmB,EAAE,OAAO,MAAM,OAAO,KAAK,CAAC;CACvM;CACA,OAAO;AACT;;;;;;;;;;;;;;;;ACpMA,SAAgB,SAAS,QAAiE;CACxF,MAAM,QAAQ,YACZ,SACC,UAAU,MAAM,QAChB,SAAS,KAAK,MACf,kBACF;CACA,MAAM,UAAU,YACd,SACC,UAAU,MAAM,UAChB,WAAW,OAAO,MACnB,2BACF;CACA,MAAM,SAAS,CAAC,GAAG,MAAM,QAAQ,GAAG,QAAQ,MAAM;CAClD,IAAI,OAAO,SAAS,GAAG,OAAO;EAAE,IAAI;EAAO;CAAO;CAClD,MAAM,cAAc,OAAO,MAAM,UAAU,MAAM,MAAM,YAAY,KAAA,CAAS;CAC5E,MAAM,eAAe,OAAO,MAAM,UAAU,MAAM,MAAM,aAAa,KAAA,CAAS;CAC9E,OAAO;EACL,IAAI;EACJ,OAAO;GACL,QAAQ;GACR,OAAO,MAAM;GACb,GAAI,cAAc,EAAE,SAAS,QAAQ,MAAM,IAAI,CAAC;GAChD,GAAI,eAAe,EAAE,UAAU,OAAO,SAAS,UAAU,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC;EAC5F;CACF;AACF"}
@@ -0,0 +1,25 @@
1
+ // src/layer.ts
2
+ /** The name of the format of a risk layer. It is the key of the layer in `Stack.layers`. */
3
+ const RISK_SCHEMA = "risk/1";
4
+ /** This function gives the first goal of each part of a layer, in the sequence of the records. */
5
+ function firstGoals(layer) {
6
+ const seen = /* @__PURE__ */ new Set();
7
+ return layer.goals.filter((goal) => {
8
+ if (seen.has(goal.part)) return false;
9
+ seen.add(goal.part);
10
+ return true;
11
+ });
12
+ }
13
+ /** This function gives the chance record of each part of a layer, by the `id` of the part. The first record of a part is used. */
14
+ function chancesByPart(layer) {
15
+ const chances = /* @__PURE__ */ new Map();
16
+ for (const chance of layer.chances ?? []) if (!chances.has(chance.part)) chances.set(chance.part, chance);
17
+ return chances;
18
+ }
19
+ /** This function tells if a value is a chance: a number from 0 to 1. */
20
+ function isChance(value) {
21
+ return value >= 0 && value <= 1;
22
+ }
23
+ export { isChance as i, chancesByPart as n, firstGoals as r, RISK_SCHEMA as t };
24
+
25
+ //# sourceMappingURL=layer-BLQnPkuy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layer-BLQnPkuy.js","names":[],"sources":["../src/layer.ts"],"sourcesContent":["// The records of the risk add-on. They are one layer of a stack, with the key `risk/1` in `Stack.layers`.\n// A goal tells how many failures a part must survive. The answers are queries over the stack: the layer keeps no list of risks.\n\n/** The name of the format of a risk layer. It is the key of the layer in `Stack.layers`. */\nexport const RISK_SCHEMA = \"risk/1\";\n\n/** The goal of one part of the stack, for example of a function of the system. */\nexport interface Goal {\n /** The `id` of the part with the goal. */\n readonly part: string;\n /**\n * The count of failures of other parts that the part survives. It is a whole number from 0.\n * With 1, no failure of one part stops the part: it has no single point of failure.\n */\n readonly survives: number;\n /** The smallest chance that the part works, from 0 to 1. The gate then needs the chance of each part of its paths. */\n readonly chance?: number | undefined;\n}\n\n/** The chance that one part works. */\nexport interface Chance {\n /** The `id` of the part. */\n readonly part: string;\n /** The chance that the part works at a moment, from 0 to 1, when each of its needs holds. */\n readonly works: number;\n /** The source of the number, for example the service agreement of a provider or a measured period. */\n readonly source?: string | undefined;\n}\n\n/** An accepted risk: one set of parts whose failure stops a part, which the owner accepts for a reason. */\nexport interface Accepted {\n /** The `id` of the part with the goal. */\n readonly part: string;\n /** The parts of the set, in any sequence. Their failure together stops the part. */\n readonly stop: readonly string[];\n /** The reason of the owner. */\n readonly reason: string;\n /** The last date of the acceptance, as `YYYY-MM-DD`. After that date the gate fails for the set. An acceptance without it has no end. */\n readonly until?: string | undefined;\n}\n\n/** A risk layer: the goals of the parts of a stack, the chance of each part, and the accepted risks. */\nexport interface RiskLayer {\n readonly schema: typeof RISK_SCHEMA;\n readonly goals: readonly Goal[];\n readonly chances?: readonly Chance[] | undefined;\n readonly accepted?: readonly Accepted[] | undefined;\n}\n\n/** This function gives the first goal of each part of a layer, in the sequence of the records. */\nexport function firstGoals(layer: RiskLayer): readonly Goal[] {\n const seen = new Set<string>();\n return layer.goals.filter((goal) => {\n if (seen.has(goal.part)) return false;\n seen.add(goal.part);\n return true;\n });\n}\n\n/** This function gives the chance record of each part of a layer, by the `id` of the part. The first record of a part is used. */\nexport function chancesByPart(layer: RiskLayer): Map<string, Chance> {\n const chances = new Map<string, Chance>();\n for (const chance of layer.chances ?? []) if (!chances.has(chance.part)) chances.set(chance.part, chance);\n return chances;\n}\n\n/** This function tells if a value is a chance: a number from 0 to 1. */\nexport function isChance(value: number): boolean {\n return value >= 0 && value <= 1;\n}\n"],"mappings":";;AAIA,MAAa,cAAc;;AA8C3B,SAAgB,WAAW,OAAmC;CAC5D,MAAM,uBAAO,IAAI,IAAY;CAC7B,OAAO,MAAM,MAAM,QAAQ,SAAS;EAClC,IAAI,KAAK,IAAI,KAAK,IAAI,GAAG,OAAO;EAChC,KAAK,IAAI,KAAK,IAAI;EAClB,OAAO;CACT,CAAC;AACH;;AAGA,SAAgB,cAAc,OAAuC;CACnE,MAAM,0BAAU,IAAI,IAAoB;CACxC,KAAK,MAAM,UAAU,MAAM,WAAW,CAAC,GAAG,IAAI,CAAC,QAAQ,IAAI,OAAO,IAAI,GAAG,QAAQ,IAAI,OAAO,MAAM,MAAM;CACxG,OAAO;AACT;;AAGA,SAAgB,SAAS,OAAwB;CAC/C,OAAO,SAAS,KAAK,SAAS;AAChC"}
@@ -0,0 +1,49 @@
1
+ /** The name of the format of a risk layer. It is the key of the layer in `Stack.layers`. */
2
+ declare const RISK_SCHEMA = "risk/1";
3
+ /** The goal of one part of the stack, for example of a function of the system. */
4
+ interface Goal {
5
+ /** The `id` of the part with the goal. */
6
+ readonly part: string;
7
+ /**
8
+ * The count of failures of other parts that the part survives. It is a whole number from 0.
9
+ * With 1, no failure of one part stops the part: it has no single point of failure.
10
+ */
11
+ readonly survives: number;
12
+ /** The smallest chance that the part works, from 0 to 1. The gate then needs the chance of each part of its paths. */
13
+ readonly chance?: number | undefined;
14
+ }
15
+ /** The chance that one part works. */
16
+ interface Chance {
17
+ /** The `id` of the part. */
18
+ readonly part: string;
19
+ /** The chance that the part works at a moment, from 0 to 1, when each of its needs holds. */
20
+ readonly works: number;
21
+ /** The source of the number, for example the service agreement of a provider or a measured period. */
22
+ readonly source?: string | undefined;
23
+ }
24
+ /** An accepted risk: one set of parts whose failure stops a part, which the owner accepts for a reason. */
25
+ interface Accepted {
26
+ /** The `id` of the part with the goal. */
27
+ readonly part: string;
28
+ /** The parts of the set, in any sequence. Their failure together stops the part. */
29
+ readonly stop: readonly string[];
30
+ /** The reason of the owner. */
31
+ readonly reason: string;
32
+ /** The last date of the acceptance, as `YYYY-MM-DD`. After that date the gate fails for the set. An acceptance without it has no end. */
33
+ readonly until?: string | undefined;
34
+ }
35
+ /** A risk layer: the goals of the parts of a stack, the chance of each part, and the accepted risks. */
36
+ interface RiskLayer {
37
+ readonly schema: typeof RISK_SCHEMA;
38
+ readonly goals: readonly Goal[];
39
+ readonly chances?: readonly Chance[] | undefined;
40
+ readonly accepted?: readonly Accepted[] | undefined;
41
+ }
42
+ /** This function gives the first goal of each part of a layer, in the sequence of the records. */
43
+ declare function firstGoals(layer: RiskLayer): readonly Goal[];
44
+ /** This function gives the chance record of each part of a layer, by the `id` of the part. The first record of a part is used. */
45
+ declare function chancesByPart(layer: RiskLayer): Map<string, Chance>;
46
+ /** This function tells if a value is a chance: a number from 0 to 1. */
47
+ declare function isChance(value: number): boolean;
48
+ export { RiskLayer as a, isChance as c, RISK_SCHEMA as i, Chance as n, chancesByPart as o, Goal as r, firstGoals as s, Accepted as t };
49
+ //# sourceMappingURL=layer-zvH_ueL5.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layer-zvH_ueL5.d.ts","names":[],"sources":["../src/layer.ts"],"mappings":";cAIa;;UAGI;;WAEN;;;;;WAKA;;WAEA;;;UAIM;;WAEN;;WAEA;;WAEA;;;UAIM;;WAEN;;WAEA;;WAEA;;WAEA;;;UAIM;WACN,eAAe;WACf,gBAAgB;WAChB,mBAAmB;WACnB,oBAAoB;;;iBAIf,WAAW,OAAO,qBAAqB;;iBAUvC,cAAc,OAAO,YAAY,YAAY;;iBAO7C,SAAS"}
@@ -0,0 +1,21 @@
1
+ import { a as RiskLayer } from "./layer-zvH_ueL5.js";
2
+ import { z } from "zod";
3
+ /**
4
+ * The schema of a risk layer. It examines the form of the record: the fields, their types and the finite numbers.
5
+ * It refuses a field that the format does not have. `checkRisk` examines the meaning of a layer with a correct form.
6
+ */
7
+ export declare const riskSchema: z.ZodType<RiskLayer>;
8
+ /** The result of the read of a risk layer: the layer, or the faults of its form. */
9
+ export type RiskParse = {
10
+ readonly ok: true;
11
+ readonly layer: RiskLayer;
12
+ } | {
13
+ readonly ok: false;
14
+ readonly errors: readonly string[];
15
+ };
16
+ /**
17
+ * This function reads the JSON of a risk layer. It gives the layer, or the faults of its form.
18
+ * Each fault names its place in the record, for example `goals.0.part`.
19
+ */
20
+ export declare function parseRisk(value: unknown): RiskParse;
21
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","names":[],"sources":["../src/schema.ts"],"mappings":";;;;;;qBAgCa,YAAY,EAAE,QAAQ;;YAQvB;WAAuB;WAAmB,OAAO;;WAAyB;WAAoB;;;;;;wBAM1F,UAAU,iBAAiB"}
@@ -0,0 +1,49 @@
1
+ import { t as RISK_SCHEMA } from "./layer-BLQnPkuy.js";
2
+ import { formErrors } from "@mark1russell7/system-stack/schema";
3
+ import { z } from "zod";
4
+ // src/schema.ts
5
+ const id = z.string().min(1);
6
+ const goal = z.strictObject({
7
+ part: id,
8
+ survives: z.number(),
9
+ chance: z.number().optional()
10
+ });
11
+ const chance = z.strictObject({
12
+ part: id,
13
+ works: z.number(),
14
+ source: z.string().optional()
15
+ });
16
+ const accepted = z.strictObject({
17
+ part: id,
18
+ stop: z.array(id).readonly(),
19
+ reason: z.string().min(1),
20
+ until: z.string().optional()
21
+ });
22
+ /**
23
+ * The schema of a risk layer. It examines the form of the record: the fields, their types and the finite numbers.
24
+ * It refuses a field that the format does not have. `checkRisk` examines the meaning of a layer with a correct form.
25
+ */
26
+ const riskSchema = z.strictObject({
27
+ schema: z.literal(RISK_SCHEMA),
28
+ goals: z.array(goal).readonly(),
29
+ chances: z.array(chance).readonly().optional(),
30
+ accepted: z.array(accepted).readonly().optional()
31
+ });
32
+ /**
33
+ * This function reads the JSON of a risk layer. It gives the layer, or the faults of its form.
34
+ * Each fault names its place in the record, for example `goals.0.part`.
35
+ */
36
+ function parseRisk(value) {
37
+ const read = riskSchema.safeParse(value);
38
+ if (read.success) return {
39
+ ok: true,
40
+ layer: read.data
41
+ };
42
+ return {
43
+ ok: false,
44
+ errors: formErrors(RISK_SCHEMA, read.error.issues)
45
+ };
46
+ }
47
+ export { parseRisk, riskSchema };
48
+
49
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","names":[],"sources":["../src/schema.ts"],"sourcesContent":["import { formErrors } from \"@mark1russell7/system-stack/schema\";\nimport { z } from \"zod\";\nimport { RISK_SCHEMA, type RiskLayer } from \"./layer.ts\";\n\n// The zod schema of a risk layer, and the read of its JSON. This module is the entry `/schema` of the package.\n// The root entry has no zod, so a page that only runs the queries gets no schema library.\n\nconst id = z.string().min(1);\n\nconst goal = z.strictObject({\n part: id,\n survives: z.number(),\n chance: z.number().optional(),\n});\n\nconst chance = z.strictObject({\n part: id,\n works: z.number(),\n source: z.string().optional(),\n});\n\nconst accepted = z.strictObject({\n part: id,\n stop: z.array(id).readonly(),\n reason: z.string().min(1),\n until: z.string().optional(),\n});\n\n/**\n * The schema of a risk layer. It examines the form of the record: the fields, their types and the finite numbers.\n * It refuses a field that the format does not have. `checkRisk` examines the meaning of a layer with a correct form.\n */\nexport const riskSchema: z.ZodType<RiskLayer> = z.strictObject({\n schema: z.literal(RISK_SCHEMA),\n goals: z.array(goal).readonly(),\n chances: z.array(chance).readonly().optional(),\n accepted: z.array(accepted).readonly().optional(),\n});\n\n/** The result of the read of a risk layer: the layer, or the faults of its form. */\nexport type RiskParse = { readonly ok: true; readonly layer: RiskLayer } | { readonly ok: false; readonly errors: readonly string[] };\n\n/**\n * This function reads the JSON of a risk layer. It gives the layer, or the faults of its form.\n * Each fault names its place in the record, for example `goals.0.part`.\n */\nexport function parseRisk(value: unknown): RiskParse {\n const read = riskSchema.safeParse(value);\n if (read.success) return { ok: true, layer: read.data };\n return { ok: false, errors: formErrors(RISK_SCHEMA, read.error.issues) };\n}\n"],"mappings":";;;;AAOA,MAAM,KAAK,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC;AAE3B,MAAM,OAAO,EAAE,aAAa;CAC1B,MAAM;CACN,UAAU,EAAE,OAAO;CACnB,QAAQ,EAAE,OAAO,CAAC,CAAC,SAAS;AAC9B,CAAC;AAED,MAAM,SAAS,EAAE,aAAa;CAC5B,MAAM;CACN,OAAO,EAAE,OAAO;CAChB,QAAQ,EAAE,OAAO,CAAC,CAAC,SAAS;AAC9B,CAAC;AAED,MAAM,WAAW,EAAE,aAAa;CAC9B,MAAM;CACN,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,SAAS;CAC3B,QAAQ,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC;CACxB,OAAO,EAAE,OAAO,CAAC,CAAC,SAAS;AAC7B,CAAC;;;;;AAMD,MAAa,aAAmC,EAAE,aAAa;CAC7D,QAAQ,EAAE,QAAQ,WAAW;CAC7B,OAAO,EAAE,MAAM,IAAI,CAAC,CAAC,SAAS;CAC9B,SAAS,EAAE,MAAM,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS;CAC7C,UAAU,EAAE,MAAM,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS;AAClD,CAAC;;;;;AASD,SAAgB,UAAU,OAA2B;CACnD,MAAM,OAAO,WAAW,UAAU,KAAK;CACvC,IAAI,KAAK,SAAS,OAAO;EAAE,IAAI;EAAM,OAAO,KAAK;CAAK;CACtD,OAAO;EAAE,IAAI;EAAO,QAAQ,WAAW,aAAa,KAAK,MAAM,MAAM;CAAE;AACzE"}
package/package.json CHANGED
@@ -1,6 +1,77 @@
1
1
  {
2
+ "$schema": "https://json.schemastore.org/package",
2
3
  "name": "@mark1russell7/stack-risk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
4
+ "version": "0.1.2",
5
+ "description": "The risks of a system stack as queries: the parts whose failure stops a function, the failures that a function survives, the chance that it works, and a gate. An add-on of system-stack.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/mark1russell7/system-stack.git",
10
+ "directory": "packages/stack-risk"
11
+ },
12
+ "homepage": "https://mark1russell7.github.io/system-stack/",
13
+ "bugs": {
14
+ "url": "https://github.com/mark1russell7/system-stack/issues"
15
+ },
16
+ "keywords": [
17
+ "risk",
18
+ "reliability",
19
+ "single-point-of-failure",
20
+ "cut-set",
21
+ "path-set",
22
+ "fault-tree",
23
+ "gate",
24
+ "system-stack"
25
+ ],
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "main": "./build/index.js",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./build/index.d.ts",
32
+ "import": "./build/index.js",
33
+ "default": "./build/index.js"
34
+ },
35
+ "./schema": {
36
+ "types": "./build/schema.d.ts",
37
+ "import": "./build/schema.js",
38
+ "default": "./build/schema.js"
39
+ },
40
+ "./add-on": {
41
+ "types": "./build/add-on.d.ts",
42
+ "import": "./build/add-on.js",
43
+ "default": "./build/add-on.js"
44
+ },
45
+ "./package.json": "./package.json"
46
+ },
47
+ "files": [
48
+ "build"
49
+ ],
50
+ "dependencies": {
51
+ "@mark1russell7/collections": "^0.11.2",
52
+ "@mark1russell7/system-stack": "0.1.4",
53
+ "zod": "^4.6.7"
54
+ },
55
+ "devDependencies": {
56
+ "@mark1russell7/cue": "^2.0.0",
57
+ "@types/node": "^26.6.5",
58
+ "@vitest/coverage-v8": "^5.0.3",
59
+ "fast-check": "^4.10.2",
60
+ "tsdown": "^0.23.1",
61
+ "typescript": "^7.0.2",
62
+ "vitest": "^5.0.3"
63
+ },
64
+ "engines": {
65
+ "node": ">=22"
66
+ },
67
+ "publishConfig": {
68
+ "access": "public"
69
+ },
70
+ "scripts": {
71
+ "build": "tsdown",
72
+ "test": "vitest run",
73
+ "test:watch": "vitest",
74
+ "typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json"
75
+ },
76
+ "types": "./build/index.d.ts"
6
77
  }