@discovery-tree/runner 0.1.6 → 0.1.7

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 -1
  2. package/dist/cli.js +172 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -207,7 +207,12 @@ url."git@my-alias:".insteadOf "git@github.com:"`), which git applies after the r
207
207
  4. At the end of every turn the runner commits anything left uncommitted
208
208
  (`git add -A && git commit`, only if the worktree is dirty) and runs
209
209
  `git push -u origin HEAD:<workBranch>` when the branch has commits on top of its base. Discovery
210
- Tree opens the pull request from that branch.
210
+ Tree opens the pull request from that branch with its GitHub App. When the app is not installed
211
+ on the repository, Discovery Tree asks the runner to open it instead, as you: after the push the
212
+ runner runs `gh pr create` (or finds the pull request already open with `gh pr list`), titled
213
+ after the task, with the agent's final message as its description. That needs the
214
+ [GitHub CLI](https://cli.github.com) signed in (`gh auth login`); without it the turn says why,
215
+ and you open the pull request from the branch yourself.
211
216
  5. When Discovery Tree closes the session, the worktree is removed (`git worktree remove --force`).
212
217
  The branch stays, locally and on the remote.
213
218
 
package/dist/cli.js CHANGED
@@ -5,6 +5,19 @@ import { hostname } from "node:os";
5
5
  import { createInterface as createInterface2 } from "node:readline/promises";
6
6
  import { parseArgs } from "node:util";
7
7
 
8
+ // ../contracts/src/agent-config.ts
9
+ var KEYED_AGENT_PROVIDER_TYPES = [
10
+ "cursor_cloud",
11
+ "openai",
12
+ "anthropic",
13
+ "webhook"
14
+ ];
15
+ var WORKSPACE_ONLY_KEY_PROVIDERS = ["voyage"];
16
+ var WORKSPACE_KEY_PROVIDER_TYPES = [
17
+ ...KEYED_AGENT_PROVIDER_TYPES,
18
+ ...WORKSPACE_ONLY_KEY_PROVIDERS
19
+ ];
20
+
8
21
  // ../contracts/src/ui-evidence.ts
9
22
  var UI_EVIDENCE_EXPECTATION = [
10
23
  "Visual evidence (required for user-visible changes): if your change alters anything a person sees or interacts with \u2014 a component, layout, styling, copy, empty state, or navigation \u2014 capture a screenshot of the change and embed it in the pull request description using markdown image syntax. Attach one image per state you changed, including before/after when the change modifies existing UI, and both themes when the product has light and dark.",
@@ -24,6 +37,7 @@ var DEFAULT_AGENT_SYSTEM_PROMPT = [
24
37
  // ../contracts/src/agent-runs.ts
25
38
  var AGENT_RUN_MAX_WALL_MS = 6 * 60 * 60 * 1e3;
26
39
  var AGENT_RUN_ABANDONED_GRACE_MS = 30 * 60 * 1e3;
40
+ var AGENT_RUN_AUTO_RECOVER_MAX_MS = 5 * 6e4;
27
41
 
28
42
  // ../contracts/src/agent-work.ts
29
43
  var AGENT_WORK_IDLE_AFTER_MS = 24 * 60 * 60 * 1e3;
@@ -36,7 +50,7 @@ var RUNNER_EVENT_TEXT_MAX_LENGTH = 64e3;
36
50
  var RUNNER_EVENTS_MAX_BODY_BYTES = 8 * 1024 * 1024;
37
51
 
38
52
  // src/version.ts
39
- var RUNNER_VERSION = true ? "0.1.6" : "0.0.0-dev";
53
+ var RUNNER_VERSION = true ? "0.1.7" : "0.0.0-dev";
40
54
  var RUNNER_PLATFORM = `${process.platform}-${process.arch}`;
41
55
 
42
56
  // src/api.ts
@@ -1375,6 +1389,94 @@ import { spawn as spawn3 } from "node:child_process";
1375
1389
  import { basename, dirname as dirname4 } from "node:path";
1376
1390
  import { createInterface } from "node:readline";
1377
1391
 
1392
+ // src/pull-request.ts
1393
+ var PULL_REQUEST_BODY_MAX_CHARS = 6e4;
1394
+ var GH_TIMEOUT_MS = 6e4;
1395
+ var PR_URL_RE = /https:\/\/github\.com\/[^/\s]+\/[^/\s]+\/pull\/\d+/;
1396
+ async function openPullRequest(input, exec2 = exec) {
1397
+ try {
1398
+ const listed = await exec2(
1399
+ "gh",
1400
+ [
1401
+ "pr",
1402
+ "list",
1403
+ "--repo",
1404
+ input.repo,
1405
+ "--head",
1406
+ input.head,
1407
+ "--state",
1408
+ "open",
1409
+ "--json",
1410
+ "url",
1411
+ "--limit",
1412
+ "1"
1413
+ ],
1414
+ { timeoutMs: GH_TIMEOUT_MS }
1415
+ );
1416
+ if (listed.code !== 0) return { status: "failed", error: ghError("pr list", listed) };
1417
+ const existing = PR_URL_RE.exec(listed.stdout)?.[0];
1418
+ if (existing) {
1419
+ const appendError = input.appendIfExisting ? await appendToDescription(existing, input.appendIfExisting, exec2) : null;
1420
+ return { status: "existing", url: existing, ...appendError ? { appendError } : {} };
1421
+ }
1422
+ const created = await exec2(
1423
+ "gh",
1424
+ [
1425
+ "pr",
1426
+ "create",
1427
+ "--repo",
1428
+ input.repo,
1429
+ "--head",
1430
+ input.head,
1431
+ ...input.base ? ["--base", input.base] : [],
1432
+ "--title",
1433
+ input.title,
1434
+ "--body-file",
1435
+ "-"
1436
+ ],
1437
+ { timeoutMs: GH_TIMEOUT_MS, input: input.body.slice(0, PULL_REQUEST_BODY_MAX_CHARS) }
1438
+ );
1439
+ const url = PR_URL_RE.exec(created.stdout)?.[0];
1440
+ if (created.code === 0 && url) return { status: "opened", url };
1441
+ if (/no commits between/i.test(created.stderr)) return { status: "no_changes" };
1442
+ return { status: "failed", error: ghError("pr create", created) };
1443
+ } catch (err) {
1444
+ const missing = err?.code === "ENOENT";
1445
+ return {
1446
+ status: "failed",
1447
+ error: missing ? "the GitHub CLI (`gh`) is not installed on this computer; see https://cli.github.com" : scrubSecrets(err instanceof Error ? err.message : String(err)).slice(0, 300)
1448
+ };
1449
+ }
1450
+ }
1451
+ async function appendToDescription(url, addition, exec2) {
1452
+ const images = [...addition.matchAll(/\]\((https?:\/\/[^)\s]+)\)/g)].map((m) => m[1]);
1453
+ if (!images.length) return null;
1454
+ const viewed = await exec2("gh", ["pr", "view", url, "--json", "body", "--jq", ".body"], {
1455
+ timeoutMs: GH_TIMEOUT_MS
1456
+ });
1457
+ if (viewed.code !== 0) return ghError("pr view", viewed);
1458
+ const body = viewed.stdout.replace(/\n$/, "");
1459
+ if (images.every((u) => body.includes(u))) return null;
1460
+ const text = addition.replace(/^## Screenshots$/m, "## Screenshots from a later turn");
1461
+ const next = body ? `${body.trimEnd()}
1462
+
1463
+ ${text}` : text;
1464
+ if (next.length > PULL_REQUEST_BODY_MAX_CHARS)
1465
+ return "the description is too long to add them to";
1466
+ const edited = await exec2("gh", ["pr", "edit", url, "--body-file", "-"], {
1467
+ timeoutMs: GH_TIMEOUT_MS,
1468
+ input: next
1469
+ });
1470
+ return edited.code === 0 ? null : ghError("pr edit", edited);
1471
+ }
1472
+ function ghError(command, res) {
1473
+ const detail = (res.stderr || res.stdout).trim().split("\n").slice(-3).join(" ");
1474
+ if (/gh auth login|not logged in|authentication/i.test(detail)) {
1475
+ return "the GitHub CLI is not signed in; run `gh auth login` on this computer";
1476
+ }
1477
+ return scrubSecrets(`gh ${command} failed${detail ? `: ${detail}` : ""}`).slice(0, 300);
1478
+ }
1479
+
1378
1480
  // src/process-tree.ts
1379
1481
  import { spawn as spawn2 } from "node:child_process";
1380
1482
  var OWN_PROCESS_GROUP = process.platform !== "win32";
@@ -2026,15 +2128,20 @@ ${text}`;
2026
2128
  });
