oh-my-codex 0.21.47 → 0.22.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.
Files changed (176) hide show
  1. package/Cargo.lock +6 -6
  2. package/Cargo.toml +1 -1
  3. package/README.md +15 -0
  4. package/crates/omx-mux/src/herdr.rs +481 -0
  5. package/crates/omx-mux/src/lib.rs +5 -0
  6. package/dist/cli/__tests__/plugin-cache-live-pin-3790.test.js +707 -47
  7. package/dist/cli/__tests__/plugin-cache-live-pin-3790.test.js.map +1 -1
  8. package/dist/cli/__tests__/session-compare.test.d.ts +2 -0
  9. package/dist/cli/__tests__/session-compare.test.d.ts.map +1 -0
  10. package/dist/cli/__tests__/session-compare.test.js +183 -0
  11. package/dist/cli/__tests__/session-compare.test.js.map +1 -0
  12. package/dist/cli/__tests__/setup-install-mode.test.js +7 -1
  13. package/dist/cli/__tests__/setup-install-mode.test.js.map +1 -1
  14. package/dist/cli/__tests__/team.test.js +8 -0
  15. package/dist/cli/__tests__/team.test.js.map +1 -1
  16. package/dist/cli/plugin-marketplace.d.ts.map +1 -1
  17. package/dist/cli/plugin-marketplace.js +328 -142
  18. package/dist/cli/plugin-marketplace.js.map +1 -1
  19. package/dist/cli/session-compare.d.ts +11 -0
  20. package/dist/cli/session-compare.d.ts.map +1 -0
  21. package/dist/cli/session-compare.js +172 -0
  22. package/dist/cli/session-compare.js.map +1 -0
  23. package/dist/cli/session-search.d.ts.map +1 -1
  24. package/dist/cli/session-search.js +7 -0
  25. package/dist/cli/session-search.js.map +1 -1
  26. package/dist/cli/team.d.ts.map +1 -1
  27. package/dist/cli/team.js +2 -0
  28. package/dist/cli/team.js.map +1 -1
  29. package/dist/config/__tests__/multiplexer.test.d.ts +2 -0
  30. package/dist/config/__tests__/multiplexer.test.d.ts.map +1 -0
  31. package/dist/config/__tests__/multiplexer.test.js +118 -0
  32. package/dist/config/__tests__/multiplexer.test.js.map +1 -0
  33. package/dist/config/multiplexer.d.ts +29 -0
  34. package/dist/config/multiplexer.d.ts.map +1 -0
  35. package/dist/config/multiplexer.js +91 -0
  36. package/dist/config/multiplexer.js.map +1 -0
  37. package/dist/hooks/__tests__/sunset-routing-contract.test.js +4 -2
  38. package/dist/hooks/__tests__/sunset-routing-contract.test.js.map +1 -1
  39. package/dist/scripts/__tests__/codex-native-hook.test.js +281 -1
  40. package/dist/scripts/__tests__/codex-native-hook.test.js.map +1 -1
  41. package/dist/scripts/__tests__/generate-release-body.test.js +142 -5
  42. package/dist/scripts/__tests__/generate-release-body.test.js.map +1 -1
  43. package/dist/scripts/codex-native-hook.d.ts.map +1 -1
  44. package/dist/scripts/codex-native-hook.js +8 -2
  45. package/dist/scripts/codex-native-hook.js.map +1 -1
  46. package/dist/scripts/generate-release-body.d.ts.map +1 -1
  47. package/dist/scripts/generate-release-body.js +14 -0
  48. package/dist/scripts/generate-release-body.js.map +1 -1
  49. package/dist/scripts/notify-hook/team-worker-lease.d.ts +15 -0
  50. package/dist/scripts/notify-hook/team-worker-lease.d.ts.map +1 -0
  51. package/dist/scripts/notify-hook/team-worker-lease.js +63 -0
  52. package/dist/scripts/notify-hook/team-worker-lease.js.map +1 -0
  53. package/dist/scripts/notify-hook/team-worker-posttooluse.d.ts +1 -0
  54. package/dist/scripts/notify-hook/team-worker-posttooluse.d.ts.map +1 -1
  55. package/dist/scripts/notify-hook/team-worker-posttooluse.js +16 -5
  56. package/dist/scripts/notify-hook/team-worker-posttooluse.js.map +1 -1
  57. package/dist/scripts/notify-hook/team-worker.d.ts +12 -1
  58. package/dist/scripts/notify-hook/team-worker.d.ts.map +1 -1
  59. package/dist/scripts/notify-hook/team-worker.js +16 -4
  60. package/dist/scripts/notify-hook/team-worker.js.map +1 -1
  61. package/dist/session-history/__tests__/compare.test.d.ts +2 -0
  62. package/dist/session-history/__tests__/compare.test.d.ts.map +1 -0
  63. package/dist/session-history/__tests__/compare.test.js +257 -0
  64. package/dist/session-history/__tests__/compare.test.js.map +1 -0
  65. package/dist/session-history/compare.d.ts +74 -0
  66. package/dist/session-history/compare.d.ts.map +1 -0
  67. package/dist/session-history/compare.js +165 -0
  68. package/dist/session-history/compare.js.map +1 -0
  69. package/dist/team/__tests__/api-interop.test.js +3 -3
  70. package/dist/team/__tests__/cross-rebase-smoke.test.js +263 -31
  71. package/dist/team/__tests__/cross-rebase-smoke.test.js.map +1 -1
  72. package/dist/team/__tests__/herdr-session-realherdr.test.d.ts +2 -0
  73. package/dist/team/__tests__/herdr-session-realherdr.test.d.ts.map +1 -0
  74. package/dist/team/__tests__/herdr-session-realherdr.test.js +68 -0
  75. package/dist/team/__tests__/herdr-session-realherdr.test.js.map +1 -0
  76. package/dist/team/__tests__/herdr-session.test.d.ts +2 -0
  77. package/dist/team/__tests__/herdr-session.test.d.ts.map +1 -0
  78. package/dist/team/__tests__/herdr-session.test.js +228 -0
  79. package/dist/team/__tests__/herdr-session.test.js.map +1 -0
  80. package/dist/team/__tests__/idle-nudge.test.js +84 -0
  81. package/dist/team/__tests__/idle-nudge.test.js.map +1 -1
  82. package/dist/team/__tests__/rebalance-policy.test.js +27 -9
  83. package/dist/team/__tests__/rebalance-policy.test.js.map +1 -1
  84. package/dist/team/__tests__/runtime-delivery.test.d.ts +2 -0
  85. package/dist/team/__tests__/runtime-delivery.test.d.ts.map +1 -0
  86. package/dist/team/__tests__/runtime-delivery.test.js +570 -0
  87. package/dist/team/__tests__/runtime-delivery.test.js.map +1 -0
  88. package/dist/team/__tests__/runtime.test.js +332 -18
  89. package/dist/team/__tests__/runtime.test.js.map +1 -1
  90. package/dist/team/__tests__/scaling.test.js +293 -0
  91. package/dist/team/__tests__/scaling.test.js.map +1 -1
  92. package/dist/team/__tests__/state-renewal.test.d.ts +2 -0
  93. package/dist/team/__tests__/state-renewal.test.d.ts.map +1 -0
  94. package/dist/team/__tests__/state-renewal.test.js +555 -0
  95. package/dist/team/__tests__/state-renewal.test.js.map +1 -0
  96. package/dist/team/__tests__/task-lease.test.d.ts +2 -0
  97. package/dist/team/__tests__/task-lease.test.d.ts.map +1 -0
  98. package/dist/team/__tests__/task-lease.test.js +179 -0
  99. package/dist/team/__tests__/task-lease.test.js.map +1 -0
  100. package/dist/team/__tests__/team-ops-contract.test.js +1 -0
  101. package/dist/team/__tests__/team-ops-contract.test.js.map +1 -1
  102. package/dist/team/__tests__/worker-bootstrap.test.js +2 -0
  103. package/dist/team/__tests__/worker-bootstrap.test.js.map +1 -1
  104. package/dist/team/api-interop.d.ts +1 -1
  105. package/dist/team/api-interop.d.ts.map +1 -1
  106. package/dist/team/api-interop.js +13 -1
  107. package/dist/team/api-interop.js.map +1 -1
  108. package/dist/team/goal-workflow.js +1 -1
  109. package/dist/team/herdr-session.d.ts +234 -0
  110. package/dist/team/herdr-session.d.ts.map +1 -0
  111. package/dist/team/herdr-session.js +489 -0
  112. package/dist/team/herdr-session.js.map +1 -0
  113. package/dist/team/idle-nudge.d.ts +16 -4
  114. package/dist/team/idle-nudge.d.ts.map +1 -1
  115. package/dist/team/idle-nudge.js +51 -19
  116. package/dist/team/idle-nudge.js.map +1 -1
  117. package/dist/team/mcp-comm.d.ts +1 -1
  118. package/dist/team/mcp-comm.d.ts.map +1 -1
  119. package/dist/team/rebalance-policy.d.ts.map +1 -1
  120. package/dist/team/rebalance-policy.js +5 -2
  121. package/dist/team/rebalance-policy.js.map +1 -1
  122. package/dist/team/runtime.d.ts.map +1 -1
  123. package/dist/team/runtime.js +356 -81
  124. package/dist/team/runtime.js.map +1 -1
  125. package/dist/team/scaling.d.ts.map +1 -1
  126. package/dist/team/scaling.js +332 -143
  127. package/dist/team/scaling.js.map +1 -1
  128. package/dist/team/state/monitor.d.ts +3 -0
  129. package/dist/team/state/monitor.d.ts.map +1 -1
  130. package/dist/team/state/monitor.js +3 -0
  131. package/dist/team/state/monitor.js.map +1 -1
  132. package/dist/team/state/tasks.d.ts +2 -1
  133. package/dist/team/state/tasks.d.ts.map +1 -1
  134. package/dist/team/state/tasks.js +28 -0
  135. package/dist/team/state/tasks.js.map +1 -1
  136. package/dist/team/state/types.d.ts +8 -0
  137. package/dist/team/state/types.d.ts.map +1 -1
  138. package/dist/team/state/types.js.map +1 -1
  139. package/dist/team/state.d.ts +14 -2
  140. package/dist/team/state.d.ts.map +1 -1
  141. package/dist/team/state.js +29 -5
  142. package/dist/team/state.js.map +1 -1
  143. package/dist/team/task-lease.d.ts +4 -0
  144. package/dist/team/task-lease.d.ts.map +1 -0
  145. package/dist/team/task-lease.js +74 -0
  146. package/dist/team/task-lease.js.map +1 -0
  147. package/dist/team/team-ops.d.ts +2 -1
  148. package/dist/team/team-ops.d.ts.map +1 -1
  149. package/dist/team/team-ops.js +1 -0
  150. package/dist/team/team-ops.js.map +1 -1
  151. package/dist/team/tmux-session.d.ts +1 -1
  152. package/dist/team/tmux-session.d.ts.map +1 -1
  153. package/dist/team/tmux-session.js +10 -2
  154. package/dist/team/tmux-session.js.map +1 -1
  155. package/dist/team/worker-bootstrap.d.ts.map +1 -1
  156. package/dist/team/worker-bootstrap.js +14 -1
  157. package/dist/team/worker-bootstrap.js.map +1 -1
  158. package/package.json +1 -1
  159. package/plugins/oh-my-codex/.codex-plugin/plugin.json +1 -1
  160. package/plugins/oh-my-codex/hooks/codex-native-hook.mjs +208 -25
  161. package/plugins/oh-my-codex/skills/worker/SKILL.md +7 -1
  162. package/prompts/build-fixer.md +2 -1
  163. package/prompts/debugger.md +2 -1
  164. package/prompts/git-master.md +2 -1
  165. package/prompts/style-reviewer.md +2 -1
  166. package/prompts/team-executor.md +3 -2
  167. package/prompts/vision.md +2 -1
  168. package/prompts/writer.md +2 -1
  169. package/skills/worker/SKILL.md +7 -1
  170. package/src/scripts/__tests__/codex-native-hook.test.ts +279 -0
  171. package/src/scripts/__tests__/generate-release-body.test.ts +151 -5
  172. package/src/scripts/codex-native-hook.ts +8 -2
  173. package/src/scripts/generate-release-body.ts +16 -0
  174. package/src/scripts/notify-hook/team-worker-lease.ts +69 -0
  175. package/src/scripts/notify-hook/team-worker-posttooluse.ts +16 -4
  176. package/src/scripts/notify-hook/team-worker.ts +15 -4
