@lmzhen/dsh-evolution-core 0.59.1 → 0.59.3
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/authority-plugin.js +7 -4
- package/lib/{extract-plan-BxDhKSki.js → extract-plan-D3ALlAmN.js} +2 -2
- package/lib/extract.js +4 -4
- package/lib/index.js +9 -9
- package/lib/io-node.js +1 -1
- package/lib/{mutation-authority-Du-ON0NY.js → mutation-authority-CZpc9F20.js} +222 -4
- package/lib/{mutation-storage-adapter-7R3ra0wj.js → mutation-storage-adapter-z1_hyrQn.js} +10 -6
- package/lib/{mutations-mvTnWkua.js → mutations-Y-wKod7W.js} +2 -2
- package/lib/{quality-CREp6s1G.js → quality-NPGr3VzA.js} +3 -3
- package/lib/{review-context-CW_kJAzn.js → review-context-yNiZadPw.js} +1 -1
- package/lib/{skill-history-YX84wO20.js → skill-history-BvLWFoOo.js} +2 -2
- package/lib/skills.js +1023 -189
- package/lib/types/authority-plugin.d.ts +7 -1
- package/lib/types/authority-plugin.js +10 -2
- package/lib/types/authority.d.ts +49 -0
- package/lib/types/authority.js +194 -0
- package/lib/types/events.d.ts +2 -0
- package/lib/types/mutation-authority.d.ts +31 -2
- package/lib/types/mutation-authority.js +45 -1
- package/lib/types/mutation-unit.d.ts +23 -1
- package/lib/types/mutation-unit.js +7 -3
- package/lib/types/skill-observation-source.d.ts +26 -0
- package/lib/types/skill-observation-source.js +56 -0
- package/lib/types/skill-observation.d.ts +87 -0
- package/lib/types/skill-observation.js +110 -0
- package/lib/types/skill-root-gate.d.ts +40 -0
- package/lib/types/skill-root-gate.js +98 -0
- package/lib/types/skill-store.d.ts +22 -3
- package/lib/types/skill-store.js +437 -217
- package/lib/types/skill-target-locks.d.ts +39 -0
- package/lib/types/skill-target-locks.js +64 -0
- package/lib/types/skill-write-intent.d.ts +63 -0
- package/lib/types/skill-write-intent.js +104 -0
- package/lib/types/skill-write-service.d.ts +85 -0
- package/lib/types/skill-write-service.js +180 -0
- package/lib/types/skills.d.ts +6 -0
- package/lib/types/skills.js +6 -0
- package/lib/{usage-DrczLTP4.js → usage-D7yHG2Cb.js} +2 -2
- package/lib/write.js +3 -3
- package/package.json +3 -3
- package/lib/{io-GxghO3Ip.js → io-Cpa8wuvj.js} +1 -1
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type Schema from '@deepseek-ai/schemastery';
|
|
2
3
|
import { MutationAuthority } from './mutation-authority.ts';
|
|
3
4
|
declare module '@deepseek-ai/cordis' {
|
|
4
5
|
interface Context {
|
|
5
6
|
evolutionMutationAuthority: MutationAuthority;
|
|
6
7
|
}
|
|
7
8
|
}
|
|
9
|
+
/** Deployment opt-in: use per-target cross-process locks for keyed skill writes. */
|
|
10
|
+
export interface Config {
|
|
11
|
+
readonly keyedSkillLocks: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare const Config: Schema<Config>;
|
|
8
14
|
export declare const name = "evolution-authority";
|
|
9
15
|
export declare const inject: string[];
|
|
10
16
|
/** Expose the single home/run-fenced mutation authority to domain packages. */
|
|
11
|
-
export declare function apply(ctx: Context): void;
|
|
17
|
+
export declare function apply(ctx: Context, config?: Config): void;
|
|
12
18
|
//# sourceMappingURL=authority-plugin.d.ts.map
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery';
|
|
1
2
|
import { evolutionHome } from "./state-store.js";
|
|
2
3
|
import { MutationAuthority } from "./mutation-authority.js";
|
|
4
|
+
export const Config = z.object({
|
|
5
|
+
keyedSkillLocks: z.boolean().default(false),
|
|
6
|
+
});
|
|
3
7
|
export const name = 'evolution-authority';
|
|
4
8
|
export const inject = ['evolutionIo'];
|
|
5
9
|
/** Expose the single home/run-fenced mutation authority to domain packages. */
|
|
6
|
-
export function apply(ctx) {
|
|
7
|
-
const authority = new MutationAuthority({
|
|
10
|
+
export function apply(ctx, config = { keyedSkillLocks: false }) {
|
|
11
|
+
const authority = new MutationAuthority({
|
|
12
|
+
io: ctx.evolutionIo.provider(),
|
|
13
|
+
home: evolutionHome(),
|
|
14
|
+
keyed: config.keyedSkillLocks === true,
|
|
15
|
+
});
|
|
8
16
|
ctx.provide('evolutionMutationAuthority', authority);
|
|
9
17
|
}
|
|
10
18
|
//# sourceMappingURL=authority-plugin.js.map
|
package/lib/types/authority.d.ts
CHANGED
|
@@ -49,6 +49,55 @@ export interface AuthorityLease {
|
|
|
49
49
|
* One home-wide writer authority. Every durable writer calls {@link HomeAuthority.runExclusive};
|
|
50
50
|
* an operation that runs inside another held operation calls {@link HomeAuthority.runNested}.
|
|
51
51
|
*/
|
|
52
|
+
/**
|
|
53
|
+
* Per-target cross-process locks for the scoped authority.
|
|
54
|
+
*
|
|
55
|
+
* Each key gets its own platform writer lock under
|
|
56
|
+
* `<home>/.authority/targets/<sha256(key)>.lock`; keys are acquired in lexical
|
|
57
|
+
* order. Different keys therefore do not exclude each other across processes,
|
|
58
|
+
* while one key remains mutually exclusive. This class is the kernel only; the
|
|
59
|
+
* mutation authority wires it into UnitTransaction after its behavior is
|
|
60
|
+
* proven.
|
|
61
|
+
*/
|
|
62
|
+
export declare class TargetLockSet {
|
|
63
|
+
private readonly home;
|
|
64
|
+
private readonly waitMs;
|
|
65
|
+
private readonly lock;
|
|
66
|
+
private readonly tails;
|
|
67
|
+
private releaseError;
|
|
68
|
+
private readonly brokenKeys;
|
|
69
|
+
/** Last release failure; diagnostics only, never a reason to wedge the key queue. */
|
|
70
|
+
get lastReleaseError(): Error | undefined;
|
|
71
|
+
constructor(home: string, waitMs?: number, lock?: typeof platformFileLock);
|
|
72
|
+
/**
|
|
73
|
+
* Run a task under all supplied target keys.
|
|
74
|
+
* @param keys - canonical target keys; duplicate/unsorted input is normalized.
|
|
75
|
+
* @param body - the critical section.
|
|
76
|
+
* @returns the body's value.
|
|
77
|
+
*/
|
|
78
|
+
run<T>(keys: readonly string[], body: () => Promise<T>): Promise<T>;
|
|
79
|
+
/** Held target queues, for leak assertions. */
|
|
80
|
+
get size(): number;
|
|
81
|
+
/**
|
|
82
|
+
* Acquire a lease over one or more target keys without running a body.
|
|
83
|
+
* Keys are normalized/sorted; a failed acquisition releases every already-held key.
|
|
84
|
+
*/
|
|
85
|
+
acquireLease(keys: readonly string[]): Promise<{
|
|
86
|
+
release(): Promise<void>;
|
|
87
|
+
}>;
|
|
88
|
+
private acquireKey;
|
|
89
|
+
private acquire;
|
|
90
|
+
/**
|
|
91
|
+
* Remove target lock files left by a release failure, but only when the
|
|
92
|
+
* recorded owner process is gone. A live owner is reported as skipped.
|
|
93
|
+
*/
|
|
94
|
+
repair(): Promise<{
|
|
95
|
+
readonly repaired: readonly string[];
|
|
96
|
+
readonly skipped: readonly string[];
|
|
97
|
+
}>;
|
|
98
|
+
private lockPath;
|
|
99
|
+
private withLockFile;
|
|
100
|
+
}
|
|
52
101
|
export declare class HomeAuthority {
|
|
53
102
|
private readonly options;
|
|
54
103
|
private readonly held;
|
package/lib/types/authority.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
* @module @lmzhen/dsh-evolution-core/authority
|
|
18
18
|
*/
|
|
19
19
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
20
|
+
import { createHash } from 'node:crypto';
|
|
20
21
|
import { mkdir, readFile, rm } from 'node:fs/promises';
|
|
21
22
|
import { dirname, join } from 'node:path';
|
|
22
23
|
import { withFileLock as platformFileLock } from '@lmzhen/dsh-evolution-adapter/values';
|
|
@@ -70,6 +71,199 @@ function isBusy(error) {
|
|
|
70
71
|
* One home-wide writer authority. Every durable writer calls {@link HomeAuthority.runExclusive};
|
|
71
72
|
* an operation that runs inside another held operation calls {@link HomeAuthority.runNested}.
|
|
72
73
|
*/
|
|
74
|
+
/**
|
|
75
|
+
* Per-target cross-process locks for the scoped authority.
|
|
76
|
+
*
|
|
77
|
+
* Each key gets its own platform writer lock under
|
|
78
|
+
* `<home>/.authority/targets/<sha256(key)>.lock`; keys are acquired in lexical
|
|
79
|
+
* order. Different keys therefore do not exclude each other across processes,
|
|
80
|
+
* while one key remains mutually exclusive. This class is the kernel only; the
|
|
81
|
+
* mutation authority wires it into UnitTransaction after its behavior is
|
|
82
|
+
* proven.
|
|
83
|
+
*/
|
|
84
|
+
export class TargetLockSet {
|
|
85
|
+
home;
|
|
86
|
+
waitMs;
|
|
87
|
+
lock;
|
|
88
|
+
tails = new Map();
|
|
89
|
+
releaseError;
|
|
90
|
+
brokenKeys = new Set();
|
|
91
|
+
/** Last release failure; diagnostics only, never a reason to wedge the key queue. */
|
|
92
|
+
get lastReleaseError() { return this.releaseError; }
|
|
93
|
+
constructor(home, waitMs = DEFAULT_WAIT_MS, lock = platformFileLock) {
|
|
94
|
+
this.home = home;
|
|
95
|
+
this.waitMs = waitMs;
|
|
96
|
+
this.lock = lock;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Run a task under all supplied target keys.
|
|
100
|
+
* @param keys - canonical target keys; duplicate/unsorted input is normalized.
|
|
101
|
+
* @param body - the critical section.
|
|
102
|
+
* @returns the body's value.
|
|
103
|
+
*/
|
|
104
|
+
async run(keys, body) {
|
|
105
|
+
const ordered = [...new Set(keys)].sort();
|
|
106
|
+
return await this.acquire(ordered, 0, body);
|
|
107
|
+
}
|
|
108
|
+
/** Held target queues, for leak assertions. */
|
|
109
|
+
get size() {
|
|
110
|
+
return this.tails.size;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Acquire a lease over one or more target keys without running a body.
|
|
114
|
+
* Keys are normalized/sorted; a failed acquisition releases every already-held key.
|
|
115
|
+
*/
|
|
116
|
+
async acquireLease(keys) {
|
|
117
|
+
const ordered = [...new Set(keys)].sort();
|
|
118
|
+
const leases = [];
|
|
119
|
+
try {
|
|
120
|
+
for (const key of ordered)
|
|
121
|
+
leases.push(await this.acquireKey(key));
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
for (const release of leases.reverse()) {
|
|
125
|
+
try {
|
|
126
|
+
await release();
|
|
127
|
+
}
|
|
128
|
+
catch { /* keep releasing the remaining keys */ }
|
|
129
|
+
}
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
let released = false;
|
|
133
|
+
return {
|
|
134
|
+
release: async () => {
|
|
135
|
+
if (released)
|
|
136
|
+
return;
|
|
137
|
+
released = true;
|
|
138
|
+
for (const release of leases.reverse())
|
|
139
|
+
await release();
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
async acquireKey(key) {
|
|
144
|
+
const prior = this.tails.get(key) ?? Promise.resolve();
|
|
145
|
+
let releaseGate;
|
|
146
|
+
const gate = new Promise((resolve) => { releaseGate = resolve; });
|
|
147
|
+
const tail = prior.then(() => gate);
|
|
148
|
+
this.tails.set(key, tail);
|
|
149
|
+
await prior;
|
|
150
|
+
let releaseLock;
|
|
151
|
+
const held = new Promise((resolve) => { releaseLock = resolve; });
|
|
152
|
+
let acquiredResolve;
|
|
153
|
+
const acquired = new Promise((resolve) => { acquiredResolve = resolve; });
|
|
154
|
+
const done = this.withLockFile(key, async () => {
|
|
155
|
+
acquiredResolve();
|
|
156
|
+
await held;
|
|
157
|
+
});
|
|
158
|
+
done.catch(() => { });
|
|
159
|
+
try {
|
|
160
|
+
await Promise.race([acquired, done]);
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
releaseGate();
|
|
164
|
+
if (this.tails.get(key) === tail)
|
|
165
|
+
this.tails.delete(key);
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
let released = false;
|
|
169
|
+
return async () => {
|
|
170
|
+
if (released)
|
|
171
|
+
return;
|
|
172
|
+
released = true;
|
|
173
|
+
releaseLock();
|
|
174
|
+
try {
|
|
175
|
+
await done;
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
this.releaseError = error instanceof Error ? error : new Error(String(error));
|
|
179
|
+
this.brokenKeys.add(key);
|
|
180
|
+
}
|
|
181
|
+
finally {
|
|
182
|
+
releaseGate();
|
|
183
|
+
if (this.tails.get(key) === tail)
|
|
184
|
+
this.tails.delete(key);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
async acquire(keys, index, body) {
|
|
189
|
+
const key = keys[index];
|
|
190
|
+
if (key === undefined)
|
|
191
|
+
return await body();
|
|
192
|
+
const prior = this.tails.get(key) ?? Promise.resolve();
|
|
193
|
+
let release;
|
|
194
|
+
const gate = new Promise((resolve) => { release = resolve; });
|
|
195
|
+
const tail = prior.then(() => gate);
|
|
196
|
+
this.tails.set(key, tail);
|
|
197
|
+
await prior;
|
|
198
|
+
try {
|
|
199
|
+
return await this.withLockFile(key, async () => await this.acquire(keys, index + 1, body));
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
release();
|
|
203
|
+
if (this.tails.get(key) === tail)
|
|
204
|
+
this.tails.delete(key);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Remove target lock files left by a release failure, but only when the
|
|
209
|
+
* recorded owner process is gone. A live owner is reported as skipped.
|
|
210
|
+
*/
|
|
211
|
+
async repair() {
|
|
212
|
+
const repaired = [];
|
|
213
|
+
const skipped = [];
|
|
214
|
+
for (const key of [...this.brokenKeys]) {
|
|
215
|
+
const path = this.lockPath(key) + '.lock';
|
|
216
|
+
let raw;
|
|
217
|
+
try {
|
|
218
|
+
raw = await readFile(path, 'utf8');
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
if (error.code === 'ENOENT') {
|
|
222
|
+
this.brokenKeys.delete(key);
|
|
223
|
+
repaired.push(key);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
skipped.push(key);
|
|
227
|
+
}
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const pid = Number.parseInt(raw.trim(), 10);
|
|
231
|
+
if (Number.isInteger(pid) && pid > 0 && pid !== process.pid && isProcessAlive(pid)) {
|
|
232
|
+
skipped.push(key);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
await rm(path, { force: true });
|
|
237
|
+
try {
|
|
238
|
+
await readFile(path, 'utf8');
|
|
239
|
+
skipped.push(key);
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
if (error.code === 'ENOENT') {
|
|
243
|
+
this.brokenKeys.delete(key);
|
|
244
|
+
repaired.push(key);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
skipped.push(key);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
skipped.push(key);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return { repaired, skipped };
|
|
256
|
+
}
|
|
257
|
+
lockPath(key) {
|
|
258
|
+
const digest = createHash('sha256').update(key).digest('hex');
|
|
259
|
+
return join(this.home, '.authority', 'targets', digest);
|
|
260
|
+
}
|
|
261
|
+
async withLockFile(key, body) {
|
|
262
|
+
const path = this.lockPath(key);
|
|
263
|
+
await mkdir(dirname(path), { recursive: true });
|
|
264
|
+
return await this.lock(path, body, { waitMs: this.waitMs });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
73
267
|
export class HomeAuthority {
|
|
74
268
|
options;
|
|
75
269
|
held = new Map();
|
package/lib/types/events.d.ts
CHANGED
|
@@ -39,6 +39,8 @@ export interface EvolutionReviewAction {
|
|
|
39
39
|
readonly action: string;
|
|
40
40
|
readonly target: string;
|
|
41
41
|
readonly status: 'ok' | 'staged' | 'error';
|
|
42
|
+
/** Machine-readable failure class for concurrent skill writes; absent on success. */
|
|
43
|
+
readonly errorKind?: 'stale' | 'busy' | 'unobserved' | 'ambiguous' | 'not-found' | undefined;
|
|
42
44
|
readonly revision?: string | undefined;
|
|
43
45
|
readonly summary?: string | undefined;
|
|
44
46
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { HomeFence } from './home-fence.ts';
|
|
2
2
|
import { LeaseStore } from './lease-store.ts';
|
|
3
3
|
import type { EvolutionIoLike } from './io.ts';
|
|
4
|
-
import type { MutationLockPort } from './mutation-unit.ts';
|
|
4
|
+
import type { MutationLockLease, MutationLockPort } from './mutation-unit.ts';
|
|
5
|
+
import { TargetLockSet } from './authority.ts';
|
|
5
6
|
export interface MutationAuthorityOptions {
|
|
6
7
|
readonly io: EvolutionIoLike;
|
|
7
8
|
readonly home: string;
|
|
@@ -9,6 +10,8 @@ export interface MutationAuthorityOptions {
|
|
|
9
10
|
readonly fence?: HomeFence;
|
|
10
11
|
readonly waitMs?: number;
|
|
11
12
|
readonly now?: () => number;
|
|
13
|
+
/** Opt in to per-target cross-process locks for writes carrying targetKeys. */
|
|
14
|
+
readonly keyed?: boolean;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
14
17
|
* The family's write authority. It owns the one home fence and the per-run lease store.
|
|
@@ -21,15 +24,41 @@ export interface MutationAuthorityOptions {
|
|
|
21
24
|
export declare class MutationAuthority {
|
|
22
25
|
private readonly leases;
|
|
23
26
|
private readonly fence;
|
|
27
|
+
private readonly keyed;
|
|
28
|
+
private readonly targetLocks;
|
|
24
29
|
/** Lease primitives for run/claim owners (review, approval). */
|
|
25
30
|
get lease(): LeaseStore;
|
|
26
31
|
/** The one home fence domain unit engines must reuse instead of taking tree-lock themselves. */
|
|
27
32
|
get homeFence(): HomeFence;
|
|
28
|
-
/** Adapter for MutationUnit call sites
|
|
33
|
+
/** Adapter for MutationUnit call sites; root fence or, when opted in, keyed targets. */
|
|
29
34
|
get mutationLock(): MutationLockPort;
|
|
35
|
+
private get rootMutationLock();
|
|
30
36
|
constructor(options: MutationAuthorityOptions);
|
|
31
37
|
}
|
|
32
38
|
/** Read the authority's fence adapter from a context; undefined means no authority is mounted. */
|
|
39
|
+
/** A keyed MutationLockPort over TargetLockSet; empty key sets are refused, not silently unlocked. */
|
|
40
|
+
export declare class TargetMutationLock implements MutationLockPort {
|
|
41
|
+
private readonly locks;
|
|
42
|
+
readonly keyed = true;
|
|
43
|
+
constructor(locks: TargetLockSet);
|
|
44
|
+
acquire(targetKeys?: readonly string[]): Promise<MutationLockLease>;
|
|
45
|
+
repair(): Promise<{
|
|
46
|
+
readonly repaired: readonly string[];
|
|
47
|
+
readonly skipped: readonly string[];
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
/** Routes keyed writes to the target lock set and keyless writes to the root fence. */
|
|
51
|
+
export declare class HybridMutationLock implements MutationLockPort {
|
|
52
|
+
private readonly keyedLock;
|
|
53
|
+
private readonly fallback;
|
|
54
|
+
readonly keyed = true;
|
|
55
|
+
constructor(keyedLock: TargetMutationLock, fallback: MutationLockPort);
|
|
56
|
+
acquire(targetKeys?: readonly string[]): Promise<MutationLockLease>;
|
|
57
|
+
repair(): Promise<{
|
|
58
|
+
readonly repaired: readonly string[];
|
|
59
|
+
readonly skipped: readonly string[];
|
|
60
|
+
}>;
|
|
61
|
+
}
|
|
33
62
|
export declare function mutationLockFrom(ctx: {
|
|
34
63
|
get?(name: string): unknown;
|
|
35
64
|
} | undefined): MutationLockPort | undefined;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { HomeFence } from "./home-fence.js";
|
|
2
2
|
import { LeaseStore } from "./lease-store.js";
|
|
3
|
+
import { TargetLockSet } from "./authority.js";
|
|
3
4
|
/**
|
|
4
5
|
* The family's write authority. It owns the one home fence and the per-run lease store.
|
|
5
6
|
*
|
|
@@ -11,12 +12,19 @@ import { LeaseStore } from "./lease-store.js";
|
|
|
11
12
|
export class MutationAuthority {
|
|
12
13
|
leases;
|
|
13
14
|
fence;
|
|
15
|
+
keyed;
|
|
16
|
+
targetLocks;
|
|
14
17
|
/** Lease primitives for run/claim owners (review, approval). */
|
|
15
18
|
get lease() { return this.leases; }
|
|
16
19
|
/** The one home fence domain unit engines must reuse instead of taking tree-lock themselves. */
|
|
17
20
|
get homeFence() { return this.fence; }
|
|
18
|
-
/** Adapter for MutationUnit call sites
|
|
21
|
+
/** Adapter for MutationUnit call sites; root fence or, when opted in, keyed targets. */
|
|
19
22
|
get mutationLock() {
|
|
23
|
+
if (!this.keyed)
|
|
24
|
+
return this.rootMutationLock;
|
|
25
|
+
return new HybridMutationLock(new TargetMutationLock(this.targetLocks), this.rootMutationLock);
|
|
26
|
+
}
|
|
27
|
+
get rootMutationLock() {
|
|
20
28
|
return {
|
|
21
29
|
acquire: async () => {
|
|
22
30
|
// A nested writer running inside an already-held home fence must reuse that
|
|
@@ -30,11 +38,47 @@ export class MutationAuthority {
|
|
|
30
38
|
};
|
|
31
39
|
}
|
|
32
40
|
constructor(options) {
|
|
41
|
+
this.keyed = options.keyed === true;
|
|
42
|
+
this.targetLocks = this.keyed ? new TargetLockSet(options.home) : undefined;
|
|
33
43
|
this.leases = options.leases ?? new LeaseStore({ io: options.io, home: options.home, ...(options.now === undefined ? {} : { now: options.now }) });
|
|
34
44
|
this.fence = options.fence ?? new HomeFence({ io: options.io, home: options.home, ...(options.waitMs === undefined ? {} : { waitMs: options.waitMs }), ...(options.now === undefined ? {} : { now: options.now }) });
|
|
35
45
|
}
|
|
36
46
|
}
|
|
37
47
|
/** Read the authority's fence adapter from a context; undefined means no authority is mounted. */
|
|
48
|
+
/** A keyed MutationLockPort over TargetLockSet; empty key sets are refused, not silently unlocked. */
|
|
49
|
+
export class TargetMutationLock {
|
|
50
|
+
locks;
|
|
51
|
+
keyed = true;
|
|
52
|
+
constructor(locks) {
|
|
53
|
+
this.locks = locks;
|
|
54
|
+
}
|
|
55
|
+
async acquire(targetKeys = []) {
|
|
56
|
+
if (targetKeys.length === 0)
|
|
57
|
+
throw new Error('target mutation lock requires at least one target key');
|
|
58
|
+
return await this.locks.acquireLease(targetKeys);
|
|
59
|
+
}
|
|
60
|
+
async repair() {
|
|
61
|
+
return await this.locks.repair();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Routes keyed writes to the target lock set and keyless writes to the root fence. */
|
|
65
|
+
export class HybridMutationLock {
|
|
66
|
+
keyedLock;
|
|
67
|
+
fallback;
|
|
68
|
+
keyed = true;
|
|
69
|
+
constructor(keyedLock, fallback) {
|
|
70
|
+
this.keyedLock = keyedLock;
|
|
71
|
+
this.fallback = fallback;
|
|
72
|
+
}
|
|
73
|
+
async acquire(targetKeys = []) {
|
|
74
|
+
return targetKeys.length === 0
|
|
75
|
+
? await this.fallback.acquire()
|
|
76
|
+
: await this.keyedLock.acquire(targetKeys);
|
|
77
|
+
}
|
|
78
|
+
async repair() {
|
|
79
|
+
return await this.keyedLock.repair();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
38
82
|
export function mutationLockFrom(ctx) {
|
|
39
83
|
if (ctx?.get === undefined)
|
|
40
84
|
return undefined;
|
|
@@ -72,7 +72,19 @@ export interface MutationLockLease {
|
|
|
72
72
|
release(): Promise<void>;
|
|
73
73
|
}
|
|
74
74
|
export interface MutationLockPort {
|
|
75
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Acquire the write lock. `targetKeys`, when supplied, asks a keyed backend
|
|
77
|
+
* to lock those skill targets instead of the whole root; a backend that only
|
|
78
|
+
* implements the root lock may ignore them.
|
|
79
|
+
*/
|
|
80
|
+
acquire(targetKeys?: readonly string[]): Promise<MutationLockLease>;
|
|
81
|
+
/** True when this port locks per target key rather than the whole home. */
|
|
82
|
+
readonly keyed?: boolean;
|
|
83
|
+
/** Best-effort removal of stale target lock files left by a failed release. */
|
|
84
|
+
repair?(): Promise<{
|
|
85
|
+
readonly repaired: readonly string[];
|
|
86
|
+
readonly skipped: readonly string[];
|
|
87
|
+
}>;
|
|
76
88
|
}
|
|
77
89
|
export interface MutationSnapshotOp {
|
|
78
90
|
target: string;
|
|
@@ -118,6 +130,14 @@ export interface MutationUnitOptions {
|
|
|
118
130
|
limits: MutationLimits;
|
|
119
131
|
serial?: <T>(task: () => Promise<T>) => Promise<T>;
|
|
120
132
|
lock?: MutationLockPort;
|
|
133
|
+
/** Canonical skill target keys whose keyed lock backend should be acquired. */
|
|
134
|
+
targetKeys?: readonly string[];
|
|
135
|
+
/** Recovery-only: derive keys from the snapshot being recovered. */
|
|
136
|
+
targetKeysForSnapshot?: (snapshot: {
|
|
137
|
+
readonly ops: readonly {
|
|
138
|
+
readonly target: string;
|
|
139
|
+
}[];
|
|
140
|
+
}) => readonly string[];
|
|
121
141
|
/** Explicit escape for a caller that already holds the home fence (nested merge/skill write). */
|
|
122
142
|
nested?: boolean;
|
|
123
143
|
journal?: MutationJournalPort;
|
|
@@ -153,6 +173,8 @@ export declare class MutationUnit {
|
|
|
153
173
|
private readonly limits;
|
|
154
174
|
private readonly serial;
|
|
155
175
|
private readonly lock;
|
|
176
|
+
private readonly targetKeys;
|
|
177
|
+
private readonly targetKeysForSnapshot;
|
|
156
178
|
private readonly journal;
|
|
157
179
|
private readonly snapshots;
|
|
158
180
|
private readonly claim;
|
|
@@ -99,6 +99,8 @@ export class MutationUnit {
|
|
|
99
99
|
limits;
|
|
100
100
|
serial;
|
|
101
101
|
lock;
|
|
102
|
+
targetKeys;
|
|
103
|
+
targetKeysForSnapshot;
|
|
102
104
|
journal;
|
|
103
105
|
snapshots;
|
|
104
106
|
claim;
|
|
@@ -118,6 +120,8 @@ export class MutationUnit {
|
|
|
118
120
|
throw new Error('mutation-unit: a lock (or an explicit nested:true under an outer fence) is required');
|
|
119
121
|
}
|
|
120
122
|
this.lock = options.lock ?? noLock;
|
|
123
|
+
this.targetKeys = options.targetKeys;
|
|
124
|
+
this.targetKeysForSnapshot = options.targetKeysForSnapshot;
|
|
121
125
|
this.journal = options.journal;
|
|
122
126
|
this.snapshots = options.snapshots;
|
|
123
127
|
this.claim = options.claim;
|
|
@@ -152,7 +156,7 @@ export class MutationUnit {
|
|
|
152
156
|
return await this.serial(async () => {
|
|
153
157
|
let lease;
|
|
154
158
|
try {
|
|
155
|
-
lease = await this.lock.acquire();
|
|
159
|
+
lease = await this.lock.acquire(this.targetKeys);
|
|
156
160
|
}
|
|
157
161
|
catch (error) {
|
|
158
162
|
const code = error instanceof TransactionNotInvokedError ? 'transact-not-invoked' : 'unit-fence-failed';
|
|
@@ -298,7 +302,7 @@ export class MutationUnit {
|
|
|
298
302
|
return await this.serial(async () => {
|
|
299
303
|
let lease;
|
|
300
304
|
try {
|
|
301
|
-
lease = await this.lock.acquire();
|
|
305
|
+
lease = await this.lock.acquire(this.targetKeysForSnapshot?.(snapshot) ?? this.targetKeys);
|
|
302
306
|
}
|
|
303
307
|
catch (error) {
|
|
304
308
|
const code = error instanceof TransactionNotInvokedError ? 'transact-not-invoked' : 'unit-fence-failed';
|
|
@@ -356,7 +360,7 @@ export class MutationUnit {
|
|
|
356
360
|
return await this.serial(async () => {
|
|
357
361
|
let lease;
|
|
358
362
|
try {
|
|
359
|
-
lease = await this.lock.acquire();
|
|
363
|
+
lease = await this.lock.acquire(this.targetKeys);
|
|
360
364
|
}
|
|
361
365
|
catch (error) {
|
|
362
366
|
return { ok: false, unitId, dryRun: false, changed: [], errors: [{ code: 'lock-failed', message: error instanceof Error ? error.message : String(error) }] };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SkillObservedVersion, SkillObservationStore, SkillTarget } from './skill-observation.ts';
|
|
2
|
+
/** One platform observation, already reduced to the values the store needs. */
|
|
3
|
+
export interface FsSkillObservation {
|
|
4
|
+
/** Absolute host path of the read target. */
|
|
5
|
+
readonly hostPath: string;
|
|
6
|
+
/** Platform `FsVersion` or another opaque freshness token. */
|
|
7
|
+
readonly version: SkillObservedVersion;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Resolve a host path under the skills root to one skill file.
|
|
11
|
+
* @param root - absolute skills root.
|
|
12
|
+
* @param hostPath - absolute path reported by the platform observation.
|
|
13
|
+
* @returns the target, or `undefined` when the path is outside the root, points
|
|
14
|
+
* at the root itself, or is not a skill file (`SKILL.md` / a support file).
|
|
15
|
+
*/
|
|
16
|
+
export declare function skillTargetFromHostPath(root: string, hostPath: string): SkillTarget | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Record one platform observation when it names a skill file.
|
|
19
|
+
* @param store - the per-context observation store.
|
|
20
|
+
* @param owner - exact reading identity (session / review task / run object).
|
|
21
|
+
* @param root - absolute skills root.
|
|
22
|
+
* @param observation - host path and opaque version from the platform event.
|
|
23
|
+
* @returns the recorded target, or `undefined` when the path is not a skill file.
|
|
24
|
+
*/
|
|
25
|
+
export declare function recordFsSkillObservation(store: SkillObservationStore, owner: object, root: string, observation: FsSkillObservation): SkillTarget | undefined;
|
|
26
|
+
//# sourceMappingURL=skill-observation-source.d.ts.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map framework filesystem observations onto skill targets.
|
|
3
|
+
*
|
|
4
|
+
* The platform emits `fs/observed(target, observation, actor)` after a
|
|
5
|
+
* successful read. This module is the skill-domain edge: it decides whether a
|
|
6
|
+
* host path names SKILL.md or a support file under one skill, and records the
|
|
7
|
+
* platform's opaque version in a {@link SkillObservationStore}.
|
|
8
|
+
*
|
|
9
|
+
* It deliberately does not decide which version model wins (platform FsVersion
|
|
10
|
+
* vs content sha256); that choice belongs to the SkillWriteService phase. Its
|
|
11
|
+
* only job is path-to-target identity, so an observation and a later write
|
|
12
|
+
* cannot disagree about which skill file they mean.
|
|
13
|
+
*
|
|
14
|
+
* @module @lmzhen/dsh-evolution-core/skill-observation-source
|
|
15
|
+
*/
|
|
16
|
+
import { relative, resolve, isAbsolute } from 'node:path';
|
|
17
|
+
import { skillBodyTarget, skillSupportTarget } from "./skill-observation.js";
|
|
18
|
+
/**
|
|
19
|
+
* Resolve a host path under the skills root to one skill file.
|
|
20
|
+
* @param root - absolute skills root.
|
|
21
|
+
* @param hostPath - absolute path reported by the platform observation.
|
|
22
|
+
* @returns the target, or `undefined` when the path is outside the root, points
|
|
23
|
+
* at the root itself, or is not a skill file (`SKILL.md` / a support file).
|
|
24
|
+
*/
|
|
25
|
+
export function skillTargetFromHostPath(root, hostPath) {
|
|
26
|
+
if (!isAbsolute(root) || !isAbsolute(hostPath))
|
|
27
|
+
return undefined;
|
|
28
|
+
const rel = relative(resolve(root), resolve(hostPath)).replaceAll('\\', '/');
|
|
29
|
+
if (rel === '' || rel.startsWith('../') || rel === '..' || isAbsolute(rel))
|
|
30
|
+
return undefined;
|
|
31
|
+
const parts = rel.split('/').filter(part => part !== '' && part !== '.');
|
|
32
|
+
if (parts.length < 2)
|
|
33
|
+
return undefined;
|
|
34
|
+
const [skill, ...rest] = parts;
|
|
35
|
+
if (skill === undefined || skill === '')
|
|
36
|
+
return undefined;
|
|
37
|
+
if (rest.length === 1 && rest[0] === 'SKILL.md')
|
|
38
|
+
return skillBodyTarget(skill);
|
|
39
|
+
return skillSupportTarget(skill, rest.join('/'));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Record one platform observation when it names a skill file.
|
|
43
|
+
* @param store - the per-context observation store.
|
|
44
|
+
* @param owner - exact reading identity (session / review task / run object).
|
|
45
|
+
* @param root - absolute skills root.
|
|
46
|
+
* @param observation - host path and opaque version from the platform event.
|
|
47
|
+
* @returns the recorded target, or `undefined` when the path is not a skill file.
|
|
48
|
+
*/
|
|
49
|
+
export function recordFsSkillObservation(store, owner, root, observation) {
|
|
50
|
+
const target = skillTargetFromHostPath(root, observation.hostPath);
|
|
51
|
+
if (target === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
store.record(owner, target, { kind: 'present', version: observation.version });
|
|
54
|
+
return target;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=skill-observation-source.js.map
|