@lmzhen/dsh-evolution-core 0.44.0 → 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 +54 -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/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/package.json +25 -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
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { S as probeText, h as isUnknown, i as isProcessAlive, m as isPresent, r as isCommittedWarning, s as transactIo } from "./io-AmUf4lVY.js";
|
|
2
|
+
import { r as contentHash } from "./mutations-DwvbjRHF.js";
|
|
2
3
|
import { basename, dirname, isAbsolute, join, relative } from "node:path";
|
|
3
4
|
import { createHash, randomUUID } from "node:crypto";
|
|
4
5
|
//#region lib/types/write-kernel.js
|
package/lib/skills.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { C as probeUnknown, S as probeText, _ as probeAbsent, a as nodeEvolutionIo, b as probePresent, f as isAbsent, h as isUnknown, m as isPresent, r as isCommittedWarning, s as transactIo, v as probeList, x as probeReason, y as probeMtime } from "./io-AmUf4lVY.js";
|
|
2
|
+
import { l as evolutionRoot, n as mutationLockFrom } from "./mutation-authority-DeJ_Wz14.js";
|
|
1
3
|
import { q as resolveCitations } from "./session-projection-EB7mUOlJ.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { X as SKILL_NAME_RE, Z as SUPPORT_DIRS, a as DEFAULT_SKILL_LIMITS, c as policyStageLimits, d as CONTENT_SPLIT_HINT, i as DEFAULT_REFERENCE_REWRITE_POLICY, n as DEFAULT_ARCHIVE_RETENTION_POLICY, o as DEFAULT_SUPPORT_FILE_CHAR_POLICY, q as PROTECTED_BUILTIN_SKILLS, r as DEFAULT_CITATION_POLICY, s as POLICY_STAGE_DEFAULTS } from "./numeric-DLeLvvGb.js";
|
|
5
|
+
import { At as normalizeFrontmatter, Bt as describeReferenceRewrite, D as appendEvolutionEvent, Dt as exceedsContentLimit, Et as ensureSkillFrontmatter, F as RULES, Ft as validateFrontmatter, Gt as isUnreadWrite, Ht as planRehoming, I as countReferenceFiles, It as yamlPlainScalarNeedsQuotes, Kt as sessionReadSkillNames, L as diffFindings, Mt as relatedSkillNames, Nt as shrinksOverLimit, O as eventsFile, Ot as frontmatterBlock, Pt as skillMdOnDisk, R as lintSkill, Rt as supportRefs, S as trimPatternBoundaries, Tt as authoringFeedback, Ut as READ_REQUIRED_ACTIONS, Vt as planReferenceRewrite, Wt as filterUnreadSkillOps, Xt as authorityFrom, a as lintBody, bt as selectSections, ct as loadUsage, gt as usageFile, h as scanContentThreats, i as assessStructureHealth, it as getRecord, jt as parseFrontmatter, kt as frontmatterCatalogInvalid, l as isReviewChannelSession, mt as suppressedFile, n as HEALTH_STAMP_RE, o as currentReviewContext, qt as sessionReadSupportFiles, r as MIN_STAMP_BODY_CHARS, t as DEFAULT_HEALTH_THRESHOLDS, ut as mutateUsage, v as makeSerialQueue, x as fuzzyPatch, yt as projectExtractBodies, zt as applyReferenceRewrite } from "./skill-health-D1pZGk5V.js";
|
|
6
|
+
import { i as loadMutations, o as recordMutation, r as contentHash } from "./mutations-DwvbjRHF.js";
|
|
7
|
+
import { A as unitJournalPort, C as readHistoryIndex, D as versionSummaryPrompt, E as versionActionKind, O as versionTarget, S as partitionVersions, T as textDiffFacts, _ as loadVersions, a as RETENTION_FEEDBACK_MIN_CHARS, b as orderVersions, c as VERSION_SUMMARY_MAX_CHARS, d as elapsedSince, et as MutationUnit, f as entryTarget, g as loadVersionContent, h as latestVersionAt, i as RETENTION_FEEDBACK_KEEP_RATIO, j as unitSnapshotPort, k as pruneTreeChangeUnits, l as blobPath, m as historyRoot, n as HISTORY_DIR, o as TEXT_DIFF_MAX_CHARS, p as historyIndexFile, r as HISTORY_INDEX_VERSION, s as TEXT_DIFF_MAX_LINES, t as BASELINE_ACTION, tt as fileMutationLock, u as contentRetentionFeedback, v as nextHistoryIndex, w as recordVersions, x as parseHistoryIndex, y as normalizeVersionSummary } from "./skill-history-CjypdYqv.js";
|
|
5
8
|
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
6
9
|
//#region lib/types/skill-row-cells.js
|
|
7
10
|
/**
|
package/lib/subagent/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { h as restrictedPresetId, n as ensureRestrictedPreset, r as restrictedPresetState } from "../ensure-
|
|
1
|
+
import { h as restrictedPresetId, n as ensureRestrictedPreset, r as restrictedPresetState } from "../ensure-C5Mo1dbB.js";
|
|
2
2
|
//#region lib/types/subagent/readiness.js
|
|
3
3
|
function serviceOf$1(ctx, name) {
|
|
4
4
|
return ctx.get(name);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import { MutationAuthority } from './mutation-authority.ts';
|
|
3
|
+
declare module '@deepseek-ai/cordis' {
|
|
4
|
+
interface Context {
|
|
5
|
+
evolutionMutationAuthority: MutationAuthority;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare const name = "evolution-authority";
|
|
9
|
+
export declare const inject: string[];
|
|
10
|
+
/** Expose the single home/run-fenced mutation authority to domain packages. */
|
|
11
|
+
export declare function apply(ctx: Context): void;
|
|
12
|
+
//# sourceMappingURL=authority-plugin.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { evolutionHome } from "./state-store.js";
|
|
2
|
+
import { MutationAuthority } from "./mutation-authority.js";
|
|
3
|
+
export const name = 'evolution-authority';
|
|
4
|
+
export const inject = ['evolutionIo'];
|
|
5
|
+
/** Expose the single home/run-fenced mutation authority to domain packages. */
|
|
6
|
+
export function apply(ctx) {
|
|
7
|
+
const authority = new MutationAuthority({ io: ctx.evolutionIo.provider(), home: evolutionHome() });
|
|
8
|
+
ctx.provide('evolutionMutationAuthority', authority);
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=authority-plugin.js.map
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { withFileLock as platformFileLock } from '@deepseek-ai/dsh-atomic-write';
|
|
2
|
+
/** The lock handle a serialized body receives; `release` is a no-op when a scope owns it. */
|
|
3
|
+
export interface TreeLock {
|
|
4
|
+
readonly token: string;
|
|
5
|
+
readonly epoch: number;
|
|
6
|
+
readonly path: string;
|
|
7
|
+
readonly acquiredAt: number;
|
|
8
|
+
release(): Promise<void>;
|
|
9
|
+
assertHeld(): Promise<boolean>;
|
|
10
|
+
}
|
|
11
|
+
/** Who holds the home lock right now, for doctor rows; `undefined` means free or unreadable. */
|
|
12
|
+
export declare function authorityHolder(home: string): Promise<{
|
|
13
|
+
readonly pid: number;
|
|
14
|
+
} | undefined>;
|
|
15
|
+
/** The write scopes the family serializes. `tree` is the outer scope, `state` the inner one. */
|
|
16
|
+
export type LockScope = 'tree' | 'state';
|
|
17
|
+
/** The platform lock timed out: another writer holds the home. */
|
|
18
|
+
export declare class AuthorityBusyError extends Error {
|
|
19
|
+
constructor(message: string);
|
|
20
|
+
}
|
|
21
|
+
/** A caller took a scope it already holds without declaring the nesting. */
|
|
22
|
+
export declare class AuthorityReentryError extends Error {
|
|
23
|
+
constructor(scope: LockScope);
|
|
24
|
+
}
|
|
25
|
+
/** A caller took the outer scope while already holding the inner one. */
|
|
26
|
+
export declare class AuthorityOrderError extends Error {
|
|
27
|
+
constructor(outer: LockScope, inner: LockScope);
|
|
28
|
+
}
|
|
29
|
+
export interface HomeAuthorityOptions {
|
|
30
|
+
/** Evolution home; the lock lives under `<home>/.authority/`. */
|
|
31
|
+
readonly home: string;
|
|
32
|
+
/** How long to wait for the home lock. Default 5000 ms. */
|
|
33
|
+
readonly waitMs?: number;
|
|
34
|
+
/** Test seam: the platform lock primitive. Production omits it. */
|
|
35
|
+
readonly lock?: typeof platformFileLock;
|
|
36
|
+
}
|
|
37
|
+
/** A release that failed after the body ran; never a reason to report the write as failed. */
|
|
38
|
+
export interface AuthorityReleaseFailure {
|
|
39
|
+
readonly scope: LockScope;
|
|
40
|
+
readonly error: Error;
|
|
41
|
+
}
|
|
42
|
+
/** A held home lock in lease form, for call sites that span several awaits. */
|
|
43
|
+
export interface AuthorityLease {
|
|
44
|
+
assertHeld(): boolean;
|
|
45
|
+
/** Resolves after the home lock is released; a release failure is recorded, not thrown. */
|
|
46
|
+
release(): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* One home-wide writer authority. Every durable writer calls {@link HomeAuthority.runExclusive};
|
|
50
|
+
* an operation that runs inside another held operation calls {@link HomeAuthority.runNested}.
|
|
51
|
+
*/
|
|
52
|
+
export declare class HomeAuthority {
|
|
53
|
+
private readonly options;
|
|
54
|
+
private readonly held;
|
|
55
|
+
private tail;
|
|
56
|
+
private pending;
|
|
57
|
+
constructor(options: HomeAuthorityOptions);
|
|
58
|
+
/** The lock path the platform primitive writes its `<path>.lock` beside. */
|
|
59
|
+
private get lockBase();
|
|
60
|
+
/** The last release failure, if any; diagnostics only. */
|
|
61
|
+
get releaseFailure(): AuthorityReleaseFailure | undefined;
|
|
62
|
+
/** True while the current call context (or this instance) holds `scope`. */
|
|
63
|
+
assertHeld(scope: LockScope): boolean;
|
|
64
|
+
/** True while this instance holds the home lock for any scope. */
|
|
65
|
+
get holding(): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Run `body` inside a held operation: order-checked, no lock acquisition.
|
|
68
|
+
* @param scope - the nested scope; must not be an outer scope of the held one.
|
|
69
|
+
* @param body - the operation to run inline.
|
|
70
|
+
* @returns the body's result.
|
|
71
|
+
*/
|
|
72
|
+
runNested<T>(scope: LockScope, body: () => Promise<T>): Promise<T>;
|
|
73
|
+
/**
|
|
74
|
+
* Run `body` while holding the home lock, in invocation order among this instance's callers.
|
|
75
|
+
* @param scope - the write scope; `tree` must be taken before `state`.
|
|
76
|
+
* @param body - the operation to serialize.
|
|
77
|
+
* @returns the body's result; a release failure is recorded, never thrown over it.
|
|
78
|
+
*/
|
|
79
|
+
runExclusive<T>(scope: LockScope, body: () => Promise<T>): Promise<T>;
|
|
80
|
+
private acquireAndRun;
|
|
81
|
+
/**
|
|
82
|
+
* Hold the home lock until {@link AuthorityLease.release}. The platform lock is callback-scoped,
|
|
83
|
+
* so the lease parks the callback on a gate while the caller works.
|
|
84
|
+
* @param scope - the write scope.
|
|
85
|
+
* @returns the lease.
|
|
86
|
+
*/
|
|
87
|
+
lease(scope?: LockScope): Promise<AuthorityLease>;
|
|
88
|
+
/**
|
|
89
|
+
* Remove a lock file left behind by a failed release, when no live process holds it.
|
|
90
|
+
* @returns `repaired` when a stale lock was removed, `noop` when nothing needed removing.
|
|
91
|
+
*/
|
|
92
|
+
repair(): Promise<'noop' | 'repaired'>;
|
|
93
|
+
}
|
|
94
|
+
/** Adapt an authority lease to the `TreeLock` handle older call sites use. */
|
|
95
|
+
export declare function leaseAsTreeLock(lease: AuthorityLease): TreeLock;
|
|
96
|
+
//# sourceMappingURL=authority.d.ts.map
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The family's one mutual-exclusion entry (W9-authority, invariant I1).
|
|
3
|
+
*
|
|
4
|
+
* Cross-process exclusion is the platform's writer lock (`withFileLock` from
|
|
5
|
+
* `@deepseek-ai/dsh-atomic-write`): one lock per evolution home, so every scope shares it and two
|
|
6
|
+
* scopes can never deadlock against each other. In-process callers queue FIFO through a promise
|
|
7
|
+
* tail, so writers run in invocation order instead of racing for the platform lock.
|
|
8
|
+
*
|
|
9
|
+
* `scope` carries the nesting order only (`tree` outside, `state` inside — the merge shell writes
|
|
10
|
+
* state while it holds the tree). Re-entrancy is explicit: `runNested` declares "this operation is
|
|
11
|
+
* inside a held one", and the held context is tracked per authority instance so a second authority
|
|
12
|
+
* for the same home is not mistaken for a re-entry. An undeclared re-entry throws instead of
|
|
13
|
+
* waiting on the platform lock forever.
|
|
14
|
+
*
|
|
15
|
+
* A failed release never masks the body's outcome: an operation whose write already landed keeps
|
|
16
|
+
* its result, and the failure is recorded for {@link HomeAuthority.releaseFailure} and `repair()`.
|
|
17
|
+
* @module @lmzhen/dsh-evolution-core/authority
|
|
18
|
+
*/
|
|
19
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
20
|
+
import { mkdir, readFile, rm } from 'node:fs/promises';
|
|
21
|
+
import { dirname, join } from 'node:path';
|
|
22
|
+
import { withFileLock as platformFileLock } from '@deepseek-ai/dsh-atomic-write';
|
|
23
|
+
import { isProcessAlive } from "./io.js";
|
|
24
|
+
/** Who holds the home lock right now, for doctor rows; `undefined` means free or unreadable. */
|
|
25
|
+
export async function authorityHolder(home) {
|
|
26
|
+
try {
|
|
27
|
+
const raw = await readFile(join(home, '.authority', 'home.lock'), 'utf8');
|
|
28
|
+
const pid = Number.parseInt(raw.trim(), 10);
|
|
29
|
+
return Number.isInteger(pid) && pid > 0 ? { pid } : undefined;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
// ENOENT means free; anything else means we could not read the lock and must not call it free.
|
|
33
|
+
if (error.code === 'ENOENT')
|
|
34
|
+
return undefined;
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const ORDER = { tree: 0, state: 1 };
|
|
39
|
+
const DEFAULT_WAIT_MS = 5_000;
|
|
40
|
+
const CONTEXT = new AsyncLocalStorage();
|
|
41
|
+
/** The platform lock timed out: another writer holds the home. */
|
|
42
|
+
export class AuthorityBusyError extends Error {
|
|
43
|
+
constructor(message) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = 'AuthorityBusyError';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** A caller took a scope it already holds without declaring the nesting. */
|
|
49
|
+
export class AuthorityReentryError extends Error {
|
|
50
|
+
constructor(scope) {
|
|
51
|
+
super('authority: "' + scope + '" would be re-entered; use runNested() inside a held operation');
|
|
52
|
+
this.name = 'AuthorityReentryError';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** A caller took the outer scope while already holding the inner one. */
|
|
56
|
+
export class AuthorityOrderError extends Error {
|
|
57
|
+
constructor(outer, inner) {
|
|
58
|
+
super('lock order violation: scope "' + inner + '" is held but "' + outer + '" was requested; take tree before state');
|
|
59
|
+
this.name = 'AuthorityOrderError';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function asError(error) {
|
|
63
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
64
|
+
}
|
|
65
|
+
/** True when the platform lock refused within its wait budget. */
|
|
66
|
+
function isBusy(error) {
|
|
67
|
+
return error instanceof Error && /timed out waiting for the writer lock/.test(error.message);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* One home-wide writer authority. Every durable writer calls {@link HomeAuthority.runExclusive};
|
|
71
|
+
* an operation that runs inside another held operation calls {@link HomeAuthority.runNested}.
|
|
72
|
+
*/
|
|
73
|
+
export class HomeAuthority {
|
|
74
|
+
options;
|
|
75
|
+
held = new Map();
|
|
76
|
+
tail = Promise.resolve();
|
|
77
|
+
pending;
|
|
78
|
+
constructor(options) {
|
|
79
|
+
this.options = options;
|
|
80
|
+
}
|
|
81
|
+
/** The lock path the platform primitive writes its `<path>.lock` beside. */
|
|
82
|
+
get lockBase() {
|
|
83
|
+
return join(this.options.home, '.authority', 'home');
|
|
84
|
+
}
|
|
85
|
+
/** The last release failure, if any; diagnostics only. */
|
|
86
|
+
get releaseFailure() {
|
|
87
|
+
return this.pending;
|
|
88
|
+
}
|
|
89
|
+
/** True while the current call context (or this instance) holds `scope`. */
|
|
90
|
+
assertHeld(scope) {
|
|
91
|
+
const context = CONTEXT.getStore();
|
|
92
|
+
if (context?.owner === this)
|
|
93
|
+
return context.scopes.has(scope);
|
|
94
|
+
return (this.held.get(scope) ?? 0) > 0;
|
|
95
|
+
}
|
|
96
|
+
/** True while this instance holds the home lock for any scope. */
|
|
97
|
+
get holding() {
|
|
98
|
+
return this.held.size > 0;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Run `body` inside a held operation: order-checked, no lock acquisition.
|
|
102
|
+
* @param scope - the nested scope; must not be an outer scope of the held one.
|
|
103
|
+
* @param body - the operation to run inline.
|
|
104
|
+
* @returns the body's result.
|
|
105
|
+
*/
|
|
106
|
+
async runNested(scope, body) {
|
|
107
|
+
const context = CONTEXT.getStore();
|
|
108
|
+
if (context?.owner !== this)
|
|
109
|
+
throw new AuthorityReentryError(scope);
|
|
110
|
+
for (const outer of context.scopes) {
|
|
111
|
+
if (ORDER[outer] > ORDER[scope])
|
|
112
|
+
throw new AuthorityOrderError(outer, scope);
|
|
113
|
+
}
|
|
114
|
+
const scopes = new Set(context.scopes);
|
|
115
|
+
scopes.add(scope);
|
|
116
|
+
return await CONTEXT.run({ owner: this, scopes }, body);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Run `body` while holding the home lock, in invocation order among this instance's callers.
|
|
120
|
+
* @param scope - the write scope; `tree` must be taken before `state`.
|
|
121
|
+
* @param body - the operation to serialize.
|
|
122
|
+
* @returns the body's result; a release failure is recorded, never thrown over it.
|
|
123
|
+
*/
|
|
124
|
+
async runExclusive(scope, body) {
|
|
125
|
+
if (CONTEXT.getStore()?.owner === this)
|
|
126
|
+
throw new AuthorityReentryError(scope);
|
|
127
|
+
const previous = this.tail;
|
|
128
|
+
let releaseNext = () => { };
|
|
129
|
+
this.tail = new Promise(resolve => { releaseNext = resolve; });
|
|
130
|
+
await previous;
|
|
131
|
+
try {
|
|
132
|
+
return await this.acquireAndRun(scope, body);
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
releaseNext();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async acquireAndRun(scope, body) {
|
|
139
|
+
const lock = this.options.lock ?? platformFileLock;
|
|
140
|
+
await mkdir(dirname(this.lockBase), { recursive: true });
|
|
141
|
+
let outcome;
|
|
142
|
+
let releaseError;
|
|
143
|
+
try {
|
|
144
|
+
await lock(this.lockBase, async () => {
|
|
145
|
+
this.held.set(scope, 1);
|
|
146
|
+
try {
|
|
147
|
+
outcome = { landed: true, value: await CONTEXT.run({ owner: this, scopes: new Set([scope]) }, body) };
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
outcome = { landed: false, error };
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
this.held.delete(scope);
|
|
154
|
+
}
|
|
155
|
+
}, { waitMs: this.options.waitMs ?? DEFAULT_WAIT_MS });
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
releaseError = error;
|
|
159
|
+
}
|
|
160
|
+
if (outcome === undefined) {
|
|
161
|
+
const error = releaseError === undefined ? new Error('authority: the lock body never ran') : releaseError;
|
|
162
|
+
throw isBusy(error) ? new AuthorityBusyError(asError(error).message) : asError(error);
|
|
163
|
+
}
|
|
164
|
+
if (releaseError !== undefined)
|
|
165
|
+
this.pending = { scope, error: asError(releaseError) };
|
|
166
|
+
if (outcome.landed)
|
|
167
|
+
return outcome.value;
|
|
168
|
+
throw outcome.error;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Hold the home lock until {@link AuthorityLease.release}. The platform lock is callback-scoped,
|
|
172
|
+
* so the lease parks the callback on a gate while the caller works.
|
|
173
|
+
* @param scope - the write scope.
|
|
174
|
+
* @returns the lease.
|
|
175
|
+
*/
|
|
176
|
+
async lease(scope = 'tree') {
|
|
177
|
+
let enter = () => { };
|
|
178
|
+
const entered = new Promise(resolve => { enter = resolve; });
|
|
179
|
+
let leave = () => { };
|
|
180
|
+
const gate = new Promise(resolve => { leave = resolve; });
|
|
181
|
+
let failure;
|
|
182
|
+
const running = this.runExclusive(scope, async () => {
|
|
183
|
+
enter();
|
|
184
|
+
await gate;
|
|
185
|
+
}).then(() => undefined, (error) => { failure = asError(error); });
|
|
186
|
+
const raced = await Promise.race([
|
|
187
|
+
entered.then(() => 'entered'),
|
|
188
|
+
running.then(() => 'ended'),
|
|
189
|
+
]);
|
|
190
|
+
if (raced === 'ended')
|
|
191
|
+
throw failure ?? new Error('authority: the lease ended before it was granted');
|
|
192
|
+
return {
|
|
193
|
+
assertHeld: () => this.assertHeld(scope),
|
|
194
|
+
release: async () => {
|
|
195
|
+
leave();
|
|
196
|
+
await running;
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Remove a lock file left behind by a failed release, when no live process holds it.
|
|
202
|
+
* @returns `repaired` when a stale lock was removed, `noop` when nothing needed removing.
|
|
203
|
+
*/
|
|
204
|
+
async repair() {
|
|
205
|
+
if (this.pending === undefined || this.held.size > 0)
|
|
206
|
+
return 'noop';
|
|
207
|
+
const lockPath = this.lockBase + '.lock';
|
|
208
|
+
let holder;
|
|
209
|
+
try {
|
|
210
|
+
const raw = await readFile(lockPath, 'utf8');
|
|
211
|
+
const pid = Number.parseInt(raw.trim(), 10);
|
|
212
|
+
holder = Number.isInteger(pid) && pid > 0 ? pid : undefined;
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
if (error.code !== 'ENOENT')
|
|
216
|
+
throw error;
|
|
217
|
+
holder = undefined;
|
|
218
|
+
}
|
|
219
|
+
if (holder !== undefined && holder !== process.pid && isProcessAlive(holder))
|
|
220
|
+
return 'noop';
|
|
221
|
+
await rm(lockPath, { force: true });
|
|
222
|
+
this.pending = undefined;
|
|
223
|
+
return 'repaired';
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/** Adapt an authority lease to the `TreeLock` handle older call sites use. */
|
|
227
|
+
export function leaseAsTreeLock(lease) {
|
|
228
|
+
return {
|
|
229
|
+
token: 'authority',
|
|
230
|
+
epoch: 0,
|
|
231
|
+
path: 'authority:tree',
|
|
232
|
+
acquiredAt: Date.now(),
|
|
233
|
+
assertHeld: async () => lease.assertHeld(),
|
|
234
|
+
release: async () => { await lease.release(); },
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=authority.js.map
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-package contracts for the dsh-evolution family.
|
|
3
|
+
*
|
|
4
|
+
* This package is intentionally a library: it declares state and event shapes,
|
|
5
|
+
* not a Cordis plugin. It must not import any evolution implementation package.
|
|
6
|
+
*
|
|
7
|
+
* @module @lmzhen/dsh-evolution-core/contract
|
|
8
|
+
*/
|
|
9
|
+
/** One platform service the family probes, with the feature that degrades without it. */
|
|
10
|
+
export interface PlatformServiceProbe {
|
|
11
|
+
/** The service key as `ctx.get` spells it. */
|
|
12
|
+
readonly service: string;
|
|
13
|
+
/** The family feature that degrades when the service is absent. */
|
|
14
|
+
readonly feature: string;
|
|
15
|
+
/** Where the probe runs. */
|
|
16
|
+
readonly side: 'host' | 'client';
|
|
17
|
+
}
|
|
18
|
+
/** Versioned process-bus event payload; consumers must tolerate unknown versions. */
|
|
19
|
+
export interface EvolutionEvent<TPayload = unknown> {
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly version: 1;
|
|
22
|
+
readonly payload: TPayload;
|
|
23
|
+
}
|
|
24
|
+
/** A lifecycle service implemented by a capability package. */
|
|
25
|
+
export interface EvolutionLifecycleService {
|
|
26
|
+
/** Stable service name used in diagnostics. */
|
|
27
|
+
readonly name: string;
|
|
28
|
+
}
|
|
29
|
+
/** Legal kind of one pending write. The runtime source stays in state-storage. */
|
|
30
|
+
export type PendingKind = 'memory' | 'skill' | 'capability';
|
|
31
|
+
/** Legal status of one pending write. */
|
|
32
|
+
export type PendingStatus = 'pending' | 'executing' | 'approved' | 'rejected';
|
|
33
|
+
/** How a run ended, as reported by its owner; run-registry, audit and UI project this one shape. */
|
|
34
|
+
export interface RunOutcome {
|
|
35
|
+
state: 'succeeded' | 'failed' | 'cancelled' | 'interrupted';
|
|
36
|
+
resultRef?: string | undefined;
|
|
37
|
+
failure?: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
/** Who owns a durable mutation; foreground user work and background review are distinct authorities. */
|
|
40
|
+
export type MutationOwner = 'foreground' | 'background_review';
|
|
41
|
+
/** Per-session review counters. */
|
|
42
|
+
export interface ReviewStateRecord {
|
|
43
|
+
turnsSinceMemory: number;
|
|
44
|
+
turnsSinceSkill: number;
|
|
45
|
+
lastTurn: number;
|
|
46
|
+
}
|
|
47
|
+
/** Curator bookkeeping record. */
|
|
48
|
+
export interface CuratorStateRecord {
|
|
49
|
+
lastRunAt: number;
|
|
50
|
+
runCount: number;
|
|
51
|
+
lastSummary: string;
|
|
52
|
+
paused: boolean;
|
|
53
|
+
}
|
|
54
|
+
/** One pending write. */
|
|
55
|
+
export interface PendingRecord {
|
|
56
|
+
id: string;
|
|
57
|
+
kind: PendingKind;
|
|
58
|
+
summary: string;
|
|
59
|
+
args: unknown;
|
|
60
|
+
createdAt: string;
|
|
61
|
+
status: PendingStatus;
|
|
62
|
+
resolvedAt?: string | undefined;
|
|
63
|
+
claimedBy?: string | undefined;
|
|
64
|
+
claimedAt?: string | undefined;
|
|
65
|
+
origin?: string | undefined;
|
|
66
|
+
sessionId?: string | undefined;
|
|
67
|
+
decidedBy?: {
|
|
68
|
+
readonly channel: 'connection.fetch' | 'command';
|
|
69
|
+
readonly principal: 'owner';
|
|
70
|
+
readonly deploymentAllowsRemote: boolean;
|
|
71
|
+
} | undefined;
|
|
72
|
+
}
|
|
73
|
+
/** Result of one atomic pending transition. */
|
|
74
|
+
export interface PendingResolution {
|
|
75
|
+
record: PendingRecord | null;
|
|
76
|
+
applied: boolean;
|
|
77
|
+
}
|
|
78
|
+
/** Provider seam for durable evolution state; implementations own the medium. */
|
|
79
|
+
export interface EvolutionStateStorage {
|
|
80
|
+
readonly name: string;
|
|
81
|
+
loadReviewState(sessionId: string): Promise<ReviewStateRecord | null>;
|
|
82
|
+
saveReviewState(sessionId: string, record: ReviewStateRecord): Promise<void>;
|
|
83
|
+
loadCuratorState(): Promise<CuratorStateRecord | null>;
|
|
84
|
+
saveCuratorState(record: CuratorStateRecord): Promise<void>;
|
|
85
|
+
transactCuratorState(task: (current: CuratorStateRecord | null) => CuratorStateRecord | null): Promise<void>;
|
|
86
|
+
listPending(status?: PendingStatus): Promise<PendingRecord[]>;
|
|
87
|
+
savePending(record: PendingRecord): Promise<void>;
|
|
88
|
+
tryResolvePending(id: string, status: Exclude<PendingStatus, 'pending'>, expectedClaimId?: string): Promise<PendingResolution>;
|
|
89
|
+
claimPending(id: string, claimId: string): Promise<PendingRecord | null>;
|
|
90
|
+
releasePendingClaim(id: string, claimId: string): Promise<void>;
|
|
91
|
+
}
|
|
92
|
+
/** Outcome counts of one reviewed plan write leg. */
|
|
93
|
+
export interface EvolutionPlanAppliedEvent {
|
|
94
|
+
sessionId: string;
|
|
95
|
+
planId: string;
|
|
96
|
+
policyFingerprint?: string | undefined;
|
|
97
|
+
memoryApplied: number;
|
|
98
|
+
skillApplied: number;
|
|
99
|
+
rejectedOps: number;
|
|
100
|
+
stagedOps?: number | undefined;
|
|
101
|
+
executionFailures?: number | undefined;
|
|
102
|
+
skippedUnread?: number | undefined;
|
|
103
|
+
executionError?: string | undefined;
|
|
104
|
+
evidenceClassReports?: number | undefined;
|
|
105
|
+
evidenceQuotes?: number | undefined;
|
|
106
|
+
estimatedInputChars?: number | undefined;
|
|
107
|
+
}
|
|
108
|
+
/** One skill mutation announcement. */
|
|
109
|
+
export interface EvolutionSkillMutatedEvent {
|
|
110
|
+
action: string;
|
|
111
|
+
name: string;
|
|
112
|
+
skillDir?: string;
|
|
113
|
+
file?: string;
|
|
114
|
+
archivedPath?: string;
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=contract.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-package contracts for the dsh-evolution family.
|
|
3
|
+
*
|
|
4
|
+
* This package is intentionally a library: it declares state and event shapes,
|
|
5
|
+
* not a Cordis plugin. It must not import any evolution implementation package.
|
|
6
|
+
*
|
|
7
|
+
* @module @lmzhen/dsh-evolution-core/contract
|
|
8
|
+
*/
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=contract.js.map
|
package/lib/types/events.d.ts
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
* these events off `session.append`). Plan-outcome durability lives in the
|
|
13
13
|
* evolution-activity store, not the session log.
|
|
14
14
|
*/
|
|
15
|
-
import type { EvolutionPlanAppliedEvent, EvolutionSkillMutatedEvent } from '
|
|
16
|
-
export type { EvolutionPlanAppliedEvent, EvolutionSkillMutatedEvent } from '
|
|
15
|
+
import type { EvolutionPlanAppliedEvent, EvolutionSkillMutatedEvent } from './contract.ts';
|
|
16
|
+
export type { EvolutionPlanAppliedEvent, EvolutionSkillMutatedEvent } from './contract.ts';
|
|
17
17
|
import type { ReviewKind } from './signals.ts';
|
|
18
18
|
export interface EvolutionReviewScheduledEvent {
|
|
19
19
|
/** Owning session (payload v2): process events carry no session envelope. */
|
|
@@ -1,5 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Lease adapter over the one HomeAuthority (W9-authority).
|
|
3
|
+
*
|
|
4
|
+
* The fence owns no lock protocol of its own any more: cross-process exclusion is the platform
|
|
5
|
+
* writer lock inside {@link HomeAuthority}, and this class only keeps the family's historical
|
|
6
|
+
* `run`/`acquire` API so call sites (mutation units, the commit shell) keep working. A failed
|
|
7
|
+
* release is recorded on the authority instead of poisoning every later writer.
|
|
8
|
+
* @module @lmzhen/dsh-evolution-core/home-fence
|
|
9
|
+
*/
|
|
10
|
+
import { HomeAuthority, type AuthorityReleaseFailure, type TreeLock } from './authority.ts';
|
|
11
|
+
export interface HomeFenceOptions {
|
|
12
|
+
/** Evolution home; the authority's lock lives under `<home>/.authority/`. */
|
|
13
|
+
readonly home: string;
|
|
14
|
+
/** How long to wait for the home lock. Default 5000 ms. */
|
|
15
|
+
readonly waitMs?: number;
|
|
16
|
+
/** Retained for call-site compatibility; the authority owns exclusion, not the IO seam. */
|
|
17
|
+
readonly io?: unknown;
|
|
18
|
+
readonly now?: () => number;
|
|
19
|
+
/** Test seam: share one authority (and its reentrancy state) across fences. */
|
|
20
|
+
readonly authority?: HomeAuthority;
|
|
3
21
|
}
|
|
4
22
|
export interface HomeFenceLease {
|
|
5
23
|
readonly lock: TreeLock;
|
|
@@ -9,23 +27,13 @@ export declare class HomeFenceError extends Error {
|
|
|
9
27
|
readonly reason: 'busy' | 'io';
|
|
10
28
|
constructor(reason: 'busy' | 'io', message: string);
|
|
11
29
|
}
|
|
12
|
-
/**
|
|
13
|
-
* One home-level writer queue. It serializes same-process callers before the
|
|
14
|
-
* tree lock, and the tree lock remains the only cross-process guard the current
|
|
15
|
-
* backend can provide (nodeEvolutionIo is single-process).
|
|
16
|
-
*
|
|
17
|
-
* `acquire` is public only for legacy call sites; W0 keeps it usable but makes a
|
|
18
|
-
* failed release advance the queue and poison the fence instead of wedging every
|
|
19
|
-
* later writer. Nested acquisition is not supported: the state layer must reuse
|
|
20
|
-
* the transaction it is already inside (see evolution-state-json appendArchive).
|
|
21
|
-
*/
|
|
30
|
+
/** The family's home-level writer queue, backed by the one authority. */
|
|
22
31
|
export declare class HomeFence {
|
|
23
|
-
private
|
|
24
|
-
private poisoned;
|
|
25
|
-
private readonly options;
|
|
32
|
+
private readonly authority;
|
|
26
33
|
constructor(options: HomeFenceOptions);
|
|
27
|
-
|
|
28
|
-
|
|
34
|
+
/** The last release failure, if any; diagnostics only. */
|
|
35
|
+
get releaseFailure(): AuthorityReleaseFailure | undefined;
|
|
29
36
|
run<T>(body: (lock: TreeLock) => Promise<T>): Promise<T>;
|
|
37
|
+
acquire(): Promise<HomeFenceLease>;
|
|
30
38
|
}
|
|
31
39
|
//# sourceMappingURL=home-fence.d.ts.map
|