@tangle-network/sdk-cli-runner 0.3.15 → 0.3.16
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.d.mts +28 -4
- package/dist/index.mjs +109 -10
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -329,10 +329,34 @@ interface HeldSpareRegistry<TEvent, TExtra extends object = object> {
|
|
|
329
329
|
parser: StreamParserConfig<TEvent>;
|
|
330
330
|
signal: AbortSignal;
|
|
331
331
|
}): HeldSpareProcess;
|
|
332
|
-
/**
|
|
333
|
-
|
|
332
|
+
/**
|
|
333
|
+
* End every spare no turn claimed, and wait for the processes to exit.
|
|
334
|
+
* Answers the spares it let finish where it kills (see `locks`).
|
|
335
|
+
*/
|
|
336
|
+
discard(): Promise<HeldSpareLetFinish[]>;
|
|
337
|
+
}
|
|
338
|
+
/** A spare a discard stopped with a grace where it kills, and why. */
|
|
339
|
+
interface HeldSpareLetFinish {
|
|
340
|
+
readonly home: string;
|
|
341
|
+
/** False when /proc never showed the spare's whole group stopped. */
|
|
342
|
+
readonly frozen: boolean;
|
|
343
|
+
/**
|
|
344
|
+
* How long before the discard the lock it found was last written. One
|
|
345
|
+
* written before the spare started is not the spare's: it was cloned into
|
|
346
|
+
* this HOME with a snapshot, or an earlier process left it.
|
|
347
|
+
*/
|
|
348
|
+
readonly lockAgeMs: number | undefined;
|
|
349
|
+
readonly spareAgeMs: number;
|
|
350
|
+
}
|
|
351
|
+
interface HeldSpareRegistryOptions<TEvent, TExtra extends object = object> {
|
|
352
|
+
/**
|
|
353
|
+
* The lock files this harness's CLI takes while it writes in its HOME. A
|
|
354
|
+
* CLI killed while it holds one leaves it, and the next process there waits
|
|
355
|
+
* on a lock nobody will release; a discard lets such a spare finish.
|
|
356
|
+
*/
|
|
357
|
+
locks?(spare: HeldSpare<TEvent, TExtra>): readonly string[];
|
|
334
358
|
}
|
|
335
|
-
declare function createHeldSpareRegistry<TEvent, TExtra extends object = object>(): HeldSpareRegistry<TEvent, TExtra>;
|
|
359
|
+
declare function createHeldSpareRegistry<TEvent, TExtra extends object = object>(options?: HeldSpareRegistryOptions<TEvent, TExtra>): HeldSpareRegistry<TEvent, TExtra>;
|
|
336
360
|
/** The argument after `flag`, if any. */
|
|
337
361
|
declare function argAfter(args: readonly string[], flag: string): string | undefined;
|
|
338
362
|
/**
|
|
@@ -623,5 +647,5 @@ declare function mergeSpawnEnv(base: NodeJS.ProcessEnv, overrides: Record<string
|
|
|
623
647
|
*/
|
|
624
648
|
declare function createStreamParser<T>(config: StreamParserConfig<T>): StreamParser<T>;
|
|
625
649
|
//#endregion
|
|
626
|
-
export { BridgeClosedError, type CliExecOptions, type CliExecResult, type CliExitResult, type CliLaunch, type CliLaunchSpec, type CliLauncher, type CliProcess, type CliSpawnConfig, type CliStdinValue, HELD_SPARE_IDLE_MS, type HeldSpare, type HeldSpareRegistry, InvalidAttachmentError, JsonRpcError, RAW_CAPTURE_ROOT, RAW_CAPTURE_TOTAL_CAP_BYTES, type RawCaptureProcess, type RpcBridge, type RpcBridgeConfig, type RpcCloseReason, type RpcNotificationHandler, type RpcRequestHandler, type RpcRequestOptions, RpcRequestTimeoutError, type StreamParserConfig, allowRetainedRawCapture, argAfter, configurePrivilegeDrop, createDeferredParser, createHeldSpareRegistry, createStreamParser, execCli, launchCliProcess, launchDifferences, mergeSpawnEnv, openRawCapture, privilegeDropCommand, privilegeDropIdentity, readProcessIdentity, removeRawCapture, resolveRawCapturePath, restoreTurnPriority, shellQuote, spawnCliProcess, spawnRpcBridge, summarizeRawCapture, trimRawCapture, trustedToolPath, withRawCaptureAttempt, yieldUntilAdopted };
|
|
650
|
+
export { BridgeClosedError, type CliExecOptions, type CliExecResult, type CliExitResult, type CliLaunch, type CliLaunchSpec, type CliLauncher, type CliProcess, type CliSpawnConfig, type CliStdinValue, HELD_SPARE_IDLE_MS, type HeldSpare, type HeldSpareLetFinish, type HeldSpareRegistry, InvalidAttachmentError, JsonRpcError, RAW_CAPTURE_ROOT, RAW_CAPTURE_TOTAL_CAP_BYTES, type RawCaptureProcess, type RpcBridge, type RpcBridgeConfig, type RpcCloseReason, type RpcNotificationHandler, type RpcRequestHandler, type RpcRequestOptions, RpcRequestTimeoutError, type StreamParserConfig, allowRetainedRawCapture, argAfter, configurePrivilegeDrop, createDeferredParser, createHeldSpareRegistry, createStreamParser, execCli, launchCliProcess, launchDifferences, mergeSpawnEnv, openRawCapture, privilegeDropCommand, privilegeDropIdentity, readProcessIdentity, removeRawCapture, resolveRawCapturePath, restoreTurnPriority, shellQuote, spawnCliProcess, spawnRpcBridge, summarizeRawCapture, trimRawCapture, trustedToolPath, withRawCaptureAttempt, yieldUntilAdopted };
|
|
627
651
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -9,6 +9,7 @@ import { open } from "node:fs/promises";
|
|
|
9
9
|
import { getPriority, setPriority, tmpdir } from "node:os";
|
|
10
10
|
import { once } from "node:events";
|
|
11
11
|
import { PassThrough } from "node:stream";
|
|
12
|
+
import { setTimeout as setTimeout$1 } from "node:timers/promises";
|
|
12
13
|
//#region src/attachment-error.ts
|
|
13
14
|
/** An attachment cannot be delivered to a native CLI. */
|
|
14
15
|
var InvalidAttachmentError = class extends Error {
|
|
@@ -872,6 +873,26 @@ function reniceTree(root, priority) {
|
|
|
872
873
|
}
|
|
873
874
|
} catch {}
|
|
874
875
|
}
|
|
876
|
+
/**
|
|
877
|
+
* Whether process `leader` and its process group are stopped: every thread of
|
|
878
|
+
* every live member reads stopped in /proc. A stop signal is delivered to each
|
|
879
|
+
* thread on its own, so a group just sent one may still have a thread inside
|
|
880
|
+
* a system call. False where /proc cannot say (not Linux).
|
|
881
|
+
*/
|
|
882
|
+
function groupStopped(leader) {
|
|
883
|
+
try {
|
|
884
|
+
return liveProcesses().filter(({ pid, group }) => pid === leader || group === leader).every(({ pid }) => threadsOf(pid).every((thread) => {
|
|
885
|
+
try {
|
|
886
|
+
const stat = readFileSync(`/proc/${pid}/task/${thread}/stat`, "utf8");
|
|
887
|
+
return /^[TtZX]/.test(stat.slice(stat.lastIndexOf(")") + 2));
|
|
888
|
+
} catch {
|
|
889
|
+
return true;
|
|
890
|
+
}
|
|
891
|
+
}));
|
|
892
|
+
} catch {
|
|
893
|
+
return false;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
875
896
|
function threadsOf(pid) {
|
|
876
897
|
try {
|
|
877
898
|
return readdirSync(`/proc/${pid}/task`);
|
|
@@ -1611,10 +1632,21 @@ const HELD_SPARE_IDLE_MS = 12e4;
|
|
|
1611
1632
|
* Its HOME stays taken, and a turn that waits for it waits, until it exits.
|
|
1612
1633
|
*/
|
|
1613
1634
|
const HELD_SPARE_STOP_GRACE_MS = 2e3;
|
|
1614
|
-
|
|
1635
|
+
/** How long a discard waits to see a killed spare exit before it fails. */
|
|
1636
|
+
const HELD_SPARE_EXIT_BOUND_MS = 2e3;
|
|
1637
|
+
/** How long a discard waits for a spare it froze to read stopped in /proc. */
|
|
1638
|
+
const HELD_SPARE_FREEZE_BOUND_MS = 30;
|
|
1639
|
+
/**
|
|
1640
|
+
* How long a lock may stay, with the spare frozen, before it is the spare's.
|
|
1641
|
+
* Another process in that HOME holds one for a single write and lets go.
|
|
1642
|
+
*/
|
|
1643
|
+
const HELD_SPARE_LOCK_SETTLE_MS = 50;
|
|
1644
|
+
const HELD_SPARE_LOCK_POLL_MS = 5;
|
|
1645
|
+
function createHeldSpareRegistry(options = {}) {
|
|
1615
1646
|
const spares = /* @__PURE__ */ new Map();
|
|
1616
|
-
|
|
1617
|
-
|
|
1647
|
+
/** Takes a spare out of service; false when a turn has it or it is going. */
|
|
1648
|
+
const retire = (spare) => {
|
|
1649
|
+
if (spare.state === "claimed" || spare.state === "stopped") return false;
|
|
1618
1650
|
spare.state = "stopped";
|
|
1619
1651
|
if (spare.idleTimer) clearTimeout(spare.idleTimer);
|
|
1620
1652
|
const forget = () => {
|
|
@@ -1627,9 +1659,74 @@ function createHeldSpareRegistry() {
|
|
|
1627
1659
|
forget();
|
|
1628
1660
|
};
|
|
1629
1661
|
spare.process.exitPromise.then(exited, exited);
|
|
1662
|
+
return true;
|
|
1663
|
+
};
|
|
1664
|
+
const stop = (spare) => {
|
|
1665
|
+
if (!retire(spare)) return;
|
|
1630
1666
|
spare.controller.abort();
|
|
1631
1667
|
spare.process.kill();
|
|
1632
1668
|
};
|
|
1669
|
+
/** Freezes a spare; true once /proc shows its whole group stopped. */
|
|
1670
|
+
const freeze = async (spare) => {
|
|
1671
|
+
spare.process.kill("SIGSTOP");
|
|
1672
|
+
const until = performance.now() + HELD_SPARE_FREEZE_BOUND_MS;
|
|
1673
|
+
while (!(spare.process.pid && groupStopped(spare.process.pid))) {
|
|
1674
|
+
if (performance.now() >= until) return false;
|
|
1675
|
+
await setTimeout$1(1);
|
|
1676
|
+
}
|
|
1677
|
+
return true;
|
|
1678
|
+
};
|
|
1679
|
+
/**
|
|
1680
|
+
* The age of a lock a frozen spare holds, if it holds one. A lock that goes
|
|
1681
|
+
* while the spare cannot run is another process's (a tenant's own turn in
|
|
1682
|
+
* that HOME, writing); one that stays is taken to be the spare's.
|
|
1683
|
+
*/
|
|
1684
|
+
const heldLockAgeMs = async (spare) => {
|
|
1685
|
+
const paths = options.locks?.(spare) ?? [];
|
|
1686
|
+
const ageMs = () => {
|
|
1687
|
+
for (const path of paths) {
|
|
1688
|
+
const lock = statSync(path, { throwIfNoEntry: false });
|
|
1689
|
+
if (lock) return Date.now() - lock.mtimeMs;
|
|
1690
|
+
}
|
|
1691
|
+
};
|
|
1692
|
+
const until = performance.now() + HELD_SPARE_LOCK_SETTLE_MS;
|
|
1693
|
+
for (let age = ageMs(); age !== void 0; age = ageMs()) {
|
|
1694
|
+
if (performance.now() >= until) return age;
|
|
1695
|
+
await setTimeout$1(HELD_SPARE_LOCK_POLL_MS);
|
|
1696
|
+
}
|
|
1697
|
+
};
|
|
1698
|
+
/**
|
|
1699
|
+
* Ends a spare for a discard. The box is changing identity and waits on the
|
|
1700
|
+
* exit, and a spare no turn ran on has nothing worth a graceful stop: it is
|
|
1701
|
+
* killed at once, its whole process group with it. The one exception is a
|
|
1702
|
+
* spare inside a write its harness guards with a lock (`locks`): killed
|
|
1703
|
+
* there it would leave the lock, so it is stopped as a turn stops it and
|
|
1704
|
+
* releases it. The discard freezes the spare before it looks, so the spare
|
|
1705
|
+
* serves nothing and takes no lock in between; one it cannot show frozen is
|
|
1706
|
+
* taken to be writing. A spare a turn already stopped is still exiting, and
|
|
1707
|
+
* is ended the same way.
|
|
1708
|
+
*/
|
|
1709
|
+
const discardOne = async (spare) => {
|
|
1710
|
+
const running = retire(spare);
|
|
1711
|
+
const frozen = options.locks === void 0 || await freeze(spare);
|
|
1712
|
+
const lockAgeMs = frozen ? await heldLockAgeMs(spare) : void 0;
|
|
1713
|
+
if (running) spare.controller.abort();
|
|
1714
|
+
if (frozen && lockAgeMs === void 0) {
|
|
1715
|
+
spare.process.kill("SIGKILL");
|
|
1716
|
+
return { boundMs: HELD_SPARE_EXIT_BOUND_MS };
|
|
1717
|
+
}
|
|
1718
|
+
if (running) spare.process.kill();
|
|
1719
|
+
spare.process.kill("SIGCONT");
|
|
1720
|
+
return {
|
|
1721
|
+
boundMs: HELD_SPARE_STOP_GRACE_MS + HELD_SPARE_EXIT_BOUND_MS,
|
|
1722
|
+
letFinish: {
|
|
1723
|
+
home: spare.home,
|
|
1724
|
+
frozen,
|
|
1725
|
+
lockAgeMs,
|
|
1726
|
+
spareAgeMs: performance.now() - spare.startedAt
|
|
1727
|
+
}
|
|
1728
|
+
};
|
|
1729
|
+
};
|
|
1633
1730
|
const claim = (spare) => {
|
|
1634
1731
|
spare.state = "claimed";
|
|
1635
1732
|
restoreTurnPriority(spare.process.pid);
|
|
@@ -1643,7 +1740,7 @@ function createHeldSpareRegistry() {
|
|
|
1643
1740
|
yieldUntilAdopted(spare.process.pid);
|
|
1644
1741
|
},
|
|
1645
1742
|
has: (home) => spares.has(home),
|
|
1646
|
-
stop,
|
|
1743
|
+
stop: (spare) => stop(spare),
|
|
1647
1744
|
claim,
|
|
1648
1745
|
adopt: (spare, turn) => {
|
|
1649
1746
|
if (spare.state === "stopped" || spare.state === "claimed") throw new Error(`A ${spare.state} spare cannot be adopted`);
|
|
@@ -1664,12 +1761,14 @@ function createHeldSpareRegistry() {
|
|
|
1664
1761
|
};
|
|
1665
1762
|
},
|
|
1666
1763
|
discard: async () => {
|
|
1667
|
-
const
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1764
|
+
const discarded = [...spares.values()];
|
|
1765
|
+
const ended = await Promise.all(discarded.map(discardOne));
|
|
1766
|
+
const boundMs = Math.max(0, ...ended.map((end) => end.boundMs));
|
|
1767
|
+
let bound;
|
|
1768
|
+
if (!await Promise.race([Promise.all(discarded.map((spare) => spare.process.exitPromise.catch(() => void 0))).then(() => true), new Promise((resolve) => {
|
|
1769
|
+
bound = setTimeout(() => resolve(false), boundMs);
|
|
1770
|
+
})]).finally(() => clearTimeout(bound))) throw new Error(`A held spare did not exit within ${boundMs} ms of being stopped`);
|
|
1771
|
+
return ended.flatMap((end) => end.letFinish ? [end.letFinish] : []);
|
|
1673
1772
|
}
|
|
1674
1773
|
};
|
|
1675
1774
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tangle-network/sdk-cli-runner",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.16",
|
|
4
4
|
"description": "Generic CLI process spawning with stdin/stdout streaming and JSONL parsing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"registry": "https://registry.npmjs.org"
|
|
27
27
|
},
|
|
28
28
|
"tangleRelease": {
|
|
29
|
-
"sourceSha": "
|
|
30
|
-
"mergedAt": "2026-11-
|
|
29
|
+
"sourceSha": "ab69b47e583110faa55b91d66c6ccc24c69cc105",
|
|
30
|
+
"mergedAt": "2026-11-22T22:13:55-08:00"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
33
|
"build": "tsdown",
|