@fro.bot/space-bus 0.15.1 → 0.15.2

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/cli.js CHANGED
@@ -1458,16 +1458,18 @@ async function waitForGroupDeath(pid, budgetMs) {
1458
1458
  // src/service.ts
1459
1459
  import {
1460
1460
  chmodSync as chmodSync3,
1461
- closeSync as closeSync4,
1461
+ closeSync as closeSync5,
1462
1462
  existsSync as existsSync3,
1463
1463
  fchmodSync,
1464
- constants as fsConstants4,
1464
+ constants as fsConstants5,
1465
+ lstatSync as lstatSync3,
1465
1466
  mkdirSync as mkdirSync3,
1466
- openSync as openSync5,
1467
- unlinkSync as unlinkSync3
1467
+ openSync as openSync6,
1468
+ readlinkSync,
1469
+ unlinkSync as unlinkSync4
1468
1470
  } from "node:fs";
1469
1471
  import { homedir as homedir5 } from "node:os";
1470
- import { isAbsolute as isAbsolute2, join as join5 } from "node:path";
1472
+ import { dirname as dirname2, isAbsolute as isAbsolute2, join as join6, sep } from "node:path";
1471
1473
 
1472
1474
  // src/launchd.ts
1473
1475
  import { spawn as spawn2 } from "node:child_process";
@@ -1571,7 +1573,7 @@ function writePlistAtomic(path, content) {
1571
1573
  }
1572
1574
  return { ok: true };
1573
1575
  }
