@wezzard/mcp-relay 0.18.0 → 0.20.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.
@@ -1,6 +1,6 @@
1
1
  {
2
- "server.mjs": "2b04c5cf78833f392f9fe8570a0099a1f276c2c24ad036dc8107f9c24371c95a",
3
- "receiver.mjs": "8b5b6fdd6cb789cdc4060e367a155cb42e7cf34187044c899789bf76b7bb0e14",
4
- "doctor.mjs": "4e3d5a58ead4be3afe461691b6bff6f1f933bbde70b19fd67677f158975b6e3f",
2
+ "server.mjs": "1244a64e2be1b9feded8fba2d28b889fa3ccc12534ab261ffaa2e6344b515c20",
3
+ "receiver.mjs": "4578f2b8524931e34eb7eea8ea37d52ec054039fc189a6aa9b89466c716eafaf",
4
+ "doctor.mjs": "04c60ecbb803e8acad636c0af22ec4eb5e0b110689435848bd68e9ea5391f9ce",
5
5
  "review-data.mjs": "da7b6b35d060e09cfc445df4febda02a1a0c0f23d9b854866aed8d604cbb3993"
6
6
  }
package/dist/receiver.mjs CHANGED
@@ -829,6 +829,7 @@ function describe(value) {
829
829
 
830
830
  // src/guest/image-evidence.ts
831
831
  var safeId = (value) => typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9_.-]{0,159}$/.test(value);
832
+ var declinedPath = (executionId) => ["state", "receiver", "declined", `${executionId}.json`];
832
833
  function afterRequested(request) {
833
834
  return request?.diagnostic !== true && request?.capture !== false && !["first", "member"].includes(request?.snapshots?.group?.phase ?? "");
834
835
  }
@@ -1067,6 +1068,33 @@ async function captureWithDriver(outFile, driver, dispatch = runArgv) {
1067
1068
  if (value.screenshot_file_path !== outFile) throw new Error("display capture returned a missing or mismatched screenshot_file_path");
1068
1069
  }
