@lmzhen/dsh-evolution-core 0.59.5 → 0.60.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/lib/authority-plugin.js +3 -2
- package/lib/composition/index.js +2 -2
- package/lib/{composition-B_F4TYOD.js → composition-BSqh70z_.js} +1 -1
- package/lib/{ensure-BpVDuDnA.js → ensure-DTCuxAxR.js} +1 -1
- package/lib/{extract-plan-D3ALlAmN.js → extract-plan-CP__XlEs.js} +2 -2
- package/lib/extract.js +4 -4
- package/lib/http-routes.js +3 -2
- package/lib/index.js +15 -15
- package/lib/{io-Cpa8wuvj.js → io-DT4-MXLr.js} +25 -1
- package/lib/io-node.js +1 -1
- package/lib/io-platform.js +163 -0
- package/lib/io-registry.js +10 -0
- package/lib/{mutation-authority-CZpc9F20.js → mutation-authority-C3RYyiaT.js} +66 -41
- package/lib/{mutation-storage-adapter-z1_hyrQn.js → mutation-storage-adapter-CSVn34dK.js} +334 -212
- package/lib/{mutations-Y-wKod7W.js → mutations-JKFlfhSO.js} +1 -1
- package/lib/policy.js +1 -1
- package/lib/{quality-NPGr3VzA.js → quality-C0ri2SgY.js} +2 -2
- package/lib/restrictor.js +1 -1
- package/lib/result.js +1 -1
- package/lib/{review-context-yNiZadPw.js → review-context-D2LIOEi-.js} +2 -2
- package/lib/runtime/plugin.js +2 -2
- package/lib/{session-projection-DM15fsRp.js → session-projection-Dnmm5bzt.js} +1 -1
- package/lib/{skill-history-BvLWFoOo.js → skill-history-BOZ4HCDB.js} +2 -2
- package/lib/skills.js +739 -129
- package/lib/state/domain/index.js +433 -0
- package/lib/state/index.js +136 -0
- package/lib/state/migrate.js +90 -0
- package/lib/state/storage.js +605 -0
- package/lib/state/store-plugin.js +23 -0
- package/lib/state/store.js +268 -0
- package/lib/subagent/index.js +1 -1
- package/lib/types/authority-plugin.d.ts +1 -1
- package/lib/types/authority-plugin.js +2 -1
- package/lib/types/authority.d.ts +1 -1
- package/lib/types/authority.js +75 -48
- package/lib/types/composition/ensure.js +1 -1
- package/lib/types/contract.d.ts +8 -0
- package/lib/types/http-routes.d.ts +1 -0
- package/lib/types/http-routes.js +2 -1
- package/lib/types/io-platform.d.ts +74 -0
- package/lib/types/io-platform.js +178 -0
- package/lib/types/io-registry.d.ts +4 -0
- package/lib/types/io-registry.js +10 -0
- package/lib/types/io.d.ts +7 -0
- package/lib/types/io.js +25 -1
- package/lib/types/mutation-authority.d.ts +1 -1
- package/lib/types/mutation-storage-adapter.d.ts +1 -1
- package/lib/types/opt-in.js +2 -2
- package/lib/types/params.js +2 -2
- package/lib/types/policy.js +1 -1
- package/lib/types/restrictor.js +2 -2
- package/lib/types/result.d.ts +1 -1
- package/lib/types/result.js +1 -1
- package/lib/types/runtime/coherence.js +1 -1
- package/lib/types/runtime/llm.d.ts +1 -1
- package/lib/types/runtime/llm.js +1 -1
- package/lib/types/runtime/platform.d.ts +3 -3
- package/lib/types/runtime/platform.js +2 -2
- package/lib/types/runtime/projections.d.ts +1 -1
- package/lib/types/scope.js +1 -1
- package/lib/types/session-projection.d.ts +1 -1
- package/lib/types/signals.d.ts +1 -1
- package/lib/types/skill-observation.d.ts +4 -5
- package/lib/types/skill-observation.js +18 -2
- package/lib/types/skill-store.d.ts +73 -4
- package/lib/types/skill-store.js +376 -29
- package/lib/types/skill-write-plan.d.ts +80 -0
- package/lib/types/skill-write-plan.js +2 -0
- package/lib/types/skill-write-service.d.ts +84 -2
- package/lib/types/skill-write-service.js +214 -35
- package/lib/types/skills.d.ts +2 -0
- package/lib/types/skills.js +2 -0
- package/lib/types/state/conformance.d.ts +47 -0
- package/lib/types/state/conformance.js +246 -0
- package/lib/types/state/constants.d.ts +52 -0
- package/lib/types/state/constants.js +52 -0
- package/lib/types/state/domain/compatibility.d.ts +40 -0
- package/lib/types/state/domain/compatibility.js +36 -0
- package/lib/types/state/domain/index.d.ts +81 -0
- package/lib/types/state/domain/index.js +592 -0
- package/lib/types/state/index.d.ts +63 -0
- package/lib/types/state/index.js +174 -0
- package/lib/types/state/medium-declaration.d.ts +37 -0
- package/lib/types/state/medium-declaration.js +53 -0
- package/lib/types/state/migrate.d.ts +34 -0
- package/lib/types/state/migrate.js +103 -0
- package/lib/types/state/record-contract.d.ts +56 -0
- package/lib/types/state/record-contract.js +120 -0
- package/lib/types/state/storage.d.ts +112 -0
- package/lib/types/state/storage.js +178 -0
- package/lib/types/state/store-plugin.d.ts +14 -0
- package/lib/types/state/store-plugin.js +18 -0
- package/lib/types/state/store.d.ts +39 -0
- package/lib/types/state/store.js +270 -0
- package/lib/types/store-ledger.js +2 -2
- package/lib/types/subagent/run.d.ts +1 -1
- package/lib/types/subagent/types.d.ts +4 -4
- package/lib/types/system-skill-writer.d.ts +21 -0
- package/lib/types/system-skill-writer.js +15 -0
- package/lib/types/{write-kernel.d.ts → target-writer.d.ts} +32 -15
- package/lib/types/{write-kernel.js → target-writer.js} +40 -8
- package/lib/types/tool-dispatch.js +6 -3
- package/lib/types/{mutation-unit.d.ts → unit-kernel.d.ts} +44 -13
- package/lib/types/{mutation-unit.js → unit-kernel.js} +163 -120
- package/lib/types/write.d.ts +1 -2
- package/lib/types/write.js +1 -2
- package/lib/{usage-D7yHG2Cb.js → usage-BO4Zj_zB.js} +2 -2
- package/lib/write.js +4 -4
- package/package.json +30 -3
- package/persisted-write-inventory.json +14 -5
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { a as isProcessAlive, c as transactIo, i as isCommittedWarning, t as TransactionNotInvokedError } from "./io-
|
|
2
|
-
import { l as exceedsContentLimit, r as contentHash } from "./mutations-
|
|
1
|
+
import { a as isProcessAlive, c as transactIo, i as isCommittedWarning, t as TransactionNotInvokedError } from "./io-DT4-MXLr.js";
|
|
2
|
+
import { l as exceedsContentLimit, r as contentHash } from "./mutations-JKFlfhSO.js";
|
|
3
3
|
import { createHash, randomUUID } from "node:crypto";
|
|
4
4
|
import { basename, dirname, isAbsolute, join, relative } from "node:path";
|
|
5
|
-
//#region lib/types/
|
|
5
|
+
//#region lib/types/target-writer.js
|
|
6
6
|
/**
|
|
7
7
|
* The family's single write primitive: compare the baseline INSIDE the write
|
|
8
8
|
* transaction, never overwrite a concurrent value, and compensate only this
|
|
9
9
|
* unit's own writes. Business state machines stay in their domain owners.
|
|
10
10
|
*/
|
|
11
|
-
var
|
|
11
|
+
var TargetWriter = class {
|
|
12
12
|
io;
|
|
13
13
|
transact;
|
|
14
14
|
now;
|
|
@@ -57,6 +57,40 @@ var WriteKernel = class {
|
|
|
57
57
|
* if no retry ever writes, the result is still a conflict.
|
|
58
58
|
*/
|
|
59
59
|
async writeOne(target, options = {}) {
|
|
60
|
+
if (target.expected === null && target.next !== null && this.io.createTextIfAbsent !== void 0) try {
|
|
61
|
+
await this.io.createTextIfAbsent(target.path, target.next);
|
|
62
|
+
return {
|
|
63
|
+
ok: true,
|
|
64
|
+
changed: [target.path],
|
|
65
|
+
errors: []
|
|
66
|
+
};
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (isCommittedWarning(error)) return {
|
|
69
|
+
ok: true,
|
|
70
|
+
changed: [target.path],
|
|
71
|
+
errors: [],
|
|
72
|
+
warnings: [error instanceof Error ? error.message : String(error)]
|
|
73
|
+
};
|
|
74
|
+
const code = error?.code;
|
|
75
|
+
if (code === "EEXIST" || code === "ENOTEMPTY") return {
|
|
76
|
+
ok: false,
|
|
77
|
+
changed: [],
|
|
78
|
+
errors: [{
|
|
79
|
+
code: "cas-conflict",
|
|
80
|
+
message: "target appeared before create: " + target.path,
|
|
81
|
+
target: target.path
|
|
82
|
+
}]
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
ok: false,
|
|
86
|
+
changed: [],
|
|
87
|
+
errors: [{
|
|
88
|
+
code: "write-failed",
|
|
89
|
+
message: error instanceof Error ? error.message : String(error),
|
|
90
|
+
target: target.path
|
|
91
|
+
}]
|
|
92
|
+
};
|
|
93
|
+
}
|
|
60
94
|
const guard = options.guardBaseline !== false;
|
|
61
95
|
let wrote = false;
|
|
62
96
|
let deferred = false;
|
|
@@ -138,22 +172,32 @@ var WriteKernel = class {
|
|
|
138
172
|
};
|
|
139
173
|
}
|
|
140
174
|
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* hold this commit's bytes.
|
|
175
|
+
* Read and verify every target without writing. A drift returns one
|
|
176
|
+
* cas-conflict per target and leaves the filesystem untouched.
|
|
144
177
|
*/
|
|
145
|
-
async
|
|
146
|
-
|
|
178
|
+
async prepare(targets) {
|
|
179
|
+
const errors = [];
|
|
180
|
+
for (const target of targets) if (await this.io.readText(target.path) !== target.expected) errors.push({
|
|
181
|
+
code: "cas-conflict",
|
|
182
|
+
message: "target changed after plan: " + target.path,
|
|
183
|
+
target: target.path
|
|
184
|
+
});
|
|
185
|
+
return errors.length === 0 ? {
|
|
186
|
+
ok: true,
|
|
187
|
+
prepared: { targets }
|
|
188
|
+
} : {
|
|
147
189
|
ok: false,
|
|
148
|
-
|
|
149
|
-
errors: [{
|
|
150
|
-
code: "cas-conflict",
|
|
151
|
-
message: "target changed after plan: " + target.path,
|
|
152
|
-
target: target.path
|
|
153
|
-
}]
|
|
190
|
+
errors
|
|
154
191
|
};
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Commit a unit that already passed {@link prepare}. This is the only write
|
|
195
|
+
* point; a later failure compensates only the targets this commit wrote, and
|
|
196
|
+
* only while they still hold this commit's bytes.
|
|
197
|
+
*/
|
|
198
|
+
async commitPrepared(prepared) {
|
|
155
199
|
const written = [];
|
|
156
|
-
for (const target of targets) {
|
|
200
|
+
for (const target of prepared.targets) {
|
|
157
201
|
const result = await this.writeOne(target);
|
|
158
202
|
if (!result.ok) {
|
|
159
203
|
const compensation = await this.compensate(written);
|
|
@@ -171,6 +215,16 @@ var WriteKernel = class {
|
|
|
171
215
|
errors: []
|
|
172
216
|
};
|
|
173
217
|
}
|
|
218
|
+
/** Prepare then commit; a preflight drift writes nothing. */
|
|
219
|
+
async commit(targets) {
|
|
220
|
+
const prepared = await this.prepare(targets);
|
|
221
|
+
if (!prepared.ok) return {
|
|
222
|
+
ok: false,
|
|
223
|
+
changed: [],
|
|
224
|
+
errors: prepared.errors
|
|
225
|
+
};
|
|
226
|
+
return await this.commitPrepared(prepared.prepared);
|
|
227
|
+
}
|
|
174
228
|
/**
|
|
175
229
|
* Restore a target to its before bytes only if it still holds the bytes this
|
|
176
230
|
* unit wrote. A concurrent writer wins and the compensation is reported.
|
|
@@ -207,7 +261,7 @@ var WriteKernel = class {
|
|
|
207
261
|
}
|
|
208
262
|
};
|
|
209
263
|
//#endregion
|
|
210
|
-
//#region lib/types/
|
|
264
|
+
//#region lib/types/unit-kernel.js
|
|
211
265
|
function underRoot(rootDir, target) {
|
|
212
266
|
if (rootDir === void 0 || rootDir === "") return false;
|
|
213
267
|
const rel = relative(rootDir, target);
|
|
@@ -333,13 +387,14 @@ const noLock = { async acquire() {
|
|
|
333
387
|
return { async release() {} };
|
|
334
388
|
} };
|
|
335
389
|
const identitySerial = async (task) => await task();
|
|
336
|
-
var
|
|
390
|
+
var UnitKernel = class {
|
|
337
391
|
io;
|
|
338
392
|
limits;
|
|
339
393
|
serial;
|
|
340
394
|
lock;
|
|
341
395
|
targetKeys;
|
|
342
396
|
targetKeysForSnapshot;
|
|
397
|
+
targetKeyResolver;
|
|
343
398
|
journal;
|
|
344
399
|
snapshots;
|
|
345
400
|
claim;
|
|
@@ -355,7 +410,7 @@ var MutationUnit = class {
|
|
|
355
410
|
this.io = options.io;
|
|
356
411
|
this.limits = options.limits;
|
|
357
412
|
this.serial = options.serial ?? identitySerial;
|
|
358
|
-
if (options.lock === void 0 && options.nested !== true) throw new Error("
|
|
413
|
+
if (options.lock === void 0 && options.nested !== true) throw new Error("unit-kernel: a lock (or an explicit nested:true under an outer fence) is required");
|
|
359
414
|
this.lock = options.lock ?? noLock;
|
|
360
415
|
this.targetKeys = options.targetKeys;
|
|
361
416
|
this.targetKeysForSnapshot = options.targetKeysForSnapshot;
|
|
@@ -370,12 +425,16 @@ var MutationUnit = class {
|
|
|
370
425
|
this.now = options.now ?? Date.now;
|
|
371
426
|
this.journalId = "j-" + String(this.now()) + "-" + Math.random().toString(36).slice(2);
|
|
372
427
|
const transact = options.transact;
|
|
373
|
-
this.kernel = new
|
|
428
|
+
this.kernel = new TargetWriter({
|
|
374
429
|
io: this.io,
|
|
375
430
|
...transact === void 0 ? {} : { transact },
|
|
376
431
|
now: this.now
|
|
377
432
|
});
|
|
378
433
|
}
|
|
434
|
+
async resolveTargetKeys(keys) {
|
|
435
|
+
if (keys === void 0 || this.targetKeyResolver === void 0) return keys;
|
|
436
|
+
return await this.targetKeyResolver(keys);
|
|
437
|
+
}
|
|
379
438
|
preview(request) {
|
|
380
439
|
const validation = validateMutationRequest(request, this.limits);
|
|
381
440
|
return {
|
|
@@ -386,7 +445,13 @@ var MutationUnit = class {
|
|
|
386
445
|
errors: validation.errors
|
|
387
446
|
};
|
|
388
447
|
}
|
|
389
|
-
|
|
448
|
+
/**
|
|
449
|
+
* Phase 1: acquire the lease/claim and read every target, verifying CAS and
|
|
450
|
+
* write validation. This performs NO durable write (no journal, snapshot,
|
|
451
|
+
* audit or event). The returned handle holds the lease until commit() or
|
|
452
|
+
* release(); callers must use try/finally.
|
|
453
|
+
*/
|
|
454
|
+
async prepare(request) {
|
|
390
455
|
const primary = request.name.trim();
|
|
391
456
|
const unitId = request.unitId ?? "u-" + String(this.now()) + "-" + Math.random().toString(36).slice(2);
|
|
392
457
|
const validation = validateMutationRequest(request, this.limits);
|
|
@@ -397,81 +462,72 @@ var MutationUnit = class {
|
|
|
397
462
|
changed: [],
|
|
398
463
|
errors: validation.errors
|
|
399
464
|
};
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
465
|
+
let lease;
|
|
466
|
+
try {
|
|
467
|
+
lease = await this.lock.acquire(await this.resolveTargetKeys(this.targetKeys));
|
|
468
|
+
} catch (error) {
|
|
469
|
+
return {
|
|
470
|
+
ok: false,
|
|
471
|
+
unitId,
|
|
472
|
+
dryRun: false,
|
|
473
|
+
changed: [],
|
|
474
|
+
errors: [{
|
|
475
|
+
code: error instanceof TransactionNotInvokedError ? "transact-not-invoked" : "unit-fence-failed",
|
|
476
|
+
message: error instanceof Error ? error.message : String(error)
|
|
477
|
+
}]
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
let claim;
|
|
481
|
+
const release = async () => {
|
|
482
|
+
await claim?.release().catch(() => {});
|
|
483
|
+
await lease.release();
|
|
406
484
|
};
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
}]
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
let claim;
|
|
424
|
-
if (this.claim !== void 0) try {
|
|
425
|
-
claim = await this.claim.acquire(primary, this.claimRunId ?? this.context?.runId ?? "");
|
|
426
|
-
} catch (error) {
|
|
427
|
-
await lease.release();
|
|
428
|
-
return {
|
|
429
|
-
ok: false,
|
|
430
|
-
unitId,
|
|
431
|
-
dryRun: false,
|
|
432
|
-
changed: [],
|
|
433
|
-
errors: [{
|
|
434
|
-
code: "claim-failed",
|
|
435
|
-
message: error instanceof Error ? error.message : String(error)
|
|
436
|
-
}]
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
const changed = [];
|
|
440
|
-
const warnings = [];
|
|
441
|
-
const bestEffort = async (task) => {
|
|
442
|
-
try {
|
|
443
|
-
await task();
|
|
444
|
-
} catch (error) {
|
|
445
|
-
if (!isCommittedWarning(error)) throw error;
|
|
446
|
-
warnings.push(error instanceof Error ? error.message : String(error));
|
|
447
|
-
}
|
|
485
|
+
if (this.claim !== void 0) try {
|
|
486
|
+
claim = await this.claim.acquire(primary, this.claimRunId ?? this.context?.runId ?? "");
|
|
487
|
+
} catch (error) {
|
|
488
|
+
await lease.release();
|
|
489
|
+
return {
|
|
490
|
+
ok: false,
|
|
491
|
+
unitId,
|
|
492
|
+
dryRun: false,
|
|
493
|
+
changed: [],
|
|
494
|
+
errors: [{
|
|
495
|
+
code: "claim-failed",
|
|
496
|
+
message: error instanceof Error ? error.message : String(error)
|
|
497
|
+
}]
|
|
448
498
|
};
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
499
|
+
}
|
|
500
|
+
const snapshots = [];
|
|
501
|
+
try {
|
|
502
|
+
for (const op of validation.ops) {
|
|
503
|
+
const current = await this.io.readText(op.target);
|
|
504
|
+
const baseline = op.expected === void 0 ? current : op.expected;
|
|
505
|
+
if (current !== baseline) {
|
|
506
|
+
await release();
|
|
507
|
+
return {
|
|
455
508
|
ok: false,
|
|
456
509
|
unitId,
|
|
457
510
|
dryRun: false,
|
|
458
|
-
changed,
|
|
511
|
+
changed: [],
|
|
459
512
|
errors: [{
|
|
460
513
|
code: "cas-drift",
|
|
461
514
|
message: "target changed after plan",
|
|
462
515
|
target: op.target
|
|
463
516
|
}]
|
|
464
517
|
};
|
|
465
|
-
snapshots.push({
|
|
466
|
-
target: op.target,
|
|
467
|
-
before: current,
|
|
468
|
-
after: op.kind === "write" ? op.content : null,
|
|
469
|
-
baseline
|
|
470
|
-
});
|
|
471
518
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
519
|
+
snapshots.push({
|
|
520
|
+
target: op.target,
|
|
521
|
+
before: current,
|
|
522
|
+
after: op.kind === "write" ? op.content : null,
|
|
523
|
+
baseline
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
for (const op of validation.ops) {
|
|
527
|
+
const refusal = this.validateWrite?.(op) ?? null;
|
|
528
|
+
if (refusal !== null) {
|
|
529
|
+
await release();
|
|
530
|
+
return {
|
|
475
531
|
ok: false,
|
|
476
532
|
unitId,
|
|
477
533
|
dryRun: false,
|
|
@@ -483,138 +539,202 @@ var MutationUnit = class {
|
|
|
483
539
|
}]
|
|
484
540
|
};
|
|
485
541
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
for (const op of validation.ops) {
|
|
515
|
-
const snapshot = snapshots.find((entry) => entry.target === op.target);
|
|
516
|
-
if (snapshot === void 0) throw new Error("snapshot missing for " + op.target);
|
|
517
|
-
try {
|
|
518
|
-
const opWarnings = await this.applyOp(op, snapshot.baseline);
|
|
519
|
-
warnings.push(...opWarnings);
|
|
520
|
-
changed.push(op.target);
|
|
521
|
-
} catch (error) {
|
|
522
|
-
if (error?.noWrite !== true) changed.push(op.target);
|
|
523
|
-
if (!isCommittedWarning(error)) throw error;
|
|
524
|
-
warnings.push(error instanceof Error ? error.message : String(error));
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
await bestEffort(async () => this.journal?.write({
|
|
528
|
-
unitId,
|
|
529
|
-
journalId: this.journalId,
|
|
530
|
-
...this.context?.runId === void 0 ? {} : { runId: this.context.runId },
|
|
531
|
-
phase: "unit-written",
|
|
532
|
-
changed,
|
|
533
|
-
target: primary,
|
|
534
|
-
roots: validation.roots.map((root) => root.name)
|
|
535
|
-
}));
|
|
536
|
-
if (this.snapshots !== void 0) await bestEffort(() => this.snapshots.write({
|
|
537
|
-
unitId,
|
|
538
|
-
journalId: this.journalId,
|
|
539
|
-
state: "committed",
|
|
540
|
-
ops: eventOps,
|
|
541
|
-
...cleanup,
|
|
542
|
-
createdAt: this.now(),
|
|
543
|
-
...this.contextFields()
|
|
544
|
-
}));
|
|
545
|
-
await bestEffort(async () => this.journal?.write({
|
|
546
|
-
unitId,
|
|
547
|
-
journalId: this.journalId,
|
|
548
|
-
...this.context?.runId === void 0 ? {} : { runId: this.context.runId },
|
|
549
|
-
phase: "committed",
|
|
550
|
-
changed,
|
|
551
|
-
target: primary,
|
|
552
|
-
roots: validation.roots.map((root) => root.name)
|
|
553
|
-
}));
|
|
554
|
-
try {
|
|
555
|
-
await this.hooks?.committed?.({
|
|
556
|
-
unitId,
|
|
557
|
-
changed,
|
|
558
|
-
ops: eventOps,
|
|
559
|
-
roots: validation.roots.map((root) => root.name)
|
|
560
|
-
});
|
|
561
|
-
} catch {}
|
|
562
|
-
return {
|
|
563
|
-
ok: true,
|
|
564
|
-
unitId,
|
|
565
|
-
dryRun: false,
|
|
566
|
-
changed,
|
|
567
|
-
errors: [],
|
|
568
|
-
...warnings.length === 0 ? {} : { warnings }
|
|
569
|
-
};
|
|
542
|
+
}
|
|
543
|
+
} catch (error) {
|
|
544
|
+
await release();
|
|
545
|
+
return {
|
|
546
|
+
ok: false,
|
|
547
|
+
unitId,
|
|
548
|
+
dryRun: false,
|
|
549
|
+
changed: [],
|
|
550
|
+
errors: [{
|
|
551
|
+
code: "write-failed",
|
|
552
|
+
message: error instanceof Error ? error.message : String(error)
|
|
553
|
+
}]
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
const eventOps = snapshots.map((snapshot) => {
|
|
557
|
+
const op = validation.ops.find((entry) => entry.target === snapshot.target);
|
|
558
|
+
return {
|
|
559
|
+
target: snapshot.target,
|
|
560
|
+
before: snapshot.before,
|
|
561
|
+
after: op?.kind === "write" ? op.content : null,
|
|
562
|
+
...op?.skill === void 0 ? {} : { skill: op.skill }
|
|
563
|
+
};
|
|
564
|
+
});
|
|
565
|
+
const changed = [];
|
|
566
|
+
const warnings = [];
|
|
567
|
+
const bestEffort = async (task) => {
|
|
568
|
+
try {
|
|
569
|
+
await task();
|
|
570
570
|
} catch (error) {
|
|
571
|
-
|
|
572
|
-
|
|
571
|
+
if (!isCommittedWarning(error)) throw error;
|
|
572
|
+
warnings.push(error instanceof Error ? error.message : String(error));
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
return {
|
|
576
|
+
kind: "prepared",
|
|
577
|
+
unitId,
|
|
578
|
+
request,
|
|
579
|
+
primary,
|
|
580
|
+
validation,
|
|
581
|
+
snapshots,
|
|
582
|
+
eventOps,
|
|
583
|
+
changed,
|
|
584
|
+
warnings,
|
|
585
|
+
bestEffort,
|
|
586
|
+
release
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
/** Phase 2: the only durable write phase. Releases the prepare lease in all paths. */
|
|
590
|
+
async commit(prepared) {
|
|
591
|
+
const { unitId, request, primary, validation, snapshots, eventOps, changed, warnings, bestEffort } = prepared;
|
|
592
|
+
const cleanup = request.cleanupPaths === void 0 ? {} : { cleanup: request.cleanupPaths };
|
|
593
|
+
try {
|
|
594
|
+
if (this.snapshots !== void 0) await bestEffort(() => this.snapshots.write({
|
|
595
|
+
unitId,
|
|
596
|
+
journalId: this.journalId,
|
|
597
|
+
state: "prepared",
|
|
598
|
+
ops: eventOps,
|
|
599
|
+
...cleanup,
|
|
600
|
+
createdAt: this.now(),
|
|
601
|
+
...this.contextFields()
|
|
602
|
+
}));
|
|
603
|
+
await bestEffort(async () => this.journal?.write({
|
|
604
|
+
unitId,
|
|
605
|
+
journalId: this.journalId,
|
|
606
|
+
...this.context?.runId === void 0 ? {} : { runId: this.context.runId },
|
|
607
|
+
phase: "prepared",
|
|
608
|
+
changed: [],
|
|
609
|
+
target: validation.roots.find((root) => root.name === primary)?.name ?? primary,
|
|
610
|
+
roots: validation.roots.map((root) => root.name)
|
|
611
|
+
}));
|
|
612
|
+
for (const op of validation.ops) {
|
|
613
|
+
const snapshot = snapshots.find((entry) => entry.target === op.target);
|
|
614
|
+
if (snapshot === void 0) throw new Error("snapshot missing for " + op.target);
|
|
573
615
|
try {
|
|
574
|
-
await this.
|
|
575
|
-
|
|
576
|
-
|
|
616
|
+
const opWarnings = await this.applyOp(op, snapshot.baseline);
|
|
617
|
+
warnings.push(...opWarnings);
|
|
618
|
+
changed.push(op.target);
|
|
619
|
+
} catch (error) {
|
|
620
|
+
if (error?.noWrite !== true) changed.push(op.target);
|
|
621
|
+
if (!isCommittedWarning(error)) throw error;
|
|
622
|
+
warnings.push(error instanceof Error ? error.message : String(error));
|
|
577
623
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
624
|
+
}
|
|
625
|
+
await bestEffort(async () => this.journal?.write({
|
|
626
|
+
unitId,
|
|
627
|
+
journalId: this.journalId,
|
|
628
|
+
...this.context?.runId === void 0 ? {} : { runId: this.context.runId },
|
|
629
|
+
phase: "unit-written",
|
|
630
|
+
changed,
|
|
631
|
+
target: primary,
|
|
632
|
+
roots: validation.roots.map((root) => root.name)
|
|
633
|
+
}));
|
|
634
|
+
if (this.snapshots !== void 0) await bestEffort(() => this.snapshots.write({
|
|
635
|
+
unitId,
|
|
636
|
+
journalId: this.journalId,
|
|
637
|
+
state: "committed",
|
|
638
|
+
ops: eventOps,
|
|
639
|
+
...cleanup,
|
|
640
|
+
createdAt: this.now(),
|
|
641
|
+
...this.contextFields()
|
|
642
|
+
}));
|
|
643
|
+
await bestEffort(async () => this.journal?.write({
|
|
644
|
+
unitId,
|
|
645
|
+
journalId: this.journalId,
|
|
646
|
+
...this.context?.runId === void 0 ? {} : { runId: this.context.runId },
|
|
647
|
+
phase: "committed",
|
|
648
|
+
changed,
|
|
649
|
+
target: primary,
|
|
650
|
+
roots: validation.roots.map((root) => root.name)
|
|
651
|
+
}));
|
|
652
|
+
try {
|
|
653
|
+
await this.hooks?.committed?.({
|
|
581
654
|
unitId,
|
|
582
|
-
journalId: this.journalId,
|
|
583
|
-
...this.context?.runId === void 0 ? {} : { runId: this.context.runId },
|
|
584
|
-
phase: "failed",
|
|
585
655
|
changed,
|
|
586
|
-
|
|
656
|
+
ops: eventOps,
|
|
657
|
+
roots: validation.roots.map((root) => root.name)
|
|
587
658
|
});
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
659
|
+
} catch {}
|
|
660
|
+
return {
|
|
661
|
+
ok: true,
|
|
662
|
+
unitId,
|
|
663
|
+
dryRun: false,
|
|
664
|
+
changed,
|
|
665
|
+
errors: [],
|
|
666
|
+
...warnings.length === 0 ? {} : { warnings }
|
|
667
|
+
};
|
|
668
|
+
} catch (error) {
|
|
669
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
670
|
+
let rollbackFailure = "";
|
|
671
|
+
try {
|
|
672
|
+
await this.rollback(snapshots.filter((snapshot) => changed.includes(snapshot.target)));
|
|
673
|
+
} catch (rollbackError) {
|
|
674
|
+
rollbackFailure = rollbackError instanceof Error ? rollbackError.message : String(rollbackError);
|
|
675
|
+
}
|
|
676
|
+
const detail = rollbackFailure === "" ? message : message + "; rollback failed: " + rollbackFailure;
|
|
677
|
+
const failureCode = error?.code === "transact-not-invoked" ? "transact-not-invoked" : "write-failed";
|
|
678
|
+
await this.journal?.write({
|
|
679
|
+
unitId,
|
|
680
|
+
journalId: this.journalId,
|
|
681
|
+
...this.context?.runId === void 0 ? {} : { runId: this.context.runId },
|
|
682
|
+
phase: "failed",
|
|
683
|
+
changed,
|
|
684
|
+
error: detail
|
|
685
|
+
});
|
|
686
|
+
try {
|
|
687
|
+
await this.hooks?.rolledBack?.({
|
|
606
688
|
unitId,
|
|
607
|
-
dryRun: false,
|
|
608
689
|
changed,
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
690
|
+
ops: snapshots.map((snapshot) => {
|
|
691
|
+
const op = validation.ops.find((entry) => entry.target === snapshot.target);
|
|
692
|
+
return {
|
|
693
|
+
target: snapshot.target,
|
|
694
|
+
before: snapshot.before,
|
|
695
|
+
after: op?.kind === "write" ? op.content : null,
|
|
696
|
+
...op?.skill === void 0 ? {} : { skill: op.skill }
|
|
697
|
+
};
|
|
698
|
+
}),
|
|
699
|
+
roots: validation.roots.map((root) => root.name)
|
|
700
|
+
});
|
|
701
|
+
} catch {}
|
|
702
|
+
return {
|
|
703
|
+
ok: false,
|
|
704
|
+
unitId,
|
|
705
|
+
dryRun: false,
|
|
706
|
+
changed,
|
|
707
|
+
errors: [{
|
|
708
|
+
code: failureCode,
|
|
709
|
+
message: detail
|
|
710
|
+
}]
|
|
711
|
+
};
|
|
712
|
+
} finally {
|
|
713
|
+
await prepared.release();
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
async apply(request) {
|
|
717
|
+
if (request.dryRun === true) {
|
|
718
|
+
const unitId = request.unitId ?? "u-" + String(this.now()) + "-" + Math.random().toString(36).slice(2);
|
|
719
|
+
const validation = validateMutationRequest(request, this.limits);
|
|
720
|
+
return validation.ok ? {
|
|
721
|
+
ok: true,
|
|
722
|
+
unitId,
|
|
723
|
+
dryRun: true,
|
|
724
|
+
changed: [],
|
|
725
|
+
errors: []
|
|
726
|
+
} : {
|
|
727
|
+
ok: false,
|
|
728
|
+
unitId,
|
|
729
|
+
dryRun: true,
|
|
730
|
+
changed: [],
|
|
731
|
+
errors: validation.errors
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
return await this.serial(async () => {
|
|
735
|
+
const prepared = await this.prepare(request);
|
|
736
|
+
if (prepared.kind !== "prepared") return prepared;
|
|
737
|
+
return await this.commit(prepared);
|
|
618
738
|
});
|
|
619
739
|
}
|
|
620
740
|
/** Recover every prepared snapshot known to the port; safe to call at boot. */
|
|
@@ -650,7 +770,7 @@ var MutationUnit = class {
|
|
|
650
770
|
return await this.serial(async () => {
|
|
651
771
|
let lease;
|
|
652
772
|
try {
|
|
653
|
-
lease = await this.lock.acquire(this.targetKeysForSnapshot?.(snapshot) ?? this.targetKeys);
|
|
773
|
+
lease = await this.lock.acquire(await this.resolveTargetKeys(this.targetKeysForSnapshot?.(snapshot) ?? this.targetKeys));
|
|
654
774
|
} catch (error) {
|
|
655
775
|
return {
|
|
656
776
|
ok: false,
|
|
@@ -778,7 +898,7 @@ var MutationUnit = class {
|
|
|
778
898
|
return await this.serial(async () => {
|
|
779
899
|
let lease;
|
|
780
900
|
try {
|
|
781
|
-
lease = await this.lock.acquire(this.targetKeys);
|
|
901
|
+
lease = await this.lock.acquire(await this.resolveTargetKeys(this.targetKeys));
|
|
782
902
|
} catch (error) {
|
|
783
903
|
return {
|
|
784
904
|
ok: false,
|
|
@@ -888,14 +1008,16 @@ var MutationUnit = class {
|
|
|
888
1008
|
return [...result.warnings ?? []];
|
|
889
1009
|
}
|
|
890
1010
|
async rollback(snapshots) {
|
|
1011
|
+
const errors = [];
|
|
891
1012
|
for (const snapshot of [...snapshots].reverse()) {
|
|
892
1013
|
const result = await this.kernel.restore({
|
|
893
1014
|
path: snapshot.target,
|
|
894
1015
|
before: snapshot.before,
|
|
895
1016
|
after: snapshot.after
|
|
896
1017
|
});
|
|
897
|
-
if (!result.ok)
|
|
1018
|
+
if (!result.ok) errors.push(snapshot.target + ": " + (result.errors[0]?.message ?? "rollback failed"));
|
|
898
1019
|
}
|
|
1020
|
+
if (errors.length > 0) throw new Error("rollback incomplete: " + errors.join("; "));
|
|
899
1021
|
}
|
|
900
1022
|
};
|
|
901
1023
|
function parseFileLock(raw) {
|
|
@@ -1267,4 +1389,4 @@ function unitJournalPort(io, home) {
|
|
|
1267
1389
|
} };
|
|
1268
1390
|
}
|
|
1269
1391
|
//#endregion
|
|
1270
|
-
export { writeUnitEvidence as C, fileMutationLock as D,
|
|
1392
|
+
export { writeUnitEvidence as C, fileMutationLock as D, UnitKernel as E, validateMutationRequest as O, upsertUnitIndex as S, writeUnitSnapshot as T, unitIndexPath as _, UNIT_JOURNAL_SCHEMA_VERSION as a, unitSnapshotsDir as b, unitJournalPath as c, UNIT_STORAGE_SCHEMA_VERSION as d, readUnitEvidence as f, unitEvidencePath as g, readUnitSnapshot as h, UNIT_JOURNAL_PHASES as i, unitJournalPhaseRank as l, readUnitReceipt as m, unitJournalPort as n, assertUnitJournalTransition as o, readUnitIndex as p, unitSnapshotPort as r, readUnitJournal as s, pruneTreeChangeUnits as t, writeUnitJournal as u, unitReceiptPath as v, writeUnitReceipt as w, unitStorageKey as x, unitSnapshotPath as y };
|