minimal-forge-work-pack-sync 0.1.49 → 0.1.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +507 -326
- package/dist/repair.js +223 -69
- package/package.json +1 -1
package/dist/repair.js
CHANGED
|
@@ -35,9 +35,9 @@ function renderPowerShellEnv(forgeApiKey) {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// packages/cli/src/localConfigRecovery.ts
|
|
38
|
-
import { randomUUID as
|
|
39
|
-
import { readFile as
|
|
40
|
-
import
|
|
38
|
+
import { randomUUID as randomUUID4 } from "node:crypto";
|
|
39
|
+
import { readFile as readFile5 } from "node:fs/promises";
|
|
40
|
+
import path4 from "node:path";
|
|
41
41
|
|
|
42
42
|
// packages/cli/src/jsonc.ts
|
|
43
43
|
function parseJsonc(input) {
|
|
@@ -419,7 +419,7 @@ var DEVICE_ENROLLMENT_PROTOCOL_VERSION = 1;
|
|
|
419
419
|
// packages/shared/src/contracts/settings.ts
|
|
420
420
|
var DOCUMENT_READER_AGENT = "document-reader";
|
|
421
421
|
var OPENROUTER_ALIAS_PROVIDER_NAME = "OpenRouter (via Forge)";
|
|
422
|
-
var WORK_PACK_SYNC_PLUGIN_VERSION = "0.1.
|
|
422
|
+
var WORK_PACK_SYNC_PLUGIN_VERSION = "0.1.53";
|
|
423
423
|
var WORK_PACK_SYNC_PLUGIN_PACKAGE = `minimal-forge-work-pack-sync@${WORK_PACK_SYNC_PLUGIN_VERSION}`;
|
|
424
424
|
var WORK_PACK_SYNC_PLUGIN_FILENAME = `minimal-forge-work-pack-sync-${WORK_PACK_SYNC_PLUGIN_VERSION}.tgz`;
|
|
425
425
|
var FIRECRAWL_KEYLESS_MCP_NAME = "firecrawl";
|
|
@@ -613,9 +613,98 @@ function isRecord2(value) {
|
|
|
613
613
|
}
|
|
614
614
|
|
|
615
615
|
// packages/cli/src/coreSyncTransaction.ts
|
|
616
|
-
import { createHash, randomUUID as
|
|
617
|
-
import { chmod as chmod3, lstat, mkdir as
|
|
616
|
+
import { createHash, randomUUID as randomUUID3 } from "node:crypto";
|
|
617
|
+
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
|
+
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";
|
|
618
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
|
|
619
708
|
var CORE_RUNTIME_MANIFEST_VERSION = 1;
|
|
620
709
|
var CoreSyncUncertainError = class extends Error {
|
|
621
710
|
constructor(message, paths) {
|
|
@@ -624,11 +713,11 @@ var CoreSyncUncertainError = class extends Error {
|
|
|
624
713
|
}
|
|
625
714
|
paths;
|
|
626
715
|
};
|
|
627
|
-
var defaultFs = { chmod: chmod3, lstat, mkdir:
|
|
716
|
+
var defaultFs = { chmod: chmod3, lstat, mkdir: mkdir4, readFile: readFile4, rename: rename4, rm: rm3, writeFile: writeFile4 };
|
|
628
717
|
function coreRuntimeManifestDigest(files) {
|
|
629
718
|
const canonical = [...files].sort((a, b2) => a.id.localeCompare(b2.id)).map((file) => ({
|
|
630
719
|
id: file.id,
|
|
631
|
-
path:
|
|
720
|
+
path: path3.resolve(file.path),
|
|
632
721
|
exists: file.bytes !== null,
|
|
633
722
|
digest: file.bytes === null ? void 0 : sha256(file.bytes),
|
|
634
723
|
mode: file.mode,
|
|
@@ -645,8 +734,8 @@ function validateCoreRuntimeManifest(manifest) {
|
|
|
645
734
|
const targets = /* @__PURE__ */ new Set();
|
|
646
735
|
for (const file of manifest.files) {
|
|
647
736
|
if (!file.id || ids.has(file.id)) throw new Error(`Duplicate core runtime file id: ${file.id}`);
|
|
648
|
-
const target =
|
|
649
|
-
if (!
|
|
737
|
+
const target = path3.resolve(file.path);
|
|
738
|
+
if (!path3.isAbsolute(file.path) || targets.has(target)) throw new Error(`Invalid or duplicate core runtime target: ${file.path}`);
|
|
650
739
|
if (file.mode !== void 0 && (!Number.isInteger(file.mode) || file.mode < 0 || file.mode > 4095)) throw new Error(`Invalid mode for ${file.id}`);
|
|
651
740
|
if (file.ownerOnly !== void 0 && typeof file.ownerOnly !== "boolean") throw new Error(`Invalid owner-only flag for ${file.id}`);
|
|
652
741
|
ids.add(file.id);
|
|
@@ -654,19 +743,28 @@ function validateCoreRuntimeManifest(manifest) {
|
|
|
654
743
|
}
|
|
655
744
|
if (coreRuntimeManifestDigest(manifest.files) !== manifest.digest) throw new Error("Core runtime manifest digest mismatch");
|
|
656
745
|
}
|
|
657
|
-
async function
|
|
658
|
-
|
|
746
|
+
async function withRecoveredCoreSync(journalPath, operation, options = {}) {
|
|
747
|
+
const fs2 = options.fs ?? defaultFs;
|
|
748
|
+
return withCoreSyncLock(journalPath, async () => {
|
|
749
|
+
const recovery = await recoverUnderLock(journalPath, fs2);
|
|
750
|
+
await options.afterRecovery?.();
|
|
751
|
+
if (recovery.status === "uncertain") throw new CoreSyncUncertainError(recovery.message, recovery.uncertainPaths);
|
|
752
|
+
return operation((manifest, hooks) => {
|
|
753
|
+
validateCoreRuntimeManifest(manifest);
|
|
754
|
+
return commitCoreFiles({ manifest, journalPath, fs: fs2, hooks });
|
|
755
|
+
});
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
async function commitCoreFiles(options) {
|
|
659
759
|
const fs2 = options.fs ?? defaultFs;
|
|
660
760
|
const hooks = options.hooks;
|
|
661
|
-
const
|
|
662
|
-
if (existingRecovery.status === "uncertain") throw new CoreSyncUncertainError(existingRecovery.message, existingRecovery.uncertainPaths);
|
|
663
|
-
const transactionId = randomUUID2();
|
|
761
|
+
const transactionId = randomUUID3();
|
|
664
762
|
const files = [];
|
|
665
763
|
try {
|
|
666
764
|
for (const desired of options.manifest.files) {
|
|
667
|
-
const target =
|
|
765
|
+
const target = path3.resolve(desired.path);
|
|
668
766
|
if (desired.ownerOnly && desired.bytes !== null && fs2 === defaultFs) await prepareOwnerOnlyFileDestination(target);
|
|
669
|
-
else await fs2.mkdir(
|
|
767
|
+
else await fs2.mkdir(path3.dirname(target), { recursive: true });
|
|
670
768
|
const original = await capture(target, fs2);
|
|
671
769
|
const suffix = `.forge-core-${transactionId}`;
|
|
672
770
|
const backup = original.exists ? `${target}${suffix}.backup` : void 0;
|
|
@@ -695,58 +793,93 @@ async function applyCoreSyncTransaction(options) {
|
|
|
695
793
|
await hooks?.boundary?.("after-journal");
|
|
696
794
|
for (const file of files) {
|
|
697
795
|
await hooks?.boundary?.(`before-commit:${file.id}`);
|
|
698
|
-
await fs2.rm(file.target, { force: true });
|
|
699
796
|
if (file.stage) await fs2.rename(file.stage, file.target);
|
|
797
|
+
else await fs2.rm(file.target, { force: true });
|
|
700
798
|
if (file.stage && file.ownerOnly && fs2 === defaultFs) await protectOwnerOnlyPath(file.target, "file");
|
|
701
799
|
await hooks?.boundary?.(`after-commit:${file.id}`);
|
|
702
800
|
}
|
|
703
801
|
await hooks?.boundary?.("before-cleanup");
|
|
704
|
-
await
|
|
705
|
-
await hooks?.boundary?.("after-cleanup");
|
|
706
|
-
return { status: "committed", manifestId: options.manifest.id, digest: options.manifest.digest };
|
|
802
|
+
await fs2.rename(options.journalPath, cleanupPath(options.journalPath));
|
|
707
803
|
} catch (error) {
|
|
708
804
|
if (!await exists(options.journalPath, fs2)) {
|
|
709
805
|
await Promise.all(files.flatMap((file) => [file.stage, file.backup].filter((item) => Boolean(item))).map((item) => fs2.rm(item, { force: true }).catch(() => void 0)));
|
|
710
806
|
throw error;
|
|
711
807
|
}
|
|
712
|
-
const recovery = await
|
|
808
|
+
const recovery = await recoverUnderLock(options.journalPath, fs2);
|
|
713
809
|
if (recovery.status === "uncertain") throw new CoreSyncUncertainError(recovery.message, recovery.uncertainPaths);
|
|
714
810
|
throw error;
|
|
715
811
|
}
|
|
716
|
-
}
|
|
717
|
-
async function recoverCoreSyncTransaction(options) {
|
|
718
|
-
const fs2 = options.fs ?? defaultFs;
|
|
719
|
-
let journal;
|
|
812
|
+
const committed = { status: "committed", manifestId: options.manifest.id, digest: options.manifest.digest };
|
|
720
813
|
try {
|
|
721
|
-
|
|
722
|
-
} catch
|
|
723
|
-
|
|
724
|
-
return { status: "uncertain", uncertainPaths: [options.journalPath], message: "Core sync journal is unreadable; exact recovery is uncertain." };
|
|
814
|
+
await cleanup(files, cleanupPath(options.journalPath), fs2);
|
|
815
|
+
} catch {
|
|
816
|
+
return committed;
|
|
725
817
|
}
|
|
726
|
-
|
|
818
|
+
await hooks?.boundary?.("after-cleanup");
|
|
819
|
+
return committed;
|
|
820
|
+
}
|
|
821
|
+
async function recoverUnderLock(activePath, fs2) {
|
|
822
|
+
const retiredPath = cleanupPath(activePath);
|
|
823
|
+
for (const journalPath of [retiredPath, activePath]) {
|
|
824
|
+
let journal;
|
|
825
|
+
try {
|
|
826
|
+
journal = JSON.parse(await fs2.readFile(journalPath, "utf8"));
|
|
827
|
+
if (!validJournal(journal)) throw new Error("Invalid journal");
|
|
828
|
+
} catch (error) {
|
|
829
|
+
if (isNotFound(error)) continue;
|
|
830
|
+
return { status: "uncertain", uncertainPaths: [journalPath], message: "Core sync journal is unreadable or invalid; exact recovery is uncertain." };
|
|
831
|
+
}
|
|
832
|
+
if (journalPath === retiredPath) {
|
|
833
|
+
await cleanup(journal.files, retiredPath, fs2);
|
|
834
|
+
} else {
|
|
835
|
+
return restoreJournal(journal, journalPath, fs2);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return { status: "nothing-to-recover" };
|
|
839
|
+
}
|
|
840
|
+
async function restoreJournal(journal, journalPath, fs2) {
|
|
727
841
|
const uncertain = [];
|
|
842
|
+
const backups = /* @__PURE__ */ new Map();
|
|
728
843
|
for (const file of journal.files) {
|
|
844
|
+
if (!file.original.exists) continue;
|
|
729
845
|
try {
|
|
730
|
-
if (!file.
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
846
|
+
if (!file.backup) throw new Error("backup missing");
|
|
847
|
+
const bytes = await fs2.readFile(file.backup);
|
|
848
|
+
if (sha256(bytes) !== file.original.digest) throw new Error("backup digest mismatch");
|
|
849
|
+
backups.set(file, bytes);
|
|
850
|
+
} catch {
|
|
851
|
+
uncertain.push(file.target);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
if (uncertain.length) return uncertainRestore(journal, uncertain);
|
|
855
|
+
for (const file of journal.files) {
|
|
856
|
+
try {
|
|
857
|
+
if (!file.original.exists) {
|
|
736
858
|
await fs2.rm(file.target, { force: true });
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
859
|
+
continue;
|
|
860
|
+
}
|
|
861
|
+
const restorePath = `${file.target}.forge-restore-${journal.transactionId}`;
|
|
862
|
+
await fs2.rm(restorePath, { force: true });
|
|
863
|
+
await writeExact(restorePath, backups.get(file), file.original.mode, fs2, file.ownerOnly);
|
|
864
|
+
await fs2.rename(restorePath, file.target);
|
|
865
|
+
if (file.ownerOnly && fs2 === defaultFs) await protectOwnerOnlyPath(file.target, "file");
|
|
866
|
+
const restored = await capture(file.target, fs2);
|
|
867
|
+
if (!restored.bytes || sha256(restored.bytes) !== file.original.digest || restored.mode !== effectiveMode(file.original.mode, file.ownerOnly)) {
|
|
868
|
+
throw new Error("restore verification failed");
|
|
741
869
|
}
|
|
742
870
|
} catch {
|
|
743
871
|
uncertain.push(file.target);
|
|
744
872
|
}
|
|
745
873
|
}
|
|
746
|
-
if (uncertain.length
|
|
747
|
-
|
|
874
|
+
if (uncertain.length) return uncertainRestore(journal, uncertain);
|
|
875
|
+
const retiredPath = cleanupPath(journalPath);
|
|
876
|
+
await fs2.rename(journalPath, retiredPath);
|
|
877
|
+
await cleanup(journal.files, retiredPath, fs2);
|
|
748
878
|
return { status: "restored", manifestId: journal.manifest.id };
|
|
749
879
|
}
|
|
880
|
+
function uncertainRestore(journal, paths) {
|
|
881
|
+
return { status: "uncertain", manifestId: journal.manifest.id, uncertainPaths: paths, message: "Core sync could not prove an exact restore for every path." };
|
|
882
|
+
}
|
|
750
883
|
async function capture(target, fs2) {
|
|
751
884
|
try {
|
|
752
885
|
const info = await fs2.lstat(target);
|
|
@@ -757,13 +890,17 @@ async function capture(target, fs2) {
|
|
|
757
890
|
throw error;
|
|
758
891
|
}
|
|
759
892
|
}
|
|
893
|
+
function effectiveMode(mode, ownerOnly = false) {
|
|
894
|
+
return ownerOnly && process.platform !== "win32" ? 384 : mode;
|
|
895
|
+
}
|
|
760
896
|
async function writeExact(target, bytes, mode, fs2, ownerOnly = false) {
|
|
761
|
-
|
|
762
|
-
await fs2.
|
|
897
|
+
const permissions = effectiveMode(mode, ownerOnly);
|
|
898
|
+
await fs2.writeFile(target, bytes, { flag: "wx", mode: permissions });
|
|
899
|
+
await fs2.chmod(target, permissions);
|
|
763
900
|
if (ownerOnly && fs2 === defaultFs) await protectOwnerOnlyPath(target, "file");
|
|
764
901
|
}
|
|
765
902
|
async function writeJournal(target, journal, fs2) {
|
|
766
|
-
await fs2.mkdir(
|
|
903
|
+
await fs2.mkdir(path3.dirname(target), { recursive: true });
|
|
767
904
|
const temporary = `${target}.next`;
|
|
768
905
|
await fs2.rm(temporary, { force: true });
|
|
769
906
|
await fs2.writeFile(temporary, `${JSON.stringify(journal, null, 2)}
|
|
@@ -771,8 +908,14 @@ async function writeJournal(target, journal, fs2) {
|
|
|
771
908
|
await fs2.chmod(temporary, 384);
|
|
772
909
|
await fs2.rename(temporary, target);
|
|
773
910
|
}
|
|
774
|
-
|
|
775
|
-
|
|
911
|
+
function cleanupPath(journalPath) {
|
|
912
|
+
return `${journalPath}.cleanup`;
|
|
913
|
+
}
|
|
914
|
+
async function cleanup(files, journalPath, fs2) {
|
|
915
|
+
for (const file of files) {
|
|
916
|
+
if (file.stage) await fs2.rm(file.stage, { force: true });
|
|
917
|
+
if (file.backup) await fs2.rm(file.backup, { force: true });
|
|
918
|
+
}
|
|
776
919
|
await fs2.rm(journalPath, { force: true });
|
|
777
920
|
}
|
|
778
921
|
async function exists(target, fs2) {
|
|
@@ -791,7 +934,12 @@ function sha256(value) {
|
|
|
791
934
|
return createHash("sha256").update(value).digest("hex");
|
|
792
935
|
}
|
|
793
936
|
function validJournal(value) {
|
|
794
|
-
|
|
937
|
+
if (value?.schemaVersion !== 1 || typeof value.transactionId !== "string" || !/^[a-zA-Z0-9_-]+$/.test(value.transactionId) || value.manifest?.version !== 1 || typeof value.manifest.id !== "string" || !Array.isArray(value.files)) return false;
|
|
938
|
+
return value.files.every((file) => {
|
|
939
|
+
if (typeof file?.id !== "string" || typeof file.target !== "string" || !path3.isAbsolute(file.target)) return false;
|
|
940
|
+
const prefix = `${file.target}.forge-core-${value.transactionId}`;
|
|
941
|
+
return (file.stage === void 0 || file.stage === `${prefix}.stage`) && (file.backup === void 0 || file.backup === `${prefix}.backup`) && typeof file.original?.exists === "boolean" && typeof file.desired?.exists === "boolean" && (file.ownerOnly === void 0 || typeof file.ownerOnly === "boolean");
|
|
942
|
+
});
|
|
795
943
|
}
|
|
796
944
|
|
|
797
945
|
// packages/cli/src/localConfigRecovery.ts
|
|
@@ -800,20 +948,22 @@ var LocalConfigRecoveryError = class extends Error {
|
|
|
800
948
|
var LAST_GOOD_FILENAME = "last-good-opencode.json";
|
|
801
949
|
function lastGoodLocalConfigFile(paths, credentials, content) {
|
|
802
950
|
assertForgeConfig(content, credentials);
|
|
803
|
-
return { id: "last-good-config", path:
|
|
951
|
+
return { id: "last-good-config", path: path4.join(paths.forgeDir, LAST_GOOD_FILENAME), bytes: Buffer.from(content), mode: 384, ownerOnly: true };
|
|
804
952
|
}
|
|
805
953
|
async function inspectLocalConfigRecovery(paths, credentials) {
|
|
806
|
-
const snapshot = await readOptional(
|
|
954
|
+
const snapshot = await readOptional(path4.join(paths.forgeDir, LAST_GOOD_FILENAME));
|
|
807
955
|
const current = await readOptional(paths.opencodeConfigPath);
|
|
808
956
|
return {
|
|
809
957
|
snapshot: snapshot === void 0 ? "missing" : isForgeConfig(snapshot, credentials) ? "available" : "invalid",
|
|
810
958
|
current: current === void 0 ? "missing" : isForgeConfig(current, credentials) && (snapshot === void 0 || !isForgeConfig(snapshot, credentials) || sameSavedForgeFields(current, snapshot)) ? "valid" : "invalid"
|
|
811
959
|
};
|
|
812
960
|
}
|
|
813
|
-
|
|
814
|
-
const
|
|
815
|
-
|
|
816
|
-
|
|
961
|
+
function repairLocalConfig(paths, credentials) {
|
|
962
|
+
const journalPath = path4.join(paths.forgeDir, "core-sync-journal.json");
|
|
963
|
+
return withRecoveredCoreSync(journalPath, () => restoreLocalConfig(paths, credentials));
|
|
964
|
+
}
|
|
965
|
+
async function restoreLocalConfig(paths, credentials) {
|
|
966
|
+
const snapshot = await readOptional(path4.join(paths.forgeDir, LAST_GOOD_FILENAME));
|
|
817
967
|
if (snapshot === void 0) throw new LocalConfigRecoveryError("N\xE3o existe uma configura\xE7\xE3o local v\xE1lida guardada. Execute sync quando o Forge estiver dispon\xEDvel.");
|
|
818
968
|
assertForgeConfig(snapshot, credentials);
|
|
819
969
|
const current = await readOptional(paths.opencodeConfigPath);
|
|
@@ -829,11 +979,11 @@ async function repairLocalConfig(paths, credentials) {
|
|
|
829
979
|
return { status: "already-valid" };
|
|
830
980
|
}
|
|
831
981
|
const replacement = current === void 0 ? snapshot : repairContent(current, snapshot);
|
|
832
|
-
const previousConfigPath = current === void 0 ? void 0 :
|
|
982
|
+
const previousConfigPath = current === void 0 ? void 0 : path4.join(paths.forgeDir, `opencode-before-repair-${randomUUID4()}.jsonc`);
|
|
833
983
|
if (previousConfigPath && current !== void 0) await writeOwnerOnlyTextAtomic(previousConfigPath, current);
|
|
834
984
|
await writeOwnerOnlyTextAtomic(paths.opencodeConfigPath, replacement);
|
|
835
985
|
try {
|
|
836
|
-
assertForgeConfig(await
|
|
986
|
+
assertForgeConfig(await readFile5(paths.opencodeConfigPath, "utf8"), credentials);
|
|
837
987
|
} catch (error) {
|
|
838
988
|
if (current !== void 0) await writeOwnerOnlyTextAtomic(paths.opencodeConfigPath, current);
|
|
839
989
|
throw error;
|
|
@@ -916,7 +1066,7 @@ function forgePluginEntries(config) {
|
|
|
916
1066
|
}
|
|
917
1067
|
async function readOptional(filePath) {
|
|
918
1068
|
try {
|
|
919
|
-
return await
|
|
1069
|
+
return await readFile5(filePath, "utf8");
|
|
920
1070
|
} catch (error) {
|
|
921
1071
|
if (error.code === "ENOENT") return void 0;
|
|
922
1072
|
throw error;
|
|
@@ -933,9 +1083,9 @@ function assertForgeVisible(config) {
|
|
|
933
1083
|
}
|
|
934
1084
|
|
|
935
1085
|
// packages/cli/src/reconnectWorker.ts
|
|
936
|
-
import { randomUUID as
|
|
937
|
-
import { access, readFile as
|
|
938
|
-
import
|
|
1086
|
+
import { randomUUID as randomUUID5 } from "node:crypto";
|
|
1087
|
+
import { access, readFile as readFile6 } from "node:fs/promises";
|
|
1088
|
+
import path5 from "node:path";
|
|
939
1089
|
|
|
940
1090
|
// packages/cli/src/deviceEnrollment.ts
|
|
941
1091
|
var MAX_RESPONSE_BYTES = 16 * 1024;
|
|
@@ -4276,12 +4426,16 @@ function validCoreManifest(value) {
|
|
|
4276
4426
|
}
|
|
4277
4427
|
|
|
4278
4428
|
// packages/cli/src/reconnectWorker.ts
|
|
4279
|
-
|
|
4280
|
-
const
|
|
4429
|
+
function reconnectWorker(paths) {
|
|
4430
|
+
const journalPath = path5.join(paths.forgeDir, "core-sync-journal.json");
|
|
4431
|
+
return withRecoveredCoreSync(journalPath, (apply) => enrollAndSaveConfig(paths, apply));
|
|
4432
|
+
}
|
|
4433
|
+
async function enrollAndSaveConfig(paths, apply) {
|
|
4434
|
+
const origin = JSON.parse(await readFile6(path5.join(paths.forgeDir, "recovery-origin.json"), "utf8"));
|
|
4281
4435
|
const forgeBaseUrl = normalizeForgeBaseUrl(origin.forgeBaseUrl);
|
|
4282
4436
|
let current;
|
|
4283
4437
|
try {
|
|
4284
|
-
current = await
|
|
4438
|
+
current = await readFile6(paths.opencodeConfigPath, "utf8");
|
|
4285
4439
|
} catch (error) {
|
|
4286
4440
|
if (error.code !== "ENOENT") throw error;
|
|
4287
4441
|
}
|
|
@@ -4312,7 +4466,7 @@ async function reconnectWorker(paths) {
|
|
|
4312
4466
|
const manifest = await fetchManifest(credentials);
|
|
4313
4467
|
const config = mergeWorkerOpenCodeConfig(existing, manifest, forgeBaseUrl, forgeApiKey, { automaticWorkPackSync: true, localConfig: true });
|
|
4314
4468
|
const content = JSON.stringify(config, null, 2) + "\n";
|
|
4315
|
-
if (current !== void 0) await writeOwnerOnlyTextAtomic(
|
|
4469
|
+
if (current !== void 0) await writeOwnerOnlyTextAtomic(path5.join(paths.forgeDir, `opencode-before-reconnect-${randomUUID5()}.jsonc`), current);
|
|
4316
4470
|
const files = [
|
|
4317
4471
|
{ id: "opencode-config", path: paths.opencodeConfigPath, bytes: Buffer.from(content), mode: 384, ownerOnly: true },
|
|
4318
4472
|
{ id: "credentials", path: paths.credentialsPath, bytes: Buffer.from(JSON.stringify(credentials) + "\n"), mode: 384, ownerOnly: true },
|
|
@@ -4326,10 +4480,10 @@ async function reconnectWorker(paths) {
|
|
|
4326
4480
|
ownerOnly: true
|
|
4327
4481
|
});
|
|
4328
4482
|
for (const file of shellCredentialFiles(paths, forgeApiKey)) {
|
|
4329
|
-
if (await fileExists(file.path)) files.push({ id:
|
|
4483
|
+
if (await fileExists(file.path)) files.push({ id: path5.basename(file.path), ...file, mode: 384, ownerOnly: true });
|
|
4330
4484
|
}
|
|
4331
4485
|
const digest = coreRuntimeManifestDigest(files);
|
|
4332
|
-
await
|
|
4486
|
+
await apply({ version: 1, id: `reconnect:${digest.slice(0, 32)}`, digest, files });
|
|
4333
4487
|
} catch {
|
|
4334
4488
|
throw new LocalConfigRecoveryError("A nova chave foi emitida, mas a configura\xE7\xE3o n\xE3o ficou verificada. Pe\xE7a ao operador para revogar essa chave correlacionada antes de tentar outra inscri\xE7\xE3o.");
|
|
4335
4489
|
}
|
|
@@ -4340,10 +4494,10 @@ async function fileExists(filePath) {
|
|
|
4340
4494
|
|
|
4341
4495
|
// packages/cli/src/paths.ts
|
|
4342
4496
|
import os3 from "node:os";
|
|
4343
|
-
import
|
|
4497
|
+
import path6 from "node:path";
|
|
4344
4498
|
import { existsSync } from "node:fs";
|
|
4345
4499
|
function resolvePaths(home = os3.homedir(), platform = process.platform, env = process.env, exists2 = existsSync) {
|
|
4346
|
-
const pathApi = platform === "win32" ?
|
|
4500
|
+
const pathApi = platform === "win32" ? path6.win32 : path6.posix;
|
|
4347
4501
|
const userHome = platform === "win32" ? env.USERPROFILE ?? home : home;
|
|
4348
4502
|
const forgeConfigRoot = env.XDG_CONFIG_HOME ?? pathApi.join(userHome, ".config");
|
|
4349
4503
|
const forgeDir = pathApi.join(forgeConfigRoot, "forge");
|