@cortexkit/opencode-antigravity-auth 3.1.5 → 3.1.8

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 (66) hide show
  1. package/dist/cli.js +73 -118
  2. package/dist/cli.js.map +3 -3
  3. package/dist/cli.meta.json +22 -32
  4. package/dist/index.js +1850 -1854
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.meta.json +179 -150
  7. package/dist/server.js +1962 -1965
  8. package/dist/server.js.map +4 -4
  9. package/dist/server.meta.json +1 -1
  10. package/dist/src/common-auth-embedded/claustrum/availability.js +1 -1
  11. package/dist/src/common-auth-embedded/claustrum/custody.js +1 -1
  12. package/dist/src/common-auth-embedded/fs/private-files.js +6 -2
  13. package/dist/src/common-auth-embedded/pi-slot/index.d.ts +1 -1
  14. package/dist/src/common-auth-embedded/pi-slot/index.js +1 -1
  15. package/dist/src/common-auth-embedded/pi-slot/slot.d.ts +6 -9
  16. package/dist/src/common-auth-embedded/pi-slot/slot.js +42 -105
  17. package/dist/src/common-auth-embedded/rpc/index.d.ts +1 -1
  18. package/dist/src/common-auth-embedded/rpc/index.js +1 -1
  19. package/dist/src/common-auth-embedded/rpc/port-file.d.ts +4 -0
  20. package/dist/src/common-auth-embedded/rpc/port-file.js +55 -49
  21. package/dist/src/common-auth-embedded/rpc/rpc-server.js +3 -2
  22. package/dist/src/common-auth-embedded/rpc/startup-sweep.js +32 -0
  23. package/dist/src/common-auth-embedded/source-output.json +53 -44
  24. package/dist/src/ga/server/index.d.ts +1 -1
  25. package/dist/src/ga/server/index.d.ts.map +1 -1
  26. package/dist/src/ga/server/index.js +6 -0
  27. package/dist/src/ga/server/index.js.map +1 -1
  28. package/dist/src/plugin/__fixtures__/verified-store.d.ts +1 -1
  29. package/dist/src/plugin/__fixtures__/verified-store.js +3 -3
  30. package/dist/src/plugin/request-helpers.js +3 -3
  31. package/dist/src/plugin/request-helpers.js.map +1 -1
  32. package/dist/src/plugin/shared/request-services.d.ts.map +1 -1
  33. package/dist/src/plugin/shared/request-services.js +5 -2
  34. package/dist/src/plugin/shared/request-services.js.map +1 -1
  35. package/dist/src/runtime-layout.d.ts.map +1 -1
  36. package/dist/src/runtime-layout.js +5 -2
  37. package/dist/src/runtime-layout.js.map +1 -1
  38. package/dist/src/setup.d.ts.map +1 -1
  39. package/dist/src/setup.js +2 -7
  40. package/dist/src/setup.js.map +1 -1
  41. package/dist/tui-build-map.json +9 -9
  42. package/dist/tui-ga-build-map.json +1 -1
  43. package/dist/types/src/ga/server/index.d.ts +1 -1
  44. package/dist/types/src/ga/server/index.d.ts.map +1 -1
  45. package/dist/types/src/plugin/shared/request-services.d.ts.map +1 -1
  46. package/dist/types/src/runtime-layout.d.ts.map +1 -1
  47. package/dist/types/src/setup.d.ts.map +1 -1
  48. package/package.json +2 -2
  49. package/src/common-auth-embedded/claustrum/availability.js +1 -1
  50. package/src/common-auth-embedded/claustrum/custody.js +1 -1
  51. package/src/common-auth-embedded/fs/private-files.js +6 -2
  52. package/src/common-auth-embedded/pi-slot/index.d.ts +1 -1
  53. package/src/common-auth-embedded/pi-slot/index.js +1 -1
  54. package/src/common-auth-embedded/pi-slot/slot.d.ts +6 -9
  55. package/src/common-auth-embedded/pi-slot/slot.js +42 -105
  56. package/src/common-auth-embedded/rpc/index.d.ts +1 -1
  57. package/src/common-auth-embedded/rpc/index.js +1 -1
  58. package/src/common-auth-embedded/rpc/port-file.d.ts +4 -0
  59. package/src/common-auth-embedded/rpc/port-file.js +55 -49
  60. package/src/common-auth-embedded/rpc/rpc-server.js +3 -2
  61. package/src/common-auth-embedded/rpc/startup-sweep.js +32 -0
  62. package/src/common-auth-embedded/source-output.json +53 -44
  63. package/src/tui-compiled/common-auth-embedded/fs/private-files.js +6 -2
  64. package/src/tui-compiled/common-auth-embedded/rpc/port-file.js +55 -49
  65. package/src/tui-raw/common-auth-embedded/fs/private-files.js +6 -2
  66. package/src/tui-raw/common-auth-embedded/rpc/port-file.js +55 -49
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "schema": 1,
3
3
  "package": "@cortexkit/common-auth",
