@lmzhen/dsh-evolution-core 0.39.2 → 0.40.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/lib/index.js +3544 -3112
- package/lib/platform/index.js +2 -2
- package/lib/{platform-Be-_wSVz.js → platform-CFp373_0.js} +3 -1
- package/lib/runtime/plugin.js +1 -1
- package/lib/types/events.d.ts +1 -1
- package/lib/types/evolution-events.d.ts +3 -1
- package/lib/types/evolution-events.js +65 -59
- package/lib/types/home-fence.d.ts +24 -0
- package/lib/types/home-fence.js +62 -0
- package/lib/types/home-owner.d.ts +4 -0
- package/lib/types/home-owner.js +37 -0
- package/lib/types/index.d.ts +4 -0
- package/lib/types/index.js +4 -0
- package/lib/types/lease-store.d.ts +55 -0
- package/lib/types/lease-store.js +116 -0
- package/lib/types/memory-store.d.ts +6 -0
- package/lib/types/memory-store.js +16 -8
- package/lib/types/mutation-authority.d.ts +59 -0
- package/lib/types/mutation-authority.js +126 -0
- package/lib/types/mutation-storage-adapter.js +7 -3
- package/lib/types/mutation-unit.d.ts +13 -0
- package/lib/types/mutation-unit.js +7 -2
- package/lib/types/mutations.d.ts +3 -1
- package/lib/types/mutations.js +40 -34
- package/lib/types/platform/index.d.ts +5 -2
- package/lib/types/platform/index.js +2 -0
- package/lib/types/platform-services.js +1 -0
- package/lib/types/proposal-report.d.ts +1 -0
- package/lib/types/proposal-report.js +12 -0
- package/lib/types/run-registry.d.ts +4 -0
- package/lib/types/run-registry.js +10 -3
- package/lib/types/skill-history.d.ts +4 -1
- package/lib/types/skill-history.js +24 -29
- package/lib/types/skill-store.d.ts +11 -3
- package/lib/types/skill-store.js +26 -12
- package/lib/types/unit-storage.d.ts +2 -0
- package/lib/types/unit-transaction.d.ts +6 -1
- package/lib/types/unit-transaction.js +20 -6
- package/lib/types/usage.d.ts +4 -0
- package/lib/types/usage.js +84 -78
- package/package.json +2 -2
- package/persisted-write-inventory.json +9 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { MutationOwner } from '@lmzhen/dsh-evolution-contract';
|
|
2
|
+
import { HomeFence } from './home-fence.ts';
|
|
3
|
+
import { LeaseStore, type LeaseToken } from './lease-store.ts';
|
|
4
|
+
import type { EvolutionIoLike } from './io.ts';
|
|
5
|
+
import { type MutationLockPort, type MutationLimits, type MutationResult, type MutationSnapshotPort, type MutationUnitOptions, type MutationRequest } from './mutation-unit.ts';
|
|
6
|
+
export interface MutationAuthorityContext {
|
|
7
|
+
readonly owner: MutationOwner;
|
|
8
|
+
readonly runId: string;
|
|
9
|
+
readonly lease: LeaseToken;
|
|
10
|
+
readonly signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
export type MutationAuthorityApplyOptions = Omit<MutationUnitOptions, 'io' | 'lock' | 'serial' | 'context'>;
|
|
13
|
+
export interface RecoveryReport {
|
|
14
|
+
readonly recovered: readonly MutationResult[];
|
|
15
|
+
readonly blocked: readonly {
|
|
16
|
+
readonly unitId: string;
|
|
17
|
+
readonly runId?: string;
|
|
18
|
+
}[];
|
|
19
|
+
}
|
|
20
|
+
export declare class MutationAuthorityError extends Error {
|
|
21
|
+
readonly code: 'lease-missing' | 'lease-mismatch' | 'fence-busy' | 'fence-io' | 'transact-failed';
|
|
22
|
+
constructor(code: MutationAuthorityError['code'], message: string);
|
|
23
|
+
}
|
|
24
|
+
export interface MutationAuthorityOptions {
|
|
25
|
+
readonly io: EvolutionIoLike;
|
|
26
|
+
readonly home: string;
|
|
27
|
+
readonly leases?: LeaseStore;
|
|
28
|
+
readonly fence?: HomeFence;
|
|
29
|
+
readonly waitMs?: number;
|
|
30
|
+
readonly now?: () => number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Plugin-side write authority: one home fence + one active lease before any
|
|
34
|
+
* multi-file unit or single-file transaction. Domain code never takes a second
|
|
35
|
+
* lock; MutationUnit remains the only multi-file transaction engine.
|
|
36
|
+
*/
|
|
37
|
+
export declare function mutationLockFrom(ctx: {
|
|
38
|
+
get?(name: string): unknown;
|
|
39
|
+
} | undefined): MutationLockPort | undefined;
|
|
40
|
+
export declare class MutationAuthority {
|
|
41
|
+
private readonly io;
|
|
42
|
+
private readonly leases;
|
|
43
|
+
private readonly fence;
|
|
44
|
+
/** Lease primitives for domain run/claim owners. */
|
|
45
|
+
get lease(): LeaseStore;
|
|
46
|
+
/** The one home fence domain unit engines must reuse instead of taking tree-lock themselves. */
|
|
47
|
+
get homeFence(): HomeFence;
|
|
48
|
+
/** Adapter for legacy MutationUnit call sites that accept a lock port. */
|
|
49
|
+
get mutationLock(): MutationLockPort;
|
|
50
|
+
constructor(options: MutationAuthorityOptions);
|
|
51
|
+
private assertLease;
|
|
52
|
+
apply(request: MutationRequest, context: MutationAuthorityContext, options: MutationAuthorityApplyOptions): Promise<MutationResult>;
|
|
53
|
+
transact(path: string, task: (current: string | null) => Promise<string | null> | string | null, context: MutationAuthorityContext): Promise<void>;
|
|
54
|
+
recover(options: {
|
|
55
|
+
readonly snapshots: MutationSnapshotPort;
|
|
56
|
+
readonly limits: MutationLimits;
|
|
57
|
+
}): Promise<RecoveryReport>;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=mutation-authority.d.ts.map
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { HomeFence, HomeFenceError } from "./home-fence.js";
|
|
2
|
+
import { LeaseStore } from "./lease-store.js";
|
|
3
|
+
import { MutationUnit, } from "./mutation-unit.js";
|
|
4
|
+
export class MutationAuthorityError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'MutationAuthorityError';
|
|
9
|
+
this.code = code;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const passiveLock = {
|
|
13
|
+
async acquire() {
|
|
14
|
+
return { async release() { } };
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
const identitySerial = async (task) => await task();
|
|
18
|
+
/**
|
|
19
|
+
* Plugin-side write authority: one home fence + one active lease before any
|
|
20
|
+
* multi-file unit or single-file transaction. Domain code never takes a second
|
|
21
|
+
* lock; MutationUnit remains the only multi-file transaction engine.
|
|
22
|
+
*/
|
|
23
|
+
export function mutationLockFrom(ctx) {
|
|
24
|
+
if (ctx?.get === undefined)
|
|
25
|
+
return undefined;
|
|
26
|
+
const authority = ctx.get('evolutionMutationAuthority');
|
|
27
|
+
return authority?.mutationLock;
|
|
28
|
+
}
|
|
29
|
+
export class MutationAuthority {
|
|
30
|
+
io;
|
|
31
|
+
leases;
|
|
32
|
+
fence;
|
|
33
|
+
/** Lease primitives for domain run/claim owners. */
|
|
34
|
+
get lease() { return this.leases; }
|
|
35
|
+
/** The one home fence domain unit engines must reuse instead of taking tree-lock themselves. */
|
|
36
|
+
get homeFence() { return this.fence; }
|
|
37
|
+
/** Adapter for legacy MutationUnit call sites that accept a lock port. */
|
|
38
|
+
get mutationLock() {
|
|
39
|
+
return {
|
|
40
|
+
acquire: async () => {
|
|
41
|
+
const lease = await this.fence.acquire();
|
|
42
|
+
return { release: async () => { await lease.release(); } };
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
constructor(options) {
|
|
47
|
+
this.io = options.io;
|
|
48
|
+
this.leases = options.leases ?? new LeaseStore({ io: options.io, home: options.home, ...(options.now === undefined ? {} : { now: options.now }) });
|
|
49
|
+
this.fence = options.fence ?? new HomeFence({ io: options.io, home: options.home, ...(options.waitMs === undefined ? {} : { waitMs: options.waitMs }), ...(options.now === undefined ? {} : { now: options.now }) });
|
|
50
|
+
}
|
|
51
|
+
async assertLease(context) {
|
|
52
|
+
const current = await this.leases.active(context.runId);
|
|
53
|
+
if (current === null)
|
|
54
|
+
throw new MutationAuthorityError('lease-missing', 'mutation lease is not active for ' + context.runId);
|
|
55
|
+
if (current.id !== context.lease.id || current.epoch !== context.lease.epoch || current.owner !== context.owner) {
|
|
56
|
+
throw new MutationAuthorityError('lease-mismatch', 'mutation lease does not own ' + context.runId);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async apply(request, context, options) {
|
|
60
|
+
if (context.signal?.aborted === true)
|
|
61
|
+
throw new MutationAuthorityError('transact-failed', 'mutation aborted before apply');
|
|
62
|
+
await this.assertLease(context);
|
|
63
|
+
try {
|
|
64
|
+
return await this.fence.run(async () => {
|
|
65
|
+
await this.assertLease(context);
|
|
66
|
+
const unitContext = { owner: context.owner, runId: context.runId, leaseId: context.lease.id };
|
|
67
|
+
const unit = new MutationUnit({ io: this.io, lock: passiveLock, serial: identitySerial, context: unitContext, ...options });
|
|
68
|
+
return await unit.apply(request);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (error instanceof HomeFenceError) {
|
|
73
|
+
throw new MutationAuthorityError(error.reason === 'busy' ? 'fence-busy' : 'fence-io', error.message);
|
|
74
|
+
}
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async transact(path, task, context) {
|
|
79
|
+
await this.assertLease(context);
|
|
80
|
+
try {
|
|
81
|
+
return await this.fence.run(async () => {
|
|
82
|
+
await this.assertLease(context);
|
|
83
|
+
const body = async (current) => await task(current);
|
|
84
|
+
if (this.io.transact !== undefined)
|
|
85
|
+
await this.io.transact(path, body);
|
|
86
|
+
else {
|
|
87
|
+
const current = await this.io.readText(path);
|
|
88
|
+
const next = await body(current);
|
|
89
|
+
if (next === null)
|
|
90
|
+
await this.io.remove(path);
|
|
91
|
+
else if (next !== current)
|
|
92
|
+
await this.io.writeText(path, next);
|
|
93
|
+
}
|
|
94
|
+
return;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (error instanceof HomeFenceError) {
|
|
99
|
+
throw new MutationAuthorityError(error.reason === 'busy' ? 'fence-busy' : 'fence-io', error.message);
|
|
100
|
+
}
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async recover(options) {
|
|
105
|
+
return await this.fence.run(async () => {
|
|
106
|
+
const unit = new MutationUnit({ io: this.io, snapshots: options.snapshots, limits: options.limits, lock: passiveLock, serial: identitySerial });
|
|
107
|
+
const prepared = await options.snapshots.listPrepared?.() ?? [];
|
|
108
|
+
const recovered = [];
|
|
109
|
+
const blocked = [];
|
|
110
|
+
for (const snapshot of prepared) {
|
|
111
|
+
if (snapshot.runId === undefined) {
|
|
112
|
+
blocked.push({ unitId: snapshot.unitId });
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const lease = await this.leases.active(snapshot.runId);
|
|
116
|
+
if (lease !== null) {
|
|
117
|
+
blocked.push({ unitId: snapshot.unitId, runId: snapshot.runId });
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
recovered.push(await unit.recover(snapshot.unitId));
|
|
121
|
+
}
|
|
122
|
+
return { recovered, blocked };
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=mutation-authority.js.map
|
|
@@ -15,7 +15,7 @@ export function unitSnapshotPort(io, home) {
|
|
|
15
15
|
schemaVersion: 1,
|
|
16
16
|
unitId: snapshot.unitId,
|
|
17
17
|
planDigest: contentHash(JSON.stringify(snapshot.ops)),
|
|
18
|
-
runId: snapshot.unitId,
|
|
18
|
+
runId: snapshot.runId ?? snapshot.unitId,
|
|
19
19
|
target: first?.target ?? '',
|
|
20
20
|
targetExists: true,
|
|
21
21
|
targetBefore: first?.before ?? null,
|
|
@@ -23,7 +23,8 @@ export function unitSnapshotPort(io, home) {
|
|
|
23
23
|
supportBefore: [],
|
|
24
24
|
supportAfter: [],
|
|
25
25
|
sourceBefore: [],
|
|
26
|
-
origin: 'foreground',
|
|
26
|
+
origin: snapshot.owner ?? 'foreground',
|
|
27
|
+
...(snapshot.leaseId === undefined ? {} : { leaseId: snapshot.leaseId }),
|
|
27
28
|
summary: 'tree-change ' + snapshot.unitId,
|
|
28
29
|
createdAt: snapshot.createdAt,
|
|
29
30
|
kind: 'tree-change',
|
|
@@ -48,6 +49,9 @@ export function unitSnapshotPort(io, home) {
|
|
|
48
49
|
ops: snapshot.ops,
|
|
49
50
|
...(snapshot.cleanup === undefined ? {} : { cleanup: snapshot.cleanup }),
|
|
50
51
|
createdAt: snapshot.createdAt,
|
|
52
|
+
...(snapshot.origin === 'foreground' || snapshot.origin === 'background_review' ? { owner: snapshot.origin } : {}),
|
|
53
|
+
runId: snapshot.runId,
|
|
54
|
+
...(snapshot.leaseId === undefined ? {} : { leaseId: snapshot.leaseId }),
|
|
51
55
|
};
|
|
52
56
|
},
|
|
53
57
|
async listPrepared() {
|
|
@@ -73,7 +77,7 @@ export function unitSnapshotPort(io, home) {
|
|
|
73
77
|
if (seen.has(snapshot.unitId))
|
|
74
78
|
continue;
|
|
75
79
|
seen.add(snapshot.unitId);
|
|
76
|
-
out.push({ unitId: snapshot.unitId, state: 'prepared', ops: snapshot.ops, ...(snapshot.cleanup === undefined ? {} : { cleanup: snapshot.cleanup }), createdAt: snapshot.createdAt });
|
|
80
|
+
out.push({ unitId: snapshot.unitId, state: 'prepared', ops: snapshot.ops, ...(snapshot.cleanup === undefined ? {} : { cleanup: snapshot.cleanup }), createdAt: snapshot.createdAt, ...(snapshot.origin === 'foreground' || snapshot.origin === 'background_review' ? { owner: snapshot.origin } : {}), runId: snapshot.runId, ...(snapshot.leaseId === undefined ? {} : { leaseId: snapshot.leaseId }) });
|
|
77
81
|
}
|
|
78
82
|
}
|
|
79
83
|
return out;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type transactIo } from './io.ts';
|
|
2
|
+
import type { MutationOwner } from '@lmzhen/dsh-evolution-contract';
|
|
2
3
|
export interface MutationRoot {
|
|
3
4
|
name: string;
|
|
4
5
|
protection: 'write' | 'delete' | 'none';
|
|
@@ -80,6 +81,15 @@ export interface MutationSnapshot {
|
|
|
80
81
|
ops: readonly MutationSnapshotOp[];
|
|
81
82
|
cleanup?: readonly string[];
|
|
82
83
|
createdAt: number;
|
|
84
|
+
owner?: MutationOwner;
|
|
85
|
+
runId?: string;
|
|
86
|
+
leaseId?: string;
|
|
87
|
+
}
|
|
88
|
+
/** Identity attached to one MutationUnit's snapshot/journal writes. */
|
|
89
|
+
export interface MutationUnitContext {
|
|
90
|
+
readonly owner: MutationOwner;
|
|
91
|
+
readonly runId: string;
|
|
92
|
+
readonly leaseId: string;
|
|
83
93
|
}
|
|
84
94
|
export interface MutationSnapshotPort {
|
|
85
95
|
write(snapshot: MutationSnapshot): Promise<void>;
|
|
@@ -105,6 +115,7 @@ export interface MutationUnitOptions {
|
|
|
105
115
|
journal?: MutationJournalPort;
|
|
106
116
|
snapshots?: MutationSnapshotPort;
|
|
107
117
|
hooks?: MutationUnitHooks;
|
|
118
|
+
context?: MutationUnitContext;
|
|
108
119
|
validateWrite?: (op: TreeChangeOp) => string | null;
|
|
109
120
|
transact?: typeof transactIo;
|
|
110
121
|
/** @deprecated the kernel always re-checks the baseline; kept for source compatibility. */
|
|
@@ -134,6 +145,7 @@ export declare class MutationUnit {
|
|
|
134
145
|
private readonly journal;
|
|
135
146
|
private readonly snapshots;
|
|
136
147
|
private readonly hooks;
|
|
148
|
+
private readonly context;
|
|
137
149
|
private readonly validateWrite;
|
|
138
150
|
private readonly kernel;
|
|
139
151
|
private readonly transactGuard;
|
|
@@ -145,6 +157,7 @@ export declare class MutationUnit {
|
|
|
145
157
|
recoverPrepared(): Promise<MutationResult[]>;
|
|
146
158
|
/** Converge a prepared unit left by a crash. Every path must be at its after-bytes; a drift refuses the whole recovery. */
|
|
147
159
|
recover(unitId: string): Promise<MutationResult>;
|
|
160
|
+
private contextFields;
|
|
148
161
|
undo(unitId: string): Promise<MutationResult>;
|
|
149
162
|
private applyOp;
|
|
150
163
|
private rollback;
|
|
@@ -101,6 +101,7 @@ export class MutationUnit {
|
|
|
101
101
|
journal;
|
|
102
102
|
snapshots;
|
|
103
103
|
hooks;
|
|
104
|
+
context;
|
|
104
105
|
validateWrite;
|
|
105
106
|
kernel;
|
|
106
107
|
transactGuard;
|
|
@@ -113,6 +114,7 @@ export class MutationUnit {
|
|
|
113
114
|
this.journal = options.journal;
|
|
114
115
|
this.snapshots = options.snapshots;
|
|
115
116
|
this.hooks = options.hooks;
|
|
117
|
+
this.context = options.context;
|
|
116
118
|
this.validateWrite = options.validateWrite;
|
|
117
119
|
this.transactGuard = options.transactGuard !== false;
|
|
118
120
|
this.now = options.now ?? Date.now;
|
|
@@ -183,7 +185,7 @@ export class MutationUnit {
|
|
|
183
185
|
});
|
|
184
186
|
const cleanup = request.cleanupPaths === undefined ? {} : { cleanup: request.cleanupPaths };
|
|
185
187
|
if (this.snapshots !== undefined)
|
|
186
|
-
await bestEffort(() => this.snapshots.write({ unitId, state: 'prepared', ops: eventOps, ...cleanup, createdAt: this.now() }));
|
|
188
|
+
await bestEffort(() => this.snapshots.write({ unitId, state: 'prepared', ops: eventOps, ...cleanup, createdAt: this.now(), ...this.contextFields() }));
|
|
187
189
|
await bestEffort(async () => this.journal?.write({ unitId, phase: 'prepared', changed: [], target: validation.roots.find(root => root.name === primary)?.name ?? primary, roots: validation.roots.map(root => root.name) }));
|
|
188
190
|
for (const op of validation.ops) {
|
|
189
191
|
const snapshot = snapshots.find(entry => entry.target === op.target);
|
|
@@ -206,7 +208,7 @@ export class MutationUnit {
|
|
|
206
208
|
// P2-1: the committed snapshot is the recovery authority. Write it BEFORE the journal's
|
|
207
209
|
// committed phase so a crash in this window recovers as committed, never rolled back.
|
|
208
210
|
if (this.snapshots !== undefined)
|
|
209
|
-
await bestEffort(() => this.snapshots.write({ unitId, state: 'committed', ops: eventOps, ...cleanup, createdAt: this.now() }));
|
|
211
|
+
await bestEffort(() => this.snapshots.write({ unitId, state: 'committed', ops: eventOps, ...cleanup, createdAt: this.now(), ...this.contextFields() }));
|
|
210
212
|
await bestEffort(async () => this.journal?.write({ unitId, phase: 'committed', changed, target: primary, roots: validation.roots.map(root => root.name) }));
|
|
211
213
|
try {
|
|
212
214
|
await this.hooks?.committed?.({ unitId, changed, ops: eventOps, roots: validation.roots.map(root => root.name) });
|
|
@@ -313,6 +315,9 @@ export class MutationUnit {
|
|
|
313
315
|
}
|
|
314
316
|
});
|
|
315
317
|
}
|
|
318
|
+
contextFields() {
|
|
319
|
+
return this.context === undefined ? {} : { owner: this.context.owner, runId: this.context.runId, leaseId: this.context.leaseId };
|
|
320
|
+
}
|
|
316
321
|
async undo(unitId) {
|
|
317
322
|
const snapshot = await this.snapshots?.read(unitId);
|
|
318
323
|
if (snapshot === undefined || snapshot === null) {
|
package/lib/types/mutations.d.ts
CHANGED
|
@@ -28,5 +28,7 @@ export declare function mutationsFile(root: string): string;
|
|
|
28
28
|
export declare function contentHash(content: string): string;
|
|
29
29
|
export declare function loadMutations(root: string, io?: EvolutionIoLike): Promise<MutationRecord[]>;
|
|
30
30
|
/** Append one record, trim to `cap`, and write atomically (versioned shape). */
|
|
31
|
-
export declare function recordMutation(root: string, io: EvolutionIoLike, record: MutationRecord, cap?: number
|
|
31
|
+
export declare function recordMutation(root: string, io: EvolutionIoLike, record: MutationRecord, cap?: number, fence?: {
|
|
32
|
+
run<T>(body: () => Promise<T>): Promise<T>;
|
|
33
|
+
}): Promise<void>;
|
|
32
34
|
//# sourceMappingURL=mutations.d.ts.map
|
package/lib/types/mutations.js
CHANGED
|
@@ -52,44 +52,50 @@ export async function loadMutations(root, io = nodeEvolutionIo()) {
|
|
|
52
52
|
return parseMutationRecords(await io.readText(mutationsFile(root)));
|
|
53
53
|
}
|
|
54
54
|
/** Append one record, trim to `cap`, and write atomically (versioned shape). */
|
|
55
|
-
export async function recordMutation(root, io, record, cap = DEFAULT_MUTATION_CAP) {
|
|
55
|
+
export async function recordMutation(root, io, record, cap = DEFAULT_MUTATION_CAP, fence) {
|
|
56
56
|
// The read-append-write runs as one atomic transact (rc.50 P2-2): a second
|
|
57
57
|
// process sharing DSH_HOME can no longer interleave its append between our
|
|
58
58
|
// read and write and lose an audit record.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
const write = async () => {
|
|
60
|
+
await transactIo(io, mutationsFile(root), (current) => {
|
|
61
|
+
// P3 (v3 audit): never overwrite a malformed audit file with a re-serialized empty.
|
|
62
|
+
// P3 (v15): parse exactly ONCE — the malformed guard and the record
|
|
63
|
+
// extraction used to JSON.parse the same bytes twice inside one transact
|
|
64
|
+
// (the guard distinguishes malformed-JSON-preserve from
|
|
65
|
+
// valid-but-shapeless-migrate, which is why the two checks are separate
|
|
66
|
+
// even though the parse is shared).
|
|
67
|
+
let parsed = [];
|
|
68
|
+
if (current !== null) {
|
|
69
|
+
try {
|
|
70
|
+
parsed = JSON.parse(current);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// C-08: the drop is not silent — every append against the malformed
|
|
74
|
+
// file warns (the wording used to claim "warn once"; the guard has no
|
|
75
|
+
// once-guard and each dropped append is independently worth seeing).
|
|
76
|
+
// Still best-effort — no retry, and the mutation itself never fails
|
|
77
|
+
// on auditing.
|
|
78
|
+
console.warn(`mutation audit record dropped: ${mutationsFile(root)} is malformed and was not overwritten`);
|
|
79
|
+
return current;
|
|
80
|
+
}
|
|
70
81
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return current;
|
|
82
|
+
// A2-11 (v18): a newer on-disk version is never downgraded by this writer.
|
|
83
|
+
if (isRecord(parsed)) {
|
|
84
|
+
const version = parsed.version;
|
|
85
|
+
if (typeof version === 'number' && version > MUTATIONS_FILE_VERSION) {
|
|
86
|
+
console.warn(`mutation audit record dropped: ${mutationsFile(root)} declares version ${version} (newer than ${MUTATIONS_FILE_VERSION}); not overwritten`);
|
|
87
|
+
return current;
|
|
88
|
+
}
|
|
79
89
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
existing.push(record);
|
|
91
|
-
const trimmed = existing.length > cap ? existing.slice(existing.length - cap) : existing;
|
|
92
|
-
return JSON.stringify({ version: MUTATIONS_FILE_VERSION, records: trimmed }, null, 2);
|
|
93
|
-
});
|
|
90
|
+
const existing = recordsFromParsed(parsed);
|
|
91
|
+
existing.push(record);
|
|
92
|
+
const trimmed = existing.length > cap ? existing.slice(existing.length - cap) : existing;
|
|
93
|
+
return JSON.stringify({ version: MUTATIONS_FILE_VERSION, records: trimmed }, null, 2);
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
if (fence !== undefined)
|
|
97
|
+
await fence.run(write);
|
|
98
|
+
else
|
|
99
|
+
await write();
|
|
94
100
|
}
|
|
95
101
|
//# sourceMappingURL=mutations.js.map
|
|
@@ -7,8 +7,11 @@
|
|
|
7
7
|
export { defineTool } from '@deepseek-ai/dsh-tools';
|
|
8
8
|
export type { ObjectJsonSchema, ToolDefinition, ToolGuard, ToolRestriction, } from '@deepseek-ai/dsh-tools';
|
|
9
9
|
export type { CommandInvocation, CommandResult } from '@deepseek-ai/dsh-commands';
|
|
10
|
-
export
|
|
11
|
-
export type {
|
|
10
|
+
export { BlockAssembler, createToolResultMessage, createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
11
|
+
export type { ContentBlock, ContextFormed, GenerateOptions, Message, RequestMessage, StreamChunk, ToolCallBlock, ToolCallId, ToolHistory, ToolSchema, } from '@deepseek-ai/dsh-llm';
|
|
12
|
+
export { SessionId } from '@deepseek-ai/dsh-session';
|
|
13
|
+
export type { EpochHeader, Session, SessionEvent } from '@deepseek-ai/dsh-session';
|
|
14
|
+
export type { Agent } from '@deepseek-ai/dsh-agent';
|
|
12
15
|
export type { PromptContext, PromptSection } from '@deepseek-ai/dsh-system-prompt';
|
|
13
16
|
export { presetsPort } from './presets.ts';
|
|
14
17
|
export type { PresetDefinitionLike, PresetDocument, PresetsPort } from './presets.ts';
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
* the platform-boundary gate keeps @deepseek-ai/dsh-* imports inside core/platform.
|
|
6
6
|
*/
|
|
7
7
|
export { defineTool } from '@deepseek-ai/dsh-tools';
|
|
8
|
+
export { BlockAssembler, createToolResultMessage, createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
9
|
+
export { SessionId } from '@deepseek-ai/dsh-session';
|
|
8
10
|
export { presetsPort } from "./presets.js";
|
|
9
11
|
export { projectionPort } from "./projections.js";
|
|
10
12
|
export { EvolutionLlmError, llmPort, modelRoute } from "./llm.js";
|
|
@@ -23,6 +23,7 @@ export const PLATFORM_SERVICE_PROBES = Object.freeze([
|
|
|
23
23
|
{ service: 'evolutionCurator', feature: '/evolution curator subcommands, skill undo and history', side: 'host' },
|
|
24
24
|
{ service: 'evolutionIo', feature: 'every durable read and write (the IO seam)', side: 'host' },
|
|
25
25
|
{ service: 'evolutionMerge', feature: 'merge command delegation and the merge doctor fact', side: 'host' },
|
|
26
|
+
{ service: 'evolutionMutationAuthority', feature: 'the family-wide mutation lease, fence and lock authority', side: 'host' },
|
|
26
27
|
{ service: 'hmr', feature: 'the preset install read-merge-write commit (the platform HMR lock serializes it)', side: 'host' },
|
|
27
28
|
{ service: 'evolutionMemoryBudget', feature: 'the memory-budget divergence section of this report', side: 'host' },
|
|
28
29
|
{ service: 'evolutionPlatform', feature: 'the single host edge object behind every family platform port', side: 'host' },
|
|
@@ -19,5 +19,6 @@ export declare function writeProposalReport(io: EvolutionIoLike, proposal: Propo
|
|
|
19
19
|
* @param limit - newest rows to return.
|
|
20
20
|
* @returns valid proposals, newest filename first.
|
|
21
21
|
*/
|
|
22
|
+
export declare function readProposalReport(io: EvolutionIoLike, id: string, dir?: string): Promise<ProposalView | null>;
|
|
22
23
|
export declare function readProposalReports(io: EvolutionIoLike, dir?: string, limit?: number): Promise<ProposalView[]>;
|
|
23
24
|
//# sourceMappingURL=proposal-report.d.ts.map
|
|
@@ -35,6 +35,18 @@ function isProposal(value) {
|
|
|
35
35
|
* @param limit - newest rows to return.
|
|
36
36
|
* @returns valid proposals, newest filename first.
|
|
37
37
|
*/
|
|
38
|
+
export async function readProposalReport(io, id, dir = proposalReportDir()) {
|
|
39
|
+
const raw = await probeText(io, join(dir, proposalReportName({ id })));
|
|
40
|
+
if (!isPresent(raw))
|
|
41
|
+
return null;
|
|
42
|
+
try {
|
|
43
|
+
const parsed = JSON.parse(raw.value);
|
|
44
|
+
return isProposal(parsed) ? parsed : null;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
38
50
|
export async function readProposalReports(io, dir = proposalReportDir(), limit = 10) {
|
|
39
51
|
const listed = await probeList(io, dir);
|
|
40
52
|
if (isUnknown(listed) || !isPresent(listed))
|
|
@@ -179,6 +179,10 @@ export interface RunRegistryOptions {
|
|
|
179
179
|
home: string;
|
|
180
180
|
/** Warn channel for a failed index write (the run itself is unaffected). */
|
|
181
181
|
warn?: (message: string) => void;
|
|
182
|
+
/** Optional family fence; when set, every index RMW runs inside it. */
|
|
183
|
+
fence?: {
|
|
184
|
+
run<T>(body: () => Promise<T>): Promise<T>;
|
|
185
|
+
} | undefined;
|
|
182
186
|
/** Clock seam for tests. */
|
|
183
187
|
now?: () => number;
|
|
184
188
|
/** Retained records (N3-clamped). */
|
|
@@ -124,6 +124,13 @@ export function newRunRegistry(options) {
|
|
|
124
124
|
const maxRecords = clampedNumber(options.maxRecords ?? DEFAULT_RUN_RECORDS, DEFAULT_RUN_RECORDS, { min: 1 });
|
|
125
125
|
const warn = options.warn ?? (() => { });
|
|
126
126
|
const path = runsFile(options.home);
|
|
127
|
+
const writeIndex = async (task) => {
|
|
128
|
+
const run = async () => { await transactIo(options.io, path, task); };
|
|
129
|
+
if (options.fence !== undefined)
|
|
130
|
+
await options.fence.run(run);
|
|
131
|
+
else
|
|
132
|
+
await run();
|
|
133
|
+
};
|
|
127
134
|
/** Rows THIS process started: its memory is the only truth for them. */
|
|
128
135
|
const owned = new Map();
|
|
129
136
|
/** Rows read from the shared index: the file is the only truth for them. */
|
|
@@ -191,7 +198,7 @@ export function newRunRegistry(options) {
|
|
|
191
198
|
// single write a non-owner may make is the orphan verdict for a row whose owner is
|
|
192
199
|
// gone. The row read inside this transaction IS the compare-and-swap: if its owner
|
|
193
200
|
// settled it in between, it no longer reads as `running` and is left alone.
|
|
194
|
-
await
|
|
201
|
+
await writeIndex((current) => {
|
|
195
202
|
const onDisk = parseIndex(current);
|
|
196
203
|
const merged = new Map();
|
|
197
204
|
for (const row of onDisk.records)
|
|
@@ -360,7 +367,7 @@ export function newRunRegistry(options) {
|
|
|
360
367
|
},
|
|
361
368
|
async supersedeKind(kind) {
|
|
362
369
|
let marked = 0;
|
|
363
|
-
await
|
|
370
|
+
await writeIndex((current) => {
|
|
364
371
|
const onDisk = parseIndex(current);
|
|
365
372
|
const merged = new Map();
|
|
366
373
|
for (const row of onDisk.records)
|
|
@@ -402,7 +409,7 @@ export function newRunRegistry(options) {
|
|
|
402
409
|
return owned.get(id)?.state === outcome.state;
|
|
403
410
|
}
|
|
404
411
|
let changed = false;
|
|
405
|
-
await
|
|
412
|
+
await writeIndex((current) => {
|
|
406
413
|
const onDisk = parseIndex(current);
|
|
407
414
|
const row = onDisk.records.find(item => item.id === id);
|
|
408
415
|
if (row === undefined)
|
|
@@ -300,7 +300,10 @@ export declare function loadVersionContent(root: string, io: EvolutionIoLike, na
|
|
|
300
300
|
* @param keep - how many versions to retain per skill.
|
|
301
301
|
* @returns the version numbers, or null when there was no content to record.
|
|
302
302
|
*/
|
|
303
|
-
export
|
|
303
|
+
export interface HistoryFence {
|
|
304
|
+
run<T>(body: () => Promise<T>): Promise<T>;
|
|
305
|
+
}
|
|
306
|
+
export declare function recordVersions(root: string, io: EvolutionIoLike, input: VersionRecordInput, keep: number, fence?: HistoryFence): Promise<RecordedVersions | null>;
|
|
304
307
|
/** What kind of change one recorded version carries, as a CLOSED set. */
|
|
305
308
|
export type VersionActionKind = 'baseline' | 'create' | 'patch' | 'update' | 'restore' | 'delete' | 'archive' | 'consolidate' | 'restructure' | 'extract' | 'merge' | 'support-write' | 'support-remove' | 'other';
|
|
306
309
|
/**
|
|
@@ -496,18 +496,7 @@ export async function loadVersionContent(root, io, name, v) {
|
|
|
496
496
|
}
|
|
497
497
|
return isPresent(probe) ? probe.value : null;
|
|
498
498
|
}
|
|
499
|
-
|
|
500
|
-
* Record the content one mutation produced, best-effort.
|
|
501
|
-
*
|
|
502
|
-
* Blobs are written FIRST: the index must never reference content that is not on disk, and a failed
|
|
503
|
-
* blob write aborts the whole record (the caller warns; the mutation itself stands).
|
|
504
|
-
* @param root - the skills root.
|
|
505
|
-
* @param io - the io seam.
|
|
506
|
-
* @param input - the write being recorded.
|
|
507
|
-
* @param keep - how many versions to retain per skill.
|
|
508
|
-
* @returns the version numbers, or null when there was no content to record.
|
|
509
|
-
*/
|
|
510
|
-
export async function recordVersions(root, io, input, keep) {
|
|
499
|
+
export async function recordVersions(root, io, input, keep, fence) {
|
|
511
500
|
if (input.before === null && input.after === null)
|
|
512
501
|
return null;
|
|
513
502
|
for (const side of [input.before, input.after]) {
|
|
@@ -560,23 +549,29 @@ export async function recordVersions(root, io, input, keep) {
|
|
|
560
549
|
}
|
|
561
550
|
const recorded = {};
|
|
562
551
|
let raceRefused = false;
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
552
|
+
const write = async () => {
|
|
553
|
+
await transactIo(io, file, (current) => {
|
|
554
|
+
const state = readHistoryIndex(current);
|
|
555
|
+
// The old comment here claimed the probe's copy covered a race. It only did when the probe had
|
|
556
|
+
// ALREADY seen the bytes as unreadable; a file that turned unreadable in between had no copy, and
|
|
557
|
+
// deriving the index from `[]` dropped every version it listed (and orphaned their blobs).
|
|
558
|
+
// Returning `current` unchanged is a byte-identical no-op, so the bytes survive and this record
|
|
559
|
+
// is abandoned below. Only an UNREADABLE state refuses: an ABSENT file is a skill with no
|
|
560
|
+
// history yet, and refusing there would abandon every first record (the index must be created).
|
|
561
|
+
if (state.kind === 'unreadable' && probe.kind !== 'unreadable') {
|
|
562
|
+
raceRefused = true;
|
|
563
|
+
return current;
|
|
564
|
+
}
|
|
565
|
+
const { versions, recorded: numbers } = nextHistoryIndex(state.kind === 'ok' ? state.versions : [], input, keep);
|
|
566
|
+
recorded.beforeVersion = numbers.beforeVersion;
|
|
567
|
+
recorded.afterVersion = numbers.afterVersion;
|
|
568
|
+
return JSON.stringify({ version: HISTORY_INDEX_VERSION, versions }, null, 2);
|
|
569
|
+
});
|
|
570
|
+
};
|
|
571
|
+
if (fence !== undefined)
|
|
572
|
+
await fence.run(write);
|
|
573
|
+
else
|
|
574
|
+
await write();
|
|
580
575
|
// v46 lint: `raceRefused` is set inside the transact task (the analyzer sees only the literal
|
|
581
576
|
// initializer), and the refusal is the S3.1 case this branch exists for.
|
|
582
577
|
// oxlint-disable-next-line typescript/no-unnecessary-condition -- set inside the transact task
|