@yawlabs/mcp 1.0.19 → 1.1.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/CHANGELOG.md +49 -0
- package/README.md +11 -8
- package/dist/{auto-upgrade-EXN56GQI.js → auto-upgrade-3IZNHFYK.js} +1 -1
- package/dist/{chunk-PUO3HCQS.js → chunk-NOPVNM7B.js} +852 -142
- package/dist/index.js +3652 -2554
- package/package.json +4 -2
|
@@ -6,16 +6,16 @@ import { createHash } from "crypto";
|
|
|
6
6
|
import {
|
|
7
7
|
closeSync,
|
|
8
8
|
openSync,
|
|
9
|
-
readFileSync as
|
|
9
|
+
readFileSync as readFileSync4,
|
|
10
10
|
realpathSync as realpathSync3,
|
|
11
11
|
renameSync,
|
|
12
|
-
statSync as
|
|
12
|
+
statSync as statSync3,
|
|
13
13
|
unlinkSync,
|
|
14
14
|
writeFileSync,
|
|
15
15
|
writeSync
|
|
16
16
|
} from "fs";
|
|
17
17
|
import { tmpdir } from "os";
|
|
18
|
-
import { dirname as
|
|
18
|
+
import { dirname as dirname4, join as join12, sep as sep2 } from "path";
|
|
19
19
|
|
|
20
20
|
// src/atomic-write.ts
|
|
21
21
|
import { chmod, lstat, mkdir, readdir, realpath, rename, stat, unlink, writeFile } from "fs/promises";
|
|
@@ -252,14 +252,14 @@ function isTestSandbox(env = process.env) {
|
|
|
252
252
|
|
|
253
253
|
// src/upgrade-cmd.ts
|
|
254
254
|
import { spawn as spawn2 } from "child_process";
|
|
255
|
-
import { readFileSync as
|
|
256
|
-
import { join as
|
|
255
|
+
import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
|
|
256
|
+
import { join as join11 } from "path";
|
|
257
257
|
|
|
258
258
|
// src/oam-spawn.ts
|
|
259
259
|
import { spawn } from "child_process";
|
|
260
|
-
import { accessSync, constants, existsSync as existsSync2, readdirSync as readdirSync2, readFileSync, statSync } from "fs";
|
|
260
|
+
import { accessSync, constants, existsSync as existsSync2, readdirSync as readdirSync2, readFileSync as readFileSync2, statSync as statSync2 } from "fs";
|
|
261
261
|
import { homedir as homedir4 } from "os";
|
|
262
|
-
import { dirname as
|
|
262
|
+
import { dirname as dirname3, isAbsolute as isAbsolute8, join as join10, sep } from "path";
|
|
263
263
|
import { fileURLToPath } from "url";
|
|
264
264
|
|
|
265
265
|
// src/install-targets.ts
|
|
@@ -596,10 +596,10 @@ function removeMember(text, container, member) {
|
|
|
596
596
|
while (from > 0 && isHorizontalSpace(text[from - 1])) from--;
|
|
597
597
|
}
|
|
598
598
|
if (members.length === 1) {
|
|
599
|
-
const
|
|
599
|
+
const open2 = container.offset + 1;
|
|
600
600
|
const close = container.offset + container.length - 1;
|
|
601
|
-
if (/^\s*$/.test(text.slice(
|
|
602
|
-
from =
|
|
601
|
+
if (/^\s*$/.test(text.slice(open2, from) + text.slice(to, close))) {
|
|
602
|
+
from = open2;
|
|
603
603
|
to = close;
|
|
604
604
|
}
|
|
605
605
|
}
|
|
@@ -622,11 +622,11 @@ function replaceValue(text, container, node, value) {
|
|
|
622
622
|
);
|
|
623
623
|
return { offset: node.offset, length: node.length, content };
|
|
624
624
|
}
|
|
625
|
-
function spliceEdits(text,
|
|
625
|
+
function spliceEdits(text, path5, value) {
|
|
626
626
|
const errors = [];
|
|
627
627
|
const root = parseTree(text, errors, { allowTrailingComma: true });
|
|
628
628
|
if (root === void 0 || errors.length > 0) return null;
|
|
629
|
-
const remaining =
|
|
629
|
+
const remaining = path5.slice();
|
|
630
630
|
let toWrite = value;
|
|
631
631
|
let parent;
|
|
632
632
|
let key;
|
|
@@ -652,8 +652,8 @@ function spliceEdits(text, path3, value) {
|
|
|
652
652
|
}
|
|
653
653
|
return null;
|
|
654
654
|
}
|
|
655
|
-
function computeEdits(text,
|
|
656
|
-
return spliceEdits(text,
|
|
655
|
+
function computeEdits(text, path5, value) {
|
|
656
|
+
return spliceEdits(text, path5, value) ?? modify(text, path5, value, { formattingOptions: FORMATTING_OPTIONS });
|
|
657
657
|
}
|
|
658
658
|
function editJsoncEntry(src, containerPath, entryName, value) {
|
|
659
659
|
if (entryName === "") {
|
|
@@ -671,40 +671,40 @@ function removeJsoncEntry(src, containerPath, entryName) {
|
|
|
671
671
|
if (edits.length === 0) return src;
|
|
672
672
|
return applyEdits(debommed, edits);
|
|
673
673
|
}
|
|
674
|
-
function editJsoncPath(src,
|
|
675
|
-
if (
|
|
674
|
+
function editJsoncPath(src, path5, value) {
|
|
675
|
+
if (path5.length === 0) {
|
|
676
676
|
throw new Error("editJsoncPath: path must not be empty");
|
|
677
677
|
}
|
|
678
|
-
if (
|
|
678
|
+
if (path5.some((seg) => seg === "")) {
|
|
679
679
|
throw new Error("editJsoncPath: path segments must be non-empty");
|
|
680
680
|
}
|
|
681
681
|
const debommed = src.charCodeAt(0) === 65279 ? src.slice(1) : src;
|
|
682
|
-
const edits = computeEdits(debommed,
|
|
682
|
+
const edits = computeEdits(debommed, path5, value);
|
|
683
683
|
if (edits.length === 0) return src;
|
|
684
684
|
return applyEdits(debommed, edits);
|
|
685
685
|
}
|
|
686
686
|
function elementValue(child) {
|
|
687
687
|
return getNodeValue(child);
|
|
688
688
|
}
|
|
689
|
-
function arrayNodeAt(text,
|
|
689
|
+
function arrayNodeAt(text, path5) {
|
|
690
690
|
const errors = [];
|
|
691
691
|
const root = parseTree(text, errors, { allowTrailingComma: true });
|
|
692
692
|
if (root === void 0 || errors.length > 0) return null;
|
|
693
|
-
const node = findNodeAtLocation(root,
|
|
693
|
+
const node = findNodeAtLocation(root, path5);
|
|
694
694
|
return node !== void 0 && node.type === "array" ? node : null;
|
|
695
695
|
}
|
|
696
|
-
function addJsoncArrayElement(src,
|
|
697
|
-
if (
|
|
696
|
+
function addJsoncArrayElement(src, path5, value) {
|
|
697
|
+
if (path5.length === 0) throw new Error("addJsoncArrayElement: path must not be empty");
|
|
698
698
|
const debommed = src.charCodeAt(0) === 65279 ? src.slice(1) : src;
|
|
699
|
-
const array = arrayNodeAt(debommed,
|
|
699
|
+
const array = arrayNodeAt(debommed, path5);
|
|
700
700
|
if (array === null) {
|
|
701
701
|
const errors = [];
|
|
702
702
|
const root = parseTree(debommed, errors, { allowTrailingComma: true });
|
|
703
|
-
const existing = root === void 0 ? void 0 : findNodeAtLocation(root,
|
|
703
|
+
const existing = root === void 0 ? void 0 : findNodeAtLocation(root, path5);
|
|
704
704
|
if (existing !== void 0) {
|
|
705
|
-
throw new Error(`addJsoncArrayElement: ${
|
|
705
|
+
throw new Error(`addJsoncArrayElement: ${path5.join(".")} is a ${existing.type}, not an array`);
|
|
706
706
|
}
|
|
707
|
-
return editJsoncPath(debommed,
|
|
707
|
+
return editJsoncPath(debommed, path5, [value]);
|
|
708
708
|
}
|
|
709
709
|
const wanted = JSON.stringify(value);
|
|
710
710
|
for (const child of array.children ?? []) {
|
|
@@ -713,31 +713,31 @@ function addJsoncArrayElement(src, path3, value) {
|
|
|
713
713
|
const edits = insertMember(debommed, array, null, value);
|
|
714
714
|
if (edits === null) {
|
|
715
715
|
const next = [...(array.children ?? []).map(elementValue), value];
|
|
716
|
-
return editJsoncPath(debommed,
|
|
716
|
+
return editJsoncPath(debommed, path5, next);
|
|
717
717
|
}
|
|
718
718
|
return applyEdits(debommed, edits);
|
|
719
719
|
}
|
|
720
|
-
function removeJsoncArrayElements(src,
|
|
721
|
-
if (
|
|
720
|
+
function removeJsoncArrayElements(src, path5, matches) {
|
|
721
|
+
if (path5.length === 0) throw new Error("removeJsoncArrayElements: path must not be empty");
|
|
722
722
|
const debommed = src.charCodeAt(0) === 65279 ? src.slice(1) : src;
|
|
723
|
-
const array = arrayNodeAt(debommed,
|
|
723
|
+
const array = arrayNodeAt(debommed, path5);
|
|
724
724
|
if (array === null) return src;
|
|
725
725
|
const doomed = (array.children ?? []).filter((child) => matches(elementValue(child)));
|
|
726
726
|
if (doomed.length === 0) return src;
|
|
727
727
|
let text = debommed;
|
|
728
728
|
for (let i = 0; i < doomed.length; i++) {
|
|
729
|
-
const current = arrayNodeAt(text,
|
|
729
|
+
const current = arrayNodeAt(text, path5);
|
|
730
730
|
if (current === null) break;
|
|
731
731
|
const member = (current.children ?? []).find((child) => matches(elementValue(child)));
|
|
732
732
|
if (member === void 0) break;
|
|
733
733
|
text = applyEdits(text, removeMember(text, current, member));
|
|
734
734
|
}
|
|
735
|
-
const emptied = arrayNodeAt(text,
|
|
735
|
+
const emptied = arrayNodeAt(text, path5);
|
|
736
736
|
if (emptied !== null && (emptied.children ?? []).length === 0) {
|
|
737
|
-
const
|
|
737
|
+
const open2 = emptied.offset + 1;
|
|
738
738
|
const close = emptied.offset + emptied.length - 1;
|
|
739
|
-
if (close >
|
|
740
|
-
text = text.slice(0,
|
|
739
|
+
if (close > open2 && /^\s*$/.test(text.slice(open2, close))) {
|
|
740
|
+
text = text.slice(0, open2) + text.slice(close);
|
|
741
741
|
}
|
|
742
742
|
}
|
|
743
743
|
return text;
|
|
@@ -1222,7 +1222,7 @@ function decodeClientConfigBytes(bytes, format) {
|
|
|
1222
1222
|
};
|
|
1223
1223
|
}
|
|
1224
1224
|
async function readClientConfigFile(site, opts = {}) {
|
|
1225
|
-
const load = opts.readFile ?? ((
|
|
1225
|
+
const load = opts.readFile ?? ((path5) => readFile(path5));
|
|
1226
1226
|
let loaded;
|
|
1227
1227
|
try {
|
|
1228
1228
|
loaded = await load(site.resolved.absolute);
|
|
@@ -1874,7 +1874,7 @@ function decodeBasicBody(body) {
|
|
|
1874
1874
|
return out;
|
|
1875
1875
|
}
|
|
1876
1876
|
function readKeyPath(text, i) {
|
|
1877
|
-
const
|
|
1877
|
+
const path5 = [];
|
|
1878
1878
|
let pos = i;
|
|
1879
1879
|
for (; ; ) {
|
|
1880
1880
|
while (isHorizontalSpace2(text[pos])) pos++;
|
|
@@ -1882,17 +1882,17 @@ function readKeyPath(text, i) {
|
|
|
1882
1882
|
if (c === '"') {
|
|
1883
1883
|
const body = readQuoted(text, pos, '"');
|
|
1884
1884
|
if (body === null) break;
|
|
1885
|
-
|
|
1885
|
+
path5.push(decodeBasicBody(body.body));
|
|
1886
1886
|
pos = body.end;
|
|
1887
1887
|
} else if (c === "'") {
|
|
1888
1888
|
const body = readQuoted(text, pos, "'");
|
|
1889
1889
|
if (body === null) break;
|
|
1890
|
-
|
|
1890
|
+
path5.push(body.body);
|
|
1891
1891
|
pos = body.end;
|
|
1892
1892
|
} else if (isBareKeyChar(c)) {
|
|
1893
1893
|
let j = pos;
|
|
1894
1894
|
while (isBareKeyChar(text[j])) j++;
|
|
1895
|
-
|
|
1895
|
+
path5.push(text.slice(pos, j));
|
|
1896
1896
|
pos = j;
|
|
1897
1897
|
} else {
|
|
1898
1898
|
break;
|
|
@@ -1906,7 +1906,7 @@ function readKeyPath(text, i) {
|
|
|
1906
1906
|
pos = after;
|
|
1907
1907
|
break;
|
|
1908
1908
|
}
|
|
1909
|
-
return { path:
|
|
1909
|
+
return { path: path5, end: pos };
|
|
1910
1910
|
}
|
|
1911
1911
|
function readQuoted(text, pos, quote) {
|
|
1912
1912
|
let i = pos + 1;
|
|
@@ -2089,7 +2089,7 @@ function scanSpan(text, from, to, state, depth) {
|
|
|
2089
2089
|
if (state === "basic" || state === "literal") state = "normal";
|
|
2090
2090
|
return { state, depth };
|
|
2091
2091
|
}
|
|
2092
|
-
var pathStartsWith = (
|
|
2092
|
+
var pathStartsWith = (path5, prefix) => prefix.length <= path5.length && prefix.every((seg, i) => path5[i] === seg);
|
|
2093
2093
|
var pathEquals = (a, b) => a.length === b.length && a.every((seg, i) => seg === b[i]);
|
|
2094
2094
|
function tomlKey(key) {
|
|
2095
2095
|
return /^[A-Za-z0-9_-]+$/.test(key) ? key : tomlString(key);
|
|
@@ -3152,27 +3152,27 @@ function patchPermissionsAllowText(raw, patterns, op) {
|
|
|
3152
3152
|
}
|
|
3153
3153
|
return text;
|
|
3154
3154
|
}
|
|
3155
|
-
async function settingsFingerprint(
|
|
3155
|
+
async function settingsFingerprint(path5) {
|
|
3156
3156
|
try {
|
|
3157
|
-
const st = await stat2(
|
|
3157
|
+
const st = await stat2(path5);
|
|
3158
3158
|
return { mtimeMs: st.mtimeMs, size: st.size };
|
|
3159
3159
|
} catch {
|
|
3160
3160
|
return null;
|
|
3161
3161
|
}
|
|
3162
3162
|
}
|
|
3163
3163
|
async function prepareClaudeCodeSettingsPatch(opts) {
|
|
3164
|
-
const
|
|
3164
|
+
const path5 = resolveClaudeCodeSettingsPath(opts.scope, {
|
|
3165
3165
|
home: opts.home,
|
|
3166
3166
|
projectDir: opts.projectDir,
|
|
3167
3167
|
claudeConfigDir: opts.claudeConfigDir
|
|
3168
3168
|
});
|
|
3169
|
-
if (!
|
|
3169
|
+
if (!path5) return null;
|
|
3170
3170
|
let existing = {};
|
|
3171
3171
|
let rawSettings = null;
|
|
3172
|
-
const fingerprint = await settingsFingerprint(
|
|
3172
|
+
const fingerprint = await settingsFingerprint(path5);
|
|
3173
3173
|
if (fingerprint !== null) {
|
|
3174
3174
|
try {
|
|
3175
|
-
const raw = await readFile2(
|
|
3175
|
+
const raw = await readFile2(path5, "utf8");
|
|
3176
3176
|
if (raw.trim().length > 0) {
|
|
3177
3177
|
const parsed = parseJsonc(raw);
|
|
3178
3178
|
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
@@ -3180,7 +3180,7 @@ async function prepareClaudeCodeSettingsPatch(opts) {
|
|
|
3180
3180
|
rawSettings = raw;
|
|
3181
3181
|
} else {
|
|
3182
3182
|
return {
|
|
3183
|
-
path:
|
|
3183
|
+
path: path5,
|
|
3184
3184
|
nextJson: "",
|
|
3185
3185
|
changed: false,
|
|
3186
3186
|
added: [],
|
|
@@ -3193,7 +3193,7 @@ async function prepareClaudeCodeSettingsPatch(opts) {
|
|
|
3193
3193
|
}
|
|
3194
3194
|
} catch (e) {
|
|
3195
3195
|
return {
|
|
3196
|
-
path:
|
|
3196
|
+
path: path5,
|
|
3197
3197
|
nextJson: "",
|
|
3198
3198
|
changed: false,
|
|
3199
3199
|
added: [],
|
|
@@ -3208,7 +3208,7 @@ async function prepareClaudeCodeSettingsPatch(opts) {
|
|
|
3208
3208
|
const merged = op === "remove" ? removePermissionsAllow(existing, [CLAUDE_CODE_ALLOW_PATTERN]) : mergePermissionsAllow(existing, [CLAUDE_CODE_ALLOW_PATTERN]);
|
|
3209
3209
|
const before = JSON.stringify(existing);
|
|
3210
3210
|
const after = JSON.stringify(merged);
|
|
3211
|
-
if (before === after) return { path:
|
|
3211
|
+
if (before === after) return { path: path5, nextJson: "", changed: false, added: [], removed: [], fingerprint };
|
|
3212
3212
|
const prevAllow = existing.permissions?.allow;
|
|
3213
3213
|
const prevAllowList = Array.isArray(prevAllow) ? prevAllow : [];
|
|
3214
3214
|
const added = op === "add" ? [CLAUDE_CODE_ALLOW_PATTERN].filter((p) => !prevAllowList.includes(p)) : [];
|
|
@@ -3217,7 +3217,7 @@ async function prepareClaudeCodeSettingsPatch(opts) {
|
|
|
3217
3217
|
const perms = existing.permissions;
|
|
3218
3218
|
if (perms !== void 0 && (typeof perms !== "object" || perms === null || Array.isArray(perms))) {
|
|
3219
3219
|
return {
|
|
3220
|
-
path:
|
|
3220
|
+
path: path5,
|
|
3221
3221
|
nextJson: "",
|
|
3222
3222
|
changed: false,
|
|
3223
3223
|
added: [],
|
|
@@ -3229,10 +3229,10 @@ async function prepareClaudeCodeSettingsPatch(opts) {
|
|
|
3229
3229
|
}
|
|
3230
3230
|
try {
|
|
3231
3231
|
const next = patchPermissionsAllowText(rawSettings, [CLAUDE_CODE_ALLOW_PATTERN], op);
|
|
3232
|
-
return { path:
|
|
3232
|
+
return { path: path5, nextJson: terminateWithNewline(next), changed: true, added, removed, fingerprint };
|
|
3233
3233
|
} catch (e) {
|
|
3234
3234
|
return {
|
|
3235
|
-
path:
|
|
3235
|
+
path: path5,
|
|
3236
3236
|
nextJson: "",
|
|
3237
3237
|
changed: false,
|
|
3238
3238
|
added: [],
|
|
@@ -3243,7 +3243,7 @@ async function prepareClaudeCodeSettingsPatch(opts) {
|
|
|
3243
3243
|
};
|
|
3244
3244
|
}
|
|
3245
3245
|
}
|
|
3246
|
-
return { path:
|
|
3246
|
+
return { path: path5, nextJson: `${JSON.stringify(merged, null, 2)}
|
|
3247
3247
|
`, changed: true, added, removed, fingerprint };
|
|
3248
3248
|
}
|
|
3249
3249
|
|
|
@@ -3762,6 +3762,10 @@ function blockedContainerFix(then, syntax = "JSON") {
|
|
|
3762
3762
|
return `make it ${syntax === "TOML" ? "a table" : "an object"} (or remove the key), then ${then}`;
|
|
3763
3763
|
}
|
|
3764
3764
|
|
|
3765
|
+
// src/oam-verdict-cache.ts
|
|
3766
|
+
import { readFileSync, statSync } from "fs";
|
|
3767
|
+
import path4 from "path";
|
|
3768
|
+
|
|
3765
3769
|
// src/paths.ts
|
|
3766
3770
|
import { realpath as realpath2, stat as stat3 } from "fs/promises";
|
|
3767
3771
|
import { homedir as homedir3 } from "os";
|
|
@@ -3875,9 +3879,581 @@ function guidePath(configDir) {
|
|
|
3875
3879
|
return path2.join(configDir, GUIDE_FILENAME);
|
|
3876
3880
|
}
|
|
3877
3881
|
|
|
3882
|
+
// src/persistence.ts
|
|
3883
|
+
import { readFile as readFile4 } from "fs/promises";
|
|
3884
|
+
import path3 from "path";
|
|
3885
|
+
|
|
3886
|
+
// src/file-lock.ts
|
|
3887
|
+
import { mkdir as mkdir2, open, readdir as readdir2, readFile as readFile3, rename as rename2, rm, stat as stat4 } from "fs/promises";
|
|
3888
|
+
import { basename as basename2, dirname, join as join9 } from "path";
|
|
3889
|
+
import { setTimeout as delay2 } from "timers/promises";
|
|
3890
|
+
var LOCK_SUFFIX = ".lock";
|
|
3891
|
+
var DEFAULT_LOCK_STALE_MS = 1e4;
|
|
3892
|
+
var DEFAULT_LOCK_WAIT_MS = 15e3;
|
|
3893
|
+
var LOCK_POLL_MS = 25;
|
|
3894
|
+
var DEFAULT_LOCK_TRANSIENT_MS = 1e3;
|
|
3895
|
+
var LOCK_FUTURE_SKEW_MS = 5e3;
|
|
3896
|
+
var lockSeq = 0;
|
|
3897
|
+
var FileLockTimeoutError = class extends Error {
|
|
3898
|
+
lockPath;
|
|
3899
|
+
/** How old the lock was at the last look, or null when it had vanished. */
|
|
3900
|
+
heldMs;
|
|
3901
|
+
constructor(message, lockPath, heldMs) {
|
|
3902
|
+
super(message);
|
|
3903
|
+
this.name = "FileLockTimeoutError";
|
|
3904
|
+
this.lockPath = lockPath;
|
|
3905
|
+
this.heldMs = heldMs;
|
|
3906
|
+
}
|
|
3907
|
+
};
|
|
3908
|
+
async function takeLock(lockPath, token, times) {
|
|
3909
|
+
let fh;
|
|
3910
|
+
try {
|
|
3911
|
+
fh = await open(lockPath, "wx");
|
|
3912
|
+
} catch (err) {
|
|
3913
|
+
if (err.code === "EEXIST") return false;
|
|
3914
|
+
throw err;
|
|
3915
|
+
}
|
|
3916
|
+
try {
|
|
3917
|
+
await fh.writeFile(token);
|
|
3918
|
+
if (times) await fh.utimes(times.atime, times.mtime).catch(() => void 0);
|
|
3919
|
+
await fh.close();
|
|
3920
|
+
} catch (err) {
|
|
3921
|
+
await fh.close().catch(() => {
|
|
3922
|
+
});
|
|
3923
|
+
await rm(lockPath, { force: true }).catch(() => {
|
|
3924
|
+
});
|
|
3925
|
+
throw err;
|
|
3926
|
+
}
|
|
3927
|
+
return true;
|
|
3928
|
+
}
|
|
3929
|
+
async function stealStaleLock(lockPath, isLive) {
|
|
3930
|
+
const stolenPath = `${lockPath}.stale-${process.pid}-${++lockSeq}`;
|
|
3931
|
+
try {
|
|
3932
|
+
await rename2(lockPath, stolenPath);
|
|
3933
|
+
} catch (err) {
|
|
3934
|
+
return err.code === "ENOENT";
|
|
3935
|
+
}
|
|
3936
|
+
let holder = null;
|
|
3937
|
+
try {
|
|
3938
|
+
const st = await stat4(stolenPath);
|
|
3939
|
+
if (isLive(Date.now() - st.mtimeMs)) {
|
|
3940
|
+
holder = { token: await readFile3(stolenPath, "utf8"), atime: st.atime, mtime: st.mtime };
|
|
3941
|
+
}
|
|
3942
|
+
} catch {
|
|
3943
|
+
}
|
|
3944
|
+
if (holder !== null) {
|
|
3945
|
+
await takeLock(lockPath, holder.token, { atime: holder.atime, mtime: holder.mtime }).catch(() => false);
|
|
3946
|
+
}
|
|
3947
|
+
await rm(stolenPath, { force: true }).catch(() => void 0);
|
|
3948
|
+
return true;
|
|
3949
|
+
}
|
|
3950
|
+
async function sweepStaleLitter(lockPath, staleMs) {
|
|
3951
|
+
const dir = dirname(lockPath);
|
|
3952
|
+
const prefix = `${basename2(lockPath)}.stale-`;
|
|
3953
|
+
let names;
|
|
3954
|
+
try {
|
|
3955
|
+
names = await readdir2(dir);
|
|
3956
|
+
} catch {
|
|
3957
|
+
return;
|
|
3958
|
+
}
|
|
3959
|
+
const now = Date.now();
|
|
3960
|
+
for (const name of names) {
|
|
3961
|
+
if (!name.startsWith(prefix)) continue;
|
|
3962
|
+
const litter = join9(dir, name);
|
|
3963
|
+
try {
|
|
3964
|
+
if (now - (await stat4(litter)).mtimeMs > staleMs) await rm(litter, { force: true });
|
|
3965
|
+
} catch {
|
|
3966
|
+
}
|
|
3967
|
+
}
|
|
3968
|
+
}
|
|
3969
|
+
async function releaseLock(lockPath, token) {
|
|
3970
|
+
try {
|
|
3971
|
+
if (await readFile3(lockPath, "utf8") !== token) return;
|
|
3972
|
+
await rm(lockPath, { force: true });
|
|
3973
|
+
} catch {
|
|
3974
|
+
}
|
|
3975
|
+
}
|
|
3976
|
+
async function withFileLock(path5, opts, describeTimeout, fn) {
|
|
3977
|
+
const lockPath = `${path5}${LOCK_SUFFIX}`;
|
|
3978
|
+
const staleMs = opts.lockStaleMs ?? DEFAULT_LOCK_STALE_MS;
|
|
3979
|
+
const waitMs = opts.lockWaitMs ?? DEFAULT_LOCK_WAIT_MS;
|
|
3980
|
+
const transientMs = opts.lockTransientMs ?? DEFAULT_LOCK_TRANSIENT_MS;
|
|
3981
|
+
const token = `${process.pid}-${++lockSeq}
|
|
3982
|
+
`;
|
|
3983
|
+
const isLive = (ageMs) => ageMs > -LOCK_FUTURE_SKEW_MS && ageMs < staleMs;
|
|
3984
|
+
await mkdir2(dirname(path5), { recursive: true });
|
|
3985
|
+
await sweepStaleLitter(lockPath, staleMs);
|
|
3986
|
+
const deadline = Date.now() + waitMs;
|
|
3987
|
+
let transientSince = null;
|
|
3988
|
+
for (; ; ) {
|
|
3989
|
+
let taken;
|
|
3990
|
+
try {
|
|
3991
|
+
taken = await takeLock(lockPath, token);
|
|
3992
|
+
} catch (err) {
|
|
3993
|
+
if (!isWin32TransientFsError(err)) throw err;
|
|
3994
|
+
const now = Date.now();
|
|
3995
|
+
transientSince ??= now;
|
|
3996
|
+
if (now >= deadline || now - transientSince >= transientMs) throw err;
|
|
3997
|
+
await delay2(LOCK_POLL_MS);
|
|
3998
|
+
continue;
|
|
3999
|
+
}
|
|
4000
|
+
if (taken) break;
|
|
4001
|
+
transientSince = null;
|
|
4002
|
+
let ageMs;
|
|
4003
|
+
try {
|
|
4004
|
+
ageMs = Date.now() - (await stat4(lockPath)).mtimeMs;
|
|
4005
|
+
} catch {
|
|
4006
|
+
ageMs = null;
|
|
4007
|
+
}
|
|
4008
|
+
if (ageMs !== null && !isLive(ageMs) && await stealStaleLock(lockPath, isLive)) continue;
|
|
4009
|
+
if (Date.now() >= deadline) throw new FileLockTimeoutError(describeTimeout(lockPath, ageMs), lockPath, ageMs);
|
|
4010
|
+
await delay2(LOCK_POLL_MS);
|
|
4011
|
+
}
|
|
4012
|
+
try {
|
|
4013
|
+
return await fn();
|
|
4014
|
+
} finally {
|
|
4015
|
+
await releaseLock(lockPath, token);
|
|
4016
|
+
}
|
|
4017
|
+
}
|
|
4018
|
+
|
|
4019
|
+
// src/json-key.ts
|
|
4020
|
+
function setJsonKey(target, key, value) {
|
|
4021
|
+
if (key === "__proto__") {
|
|
4022
|
+
Object.defineProperty(target, key, { value, writable: true, enumerable: true, configurable: true });
|
|
4023
|
+
return;
|
|
4024
|
+
}
|
|
4025
|
+
target[key] = value;
|
|
4026
|
+
}
|
|
4027
|
+
|
|
4028
|
+
// src/persistence.ts
|
|
4029
|
+
var STATE_SCHEMA_VERSION = 2;
|
|
4030
|
+
var STATE_FILENAME = "state.json";
|
|
4031
|
+
var DISABLE_PERSISTENCE_ENV = "YAW_MCP_DISABLE_PERSISTENCE";
|
|
4032
|
+
function isPersistenceDisabled(env = process.env) {
|
|
4033
|
+
const raw = env[DISABLE_PERSISTENCE_ENV]?.trim();
|
|
4034
|
+
if (raw === void 0 || raw === "") return false;
|
|
4035
|
+
return raw === "1" || raw.toLowerCase() === "true";
|
|
4036
|
+
}
|
|
4037
|
+
var READABLE_STATE_VERSIONS = /* @__PURE__ */ new Set([1, STATE_SCHEMA_VERSION]);
|
|
4038
|
+
function isReadableStateVersion(version) {
|
|
4039
|
+
return typeof version === "number" && READABLE_STATE_VERSIONS.has(version);
|
|
4040
|
+
}
|
|
4041
|
+
var TOOLCACHE_MAX_NAMESPACES = 64;
|
|
4042
|
+
var TOOLCACHE_MAX_TOOLS_PER_NAMESPACE = 512;
|
|
4043
|
+
var TOOLCACHE_MAX_DESCRIPTION_CHARS = 2e3;
|
|
4044
|
+
var TOOLCACHE_TTL_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
4045
|
+
var TOOLCACHE_MAX_META_CHARS = 128;
|
|
4046
|
+
var PREWARM_FAILURE_BACKOFF_MS = 60 * 60 * 1e3;
|
|
4047
|
+
var PREWARM_FAILURE_MAX_ENTRIES = 64;
|
|
4048
|
+
var PREWARM_FAILURE_MAX_MESSAGE_CHARS = 300;
|
|
4049
|
+
function statePath(configDir = userConfigDir()) {
|
|
4050
|
+
return path3.join(configDir, STATE_FILENAME);
|
|
4051
|
+
}
|
|
4052
|
+
function emptyState() {
|
|
4053
|
+
return { version: STATE_SCHEMA_VERSION, savedAt: 0, learning: {}, packHistory: [], toolCache: {} };
|
|
4054
|
+
}
|
|
4055
|
+
var NO_RAW_COUNTS = { learning: 0, packHistory: 0, toolCache: 0, prewarmFailures: 0 };
|
|
4056
|
+
async function loadState(filePath = statePath()) {
|
|
4057
|
+
return (await loadStateClassified(filePath)).state;
|
|
4058
|
+
}
|
|
4059
|
+
async function loadStateClassified(filePath = statePath()) {
|
|
4060
|
+
let raw;
|
|
4061
|
+
try {
|
|
4062
|
+
raw = await readFile4(filePath, "utf8");
|
|
4063
|
+
} catch (err) {
|
|
4064
|
+
if (isFileNotFound(err) || err.code === "ENOTDIR")
|
|
4065
|
+
return { state: emptyState(), rawCounts: NO_RAW_COUNTS, parsedCleanly: true };
|
|
4066
|
+
log("warn", "Could not read yaw-mcp state file; flagged unreadable so a save cannot overwrite it", {
|
|
4067
|
+
error: errorMessage(err)
|
|
4068
|
+
});
|
|
4069
|
+
return { state: { ...emptyState(), loadFailed: true }, rawCounts: NO_RAW_COUNTS, parsedCleanly: false };
|
|
4070
|
+
}
|
|
4071
|
+
try {
|
|
4072
|
+
const parsed = JSON.parse(raw.charCodeAt(0) === 65279 ? raw.slice(1) : raw);
|
|
4073
|
+
if (!parsed || typeof parsed !== "object")
|
|
4074
|
+
return { state: emptyState(), rawCounts: NO_RAW_COUNTS, parsedCleanly: false };
|
|
4075
|
+
if (!isReadableStateVersion(parsed.version))
|
|
4076
|
+
return { state: emptyState(), rawCounts: NO_RAW_COUNTS, parsedCleanly: false };
|
|
4077
|
+
const p = parsed;
|
|
4078
|
+
return {
|
|
4079
|
+
state: {
|
|
4080
|
+
version: STATE_SCHEMA_VERSION,
|
|
4081
|
+
savedAt: typeof p.savedAt === "number" ? p.savedAt : 0,
|
|
4082
|
+
learning: sanitizeLearning(p.learning),
|
|
4083
|
+
packHistory: sanitizePackHistory(p.packHistory),
|
|
4084
|
+
// Absent on a v1 file -> sanitizeToolCache(undefined) -> {}. That IS
|
|
4085
|
+
// the v1 -> v2 migration; no other field changed shape.
|
|
4086
|
+
toolCache: sanitizeToolCache(p.toolCache),
|
|
4087
|
+
...withPrewarmFailures(sanitizePrewarmFailures(p.prewarmFailures))
|
|
4088
|
+
},
|
|
4089
|
+
// Counted off `p`, BEFORE the sanitizers above run: what the file held,
|
|
4090
|
+
// not what survived. See RawStateCounts.
|
|
4091
|
+
rawCounts: {
|
|
4092
|
+
learning: countRawEntries(p.learning),
|
|
4093
|
+
packHistory: countRawEntries(p.packHistory),
|
|
4094
|
+
toolCache: countRawEntries(p.toolCache),
|
|
4095
|
+
prewarmFailures: countRawEntries(p.prewarmFailures)
|
|
4096
|
+
},
|
|
4097
|
+
parsedCleanly: true
|
|
4098
|
+
};
|
|
4099
|
+
} catch (err) {
|
|
4100
|
+
log("warn", "Failed to load yaw-mcp state, starting fresh", { error: errorMessage(err) });
|
|
4101
|
+
return { state: emptyState(), rawCounts: NO_RAW_COUNTS, parsedCleanly: false };
|
|
4102
|
+
}
|
|
4103
|
+
}
|
|
4104
|
+
function countRawEntries(input) {
|
|
4105
|
+
if (!input || typeof input !== "object") return 0;
|
|
4106
|
+
return Array.isArray(input) ? input.length : Object.keys(input).length;
|
|
4107
|
+
}
|
|
4108
|
+
var saveChain = Promise.resolve();
|
|
4109
|
+
function chained(fn) {
|
|
4110
|
+
const next = saveChain.then(fn);
|
|
4111
|
+
saveChain = next.then(
|
|
4112
|
+
() => void 0,
|
|
4113
|
+
() => void 0
|
|
4114
|
+
);
|
|
4115
|
+
return next;
|
|
4116
|
+
}
|
|
4117
|
+
var STATE_LOCK_WAIT_MS = 1500;
|
|
4118
|
+
var STATE_LOCK_STALE_MS = 1e4;
|
|
4119
|
+
function stateLockTimeoutMessage(lockPath, heldMs) {
|
|
4120
|
+
const held = heldMs === null ? "" : ` for ${Math.round(heldMs / 1e3)}s`;
|
|
4121
|
+
return `state.json is locked by another yaw-mcp process (${lockPath}, held${held})`;
|
|
4122
|
+
}
|
|
4123
|
+
function lockOptions(lock) {
|
|
4124
|
+
return {
|
|
4125
|
+
lockWaitMs: lock.lockWaitMs ?? STATE_LOCK_WAIT_MS,
|
|
4126
|
+
lockStaleMs: lock.lockStaleMs ?? STATE_LOCK_STALE_MS,
|
|
4127
|
+
lockTransientMs: lock.lockTransientMs
|
|
4128
|
+
};
|
|
4129
|
+
}
|
|
4130
|
+
function buildPayload(state) {
|
|
4131
|
+
return {
|
|
4132
|
+
version: STATE_SCHEMA_VERSION,
|
|
4133
|
+
savedAt: Date.now(),
|
|
4134
|
+
// Sanitize on the way out too: the caps must hold for the bytes we
|
|
4135
|
+
// WRITE, not merely for what a later load is willing to read back, and
|
|
4136
|
+
// they hold for every section -- not only the tool cache -- so the bound
|
|
4137
|
+
// on the file is this module's guarantee rather than each writer's.
|
|
4138
|
+
learning: capLearning(sanitizeLearning(state.learning)),
|
|
4139
|
+
packHistory: capPackHistory(sanitizePackHistory(state.packHistory)),
|
|
4140
|
+
toolCache: sanitizeToolCache(state.toolCache),
|
|
4141
|
+
...withPrewarmFailures(sanitizePrewarmFailures(state.prewarmFailures))
|
|
4142
|
+
};
|
|
4143
|
+
}
|
|
4144
|
+
async function writeStatePayload(payload, filePath) {
|
|
4145
|
+
await atomicWriteFile(filePath, JSON.stringify(payload, null, 2));
|
|
4146
|
+
}
|
|
4147
|
+
function mergePrewarmFailures(ours, disk, toolCache) {
|
|
4148
|
+
const merged = {};
|
|
4149
|
+
for (const [ns, f] of Object.entries(disk)) setJsonKey(merged, ns, f);
|
|
4150
|
+
for (const [ns, f] of Object.entries(ours)) {
|
|
4151
|
+
const other = ownValue(merged, ns);
|
|
4152
|
+
if (other === void 0 || f.failedAt >= other.failedAt) setJsonKey(merged, ns, f);
|
|
4153
|
+
}
|
|
4154
|
+
const out = {};
|
|
4155
|
+
for (const [ns, f] of Object.entries(merged)) {
|
|
4156
|
+
const learned = ownValue(toolCache, ns);
|
|
4157
|
+
if (learned !== void 0 && learned.learnedAt >= f.failedAt) continue;
|
|
4158
|
+
setJsonKey(out, ns, f);
|
|
4159
|
+
}
|
|
4160
|
+
return out;
|
|
4161
|
+
}
|
|
4162
|
+
function ownValue(map, key) {
|
|
4163
|
+
return Object.hasOwn(map, key) ? map[key] : void 0;
|
|
4164
|
+
}
|
|
4165
|
+
function packKey(c) {
|
|
4166
|
+
return `${c.namespace}\0${c.toolName}\0${c.at}`;
|
|
4167
|
+
}
|
|
4168
|
+
function countKeys(calls) {
|
|
4169
|
+
const counts = /* @__PURE__ */ new Map();
|
|
4170
|
+
for (const c of calls) counts.set(packKey(c), (counts.get(packKey(c)) ?? 0) + 1);
|
|
4171
|
+
return counts;
|
|
4172
|
+
}
|
|
4173
|
+
function takeOne(counts, key) {
|
|
4174
|
+
const n = counts.get(key) ?? 0;
|
|
4175
|
+
if (n === 0) return false;
|
|
4176
|
+
counts.set(key, n - 1);
|
|
4177
|
+
return true;
|
|
4178
|
+
}
|
|
4179
|
+
function mergeStateDelta(disk, baseline, current) {
|
|
4180
|
+
const learning = {};
|
|
4181
|
+
for (const [ns, u] of Object.entries(disk.learning)) setJsonKey(learning, ns, { ...u });
|
|
4182
|
+
for (const [ns, cur] of Object.entries(current.learning)) {
|
|
4183
|
+
const base = ownValue(baseline.learning, ns);
|
|
4184
|
+
const dDispatched = cur.dispatched - (base?.dispatched ?? 0);
|
|
4185
|
+
const dSucceeded = cur.succeeded - (base?.succeeded ?? 0);
|
|
4186
|
+
const touched = dDispatched !== 0 || dSucceeded !== 0 || cur.lastUsedAt > (base?.lastUsedAt ?? -1);
|
|
4187
|
+
if (!touched) continue;
|
|
4188
|
+
const prev = ownValue(learning, ns) ?? { dispatched: 0, succeeded: 0, lastUsedAt: 0 };
|
|
4189
|
+
const dispatched = Math.max(0, prev.dispatched + dDispatched);
|
|
4190
|
+
const succeeded = Math.min(dispatched, Math.max(0, prev.succeeded + dSucceeded));
|
|
4191
|
+
setJsonKey(learning, ns, { dispatched, succeeded, lastUsedAt: Math.max(prev.lastUsedAt, cur.lastUsedAt) });
|
|
4192
|
+
}
|
|
4193
|
+
const inBaseline = countKeys(baseline.packHistory);
|
|
4194
|
+
const onDisk = countKeys(disk.packHistory);
|
|
4195
|
+
const packHistory = disk.packHistory.map((c) => ({ ...c }));
|
|
4196
|
+
for (const c of current.packHistory) {
|
|
4197
|
+
const k = packKey(c);
|
|
4198
|
+
if (takeOne(inBaseline, k) || takeOne(onDisk, k)) continue;
|
|
4199
|
+
packHistory.push({ namespace: c.namespace, toolName: c.toolName, at: c.at });
|
|
4200
|
+
}
|
|
4201
|
+
packHistory.sort((a, b) => a.at - b.at);
|
|
4202
|
+
const toolCache = {};
|
|
4203
|
+
for (const [ns, e] of Object.entries(disk.toolCache)) setJsonKey(toolCache, ns, e);
|
|
4204
|
+
for (const [ns, cur] of Object.entries(current.toolCache ?? {})) {
|
|
4205
|
+
const diskEntry = ownValue(toolCache, ns);
|
|
4206
|
+
if (diskEntry !== void 0) {
|
|
4207
|
+
if (cur.learnedAt > diskEntry.learnedAt) setJsonKey(toolCache, ns, cur);
|
|
4208
|
+
continue;
|
|
4209
|
+
}
|
|
4210
|
+
const base = ownValue(baseline.toolCache ?? {}, ns);
|
|
4211
|
+
if (base === void 0 || cur.learnedAt > base.learnedAt) setJsonKey(toolCache, ns, cur);
|
|
4212
|
+
}
|
|
4213
|
+
const ourFailures = {};
|
|
4214
|
+
for (const [ns, cur] of Object.entries(current.prewarmFailures ?? {})) {
|
|
4215
|
+
const base = ownValue(baseline.prewarmFailures ?? {}, ns);
|
|
4216
|
+
if (base === void 0 || cur.failedAt > base.failedAt) setJsonKey(ourFailures, ns, cur);
|
|
4217
|
+
}
|
|
4218
|
+
const prewarmFailures = mergePrewarmFailures(ourFailures, disk.prewarmFailures ?? {}, toolCache);
|
|
4219
|
+
return { learning, packHistory: capPackHistory(packHistory), toolCache, ...withPrewarmFailures(prewarmFailures) };
|
|
4220
|
+
}
|
|
4221
|
+
var StateSync = class {
|
|
4222
|
+
baseline = { learning: {}, packHistory: [], toolCache: {} };
|
|
4223
|
+
filePath;
|
|
4224
|
+
lock;
|
|
4225
|
+
hooks;
|
|
4226
|
+
constructor(hooks, opts = {}) {
|
|
4227
|
+
this.hooks = hooks;
|
|
4228
|
+
this.filePath = opts.filePath;
|
|
4229
|
+
this.lock = opts.lock ?? {};
|
|
4230
|
+
}
|
|
4231
|
+
/** Record what this process loaded at startup. Pass the in-memory view
|
|
4232
|
+
* AFTER hydration (the stores apply their own caps and sorting), not the
|
|
4233
|
+
* raw file, or the first delta is computed against the wrong base. */
|
|
4234
|
+
setBaseline(state) {
|
|
4235
|
+
this.baseline = cloneSavable(state);
|
|
4236
|
+
}
|
|
4237
|
+
/** Save this process's pending changes. Resolves true when they reached
|
|
4238
|
+
* disk, false when they are still pending for the next save. */
|
|
4239
|
+
save() {
|
|
4240
|
+
return chained(() => this.saveLocked());
|
|
4241
|
+
}
|
|
4242
|
+
async saveLocked() {
|
|
4243
|
+
const filePath = this.filePath ?? statePath();
|
|
4244
|
+
let written;
|
|
4245
|
+
try {
|
|
4246
|
+
written = await withFileLock(filePath, lockOptions(this.lock), stateLockTimeoutMessage, async () => {
|
|
4247
|
+
const onDisk = await loadStateClassified(filePath);
|
|
4248
|
+
if (onDisk.state.loadFailed) return null;
|
|
4249
|
+
const snapshot = cloneSavable(this.hooks.exportCurrent());
|
|
4250
|
+
const merged = onDisk.parsedCleanly ? mergeStateDelta(onDisk.state, this.baseline, snapshot) : mergeStateDelta(emptyState(), { learning: {}, packHistory: [] }, snapshot);
|
|
4251
|
+
const payload = buildPayload(merged);
|
|
4252
|
+
await writeStatePayload(payload, filePath);
|
|
4253
|
+
return { payload, snapshot };
|
|
4254
|
+
});
|
|
4255
|
+
} catch (err) {
|
|
4256
|
+
log("warn", "yaw-mcp state save deferred; pending changes kept for the next save", {
|
|
4257
|
+
error: errorMessage(err),
|
|
4258
|
+
lockTimeout: err instanceof FileLockTimeoutError
|
|
4259
|
+
});
|
|
4260
|
+
return false;
|
|
4261
|
+
}
|
|
4262
|
+
if (written === null) {
|
|
4263
|
+
log("warn", "yaw-mcp state save deferred: state.json could not be re-read; pending changes kept");
|
|
4264
|
+
return false;
|
|
4265
|
+
}
|
|
4266
|
+
const refreshed = mergeStateDelta(written.payload, written.snapshot, this.hooks.exportCurrent());
|
|
4267
|
+
this.hooks.applyMerged(refreshed);
|
|
4268
|
+
this.baseline = cloneSavable({
|
|
4269
|
+
learning: written.payload.learning,
|
|
4270
|
+
packHistory: written.payload.packHistory,
|
|
4271
|
+
toolCache: written.snapshot.toolCache,
|
|
4272
|
+
prewarmFailures: written.snapshot.prewarmFailures
|
|
4273
|
+
});
|
|
4274
|
+
return true;
|
|
4275
|
+
}
|
|
4276
|
+
};
|
|
4277
|
+
function cloneSavable(s) {
|
|
4278
|
+
const learning = {};
|
|
4279
|
+
for (const [ns, u] of Object.entries(s.learning)) setJsonKey(learning, ns, { ...u });
|
|
4280
|
+
const toolCache = {};
|
|
4281
|
+
for (const [ns, e] of Object.entries(s.toolCache ?? {}))
|
|
4282
|
+
setJsonKey(toolCache, ns, { ...e, tools: e.tools.map((t) => ({ ...t })) });
|
|
4283
|
+
const prewarmFailures = {};
|
|
4284
|
+
for (const [ns, f] of Object.entries(s.prewarmFailures ?? {})) setJsonKey(prewarmFailures, ns, { ...f });
|
|
4285
|
+
return { learning, packHistory: s.packHistory.map((c) => ({ ...c })), toolCache, prewarmFailures };
|
|
4286
|
+
}
|
|
4287
|
+
var PACK_HISTORY_MAX_ENTRIES = 100;
|
|
4288
|
+
var LEARNING_MAX_NAMESPACES = 512;
|
|
4289
|
+
function capPackHistory(history) {
|
|
4290
|
+
return history.length > PACK_HISTORY_MAX_ENTRIES ? history.slice(-PACK_HISTORY_MAX_ENTRIES) : history;
|
|
4291
|
+
}
|
|
4292
|
+
function capLearning(learning) {
|
|
4293
|
+
const entries = Object.entries(learning);
|
|
4294
|
+
if (entries.length <= LEARNING_MAX_NAMESPACES) return learning;
|
|
4295
|
+
entries.sort((a, b) => b[1].lastUsedAt - a[1].lastUsedAt);
|
|
4296
|
+
const out = {};
|
|
4297
|
+
for (const [k, v] of entries.slice(0, LEARNING_MAX_NAMESPACES)) setJsonKey(out, k, v);
|
|
4298
|
+
return out;
|
|
4299
|
+
}
|
|
4300
|
+
function sanitizeLearning(input) {
|
|
4301
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) return {};
|
|
4302
|
+
const out = {};
|
|
4303
|
+
for (const [k, v] of Object.entries(input)) {
|
|
4304
|
+
if (!k) continue;
|
|
4305
|
+
if (!v || typeof v !== "object") continue;
|
|
4306
|
+
const u = v;
|
|
4307
|
+
if (typeof u.dispatched !== "number" || !Number.isFinite(u.dispatched) || u.dispatched < 0) continue;
|
|
4308
|
+
if (typeof u.succeeded !== "number" || !Number.isFinite(u.succeeded) || u.succeeded < 0) continue;
|
|
4309
|
+
if (typeof u.lastUsedAt !== "number" || !Number.isFinite(u.lastUsedAt) || u.lastUsedAt < 0) continue;
|
|
4310
|
+
const succeeded = Math.min(u.succeeded, u.dispatched);
|
|
4311
|
+
setJsonKey(out, k, { dispatched: u.dispatched, succeeded, lastUsedAt: u.lastUsedAt });
|
|
4312
|
+
}
|
|
4313
|
+
return out;
|
|
4314
|
+
}
|
|
4315
|
+
function sanitizePackHistory(input) {
|
|
4316
|
+
if (!Array.isArray(input)) return [];
|
|
4317
|
+
const out = [];
|
|
4318
|
+
for (const item of input) {
|
|
4319
|
+
if (!item || typeof item !== "object") continue;
|
|
4320
|
+
const c = item;
|
|
4321
|
+
if (typeof c.namespace !== "string" || !c.namespace) continue;
|
|
4322
|
+
if (typeof c.toolName !== "string" || !c.toolName) continue;
|
|
4323
|
+
if (typeof c.at !== "number" || !Number.isFinite(c.at) || c.at < 0) continue;
|
|
4324
|
+
out.push({ namespace: c.namespace, toolName: c.toolName, at: c.at });
|
|
4325
|
+
}
|
|
4326
|
+
return out;
|
|
4327
|
+
}
|
|
4328
|
+
function sanitizeToolCache(input) {
|
|
4329
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) return {};
|
|
4330
|
+
const now = Date.now();
|
|
4331
|
+
const kept = [];
|
|
4332
|
+
for (const [namespace, value] of Object.entries(input)) {
|
|
4333
|
+
if (!namespace) continue;
|
|
4334
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) continue;
|
|
4335
|
+
const entry = value;
|
|
4336
|
+
const learnedAt = entry.learnedAt;
|
|
4337
|
+
if (typeof learnedAt !== "number" || !Number.isFinite(learnedAt) || learnedAt < 0) continue;
|
|
4338
|
+
if (now - learnedAt > TOOLCACHE_TTL_MS) continue;
|
|
4339
|
+
if (!Array.isArray(entry.tools)) continue;
|
|
4340
|
+
const tools = [];
|
|
4341
|
+
for (const raw of entry.tools) {
|
|
4342
|
+
if (tools.length >= TOOLCACHE_MAX_TOOLS_PER_NAMESPACE) break;
|
|
4343
|
+
if (!raw || typeof raw !== "object") continue;
|
|
4344
|
+
const t = raw;
|
|
4345
|
+
if (typeof t.name !== "string" || !t.name) continue;
|
|
4346
|
+
const description = typeof t.description === "string" ? t.description.slice(0, TOOLCACHE_MAX_DESCRIPTION_CHARS) : void 0;
|
|
4347
|
+
tools.push(description === void 0 ? { name: t.name } : { name: t.name, description });
|
|
4348
|
+
}
|
|
4349
|
+
if (tools.length === 0 && Array.isArray(entry.tools) && entry.tools.length > 0) continue;
|
|
4350
|
+
const survivor = { tools, learnedAt };
|
|
4351
|
+
const configKey = metaString(entry.configKey);
|
|
4352
|
+
if (configKey !== void 0) survivor.configKey = configKey;
|
|
4353
|
+
const serverVersion = metaString(entry.serverVersion);
|
|
4354
|
+
if (serverVersion !== void 0) survivor.serverVersion = serverVersion;
|
|
4355
|
+
kept.push([namespace, survivor]);
|
|
4356
|
+
}
|
|
4357
|
+
if (kept.length > TOOLCACHE_MAX_NAMESPACES) {
|
|
4358
|
+
kept.sort((a, b) => b[1].learnedAt - a[1].learnedAt);
|
|
4359
|
+
kept.length = TOOLCACHE_MAX_NAMESPACES;
|
|
4360
|
+
}
|
|
4361
|
+
return Object.fromEntries(kept);
|
|
4362
|
+
}
|
|
4363
|
+
function metaString(value) {
|
|
4364
|
+
if (typeof value !== "string" || value.length === 0) return void 0;
|
|
4365
|
+
return value.slice(0, TOOLCACHE_MAX_META_CHARS);
|
|
4366
|
+
}
|
|
4367
|
+
function withPrewarmFailures(failures) {
|
|
4368
|
+
return Object.keys(failures).length > 0 ? { prewarmFailures: failures } : {};
|
|
4369
|
+
}
|
|
4370
|
+
function sanitizePrewarmFailures(input) {
|
|
4371
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) return {};
|
|
4372
|
+
const now = Date.now();
|
|
4373
|
+
const kept = [];
|
|
4374
|
+
for (const [namespace, value] of Object.entries(input)) {
|
|
4375
|
+
if (!namespace) continue;
|
|
4376
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) continue;
|
|
4377
|
+
const v = value;
|
|
4378
|
+
const failedAt = v.failedAt;
|
|
4379
|
+
if (typeof failedAt !== "number" || !Number.isFinite(failedAt) || failedAt < 0) continue;
|
|
4380
|
+
if (now - failedAt > PREWARM_FAILURE_BACKOFF_MS) continue;
|
|
4381
|
+
const configKey = metaString(v.configKey);
|
|
4382
|
+
if (configKey === void 0) continue;
|
|
4383
|
+
const message = typeof v.message === "string" ? v.message.slice(0, PREWARM_FAILURE_MAX_MESSAGE_CHARS) : "";
|
|
4384
|
+
kept.push([namespace, { failedAt, configKey, message }]);
|
|
4385
|
+
}
|
|
4386
|
+
if (kept.length > PREWARM_FAILURE_MAX_ENTRIES) {
|
|
4387
|
+
kept.sort((a, b) => b[1].failedAt - a[1].failedAt);
|
|
4388
|
+
kept.length = PREWARM_FAILURE_MAX_ENTRIES;
|
|
4389
|
+
}
|
|
4390
|
+
const out = {};
|
|
4391
|
+
for (const [ns, f] of kept) setJsonKey(out, ns, f);
|
|
4392
|
+
return out;
|
|
4393
|
+
}
|
|
4394
|
+
function isFileNotFound(err) {
|
|
4395
|
+
return !!err && typeof err === "object" && "code" in err && err.code === "ENOENT";
|
|
4396
|
+
}
|
|
4397
|
+
function errorMessage(err) {
|
|
4398
|
+
return err instanceof Error ? err.message : String(err);
|
|
4399
|
+
}
|
|
4400
|
+
|
|
4401
|
+
// src/oam-verdict-cache.ts
|
|
4402
|
+
var OAM_VERDICT_FILENAME = "oam-probe.json";
|
|
4403
|
+
var SCHEMA = 1;
|
|
4404
|
+
var MAX_ENTRIES = 16;
|
|
4405
|
+
function verdictFile() {
|
|
4406
|
+
return path4.join(userConfigDir(), OAM_VERDICT_FILENAME);
|
|
4407
|
+
}
|
|
4408
|
+
function oamBinaryIdentity(absPath) {
|
|
4409
|
+
try {
|
|
4410
|
+
const st = statSync(absPath);
|
|
4411
|
+
if (!st.isFile()) return null;
|
|
4412
|
+
return { path: absPath, size: st.size, mtimeMs: st.mtimeMs };
|
|
4413
|
+
} catch {
|
|
4414
|
+
return null;
|
|
4415
|
+
}
|
|
4416
|
+
}
|
|
4417
|
+
function readFileEntries() {
|
|
4418
|
+
try {
|
|
4419
|
+
const parsed = JSON.parse(readFileSync(verdictFile(), "utf8"));
|
|
4420
|
+
if (!parsed || parsed.schema !== SCHEMA || typeof parsed.entries !== "object" || parsed.entries === null) {
|
|
4421
|
+
return {};
|
|
4422
|
+
}
|
|
4423
|
+
return parsed.entries;
|
|
4424
|
+
} catch {
|
|
4425
|
+
return {};
|
|
4426
|
+
}
|
|
4427
|
+
}
|
|
4428
|
+
function keyOf(p) {
|
|
4429
|
+
return process.platform === "win32" ? p.toLowerCase() : p;
|
|
4430
|
+
}
|
|
4431
|
+
function readOamVerdict(id) {
|
|
4432
|
+
if (isPersistenceDisabled()) return void 0;
|
|
4433
|
+
const entry = readFileEntries()[keyOf(id.path)];
|
|
4434
|
+
if (!entry || entry.size !== id.size || entry.mtimeMs !== id.mtimeMs) return void 0;
|
|
4435
|
+
if (entry.version !== null && typeof entry.version !== "string") return void 0;
|
|
4436
|
+
return entry.version;
|
|
4437
|
+
}
|
|
4438
|
+
async function writeOamVerdict(id, version) {
|
|
4439
|
+
if (isPersistenceDisabled()) return;
|
|
4440
|
+
try {
|
|
4441
|
+
const entries = readFileEntries();
|
|
4442
|
+
entries[keyOf(id.path)] = { size: id.size, mtimeMs: id.mtimeMs, version, probedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
4443
|
+
const kept = Object.entries(entries).sort((a, b) => String(b[1].probedAt).localeCompare(String(a[1].probedAt))).slice(0, MAX_ENTRIES);
|
|
4444
|
+
const file = { schema: SCHEMA, entries: Object.fromEntries(kept) };
|
|
4445
|
+
await atomicWriteFile(verdictFile(), `${JSON.stringify(file, null, 2)}
|
|
4446
|
+
`);
|
|
4447
|
+
} catch (err) {
|
|
4448
|
+
log("debug", "could not record the oam --version verdict; the next launch probes again", {
|
|
4449
|
+
error: err instanceof Error ? err.message : String(err)
|
|
4450
|
+
});
|
|
4451
|
+
}
|
|
4452
|
+
}
|
|
4453
|
+
|
|
3878
4454
|
// src/stable-entry.ts
|
|
3879
4455
|
import { lstatSync, readdirSync, realpathSync } from "fs";
|
|
3880
|
-
import { dirname, isAbsolute as isAbsolute7, resolve as resolve7 } from "path";
|
|
4456
|
+
import { dirname as dirname2, isAbsolute as isAbsolute7, resolve as resolve7 } from "path";
|
|
3881
4457
|
function isEphemeralMountPath(p) {
|
|
3882
4458
|
const posix = p.replace(/\\/g, "/");
|
|
3883
4459
|
return posix.includes("/AppTranslocation/") || // AppImage's FUSE mount. Anchored on the `.mount_` segment the runtime
|
|
@@ -3913,7 +4489,7 @@ function stableSpellingOf(entry, deps = {}) {
|
|
|
3913
4489
|
}
|
|
3914
4490
|
function aliasSpellingOf(entry, target, deps) {
|
|
3915
4491
|
const realpath3 = deps.realpath ?? realpathSync;
|
|
3916
|
-
const
|
|
4492
|
+
const readdir3 = deps.readdir ?? ((p) => readdirSync(p));
|
|
3917
4493
|
const isSymlink = deps.isSymlink ?? ((p) => {
|
|
3918
4494
|
try {
|
|
3919
4495
|
return lstatSync(p).isSymbolicLink();
|
|
@@ -3922,14 +4498,14 @@ function aliasSpellingOf(entry, target, deps) {
|
|
|
3922
4498
|
}
|
|
3923
4499
|
});
|
|
3924
4500
|
for (const ancestor of ancestorsOf(entry)) {
|
|
3925
|
-
const parent =
|
|
4501
|
+
const parent = dirname2(ancestor);
|
|
3926
4502
|
if (parent === ancestor) continue;
|
|
3927
4503
|
const sepChar = endsWithSeparator(parent) ? "" : ancestor.charAt(parent.length);
|
|
3928
4504
|
const self = ancestor.slice(parent.length + sepChar.length);
|
|
3929
4505
|
if (!looksLikeVersionDir(self)) continue;
|
|
3930
4506
|
let names;
|
|
3931
4507
|
try {
|
|
3932
|
-
names =
|
|
4508
|
+
names = readdir3(parent);
|
|
3933
4509
|
} catch {
|
|
3934
4510
|
continue;
|
|
3935
4511
|
}
|
|
@@ -3959,9 +4535,9 @@ function endsWithSeparator(p) {
|
|
|
3959
4535
|
}
|
|
3960
4536
|
function ancestorsOf(entry) {
|
|
3961
4537
|
const out = [];
|
|
3962
|
-
let cur =
|
|
4538
|
+
let cur = dirname2(entry);
|
|
3963
4539
|
while (true) {
|
|
3964
|
-
const parent =
|
|
4540
|
+
const parent = dirname2(cur);
|
|
3965
4541
|
if (parent === cur) break;
|
|
3966
4542
|
out.push(cur);
|
|
3967
4543
|
cur = parent;
|
|
@@ -4008,7 +4584,7 @@ function npxSpec(args) {
|
|
|
4008
4584
|
const spec = args[idx];
|
|
4009
4585
|
return spec.startsWith("-") ? null : spec;
|
|
4010
4586
|
}
|
|
4011
|
-
var MIN_OAM_VERSION = "0.
|
|
4587
|
+
var MIN_OAM_VERSION = "0.18.0";
|
|
4012
4588
|
var OAM_INSTALL_SH = "curl -fsSL https://oamjs.org/install.sh | sh";
|
|
4013
4589
|
var OAM_INSTALL_PS1 = "irm https://oamjs.org/install.ps1 | iex";
|
|
4014
4590
|
function oamInstallCommand(os) {
|
|
@@ -4037,9 +4613,11 @@ var warnedOamUnavailable = false;
|
|
|
4037
4613
|
function oamFailureLabel(failure) {
|
|
4038
4614
|
if (failure === "timeout") return "`oam --version` did not answer in time";
|
|
4039
4615
|
if (failure === "exit") return "`oam --version` exited non-zero";
|
|
4616
|
+
if (failure === "build-tree") return "the binary is a local build-tree oam, not a release";
|
|
4040
4617
|
return "the binary could not be executed";
|
|
4041
4618
|
}
|
|
4042
4619
|
var oamProbeCache;
|
|
4620
|
+
var oamProbeCacheExpiresAt;
|
|
4043
4621
|
function parseOamVersion(out) {
|
|
4044
4622
|
const m = /\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?/.exec(out);
|
|
4045
4623
|
return m ? m[0] : null;
|
|
@@ -4091,13 +4669,19 @@ function winNormalize(p, platform = process.platform) {
|
|
|
4091
4669
|
}
|
|
4092
4670
|
function isExecutableFile(candidate, platform) {
|
|
4093
4671
|
try {
|
|
4094
|
-
if (!
|
|
4672
|
+
if (!statSync2(candidate).isFile()) return false;
|
|
4095
4673
|
if (platform !== "win32") accessSync(candidate, constants.X_OK);
|
|
4096
4674
|
return true;
|
|
4097
4675
|
} catch {
|
|
4098
4676
|
return false;
|
|
4099
4677
|
}
|
|
4100
4678
|
}
|
|
4679
|
+
var BUILD_TREE_SEGMENTS = /* @__PURE__ */ new Set(["target", "debug", "out"]);
|
|
4680
|
+
var BUILD_TREE_PREFIXES = ["cmake-build-"];
|
|
4681
|
+
function isBuildTreePath(p) {
|
|
4682
|
+
const parts = p.toLowerCase().split(/[\\/]+/);
|
|
4683
|
+
return parts.some((seg) => BUILD_TREE_SEGMENTS.has(seg) || BUILD_TREE_PREFIXES.some((pre) => seg.startsWith(pre)));
|
|
4684
|
+
}
|
|
4101
4685
|
function resolveBinAbsolute(bin, env = process.env, platform = process.platform) {
|
|
4102
4686
|
if (isAbsolute8(bin)) return isExecutableFile(bin, platform) ? winNormalize(bin, platform) : null;
|
|
4103
4687
|
if (bin.includes("/") || platform === "win32" && bin.includes("\\")) return null;
|
|
@@ -4110,7 +4694,7 @@ function resolveBinAbsolute(bin, env = process.env, platform = process.platform)
|
|
|
4110
4694
|
for (const dir of dirs) {
|
|
4111
4695
|
const clean = dir.replace(/^"|"$/g, "");
|
|
4112
4696
|
for (const ext of exts) {
|
|
4113
|
-
const candidate =
|
|
4697
|
+
const candidate = join10(clean, bin + ext);
|
|
4114
4698
|
if (!isAbsolute8(candidate)) continue;
|
|
4115
4699
|
if (isExecutableFile(candidate, platform)) return winNormalize(candidate, platform);
|
|
4116
4700
|
}
|
|
@@ -4126,7 +4710,8 @@ function classifyProbeFailure(err) {
|
|
|
4126
4710
|
if (code === "EOAMEXIT") return "exit";
|
|
4127
4711
|
return "spawn";
|
|
4128
4712
|
}
|
|
4129
|
-
var OAM_PROBE_TIMEOUT_MS =
|
|
4713
|
+
var OAM_PROBE_TIMEOUT_MS = 1e4;
|
|
4714
|
+
var OAM_PROBE_TIMEOUT_RECHECK_MS = 6e4;
|
|
4130
4715
|
var OAM_PROBE_KILL_SIGNAL = "SIGKILL";
|
|
4131
4716
|
var OAM_PROBE_MAX_OUTPUT = 8 * 1024;
|
|
4132
4717
|
var VERSION_CARRY = 128;
|
|
@@ -4230,6 +4815,10 @@ function spawnVersionProbe(bin) {
|
|
|
4230
4815
|
var oamProbeInFlight;
|
|
4231
4816
|
var oamProbeGeneration = 0;
|
|
4232
4817
|
async function probeOam(run = spawnVersionProbe) {
|
|
4818
|
+
if (oamProbeCache !== void 0 && oamProbeCacheExpiresAt !== void 0 && Date.now() >= oamProbeCacheExpiresAt) {
|
|
4819
|
+
oamProbeCache = void 0;
|
|
4820
|
+
oamProbeCacheExpiresAt = void 0;
|
|
4821
|
+
}
|
|
4233
4822
|
if (oamProbeCache !== void 0) return oamProbeCache;
|
|
4234
4823
|
if (oamProbeInFlight !== void 0) return oamProbeInFlight;
|
|
4235
4824
|
const generation = oamProbeGeneration;
|
|
@@ -4241,10 +4830,33 @@ async function probeOam(run = spawnVersionProbe) {
|
|
|
4241
4830
|
async function probeOamUncached(run, generation) {
|
|
4242
4831
|
const explicit = Boolean(process.env.OAM_BIN);
|
|
4243
4832
|
const bin = winNormalize(process.env.OAM_BIN || (process.platform === "win32" ? "oam.exe" : "oam"));
|
|
4244
|
-
const
|
|
4245
|
-
|
|
4833
|
+
const absResolved = resolveBinAbsolute(bin);
|
|
4834
|
+
const publish = (probe, ttlMs) => {
|
|
4835
|
+
if (generation === oamProbeGeneration) {
|
|
4836
|
+
oamProbeCache = probe;
|
|
4837
|
+
oamProbeCacheExpiresAt = ttlMs === void 0 ? void 0 : Date.now() + ttlMs;
|
|
4838
|
+
}
|
|
4246
4839
|
return probe;
|
|
4247
4840
|
};
|
|
4841
|
+
if (run === spawnVersionProbe && absResolved !== null && isBuildTreePath(absResolved)) {
|
|
4842
|
+
log("warn", "oam resolves to a local build-tree binary; falling back to node for this process", {
|
|
4843
|
+
bin: absResolved,
|
|
4844
|
+
fix: explicit ? "point OAM_BIN at the installed oam (~/.oam/bin)" : "put ~/.oam/bin ahead of the build tree on PATH",
|
|
4845
|
+
thenRestart: "restart yaw-mcp; this probe is cached for the process lifetime"
|
|
4846
|
+
});
|
|
4847
|
+
return publish({
|
|
4848
|
+
bin: null,
|
|
4849
|
+
binPath: null,
|
|
4850
|
+
version: null,
|
|
4851
|
+
belowMin: false,
|
|
4852
|
+
failure: "build-tree",
|
|
4853
|
+
failureDetail: `${absResolved} is a compiler output binary (build-tree), not a release`
|
|
4854
|
+
});
|
|
4855
|
+
}
|
|
4856
|
+
const identity = (() => {
|
|
4857
|
+
if (run !== spawnVersionProbe) return null;
|
|
4858
|
+
return absResolved ? oamBinaryIdentity(absResolved) : null;
|
|
4859
|
+
})();
|
|
4248
4860
|
const runRetryingTimeout = async () => {
|
|
4249
4861
|
try {
|
|
4250
4862
|
return await run(bin);
|
|
@@ -4258,7 +4870,15 @@ async function probeOamUncached(run, generation) {
|
|
|
4258
4870
|
}
|
|
4259
4871
|
};
|
|
4260
4872
|
try {
|
|
4261
|
-
const
|
|
4873
|
+
const remembered = identity ? readOamVerdict(identity) : void 0;
|
|
4874
|
+
let version;
|
|
4875
|
+
if (remembered !== void 0) {
|
|
4876
|
+
version = remembered;
|
|
4877
|
+
log("debug", "oam --version answered from the per-binary cache", { bin, oamVersion: version });
|
|
4878
|
+
} else {
|
|
4879
|
+
version = parseOamVersion(await runRetryingTimeout());
|
|
4880
|
+
if (identity) void writeOamVerdict(identity, version);
|
|
4881
|
+
}
|
|
4262
4882
|
if (version !== null && compareVersions(version, MIN_OAM_VERSION) < 0) {
|
|
4263
4883
|
log("warn", "oam is installed but below the minimum supported version; falling back to node", {
|
|
4264
4884
|
// The full token, prerelease suffix included: a build reporting
|
|
@@ -4290,9 +4910,10 @@ async function probeOamUncached(run, generation) {
|
|
|
4290
4910
|
}
|
|
4291
4911
|
return publish({
|
|
4292
4912
|
bin,
|
|
4293
|
-
// Resolved once, alongside the probe that proved the
|
|
4294
|
-
// install path never has to re-derive it (or spawn
|
|
4295
|
-
|
|
4913
|
+
// Resolved once (absResolved, above), alongside the probe that proved the
|
|
4914
|
+
// name spawns, so the install path never has to re-derive it (or spawn
|
|
4915
|
+
// `where`/`which`).
|
|
4916
|
+
binPath: absResolved,
|
|
4296
4917
|
version,
|
|
4297
4918
|
belowMin: false,
|
|
4298
4919
|
failure: null,
|
|
@@ -4303,10 +4924,15 @@ async function probeOamUncached(run, generation) {
|
|
|
4303
4924
|
const absent = code === "ENOENT" && !explicit;
|
|
4304
4925
|
const shellShim = isShellShimSpawnRefusal(code, bin);
|
|
4305
4926
|
if (code === "ETIMEDOUT") {
|
|
4306
|
-
log(
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4927
|
+
log(
|
|
4928
|
+
"warn",
|
|
4929
|
+
"oam did not respond to --version twice; falling back to node for now (probed again on a later connect)",
|
|
4930
|
+
{
|
|
4931
|
+
timeoutMs: OAM_PROBE_TIMEOUT_MS,
|
|
4932
|
+
recheckAfterMs: OAM_PROBE_TIMEOUT_RECHECK_MS,
|
|
4933
|
+
bin
|
|
4934
|
+
}
|
|
4935
|
+
);
|
|
4310
4936
|
} else if (shellShim) {
|
|
4311
4937
|
log(
|
|
4312
4938
|
"warn",
|
|
@@ -4324,14 +4950,17 @@ async function probeOamUncached(run, generation) {
|
|
|
4324
4950
|
error: err instanceof Error ? err.message : String(err)
|
|
4325
4951
|
});
|
|
4326
4952
|
}
|
|
4327
|
-
return publish(
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4953
|
+
return publish(
|
|
4954
|
+
{
|
|
4955
|
+
bin: null,
|
|
4956
|
+
binPath: null,
|
|
4957
|
+
version: null,
|
|
4958
|
+
belowMin: false,
|
|
4959
|
+
failure: absent ? null : classifyProbeFailure(err),
|
|
4960
|
+
failureDetail: absent ? null : shellShim ? `${bin} is a .cmd/.bat shim and the version probe spawns without a shell; point OAM_BIN at the .exe it wraps` : err instanceof Error ? err.message : String(err)
|
|
4961
|
+
},
|
|
4962
|
+
code === "ETIMEDOUT" ? OAM_PROBE_TIMEOUT_RECHECK_MS : void 0
|
|
4963
|
+
);
|
|
4335
4964
|
}
|
|
4336
4965
|
}
|
|
4337
4966
|
var WIN_EXE_EXT = /\.(?:exe|cmd|bat)$/i;
|
|
@@ -4400,10 +5029,61 @@ function rewriteForOam(command, args, deps) {
|
|
|
4400
5029
|
}
|
|
4401
5030
|
return { command, args };
|
|
4402
5031
|
}
|
|
5032
|
+
function managedNodeBin(env = process.env, platform = process.platform, execPath = process.execPath) {
|
|
5033
|
+
if (nodeLaunchKind(execPath) === "node") return execPath;
|
|
5034
|
+
return resolveBinAbsolute("node", env, platform);
|
|
5035
|
+
}
|
|
5036
|
+
function rewriteNpmToManagedNode(command, args, deps) {
|
|
5037
|
+
if (!deps.nodeBin) {
|
|
5038
|
+
log("debug", "no node binary located for the managed sidecar spawn; staying on npx", { command });
|
|
5039
|
+
return { command, args };
|
|
5040
|
+
}
|
|
5041
|
+
if (nodeLaunchKind(command) !== "npx") return { command, args };
|
|
5042
|
+
const specIdx = npxSpecIndex(args);
|
|
5043
|
+
const spec = specIdx === -1 ? void 0 : args[specIdx];
|
|
5044
|
+
if (!spec) return { command, args };
|
|
5045
|
+
if (spec.startsWith("-")) {
|
|
5046
|
+
log("debug", "npx launch carries flags yaw-mcp does not parse; staying on npx instead of the managed tree", {
|
|
5047
|
+
flag: spec,
|
|
5048
|
+
argc: args.length
|
|
5049
|
+
});
|
|
5050
|
+
return { command, args };
|
|
5051
|
+
}
|
|
5052
|
+
if (!isRegistrySpec(spec)) {
|
|
5053
|
+
log("debug", "npx spec is a git/path target, not a registry package; staying on npx instead of the managed tree", {
|
|
5054
|
+
spec
|
|
5055
|
+
});
|
|
5056
|
+
return { command, args };
|
|
5057
|
+
}
|
|
5058
|
+
const constraint = specConstraint(spec);
|
|
5059
|
+
if (constraint.kind === "range") {
|
|
5060
|
+
log("debug", "npx spec constrains the version with a range yaw-mcp cannot evaluate; staying on npx", {
|
|
5061
|
+
package: packageName(spec),
|
|
5062
|
+
range: constraint.raw
|
|
5063
|
+
});
|
|
5064
|
+
return { command, args };
|
|
5065
|
+
}
|
|
5066
|
+
const wantVersion = constraint.kind === "exact" ? constraint.version : null;
|
|
5067
|
+
const entry = deps.resolveEntry(packageName(spec), wantVersion);
|
|
5068
|
+
if (!entry) {
|
|
5069
|
+
if (wantVersion !== null) {
|
|
5070
|
+
log("debug", "no on-disk copy declares the pinned version; staying on npx so the pin is honoured", {
|
|
5071
|
+
package: packageName(spec),
|
|
5072
|
+
version: wantVersion
|
|
5073
|
+
});
|
|
5074
|
+
} else {
|
|
5075
|
+
log("debug", "npx package has no on-disk entry; staying on npx instead of the managed tree", {
|
|
5076
|
+
package: packageName(spec)
|
|
5077
|
+
});
|
|
5078
|
+
}
|
|
5079
|
+
return { command, args };
|
|
5080
|
+
}
|
|
5081
|
+
return { command: deps.nodeBin, args: [entry, ...args.slice(specIdx + 1)] };
|
|
5082
|
+
}
|
|
4403
5083
|
var NPX_CACHE_MARKER = `${sep}_npx${sep}`;
|
|
4404
5084
|
function npxHashNodeModules(npxRoot) {
|
|
4405
5085
|
try {
|
|
4406
|
-
return readdirSync2(npxRoot, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) =>
|
|
5086
|
+
return readdirSync2(npxRoot, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => join10(npxRoot, e.name, "node_modules"));
|
|
4407
5087
|
} catch {
|
|
4408
5088
|
return [];
|
|
4409
5089
|
}
|
|
@@ -4454,7 +5134,7 @@ function npmrcValue(raw) {
|
|
|
4454
5134
|
function npmrcCache(file) {
|
|
4455
5135
|
let text;
|
|
4456
5136
|
try {
|
|
4457
|
-
text =
|
|
5137
|
+
text = readFileSync2(file, "utf8");
|
|
4458
5138
|
} catch {
|
|
4459
5139
|
return null;
|
|
4460
5140
|
}
|
|
@@ -4464,7 +5144,7 @@ function npmrcCache(file) {
|
|
|
4464
5144
|
if (!m) continue;
|
|
4465
5145
|
const value = expandNpmrcEnv(npmrcValue(m[1]));
|
|
4466
5146
|
if (!value) continue;
|
|
4467
|
-
last = value.startsWith("~/") ?
|
|
5147
|
+
last = value.startsWith("~/") ? join10(homedir4(), value.slice(2)) : value;
|
|
4468
5148
|
}
|
|
4469
5149
|
return last;
|
|
4470
5150
|
}
|
|
@@ -4487,34 +5167,34 @@ function resolveNpmCacheDir(fromUrl) {
|
|
|
4487
5167
|
const envKey = Object.keys(process.env).find((k) => /^npm_config_cache$/i.test(k));
|
|
4488
5168
|
const fromEnv = envKey === void 0 ? void 0 : process.env[envKey];
|
|
4489
5169
|
if (fromEnv) return fromEnv;
|
|
4490
|
-
const candidates = [
|
|
5170
|
+
const candidates = [join10(homedir4(), ".npmrc")];
|
|
4491
5171
|
for (const nodeModules of ownNodeModules(fromUrl)) {
|
|
4492
5172
|
candidates.push(
|
|
4493
|
-
|
|
5173
|
+
join10(dirname3(nodeModules), "etc", "npmrc"),
|
|
4494
5174
|
// <prefix>/node_modules (Windows)
|
|
4495
|
-
|
|
5175
|
+
join10(dirname3(dirname3(nodeModules)), "etc", "npmrc"),
|
|
4496
5176
|
// <prefix>/lib/node_modules (POSIX)
|
|
4497
|
-
|
|
5177
|
+
join10(nodeModules, "npm", "npmrc")
|
|
4498
5178
|
// builtin, beside npm itself
|
|
4499
5179
|
);
|
|
4500
5180
|
}
|
|
4501
|
-
const nodeDir =
|
|
5181
|
+
const nodeDir = dirname3(process.execPath);
|
|
4502
5182
|
candidates.push(
|
|
4503
|
-
|
|
5183
|
+
join10(nodeDir, "node_modules", "npm", "npmrc"),
|
|
4504
5184
|
// Windows layout
|
|
4505
|
-
|
|
5185
|
+
join10(nodeDir, "..", "lib", "node_modules", "npm", "npmrc")
|
|
4506
5186
|
// POSIX layout
|
|
4507
5187
|
);
|
|
4508
5188
|
for (const file of candidates) {
|
|
4509
5189
|
const cache = npmrcCache(file);
|
|
4510
5190
|
if (cache) return cache;
|
|
4511
5191
|
}
|
|
4512
|
-
const fallback = process.platform === "win32" ? process.env.LOCALAPPDATA &&
|
|
5192
|
+
const fallback = process.platform === "win32" ? process.env.LOCALAPPDATA && join10(process.env.LOCALAPPDATA, "npm-cache") : join10(homedir4(), ".npm");
|
|
4513
5193
|
return fallback && existsSync2(fallback) ? fallback : null;
|
|
4514
5194
|
}
|
|
4515
5195
|
function npmCacheNpxNodeModules(cacheDir2) {
|
|
4516
5196
|
if (!cacheDir2) return [];
|
|
4517
|
-
return npxHashNodeModules(
|
|
5197
|
+
return npxHashNodeModules(join10(cacheDir2, "_npx"));
|
|
4518
5198
|
}
|
|
4519
5199
|
function ownNodeModules(fromUrl = import.meta.url) {
|
|
4520
5200
|
let here;
|
|
@@ -4529,11 +5209,11 @@ function ownNodeModules(fromUrl = import.meta.url) {
|
|
|
4529
5209
|
return [here.slice(0, idx + seg.length - sep.length)];
|
|
4530
5210
|
}
|
|
4531
5211
|
function packageEntry(pkgDir, pkg) {
|
|
4532
|
-
const pjPath =
|
|
5212
|
+
const pjPath = join10(pkgDir, "package.json");
|
|
4533
5213
|
if (!existsSync2(pjPath)) return null;
|
|
4534
5214
|
let parsed;
|
|
4535
5215
|
try {
|
|
4536
|
-
parsed = JSON.parse(
|
|
5216
|
+
parsed = JSON.parse(readFileSync2(pjPath, "utf8"));
|
|
4537
5217
|
} catch {
|
|
4538
5218
|
return null;
|
|
4539
5219
|
}
|
|
@@ -4550,7 +5230,7 @@ function packageEntry(pkgDir, pkg) {
|
|
|
4550
5230
|
}
|
|
4551
5231
|
if (!rel && typeof j.main === "string") rel = j.main;
|
|
4552
5232
|
if (typeof rel !== "string" || rel.length === 0) return null;
|
|
4553
|
-
const entry = isAbsolute8(rel) ? rel :
|
|
5233
|
+
const entry = isAbsolute8(rel) ? rel : join10(pkgDir, rel);
|
|
4554
5234
|
if (!existsSync2(entry)) return null;
|
|
4555
5235
|
return {
|
|
4556
5236
|
entry,
|
|
@@ -4575,7 +5255,7 @@ function resolveNpmEntry(pkg, fromUrl = import.meta.url, npmCache = npmCacheDir(
|
|
|
4575
5255
|
...ownDurable.map((nodeModules) => ({ nodeModules, source: "durable" }))
|
|
4576
5256
|
];
|
|
4577
5257
|
for (const { nodeModules, source } of durable) {
|
|
4578
|
-
const hit = packageEntry(
|
|
5258
|
+
const hit = packageEntry(join10(nodeModules, ...parts), pkg);
|
|
4579
5259
|
if (!hit) continue;
|
|
4580
5260
|
if (wantVersion !== null && !satisfiesExactPin(hit.version, wantVersion)) continue;
|
|
4581
5261
|
notePinnedSidecar(pkg, hit.version, source, nodeModules);
|
|
@@ -4586,7 +5266,7 @@ function resolveNpmEntry(pkg, fromUrl = import.meta.url, npmCache = npmCacheDir(
|
|
|
4586
5266
|
let bestRoot = null;
|
|
4587
5267
|
let bestParsed = null;
|
|
4588
5268
|
for (const nodeModules of roots) {
|
|
4589
|
-
const hit = packageEntry(
|
|
5269
|
+
const hit = packageEntry(join10(nodeModules, ...parts), pkg);
|
|
4590
5270
|
if (!hit) continue;
|
|
4591
5271
|
if (wantVersion !== null && !satisfiesExactPin(hit.version, wantVersion)) continue;
|
|
4592
5272
|
const parsed = hit.version === null ? null : parseSemver(hit.version);
|
|
@@ -4647,7 +5327,7 @@ function resolveStableNpmEntry(pkg, fromUrl = import.meta.url, deps = {}) {
|
|
|
4647
5327
|
for (const nodeModules of ownNodeModules(fromUrl)) {
|
|
4648
5328
|
if (nodeModules.includes(NPX_CACHE_MARKER)) continue;
|
|
4649
5329
|
if (isEphemeralMountPath(nodeModules)) continue;
|
|
4650
|
-
const hit = packageEntry(
|
|
5330
|
+
const hit = packageEntry(join10(nodeModules, ...pkg.split("/")), pkg);
|
|
4651
5331
|
if (hit) return stableSpellingOf(hit.entry, deps);
|
|
4652
5332
|
}
|
|
4653
5333
|
return null;
|
|
@@ -4664,9 +5344,10 @@ async function resolveOamSpawn(command, args, optedIn = true) {
|
|
|
4664
5344
|
detail: probe.failureDetail,
|
|
4665
5345
|
// No install commands: the install is already there. Pointing OAM_BIN
|
|
4666
5346
|
// at a working copy is the actionable move, and the probe is cached for
|
|
4667
|
-
// the process lifetime, so repairing it needs a restart to take effect
|
|
5347
|
+
// the process lifetime, so repairing it needs a restart to take effect
|
|
5348
|
+
// -- except a timeout, which lapses and is probed again on its own.
|
|
4668
5349
|
overrideWith: "OAM_BIN",
|
|
4669
|
-
thenRestart: "restart yaw-mcp; this probe is cached for the process lifetime"
|
|
5350
|
+
thenRestart: probe.failure === "timeout" ? `nothing to do if oam was only slow: the probe runs again after ${OAM_PROBE_TIMEOUT_RECHECK_MS / 1e3}s` : "restart yaw-mcp; this probe is cached for the process lifetime"
|
|
4670
5351
|
});
|
|
4671
5352
|
} else {
|
|
4672
5353
|
log("warn", "a server opted in to oam but oam is not installed; running it on node instead", {
|
|
@@ -4688,6 +5369,20 @@ async function resolveOamSpawn(command, args, optedIn = true) {
|
|
|
4688
5369
|
resolveEntry: (pkg, wantVersion) => resolveNpmEntry(pkg, void 0, void 0, void 0, wantVersion)
|
|
4689
5370
|
});
|
|
4690
5371
|
}
|
|
5372
|
+
var managedEntryMemo = /* @__PURE__ */ new Map();
|
|
5373
|
+
function resolveManagedSpawn(command, args, fromUrl = import.meta.url, npmCache = npmCacheDir(fromUrl), managedRoot = sidecarsNodeModules(), nodeBin = managedNodeBin()) {
|
|
5374
|
+
return rewriteNpmToManagedNode(command, args, {
|
|
5375
|
+
nodeBin,
|
|
5376
|
+
resolveEntry: (pkg, wantVersion) => {
|
|
5377
|
+
if (wantVersion !== null) return resolveNpmEntry(pkg, fromUrl, npmCache, managedRoot, wantVersion);
|
|
5378
|
+
const memoed = managedEntryMemo.get(pkg);
|
|
5379
|
+
if (memoed !== void 0) return memoed;
|
|
5380
|
+
const hit = resolveNpmEntry(pkg, fromUrl, npmCache, managedRoot, null);
|
|
5381
|
+
if (hit !== null && !hit.includes(NPX_CACHE_MARKER)) managedEntryMemo.set(pkg, hit);
|
|
5382
|
+
return hit;
|
|
5383
|
+
}
|
|
5384
|
+
});
|
|
5385
|
+
}
|
|
4691
5386
|
function isOamCommand(command) {
|
|
4692
5387
|
const base = command.split(/[\\/]/).pop() ?? command;
|
|
4693
5388
|
return /^oam(\.exe)?$/i.test(base);
|
|
@@ -4806,7 +5501,7 @@ function packageDirOf(p) {
|
|
|
4806
5501
|
function defaultInstalledVersion(pkgDir) {
|
|
4807
5502
|
if (isTestSandbox()) return null;
|
|
4808
5503
|
try {
|
|
4809
|
-
const parsed = JSON.parse(
|
|
5504
|
+
const parsed = JSON.parse(readFileSync3(join11(pkgDir, "package.json"), "utf8"));
|
|
4810
5505
|
return typeof parsed.version === "string" ? parsed.version : null;
|
|
4811
5506
|
} catch {
|
|
4812
5507
|
return null;
|
|
@@ -4990,18 +5685,18 @@ async function oamFloorLines(probe) {
|
|
|
4990
5685
|
}
|
|
4991
5686
|
async function defaultRunningPrefix(argvPath) {
|
|
4992
5687
|
if (isTestSandbox()) return null;
|
|
4993
|
-
const { detectRunningInstallPrefix: detectRunningInstallPrefix2 } = await import("./auto-upgrade-
|
|
5688
|
+
const { detectRunningInstallPrefix: detectRunningInstallPrefix2 } = await import("./auto-upgrade-3IZNHFYK.js");
|
|
4994
5689
|
return detectRunningInstallPrefix2(argvPath);
|
|
4995
5690
|
}
|
|
4996
5691
|
async function defaultAcquireLock(prefixDir) {
|
|
4997
5692
|
if (isTestSandbox()) return () => {
|
|
4998
5693
|
};
|
|
4999
|
-
const { acquireUpgradeLock: acquireUpgradeLock2 } = await import("./auto-upgrade-
|
|
5694
|
+
const { acquireUpgradeLock: acquireUpgradeLock2 } = await import("./auto-upgrade-3IZNHFYK.js");
|
|
5000
5695
|
return acquireUpgradeLock2(prefixDir);
|
|
5001
5696
|
}
|
|
5002
5697
|
async function upgradeLockPath(prefixDir) {
|
|
5003
|
-
const { UPGRADE_LOCK_NAME: UPGRADE_LOCK_NAME2 } = await import("./auto-upgrade-
|
|
5004
|
-
return
|
|
5698
|
+
const { UPGRADE_LOCK_NAME: UPGRADE_LOCK_NAME2 } = await import("./auto-upgrade-3IZNHFYK.js");
|
|
5699
|
+
return join11(prefixDir, UPGRADE_LOCK_NAME2);
|
|
5005
5700
|
}
|
|
5006
5701
|
async function defaultSpawn(cmd, args, cwd) {
|
|
5007
5702
|
return new Promise((resolve8) => {
|
|
@@ -5062,7 +5757,7 @@ async function runUpgrade(opts = {}) {
|
|
|
5062
5757
|
if (method === "global-npm") {
|
|
5063
5758
|
rawPrefix = await (opts.runningPrefix ?? defaultRunningPrefix)(argvPath);
|
|
5064
5759
|
if (rawPrefix !== null) {
|
|
5065
|
-
const { quoteArgForDisplay: quoteArgForDisplay2, quoteShellArgIfNeeded: quoteShellArgIfNeeded2 } = await import("./auto-upgrade-
|
|
5760
|
+
const { quoteArgForDisplay: quoteArgForDisplay2, quoteShellArgIfNeeded: quoteShellArgIfNeeded2 } = await import("./auto-upgrade-3IZNHFYK.js");
|
|
5066
5761
|
const spawnForm = quoteShellArgIfNeeded2(rawPrefix, opts.platform);
|
|
5067
5762
|
const displayForm = quoteArgForDisplay2(rawPrefix, opts.platform);
|
|
5068
5763
|
if (spawnForm !== null && displayForm !== null) {
|
|
@@ -5145,10 +5840,10 @@ async function runUpgrade(opts = {}) {
|
|
|
5145
5840
|
printErr(` ${commandLine}`);
|
|
5146
5841
|
return { exitCode: 2, lines };
|
|
5147
5842
|
}
|
|
5148
|
-
let
|
|
5843
|
+
let releaseLock2 = null;
|
|
5149
5844
|
if (method === "global-npm" && rawPrefix !== null) {
|
|
5150
|
-
|
|
5151
|
-
if (
|
|
5845
|
+
releaseLock2 = await (opts.acquireLock ?? defaultAcquireLock)(rawPrefix);
|
|
5846
|
+
if (releaseLock2 === null) {
|
|
5152
5847
|
printErr(
|
|
5153
5848
|
`yaw-mcp upgrade --run: another yaw-mcp process is upgrading this install (lock: ${await upgradeLockPath(rawPrefix)}); try again once it finishes.`
|
|
5154
5849
|
);
|
|
@@ -5167,7 +5862,7 @@ async function runUpgrade(opts = {}) {
|
|
|
5167
5862
|
try {
|
|
5168
5863
|
code = await runner(runSpec.cmd, runSpec.args, runSpec.cwd);
|
|
5169
5864
|
} finally {
|
|
5170
|
-
|
|
5865
|
+
releaseLock2?.();
|
|
5171
5866
|
}
|
|
5172
5867
|
if (code === 0) {
|
|
5173
5868
|
print("");
|
|
@@ -5194,7 +5889,7 @@ async function runUpgrade(opts = {}) {
|
|
|
5194
5889
|
return { exitCode: 3, lines };
|
|
5195
5890
|
}
|
|
5196
5891
|
function readCurrentVersion() {
|
|
5197
|
-
return true ? "1.0
|
|
5892
|
+
return true ? "1.1.0" : "dev";
|
|
5198
5893
|
}
|
|
5199
5894
|
|
|
5200
5895
|
// src/auto-upgrade.ts
|
|
@@ -5217,7 +5912,7 @@ function detectRunningInstallPrefix(argvPath) {
|
|
|
5217
5912
|
} catch {
|
|
5218
5913
|
return null;
|
|
5219
5914
|
}
|
|
5220
|
-
const dir =
|
|
5915
|
+
const dir = dirname4(resolved);
|
|
5221
5916
|
const idx = dir.lastIndexOf(`${sep2}node_modules${sep2}`);
|
|
5222
5917
|
if (idx === -1) return null;
|
|
5223
5918
|
const candidate = dir.slice(0, idx);
|
|
@@ -5248,21 +5943,21 @@ var LOCK_CREATE_RETRY_DELAYS_MS = [1, 5, 20];
|
|
|
5248
5943
|
function sleepSync(ms) {
|
|
5249
5944
|
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
5250
5945
|
}
|
|
5251
|
-
function openLockSync(
|
|
5946
|
+
function openLockSync(path5) {
|
|
5252
5947
|
for (const ms of LOCK_CREATE_RETRY_DELAYS_MS) {
|
|
5253
5948
|
try {
|
|
5254
|
-
return openSync(
|
|
5949
|
+
return openSync(path5, "wx");
|
|
5255
5950
|
} catch (err) {
|
|
5256
5951
|
if (!isWin32TransientFsError(err)) throw err;
|
|
5257
5952
|
sleepSync(ms);
|
|
5258
5953
|
}
|
|
5259
5954
|
}
|
|
5260
|
-
return openSync(
|
|
5955
|
+
return openSync(path5, "wx");
|
|
5261
5956
|
}
|
|
5262
5957
|
function lockHolderAlive(lockPath) {
|
|
5263
5958
|
let pid;
|
|
5264
5959
|
try {
|
|
5265
|
-
pid = Number.parseInt(
|
|
5960
|
+
pid = Number.parseInt(readFileSync4(lockPath, "utf8").trim(), 10);
|
|
5266
5961
|
} catch {
|
|
5267
5962
|
return true;
|
|
5268
5963
|
}
|
|
@@ -5276,8 +5971,8 @@ function lockHolderAlive(lockPath) {
|
|
|
5276
5971
|
}
|
|
5277
5972
|
}
|
|
5278
5973
|
function acquireUpgradeLock(dir, lockName = UPGRADE_LOCK_NAME, opts = {}) {
|
|
5279
|
-
const lockPath =
|
|
5280
|
-
const holderAlive = (
|
|
5974
|
+
const lockPath = join12(dir, lockName);
|
|
5975
|
+
const holderAlive = (path5) => opts.probeHolder ? lockHolderAlive(path5) : true;
|
|
5281
5976
|
const mine = String(process.pid);
|
|
5282
5977
|
const take = () => {
|
|
5283
5978
|
try {
|
|
@@ -5293,7 +5988,7 @@ function acquireUpgradeLock(dir, lockName = UPGRADE_LOCK_NAME, opts = {}) {
|
|
|
5293
5988
|
if (released) return;
|
|
5294
5989
|
released = true;
|
|
5295
5990
|
try {
|
|
5296
|
-
if (
|
|
5991
|
+
if (readFileSync4(lockPath, "utf8").trim() !== mine) return;
|
|
5297
5992
|
unlinkSync(lockPath);
|
|
5298
5993
|
} catch {
|
|
5299
5994
|
}
|
|
@@ -5309,7 +6004,7 @@ function acquireUpgradeLock(dir, lockName = UPGRADE_LOCK_NAME, opts = {}) {
|
|
|
5309
6004
|
const isLive = (ageMs2) => ageMs2 > -UPGRADE_LOCK_FUTURE_SKEW_MS && ageMs2 < UPGRADE_LOCK_STALE_MS;
|
|
5310
6005
|
let ageMs;
|
|
5311
6006
|
try {
|
|
5312
|
-
ageMs = Date.now() -
|
|
6007
|
+
ageMs = Date.now() - statSync3(lockPath).mtimeMs;
|
|
5313
6008
|
} catch {
|
|
5314
6009
|
ageMs = UPGRADE_LOCK_STALE_MS;
|
|
5315
6010
|
}
|
|
@@ -5324,12 +6019,12 @@ function acquireUpgradeLock(dir, lockName = UPGRADE_LOCK_NAME, opts = {}) {
|
|
|
5324
6019
|
}
|
|
5325
6020
|
let stoleLive = false;
|
|
5326
6021
|
try {
|
|
5327
|
-
stoleLive = isLive(Date.now() -
|
|
6022
|
+
stoleLive = isLive(Date.now() - statSync3(stolenPath).mtimeMs) && holderAlive(stolenPath);
|
|
5328
6023
|
} catch {
|
|
5329
6024
|
}
|
|
5330
6025
|
if (stoleLive) {
|
|
5331
6026
|
try {
|
|
5332
|
-
const holder =
|
|
6027
|
+
const holder = readFileSync4(stolenPath, "utf8");
|
|
5333
6028
|
const fd = openLockSync(lockPath);
|
|
5334
6029
|
try {
|
|
5335
6030
|
writeSync(fd, holder);
|
|
@@ -5362,10 +6057,10 @@ function defaultFetchLatest() {
|
|
|
5362
6057
|
}
|
|
5363
6058
|
var UPGRADE_CHECK_INTERVAL_MS = 60 * 60 * 1e3;
|
|
5364
6059
|
var UPGRADE_ATTEMPT_COOLDOWN_MS = 6 * 60 * 60 * 1e3;
|
|
5365
|
-
function readMemo(
|
|
6060
|
+
function readMemo(path5) {
|
|
5366
6061
|
let parsed;
|
|
5367
6062
|
try {
|
|
5368
|
-
parsed = JSON.parse(
|
|
6063
|
+
parsed = JSON.parse(readFileSync4(path5, "utf8"));
|
|
5369
6064
|
} catch {
|
|
5370
6065
|
return null;
|
|
5371
6066
|
}
|
|
@@ -5375,32 +6070,32 @@ function readMemo(path3) {
|
|
|
5375
6070
|
const ageMs = Date.now() - memo.at;
|
|
5376
6071
|
return ageMs < -UPGRADE_LOCK_FUTURE_SKEW_MS ? null : { ageMs, memo };
|
|
5377
6072
|
}
|
|
5378
|
-
function writeMemo(
|
|
6073
|
+
function writeMemo(path5, fields) {
|
|
5379
6074
|
try {
|
|
5380
|
-
writeFileSync(
|
|
6075
|
+
writeFileSync(path5, `${JSON.stringify({ at: Date.now(), ...fields })}
|
|
5381
6076
|
`);
|
|
5382
6077
|
} catch {
|
|
5383
6078
|
}
|
|
5384
6079
|
}
|
|
5385
6080
|
function checkMemoPath() {
|
|
5386
|
-
return
|
|
6081
|
+
return join12(tmpdir(), `.yaw-mcp-upgrade-check-${process.getuid?.() ?? "win"}.json`);
|
|
5387
6082
|
}
|
|
5388
|
-
function readCheckMemo(
|
|
5389
|
-
const read = readMemo(
|
|
6083
|
+
function readCheckMemo(path5) {
|
|
6084
|
+
const read = readMemo(path5);
|
|
5390
6085
|
if (read === null || read.ageMs >= UPGRADE_CHECK_INTERVAL_MS) return null;
|
|
5391
6086
|
const latest = read.memo.latest;
|
|
5392
6087
|
if (latest !== null && typeof latest !== "string") return null;
|
|
5393
6088
|
return { latest };
|
|
5394
6089
|
}
|
|
5395
|
-
function writeCheckMemo(
|
|
5396
|
-
writeMemo(
|
|
6090
|
+
function writeCheckMemo(path5, latest) {
|
|
6091
|
+
writeMemo(path5, { latest });
|
|
5397
6092
|
}
|
|
5398
6093
|
function attemptMemoPath(dir, lockName) {
|
|
5399
|
-
return
|
|
6094
|
+
return join12(dir, `${lockName}.attempt`);
|
|
5400
6095
|
}
|
|
5401
6096
|
function attemptMemoFallbackPath(dir, lockName, fallbackDir) {
|
|
5402
6097
|
const scope = createHash("sha256").update(`${dir}\0${lockName}`).digest("hex").slice(0, 16);
|
|
5403
|
-
return
|
|
6098
|
+
return join12(fallbackDir, `.yaw-mcp-upgrade-attempt-${process.getuid?.() ?? "win"}-${scope}.json`);
|
|
5404
6099
|
}
|
|
5405
6100
|
function defaultCheckedRecently() {
|
|
5406
6101
|
if (isTestSandbox()) return null;
|
|
@@ -5486,7 +6181,7 @@ function defaultSpawn2(cmd, args, onDone = () => {
|
|
|
5486
6181
|
}
|
|
5487
6182
|
async function maybeAutoUpgrade(deps = {}) {
|
|
5488
6183
|
if (isFeatureDisabled("YAW_MCP_AUTO_UPGRADE")) return;
|
|
5489
|
-
const current = deps.currentVersion ?? (true ? "1.0
|
|
6184
|
+
const current = deps.currentVersion ?? (true ? "1.1.0" : "dev");
|
|
5490
6185
|
if (current === "dev") return;
|
|
5491
6186
|
const argvPath = deps.argvPath ?? process.argv[1];
|
|
5492
6187
|
let method = (deps.isSeaImpl ? await deps.isSeaImpl() : await detectSea()) ? "binary" : detectInstallMethod(argvPath, realpathSync3, ["unknown", "local-node-modules"]);
|
|
@@ -5517,8 +6212,8 @@ async function maybeAutoUpgrade(deps = {}) {
|
|
|
5517
6212
|
});
|
|
5518
6213
|
return;
|
|
5519
6214
|
}
|
|
5520
|
-
const
|
|
5521
|
-
if (
|
|
6215
|
+
const releaseLock2 = (deps.acquireLockImpl ?? defaultAcquireLock2)(lockDir, lockName);
|
|
6216
|
+
if (releaseLock2 === null) {
|
|
5522
6217
|
log("info", "yaw-mcp self-upgrade: another process is already upgrading this install; skipping this one", {
|
|
5523
6218
|
current,
|
|
5524
6219
|
latest,
|
|
@@ -5543,9 +6238,9 @@ async function maybeAutoUpgrade(deps = {}) {
|
|
|
5543
6238
|
}
|
|
5544
6239
|
(deps.recordAttemptImpl ?? defaultRecordAttempt)(lockDir, lockName, latest);
|
|
5545
6240
|
try {
|
|
5546
|
-
(deps.spawnImpl ?? defaultSpawn2)(globalSpec.cmd, globalSpec.args,
|
|
6241
|
+
(deps.spawnImpl ?? defaultSpawn2)(globalSpec.cmd, globalSpec.args, releaseLock2);
|
|
5547
6242
|
} catch (err) {
|
|
5548
|
-
|
|
6243
|
+
releaseLock2();
|
|
5549
6244
|
log("warn", `yaw-mcp self-upgrade: could not start ${globalSpec.cmd}`, {
|
|
5550
6245
|
error: err instanceof Error ? err.message : String(err)
|
|
5551
6246
|
});
|
|
@@ -5596,8 +6291,11 @@ export {
|
|
|
5596
6291
|
setLogSurface,
|
|
5597
6292
|
log,
|
|
5598
6293
|
createStreamWriter,
|
|
5599
|
-
isWin32TransientFsError,
|
|
5600
6294
|
atomicWriteFile,
|
|
6295
|
+
withFileLock,
|
|
6296
|
+
setJsonKey,
|
|
6297
|
+
stripJsoncComments,
|
|
6298
|
+
stripTrailingCommas,
|
|
5601
6299
|
parseJsonc,
|
|
5602
6300
|
editJsoncPath,
|
|
5603
6301
|
cacheDir,
|
|
@@ -5615,6 +6313,7 @@ export {
|
|
|
5615
6313
|
isFeatureDisabled,
|
|
5616
6314
|
isTestSandbox,
|
|
5617
6315
|
launchOf,
|
|
6316
|
+
canonicalJson,
|
|
5618
6317
|
describeValueShape,
|
|
5619
6318
|
readStrictJson,
|
|
5620
6319
|
CURRENT_OS,
|
|
@@ -5666,6 +6365,16 @@ export {
|
|
|
5666
6365
|
describeJsonShape,
|
|
5667
6366
|
unparseableConfigFix,
|
|
5668
6367
|
blockedContainerFix,
|
|
6368
|
+
STATE_SCHEMA_VERSION,
|
|
6369
|
+
STATE_FILENAME,
|
|
6370
|
+
isPersistenceDisabled,
|
|
6371
|
+
isReadableStateVersion,
|
|
6372
|
+
PREWARM_FAILURE_BACKOFF_MS,
|
|
6373
|
+
statePath,
|
|
6374
|
+
loadState,
|
|
6375
|
+
loadStateClassified,
|
|
6376
|
+
StateSync,
|
|
6377
|
+
isFileNotFound,
|
|
5669
6378
|
packageName,
|
|
5670
6379
|
specConstraint,
|
|
5671
6380
|
isRegistrySpec,
|
|
@@ -5683,6 +6392,7 @@ export {
|
|
|
5683
6392
|
nodeLaunchKind,
|
|
5684
6393
|
resolveStableNpmEntry,
|
|
5685
6394
|
resolveOamSpawn,
|
|
6395
|
+
resolveManagedSpawn,
|
|
5686
6396
|
isOamCommand,
|
|
5687
6397
|
isOamLaunch,
|
|
5688
6398
|
POSIX_SHELL,
|