1069
1070
  async function receive(input, options = {}) {
1071
+ const result = await answer(input, options);
1072
+ const executionId = input?.executionId;
1073
+ if (!safeId(executionId) || result.executionId !== executionId) return result;
1074
+ const root = runtimeRoot(options);
1075
+ try {
1076
+ if (!await optionalJson(join3(root, "state", "receiver", "receipts", `${executionId}.json`))) await durableJson(join3(root, ...declinedPath(executionId)), result);
1077
+ } catch {
1078
+ }
1079
+ return result;
1080
+ }
1081
+ var runtimeRoot = (options) => resolve(options.root ?? process.env.RELAY_RUNTIME_ROOT ?? "/var/tmp/relay-driver-runtime");
1082
+ async function stopCapture(path) {
1083
+ if (!text(path)) return;
1084
+ const listing = await new Promise((done) => execFile("ps", ["-axww", "-o", "pid=,command="], { encoding: "utf8", maxBuffer: 16 * 1024 * 1024 }, (error, stdout) => done(error ? "" : stdout)));
1085
+ const pids = listing.split("\n").map((line) => /^\s*(\d+)\s+(.*)$/.exec(line)).filter((m) => !!m && Number(m[1]) !== process.pid && m[2].includes(path) && m[2].includes("get_desktop_state")).map((m) => Number(m[1]));
1086
+ for (const pid of pids) {
1087
+ try {
1088
+ process.kill(-pid, "SIGKILL");
1089
+ } catch {
1090
+ try {
1091
+ process.kill(pid, "SIGKILL");
1092
+ } catch {
1093
+ }
1094
+ }
1095
+ }
1096
+ }
1097
+ async function answer(input, options) {
1070
1098
  const request = input;
1071
1099
  const executionId = request && safeId(request.executionId) ? request.executionId : "unknown";
1072
1100
  const evidenceMode = request?.diagnostic === true ? "diagnostic" : void 0;
@@ -1082,7 +1110,7 @@ async function receive(input, options = {}) {
1082
1110
  return response("refused", "request is not serializable JSON");
1083
1111
  }
1084
1112
  if (!request || !safeId(request.executionId) || !safeId(request.sessionId) || !safeId(request.attemptId)) return response("refused", "safe executionId, sessionId and attemptId are required");
1085
- const root = resolve(options.root ?? process.env.RELAY_RUNTIME_ROOT ?? "/var/tmp/relay-driver-runtime");
1113
+ const root = runtimeRoot(options);
1086
1114
  const state = join3(root, "state");
1087
1115
  const lock = join3(root, ".receiver-lock");
1088
1116
  const receiptPath = join3(state, "receiver", "receipts", `${executionId}.json`);
@@ -1092,9 +1120,30 @@ async function receive(input, options = {}) {
1092
1120
  const ownerPath = join3(lock, "owner.json");
1093
1121
  const operationPids = [];
1094
1122
  let pendingSpawn = false;
1095
- const markLock = (active, settled = false) => durableJson(ownerPath, { pid: process.pid, executionId, active, operationPids, pendingSpawn, settled });
1096
- const runOwned = async (argv2, dispatchOptions) => {
1097
- pendingSpawn = true;
1123
+ let capturePath;
1124
+ const ownerRecord = (active, settled = false) => ({ pid: process.pid, executionId, active, operationPids, pendingSpawn, ...pendingSpawn === "capture" && capturePath ? { capturePath } : {}, settled });
1125
+ const markLock = (active, settled = false) => durableJson(ownerPath, ownerRecord(active, settled));
1126
+ const takeLock = async () => {
1127
+ const staged = join3(root, `.receiver-lock-new-${randomUUID()}`);
1128
+ await mkdir3(staged);
1129
+ try {
1130
+ await durableJson(join3(staged, "owner.json"), ownerRecord(false));
1131
+ await rename3(staged, lock);
1132
+ return true;
1133
+ } catch (error) {
1134
+ await rm2(staged, { recursive: true, force: true }).catch(() => {
1135
+ });
1136
+ if (["EEXIST", "ENOTEMPTY"].includes(error.code ?? "")) return false;
1137
+ throw error;
1138
+ }
1139
+ };
1140
+ const dropLock = async () => {
1141
+ const retired = join3(root, `.receiver-lock-retired-${randomUUID()}`);
1142
+ await rename3(lock, retired);
1143
+ await rm2(retired, { recursive: true, force: true });
1144
+ };
1145
+ const owned = (kind) => async (argv2, dispatchOptions) => {
1146
+ pendingSpawn = kind;
1098
1147
  await markLock(true);
1099
1148
  const result = await runArgv(argv2, dispatchOptions, async (pid) => {
1100
1149
  operationPids.push(pid);
@@ -1106,6 +1155,7 @@ async function receive(input, options = {}) {
1106
1155
  if (!keepLock) await markLock(false);
1107
1156
  return result;
1108
1157
  };
1158
+ const runOwned = owned("action");
1109
1159
  let reconcileGroup;
1110
1160
  let dispatchResult;
1111
1161
  let argv;
@@ -1115,37 +1165,39 @@ async function receive(input, options = {}) {
1115
1165
  const receipt = await optionalJson(receiptPath);
1116
1166
  if (receipt) return receipt;
1117
1167
  if (await optionalJson(startedPath)) return response("uncertain", "execution was already started; it will not be replayed");
1118
- try {
1119
- await mkdir3(lock);
1120
- ownsLock = true;
1121
- } catch (error) {
1122
- if (error.code !== "EEXIST") throw error;
1168
+ ownsLock = await takeLock();
1169
+ if (!ownsLock) {
1123
1170
  const owner = await optionalJson(ownerPath);
1124
1171
  const absent = (pid, group2 = false) => {
1125
1172
  if (!Number.isInteger(pid) || !pid || pid < 1) return false;
1126
1173
  try {
1127
1174
  process.kill(group2 ? -pid : pid, 0);
1128
1175
  return false;
1129
- } catch (error2) {
1130
- return error2.code === "ESRCH";
1176
+ } catch (error) {
1177
+ return error.code === "ESRCH";
1131
1178
  }
1132
1179
  };
1133
- if (!owner || !absent(owner.pid) && owner.settled !== true || owner.active !== false && !(owner.pendingSpawn === false && Array.isArray(owner.operationPids) && owner.operationPids.length > 0 && owner.operationPids.every((pid) => absent(pid, true)))) return response("uncertain", "receiver lock ownership or active operation is unconfirmed; input is not replayed");
1180
+ const notStarted = "so nothing of this execution started; it is not queued or replayed";
1181
+ const held = (detail) => owner?.executionId === executionId ? response("uncertain", `this execution already holds the receiver lock (${detail}); input is not replayed`) : response("refused", `a previous run, ${safeId(owner?.executionId) ? `execution ${owner.executionId}` : "one its lock does not name"}, still holds the receiver lock (${detail}), ${notStarted}. A new run is accepted once the receiver can prove that run ended`);
1182
+ if (!owner) return held("the lock records no owner");
1183
+ if (!absent(owner.pid) && owner.settled !== true) return held(`its receiver, process ${owner.pid}, is still running`);
1184
+ const groupsGone = Array.isArray(owner.operationPids) && owner.operationPids.every((pid) => absent(pid, true));
1185
+ const captureOnly = owner.pendingSpawn === "capture" && absent(owner.pid) && groupsGone;
1186
+ if (owner.active !== false && !captureOnly && !(owner.pendingSpawn === false && Array.isArray(owner.operationPids) && owner.operationPids.length > 0 && groupsGone)) {
1187
+ const groups2 = Array.isArray(owner.operationPids) ? owner.operationPids.filter((pid) => !absent(pid, true)) : [];
1188
+ return held(owner.pendingSpawn === true || owner.pendingSpawn === "action" ? "its receiver ended while starting a process it had not yet recorded" : !groups2.length ? "its receiver ended without recording the processes it started" : `its receiver ended, and its script's process group ${groups2.join(", ")} still runs`);
1189
+ }
1134
1190
  try {
1135
1191
  await mkdir3(join3(lock, "recovery"));
1136
1192
  } catch {
1137
- return response("uncertain", "receiver lock reconciliation is already in progress");
1193
+ return response("refused", `receiver lock reconciliation is already in progress, ${notStarted}`);
1138
1194
  }
1139
- if (JSON.stringify(await optionalJson(ownerPath)) !== JSON.stringify(owner)) return response("uncertain", "receiver lock changed during reconciliation");
1195
+ if (JSON.stringify(await optionalJson(ownerPath)) !== JSON.stringify(owner)) return response("refused", `receiver lock changed during reconciliation, ${notStarted}`);
1196
+ if (captureOnly) await stopCapture(owner.capturePath);
1140
1197
  await rename3(lock, join3(root, `.receiver-lock-retired-${randomUUID()}`));
1141
- try {
1142
- await mkdir3(lock);
1143
- ownsLock = true;
1144
- } catch {
1145
- return response("uncertain", "receiver acquired by another operation during reconciliation");
1146
- }
1198
+ ownsLock = await takeLock().catch(() => false);
1199
+ if (!ownsLock) return response("refused", `receiver acquired by another operation during reconciliation, ${notStarted}`);
1147
1200
  }
1148
- await markLock(false);
1149
1201
  const recovered = await optionalJson(receiptPath);
1150
1202
  if (recovered) return recovered;
1151
1203
  if (await optionalJson(startedPath)) return response("uncertain", "execution was already started; it will not be replayed");
@@ -1159,11 +1211,13 @@ async function receive(input, options = {}) {
1159
1211
  let captureFailure;
1160
1212
  const capture = async (out) => {
1161
1213
  try {
1162
- pendingSpawn = true;
1214
+ pendingSpawn = "capture";
1215
+ capturePath = out;
1163
1216
  await markLock(true);
1164
- await (options.capture ?? ((file2) => captureWithDriver(file2, options.cuaDriver ?? process.env.RELAY_CUA_DRIVER ?? (process.platform === "darwin" ? "/Applications/CuaDriver.app/Contents/MacOS/cua-driver" : "cua-driver"), runOwned)))(out);
1217
+ await (options.capture ?? ((file2) => captureWithDriver(file2, options.cuaDriver ?? process.env.RELAY_CUA_DRIVER ?? (process.platform === "darwin" ? "/Applications/CuaDriver.app/Contents/MacOS/cua-driver" : "cua-driver"), owned("capture"))))(out);
1165
1218
  if (!keepLock) {
1166
1219
  pendingSpawn = false;
1220
+ capturePath = void 0;
1167
1221
  await markLock(false);
1168
1222
  }
1169
1223
  const file = await stat2(out);
@@ -1277,7 +1331,7 @@ async function receive(input, options = {}) {
1277
1331
  try {
1278
1332
  const outcome = await engine.callWithHandle(actionId, params, async () => {
1279
1333
  dispatched = true;
1280
- pendingSpawn = true;
1334
+ pendingSpawn = "action";
1281
1335
  await markLock(true);
1282
1336
  try {
1283
1337
  const dispatchOptions = { cwd: request.cwd, env: process.env, timeoutMs };
@@ -1316,7 +1370,7 @@ async function receive(input, options = {}) {
1316
1370
  } catch {
1317
1371
  keepLock = true;
1318
1372
  }
1319
- if (ownsLock && !keepLock) await rm2(lock, { recursive: true, force: true }).catch(() => {
1373
+ if (ownsLock && !keepLock) await dropLock().catch(() => {
1320
1374
  });
1321
1375
  else if (ownsLock) await markLock(true, true).catch(() => {
1322
1376
  });