create-principles-disciple 1.153.0 → 1.153.2
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/_release/product-identity.json +2 -2
- package/console/dist/server/models/ActivationsConsoleModel.d.ts +8 -0
- package/console/dist/server/models/ActivationsConsoleModel.js +3 -2
- package/console/dist/server/models/ExaminerEvidenceModel.d.ts +6 -2
- package/console/dist/server/models/ExaminerEvidenceModel.js +13 -2
- package/console/dist/server/routes/examiner-executor.d.ts +33 -0
- package/console/dist/server/routes/examiner-executor.js +95 -0
- package/console/dist/server/routes/examiner.js +12 -9
- package/console/dist/web/assets/app.css +8 -10
- package/console/dist/web/assets/app.js +29 -29
- package/console/package.json +2 -2
- package/core/dist/ledger-file-lock.d.ts +38 -0
- package/core/dist/ledger-file-lock.d.ts.map +1 -0
- package/core/dist/ledger-file-lock.js +240 -0
- package/core/dist/ledger-file-lock.js.map +1 -0
- package/core/dist/principle-text-normalization.d.ts +32 -0
- package/core/dist/principle-text-normalization.d.ts.map +1 -0
- package/core/dist/principle-text-normalization.js +34 -0
- package/core/dist/principle-text-normalization.js.map +1 -0
- package/core/dist/principle-tree-ledger.d.ts +22 -21
- package/core/dist/principle-tree-ledger.d.ts.map +1 -1
- package/core/dist/principle-tree-ledger.js +77 -204
- package/core/dist/principle-tree-ledger.js.map +1 -1
- package/core/dist/runtime/adapter/principle-tree-ledger-adapter.d.ts +13 -0
- package/core/dist/runtime/adapter/principle-tree-ledger-adapter.d.ts.map +1 -1
- package/core/dist/runtime/adapter/principle-tree-ledger-adapter.js +90 -1
- package/core/dist/runtime/adapter/principle-tree-ledger-adapter.js.map +1 -1
- package/core/dist/runtime/adapter/schema-prompt-adapter.d.ts.map +1 -1
- package/core/dist/runtime/adapter/schema-prompt-adapter.js +21 -30
- package/core/dist/runtime/adapter/schema-prompt-adapter.js.map +1 -1
- package/core/dist/runtime/candidate-intake-service.d.ts +40 -1
- package/core/dist/runtime/candidate-intake-service.d.ts.map +1 -1
- package/core/dist/runtime/candidate-intake-service.js +548 -110
- package/core/dist/runtime/candidate-intake-service.js.map +1 -1
- package/core/dist/runtime/candidate-intake.d.ts +59 -2
- package/core/dist/runtime/candidate-intake.d.ts.map +1 -1
- package/core/dist/runtime/candidate-intake.js +23 -0
- package/core/dist/runtime/candidate-intake.js.map +1 -1
- package/core/dist/runtime/candidate-seed-suppression.d.ts +63 -0
- package/core/dist/runtime/candidate-seed-suppression.d.ts.map +1 -0
- package/core/dist/runtime/candidate-seed-suppression.js +29 -0
- package/core/dist/runtime/candidate-seed-suppression.js.map +1 -0
- package/core/dist/runtime/config/pd-config-agent-binding.d.ts +5 -5
- package/core/dist/runtime/config/pd-config-agent-binding.d.ts.map +1 -1
- package/core/dist/runtime/config/pd-config-agent-binding.js +22 -16
- package/core/dist/runtime/config/pd-config-agent-binding.js.map +1 -1
- package/core/dist/runtime/config/pd-credentials.d.ts.map +1 -1
- package/core/dist/runtime/config/pd-credentials.js +7 -1
- package/core/dist/runtime/config/pd-credentials.js.map +1 -1
- package/core/dist/runtime/diagnostician/distiller-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/diagnostician/distiller-prompt-builder.js +30 -3
- package/core/dist/runtime/diagnostician/distiller-prompt-builder.js.map +1 -1
- package/core/dist/runtime/diagnostician/rootcause-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/diagnostician/rootcause-prompt-builder.js +16 -5
- package/core/dist/runtime/diagnostician/rootcause-prompt-builder.js.map +1 -1
- package/core/dist/runtime/diagnostician/router-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/diagnostician/router-prompt-builder.js +16 -4
- package/core/dist/runtime/diagnostician/router-prompt-builder.js.map +1 -1
- package/core/dist/runtime/index.d.ts +4 -2
- package/core/dist/runtime/index.d.ts.map +1 -1
- package/core/dist/runtime/index.js +3 -1
- package/core/dist/runtime/index.js.map +1 -1
- package/core/dist/runtime/internalization/dreamer-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/internalization/dreamer-prompt-builder.js +177 -29
- package/core/dist/runtime/internalization/dreamer-prompt-builder.js.map +1 -1
- package/core/dist/runtime/internalization/internalization-orchestrator.d.ts +16 -2
- package/core/dist/runtime/internalization/internalization-orchestrator.d.ts.map +1 -1
- package/core/dist/runtime/internalization/internalization-orchestrator.js +48 -14
- package/core/dist/runtime/internalization/internalization-orchestrator.js.map +1 -1
- package/core/dist/runtime/internalization/philosopher-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/internalization/philosopher-prompt-builder.js +7 -6
- package/core/dist/runtime/internalization/philosopher-prompt-builder.js.map +1 -1
- package/core/dist/runtime/internalization/scribe-prompt-builder.d.ts +13 -6
- package/core/dist/runtime/internalization/scribe-prompt-builder.d.ts.map +1 -1
- package/core/dist/runtime/internalization/scribe-prompt-builder.js +15 -10
- package/core/dist/runtime/internalization/scribe-prompt-builder.js.map +1 -1
- package/core/dist/runtime/pain-signal-bridge.d.ts +3 -0
- package/core/dist/runtime/pain-signal-bridge.d.ts.map +1 -1
- package/core/dist/runtime/pain-signal-bridge.js +224 -93
- package/core/dist/runtime/pain-signal-bridge.js.map +1 -1
- package/core/dist/runtime/pain-submit-capability.d.ts.map +1 -1
- package/core/dist/runtime/pain-submit-capability.js +3 -5
- package/core/dist/runtime/pain-submit-capability.js.map +1 -1
- package/core/dist/runtime/pain-to-principle-service.d.ts.map +1 -1
- package/core/dist/runtime/pain-to-principle-service.js +18 -21
- package/core/dist/runtime/pain-to-principle-service.js.map +1 -1
- package/core/dist/runtime/store/candidate/admission-document.d.ts +129 -0
- package/core/dist/runtime/store/candidate/admission-document.d.ts.map +1 -1
- package/core/dist/runtime/store/candidate/admission-document.js +196 -0
- package/core/dist/runtime/store/candidate/admission-document.js.map +1 -1
- package/core/dist/runtime/store/candidate/candidate-store.d.ts +54 -1
- package/core/dist/runtime/store/candidate/candidate-store.d.ts.map +1 -1
- package/core/dist/runtime/store/candidate/memory-candidate-store.d.ts +32 -1
- package/core/dist/runtime/store/candidate/memory-candidate-store.d.ts.map +1 -1
- package/core/dist/runtime/store/candidate/memory-candidate-store.js +24 -1
- package/core/dist/runtime/store/candidate/memory-candidate-store.js.map +1 -1
- package/core/dist/runtime/store/candidate/sqlite-candidate-store.d.ts +32 -1
- package/core/dist/runtime/store/candidate/sqlite-candidate-store.d.ts.map +1 -1
- package/core/dist/runtime/store/candidate/sqlite-candidate-store.js +76 -1
- package/core/dist/runtime/store/candidate/sqlite-candidate-store.js.map +1 -1
- package/core/dist/runtime/store/runtime-state-manager.d.ts +34 -1
- package/core/dist/runtime/store/runtime-state-manager.d.ts.map +1 -1
- package/core/dist/runtime/store/runtime-state-manager.js +10 -0
- package/core/dist/runtime/store/runtime-state-manager.js.map +1 -1
- package/core/package.json +1 -1
- package/host-runtime/dist/governance-signal-admission.js +1 -5
- package/host-runtime/package.json +1 -1
- package/package.json +3 -3
- package/pd-cli/dist/commands/candidate.d.ts.map +1 -1
- package/pd-cli/dist/commands/candidate.js +243 -28
- package/pd-cli/dist/commands/candidate.js.map +1 -1
- package/pd-cli/dist/commands/diagnose.d.ts.map +1 -1
- package/pd-cli/dist/commands/diagnose.js +110 -7
- package/pd-cli/dist/commands/diagnose.js.map +1 -1
- package/pd-cli/dist/commands/pain-retry.d.ts.map +1 -1
- package/pd-cli/dist/commands/pain-retry.js +3 -0
- package/pd-cli/dist/commands/pain-retry.js.map +1 -1
- package/pd-cli/dist/commands/runtime-internalization-run-once.d.ts.map +1 -1
- package/pd-cli/dist/commands/runtime-internalization-run-once.js +68 -2
- package/pd-cli/dist/commands/runtime-internalization-run-once.js.map +1 -1
- package/pd-cli/dist/program.d.ts.map +1 -1
- package/pd-cli/dist/program.js +11 -1
- package/pd-cli/dist/program.js.map +1 -1
- package/pd-cli/dist/services/admission-write-gate.d.ts.map +1 -1
- package/pd-cli/dist/services/admission-write-gate.js +18 -7
- package/pd-cli/dist/services/admission-write-gate.js.map +1 -1
- package/pd-cli/dist/services/demo-story-a-runner.d.ts.map +1 -1
- package/pd-cli/dist/services/demo-story-a-runner.js +8 -3
- package/pd-cli/dist/services/demo-story-a-runner.js.map +1 -1
- package/pd-cli/dist/services/reuse-park-release.d.ts +142 -0
- package/pd-cli/dist/services/reuse-park-release.d.ts.map +1 -0
- package/pd-cli/dist/services/reuse-park-release.js +201 -0
- package/pd-cli/dist/services/reuse-park-release.js.map +1 -0
- package/pd-cli/package.json +1 -1
- package/plugin/dist/bundle.js +819 -629
- package/plugin/dist/openclaw.plugin.json +1 -1
- package/plugin/openclaw.plugin.json +1 -1
- package/plugin/package.json +1 -1
- package/release-manager/package.json +2 -2
package/console/package.json
CHANGED
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"react": "^19.3.1",
|
|
51
51
|
"react-dom": "^19.3.0",
|
|
52
52
|
"react-i18next": "^17.0.11",
|
|
53
|
-
"react-router-dom": "^7.18.
|
|
54
|
-
"semver": "^7.8.
|
|
53
|
+
"react-router-dom": "^7.18.5",
|
|
54
|
+
"semver": "^7.8.6",
|
|
55
55
|
"sonner": "^2.0.8",
|
|
56
56
|
"tailwind-merge": "^3.7.0",
|
|
57
57
|
"tailwindcss": "^4.3.3"
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export declare class LockAcquisitionError extends Error {
|
|
2
|
+
readonly filePath: string;
|
|
3
|
+
readonly lockPath: string;
|
|
4
|
+
constructor(message: string, filePath: string, lockPath: string);
|
|
5
|
+
}
|
|
6
|
+
export interface LockOptions {
|
|
7
|
+
maxRetries?: number;
|
|
8
|
+
baseRetryDelayMs?: number;
|
|
9
|
+
maxRetryDelayMs?: number;
|
|
10
|
+
lockStaleMs?: number;
|
|
11
|
+
lockSuffix?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface LockContext {
|
|
14
|
+
lockPath: string;
|
|
15
|
+
pid: number;
|
|
16
|
+
acquiredAt: number;
|
|
17
|
+
}
|
|
18
|
+
export declare function tryAcquireLock(lockPath: string, pid: number): boolean;
|
|
19
|
+
export declare function readLockPid(lockPath: string): number | null;
|
|
20
|
+
export declare function safeReleaseLock(lockPath: string, expectedPid: number): void;
|
|
21
|
+
export declare function cleanupStaleLock(lockPath: string, _staleMs: number): boolean;
|
|
22
|
+
export declare function calculateBackoff(attempt: number, baseMs: number, maxMs: number): number;
|
|
23
|
+
export declare function sleepSync(ms: number): void;
|
|
24
|
+
export declare function tryAcquireWithStaleCleanup(filePath: string, opts: Required<LockOptions>, pid: number): LockContext | null;
|
|
25
|
+
export declare function acquireLock(filePath: string, options?: LockOptions): LockContext;
|
|
26
|
+
export declare function acquireLockAsync(filePath: string, options?: LockOptions): Promise<LockContext>;
|
|
27
|
+
export declare function releaseLock(ctx: LockContext): void;
|
|
28
|
+
/**
|
|
29
|
+
* Run `fn` while holding the ledger file lock. FAIL-LOUD: if the lock cannot
|
|
30
|
+
* be acquired after bounded retries, throws LockAcquisitionError carrying the
|
|
31
|
+
* file + lock path (EP-03 / ERR-009 — no silent fallback).
|
|
32
|
+
*
|
|
33
|
+
* Exported so tests and integrators can drive the lock primitive directly with
|
|
34
|
+
* a tight retry budget (the public mutators use production defaults).
|
|
35
|
+
*/
|
|
36
|
+
export declare function withLock<T>(filePath: string, fn: () => T, options?: LockOptions): T;
|
|
37
|
+
export declare function withLockAsync<T>(filePath: string, fn: () => Promise<T>, options?: LockOptions): Promise<T>;
|
|
38
|
+
//# sourceMappingURL=ledger-file-lock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger-file-lock.d.ts","sourceRoot":"","sources":["../src/ledger-file-lock.ts"],"names":[],"mappings":"AAoCA,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,SAAgB,QAAQ,EAAE,MAAM,CAAC;IACjC,SAAgB,QAAQ,EAAE,MAAM,CAAC;IACjC,YAAY,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAK9D;CACF;AAED,MAAM,WAAW,WAAW;IAC1B,UAAU,CAAC,EAAE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;GAOG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,CAAC,CAOnF;AAsBD,wBAAgB,aAAa,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CAE1G"}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ledger file lock - the O_EXCL cross-process primitive shared by the
|
|
3
|
+
* principle-tree-ledger mutators (extracted verbatim from
|
|
4
|
+
* principle-tree-ledger.ts, PRI-459 lineage).
|
|
5
|
+
*
|
|
6
|
+
* INTERNAL module: not reachable through the package exports map. The
|
|
7
|
+
* ledger module re-exports `withLock` and `LockAcquisitionError` for its
|
|
8
|
+
* existing public consumers; the ASYNC twin (`withLockAsync`, Phase 7.2
|
|
9
|
+
* P1-02) serves the runtime adapter's per-candidate coordination lock and
|
|
10
|
+
* stays internal - an in-core consumer never justifies widening the
|
|
11
|
+
* public surface.
|
|
12
|
+
*
|
|
13
|
+
* Semantics (unchanged): O_EXCL create with owner-only mode, PID-liveness
|
|
14
|
+
* stale cleanup (a dead holder's lock is reclaimable, a live one's never -
|
|
15
|
+
* file age is never the sole eviction signal), bounded exponential backoff,
|
|
16
|
+
* fail-loud LockAcquisitionError. Lock ordering rule: a candidate
|
|
17
|
+
* coordination lock is ALWAYS acquired BEFORE any ledger lock.
|
|
18
|
+
*/
|
|
19
|
+
import * as fs from 'fs';
|
|
20
|
+
import * as path from 'path';
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// File lock (PRI-459: hoisted from openclaw-plugin/utils/file-lock.ts)
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
/* global NodeJS */
|
|
25
|
+
//
|
|
26
|
+
// The principle_training_state.json file has a SINGLE writer contract. Before
|
|
27
|
+
// PRI-459, core wrote it UNLOCKED and the plugin wrote it WITH a lock — but
|
|
28
|
+
// neither knew about the other's lock, so concurrent writers (e.g.
|
|
29
|
+
// evolution-worker async + a pd-cli command) could lose updates. The atomic
|
|
30
|
+
// rename only prevents torn files; it does NOT prevent lost updates.
|
|
31
|
+
//
|
|
32
|
+
// The lock is now owned by core (the single mutator source of truth). Both
|
|
33
|
+
// the sync and async mutate paths acquire it. Acquisition failure fails LOUD
|
|
34
|
+
// (EP-03 / ERR-009) — never silently swallowed.
|
|
35
|
+
export class LockAcquisitionError extends Error {
|
|
36
|
+
filePath;
|
|
37
|
+
lockPath;
|
|
38
|
+
constructor(message, filePath, lockPath) {
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = 'LockAcquisitionError';
|
|
41
|
+
this.filePath = filePath;
|
|
42
|
+
this.lockPath = lockPath;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const DEFAULT_LOCK_OPTIONS = {
|
|
46
|
+
maxRetries: 50,
|
|
47
|
+
baseRetryDelayMs: 10,
|
|
48
|
+
maxRetryDelayMs: 500,
|
|
49
|
+
lockStaleMs: 10000,
|
|
50
|
+
lockSuffix: '.lock',
|
|
51
|
+
};
|
|
52
|
+
function isProcessAlive(pid) {
|
|
53
|
+
try {
|
|
54
|
+
process.kill(pid, 0);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function isErrnoException(value) {
|
|
62
|
+
return typeof value === 'object' && value !== null && Object.hasOwn(value, 'code');
|
|
63
|
+
}
|
|
64
|
+
export function tryAcquireLock(lockPath, pid) {
|
|
65
|
+
try {
|
|
66
|
+
const lockDir = path.dirname(lockPath);
|
|
67
|
+
if (!fs.existsSync(lockDir)) {
|
|
68
|
+
fs.mkdirSync(lockDir, { recursive: true });
|
|
69
|
+
}
|
|
70
|
+
const flags = fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL;
|
|
71
|
+
// CodeQL fix (js/insecure-temporary-file): restrict lock file permissions
|
|
72
|
+
// to owner-only (0o600). O_EXCL already prevents pre-existing-file/symlink
|
|
73
|
+
// attacks on creation; the mode tightens permissions so the lock file is
|
|
74
|
+
// not world-readable/writable even when stateDir is in os.tmpdir().
|
|
75
|
+
const fd = fs.openSync(lockPath, flags, 0o600);
|
|
76
|
+
fs.writeSync(fd, String(pid));
|
|
77
|
+
fs.fsyncSync(fd);
|
|
78
|
+
fs.closeSync(fd);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
// Runtime Contract Rule #2: never `as` to bypass validation on caught
|
|
83
|
+
// unknown. A non-object `err` would throw on `.code` access and mask the
|
|
84
|
+
// original EEXIST (EP-01 / ERR-001).
|
|
85
|
+
if (isErrnoException(err) && err.code === 'EEXIST') {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
throw err;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export function readLockPid(lockPath) {
|
|
92
|
+
try {
|
|
93
|
+
const content = fs.readFileSync(lockPath, 'utf8');
|
|
94
|
+
const pid = parseInt(content.trim(), 10);
|
|
95
|
+
return Number.isNaN(pid) ? null : pid;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
export function safeReleaseLock(lockPath, expectedPid) {
|
|
102
|
+
try {
|
|
103
|
+
if (readLockPid(lockPath) === expectedPid) {
|
|
104
|
+
fs.unlinkSync(lockPath);
|
|
105
|
+
}
|
|
106
|
+
// If PID does not match, the lock was re-acquired by another process; leave it.
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
// best effort
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export function cleanupStaleLock(lockPath, _staleMs) {
|
|
113
|
+
// PRI-459 review: reclaim ONLY when the holder is dead/unknown. The file
|
|
114
|
+
// age must NEVER be the sole eviction signal — a legitimately long write
|
|
115
|
+
// that exceeds lockStaleMs would otherwise let a second writer steal the
|
|
116
|
+
// lock and re-introduce the lost-update class this PR exists to eliminate.
|
|
117
|
+
// PID liveness is the sole authority. `_staleMs` is retained on the
|
|
118
|
+
// signature for back-compat with callers but is intentionally unused.
|
|
119
|
+
try {
|
|
120
|
+
const pid = readLockPid(lockPath);
|
|
121
|
+
const isDead = pid === null || !isProcessAlive(pid);
|
|
122
|
+
if (isDead) {
|
|
123
|
+
try {
|
|
124
|
+
fs.unlinkSync(lockPath);
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
export function calculateBackoff(attempt, baseMs, maxMs) {
|
|
138
|
+
const exponentialDelay = Math.min(baseMs * Math.pow(2, attempt), maxMs);
|
|
139
|
+
const jitter = exponentialDelay * 0.2 * Math.random();
|
|
140
|
+
return Math.floor(exponentialDelay + jitter);
|
|
141
|
+
}
|
|
142
|
+
export function sleepSync(ms) {
|
|
143
|
+
// Non-spinning blocking sleep. Atomics.wait parks the thread instead of
|
|
144
|
+
// busy-polling Date.now(), so lock-contention retries don't burn a CPU
|
|
145
|
+
// core for the whole backoff window. Returns immediately when ms <= 0
|
|
146
|
+
// (Atomics.wait requires a positive timeout).
|
|
147
|
+
if (ms <= 0)
|
|
148
|
+
return;
|
|
149
|
+
const shared = new Int32Array(new SharedArrayBuffer(4));
|
|
150
|
+
Atomics.wait(shared, 0, 0, ms);
|
|
151
|
+
}
|
|
152
|
+
export function tryAcquireWithStaleCleanup(filePath, opts, pid) {
|
|
153
|
+
const lockPath = filePath + opts.lockSuffix;
|
|
154
|
+
if (tryAcquireLock(lockPath, pid) && readLockPid(lockPath) === pid) {
|
|
155
|
+
return { lockPath, pid, acquiredAt: Date.now() };
|
|
156
|
+
}
|
|
157
|
+
cleanupStaleLock(lockPath, opts.lockStaleMs);
|
|
158
|
+
if (tryAcquireLock(lockPath, pid) && readLockPid(lockPath) === pid) {
|
|
159
|
+
return { lockPath, pid, acquiredAt: Date.now() };
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
function buildLockError(filePath, opts) {
|
|
164
|
+
const lockPath = filePath + opts.lockSuffix;
|
|
165
|
+
const holderPid = readLockPid(lockPath);
|
|
166
|
+
const holderStatus = holderPid !== null
|
|
167
|
+
? (isProcessAlive(holderPid) ? `alive (PID ${holderPid})` : `dead (PID ${holderPid})`)
|
|
168
|
+
: 'unknown';
|
|
169
|
+
return new LockAcquisitionError(`Failed to acquire lock for ${filePath}. Lock holder: ${holderStatus}.`, filePath, lockPath);
|
|
170
|
+
}
|
|
171
|
+
export function acquireLock(filePath, options = {}) {
|
|
172
|
+
const opts = { ...DEFAULT_LOCK_OPTIONS, ...options };
|
|
173
|
+
const { pid } = process;
|
|
174
|
+
for (let attempt = 0; attempt < opts.maxRetries; attempt++) {
|
|
175
|
+
const ctx = tryAcquireWithStaleCleanup(filePath, opts, pid);
|
|
176
|
+
if (ctx)
|
|
177
|
+
return ctx;
|
|
178
|
+
if (attempt < opts.maxRetries - 1) {
|
|
179
|
+
sleepSync(calculateBackoff(attempt, opts.baseRetryDelayMs, opts.maxRetryDelayMs));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
throw buildLockError(filePath, opts);
|
|
183
|
+
}
|
|
184
|
+
export async function acquireLockAsync(filePath, options = {}) {
|
|
185
|
+
const opts = { ...DEFAULT_LOCK_OPTIONS, ...options };
|
|
186
|
+
const { pid } = process;
|
|
187
|
+
for (let attempt = 0; attempt < opts.maxRetries; attempt++) {
|
|
188
|
+
const ctx = tryAcquireWithStaleCleanup(filePath, opts, pid);
|
|
189
|
+
if (ctx)
|
|
190
|
+
return ctx;
|
|
191
|
+
if (attempt < opts.maxRetries - 1) {
|
|
192
|
+
await new Promise((resolve) => setTimeout(resolve, calculateBackoff(attempt, opts.baseRetryDelayMs, opts.maxRetryDelayMs)));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
throw buildLockError(filePath, opts);
|
|
196
|
+
}
|
|
197
|
+
export function releaseLock(ctx) {
|
|
198
|
+
safeReleaseLock(ctx.lockPath, ctx.pid);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Run `fn` while holding the ledger file lock. FAIL-LOUD: if the lock cannot
|
|
202
|
+
* be acquired after bounded retries, throws LockAcquisitionError carrying the
|
|
203
|
+
* file + lock path (EP-03 / ERR-009 — no silent fallback).
|
|
204
|
+
*
|
|
205
|
+
* Exported so tests and integrators can drive the lock primitive directly with
|
|
206
|
+
* a tight retry budget (the public mutators use production defaults).
|
|
207
|
+
*/
|
|
208
|
+
export function withLock(filePath, fn, options) {
|
|
209
|
+
const ctx = acquireLock(filePath, options);
|
|
210
|
+
try {
|
|
211
|
+
return fn();
|
|
212
|
+
}
|
|
213
|
+
finally {
|
|
214
|
+
releaseLock(ctx);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Async variant of {@link withLock} — same O_EXCL cross-process semantics,
|
|
219
|
+
* PID-liveness stale cleanup and fail-loud LockAcquisitionError, with
|
|
220
|
+
* non-blocking backoff so an async critical section (e.g. the candidate
|
|
221
|
+
* intake coordination section: SQLite reads → ledger write → evidence audit)
|
|
222
|
+
* can await WITHOUT freezing the event loop. Exported for the adapter's
|
|
223
|
+
* candidate-level coordination lock (Phase 7.2 P1-02) and for integrations
|
|
224
|
+
* that must serialize async work on the same discipline as the ledger
|
|
225
|
+
* mutators. Lock ordering rule: a candidate coordination lock is ALWAYS
|
|
226
|
+
* acquired BEFORE any ledger lock, never the reverse.
|
|
227
|
+
*/
|
|
228
|
+
async function withLockAsyncInternal(filePath, fn, options) {
|
|
229
|
+
const ctx = await acquireLockAsync(filePath, options);
|
|
230
|
+
try {
|
|
231
|
+
return await fn();
|
|
232
|
+
}
|
|
233
|
+
finally {
|
|
234
|
+
releaseLock(ctx);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
export function withLockAsync(filePath, fn, options) {
|
|
238
|
+
return withLockAsyncInternal(filePath, fn, options);
|
|
239
|
+
}
|
|
240
|
+
//# sourceMappingURL=ledger-file-lock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger-file-lock.js","sourceRoot":"","sources":["../src/ledger-file-lock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,8EAA8E;AAC9E,uEAAuE;AACvE,8EAA8E;AAC9E,mBAAmB;AACnB,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,mEAAmE;AACnE,4EAA4E;AAC5E,qEAAqE;AACrE,EAAE;AACF,2EAA2E;AAC3E,6EAA6E;AAC7E,gDAAgD;AAEhD,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC7B,QAAQ,CAAS;IACjB,QAAQ,CAAS;IACjC,YAAY,OAAe,EAAE,QAAgB,EAAE,QAAgB;QAC7D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAgBD,MAAM,oBAAoB,GAA0B;IAClD,UAAU,EAAE,EAAE;IACd,gBAAgB,EAAE,EAAE;IACpB,eAAe,EAAE,GAAG;IACpB,WAAW,EAAE,KAAK;IAClB,UAAU,EAAE,OAAO;CACpB,CAAC;AAEF,SAAS,cAAc,CAAC,GAAW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,OAAO,EAAE,EAAE,CAAC;IACd,CAAC;YAAS,CAAC;QACT,WAAW,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,KAAK,UAAU,qBAAqB,CAAI,QAAgB,EAAE,EAAoB,EAAE,OAAqB;IACnG,MAAM,GAAG,GAAG,MAAM,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IACtD,IAAI,CAAC;QACH,OAAO,MAAM,EAAE,EAAE,CAAC;IACpB,CAAC;YAAS,CAAC;QACT,WAAW,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,aAAa,CAAI,QAAgB,EAAE,EAAoB,EAAE,OAAqB;IAC5F,OAAO,qBAAqB,CAAC,QAAQ,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 7.2 B1 — normalization for the ledger's exact-duplicate guard.
|
|
3
|
+
*
|
|
4
|
+
* Package-internal (not on any public subpath export map): its only
|
|
5
|
+
* production consumer is principle-tree-ledger.ts's in-lock duplicate scan,
|
|
6
|
+
* and tests import it relatively. Keeping it out of the public surface is the
|
|
7
|
+
* point — the export-surface admission gate would otherwise demand a public
|
|
8
|
+
* consumer that does not exist.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Normalize a principle text for EXACT-duplicate comparison
|
|
12
|
+
* (LEDGER_WRITE_SAFETY_SPEC §3.2, 7.1 revision).
|
|
13
|
+
*
|
|
14
|
+
* The algorithm is deliberately CONSERVATIVE — it only folds differences that
|
|
15
|
+
* cannot change a principle's meaning at the byte level:
|
|
16
|
+
*
|
|
17
|
+
* 1. NFKC equivalence folding (fullwidth ↔ halfwidth and other Unicode
|
|
18
|
+
* compatibility equivalents);
|
|
19
|
+
* 2. ALL punctuation is PRESERVED verbatim — no "punctuation → whitespace"
|
|
20
|
+
* folding. Distinct punctuation (',' vs '。', '&&' vs '||') stays
|
|
21
|
+
* distinct; NFKC maps fullwidth punctuation onto its ASCII equivalent,
|
|
22
|
+
* which is exactly the intended equivalence class;
|
|
23
|
+
* 3. runs of whitespace collapse to a single space and the result is
|
|
24
|
+
* trimmed — whitespace-level duplicates ARE duplicates;
|
|
25
|
+
* 4. NO case folding — `useMockHandler` and `usemockhandler` are different
|
|
26
|
+
* code identifiers and must stay distinct (negative control N6).
|
|
27
|
+
*
|
|
28
|
+
* This is exact-match protection only. It is NOT semantic deduplication:
|
|
29
|
+
* near-duplicates remain the reuse gate's responsibility.
|
|
30
|
+
*/
|
|
31
|
+
export declare function normalizePrincipleText(text: string): string;
|
|
32
|
+
//# sourceMappingURL=principle-text-normalization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principle-text-normalization.d.ts","sourceRoot":"","sources":["../src/principle-text-normalization.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE3D"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 7.2 B1 — normalization for the ledger's exact-duplicate guard.
|
|
3
|
+
*
|
|
4
|
+
* Package-internal (not on any public subpath export map): its only
|
|
5
|
+
* production consumer is principle-tree-ledger.ts's in-lock duplicate scan,
|
|
6
|
+
* and tests import it relatively. Keeping it out of the public surface is the
|
|
7
|
+
* point — the export-surface admission gate would otherwise demand a public
|
|
8
|
+
* consumer that does not exist.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Normalize a principle text for EXACT-duplicate comparison
|
|
12
|
+
* (LEDGER_WRITE_SAFETY_SPEC §3.2, 7.1 revision).
|
|
13
|
+
*
|
|
14
|
+
* The algorithm is deliberately CONSERVATIVE — it only folds differences that
|
|
15
|
+
* cannot change a principle's meaning at the byte level:
|
|
16
|
+
*
|
|
17
|
+
* 1. NFKC equivalence folding (fullwidth ↔ halfwidth and other Unicode
|
|
18
|
+
* compatibility equivalents);
|
|
19
|
+
* 2. ALL punctuation is PRESERVED verbatim — no "punctuation → whitespace"
|
|
20
|
+
* folding. Distinct punctuation (',' vs '。', '&&' vs '||') stays
|
|
21
|
+
* distinct; NFKC maps fullwidth punctuation onto its ASCII equivalent,
|
|
22
|
+
* which is exactly the intended equivalence class;
|
|
23
|
+
* 3. runs of whitespace collapse to a single space and the result is
|
|
24
|
+
* trimmed — whitespace-level duplicates ARE duplicates;
|
|
25
|
+
* 4. NO case folding — `useMockHandler` and `usemockhandler` are different
|
|
26
|
+
* code identifiers and must stay distinct (negative control N6).
|
|
27
|
+
*
|
|
28
|
+
* This is exact-match protection only. It is NOT semantic deduplication:
|
|
29
|
+
* near-duplicates remain the reuse gate's responsibility.
|
|
30
|
+
*/
|
|
31
|
+
export function normalizePrincipleText(text) {
|
|
32
|
+
return text.normalize('NFKC').replace(/\s+/g, ' ').trim();
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=principle-text-normalization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principle-text-normalization.js","sourceRoot":"","sources":["../src/principle-text-normalization.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAAY;IACjD,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AAC5D,CAAC"}
|
|
@@ -42,27 +42,7 @@ export declare function readLedgerFileState(stateDir: string): {
|
|
|
42
42
|
ledger: HybridLedgerStore;
|
|
43
43
|
availability: LedgerFileAvailability;
|
|
44
44
|
};
|
|
45
|
-
export
|
|
46
|
-
readonly filePath: string;
|
|
47
|
-
readonly lockPath: string;
|
|
48
|
-
constructor(message: string, filePath: string, lockPath: string);
|
|
49
|
-
}
|
|
50
|
-
interface LockOptions {
|
|
51
|
-
maxRetries?: number;
|
|
52
|
-
baseRetryDelayMs?: number;
|
|
53
|
-
maxRetryDelayMs?: number;
|
|
54
|
-
lockStaleMs?: number;
|
|
55
|
-
lockSuffix?: string;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Run `fn` while holding the ledger file lock. FAIL-LOUD: if the lock cannot
|
|
59
|
-
* be acquired after bounded retries, throws LockAcquisitionError carrying the
|
|
60
|
-
* file + lock path (EP-03 / ERR-009 — no silent fallback).
|
|
61
|
-
*
|
|
62
|
-
* Exported so tests and integrators can drive the lock primitive directly with
|
|
63
|
-
* a tight retry budget (the public mutators use production defaults).
|
|
64
|
-
*/
|
|
65
|
-
export declare function withLock<T>(filePath: string, fn: () => T, options?: LockOptions): T;
|
|
45
|
+
export { withLock, LockAcquisitionError } from './ledger-file-lock.js';
|
|
66
46
|
/**
|
|
67
47
|
* Read-modify-write the ledger file atomically AND under a cross-process file
|
|
68
48
|
* lock. Single source of truth since PRI-459: every mutation (sync + async)
|
|
@@ -82,6 +62,27 @@ export declare class LedgerIntegrityError extends Error {
|
|
|
82
62
|
}
|
|
83
63
|
export declare function loadLedger(stateDir: string): HybridLedgerStore;
|
|
84
64
|
export declare function saveLedger(stateDir: string, store: HybridLedgerStore): void;
|
|
65
|
+
/**
|
|
66
|
+
* Register a principle in the ledger.
|
|
67
|
+
*
|
|
68
|
+
* Phase 7.2 B1 — deterministic exact-duplicate guard: INSIDE the single-writer
|
|
69
|
+
* lock and BEFORE the write, the incoming principle's normalized text
|
|
70
|
+
* (see {@link normalizePrincipleText}) is compared against every principle in
|
|
71
|
+
* an eligible lifecycle state (candidate / probation / active). On a hit:
|
|
72
|
+
*
|
|
73
|
+
* - NO new ledger entry is created;
|
|
74
|
+
* - the EXISTING (surviving) principle is returned — a caller detects the
|
|
75
|
+
* dedup when the returned id differs from the id it attempted to write;
|
|
76
|
+
* - only `tree.lastUpdated` is refreshed (same convention as the
|
|
77
|
+
* appendReuseEvidence idempotent no-op).
|
|
78
|
+
*
|
|
79
|
+
* Because the check runs under the cross-process file lock, concurrent
|
|
80
|
+
* writers adding the same text register exactly one entry. Reuse-evidence
|
|
81
|
+
* auditing for a dedup resolution happens OUTSIDE this lock:
|
|
82
|
+
* appendReuseEvidence acquires its own mutateLedger lock, so calling it from
|
|
83
|
+
* inside this callback would self-deadlock the O_EXCL lock (same-process
|
|
84
|
+
* re-entry exhausts its retries and throws LockAcquisitionError).
|
|
85
|
+
*/
|
|
85
86
|
export declare function addPrincipleToLedger(stateDir: string, principle: LedgerPrinciple): LedgerPrinciple;
|
|
86
87
|
export declare function updatePrinciple(stateDir: string, principleId: string, updates: Partial<LedgerPrinciple>): LedgerPrinciple;
|
|
87
88
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"principle-tree-ledger.d.ts","sourceRoot":"","sources":["../src/principle-tree-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAMH,OAAO,KAAK,EAAwB,2BAA2B,EAAE,4BAA4B,EAAE,kCAAkC,EAAE,0BAA0B,EAA+B,MAAM,2CAA2C,CAAC;AAG9O,OAAO,KAAK,EACV,SAAS,EACT,IAAI,EACJ,cAAc,EACd,qBAAqB,EACrB,eAAe,EACf,UAAU,EACV,eAAe,EACf,4BAA4B,EAC5B,4BAA4B,EAC5B,iBAAiB,EAClB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"principle-tree-ledger.d.ts","sourceRoot":"","sources":["../src/principle-tree-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAMH,OAAO,KAAK,EAAwB,2BAA2B,EAAE,4BAA4B,EAAE,kCAAkC,EAAE,0BAA0B,EAA+B,MAAM,2CAA2C,CAAC;AAG9O,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