minimal-forge-work-pack-sync 0.1.53 → 0.1.55

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 (3) hide show
  1. package/dist/index.js +467 -200
  2. package/dist/repair.js +105 -104
  3. package/package.json +1 -1
package/dist/repair.js CHANGED
@@ -1,8 +1,94 @@
1
1
  // packages/cli/src/repair.ts
2
2
  import { createInterface } from "node:readline/promises";
3
3
 
4
+ // packages/cli/src/coreSyncLock.ts
5
+ import { randomUUID } from "node:crypto";
6
+ import { mkdir, readdir, readFile, rename, rmdir, unlink, writeFile } from "node:fs/promises";
7
+ import { hostname } from "node:os";
8
+ import path from "node:path";
9
+ function hasCode(error, ...codes) {
10
+ return error instanceof Error && codes.includes(error.code ?? "");
11
+ }
12
+ var CoreSyncLockError = class extends Error {
13
+ };
14
+ function unknownOwner() {
15
+ return new CoreSyncLockError("O propriet\xE1rio do bloqueio de sincroniza\xE7\xE3o Forge n\xE3o foi confirmado nesta m\xE1quina. Pe\xE7a ao operador para verificar o processo e a m\xE1quina de origem.");
16
+ }
17
+ async function removeEmpty(directory) {
18
+ try {
19
+ await rmdir(directory);
20
+ } catch (error) {
21
+ if (!hasCode(error, "ENOENT", "EEXIST", "ENOTEMPTY")) throw error;
22
+ }
23
+ }
24
+ async function reclaim(lockPath, renameError) {
25
+ let entries;
26
+ try {
27
+ entries = await readdir(lockPath);
28
+ } catch (error) {
29
+ if (!hasCode(error, "ENOENT")) throw error;
30
+ if (hasCode(renameError, "EACCES", "EPERM")) throw renameError;
31
+ return;
32
+ }
33
+ if (entries.length) {
34
+ const marker = entries[0];
35
+ const match = /^owner-([1-9]\d*)-[0-9a-f-]{36}$/.exec(marker);
36
+ if (entries.length !== 1 || !match) throw unknownOwner();
37
+ const markerPath = path.join(lockPath, marker);
38
+ let host;
39
+ try {
40
+ host = await readFile(markerPath, "utf8");
41
+ } catch (error) {
42
+ if (hasCode(error, "ENOENT")) return;
43
+ throw error;
44
+ }
45
+ if (host !== hostname()) throw unknownOwner();
46
+ try {
47
+ process.kill(Number(match[1]), 0);
48
+ } catch (error) {
49
+ if (!hasCode(error, "ESRCH")) throw error;
50
+ await unlink(markerPath).catch((cause) => {
51
+ if (!hasCode(cause, "ENOENT")) throw cause;
52
+ });
53
+ await removeEmpty(lockPath);
54
+ return;
55
+ }
56
+ throw new CoreSyncLockError("J\xE1 est\xE1 em curso uma sincroniza\xE7\xE3o do Forge. Aguarde que termine e volte a tentar.");
57
+ }
58
+ await removeEmpty(lockPath);
59
+ }
60
+ async function withCoreSyncLock(journalPath, operation) {
61
+ const lockPath = journalPath + ".lock";
62
+ const id = randomUUID();
63
+ const candidate = lockPath + "-" + id;
64
+ const marker = `owner-${process.pid}-${id}`;
65
+ await mkdir(path.dirname(lockPath), { recursive: true });
66
+ await mkdir(candidate, { mode: 448 });
67
+ let directory = candidate;
68
+ try {
69
+ await writeFile(path.join(candidate, marker), hostname(), { mode: 384, flag: "wx" });
70
+ for (let attempt = 0; attempt < 3; attempt++) {
71
+ try {
72
+ await rename(candidate, lockPath);
73
+ } catch (error) {
74
+ if (!hasCode(error, "EEXIST", "ENOTEMPTY", "EPERM", "EACCES")) throw error;
75
+ await reclaim(lockPath, error);
76
+ continue;
77
+ }
78
+ directory = lockPath;
79
+ return await operation();
80
+ }
81
+ throw new CoreSyncLockError("Outra sincroniza\xE7\xE3o Forge pode estar em curso. Aguarde que termine e volte a tentar.");
82
+ } finally {
83
+ await unlink(path.join(directory, marker)).catch((error) => {
84
+ if (!hasCode(error, "ENOENT")) throw error;
85
+ });
86
+ await removeEmpty(directory);
87
+ }
88
+ }
89
+
4
90
  // packages/cli/src/credentials.ts
