@cleocode/caamp 1.3.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1613,120 +1613,9 @@ async function configureProviderGlobalAndProject(provider, options) {
1613
1613
  };
1614
1614
  }
1615
1615
 
1616
- // src/core/lock-utils.ts
1617
- import { open, readFile as readFile6, writeFile as writeFile6, mkdir as mkdir4, rm as rm3, rename, stat } from "fs/promises";
1618
- import { existsSync as existsSync10 } from "fs";
1619
- var LOCK_GUARD_PATH = `${LOCK_FILE_PATH}.lock`;
1620
- var STALE_LOCK_MS = 5e3;
1621
- function sleep(ms) {
1622
- return new Promise((resolve4) => setTimeout(resolve4, ms));
1623
- }
1624
- async function removeStaleLock() {
1625
- try {
1626
- const info = await stat(LOCK_GUARD_PATH);
1627
- if (Date.now() - info.mtimeMs > STALE_LOCK_MS) {
1628
- await rm3(LOCK_GUARD_PATH, { force: true });
1629
- return true;
1630
- }
1631
- } catch {
1632
- }
1633
- return false;
1634
- }
1635
- async function acquireLockGuard(retries = 40, delayMs = 25) {
1636
- await mkdir4(AGENTS_HOME, { recursive: true });
1637
- for (let attempt = 0; attempt < retries; attempt += 1) {
1638
- try {
1639
- const handle = await open(LOCK_GUARD_PATH, "wx");
1640
- await handle.close();
1641
- return;
1642
- } catch (error) {
1643
- if (!(error instanceof Error) || !("code" in error) || error.code !== "EEXIST") {
1644
- throw error;
1645
- }
1646
- if (attempt === 0) {
1647
- const removed = await removeStaleLock();
1648
- if (removed) continue;
1649
- }
1650
- await sleep(delayMs);
1651
- }
1652
- }
1653
- throw new Error("Timed out waiting for lock file guard");
1654
- }
1655
- async function releaseLockGuard() {
1656
- await rm3(LOCK_GUARD_PATH, { force: true });
1657
- }
1658
- async function writeLockFileUnsafe(lock) {
1659
- const tmpPath = `${LOCK_FILE_PATH}.tmp-${process.pid}-${Date.now()}`;
1660
- await writeFile6(tmpPath, JSON.stringify(lock, null, 2) + "\n", "utf-8");
1661
- await rename(tmpPath, LOCK_FILE_PATH);
1662
- }
1663
- async function readLockFile() {
1664
- try {
1665
- if (!existsSync10(LOCK_FILE_PATH)) {
1666
- return { version: 1, skills: {}, mcpServers: {} };
1667
- }
1668
- const content = await readFile6(LOCK_FILE_PATH, "utf-8");
1669
- return JSON.parse(content);
1670
- } catch {
1671
- return { version: 1, skills: {}, mcpServers: {} };
1672
- }
1673
- }
1674
- async function updateLockFile(updater) {
1675
- await acquireLockGuard();
1676
- try {
1677
- const lock = await readLockFile();
1678
- await updater(lock);
1679
- await writeLockFileUnsafe(lock);
1680
- return lock;
1681
- } finally {
1682
- await releaseLockGuard();
1683
- }
1684
- }
1685
-
1686
- // src/core/mcp/lock.ts
1687
- async function recordMcpInstall(serverName, source, sourceType, agents, isGlobal) {
1688
- await updateLockFile((lock) => {
1689
- const now = (/* @__PURE__ */ new Date()).toISOString();
1690
- const existing = lock.mcpServers[serverName];
1691
- lock.mcpServers[serverName] = {
1692
- name: serverName,
1693
- scopedName: serverName,
1694
- source,
1695
- sourceType,
1696
- installedAt: existing?.installedAt ?? now,
1697
- updatedAt: now,
1698
- agents: [.../* @__PURE__ */ new Set([...existing?.agents ?? [], ...agents])],
1699
- canonicalPath: "",
1700
- isGlobal
1701
- };
1702
- });
1703
- }
1704
- async function removeMcpFromLock(serverName) {
1705
- let removed = false;
1706
- await updateLockFile((lock) => {
1707
- if (!(serverName in lock.mcpServers)) return;
1708
- delete lock.mcpServers[serverName];
1709
- removed = true;
1710
- });
1711
- return removed;
1712
- }
1713
- async function getTrackedMcpServers() {
1714
- const lock = await readLockFile();
1715
- return lock.mcpServers;
1716
- }
1717
- async function saveLastSelectedAgents(agents) {
1718
- await updateLockFile((lock) => {
1719
- lock.lastSelectedAgents = agents;
1720
- });
1721
- }
1722
- async function getLastSelectedAgents() {
1723
- const lock = await readLockFile();
1724
- return lock.lastSelectedAgents;
1725
- }
1726
-
1727
1616
  // src/core/mcp/cleo.ts
1728
1617
  import { execFileSync as execFileSync2 } from "child_process";
1729
- import { existsSync as existsSync11 } from "fs";
1618
+ import { existsSync as existsSync10 } from "fs";
1730
1619
  import { homedir as homedir2 } from "os";
1731
1620
  import { isAbsolute as isAbsolute2, resolve as resolve3 } from "path";
1732
1621
  var CLEO_SERVER_NAMES = {
@@ -1820,7 +1709,7 @@ function checkCommandReachability(command) {
1820
1709
  if (hasPathSeparator || command.startsWith("~")) {
1821
1710
  const expanded = expandHome(command);
1822
1711
  const candidate = isAbsolute2(expanded) ? expanded : resolve3(process.cwd(), expanded);
1823
- if (existsSync11(candidate)) {
1712
+ if (existsSync10(candidate)) {
1824
1713
  return { reachable: true, method: "path", detail: candidate };
1825
1714
  }
1826
1715
  return { reachable: false, method: "path", detail: candidate };
@@ -1849,10 +1738,269 @@ function parseEnvAssignments(values) {
1849
1738
  }
1850
1739
  return env;
1851
1740
  }
1741
+ function extractVersionTag(packageSpec) {
1742
+ if (!packageSpec) return void 0;
1743
+ const atIndex = packageSpec.lastIndexOf("@");
1744
+ if (atIndex <= 0) return void 0;
1745
+ return packageSpec.slice(atIndex + 1);
1746
+ }
1852
1747
  function isCleoSource(source) {
1853
1748
  return source.trim().toLowerCase() === "cleo";
1854
1749
  }
1855
1750
 
1751
+ // src/core/lock-utils.ts
1752
+ import { open, readFile as readFile6, writeFile as writeFile6, mkdir as mkdir4, rm as rm3, rename, stat } from "fs/promises";
1753
+ import { existsSync as existsSync11 } from "fs";
1754
+ var LOCK_GUARD_PATH = `${LOCK_FILE_PATH}.lock`;
1755
+ var STALE_LOCK_MS = 5e3;
1756
+ function sleep(ms) {
1757
+ return new Promise((resolve4) => setTimeout(resolve4, ms));
1758
+ }
1759
+ async function removeStaleLock() {
1760
+ try {
1761
+ const info = await stat(LOCK_GUARD_PATH);
1762
+ if (Date.now() - info.mtimeMs > STALE_LOCK_MS) {
1763
+ await rm3(LOCK_GUARD_PATH, { force: true });
1764
+ return true;
1765
+ }
1766
+ } catch {
1767
+ }
1768
+ return false;
1769
+ }
1770
+ async function acquireLockGuard(retries = 40, delayMs = 25) {
1771
+ await mkdir4(AGENTS_HOME, { recursive: true });
1772
+ for (let attempt = 0; attempt < retries; attempt += 1) {
1773
+ try {
1774
+ const handle = await open(LOCK_GUARD_PATH, "wx");
1775
+ await handle.close();
1776
+ return;
1777
+ } catch (error) {
1778
+ if (!(error instanceof Error) || !("code" in error) || error.code !== "EEXIST") {
1779
+ throw error;
1780
+ }
1781
+ if (attempt === 0) {
1782
+ const removed = await removeStaleLock();
1783
+ if (removed) continue;
1784
+ }
1785
+ await sleep(delayMs);
1786
+ }
1787
+ }
1788
+ throw new Error("Timed out waiting for lock file guard");
1789
+ }
1790
+ async function releaseLockGuard() {
1791
+ await rm3(LOCK_GUARD_PATH, { force: true });
1792
+ }
1793
+ async function writeLockFileUnsafe(lock) {
1794
+ const tmpPath = `${LOCK_FILE_PATH}.tmp-${process.pid}-${Date.now()}`;
1795
+ await writeFile6(tmpPath, JSON.stringify(lock, null, 2) + "\n", "utf-8");
1796
+ await rename(tmpPath, LOCK_FILE_PATH);
1797
+ }
1798
+ async function readLockFile() {
1799
+ try {
1800
+ if (!existsSync11(LOCK_FILE_PATH)) {
1801
+ return { version: 1, skills: {}, mcpServers: {} };
1802
+ }
1803
+ const content = await readFile6(LOCK_FILE_PATH, "utf-8");
1804
+ return JSON.parse(content);
1805
+ } catch {
1806
+ return { version: 1, skills: {}, mcpServers: {} };
1807
+ }
1808
+ }
1809
+ async function updateLockFile(updater) {
1810
+ await acquireLockGuard();
1811
+ try {
1812
+ const lock = await readLockFile();
1813
+ await updater(lock);
1814
+ await writeLockFileUnsafe(lock);
1815
+ return lock;
1816
+ } finally {
1817
+ await releaseLockGuard();
1818
+ }
1819
+ }
1820
+
1821
+ // src/core/mcp/lock.ts
1822
+ async function recordMcpInstall(serverName, source, sourceType, agents, isGlobal, version) {
1823
+ await updateLockFile((lock) => {
1824
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1825
+ const existing = lock.mcpServers[serverName];
1826
+ lock.mcpServers[serverName] = {
1827
+ name: serverName,
1828
+ scopedName: serverName,
1829
+ source,
1830
+ sourceType,
1831
+ version: version ?? existing?.version,
1832
+ installedAt: existing?.installedAt ?? now,
1833
+ updatedAt: now,
1834
+ agents: [.../* @__PURE__ */ new Set([...existing?.agents ?? [], ...agents])],
1835
+ canonicalPath: "",
1836
+ isGlobal
1837
+ };
1838
+ });
1839
+ }
1840
+ async function removeMcpFromLock(serverName) {
1841
+ let removed = false;
1842
+ await updateLockFile((lock) => {
1843
+ if (!(serverName in lock.mcpServers)) return;
1844
+ delete lock.mcpServers[serverName];
1845
+ removed = true;
1846
+ });
1847
+ return removed;
1848
+ }
1849
+ async function getTrackedMcpServers() {
1850
+ const lock = await readLockFile();
1851
+ return lock.mcpServers;
1852
+ }
1853
+ async function saveLastSelectedAgents(agents) {
1854
+ await updateLockFile((lock) => {
1855
+ lock.lastSelectedAgents = agents;
1856
+ });
1857
+ }
1858
+ async function getLastSelectedAgents() {
1859
+ const lock = await readLockFile();
1860
+ return lock.lastSelectedAgents;
1861
+ }
1862
+
1863
+ // src/core/mcp/reconcile.ts
1864
+ function inferCleoLockData(config, channel) {
1865
+ const command = typeof config.command === "string" ? config.command : "";
1866
+ const args = Array.isArray(config.args) ? config.args.filter((a) => typeof a === "string") : [];
1867
+ const packageArg = args.find(
1868
+ (a) => a.includes(CLEO_MCP_NPM_PACKAGE)
1869
+ );
1870
+ if (packageArg) {
1871
+ const version = extractVersionTag(packageArg);
1872
+ return {
1873
+ source: packageArg,
1874
+ sourceType: "package",
1875
+ version
1876
+ };
1877
+ }
1878
+ if (channel === "dev" || command.includes("/") || command.includes("\\")) {
1879
+ return {
1880
+ source: command,
1881
+ sourceType: "command",
1882
+ version: void 0
1883
+ };
1884
+ }
1885
+ const full = args.length > 0 ? `${command} ${args.join(" ")}` : command;
1886
+ return {
1887
+ source: full || "unknown",
1888
+ sourceType: "command",
1889
+ version: void 0
1890
+ };
1891
+ }
1892
+ async function reconcileCleoLock(options = {}) {
1893
+ const result = {
1894
+ backfilled: [],
1895
+ pruned: [],
1896
+ alreadyTracked: 0,
1897
+ errors: []
1898
+ };
1899
+ const lockEntries = await getTrackedMcpServers();
1900
+ const providers = getInstalledProviders();
1901
+ const targetProviders = options.providerIds?.length ? providers.filter((p) => options.providerIds.includes(p.id)) : providers;
1902
+ const scopes = [];
1903
+ if (options.global && !options.project) {
1904
+ scopes.push("global");
1905
+ } else if (options.project && !options.global) {
1906
+ scopes.push("project");
1907
+ } else {
1908
+ scopes.push("project", "global");
1909
+ }
1910
+ const groups = /* @__PURE__ */ new Map();
1911
+ const liveCleoServerNames = /* @__PURE__ */ new Set();
1912
+ for (const scope of scopes) {
1913
+ for (const provider of targetProviders) {
1914
+ let entries;
1915
+ try {
1916
+ entries = await listMcpServers(provider, scope);
1917
+ } catch {
1918
+ result.errors.push({
1919
+ message: `Failed to read config for ${provider.id} (${scope})`
1920
+ });
1921
+ continue;
1922
+ }
1923
+ for (const entry of entries) {
1924
+ const channel = resolveChannelFromServerName(entry.name);
1925
+ if (!channel) continue;
1926
+ liveCleoServerNames.add(entry.name);
1927
+ const isGlobal = scope === "global";
1928
+ const groupKey = `${entry.name}:${isGlobal ? "global" : "project"}`;
1929
+ if (lockEntries[entry.name] !== void 0) {
1930
+ const existing2 = groups.get(groupKey);
1931
+ if (!existing2) {
1932
+ result.alreadyTracked++;
1933
+ }
1934
+ continue;
1935
+ }
1936
+ const existing = groups.get(groupKey);
1937
+ if (existing) {
1938
+ if (!existing.agents.includes(provider.id)) {
1939
+ existing.agents.push(provider.id);
1940
+ }
1941
+ } else {
1942
+ groups.set(groupKey, {
1943
+ serverName: entry.name,
1944
+ channel,
1945
+ scope,
1946
+ agents: [provider.id],
1947
+ config: entry.config
1948
+ });
1949
+ }
1950
+ }
1951
+ }
1952
+ }
1953
+ for (const group of groups.values()) {
1954
+ const inferred = inferCleoLockData(group.config, group.channel);
1955
+ if (!options.dryRun) {
1956
+ try {
1957
+ await recordMcpInstall(
1958
+ group.serverName,
1959
+ inferred.source,
1960
+ inferred.sourceType,
1961
+ group.agents,
1962
+ group.scope === "global",
1963
+ inferred.version
1964
+ );
1965
+ } catch (err) {
1966
+ result.errors.push({
1967
+ message: `Failed to backfill ${group.serverName}: ${err instanceof Error ? err.message : String(err)}`
1968
+ });
1969
+ continue;
1970
+ }
1971
+ }
1972
+ result.backfilled.push({
1973
+ serverName: group.serverName,
1974
+ channel: group.channel,
1975
+ scope: group.scope,
1976
+ agents: group.agents,
1977
+ source: inferred.source,
1978
+ sourceType: inferred.sourceType,
1979
+ version: inferred.version
1980
+ });
1981
+ }
1982
+ if (options.prune) {
1983
+ for (const [serverName] of Object.entries(lockEntries)) {
1984
+ const channel = resolveChannelFromServerName(serverName);
1985
+ if (!channel) continue;
1986
+ if (!liveCleoServerNames.has(serverName)) {
1987
+ if (!options.dryRun) {
1988
+ try {
1989
+ await removeMcpFromLock(serverName);
1990
+ } catch (err) {
1991
+ result.errors.push({
1992
+ message: `Failed to prune ${serverName}: ${err instanceof Error ? err.message : String(err)}`
1993
+ });
1994
+ continue;
1995
+ }
1996
+ }
1997
+ result.pruned.push(serverName);
1998
+ }
1999
+ }
2000
+ }
2001
+ return result;
2002
+ }
2003
+
1856
2004
  // src/core/sources/parser.ts
1857
2005
  var GITHUB_SHORTHAND = /^([a-zA-Z0-9_.-]+)\/([a-zA-Z0-9_.-]+)(?:\/(.+))?$/;
1858
2006
  var GITHUB_URL = /^https?:\/\/(?:www\.)?github\.com\/([^/]+)\/([^/]+)(?:\/(?:tree|blob)\/([^/]+)(?:\/(.+))?)?/;
@@ -3817,19 +3965,22 @@ export {
3817
3965
  applyMcpInstallWithPolicy,
3818
3966
  updateInstructionsSingleOperation,
3819
3967
  configureProviderGlobalAndProject,
3820
- readLockFile,
3821
- recordMcpInstall,
3822
- removeMcpFromLock,
3823
- getTrackedMcpServers,
3824
- saveLastSelectedAgents,
3825
- getLastSelectedAgents,
3826
3968
  normalizeCleoChannel,
3827
3969
  resolveCleoServerName,
3828
3970
  resolveChannelFromServerName,
3829
3971
  buildCleoProfile,
3830
3972
  checkCommandReachability,
3831
3973
  parseEnvAssignments,
3974
+ extractVersionTag,
3832
3975
  isCleoSource,
3976
+ readLockFile,
3977
+ recordMcpInstall,
3978
+ removeMcpFromLock,
3979
+ getTrackedMcpServers,
3980
+ saveLastSelectedAgents,
3981
+ getLastSelectedAgents,
3982
+ inferCleoLockData,
3983
+ reconcileCleoLock,
3833
3984
  parseSource,
3834
3985
  isMarketplaceScoped,
3835
3986
  formatNetworkError,
@@ -3869,4 +4020,4 @@ export {
3869
4020
  toSarif,
3870
4021
  validateSkill
3871
4022
  };
3872
- //# sourceMappingURL=chunk-DT22SZ7X.js.map
4023
+ //# sourceMappingURL=chunk-G7UPJOYG.js.map