@discovery-tree/runner 0.1.24 → 0.1.25

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.
Files changed (3) hide show
  1. package/README.md +6 -2
  2. package/dist/cli.js +139 -21
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -161,8 +161,12 @@ never offered to you. What it may do without asking:
161
161
  still descending from Claude Code by parent process id, or seen descending from it earlier
162
162
  (it looks every 30 seconds, and once more just before it stops Claude Code), with
163
163
  `taskkill /T /F`. Without PowerShell it falls back to those alone. An interrupted turn
164
- leaves it running (unless Claude Code does not answer the interrupt and the SIGINT the
165
- runner then sends ends it). It can be left running only if it moves itself out of that
164
+ leaves it running as long as the agent stops by itself, on its interrupt request or on
165
+ SIGINT, which go to the agent alone. A Stop always ends the turn: Claude Code gets an interrupt
166
+ request, Cursor and Codex get SIGINT, and an agent still running 5 seconds after each step
167
+ gets the next one, SIGTERM and then SIGKILL to its whole tree (pressing Stop again takes
168
+ the next step at once); once it gets to SIGTERM, what the agent started ends with it. It
169
+ can be left running only if it moves itself out of that
166
170
  group (`setsid`, a daemon; on Windows, a process started in the first moments of Claude
167
171
  Code, before it is in its job, whose parent then exited unseen), or if the runner itself is
168
172
  killed with SIGKILL (or the computer loses power). On Windows the jobs end with the runner
package/dist/cli.js CHANGED
@@ -495,6 +495,7 @@ checkout: ${EXAMPLE_CHECKOUT}
495
495
  cwd: .
496
496
  url: http://localhost:8000
497
497
  expect: 6h
498
+ limit: 7h
498
499
  ---
499
500
  Records order books into $DT_JOB_DATA_DIR/books, which the backtest reads.
500
501
  -->