@@ -180,6 +180,39 @@ async function openDirectoryRef(path) {
180
180
  throw error;
181
181
  }
182
182
  }
183
+ /**
184
+ * Opens the external live-pin directory for a given version.
185
+ * Anchors .omx-pins as a child of cacheBase to prevent symlink escapes.
186
+ */
187
+ async function openExternalLivePinDirectory(cacheBaseRef, version) {
188
+ // First, anchor or create .omx-pins as a child of cacheBase
189
+ let pinParentRef;
190
+ try {
191
+ try {
192
+ pinParentRef = await openDirectoryChild(cacheBaseRef, ".omx-pins");
193
+ }
194
+ catch (error) {
195
+ if (!isMissingPathError(error))
196
+ throw error;
197
+ await mkdirDirectoryChild(cacheBaseRef, ".omx-pins");
198
+ pinParentRef = await openDirectoryChild(cacheBaseRef, ".omx-pins");
199
+ }
200
+ // Then open or create the version directory under .omx-pins
201
+ try {
202
+ return await openDirectoryChild(pinParentRef, version);
203
+ }
204
+ catch (error) {
205
+ if (!isMissingPathError(error))
206
+ throw error;
207
+ await mkdirDirectoryChild(pinParentRef, version);
208
+ return await openDirectoryChild(pinParentRef, version);
209
+ }
210
+ }
211
+ finally {
212
+ if (pinParentRef)
213
+ await pinParentRef.handle.close().catch(() => undefined);
214
+ }
215
+ }
183
216
  async function openDirectoryChild(parent, name) {
184
217
  const noFollowFlags = typeof fsConstants.O_NOFOLLOW === "number" ? fsConstants.O_NOFOLLOW : 0;
185
218
  const directoryFlags = typeof fsConstants.O_DIRECTORY === "number" ? fsConstants.O_DIRECTORY : 0;
@@ -1085,49 +1118,97 @@ async function removePendingOmxPluginCacheLivePinIntent(cacheBaseRef, version, s
1085
1118
  async function isLivePinOwnerAlive(record) {
1086
1119
  return isProcessOwnerAlive(record.pid, record.processStartHash);
1087
1120
  }
1088
- async function hasLiveSessionPinRecord(snapshotRef, ownerPid, sessionHash) {
1121
+ async function hasLiveSessionPinRecord(cacheBase, version, ownerPid, sessionHash, snapshotRef) {
1122
+ // Check external pin directory
1089
1123
  let pinDirectoryRef;
1090
1124
  try {
1091
- pinDirectoryRef = await openDirectoryChild(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1092
- if (await livePinRetirementGateExists(pinDirectoryRef))
1093
- return false;
1094
- for (const entry of await readdir(pinDirectoryRef.scanOperationPath, { withFileTypes: true })) {
1095
- if (!entry.isFile() || entry.isSymbolicLink() || !entry.name.startsWith("pin-"))
1096
- continue;
1097
- const path = join(pinDirectoryRef.operationPath, entry.name);
1098
- const stats = await lstat(path);
1099
- if (!stats.isFile() ||
1100
- stats.isSymbolicLink() ||
1101
- stats.nlink !== 1 ||
1102
- stats.size <= 0 ||
1103
- stats.size > OMX_PLUGIN_LIVE_PIN_RECORD_MAX_BYTES)
1104
- continue;
1105
- const bytes = await readOmxPluginCacheFileNoFollow(path, { anchorDir: pinDirectoryRef.operationPath });
1106
- if (bytes === null)
1107
- continue;
1108
- let parsed;
1109
- try {
1110
- parsed = JSON.parse(bytes.toString("utf-8"));
1111
- }
1112
- catch {
1113
- continue;
1125
+ const pinPath = omxPluginCacheLivePinDirectory(cacheBase, version);
1126
+ const pinDirExists = await lstat(pinPath).catch(() => null);
1127
+ if (pinDirExists?.isDirectory()) {
1128
+ pinDirectoryRef = await openDirectoryRef(pinPath);
1129
+ if (!(await livePinRetirementGateExists(pinDirectoryRef))) {
1130
+ for (const entry of await readdir(pinDirectoryRef.scanOperationPath, { withFileTypes: true })) {
1131
+ if (!entry.isFile() || entry.isSymbolicLink() || !entry.name.startsWith("pin-"))
1132
+ continue;
1133
+ const path = join(pinDirectoryRef.operationPath, entry.name);
1134
+ const stats = await lstat(path);
1135
+ if (!stats.isFile() ||
1136
+ stats.isSymbolicLink() ||
1137
+ stats.nlink !== 1 ||
1138
+ stats.size <= 0 ||
1139
+ stats.size > OMX_PLUGIN_LIVE_PIN_RECORD_MAX_BYTES)
1140
+ continue;
1141
+ const bytes = await readOmxPluginCacheFileNoFollow(path, { anchorDir: pinDirectoryRef.operationPath });
1142
+ if (bytes === null)
1143
+ continue;
1144
+ let parsed;
1145
+ try {
1146
+ parsed = JSON.parse(bytes.toString("utf-8"));
1147
+ }
1148
+ catch {
1149
+ continue;
1150
+ }
1151
+ const record = parseOmxPluginCacheLivePinRecord(parsed);
1152
+ if (record?.pid === ownerPid &&
1153
+ record.sessionHash === sessionHash &&
1154
+ entry.name === `pin-${record.pid}-${sessionHash.slice(0, 24)}-${record.processStartHash ?? "unknown"}.json` &&
1155
+ (await isLivePinOwnerAlive(record)))
1156
+ return true;
1157
+ }
1114
1158
  }
1115
- const record = parseOmxPluginCacheLivePinRecord(parsed);
1116
- if (record?.pid === ownerPid &&
1117
- record.sessionHash === sessionHash &&
1118
- entry.name === `pin-${record.pid}-${sessionHash.slice(0, 24)}-${record.processStartHash ?? "unknown"}.json` &&
1119
- (await isLivePinOwnerAlive(record)))
1120
- return true;
1121
1159
  }
1122
- return false;
1123
- }
1124
- catch {
1125
- return false;
1126
1160
  }
1127
1161
  finally {
1128
1162
  if (pinDirectoryRef)
1129
1163
  await pinDirectoryRef.handle.close().catch(() => undefined);
1130
1164
  }
1165
+ // Check legacy in-snapshot pin directory (read-only)
1166
+ if (snapshotRef) {
1167
+ try {
1168
+ const legacyPinDir = await openDirectoryChild(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER).catch(() => null);
1169
+ if (legacyPinDir) {
1170
+ try {
1171
+ if (!(await livePinRetirementGateExists(legacyPinDir))) {
1172
+ for (const entry of await readdir(legacyPinDir.scanOperationPath, { withFileTypes: true })) {
1173
+ if (!entry.isFile() || entry.isSymbolicLink() || !entry.name.startsWith("pin-"))
1174
+ continue;
1175
+ const path = join(legacyPinDir.operationPath, entry.name);
1176
+ const stats = await lstat(path);
1177
+ if (!stats.isFile() ||
1178
+ stats.isSymbolicLink() ||
1179
+ stats.nlink !== 1 ||
1180
+ stats.size <= 0 ||
1181
+ stats.size > OMX_PLUGIN_LIVE_PIN_RECORD_MAX_BYTES)
1182
+ continue;
1183
+ const bytes = await readOmxPluginCacheFileNoFollow(path, { anchorDir: legacyPinDir.operationPath });
1184
+ if (bytes === null)
1185
+ continue;
1186
+ let parsed;
1187
+ try {
1188
+ parsed = JSON.parse(bytes.toString("utf-8"));
1189
+ }
1190
+ catch {
1191
+ continue;
1192
+ }
1193
+ const record = parseOmxPluginCacheLivePinRecord(parsed);
1194
+ if (record?.pid === ownerPid &&
1195
+ record.sessionHash === sessionHash &&
1196
+ entry.name === `pin-${record.pid}-${sessionHash.slice(0, 24)}-${record.processStartHash ?? "unknown"}.json` &&
1197
+ (await isLivePinOwnerAlive(record)))
1198
+ return true;
1199
+ }
1200
+ }
1201
+ }
1202
+ finally {
1203
+ await legacyPinDir.handle.close().catch(() => undefined);
1204
+ }
1205
+ }
1206
+ }
1207
+ catch {
1208
+ // Ignore errors reading legacy location
1209
+ }
1210
+ }
1211
+ return false;
1131
1212
  }
1132
1213
  async function hasLivePinIntent(cacheBaseRef, version, mutationLockContent, intentLockContent) {
1133
1214
  const versionHash = omxPluginCacheLivePinVersionHash(version);
@@ -1217,7 +1298,7 @@ async function hasLivePinIntent(cacheBaseRef, version, mutationLockContent, inte
1217
1298
  }
1218
1299
  return false;
1219
1300
  }
1220
- async function sweepExpiredOmxPluginCacheLivePinIntents(snapshotRef, cacheBaseRef, version) {
1301
+ async function sweepExpiredOmxPluginCacheLivePinIntents(cacheBaseRef, version) {
1221
1302
  let mutationLockContent = null;
1222
1303
  let intentLockContent = null;
1223
1304
  try {
@@ -1230,7 +1311,11 @@ async function sweepExpiredOmxPluginCacheLivePinIntents(snapshotRef, cacheBaseRe
1230
1311
  await hasLivePinIntent(cacheBaseRef, version, mutationLockContent, intentLockContent);
1231
1312
  let pinDirectoryRef;
1232
1313
  try {
1233
- pinDirectoryRef = await openDirectoryChild(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1314
+ const pinDirPath = omxPluginCacheLivePinDirectory(cacheBaseRef.path, version);
1315
+ const pinDirStats = await lstat(pinDirPath).catch(() => null);
1316
+ if (!pinDirStats?.isDirectory())
1317
+ return;
1318
+ pinDirectoryRef = await openDirectoryRef(pinDirPath);
1234
1319
  }
1235
1320
  catch {
1236
1321
  return;
@@ -1291,16 +1376,19 @@ async function sweepExpiredOmxPluginCacheLivePinIntents(snapshotRef, cacheBaseRe
1291
1376
  removedExpiredMarker = true;
1292
1377
  }
1293
1378
  if (removedExpiredMarker && (await readdir(pinDirectoryRef.scanOperationPath)).length === 0) {
1294
- const pinDirectoryStats = await pinDirectoryRef.handle.stat();
1295
1379
  await pinDirectoryRef.handle.close();
1296
1380
  pinDirectoryRef = undefined;
1297
1381
  if (!(await omxPluginCacheLivePinMutationLockIsOwned(cacheBaseRef, version, mutationLockContent)) ||
1298
1382
  !(await omxPluginCacheLivePinMutationLockIsOwned(cacheBaseRef, version, intentLockContent, omxPluginCacheLivePinIntentLockName(version))))
1299
1383
  return;
1300
- await removeChildIfIdentity(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER, pinDirectoryStats, {
1301
- force: true,
1302
- emptyDirectory: true,
1303
- });
1384
+ // Remove the now-empty external pin directory
1385
+ const pinDirPath = omxPluginCacheLivePinDirectory(cacheBaseRef.path, version);
1386
+ try {
1387
+ await rmdir(pinDirPath).catch(() => undefined);
1388
+ }
1389
+ catch {
1390
+ // Ignore errors removing the directory
1391
+ }
1304
1392
  }
1305
1393
  }
1306
1394
  finally {
@@ -1354,29 +1442,45 @@ async function isLivePinnedSnapshot(snapshotRef, cacheBaseRef, version, retireme
1354
1442
  return true;
1355
1443
  if (await isPrePinManagedSnapshot(snapshotRef, version))
1356
1444
  return true;
1357
- const markerPath = join(snapshotRef.operationPath, OMX_PLUGIN_LIVE_PIN_MARKER);
1358
- let markerStats;
1445
+ // Check the external pin directory
1446
+ // Use the cacheBase from the passed cacheBaseRef instead of re-deriving from codexHome()
1447
+ const pinDirPath = omxPluginCacheLivePinDirectory(cacheBaseRef.path, version);
1448
+ let pinDirStats;
1359
1449
  try {
1360
- markerStats = await lstat(markerPath);
1450
+ pinDirStats = await lstat(pinDirPath);
1361
1451
  }
1362
1452
  catch (error) {
1363
- if (isMissingPathError(error))
1364
- return false;
1453
+ if (isMissingPathError(error)) {
1454
+ // External pin dir missing; check legacy in-snapshot pins
1455
+ return await hasLegacyInSnapshotLivePin(snapshotRef);
1456
+ }
1365
1457
  return true;
1366
1458
  }
1367
- if (markerStats.isSymbolicLink())
1459
+ if (pinDirStats.isSymbolicLink())
1368
1460
  return true;
1369
- if (markerStats.isFile())
1461
+ if (pinDirStats.isFile())
1370
1462
  return true;
1371
- if (!markerStats.isDirectory())
1463
+ if (!pinDirStats.isDirectory())
1372
1464
  return true;
1373
1465
  let pinDirectoryRef;
1374
1466
  try {
1375
- pinDirectoryRef = await openDirectoryChild(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1467
+ pinDirectoryRef = await openDirectoryRef(pinDirPath);
1376
1468
  }
1377
1469
  catch {
1378
1470
  return true;
1379
1471
  }
1472
+ try {
1473
+ return (await hasLivePinDirectoryEntries(pinDirectoryRef, retirementGateContent, true)) ||
1474
+ (await hasLegacyInSnapshotLivePin(snapshotRef));
1475
+ }
1476
+ catch {
1477
+ return true;
1478
+ }
1479
+ finally {
1480
+ await pinDirectoryRef.handle.close();
1481
+ }
1482
+ }
1483
+ async function hasLivePinDirectoryEntries(pinDirectoryRef, retirementGateContent, recoverRetirementGate = false) {
1380
1484
  try {
1381
1485
  const entries = await readdir(pinDirectoryRef.scanOperationPath, { withFileTypes: true });
1382
1486
  if (entries.length === 0) {
@@ -1425,8 +1529,11 @@ async function isLivePinnedSnapshot(snapshotRef, cacheBaseRef, version, retireme
1425
1529
  continue;
1426
1530
  }
1427
1531
  if (entry.name === OMX_PLUGIN_LIVE_PIN_RETIRING_MARKER) {
1428
- if (!retirementGateContent)
1532
+ if (!retirementGateContent) {
1533
+ if (recoverRetirementGate && await removeStaleLivePinRetirementGate(pinDirectoryRef, false))
1534
+ continue;
1429
1535
  return true;
1536
+ }
1430
1537
  const stats = await lstat(pinPath);
1431
1538
  if (!stats.isFile() || stats.isSymbolicLink() || stats.nlink !== 1 || stats.size <= 0 || stats.size > 256)
1432
1539
  return true;
@@ -1463,8 +1570,29 @@ async function isLivePinnedSnapshot(snapshotRef, cacheBaseRef, version, retireme
1463
1570
  catch {
1464
1571
  return true;
1465
1572
  }
1466
- finally {
1467
- await pinDirectoryRef.handle.close();
1573
+ }
1574
+ async function hasLegacyInSnapshotLivePin(snapshotRef) {
1575
+ try {
1576
+ // First check for the simple file marker (pre-pin style)
1577
+ const legacyPinMarkerPath = join(snapshotRef.operationPath, OMX_PLUGIN_LIVE_PIN_MARKER);
1578
+ try {
1579
+ const markerStats = await lstat(legacyPinMarkerPath);
1580
+ if (!markerStats.isDirectory() || markerStats.isSymbolicLink())
1581
+ return true;
1582
+ }
1583
+ catch (error) {
1584
+ return !isMissingPathError(error);
1585
+ }
1586
+ const legacyPinDir = await openDirectoryChild(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1587
+ try {
1588
+ return await hasLivePinDirectoryEntries(legacyPinDir);
1589
+ }
1590
+ finally {
1591
+ await legacyPinDir.handle.close().catch(() => undefined);
1592
+ }
1593
+ }
1594
+ catch {
1595
+ return true;
1468
1596
  }
1469
1597
  }
1470
1598
  async function livePinRetirementGateExists(pinDirectoryRef) {
@@ -1476,34 +1604,46 @@ async function livePinRetirementGateExists(pinDirectoryRef) {
1476
1604
  return !isMissingPathError(error);
1477
1605
  }
1478
1606
  }
1479
- async function createLivePinRetirementGate(snapshotPath, snapshotStats) {
1480
- let snapshotRef;
1607
+ async function removeStaleLivePinRetirementGate(pinDirectoryRef, remove = true) {
1608
+ const gatePath = join(pinDirectoryRef.operationPath, OMX_PLUGIN_LIVE_PIN_RETIRING_MARKER);
1609
+ try {
1610
+ const stats = await lstat(gatePath);
1611
+ if (!stats.isFile() || stats.isSymbolicLink() || stats.nlink !== 1 || stats.size <= 0 || stats.size > 256)
1612
+ return false;
1613
+ const bytes = await readOmxPluginCacheFileNoFollow(gatePath, { anchorDir: pinDirectoryRef.operationPath });
1614
+ if (!bytes)
1615
+ return false;
1616
+ const parsed = JSON.parse(bytes.toString("utf-8"));
1617
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
1618
+ return false;
1619
+ const record = parsed;
1620
+ if (record.version !== 1 || typeof record.pid !== "number" || !Number.isSafeInteger(record.pid) || record.pid <= 0 ||
1621
+ typeof record.token !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(record.token) ||
1622
+ Object.keys(record).some((key) => key !== "version" && key !== "pid" && key !== "token") ||
1623
+ isProcessAlive(record.pid))
1624
+ return false;
1625
+ // Never reclaim by age: paused or PID-reused owners must remain protected.
1626
+ const current = await lstat(gatePath);
1627
+ const currentBytes = await readOmxPluginCacheFileNoFollow(gatePath, { anchorDir: pinDirectoryRef.operationPath });
1628
+ if (!sameFileIdentity(stats, current) || current.nlink !== 1 || !currentBytes?.equals(bytes) || isProcessAlive(record.pid))
1629
+ return false;
1630
+ // Scans only establish that the gate is abandoned. Reclaim it immediately
1631
+ // before replacement so an empty-directory initialization lease is not renewed.
1632
+ if (!remove)
1633
+ return true;
1634
+ return await removeChildIfIdentity(pinDirectoryRef, OMX_PLUGIN_LIVE_PIN_RETIRING_MARKER, current, { force: true });
1635
+ }
1636
+ catch (error) {
1637
+ return isMissingPathError(error);
1638
+ }
1639
+ }
1640
+ async function createLivePinRetirementGate(cacheBaseRef, version) {
1481
1641
  let pinDirectoryRef;
1482
1642
  try {
1483
- snapshotRef = await openDirectoryRef(snapshotPath);
1484
- if (!sameFileIdentity(await snapshotRef.handle.stat(), snapshotStats))
1643
+ // Create/access the external pin directory (properly anchored)
1644
+ pinDirectoryRef = await openExternalLivePinDirectory(cacheBaseRef, version);
1645
+ if (!(await removeStaleLivePinRetirementGate(pinDirectoryRef)))
1485
1646
  return null;
1486
- const markerPath = join(snapshotRef.operationPath, OMX_PLUGIN_LIVE_PIN_MARKER);
1487
- let markerStats = null;
1488
- try {
1489
- markerStats = await lstat(markerPath);
1490
- }
1491
- catch (error) {
1492
- if (!isMissingPathError(error))
1493
- return null;
1494
- }
1495
- if (markerStats?.isSymbolicLink() || (markerStats && !markerStats.isDirectory()))
1496
- return null;
1497
- if (!markerStats) {
1498
- try {
1499
- await mkdirDirectoryChildExclusive(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1500
- }
1501
- catch (error) {
1502
- if (error.code !== "EEXIST")
1503
- return null;
1504
- }
1505
- }
1506
- pinDirectoryRef = await openDirectoryChild(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1507
1647
  const gateContent = `${JSON.stringify({ version: 1, pid: process.pid, token: randomUUID() })}\n`;
1508
1648
  await createExclusiveFileChild(pinDirectoryRef, OMX_PLUGIN_LIVE_PIN_RETIRING_MARKER, gateContent);
1509
1649
  return gateContent;
@@ -1514,33 +1654,82 @@ async function createLivePinRetirementGate(snapshotPath, snapshotStats) {
1514
1654
  finally {
1515
1655
  if (pinDirectoryRef)
1516
1656
  await pinDirectoryRef.handle.close().catch(() => undefined);
1517
- if (snapshotRef)
1518
- await snapshotRef.handle.close().catch(() => undefined);
1519
1657
  }
1520
1658
  }
1521
- async function removeLivePinRetirementGate(snapshotPath, snapshotStats, gateContent) {
1522
- let snapshotRef;
1659
+ async function removeLivePinRetirementGate(cacheBaseRef, version, gateContent) {
1523
1660
  let pinDirectoryRef;
1524
1661
  try {
1525
- snapshotRef = await openDirectoryRef(snapshotPath);
1526
- if (!sameFileIdentity(await snapshotRef.handle.stat(), snapshotStats))
1662
+ // Access the external pin directory (properly anchored)
1663
+ try {
1664
+ pinDirectoryRef = await openExternalLivePinDirectory(cacheBaseRef, version);
1665
+ }
1666
+ catch {
1527
1667
  return false;
1528
- pinDirectoryRef = await openDirectoryChild(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1668
+ }
1529
1669
  const gatePath = join(pinDirectoryRef.operationPath, OMX_PLUGIN_LIVE_PIN_RETIRING_MARKER);
1670
+ let stats;
1671
+ try {
1672
+ stats = await lstat(gatePath);
1673
+ }
1674
+ catch (error) {
1675
+ if (isMissingPathError(error))
1676
+ return false;
1677
+ throw error;
1678
+ }
1530
1679
  const bytes = await readOmxPluginCacheFileNoFollow(gatePath, { anchorDir: pinDirectoryRef.operationPath });
1531
1680
  if (bytes?.toString("utf-8") !== gateContent)
1532
1681
  return false;
1533
- await removeChild(pinDirectoryRef, OMX_PLUGIN_LIVE_PIN_RETIRING_MARKER, { force: true });
1534
- return true;
1682
+ const gateRemoved = await removeChildIfIdentity(pinDirectoryRef, OMX_PLUGIN_LIVE_PIN_RETIRING_MARKER, stats, { force: true });
1683
+ // After removing the gate, try to remove the empty version directory
1684
+ // to prevent it from blocking subsequent repairs within the lease window.
1685
+ // Remove through the anchored parent (.omx-pins) by name to avoid TOCTOU issues.
1686
+ if (gateRemoved) {
1687
+ const entries = await readdir(pinDirectoryRef.operationPath, { withFileTypes: true });
1688
+ if (entries.length === 0) {
1689
+ // Directory is empty; get anchored parent and remove by name
1690
+ let pinParentRef;
1691
+ try {
1692
+ pinParentRef = await openDirectoryChild(cacheBaseRef, ".omx-pins");
1693
+ // Remove the version directory through its parent by name (not through fd path)
1694
+ await removeChild(pinParentRef, version, {
1695
+ emptyDirectory: true,
1696
+ force: false,
1697
+ });
1698
+ }
1699
+ catch (error) {
1700
+ const code = error.code;
1701
+ // Only ENOTEMPTY and ENOENT are expected (race with new pins, or directory not present).
1702
+ // All other errors (permission, I/O, etc.) should be reported and re-thrown.
1703
+ if (code !== "ENOTEMPTY" && code !== "ENOENT") {
1704
+ console.error(`[omx plugin cache] Unexpected error removing empty version directory ${version} during gate cleanup: ${code} - ${error.message}`);
1705
+ throw error;
1706
+ }
1707
+ // Log expected errors for visibility in troubleshooting
1708
+ if (code === "ENOTEMPTY") {
1709
+ console.warn(`[omx plugin cache] Empty version directory ${version} was recreated or contains new pins during cleanup`);
1710
+ }
1711
+ }
1712
+ finally {
1713
+ if (pinParentRef)
1714
+ await pinParentRef.handle.close().catch(() => undefined);
1715
+ }
1716
+ }
1717
+ }
1718
+ return gateRemoved;
1535
1719
  }
1536
- catch {
1720
+ catch (error) {
1721
+ // Errors here (beyond expected missing path) indicate unexpected I/O, permission, or logical issues.
1722
+ // Convert to false but report for visibility; if this is called in the context of snapshot removal,
1723
+ // the caller (removeChildIfIdentity) will handle the failure appropriately.
1724
+ if (!error.code || !isMissingPathError(error)) {
1725
+ const code = error.code;
1726
+ console.error(`[omx plugin cache] Unexpected error in removeLivePinRetirementGate for version ${version}: ${code} - ${error.message}`);
1727
+ }
1537
1728
  return false;
1538
1729
  }
1539
1730
  finally {
1540
1731
  if (pinDirectoryRef)
1541
1732
  await pinDirectoryRef.handle.close().catch(() => undefined);
1542
- if (snapshotRef)
1543
- await snapshotRef.handle.close().catch(() => undefined);
1544
1733
  }
1545
1734
  }
1546
1735
  export async function registerOmxPluginCacheLivePin(pluginRoot, sessionId, ownerPid) {
@@ -1599,7 +1788,7 @@ export async function registerOmxPluginCacheLivePin(pluginRoot, sessionId, owner
1599
1788
  (await hasPublicationMarkerEntry(snapshotRef.operationPath, ".omx-incomplete")) ||
1600
1789
  !(await hasRegularPublicationMarker(snapshotRef.operationPath, OMX_PLUGIN_MANAGED_MARKER)))
1601
1790
  return false;
1602
- return await hasLiveSessionPinRecord(snapshotRef, ownerPid, sessionHash);
1791
+ return await hasLiveSessionPinRecord(cacheBase, version, ownerPid, sessionHash, snapshotRef);
1603
1792
  }
1604
1793
  const [canonicalRoot, canonicalExpected] = await Promise.all([
1605
1794
  canonicalRealpath(rootPath),
@@ -1620,31 +1809,10 @@ export async function registerOmxPluginCacheLivePin(pluginRoot, sessionId, owner
1620
1809
  (await hasPublicationMarkerEntry(snapshotRef.operationPath, ".omx-incomplete")) ||
1621
1810
  !(await hasRegularPublicationMarker(snapshotRef.operationPath, OMX_PLUGIN_MANAGED_MARKER)))
1622
1811
  return false;
1623
- const markerPath = join(snapshotRef.operationPath, OMX_PLUGIN_LIVE_PIN_MARKER);
1624
- let markerStats = null;
1625
- try {
1626
- markerStats = await lstat(markerPath);
1627
- }
1628
- catch (error) {
1629
- if (!isMissingPathError(error))
1630
- throw error;
1631
- }
1632
- if (markerStats?.isFile() && !markerStats.isSymbolicLink()) {
1633
- await removePendingOmxPluginCacheLivePinIntent(cacheBaseRef, version, sessionHash);
1634
- return true;
1635
- }
1636
- if (markerStats && (markerStats.isSymbolicLink() || !markerStats.isDirectory()))
1812
+ // Access the external pin directory (properly anchored)
1813
+ pinDirectoryRef = await openExternalLivePinDirectory(cacheBaseRef, version);
1814
+ if (!(await removeStaleLivePinRetirementGate(pinDirectoryRef)))
1637
1815
  return false;
1638
- if (!markerStats) {
1639
- try {
1640
- await mkdirDirectoryChildExclusive(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1641
- }
1642
- catch (error) {
1643
- if (error.code !== "EEXIST")
1644
- throw error;
1645
- }
1646
- }
1647
- pinDirectoryRef = await openDirectoryChild(snapshotRef, OMX_PLUGIN_LIVE_PIN_MARKER);
1648
1816
  await writePendingOmxPluginCacheLivePinRegistration(pinDirectoryRef, sessionHash);
1649
1817
  const processStartIdentity = await readProcessStartIdentity(ownerPid);
1650
1818
  const processStartHash = processStartIdentity ? hashProcessStartIdentity(processStartIdentity) : null;
@@ -2014,6 +2182,13 @@ export async function expectedPackagedOmxSkillNames(packagedMarketplace, options
2014
2182
  export function omxPluginCacheBase(codexHomeDir) {
2015
2183
  return join(codexHomeDir, "plugins", "cache", OMX_LOCAL_MARKETPLACE_NAME, OMX_PLUGIN_NAME);
2016
2184
  }
2185
+ /**
2186
+ * Returns the external live-pin directory path for a given version.
2187
+ * This is outside the plugin version directory to avoid changing the Codex content hash.
2188
+ */
2189
+ function omxPluginCacheLivePinDirectory(cacheBase, version) {
2190
+ return join(cacheBase, ".omx-pins", version);
2191
+ }
2017
2192
  function isMissingPathError(error) {
2018
2193
  return error.code === "ENOENT";
2019
2194
  }
@@ -2286,7 +2461,7 @@ export async function removeChildIfIdentity(cacheBaseRef, childName, childStats,
2286
2461
  if (!currentQuarantineStats || !sameFileIdentity(currentQuarantineStats, quarantineStats))
2287
2462
  return false;
2288
2463
  if (retirementGateContent &&
2289
- !(await removeLivePinRetirementGate(quarantinePath, quarantineStats, retirementGateContent)))
2464
+ !(await removeLivePinRetirementGate(cacheBaseRef, options.expectedManagedVersion, retirementGateContent)))
2290
2465
  return false;
2291
2466
  try {
2292
2467
  await renameChild(cacheBaseRef, quarantineName, cacheBaseRef, childName);
@@ -2347,7 +2522,7 @@ export async function removeChildIfIdentity(cacheBaseRef, childName, childStats,
2347
2522
  options.preservedPaths?.push(join(cacheBaseRef.path, restored ? childName : quarantineName));
2348
2523
  return false;
2349
2524
  }
2350
- const retirementGateContent = await createLivePinRetirementGate(quarantinePath, quarantineStats);
2525
+ const retirementGateContent = await createLivePinRetirementGate(cacheBaseRef, options.expectedManagedVersion);
2351
2526
  if (!retirementGateContent) {
2352
2527
  options.preservedPaths?.push(join(cacheBaseRef.path, quarantineName));
2353
2528
  return false;
@@ -2356,39 +2531,50 @@ export async function removeChildIfIdentity(cacheBaseRef, childName, childStats,
2356
2531
  if (!quarantineState.owned || quarantineState.livePin) {
2357
2532
  const restored = quarantineState.livePin && (await restoreQuarantinedSnapshot(retirementGateContent));
2358
2533
  if (!quarantineState.livePin) {
2359
- await removeLivePinRetirementGate(quarantinePath, quarantineStats, retirementGateContent);
2534
+ await removeLivePinRetirementGate(cacheBaseRef, options.expectedManagedVersion, retirementGateContent);
2360
2535
  }
2361
2536
  options.preservedPaths?.push(join(cacheBaseRef.path, restored ? childName : quarantineName));
2362
2537
  return false;
2363
2538
  }
2364
2539
  let preservationNeeded = false;
2365
2540
  let livePinAtDestructiveCheck = false;
2366
- const removed = await removeChild(cacheBaseRef, quarantineName, {
2367
- ...options,
2368
- preserveRecursive: false,
2369
- beforeDestructiveRemove: async () => {
2370
- if (!(await omxPluginCacheLivePinMutationLockIsOwned(cacheBaseRef, options.expectedManagedVersion, mutationLockContent)) ||
2371
- !(await omxPluginCacheLivePinMutationLockIsOwned(cacheBaseRef, options.expectedManagedVersion, intentLockContent, omxPluginCacheLivePinIntentLockName(options.expectedManagedVersion)))) {
2372
- preservationNeeded = true;
2373
- livePinAtDestructiveCheck = true;
2374
- return false;
2375
- }
2376
- const state = await verifyQuarantineOwnership(retirementGateContent);
2377
- if (!state.owned || state.livePin) {
2378
- preservationNeeded = true;
2379
- livePinAtDestructiveCheck ||= state.livePin;
2541
+ try {
2542
+ const removed = await removeChild(cacheBaseRef, quarantineName, {
2543
+ ...options,
2544
+ preserveRecursive: false,
2545
+ beforeDestructiveRemove: async () => {
2546
+ if (!(await omxPluginCacheLivePinMutationLockIsOwned(cacheBaseRef, options.expectedManagedVersion, mutationLockContent)) ||
2547
+ !(await omxPluginCacheLivePinMutationLockIsOwned(cacheBaseRef, options.expectedManagedVersion, intentLockContent, omxPluginCacheLivePinIntentLockName(options.expectedManagedVersion)))) {
2548
+ preservationNeeded = true;
2549
+ livePinAtDestructiveCheck = true;
2550
+ return false;
2551
+ }
2552
+ const state = await verifyQuarantineOwnership(retirementGateContent);
2553
+ if (!state.owned || state.livePin) {
2554
+ preservationNeeded = true;
2555
+ livePinAtDestructiveCheck ||= state.livePin;
2556
+ }
2557
+ return state.owned && !state.livePin;
2558
+ },
2559
+ });
2560
+ if (removed) {
2561
+ // Successfully removed the snapshot; remove the retirement gate
2562
+ await removeLivePinRetirementGate(cacheBaseRef, options.expectedManagedVersion, retirementGateContent);
2563
+ }
2564
+ else if (preservationNeeded) {
2565
+ const restored = livePinAtDestructiveCheck && (await restoreQuarantinedSnapshot(retirementGateContent));
2566
+ if (!livePinAtDestructiveCheck) {
2567
+ await removeLivePinRetirementGate(cacheBaseRef, options.expectedManagedVersion, retirementGateContent);
2380
2568
  }
2381
- return state.owned && !state.livePin;
2382
- },
2383
- });
2384
- if (!removed && preservationNeeded) {
2385
- const restored = livePinAtDestructiveCheck && (await restoreQuarantinedSnapshot(retirementGateContent));
2386
- if (!livePinAtDestructiveCheck) {
2387
- await removeLivePinRetirementGate(quarantinePath, quarantineStats, retirementGateContent);
2569
+ options.preservedPaths?.push(join(cacheBaseRef.path, restored ? childName : quarantineName));
2388
2570
  }
2389
- options.preservedPaths?.push(join(cacheBaseRef.path, restored ? childName : quarantineName));
2571
+ return removed;
2572
+ }
2573
+ catch (error) {
2574
+ // Clean up gate if removal operation fails
2575
+ await removeLivePinRetirementGate(cacheBaseRef, options.expectedManagedVersion, retirementGateContent).catch(() => { });
2576
+ throw error;
2390
2577
  }
2391
- return removed;
2392
2578
  }
2393
2579
  }
2394
2580
  finally {
@@ -3418,7 +3604,7 @@ export async function retireUnpinnedManagedSnapshots(codexHomeDir, currentVersio
3418
3604
  continue;
3419
3605
  if (!(await hasRegularPublicationMarker(candidateRef.operationPath, OMX_PLUGIN_MANAGED_MARKER)))
3420
3606
  continue;
3421
- await sweepExpiredOmxPluginCacheLivePinIntents(candidateRef, baseRef, entry.name);
3607
+ await sweepExpiredOmxPluginCacheLivePinIntents(baseRef, entry.name);
3422
3608
  if (entry.name === currentVersion)
3423
3609
  continue;
3424
3610
  if (await isLivePinnedSnapshot(candidateRef, baseRef, entry.name))