5
- import { chmod, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
91
+ import { chmod, mkdir as mkdir2, readFile as readFile2, rename as rename2, rm, writeFile as writeFile2 } from "node:fs/promises";
6
92
  function shellCredentialFiles(paths, forgeApiKey) {
7
93
  return [
8
94
  { path: paths.envPath, bytes: Buffer.from(`export FORGE_API_KEY=${shellQuote(forgeApiKey)}
@@ -11,7 +97,7 @@ function shellCredentialFiles(paths, forgeApiKey) {
11
97
  ];
12
98
  }
13
99
  async function readCredentials(paths) {
14
- const raw = await readFile(paths.credentialsPath, "utf8");
100
+ const raw = await readFile2(paths.credentialsPath, "utf8");
15
101
  const parsed = JSON.parse(raw);
16
102
  if (!parsed.forgeApiKey || !parsed.forgeBaseUrl) {
17
103
  throw new Error(`Ficheiro de credenciais inv\xE1lido em ${paths.credentialsPath}`);
@@ -116,9 +202,9 @@ function stripTrailingCommas(input) {
116
202
 
117
203
  // packages/cli/src/ownerOnlyFile.ts
118
204
  import { spawnSync as nodeSpawnSync } from "node:child_process";
119
- import { randomUUID } from "node:crypto";
120
- import { chmod as chmod2, mkdir as mkdir2, rename as rename2, rm as rm2, stat, writeFile as writeFile2 } from "node:fs/promises";
121
- import path from "node:path";
205
+ import { randomUUID as randomUUID2 } from "node:crypto";
206
+ import { chmod as chmod2, mkdir as mkdir3, rename as rename3, rm as rm2, stat, writeFile as writeFile3 } from "node:fs/promises";
207
+ import path2 from "node:path";
122
208
  async function protectOwnerOnlyPath(filePath, kind, options = {}) {
123
209
  const platform = options.platform ?? process.platform;
124
210
  if (platform !== "win32") {
@@ -160,7 +246,7 @@ async function assertExistingDirectoryIsSafe(directory, options) {
160
246
  }
161
247
  }
162
248
  async function prepareOwnerOnlyFileDestination(filePath, options = {}) {
163
- const directory = path.dirname(filePath);
249
+ const directory = path2.dirname(filePath);
164
250
  let existing;
165
251
  try {
166
252
  existing = await stat(directory);
@@ -174,7 +260,7 @@ async function prepareOwnerOnlyFileDestination(filePath, options = {}) {
174
260
  }
175
261
  let createdDirectory;
176
262
  try {
177
- createdDirectory = await mkdir2(directory, { recursive: true, mode: 448 });
263
+ createdDirectory = await mkdir3(directory, { recursive: true, mode: 448 });
178
264
  } catch (error) {
179
265
  if (error.code !== "EEXIST" || !(await stat(directory)).isDirectory()) throw error;
180
266
  await assertExistingDirectoryIsSafe(directory, options);
@@ -184,13 +270,13 @@ async function prepareOwnerOnlyFileDestination(filePath, options = {}) {
184
270
  else await assertExistingDirectoryIsSafe(directory, options);
185
271
  }
186
272
  async function writeOwnerOnlyTextAtomic(filePath, content, options = {}) {
187
- const directory = path.dirname(filePath);
273
+ const directory = path2.dirname(filePath);
188
274
  await prepareOwnerOnlyFileDestination(filePath, options);
189
- const temporary = path.join(directory, `.${path.basename(filePath)}.${randomUUID()}.tmp`);
275
+ const temporary = path2.join(directory, `.${path2.basename(filePath)}.${randomUUID2()}.tmp`);
190
276
  try {
191
- await writeFile2(temporary, content, { flag: "wx", mode: 384 });
277
+ await writeFile3(temporary, content, { flag: "wx", mode: 384 });
192
278
  await protectOwnerOnlyPath(temporary, "file", options);
193
- await rename2(temporary, filePath);
279
+ await rename3(temporary, filePath);
194
280
  await protectOwnerOnlyPath(filePath, "file", options);
195
281
  } catch (error) {
196
282
  await rm2(temporary, { force: true }).catch(() => void 0);
@@ -199,7 +285,7 @@ async function writeOwnerOnlyTextAtomic(filePath, content, options = {}) {
199
285
  }
200
286
 
201
287
  // packages/cli/src/remoteConfig.ts
202
- import { readFile as readFile2 } from "node:fs/promises";
288
+ import { readFile as readFile3 } from "node:fs/promises";
203
289
 
204
290
  // packages/shared/src/contracts/models.ts
205
291
  var BUILT_IN_OPENROUTER_MODELS = [
@@ -419,7 +505,7 @@ var DEVICE_ENROLLMENT_PROTOCOL_VERSION = 1;
419
505
  // packages/shared/src/contracts/settings.ts
420
506
  var DOCUMENT_READER_AGENT = "document-reader";
421
507
  var OPENROUTER_ALIAS_PROVIDER_NAME = "OpenRouter (via Forge)";
422
- var WORK_PACK_SYNC_PLUGIN_VERSION = "0.1.53";
508
+ var WORK_PACK_SYNC_PLUGIN_VERSION = "0.1.55";
423
509
  var WORK_PACK_SYNC_PLUGIN_PACKAGE = `minimal-forge-work-pack-sync@${WORK_PACK_SYNC_PLUGIN_VERSION}`;
424
510
  var WORK_PACK_SYNC_PLUGIN_FILENAME = `minimal-forge-work-pack-sync-${WORK_PACK_SYNC_PLUGIN_VERSION}.tgz`;
425
511
  var FIRECRAWL_KEYLESS_MCP_NAME = "firecrawl";
@@ -599,7 +685,7 @@ function normalizeBaseUrl(value) {
599
685
  async function readAuthJson(paths) {
600
686
  let raw;
601
687
  try {
602
- raw = await readFile2(paths.opencodeAuthPath, "utf8");
688
+ raw = await readFile3(paths.opencodeAuthPath, "utf8");
603
689
  } catch (error) {
604
690
  if (error.code === "ENOENT") return {};
605
691
  throw error;
@@ -616,95 +702,6 @@ function isRecord2(value) {
616
702
  import { createHash, randomUUID as randomUUID3 } from "node:crypto";
617
703
  import { chmod as chmod3, lstat, mkdir as mkdir4, readFile as readFile4, rename as rename4, rm as rm3, writeFile as writeFile4 } from "node:fs/promises";
618
704
  import path3 from "node:path";
619
-
620
- // packages/cli/src/coreSyncLock.ts
621
- import { randomUUID as randomUUID2 } from "node:crypto";
622
- import { mkdir as mkdir3, readdir, readFile as readFile3, rename as rename3, rmdir, unlink, writeFile as writeFile3 } from "node:fs/promises";
623
- import { hostname } from "node:os";
624
- import path2 from "node:path";
625
- function hasCode(error, ...codes) {
626
- return error instanceof Error && codes.includes(error.code ?? "");
627
- }
628
- function busy() {
629
- return new Error("Outra sincroniza\xE7\xE3o Forge pode estar em curso. Aguarde que termine e volte a tentar.");
630
- }
631
- function unknownOwner() {
632
- return new Error("O propriet\xE1rio do bloqueio de sincroniza\xE7\xE3o Forge n\xE3o foi confirmado nesta m\xE1quina. Pe\xE7a ao operador para verificar o processo e a m\xE1quina de origem.");
633
- }
634
- async function removeEmpty(directory) {
635
- try {
636
- await rmdir(directory);
637
- } catch (error) {
638
- if (!hasCode(error, "ENOENT", "EEXIST", "ENOTEMPTY")) throw error;
639
- }
640
- }
641
- async function reclaim(lockPath, renameError) {
642
- let entries;
643
- try {
644
- entries = await readdir(lockPath);
645
- } catch (error) {
646
- if (!hasCode(error, "ENOENT")) throw error;
647
- if (hasCode(renameError, "EACCES", "EPERM")) throw renameError;
648
- return;
649
- }
650
- if (entries.length) {
651
- const marker = entries[0];
652
- const match = /^owner-([1-9]\d*)-[0-9a-f-]{36}$/.exec(marker);
653
- if (entries.length !== 1 || !match) throw unknownOwner();
654
- const markerPath = path2.join(lockPath, marker);
655
- let host;
656
- try {
657
- host = await readFile3(markerPath, "utf8");
658
- } catch (error) {
659
- if (hasCode(error, "ENOENT")) return;
660
- throw error;
661
- }
662
- if (host !== hostname()) throw unknownOwner();
663
- try {
664
- process.kill(Number(match[1]), 0);
665
- } catch (error) {
666
- if (!hasCode(error, "ESRCH")) throw error;
667
- await unlink(markerPath).catch((cause) => {
668
- if (!hasCode(cause, "ENOENT")) throw cause;
669
- });
670
- await removeEmpty(lockPath);
671
- return;
672
- }
673
- throw busy();
674
- }
675
- await removeEmpty(lockPath);
676
- }
677
- async function withCoreSyncLock(journalPath, operation) {
678
- const lockPath = journalPath + ".lock";
679
- const id = randomUUID2();
680
- const candidate = lockPath + "-" + id;
681
- const marker = `owner-${process.pid}-${id}`;
682
- await mkdir3(path2.dirname(lockPath), { recursive: true });
683
- await mkdir3(candidate, { mode: 448 });
684
- let directory = candidate;
685
- try {
686
- await writeFile3(path2.join(candidate, marker), hostname(), { mode: 384, flag: "wx" });
687
- for (let attempt = 0; attempt < 3; attempt++) {
688
- try {
689
- await rename3(candidate, lockPath);
690
- } catch (error) {
691
- if (!hasCode(error, "EEXIST", "ENOTEMPTY", "EPERM", "EACCES")) throw error;
692
- await reclaim(lockPath, error);
693
- continue;
694
- }
695
- directory = lockPath;
696
- return await operation();
697
- }
698
- throw busy();
699
- } finally {
700
- await unlink(path2.join(directory, marker)).catch((error) => {
701
- if (!hasCode(error, "ENOENT")) throw error;
702
- });
703
- await removeEmpty(directory);
704
- }
705
- }
706
-
707
- // packages/cli/src/coreSyncTransaction.ts
708
705
  var CORE_RUNTIME_MANIFEST_VERSION = 1;
709
706
  var CoreSyncUncertainError = class extends Error {
710
707
  constructor(message, paths) {
@@ -4573,7 +4570,11 @@ async function main() {
4573
4570
  if (result.previousConfigPath) console.log(`A c\xF3pia anterior foi preservada em ${result.previousConfigPath}`);
4574
4571
  }
4575
4572
  main().catch((error) => {
4573
+ process.exitCode = 1;
4574
+ if (error instanceof CoreSyncLockError) {
4575
+ console.error(error.message);
4576
+ return;
4577
+ }
4576
4578
  if (error instanceof LocalConfigRecoveryError) console.error(error.message);
4577
4579
  console.error("N\xE3o foi poss\xEDvel concluir a recupera\xE7\xE3o. Preserve os ficheiros e pe\xE7a apoio ao operador. Se um c\xF3digo de inscri\xE7\xE3o j\xE1 foi aprovado, confirme o estado e revogue a chave correlacionada antes de repetir.");
4578
- process.exitCode = 1;
4579
4580
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "minimal-forge-work-pack-sync",
3
- "version": "0.1.53",
3
+ "version": "0.1.55",
4
4
  "description": "Minimal Forge Work Pack startup synchronization for OpenCode and OpenWork.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {