minimal-forge-work-pack-sync 0.1.51 → 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 +413 -267
- package/dist/repair.js +223 -69
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7615,7 +7615,7 @@ var WORKER_FEEDBACK_MAX_MESSAGE_LENGTH = 24e3;
|
|
|
7615
7615
|
// packages/shared/src/contracts/settings.ts
|
|
7616
7616
|
var DOCUMENT_READER_AGENT = "document-reader";
|
|
7617
7617
|
var OPENROUTER_ALIAS_PROVIDER_NAME = "OpenRouter (via Forge)";
|
|
7618
|
-
var WORK_PACK_SYNC_PLUGIN_VERSION = "0.1.
|
|
7618
|
+
var WORK_PACK_SYNC_PLUGIN_VERSION = "0.1.53";
|
|
7619
7619
|
var WORK_PACK_SYNC_PLUGIN_PACKAGE = `minimal-forge-work-pack-sync@${WORK_PACK_SYNC_PLUGIN_VERSION}`;
|
|
7620
7620
|
var WORK_PACK_SYNC_PLUGIN_FILENAME = `minimal-forge-work-pack-sync-${WORK_PACK_SYNC_PLUGIN_VERSION}.tgz`;
|
|
7621
7621
|
var FIRECRAWL_KEYLESS_MCP_NAME = "firecrawl";
|
|
@@ -8069,7 +8069,7 @@ async function writeOwnerOnlyTextAtomic(filePath, content, options = {}) {
|
|
|
8069
8069
|
}
|
|
8070
8070
|
|
|
8071
8071
|
// packages/cli/src/workPackSyncPlugin.ts
|
|
8072
|
-
import { createHash as createHash7, randomUUID as
|
|
8072
|
+
import { createHash as createHash7, randomUUID as randomUUID5 } from "node:crypto";
|
|
8073
8073
|
import { pathToFileURL } from "node:url";
|
|
8074
8074
|
|
|
8075
8075
|
// packages/cli/src/skillUsageReporter.ts
|
|
@@ -21140,7 +21140,7 @@ function isRecord2(value) {
|
|
|
21140
21140
|
}
|
|
21141
21141
|
|
|
21142
21142
|
// packages/cli/src/workPackSyncPlugin.ts
|
|
21143
|
-
import { link, mkdir as
|
|
21143
|
+
import { link, mkdir as mkdir8, mkdtemp as mkdtemp2, readdir as readdir7, readFile as readFile14, realpath as realpath2, rename as rename9, rm as rm6, rmdir as rmdir2, stat as stat3, unlink as unlink2, writeFile as writeFile9 } from "node:fs/promises";
|
|
21144
21144
|
import os5 from "node:os";
|
|
21145
21145
|
import path14 from "node:path";
|
|
21146
21146
|
|
|
@@ -21591,9 +21591,9 @@ function assertSlug(slug) {
|
|
|
21591
21591
|
// packages/cli/src/sync.ts
|
|
21592
21592
|
import { createHash as createHash4 } from "node:crypto";
|
|
21593
21593
|
import { spawnSync } from "node:child_process";
|
|
21594
|
-
import { chmod as chmod4, cp, lstat as lstat5, mkdtemp, mkdir as
|
|
21594
|
+
import { chmod as chmod4, cp, lstat as lstat5, mkdtemp, mkdir as mkdir6, readdir as readdir5, readFile as readFile9, rm as rm4, rename as rename7, writeFile as writeFile7 } from "node:fs/promises";
|
|
21595
21595
|
import os4 from "node:os";
|
|
21596
|
-
import
|
|
21596
|
+
import path10 from "node:path";
|
|
21597
21597
|
|
|
21598
21598
|
// node_modules/tar/dist/esm/index.min.js
|
|
21599
21599
|
import Qr from "events";
|
|
@@ -24573,9 +24573,98 @@ function renderTuiConfig(manifest) {
|
|
|
24573
24573
|
}
|
|
24574
24574
|
|
|
24575
24575
|
// packages/cli/src/coreSyncTransaction.ts
|
|
24576
|
-
import { createHash as createHash3, randomUUID as
|
|
24577
|
-
import { chmod as chmod3, lstat as lstat4, mkdir as
|
|
24576
|
+
import { createHash as createHash3, randomUUID as randomUUID4 } from "node:crypto";
|
|
24577
|
+
import { chmod as chmod3, lstat as lstat4, mkdir as mkdir5, readFile as readFile6, rename as rename6, rm as rm3, writeFile as writeFile6 } from "node:fs/promises";
|
|
24578
|
+
import path8 from "node:path";
|
|
24579
|
+
|
|
24580
|
+
// packages/cli/src/coreSyncLock.ts
|
|
24581
|
+
import { randomUUID as randomUUID3 } from "node:crypto";
|
|
24582
|
+
import { mkdir as mkdir4, readdir as readdir4, readFile as readFile5, rename as rename5, rmdir, unlink, writeFile as writeFile5 } from "node:fs/promises";
|
|
24583
|
+
import { hostname as hostname3 } from "node:os";
|
|
24578
24584
|
import path7 from "node:path";
|
|
24585
|
+
function hasCode(error45, ...codes) {
|
|
24586
|
+
return error45 instanceof Error && codes.includes(error45.code ?? "");
|
|
24587
|
+
}
|
|
24588
|
+
function busy() {
|
|
24589
|
+
return new Error("Outra sincroniza\xE7\xE3o Forge pode estar em curso. Aguarde que termine e volte a tentar.");
|
|
24590
|
+
}
|
|
24591
|
+
function unknownOwner() {
|
|
24592
|
+
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.");
|
|
24593
|
+
}
|
|
24594
|
+
async function removeEmpty(directory) {
|
|
24595
|
+
try {
|
|
24596
|
+
await rmdir(directory);
|
|
24597
|
+
} catch (error45) {
|
|
24598
|
+
if (!hasCode(error45, "ENOENT", "EEXIST", "ENOTEMPTY")) throw error45;
|
|
24599
|
+
}
|
|
24600
|
+
}
|
|
24601
|
+
async function reclaim(lockPath, renameError) {
|
|
24602
|
+
let entries;
|
|
24603
|
+
try {
|
|
24604
|
+
entries = await readdir4(lockPath);
|
|
24605
|
+
} catch (error45) {
|
|
24606
|
+
if (!hasCode(error45, "ENOENT")) throw error45;
|
|
24607
|
+
if (hasCode(renameError, "EACCES", "EPERM")) throw renameError;
|
|
24608
|
+
return;
|
|
24609
|
+
}
|
|
24610
|
+
if (entries.length) {
|
|
24611
|
+
const marker = entries[0];
|
|
24612
|
+
const match = /^owner-([1-9]\d*)-[0-9a-f-]{36}$/.exec(marker);
|
|
24613
|
+
if (entries.length !== 1 || !match) throw unknownOwner();
|
|
24614
|
+
const markerPath = path7.join(lockPath, marker);
|
|
24615
|
+
let host;
|
|
24616
|
+
try {
|
|
24617
|
+
host = await readFile5(markerPath, "utf8");
|
|
24618
|
+
} catch (error45) {
|
|
24619
|
+
if (hasCode(error45, "ENOENT")) return;
|
|
24620
|
+
throw error45;
|
|
24621
|
+
}
|
|
24622
|
+
if (host !== hostname3()) throw unknownOwner();
|
|
24623
|
+
try {
|
|
24624
|
+
process.kill(Number(match[1]), 0);
|
|
24625
|
+
} catch (error45) {
|
|
24626
|
+
if (!hasCode(error45, "ESRCH")) throw error45;
|
|
24627
|
+
await unlink(markerPath).catch((cause) => {
|
|
24628
|
+
if (!hasCode(cause, "ENOENT")) throw cause;
|
|
24629
|
+
});
|
|
24630
|
+
await removeEmpty(lockPath);
|
|
24631
|
+
return;
|
|
24632
|
+
}
|
|
24633
|
+
throw busy();
|
|
24634
|
+
}
|
|
24635
|
+
await removeEmpty(lockPath);
|
|
24636
|
+
}
|
|
24637
|
+
async function withCoreSyncLock(journalPath, operation) {
|
|
24638
|
+
const lockPath = journalPath + ".lock";
|
|
24639
|
+
const id = randomUUID3();
|
|
24640
|
+
const candidate = lockPath + "-" + id;
|
|
24641
|
+
const marker = `owner-${process.pid}-${id}`;
|
|
24642
|
+
await mkdir4(path7.dirname(lockPath), { recursive: true });
|
|
24643
|
+
await mkdir4(candidate, { mode: 448 });
|
|
24644
|
+
let directory = candidate;
|
|
24645
|
+
try {
|
|
24646
|
+
await writeFile5(path7.join(candidate, marker), hostname3(), { mode: 384, flag: "wx" });
|
|
24647
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
24648
|
+
try {
|
|
24649
|
+
await rename5(candidate, lockPath);
|
|
24650
|
+
} catch (error45) {
|
|
24651
|
+
if (!hasCode(error45, "EEXIST", "ENOTEMPTY", "EPERM", "EACCES")) throw error45;
|
|
24652
|
+
await reclaim(lockPath, error45);
|
|
24653
|
+
continue;
|
|
24654
|
+
}
|
|
24655
|
+
directory = lockPath;
|
|
24656
|
+
return await operation();
|
|
24657
|
+
}
|
|
24658
|
+
throw busy();
|
|
24659
|
+
} finally {
|
|
24660
|
+
await unlink(path7.join(directory, marker)).catch((error45) => {
|
|
24661
|
+
if (!hasCode(error45, "ENOENT")) throw error45;
|
|
24662
|
+
});
|
|
24663
|
+
await removeEmpty(directory);
|
|
24664
|
+
}
|
|
24665
|
+
}
|
|
24666
|
+
|
|
24667
|
+
// packages/cli/src/coreSyncTransaction.ts
|
|
24579
24668
|
var CORE_RUNTIME_MANIFEST_VERSION = 1;
|
|
24580
24669
|
var CoreSyncUncertainError = class extends Error {
|
|
24581
24670
|
constructor(message, paths) {
|
|
@@ -24584,11 +24673,11 @@ var CoreSyncUncertainError = class extends Error {
|
|
|
24584
24673
|
}
|
|
24585
24674
|
paths;
|
|
24586
24675
|
};
|
|
24587
|
-
var defaultFs = { chmod: chmod3, lstat: lstat4, mkdir:
|
|
24676
|
+
var defaultFs = { chmod: chmod3, lstat: lstat4, mkdir: mkdir5, readFile: readFile6, rename: rename6, rm: rm3, writeFile: writeFile6 };
|
|
24588
24677
|
function coreRuntimeManifestDigest(files) {
|
|
24589
24678
|
const canonical = [...files].sort((a, b2) => a.id.localeCompare(b2.id)).map((file2) => ({
|
|
24590
24679
|
id: file2.id,
|
|
24591
|
-
path:
|
|
24680
|
+
path: path8.resolve(file2.path),
|
|
24592
24681
|
exists: file2.bytes !== null,
|
|
24593
24682
|
digest: file2.bytes === null ? void 0 : sha256(file2.bytes),
|
|
24594
24683
|
mode: file2.mode,
|
|
@@ -24605,8 +24694,8 @@ function validateCoreRuntimeManifest(manifest) {
|
|
|
24605
24694
|
const targets = /* @__PURE__ */ new Set();
|
|
24606
24695
|
for (const file2 of manifest.files) {
|
|
24607
24696
|
if (!file2.id || ids.has(file2.id)) throw new Error(`Duplicate core runtime file id: ${file2.id}`);
|
|
24608
|
-
const target =
|
|
24609
|
-
if (!
|
|
24697
|
+
const target = path8.resolve(file2.path);
|
|
24698
|
+
if (!path8.isAbsolute(file2.path) || targets.has(target)) throw new Error(`Invalid or duplicate core runtime target: ${file2.path}`);
|
|
24610
24699
|
if (file2.mode !== void 0 && (!Number.isInteger(file2.mode) || file2.mode < 0 || file2.mode > 4095)) throw new Error(`Invalid mode for ${file2.id}`);
|
|
24611
24700
|
if (file2.ownerOnly !== void 0 && typeof file2.ownerOnly !== "boolean") throw new Error(`Invalid owner-only flag for ${file2.id}`);
|
|
24612
24701
|
ids.add(file2.id);
|
|
@@ -24614,19 +24703,28 @@ function validateCoreRuntimeManifest(manifest) {
|
|
|
24614
24703
|
}
|
|
24615
24704
|
if (coreRuntimeManifestDigest(manifest.files) !== manifest.digest) throw new Error("Core runtime manifest digest mismatch");
|
|
24616
24705
|
}
|
|
24617
|
-
async function
|
|
24618
|
-
|
|
24706
|
+
async function withRecoveredCoreSync(journalPath, operation, options = {}) {
|
|
24707
|
+
const fs2 = options.fs ?? defaultFs;
|
|
24708
|
+
return withCoreSyncLock(journalPath, async () => {
|
|
24709
|
+
const recovery = await recoverUnderLock(journalPath, fs2);
|
|
24710
|
+
await options.afterRecovery?.();
|
|
24711
|
+
if (recovery.status === "uncertain") throw new CoreSyncUncertainError(recovery.message, recovery.uncertainPaths);
|
|
24712
|
+
return operation((manifest, hooks) => {
|
|
24713
|
+
validateCoreRuntimeManifest(manifest);
|
|
24714
|
+
return commitCoreFiles({ manifest, journalPath, fs: fs2, hooks });
|
|
24715
|
+
});
|
|
24716
|
+
});
|
|
24717
|
+
}
|
|
24718
|
+
async function commitCoreFiles(options) {
|
|
24619
24719
|
const fs2 = options.fs ?? defaultFs;
|
|
24620
24720
|
const hooks = options.hooks;
|
|
24621
|
-
const
|
|
24622
|
-
if (existingRecovery.status === "uncertain") throw new CoreSyncUncertainError(existingRecovery.message, existingRecovery.uncertainPaths);
|
|
24623
|
-
const transactionId = randomUUID3();
|
|
24721
|
+
const transactionId = randomUUID4();
|
|
24624
24722
|
const files = [];
|
|
24625
24723
|
try {
|
|
24626
24724
|
for (const desired of options.manifest.files) {
|
|
24627
|
-
const target =
|
|
24725
|
+
const target = path8.resolve(desired.path);
|
|
24628
24726
|
if (desired.ownerOnly && desired.bytes !== null && fs2 === defaultFs) await prepareOwnerOnlyFileDestination(target);
|
|
24629
|
-
else await fs2.mkdir(
|
|
24727
|
+
else await fs2.mkdir(path8.dirname(target), { recursive: true });
|
|
24630
24728
|
const original = await capture(target, fs2);
|
|
24631
24729
|
const suffix = `.forge-core-${transactionId}`;
|
|
24632
24730
|
const backup = original.exists ? `${target}${suffix}.backup` : void 0;
|
|
@@ -24655,58 +24753,93 @@ async function applyCoreSyncTransaction(options) {
|
|
|
24655
24753
|
await hooks?.boundary?.("after-journal");
|
|
24656
24754
|
for (const file2 of files) {
|
|
24657
24755
|
await hooks?.boundary?.(`before-commit:${file2.id}`);
|
|
24658
|
-
await fs2.rm(file2.target, { force: true });
|
|
24659
24756
|
if (file2.stage) await fs2.rename(file2.stage, file2.target);
|
|
24757
|
+
else await fs2.rm(file2.target, { force: true });
|
|
24660
24758
|
if (file2.stage && file2.ownerOnly && fs2 === defaultFs) await protectOwnerOnlyPath(file2.target, "file");
|
|
24661
24759
|
await hooks?.boundary?.(`after-commit:${file2.id}`);
|
|
24662
24760
|
}
|
|
24663
24761
|
await hooks?.boundary?.("before-cleanup");
|
|
24664
|
-
await
|
|
24665
|
-
await hooks?.boundary?.("after-cleanup");
|
|
24666
|
-
return { status: "committed", manifestId: options.manifest.id, digest: options.manifest.digest };
|
|
24762
|
+
await fs2.rename(options.journalPath, cleanupPath(options.journalPath));
|
|
24667
24763
|
} catch (error45) {
|
|
24668
24764
|
if (!await exists(options.journalPath, fs2)) {
|
|
24669
24765
|
await Promise.all(files.flatMap((file2) => [file2.stage, file2.backup].filter((item) => Boolean(item))).map((item) => fs2.rm(item, { force: true }).catch(() => void 0)));
|
|
24670
24766
|
throw error45;
|
|
24671
24767
|
}
|
|
24672
|
-
const recovery = await
|
|
24768
|
+
const recovery = await recoverUnderLock(options.journalPath, fs2);
|
|
24673
24769
|
if (recovery.status === "uncertain") throw new CoreSyncUncertainError(recovery.message, recovery.uncertainPaths);
|
|
24674
24770
|
throw error45;
|
|
24675
24771
|
}
|
|
24676
|
-
}
|
|
24677
|
-
async function recoverCoreSyncTransaction(options) {
|
|
24678
|
-
const fs2 = options.fs ?? defaultFs;
|
|
24679
|
-
let journal;
|
|
24772
|
+
const committed = { status: "committed", manifestId: options.manifest.id, digest: options.manifest.digest };
|
|
24680
24773
|
try {
|
|
24681
|
-
|
|
24682
|
-
} catch
|
|
24683
|
-
|
|
24684
|
-
|
|
24774
|
+
await cleanup(files, cleanupPath(options.journalPath), fs2);
|
|
24775
|
+
} catch {
|
|
24776
|
+
return committed;
|
|
24777
|
+
}
|
|
24778
|
+
await hooks?.boundary?.("after-cleanup");
|
|
24779
|
+
return committed;
|
|
24780
|
+
}
|
|
24781
|
+
async function recoverUnderLock(activePath, fs2) {
|
|
24782
|
+
const retiredPath = cleanupPath(activePath);
|
|
24783
|
+
for (const journalPath of [retiredPath, activePath]) {
|
|
24784
|
+
let journal;
|
|
24785
|
+
try {
|
|
24786
|
+
journal = JSON.parse(await fs2.readFile(journalPath, "utf8"));
|
|
24787
|
+
if (!validJournal(journal)) throw new Error("Invalid journal");
|
|
24788
|
+
} catch (error45) {
|
|
24789
|
+
if (isNotFound(error45)) continue;
|
|
24790
|
+
return { status: "uncertain", uncertainPaths: [journalPath], message: "Core sync journal is unreadable or invalid; exact recovery is uncertain." };
|
|
24791
|
+
}
|
|
24792
|
+
if (journalPath === retiredPath) {
|
|
24793
|
+
await cleanup(journal.files, retiredPath, fs2);
|
|
24794
|
+
} else {
|
|
24795
|
+
return restoreJournal(journal, journalPath, fs2);
|
|
24796
|
+
}
|
|
24685
24797
|
}
|
|
24686
|
-
|
|
24798
|
+
return { status: "nothing-to-recover" };
|
|
24799
|
+
}
|
|
24800
|
+
async function restoreJournal(journal, journalPath, fs2) {
|
|
24687
24801
|
const uncertain = [];
|
|
24802
|
+
const backups = /* @__PURE__ */ new Map();
|
|
24688
24803
|
for (const file2 of journal.files) {
|
|
24804
|
+
if (!file2.original.exists) continue;
|
|
24689
24805
|
try {
|
|
24690
|
-
if (!file2.
|
|
24691
|
-
|
|
24692
|
-
|
|
24693
|
-
|
|
24694
|
-
|
|
24695
|
-
|
|
24806
|
+
if (!file2.backup) throw new Error("backup missing");
|
|
24807
|
+
const bytes = await fs2.readFile(file2.backup);
|
|
24808
|
+
if (sha256(bytes) !== file2.original.digest) throw new Error("backup digest mismatch");
|
|
24809
|
+
backups.set(file2, bytes);
|
|
24810
|
+
} catch {
|
|
24811
|
+
uncertain.push(file2.target);
|
|
24812
|
+
}
|
|
24813
|
+
}
|
|
24814
|
+
if (uncertain.length) return uncertainRestore(journal, uncertain);
|
|
24815
|
+
for (const file2 of journal.files) {
|
|
24816
|
+
try {
|
|
24817
|
+
if (!file2.original.exists) {
|
|
24696
24818
|
await fs2.rm(file2.target, { force: true });
|
|
24697
|
-
|
|
24698
|
-
|
|
24699
|
-
|
|
24700
|
-
|
|
24819
|
+
continue;
|
|
24820
|
+
}
|
|
24821
|
+
const restorePath = `${file2.target}.forge-restore-${journal.transactionId}`;
|
|
24822
|
+
await fs2.rm(restorePath, { force: true });
|
|
24823
|
+
await writeExact(restorePath, backups.get(file2), file2.original.mode, fs2, file2.ownerOnly);
|
|
24824
|
+
await fs2.rename(restorePath, file2.target);
|
|
24825
|
+
if (file2.ownerOnly && fs2 === defaultFs) await protectOwnerOnlyPath(file2.target, "file");
|
|
24826
|
+
const restored = await capture(file2.target, fs2);
|
|
24827
|
+
if (!restored.bytes || sha256(restored.bytes) !== file2.original.digest || restored.mode !== effectiveMode(file2.original.mode, file2.ownerOnly)) {
|
|
24828
|
+
throw new Error("restore verification failed");
|
|
24701
24829
|
}
|
|
24702
24830
|
} catch {
|
|
24703
24831
|
uncertain.push(file2.target);
|
|
24704
24832
|
}
|
|
24705
24833
|
}
|
|
24706
|
-
if (uncertain.length
|
|
24707
|
-
|
|
24834
|
+
if (uncertain.length) return uncertainRestore(journal, uncertain);
|
|
24835
|
+
const retiredPath = cleanupPath(journalPath);
|
|
24836
|
+
await fs2.rename(journalPath, retiredPath);
|
|
24837
|
+
await cleanup(journal.files, retiredPath, fs2);
|
|
24708
24838
|
return { status: "restored", manifestId: journal.manifest.id };
|
|
24709
24839
|
}
|
|
24840
|
+
function uncertainRestore(journal, paths) {
|
|
24841
|
+
return { status: "uncertain", manifestId: journal.manifest.id, uncertainPaths: paths, message: "Core sync could not prove an exact restore for every path." };
|
|
24842
|
+
}
|
|
24710
24843
|
async function capture(target, fs2) {
|
|
24711
24844
|
try {
|
|
24712
24845
|
const info = await fs2.lstat(target);
|
|
@@ -24717,13 +24850,17 @@ async function capture(target, fs2) {
|
|
|
24717
24850
|
throw error45;
|
|
24718
24851
|
}
|
|
24719
24852
|
}
|
|
24853
|
+
function effectiveMode(mode, ownerOnly = false) {
|
|
24854
|
+
return ownerOnly && process.platform !== "win32" ? 384 : mode;
|
|
24855
|
+
}
|
|
24720
24856
|
async function writeExact(target, bytes, mode, fs2, ownerOnly = false) {
|
|
24721
|
-
|
|
24722
|
-
await fs2.
|
|
24857
|
+
const permissions = effectiveMode(mode, ownerOnly);
|
|
24858
|
+
await fs2.writeFile(target, bytes, { flag: "wx", mode: permissions });
|
|
24859
|
+
await fs2.chmod(target, permissions);
|
|
24723
24860
|
if (ownerOnly && fs2 === defaultFs) await protectOwnerOnlyPath(target, "file");
|
|
24724
24861
|
}
|
|
24725
24862
|
async function writeJournal(target, journal, fs2) {
|
|
24726
|
-
await fs2.mkdir(
|
|
24863
|
+
await fs2.mkdir(path8.dirname(target), { recursive: true });
|
|
24727
24864
|
const temporary = `${target}.next`;
|
|
24728
24865
|
await fs2.rm(temporary, { force: true });
|
|
24729
24866
|
await fs2.writeFile(temporary, `${JSON.stringify(journal, null, 2)}
|
|
@@ -24731,8 +24868,14 @@ async function writeJournal(target, journal, fs2) {
|
|
|
24731
24868
|
await fs2.chmod(temporary, 384);
|
|
24732
24869
|
await fs2.rename(temporary, target);
|
|
24733
24870
|
}
|
|
24734
|
-
|
|
24735
|
-
|
|
24871
|
+
function cleanupPath(journalPath) {
|
|
24872
|
+
return `${journalPath}.cleanup`;
|
|
24873
|
+
}
|
|
24874
|
+
async function cleanup(files, journalPath, fs2) {
|
|
24875
|
+
for (const file2 of files) {
|
|
24876
|
+
if (file2.stage) await fs2.rm(file2.stage, { force: true });
|
|
24877
|
+
if (file2.backup) await fs2.rm(file2.backup, { force: true });
|
|
24878
|
+
}
|
|
24736
24879
|
await fs2.rm(journalPath, { force: true });
|
|
24737
24880
|
}
|
|
24738
24881
|
async function exists(target, fs2) {
|
|
@@ -24751,7 +24894,12 @@ function sha256(value) {
|
|
|
24751
24894
|
return createHash3("sha256").update(value).digest("hex");
|
|
24752
24895
|
}
|
|
24753
24896
|
function validJournal(value) {
|
|
24754
|
-
|
|
24897
|
+
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;
|
|
24898
|
+
return value.files.every((file2) => {
|
|
24899
|
+
if (typeof file2?.id !== "string" || typeof file2.target !== "string" || !path8.isAbsolute(file2.target)) return false;
|
|
24900
|
+
const prefix = `${file2.target}.forge-core-${value.transactionId}`;
|
|
24901
|
+
return (file2.stage === void 0 || file2.stage === `${prefix}.stage`) && (file2.backup === void 0 || file2.backup === `${prefix}.backup`) && typeof file2.original?.exists === "boolean" && typeof file2.desired?.exists === "boolean" && (file2.ownerOnly === void 0 || typeof file2.ownerOnly === "boolean");
|
|
24902
|
+
});
|
|
24755
24903
|
}
|
|
24756
24904
|
|
|
24757
24905
|
// packages/cli/src/jsonc.ts
|
|
@@ -24830,7 +24978,7 @@ function stripTrailingCommas(input) {
|
|
|
24830
24978
|
}
|
|
24831
24979
|
|
|
24832
24980
|
// packages/cli/src/remoteConfig.ts
|
|
24833
|
-
import { readFile as
|
|
24981
|
+
import { readFile as readFile7 } from "node:fs/promises";
|
|
24834
24982
|
var WELLKNOWN_ENV_KEY = "FORGE_API_KEY";
|
|
24835
24983
|
async function removeWellKnownAuth(paths, credentials, previous) {
|
|
24836
24984
|
const content = await prepareWellKnownAuthRemoval(paths, credentials, previous);
|
|
@@ -24888,7 +25036,7 @@ function normalizeBaseUrl(value) {
|
|
|
24888
25036
|
async function readAuthJson(paths) {
|
|
24889
25037
|
let raw;
|
|
24890
25038
|
try {
|
|
24891
|
-
raw = await
|
|
25039
|
+
raw = await readFile7(paths.opencodeAuthPath, "utf8");
|
|
24892
25040
|
} catch (error45) {
|
|
24893
25041
|
if (error45.code === "ENOENT") return {};
|
|
24894
25042
|
throw error45;
|
|
@@ -24902,21 +25050,21 @@ function isRecord3(value) {
|
|
|
24902
25050
|
}
|
|
24903
25051
|
|
|
24904
25052
|
// packages/cli/src/localConfigRecovery.ts
|
|
24905
|
-
import { readFile as
|
|
24906
|
-
import
|
|
25053
|
+
import { readFile as readFile8 } from "node:fs/promises";
|
|
25054
|
+
import path9 from "node:path";
|
|
24907
25055
|
var LocalConfigRecoveryError = class extends Error {
|
|
24908
25056
|
};
|
|
24909
25057
|
var LAST_GOOD_FILENAME = "last-good-opencode.json";
|
|
24910
25058
|
function lastGoodLocalConfigFile(paths, credentials, content) {
|
|
24911
25059
|
assertForgeConfig(content, credentials);
|
|
24912
|
-
return { id: "last-good-config", path:
|
|
25060
|
+
return { id: "last-good-config", path: path9.join(paths.forgeDir, LAST_GOOD_FILENAME), bytes: Buffer.from(content), mode: 384, ownerOnly: true };
|
|
24913
25061
|
}
|
|
24914
25062
|
async function retiredLastGoodLocalConfigFile(paths) {
|
|
24915
|
-
const filePath =
|
|
25063
|
+
const filePath = path9.join(paths.forgeDir, LAST_GOOD_FILENAME);
|
|
24916
25064
|
return await readOptional(filePath) === void 0 ? void 0 : { id: "last-good-config", path: filePath, bytes: null };
|
|
24917
25065
|
}
|
|
24918
25066
|
async function inspectLocalConfigRecovery(paths, credentials) {
|
|
24919
|
-
const snapshot = await readOptional(
|
|
25067
|
+
const snapshot = await readOptional(path9.join(paths.forgeDir, LAST_GOOD_FILENAME));
|
|
24920
25068
|
const current = await readOptional(paths.opencodeConfigPath);
|
|
24921
25069
|
return {
|
|
24922
25070
|
snapshot: snapshot === void 0 ? "missing" : isForgeConfig(snapshot, credentials) ? "available" : "invalid",
|
|
@@ -24959,7 +25107,7 @@ function forgePluginEntries(config2) {
|
|
|
24959
25107
|
}
|
|
24960
25108
|
async function readOptional(filePath) {
|
|
24961
25109
|
try {
|
|
24962
|
-
return await
|
|
25110
|
+
return await readFile8(filePath, "utf8");
|
|
24963
25111
|
} catch (error45) {
|
|
24964
25112
|
if (error45.code === "ENOENT") return void 0;
|
|
24965
25113
|
throw error45;
|
|
@@ -25235,24 +25383,24 @@ function validCoreManifest(value) {
|
|
|
25235
25383
|
return modelIds.every((id) => typeof id === "string" && id.length > 0) && modelIds.includes(config2.model) && modelIds.includes(config2.small_model);
|
|
25236
25384
|
}
|
|
25237
25385
|
async function applyManifest(paths, credentials, manifest, options = {}) {
|
|
25238
|
-
const journalPath =
|
|
25239
|
-
const recovery = await recoverCoreSyncTransaction({ journalPath });
|
|
25240
|
-
await retireDebugCapture(paths);
|
|
25241
|
-
if (recovery.status === "uncertain") {
|
|
25242
|
-
throw new CoreSyncUncertainError(recovery.message, recovery.uncertainPaths);
|
|
25243
|
-
}
|
|
25244
|
-
await mkdir5(paths.opencodeDir, { recursive: true });
|
|
25386
|
+
const journalPath = path10.join(paths.forgeDir, "core-sync-journal.json");
|
|
25245
25387
|
return reconcileSyncSnapshot({
|
|
25246
|
-
applyCore: () =>
|
|
25388
|
+
applyCore: () => withRecoveredCoreSync(
|
|
25389
|
+
journalPath,
|
|
25390
|
+
(apply) => applyCoreManifest(paths, credentials, manifest, options, apply),
|
|
25391
|
+
{ afterRecovery: () => retireDebugCapture(paths) }
|
|
25392
|
+
),
|
|
25247
25393
|
applyWorkPacks: () => applyWorkPackManifest(paths, credentials, manifest, options)
|
|
25248
25394
|
});
|
|
25249
25395
|
}
|
|
25250
|
-
async function applyCoreManifest(paths, credentials, manifest, options) {
|
|
25251
|
-
|
|
25252
|
-
|
|
25253
|
-
const
|
|
25254
|
-
const
|
|
25255
|
-
const
|
|
25396
|
+
async function applyCoreManifest(paths, credentials, manifest, options, apply) {
|
|
25397
|
+
await options.beforeCore?.();
|
|
25398
|
+
await mkdir6(paths.opencodeDir, { recursive: true });
|
|
25399
|
+
const attachmentPluginPath = path10.join(paths.pluginDir, FORGE_PLUGIN_FILENAME);
|
|
25400
|
+
const evidencePluginPath = path10.join(paths.pluginDir, FORGE_EVIDENCE_PLUGIN_FILENAME);
|
|
25401
|
+
const debugTracePluginPath = path10.join(paths.pluginDir, FORGE_DEBUG_TRACE_PLUGIN_FILENAME);
|
|
25402
|
+
const legacyAttachmentPluginPath = path10.join(paths.opencodeDir, "plugin", FORGE_PLUGIN_FILENAME);
|
|
25403
|
+
const debugTraceConfigPath = path10.join(paths.forgeDir, FORGE_DEBUG_TRACE_CONFIG_FILENAME);
|
|
25256
25404
|
const rules = attachmentBridgeRules(manifest.config.models);
|
|
25257
25405
|
let bridgeEnabled = Boolean(manifest.config.documentReaderModel) && Object.keys(rules).length > 0;
|
|
25258
25406
|
const existingConfig = await readJsoncObject(paths.opencodeConfigPath);
|
|
@@ -25324,28 +25472,25 @@ async function applyCoreManifest(paths, credentials, manifest, options) {
|
|
|
25324
25472
|
if (retired) files.push(retired);
|
|
25325
25473
|
}
|
|
25326
25474
|
const digest2 = coreRuntimeManifestDigest(files);
|
|
25327
|
-
await
|
|
25328
|
-
|
|
25329
|
-
|
|
25330
|
-
|
|
25331
|
-
|
|
25332
|
-
|
|
25333
|
-
},
|
|
25334
|
-
journalPath: path9.join(paths.forgeDir, "core-sync-journal.json"),
|
|
25335
|
-
hooks: options.coreTransactionHooks
|
|
25336
|
-
});
|
|
25475
|
+
await apply({
|
|
25476
|
+
version: 1,
|
|
25477
|
+
id: `derived:${digest2.slice(0, 32)}`,
|
|
25478
|
+
digest: digest2,
|
|
25479
|
+
files
|
|
25480
|
+
}, options.coreTransactionHooks);
|
|
25337
25481
|
await removeLegacyDebugTraceRuntime(paths);
|
|
25482
|
+
await options.afterCore?.();
|
|
25338
25483
|
return { configPath: paths.opencodeConfigPath, tuiPath: paths.tuiConfigPath };
|
|
25339
25484
|
}
|
|
25340
25485
|
async function applyWorkPackManifest(paths, credentials, manifest, options) {
|
|
25341
|
-
await
|
|
25342
|
-
await
|
|
25486
|
+
await mkdir6(paths.skillsDir, { recursive: true });
|
|
25487
|
+
await mkdir6(paths.commandsDir, { recursive: true });
|
|
25343
25488
|
const recovery = await recoverInterruptedWorkPackInstalls(paths, options.cleanupWorkPackBackup, options.workPackProjectionBoundary);
|
|
25344
25489
|
const managed = await Promise.all((await managedWorkPackSlugs(paths.skillsDir)).map(async (slug) => ({
|
|
25345
25490
|
slug,
|
|
25346
|
-
version: (await
|
|
25347
|
-
agents: await readAgentRecord(
|
|
25348
|
-
commands: await readCommandRecord(
|
|
25491
|
+
version: (await readFile9(path10.join(paths.skillsDir, slug, ".forge-version"), "utf8")).trim(),
|
|
25492
|
+
agents: await readAgentRecord(path10.join(paths.skillsDir, slug)),
|
|
25493
|
+
commands: await readCommandRecord(path10.join(paths.skillsDir, slug))
|
|
25349
25494
|
})));
|
|
25350
25495
|
const desired = manifest.skills.map((skill) => ({
|
|
25351
25496
|
slug: skill.slug,
|
|
@@ -25369,7 +25514,7 @@ async function applyWorkPackManifest(paths, credentials, manifest, options) {
|
|
|
25369
25514
|
options
|
|
25370
25515
|
)),
|
|
25371
25516
|
recoveryFailures: recovery.failures,
|
|
25372
|
-
writeAtomicReceipt: (receipt) => writeJsonAtomic2(
|
|
25517
|
+
writeAtomicReceipt: (receipt) => writeJsonAtomic2(path10.join(paths.forgeDir, "work-pack-sync-receipt.json"), receipt)
|
|
25373
25518
|
});
|
|
25374
25519
|
const installed = workPacks.outcomes.filter((outcome) => outcome.status === "installed").map((outcome) => `${outcome.slug}@${outcome.version}`);
|
|
25375
25520
|
const removed = workPacks.outcomes.filter((outcome) => outcome.status === "removed").map((outcome) => outcome.slug);
|
|
@@ -25380,7 +25525,7 @@ async function applyWorkPackManifest(paths, credentials, manifest, options) {
|
|
|
25380
25525
|
async function collectWorkPackObservations(paths, reconciliation) {
|
|
25381
25526
|
const installedVersions = /* @__PURE__ */ new Map();
|
|
25382
25527
|
for (const slug of await managedWorkPackSlugs(paths.skillsDir)) {
|
|
25383
|
-
const version2 = (await
|
|
25528
|
+
const version2 = (await readFile9(path10.join(paths.skillsDir, slug, ".forge-version"), "utf8")).trim();
|
|
25384
25529
|
if (version2) installedVersions.set(slug, version2);
|
|
25385
25530
|
}
|
|
25386
25531
|
const observations = new Map(
|
|
@@ -25411,7 +25556,7 @@ async function enrichIntegrityFailure(paths, operation, result) {
|
|
|
25411
25556
|
}
|
|
25412
25557
|
}
|
|
25413
25558
|
async function operateWorkPack(paths, credentials, skillBySlug, approvedModelIds, cleanupPendingSlugs, recoveryFailures, operation, options) {
|
|
25414
|
-
const target =
|
|
25559
|
+
const target = path10.join(paths.skillsDir, operation.pack.slug);
|
|
25415
25560
|
const recoveryFailure = recoveryFailures.get(operation.pack.slug);
|
|
25416
25561
|
if (recoveryFailure) return { status: "failed", restoration: "exact", code: recoveryFailure };
|
|
25417
25562
|
let ownership;
|
|
@@ -25450,7 +25595,7 @@ async function operateWorkPack(paths, credentials, skillBySlug, approvedModelIds
|
|
|
25450
25595
|
let previousManagedPaths;
|
|
25451
25596
|
let archive;
|
|
25452
25597
|
try {
|
|
25453
|
-
const currentVersion = await
|
|
25598
|
+
const currentVersion = await readFile9(path10.join(target, ".forge-version"), "utf8").catch(() => "");
|
|
25454
25599
|
const sameVersion = currentVersion.trim() === skill.version;
|
|
25455
25600
|
if (ownership === "managed") {
|
|
25456
25601
|
let localState = await checkWorkPackInstalledState({
|
|
@@ -25561,8 +25706,8 @@ function jsonBytes(value) {
|
|
|
25561
25706
|
`);
|
|
25562
25707
|
}
|
|
25563
25708
|
async function removeLegacyDebugTraceRuntime(paths) {
|
|
25564
|
-
const launchAgentPath =
|
|
25565
|
-
const launchAgent = await
|
|
25709
|
+
const launchAgentPath = path10.join(os4.homedir(), "Library", "LaunchAgents", "com.minimal-forge.debug-trace.plist");
|
|
25710
|
+
const launchAgent = await readFile9(launchAgentPath, "utf8").catch((error45) => {
|
|
25566
25711
|
if (error45.code === "ENOENT") return void 0;
|
|
25567
25712
|
throw error45;
|
|
25568
25713
|
});
|
|
@@ -25584,13 +25729,13 @@ async function removeLegacyDebugTraceRuntime(paths) {
|
|
|
25584
25729
|
"debug-trace-service.err.log",
|
|
25585
25730
|
"opencode-debug-trace"
|
|
25586
25731
|
]) {
|
|
25587
|
-
await rm4(
|
|
25732
|
+
await rm4(path10.join(paths.forgeDir, filename), { force: true });
|
|
25588
25733
|
}
|
|
25589
|
-
await rm4(
|
|
25590
|
-
await rm4(
|
|
25734
|
+
await rm4(path10.join(paths.forgeDir, "claude-tap-traces"), { recursive: true, force: true });
|
|
25735
|
+
await rm4(path10.join(paths.forgeDir, "verified-claude-tap"), { recursive: true, force: true });
|
|
25591
25736
|
}
|
|
25592
25737
|
async function isManagedDebugTraceConfig(configPath) {
|
|
25593
|
-
const raw = await
|
|
25738
|
+
const raw = await readFile9(configPath, "utf8").catch((error45) => {
|
|
25594
25739
|
if (error45.code === "ENOENT") return void 0;
|
|
25595
25740
|
throw error45;
|
|
25596
25741
|
});
|
|
@@ -25654,7 +25799,7 @@ var WorkPackLocalEditsError = class extends Error {
|
|
|
25654
25799
|
async function assertWorkPackUnchangedAtBoundary(paths, target, snapshot) {
|
|
25655
25800
|
const ownership = await workPackOwnership(target);
|
|
25656
25801
|
if (ownership === "absent") return;
|
|
25657
|
-
if (ownership === "foreign") throw foreignWorkPackError(
|
|
25802
|
+
if (ownership === "foreign") throw foreignWorkPackError(path10.basename(target));
|
|
25658
25803
|
const localState = await checkWorkPackInstalledState({
|
|
25659
25804
|
packDir: target,
|
|
25660
25805
|
agentsDir: paths.agentsDir,
|
|
@@ -25696,31 +25841,31 @@ async function preserveWorkPackExtras(target, nextPath, actual) {
|
|
|
25696
25841
|
`A nova vers\xE3o usa o caminho adicional "${name}". O pacote anterior e os ficheiros locais foram preservados.`
|
|
25697
25842
|
);
|
|
25698
25843
|
}
|
|
25699
|
-
for (const directory of directories) await
|
|
25844
|
+
for (const directory of directories) await mkdir6(path10.join(nextPath, directory), { recursive: true });
|
|
25700
25845
|
for (const file2 of extras) {
|
|
25701
|
-
const destination =
|
|
25702
|
-
await
|
|
25703
|
-
await cp(
|
|
25704
|
-
if (createHash4("sha256").update(await
|
|
25846
|
+
const destination = path10.join(nextPath, file2.path);
|
|
25847
|
+
await mkdir6(path10.dirname(destination), { recursive: true });
|
|
25848
|
+
await cp(path10.join(target, file2.path), destination, { errorOnExist: true, force: false });
|
|
25849
|
+
if (createHash4("sha256").update(await readFile9(destination)).digest("hex") !== file2.sha256)
|
|
25705
25850
|
throw new WorkPackLocalEditsError("local-edits-preserved");
|
|
25706
25851
|
}
|
|
25707
25852
|
}
|
|
25708
25853
|
async function installSkillArchive(paths, slug, version2, archive, options = {}) {
|
|
25709
|
-
const tempRoot = await mkdtemp(
|
|
25710
|
-
const archivePath =
|
|
25711
|
-
const extractPath =
|
|
25712
|
-
const nextPath =
|
|
25713
|
-
const target =
|
|
25714
|
-
const backup =
|
|
25854
|
+
const tempRoot = await mkdtemp(path10.join(os4.tmpdir(), `forge-${slug}-`));
|
|
25855
|
+
const archivePath = path10.join(tempRoot, `${slug}.tar`);
|
|
25856
|
+
const extractPath = path10.join(tempRoot, "extract");
|
|
25857
|
+
const nextPath = path10.join(tempRoot, slug);
|
|
25858
|
+
const target = path10.join(paths.skillsDir, slug);
|
|
25859
|
+
const backup = path10.join(paths.skillsDir, `${slug}.previous`);
|
|
25715
25860
|
let agentFiles;
|
|
25716
25861
|
let commandFiles;
|
|
25717
25862
|
let previousAgents;
|
|
25718
25863
|
let previousCommands;
|
|
25719
25864
|
try {
|
|
25720
|
-
await
|
|
25721
|
-
await
|
|
25865
|
+
await mkdir6(extractPath, { recursive: true });
|
|
25866
|
+
await writeFile7(archivePath, archive);
|
|
25722
25867
|
await So({ file: archivePath, cwd: extractPath, strict: true });
|
|
25723
|
-
const extractedSkillPath =
|
|
25868
|
+
const extractedSkillPath = path10.join(extractPath, slug);
|
|
25724
25869
|
await assertNoReservedMarkerAliases(extractedSkillPath);
|
|
25725
25870
|
await validateSkillFolder(extractedSkillPath, slug);
|
|
25726
25871
|
agentFiles = await normalizePackAgentModels(
|
|
@@ -25747,15 +25892,15 @@ async function installSkillArchive(paths, slug, version2, archive, options = {})
|
|
|
25747
25892
|
}
|
|
25748
25893
|
throw error45;
|
|
25749
25894
|
}
|
|
25750
|
-
await
|
|
25751
|
-
await
|
|
25895
|
+
await rename7(path10.join(extractPath, slug), nextPath);
|
|
25896
|
+
await writeFile7(path10.join(nextPath, ".forge-version"), `${version2}
|
|
25752
25897
|
`);
|
|
25753
25898
|
if (agentFiles.length > 0) {
|
|
25754
|
-
await
|
|
25899
|
+
await writeFile7(path10.join(nextPath, ".forge-agents"), `${agentFiles.map((agent) => agent.filename).join("\n")}
|
|
25755
25900
|
`);
|
|
25756
25901
|
}
|
|
25757
25902
|
if (commandFiles.length > 0) {
|
|
25758
|
-
await
|
|
25903
|
+
await writeFile7(path10.join(nextPath, ".forge-commands"), `${commandFiles.map((command) => command.filename).join("\n")}
|
|
25759
25904
|
`);
|
|
25760
25905
|
}
|
|
25761
25906
|
await writeWorkPackInstalledState({ packDir: nextPath, version: version2, agents: agentFiles, commands: commandFiles });
|
|
@@ -25766,13 +25911,13 @@ async function installSkillArchive(paths, slug, version2, archive, options = {})
|
|
|
25766
25911
|
const agentTransactionState = new Map(
|
|
25767
25912
|
[...agentSnapshot.keys()].map((filePath) => [
|
|
25768
25913
|
filePath,
|
|
25769
|
-
agentFiles.find((agent) => agent.filename ===
|
|
25914
|
+
agentFiles.find((agent) => agent.filename === path10.basename(filePath))?.content
|
|
25770
25915
|
])
|
|
25771
25916
|
);
|
|
25772
25917
|
const commandTransactionState = new Map(
|
|
25773
25918
|
[...commandSnapshot.keys()].map((filePath) => [
|
|
25774
25919
|
filePath,
|
|
25775
|
-
commandFiles.find((command) => command.filename ===
|
|
25920
|
+
commandFiles.find((command) => command.filename === path10.basename(filePath))?.content
|
|
25776
25921
|
])
|
|
25777
25922
|
);
|
|
25778
25923
|
const previousManagedPaths = await managedWorkPackPaths(paths, target);
|
|
@@ -25793,7 +25938,7 @@ async function installSkillArchive(paths, slug, version2, archive, options = {})
|
|
|
25793
25938
|
throw error45;
|
|
25794
25939
|
}
|
|
25795
25940
|
const writeProjection = options.writeWorkPackProjection ?? writeTextAtomic;
|
|
25796
|
-
const claimedBackup =
|
|
25941
|
+
const claimedBackup = path10.join(tempRoot, `${slug}-claimed-previous`);
|
|
25797
25942
|
const transactionPath = `${backup}${WORK_PACK_TRANSACTION_SUFFIX}`;
|
|
25798
25943
|
let targetBackedUp = false;
|
|
25799
25944
|
let targetInstalled = false;
|
|
@@ -25803,7 +25948,7 @@ async function installSkillArchive(paths, slug, version2, archive, options = {})
|
|
|
25803
25948
|
await rm4(transactionPath, { force: true });
|
|
25804
25949
|
await writeTextAtomic(transactionPath, "install\n");
|
|
25805
25950
|
try {
|
|
25806
|
-
await
|
|
25951
|
+
await rename7(target, backup);
|
|
25807
25952
|
targetBackedUp = true;
|
|
25808
25953
|
} catch (error45) {
|
|
25809
25954
|
if (error45.code !== "ENOENT") throw error45;
|
|
@@ -25813,33 +25958,33 @@ async function installSkillArchive(paths, slug, version2, archive, options = {})
|
|
|
25813
25958
|
await options.installWorkPackBoundary?.("after-backup");
|
|
25814
25959
|
await assertWorkPackUnchangedAtBoundary(paths, backup, originalTree);
|
|
25815
25960
|
}
|
|
25816
|
-
await
|
|
25961
|
+
await rename7(nextPath, target);
|
|
25817
25962
|
targetInstalled = true;
|
|
25818
25963
|
for (const filename of previousAgents.filter((name) => !nextAgentNames.has(name))) {
|
|
25819
|
-
const filePath =
|
|
25964
|
+
const filePath = path10.join(paths.agentsDir, filename);
|
|
25820
25965
|
await options.workPackProjectionBoundary?.("install", filePath);
|
|
25821
25966
|
await assertProjectionState(filePath, agentSnapshot.get(filePath));
|
|
25822
25967
|
await rm4(filePath, { force: true });
|
|
25823
25968
|
}
|
|
25824
25969
|
if (agentFiles.length > 0) {
|
|
25825
|
-
await
|
|
25970
|
+
await mkdir6(paths.agentsDir, { recursive: true });
|
|
25826
25971
|
for (const agent of agentFiles) {
|
|
25827
|
-
const filePath =
|
|
25972
|
+
const filePath = path10.join(paths.agentsDir, agent.filename);
|
|
25828
25973
|
await options.workPackProjectionBoundary?.("install", filePath);
|
|
25829
25974
|
await assertProjectionState(filePath, agentSnapshot.get(filePath));
|
|
25830
25975
|
await writeProjection(filePath, agent.content);
|
|
25831
25976
|
}
|
|
25832
25977
|
}
|
|
25833
25978
|
for (const filename of previousCommands.filter((name) => !nextCommandNames.has(name))) {
|
|
25834
|
-
const filePath =
|
|
25979
|
+
const filePath = path10.join(paths.commandsDir, filename);
|
|
25835
25980
|
await options.workPackProjectionBoundary?.("install", filePath);
|
|
25836
25981
|
await assertProjectionState(filePath, commandSnapshot.get(filePath));
|
|
25837
25982
|
await rm4(filePath, { force: true });
|
|
25838
25983
|
}
|
|
25839
25984
|
if (commandFiles.length > 0) {
|
|
25840
|
-
await
|
|
25985
|
+
await mkdir6(paths.commandsDir, { recursive: true });
|
|
25841
25986
|
for (const command of commandFiles) {
|
|
25842
|
-
const filePath =
|
|
25987
|
+
const filePath = path10.join(paths.commandsDir, command.filename);
|
|
25843
25988
|
await options.workPackProjectionBoundary?.("install", filePath);
|
|
25844
25989
|
await assertProjectionState(filePath, commandSnapshot.get(filePath));
|
|
25845
25990
|
await writeProjection(filePath, command.content);
|
|
@@ -25849,10 +25994,10 @@ async function installSkillArchive(paths, slug, version2, archive, options = {})
|
|
|
25849
25994
|
if (targetBackedUp) await options.installWorkPackBoundary?.("before-commit");
|
|
25850
25995
|
await assertWorkPackTreeUnchangedAtBoundary(target, installedTree);
|
|
25851
25996
|
if (targetBackedUp) {
|
|
25852
|
-
await
|
|
25997
|
+
await rename7(backup, claimedBackup);
|
|
25853
25998
|
backupClaimed = true;
|
|
25854
25999
|
await assertWorkPackTreeUnchangedAtBoundary(claimedBackup, originalTree);
|
|
25855
|
-
await
|
|
26000
|
+
await writeFile7(path10.join(claimedBackup, WORK_PACK_COMMITTED_MARKER), `${version2}
|
|
25856
26001
|
`);
|
|
25857
26002
|
await writeTextAtomic(transactionPath, "committed\n");
|
|
25858
26003
|
}
|
|
@@ -25864,11 +26009,11 @@ async function installSkillArchive(paths, slug, version2, archive, options = {})
|
|
|
25864
26009
|
await restoreProjectionState(commandSnapshot, commandTransactionState);
|
|
25865
26010
|
}
|
|
25866
26011
|
if (targetBackedUp) {
|
|
25867
|
-
const restoredTarget =
|
|
26012
|
+
const restoredTarget = path10.join(tempRoot, `${slug}-restore`);
|
|
25868
26013
|
const rollbackSource = backupClaimed ? claimedBackup : backup;
|
|
25869
26014
|
await cp(rollbackSource, restoredTarget, { recursive: true });
|
|
25870
26015
|
if (targetInstalled) await rm4(target, { recursive: true, force: true });
|
|
25871
|
-
await
|
|
26016
|
+
await rename7(restoredTarget, target);
|
|
25872
26017
|
await rm4(rollbackSource, { recursive: true, force: true });
|
|
25873
26018
|
await rm4(transactionPath, { force: true });
|
|
25874
26019
|
} else if (targetInstalled) {
|
|
@@ -25894,26 +26039,26 @@ async function installSkillArchive(paths, slug, version2, archive, options = {})
|
|
|
25894
26039
|
await rm4(transactionPath, { force: true });
|
|
25895
26040
|
} else {
|
|
25896
26041
|
cleanup2 = "pending";
|
|
25897
|
-
if (backupClaimed) await
|
|
26042
|
+
if (backupClaimed) await rename7(claimedBackup, backup);
|
|
25898
26043
|
}
|
|
25899
26044
|
}
|
|
25900
26045
|
await rm4(tempRoot, { recursive: true, force: true }).catch(() => void 0);
|
|
25901
26046
|
return { cleanup: cleanup2 };
|
|
25902
26047
|
}
|
|
25903
26048
|
async function adoptLegacyWorkPackState(paths, slug, version2, archive, approvedModelIds) {
|
|
25904
|
-
const comparisonRoot = await mkdtemp(
|
|
26049
|
+
const comparisonRoot = await mkdtemp(path10.join(os4.tmpdir(), `forge-${slug}-baseline-`));
|
|
25905
26050
|
const comparisonPaths = {
|
|
25906
26051
|
...paths,
|
|
25907
|
-
skillsDir:
|
|
25908
|
-
agentsDir:
|
|
25909
|
-
commandsDir:
|
|
26052
|
+
skillsDir: path10.join(comparisonRoot, "skills"),
|
|
26053
|
+
agentsDir: path10.join(comparisonRoot, "agents"),
|
|
26054
|
+
commandsDir: path10.join(comparisonRoot, "commands")
|
|
25910
26055
|
};
|
|
25911
26056
|
try {
|
|
25912
|
-
await
|
|
26057
|
+
await mkdir6(comparisonPaths.skillsDir, { recursive: true });
|
|
25913
26058
|
await installSkillArchive(comparisonPaths, slug, version2, archive, { approvedModelIds });
|
|
25914
26059
|
return adoptWorkPackInstalledState({
|
|
25915
|
-
expectedPackDir:
|
|
25916
|
-
packDir:
|
|
26060
|
+
expectedPackDir: path10.join(comparisonPaths.skillsDir, slug),
|
|
26061
|
+
packDir: path10.join(paths.skillsDir, slug),
|
|
25917
26062
|
agentsDir: paths.agentsDir,
|
|
25918
26063
|
commandsDir: paths.commandsDir,
|
|
25919
26064
|
allowAdditional: true
|
|
@@ -25925,7 +26070,7 @@ async function adoptLegacyWorkPackState(paths, slug, version2, archive, approved
|
|
|
25925
26070
|
async function recoverInterruptedWorkPackInstalls(paths, cleanupWorkPackBackup, workPackProjectionBoundary) {
|
|
25926
26071
|
const cleanupPending = /* @__PURE__ */ new Set();
|
|
25927
26072
|
const failures = /* @__PURE__ */ new Map();
|
|
25928
|
-
const entries = await
|
|
26073
|
+
const entries = await readdir5(paths.skillsDir, { withFileTypes: true }).catch((error45) => {
|
|
25929
26074
|
if (error45.code === "ENOENT") return [];
|
|
25930
26075
|
throw error45;
|
|
25931
26076
|
});
|
|
@@ -25933,7 +26078,7 @@ async function recoverInterruptedWorkPackInstalls(paths, cleanupWorkPackBackup,
|
|
|
25933
26078
|
if (!entry.name.endsWith(".previous")) continue;
|
|
25934
26079
|
const slug = entry.name.slice(0, -".previous".length);
|
|
25935
26080
|
if (!slug) continue;
|
|
25936
|
-
const target =
|
|
26081
|
+
const target = path10.join(paths.skillsDir, slug);
|
|
25937
26082
|
const backup = `${target}.previous`;
|
|
25938
26083
|
const transactionState = await readWorkPackTransactionState(backup);
|
|
25939
26084
|
if (transactionState === "committed") {
|
|
@@ -25966,9 +26111,9 @@ async function recoverInterruptedWorkPackInstall(paths, target, cleanupWorkPackB
|
|
|
25966
26111
|
const backup = `${target}.previous`;
|
|
25967
26112
|
const backupOwnership = await workPackOwnership(backup);
|
|
25968
26113
|
if (backupOwnership === "absent") return "recovered";
|
|
25969
|
-
if (backupOwnership === "foreign") throw foreignWorkPackError(
|
|
25970
|
-
if (await workPackOwnership(target) === "foreign") throw foreignWorkPackError(
|
|
25971
|
-
const committed = await
|
|
26114
|
+
if (backupOwnership === "foreign") throw foreignWorkPackError(path10.basename(target));
|
|
26115
|
+
if (await workPackOwnership(target) === "foreign") throw foreignWorkPackError(path10.basename(target));
|
|
26116
|
+
const committed = await readFile9(path10.join(backup, WORK_PACK_COMMITTED_MARKER), "utf8").catch((error45) => {
|
|
25972
26117
|
if (error45.code === "ENOENT") return void 0;
|
|
25973
26118
|
throw error45;
|
|
25974
26119
|
});
|
|
@@ -26016,38 +26161,38 @@ async function recoverInterruptedWorkPackInstall(paths, target, cleanupWorkPackB
|
|
|
26016
26161
|
);
|
|
26017
26162
|
const previousAgentNames = new Set(previousAgents.map((agent) => agent.filename));
|
|
26018
26163
|
const previousCommandNames = new Set(previousCommands.map((command) => command.filename));
|
|
26019
|
-
const recoveryRoot = await mkdtemp(
|
|
26020
|
-
const recoveredTarget =
|
|
26164
|
+
const recoveryRoot = await mkdtemp(path10.join(os4.tmpdir(), "forge-recover-"));
|
|
26165
|
+
const recoveredTarget = path10.join(recoveryRoot, path10.basename(target));
|
|
26021
26166
|
await cp(backup, recoveredTarget, { recursive: true });
|
|
26022
26167
|
try {
|
|
26023
26168
|
for (const filename of currentAgents.filter((name) => !previousAgentNames.has(name))) {
|
|
26024
|
-
const filePath =
|
|
26169
|
+
const filePath = path10.join(paths.agentsDir, filename);
|
|
26025
26170
|
await workPackProjectionBoundary?.("recover", filePath);
|
|
26026
26171
|
await assertProjectionState(filePath, agentRecoveryState.get(filePath));
|
|
26027
26172
|
await rm4(filePath, { force: true });
|
|
26028
26173
|
}
|
|
26029
|
-
await
|
|
26174
|
+
await mkdir6(paths.agentsDir, { recursive: true });
|
|
26030
26175
|
for (const agent of previousAgents) {
|
|
26031
|
-
const filePath =
|
|
26176
|
+
const filePath = path10.join(paths.agentsDir, agent.filename);
|
|
26032
26177
|
await workPackProjectionBoundary?.("recover", filePath);
|
|
26033
26178
|
await assertProjectionState(filePath, agentRecoveryState.get(filePath));
|
|
26034
26179
|
await writeTextAtomic(filePath, agent.content);
|
|
26035
26180
|
}
|
|
26036
26181
|
for (const filename of currentCommands.filter((name) => !previousCommandNames.has(name))) {
|
|
26037
|
-
const filePath =
|
|
26182
|
+
const filePath = path10.join(paths.commandsDir, filename);
|
|
26038
26183
|
await workPackProjectionBoundary?.("recover", filePath);
|
|
26039
26184
|
await assertProjectionState(filePath, commandRecoveryState.get(filePath));
|
|
26040
26185
|
await rm4(filePath, { force: true });
|
|
26041
26186
|
}
|
|
26042
|
-
await
|
|
26187
|
+
await mkdir6(paths.commandsDir, { recursive: true });
|
|
26043
26188
|
for (const command of previousCommands) {
|
|
26044
|
-
const filePath =
|
|
26189
|
+
const filePath = path10.join(paths.commandsDir, command.filename);
|
|
26045
26190
|
await workPackProjectionBoundary?.("recover", filePath);
|
|
26046
26191
|
await assertProjectionState(filePath, commandRecoveryState.get(filePath));
|
|
26047
26192
|
await writeTextAtomic(filePath, command.content);
|
|
26048
26193
|
}
|
|
26049
26194
|
await rm4(target, { recursive: true, force: true });
|
|
26050
|
-
await
|
|
26195
|
+
await rename7(recoveredTarget, target);
|
|
26051
26196
|
await rm4(backup, { recursive: true, force: true });
|
|
26052
26197
|
await rm4(`${backup}${WORK_PACK_TRANSACTION_SUFFIX}`, { force: true });
|
|
26053
26198
|
} finally {
|
|
@@ -26059,7 +26204,7 @@ async function removeWorkPackBackup(backupPath) {
|
|
|
26059
26204
|
await rm4(backupPath, { recursive: true, force: true });
|
|
26060
26205
|
}
|
|
26061
26206
|
async function readWorkPackTransactionFile(backup) {
|
|
26062
|
-
return
|
|
26207
|
+
return readFile9(`${backup}${WORK_PACK_TRANSACTION_SUFFIX}`, "utf8").catch((error45) => {
|
|
26063
26208
|
if (error45.code === "ENOENT") return void 0;
|
|
26064
26209
|
throw error45;
|
|
26065
26210
|
});
|
|
@@ -26073,7 +26218,7 @@ function isManagedProjectionName(filename) {
|
|
|
26073
26218
|
return filename.length > 0 && !filename.includes("/") && !filename.includes("\\") && filename !== "..";
|
|
26074
26219
|
}
|
|
26075
26220
|
async function recordRemovedProjection(transactionPath, kind, filename) {
|
|
26076
|
-
const current = await
|
|
26221
|
+
const current = await readFile9(transactionPath, "utf8");
|
|
26077
26222
|
await writeTextAtomic(transactionPath, `${current.trimEnd()}
|
|
26078
26223
|
${kind}:${filename}
|
|
26079
26224
|
`);
|
|
@@ -26110,7 +26255,7 @@ async function workPackOwnership(target) {
|
|
|
26110
26255
|
});
|
|
26111
26256
|
if (!targetStat) return "absent";
|
|
26112
26257
|
if (!targetStat.isDirectory()) return "foreign";
|
|
26113
|
-
const marker = await
|
|
26258
|
+
const marker = await readFile9(path10.join(target, ".forge-version"), "utf8").catch((error45) => {
|
|
26114
26259
|
if (error45.code === "ENOENT") return void 0;
|
|
26115
26260
|
throw error45;
|
|
26116
26261
|
});
|
|
@@ -26136,11 +26281,11 @@ async function assertProjectionRecoveryIsSafe(directory, currentFiles, previousF
|
|
|
26136
26281
|
expected.set(file2.filename, contents);
|
|
26137
26282
|
}
|
|
26138
26283
|
for (const [filename, contents] of expected) {
|
|
26139
|
-
const projected = await
|
|
26284
|
+
const projected = await readFile9(path10.join(directory, filename), "utf8").catch((error45) => {
|
|
26140
26285
|
if (error45.code === "ENOENT") return void 0;
|
|
26141
26286
|
throw error45;
|
|
26142
26287
|
});
|
|
26143
|
-
validated.set(
|
|
26288
|
+
validated.set(path10.join(directory, filename), projected);
|
|
26144
26289
|
const missingPreviousProjection = missingPreviousProjectionIsLocalDeletion(
|
|
26145
26290
|
filename,
|
|
26146
26291
|
projected,
|
|
@@ -26158,19 +26303,19 @@ async function assertProjectionRecoveryIsSafe(directory, currentFiles, previousF
|
|
|
26158
26303
|
return validated;
|
|
26159
26304
|
}
|
|
26160
26305
|
async function assertProjectionState(filePath, expected) {
|
|
26161
|
-
const current = await
|
|
26306
|
+
const current = await readFile9(filePath, "utf8").catch((error45) => {
|
|
26162
26307
|
if (error45.code === "ENOENT") return void 0;
|
|
26163
26308
|
throw error45;
|
|
26164
26309
|
});
|
|
26165
26310
|
if (current !== expected) {
|
|
26166
|
-
throw new WorkPackLocalEditsError("local-edits-preserved", `A proje\xE7\xE3o ${
|
|
26311
|
+
throw new WorkPackLocalEditsError("local-edits-preserved", `A proje\xE7\xE3o ${path10.basename(filePath)} foi alterada durante a opera\xE7\xE3o`);
|
|
26167
26312
|
}
|
|
26168
26313
|
}
|
|
26169
26314
|
async function captureProjectionState(directory, filenames) {
|
|
26170
26315
|
const snapshot = /* @__PURE__ */ new Map();
|
|
26171
26316
|
for (const filename of filenames) {
|
|
26172
|
-
const filePath =
|
|
26173
|
-
const content = await
|
|
26317
|
+
const filePath = path10.join(directory, filename);
|
|
26318
|
+
const content = await readFile9(filePath, "utf8").catch((error45) => {
|
|
26174
26319
|
if (error45.code === "ENOENT") return void 0;
|
|
26175
26320
|
throw error45;
|
|
26176
26321
|
});
|
|
@@ -26181,7 +26326,7 @@ async function captureProjectionState(directory, filenames) {
|
|
|
26181
26326
|
async function restoreProjectionState(snapshot, transactionState) {
|
|
26182
26327
|
for (const [filePath, content] of snapshot) {
|
|
26183
26328
|
if (transactionState) {
|
|
26184
|
-
const current = await
|
|
26329
|
+
const current = await readFile9(filePath, "utf8").catch((error45) => {
|
|
26185
26330
|
if (error45.code === "ENOENT") return void 0;
|
|
26186
26331
|
throw error45;
|
|
26187
26332
|
});
|
|
@@ -26192,8 +26337,8 @@ async function restoreProjectionState(snapshot, transactionState) {
|
|
|
26192
26337
|
}
|
|
26193
26338
|
}
|
|
26194
26339
|
async function readPackAgentFiles(skillFolder, managedNames) {
|
|
26195
|
-
const agentsFolder =
|
|
26196
|
-
const entries = await
|
|
26340
|
+
const agentsFolder = path10.join(skillFolder, "agents");
|
|
26341
|
+
const entries = await readdir5(agentsFolder, { withFileTypes: true }).catch((error45) => {
|
|
26197
26342
|
if (error45.code === "ENOENT") return [];
|
|
26198
26343
|
throw error45;
|
|
26199
26344
|
});
|
|
@@ -26201,7 +26346,7 @@ async function readPackAgentFiles(skillFolder, managedNames) {
|
|
|
26201
26346
|
for (const entry of entries) {
|
|
26202
26347
|
if (!entry.isFile() || !entry.name.endsWith(".md") || managedNames && !managedNames.has(entry.name)) continue;
|
|
26203
26348
|
if (entry.name.startsWith("._")) continue;
|
|
26204
|
-
const content = await
|
|
26349
|
+
const content = await readFile9(path10.join(agentsFolder, entry.name), "utf8");
|
|
26205
26350
|
validateAgentFile(entry.name, content);
|
|
26206
26351
|
files.push({ filename: entry.name, content });
|
|
26207
26352
|
}
|
|
@@ -26212,7 +26357,7 @@ async function normalizePackAgentModels(skillFolder, agentFiles, approvedModelId
|
|
|
26212
26357
|
for (const agent of agentFiles) {
|
|
26213
26358
|
const content = normalizePackAgentModel(agent.filename, agent.content, approvedModelIds, slug);
|
|
26214
26359
|
if (content !== agent.content) {
|
|
26215
|
-
await
|
|
26360
|
+
await writeFile7(path10.join(skillFolder, "agents", agent.filename), content);
|
|
26216
26361
|
}
|
|
26217
26362
|
normalized.push({ ...agent, content });
|
|
26218
26363
|
}
|
|
@@ -26232,8 +26377,8 @@ function normalizePackAgentModel(filename, content, approvedModelIds, slug) {
|
|
|
26232
26377
|
return normalizePackExtensionModel(`agents/${filename}`, content, approvedModelIds, slug);
|
|
26233
26378
|
}
|
|
26234
26379
|
async function readPackCommandFiles(skillFolder, managedNames) {
|
|
26235
|
-
const commandsFolder =
|
|
26236
|
-
const entries = await
|
|
26380
|
+
const commandsFolder = path10.join(skillFolder, "commands");
|
|
26381
|
+
const entries = await readdir5(commandsFolder, { withFileTypes: true }).catch((error45) => {
|
|
26237
26382
|
if (error45.code === "ENOENT") return [];
|
|
26238
26383
|
throw error45;
|
|
26239
26384
|
});
|
|
@@ -26241,7 +26386,7 @@ async function readPackCommandFiles(skillFolder, managedNames) {
|
|
|
26241
26386
|
for (const entry of entries) {
|
|
26242
26387
|
if (!entry.isFile() || !entry.name.endsWith(".md") || managedNames && !managedNames.has(entry.name)) continue;
|
|
26243
26388
|
if (entry.name.startsWith("._")) continue;
|
|
26244
|
-
const content = await
|
|
26389
|
+
const content = await readFile9(path10.join(commandsFolder, entry.name), "utf8");
|
|
26245
26390
|
validateCommandFile(entry.name, content);
|
|
26246
26391
|
files.push({ filename: entry.name, content });
|
|
26247
26392
|
}
|
|
@@ -26252,7 +26397,7 @@ async function normalizePackCommandModels(skillFolder, commandFiles, approvedMod
|
|
|
26252
26397
|
for (const command of commandFiles) {
|
|
26253
26398
|
const content = normalizePackExtensionModel(`commands/${command.filename}`, command.content, approvedModelIds, slug);
|
|
26254
26399
|
if (content !== command.content) {
|
|
26255
|
-
await
|
|
26400
|
+
await writeFile7(path10.join(skillFolder, "commands", command.filename), content);
|
|
26256
26401
|
}
|
|
26257
26402
|
normalized.push({ ...command, content });
|
|
26258
26403
|
}
|
|
@@ -26274,7 +26419,7 @@ async function assertNoForeignAgentConflicts(agentsDir, agentFiles, ownedFilenam
|
|
|
26274
26419
|
const owned = new Set(ownedFilenames);
|
|
26275
26420
|
for (const agent of agentFiles) {
|
|
26276
26421
|
if (owned.has(agent.filename)) continue;
|
|
26277
|
-
const existing = await
|
|
26422
|
+
const existing = await readFile9(path10.join(agentsDir, agent.filename), "utf8").catch((error45) => {
|
|
26278
26423
|
if (error45.code === "ENOENT") return void 0;
|
|
26279
26424
|
throw error45;
|
|
26280
26425
|
});
|
|
@@ -26287,7 +26432,7 @@ async function assertNoForeignCommandConflicts(commandsDir, commandFiles, ownedF
|
|
|
26287
26432
|
const owned = new Set(ownedFilenames);
|
|
26288
26433
|
for (const command of commandFiles) {
|
|
26289
26434
|
if (owned.has(command.filename)) continue;
|
|
26290
|
-
const existing = await
|
|
26435
|
+
const existing = await readFile9(path10.join(commandsDir, command.filename), "utf8").catch((error45) => {
|
|
26291
26436
|
if (error45.code === "ENOENT") return void 0;
|
|
26292
26437
|
throw error45;
|
|
26293
26438
|
});
|
|
@@ -26297,28 +26442,28 @@ async function assertNoForeignCommandConflicts(commandsDir, commandFiles, ownedF
|
|
|
26297
26442
|
}
|
|
26298
26443
|
}
|
|
26299
26444
|
async function readAgentRecord(skillFolder) {
|
|
26300
|
-
const raw = await
|
|
26445
|
+
const raw = await readFile9(path10.join(skillFolder, ".forge-agents"), "utf8").catch(() => "");
|
|
26301
26446
|
return raw.split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && !line.includes("/") && !line.includes("\\") && line !== "..");
|
|
26302
26447
|
}
|
|
26303
26448
|
async function readCommandRecord(skillFolder) {
|
|
26304
|
-
const raw = await
|
|
26449
|
+
const raw = await readFile9(path10.join(skillFolder, ".forge-commands"), "utf8").catch(() => "");
|
|
26305
26450
|
return raw.split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && !line.includes("/") && !line.includes("\\") && line !== "..");
|
|
26306
26451
|
}
|
|
26307
26452
|
async function managedWorkPackPaths(paths, skillFolder) {
|
|
26308
26453
|
const files = await listFilesRecursively(skillFolder);
|
|
26309
|
-
const agents = (await readAgentRecord(skillFolder)).map((filename) =>
|
|
26310
|
-
const commands = (await readCommandRecord(skillFolder)).map((filename) =>
|
|
26454
|
+
const agents = (await readAgentRecord(skillFolder)).map((filename) => path10.join(paths.agentsDir, filename));
|
|
26455
|
+
const commands = (await readCommandRecord(skillFolder)).map((filename) => path10.join(paths.commandsDir, filename));
|
|
26311
26456
|
return [.../* @__PURE__ */ new Set([...files, ...agents, ...commands])];
|
|
26312
26457
|
}
|
|
26313
26458
|
async function managedWorkPackSlugs(skillsDir) {
|
|
26314
|
-
const entries = await
|
|
26459
|
+
const entries = await readdir5(skillsDir, { withFileTypes: true }).catch((error45) => {
|
|
26315
26460
|
if (error45.code === "ENOENT") return [];
|
|
26316
26461
|
throw error45;
|
|
26317
26462
|
});
|
|
26318
26463
|
const slugs = [];
|
|
26319
26464
|
for (const entry of entries) {
|
|
26320
26465
|
if (!entry.isDirectory() || entry.name.endsWith(".previous")) continue;
|
|
26321
|
-
if (await workPackOwnership(
|
|
26466
|
+
if (await workPackOwnership(path10.join(skillsDir, entry.name)) === "managed") slugs.push(entry.name);
|
|
26322
26467
|
}
|
|
26323
26468
|
return slugs.sort((left, right) => left.localeCompare(right));
|
|
26324
26469
|
}
|
|
@@ -26328,21 +26473,21 @@ async function removeManagedWorkPackTransaction(paths, target, options) {
|
|
|
26328
26473
|
const agents = await readAgentRecord(target);
|
|
26329
26474
|
const commands = await readCommandRecord(target);
|
|
26330
26475
|
const snapshots = await Promise.all([
|
|
26331
|
-
...agents.map((filename) => captureExactFile(
|
|
26332
|
-
...commands.map((filename) => captureExactFile(
|
|
26476
|
+
...agents.map((filename) => captureExactFile(path10.join(paths.agentsDir, filename))),
|
|
26477
|
+
...commands.map((filename) => captureExactFile(path10.join(paths.commandsDir, filename)))
|
|
26333
26478
|
]);
|
|
26334
26479
|
await assertWorkPackUnchangedAtBoundary(paths, target);
|
|
26335
26480
|
const backup = `${target}.previous`;
|
|
26336
26481
|
const transactionPath = `${backup}${WORK_PACK_TRANSACTION_SUFFIX}`;
|
|
26337
|
-
const transactionRoot = await mkdtemp(
|
|
26338
|
-
const claimedBackup =
|
|
26482
|
+
const transactionRoot = await mkdtemp(path10.join(os4.tmpdir(), "forge-remove-"));
|
|
26483
|
+
const claimedBackup = path10.join(transactionRoot, path10.basename(target));
|
|
26339
26484
|
let projectionsRemoved = false;
|
|
26340
26485
|
let backupClaimed = false;
|
|
26341
26486
|
try {
|
|
26342
26487
|
await rm4(backup, { recursive: true, force: true });
|
|
26343
26488
|
await rm4(transactionPath, { force: true });
|
|
26344
26489
|
await writeTextAtomic(transactionPath, "remove\n");
|
|
26345
|
-
await
|
|
26490
|
+
await rename7(target, backup);
|
|
26346
26491
|
} catch (error45) {
|
|
26347
26492
|
await rm4(transactionPath, { force: true }).catch(() => void 0);
|
|
26348
26493
|
await rm4(transactionRoot, { recursive: true, force: true }).catch(() => void 0);
|
|
@@ -26353,7 +26498,7 @@ async function removeManagedWorkPackTransaction(paths, target, options) {
|
|
|
26353
26498
|
await assertWorkPackUnchangedAtBoundary(paths, backup);
|
|
26354
26499
|
projectionsRemoved = agents.length > 0;
|
|
26355
26500
|
for (const filename of agents) {
|
|
26356
|
-
const filePath =
|
|
26501
|
+
const filePath = path10.join(paths.agentsDir, filename);
|
|
26357
26502
|
await options.workPackProjectionBoundary?.("remove", filePath);
|
|
26358
26503
|
await assertExactFileState(snapshots, filePath);
|
|
26359
26504
|
await recordRemovedProjection(transactionPath, "agent", filename);
|
|
@@ -26362,7 +26507,7 @@ async function removeManagedWorkPackTransaction(paths, target, options) {
|
|
|
26362
26507
|
await options.removeWorkPackBoundary?.("after-agents");
|
|
26363
26508
|
projectionsRemoved ||= commands.length > 0;
|
|
26364
26509
|
for (const filename of commands) {
|
|
26365
|
-
const filePath =
|
|
26510
|
+
const filePath = path10.join(paths.commandsDir, filename);
|
|
26366
26511
|
await options.workPackProjectionBoundary?.("remove", filePath);
|
|
26367
26512
|
await assertExactFileState(snapshots, filePath);
|
|
26368
26513
|
await recordRemovedProjection(transactionPath, "command", filename);
|
|
@@ -26371,7 +26516,7 @@ async function removeManagedWorkPackTransaction(paths, target, options) {
|
|
|
26371
26516
|
await options.removeWorkPackBoundary?.("after-commands");
|
|
26372
26517
|
await assertWorkPackTreeUnchangedAtBoundary(backup);
|
|
26373
26518
|
await options.removeWorkPackBoundary?.("before-commit");
|
|
26374
|
-
await
|
|
26519
|
+
await rename7(backup, claimedBackup);
|
|
26375
26520
|
backupClaimed = true;
|
|
26376
26521
|
await assertWorkPackTreeUnchangedAtBoundary(claimedBackup);
|
|
26377
26522
|
await writeTextAtomic(transactionPath, "committed\n");
|
|
@@ -26382,13 +26527,13 @@ async function removeManagedWorkPackTransaction(paths, target, options) {
|
|
|
26382
26527
|
} catch (error45) {
|
|
26383
26528
|
try {
|
|
26384
26529
|
if (projectionsRemoved) await restoreRemovedExactFiles(snapshots);
|
|
26385
|
-
await
|
|
26530
|
+
await rename7(backupClaimed ? claimedBackup : backup, target);
|
|
26386
26531
|
await rm4(transactionPath, { force: true });
|
|
26387
26532
|
if (projectionsRemoved) await verifyRestoredOrLocallyChangedFiles(snapshots);
|
|
26388
26533
|
if (await workPackOwnership(target) !== "managed") throw new Error("restored Work Pack marker missing");
|
|
26389
26534
|
await rm4(transactionRoot, { recursive: true, force: true }).catch(() => void 0);
|
|
26390
26535
|
} catch (restoreError) {
|
|
26391
|
-
throw new WorkPackRecoveryUncertainError(`Falha ao remover e restaurar o pacote de trabalho ${
|
|
26536
|
+
throw new WorkPackRecoveryUncertainError(`Falha ao remover e restaurar o pacote de trabalho ${path10.basename(target)}`, {
|
|
26392
26537
|
cause: new AggregateError([error45, restoreError])
|
|
26393
26538
|
});
|
|
26394
26539
|
}
|
|
@@ -26399,7 +26544,7 @@ async function captureExactFile(filePath) {
|
|
|
26399
26544
|
try {
|
|
26400
26545
|
const info = await lstat5(filePath);
|
|
26401
26546
|
if (!info.isFile()) throw new Error(`A proje\xE7\xE3o gerida n\xE3o \xE9 um ficheiro regular: ${filePath}`);
|
|
26402
|
-
return { path: filePath, exists: true, bytes: await
|
|
26547
|
+
return { path: filePath, exists: true, bytes: await readFile9(filePath), mode: info.mode & 4095 };
|
|
26403
26548
|
} catch (error45) {
|
|
26404
26549
|
if (error45.code === "ENOENT") return { path: filePath, exists: false };
|
|
26405
26550
|
throw error45;
|
|
@@ -26409,18 +26554,18 @@ async function assertExactFileState(snapshots, filePath) {
|
|
|
26409
26554
|
const expected = snapshots.find((snapshot) => snapshot.path === filePath);
|
|
26410
26555
|
const current = await captureExactFile(filePath);
|
|
26411
26556
|
if (!expected || current.exists !== expected.exists) {
|
|
26412
|
-
throw new WorkPackLocalEditsError("local-edits-preserved", `A proje\xE7\xE3o ${
|
|
26557
|
+
throw new WorkPackLocalEditsError("local-edits-preserved", `A proje\xE7\xE3o ${path10.basename(filePath)} foi alterada durante a opera\xE7\xE3o`);
|
|
26413
26558
|
}
|
|
26414
26559
|
if (current.exists && (!current.bytes?.equals(expected.bytes) || current.mode !== expected.mode)) {
|
|
26415
|
-
throw new WorkPackLocalEditsError("local-edits-preserved", `A proje\xE7\xE3o ${
|
|
26560
|
+
throw new WorkPackLocalEditsError("local-edits-preserved", `A proje\xE7\xE3o ${path10.basename(filePath)} foi alterada durante a opera\xE7\xE3o`);
|
|
26416
26561
|
}
|
|
26417
26562
|
}
|
|
26418
26563
|
async function restoreRemovedExactFiles(snapshots) {
|
|
26419
26564
|
for (const snapshot of snapshots) {
|
|
26420
26565
|
if ((await captureExactFile(snapshot.path)).exists) continue;
|
|
26421
26566
|
if (!snapshot.exists) continue;
|
|
26422
|
-
await
|
|
26423
|
-
await
|
|
26567
|
+
await mkdir6(path10.dirname(snapshot.path), { recursive: true });
|
|
26568
|
+
await writeFile7(snapshot.path, snapshot.bytes);
|
|
26424
26569
|
await chmod4(snapshot.path, snapshot.mode);
|
|
26425
26570
|
}
|
|
26426
26571
|
}
|
|
@@ -26431,20 +26576,20 @@ async function verifyRestoredOrLocallyChangedFiles(snapshots) {
|
|
|
26431
26576
|
}
|
|
26432
26577
|
}
|
|
26433
26578
|
async function listFilesRecursively(root) {
|
|
26434
|
-
const entries = await
|
|
26579
|
+
const entries = await readdir5(root, { withFileTypes: true }).catch((error45) => {
|
|
26435
26580
|
if (error45.code === "ENOENT") return [];
|
|
26436
26581
|
throw error45;
|
|
26437
26582
|
});
|
|
26438
26583
|
const files = [];
|
|
26439
26584
|
for (const entry of entries) {
|
|
26440
|
-
const absolute =
|
|
26585
|
+
const absolute = path10.join(root, entry.name);
|
|
26441
26586
|
if (entry.isDirectory()) files.push(...await listFilesRecursively(absolute));
|
|
26442
26587
|
else if (entry.isFile()) files.push(absolute);
|
|
26443
26588
|
}
|
|
26444
26589
|
return files;
|
|
26445
26590
|
}
|
|
26446
26591
|
async function validateSkillFolder(folder, slug) {
|
|
26447
|
-
const skillMd = await
|
|
26592
|
+
const skillMd = await readFile9(path10.join(folder, "SKILL.md"), "utf8");
|
|
26448
26593
|
const frontmatter = /^---\n([\s\S]+?)\n---/.exec(skillMd)?.[1] ?? "";
|
|
26449
26594
|
if (!frontmatter.includes(`name: ${slug}`) && !frontmatter.includes(`name: "${slug}"`)) {
|
|
26450
26595
|
throw new Error(`O campo name no frontmatter de SKILL.md tem de corresponder a ${slug}`);
|
|
@@ -26455,7 +26600,7 @@ async function validateSkillFolder(folder, slug) {
|
|
|
26455
26600
|
}
|
|
26456
26601
|
async function assertNoReservedMarkerAliases(skillFolder) {
|
|
26457
26602
|
const reserved = /* @__PURE__ */ new Set([".forge-version", ".forge-agents", ".forge-commands", WORK_PACK_LOCAL_STATE_FILENAME]);
|
|
26458
|
-
const entries = await
|
|
26603
|
+
const entries = await readdir5(skillFolder);
|
|
26459
26604
|
const marker = entries.find((entry) => reserved.has(entry.toLowerCase()));
|
|
26460
26605
|
if (marker) throw new Error(`O pacote de trabalho cont\xE9m o marcador Forge reservado ${marker}`);
|
|
26461
26606
|
}
|
|
@@ -26465,14 +26610,14 @@ async function writeJsonAtomic2(filePath, value) {
|
|
|
26465
26610
|
await writeTextAtomic(filePath, content);
|
|
26466
26611
|
}
|
|
26467
26612
|
async function writeTextAtomic(filePath, content) {
|
|
26468
|
-
const dir =
|
|
26469
|
-
await
|
|
26470
|
-
const tempPath =
|
|
26471
|
-
await
|
|
26472
|
-
await
|
|
26613
|
+
const dir = path10.dirname(filePath);
|
|
26614
|
+
await mkdir6(dir, { recursive: true });
|
|
26615
|
+
const tempPath = path10.join(dir, `.${path10.basename(filePath)}.${process.pid}.tmp`);
|
|
26616
|
+
await writeFile7(tempPath, content);
|
|
26617
|
+
await rename7(tempPath, filePath);
|
|
26473
26618
|
}
|
|
26474
26619
|
async function readPluginFile(pluginPath) {
|
|
26475
|
-
return
|
|
26620
|
+
return readFile9(pluginPath, "utf8").catch((error45) => {
|
|
26476
26621
|
if (error45.code === "ENOENT") return void 0;
|
|
26477
26622
|
throw error45;
|
|
26478
26623
|
});
|
|
@@ -26505,7 +26650,7 @@ function withPluginPaths(config2, pluginPaths, removePluginPaths = []) {
|
|
|
26505
26650
|
return next;
|
|
26506
26651
|
}
|
|
26507
26652
|
async function readJsoncObject(filePath) {
|
|
26508
|
-
const raw = await
|
|
26653
|
+
const raw = await readFile9(filePath, "utf8").catch((error45) => {
|
|
26509
26654
|
if (error45.code === "ENOENT") return "";
|
|
26510
26655
|
throw error45;
|
|
26511
26656
|
});
|
|
@@ -26609,15 +26754,14 @@ function isRecord5(value) {
|
|
|
26609
26754
|
}
|
|
26610
26755
|
|
|
26611
26756
|
// packages/cli/src/localWorkerSync.ts
|
|
26612
|
-
import { readFile as
|
|
26613
|
-
import path12 from "node:path";
|
|
26757
|
+
import { readFile as readFile12 } from "node:fs/promises";
|
|
26614
26758
|
|
|
26615
26759
|
// packages/cli/src/recoveryLauncher.ts
|
|
26616
|
-
import { chmod as chmod5, readFile as
|
|
26760
|
+
import { chmod as chmod5, readFile as readFile10 } from "node:fs/promises";
|
|
26617
26761
|
import { execFileSync } from "node:child_process";
|
|
26618
|
-
import
|
|
26762
|
+
import path11 from "node:path";
|
|
26619
26763
|
async function installRecoveryLauncher(paths, forgeBaseUrl, source = new URL("./repair.js", import.meta.url)) {
|
|
26620
|
-
const program = await
|
|
26764
|
+
const program = await readFile10(source, "utf8");
|
|
26621
26765
|
let nodePath;
|
|
26622
26766
|
try {
|
|
26623
26767
|
const runtime = JSON.parse(execFileSync(
|
|
@@ -26625,16 +26769,16 @@ async function installRecoveryLauncher(paths, forgeBaseUrl, source = new URL("./
|
|
|
26625
26769
|
["-p", 'JSON.stringify({ path: process.execPath, major: Number(process.versions.node.split(".")[0]) })'],
|
|
26626
26770
|
{ encoding: "utf8", timeout: 5e3, windowsHide: true }
|
|
26627
26771
|
));
|
|
26628
|
-
if (runtime.major < 22 || typeof runtime.major !== "number" || typeof runtime.path !== "string" || !
|
|
26772
|
+
if (runtime.major < 22 || typeof runtime.major !== "number" || typeof runtime.path !== "string" || !path11.isAbsolute(runtime.path)) throw new Error();
|
|
26629
26773
|
nodePath = runtime.path;
|
|
26630
26774
|
} catch {
|
|
26631
26775
|
throw new Error("A recupera\xE7\xE3o precisa do Node 22 ou superior j\xE1 previsto na prepara\xE7\xE3o do computador. Pe\xE7a ao respons\xE1vel para verificar a instala\xE7\xE3o.");
|
|
26632
26776
|
}
|
|
26633
26777
|
const launcherContent = renderRecoveryLauncher(nodePath, process.platform);
|
|
26634
|
-
await writeOwnerOnlyTextAtomic(
|
|
26635
|
-
await writeOwnerOnlyTextAtomic(
|
|
26778
|
+
await writeOwnerOnlyTextAtomic(path11.join(paths.forgeDir, "forge-repair.mjs"), program);
|
|
26779
|
+
await writeOwnerOnlyTextAtomic(path11.join(paths.forgeDir, "recovery-origin.json"), JSON.stringify({ forgeBaseUrl }) + "\n");
|
|
26636
26780
|
const windows = process.platform === "win32";
|
|
26637
|
-
const launcher =
|
|
26781
|
+
const launcher = path11.join(paths.forgeDir, windows ? "Reparar Forge.cmd" : "Reparar Forge.command");
|
|
26638
26782
|
await writeOwnerOnlyTextAtomic(launcher, launcherContent);
|
|
26639
26783
|
if (!windows) await chmod5(launcher, 448);
|
|
26640
26784
|
return launcher;
|
|
@@ -26655,15 +26799,15 @@ exec '${nodePath.replace(/'/g, "'\\''")}' ./forge-repair.mjs "$@"
|
|
|
26655
26799
|
|
|
26656
26800
|
// packages/cli/src/rollback.ts
|
|
26657
26801
|
import { createHash as createHash6 } from "node:crypto";
|
|
26658
|
-
import { chmod as chmod6, mkdir as
|
|
26659
|
-
import
|
|
26802
|
+
import { chmod as chmod6, mkdir as mkdir7, readFile as readFile11, readdir as readdir6, rename as rename8, rm as rm5, stat as stat2, writeFile as writeFile8 } from "node:fs/promises";
|
|
26803
|
+
import path12 from "node:path";
|
|
26660
26804
|
async function recordRollbackEvidencePlugin(paths, expectedContent) {
|
|
26661
26805
|
if ((await readRollbackLifecycle(paths))?.state !== "pilot") return;
|
|
26662
26806
|
const capsule = await readRollbackCapsule(paths);
|
|
26663
|
-
const pluginPath =
|
|
26807
|
+
const pluginPath = path12.join(paths.pluginDir, FORGE_EVIDENCE_PLUGIN_FILENAME);
|
|
26664
26808
|
const managed = capsule?.managed.files.find((file2) => file2.path === pluginPath);
|
|
26665
26809
|
if (!capsule || !managed) return;
|
|
26666
|
-
const current = await
|
|
26810
|
+
const current = await readFile11(pluginPath, "utf8").catch(() => void 0);
|
|
26667
26811
|
if (current !== expectedContent) return;
|
|
26668
26812
|
const fingerprint = sha2562(Buffer.from(current));
|
|
26669
26813
|
if (managed.sha256 === fingerprint) return;
|
|
@@ -26677,20 +26821,20 @@ async function readRollbackCapsule(paths) {
|
|
|
26677
26821
|
return readJson(paths.rollbackCapsulePath);
|
|
26678
26822
|
}
|
|
26679
26823
|
async function writeProtectedJsonAtomic(filePath, value) {
|
|
26680
|
-
const directory =
|
|
26681
|
-
await
|
|
26824
|
+
const directory = path12.dirname(filePath);
|
|
26825
|
+
await mkdir7(directory, { recursive: true, mode: 448 });
|
|
26682
26826
|
await protectRollbackPath(directory, "directory");
|
|
26683
|
-
const temporary =
|
|
26684
|
-
await
|
|
26827
|
+
const temporary = path12.join(directory, `.${path12.basename(filePath)}.${process.pid}.tmp`);
|
|
26828
|
+
await writeFile8(temporary, `${JSON.stringify(value, null, 2)}
|
|
26685
26829
|
`, { mode: 384 });
|
|
26686
26830
|
await protectRollbackPath(temporary, "file");
|
|
26687
|
-
await
|
|
26831
|
+
await rename8(temporary, filePath);
|
|
26688
26832
|
}
|
|
26689
26833
|
async function protectRollbackPath(filePath, kind, options = {}) {
|
|
26690
26834
|
await protectOwnerOnlyPath(filePath, kind, { ...options, failureContext: "o material de rollback" });
|
|
26691
26835
|
}
|
|
26692
26836
|
async function readJson(filePath) {
|
|
26693
|
-
const raw = await
|
|
26837
|
+
const raw = await readFile11(filePath, "utf8").catch((error45) => {
|
|
26694
26838
|
if (error45.code === "ENOENT") return void 0;
|
|
26695
26839
|
throw error45;
|
|
26696
26840
|
});
|
|
@@ -26703,11 +26847,17 @@ function sha2562(value) {
|
|
|
26703
26847
|
|
|
26704
26848
|
// packages/cli/src/localWorkerSync.ts
|
|
26705
26849
|
async function syncLocalWorker(paths, credentials, manifest) {
|
|
26706
|
-
const
|
|
26707
|
-
|
|
26850
|
+
const result = await applyManifest(paths, credentials, manifest, {
|
|
26851
|
+
beforeCore: () => prepareLocalStartup(paths, credentials),
|
|
26852
|
+
afterCore: () => removeWellKnownAuth(paths, credentials)
|
|
26853
|
+
});
|
|
26854
|
+
await recordRollbackEvidencePlugin(paths, renderEvidencePlugin());
|
|
26855
|
+
return result;
|
|
26856
|
+
}
|
|
26857
|
+
async function prepareLocalStartup(paths, credentials) {
|
|
26708
26858
|
let current;
|
|
26709
26859
|
try {
|
|
26710
|
-
current = JSON.parse(await
|
|
26860
|
+
current = JSON.parse(await readFile12(paths.opencodeConfigPath, "utf8"));
|
|
26711
26861
|
} catch {
|
|
26712
26862
|
throw new Error("A configura\xE7\xE3o local n\xE3o \xE9 JSON simples v\xE1lido; preserve os ficheiros e pe\xE7a apoio.");
|
|
26713
26863
|
}
|
|
@@ -26719,14 +26869,10 @@ async function syncLocalWorker(paths, credentials, manifest) {
|
|
|
26719
26869
|
throw new Error("A configura\xE7\xE3o local foi alterada ou a c\xF3pia v\xE1lida n\xE3o corresponde \xE0 credencial; preserve os ficheiros e pe\xE7a apoio.");
|
|
26720
26870
|
}
|
|
26721
26871
|
await installRecoveryLauncher(paths, credentials.forgeBaseUrl);
|
|
26722
|
-
const result = await applyManifest(paths, credentials, manifest);
|
|
26723
|
-
await removeWellKnownAuth(paths, credentials);
|
|
26724
|
-
await recordRollbackEvidencePlugin(paths, renderEvidencePlugin());
|
|
26725
|
-
return result;
|
|
26726
26872
|
}
|
|
26727
26873
|
|
|
26728
26874
|
// packages/cli/src/openworkEngine.ts
|
|
26729
|
-
import { lstat as lstat6, readFile as
|
|
26875
|
+
import { lstat as lstat6, readFile as readFile13, realpath } from "node:fs/promises";
|
|
26730
26876
|
import path13 from "node:path";
|
|
26731
26877
|
var OPENWORK_ENGINE_HEADER = "// Minimal Forge OpenWork engine configuration\n";
|
|
26732
26878
|
async function inspectOpenworkEngineConfig(directory) {
|
|
@@ -26737,7 +26883,7 @@ async function inspectOpenworkEngineConfig(directory) {
|
|
|
26737
26883
|
if (!generatedEntry.isFile() || generatedEntry.isSymbolicLink()) throw new Error("Preserve a configura\xE7\xE3o do motor; o ficheiro gerado n\xE3o \xE9 regular.");
|
|
26738
26884
|
let generated;
|
|
26739
26885
|
try {
|
|
26740
|
-
generated = JSON.parse(await
|
|
26886
|
+
generated = JSON.parse(await readFile13(generatedPath, "utf8"));
|
|
26741
26887
|
} catch {
|
|
26742
26888
|
throw new Error("N\xE3o foi poss\xEDvel reconhecer a configura\xE7\xE3o do motor OpenCode 2.");
|
|
26743
26889
|
}
|
|
@@ -26749,7 +26895,7 @@ async function inspectOpenworkEngineConfig(directory) {
|
|
|
26749
26895
|
try {
|
|
26750
26896
|
const entry = await lstat6(file2);
|
|
26751
26897
|
if (!entry.isFile() || entry.isSymbolicLink()) throw new Error("Preserve o ficheiro complementar existente; n\xE3o \xE9 um ficheiro regular.");
|
|
26752
|
-
content = await
|
|
26898
|
+
content = await readFile13(file2, "utf8");
|
|
26753
26899
|
} catch (error45) {
|
|
26754
26900
|
if (error45.code !== "ENOENT") throw error45;
|
|
26755
26901
|
}
|
|
@@ -26785,7 +26931,7 @@ async function writeOpenworkEngineConfig(directory, config2) {
|
|
|
26785
26931
|
}
|
|
26786
26932
|
|
|
26787
26933
|
// packages/cli/src/workPackSyncPlugin.ts
|
|
26788
|
-
var PLUGIN_VERSION = "0.1.
|
|
26934
|
+
var PLUGIN_VERSION = "0.1.53";
|
|
26789
26935
|
var FORGE_DECISION_TIMEOUT_MS = 11e4;
|
|
26790
26936
|
var STATUS_FILENAME = "work-pack-startup-status.json";
|
|
26791
26937
|
var activeSync = /* @__PURE__ */ new Map();
|
|
@@ -26824,7 +26970,7 @@ function createEvidenceReporters(forgeBaseUrl, state) {
|
|
|
26824
26970
|
report(sessionID, callID, slug, kind);
|
|
26825
26971
|
};
|
|
26826
26972
|
const reportReasoningLoop = (sessionID, outcome) => {
|
|
26827
|
-
reportEvidence(sessionID, `reasoning-loop:${
|
|
26973
|
+
reportEvidence(sessionID, `reasoning-loop:${randomUUID5()}`, outcome, "reasoning_loop").catch(() => {
|
|
26828
26974
|
console.error("[Forge evidence] Reasoning guard identity unavailable; event not queued");
|
|
26829
26975
|
});
|
|
26830
26976
|
};
|
|
@@ -26832,7 +26978,7 @@ function createEvidenceReporters(forgeBaseUrl, state) {
|
|
|
26832
26978
|
const slug = typeof name === "string" ? name.trim().toLowerCase() : "";
|
|
26833
26979
|
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(slug)) return;
|
|
26834
26980
|
try {
|
|
26835
|
-
await
|
|
26981
|
+
await readFile14(path14.join(state.paths.skillsDir, slug, ".forge-version"), "utf8");
|
|
26836
26982
|
} catch {
|
|
26837
26983
|
return;
|
|
26838
26984
|
}
|
|
@@ -26866,10 +27012,10 @@ var ForgeWorkPackSyncServer = async (_input, options) => {
|
|
|
26866
27012
|
const name = Array.isArray(entry) ? entry[0] : entry;
|
|
26867
27013
|
return name === legacyPath || name === pathToFileURL(legacyPath).href;
|
|
26868
27014
|
});
|
|
26869
|
-
const legacy = registered ? await
|
|
27015
|
+
const legacy = registered ? await readFile14(legacyPath, "utf8").catch(() => "") : "";
|
|
26870
27016
|
const oldLegacyRegistered = registered && legacy.startsWith(FORGE_PLUGIN_HEADER) && legacy.includes("work_pack_invocation") && !legacy.includes("minimal-forge.skill-usage.v1");
|
|
26871
27017
|
if (oldLegacyRegistered) legacyOwners.add(legacyOwnerKey);
|
|
26872
|
-
if (oldLegacyRegistered) await
|
|
27018
|
+
if (oldLegacyRegistered) await writeFile9(legacyPath, renderEvidencePlugin()).catch(() => {
|
|
26873
27019
|
console.error("[Forge evidence] Legacy hook upgrade failed; existing hook retained");
|
|
26874
27020
|
});
|
|
26875
27021
|
await projectCurrentExtensions(config2, state);
|
|
@@ -26891,7 +27037,7 @@ async function ForgeWorkPackSyncSetup(ctx) {
|
|
|
26891
27037
|
throw new Error("A pasta do motor ativo mudou. Confirme a liga\xE7\xE3o Forge neste perfil do OpenWork.");
|
|
26892
27038
|
}
|
|
26893
27039
|
const credentials = await readForgeCredentials(state.paths, forgeBaseUrl);
|
|
26894
|
-
const canonical = objectRecord2(parseJsonc(await
|
|
27040
|
+
const canonical = objectRecord2(parseJsonc(await readFile14(state.paths.opencodeConfigPath, "utf8")));
|
|
26895
27041
|
const config2 = renderOpenworkEngineConfig(state.manifest, credentials, state.paths, directory, canonical);
|
|
26896
27042
|
if (state.partialWorkPackSync) {
|
|
26897
27043
|
const companion = await inspectOpenworkEngineConfig(directory);
|
|
@@ -26946,10 +27092,10 @@ function createForgeImageTool(forgeBaseUrl, gatewayBaseUrl, paths) {
|
|
|
26946
27092
|
referencePath: tool.schema.string().optional().describe("Caminho absoluto de uma imagem PNG, JPEG ou WebP a editar, at\xE9 2,5 MB")
|
|
26947
27093
|
},
|
|
26948
27094
|
async execute(args, context) {
|
|
26949
|
-
const requestId =
|
|
27095
|
+
const requestId = randomUUID5();
|
|
26950
27096
|
let referenceDataUrl;
|
|
26951
27097
|
if (args.referencePath) {
|
|
26952
|
-
const bytes = await
|
|
27098
|
+
const bytes = await readFile14(args.referencePath);
|
|
26953
27099
|
if (bytes.byteLength > 25e5) throw new Error("A imagem de refer\xEAncia \xE9 demasiado grande. Reduza-a antes de tentar novamente.");
|
|
26954
27100
|
const mime = bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])) ? "image/png" : bytes.subarray(0, 3).equals(Buffer.from([255, 216, 255])) ? "image/jpeg" : bytes.subarray(0, 4).toString() === "RIFF" && bytes.subarray(8, 12).toString() === "WEBP" ? "image/webp" : void 0;
|
|
26955
27101
|
if (!mime) throw new Error("A refer\xEAncia tem de ser PNG, JPEG ou WebP.");
|
|
@@ -26976,7 +27122,7 @@ function createForgeImageTool(forgeBaseUrl, gatewayBaseUrl, paths) {
|
|
|
26976
27122
|
throw new Error(`O PNG recebido \xE9 inv\xE1lido. Refer\xEAncia: ${requestId}`);
|
|
26977
27123
|
}
|
|
26978
27124
|
const destination = path14.join(context.directory, `forge-image-${requestId}.png`);
|
|
26979
|
-
await
|
|
27125
|
+
await writeFile9(destination, image, { flag: "wx" });
|
|
26980
27126
|
const reset = body.quota && new Intl.DateTimeFormat("pt-PT", { dateStyle: "long", timeZone: "Europe/Lisbon" }).format(new Date(body.quota.resetsAt));
|
|
26981
27127
|
const remaining = body.quota ? ` Restam ${body.quota.remaining} imagens at\xE9 ${reset}.` : "";
|
|
26982
27128
|
return {
|
|
@@ -27026,7 +27172,7 @@ function createForgeDecisionTool(forgeBaseUrl, decisionBaseUrl, paths, responseD
|
|
|
27026
27172
|
const file2 = await fileInsideWorkspace(context.directory, args.request_file);
|
|
27027
27173
|
const planFile = path14.join(path14.dirname(path14.dirname(file2)), "plan.json");
|
|
27028
27174
|
if (!responseDirectory && path14.basename(path14.dirname(file2)) === "requests" && await fileExists(planFile)) {
|
|
27029
|
-
const plan = JSON.parse(await
|
|
27175
|
+
const plan = JSON.parse(await readFile14(planFile, "utf8"));
|
|
27030
27176
|
if (plan?.engine === "jev-batch" && plan.model === "typesafe/jev-1.13" && Array.isArray(plan.batches)) {
|
|
27031
27177
|
let batch;
|
|
27032
27178
|
for (const candidate of plan.batches) {
|
|
@@ -27039,7 +27185,7 @@ function createForgeDecisionTool(forgeBaseUrl, decisionBaseUrl, paths, responseD
|
|
|
27039
27185
|
return createForgeDecisionRunTool(forgeBaseUrl, decisionBaseUrl, paths).execute({ plan_file: planFile, batch_id: batch.batch_id }, context);
|
|
27040
27186
|
}
|
|
27041
27187
|
}
|
|
27042
|
-
const raw = await
|
|
27188
|
+
const raw = await readFile14(file2, "utf8");
|
|
27043
27189
|
if (Buffer.byteLength(raw) > 512 * 1024) throw new Error("O ficheiro de pedido excede 512 KiB.");
|
|
27044
27190
|
let parsed;
|
|
27045
27191
|
try {
|
|
@@ -27063,8 +27209,8 @@ function createForgeDecisionTool(forgeBaseUrl, decisionBaseUrl, paths, responseD
|
|
|
27063
27209
|
try {
|
|
27064
27210
|
const credentials = await readForgeCredentials(paths, forgeBaseUrl);
|
|
27065
27211
|
const probeFile = path14.join(responseDir, ".write-test");
|
|
27066
|
-
await
|
|
27067
|
-
await
|
|
27212
|
+
await writeFile9(probeFile, "", { flag: "wx", mode: 384 });
|
|
27213
|
+
await unlink2(probeFile);
|
|
27068
27214
|
context.abort.throwIfAborted();
|
|
27069
27215
|
await beforeCall?.(responseFile);
|
|
27070
27216
|
const response = await fetch(`${decisionBaseUrl}/v1/decisions`, {
|
|
@@ -27085,7 +27231,7 @@ function createForgeDecisionTool(forgeBaseUrl, decisionBaseUrl, paths, responseD
|
|
|
27085
27231
|
}
|
|
27086
27232
|
if (response.ok && body && typeof body === "object" && body.answers && typeof body.answers === "object") {
|
|
27087
27233
|
try {
|
|
27088
|
-
await
|
|
27234
|
+
await writeFile9(responseFile, raw, { flag: "wx", mode: 384 });
|
|
27089
27235
|
return JSON.stringify({ ...body, response_file: responseFile }, null, 2);
|
|
27090
27236
|
} catch (error45) {
|
|
27091
27237
|
return JSON.stringify({
|
|
@@ -27105,7 +27251,7 @@ function createForgeDecisionTool(forgeBaseUrl, decisionBaseUrl, paths, responseD
|
|
|
27105
27251
|
}
|
|
27106
27252
|
throw new Error(safeError);
|
|
27107
27253
|
} finally {
|
|
27108
|
-
await
|
|
27254
|
+
await rmdir2(responseDir).catch(() => void 0);
|
|
27109
27255
|
}
|
|
27110
27256
|
}
|
|
27111
27257
|
});
|
|
@@ -27126,7 +27272,7 @@ async function fileExists(candidate) {
|
|
|
27126
27272
|
throw error45;
|
|
27127
27273
|
}
|
|
27128
27274
|
}
|
|
27129
|
-
async function ensureJevDirectory(directory, filesystem = { stat: stat3, mkdir:
|
|
27275
|
+
async function ensureJevDirectory(directory, filesystem = { stat: stat3, mkdir: mkdir8 }) {
|
|
27130
27276
|
try {
|
|
27131
27277
|
if ((await filesystem.stat(directory)).isDirectory()) return;
|
|
27132
27278
|
throw new Error(`O destino ${path14.basename(directory)} existe, mas n\xE3o \xE9 uma pasta. Preservar o conte\xFAdo e verificar o caminho.`);
|
|
@@ -27149,7 +27295,7 @@ function canonicalJson(value) {
|
|
|
27149
27295
|
return JSON.stringify(value);
|
|
27150
27296
|
}
|
|
27151
27297
|
async function validateSavedDecision(file2, request) {
|
|
27152
|
-
const body = JSON.parse(await
|
|
27298
|
+
const body = JSON.parse(await readFile14(file2, "utf8"));
|
|
27153
27299
|
const answers = body.answers;
|
|
27154
27300
|
const questions = request.questions;
|
|
27155
27301
|
const usage = body.usage;
|
|
@@ -27160,7 +27306,7 @@ async function validateSavedDecision(file2, request) {
|
|
|
27160
27306
|
}
|
|
27161
27307
|
async function acquireJevRunLock(lockDir) {
|
|
27162
27308
|
try {
|
|
27163
|
-
await
|
|
27309
|
+
await mkdir8(lockDir);
|
|
27164
27310
|
return {};
|
|
27165
27311
|
} catch (error45) {
|
|
27166
27312
|
if (error45.code !== "EEXIST") throw error45;
|
|
@@ -27168,7 +27314,7 @@ async function acquireJevRunLock(lockDir) {
|
|
|
27168
27314
|
let owner;
|
|
27169
27315
|
let ownerRaw;
|
|
27170
27316
|
try {
|
|
27171
|
-
ownerRaw = await
|
|
27317
|
+
ownerRaw = await readFile14(path14.join(lockDir, "owner.json"), "utf8");
|
|
27172
27318
|
owner = JSON.parse(ownerRaw);
|
|
27173
27319
|
} catch {
|
|
27174
27320
|
throw new Error("Chamada interrompida com lock sem identifica\xE7\xE3o; n\xE3o \xE9 poss\xEDvel confirmar que terminou. Preservar o lock e verificar a execu\xE7\xE3o.");
|
|
@@ -27181,9 +27327,9 @@ async function acquireJevRunLock(lockDir) {
|
|
|
27181
27327
|
} catch (error45) {
|
|
27182
27328
|
if (error45.code === "ESRCH") {
|
|
27183
27329
|
const archived = `${lockDir}.interrupted-${createHash7("sha256").update(ownerRaw).digest("hex")}`;
|
|
27184
|
-
await
|
|
27330
|
+
await rename9(lockDir, archived);
|
|
27185
27331
|
try {
|
|
27186
|
-
await
|
|
27332
|
+
await mkdir8(lockDir);
|
|
27187
27333
|
} catch {
|
|
27188
27334
|
throw new Error("Este plano j\xE1 est\xE1 em execu\xE7\xE3o; a recupera\xE7\xE3o ficou preservada.");
|
|
27189
27335
|
}
|
|
@@ -27204,7 +27350,7 @@ function createForgeDecisionRunTool(forgeBaseUrl, decisionBaseUrl, paths) {
|
|
|
27204
27350
|
const planFile = await fileInsideWorkspace(context.directory, args.plan_file);
|
|
27205
27351
|
if (path14.basename(planFile) !== "plan.json" || path14.basename(path14.dirname(planFile)) !== "jev") throw new Error("Indica o ficheiro jev/plan.json.");
|
|
27206
27352
|
const runDir = path14.dirname(path14.dirname(planFile));
|
|
27207
|
-
const plan = JSON.parse(await
|
|
27353
|
+
const plan = JSON.parse(await readFile14(planFile, "utf8"));
|
|
27208
27354
|
if (plan.engine !== "jev-batch" || plan.model !== "typesafe/jev-1.13" || !Array.isArray(plan.batches)) throw new Error("O plano Jev \xE9 inv\xE1lido.");
|
|
27209
27355
|
const allBatches = plan.batches;
|
|
27210
27356
|
const batches = args.batch_id ? allBatches.filter((batch) => batch.batch_id === args.batch_id) : allBatches;
|
|
@@ -27236,7 +27382,7 @@ function createForgeDecisionRunTool(forgeBaseUrl, decisionBaseUrl, paths) {
|
|
|
27236
27382
|
const responseFile = path14.join(responseDir, `${batchId}.response.json`);
|
|
27237
27383
|
const requestFile = await fileInsideWorkspace(context.directory, batch.request_file);
|
|
27238
27384
|
if (path14.dirname(requestFile) !== path14.join(runDir, "jev", "requests")) throw new Error(`${batchId}: pedido fora do run.`);
|
|
27239
|
-
const request = JSON.parse(await
|
|
27385
|
+
const request = JSON.parse(await readFile14(requestFile, "utf8"));
|
|
27240
27386
|
const fingerprint = createHash7("sha256").update(canonicalJson(request)).digest("hex");
|
|
27241
27387
|
if (fingerprint !== batch.request_fingerprint) throw new Error(`${batchId}: fingerprint do pedido n\xE3o corresponde ao plano.`);
|
|
27242
27388
|
if (await fileExists(acceptedFile)) {
|
|
@@ -27251,7 +27397,7 @@ function createForgeDecisionRunTool(forgeBaseUrl, decisionBaseUrl, paths) {
|
|
|
27251
27397
|
}
|
|
27252
27398
|
const attemptsFile = path14.join(attemptsDir, `${batchId}.json`);
|
|
27253
27399
|
let attempts = { request_fingerprint: fingerprint, attempts: 0, response_files: [] };
|
|
27254
|
-
if (await fileExists(attemptsFile)) attempts = JSON.parse(await
|
|
27400
|
+
if (await fileExists(attemptsFile)) attempts = JSON.parse(await readFile14(attemptsFile, "utf8"));
|
|
27255
27401
|
else if (interrupted.batch_id === batchId) {
|
|
27256
27402
|
if (interrupted.request_fingerprint !== fingerprint) throw new Error(`${batchId}: fingerprint da chamada interrompida n\xE3o corresponde ao plano.`);
|
|
27257
27403
|
attempts.attempts = 1;
|
|
@@ -27264,7 +27410,7 @@ function createForgeDecisionRunTool(forgeBaseUrl, decisionBaseUrl, paths) {
|
|
|
27264
27410
|
if (!await fileExists(attemptsFile) && attempts.attempts > 0) await persistAttempts();
|
|
27265
27411
|
const stop = (error45, detail = {}) => JSON.stringify({ verified: false, saved, skipped, recovered, repeated, stopped_at: batchId, ...detail, error: error45 });
|
|
27266
27412
|
const recoverResponse = async () => {
|
|
27267
|
-
const legacyAllocations = interrupted.batch_id === batchId && interrupted.request_fingerprint === fingerprint ? (await
|
|
27413
|
+
const legacyAllocations = interrupted.batch_id === batchId && interrupted.request_fingerprint === fingerprint ? (await readdir7(responseDir)).filter((name) => name.startsWith(".forge-decision-")).map((name) => path14.join(responseDir, name, "response.json")) : [];
|
|
27268
27414
|
const candidates = [...attempts.response_files, ...legacyAllocations];
|
|
27269
27415
|
for (const candidate of new Set(candidates)) {
|
|
27270
27416
|
if (typeof candidate !== "string") throw new Error("Registo de recupera\xE7\xE3o inv\xE1lido.");
|
|
@@ -27329,7 +27475,7 @@ function createForgeDecisionRunTool(forgeBaseUrl, decisionBaseUrl, paths) {
|
|
|
27329
27475
|
let identical = false;
|
|
27330
27476
|
if (error45.code === "EEXIST") {
|
|
27331
27477
|
try {
|
|
27332
|
-
identical = await
|
|
27478
|
+
identical = await readFile14(source, "utf8") === await readFile14(responseFile, "utf8");
|
|
27333
27479
|
} catch {
|
|
27334
27480
|
}
|
|
27335
27481
|
}
|
|
@@ -27337,7 +27483,7 @@ function createForgeDecisionRunTool(forgeBaseUrl, decisionBaseUrl, paths) {
|
|
|
27337
27483
|
return stop("Resposta conclu\xEDda mas n\xE3o guardada no destino do lote; resolver a grava\xE7\xE3o e retomar sem nova chamada.", { recovery_file: source, persistence_code: error45.code });
|
|
27338
27484
|
}
|
|
27339
27485
|
}
|
|
27340
|
-
await
|
|
27486
|
+
await unlink2(source).then(() => rmdir2(path14.dirname(source))).catch(() => void 0);
|
|
27341
27487
|
saved++;
|
|
27342
27488
|
if (args.batch_id) return JSON.stringify({ ...await validateSavedDecision(responseFile, request), response_file: responseFile, repeated });
|
|
27343
27489
|
}
|
|
@@ -27433,7 +27579,7 @@ async function synchronizeAtStartup(forgeBaseUrl, observe, localConfig, input) {
|
|
|
27433
27579
|
const result = localConfig ? await syncLocalWorker(paths, credentials, manifest) : await applyWorkPackManifest(paths, credentials, manifest, {});
|
|
27434
27580
|
if (localConfig) {
|
|
27435
27581
|
appliedLocalSync = { manifest, partialWorkPackSync: result };
|
|
27436
|
-
const config2 = objectRecord2(parseJsonc(await
|
|
27582
|
+
const config2 = objectRecord2(parseJsonc(await readFile14(paths.opencodeConfigPath, "utf8")));
|
|
27437
27583
|
const targetVersion = configuredWorkPackSyncPluginVersion(config2);
|
|
27438
27584
|
if (targetVersion) pluginUpdate = { targetVersion, restartRequired: targetVersion !== PLUGIN_VERSION };
|
|
27439
27585
|
if (pluginUpdate?.restartRequired) {
|
|
@@ -27485,7 +27631,7 @@ async function synchronizeAtStartup(forgeBaseUrl, observe, localConfig, input) {
|
|
|
27485
27631
|
}
|
|
27486
27632
|
async function savedGatewayBaseUrl(paths, credentials, forgeBaseUrl) {
|
|
27487
27633
|
try {
|
|
27488
|
-
const config2 = objectRecord2(parseJsonc(await
|
|
27634
|
+
const config2 = objectRecord2(parseJsonc(await readFile14(paths.opencodeConfigPath, "utf8")));
|
|
27489
27635
|
const options = objectRecord2(objectRecord2(objectRecord2(config2.provider).forge).options);
|
|
27490
27636
|
if (options.apiKey !== credentials.forgeApiKey || typeof options.baseURL !== "string" || !options.baseURL.endsWith("/v1")) return void 0;
|
|
27491
27637
|
const origin = options.baseURL.slice(0, -"/v1".length);
|
|
@@ -27510,7 +27656,7 @@ function resolveStartupPaths() {
|
|
|
27510
27656
|
return resolvePaths(void 0, void 0, env);
|
|
27511
27657
|
}
|
|
27512
27658
|
async function readForgeCredentials(paths, forgeBaseUrl) {
|
|
27513
|
-
const raw = await
|
|
27659
|
+
const raw = await readFile14(paths.opencodeAuthPath, "utf8").catch((error45) => {
|
|
27514
27660
|
if (error45.code === "ENOENT") return void 0;
|
|
27515
27661
|
throw error45;
|
|
27516
27662
|
});
|
|
@@ -27554,12 +27700,12 @@ function objectRecord2(value) {
|
|
|
27554
27700
|
return value && typeof value === "object" && !Array.isArray(value) ? { ...value } : {};
|
|
27555
27701
|
}
|
|
27556
27702
|
async function writeStatus(paths, value) {
|
|
27557
|
-
await
|
|
27703
|
+
await mkdir8(paths.forgeDir, { recursive: true });
|
|
27558
27704
|
const target = path14.join(paths.forgeDir, STATUS_FILENAME);
|
|
27559
27705
|
const temporary = path14.join(paths.forgeDir, `.${STATUS_FILENAME}.${process.pid}.tmp`);
|
|
27560
|
-
await
|
|
27706
|
+
await writeFile9(temporary, `${JSON.stringify(value, null, 2)}
|
|
27561
27707
|
`, { mode: 384 });
|
|
27562
|
-
await
|
|
27708
|
+
await rename9(temporary, target);
|
|
27563
27709
|
}
|
|
27564
27710
|
function safeErrorMessage(error45) {
|
|
27565
27711
|
const message = error45 instanceof Error ? error45.message : "erro desconhecido";
|