@mark1russell7/stack-placement 0.0.0-stage → 0.1.1

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/build/index.js ADDED
@@ -0,0 +1,477 @@
1
+ import { t as PLACEMENT_SCHEMA } from "./layer-BSj5LGH9.js";
2
+ import { n as PLACEMENT_CODES, r as checkPlacement, t as joinPlacement } from "./join-BHqjGNJL.js";
3
+ import { joinStacks } from "@mark1russell7/system-stack";
4
+ import { evolve, frontOf, sweep } from "@mark1russell7/strategy-search";
5
+ import { STRATEGY_SPACE_SCHEMA, checkSpace, configId, spaceSize } from "@mark1russell7/strategy-space";
6
+ // src/place.ts
7
+ /**
8
+ * These are the codes of the faults of a choice. Each one is an error.
9
+ *
10
+ * - `slot-unknown`: the choice names a slot that the layer does not have.
11
+ * - `slot-empty`: the choice leaves a slot empty, and the slot is not optional.
12
+ * - `candidate-unknown`: the choice names a key that the slot does not have.
13
+ * - `shared-unknown`: a chosen candidate uses a shared fragment that the layer does not have.
14
+ */
15
+ const CHOICE_CODES = [
16
+ "slot-unknown",
17
+ "slot-empty",
18
+ "candidate-unknown",
19
+ "shared-unknown"
20
+ ];
21
+ /** The name of the fragment of the base stack in a join. */
22
+ const BASE_FRAGMENT = "base";
23
+ /** This function gives the name of the fragment of the chosen candidate of a slot in a join. */
24
+ function slotFragment(slot) {
25
+ return `slot:${slot}`;
26
+ }
27
+ /** This function gives the name of a shared fragment in a join. */
28
+ function sharedFragment(id) {
29
+ return `shared:${id}`;
30
+ }
31
+ /** This function gives a fault of a choice. */
32
+ function choiceFault(code, at, message) {
33
+ return {
34
+ level: "error",
35
+ code,
36
+ layer: PLACEMENT_SCHEMA,
37
+ at,
38
+ from: [],
39
+ message
40
+ };
41
+ }
42
+ /** This function gives a stack with no placement layer. The other layers stay as they are. */
43
+ function withoutPlacement(stack) {
44
+ if (stack.layers === void 0 || !Object.hasOwn(stack.layers, "placement/1")) return stack;
45
+ const layers = Object.fromEntries(Object.entries(stack.layers).filter(([name]) => name !== PLACEMENT_SCHEMA));
46
+ return {
47
+ ...stack,
48
+ layers
49
+ };
50
+ }
51
+ /**
52
+ * This function gives the placed stack of one choice.
53
+ *
54
+ * - The fragments are the base stack with no placement layer, the chosen candidate of each slot, then each shared fragment that a chosen candidate uses.
55
+ * - A shared fragment is one fragment, also when two or more chosen candidates use it. Thus the stack counts a shared cause of failure one time.
56
+ * - `joinStacks` of the core joins the fragments. Each add-on of `joiners` joins its layer, for example the chances of `risk/1`.
57
+ * - The expected cost is the sum of the costs of the chosen candidates and of their shared fragments.
58
+ *
59
+ * A fault of the choice gives no join. The function is pure, and it does not change the layer or the stack.
60
+ *
61
+ * @param layer - The placement layer
62
+ * @param stack - The base stack. A placement layer of the stack is not part of the placed stack.
63
+ * @param choice - The key of the candidate of each slot
64
+ * @param joiners - The add-ons that join the layers of the fragments, for example the risk add-on and the spend add-on
65
+ * @returns The placed stack and its cost, or the findings
66
+ */
67
+ function placedStack(layer, stack, choice, joiners) {
68
+ const findings = [];
69
+ const slots = new Set(layer.slots.map((slot) => slot.id));
70
+ for (const id of Object.keys(choice).sort()) if (!slots.has(id)) findings.push(choiceFault("slot-unknown", id, `The choice names the slot "${id}". The layer has no slot "${id}".`));
71
+ const chosen = [];
72
+ for (const slot of layer.slots) {
73
+ const key = Object.hasOwn(choice, slot.id) ? choice[slot.id] : null;
74
+ if (key === null || key === void 0) {
75
+ if (slot.optional !== true) findings.push(choiceFault("slot-empty", slot.id, `The choice leaves the slot "${slot.id}" empty, and the slot is not optional.`));
76
+ continue;
77
+ }
78
+ const candidate = slot.candidates.find((item) => item.key === key);
79
+ if (candidate === void 0) findings.push(choiceFault("candidate-unknown", slot.id, `The slot "${slot.id}" has no candidate "${key}".`));
80
+ else chosen.push({
81
+ slot: slot.id,
82
+ candidate
83
+ });
84
+ }
85
+ const shared = layer.shared ?? [];
86
+ const known = new Set(shared.map((item) => item.id));
87
+ const uses = new Set(chosen.flatMap((item) => item.candidate.uses ?? []));
88
+ for (const id of uses) if (!known.has(id)) findings.push(choiceFault("shared-unknown", id, `A chosen candidate uses the shared fragment "${id}". The layer has no shared fragment "${id}".`));
89
+ if (findings.length > 0) return {
90
+ ok: false,
91
+ findings
92
+ };
93
+ const used = shared.filter((item) => uses.has(item.id));
94
+ const fragments = [
95
+ {
96
+ name: BASE_FRAGMENT,
97
+ stack: withoutPlacement(stack)
98
+ },
99
+ ...chosen.map((item) => ({
100
+ name: slotFragment(item.slot),
101
+ stack: item.candidate.stack
102
+ })),
103
+ ...used.map((item) => ({
104
+ name: sharedFragment(item.id),
105
+ stack: item.stack
106
+ }))
107
+ ];
108
+ const joined = joinStacks(fragments, joiners);
109
+ if (!joined.ok) return {
110
+ ok: false,
111
+ findings: joined.findings
112
+ };
113
+ let cost = 0;
114
+ for (const item of chosen) cost += item.candidate.cost;
115
+ for (const item of used) cost += item.cost ?? 0;
116
+ return {
117
+ ok: true,
118
+ stack: joined.stack,
119
+ cost,
120
+ shared: used.map((item) => item.id)
121
+ };
122
+ }
123
+ // src/plan.ts
124
+ /** The name of the strategy space of a placement. A config of the search names the space with it. */
125
+ const PLACEMENT_ENGINE = "stack-placement";
126
+ /** The name and the version of the evaluation of a choice. It is part of the `id` of a run. */
127
+ const PLACEMENT_EVALUATOR = "stack-placement/1";
128
+ /** The objectives of a plan: the expected cost of a month, as low as possible. And the chance that the part works, as high as possible. */
129
+ const PLACEMENT_OBJECTIVES = [{
130
+ name: "cost",
131
+ goal: "min",
132
+ unit: "currency of the layer"
133
+ }, {
134
+ name: "chance",
135
+ goal: "max",
136
+ unit: "chance from 0 to 1"
137
+ }];
138
+ /**
139
+ * These are the names of the breaks of a choice. A choice with a break is not on the front.
140
+ *
141
+ * - `join`: the choice has a fault, or its fragments do not join into one stack.
142
+ * - `measure`: a measure throws a `RangeError` for the placed stack. Or it gives a value out of its range.
143
+ * - `cost`: the expected cost is more than the limit `cost`.
144
+ * - `bound`: the bound of the cost of a month is more than the limit `bound`, or a service has no bound.
145
+ * - `chance`: the chance that the part works is less than the limit `chance`.
146
+ */
147
+ const PLAN_BREAKS = [
148
+ "join",
149
+ "measure",
150
+ "cost",
151
+ "bound",
152
+ "chance"
153
+ ];
154
+ /** The defaults of a plan. */
155
+ const PLAN_DEFAULTS = {
156
+ evaluations: 2e3,
157
+ seed: "stack-placement",
158
+ importance: 5
159
+ };
160
+ /** The relative tolerance of a comparison with a limit. Thus a sum with a rounding error does not break a limit. */
161
+ const LIMIT_TOLERANCE = 1e-9;
162
+ /**
163
+ * The steps of the values that the search compares: a cost to 1e-9 of the currency, a chance to 1e-12. Two choices that give
164
+ * one true value can give floats with a different rounding error. With these steps, such choices tie, and the front keeps each.
165
+ * A row keeps the value before the step.
166
+ */
167
+ const OBJECTIVE_STEPS = {
168
+ cost: 1e-9,
169
+ chance: 1e-12
170
+ };
171
+ /**
172
+ * This function gives the strategy space of a layer. Each slot is one stage, and each candidate is one option with no params.
173
+ * An optional slot is an optional stage.
174
+ */
175
+ function placementSpace(layer) {
176
+ return {
177
+ schema: STRATEGY_SPACE_SCHEMA,
178
+ engine: PLACEMENT_ENGINE,
179
+ version: "1",
180
+ stages: layer.slots.map((slot) => ({
181
+ name: slot.id,
182
+ options: slot.candidates.map((candidate) => ({
183
+ id: candidate.key,
184
+ params: []
185
+ })),
186
+ ...slot.optional === true ? { optional: true } : {}
187
+ }))
188
+ };
189
+ }
190
+ /** This function gives the choice of a config of the space of a layer. A stage with no option gives the value `null`. */
191
+ function choiceOf(config) {
192
+ return Object.fromEntries(Object.entries(config.stages).map(([stage, picked]) => [stage, picked?.option ?? null]));
193
+ }
194
+ /** This function tells if a whole number is 1 or more. */
195
+ function isCount(value) {
196
+ return Number.isSafeInteger(value) && value >= 1;
197
+ }
198
+ /** This function tells if a value is more than a limit, with the tolerance `LIMIT_TOLERANCE`. */
199
+ function over(value, limit) {
200
+ return value > limit + LIMIT_TOLERANCE * Math.max(1, Math.abs(limit));
201
+ }
202
+ /** This function gives a value at the nearest multiple of a step. A value that is not finite stays as it is. */
203
+ function stepped(value, step) {
204
+ return Number.isFinite(value) ? Math.round(value / step) * step : value;
205
+ }
206
+ /** This function tells if a value is a finite number, 0 or more. */
207
+ function isZeroOrMore(value) {
208
+ return typeof value === "number" && Number.isFinite(value) && value >= 0;
209
+ }
210
+ /**
211
+ * This function gives the problems of the options of a plan for a layer. A problem is a bad count, a measure that is not a function,
212
+ * or the limit `bound` with no bound measure.
213
+ *
214
+ * @param options - The options of a plan
215
+ * @param layer - The placement layer, for the check of its limits
216
+ * @returns The problems, or an empty list for good options
217
+ */
218
+ function planProblems(options, layer) {
219
+ const problems = [];
220
+ for (const [name, value] of [["evaluations", options.evaluations], ["importance", options.importance]]) if (value !== void 0 && !isCount(value)) problems.push(`The option "${name}" is ${String(value)}. It must be a whole number of 1 or more.`);
221
+ for (const [name, value, needed] of [
222
+ [
223
+ "chance",
224
+ options.chance,
225
+ true
226
+ ],
227
+ [
228
+ "bound",
229
+ options.bound,
230
+ false
231
+ ],
232
+ [
233
+ "cost",
234
+ options.cost,
235
+ false
236
+ ]
237
+ ]) if ((needed || value !== void 0) && typeof value !== "function") problems.push(`The measure "${name}" is not a function.`);
238
+ if (layer?.limits?.bound !== void 0 && options.bound === void 0) problems.push(`The layer has the limit "bound", and the options have no bound measure.`);
239
+ return problems;
240
+ }
241
+ /**
242
+ * This function gives the plan of a placement: the front of the choices, with the expected cost against the chance that the part works.
243
+ *
244
+ * - Each choice gives one placed stack, from `placedStack`. The measures of the options give its cost, its chance and its bound.
245
+ * - A space with the count `evaluations` of choices or less gets a grid of each choice. A larger space gets a seeded genetic search.
246
+ * - A choice that breaks a limit, or whose stack has a fault, is not on the front. `breaks` counts each break.
247
+ *
248
+ * The result is deterministic when each measure is deterministic: one layer, one stack and one set of options give one plan.
249
+ *
250
+ * @param layer - The placement layer
251
+ * @param stack - The base stack
252
+ * @param options - The joiners, the measures and the options of the search
253
+ * @returns The plan
254
+ * @throws RangeError when an option is bad, or when the space of the layer has a problem, for example a slot with no candidate
255
+ */
256
+ function planPlacement(layer, stack, options) {
257
+ const problems = planProblems(options, layer);
258
+ const space = placementSpace(layer);
259
+ problems.push(...checkSpace(space).map((item) => item.detail));
260
+ if (problems.length > 0) throw new RangeError(`The plan of the placement has problems: ${problems.join(" ")}`);
261
+ const evaluations = options.evaluations ?? PLAN_DEFAULTS.evaluations;
262
+ const importance = options.importance ?? PLAN_DEFAULTS.importance;
263
+ const limits = layer.limits ?? {};
264
+ const sources = (choice) => {
265
+ const found = {};
266
+ for (const slot of layer.slots) {
267
+ const source = slot.candidates.find((candidate) => candidate.key === choice[slot.id])?.source;
268
+ if (source !== void 0) found[slot.id] = source;
269
+ }
270
+ return found;
271
+ };
272
+ const read = (placed, layerCost) => {
273
+ try {
274
+ const cost = options.cost === void 0 ? layerCost : options.cost(placed);
275
+ const given = options.chance(placed, layer.part);
276
+ const chance = typeof given === "number" ? { value: given } : given;
277
+ const bound = options.bound === void 0 ? null : options.bound(placed);
278
+ if (!isZeroOrMore(cost)) return void 0;
279
+ if (typeof chance.value !== "number" || !(Number.isNaN(chance.value) || chance.value >= 0 && chance.value <= 1)) return void 0;
280
+ if (bound !== null && !(isZeroOrMore(bound.bounded) && Array.isArray(bound.open))) return void 0;
281
+ return {
282
+ cost,
283
+ chance,
284
+ bound
285
+ };
286
+ } catch (error) {
287
+ if (error instanceof RangeError) return void 0;
288
+ throw error;
289
+ }
290
+ };
291
+ const assess = (config, choice) => {
292
+ const placed = placedStack(layer, stack, choice, options.joiners);
293
+ if (!placed.ok) return { measure: {
294
+ breaks: ["join"],
295
+ values: {}
296
+ } };
297
+ const readings = read(placed.stack, placed.cost);
298
+ if (readings === void 0) return { measure: {
299
+ breaks: ["measure"],
300
+ values: {}
301
+ } };
302
+ const { cost, bound } = readings;
303
+ const chance = readings.chance.value;
304
+ const breaks = [];
305
+ if (limits.cost !== void 0 && over(cost, limits.cost)) breaks.push("cost");
306
+ if (limits.bound !== void 0 && bound !== null && (bound.open.length > 0 || over(bound.bounded, limits.bound))) breaks.push("bound");
307
+ if (limits.chance !== void 0 && !Number.isNaN(chance) && over(limits.chance, chance)) breaks.push("chance");
308
+ const measure = {
309
+ breaks,
310
+ values: {
311
+ cost: stepped(cost, OBJECTIVE_STEPS.cost),
312
+ chance: stepped(chance, OBJECTIVE_STEPS.chance)
313
+ }
314
+ };
315
+ if (Number.isNaN(chance) || breaks.length > 0) return { measure };
316
+ return {
317
+ measure,
318
+ row: {
319
+ config,
320
+ choice,
321
+ sources: sources(choice),
322
+ shared: placed.shared,
323
+ cost,
324
+ chance,
325
+ bound,
326
+ importance: (readings.chance.importance ?? []).slice(0, importance),
327
+ assumed: readings.chance.assumed ?? []
328
+ }
329
+ };
330
+ };
331
+ const assessed = /* @__PURE__ */ new Map();
332
+ const evaluate = (config) => {
333
+ const id = configId(space, config);
334
+ const known = assessed.get(id);
335
+ if (known !== void 0) return known.measure;
336
+ const fresh = assess(id, choiceOf(config));
337
+ assessed.set(id, fresh);
338
+ return fresh.measure;
339
+ };
340
+ const size = spaceSize(space, { levels: 1 }).base;
341
+ const grid = size <= BigInt(evaluations);
342
+ const run = grid ? sweep(space, {
343
+ objectives: PLACEMENT_OBJECTIVES,
344
+ evaluate,
345
+ enumerate: {
346
+ mode: "grid",
347
+ levels: 1,
348
+ limit: Math.max(1, Number(size))
349
+ },
350
+ evaluator: PLACEMENT_EVALUATOR
351
+ }) : evolve(space, {
352
+ objectives: PLACEMENT_OBJECTIVES,
353
+ evaluate,
354
+ seed: options.seed ?? PLAN_DEFAULTS.seed,
355
+ evaluations,
356
+ evaluator: PLACEMENT_EVALUATOR
357
+ });
358
+ const front = frontOf(run).map((entry) => assessed.get(entry.row.config)?.row).filter((row) => row !== void 0).sort((a, b) => a.cost - b.cost || b.chance - a.chance || (a.config < b.config ? -1 : a.config > b.config ? 1 : 0));
359
+ return {
360
+ method: grid ? "grid" : "evolve",
361
+ run: run.run,
362
+ evaluations: run.rows.length,
363
+ size: size.toString(),
364
+ counts: run.counts,
365
+ front,
366
+ breaks: breakCounts(run.rows)
367
+ };
368
+ }
369
+ /** This function counts the rows with each break, in the order of `PLAN_BREAKS`. */
370
+ function breakCounts(rows) {
371
+ const counts = {};
372
+ for (const name of PLAN_BREAKS) {
373
+ const count = rows.filter((row) => row.breaks.includes(name)).length;
374
+ if (count > 0) counts[name] = count;
375
+ }
376
+ return counts;
377
+ }
378
+ // src/sample.ts
379
+ /** The date of the add-ons of the sample. */
380
+ const SAMPLE_TODAY = "2026-11-18";
381
+ /** This function gives a sample candidate: one host of a provider, with its chance and its fixed price. */
382
+ function sampleHost(slot, key, provider, works, cost) {
383
+ return {
384
+ key,
385
+ cost,
386
+ uses: [provider],
387
+ source: `offer-${key}`,
388
+ stack: {
389
+ schema: "stack/1",
390
+ parts: [{
391
+ id: slot,
392
+ kind: "host",
393
+ needs: [provider]
394
+ }],
395
+ layers: {
396
+ "risk/1": {
397
+ schema: "risk/1",
398
+ goals: [],
399
+ chances: [{
400
+ part: slot,
401
+ works,
402
+ source: "An estimate"
403
+ }]
404
+ },
405
+ "spend/1": {
406
+ schema: "spend/1",
407
+ currency: "USD",
408
+ kinds: [],
409
+ proofDays: 30,
410
+ services: [{
411
+ part: slot,
412
+ billing: "fixed",
413
+ price: cost
414
+ }]
415
+ }
416
+ }
417
+ }
418
+ };
419
+ }
420
+ /** This function gives a sample shared fragment: one provider, with its chance. */
421
+ function sampleProvider(id, works) {
422
+ return {
423
+ id,
424
+ stack: {
425
+ schema: "stack/1",
426
+ parts: [{
427
+ id,
428
+ kind: "provider"
429
+ }],
430
+ layers: { "risk/1": {
431
+ schema: "risk/1",
432
+ goals: [],
433
+ chances: [{
434
+ part: id,
435
+ works,
436
+ source: "An estimate"
437
+ }]
438
+ } }
439
+ }
440
+ };
441
+ }
442
+ /** The base stack of the sample. The function `send` works when one of the parts `copy-a` and `copy-b` works. */
443
+ const SAMPLE_BASE = {
444
+ schema: "stack/1",
445
+ parts: [{
446
+ id: "send",
447
+ kind: "function",
448
+ name: "A message gets to its reader",
449
+ needs: [{ any: ["copy-a", "copy-b"] }]
450
+ }]
451
+ };
452
+ /**
453
+ * The sample placement layer. Each name and each number is an example.
454
+ * The slot `copy-a` needs a candidate, and the slot `copy-b` can stay empty. The two candidates of a slot use two different providers.
455
+ */
456
+ const SAMPLE_PLACEMENT = {
457
+ schema: PLACEMENT_SCHEMA,
458
+ part: "send",
459
+ currency: "USD",
460
+ slots: [{
461
+ id: "copy-a",
462
+ candidates: [sampleHost("copy-a", "small-x", "provider-x", .99, 5), sampleHost("copy-a", "small-y", "provider-y", .98, 6)]
463
+ }, {
464
+ id: "copy-b",
465
+ optional: true,
466
+ candidates: [sampleHost("copy-b", "small-x", "provider-x", .99, 5), sampleHost("copy-b", "small-y", "provider-y", .98, 6)]
467
+ }],
468
+ shared: [sampleProvider("provider-x", .999), sampleProvider("provider-y", .995)]
469
+ };
470
+ /** The base stack of the sample with the sample placement layer. */
471
+ const SAMPLE_STACK = {
472
+ ...SAMPLE_BASE,
473
+ layers: { [PLACEMENT_SCHEMA]: SAMPLE_PLACEMENT }
474
+ };
475
+ export { BASE_FRAGMENT, CHOICE_CODES, LIMIT_TOLERANCE, OBJECTIVE_STEPS, PLACEMENT_CODES, PLACEMENT_ENGINE, PLACEMENT_EVALUATOR, PLACEMENT_OBJECTIVES, PLACEMENT_SCHEMA, PLAN_BREAKS, PLAN_DEFAULTS, SAMPLE_BASE, SAMPLE_PLACEMENT, SAMPLE_STACK, SAMPLE_TODAY, checkPlacement, choiceOf, joinPlacement, placedStack, placementSpace, planPlacement, planProblems, sharedFragment, slotFragment };
476
+
477
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/place.ts","../src/plan.ts","../src/sample.ts"],"sourcesContent":["import { joinStacks, type LayerJoiner, type Stack, type StackFragment, type StackJoinFinding } from \"@mark1russell7/system-stack\";\nimport { PLACEMENT_SCHEMA, type Candidate, type Choice, type PlacementLayer, type Shared } from \"./layer.ts\";\n\n// The placed stack of one choice: the base stack, the chosen candidates and their shared fragments, as one stack.\n\n/**\n * These are the codes of the faults of a choice. Each one is an error.\n *\n * - `slot-unknown`: the choice names a slot that the layer does not have.\n * - `slot-empty`: the choice leaves a slot empty, and the slot is not optional.\n * - `candidate-unknown`: the choice names a key that the slot does not have.\n * - `shared-unknown`: a chosen candidate uses a shared fragment that the layer does not have.\n */\nexport const CHOICE_CODES: readonly [\"slot-unknown\", \"slot-empty\", \"candidate-unknown\", \"shared-unknown\"] = [\"slot-unknown\", \"slot-empty\", \"candidate-unknown\", \"shared-unknown\"];\n\n/** A code of `CHOICE_CODES`. */\nexport type ChoiceCode = (typeof CHOICE_CODES)[number];\n\n/** The name of the fragment of the base stack in a join. */\nexport const BASE_FRAGMENT = \"base\";\n\n/** This function gives the name of the fragment of the chosen candidate of a slot in a join. */\nexport function slotFragment(slot: string): string {\n return `slot:${slot}`;\n}\n\n/** This function gives the name of a shared fragment in a join. */\nexport function sharedFragment(id: string): string {\n return `shared:${id}`;\n}\n\n/**\n * The result of `placedStack`.\n *\n * - `ok: true`: `stack` is the placed stack. `cost` is the expected cost of a month. `shared` has the `id` of each shared fragment of the stack.\n * - `ok: false`: `findings` has the faults of the choice, or the findings of the join of the fragments.\n */\nexport type Placed =\n | { readonly ok: true; readonly stack: Stack; readonly cost: number; readonly shared: readonly string[] }\n | { readonly ok: false; readonly findings: readonly StackJoinFinding[] };\n\n/** One chosen candidate and its slot. */\ninterface Chosen {\n readonly slot: string;\n readonly candidate: Candidate;\n}\n\n/** This function gives a fault of a choice. */\nfunction choiceFault(code: ChoiceCode, at: string, message: string): StackJoinFinding {\n return { level: \"error\", code, layer: PLACEMENT_SCHEMA, at, from: [], message };\n}\n\n/** This function gives a stack with no placement layer. The other layers stay as they are. */\nfunction withoutPlacement(stack: Stack): Stack {\n if (stack.layers === undefined || !Object.hasOwn(stack.layers, PLACEMENT_SCHEMA)) return stack;\n const layers = Object.fromEntries(Object.entries(stack.layers).filter(([name]) => name !== PLACEMENT_SCHEMA));\n return { ...stack, layers };\n}\n\n/**\n * This function gives the placed stack of one choice.\n *\n * - The fragments are the base stack with no placement layer, the chosen candidate of each slot, then each shared fragment that a chosen candidate uses.\n * - A shared fragment is one fragment, also when two or more chosen candidates use it. Thus the stack counts a shared cause of failure one time.\n * - `joinStacks` of the core joins the fragments. Each add-on of `joiners` joins its layer, for example the chances of `risk/1`.\n * - The expected cost is the sum of the costs of the chosen candidates and of their shared fragments.\n *\n * A fault of the choice gives no join. The function is pure, and it does not change the layer or the stack.\n *\n * @param layer - The placement layer\n * @param stack - The base stack. A placement layer of the stack is not part of the placed stack.\n * @param choice - The key of the candidate of each slot\n * @param joiners - The add-ons that join the layers of the fragments, for example the risk add-on and the spend add-on\n * @returns The placed stack and its cost, or the findings\n */\nexport function placedStack(layer: PlacementLayer, stack: Stack, choice: Choice, joiners: readonly LayerJoiner<unknown>[]): Placed {\n const findings: StackJoinFinding[] = [];\n const slots = new Set(layer.slots.map((slot) => slot.id));\n for (const id of Object.keys(choice).sort()) {\n if (!slots.has(id)) findings.push(choiceFault(\"slot-unknown\", id, `The choice names the slot \"${id}\". The layer has no slot \"${id}\".`));\n }\n const chosen: Chosen[] = [];\n for (const slot of layer.slots) {\n const key = Object.hasOwn(choice, slot.id) ? choice[slot.id] : null;\n if (key === null || key === undefined) {\n if (slot.optional !== true) findings.push(choiceFault(\"slot-empty\", slot.id, `The choice leaves the slot \"${slot.id}\" empty, and the slot is not optional.`));\n continue;\n }\n const candidate = slot.candidates.find((item) => item.key === key);\n if (candidate === undefined) findings.push(choiceFault(\"candidate-unknown\", slot.id, `The slot \"${slot.id}\" has no candidate \"${key}\".`));\n else chosen.push({ slot: slot.id, candidate });\n }\n const shared: readonly Shared[] = layer.shared ?? [];\n const known = new Set(shared.map((item) => item.id));\n const uses = new Set(chosen.flatMap((item) => item.candidate.uses ?? []));\n for (const id of uses) {\n if (!known.has(id)) findings.push(choiceFault(\"shared-unknown\", id, `A chosen candidate uses the shared fragment \"${id}\". The layer has no shared fragment \"${id}\".`));\n }\n if (findings.length > 0) return { ok: false, findings };\n const used = shared.filter((item) => uses.has(item.id));\n const fragments: StackFragment[] = [\n { name: BASE_FRAGMENT, stack: withoutPlacement(stack) },\n ...chosen.map((item) => ({ name: slotFragment(item.slot), stack: item.candidate.stack })),\n ...used.map((item) => ({ name: sharedFragment(item.id), stack: item.stack })),\n ];\n const joined = joinStacks(fragments, joiners);\n if (!joined.ok) return { ok: false, findings: joined.findings };\n let cost = 0;\n for (const item of chosen) cost += item.candidate.cost;\n for (const item of used) cost += item.cost ?? 0;\n return { ok: true, stack: joined.stack, cost, shared: used.map((item) => item.id) };\n}\n","import { evolve, frontOf, sweep, type EvaluationRow, type Measure, type Objective, type SweepCounts } from \"@mark1russell7/strategy-search\";\nimport { checkSpace, configId, spaceSize, STRATEGY_SPACE_SCHEMA, type StrategyConfig, type StrategySpace } from \"@mark1russell7/strategy-space\";\nimport type { LayerJoiner, Stack } from \"@mark1russell7/system-stack\";\nimport type { Choice, PlacementLayer } from \"./layer.ts\";\nimport { placedStack } from \"./place.ts\";\n\n// The plan of a placement: the front of the choices, with the expected cost against the chance that the part works.\n\n/** The name of the strategy space of a placement. A config of the search names the space with it. */\nexport const PLACEMENT_ENGINE = \"stack-placement\";\n\n/** The name and the version of the evaluation of a choice. It is part of the `id` of a run. */\nexport const PLACEMENT_EVALUATOR = \"stack-placement/1\";\n\n/** The objectives of a plan: the expected cost of a month, as low as possible. And the chance that the part works, as high as possible. */\nexport const PLACEMENT_OBJECTIVES: readonly Objective[] = [\n { name: \"cost\", goal: \"min\", unit: \"currency of the layer\" },\n { name: \"chance\", goal: \"max\", unit: \"chance from 0 to 1\" },\n];\n\n/**\n * These are the names of the breaks of a choice. A choice with a break is not on the front.\n *\n * - `join`: the choice has a fault, or its fragments do not join into one stack.\n * - `measure`: a measure throws a `RangeError` for the placed stack. Or it gives a value out of its range.\n * - `cost`: the expected cost is more than the limit `cost`.\n * - `bound`: the bound of the cost of a month is more than the limit `bound`, or a service has no bound.\n * - `chance`: the chance that the part works is less than the limit `chance`.\n */\nexport const PLAN_BREAKS: readonly [\"join\", \"measure\", \"cost\", \"bound\", \"chance\"] = [\"join\", \"measure\", \"cost\", \"bound\", \"chance\"];\n\n/** A name of `PLAN_BREAKS`. */\nexport type PlanBreak = (typeof PLAN_BREAKS)[number];\n\n/** The defaults of a plan. */\nexport const PLAN_DEFAULTS: { readonly evaluations: 2000; readonly seed: \"stack-placement\"; readonly importance: 5 } = { evaluations: 2000, seed: \"stack-placement\", importance: 5 };\n\n/** The relative tolerance of a comparison with a limit. Thus a sum with a rounding error does not break a limit. */\nexport const LIMIT_TOLERANCE = 1e-9;\n\n/**\n * The steps of the values that the search compares: a cost to 1e-9 of the currency, a chance to 1e-12. Two choices that give\n * one true value can give floats with a different rounding error. With these steps, such choices tie, and the front keeps each.\n * A row keeps the value before the step.\n */\nexport const OBJECTIVE_STEPS: { readonly cost: 1e-9; readonly chance: 1e-12 } = { cost: 1e-9, chance: 1e-12 };\n\n/** The importance of one part for a chance, for example the Birnbaum importance of `stack-risk`. */\nexport interface PartWeight {\n /** The `id` of the part. */\n readonly part: string;\n /** The chance when the part works, minus the chance when the part fails. */\n readonly importance: number;\n}\n\n/**\n * The reading of a chance measure. The value NaN tells that the measure cannot decide, for example at the end of a budget of steps.\n * The search counts such a choice as unread, and the front does not have it.\n */\nexport interface ChanceReading {\n /** The chance that the part works, from 0 to 1, or NaN. */\n readonly value: number;\n /** The most important parts for the chance, from the largest importance. */\n readonly importance?: readonly PartWeight[] | undefined;\n /** The parts that the measure takes as parts that work, because it has no chance for them. */\n readonly assumed?: readonly string[] | undefined;\n}\n\n/** The reading of a bound measure: the largest cost of a month, and the part of each service with no bound. */\nexport interface BoundReading {\n /** The sum of the largest costs of the services with a bound, in the currency of the layer. */\n readonly bounded: number;\n /** The part of each service with no bound. With one or more, the cost of the placed stack has no bound. */\n readonly open: readonly string[];\n}\n\n/**\n * The measure of the chance that the part `part` of a placed stack works. The caller makes it, for example from `worksChance` of `stack-risk`.\n * A measure throws a `RangeError` for a stack that it refuses.\n */\nexport type ChanceMeasure = (stack: Stack, part: string) => number | ChanceReading;\n\n/** The measure of the bound of the cost of a month of a placed stack, for example from `monthTotal` of `stack-spend`. */\nexport type BoundMeasure = (stack: Stack) => BoundReading;\n\n/** The measure of the expected cost of a month of a placed stack. */\nexport type CostMeasure = (stack: Stack) => number;\n\n/**\n * The options of `planPlacement`. The add-on knows no other add-on. Thus the caller gives the joiners and the measures.\n * A host, a site or a test makes them, for example from the add-ons of risk and spend.\n */\nexport interface PlanOptions {\n /** The add-ons that join the layers of the fragments, one for each layer of a fragment. */\n readonly joiners: readonly LayerJoiner<unknown>[];\n /** The measure of the chance. */\n readonly chance: ChanceMeasure;\n /** The measure of the bound of the cost. A layer with the limit `bound` needs it. Without it, each row has the bound `null`. */\n readonly bound?: BoundMeasure | undefined;\n /** The measure of the expected cost. The default is the sum of the costs of the chosen candidates and of their shared fragments. */\n readonly cost?: CostMeasure | undefined;\n /** The most evaluations. A space with this count of choices or less gets a grid of each choice. A larger space gets a genetic search. The default is 2000. */\n readonly evaluations?: number | undefined;\n /** The seed of a genetic search: a whole number, or a name. The default is `stack-placement`. */\n readonly seed?: number | string | undefined;\n /** The count of the most important parts in each row of the front. The default is 5. */\n readonly importance?: number | undefined;\n}\n\n/** One row of the front of a plan: a choice that keeps each limit, and its measures. */\nexport interface PlacementRow {\n /** The `id` of the config of the choice in the search. */\n readonly config: string;\n /** The key of the candidate of each slot. An empty optional slot has the value `null`. */\n readonly choice: Choice;\n /** The source of each chosen candidate with a source, by the `id` of its slot. */\n readonly sources: Readonly<Record<string, string>>;\n /** The `id` of each shared fragment of the placed stack. */\n readonly shared: readonly string[];\n /** The expected cost of a month. */\n readonly cost: number;\n /** The chance that the part of the layer works. */\n readonly chance: number;\n /** The reading of the bound measure, or `null` when the plan has no bound measure. */\n readonly bound: BoundReading | null;\n /** The most important parts for the chance, from the reading of the chance measure. */\n readonly importance: readonly PartWeight[];\n /** The parts that the chance measure takes as parts that work. */\n readonly assumed: readonly string[];\n}\n\n/**\n * The plan of a placement.\n *\n * - `method`: `grid` for each choice of the space, `evolve` for a genetic search of a larger space.\n * - `front`: the choices that no other choice beats on the cost and on the chance, from the lowest cost.\n * Rows with one cost are from the largest chance. Rows with equal values are in the order of their config.\n * - `breaks`: the count of the evaluated choices with each break, for the breaks with one or more choices.\n */\nexport interface PlacementPlan {\n readonly method: \"grid\" | \"evolve\";\n /** The `id` of the run of the search, from the space, the enumeration, the objectives and the evaluator. */\n readonly run: string;\n /** The count of the evaluated choices. */\n readonly evaluations: number;\n /** The count of the choices of the space, as a decimal text. The count can be larger than the largest safe number. */\n readonly size: string;\n readonly counts: SweepCounts;\n readonly front: readonly PlacementRow[];\n readonly breaks: Readonly<Partial<Record<PlanBreak, number>>>;\n}\n\n/**\n * This function gives the strategy space of a layer. Each slot is one stage, and each candidate is one option with no params.\n * An optional slot is an optional stage.\n */\nexport function placementSpace(layer: PlacementLayer): StrategySpace {\n return {\n schema: STRATEGY_SPACE_SCHEMA,\n engine: PLACEMENT_ENGINE,\n version: \"1\",\n stages: layer.slots.map((slot) => ({\n name: slot.id,\n options: slot.candidates.map((candidate) => ({ id: candidate.key, params: [] })),\n ...(slot.optional === true ? { optional: true } : {}),\n })),\n };\n}\n\n/** This function gives the choice of a config of the space of a layer. A stage with no option gives the value `null`. */\nexport function choiceOf(config: StrategyConfig): Choice {\n return Object.fromEntries(Object.entries(config.stages).map(([stage, picked]) => [stage, picked?.option ?? null]));\n}\n\n/** This function tells if a whole number is 1 or more. */\nfunction isCount(value: number): boolean {\n return Number.isSafeInteger(value) && value >= 1;\n}\n\n/** This function tells if a value is more than a limit, with the tolerance `LIMIT_TOLERANCE`. */\nfunction over(value: number, limit: number): boolean {\n return value > limit + LIMIT_TOLERANCE * Math.max(1, Math.abs(limit));\n}\n\n/** This function gives a value at the nearest multiple of a step. A value that is not finite stays as it is. */\nfunction stepped(value: number, step: number): number {\n return Number.isFinite(value) ? Math.round(value / step) * step : value;\n}\n\n/** This function tells if a value is a finite number, 0 or more. */\nfunction isZeroOrMore(value: unknown): value is number {\n return typeof value === \"number\" && Number.isFinite(value) && value >= 0;\n}\n\n/**\n * This function gives the problems of the options of a plan for a layer. A problem is a bad count, a measure that is not a function,\n * or the limit `bound` with no bound measure.\n *\n * @param options - The options of a plan\n * @param layer - The placement layer, for the check of its limits\n * @returns The problems, or an empty list for good options\n */\nexport function planProblems(options: PlanOptions, layer?: PlacementLayer): string[] {\n const problems: string[] = [];\n for (const [name, value] of [\n [\"evaluations\", options.evaluations],\n [\"importance\", options.importance],\n ] as const) {\n if (value !== undefined && !isCount(value)) problems.push(`The option \"${name}\" is ${String(value)}. It must be a whole number of 1 or more.`);\n }\n for (const [name, value, needed] of [\n [\"chance\", options.chance, true],\n [\"bound\", options.bound, false],\n [\"cost\", options.cost, false],\n ] as const) {\n if ((needed || value !== undefined) && typeof value !== \"function\") problems.push(`The measure \"${name}\" is not a function.`);\n }\n if (layer?.limits?.bound !== undefined && options.bound === undefined) problems.push(`The layer has the limit \"bound\", and the options have no bound measure.`);\n return problems;\n}\n\n/** The assessment of one choice: its measure for the search, and its row when it keeps each limit. */\ninterface Assessment {\n readonly measure: Measure;\n readonly row?: PlacementRow;\n}\n\n/** The readings of the measures of one placed stack. */\ninterface Readings {\n readonly cost: number;\n readonly chance: ChanceReading;\n readonly bound: BoundReading | null;\n}\n\n/**\n * This function gives the plan of a placement: the front of the choices, with the expected cost against the chance that the part works.\n *\n * - Each choice gives one placed stack, from `placedStack`. The measures of the options give its cost, its chance and its bound.\n * - A space with the count `evaluations` of choices or less gets a grid of each choice. A larger space gets a seeded genetic search.\n * - A choice that breaks a limit, or whose stack has a fault, is not on the front. `breaks` counts each break.\n *\n * The result is deterministic when each measure is deterministic: one layer, one stack and one set of options give one plan.\n *\n * @param layer - The placement layer\n * @param stack - The base stack\n * @param options - The joiners, the measures and the options of the search\n * @returns The plan\n * @throws RangeError when an option is bad, or when the space of the layer has a problem, for example a slot with no candidate\n */\nexport function planPlacement(layer: PlacementLayer, stack: Stack, options: PlanOptions): PlacementPlan {\n const problems = planProblems(options, layer);\n const space = placementSpace(layer);\n problems.push(...checkSpace(space).map((item) => item.detail));\n if (problems.length > 0) throw new RangeError(`The plan of the placement has problems: ${problems.join(\" \")}`);\n const evaluations = options.evaluations ?? PLAN_DEFAULTS.evaluations;\n const importance = options.importance ?? PLAN_DEFAULTS.importance;\n const limits = layer.limits ?? {};\n const sources = (choice: Choice): Record<string, string> => {\n const found: Record<string, string> = {};\n for (const slot of layer.slots) {\n const source = slot.candidates.find((candidate) => candidate.key === choice[slot.id])?.source;\n if (source !== undefined) found[slot.id] = source;\n }\n return found;\n };\n // This function gives the readings of the measures of a placed stack, or `undefined` when a measure refuses the stack.\n const read = (placed: Stack, layerCost: number): Readings | undefined => {\n try {\n const cost = options.cost === undefined ? layerCost : options.cost(placed);\n const given = options.chance(placed, layer.part);\n const chance: ChanceReading = typeof given === \"number\" ? { value: given } : given;\n const bound = options.bound === undefined ? null : options.bound(placed);\n if (!isZeroOrMore(cost)) return undefined;\n if (typeof chance.value !== \"number\" || !(Number.isNaN(chance.value) || (chance.value >= 0 && chance.value <= 1))) return undefined;\n if (bound !== null && !(isZeroOrMore(bound.bounded) && Array.isArray(bound.open))) return undefined;\n return { cost, chance, bound };\n } catch (error) {\n if (error instanceof RangeError) return undefined;\n throw error;\n }\n };\n const assess = (config: string, choice: Choice): Assessment => {\n const placed = placedStack(layer, stack, choice, options.joiners);\n if (!placed.ok) return { measure: { breaks: [\"join\"], values: {} } };\n const readings = read(placed.stack, placed.cost);\n if (readings === undefined) return { measure: { breaks: [\"measure\"], values: {} } };\n const { cost, bound } = readings;\n const chance = readings.chance.value;\n const breaks: PlanBreak[] = [];\n if (limits.cost !== undefined && over(cost, limits.cost)) breaks.push(\"cost\");\n if (limits.bound !== undefined && bound !== null && (bound.open.length > 0 || over(bound.bounded, limits.bound))) breaks.push(\"bound\");\n if (limits.chance !== undefined && !Number.isNaN(chance) && over(limits.chance, chance)) breaks.push(\"chance\");\n const measure: Measure = { breaks, values: { cost: stepped(cost, OBJECTIVE_STEPS.cost), chance: stepped(chance, OBJECTIVE_STEPS.chance) } };\n if (Number.isNaN(chance) || breaks.length > 0) return { measure };\n return {\n measure,\n row: {\n config,\n choice,\n sources: sources(choice),\n shared: placed.shared,\n cost,\n chance,\n bound,\n importance: (readings.chance.importance ?? []).slice(0, importance),\n assumed: readings.chance.assumed ?? [],\n },\n };\n };\n const assessed = new Map<string, Assessment>();\n const evaluate = (config: StrategyConfig): Measure => {\n const id = configId(space, config);\n const known = assessed.get(id);\n if (known !== undefined) return known.measure;\n const fresh = assess(id, choiceOf(config));\n assessed.set(id, fresh);\n return fresh.measure;\n };\n const size = spaceSize(space, { levels: 1 }).base;\n const grid = size <= BigInt(evaluations);\n const run =\n grid\n ? sweep(space, { objectives: PLACEMENT_OBJECTIVES, evaluate, enumerate: { mode: \"grid\", levels: 1, limit: Math.max(1, Number(size)) }, evaluator: PLACEMENT_EVALUATOR })\n : evolve(space, { objectives: PLACEMENT_OBJECTIVES, evaluate, seed: options.seed ?? PLAN_DEFAULTS.seed, evaluations, evaluator: PLACEMENT_EVALUATOR });\n const front = frontOf(run)\n .map((entry) => assessed.get(entry.row.config)?.row)\n .filter((row): row is PlacementRow => row !== undefined)\n .sort((a, b) => a.cost - b.cost || b.chance - a.chance || (a.config < b.config ? -1 : a.config > b.config ? 1 : 0));\n return { method: grid ? \"grid\" : \"evolve\", run: run.run, evaluations: run.rows.length, size: size.toString(), counts: run.counts, front, breaks: breakCounts(run.rows) };\n}\n\n/** This function counts the rows with each break, in the order of `PLAN_BREAKS`. */\nfunction breakCounts(rows: readonly EvaluationRow[]): Partial<Record<PlanBreak, number>> {\n const counts: Partial<Record<PlanBreak, number>> = {};\n for (const name of PLAN_BREAKS) {\n const count = rows.filter((row) => row.breaks.includes(name)).length;\n if (count > 0) counts[name] = count;\n }\n return counts;\n}\n","import type { Stack } from \"@mark1russell7/system-stack\";\nimport { PLACEMENT_SCHEMA, type Candidate, type PlacementLayer, type Shared } from \"./layer.ts\";\n\n// A sample placement: a function that works with one copy of two, and two hosts of two providers for each copy.\n\n/** The date of the add-ons of the sample. */\nexport const SAMPLE_TODAY = \"2026-11-18\";\n\n/** This function gives a sample candidate: one host of a provider, with its chance and its fixed price. */\nfunction sampleHost(slot: string, key: string, provider: string, works: number, cost: number): Candidate {\n return {\n key,\n cost,\n uses: [provider],\n source: `offer-${key}`,\n stack: {\n schema: \"stack/1\",\n parts: [{ id: slot, kind: \"host\", needs: [provider] }],\n layers: {\n \"risk/1\": { schema: \"risk/1\", goals: [], chances: [{ part: slot, works, source: \"An estimate\" }] },\n \"spend/1\": { schema: \"spend/1\", currency: \"USD\", kinds: [], proofDays: 30, services: [{ part: slot, billing: \"fixed\", price: cost }] },\n },\n },\n };\n}\n\n/** This function gives a sample shared fragment: one provider, with its chance. */\nfunction sampleProvider(id: string, works: number): Shared {\n return {\n id,\n stack: { schema: \"stack/1\", parts: [{ id, kind: \"provider\" }], layers: { \"risk/1\": { schema: \"risk/1\", goals: [], chances: [{ part: id, works, source: \"An estimate\" }] } } },\n };\n}\n\n/** The base stack of the sample. The function `send` works when one of the parts `copy-a` and `copy-b` works. */\nexport const SAMPLE_BASE: Stack = {\n schema: \"stack/1\",\n parts: [{ id: \"send\", kind: \"function\", name: \"A message gets to its reader\", needs: [{ any: [\"copy-a\", \"copy-b\"] }] }],\n};\n\n/**\n * The sample placement layer. Each name and each number is an example.\n * The slot `copy-a` needs a candidate, and the slot `copy-b` can stay empty. The two candidates of a slot use two different providers.\n */\nexport const SAMPLE_PLACEMENT: PlacementLayer = {\n schema: PLACEMENT_SCHEMA,\n part: \"send\",\n currency: \"USD\",\n slots: [\n { id: \"copy-a\", candidates: [sampleHost(\"copy-a\", \"small-x\", \"provider-x\", 0.99, 5), sampleHost(\"copy-a\", \"small-y\", \"provider-y\", 0.98, 6)] },\n { id: \"copy-b\", optional: true, candidates: [sampleHost(\"copy-b\", \"small-x\", \"provider-x\", 0.99, 5), sampleHost(\"copy-b\", \"small-y\", \"provider-y\", 0.98, 6)] },\n ],\n shared: [sampleProvider(\"provider-x\", 0.999), sampleProvider(\"provider-y\", 0.995)],\n};\n\n/** The base stack of the sample with the sample placement layer. */\nexport const SAMPLE_STACK: Stack = { ...SAMPLE_BASE, layers: { [PLACEMENT_SCHEMA]: SAMPLE_PLACEMENT } };\n"],"mappings":";;;;;;;;;;;;;;AAaA,MAAa,eAA+F;CAAC;CAAgB;CAAc;CAAqB;AAAgB;;AAMhL,MAAa,gBAAgB;;AAG7B,SAAgB,aAAa,MAAsB;CACjD,OAAO,QAAQ;AACjB;;AAGA,SAAgB,eAAe,IAAoB;CACjD,OAAO,UAAU;AACnB;;AAmBA,SAAS,YAAY,MAAkB,IAAY,SAAmC;CACpF,OAAO;EAAE,OAAO;EAAS;EAAM,OAAO;EAAkB;EAAI,MAAM,CAAC;EAAG;CAAQ;AAChF;;AAGA,SAAS,iBAAiB,OAAqB;CAC7C,IAAI,MAAM,WAAW,KAAA,KAAa,CAAC,OAAO,OAAO,MAAM,QAAA,aAAwB,GAAG,OAAO;CACzF,MAAM,SAAS,OAAO,YAAY,OAAO,QAAQ,MAAM,MAAM,CAAC,CAAC,QAAQ,CAAC,UAAU,SAAS,gBAAgB,CAAC;CAC5G,OAAO;EAAE,GAAG;EAAO;CAAO;AAC5B;;;;;;;;;;;;;;;;;AAkBA,SAAgB,YAAY,OAAuB,OAAc,QAAgB,SAAkD;CACjI,MAAM,WAA+B,CAAC;CACtC,MAAM,QAAQ,IAAI,IAAI,MAAM,MAAM,KAAK,SAAS,KAAK,EAAE,CAAC;CACxD,KAAK,MAAM,MAAM,OAAO,KAAK,MAAM,CAAC,CAAC,KAAK,GACxC,IAAI,CAAC,MAAM,IAAI,EAAE,GAAG,SAAS,KAAK,YAA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