@yaks/secrets 0.0.0-stage → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/LICENSE +202 -0
  2. package/_dist/graph.d.ts +9 -0
  3. package/_dist/graph.d.ts.map +1 -0
  4. package/_dist/mod.d.ts +67 -0
  5. package/_dist/mod.d.ts.map +1 -0
  6. package/_dist/op.d.ts +37 -0
  7. package/_dist/op.d.ts.map +1 -0
  8. package/_dist/plugin.d.ts +19 -0
  9. package/_dist/plugin.d.ts.map +1 -0
  10. package/_dist/records.d.ts +11 -0
  11. package/_dist/records.d.ts.map +1 -0
  12. package/_dist/ref.d.ts +4 -0
  13. package/_dist/ref.d.ts.map +1 -0
  14. package/_dist/reveal.d.ts +14 -0
  15. package/_dist/reveal.d.ts.map +1 -0
  16. package/_dist/sealed.d.ts +10 -0
  17. package/_dist/sealed.d.ts.map +1 -0
  18. package/_dist/sentinel.d.ts +9 -0
  19. package/_dist/sentinel.d.ts.map +1 -0
  20. package/_dist/signed.d.ts +16 -0
  21. package/_dist/signed.d.ts.map +1 -0
  22. package/_dist/vault.d.ts +49 -0
  23. package/_dist/vault.d.ts.map +1 -0
  24. package/_dist/views.d.ts +3 -0
  25. package/_dist/views.d.ts.map +1 -0
  26. package/_dist/vocab.d.ts +6 -0
  27. package/_dist/vocab.d.ts.map +1 -0
  28. package/deno.json +1 -0
  29. package/graph.js +12 -0
  30. package/graph.js.map +1 -0
  31. package/graph.ts +21 -0
  32. package/mod.js +67 -0
  33. package/mod.js.map +1 -0
  34. package/mod.ts +83 -0
  35. package/op.js +170 -0
  36. package/op.js.map +1 -0
  37. package/op.ts +188 -0
  38. package/package.json +51 -4
  39. package/plugin.js +429 -0
  40. package/plugin.js.map +1 -0
  41. package/plugin.ts +464 -0
  42. package/reads.js +51 -0
  43. package/reads.js.map +1 -0
  44. package/reads.ts +56 -0
  45. package/records.js +45 -0
  46. package/records.js.map +1 -0
  47. package/records.ts +58 -0
  48. package/ref.js +7 -0
  49. package/ref.js.map +1 -0
  50. package/ref.ts +11 -0
  51. package/reveal.js +50 -0
  52. package/reveal.js.map +1 -0
  53. package/reveal.ts +98 -0
  54. package/sealed.js +35 -0
  55. package/sealed.js.map +1 -0
  56. package/sealed.ts +31 -0
  57. package/sentinel.js +66 -0
  58. package/sentinel.js.map +1 -0
  59. package/sentinel.ts +97 -0
  60. package/signed.js +107 -0
  61. package/signed.js.map +1 -0
  62. package/signed.ts +151 -0
  63. package/vault.js +67 -0
  64. package/vault.js.map +1 -0
  65. package/vault.ts +130 -0
  66. package/views.js +27 -0
  67. package/views.js.map +1 -0
  68. package/views.ts +34 -0
  69. package/vocab.js +16 -0
  70. package/vocab.js.map +1 -0
  71. package/vocab.json +36 -0
  72. package/vocab.ts +17 -0
  73. package/README.md +0 -3