2027
2129
  return "";
2028
2130
  });
2131
+ const failed = turnFailed || push.failed;
2132
+ const text = withScreenshots(
2133
+ interrupted ? turn.isError ? null : turn.text : turn.text,
2134
+ screenshots
2135
+ );
2136
+ const prUrl = push.branch ? await this.openPullRequest(text, screenshots) : null;
2029
2137
  this.record.turnActive = false;
2030
2138
  await this.deps.store.save(this.record);
2031
- const failed = turnFailed || push.failed;
2032
- const text = interrupted ? turn.isError ? null : turn.text : turn.text;
2033
2139
  this.emit({
2034
2140
  type: "result",
2035
2141
  status: failed ? "failed" : "idle",
2036
- text: withScreenshots(text, screenshots),
2142
+ text,
2037
2143
  branch: push.branch,
2144
+ ...prUrl ? { prUrl } : {},
2038
2145
  // The turn's work is only on this computer (committing or pushing it failed): a
2039
2146
  // continuation of the run must come back to this runner to find it.
2040
2147
  ...push.failed ? { unpushedWork: true } : {},
@@ -2048,6 +2155,55 @@ ${text}`;
2048
2155
  { pushed: push.branch ?? void 0 }
2049
2156
  );
2050
2157
  }
2158
+ /**
2159
+ * When the runner opens this session's pull request (the GitHub App cannot), opens it from
2160
+ * the work branch with the person's `gh`, or finds the one already open; its URL, or null.
2161
+ * A failure is said in the conversation, and the server then shows the branch instead.
2162
+ */
2163
+ async openPullRequest(text, screenshots) {
2164
+ const { pullRequest, workBranch, repository, baseRef } = this.record;
2165
+ const repo = parseRepoUrl(repository ?? "");
2166
+ if (!pullRequest || !workBranch || !repo) return null;
2167
+ const body = [
2168
+ text?.trim() || "Work by the Claude Code (local) agent.",
2169
+ "",
2170
+ "---",
2171
+ pullRequest.footer
2172
+ ].join("\n");
2173
+ const outcome = await (this.deps.openPullRequest ?? openPullRequest)({
2174
+ repo: `${repo.owner}/${repo.repo}`,
2175
+ head: workBranch,
2176
+ base: baseRef,
2177
+ title: pullRequest.title,
2178
+ body,
2179
+ // A pull request already open keeps its description; this turn's screenshots join it.
2180
+ appendIfExisting: screenshots || null
2181
+ });
2182
+ if (outcome.status === "opened" || outcome.status === "existing") {
2183
+ if (outcome.appendError) {
2184
+ this.emit({
2185
+ type: "error",
2186
+ message: `This turn's screenshots were not added to the pull request description: ${outcome.appendError}.`,
2187
+ retryable: false
2188
+ });
2189
+ }
2190
+ if (outcome.status === "opened") {
2191
+ this.deps.logger.info(`session ${this.sessionId}: opened ${outcome.url}`);
2192
+ }
2193
+ return outcome.url;
2194
+ }
2195
+ if (outcome.status === "failed") {
2196
+ this.deps.logger.warn(
2197
+ `session ${this.sessionId}: could not open the pull request: ${outcome.error}`
2198
+ );
2199
+ this.emit({
2200
+ type: "error",
2201
+ message: `The runner could not open the pull request for ${workBranch}: ${outcome.error}.`,
2202
+ retryable: false
2203
+ });
2204
+ }
2205
+ return null;
2206
+ }
2051
2207
  /** The screenshot folder exists and is kept out of git before the agent can use it. */
