@tangle-network/sdk-cli-runner 0.3.17 → 0.3.19

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 CHANGED
@@ -159,6 +159,13 @@ interface CliProcess {
159
159
  * no-op for any other process, or once the process has exited.
160
160
  */
161
161
  beginRawCapture?(): void;
162
+ /**
163
+ * Bound a process spawned without `timeoutMs` (a held spare a turn adopted)
164
+ * to `budgetMs` from now. Past it the process ends as one spawned now with
165
+ * that `timeoutMs` would: SIGTERM then escalation, and an exit with
166
+ * `timedOut`, `timeoutReason: "fixed"` and the deadline's diagnostic.
167
+ */
168
+ armDeadline?(budgetMs: number): void;
162
169
  }
163
170
  /**
164
171
  * Configuration for stream parsing.
@@ -274,7 +281,7 @@ declare function execCli<T = unknown>(config: CliSpawnConfig, parserConfig?: Str
274
281
  //#endregion
275
282
  //#region src/held-spare.d.ts
276
283
  /** The process handle a spare keeps and a turn takes over. */
277
- type HeldSpareProcess = Pick<CliProcess, "pid" | "write" | "closeInput" | "kill" | "exitPromise" | "beginRawCapture"> & {
284
+ type HeldSpareProcess = Pick<CliProcess, "pid" | "write" | "closeInput" | "kill" | "exitPromise" | "beginRawCapture" | "armDeadline"> & {
278
285
  stderr?: NodeJS.ReadableStream;
279
286
  };
280
287
  /**
@@ -320,14 +327,17 @@ interface HeldSpareRegistry<TEvent, TExtra extends object = object> {
320
327
  * The turn takes the spare's process: the spare is claimed, its raw capture
321
328
  * opens in the turn's attempt (what the process printed before is the
322
329
  * turn's too), its output so far and from now on goes to the turn's parser,
323
- * and the turn's abort signal stops it. Returns the handle the turn drives.
324
- * Throws for a spare that is stopped or already claimed, and stops the spare
325
- * and throws when its capture cannot open or already failed while held; the
330
+ * the turn's abort signal stops it, and the turn's `timeoutMs` bounds it
331
+ * from now, as it bounds a process the turn spawns. Returns the handle the
332
+ * turn drives. Throws for a spare that is stopped or already claimed, and
333
+ * stops the spare and throws when its capture cannot open or already failed
334
+ * while held, or when the turn has a deadline its process cannot take; the
326
335
  * turn then starts its own process.
327
336
  */
328
337
  adopt(spare: HeldSpare<TEvent, TExtra>, turn: {
329
338
  parser: StreamParserConfig<TEvent>;
330
339
  signal: AbortSignal;
340
+ timeoutMs?: number;
331
341
  }): HeldSpareProcess;
332
342
  /**
333
343
  * End every spare no turn claimed, and wait for the processes to exit.
@@ -434,6 +444,8 @@ type RawCaptureAttempt = {
434
444
  declare function withRawCaptureAttempt<T>(attempt: RawCaptureAttempt, run: () => Promise<T>): Promise<T>;
435
445
  /** The sidecar owns this directory; the CLI's writable HOME cannot contain its evidence. */
436
446
  declare function resolveRawCapturePath(runtimeHome: string): string;
447
+ /** Prepare root-owned executor state without opening or enabling an evidence spool. */
448
+ declare function ensureRawCaptureDirectory(runtimeHome: string): string;
437
449
  /**
438
450
  * Opt one runtime HOME in to full capture, inside the shared byte budget, when
439
451
  * the platform default is off. The Sidecar calls this for a session created with retention
@@ -648,5 +660,5 @@ declare function mergeSpawnEnv(base: NodeJS.ProcessEnv, overrides: Record<string
648
660
  */
649
661
  declare function createStreamParser<T>(config: StreamParserConfig<T>): StreamParser<T>;
650
662
  //#endregion
651
- 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 };
663
+ 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, ensureRawCaptureDirectory, execCli, launchCliProcess, launchDifferences, mergeSpawnEnv, openRawCapture, privilegeDropCommand, privilegeDropIdentity, readProcessIdentity, removeRawCapture, resolveRawCapturePath, restoreTurnPriority, shellQuote, spawnCliProcess, spawnRpcBridge, summarizeRawCapture, trimRawCapture, trustedToolPath, withRawCaptureAttempt, yieldUntilAdopted };
652
664
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -143,26 +143,37 @@ var CliDeadlines = class {
143
143
  timeoutReason;
144
144
  timeoutDiagnostic;
145
145
  fixedTimerId;
146
+ exited = false;
146
147
  constructor(config, child, killer, spawnedAt) {
147
148
  this.config = config;
148
149
  this.child = child;
149
150
  this.killer = killer;
150
151
  this.spawnedAt = spawnedAt;
151
152
  }
152
- arm() {
153
- const { timeoutMs } = this.config;
154
- if (!(timeoutMs && timeoutMs > 0)) return;
155
- this.fixedTimerId = setTimeout(() => this.killOnTimeout(timeoutMs), timeoutMs);
153
+ /**
154
+ * Arm the fixed bound: the spawn's `timeoutMs`, counted from the spawn, or
155
+ * `budgetMs` counted from now, for a process a turn took over after it
156
+ * started (a held spare), as if that turn had spawned it now. A process has
157
+ * one fixed bound: arming a second throws. One that exited arms nothing.
158
+ */
159
+ arm(budgetMs) {
160
+ const adopted = budgetMs !== void 0;
161
+ const timeoutMs = adopted ? budgetMs : this.config.timeoutMs;
162
+ if (this.exited || !(timeoutMs && timeoutMs > 0)) return;
163
+ if (this.fixedTimerId) throw new Error(`"${this.config.command}" already has a deadline`);
164
+ const from = adopted ? Date.now() : this.spawnedAt;
165
+ this.fixedTimerId = setTimeout(() => this.killOnTimeout(timeoutMs, from), timeoutMs);
156
166
  }
157
167
  clearAll() {
168
+ this.exited = true;
158
169
  if (this.fixedTimerId) clearTimeout(this.fixedTimerId);
159
170
  this.killer.clearEscalation();
160
171
  }
161
- killOnTimeout(budgetMs) {
172
+ killOnTimeout(budgetMs, from) {
162
173
  if (this.timedOut) return;
163
174
  this.timedOut = true;
164
175
  this.timeoutReason = "fixed";
165
- this.timeoutDiagnostic = `"${this.config.command}" ran past its deadline of ${budgetMs}ms; killed after ${Date.now() - this.spawnedAt}ms`;
176
+ this.timeoutDiagnostic = `"${this.config.command}" ran past its deadline of ${budgetMs}ms; killed after ${Date.now() - from}ms`;
166
177
  this.killer.kill("SIGTERM");
167
178
  this.killer.escalate(() => this.child.exitCode === null);
168
179
  }
@@ -252,7 +263,7 @@ function resolveRawCapturePath(runtimeHome) {
252
263
  if (!isAbsolute(runtimeHome)) throw new Error("runtimeHome must be absolute for raw capture");
253
264
  return join(RAW_ROOT, createHash("sha256").update(resolve(runtimeHome)).digest("hex"), "process-io.frames");
254
265
  }
255
- function assertProtectedCaptureDirectory(directory) {
266
+ function assertProtectedCaptureDirectory(directory, create = false) {
256
267
  if (process.getuid?.() !== 0) throw new Error("sidecar raw capture requires root ownership");
257
268
  for (const part of [
258
269
  "/root",
@@ -260,10 +271,20 @@ function assertProtectedCaptureDirectory(directory) {
260
271
  RAW_ROOT,
261
272
  directory
262
273
  ]) {
274
+ if (create && part !== "/root") mkdirSync(part, {
275
+ recursive: true,
276
+ mode: 448
277
+ });
263
278
  const entry = lstatSync(part);
264
279
  if (!entry.isDirectory() || entry.isSymbolicLink() || entry.uid !== 0 || (entry.mode & 63) !== 0 || realpathSync(part) !== part) throw new Error(`raw capture parent is not protected: ${part}`);
265
280
  }
266
281
  }
282
+ /** Prepare root-owned executor state without opening or enabling an evidence spool. */
283
+ function ensureRawCaptureDirectory(runtimeHome) {
284
+ const directory = dirname(resolveRawCapturePath(runtimeHome));
285
+ assertProtectedCaptureDirectory(directory, true);
286
+ return directory;
287
+ }
267
288
  /**
268
289
  * Opt one runtime HOME in to full capture, inside the shared byte budget, when
269
290
  * the platform default is off. The Sidecar calls this for a session created with retention
@@ -488,7 +509,8 @@ function openRawCapture(file, options) {
488
509
  };
489
510
  if (mode === "none") return disabled;
490
511
  try {
491
- mkdirSync(directory, {
512
+ if (protectedPath) assertProtectedCaptureDirectory(directory, true);
513
+ else mkdirSync(directory, {
492
514
  recursive: true,
493
515
  mode: 448
494
516
  });
@@ -496,7 +518,6 @@ function openRawCapture(file, options) {
496
518
  if (protectedPath && captureCapacityError(error)) return disabled;
497
519
  throw error;
498
520
  }
499
- if (protectedPath) assertProtectedCaptureDirectory(directory);
500
521
  const parent = lstatSync(directory);
501
522
  if (!parent.isDirectory() || parent.isSymbolicLink() || (parent.mode & 63) !== 0) throw new Error("raw capture directory must be a private real directory");
502
523
  const path = resolve(file);
@@ -1386,6 +1407,9 @@ function attachCliProcess(config, parserConfig, run) {
1386
1407
  beginRawCapture() {
1387
1408
  capture.begin();
1388
1409
  },
1410
+ armDeadline(budgetMs) {
1411
+ deadlines.arm(budgetMs);
1412
+ },
1389
1413
  exitPromise
1390
1414
  };
1391
1415
  }
@@ -1750,6 +1774,11 @@ function createHeldSpareRegistry(options = {}) {
1750
1774
  claim,
1751
1775
  adopt: (spare, turn) => {
1752
1776
  if (spare.state === "stopped" || spare.state === "claimed") throw new Error(`A ${spare.state} spare cannot be adopted`);
1777
+ const armDeadline = spare.process.armDeadline;
1778
+ if (turn.timeoutMs !== void 0 && !armDeadline) {
1779
+ stop(spare);
1780
+ throw new Error("The spare's process cannot take the turn's deadline");
1781
+ }
1753
1782
  try {
1754
1783
  spare.process.beginRawCapture?.();
1755
1784
  } catch (error) {
@@ -1761,6 +1790,7 @@ function createHeldSpareRegistry(options = {}) {
1761
1790
  const kill = () => spare.process.kill();
1762
1791
  if (turn.signal.aborted) kill();
1763
1792
  else turn.signal.addEventListener("abort", kill, { once: true });
1793
+ if (turn.timeoutMs !== void 0) armDeadline?.(turn.timeoutMs);
1764
1794
  return {
1765
1795
  ...spare.process,
1766
1796
  exitPromise: spare.process.exitPromise.finally(() => turn.signal.removeEventListener("abort", kill))
@@ -2597,6 +2627,6 @@ function shellQuote(value) {
2597
2627
  return `'${value.replace(/'/g, `'"'"'`)}'`;
2598
2628
  }
2599
2629
  //#endregion
2600
- export { BridgeClosedError, HELD_SPARE_IDLE_MS, InvalidAttachmentError, JsonRpcError, RAW_CAPTURE_ROOT, RAW_CAPTURE_TOTAL_CAP_BYTES, RpcRequestTimeoutError, 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 };
2630
+ export { BridgeClosedError, HELD_SPARE_IDLE_MS, InvalidAttachmentError, JsonRpcError, RAW_CAPTURE_ROOT, RAW_CAPTURE_TOTAL_CAP_BYTES, RpcRequestTimeoutError, allowRetainedRawCapture, argAfter, configurePrivilegeDrop, createDeferredParser, createHeldSpareRegistry, createStreamParser, ensureRawCaptureDirectory, execCli, launchCliProcess, launchDifferences, mergeSpawnEnv, openRawCapture, privilegeDropCommand, privilegeDropIdentity, readProcessIdentity, removeRawCapture, resolveRawCapturePath, restoreTurnPriority, shellQuote, spawnCliProcess, spawnRpcBridge, summarizeRawCapture, trimRawCapture, trustedToolPath, withRawCaptureAttempt, yieldUntilAdopted };
2601
2631
 
2602
2632
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/sdk-cli-runner",
3
- "version": "0.3.17",
3
+ "version": "0.3.19",
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": "e71746bf3cf7d504d76e0e24659e55501b42a175",
30
- "mergedAt": "2026-11-22T22:31:59-08:00"
29
+ "sourceSha": "bc61bd4c2d084826d3968e8c31196aee2a5e2343",
30
+ "mergedAt": "2026-11-28T03:17:00-08:00"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "tsdown",