package/plugin.js ADDED
@@ -0,0 +1,429 @@
1
+ // The graph plugin that finishes a secret's write, in two halves: a secret's
2
+ // value comes out of a write in its first phase, and goes into the vault once
3
+ // the write has committed, so nothing the graph keeps — storage, the journal,
4
+ // a broadcast, a backup — ever holds more than the handle.
5
+ //
6
+ // `normalize` is the first thing `apply()` does. It takes each written value
7
+ // and puts the secret's handle where the value was (./sentinel.ts): the handle
8
+ // the vault already keeps for that secret, so a new value is a rotation behind
9
+ // the same handle, or a new one for a secret written the first time. The value
10
+ // itself waits in this process's memory, keyed by the handle, and never on the
11
+ // bundle: a `$` key would ride the bundle through every phase, and the point is
12
+ // that no phase but this one sees it. The same hook reaches into a tool call's
13
+ // arguments, because `graph_apply` records what it was asked in the `call` row
14
+ // before it applies it — without this the value would land in the graph as the
15
+ // arguments of a call. A handle minted there is remembered for its secret, so the
16
+ // write the call goes on to make gets the same one.
17
+ //
18
+ // The bundle that writes a value, or carries a handle a value is waiting
19
+ // behind, is marked `provisional` (@yaks/effects) with a note saying the key is
20
+ // being saved. The batch commits with the handle and the mark.
21
+ //
22
+ // The other half is the same plugin's `effect` hook (`sealing` below). After
23
+ // the batch commits it seals each waiting value into the vault under the
24
+ // secret's entity id and removes the mark; a deleted secret is moved to the vault’s trash the same way. It is not an effect another process could run: the value
25
+ // is in this process's memory and nowhere else, which is the point, so the
26
+ // process that wrote it seals it. `apply()` waits for its effect hooks, so
27
+ // whoever wrote the value gets their answer once it is sealed and never sees
28
+ // the mark; only another reader in between does. A seal that fails says so on
29
+ // the secret: `provisional{note}` while a seal is being retried,
30
+ // `failed{reason}` when sealing was given up on.
31
+ //
32
+ // A secret a batch deletes, itself or by a cascade, or takes the component
33
+ // from, moves to the vault’s trash after the commit too, and nothing else is:
34
+ // which of those entities hold a secret is read inside the transaction while
35
+ // their rows still stand (`noting`), since after it a deleted entity is only a
36
+ // tombstone. Every other delete (an effect row the pool finished, a page, a
37
+ // person) asks the vault nothing.
38
+ //
39
+ // Nothing touches the vault inside the transaction. A vault may answer later
40
+ // (D1 on yaks.app) and a transaction may not wait for it (a Durable Object's
41
+ // commits the moment its body returns). A value a refused batch took, or one a
42
+ // call carried that never ran, is let go after a while rather than kept for the
43
+ // life of the process.
44
+ import { PROVISIONAL } from "@yaks/effects";
45
+ import { after, each, isPromise, sleep, system } from "@yaks/fp";
46
+ import { dead, TOMBSTONE } from "@yaks/graph/bundle";
47
+ import { hook } from "@yaks/graph/plugin";
48
+ import { doomed } from "@yaks/graph/read";
49
+ import { isOpRef } from "./ref.js";
50
+ import { secretEids } from "./sealed.js";
51
+ import { found as keptAt } from "./reveal.js";
52
+ import { handle, isHandle } from "./sentinel.js";
53
+ import { concealedBundles, concealedRows } from "./reads.js";
54
+ export let SECRET = 'secret';
55
+ let HOLD_MS = 10 * 60_000;
56
+ // What a reader is told while the value is on its way to the vault.
57
+ let SAVING = 'saving the key';
58
+ let SET = '[set]';
59
+ let obj = (v)=>v && typeof v == 'object' && !Array.isArray(v) ? v : undefined;
60
+ // A written value: a `secret` patch carrying a string that is not already a
61
+ // handle.
62
+ let plain = (v)=>{
63
+ let value = obj(v)?.value;
64
+ return typeof value == 'string' && value != SET && !isHandle(value) ? value : undefined;
65
+ };
66
+ // Which secret a bundle writes: the entity it names, or, for an alias not yet
67
+ // minted, the one its name derives. A bundle with neither gets a handle of its
68
+ // own.
69
+ let whose = (o)=>{
70
+ let eid = obj(o.entity)?.eid;
71
+ let name = obj(o[SECRET])?.name;
72
+ let named = typeof name == 'string' ? secretEids(name) : [];
73
+ if (typeof eid != 'string' || eid.startsWith('$')) return named;
74
+ return named.includes(eid) ? [
75
+ eid,
76
+ ...named.filter((x)=>x != eid)
77
+ ] : [
78
+ eid
79
+ ];
80
+ };
81
+ // Every written secret inside a JSON value — a tool call's arguments carry
82
+ // whole bundles — replaced by whatever `to` answers for it, always in the same
83
+ // order, so one pass can find them and a second can swap them.
84
+ let walk = (v, to)=>{
85
+ if (Array.isArray(v)) return v.map((x)=>walk(x, to));
86
+ let o = obj(v);
87
+ if (!o) return v;
88
+ let value = plain(o[SECRET]);
89
+ let now = value == null ? undefined : to(value, whose(o));
90
+ let out = Object.fromEntries(Object.entries(o).map(([k, x])=>[
91
+ k,
92
+ k == SECRET ? x : walk(x, to)
93
+ ]));
94
+ if (now != null) out[SECRET] = {
95
+ ...obj(o[SECRET]),
96
+ value: now
97
+ };
98
+ return out;
99
+ };
100
+ let args = (b)=>obj(b.call)?.args;
101
+ // The secrets inside a call's arguments, in the order `walk` visits them.
102
+ let inside = (called)=>{
103
+ let out = [];
104
+ if (called != null) {
105
+ walk(called, (value, eids)=>void out.push([
106
+ value,
107
+ eids
108
+ ]));
109
+ }
110
+ return out;
111
+ };
112
+ let writes = (b)=>plain(b[SECRET]) != null || inside(args(b)).length > 0;
113
+ /** Whether a batch carries a secret's value — in a `secret`, or inside a
114
+ * call's arguments — which only the vault may keep. A host that writes a
115
+ * batch down before applying it (a write log) keeps none that does. */ export let carries = (bundles)=>Array.isArray(bundles) && bundles.some((b)=>!!obj(b) && writes(b));
116
+ let holdings = new WeakMap();
117
+ let holding = (vault)=>{
118
+ let h = holdings.get(vault);
119
+ if (!h) holdings.set(vault, h = {
120
+ waiting: new Map(),
121
+ minted: new Map()
122
+ });
123
+ return h;
124
+ };
125
+ let eidOf = (b)=>b.entity.eid;
126
+ // The entities a batch deletes, and those it takes only the secret from.
127
+ let leaving = (bundles)=>({
128
+ killed: [
129
+ ...new Set(bundles.filter(dead).map(eidOf))
130
+ ],
131
+ cleared: bundles.filter((b)=>b[SECRET] === null).map(eidOf)
132
+ });
133
+ // Note which of the entities a batch removes a secret from hold one now:
134
+ // those it deletes, everything a cascade deletes with them, and those it
135
+ // clears the component on.
136
+ let noting = (vocab, going)=>hook([
137
+ SECRET,
138
+ '$delete',
139
+ TOMBSTONE
140
+ ], (bundles, tx, _err, context)=>{
141
+ let at = context?.transaction;
142
+ let { killed, cleared } = leaving(bundles);
143
+ if (!at || !killed.length && !cleared.length) return bundles;
144
+ return after(killed.length ? doomed(tx, vocab, killed) : [], (gone)=>after(tx.get([
145
+ ...new Set([
146
+ ...gone,
147
+ ...cleared
148
+ ])
149
+ ], [
150
+ SECRET
151
+ ]), (found)=>{
152
+ let held = found.filter((b)=>b[SECRET] != null);
153
+ if (held.length) going.set(at, new Set(held.map(eidOf)));
154
+ return bundles;
155
+ }));
156
+ });
157
+ /** The plugin, over the vault its secrets are kept in, the door what it says
158
+ * about a seal is written through — the graph's own `apply()`, trusted, since
159
+ * `provisional` coming off is the host's word, never a client's — and the
160
+ * graph's vocabulary, which says what a delete takes with it. The clock says
161
+ * when a value held too long is let go, and waits out a vault's retry
162
+ * (default the runtime's). */ export let secrets = (vault, write, vocab, clock = system)=>{
163
+ let { waiting, minted } = holding(vault);
164
+ let going = new WeakMap();
165
+ // The handle for a secret being written: the one the vault keeps for it, the
166
+ // one this process gave it moments ago, or a new one. The vault is asked
167
+ // before anything commits, so a read it calls retryable is tried again
168
+ // rather than refusing the write that carries the value.
169
+ let handleOf = (eids)=>!eids.length ? handle() : after(tried(()=>keptAt(vault, eids), clock), (kept)=>{
170
+ let h = kept?.handle ?? eids.map((eid)=>minted.get(eid)?.handle).find(Boolean) ?? handle();
171
+ if (!kept) {
172
+ minted.set(eids[0], {
173
+ handle: h,
174
+ until: clock.now() + HOLD_MS
175
+ });
176
+ }
177
+ return h;
178
+ });
179
+ let hold = async (value, eids)=>{
180
+ let h = await handleOf(eids);
181
+ waiting.set(h, {
182
+ value,
183
+ eids,
184
+ until: clock.now() + HOLD_MS
185
+ });
186
+ return h;
187
+ };
188
+ // A handle a value is waiting behind, written for the secret it was held
189
+ // for: the write a call goes on to make.
190
+ let behind = (b)=>{
191
+ let h = obj(b[SECRET])?.value;
192
+ let held = typeof h == 'string' ? waiting.get(h) : undefined;
193
+ return !!held && (!held.eids.length || whose(b).some((eid)=>held.eids.includes(eid)));
194
+ };
195
+ let hide = (b)=>{
196
+ let own = plain(b[SECRET]);
197
+ if (obj(b[SECRET])?.value == SET) {
198
+ return after(handleOf(whose(b)), (value)=>hide({
199
+ ...b,
200
+ [SECRET]: {
201
+ ...obj(b[SECRET]),
202
+ value
203
+ }
204
+ }));
205
+ }
206
+ let called = args(b);
207
+ let found = inside(called);
208
+ let mark = own != null || behind(b);
209
+ if (!mark && !found.length) return b;
210
+ let asked = own == null ? found : [
211
+ [
212
+ own,
213
+ whose(b)
214
+ ],
215
+ ...found
216
+ ];
217
+ return Promise.all(asked.map(([value, eid])=>hold(value, eid))).then((hs)=>{
218
+ let i = own == null ? 0 : 1;
219
+ return {
220
+ ...b,
221
+ ...own == null ? {} : {
222
+ [SECRET]: {
223
+ ...obj(b[SECRET]),
224
+ value: hs[0]
225
+ }
226
+ },
227
+ ...!found.length ? {} : {
228
+ call: {
229
+ ...obj(b.call),
230
+ args: walk(called, ()=>hs[i++])
231
+ }
232
+ },
233
+ // The graph keeps the handle and the vault the value, so a new
234
+ // value behind a handle the graph already holds is still a write
235
+ // (`$touch`, @yaks/graph unchanged.ts): the seal reads it.
236
+ ...mark ? {
237
+ [PROVISIONAL]: {
238
+ note: SAVING
239
+ },
240
+ $touch: true
241
+ } : {}
242
+ };
243
+ });
244
+ };
245
+ return {
246
+ name: '@yaks/secrets',
247
+ answerRows: (_ctx, query, rows)=>concealedRows(vocab, query, rows),
248
+ answer: (_ctx, bundles)=>concealedBundles(bundles),
249
+ // What `noting` reads: whether the entities a batch removes a secret from
250
+ // hold one, and what points at those it deletes, read with the batch.
251
+ wants: (bundles)=>{
252
+ let { killed, cleared } = leaving(bundles);
253
+ return [
254
+ ...killed.length || cleared.length ? [
255
+ {
256
+ eids: [
257
+ ...killed,
258
+ ...cleared
259
+ ],
260
+ select: [
261
+ SECRET
262
+ ]
263
+ }
264
+ ] : [],
265
+ ...killed.length ? [
266
+ {
267
+ about: killed,
268
+ comps: [
269
+ ...new Set(vocab.deaths('cascade').map(([c])=>c))
270
+ ]
271
+ }
272
+ ] : []
273
+ ];
274
+ },
275
+ hooks: {
276
+ precondition: noting(vocab, going),
277
+ effect: sealing(vault, write, clock, going),
278
+ normalize: (bundles)=>{
279
+ let now = clock.now();
280
+ for (let [h, w] of waiting)if (w.until < now) waiting.delete(h);
281
+ for (let [e, m] of minted)if (m.until < now) minted.delete(e);
282
+ if (!bundles.some((b)=>writes(b) || behind(b) || obj(b[SECRET])?.value == SET)) return bundles;
283
+ return each(bundles, [], (out, b)=>after(hide(b), (one)=>[
284
+ ...out,
285
+ one
286
+ ]));
287
+ }
288
+ }
289
+ };
290
+ };
291
+ // Run `step`, then `ok` or `no`, staying synchronous for a vault that is.
292
+ let settle = (step, ok, no)=>{
293
+ try {
294
+ let r = step();
295
+ return isPromise(r) ? r.then(ok, no) : ok(r);
296
+ } catch (e) {
297
+ return no(e);
298
+ }
299
+ };
300
+ /** A failure the vault says trying again may cure: an error carrying
301
+ * `retryable: true`, the flag the Workers runtime sets on its own (@yaks/d1's
302
+ * vault sets it for the D1 errors Cloudflare documents as transient). */ export let retryable = (e)=>!!e && typeof e == 'object' && e.retryable === true;
303
+ // How often a seal or a drop is tried, and how long it waits between tries:
304
+ // exponential backoff with jitter, as Cloudflare advises for D1 writes
305
+ // (https://developers.cloudflare.com/d1/best-practices/retry-queries/).
306
+ let TRIES = 5;
307
+ let pause = (n, clock)=>sleep(Math.min(2_000, 50 * 2 ** n) * (0.5 + Math.random() / 2), undefined, clock);
308
+ let told = (e)=>e instanceof Error ? e.message : String(e);
309
+ // What `step` answers once it succeeds, tried again after a pause while it
310
+ // fails in a way the vault calls retryable, up to `TRIES` times in all;
311
+ // `again` hears each failure that will be tried again. The last failure is
312
+ // thrown.
313
+ let tried = (step, clock, again = ()=>{})=>{
314
+ let attempt = (n)=>settle(step, (done)=>done, (err)=>{
315
+ if (!retryable(err) || n >= TRIES) throw err;
316
+ return after(again(err), ()=>pause(n, clock).then(()=>attempt(n + 1)));
317
+ });
318
+ return attempt(1);
319
+ };
320
+ /**
321
+ * The half that finishes a secret's write, over the vault the plugin holds its
322
+ * values for: after the commit, each waiting value sealed under the secret's
323
+ * entity id and the mark removed. A secret that goes — its entity, or its
324
+ * component — is dropped from the vault, tried again the same way while the
325
+ * vault calls the failure retryable; a drop that never lands is reported.
326
+ *
327
+ * A seal can fail two ways. One the vault calls {@link retryable} is expected:
328
+ * the provisional mark says it is trying again, and the seal is tried
329
+ * again here, while the value is still in memory and the writer is still
330
+ * waiting; a later success removes the mark and its message. Anything else —
331
+ * or a retryable failure that outlasts every try, so one failure can be both —
332
+ * ends the operation: the provisional mark comes off, `failed{reason}` records
333
+ * why sealing was given up on, and the original failure is rethrown for the
334
+ * graph to report. Either way the value is gone, and the person gives it again.
335
+ */ let sealing = (vault, write, clock, going)=>{
336
+ let { waiting, minted } = holding(vault);
337
+ let seal = (eid, comp, tx)=>{
338
+ let h = comp.value;
339
+ let held = typeof h == 'string' ? waiting.get(h) : undefined;
340
+ if (!held) {
341
+ if (typeof h != 'string' || !isHandle(h)) return;
342
+ return after(vault.read(eid), (active)=>{
343
+ if (active?.handle == h) return;
344
+ return after(vault.trash(), (trash)=>{
345
+ let found = trash.find((v)=>v.eid == eid && v.handle == h);
346
+ return found ? tried(()=>vault.restore(found.id), clock) : undefined;
347
+ });
348
+ });
349
+ }
350
+ if (held.eids.length && !held.eids.includes(eid)) return;
351
+ waiting.delete(h);
352
+ for (let address of held.eids)minted.delete(address);
353
+ let kept = isOpRef(held.value) ? {
354
+ op: held.value
355
+ } : {
356
+ value: held.value
357
+ };
358
+ let name = comp.name;
359
+ let put = (named)=>vault.seal(eid, {
360
+ ...named ? {
361
+ name: named
362
+ } : {},
363
+ handle: h,
364
+ ...kept
365
+ });
366
+ let mark = (said)=>write([
367
+ {
368
+ ...said,
369
+ entity: {
370
+ eid
371
+ }
372
+ }
373
+ ]);
374
+ // Sealed: the mark comes off, and with it any failure an earlier try, or
375
+ // an earlier write, left on this secret.
376
+ let sealed = ()=>after(tx.get([
377
+ eid
378
+ ]), ([b])=>mark({
379
+ entity: {
380
+ eid
381
+ },
382
+ [PROVISIONAL]: null,
383
+ ...b?.failed ? {
384
+ failed: null
385
+ } : {},
386
+ ...b?.content ? {
387
+ content: null
388
+ } : {}
389
+ }));
390
+ return settle(()=>tried(()=>typeof name == 'string' ? put(name) : after(vault.read(eid), (was)=>put(was?.name)), clock, (err)=>mark({
391
+ entity: {
392
+ eid
393
+ },
394
+ [PROVISIONAL]: {
395
+ note: 'Saving the key, trying again'
396
+ },
397
+ content: {
398
+ body: `saving the key, trying again: ${told(err)}`
399
+ }
400
+ })), sealed, (err)=>after(mark({
401
+ entity: {
402
+ eid
403
+ },
404
+ [PROVISIONAL]: null,
405
+ failed: {
406
+ reason: `the key could not be saved: ${told(err)}`
407
+ },
408
+ content: null
409
+ }), ()=>{
410
+ throw err;
411
+ }));
412
+ };
413
+ let drop = (eid)=>{
414
+ minted.delete(eid);
415
+ return tried(()=>vault.drop(eid), clock);
416
+ };
417
+ return (bundles, tx, _err, context)=>{
418
+ let held = context?.transaction && going.get(context.transaction);
419
+ return after(each(bundles, null, (_, b)=>{
420
+ let eid = b.entity.eid;
421
+ if (dead(b) || b[SECRET] === null) {
422
+ return held && held.has(eid) ? drop(eid) : null;
423
+ }
424
+ let comp = obj(b[SECRET]);
425
+ return comp ? seal(eid, comp, tx) : null;
426
+ }), ()=>bundles);
427
+ };
428
+ };
429
+ //# sourceMappingURL=plugin.js.map
package/plugin.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["./plugin.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,8EAA8E;AAC9E,8EAA8E;AAC9E,2DAA2D;AAC3D,EAAE;AACF,6EAA6E;AAC7E,+EAA+E;AAC/E,+EAA+E;AAC/E,+EAA+E;AAC/E,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,+EAA+E;AAC/E,+EAA+E;AAC/E,kFAAkF;AAClF,oDAAoD;AACpD,EAAE;AACF,yEAAyE;AACzE,gFAAgF;AAChF,+DAA+D;AAC/D,EAAE;AACF,6EAA6E;AAC7E,yEAAyE;AACzE,iKAAiK;AACjK,2EAA2E;AAC3E,2EAA2E;AAC3E,6EAA6E;AAC7E,8EAA8E;AAC9E,iEAAiE;AACjE,iDAAiD;AACjD,EAAE;AACF,2EAA2E;AAC3E,8EAA8E;AAC9E,6EAA6E;AAC7E,+EAA+E;AAC/E,4EAA4E;AAC5E,kCAAkC;AAClC,EAAE;AACF,6EAA6E;AAC7E,6EAA6E;AAC7E,+EAA+E;AAC/E,gFAAgF;AAChF,uBAAuB;AAEvB,SAAS,WAAW,6BAA8C;AAElE,SAAS,KAAK,EAAc,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,wBAA6B;AACvF,SAAS,IAAI,EAAE,SAAS,kCAAuC;AAC/D,SAAS,IAAI,kCAAyD;AACtE,SAAS,MAAM,gCAAqC;AAEpD,SAAS,OAAO,mBAAkB;AAClC,SAAS,UAAU,sBAAqB;AACxC,SAAS,SAAS,MAAM,sBAAqB;AAC7C,SAAS,MAAM,EAAE,QAAQ,wBAAuB;AAEhD,SAAS,gBAAgB,EAAE,aAAa,qBAAoB;AAE5D,O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