4
- "version": "0.52.0",
4
+ "version": "0.53.0",
5
5
  "artifactStatus": "published",
6
+ "archiveBytes": 500371,
6
7
  "publicRoots": {
7
8
  "./rpc": {
8
9
  "types": "./dist/rpc/index.d.ts",
@@ -73,16 +74,16 @@
73
74
  "import": "./src/quota-sidebar/index.tsx"
74
75
  }
75
76
  },
76
- "input": "tools/common-auth-build/inputs/cortexkit-common-auth-0.52.0.tgz",
77
- "tarballSha256": "c29dfd830c1515a2d2785d575726b20be4efdb77991c37eaf4de940ee0486df5",
78
- "sri": "sha512-rl6Y0fr5ljXxukT/XcG0uh4qjt3+Xp6Cgt+BM8jRuIGxtf71sH5GSUcyyGk9Ptr7le27Z04f+RXjv5MhBtASfg==",
79
- "shasum": "17cee2f576a4bd9005f2732adb3b48659137c1c5",
80
- "canonicalFiles": 256,
81
- "canonicalBytes": 1514810,
77
+ "input": "tools/common-auth-build/inputs/cortexkit-common-auth-0.53.0.tgz",
78
+ "tarballSha256": "f9ffedd0b1fdfbb689c6eb1cae27561228b6e80a52d26ec5e6fa4dd1fe47e7ab",
79
+ "sri": "sha512-WwIae25lpa6W07Rw0JTS45eZohHj9ZSa9Yyc62Z9098lP+ztzEt4zJjDsz5KgbX8Ew/SEfRT4s4buXAnoUrA5Q==",
80
+ "shasum": "0198ecd60e908146221ea5ab1436ea47077fc212",
81
+ "canonicalFiles": 257,
82
+ "canonicalBytes": 1514096,
82
83
  "generator": {
83
84
  "path": "packages/opencode/scripts/embed-common-auth.ts",
84
85
  "version": 4,
85
- "sha256": "c9d52ac06eed3eaf0eea74a60694d0a87d38b949a68fb986d726c0d88138f765"
86
+ "sha256": "f54aa9e14e1cac3ba0f2158002f58f50ef592638e507fbc4d6302ad4322757da"
86
87
  },
