@tangle-network/sdk-cli-runner 0.3.18 → 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.
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
  }
@@ -1396,6 +1407,9 @@ function attachCliProcess(config, parserConfig, run) {
1396
1407
  beginRawCapture() {
1397
1408
  capture.begin();
1398
1409
  },
1410
+ armDeadline(budgetMs) {
1411
+ deadlines.arm(budgetMs);
1412
+ },
1399
1413
  exitPromise
1400
1414
  };
1401
1415
  }
@@ -1760,6 +1774,11 @@ function createHeldSpareRegistry(options = {}) {
1760
1774
  claim,
1761
1775
  adopt: (spare, turn) => {
1762
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
+ }
1763
1782
  try {
1764
1783
  spare.process.beginRawCapture?.();
1765
1784
  } catch (error) {
@@ -1771,6 +1790,7 @@ function createHeldSpareRegistry(options = {}) {
1771
1790
  const kill = () => spare.process.kill();
1772
1791
  if (turn.signal.aborted) kill();
1773
1792
  else turn.signal.addEventListener("abort", kill, { once: true });
1793
+ if (turn.timeoutMs !== void 0) armDeadline?.(turn.timeoutMs);
1774
1794
  return {
1775
1795
  ...spare.process,
1776
1796
  exitPromise: spare.process.exitPromise.finally(() => turn.signal.removeEventListener("abort", kill))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/sdk-cli-runner",
3
- "version": "0.3.18",
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": "57453ab6787f82ae4359215373fc5dd0070d8aa9",
30
- "mergedAt": "2026-11-24T01:40:28-08:00"
29
+ "sourceSha": "bc61bd4c2d084826d3968e8c31196aee2a5e2343",
30
+ "mergedAt": "2026-11-28T03:17:00-08:00"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "tsdown",