@discovery-tree/runner 0.1.7 → 0.1.8

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 (2) hide show
  1. package/dist/cli.js +59 -6
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -50,7 +50,7 @@ var RUNNER_EVENT_TEXT_MAX_LENGTH = 64e3;
50
50
  var RUNNER_EVENTS_MAX_BODY_BYTES = 8 * 1024 * 1024;
51
51
 
52
52
  // src/version.ts
53
- var RUNNER_VERSION = true ? "0.1.7" : "0.0.0-dev";
53
+ var RUNNER_VERSION = true ? "0.1.8" : "0.0.0-dev";
54
54
  var RUNNER_PLATFORM = `${process.platform}-${process.arch}`;
55
55
 
56
56
  // src/api.ts
@@ -1821,6 +1821,11 @@ var AgentSession = class {
1821
1821
  * conversation, with the preface the continuation brought.
1822
1822
  */
1823
1823
  resumeRetry = null;
1824
+ /**
1825
+ * The process failed to resume before any turn began (its error result came while a
1826
+ * message was still being delivered): it is ending, so no message may be written to it.
1827
+ */
1828
+ resumeDoomed = false;
1824
1829
  exitWaiters = [];
1825
1830
  /** Settles once the current process has exited and what it left in its group is gone. */
1826
1831
  groupReaped = Promise.resolve();
@@ -1939,7 +1944,7 @@ var AgentSession = class {
1939
1944
  * result that will never come.
1940
1945
  */
1941
1946
  async beginTurn(text, before) {
1942
- while (!this.child || this.stopping) await this.ensureProcess();
1947
+ while (!this.child || this.stopping || this.resumeDoomed) await this.ensureProcess();
1943
1948
  let content = text;
1944
1949
  const preface = this.record.freshPreface;
1945
1950
  if (preface && !this.spawnedWithResume) {
@@ -1974,7 +1979,7 @@ ${text}`;
1974
1979
  return new Promise((resolve) => this.exitWaiters.push(resolve));
1975
1980
  }
1976
1981
  async ensureProcess() {
1977
- while (this.child && this.stopping) await this.waitForExit();
1982
+ while (this.child && (this.stopping || this.resumeDoomed)) await this.waitForExit();
1978
1983
  if (this.child) return;
1979
1984
  await this.prepareEvidenceDir();
1980
1985
  const { bin, features } = await this.deps.claude();
@@ -2013,6 +2018,7 @@ ${text}`;
2013
2018
  });
2014
2019
  this.child = child;
2015
2020
  this.stopping = null;
2021
+ this.resumeDoomed = false;
2016
2022
  this.reapGraceMs = void 0;
2017
2023
  this.stderrTail = "";
2018
2024
  this.sawResultSinceSpawn = false;
@@ -2087,6 +2093,16 @@ ${text}`;
2087
2093
  this.child?.stdin.end();
2088
2094
  return;
2089
2095
  }
2096
+ if (mapped.turnEnd && this.resumeFailedBeforeTurn(mapped.turnEnd.isError)) {
2097
+ this.deps.logger.warn(
2098
+ `session ${this.sessionId}: the Claude Code conversation to resume was not found; starting a fresh one`
2099
+ );
2100
+ this.record.claudeSessionId = null;
2101
+ this.resumeDoomed = true;
2102
+ await this.deps.store.save(this.record);
2103
+ this.child?.stdin.end();
2104
+ return;
2105
+ }
2090
2106
  if (mapped.turnEnd) {
2091
2107
  this.sawResultSinceSpawn = true;
2092
2108
  await this.endTurn(mapped.turnEnd);
@@ -2097,7 +2113,10 @@ ${text}`;
2097
2113
  * (no `system/init`): Claude Code did not find the conversation on this computer.
2098
2114
  */
2099
2115
  resumeNotFound(isError) {
2100
- return isError && this.spawnedWithResume && !this.sawInitSinceSpawn && !this.interruptRequested && this.turnText !== null;
2116
+ return isError && this.spawnedWithResume && !this.sawInitSinceSpawn && !this.interruptRequested && this.record.turnActive && this.turnText !== null;
2117
+ }
2118
+ resumeFailedBeforeTurn(isError) {
2119
+ return isError && this.spawnedWithResume && !this.sawInitSinceSpawn && !this.interruptRequested && !this.record.turnActive;
2101
2120
  }
2102
2121
  planFreshRetry() {
2103
2122
  this.deps.logger.warn(
@@ -2389,6 +2408,7 @@ ${screenshots}`;
2389
2408
  var SCREENSHOTS_TEXT_BUDGET = 59e3;
2390
2409
 
2391
2410
  // src/session-manager.ts
2411
+ var MAX_REFUSED_STARTS = 100;
2392
2412
  var SessionManager = class {
2393
2413
  constructor(opts) {
2394
2414
  this.opts = opts;
@@ -2397,6 +2417,13 @@ var SessionManager = class {
2397
2417
  sessions = /* @__PURE__ */ new Map();
2398
2418
  starting = /* @__PURE__ */ new Map();
2399
2419
  closing = /* @__PURE__ */ new Map();
2420
+ /**
2421
+ * Why a session's last `start_session` was refused, kept so a message that follows it
2422
+ * (handled right after, in the same session order) is answered with that reason — and,
2423
+ * when the start will be delivered again, as retryable, so the message goes again with
2424
+ * it — rather than as a message to a session this runner never heard of. Bounded.
2425
+ */
2426
+ refusedStarts = /* @__PURE__ */ new Map();
2400
2427
  /**
2401
2428
  * Slots taken by dormant sessions being resumed. Counted from the (synchronous) capacity
2402
2429
  * check on, so two resumes in flight cannot both see a free slot while their processes
@@ -2453,7 +2480,7 @@ var SessionManager = class {
2453
2480
  assertSafeSessionId(cmd.sessionId);
2454
2481
  switch (cmd.type) {
2455
2482
  case "start_session":
2456
- return await this.start(cmd);
2483
+ return this.noteStart(cmd.sessionId, await this.start(cmd));
2457
2484
  case "send_message":
2458
2485
  return await this.send(cmd.sessionId, cmd.text, cmd.id);
2459
2486
  case "interrupt":
@@ -2468,9 +2495,24 @@ var SessionManager = class {
2468
2495
  };
2469
2496
  }
2470
2497
  } catch (err) {
2471
- return { ok: false, error: safeError(err) };
2498
+ const refused = { ok: false, error: safeError(err) };
2499
+ return cmd.type === "start_session" ? this.noteStart(cmd.sessionId, refused) : refused;
2472
2500
  }
2473
2501
  }
2502
+ noteStart(sessionId, ack) {
2503
+ this.refusedStarts.delete(sessionId);
2504
+ if (!ack.ok) {
2505
+ this.refusedStarts.set(sessionId, {
2506
+ error: ack.error || "no reason given",
2507
+ retryable: ack.retryable === true
2508
+ });
2509
+ while (this.refusedStarts.size > MAX_REFUSED_STARTS) {
2510
+ const oldest = this.refusedStarts.keys().next().value;
2511
+ this.refusedStarts.delete(oldest);
2512
+ }
2513
+ }
2514
+ return ack;
2515
+ }
2474
2516
  async start(cmd) {
2475
2517
  if (this.sessions.has(cmd.sessionId)) return { ok: true };
2476
2518
  const inFlight = this.starting.get(cmd.sessionId);
@@ -2605,6 +2647,17 @@ var SessionManager = class {
2605
2647
  async send(sessionId, text, commandId) {
2606
2648
  const session = this.sessions.get(sessionId);
2607
2649
  if (!session) {
2650
+ const refused = this.refusedStarts.get(sessionId);
2651
+ if (refused) {
2652
+ return refused.retryable ? {
2653
+ ok: false,
2654
+ retryable: true,
2655
+ error: `The agent has not started yet, so the message will go when it does: ${refused.error}`
2656
+ } : {
2657
+ ok: false,
2658
+ error: `The agent could not be started, so the message was not sent: ${refused.error}`
2659
+ };
2660
+ }
2608
2661
  return {
2609
2662
  ok: false,
2610
2663
  error: "This runner has no such session (it may have been closed or removed)."
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discovery-tree/runner",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
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": {