87
88
  "files": [
88
89
  {
@@ -256,9 +257,9 @@
256
257
  {
257
258
  "source": "package/dist/claustrum/availability.js",
258
259
  "output": "claustrum/availability.js",
259
- "bytes": 4615,
260
- "sourceSha256": "11eaa35ab38ab433f249b4b11b9e284fac5ab7e785c965fe070aef8cde593089",
261
- "outputSha256": "11eaa35ab38ab433f249b4b11b9e284fac5ab7e785c965fe070aef8cde593089",
260
+ "bytes": 4613,
261
+ "sourceSha256": "848f2c5224a37b903b186cd5dd43710ad0d5cfe590600263b613b0ea22ff1585",
262
+ "outputSha256": "848f2c5224a37b903b186cd5dd43710ad0d5cfe590600263b613b0ea22ff1585",
262
263
  "transform": "verbatim"
263
264
  },
264
265
  {
@@ -288,9 +289,9 @@
288
289
  {
289
290
  "source": "package/dist/claustrum/custody.js",
290
291
  "output": "claustrum/custody.js",
291
- "bytes": 43691,
292
- "sourceSha256": "65a376031c602bc4592fa6402c3cb5a14219245c10c134aed7239fdf11f1e88c",
293
- "outputSha256": "65a376031c602bc4592fa6402c3cb5a14219245c10c134aed7239fdf11f1e88c",
292
+ "bytes": 43686,
293
+ "sourceSha256": "174adfdaa909149453a34666fdf656aff90c7357e0000338c5a0a2a29d7c1e90",
294
+ "outputSha256": "174adfdaa909149453a34666fdf656aff90c7357e0000338c5a0a2a29d7c1e90",
294
295
  "transform": "verbatim"
295
296
  },
296
297
  {
@@ -680,9 +681,9 @@
680
681
  {
681
682
  "source": "package/dist/fs/private-files.js",
682
683
  "output": "fs/private-files.js",
683
- "bytes": 15098,
684
- "sourceSha256": "d26406b3fd7c4452905c4afe2da1e24c8079a20b89a449d2af45e95c4b5cc189",
685
- "outputSha256": "d26406b3fd7c4452905c4afe2da1e24c8079a20b89a449d2af45e95c4b5cc189",
684
+ "bytes": 15235,
685
+ "sourceSha256": "4fd7ca5b40dbdcc27044be3d73eaf6910dd75572cf199aea049fd658bcadf64c",
686
+ "outputSha256": "4fd7ca5b40dbdcc27044be3d73eaf6910dd75572cf199aea049fd658bcadf64c",
686
687
  "transform": "verbatim"
687
688
  },
688
689
  {
@@ -968,17 +969,17 @@
968
969
  {
969
970
  "source": "package/dist/pi-slot/index.d.ts",
970
971
  "output": "pi-slot/index.d.ts",
971
- "bytes": 230,
972
- "sourceSha256": "8b2c9f002a5f8ee7f5e1da5ed2f1e13e0acfbeb0c8d9f0296d5215c3150e8798",
973
- "outputSha256": "8b2c9f002a5f8ee7f5e1da5ed2f1e13e0acfbeb0c8d9f0296d5215c3150e8798",
972
+ "bytes": 253,
973
+ "sourceSha256": "d095c841ccfdf0d40665409a03c459e1143ba39d7dd9f9ad4ed424592ced3e26",
974
+ "outputSha256": "d095c841ccfdf0d40665409a03c459e1143ba39d7dd9f9ad4ed424592ced3e26",
974
975
  "transform": "verbatim"
975
976
  },
976
977
  {
977
978
  "source": "package/dist/pi-slot/index.js",
978
979
  "output": "pi-slot/index.js",
979
- "bytes": 138,
980
- "sourceSha256": "4364d23400afcc9650b055673cccceacd2c1239bac4abe6cda34cf1e34a88472",
981
- "outputSha256": "4364d23400afcc9650b055673cccceacd2c1239bac4abe6cda34cf1e34a88472",
980
+ "bytes": 161,
981
+ "sourceSha256": "d12cf2c6d6ee31c089955de1ef7d6039afbe08e7792e1702fdf83cd10d8a084d",
982
+ "outputSha256": "d12cf2c6d6ee31c089955de1ef7d6039afbe08e7792e1702fdf83cd10d8a084d",
982
983
  "transform": "verbatim"
983
984
  },
984
985
  {
@@ -992,17 +993,17 @@
992
993
  {
993
994
  "source": "package/dist/pi-slot/slot.d.ts",
994
995
  "output": "pi-slot/slot.d.ts",
995
- "bytes": 2394,
996
- "sourceSha256": "b92f1590f1f0e4dbbc6331923b9d2cf95e3200936063d5ed192a186f3271eb17",
997
- "outputSha256": "b92f1590f1f0e4dbbc6331923b9d2cf95e3200936063d5ed192a186f3271eb17",
996
+ "bytes": 2135,
997
+ "sourceSha256": "bf45905365a9f9eb6be3e54e845420a3b22de479c51fe1c5c8d5009468a2f9b3",
998
+ "outputSha256": "bf45905365a9f9eb6be3e54e845420a3b22de479c51fe1c5c8d5009468a2f9b3",
998
999
  "transform": "verbatim"
999
1000
  },
1000
1001
  {
1001
1002
  "source": "package/dist/pi-slot/slot.js",
1002
1003
  "output": "pi-slot/slot.js",
1003
- "bytes": 15120,
1004
- "sourceSha256": "13a7d5941b79c6d444cf9dc6f23263cf9237146c7dd5de7d162dbc21e015532c",
1005
- "outputSha256": "13a7d5941b79c6d444cf9dc6f23263cf9237146c7dd5de7d162dbc21e015532c",
1004
+ "bytes": 12323,
1005
+ "sourceSha256": "f7bf6aada7b772b3e7214a7fbe3d3bb993307905041b31ad712e608e4c3f3636",
1006
+ "outputSha256": "f7bf6aada7b772b3e7214a7fbe3d3bb993307905041b31ad712e608e4c3f3636",
1006
1007
  "transform": "verbatim"
1007
1008
  },
1008
1009
  {
@@ -1224,17 +1225,17 @@
1224
1225
  {
1225
1226
  "source": "package/dist/rpc/index.d.ts",
1226
1227
  "output": "rpc/index.d.ts",
1227
- "bytes": 417,
1228
- "sourceSha256": "62bda8e27c98b193ba0aee81a9e405472688618952907d91c53b9635b1142eef",
1229
- "outputSha256": "62bda8e27c98b193ba0aee81a9e405472688618952907d91c53b9635b1142eef",
1228
+ "bytes": 603,
1229
+ "sourceSha256": "30cd1c8dfe8f0fee79139e72c376a3e1da0d98284ce7549af5fc1e7a97b7f1df",
1230
+ "outputSha256": "30cd1c8dfe8f0fee79139e72c376a3e1da0d98284ce7549af5fc1e7a97b7f1df",
1230
1231
  "transform": "verbatim"
1231
1232
  },
1232
1233
  {
1233
1234
  "source": "package/dist/rpc/index.js",
1234
1235
  "output": "rpc/index.js",
1235
- "bytes": 172,
1236
- "sourceSha256": "31aadda5190c9f8c5dd4b7c0f4bb11ce87fb79715ac4785163008fb5f0062998",
1237
- "outputSha256": "31aadda5190c9f8c5dd4b7c0f4bb11ce87fb79715ac4785163008fb5f0062998",
1236
+ "bytes": 308,
1237
+ "sourceSha256": "26d0da9478fb8e7f10939321515388cce283070ea9b1df3132c74350c7a2b435",
1238
+ "outputSha256": "26d0da9478fb8e7f10939321515388cce283070ea9b1df3132c74350c7a2b435",
1238
1239
  "transform": "verbatim"
1239
1240
  },
1240
1241
  {
@@ -1256,17 +1257,17 @@
1256
1257
  {
1257
1258
  "source": "package/dist/rpc/port-file.d.ts",
1258
1259
  "output": "rpc/port-file.d.ts",
1259
- "bytes": 2015,
1260
- "sourceSha256": "ce68cc6b458df9296e4227aef1cddce4ca5912c629ef8fa3cc53c35b90362b75",
1261
- "outputSha256": "ce68cc6b458df9296e4227aef1cddce4ca5912c629ef8fa3cc53c35b90362b75",
1260
+ "bytes": 2293,
1261
+ "sourceSha256": "e4b3574f71fa5a497b1d806913d19811e0baa0cc107165b4b99533453396484d",
1262
+ "outputSha256": "e4b3574f71fa5a497b1d806913d19811e0baa0cc107165b4b99533453396484d",
1262
1263
  "transform": "verbatim"
1263
1264
  },
1264
1265
  {
1265
1266
  "source": "package/dist/rpc/port-file.js",
1266
1267
  "output": "rpc/port-file.js",
1267
- "bytes": 10599,
1268
- "sourceSha256": "d8061667a56a35d331ce5698fd78685fee2833d985f46505d286648aef7251aa",
1269
- "outputSha256": "d8061667a56a35d331ce5698fd78685fee2833d985f46505d286648aef7251aa",
1268
+ "bytes": 10758,
1269
+ "sourceSha256": "2c497513d8a3f185f17e55633b50883f633421cbe12305e0c46f86b6b820d9f2",
1270
+ "outputSha256": "2c497513d8a3f185f17e55633b50883f633421cbe12305e0c46f86b6b820d9f2",
1270
1271
  "transform": "verbatim"
1271
1272
  },
1272
1273
  {
@@ -1296,9 +1297,9 @@
1296
1297
  {
1297
1298
  "source": "package/dist/rpc/rpc-server.js",
1298
1299
  "output": "rpc/rpc-server.js",
1299
- "bytes": 14231,
1300
- "sourceSha256": "62b92588917afeaa5ddb187c0e47fe5e318c313bc3e6065806052c3bef008f04",
1301
- "outputSha256": "62b92588917afeaa5ddb187c0e47fe5e318c313bc3e6065806052c3bef008f04",
1300
+ "bytes": 14282,
1301
+ "sourceSha256": "658fc7e48f327037bbb5e4e0b750417426865b06f08914cd2d775fe7f34bd8b7",
1302
+ "outputSha256": "658fc7e48f327037bbb5e4e0b750417426865b06f08914cd2d775fe7f34bd8b7",
1302
1303
  "transform": "verbatim"
1303
1304
  },
1304
1305
  {
@@ -1317,6 +1318,14 @@
1317
1318
  "outputSha256": "7a7bb67f11f072e2fd655694445458295c5461579bc4e73b11559c9d18d3c93d",
1318
1319
  "transform": "verbatim"
1319
1320
  },
1321
+ {
1322
+ "source": "package/dist/rpc/startup-sweep.js",
1323
+ "output": "rpc/startup-sweep.js",
1324
+ "bytes": 1356,
1325
+ "sourceSha256": "674fe3a16b48b3c184273d7bc55531b83e703b6aa32f2413aec98a50f0dd1e30",
1326
+ "outputSha256": "674fe3a16b48b3c184273d7bc55531b83e703b6aa32f2413aec98a50f0dd1e30",
1327
+ "transform": "verbatim"
1328
+ },
1320
1329
  {
1321
1330
  "source": "package/dist/settings/define.d.ts",
1322
1331
  "output": "settings/define.d.ts",
@@ -24,7 +24,9 @@ export function setPrivateFilesBackendForTest(api) {
24
24
  /** The fixed `reason` of a refusal because the Windows native addon did not load. */
25
25
  export const PRIVATE_FILES_UNAVAILABLE_REASON = 'native-addon-unavailable';
26
26
  function privateFilesUnavailable(cause) {
27
- return Object.assign(new Error('Windows private-files native addon is unavailable', { cause }), {
27
+ return Object.assign(new Error('Windows private-files native addon could not be loaded', {
28
+ cause,
29
+ }), {
28
30
  code: 'ERR_PRIVATE_FILES_UNAVAILABLE',
29
31
  reason: PRIVATE_FILES_UNAVAILABLE_REASON,
30
32
  });
@@ -83,7 +85,9 @@ export class PrivateFileError extends Error {
83
85
  constructor(path, stage, cause) {
84
86
  const detail = cause;
85
87
  super(detail?.code === 'ERR_PRIVATE_FILES_UNAVAILABLE'
86
- ? `Private file refused at ${stage}: ${path} (${PRIVATE_FILES_UNAVAILABLE_REASON}). The Windows native addon of @cortexkit/private-files could not be loaded, so owner-only access cannot be enforced and nothing was read or written. Reinstall the plugin so the private-files Windows prebuild for this architecture is present; Windows has no safe fallback to permission bits.`
88
+ ? // The reason stays on `.reason`: its word "unavailable" would make
89
+ // OpenCode 1 retry a plain error carrying this message.
90
+ `Private file refused at ${stage}: ${path}. The Windows native addon of @cortexkit/private-files could not be loaded, so owner-only access cannot be enforced and nothing was read or written. Reinstall the plugin so the private-files Windows prebuild for this architecture is present; Windows has no safe fallback to permission bits.`
87
91
  : `Private file refused at ${stage}: ${path} (${detail?.reason ?? detail?.code ?? 'io-error'}). Keep this file in a folder that only you, the system or administrators can change, such as a folder in your home directory. If other users could read the file before, sign in again to replace the credentials it held. On Windows the file must also be on a local drive that keeps Windows file permissions, such as NTFS.`, { cause });
88
92
  this.path = path;
89
93
  this.stage = stage;
@@ -94,9 +94,8 @@ async function removeCorruptPortFile(portFile, log) {
94
94
  export async function writePortFile(dir, entry, options = {}) {
95
95
  // The directory can be removed by another project's sweep between our
96
96
  // mkdir and the first open/rename, so the whole create-then-rename
97
- // unit is retried once on ENOENT. The retry recreates the directory; a
98
- // persistent ENOENT (e.g. permission, read-only parent) will surface on
99
- // the second attempt — failing fast beats an unbounded loop.
97
+ // unit gets up to five attempts on ENOENT. Each retry recreates the
98
+ // directory after a short delay, but persistent removal still surfaces.
100
99
  const writeOnce = async () => {
101
100
  const target = join(dir, `port-${entry.pid}.json`);
102
101
  if (windowsPrivateFiles) {
@@ -136,14 +135,15 @@ export async function writePortFile(dir, entry, options = {}) {
136
135
  }
137
136
  return target;
138
137
  };
139
- try {
140
- return await writeOnce();
141
- }
142
- catch (error) {
143
- if (error.code === 'ENOENT') {
138
+ for (let attempt = 1;; attempt += 1) {
139
+ try {
144
140
  return await writeOnce();
145
141
  }
146
- throw error;
142
+ catch (error) {
143
+ if (error.code !== 'ENOENT' || attempt === 5)
144
+ throw error;
145
+ await new Promise((resolve) => setTimeout(resolve, 25));
146
+ }
147
147
  }
148
148
  }
149
149
  export async function sweepRpcState(root, activeDir, isManagedDir, log, options = {}) {
@@ -163,52 +163,58 @@ export async function sweepRpcState(root, activeDir, isManagedDir, log, options
163
163
  continue;
164
164
  }
165
165
  const dir = join(root, projectDir.name);
166
- let names;
166
+ if (!(await sweepRpcDirectory(dir, log, options, cutoff)))
167
+ continue;
168
+ if (resolve(dir) !== active)
169
+ await rmdir(dir).catch(() => { });
170
+ }
171
+ }
172
+ /** Internal folder cleanup shared by full and throttled startup sweeps. */
173
+ export async function sweepRpcDirectory(dir, log, options = {}, cutoff = staleBefore(options)) {
174
+ let names;
175
+ try {
176
+ names = await readdir(dir);
177
+ }
178
+ catch (error) {
179
+ if (error.code === 'ENOENT')
180
+ return false;
181
+ throw error;
182
+ }
183
+ for (const name of names) {
184
+ if (!name.startsWith('port-') || !name.endsWith('.json'))
185
+ continue;
186
+ const portFile = join(dir, name);
187
+ const info = await stat(portFile).catch(() => undefined);
188
+ let raw;
167
189
  try {
168
- names = await readdir(dir);
190
+ raw = await readPrivateText(portFile);
169
191
  }
170
192
  catch (error) {
171
- if (error.code === 'ENOENT')
172
- continue;
173
- throw error;
193
+ if (error instanceof PrivateFileError && error.code !== 'ENOENT')
194
+ throw error;
195
+ continue;
174
196
  }
175
- for (const name of names) {
176
- if (!name.startsWith('port-') || !name.endsWith('.json'))
177
- continue;
178
- const portFile = join(dir, name);
179
- const info = await stat(portFile).catch(() => undefined);
180
- let raw;
181
- try {
182
- raw = await readPrivateText(portFile);
183
- }
184
- catch (error) {
185
- if (error instanceof PrivateFileError && error.code !== 'ENOENT')
186
- throw error;
187
- continue;
188
- }
189
- if (raw === undefined)
190
- continue;
191
- let parsed;
192
- try {
193
- parsed = JSON.parse(raw);
194
- }
195
- catch {
196
- await removeCorruptPortFile(portFile, log);
197
- continue;
198
- }
199
- if (!isUsablePortFileEntry(parsed, name)) {
200
- await removeCorruptPortFile(portFile, log);
201
- continue;
202
- }
203
- const entry = parsed;
204
- if (!pidAlive(entry.pid))
205
- await unlink(portFile).catch(() => { });
206
- else if (isStalePortFile(entry, info, cutoff))
207
- await removeStalePortFile(portFile, raw, info, options.beforeRemove);
197
+ if (raw === undefined)
198
+ continue;
199
+ let parsed;
200
+ try {
201
+ parsed = JSON.parse(raw);
208
202
  }
209
- if (resolve(dir) !== active)
210
- await rmdir(dir).catch(() => { });
203
+ catch {
204
+ await removeCorruptPortFile(portFile, log);
205
+ continue;
206
+ }
207
+ if (!isUsablePortFileEntry(parsed, name)) {
208
+ await removeCorruptPortFile(portFile, log);
209
+ continue;
210
+ }
211
+ const entry = parsed;
212
+ if (!pidAlive(entry.pid))
213
+ await unlink(portFile).catch(() => { });
214
+ else if (isStalePortFile(entry, info, cutoff))
215
+ await removeStalePortFile(portFile, raw, info, options.beforeRemove);
211
216
  }
217
+ return true;
212
218
  }
213
219
  /** Internal cache validation: probe only the selected file and its PID. */
214
220
  export async function portFileIdentity(dir, entry, options = {}) {
@@ -24,7 +24,9 @@ export function setPrivateFilesBackendForTest(api) {
24
24
  /** The fixed `reason` of a refusal because the Windows native addon did not load. */
25
25
  export const PRIVATE_FILES_UNAVAILABLE_REASON = 'native-addon-unavailable';
26
26
  function privateFilesUnavailable(cause) {
27
- return Object.assign(new Error('Windows private-files native addon is unavailable', { cause }), {
27
+ return Object.assign(new Error('Windows private-files native addon could not be loaded', {
28
+ cause,
29
+ }), {
28
30
  code: 'ERR_PRIVATE_FILES_UNAVAILABLE',
29
31
  reason: PRIVATE_FILES_UNAVAILABLE_REASON,
30
32
  });
@@ -83,7 +85,9 @@ export class PrivateFileError extends Error {
83
85
  constructor(path, stage, cause) {
84
86
  const detail = cause;
85
87
  super(detail?.code === 'ERR_PRIVATE_FILES_UNAVAILABLE'
86
- ? `Private file refused at ${stage}: ${path} (${PRIVATE_FILES_UNAVAILABLE_REASON}). The Windows native addon of @cortexkit/private-files could not be loaded, so owner-only access cannot be enforced and nothing was read or written. Reinstall the plugin so the private-files Windows prebuild for this architecture is present; Windows has no safe fallback to permission bits.`
88
+ ? // The reason stays on `.reason`: its word "unavailable" would make
89
+ // OpenCode 1 retry a plain error carrying this message.
90
+ `Private file refused at ${stage}: ${path}. The Windows native addon of @cortexkit/private-files could not be loaded, so owner-only access cannot be enforced and nothing was read or written. Reinstall the plugin so the private-files Windows prebuild for this architecture is present; Windows has no safe fallback to permission bits.`
87
91
  : `Private file refused at ${stage}: ${path} (${detail?.reason ?? detail?.code ?? 'io-error'}). Keep this file in a folder that only you, the system or administrators can change, such as a folder in your home directory. If other users could read the file before, sign in again to replace the credentials it held. On Windows the file must also be on a local drive that keeps Windows file permissions, such as NTFS.`, { cause });
88
92
  this.path = path;
89
93
  this.stage = stage;
@@ -94,9 +94,8 @@ async function removeCorruptPortFile(portFile, log) {
94
94
  export async function writePortFile(dir, entry, options = {}) {
95
95
  // The directory can be removed by another project's sweep between our
96
96
  // mkdir and the first open/rename, so the whole create-then-rename
97
- // unit is retried once on ENOENT. The retry recreates the directory; a
98
- // persistent ENOENT (e.g. permission, read-only parent) will surface on
99
- // the second attempt — failing fast beats an unbounded loop.
97
+ // unit gets up to five attempts on ENOENT. Each retry recreates the
98
+ // directory after a short delay, but persistent removal still surfaces.
100
99
  const writeOnce = async () => {
101
100
  const target = join(dir, `port-${entry.pid}.json`);
102
101
  if (windowsPrivateFiles) {
@@ -136,14 +135,15 @@ export async function writePortFile(dir, entry, options = {}) {
136
135
  }
137
136
  return target;
138
137
  };
139
- try {
140
- return await writeOnce();
141
- }
142
- catch (error) {
143
- if (error.code === 'ENOENT') {
138
+ for (let attempt = 1;; attempt += 1) {
139
+ try {
144
140
  return await writeOnce();
145
141
  }
146
- throw error;
142
+ catch (error) {
143
+ if (error.code !== 'ENOENT' || attempt === 5)
144
+ throw error;
145
+ await new Promise((resolve) => setTimeout(resolve, 25));
146
+ }
147
147
  }
148
148
  }
149
149
  export async function sweepRpcState(root, activeDir, isManagedDir, log, options = {}) {
@@ -163,52 +163,58 @@ export async function sweepRpcState(root, activeDir, isManagedDir, log, options
163
163
  continue;
164
164
  }
165
165
  const dir = join(root, projectDir.name);
166
- let names;
166
+ if (!(await sweepRpcDirectory(dir, log, options, cutoff)))
167
+ continue;
168
+ if (resolve(dir) !== active)
169
+ await rmdir(dir).catch(() => { });
170
+ }
171
+ }
172
+ /** Internal folder cleanup shared by full and throttled startup sweeps. */
173
+ export async function sweepRpcDirectory(dir, log, options = {}, cutoff = staleBefore(options)) {
174
+ let names;
175
+ try {
176
+ names = await readdir(dir);
177
+ }
178
+ catch (error) {
179
+ if (error.code === 'ENOENT')
180
+ return false;
181
+ throw error;
182
+ }
183
+ for (const name of names) {
184
+ if (!name.startsWith('port-') || !name.endsWith('.json'))
185
+ continue;
186
+ const portFile = join(dir, name);
187
+ const info = await stat(portFile).catch(() => undefined);
188
+ let raw;
167
189
  try {
168
- names = await readdir(dir);
190
+ raw = await readPrivateText(portFile);
169
191
  }
170
192
  catch (error) {
171
- if (error.code === 'ENOENT')
172
- continue;
173
- throw error;
193
+ if (error instanceof PrivateFileError && error.code !== 'ENOENT')
194
+ throw error;
195
+ continue;
174
196
  }
175
- for (const name of names) {
176
- if (!name.startsWith('port-') || !name.endsWith('.json'))
177
- continue;
178
- const portFile = join(dir, name);
179
- const info = await stat(portFile).catch(() => undefined);
180
- let raw;
181
- try {
182
- raw = await readPrivateText(portFile);
183
- }
184
- catch (error) {
185
- if (error instanceof PrivateFileError && error.code !== 'ENOENT')
186
- throw error;
187
- continue;
188
- }
189
- if (raw === undefined)
190
- continue;
191
- let parsed;
192
- try {
193
- parsed = JSON.parse(raw);
194
- }
195
- catch {
196
- await removeCorruptPortFile(portFile, log);
197
- continue;
198
- }
199
- if (!isUsablePortFileEntry(parsed, name)) {
200
- await removeCorruptPortFile(portFile, log);
201
- continue;
202
- }
203
- const entry = parsed;
204
- if (!pidAlive(entry.pid))
205
- await unlink(portFile).catch(() => { });
206
- else if (isStalePortFile(entry, info, cutoff))
207
- await removeStalePortFile(portFile, raw, info, options.beforeRemove);
197
+ if (raw === undefined)
198
+ continue;
199
+ let parsed;
200
+ try {
201
+ parsed = JSON.parse(raw);
208
202
  }
209
- if (resolve(dir) !== active)
210
- await rmdir(dir).catch(() => { });
203
+ catch {
204
+ await removeCorruptPortFile(portFile, log);
205
+ continue;
206
+ }
207
+ if (!isUsablePortFileEntry(parsed, name)) {
208
+ await removeCorruptPortFile(portFile, log);
209
+ continue;
210
+ }
211
+ const entry = parsed;
212
+ if (!pidAlive(entry.pid))
213
+ await unlink(portFile).catch(() => { });
214
+ else if (isStalePortFile(entry, info, cutoff))
215
+ await removeStalePortFile(portFile, raw, info, options.beforeRemove);
211
216
  }
217
+ return true;
212
218
  }
213
219
  /** Internal cache validation: probe only the selected file and its PID. */
214
220
  export async function portFileIdentity(dir, entry, options = {}) {