@lmzhen/dsh-evolution-core 0.43.2-rc.1 → 0.45.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/lib/authority-plugin.js +14 -0
- package/lib/composition/index.js +2 -2
- package/lib/{composition-CVafrlEw.js → composition-Da8NfRI7.js} +2 -2
- package/lib/contract.js +1 -0
- package/lib/index.js +59 -133
- package/lib/{mutations-jUuOPaCn.js → io-AmUf4lVY.js} +54 -84
- package/lib/io-node.js +24 -0
- package/lib/io-registry.js +96 -0
- package/lib/mutation-authority-DeJ_Wz14.js +537 -0
- package/lib/mutations-DwvbjRHF.js +75 -0
- package/lib/numeric-DLeLvvGb.js +324 -0
- package/lib/platform/index.js +1 -1
- package/lib/{platform-B06_h8t-.js → platform-CFA12Jam.js} +36 -19
- package/lib/policy.js +99 -0
- package/lib/runtime/plugin.js +8 -1
- package/lib/{skill-health-BLGZTIv8.js → skill-health-D1pZGk5V.js} +6 -835
- package/lib/{skill-history-_Cssts5o.js → skill-history-CjypdYqv.js} +2 -1
- package/lib/skills.js +6 -3
- package/lib/subagent/index.js +1 -1
- package/lib/types/authority-plugin.d.ts +12 -0
- package/lib/types/authority-plugin.js +10 -0
- package/lib/types/authority.d.ts +96 -0
- package/lib/types/authority.js +237 -0
- package/lib/types/contract.d.ts +116 -0
- package/lib/types/contract.js +10 -0
- package/lib/types/events.d.ts +2 -2
- package/lib/types/home-fence.d.ts +25 -17
- package/lib/types/home-fence.js +42 -92
- package/lib/types/index.d.ts +2 -2
- package/lib/types/index.js +2 -2
- package/lib/types/io-node.d.ts +15 -0
- package/lib/types/io-node.js +36 -0
- package/lib/types/io-registry.d.ts +71 -0
- package/lib/types/io-registry.js +126 -0
- package/lib/types/io.d.ts +29 -2
- package/lib/types/io.js +50 -9
- package/lib/types/journal-protocol.d.ts +35 -0
- package/lib/types/journal-protocol.js +29 -0
- package/lib/types/lease-store.d.ts +1 -1
- package/lib/types/mutation-unit.d.ts +1 -1
- package/lib/types/params.d.ts +2 -16
- package/lib/types/params.js +6 -44
- package/lib/types/platform/llm.js +4 -1
- package/lib/types/platform/projections.d.ts +10 -4
- package/lib/types/platform/projections.js +34 -17
- package/lib/types/platform-services.d.ts +2 -2
- package/lib/types/platform-services.js +1 -0
- package/lib/types/policy.d.ts +68 -0
- package/lib/types/policy.js +161 -0
- package/lib/types/run-registry.d.ts +2 -2
- package/lib/types/runtime/projections.js +12 -0
- package/lib/types/state-store.d.ts +1 -1
- package/lib/types/state-store.js +1 -1
- package/lib/types/store-ledger.js +2 -2
- package/lib/types/write-inventory.js +3 -1
- package/lib/write.js +2 -2
- package/locale/de.json +6 -0
- package/locale/en.json +6 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/runtime/de.json +6 -0
- package/locale/runtime/en.json +6 -0
- package/locale/runtime/es.json +6 -0
- package/locale/runtime/fr.json +6 -0
- package/locale/runtime/ja.json +6 -0
- package/locale/runtime/pt.json +6 -0
- package/locale/runtime/ru.json +6 -0
- package/locale/runtime/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +29 -4
- package/persisted-write-inventory.json +10 -37
- package/lib/types/home-owner.d.ts +0 -4
- package/lib/types/home-owner.js +0 -37
- package/lib/types/tree-lock.d.ts +0 -54
- package/lib/types/tree-lock.js +0 -127
- package/lib/{ensure-BjToTMMj.js → ensure-C5Mo1dbB.js} +1 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Service } from "@deepseek-ai/cordis";
|
|
2
|
+
//#region lib/types/io-registry.js
|
|
3
|
+
/**
|
|
4
|
+
* IO seam for evolution providers.
|
|
5
|
+
* @module @lmzhen/dsh-evolution-core/io-registry
|
|
6
|
+
*/
|
|
7
|
+
var EvolutionIoRegistry = class extends Service {
|
|
8
|
+
providers = /* @__PURE__ */ new Map();
|
|
9
|
+
/** Name of the provider that declared itself the default (P2-7, v14). */
|
|
10
|
+
defaultName;
|
|
11
|
+
/** P2-3 (v15): per-name dispose — the idempotent re-registration returns
|
|
12
|
+
* the SAME function instance, so double-dispose is a single removal. */
|
|
13
|
+
disposals = /* @__PURE__ */ new Map();
|
|
14
|
+
constructor(ctx) {
|
|
15
|
+
super(ctx, "evolutionIo");
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* @param options.default - mark this provider as the one a nameless
|
|
19
|
+
* `provider()` resolves. The first explicit default wins; without any
|
|
20
|
+
* default the registry keeps its historical registration-order fallback.
|
|
21
|
+
*
|
|
22
|
+
* P2-3 (v15): re-registering the IDENTICAL provider object is idempotent —
|
|
23
|
+
* it returns the original dispose and does not throw. This is what lets a
|
|
24
|
+
* backend (io-node) re-apply (HMR / re-mounted row) without the silent
|
|
25
|
+
* `hasProvider` early-return the v14 fix used, which had made the
|
|
26
|
+
* fail-loud-below unreachable for a FOREIGN provider under the same name.
|
|
27
|
+
* A different object under a registered name still throws.
|
|
28
|
+
*/
|
|
29
|
+
registerProvider(provider, options = {}) {
|
|
30
|
+
const idempotent = this.providers.get(provider.name) === provider;
|
|
31
|
+
if (!idempotent && this.providers.has(provider.name)) throw new Error(`evolution IO provider "${provider.name}" already registered`);
|
|
32
|
+
if (options.default === true) {
|
|
33
|
+
if (this.defaultName !== void 0 && this.defaultName !== provider.name) this.ctx.logger.warn(`evolution IO provider "${provider.name}" declared itself the default, but "${this.defaultName}" already is — the first declaration wins`);
|
|
34
|
+
this.defaultName ??= provider.name;
|
|
35
|
+
}
|
|
36
|
+
if (idempotent) {
|
|
37
|
+
const existing = this.disposals.get(provider.name);
|
|
38
|
+
if (existing !== void 0) return existing;
|
|
39
|
+
}
|
|
40
|
+
const dispose = () => {
|
|
41
|
+
if (this.disposals.get(provider.name) !== dispose) return;
|
|
42
|
+
this.providers.delete(provider.name);
|
|
43
|
+
this.disposals.delete(provider.name);
|
|
44
|
+
if (this.defaultName === provider.name) this.defaultName = void 0;
|
|
45
|
+
};
|
|
46
|
+
this.providers.set(provider.name, provider);
|
|
47
|
+
this.disposals.set(provider.name, dispose);
|
|
48
|
+
return dispose;
|
|
49
|
+
}
|
|
50
|
+
provider(name) {
|
|
51
|
+
if (name) {
|
|
52
|
+
const provider = this.providers.get(name);
|
|
53
|
+
if (provider) return provider;
|
|
54
|
+
throw new Error(`evolution IO provider "${name}" is not registered`);
|
|
55
|
+
}
|
|
56
|
+
const first = (this.defaultName === void 0 ? void 0 : this.providers.get(this.defaultName)) ?? this.providers.values().next().value;
|
|
57
|
+
if (!first) throw new Error("no evolution IO provider registered — mount evolution-io-node (@lmzhen/dsh-evolution-io-node published, @lmzhen/dsh-evolution-core/io-node in the dev overlay; ships with evolution-host/evolution-all) and retry");
|
|
58
|
+
return first;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const mediumObservations = /* @__PURE__ */ new WeakMap();
|
|
62
|
+
/**
|
|
63
|
+
* The read-only face of the marker: what served THIS registry, without resolving anything.
|
|
64
|
+
* @param registry - the seam registry the caller already holds.
|
|
65
|
+
* @returns `known` with the observation, or `unknown` when nothing resolved through this registry yet.
|
|
66
|
+
*/
|
|
67
|
+
function describeMedium(registry) {
|
|
68
|
+
const observation = mediumObservations.get(registry);
|
|
69
|
+
if (observation === void 0) return { state: "unknown" };
|
|
70
|
+
return {
|
|
71
|
+
state: "known",
|
|
72
|
+
requested: observation.requested,
|
|
73
|
+
resolved: observation.resolved,
|
|
74
|
+
via: observation.via
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function preferProvider(registry, names) {
|
|
78
|
+
for (const name of names) try {
|
|
79
|
+
const resolved = registry.provider(name);
|
|
80
|
+
mediumObservations.set(registry, {
|
|
81
|
+
requested: names,
|
|
82
|
+
resolved: name,
|
|
83
|
+
via: "preference"
|
|
84
|
+
});
|
|
85
|
+
return resolved;
|
|
86
|
+
} catch {}
|
|
87
|
+
const fallback = registry.provider();
|
|
88
|
+
mediumObservations.set(registry, {
|
|
89
|
+
requested: names,
|
|
90
|
+
resolved: void 0,
|
|
91
|
+
via: "default"
|
|
92
|
+
});
|
|
93
|
+
return fallback;
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { EvolutionIoRegistry, EvolutionIoRegistry as default, describeMedium, preferProvider };
|
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
import { i as isProcessAlive } from "./io-AmUf4lVY.js";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
import { mkdir, readFile, rm } from "node:fs/promises";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { withFileLock } from "@deepseek-ai/dsh-atomic-write";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
//#region lib/types/authority.js
|
|
9
|
+
/**
|
|
10
|
+
* The family's one mutual-exclusion entry (W9-authority, invariant I1).
|
|
11
|
+
*
|
|
12
|
+
* Cross-process exclusion is the platform's writer lock (`withFileLock` from
|
|
13
|
+
* `@deepseek-ai/dsh-atomic-write`): one lock per evolution home, so every scope shares it and two
|
|
14
|
+
* scopes can never deadlock against each other. In-process callers queue FIFO through a promise
|
|
15
|
+
* tail, so writers run in invocation order instead of racing for the platform lock.
|
|
16
|
+
*
|
|
17
|
+
* `scope` carries the nesting order only (`tree` outside, `state` inside — the merge shell writes
|
|
18
|
+
* state while it holds the tree). Re-entrancy is explicit: `runNested` declares "this operation is
|
|
19
|
+
* inside a held one", and the held context is tracked per authority instance so a second authority
|
|
20
|
+
* for the same home is not mistaken for a re-entry. An undeclared re-entry throws instead of
|
|
21
|
+
* waiting on the platform lock forever.
|
|
22
|
+
*
|
|
23
|
+
* A failed release never masks the body's outcome: an operation whose write already landed keeps
|
|
24
|
+
* its result, and the failure is recorded for {@link HomeAuthority.releaseFailure} and `repair()`.
|
|
25
|
+
* @module @lmzhen/dsh-evolution-core/authority
|
|
26
|
+
*/
|
|
27
|
+
/** Who holds the home lock right now, for doctor rows; `undefined` means free or unreadable. */
|
|
28
|
+
async function authorityHolder(home) {
|
|
29
|
+
try {
|
|
30
|
+
const raw = await readFile(join(home, ".authority", "home.lock"), "utf8");
|
|
31
|
+
const pid = Number.parseInt(raw.trim(), 10);
|
|
32
|
+
return Number.isInteger(pid) && pid > 0 ? { pid } : void 0;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (error.code === "ENOENT") return void 0;
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const ORDER = {
|
|
39
|
+
tree: 0,
|
|
40
|
+
state: 1
|
|
41
|
+
};
|
|
42
|
+
const DEFAULT_WAIT_MS = 5e3;
|
|
43
|
+
const CONTEXT = new AsyncLocalStorage();
|
|
44
|
+
/** The platform lock timed out: another writer holds the home. */
|
|
45
|
+
var AuthorityBusyError = class extends Error {
|
|
46
|
+
constructor(message) {
|
|
47
|
+
super(message);
|
|
48
|
+
this.name = "AuthorityBusyError";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
/** A caller took a scope it already holds without declaring the nesting. */
|
|
52
|
+
var AuthorityReentryError = class extends Error {
|
|
53
|
+
constructor(scope) {
|
|
54
|
+
super("authority: \"" + scope + "\" would be re-entered; use runNested() inside a held operation");
|
|
55
|
+
this.name = "AuthorityReentryError";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/** A caller took the outer scope while already holding the inner one. */
|
|
59
|
+
var AuthorityOrderError = class extends Error {
|
|
60
|
+
constructor(outer, inner) {
|
|
61
|
+
super("lock order violation: scope \"" + inner + "\" is held but \"" + outer + "\" was requested; take tree before state");
|
|
62
|
+
this.name = "AuthorityOrderError";
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
function asError(error) {
|
|
66
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
67
|
+
}
|
|
68
|
+
/** True when the platform lock refused within its wait budget. */
|
|
69
|
+
function isBusy(error) {
|
|
70
|
+
return error instanceof Error && /timed out waiting for the writer lock/.test(error.message);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* One home-wide writer authority. Every durable writer calls {@link HomeAuthority.runExclusive};
|
|
74
|
+
* an operation that runs inside another held operation calls {@link HomeAuthority.runNested}.
|
|
75
|
+
*/
|
|
76
|
+
var HomeAuthority = class {
|
|
77
|
+
options;
|
|
78
|
+
held = /* @__PURE__ */ new Map();
|
|
79
|
+
tail = Promise.resolve();
|
|
80
|
+
pending;
|
|
81
|
+
constructor(options) {
|
|
82
|
+
this.options = options;
|
|
83
|
+
}
|
|
84
|
+
/** The lock path the platform primitive writes its `<path>.lock` beside. */
|
|
85
|
+
get lockBase() {
|
|
86
|
+
return join(this.options.home, ".authority", "home");
|
|
87
|
+
}
|
|
88
|
+
/** The last release failure, if any; diagnostics only. */
|
|
89
|
+
get releaseFailure() {
|
|
90
|
+
return this.pending;
|
|
91
|
+
}
|
|
92
|
+
/** True while the current call context (or this instance) holds `scope`. */
|
|
93
|
+
assertHeld(scope) {
|
|
94
|
+
const context = CONTEXT.getStore();
|
|
95
|
+
if (context?.owner === this) return context.scopes.has(scope);
|
|
96
|
+
return (this.held.get(scope) ?? 0) > 0;
|
|
97
|
+
}
|
|
98
|
+
/** True while this instance holds the home lock for any scope. */
|
|
99
|
+
get holding() {
|
|
100
|
+
return this.held.size > 0;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Run `body` inside a held operation: order-checked, no lock acquisition.
|
|
104
|
+
* @param scope - the nested scope; must not be an outer scope of the held one.
|
|
105
|
+
* @param body - the operation to run inline.
|
|
106
|
+
* @returns the body's result.
|
|
107
|
+
*/
|
|
108
|
+
async runNested(scope, body) {
|
|
109
|
+
const context = CONTEXT.getStore();
|
|
110
|
+
if (context?.owner !== this) throw new AuthorityReentryError(scope);
|
|
111
|
+
for (const outer of context.scopes) if (ORDER[outer] > ORDER[scope]) throw new AuthorityOrderError(outer, scope);
|
|
112
|
+
const scopes = new Set(context.scopes);
|
|
113
|
+
scopes.add(scope);
|
|
114
|
+
return await CONTEXT.run({
|
|
115
|
+
owner: this,
|
|
116
|
+
scopes
|
|
117
|
+
}, body);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Run `body` while holding the home lock, in invocation order among this instance's callers.
|
|
121
|
+
* @param scope - the write scope; `tree` must be taken before `state`.
|
|
122
|
+
* @param body - the operation to serialize.
|
|
123
|
+
* @returns the body's result; a release failure is recorded, never thrown over it.
|
|
124
|
+
*/
|
|
125
|
+
async runExclusive(scope, body) {
|
|
126
|
+
if (CONTEXT.getStore()?.owner === this) throw new AuthorityReentryError(scope);
|
|
127
|
+
const previous = this.tail;
|
|
128
|
+
let releaseNext = () => {};
|
|
129
|
+
this.tail = new Promise((resolve) => {
|
|
130
|
+
releaseNext = resolve;
|
|
131
|
+
});
|
|
132
|
+
await previous;
|
|
133
|
+
try {
|
|
134
|
+
return await this.acquireAndRun(scope, body);
|
|
135
|
+
} finally {
|
|
136
|
+
releaseNext();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async acquireAndRun(scope, body) {
|
|
140
|
+
const lock = this.options.lock ?? withFileLock;
|
|
141
|
+
await mkdir(dirname(this.lockBase), { recursive: true });
|
|
142
|
+
let outcome;
|
|
143
|
+
let releaseError;
|
|
144
|
+
try {
|
|
145
|
+
await lock(this.lockBase, async () => {
|
|
146
|
+
this.held.set(scope, 1);
|
|
147
|
+
try {
|
|
148
|
+
outcome = {
|
|
149
|
+
landed: true,
|
|
150
|
+
value: await CONTEXT.run({
|
|
151
|
+
owner: this,
|
|
152
|
+
scopes: new Set([scope])
|
|
153
|
+
}, body)
|
|
154
|
+
};
|
|
155
|
+
} catch (error) {
|
|
156
|
+
outcome = {
|
|
157
|
+
landed: false,
|
|
158
|
+
error
|
|
159
|
+
};
|
|
160
|
+
} finally {
|
|
161
|
+
this.held.delete(scope);
|
|
162
|
+
}
|
|
163
|
+
}, { waitMs: this.options.waitMs ?? DEFAULT_WAIT_MS });
|
|
164
|
+
} catch (error) {
|
|
165
|
+
releaseError = error;
|
|
166
|
+
}
|
|
167
|
+
if (outcome === void 0) {
|
|
168
|
+
const error = releaseError === void 0 ? /* @__PURE__ */ new Error("authority: the lock body never ran") : releaseError;
|
|
169
|
+
throw isBusy(error) ? new AuthorityBusyError(asError(error).message) : asError(error);
|
|
170
|
+
}
|
|
171
|
+
if (releaseError !== void 0) this.pending = {
|
|
172
|
+
scope,
|
|
173
|
+
error: asError(releaseError)
|
|
174
|
+
};
|
|
175
|
+
if (outcome.landed) return outcome.value;
|
|
176
|
+
throw outcome.error;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Hold the home lock until {@link AuthorityLease.release}. The platform lock is callback-scoped,
|
|
180
|
+
* so the lease parks the callback on a gate while the caller works.
|
|
181
|
+
* @param scope - the write scope.
|
|
182
|
+
* @returns the lease.
|
|
183
|
+
*/
|
|
184
|
+
async lease(scope = "tree") {
|
|
185
|
+
let enter = () => {};
|
|
186
|
+
const entered = new Promise((resolve) => {
|
|
187
|
+
enter = resolve;
|
|
188
|
+
});
|
|
189
|
+
let leave = () => {};
|
|
190
|
+
const gate = new Promise((resolve) => {
|
|
191
|
+
leave = resolve;
|
|
192
|
+
});
|
|
193
|
+
let failure;
|
|
194
|
+
const running = this.runExclusive(scope, async () => {
|
|
195
|
+
enter();
|
|
196
|
+
await gate;
|
|
197
|
+
}).then(() => void 0, (error) => {
|
|
198
|
+
failure = asError(error);
|
|
199
|
+
});
|
|
200
|
+
if (await Promise.race([entered.then(() => "entered"), running.then(() => "ended")]) === "ended") throw failure ?? /* @__PURE__ */ new Error("authority: the lease ended before it was granted");
|
|
201
|
+
return {
|
|
202
|
+
assertHeld: () => this.assertHeld(scope),
|
|
203
|
+
release: async () => {
|
|
204
|
+
leave();
|
|
205
|
+
await running;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Remove a lock file left behind by a failed release, when no live process holds it.
|
|
211
|
+
* @returns `repaired` when a stale lock was removed, `noop` when nothing needed removing.
|
|
212
|
+
*/
|
|
213
|
+
async repair() {
|
|
214
|
+
if (this.pending === void 0 || this.held.size > 0) return "noop";
|
|
215
|
+
const lockPath = this.lockBase + ".lock";
|
|
216
|
+
let holder;
|
|
217
|
+
try {
|
|
218
|
+
const raw = await readFile(lockPath, "utf8");
|
|
219
|
+
const pid = Number.parseInt(raw.trim(), 10);
|
|
220
|
+
holder = Number.isInteger(pid) && pid > 0 ? pid : void 0;
|
|
221
|
+
} catch (error) {
|
|
222
|
+
if (error.code !== "ENOENT") throw error;
|
|
223
|
+
holder = void 0;
|
|
224
|
+
}
|
|
225
|
+
if (holder !== void 0 && holder !== process.pid && isProcessAlive(holder)) return "noop";
|
|
226
|
+
await rm(lockPath, { force: true });
|
|
227
|
+
this.pending = void 0;
|
|
228
|
+
return "repaired";
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
/** Adapt an authority lease to the `TreeLock` handle older call sites use. */
|
|
232
|
+
function leaseAsTreeLock(lease) {
|
|
233
|
+
return {
|
|
234
|
+
token: "authority",
|
|
235
|
+
epoch: 0,
|
|
236
|
+
path: "authority:tree",
|
|
237
|
+
acquiredAt: Date.now(),
|
|
238
|
+
assertHeld: async () => lease.assertHeld(),
|
|
239
|
+
release: async () => {
|
|
240
|
+
await lease.release();
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
//#endregion
|
|
245
|
+
//#region lib/types/state-store.js
|
|
246
|
+
/**
|
|
247
|
+
* Evolution home path helpers: the DSH root and `$DSH_HOME/evolution` for
|
|
248
|
+
* plugin-owned sidecar state (reports, activity store, feedback file,
|
|
249
|
+
* state-domain data).
|
|
250
|
+
*
|
|
251
|
+
* C-10: PATH HELPERS ONLY — despite the file name there is no store
|
|
252
|
+
* here. Durable evolution state lives in the state stack (evolution-state over
|
|
253
|
+
* evolution-state-domain); skills and memories live in skill-store.ts
|
|
254
|
+
* / memory-store.ts. The file name is kept deliberately: renaming it would
|
|
255
|
+
* touch every family import for zero behavior change, and the audit records
|
|
256
|
+
* the mismatch as known naming debt.
|
|
257
|
+
*/
|
|
258
|
+
/**
|
|
259
|
+
* DSH home root: `$DSH_HOME` or `~/.dsh`. Single source of the empty-string
|
|
260
|
+
* fallback — an EMPTY or WHITESPACE-ONLY DSH_HOME resolves to the default
|
|
261
|
+
* home, never to a CWD-relative path (0.3.19 W1.3, 0.3.22 F-207);
|
|
262
|
+
* V8-06 (0.3.47) extends the guard to whitespace.
|
|
263
|
+
* C-11 correction (S2.2, PLAN 2026-09-16): upstream `resolveDshHome`
|
|
264
|
+
* (util/home-paths) uses `trim()` ONLY as the ADOPTION test and then uses the
|
|
265
|
+
* RAW env value — the earlier "same trimmed source" form returned the trimmed
|
|
266
|
+
* text, so `DSH_HOME=" /x "` produced `/x` where upstream produced the literal
|
|
267
|
+
* padded path. The value semantics now match upstream line for line:
|
|
268
|
+
* `const selected = configured ?? (fromEnv !== undefined &&
|
|
269
|
+
* fromEnv.trim().length > 0 ? fromEnv : defaultDshHome())`.
|
|
270
|
+
* OPT-27 (2026-09, plan D5 — accepted): the v10-era "no `~` expansion, no
|
|
271
|
+
* resolve" divergence from upstream `resolveDshHome` is RETIRED. It became
|
|
272
|
+
* load-bearing when the skill-catalog shadow made "same tree as the upstream
|
|
273
|
+
* `USER_DSH_RANK` provider" a hard contract: upstream watches the EXPANDED
|
|
274
|
+
* absolute `<home>/skills` while this value fed a literal `~/x` (a directory
|
|
275
|
+
* named `~` under the host CWD) or a CWD-relative path — split-brain skill
|
|
276
|
+
* trees, preset installs the platform never reads, doctor probes of a
|
|
277
|
+
* directory nothing serves.
|
|
278
|
+
* S2.2 (PLAN 2026-09-16), final correction: the result is ALWAYS
|
|
279
|
+
* `resolve(expandHomePath(selected))` — the earlier form returned an
|
|
280
|
+
* already-absolute value VERBATIM, so a trailing slash or `..` segment
|
|
281
|
+
* landed unnormalized while upstream normalizes every value. Relative
|
|
282
|
+
* `DSH_HOME` values change landing spot; a CLEAN absolute home stays
|
|
283
|
+
* byte-identical (resolve is a no-op on it).
|
|
284
|
+
*/
|
|
285
|
+
function evolutionRoot(env = process.env) {
|
|
286
|
+
const fromEnv = env.DSH_HOME;
|
|
287
|
+
const selected = fromEnv !== void 0 && fromEnv.trim().length > 0 ? fromEnv : join(homedir(), ".dsh");
|
|
288
|
+
return resolve(selected === "~" ? homedir() : selected.startsWith("~/") || selected.startsWith("~\\") ? join(homedir(), selected.slice(2)) : selected);
|
|
289
|
+
}
|
|
290
|
+
/** Evolution home path helper: `$DSH_HOME/evolution` for plugin-owned sidecar
|
|
291
|
+
* state (reports, activity store, feedback file, state-domain data). */
|
|
292
|
+
function evolutionHome(env = process.env) {
|
|
293
|
+
return join(evolutionRoot(env), "evolution");
|
|
294
|
+
}
|
|
295
|
+
//#endregion
|
|
296
|
+
//#region lib/types/home-fence.js
|
|
297
|
+
/**
|
|
298
|
+
* Lease adapter over the one HomeAuthority (W9-authority).
|
|
299
|
+
*
|
|
300
|
+
* The fence owns no lock protocol of its own any more: cross-process exclusion is the platform
|
|
301
|
+
* writer lock inside {@link HomeAuthority}, and this class only keeps the family's historical
|
|
302
|
+
* `run`/`acquire` API so call sites (mutation units, the commit shell) keep working. A failed
|
|
303
|
+
* release is recorded on the authority instead of poisoning every later writer.
|
|
304
|
+
* @module @lmzhen/dsh-evolution-core/home-fence
|
|
305
|
+
*/
|
|
306
|
+
var HomeFenceError = class extends Error {
|
|
307
|
+
reason;
|
|
308
|
+
constructor(reason, message) {
|
|
309
|
+
super(message);
|
|
310
|
+
this.name = "HomeFenceError";
|
|
311
|
+
this.reason = reason;
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
/** A lock view for bodies that only need `assertHeld`; the authority releases on scope exit. */
|
|
315
|
+
function lockView(authority) {
|
|
316
|
+
return {
|
|
317
|
+
token: "authority",
|
|
318
|
+
epoch: 0,
|
|
319
|
+
path: "authority:tree",
|
|
320
|
+
acquiredAt: Date.now(),
|
|
321
|
+
assertHeld: async () => authority.assertHeld("tree"),
|
|
322
|
+
release: async () => {}
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
function classify(error) {
|
|
326
|
+
return error instanceof AuthorityBusyError ? new HomeFenceError("busy", error.message) : error;
|
|
327
|
+
}
|
|
328
|
+
/** The family's home-level writer queue, backed by the one authority. */
|
|
329
|
+
var HomeFence = class {
|
|
330
|
+
authority;
|
|
331
|
+
constructor(options) {
|
|
332
|
+
this.authority = options.authority ?? new HomeAuthority({
|
|
333
|
+
home: options.home,
|
|
334
|
+
...options.waitMs === void 0 ? {} : { waitMs: options.waitMs }
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
/** The last release failure, if any; diagnostics only. */
|
|
338
|
+
get releaseFailure() {
|
|
339
|
+
return this.authority.releaseFailure;
|
|
340
|
+
}
|
|
341
|
+
async run(body) {
|
|
342
|
+
try {
|
|
343
|
+
return await this.authority.runExclusive("tree", async () => await body(lockView(this.authority)));
|
|
344
|
+
} catch (error) {
|
|
345
|
+
throw classify(error);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
async acquire() {
|
|
349
|
+
try {
|
|
350
|
+
const lease = await this.authority.lease("tree");
|
|
351
|
+
return {
|
|
352
|
+
lock: lockView(this.authority),
|
|
353
|
+
release: async () => {
|
|
354
|
+
await lease.release();
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
} catch (error) {
|
|
358
|
+
throw classify(error);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region lib/types/lease-store.js
|
|
364
|
+
const DEFAULT_LEASE_TTL_MS = 3e4;
|
|
365
|
+
function leaseFile(home, runId) {
|
|
366
|
+
return join(home, "leases", encodeURIComponent(runId) + ".json");
|
|
367
|
+
}
|
|
368
|
+
function parseLease(raw) {
|
|
369
|
+
if (raw === null) return void 0;
|
|
370
|
+
try {
|
|
371
|
+
const value = JSON.parse(raw);
|
|
372
|
+
if (typeof value.id !== "string" || typeof value.runId !== "string" || typeof value.owner !== "string" || typeof value.pid !== "number" || typeof value.epoch !== "number" || typeof value.issuedAt !== "number" || typeof value.expiresAt !== "number" || typeof value.heartbeatAt !== "number" || value.state !== "active" && value.state !== "released") return void 0;
|
|
373
|
+
return value;
|
|
374
|
+
} catch {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
async function transact(io, path, task) {
|
|
379
|
+
if (io.transact !== void 0) {
|
|
380
|
+
await io.transact(path, task);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
const current = await io.readText(path);
|
|
384
|
+
const next = task(current);
|
|
385
|
+
if (next === null) await io.remove(path);
|
|
386
|
+
else if (next !== current) await io.writeText(path, next);
|
|
387
|
+
}
|
|
388
|
+
function tokenOf(record) {
|
|
389
|
+
return {
|
|
390
|
+
id: record.id,
|
|
391
|
+
owner: record.owner,
|
|
392
|
+
runId: record.runId,
|
|
393
|
+
epoch: record.epoch
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
/** Durable single-host lease store. One active lease per runId; expired leases are reclaimable. */
|
|
397
|
+
var LeaseStore = class {
|
|
398
|
+
io;
|
|
399
|
+
home;
|
|
400
|
+
ttlMs;
|
|
401
|
+
now;
|
|
402
|
+
pid;
|
|
403
|
+
constructor(options) {
|
|
404
|
+
this.io = options.io;
|
|
405
|
+
this.home = options.home;
|
|
406
|
+
this.ttlMs = options.ttlMs ?? 3e4;
|
|
407
|
+
this.now = options.now ?? Date.now;
|
|
408
|
+
this.pid = options.pid ?? process.pid;
|
|
409
|
+
}
|
|
410
|
+
path(runId) {
|
|
411
|
+
if (runId.trim() === "") throw new Error("lease runId must not be empty");
|
|
412
|
+
return leaseFile(this.home, runId);
|
|
413
|
+
}
|
|
414
|
+
async acquire(owner, runId, ttlMs = this.ttlMs) {
|
|
415
|
+
if (ttlMs <= 0) throw new Error("lease ttl must be positive");
|
|
416
|
+
const path = this.path(runId);
|
|
417
|
+
const now = this.now();
|
|
418
|
+
let created;
|
|
419
|
+
let holder;
|
|
420
|
+
await transact(this.io, path, (raw) => {
|
|
421
|
+
const current = parseLease(raw);
|
|
422
|
+
if (current !== void 0 && current.state === "active" && now < current.expiresAt) {
|
|
423
|
+
holder = current;
|
|
424
|
+
return raw;
|
|
425
|
+
}
|
|
426
|
+
created = {
|
|
427
|
+
id: randomUUID(),
|
|
428
|
+
owner,
|
|
429
|
+
runId,
|
|
430
|
+
pid: this.pid,
|
|
431
|
+
epoch: (current?.epoch ?? 0) + 1,
|
|
432
|
+
issuedAt: now,
|
|
433
|
+
expiresAt: now + ttlMs,
|
|
434
|
+
heartbeatAt: now,
|
|
435
|
+
state: "active"
|
|
436
|
+
};
|
|
437
|
+
return JSON.stringify(created);
|
|
438
|
+
});
|
|
439
|
+
if (created !== void 0) return {
|
|
440
|
+
ok: true,
|
|
441
|
+
lease: created,
|
|
442
|
+
token: tokenOf(created)
|
|
443
|
+
};
|
|
444
|
+
if (holder !== void 0) return {
|
|
445
|
+
ok: false,
|
|
446
|
+
reason: "busy",
|
|
447
|
+
holder
|
|
448
|
+
};
|
|
449
|
+
throw new Error("lease acquire did not create or observe a record");
|
|
450
|
+
}
|
|
451
|
+
async get(runId) {
|
|
452
|
+
return parseLease(await this.io.readText(this.path(runId))) ?? null;
|
|
453
|
+
}
|
|
454
|
+
async active(runId) {
|
|
455
|
+
const record = await this.get(runId);
|
|
456
|
+
return record !== null && record.state === "active" && this.now() < record.expiresAt ? record : null;
|
|
457
|
+
}
|
|
458
|
+
async renew(token, ttlMs = this.ttlMs) {
|
|
459
|
+
const path = this.path(token.runId);
|
|
460
|
+
let next = null;
|
|
461
|
+
await transact(this.io, path, (raw) => {
|
|
462
|
+
const current = parseLease(raw);
|
|
463
|
+
if (current === void 0 || current.state !== "active" || current.id !== token.id || current.epoch !== token.epoch) return raw;
|
|
464
|
+
const now = this.now();
|
|
465
|
+
next = {
|
|
466
|
+
...current,
|
|
467
|
+
heartbeatAt: now,
|
|
468
|
+
expiresAt: now + ttlMs
|
|
469
|
+
};
|
|
470
|
+
return JSON.stringify(next);
|
|
471
|
+
});
|
|
472
|
+
return next;
|
|
473
|
+
}
|
|
474
|
+
async release(token) {
|
|
475
|
+
const path = this.path(token.runId);
|
|
476
|
+
await transact(this.io, path, (raw) => {
|
|
477
|
+
const current = parseLease(raw);
|
|
478
|
+
if (current === void 0 || current.id !== token.id || current.epoch !== token.epoch) return raw;
|
|
479
|
+
return JSON.stringify({
|
|
480
|
+
...current,
|
|
481
|
+
state: "released",
|
|
482
|
+
heartbeatAt: this.now()
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
//#endregion
|
|
488
|
+
//#region lib/types/mutation-authority.js
|
|
489
|
+
/**
|
|
490
|
+
* The family's write authority. It owns the one home fence and the per-run lease store.
|
|
491
|
+
*
|
|
492
|
+
* The lease is a per-run mutex for long-running review/approval work; foreground writes
|
|
493
|
+
* have no run identity and are serialized by the fence. The former apply/transact/recover
|
|
494
|
+
* facade claimed to gate every write on a lease but had no production caller, so it was
|
|
495
|
+
* removed rather than kept as a false narrow waist.
|
|
496
|
+
*/
|
|
497
|
+
var MutationAuthority = class {
|
|
498
|
+
leases;
|
|
499
|
+
fence;
|
|
500
|
+
/** Lease primitives for run/claim owners (review, approval). */
|
|
501
|
+
get lease() {
|
|
502
|
+
return this.leases;
|
|
503
|
+
}
|
|
504
|
+
/** The one home fence domain unit engines must reuse instead of taking tree-lock themselves. */
|
|
505
|
+
get homeFence() {
|
|
506
|
+
return this.fence;
|
|
507
|
+
}
|
|
508
|
+
/** Adapter for MutationUnit call sites that accept a lock port. It is the fence, no more. */
|
|
509
|
+
get mutationLock() {
|
|
510
|
+
return { acquire: async () => {
|
|
511
|
+
const lease = await this.fence.acquire();
|
|
512
|
+
return { release: async () => {
|
|
513
|
+
await lease.release();
|
|
514
|
+
} };
|
|
515
|
+
} };
|
|
516
|
+
}
|
|
517
|
+
constructor(options) {
|
|
518
|
+
this.leases = options.leases ?? new LeaseStore({
|
|
519
|
+
io: options.io,
|
|
520
|
+
home: options.home,
|
|
521
|
+
...options.now === void 0 ? {} : { now: options.now }
|
|
522
|
+
});
|
|
523
|
+
this.fence = options.fence ?? new HomeFence({
|
|
524
|
+
io: options.io,
|
|
525
|
+
home: options.home,
|
|
526
|
+
...options.waitMs === void 0 ? {} : { waitMs: options.waitMs },
|
|
527
|
+
...options.now === void 0 ? {} : { now: options.now }
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
/** Read the authority's fence adapter from a context; undefined means no authority is mounted. */
|
|
532
|
+
function mutationLockFrom(ctx) {
|
|
533
|
+
if (ctx?.get === void 0) return void 0;
|
|
534
|
+
return ctx.get("evolutionMutationAuthority")?.mutationLock;
|
|
535
|
+
}
|
|
536
|
+
//#endregion
|
|
537
|
+
export { leaseFile as a, evolutionHome as c, AuthorityOrderError as d, AuthorityReentryError as f, leaseAsTreeLock as h, LeaseStore as i, evolutionRoot as l, authorityHolder as m, mutationLockFrom as n, HomeFence as o, HomeAuthority as p, DEFAULT_LEASE_TTL_MS as r, HomeFenceError as s, MutationAuthority as t, AuthorityBusyError as u };
|