2052
2208
  async prepareEvidenceDir() {
2053
2209
  if (!this.record.workBranch || !this.record.repoDir) return;
@@ -2400,6 +2556,7 @@ var SessionManager = class {
2400
2556
  repoDir: wt.repoDir,
2401
2557
  repository: cmd.repository.url,
2402
2558
  workBranch: cmd.repository.workBranch,
2559
+ ...cmd.pullRequest ? { pullRequest: cmd.pullRequest } : {},
2403
2560
  baseRef: wt.baseRef,
2404
2561
  baseSha: wt.baseSha,
2405
2562
  // A reused kept worktree still holds the uncommitted changes it was kept for: keep
@@ -2591,7 +2748,8 @@ var SessionManager = class {
2591
2748
  disallowedTools: this.opts.config.disallowedTools ?? [],
2592
2749
  loadProjectSettings: this.opts.config.loadProjectSettings ?? false,
2593
2750
  interruptGraceMs: this.opts.interruptGraceMs,
2594
- onClaudeSignIn: this.opts.onClaudeSignIn
2751
+ onClaudeSignIn: this.opts.onClaudeSignIn,
2752
+ openPullRequest: this.opts.openPullRequest
2595
2753
  });
2596
2754
  }
2597
2755
  };
@@ -2726,8 +2884,13 @@ var RunnerDaemon = class {
2726
2884
  protocolVersion: RUNNER_PROTOCOL_VERSION,
2727
2885
  runnerVersion: RUNNER_VERSION,
2728
2886
  platform: RUNNER_PLATFORM,
2729
- // This runner continues closed sessions (`start_session` with `resume`).
2730
- agent: { ...await this.agent(), resumesClosedSessions: true },
2887
+ // This runner continues closed sessions (`start_session` with `resume`), and opens
2888
+ // pull requests itself when asked (`start_session` with `pullRequest`).
2889
+ agent: {
2890
+ ...await this.agent(),
2891
+ resumesClosedSessions: true,
2892
+ opensPullRequests: true
2893
+ },
2731
2894
  activeSessionIds: this.manager.activeSessionIds(),
2732
2895
  maxConcurrentSessions: this.opts.config.maxSessions
2733
2896
  });
@@ -2879,13 +3042,13 @@ async function runDoctor(env = process.env) {
2879
3042
  checks.push({
2880
3043
  name: "GitHub CLI",
2881
3044
  level: res.code === 0 ? "ok" : "warn",
2882
- detail: res.code === 0 ? "signed in: the agent can read its pull request, checks and runs" : "not signed in: run `gh auth login` so the agent can read its pull request"
3045
+ detail: res.code === 0 ? "signed in: pull requests can be opened as you, and the agent can read them" : "not signed in: run `gh auth login` so pull requests can be opened as you"
2883
3046
  });
2884
3047
  } catch {
2885
3048
  checks.push({
2886
3049
  name: "GitHub CLI",
2887
3050
  level: "warn",
2888
- detail: "not installed (no `gh` on PATH): the agent cannot read its pull request; see https://cli.github.com"
3051
+ detail: "not installed (no `gh` on PATH): pull requests cannot be opened as you; see https://cli.github.com"
2889
3052
  });
2890
3053
  }
2891
3054
  const bin = await findClaude(env);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discovery-tree/runner",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
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": {