1574
- function verifyPlistSafe(path) {
1576
+ function verifyPlistSafe(path, expectedUid = typeof process.getuid === "function" ? process.getuid() : undefined) {
1575
1577
  let mode;
1576
1578
  let uid;
1577
1579
  try {
@@ -1589,7 +1591,7 @@ function verifyPlistSafe(path) {
1589
1591
  } catch (err) {
1590
1592
  return { safe: false, reason: `failed to stat plist: ${String(err)}` };
1591
1593
  }
1592
- if (typeof process.getuid === "function" && uid !== process.getuid()) {
1594
+ if (expectedUid !== undefined && uid !== expectedUid) {
1593
1595
  return {
1594
1596
  safe: false,
1595
1597
  reason: `plist is not owned by the current user (uid ${uid})`
@@ -1689,6 +1691,180 @@ ${result.stderr}`.toLowerCase();
1689
1691
  };
1690
1692
  }
1691
1693
 
1694
+ // src/service-lock.ts
1695
+ import { execFileSync as execFileSync2 } from "node:child_process";
1696
+ import { randomBytes as randomBytes5 } from "node:crypto";
1697
+ import {
1698
+ closeSync as closeSync4,
1699
+ constants as fsConstants4,
1700
+ openSync as openSync5,
1701
+ readFileSync as readFileSync5,
1702
+ unlinkSync as unlinkSync3,
1703
+ writeFileSync as writeFileSync3
1704
+ } from "node:fs";
1705
+ import { join as join5 } from "node:path";
1706
+ import { z as z4 } from "zod";
1707
+ var lockSchema = z4.object({
1708
+ pid: z4.number().int().min(2),
1709
+ startTime: z4.string(),
1710
+ since: z4.number(),
1711
+ token: z4.string().min(16)
1712
+ });
1713
+ function serviceLockPath(stateDir) {
1714
+ return join5(stateDir, "service.lock");
1715
+ }
1716
+ function isErrno(err, code) {
1717
+ return typeof err === "object" && err !== null && "code" in err && err.code === code;
1718
+ }
1719
+ function createExclusive(path, content) {
1720
+ let fd;
1721
+ try {
1722
+ fd = openSync5(path, fsConstants4.O_EXCL | fsConstants4.O_CREAT | fsConstants4.O_WRONLY, 384);
1723
+ } catch (err) {
1724
+ if (isErrno(err, "EEXIST"))
1725
+ return false;
1726
+ throw err;
1727
+ }
1728
+ try {
1729
+ writeFileSync3(fd, content);
1730
+ } finally {
1731
+ closeSync4(fd);
1732
+ }
1733
+ return true;
1734
+ }
1735
+ function readRaw(path) {
1736
+ try {
1737
+ return readFileSync5(path, "utf8");
1738
+ } catch (err) {
1739
+ if (isErrno(err, "ENOENT"))
1740
+ return;
1741
+ throw err;
1742
+ }
1743
+ }
1744
+ function parseLock(raw) {
1745
+ try {
1746
+ const parsed = lockSchema.safeParse(JSON.parse(raw));
1747
+ return parsed.success ? parsed.data : null;
1748
+ } catch {
1749
+ return null;
1750
+ }
1751
+ }
1752
+ function processStartTime(pid) {
1753
+ try {
1754
+ const out = execFileSync2("ps", ["-p", String(pid), "-o", "lstart="], {
1755
+ env: { ...process.env, LC_ALL: "C", TZ: "UTC0" },
1756
+ encoding: "utf8",
1757
+ stdio: ["ignore", "pipe", "ignore"]
1758
+ }).trim();
1759
+ return out.length > 0 ? out : null;
1760
+ } catch {
1761
+ return null;
1762
+ }
1763
+ }
1764
+ function pidMayExist(pid) {
1765
+ try {
1766
+ process.kill(pid, 0);
1767
+ return true;
1768
+ } catch (err) {
1769
+ return !isErrno(err, "ESRCH");
1770
+ }
1771
+ }
1772
+ function ownerMayBeLive(lock) {
1773
+ if (!pidMayExist(lock.pid))
1774
+ return false;
1775
+ if (lock.startTime === "")
1776
+ return true;
1777
+ const current = processStartTime(lock.pid);
1778
+ return current === null || current === lock.startTime;
1779
+ }
1780
+ function busy(lockPath, lock) {
1781
+ return {
1782
+ ok: false,
1783
+ error: `another space-bus service operation is in progress for this roster (held by pid ${lock.pid}; lock ${lockPath}) — retry when it finishes`
1784
+ };
1785
+ }
1786
+ function unattributable(lockPath) {
1787
+ return {
1788
+ ok: false,
1789
+ error: `the service lock ${lockPath} exists but is incomplete or unreadable, so its owner cannot be identified; it may belong to a command that is still starting. Do not remove it unless every space-bus service command for this roster has stopped, then delete the file and retry`
1790
+ };
1791
+ }
1792
+ function makeRelease(lockPath, token) {
1793
+ return () => {
1794
+ try {
1795
+ const raw = readRaw(lockPath);
1796
+ if (raw !== undefined && parseLock(raw)?.token === token) {
1797
+ unlinkSync3(lockPath);
1798
+ }
1799
+ } catch {}
1800
+ };
1801
+ }
1802
+ function reclaim(lockPath, seenRaw, mine, token) {
1803
+ const mutex = `${lockPath}.reclaim`;
1804
+ if (!createExclusive(mutex, String(process.pid))) {
1805
+ return {
1806
+ ok: false,
1807
+ error: `another process is reclaiming the service lock (${mutex}). Do not remove that file unless every space-bus service command for this roster has stopped, then delete it and retry`
1808
+ };
1809
+ }
1810
+ try {
1811
+ const nowRaw = readRaw(lockPath);
1812
+ if (nowRaw !== undefined && nowRaw !== seenRaw) {
1813
+ return contended(lockPath, nowRaw);
1814
+ }
1815
+ if (nowRaw !== undefined)
1816
+ unlinkSync3(lockPath);
1817
+ if (!createExclusive(lockPath, mine)) {
1818
+ return contended(lockPath, readRaw(lockPath));
1819
+ }
1820
+ return { ok: true, lock: { release: makeRelease(lockPath, token) } };
1821
+ } finally {
1822
+ try {
1823
+ unlinkSync3(mutex);
1824
+ } catch {}
1825
+ }
1826
+ }
1827
+ function contended(lockPath, raw) {
1828
+ const held = raw === undefined ? null : parseLock(raw);
1829
+ return held ? busy(lockPath, held) : unattributable(lockPath);
1830
+ }
1831
+ function tryAcquire(stateDir) {
1832
+ const lockPath = serviceLockPath(stateDir);
1833
+ const token = randomBytes5(16).toString("hex");
1834
+ const mine = JSON.stringify({
1835
+ pid: process.pid,
1836
+ startTime: processStartTime(process.pid) ?? "",
1837
+ since: Date.now(),
1838
+ token
1839
+ });
1840
+ const won = () => ({
1841
+ ok: true,
1842
+ lock: { release: makeRelease(lockPath, token) }
1843
+ });
1844
+ if (createExclusive(lockPath, mine))
1845
+ return won();
1846
+ const seenRaw = readRaw(lockPath);
1847
+ if (seenRaw === undefined) {
1848
+ return createExclusive(lockPath, mine) ? won() : contended(lockPath, readRaw(lockPath));
1849
+ }
1850
+ const held = parseLock(seenRaw);
1851
+ if (held === null)
1852
+ return unattributable(lockPath);
1853
+ if (ownerMayBeLive(held))
1854
+ return busy(lockPath, held);
1855
+ return reclaim(lockPath, seenRaw, mine, token);
1856
+ }
1857
+ function acquireServiceLock(stateDir) {
1858
+ try {
1859
+ return tryAcquire(stateDir);
1860
+ } catch (err) {
1861
+ return {
1862
+ ok: false,
1863
+ error: `failed to acquire the service lock ${serviceLockPath(stateDir)}: ${String(err)}`
1864
+ };
1865
+ }
1866
+ }
1867
+
1692
1868
  // src/service.ts
1693
1869
  var NOT_SUPPORTED_ERROR = "space-bus service is not supported on this platform (v1 supports macOS/launchd)";
1694
1870
  var EPHEMERAL_CACHE_SEGMENTS = [
@@ -1718,7 +1894,8 @@ function resolveDeps(deps) {
1718
1894
  now: deps.now ?? Date.now,
1719
1895
  verifyBudgetMs: deps.verifyBudgetMs ?? DEFAULT_VERIFY_BUDGET_MS,
1720
1896
  verifyPollMs: deps.verifyPollMs ?? DEFAULT_VERIFY_POLL_MS,
1721
- launchAgentsDir: deps.launchAgentsDir ?? join5(homedir5(), "Library", "LaunchAgents")
1897
+ launchAgentsDir: deps.launchAgentsDir ?? join6(homedir5(), "Library", "LaunchAgents"),
1898
+ plistOwnerUid: deps.plistOwnerUid
1722
1899
  };
1723
1900
  }
1724
1901
  function isEphemeralCachePath(path) {
@@ -1742,10 +1919,96 @@ function identityFor(rosterPath, launchAgentsDir) {
1742
1919
  stateDir: stateDirFor(rosterPath)
1743
1920
  };
1744
1921
  }
1922
+ var MAX_ROSTER_LINK_HOPS = 40;
1923
+ function statStep(path) {
1924
+ try {
1925
+ return lstatSync3(path).isSymbolicLink() ? { kind: "link", target: readlinkSync(path) } : { kind: "plain" };
1926
+ } catch (err) {
1927
+ return isEnoent3(err) ? { kind: "missing" } : { kind: "error", detail: `${path}: ${String(err)}` };
1928
+ }
1929
+ }
1930
+ function splitPath(path) {
1931
+ return path.split(sep).filter((s) => s !== "" && s !== ".");
1932
+ }
1933
+ function realpathLoose(input) {
1934
+ const fail = (detail) => ({
1935
+ ok: false,
1936
+ error: `cannot resolve roster path ${input}: ${detail}`
1937
+ });
1938
+ let pending = splitPath(input);
1939
+ let current = sep;
1940
+ let hops = 0;
1941
+ while (pending.length > 0) {
1942
+ const [segment = "", ...rest] = pending;
1943
+ pending = rest;
1944
+ if (segment === "..") {
1945
+ current = dirname2(current);
1946
+ continue;
1947
+ }
1948
+ const candidate = join6(current, segment);
1949
+ const step = statStep(candidate);
1950
+ if (step.kind === "missing") {
1951
+ return { ok: true, value: join6(candidate, ...rest) };
1952
+ }
1953
+ if (step.kind === "plain") {
1954
+ current = candidate;
1955
+ continue;
1956
+ }
1957
+ if (step.kind === "error")
1958
+ return fail(step.detail);
1959
+ hops += 1;
1960
+ if (hops > MAX_ROSTER_LINK_HOPS) {
1961
+ return fail("roster symlink chain loops or is too long");
1962
+ }
1963
+ if (isAbsolute2(step.target))
1964
+ current = sep;
1965
+ pending = [...splitPath(step.target), ...pending];
1966
+ }
1967
+ return { ok: true, value: current };
1968
+ }
1969
+ function isSymlink(path) {
1970
+ try {
1971
+ return lstatSync3(path).isSymbolicLink();
1972
+ } catch {
1973
+ return false;
1974
+ }
1975
+ }
1976
+ function resolveIdentity(rosterPath, launchAgentsDir) {
1977
+ const given = identityFor(rosterPath, launchAgentsDir);
1978
+ const walked = isAbsolute2(rosterPath) ? realpathLoose(rosterPath) : { ok: true, value: rosterPath };
1979
+ if (!walked.ok)
1980
+ return walked;
1981
+ if (walked.value === rosterPath)
1982
+ return { ok: true, value: { ...given, rosterPath } };
1983
+ const canonical = identityFor(walked.value, launchAgentsDir);
1984
+ const hasCanonical = existsSync3(canonical.plistFilePath);
1985
+ const hasGiven = existsSync3(given.plistFilePath);
1986
+ if (hasCanonical && hasGiven) {
1987
+ return {
1988
+ ok: false,
1989
+ error: `ambiguous service identity for roster ${rosterPath}: a plist exists for both ${walked.value} (${canonical.plistFilePath}) and the path as given (${given.plistFilePath}); boot out and remove the stale one by hand`
1990
+ };
1991
+ }
1992
+ const useCanonical = hasCanonical || !hasGiven && isSymlink(rosterPath);
1993
+ return useCanonical ? { ok: true, value: { ...canonical, rosterPath: walked.value } } : { ok: true, value: { ...given, rosterPath } };
1994
+ }
1995
+ async function withServiceLock(stateDir, run) {
1996
+ const dir = ensureStateDir(stateDir);
1997
+ if (!dir.ok)
1998
+ return dir;
1999
+ const acquired = acquireServiceLock(stateDir);
2000
+ if (!acquired.ok)
2001
+ return acquired;
2002
+ try {
2003
+ return await run();
2004
+ } finally {
2005
+ acquired.lock.release();
2006
+ }
2007
+ }
1745
2008
  function preCreateLog(path) {
1746
2009
  let fd;
1747
2010
  try {
1748
- fd = openSync5(path, fsConstants4.O_WRONLY | fsConstants4.O_APPEND | fsConstants4.O_CREAT | fsConstants4.O_NOFOLLOW, 384);
2011
+ fd = openSync6(path, fsConstants5.O_WRONLY | fsConstants5.O_APPEND | fsConstants5.O_CREAT | fsConstants5.O_NOFOLLOW, 384);
1749
2012
  } catch (err) {
1750
2013
  if (isSymlinkError(err)) {
1751
2014
  return { ok: false, error: `refusing symlinked log path: ${path}` };
@@ -1757,7 +2020,7 @@ function preCreateLog(path) {
1757
2020
  } catch (err) {
1758
2021
  return { ok: false, error: `failed to chmod log ${path}: ${String(err)}` };
1759
2022
  } finally {
1760
- closeSync4(fd);
2023
+ closeSync5(fd);
1761
2024
  }
1762
2025
  return { ok: true };
1763
2026
  }
@@ -1878,7 +2141,14 @@ async function installService(rosterPath, deps = {}) {
1878
2141
  const gate = platformGate(resolved.platform);
1879
2142
  if (!gate.ok)
1880
2143
  return { ok: false, error: gate.error };
1881
- const { label, plistFilePath, stateDir } = identityFor(rosterPath, resolved.launchAgentsDir);
2144
+ const identity = resolveIdentity(rosterPath, resolved.launchAgentsDir);
2145
+ if (!identity.ok)
2146
+ return { ok: false, error: identity.error };
2147
+ return withServiceLock(identity.value.stateDir, () => installLocked(resolved, identity.value));
2148
+ }
2149
+ async function installLocked(resolved, identity) {
2150
+ const { label, plistFilePath, stateDir } = identity;
2151
+ const roster = identity.rosterPath;
1882
2152
  const warning = ephemeralCacheWarning(resolved);
1883
2153
  const fsResult = prepareInstallFilesystem(resolved, stateDir);
1884
2154
  if (!fsResult.ok)
@@ -1902,7 +2172,7 @@ async function installService(rosterPath, deps = {}) {
1902
2172
  const plistXml = renderPlist({
1903
2173
  runtime: resolved.execPath,
1904
2174
  cliEntry: resolved.cliEntryPath,
1905
- rosterPath,
2175
+ rosterPath: roster,
1906
2176
  stateDir,
1907
2177
  label,
1908
2178
  path: process.env["PATH"],
@@ -1912,7 +2182,7 @@ async function installService(rosterPath, deps = {}) {
1912
2182
  if (!writeResult.ok) {
1913
2183
  return { ok: false, error: writeResult.error };
1914
2184
  }
1915
- const safety = verifyPlistSafe(plistFilePath);
2185
+ const safety = verifyPlistSafe(plistFilePath, resolved.plistOwnerUid);
1916
2186
  if (!safety.safe) {
1917
2187
  return { ok: false, error: `refusing to install: ${safety.reason}` };
1918
2188
  }
@@ -1926,7 +2196,7 @@ async function installService(rosterPath, deps = {}) {
1926
2196
  error: `launchctl kickstart failed (exit ${kickstartResult.code})${stderrDetail(kickstartResult)}`
1927
2197
  };
1928
2198
  }
1929
- const verified = await verifyRunning(rosterPath, label, resolved, plistFilePath);
2199
+ const verified = await verifyRunning(roster, label, resolved, plistFilePath);
1930
2200
  if (!verified.ok)
1931
2201
  return verified;
1932
2202
  return {
@@ -1962,7 +2232,7 @@ async function verifyRunning(rosterPath, label, resolved, plistFilePath) {
1962
2232
  await resolved.sleep(resolved.verifyPollMs);
1963
2233
  }
1964
2234
  const stateDir = stateDirFor(rosterPath);
1965
- const logPath = join5(stateDir, "service.log");
2235
+ const logPath = join6(stateDir, "service.log");
1966
2236
  return {
1967
2237
  ok: false,
1968
2238
  error: `install did not verify as running within ${resolved.verifyBudgetMs}ms ` + `(loaded=${lastLoaded}, running=${lastRunning}` + `${lastProbeError ? `, last probe error: ${lastProbeError}` : ""})` + ` — label=${label}, plist=${plistFilePath ?? plistPath(label, resolved.launchAgentsDir)}, log=${logPath}`
@@ -1973,7 +2243,13 @@ async function uninstallService(rosterPath, deps = {}) {
1973
2243
  const gate = platformGate(resolved.platform);
1974
2244
  if (!gate.ok)
1975
2245
  return { ok: false, error: gate.error };
1976
- const { label, plistFilePath } = identityFor(rosterPath, resolved.launchAgentsDir);
2246
+ const identity = resolveIdentity(rosterPath, resolved.launchAgentsDir);
2247
+ if (!identity.ok)
2248
+ return { ok: false, error: identity.error };
2249
+ return withServiceLock(identity.value.stateDir, () => uninstallLocked(resolved, identity.value));
2250
+ }
2251
+ async function uninstallLocked(resolved, identity) {
2252
+ const { label, plistFilePath } = identity;
1977
2253
  const job = await probeJob(resolved, label);
1978
2254
  if (!job.ok) {
1979
2255
  return { ok: false, error: `failed to probe job: ${job.error}` };
@@ -1987,7 +2263,7 @@ async function uninstallService(rosterPath, deps = {}) {
1987
2263
  }
1988
2264
  let plistRemoved = false;
1989
2265
  try {
1990
- unlinkSync3(plistFilePath);
2266
+ unlinkSync4(plistFilePath);
1991
2267
  plistRemoved = true;
1992
2268
  } catch (err) {
1993
2269
  if (!isEnoent3(err)) {
@@ -2011,13 +2287,16 @@ async function serviceStatus(rosterPath, deps = {}) {
2011
2287
  const gate = platformGate(resolved.platform);
2012
2288
  if (!gate.ok)
2013
2289
  return { ok: false, error: gate.error };
2014
- const { label, plistFilePath } = identityFor(rosterPath, resolved.launchAgentsDir);
2290
+ const identity = resolveIdentity(rosterPath, resolved.launchAgentsDir);
2291
+ if (!identity.ok)
2292
+ return { ok: false, error: identity.error };
2293
+ const { label, plistFilePath } = identity.value;
2015
2294
  const installed = existsSync3(plistFilePath);
2016
2295
  const job = await probeJob(resolved, label);
2017
2296
  if (!job.ok) {
2018
2297
  return { ok: false, error: `failed to probe job: ${job.error}` };
2019
2298
  }
2020
- const status = resolved.serverStatus(rosterPath);
2299
+ const status = resolved.serverStatus(identity.value.rosterPath);
2021
2300
  return {
2022
2301
  ok: true,
2023
2302
  installed,
@@ -2033,7 +2312,13 @@ async function stopService(rosterPath, deps = {}) {
2033
2312
  const gate = platformGate(resolved.platform);
2034
2313
  if (!gate.ok)
2035
2314
  return { ok: false, error: gate.error };
2036
- const { label } = identityFor(rosterPath, resolved.launchAgentsDir);
2315
+ const identity = resolveIdentity(rosterPath, resolved.launchAgentsDir);
2316
+ if (!identity.ok)
2317
+ return { ok: false, error: identity.error };
2318
+ return withServiceLock(identity.value.stateDir, () => stopLocked(resolved, identity.value));
2319
+ }
2320
+ async function stopLocked(resolved, identity) {
2321
+ const { label } = identity;
2037
2322
  const job = await probeJob(resolved, label);
2038
2323
  if (!job.ok) {
2039
2324
  return { ok: false, error: `failed to probe job: ${job.error}` };
@@ -2051,7 +2336,14 @@ async function startService(rosterPath, deps = {}) {
2051
2336
  const gate = platformGate(resolved.platform);
2052
2337
  if (!gate.ok)
2053
2338
  return { ok: false, error: gate.error };
2054
- const { label, plistFilePath, stateDir } = identityFor(rosterPath, resolved.launchAgentsDir);
2339
+ const identity = resolveIdentity(rosterPath, resolved.launchAgentsDir);
2340
+ if (!identity.ok)
2341
+ return { ok: false, error: identity.error };
2342
+ return withServiceLock(identity.value.stateDir, () => startLocked(resolved, identity.value));
2343
+ }
2344
+ async function startLocked(resolved, identity) {
2345
+ const { label, plistFilePath, stateDir } = identity;
2346
+ const roster = identity.rosterPath;
2055
2347
  if (!existsSync3(plistFilePath)) {
2056
2348
  return {
2057
2349
  ok: false,
@@ -2065,7 +2357,7 @@ async function startService(rosterPath, deps = {}) {
2065
2357
  const plistXml = renderPlist({
2066
2358
  runtime: resolved.execPath,
2067
2359
  cliEntry: resolved.cliEntryPath,
2068
- rosterPath,
2360
+ rosterPath: roster,
2069
2361
  stateDir,
2070
2362
  label,
2071
2363
  path: process.env["PATH"],
@@ -2075,7 +2367,7 @@ async function startService(rosterPath, deps = {}) {
2075
2367
  if (!writeResult.ok) {
2076
2368
  return { ok: false, error: writeResult.error };
2077
2369
  }
2078
- const safety = verifyPlistSafe(plistFilePath);
2370
+ const safety = verifyPlistSafe(plistFilePath, resolved.plistOwnerUid);
2079
2371
  if (!safety.safe) {
2080
2372
  return { ok: false, error: `refusing to start: ${safety.reason}` };
2081
2373
  }
@@ -2093,7 +2385,7 @@ async function startService(rosterPath, deps = {}) {
2093
2385
  error: `launchctl kickstart failed (exit ${kickstartResult.code})${stderrDetail(kickstartResult)}`
2094
2386
  };
2095
2387
  }
2096
- const verified = await verifyRunning(rosterPath, label, resolved, plistFilePath);
2388
+ const verified = await verifyRunning(roster, label, resolved, plistFilePath);
2097
2389
  if (!verified.ok)
2098
2390
  return verified;
2099
2391
  return { ok: true, label, pid: verified.pid };
package/dist/launchd.d.ts CHANGED
@@ -69,9 +69,11 @@ export type PlistSafety = {
69
69
  /**
70
70
  * Refuses a plist that isn't owned by the current user or is
71
71
  * group/world-writable — a tampered plist must never become login-time
72
- * code execution (R15).
72
+ * code execution (R15). `expectedUid` defaults to `process.getuid()`; it is a
73
+ * test seam (a foreign-owned file cannot be made without privilege), and
74
+ * production callers leave it unset.
73
75
  */
74
- export declare function verifyPlistSafe(path: string): PlistSafety;
76
+ export declare function verifyPlistSafe(path: string, expectedUid?: number | undefined): PlistSafety;
75
77
  export interface ExecResult {
76
78
  code: number;
77
79
  stdout: string;
package/dist/mcp.js CHANGED
@@ -3015,7 +3015,7 @@ async function mcpLoadContext(rosterName, ensure = ensureServer) {
3015
3015
  }
3016
3016
  throw ambientError;
3017
3017
  }
3018
- var version = "0.15.1";
3018
+ var version = "0.15.2";
3019
3019
  var server = new McpServer({
3020
3020
  name: "space-bus",
3021
3021
  version
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @experimental
3
+ * Per-roster lifecycle lock for the `space-bus service` mutating verbs
4
+ * (install/start/stop/uninstall). Node-only lane, like `service.ts`.
5
+ *
6
+ * Distinct from `discovery.ts`'s daemon spawn lock (`spawn.lock`): that one
7
+ * serializes spawning the daemon; this one (`service.lock`) serializes the
8
+ * launchd lifecycle (print -> bootout -> plist write -> bootstrap -> verify).
9
+ *
10
+ * It deliberately does NOT reuse `acquireLock`/`releaseLock`:
11
+ * - `acquireLock` reclaims a dead owner's lock with read -> unlink -> create.
12
+ * Two contenders can both read the same dead lock; the slower one then
13
+ * unlinks the faster one's NEW, live lock. Here reclaim runs under a short
14
+ * mutex and re-reads the lock to confirm it is unchanged before unlinking.
15
+ * - `releaseLock` unlinks unconditionally. Here release only unlinks a lock
16
+ * whose token is still ours.
17
+ * - `acquireLock` records an empty start time when `ps` fails and then treats
18
+ * that live owner as dead, treats any `kill(pid, 0)` error (including EPERM)
19
+ * as "dead", keys identity on `comm` (which can change) and reclaims an
20
+ * unparsable lock by age. Here, an owner is dead only when its pid is
21
+ * provably gone (ESRCH) or provably a different process (its start time is
22
+ * readable and differs); anything uncertain counts as live. A malformed or
23
+ * incomplete lock is NEVER reclaimed automatically: age is not evidence of
24
+ * death (a writer paused between create and write can resume later).
25
+ *
26
+ * Semantics: fail-fast (never waits), cross-process (an O_EXCL file under the
27
+ * roster's state dir), and not re-entrant (a second acquire in the same
28
+ * process is refused like any other contender; the verbs take it exactly once).
29
+ *
30
+ * Limitations: same-user trust boundary only; liveness is `kill -0` plus the
31
+ * process start time from `ps` (second granularity). A malformed lock, or a
32
+ * `service.lock.reclaim` left by a process that crashed inside the reclaim
33
+ * window, makes acquisition fail closed until an operator removes the file;
34
+ * automatic cleanup of either would be racy.
35
+ */
36
+ export type ServiceLock = {
37
+ release: () => void;
38
+ };
39
+ export type AcquireResult = {
40
+ ok: true;
41
+ lock: ServiceLock;
42
+ } | {
43
+ ok: false;
44
+ error: string;
45
+ };
46
+ export declare function serviceLockPath(stateDir: string): string;
47
+ /**
48
+ * Process start time (`ps lstart`, second granularity) or null if it cannot be
49
+ * read. Start time only: unlike `comm` it cannot change while the process runs.
50
+ */
51
+ export declare function processStartTime(pid: number): string | null;
52
+ /**
53
+ * Takes the roster's service lock, or fails fast. `stateDir` must already
54
+ * exist at 0700 (the caller hardens it). Never blocks, never steals a lock
55
+ * whose owner may be live, never reclaims a malformed lock, and reports I/O
56
+ * failures as `ok:false` rather than throwing.
57
+ */
58
+ export declare function acquireServiceLock(stateDir: string): AcquireResult;
package/dist/service.d.ts CHANGED
@@ -29,6 +29,12 @@ export interface ServiceDeps {
29
29
  * exercise the default against the real filesystem.
30
30
  */
31
31
  launchAgentsDir?: string;
32
+ /**
33
+ * The uid the written plist must be owned by. Defaults to the process uid.
34
+ * Test seam only: lets a test exercise the foreign-owner refusal without
35
+ * privilege. Production callers leave it unset.
36
+ */
37
+ plistOwnerUid?: number;
32
38
  }
33
39
  export type PlatformGate = {
34
40
  ok: true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fro.bot/space-bus",
3
- "version": "0.15.1",
3
+ "version": "0.15.2",
4
4
  "description": "Space Bus — workspace agent bus for OpenCode. One control agent tasking per-project agents over the OpenCode server API, with an MCP facade for Claude Desktop.",
5
5
  "type": "module",
6
6
  "license": "MIT",