@yawlabs/mcp 1.0.20 → 1.1.1
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 +38 -0
- package/README.md +11 -8
- package/dist/{auto-upgrade-2WRN36F2.js → auto-upgrade-EXON4SEO.js} +1 -1
- package/dist/{chunk-JGEO2RRL.js → chunk-UOWMJUH4.js} +865 -141
- package/dist/index.js +3654 -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;
|
|
@@ -4029,6 +4605,18 @@ function oamInstallAdvice(os) {
|
|
|
4029
4605
|
if (os === CURRENT_OS && !oamPublishesBinaryForThisMachine()) return oamNoBinaryReason();
|
|
4030
4606
|
return oamInstallCommand(os);
|
|
4031
4607
|
}
|
|
4608
|
+
function oamReleaseDir(os) {
|
|
4609
|
+
return os === "windows" ? "%LOCALAPPDATA%\\oam\\bin" : "~/.oam/bin";
|
|
4610
|
+
}
|
|
4611
|
+
function oamBuildTreeFix(os, viaOamBin = false) {
|
|
4612
|
+
const dir = oamReleaseDir(os);
|
|
4613
|
+
const where = `${dir} (the installer default; OAM_INSTALL_DIR changes it)`;
|
|
4614
|
+
if (viaOamBin) {
|
|
4615
|
+
return `point OAM_BIN at an installed release oam, or unset it to use the oam on PATH; with no release installed, install one first (${oamInstallCommand(os)}, into ${where})`;
|
|
4616
|
+
}
|
|
4617
|
+
const override = os === "windows" ? " (OAM_SKIP_PATH_SHADOW=1 overrides it)" : "";
|
|
4618
|
+
return `put the release oam's dir, ${where}, ahead of the build tree on PATH, or point OAM_BIN at an installed release oam; with no release installed, reorder PATH first, since the installer (${oamInstallCommand(os)}) refuses while another oam resolves ahead of its dir${override}`;
|
|
4619
|
+
}
|
|
4032
4620
|
function oamHeapOomHint(stderr) {
|
|
4033
4621
|
if (!stderr.includes("OAM-RT-OOM")) return null;
|
|
4034
4622
|
return `The oam-hosted child hit its V8 heap cap. Raise it with OAM_MAX_HEAP_MB=<mb> in this server's env, or set "runtime": "node" for this server in bundles.json.`;
|
|
@@ -4037,9 +4625,11 @@ var warnedOamUnavailable = false;
|
|
|
4037
4625
|
function oamFailureLabel(failure) {
|
|
4038
4626
|
if (failure === "timeout") return "`oam --version` did not answer in time";
|
|
4039
4627
|
if (failure === "exit") return "`oam --version` exited non-zero";
|
|
4628
|
+
if (failure === "build-tree") return "the binary is a local build-tree oam, not a release";
|
|
4040
4629
|
return "the binary could not be executed";
|
|
4041
4630
|
}
|
|
4042
4631
|
var oamProbeCache;
|
|
4632
|
+
var oamProbeCacheExpiresAt;
|
|
4043
4633
|
function parseOamVersion(out) {
|
|
4044
4634
|
const m = /\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?/.exec(out);
|
|
4045
4635
|
return m ? m[0] : null;
|
|
@@ -4091,13 +4681,19 @@ function winNormalize(p, platform = process.platform) {
|
|
|
4091
4681
|
}
|
|
4092
4682
|
function isExecutableFile(candidate, platform) {
|
|
4093
4683
|
try {
|
|
4094
|
-
if (!
|
|
4684
|
+
if (!statSync2(candidate).isFile()) return false;
|
|
4095
4685
|
if (platform !== "win32") accessSync(candidate, constants.X_OK);
|
|
4096
4686
|
return true;
|
|
4097
4687
|
} catch {
|
|
4098
4688
|
return false;
|
|
4099
4689
|
}
|
|
4100
4690
|
}
|
|
4691
|
+
var BUILD_TREE_SEGMENTS = /* @__PURE__ */ new Set(["target", "debug", "out"]);
|
|
4692
|
+
var BUILD_TREE_PREFIXES = ["cmake-build-"];
|
|
4693
|
+
function isBuildTreePath(p) {
|
|
4694
|
+
const parts = p.toLowerCase().split(/[\\/]+/);
|
|
4695
|
+
return parts.some((seg) => BUILD_TREE_SEGMENTS.has(seg) || BUILD_TREE_PREFIXES.some((pre) => seg.startsWith(pre)));
|
|
4696
|
+
}
|
|
4101
4697
|
function resolveBinAbsolute(bin, env = process.env, platform = process.platform) {
|
|
4102
4698
|
if (isAbsolute8(bin)) return isExecutableFile(bin, platform) ? winNormalize(bin, platform) : null;
|
|
4103
4699
|
if (bin.includes("/") || platform === "win32" && bin.includes("\\")) return null;
|
|
@@ -4110,7 +4706,7 @@ function resolveBinAbsolute(bin, env = process.env, platform = process.platform)
|
|
|
4110
4706
|
for (const dir of dirs) {
|
|
4111
4707
|
const clean = dir.replace(/^"|"$/g, "");
|
|
4112
4708
|
for (const ext of exts) {
|
|
4113
|
-
const candidate =
|
|
4709
|
+
const candidate = join10(clean, bin + ext);
|
|
4114
4710
|
if (!isAbsolute8(candidate)) continue;
|
|
4115
4711
|
if (isExecutableFile(candidate, platform)) return winNormalize(candidate, platform);
|
|
4116
4712
|
}
|
|
@@ -4126,7 +4722,8 @@ function classifyProbeFailure(err) {
|
|
|
4126
4722
|
if (code === "EOAMEXIT") return "exit";
|
|
4127
4723
|
return "spawn";
|
|
4128
4724
|
}
|
|
4129
|
-
var OAM_PROBE_TIMEOUT_MS =
|
|
4725
|
+
var OAM_PROBE_TIMEOUT_MS = 1e4;
|
|
4726
|
+
var OAM_PROBE_TIMEOUT_RECHECK_MS = 6e4;
|
|
4130
4727
|
var OAM_PROBE_KILL_SIGNAL = "SIGKILL";
|
|
4131
4728
|
var OAM_PROBE_MAX_OUTPUT = 8 * 1024;
|
|
4132
4729
|
var VERSION_CARRY = 128;
|
|
@@ -4230,6 +4827,10 @@ function spawnVersionProbe(bin) {
|
|
|
4230
4827
|
var oamProbeInFlight;
|
|
4231
4828
|
var oamProbeGeneration = 0;
|
|
4232
4829
|
async function probeOam(run = spawnVersionProbe) {
|
|
4830
|
+
if (oamProbeCache !== void 0 && oamProbeCacheExpiresAt !== void 0 && Date.now() >= oamProbeCacheExpiresAt) {
|
|
4831
|
+
oamProbeCache = void 0;
|
|
4832
|
+
oamProbeCacheExpiresAt = void 0;
|
|
4833
|
+
}
|
|
4233
4834
|
if (oamProbeCache !== void 0) return oamProbeCache;
|
|
4234
4835
|
if (oamProbeInFlight !== void 0) return oamProbeInFlight;
|
|
4235
4836
|
const generation = oamProbeGeneration;
|
|
@@ -4241,10 +4842,34 @@ async function probeOam(run = spawnVersionProbe) {
|
|
|
4241
4842
|
async function probeOamUncached(run, generation) {
|
|
4242
4843
|
const explicit = Boolean(process.env.OAM_BIN);
|
|
4243
4844
|
const bin = winNormalize(process.env.OAM_BIN || (process.platform === "win32" ? "oam.exe" : "oam"));
|
|
4244
|
-
const
|
|
4245
|
-
|
|
4845
|
+
const absResolved = resolveBinAbsolute(bin);
|
|
4846
|
+
const publish = (probe, ttlMs) => {
|
|
4847
|
+
if (generation === oamProbeGeneration) {
|
|
4848
|
+
oamProbeCache = probe;
|
|
4849
|
+
oamProbeCacheExpiresAt = ttlMs === void 0 ? void 0 : Date.now() + ttlMs;
|
|
4850
|
+
}
|
|
4246
4851
|
return probe;
|
|
4247
4852
|
};
|
|
4853
|
+
if (run === spawnVersionProbe && absResolved !== null && isBuildTreePath(absResolved)) {
|
|
4854
|
+
log("warn", "oam resolves to a local build-tree binary; falling back to node for this process", {
|
|
4855
|
+
bin: absResolved,
|
|
4856
|
+
fix: oamBuildTreeFix(CURRENT_OS, explicit),
|
|
4857
|
+
thenRestart: "restart yaw-mcp; this probe is cached for the process lifetime"
|
|
4858
|
+
});
|
|
4859
|
+
return publish({
|
|
4860
|
+
bin: null,
|
|
4861
|
+
binPath: null,
|
|
4862
|
+
version: null,
|
|
4863
|
+
belowMin: false,
|
|
4864
|
+
failure: "build-tree",
|
|
4865
|
+
failureDetail: `${absResolved} is a compiler output binary (build-tree), not a release`,
|
|
4866
|
+
viaOamBin: explicit
|
|
4867
|
+
});
|
|
4868
|
+
}
|
|
4869
|
+
const identity = (() => {
|
|
4870
|
+
if (run !== spawnVersionProbe) return null;
|
|
4871
|
+
return absResolved ? oamBinaryIdentity(absResolved) : null;
|
|
4872
|
+
})();
|
|
4248
4873
|
const runRetryingTimeout = async () => {
|
|
4249
4874
|
try {
|
|
4250
4875
|
return await run(bin);
|
|
@@ -4258,7 +4883,15 @@ async function probeOamUncached(run, generation) {
|
|
|
4258
4883
|
}
|
|
4259
4884
|
};
|
|
4260
4885
|
try {
|
|
4261
|
-
const
|
|
4886
|
+
const remembered = identity ? readOamVerdict(identity) : void 0;
|
|
4887
|
+
let version;
|
|
4888
|
+
if (remembered !== void 0) {
|
|
4889
|
+
version = remembered;
|
|
4890
|
+
log("debug", "oam --version answered from the per-binary cache", { bin, oamVersion: version });
|
|
4891
|
+
} else {
|
|
4892
|
+
version = parseOamVersion(await runRetryingTimeout());
|
|
4893
|
+
if (identity) void writeOamVerdict(identity, version);
|
|
4894
|
+
}
|
|
4262
4895
|
if (version !== null && compareVersions(version, MIN_OAM_VERSION) < 0) {
|
|
4263
4896
|
log("warn", "oam is installed but below the minimum supported version; falling back to node", {
|
|
4264
4897
|
// The full token, prerelease suffix included: a build reporting
|
|
@@ -4290,9 +4923,10 @@ async function probeOamUncached(run, generation) {
|
|
|
4290
4923
|
}
|
|
4291
4924
|
return publish({
|
|
4292
4925
|
bin,
|
|
4293
|
-
// Resolved once, alongside the probe that proved the
|
|
4294
|
-
// install path never has to re-derive it (or spawn
|
|
4295
|
-
|
|
4926
|
+
// Resolved once (absResolved, above), alongside the probe that proved the
|
|
4927
|
+
// name spawns, so the install path never has to re-derive it (or spawn
|
|
4928
|
+
// `where`/`which`).
|
|
4929
|
+
binPath: absResolved,
|
|
4296
4930
|
version,
|
|
4297
4931
|
belowMin: false,
|
|
4298
4932
|
failure: null,
|
|
@@ -4303,10 +4937,15 @@ async function probeOamUncached(run, generation) {
|
|
|
4303
4937
|
const absent = code === "ENOENT" && !explicit;
|
|
4304
4938
|
const shellShim = isShellShimSpawnRefusal(code, bin);
|
|
4305
4939
|
if (code === "ETIMEDOUT") {
|
|
4306
|
-
log(
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4940
|
+
log(
|
|
4941
|
+
"warn",
|
|
4942
|
+
"oam did not respond to --version twice; falling back to node for now (probed again on a later connect)",
|
|
4943
|
+
{
|
|
4944
|
+
timeoutMs: OAM_PROBE_TIMEOUT_MS,
|
|
4945
|
+
recheckAfterMs: OAM_PROBE_TIMEOUT_RECHECK_MS,
|
|
4946
|
+
bin
|
|
4947
|
+
}
|
|
4948
|
+
);
|
|
4310
4949
|
} else if (shellShim) {
|
|
4311
4950
|
log(
|
|
4312
4951
|
"warn",
|
|
@@ -4324,14 +4963,17 @@ async function probeOamUncached(run, generation) {
|
|
|
4324
4963
|
error: err instanceof Error ? err.message : String(err)
|
|
4325
4964
|
});
|
|
4326
4965
|
}
|
|
4327
|
-
return publish(
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4966
|
+
return publish(
|
|
4967
|
+
{
|
|
4968
|
+
bin: null,
|
|
4969
|
+
binPath: null,
|
|
4970
|
+
version: null,
|
|
4971
|
+
belowMin: false,
|
|
4972
|
+
failure: absent ? null : classifyProbeFailure(err),
|
|
4973
|
+
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)
|
|
4974
|
+
},
|
|
4975
|
+
code === "ETIMEDOUT" ? OAM_PROBE_TIMEOUT_RECHECK_MS : void 0
|
|
4976
|
+
);
|
|
4335
4977
|
}
|
|
4336
4978
|
}
|
|
4337
4979
|
var WIN_EXE_EXT = /\.(?:exe|cmd|bat)$/i;
|
|
@@ -4400,10 +5042,61 @@ function rewriteForOam(command, args, deps) {
|
|
|
4400
5042
|
}
|
|
4401
5043
|
return { command, args };
|
|
4402
5044
|
}
|
|
5045
|
+
function managedNodeBin(env = process.env, platform = process.platform, execPath = process.execPath) {
|
|
5046
|
+
if (nodeLaunchKind(execPath) === "node") return execPath;
|
|
5047
|
+
return resolveBinAbsolute("node", env, platform);
|
|
5048
|
+
}
|
|
5049
|
+
function rewriteNpmToManagedNode(command, args, deps) {
|
|
5050
|
+
if (!deps.nodeBin) {
|
|
5051
|
+
log("debug", "no node binary located for the managed sidecar spawn; staying on npx", { command });
|
|
5052
|
+
return { command, args };
|
|
5053
|
+
}
|
|
5054
|
+
if (nodeLaunchKind(command) !== "npx") return { command, args };
|
|
5055
|
+
const specIdx = npxSpecIndex(args);
|
|
5056
|
+
const spec = specIdx === -1 ? void 0 : args[specIdx];
|
|
5057
|
+
if (!spec) return { command, args };
|
|
5058
|
+
if (spec.startsWith("-")) {
|
|
5059
|
+
log("debug", "npx launch carries flags yaw-mcp does not parse; staying on npx instead of the managed tree", {
|
|
5060
|
+
flag: spec,
|
|
5061
|
+
argc: args.length
|
|
5062
|
+
});
|
|
5063
|
+
return { command, args };
|
|
5064
|
+
}
|
|
5065
|
+
if (!isRegistrySpec(spec)) {
|
|
5066
|
+
log("debug", "npx spec is a git/path target, not a registry package; staying on npx instead of the managed tree", {
|
|
5067
|
+
spec
|
|
5068
|
+
});
|
|
5069
|
+
return { command, args };
|
|
5070
|
+
}
|
|
5071
|
+
const constraint = specConstraint(spec);
|
|
5072
|
+
if (constraint.kind === "range") {
|
|
5073
|
+
log("debug", "npx spec constrains the version with a range yaw-mcp cannot evaluate; staying on npx", {
|
|
5074
|
+
package: packageName(spec),
|
|
5075
|
+
range: constraint.raw
|
|
5076
|
+
});
|
|
5077
|
+
return { command, args };
|
|
5078
|
+
}
|
|
5079
|
+
const wantVersion = constraint.kind === "exact" ? constraint.version : null;
|
|
5080
|
+
const entry = deps.resolveEntry(packageName(spec), wantVersion);
|
|
5081
|
+
if (!entry) {
|
|
5082
|
+
if (wantVersion !== null) {
|
|
5083
|
+
log("debug", "no on-disk copy declares the pinned version; staying on npx so the pin is honoured", {
|
|
5084
|
+
package: packageName(spec),
|
|
5085
|
+
version: wantVersion
|
|
5086
|
+
});
|
|
5087
|
+
} else {
|
|
5088
|
+
log("debug", "npx package has no on-disk entry; staying on npx instead of the managed tree", {
|
|
5089
|
+
package: packageName(spec)
|
|
5090
|
+
});
|
|
5091
|
+
}
|
|
5092
|
+
return { command, args };
|
|
5093
|
+
}
|
|
5094
|
+
return { command: deps.nodeBin, args: [entry, ...args.slice(specIdx + 1)] };
|
|
5095
|
+
}
|
|
4403
5096
|
var NPX_CACHE_MARKER = `${sep}_npx${sep}`;
|
|
4404
5097
|
function npxHashNodeModules(npxRoot) {
|
|
4405
5098
|
try {
|
|
4406
|
-
return readdirSync2(npxRoot, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) =>
|
|
5099
|
+
return readdirSync2(npxRoot, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => join10(npxRoot, e.name, "node_modules"));
|
|
4407
5100
|
} catch {
|
|
4408
5101
|
return [];
|
|
4409
5102
|
}
|
|
@@ -4454,7 +5147,7 @@ function npmrcValue(raw) {
|
|
|
4454
5147
|
function npmrcCache(file) {
|
|
4455
5148
|
let text;
|
|
4456
5149
|
try {
|
|
4457
|
-
text =
|
|
5150
|
+
text = readFileSync2(file, "utf8");
|
|
4458
5151
|
} catch {
|
|
4459
5152
|
return null;
|
|
4460
5153
|
}
|
|
@@ -4464,7 +5157,7 @@ function npmrcCache(file) {
|
|
|
4464
5157
|
if (!m) continue;
|
|
4465
5158
|
const value = expandNpmrcEnv(npmrcValue(m[1]));
|
|
4466
5159
|
if (!value) continue;
|
|
4467
|
-
last = value.startsWith("~/") ?
|
|
5160
|
+
last = value.startsWith("~/") ? join10(homedir4(), value.slice(2)) : value;
|
|
4468
5161
|
}
|
|
4469
5162
|
return last;
|
|
4470
5163
|
}
|
|
@@ -4487,34 +5180,34 @@ function resolveNpmCacheDir(fromUrl) {
|
|
|
4487
5180
|
const envKey = Object.keys(process.env).find((k) => /^npm_config_cache$/i.test(k));
|
|
4488
5181
|
const fromEnv = envKey === void 0 ? void 0 : process.env[envKey];
|
|
4489
5182
|
if (fromEnv) return fromEnv;
|
|
4490
|
-
const candidates = [
|
|
5183
|
+
const candidates = [join10(homedir4(), ".npmrc")];
|
|
4491
5184
|
for (const nodeModules of ownNodeModules(fromUrl)) {
|
|
4492
5185
|
candidates.push(
|
|
4493
|
-
|
|
5186
|
+
join10(dirname3(nodeModules), "etc", "npmrc"),
|
|
4494
5187
|
// <prefix>/node_modules (Windows)
|
|
4495
|
-
|
|
5188
|
+
join10(dirname3(dirname3(nodeModules)), "etc", "npmrc"),
|
|
4496
5189
|
// <prefix>/lib/node_modules (POSIX)
|
|
4497
|
-
|
|
5190
|
+
join10(nodeModules, "npm", "npmrc")
|
|
4498
5191
|
// builtin, beside npm itself
|
|
4499
5192
|
);
|
|
4500
5193
|
}
|
|
4501
|
-
const nodeDir =
|
|
5194
|
+
const nodeDir = dirname3(process.execPath);
|
|
4502
5195
|
candidates.push(
|
|
4503
|
-
|
|
5196
|
+
join10(nodeDir, "node_modules", "npm", "npmrc"),
|
|
4504
5197
|
// Windows layout
|
|
4505
|
-
|
|
5198
|
+
join10(nodeDir, "..", "lib", "node_modules", "npm", "npmrc")
|
|
4506
5199
|
// POSIX layout
|
|
4507
5200
|
);
|
|
4508
5201
|
for (const file of candidates) {
|
|
4509
5202
|
const cache = npmrcCache(file);
|
|
4510
5203
|
if (cache) return cache;
|
|
4511
5204
|
}
|
|
4512
|
-
const fallback = process.platform === "win32" ? process.env.LOCALAPPDATA &&
|
|
5205
|
+
const fallback = process.platform === "win32" ? process.env.LOCALAPPDATA && join10(process.env.LOCALAPPDATA, "npm-cache") : join10(homedir4(), ".npm");
|
|
4513
5206
|
return fallback && existsSync2(fallback) ? fallback : null;
|
|
4514
5207
|
}
|
|
4515
5208
|
function npmCacheNpxNodeModules(cacheDir2) {
|
|
4516
5209
|
if (!cacheDir2) return [];
|
|
4517
|
-
return npxHashNodeModules(
|
|
5210
|
+
return npxHashNodeModules(join10(cacheDir2, "_npx"));
|
|
4518
5211
|
}
|
|
4519
5212
|
function ownNodeModules(fromUrl = import.meta.url) {
|
|
4520
5213
|
let here;
|
|
@@ -4529,11 +5222,11 @@ function ownNodeModules(fromUrl = import.meta.url) {
|
|
|
4529
5222
|
return [here.slice(0, idx + seg.length - sep.length)];
|
|
4530
5223
|
}
|
|
4531
5224
|
function packageEntry(pkgDir, pkg) {
|
|
4532
|
-
const pjPath =
|
|
5225
|
+
const pjPath = join10(pkgDir, "package.json");
|
|
4533
5226
|
if (!existsSync2(pjPath)) return null;
|
|
4534
5227
|
let parsed;
|
|
4535
5228
|
try {
|
|
4536
|
-
parsed = JSON.parse(
|
|
5229
|
+
parsed = JSON.parse(readFileSync2(pjPath, "utf8"));
|
|
4537
5230
|
} catch {
|
|
4538
5231
|
return null;
|
|
4539
5232
|
}
|
|
@@ -4550,7 +5243,7 @@ function packageEntry(pkgDir, pkg) {
|
|
|
4550
5243
|
}
|
|
4551
5244
|
if (!rel && typeof j.main === "string") rel = j.main;
|
|
4552
5245
|
if (typeof rel !== "string" || rel.length === 0) return null;
|
|
4553
|
-
const entry = isAbsolute8(rel) ? rel :
|
|
5246
|
+
const entry = isAbsolute8(rel) ? rel : join10(pkgDir, rel);
|
|
4554
5247
|
if (!existsSync2(entry)) return null;
|
|
4555
5248
|
return {
|
|
4556
5249
|
entry,
|
|
@@ -4575,7 +5268,7 @@ function resolveNpmEntry(pkg, fromUrl = import.meta.url, npmCache = npmCacheDir(
|
|
|
4575
5268
|
...ownDurable.map((nodeModules) => ({ nodeModules, source: "durable" }))
|
|
4576
5269
|
];
|
|
4577
5270
|
for (const { nodeModules, source } of durable) {
|
|
4578
|
-
const hit = packageEntry(
|
|
5271
|
+
const hit = packageEntry(join10(nodeModules, ...parts), pkg);
|
|
4579
5272
|
if (!hit) continue;
|
|
4580
5273
|
if (wantVersion !== null && !satisfiesExactPin(hit.version, wantVersion)) continue;
|
|
4581
5274
|
notePinnedSidecar(pkg, hit.version, source, nodeModules);
|
|
@@ -4586,7 +5279,7 @@ function resolveNpmEntry(pkg, fromUrl = import.meta.url, npmCache = npmCacheDir(
|
|
|
4586
5279
|
let bestRoot = null;
|
|
4587
5280
|
let bestParsed = null;
|
|
4588
5281
|
for (const nodeModules of roots) {
|
|
4589
|
-
const hit = packageEntry(
|
|
5282
|
+
const hit = packageEntry(join10(nodeModules, ...parts), pkg);
|
|
4590
5283
|
if (!hit) continue;
|
|
4591
5284
|
if (wantVersion !== null && !satisfiesExactPin(hit.version, wantVersion)) continue;
|
|
4592
5285
|
const parsed = hit.version === null ? null : parseSemver(hit.version);
|
|
@@ -4647,7 +5340,7 @@ function resolveStableNpmEntry(pkg, fromUrl = import.meta.url, deps = {}) {
|
|
|
4647
5340
|
for (const nodeModules of ownNodeModules(fromUrl)) {
|
|
4648
5341
|
if (nodeModules.includes(NPX_CACHE_MARKER)) continue;
|
|
4649
5342
|
if (isEphemeralMountPath(nodeModules)) continue;
|
|
4650
|
-
const hit = packageEntry(
|
|
5343
|
+
const hit = packageEntry(join10(nodeModules, ...pkg.split("/")), pkg);
|
|
4651
5344
|
if (hit) return stableSpellingOf(hit.entry, deps);
|
|
4652
5345
|
}
|
|
4653
5346
|
return null;
|
|
@@ -4664,9 +5357,10 @@ async function resolveOamSpawn(command, args, optedIn = true) {
|
|
|
4664
5357
|
detail: probe.failureDetail,
|
|
4665
5358
|
// No install commands: the install is already there. Pointing OAM_BIN
|
|
4666
5359
|
// 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
|
|
5360
|
+
// the process lifetime, so repairing it needs a restart to take effect
|
|
5361
|
+
// -- except a timeout, which lapses and is probed again on its own.
|
|
4668
5362
|
overrideWith: "OAM_BIN",
|
|
4669
|
-
thenRestart: "restart yaw-mcp; this probe is cached for the process lifetime"
|
|
5363
|
+
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
5364
|
});
|
|
4671
5365
|
} else {
|
|
4672
5366
|
log("warn", "a server opted in to oam but oam is not installed; running it on node instead", {
|
|
@@ -4688,6 +5382,20 @@ async function resolveOamSpawn(command, args, optedIn = true) {
|
|
|
4688
5382
|
resolveEntry: (pkg, wantVersion) => resolveNpmEntry(pkg, void 0, void 0, void 0, wantVersion)
|
|
4689
5383
|
});
|
|
4690
5384
|
}
|
|
5385
|
+
var managedEntryMemo = /* @__PURE__ */ new Map();
|
|
5386
|
+
function resolveManagedSpawn(command, args, fromUrl = import.meta.url, npmCache = npmCacheDir(fromUrl), managedRoot = sidecarsNodeModules(), nodeBin = managedNodeBin()) {
|
|
5387
|
+
return rewriteNpmToManagedNode(command, args, {
|
|
5388
|
+
nodeBin,
|
|
5389
|
+
resolveEntry: (pkg, wantVersion) => {
|
|
5390
|
+
if (wantVersion !== null) return resolveNpmEntry(pkg, fromUrl, npmCache, managedRoot, wantVersion);
|
|
5391
|
+
const memoed = managedEntryMemo.get(pkg);
|
|
5392
|
+
if (memoed !== void 0) return memoed;
|
|
5393
|
+
const hit = resolveNpmEntry(pkg, fromUrl, npmCache, managedRoot, null);
|
|
5394
|
+
if (hit !== null && !hit.includes(NPX_CACHE_MARKER)) managedEntryMemo.set(pkg, hit);
|
|
5395
|
+
return hit;
|
|
5396
|
+
}
|
|
5397
|
+
});
|
|
5398
|
+
}
|
|
4691
5399
|
function isOamCommand(command) {
|
|
4692
5400
|
const base = command.split(/[\\/]/).pop() ?? command;
|
|
4693
5401
|
return /^oam(\.exe)?$/i.test(base);
|
|
@@ -4806,7 +5514,7 @@ function packageDirOf(p) {
|
|
|
4806
5514
|
function defaultInstalledVersion(pkgDir) {
|
|
4807
5515
|
if (isTestSandbox()) return null;
|
|
4808
5516
|
try {
|
|
4809
|
-
const parsed = JSON.parse(
|
|
5517
|
+
const parsed = JSON.parse(readFileSync3(join11(pkgDir, "package.json"), "utf8"));
|
|
4810
5518
|
return typeof parsed.version === "string" ? parsed.version : null;
|
|
4811
5519
|
} catch {
|
|
4812
5520
|
return null;
|
|
@@ -4990,18 +5698,18 @@ async function oamFloorLines(probe) {
|
|
|
4990
5698
|
}
|
|
4991
5699
|
async function defaultRunningPrefix(argvPath) {
|
|
4992
5700
|
if (isTestSandbox()) return null;
|
|
4993
|
-
const { detectRunningInstallPrefix: detectRunningInstallPrefix2 } = await import("./auto-upgrade-
|
|
5701
|
+
const { detectRunningInstallPrefix: detectRunningInstallPrefix2 } = await import("./auto-upgrade-EXON4SEO.js");
|
|
4994
5702
|
return detectRunningInstallPrefix2(argvPath);
|
|
4995
5703
|
}
|
|
4996
5704
|
async function defaultAcquireLock(prefixDir) {
|
|
4997
5705
|
if (isTestSandbox()) return () => {
|
|
4998
5706
|
};
|
|
4999
|
-
const { acquireUpgradeLock: acquireUpgradeLock2 } = await import("./auto-upgrade-
|
|
5707
|
+
const { acquireUpgradeLock: acquireUpgradeLock2 } = await import("./auto-upgrade-EXON4SEO.js");
|
|
5000
5708
|
return acquireUpgradeLock2(prefixDir);
|
|
5001
5709
|
}
|
|
5002
5710
|
async function upgradeLockPath(prefixDir) {
|
|
5003
|
-
const { UPGRADE_LOCK_NAME: UPGRADE_LOCK_NAME2 } = await import("./auto-upgrade-
|
|
5004
|
-
return
|
|
5711
|
+
const { UPGRADE_LOCK_NAME: UPGRADE_LOCK_NAME2 } = await import("./auto-upgrade-EXON4SEO.js");
|
|
5712
|
+
return join11(prefixDir, UPGRADE_LOCK_NAME2);
|
|
5005
5713
|
}
|
|
5006
5714
|
async function defaultSpawn(cmd, args, cwd) {
|
|
5007
5715
|
return new Promise((resolve8) => {
|
|
@@ -5062,7 +5770,7 @@ async function runUpgrade(opts = {}) {
|
|
|
5062
5770
|
if (method === "global-npm") {
|
|
5063
5771
|
rawPrefix = await (opts.runningPrefix ?? defaultRunningPrefix)(argvPath);
|
|
5064
5772
|
if (rawPrefix !== null) {
|
|
5065
|
-
const { quoteArgForDisplay: quoteArgForDisplay2, quoteShellArgIfNeeded: quoteShellArgIfNeeded2 } = await import("./auto-upgrade-
|
|
5773
|
+
const { quoteArgForDisplay: quoteArgForDisplay2, quoteShellArgIfNeeded: quoteShellArgIfNeeded2 } = await import("./auto-upgrade-EXON4SEO.js");
|
|
5066
5774
|
const spawnForm = quoteShellArgIfNeeded2(rawPrefix, opts.platform);
|
|
5067
5775
|
const displayForm = quoteArgForDisplay2(rawPrefix, opts.platform);
|
|
5068
5776
|
if (spawnForm !== null && displayForm !== null) {
|
|
@@ -5145,10 +5853,10 @@ async function runUpgrade(opts = {}) {
|
|
|
5145
5853
|
printErr(` ${commandLine}`);
|
|
5146
5854
|
return { exitCode: 2, lines };
|
|
5147
5855
|
}
|
|
5148
|
-
let
|
|
5856
|
+
let releaseLock2 = null;
|
|
5149
5857
|
if (method === "global-npm" && rawPrefix !== null) {
|
|
5150
|
-
|
|
5151
|
-
if (
|
|
5858
|
+
releaseLock2 = await (opts.acquireLock ?? defaultAcquireLock)(rawPrefix);
|
|
5859
|
+
if (releaseLock2 === null) {
|
|
5152
5860
|
printErr(
|
|
5153
5861
|
`yaw-mcp upgrade --run: another yaw-mcp process is upgrading this install (lock: ${await upgradeLockPath(rawPrefix)}); try again once it finishes.`
|
|
5154
5862
|
);
|
|
@@ -5167,7 +5875,7 @@ async function runUpgrade(opts = {}) {
|
|
|
5167
5875
|
try {
|
|
5168
5876
|
code = await runner(runSpec.cmd, runSpec.args, runSpec.cwd);
|
|
5169
5877
|
} finally {
|
|
5170
|
-
|
|
5878
|
+
releaseLock2?.();
|
|
5171
5879
|
}
|
|
5172
5880
|
if (code === 0) {
|
|
5173
5881
|
print("");
|
|
@@ -5194,7 +5902,7 @@ async function runUpgrade(opts = {}) {
|
|
|
5194
5902
|
return { exitCode: 3, lines };
|
|
5195
5903
|
}
|
|
5196
5904
|
function readCurrentVersion() {
|
|
5197
|
-
return true ? "1.
|
|
5905
|
+
return true ? "1.1.1" : "dev";
|
|
5198
5906
|
}
|
|
5199
5907
|
|
|
5200
5908
|
// src/auto-upgrade.ts
|
|
@@ -5217,7 +5925,7 @@ function detectRunningInstallPrefix(argvPath) {
|
|
|
5217
5925
|
} catch {
|
|
5218
5926
|
return null;
|
|
5219
5927
|
}
|
|
5220
|
-
const dir =
|
|
5928
|
+
const dir = dirname4(resolved);
|
|
5221
5929
|
const idx = dir.lastIndexOf(`${sep2}node_modules${sep2}`);
|
|
5222
5930
|
if (idx === -1) return null;
|
|
5223
5931
|
const candidate = dir.slice(0, idx);
|
|
@@ -5248,21 +5956,21 @@ var LOCK_CREATE_RETRY_DELAYS_MS = [1, 5, 20];
|
|
|
5248
5956
|
function sleepSync(ms) {
|
|
5249
5957
|
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
5250
5958
|
}
|
|
5251
|
-
function openLockSync(
|
|
5959
|
+
function openLockSync(path5) {
|
|
5252
5960
|
for (const ms of LOCK_CREATE_RETRY_DELAYS_MS) {
|
|
5253
5961
|
try {
|
|
5254
|
-
return openSync(
|
|
5962
|
+
return openSync(path5, "wx");
|
|
5255
5963
|
} catch (err) {
|
|
5256
5964
|
if (!isWin32TransientFsError(err)) throw err;
|
|
5257
5965
|
sleepSync(ms);
|
|
5258
5966
|
}
|
|
5259
5967
|
}
|
|
5260
|
-
return openSync(
|
|
5968
|
+
return openSync(path5, "wx");
|
|
5261
5969
|
}
|
|
5262
5970
|
function lockHolderAlive(lockPath) {
|
|
5263
5971
|
let pid;
|
|
5264
5972
|
try {
|
|
5265
|
-
pid = Number.parseInt(
|
|
5973
|
+
pid = Number.parseInt(readFileSync4(lockPath, "utf8").trim(), 10);
|
|
5266
5974
|
} catch {
|
|
5267
5975
|
return true;
|
|
5268
5976
|
}
|
|
@@ -5276,8 +5984,8 @@ function lockHolderAlive(lockPath) {
|
|
|
5276
5984
|
}
|
|
5277
5985
|
}
|
|
5278
5986
|
function acquireUpgradeLock(dir, lockName = UPGRADE_LOCK_NAME, opts = {}) {
|
|
5279
|
-
const lockPath =
|
|
5280
|
-
const holderAlive = (
|
|
5987
|
+
const lockPath = join12(dir, lockName);
|
|
5988
|
+
const holderAlive = (path5) => opts.probeHolder ? lockHolderAlive(path5) : true;
|
|
5281
5989
|
const mine = String(process.pid);
|
|
5282
5990
|
const take = () => {
|
|
5283
5991
|
try {
|
|
@@ -5293,7 +6001,7 @@ function acquireUpgradeLock(dir, lockName = UPGRADE_LOCK_NAME, opts = {}) {
|
|
|
5293
6001
|
if (released) return;
|
|
5294
6002
|
released = true;
|
|
5295
6003
|
try {
|
|
5296
|
-
if (
|
|
6004
|
+
if (readFileSync4(lockPath, "utf8").trim() !== mine) return;
|
|
5297
6005
|
unlinkSync(lockPath);
|
|
5298
6006
|
} catch {
|
|
5299
6007
|
}
|
|
@@ -5309,7 +6017,7 @@ function acquireUpgradeLock(dir, lockName = UPGRADE_LOCK_NAME, opts = {}) {
|
|
|
5309
6017
|
const isLive = (ageMs2) => ageMs2 > -UPGRADE_LOCK_FUTURE_SKEW_MS && ageMs2 < UPGRADE_LOCK_STALE_MS;
|
|
5310
6018
|
let ageMs;
|
|
5311
6019
|
try {
|
|
5312
|
-
ageMs = Date.now() -
|
|
6020
|
+
ageMs = Date.now() - statSync3(lockPath).mtimeMs;
|
|
5313
6021
|
} catch {
|
|
5314
6022
|
ageMs = UPGRADE_LOCK_STALE_MS;
|
|
5315
6023
|
}
|
|
@@ -5324,12 +6032,12 @@ function acquireUpgradeLock(dir, lockName = UPGRADE_LOCK_NAME, opts = {}) {
|
|
|
5324
6032
|
}
|
|
5325
6033
|
let stoleLive = false;
|
|
5326
6034
|
try {
|
|
5327
|
-
stoleLive = isLive(Date.now() -
|
|
6035
|
+
stoleLive = isLive(Date.now() - statSync3(stolenPath).mtimeMs) && holderAlive(stolenPath);
|
|
5328
6036
|
} catch {
|
|
5329
6037
|
}
|
|
5330
6038
|
if (stoleLive) {
|
|
5331
6039
|
try {
|
|
5332
|
-
const holder =
|
|
6040
|
+
const holder = readFileSync4(stolenPath, "utf8");
|
|
5333
6041
|
const fd = openLockSync(lockPath);
|
|
5334
6042
|
try {
|
|
5335
6043
|
writeSync(fd, holder);
|
|
@@ -5362,10 +6070,10 @@ function defaultFetchLatest() {
|
|
|
5362
6070
|
}
|
|
5363
6071
|
var UPGRADE_CHECK_INTERVAL_MS = 60 * 60 * 1e3;
|
|
5364
6072
|
var UPGRADE_ATTEMPT_COOLDOWN_MS = 6 * 60 * 60 * 1e3;
|
|
5365
|
-
function readMemo(
|
|
6073
|
+
function readMemo(path5) {
|
|
5366
6074
|
let parsed;
|
|
5367
6075
|
try {
|
|
5368
|
-
parsed = JSON.parse(
|
|
6076
|
+
parsed = JSON.parse(readFileSync4(path5, "utf8"));
|
|
5369
6077
|
} catch {
|
|
5370
6078
|
return null;
|
|
5371
6079
|
}
|
|
@@ -5375,32 +6083,32 @@ function readMemo(path3) {
|
|
|
5375
6083
|
const ageMs = Date.now() - memo.at;
|
|
5376
6084
|
return ageMs < -UPGRADE_LOCK_FUTURE_SKEW_MS ? null : { ageMs, memo };
|
|
5377
6085
|
}
|
|
5378
|
-
function writeMemo(
|
|
6086
|
+
function writeMemo(path5, fields) {
|
|
5379
6087
|
try {
|
|
5380
|
-
writeFileSync(
|
|
6088
|
+
writeFileSync(path5, `${JSON.stringify({ at: Date.now(), ...fields })}
|
|
5381
6089
|
`);
|
|
5382
6090
|
} catch {
|
|
5383
6091
|
}
|
|
5384
6092
|
}
|
|
5385
6093
|
function checkMemoPath() {
|
|
5386
|
-
return
|
|
6094
|
+
return join12(tmpdir(), `.yaw-mcp-upgrade-check-${process.getuid?.() ?? "win"}.json`);
|
|
5387
6095
|
}
|
|
5388
|
-
function readCheckMemo(
|
|
5389
|
-
const read = readMemo(
|
|
6096
|
+
function readCheckMemo(path5) {
|
|
6097
|
+
const read = readMemo(path5);
|
|
5390
6098
|
if (read === null || read.ageMs >= UPGRADE_CHECK_INTERVAL_MS) return null;
|
|
5391
6099
|
const latest = read.memo.latest;
|
|
5392
6100
|
if (latest !== null && typeof latest !== "string") return null;
|
|
5393
6101
|
return { latest };
|
|
5394
6102
|
}
|
|
5395
|
-
function writeCheckMemo(
|
|
5396
|
-
writeMemo(
|
|
6103
|
+
function writeCheckMemo(path5, latest) {
|
|
6104
|
+
writeMemo(path5, { latest });
|
|
5397
6105
|
}
|
|
5398
6106
|
function attemptMemoPath(dir, lockName) {
|
|
5399
|
-
return
|
|
6107
|
+
return join12(dir, `${lockName}.attempt`);
|
|
5400
6108
|
}
|
|
5401
6109
|
function attemptMemoFallbackPath(dir, lockName, fallbackDir) {
|
|
5402
6110
|
const scope = createHash("sha256").update(`${dir}\0${lockName}`).digest("hex").slice(0, 16);
|
|
5403
|
-
return
|
|
6111
|
+
return join12(fallbackDir, `.yaw-mcp-upgrade-attempt-${process.getuid?.() ?? "win"}-${scope}.json`);
|
|
5404
6112
|
}
|
|
5405
6113
|
function defaultCheckedRecently() {
|
|
5406
6114
|
if (isTestSandbox()) return null;
|
|
@@ -5486,7 +6194,7 @@ function defaultSpawn2(cmd, args, onDone = () => {
|
|
|
5486
6194
|
}
|
|
5487
6195
|
async function maybeAutoUpgrade(deps = {}) {
|
|
5488
6196
|
if (isFeatureDisabled("YAW_MCP_AUTO_UPGRADE")) return;
|
|
5489
|
-
const current = deps.currentVersion ?? (true ? "1.
|
|
6197
|
+
const current = deps.currentVersion ?? (true ? "1.1.1" : "dev");
|
|
5490
6198
|
if (current === "dev") return;
|
|
5491
6199
|
const argvPath = deps.argvPath ?? process.argv[1];
|
|
5492
6200
|
let method = (deps.isSeaImpl ? await deps.isSeaImpl() : await detectSea()) ? "binary" : detectInstallMethod(argvPath, realpathSync3, ["unknown", "local-node-modules"]);
|
|
@@ -5517,8 +6225,8 @@ async function maybeAutoUpgrade(deps = {}) {
|
|
|
5517
6225
|
});
|
|
5518
6226
|
return;
|
|
5519
6227
|
}
|
|
5520
|
-
const
|
|
5521
|
-
if (
|
|
6228
|
+
const releaseLock2 = (deps.acquireLockImpl ?? defaultAcquireLock2)(lockDir, lockName);
|
|
6229
|
+
if (releaseLock2 === null) {
|
|
5522
6230
|
log("info", "yaw-mcp self-upgrade: another process is already upgrading this install; skipping this one", {
|
|
5523
6231
|
current,
|
|
5524
6232
|
latest,
|
|
@@ -5543,9 +6251,9 @@ async function maybeAutoUpgrade(deps = {}) {
|
|
|
5543
6251
|
}
|
|
5544
6252
|
(deps.recordAttemptImpl ?? defaultRecordAttempt)(lockDir, lockName, latest);
|
|
5545
6253
|
try {
|
|
5546
|
-
(deps.spawnImpl ?? defaultSpawn2)(globalSpec.cmd, globalSpec.args,
|
|
6254
|
+
(deps.spawnImpl ?? defaultSpawn2)(globalSpec.cmd, globalSpec.args, releaseLock2);
|
|
5547
6255
|
} catch (err) {
|
|
5548
|
-
|
|
6256
|
+
releaseLock2();
|
|
5549
6257
|
log("warn", `yaw-mcp self-upgrade: could not start ${globalSpec.cmd}`, {
|
|
5550
6258
|
error: err instanceof Error ? err.message : String(err)
|
|
5551
6259
|
});
|
|
@@ -5596,8 +6304,11 @@ export {
|
|
|
5596
6304
|
setLogSurface,
|
|
5597
6305
|
log,
|
|
5598
6306
|
createStreamWriter,
|
|
5599
|
-
isWin32TransientFsError,
|
|
5600
6307
|
atomicWriteFile,
|
|
6308
|
+
withFileLock,
|
|
6309
|
+
setJsonKey,
|
|
6310
|
+
stripJsoncComments,
|
|
6311
|
+
stripTrailingCommas,
|
|
5601
6312
|
parseJsonc,
|
|
5602
6313
|
editJsoncPath,
|
|
5603
6314
|
cacheDir,
|
|
@@ -5615,6 +6326,7 @@ export {
|
|
|
5615
6326
|
isFeatureDisabled,
|
|
5616
6327
|
isTestSandbox,
|
|
5617
6328
|
launchOf,
|
|
6329
|
+
canonicalJson,
|
|
5618
6330
|
describeValueShape,
|
|
5619
6331
|
readStrictJson,
|
|
5620
6332
|
CURRENT_OS,
|
|
@@ -5666,6 +6378,16 @@ export {
|
|
|
5666
6378
|
describeJsonShape,
|
|
5667
6379
|
unparseableConfigFix,
|
|
5668
6380
|
blockedContainerFix,
|
|
6381
|
+
STATE_SCHEMA_VERSION,
|
|
6382
|
+
STATE_FILENAME,
|
|
6383
|
+
isPersistenceDisabled,
|
|
6384
|
+
isReadableStateVersion,
|
|
6385
|
+
PREWARM_FAILURE_BACKOFF_MS,
|
|
6386
|
+
statePath,
|
|
6387
|
+
loadState,
|
|
6388
|
+
loadStateClassified,
|
|
6389
|
+
StateSync,
|
|
6390
|
+
isFileNotFound,
|
|
5669
6391
|
packageName,
|
|
5670
6392
|
specConstraint,
|
|
5671
6393
|
isRegistrySpec,
|
|
@@ -5676,6 +6398,7 @@ export {
|
|
|
5676
6398
|
oamPublishesBinaryForThisMachine,
|
|
5677
6399
|
oamNoBinaryReason,
|
|
5678
6400
|
oamInstallAdvice,
|
|
6401
|
+
oamBuildTreeFix,
|
|
5679
6402
|
oamHeapOomHint,
|
|
5680
6403
|
oamFailureLabel,
|
|
5681
6404
|
compareVersions,
|
|
@@ -5683,6 +6406,7 @@ export {
|
|
|
5683
6406
|
nodeLaunchKind,
|
|
5684
6407
|
resolveStableNpmEntry,
|
|
5685
6408
|
resolveOamSpawn,
|
|
6409
|
+
resolveManagedSpawn,
|
|
5686
6410
|
isOamCommand,
|
|
5687
6411
|
isOamLaunch,
|
|
5688
6412
|
POSIX_SHELL,
|