@@ -526,14 +527,23 @@ Rules:
526
527
  - \`url\`: where it serves, when it does: an http://localhost address. Leave it out otherwise.
527
528
  - \`expect\`: how long it should run (\`45m\`, \`6h\`, \`2d\`); leave it out for one that runs
528
529
  until stopped.
530
+ - \`limit\`: how long it may run before Discovery Tree stops it (\`45m\`, \`7h\`, \`2d\`),
531
+ counted from when it starts. Give one for any job that should finish, a little past
532
+ \`expect\`, so one that hangs does not run for ever. Leave it out for one that serves until
533
+ a person stops it.
529
534
  - Below \`---\`, what it is for and where its output goes, at most
530
535
  ${MAX_JOB_REQUEST_PURPOSE_CHARS} characters.
531
536
  - The same job asked for again while it still waits or runs is not asked for twice.
532
- - If a job you asked for fails or is lost, this run hears about it: a message while it is
533
- still going, or a continuation if it has finished. Fix the command and ask again with a
534
- new DT-JOB block. That ask still waits for the owner's approval. After several failures
535
- in a row the run is not told again, and the job says that automatic tries have stopped.
536
- A later run of the same job that exits cleanly starts that count over.
537
+ - When a job you asked for ends (it exits, fails, is lost, or reaches its \`limit\`), this
538
+ run is woken with its report: how it ended, how long it ran, and the last lines of its
539
+ log. A message while the run is still going, or a continuation if it has finished. So
540
+ have the job print a short summary of what it did and where its output is as it ends:
541
+ that is what you will read. Then take the follow-up: check the output, finish the task,
542
+ or fix the command and ask again with a new DT-JOB block (which still waits for the
543
+ owner's approval). A job a person stops does not wake the run. After several failures
544
+ (or limits reached) in a row the run is not woken again, and the job says that
545
+ automatic tries have stopped. A later run of the same job that exits cleanly starts that
546
+ count over.
537
547
  - What the job writes in your checkout is kept for ${TREE_JOB_OUTPUT_KEPT_MS / 864e5} days after the job ends, then removed with
538
548
  the checkout; its data folder stays. Say where its output goes (below \`---\`), and say in a
539
549
  DT-RESULT block where to find it: in the job's data folder (its log names the path), and
@@ -689,7 +699,7 @@ function toolGrantRepositoryKey(url) {
689
699
  var RUN_STUCK_TOOL_CALL_AFTER_MS = 15 * 6e4;
690
700
 
691
701
  // src/version.ts
692
- var RUNNER_VERSION = true ? "0.1.24" : "0.0.0-dev";
702
+ var RUNNER_VERSION = true ? "0.1.25" : "0.0.0-dev";
693
703
  var RUNNER_PLATFORM = `${process.platform}-${process.arch}`;
694
704
 
695
705
  // src/api.ts
@@ -7971,6 +7981,15 @@ var AgentSession = class {
7971
7981
  stopping = null;
7972
7982
  interruptRequested = false;
7973
7983
  interruptTimer = null;
7984
+ /** The process the interrupt under way is for, and how far it has escalated (0: asked). */
7985
+ interruptChild = null;
7986
+ interruptStage = 0;
7987
+ /** per_turn agents: an interrupt came while a turn's process was starting; applied once it is up. */
7988
+ interruptPending = false;
7989
+ /** The turn's result is being reported (push, pull request): see `interrupt`. */
7990
+ endingTurn = false;
7991
+ /** Interrupt command ids handled, so a redelivered command is not a second Stop. */
7992
+ interruptIds = [];
7974
7993
  controlSeq = 0;
7975
7994
  stderrTail = "";
7976
7995
  /** `stderrTail` lost its start (it keeps the last 4,000 characters). */
@@ -8317,31 +8336,110 @@ var AgentSession = class {
8317
8336
  }
8318
8337
  }
8319
8338
  /**
8320
- * Stops the current turn. An agent that takes an interrupt request on stdin (Claude Code,
8321
- * as the Agent SDK sends it) gets one and ends the turn itself; if it has not within the
8322
- * grace period, or takes none, SIGINT does the same the hard way.
8339
+ * Stops the current turn, escalating until it does. An agent that takes an interrupt
8340
+ * request on stdin (Claude Code, as the Agent SDK sends it) gets one and ends the turn
8341
+ * itself; one that takes none (Cursor, Codex) gets SIGINT. Each step not answered within
8342
+ * the grace period gives way to the next: SIGINT, then SIGTERM and SIGKILL to the agent's
8343
+ * whole tree, so a CLI that traps or ignores SIGINT cannot keep a stopped turn running. A
8344
+ * repeated interrupt (the person pressing Stop again) takes the next step at once.
8345
+ *
8346
+ * A Stop stops whatever the agent is doing or about to do. On a per_turn agent, one that
8347
+ * finds a turn's process starting (a message just sent, a resume retry) or messages queued
8348
+ * to start once the last turn is over (while it is reported, or just after) is held, and
8349
+ * applied as soon as that turn's process is up. While a finished turn is being reported
8350
+ * (push, pull request), Claude Code gets the interrupt request alone, which stops a turn
8351
+ * it began meanwhile on a message sent mid-turn and is ignored if it is idle: no signal
8352
+ * escalates against an agent that may be idle. A redelivered command (same `commandId`)
8353
+ * changes nothing. Returns whether anything was sent or held.
8323
8354
  */
8324
- interrupt() {
8355
+ interrupt(commandId) {
8356
+ if (commandId) {
8357
+ if (this.interruptIds.includes(commandId)) return false;
8358
+ this.interruptIds.push(commandId);
8359
+ if (this.interruptIds.length > 50) this.interruptIds.shift();
8360
+ }
8361
+ if (this.driver.processModel === "per_turn") {
8362
+ const queuedNext = this.queuedTurns.length > 0 && (this.endingTurn || !this.record.turnActive);
8363
+ if (this.startingTurn || queuedNext) {
8364
+ this.interruptPending = true;
8365
+ return true;
8366
+ }
8367
+ }
8368
+ if (this.endingTurn) return this.requestInterruptOnly();
8325
8369
  const child = this.child;
8326
8370
  if (!child || !this.record.turnActive) return false;
8371
+ if (this.interruptRequested && this.interruptChild === child) {
8372
+ this.escalateInterrupt(child);
8373
+ return true;
8374
+ }
8327
8375
  this.interruptRequested = true;
8376
+ this.interruptChild = child;
8377
+ this.interruptStage = 0;
8328
8378
  this.permissions.expireAll(PERMISSION_EXPIRED_INTERRUPTED);
8329
- const sigint = () => child.kill("SIGINT");
8330
8379
  const message = this.driver.interruptMessage?.(`dt_interrupt_${++this.controlSeq}`);
8331
8380
  if (message === void 0) {
8332
- sigint();
8381
+ this.escalateInterrupt(child);
8382
+ return true;
8383
+ }
8384
+ try {
8385
+ this.write(message);
8386
+ this.armInterruptTimer(child);
8387
+ } catch {
8388
+ this.escalateInterrupt(child);
8389
+ }
8390
+ return true;
8391
+ }
8392
+ /** The agent's own interrupt request, with no escalation behind it (see `interrupt`). */
8393
+ requestInterruptOnly() {
8394
+ const message = this.driver.interruptMessage?.(`dt_interrupt_${++this.controlSeq}`);
8395
+ if (!this.child || message === void 0) return false;
8396
+ try {
8397
+ this.write(message);
8333
8398
  return true;
8399
+ } catch {
8400
+ return false;
8334
8401
  }
8335
- this.write(message);
8402
+ }
8403
+ /**
8404
+ * The next step of an interrupt: SIGINT to the agent alone (a dev server it started in the
8405
+ * background outlives an interrupted turn, though not the agent, if SIGINT ends it), then
8406
+ * SIGTERM and SIGKILL to its tree. Nothing once the process is gone or the turn is over.
8407
+ */
8408
+ escalateInterrupt(child) {
8409
+ if (this.interruptTimer) clearTimeout(this.interruptTimer);
8410
+ this.interruptTimer = null;
8411
+ if (this.child !== child || !this.record.turnActive || this.endingTurn) return;
8412
+ if (child.exitCode !== null || child.signalCode !== null) return;
8413
+ const stage = ++this.interruptStage;
8414
+ if (stage === 1) {
8415
+ child.kill("SIGINT");
8416
+ } else if (stage === 2) {
8417
+ this.deps.logger.warn(
8418
+ `session ${this.sessionId}: ${this.driver.displayName} did not stop on SIGINT; sending SIGTERM`
8419
+ );
8420
+ signalTree(child, "SIGTERM");
8421
+ } else {
8422
+ this.deps.logger.warn(
8423
+ `session ${this.sessionId}: ${this.driver.displayName} did not stop on SIGTERM; killing it`
8424
+ );
8425
+ signalTree(child, "SIGKILL");
8426
+ return;
8427
+ }
8428
+ this.armInterruptTimer(child);
8429
+ }
8430
+ armInterruptTimer(child) {
8336
8431
  if (this.interruptTimer) clearTimeout(this.interruptTimer);
8337
8432
  this.interruptTimer = setTimeout(() => {
8338
8433
  this.interruptTimer = null;
8339
- if (this.child === child && this.record.turnActive) {
8340
- this.deps.logger.warn(`session ${this.sessionId}: no answer to interrupt; sending SIGINT`);
8341
- sigint();
8434
+ if (this.child === child && this.record.turnActive && !this.endingTurn) {
8435
+ if (this.interruptStage === 0) {
8436
+ this.deps.logger.warn(
8437
+ `session ${this.sessionId}: no answer to interrupt; sending SIGINT`
8438
+ );
8439
+ }
8440
+ this.escalateInterrupt(child);
8342
8441
  }
8343
8442
  }, this.deps.interruptGraceMs ?? 5e3);
8344
- return true;
8345
8443
  }
8346
8444
  /**
8347
8445
  * Ends the process (closing stdin first, so the agent saves its session and exits), and
@@ -8453,6 +8551,11 @@ ${text}` : text;
8453
8551
  this.emit({ type: "status", status: "running" });
8454
8552
  } finally {
8455
8553
  this.startingTurn = false;
8554
+ const pending = this.interruptPending;
8555
+ this.interruptPending = false;
8556
+ if (pending && this.interrupt()) {
8557
+ this.deps.logger.info(`session ${this.sessionId}: interrupting the turn just started`);
8558
+ }
8456
8559
  }
8457
8560
  }
8458
8561
  /** The turn's message went to the agent: the turn is active, and said to be. */
@@ -8696,7 +8799,16 @@ ${text}` : text;
8696
8799
  this.resumeRetry = this.turnText;
8697
8800
  this.record.conversationId = null;
8698
8801
  }
8802
+ /** Reports the turn's end; meanwhile a Stop does not escalate (see `interrupt`). */
8699
8803
  async endTurn(rawTurn) {
8804
+ this.endingTurn = true;
8805
+ try {
8806
+ await this.reportTurnEnd(rawTurn);
8807
+ } finally {
8808
+ this.endingTurn = false;
8809
+ }
8810
+ }
8811
+ async reportTurnEnd(rawTurn) {
8700
8812
  const redactor = this.deps.environment?.redactor;
8701
8813
  const turn = redactor?.active && rawTurn.text ? { ...rawTurn, text: redactor.redact(rawTurn.text) } : rawTurn;
8702
8814
  this.permissions.expireAll(PERMISSION_EXPIRED_TURN_ENDED);
@@ -8706,6 +8818,7 @@ ${text}` : text;
8706
8818
  }
8707
8819
  const interrupted = this.interruptRequested;
8708
8820
  this.interruptRequested = false;
8821
+ this.interruptChild = null;
8709
8822
  if (this.interruptTimer) clearTimeout(this.interruptTimer);
8710
8823
  this.interruptTimer = null;
8711
8824
  const turnFailed = turn.isError && !interrupted;
@@ -8937,9 +9050,10 @@ ${text}` : text;
8937
9050
  const waiters = this.exitWaiters;
8938
9051
  this.exitWaiters = [];
8939
9052
  try {
8940
- if (this.resumeRetry === null && !reason && this.record.turnActive && this.resumeNotFound(true) && this.driver.resumeNotFound(this.stderrTail)) {
9053
+ if (this.resumeRetry === null && !reason && this.record.turnActive && !this.interruptRequested && this.resumeNotFound(true) && this.driver.resumeNotFound(this.stderrTail)) {
8941
9054
  this.planFreshRetry();
8942
9055
  }
9056
+ if (this.resumeRetry !== null && this.interruptRequested) this.resumeRetry = null;
8943
9057
  if (this.resumeRetry !== null) {
8944
9058
  const text = this.resumeRetry;
8945
9059
  this.resumeRetry = null;
@@ -8952,6 +9066,7 @@ ${text}` : text;
8952
9066
  else await this.beginTurn(text);
8953
9067
  } catch (err) {
8954
9068
  this.startingTurn = false;
9069
+ this.interruptPending = false;
8955
9070
  this.emit(
8956
9071
  {
8957
9072
  type: "error",
@@ -8964,6 +9079,7 @@ ${text}` : text;
8964
9079
  return;
8965
9080
  }
8966
9081
  if (reason) {
9082
+ this.interruptRequested = false;
8967
9083
  this.deps.logger.info(
8968
9084
  `session ${this.sessionId}: ${this.driver.displayName} stopped (${reason})`
8969
9085
  );
@@ -8980,6 +9096,7 @@ ${text}` : text;
8980
9096
  return;
8981
9097
  }
8982
9098
  if (!this.record.turnActive) {
9099
+ this.interruptRequested = false;
8983
9100
  if (this.driver.processModel === "persistent") {
8984
9101
  this.deps.logger.info(
8985
9102
  `session ${this.sessionId}: ${this.driver.displayName} exited while idle`,
@@ -9146,7 +9263,7 @@ var SessionManager = class {
9146
9263
  case "send_message":
9147
9264
  return await this.send(cmd.sessionId, cmd.text, cmd.id);
9148
9265
  case "interrupt":
9149
- return this.interrupt(cmd.sessionId);
9266
+ return await this.interrupt(cmd.sessionId, cmd.id);
9150
9267
  case "close_session":
9151
9268
  await this.close(cmd.sessionId);
9152
9269
  return { ok: true };
@@ -9570,10 +9687,11 @@ var SessionManager = class {
9570
9687
  await session.grantTool(cmd.denialId, cmd.scope, cmd.rule.trim());
9571
9688
  return { ok: true };
9572
9689
  }
9573
- interrupt(sessionId) {
9690
+ async interrupt(sessionId, commandId) {
9691
+ await this.starting.get(sessionId)?.catch(() => void 0);
9574
9692
  const session = this.sessions.get(sessionId);
9575
9693
  if (!session) return { ok: true };
9576
- if (session.interrupt()) this.opts.logger.info(`session ${sessionId}: interrupting`);
9694
+ if (session.interrupt(commandId)) this.opts.logger.info(`session ${sessionId}: interrupting`);
9577
9695
  return { ok: true };
9578
9696
  }
9579
9697
  /** Stops the agent, removes the worktree (the branch stays), forgets the session. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discovery-tree/runner",
3
- "version": "0.1.24",
3
+ "version": "0.1.25",
4
4
  "description": "dt-runner: run Discovery Tree agent sessions on your own computer with your own signed-in Claude Code",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {