@h-rig/runtime 0.0.6-alpha.0 → 0.0.6-alpha.10

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 (42) hide show
  1. package/dist/bin/rig-agent-dispatch.js +133 -14
  2. package/dist/bin/rig-agent.js +83 -26
  3. package/dist/src/control-plane/agent-wrapper.js +133 -14
  4. package/dist/src/control-plane/harness-main.js +83 -26
  5. package/dist/src/control-plane/hooks/completion-verification.js +85 -28
  6. package/dist/src/control-plane/hooks/inject-context.js +2 -2
  7. package/dist/src/control-plane/hooks/submodule-branch.js +26 -3
  8. package/dist/src/control-plane/hooks/task-runtime-start.js +26 -3
  9. package/dist/src/control-plane/native/git-ops.js +81 -24
  10. package/dist/src/control-plane/native/harness-cli.js +83 -26
  11. package/dist/src/control-plane/native/pr-automation.js +88 -17
  12. package/dist/src/control-plane/native/run-ops.js +23 -6
  13. package/dist/src/control-plane/native/task-ops.js +2 -2
  14. package/dist/src/control-plane/native/validator.js +2 -2
  15. package/dist/src/control-plane/native/verifier.js +2 -2
  16. package/dist/src/control-plane/runtime/index.js +38 -9
  17. package/dist/src/control-plane/runtime/isolation/home.js +31 -6
  18. package/dist/src/control-plane/runtime/isolation/index.js +38 -9
  19. package/dist/src/control-plane/runtime/isolation/runner.js +31 -6
  20. package/dist/src/control-plane/runtime/isolation/shared.js +9 -6
  21. package/dist/src/control-plane/runtime/isolation.js +38 -9
  22. package/dist/src/control-plane/runtime/queue.js +38 -9
  23. package/dist/src/control-plane/tasks/source-aware-task-config-source.js +14 -2
  24. package/dist/src/control-plane/tasks/source-lifecycle.js +2 -2
  25. package/dist/src/index.js +15 -13
  26. package/dist/src/local-server.js +20 -14
  27. package/native/darwin-arm64/{bin/rig-git → rig-git} +0 -0
  28. package/native/darwin-arm64/rig-git.build-manifest.json +4 -0
  29. package/native/darwin-arm64/{bin/rig-shell → rig-shell} +0 -0
  30. package/native/darwin-arm64/rig-shell.build-manifest.json +4 -0
  31. package/native/darwin-arm64/{bin/rig-tools → rig-tools} +0 -0
  32. package/native/darwin-arm64/rig-tools.build-manifest.json +4 -0
  33. package/native/darwin-arm64/{lib/runtime-native.dylib → runtime-native.dylib} +0 -0
  34. package/package.json +8 -7
  35. package/native/darwin-arm64/lib/runtime-native-darwin-arm64.dylib +0 -0
  36. package/native/darwin-arm64/manifest.json +0 -1
  37. package/native/linux-x64/bin/rig-git +0 -0
  38. package/native/linux-x64/bin/rig-shell +0 -0
  39. package/native/linux-x64/bin/rig-tools +0 -0
  40. package/native/linux-x64/lib/runtime-native-linux-x64.so +0 -0
  41. package/native/linux-x64/lib/runtime-native.so +0 -0
  42. package/native/linux-x64/manifest.json +0 -1
@@ -2951,6 +2951,17 @@ async function readSourceAwareTaskStatus(projectRoot, taskId, options = {}) {
2951
2951
  return null;
2952
2952
  }
2953
2953
  }
2954
+ function updateGithubIssueTaskBySourceIssueId(sourceIssueId, taskId, update, options = {}) {
2955
+ const parsed = sourceIssueId?.trim().match(/^([^/]+)\/([^#]+)#(\d+)$/);
2956
+ if (!parsed || parsed[3] !== taskId) {
2957
+ return false;
2958
+ }
2959
+ applyGithubIssueUpdate(options.ghBinary ?? "gh", options.spawn ?? spawnSync, parsed[3], {
2960
+ sourceIssueId: sourceIssueId.trim(),
2961
+ taskSource: { kind: "github-issues", owner: parsed[1], repo: parsed[2] }
2962
+ }, update);
2963
+ return true;
2964
+ }
2954
2965
  function updateSourceAwareTaskConfigTask(projectRoot, taskId, update, options = {}) {
2955
2966
  const configPath = options.configPath ?? resolve13(projectRoot, ".rig", "task-config.json");
2956
2967
  const rawEntry = readRawTaskEntry(configPath, taskId);
@@ -3306,8 +3317,8 @@ function ensureStatusLabel(bin, repo, spawnFn, label) {
3306
3317
  }
3307
3318
  }
3308
3319
  function selectedGitHubEnv() {
3309
- const token = process.env.RIG_GITHUB_SELECTED_TOKEN?.trim() ?? "";
3310
- return { GH_TOKEN: token, GITHUB_TOKEN: token };
3320
+ const token = process.env.RIG_GITHUB_SELECTED_TOKEN?.trim() || process.env.RIG_GITHUB_TOKEN?.trim() || "";
3321
+ return { GH_TOKEN: token, GITHUB_TOKEN: token, RIG_GITHUB_TOKEN: token };
3311
3322
  }
3312
3323
  function ghSpawnOptions() {
3313
3324
  return { encoding: "utf-8", env: { ...process.env, ...selectedGitHubEnv() } };
@@ -5710,20 +5721,23 @@ function hashProjectPath(workspaceDir) {
5710
5721
  }
5711
5722
  function resolveGithubCliBinaryPath() {
5712
5723
  const explicit = process.env.RIG_GH_BIN?.trim();
5713
- if (explicit && existsSync23(explicit)) {
5724
+ if (explicit && existsSync23(explicit) && !isRuntimeGatewayGhPath(explicit)) {
5714
5725
  return explicit;
5715
5726
  }
5716
- const bunResolved = Bun.which("gh");
5717
- if (bunResolved && existsSync23(bunResolved)) {
5718
- return bunResolved;
5719
- }
5720
- for (const candidate of ["/opt/homebrew/bin/gh", "/usr/local/bin/gh", "/usr/bin/gh"]) {
5727
+ for (const candidate of ["/usr/bin/gh", "/opt/homebrew/bin/gh", "/usr/local/bin/gh"]) {
5721
5728
  if (existsSync23(candidate)) {
5722
5729
  return candidate;
5723
5730
  }
5724
5731
  }
5732
+ const bunResolved = Bun.which("gh");
5733
+ if (bunResolved && existsSync23(bunResolved) && !isRuntimeGatewayGhPath(bunResolved)) {
5734
+ return bunResolved;
5735
+ }
5725
5736
  return "";
5726
5737
  }
5738
+ function isRuntimeGatewayGhPath(candidate) {
5739
+ return /\/\.rig\/bin\/gh$/.test(candidate.replace(/\\/g, "/"));
5740
+ }
5727
5741
  async function resolveGithubCliAuthToken(ghBinary = "") {
5728
5742
  const gh = ghBinary || resolveGithubCliBinaryPath();
5729
5743
  if (!gh) {
@@ -5824,6 +5838,8 @@ async function runtimeEnv(projectRoot, runtime) {
5824
5838
  XDG_CACHE_HOME: runtime.cacheDir,
5825
5839
  XDG_STATE_HOME: runtime.stateDir,
5826
5840
  RIG_AGENT_ID: runtime.id,
5841
+ ...process.env.RIG_RUN_ID?.trim() ? { RIG_RUN_ID: process.env.RIG_RUN_ID.trim() } : {},
5842
+ ...process.env.RIG_SERVER_RUN_ID?.trim() ? { RIG_SERVER_RUN_ID: process.env.RIG_SERVER_RUN_ID.trim() } : {},
5827
5843
  RIG_TASK_ID: runtime.taskId,
5828
5844
  RIG_TASK_RUNTIME_ID: runtime.id,
5829
5845
  RIG_TASK_WORKSPACE: runtime.workspaceDir,
@@ -5887,6 +5903,10 @@ async function runtimeEnv(projectRoot, runtime) {
5887
5903
  env[key] = value;
5888
5904
  }
5889
5905
  }
5906
+ const rigGithubToken = process.env.RIG_GITHUB_TOKEN?.trim() || "";
5907
+ if (rigGithubToken && !env.GITHUB_TOKEN && !env.GH_TOKEN) {
5908
+ env.GITHUB_TOKEN = rigGithubToken;
5909
+ }
5890
5910
  const fallbackGithubToken = !env.GITHUB_TOKEN && !env.GH_TOKEN ? await resolveGithubCliAuthToken(hostGhBinary) : "";
5891
5911
  if (fallbackGithubToken) {
5892
5912
  env.GITHUB_TOKEN = fallbackGithubToken;
@@ -5897,6 +5917,13 @@ async function runtimeEnv(projectRoot, runtime) {
5897
5917
  if (!env.GH_TOKEN && env.GITHUB_TOKEN) {
5898
5918
  env.GH_TOKEN = env.GITHUB_TOKEN;
5899
5919
  }
5920
+ const gitHubToken = env.GITHUB_TOKEN || env.GH_TOKEN || rigGithubToken;
5921
+ if (gitHubToken) {
5922
+ env.RIG_GITHUB_TOKEN = gitHubToken;
5923
+ env.GITHUB_TOKEN = env.GITHUB_TOKEN || gitHubToken;
5924
+ env.GH_TOKEN = env.GH_TOKEN || gitHubToken;
5925
+ applyGitHubCredentialHelperEnv(env);
5926
+ }
5900
5927
  if (!env.GREPTILE_GITHUB_TOKEN && env.GITHUB_TOKEN) {
5901
5928
  env.GREPTILE_GITHUB_TOKEN = env.GITHUB_TOKEN;
5902
5929
  }
@@ -5977,12 +6004,21 @@ async function materializeRuntimeCertBundle(runtime) {
5977
6004
  }
5978
6005
  return targetPath;
5979
6006
  }
6007
+ function applyGitHubCredentialHelperEnv(env) {
6008
+ env.GIT_TERMINAL_PROMPT = "0";
6009
+ env.GIT_CONFIG_COUNT = "2";
6010
+ env.GIT_CONFIG_KEY_0 = "credential.helper";
6011
+ env.GIT_CONFIG_VALUE_0 = "";
6012
+ env.GIT_CONFIG_KEY_1 = "credential.helper";
6013
+ env.GIT_CONFIG_VALUE_1 = '!f() { test "$1" = get || exit 0; token="${GITHUB_TOKEN:-${GH_TOKEN:-${RIG_GITHUB_TOKEN:-}}}"; test -n "$token" || exit 0; echo username=x-access-token; echo password="$token"; }; f';
6014
+ }
5980
6015
  function persistRuntimeSecrets(runtimeRoot, env) {
5981
6016
  const secretsPath = resolve25(runtimeRoot, "runtime-secrets.json");
5982
6017
  const persisted = {};
5983
6018
  for (const key of [
5984
6019
  "GITHUB_TOKEN",
5985
6020
  "GH_TOKEN",
6021
+ "RIG_GITHUB_TOKEN",
5986
6022
  "GREPTILE_GITHUB_TOKEN",
5987
6023
  "GREPTILE_API_KEY",
5988
6024
  "AI_REVIEW_MODE",
@@ -8461,7 +8497,11 @@ async function ensureAgentRuntime(options) {
8461
8497
  mkdirSync18(runtime.binDir, { recursive: true });
8462
8498
  mkdirSync18(workspaceLayout.distDir, { recursive: true });
8463
8499
  prepareRuntimeWorkspace(options.projectRoot, workspaceDir);
8464
- await resetEphemeralTaskArtifacts(workspaceDir, options.taskId);
8500
+ if (options.preserveTaskArtifacts) {
8501
+ console.log(`[rig-agent] Preserving runtime task artifacts for resume of ${options.taskId}.`);
8502
+ } else {
8503
+ await resetEphemeralTaskArtifacts(workspaceDir, options.taskId);
8504
+ }
8465
8505
  const ctx = {
8466
8506
  runtimeId: options.id,
8467
8507
  taskId: options.taskId,
@@ -9173,7 +9213,8 @@ async function runAgentWrapper(options = {}) {
9173
9213
  taskId,
9174
9214
  mode: "worktree",
9175
9215
  provider,
9176
- taskRecordReader: taskRecordReaderFromEnv(taskId)
9216
+ taskRecordReader: taskRecordReaderFromEnv(taskId),
9217
+ preserveTaskArtifacts: process.env.RIG_RUN_RESUME === "1" || process.env.RIG_RUNTIME_ARTIFACT_CLEANUP === "preserve"
9177
9218
  });
9178
9219
  emitWrapperEvent("runtime.provision.completed", {
9179
9220
  runtimeId: runtime.id,
@@ -9193,6 +9234,18 @@ async function runAgentWrapper(options = {}) {
9193
9234
  console.error(`[rig-agent] Failed to provision runtime: ${error}`);
9194
9235
  return 1;
9195
9236
  }
9237
+ try {
9238
+ await waitForDirtyBaselineReady(runtime, taskId);
9239
+ } catch (error) {
9240
+ emitWrapperEvent("runtime.baseline.failed", {
9241
+ runtimeId: runtime.id,
9242
+ taskId,
9243
+ workspaceDir: runtime.workspaceDir,
9244
+ error: error instanceof Error ? error.message : String(error)
9245
+ });
9246
+ console.error(`[rig-agent] Failed to apply selected baseline before provider launch: ${error}`);
9247
+ return 1;
9248
+ }
9196
9249
  const providerArgs = buildProviderArgs(provider, runtime, argv);
9197
9250
  const providerCommand = [providerBinary(provider), ...providerArgs];
9198
9251
  emitWrapperEvent("provider.launch", {
@@ -9324,12 +9377,16 @@ function buildProviderArgs(provider, runtime, argv) {
9324
9377
  }
9325
9378
  if (provider === "pi") {
9326
9379
  const piArgs = [...argv];
9380
+ const piProvider = cliOptionValue(piArgs, "--provider") || process.env.RIG_PI_PROVIDER?.trim() || "openai-codex";
9327
9381
  if (!hasCliOption(piArgs, "--provider")) {
9328
- piArgs.unshift(process.env.RIG_PI_PROVIDER?.trim() || "openai-codex");
9382
+ piArgs.unshift(piProvider);
9329
9383
  piArgs.unshift("--provider");
9330
9384
  }
9331
- if (!hasCliOption(piArgs, "--model")) {
9332
- piArgs.unshift(process.env.RIG_PI_MODEL?.trim() || "gpt-5.5");
9385
+ const model = cliOptionValue(piArgs, "--model") || process.env.RIG_PI_MODEL?.trim() || "gpt-5.5";
9386
+ if (hasCliOption(piArgs, "--model")) {
9387
+ rewriteCliOptionValue(piArgs, "--model", normalizePiModelForProvider(model, piProvider));
9388
+ } else {
9389
+ piArgs.unshift(normalizePiModelForProvider(model, piProvider));
9333
9390
  piArgs.unshift("--model");
9334
9391
  }
9335
9392
  return piArgs;
@@ -9397,6 +9454,38 @@ function resolveProvider() {
9397
9454
  function hasCliOption(argv, option) {
9398
9455
  return argv.some((arg) => arg === option || arg.startsWith(`${option}=`));
9399
9456
  }
9457
+ function cliOptionValue(argv, option) {
9458
+ for (let index = 0;index < argv.length; index += 1) {
9459
+ const arg = argv[index];
9460
+ if (arg === option) {
9461
+ const next = argv[index + 1];
9462
+ return next && !next.startsWith("--") ? next : undefined;
9463
+ }
9464
+ if (arg?.startsWith(`${option}=`)) {
9465
+ return arg.slice(option.length + 1);
9466
+ }
9467
+ }
9468
+ return;
9469
+ }
9470
+ function rewriteCliOptionValue(argv, option, value) {
9471
+ for (let index = 0;index < argv.length; index += 1) {
9472
+ const arg = argv[index];
9473
+ if (arg === option && argv[index + 1] && !argv[index + 1].startsWith("--")) {
9474
+ argv[index + 1] = value;
9475
+ return;
9476
+ }
9477
+ if (arg?.startsWith(`${option}=`)) {
9478
+ argv[index] = `${option}=${value}`;
9479
+ return;
9480
+ }
9481
+ }
9482
+ }
9483
+ function normalizePiModelForProvider(model, provider) {
9484
+ if (provider === "openrouter" && model === "openai-codex/gpt-5.5") {
9485
+ return "openai/gpt-5.5";
9486
+ }
9487
+ return model;
9488
+ }
9400
9489
  function providerBinary(provider) {
9401
9490
  if (provider === "codex") {
9402
9491
  return Bun.which("codex") || "codex";
@@ -9413,6 +9502,34 @@ function providerBinary(provider) {
9413
9502
  function emitWrapperEvent(type, payload) {
9414
9503
  console.log(`__RIG_WRAPPER_EVENT__${JSON.stringify({ type, payload, at: new Date().toISOString() })}`);
9415
9504
  }
9505
+ function sleep(ms) {
9506
+ return new Promise((resolveSleep) => setTimeout(resolveSleep, ms));
9507
+ }
9508
+ async function waitForDirtyBaselineReady(runtime, taskId) {
9509
+ const readyFile = process.env.RIG_DIRTY_BASELINE_READY_FILE?.trim();
9510
+ if (process.env.RIG_BASELINE_MODE !== "dirty-snapshot" || !readyFile)
9511
+ return;
9512
+ const timeoutMs = Number.parseInt(process.env.RIG_DIRTY_BASELINE_TIMEOUT_MS ?? "30000", 10);
9513
+ const deadline = Date.now() + (Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 30000);
9514
+ emitWrapperEvent("runtime.baseline.waiting", {
9515
+ runtimeId: runtime.id,
9516
+ taskId,
9517
+ workspaceDir: runtime.workspaceDir,
9518
+ readyFile
9519
+ });
9520
+ while (!existsSync33(readyFile)) {
9521
+ if (Date.now() >= deadline) {
9522
+ throw new Error(`Timed out waiting for dirty baseline ready file: ${readyFile}`);
9523
+ }
9524
+ await sleep(50);
9525
+ }
9526
+ emitWrapperEvent("runtime.baseline.completed", {
9527
+ runtimeId: runtime.id,
9528
+ taskId,
9529
+ workspaceDir: runtime.workspaceDir,
9530
+ readyFile
9531
+ });
9532
+ }
9416
9533
  async function resolveTaskId(projectRoot, monorepoRoot) {
9417
9534
  if (process.env.RIG_TASK_ID) {
9418
9535
  return process.env.RIG_TASK_ID;
@@ -9488,7 +9605,9 @@ async function updateTaskSourceAfterRun(projectRoot, taskId, runtime) {
9488
9605
  } catch (error) {
9489
9606
  let fallbackUpdated = false;
9490
9607
  try {
9491
- fallbackUpdated = updateSourceAwareTaskConfigTask(projectRoot, taskId, { status: "closed", comment });
9608
+ const sourceIssueId = loadRuntimeContextFromEnv()?.sourceTask?.sourceIssueId;
9609
+ fallbackUpdated = updateGithubIssueTaskBySourceIssueId(sourceIssueId, taskId, { status: "closed", comment });
9610
+ fallbackUpdated = updateSourceAwareTaskConfigTask(projectRoot, taskId, { status: "closed", comment }) || fallbackUpdated;
9492
9611
  } catch (fallbackError) {
9493
9612
  console.error(`[rig-agent] Source-aware compatibility update also failed for ${taskId}: ${fallbackError instanceof Error ? fallbackError.message : String(fallbackError)}`);
9494
9613
  }
@@ -4421,8 +4421,8 @@ function githubStatusFor(issue) {
4421
4421
  return "open";
4422
4422
  }
4423
4423
  function selectedGitHubEnv() {
4424
- const token = process.env.RIG_GITHUB_SELECTED_TOKEN?.trim() ?? "";
4425
- return { GH_TOKEN: token, GITHUB_TOKEN: token };
4424
+ const token = process.env.RIG_GITHUB_SELECTED_TOKEN?.trim() || process.env.RIG_GITHUB_TOKEN?.trim() || "";
4425
+ return { GH_TOKEN: token, GITHUB_TOKEN: token, RIG_GITHUB_TOKEN: token };
4426
4426
  }
4427
4427
  function ghSpawnOptions() {
4428
4428
  return { encoding: "utf-8", env: { ...process.env, ...selectedGitHubEnv() } };
@@ -6914,12 +6914,12 @@ var TASK_ARTIFACT_STAGE_FALLBACK = new Set([
6914
6914
  "task-result.json",
6915
6915
  "validation-summary.json"
6916
6916
  ]);
6917
- function resolveHostRigBinDir(root) {
6918
- return resolve26(root, ".rig", "bin");
6919
- }
6920
6917
  function isRuntimeGatewayGitPath(candidate) {
6921
6918
  return /\/\.rig\/bin\/git$/.test(candidate.replace(/\\/g, "/"));
6922
6919
  }
6920
+ function isRuntimeGatewayGhPath(candidate) {
6921
+ return /\/\.rig\/bin\/gh$/.test(candidate.replace(/\\/g, "/"));
6922
+ }
6923
6923
  function resolveOptionalMonorepoRoot(projectRoot) {
6924
6924
  const runtimeWorkspace = process.env.RIG_TASK_WORKSPACE?.trim();
6925
6925
  if (runtimeWorkspace && existsSync22(resolve26(runtimeWorkspace, ".git"))) {
@@ -6954,6 +6954,9 @@ function resolveGitBinary(projectRoot) {
6954
6954
  }
6955
6955
  return "git";
6956
6956
  }
6957
+ function escapeRegExp2(value) {
6958
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
6959
+ }
6957
6960
  function safeCurrentTaskId(projectRoot) {
6958
6961
  try {
6959
6962
  const taskId = currentTaskId(projectRoot);
@@ -7112,17 +7115,15 @@ function gitOpenPr(options) {
7112
7115
  const target = options.target || (taskId ? "monorepo" : "project");
7113
7116
  let repoRoot = options.projectRoot;
7114
7117
  let repoLabel = "project-rig";
7115
- let defaultBase = process.env.RIG_PR_BASE_PROJECT || "main";
7118
+ const envBase = target === "monorepo" ? process.env.RIG_PR_BASE_MONOREPO?.trim() || "" : process.env.RIG_PR_BASE_PROJECT?.trim() || "";
7116
7119
  if (target === "monorepo") {
7117
7120
  repoRoot = resolveOptionalMonorepoRoot(options.projectRoot) || resolveMonorepoRoot2(options.projectRoot);
7118
7121
  repoLabel = "monorepo";
7119
- defaultBase = process.env.RIG_PR_BASE_MONOREPO || "main";
7120
7122
  if (taskId) {
7121
7123
  gitSyncBranch(options.projectRoot, taskId, "monorepo");
7122
7124
  }
7123
7125
  } else if (taskId) {
7124
7126
  gitSyncBranch(options.projectRoot, taskId, "project");
7125
- defaultBase = inferProjectBase(options.projectRoot, defaultBase);
7126
7127
  }
7127
7128
  if (!existsSync22(resolve26(repoRoot, ".git"))) {
7128
7129
  throw new Error(`Repository not available for open-pr target ${target}: ${repoRoot}`);
@@ -7131,9 +7132,9 @@ function gitOpenPr(options) {
7131
7132
  if (!branch || branch === "HEAD") {
7132
7133
  throw new Error(`Cannot open PR from detached HEAD in ${repoLabel}. Checkout a branch first.`);
7133
7134
  }
7134
- const base = options.base || defaultBase;
7135
7135
  const repoNameWithOwner = resolveRepoNameWithOwner(options.projectRoot, repoRoot);
7136
7136
  const networkRemote = resolveNetworkRemoteName(options.projectRoot, repoRoot, repoNameWithOwner);
7137
+ const base = options.base || envBase || inferRepositoryDefaultBase(options.projectRoot, repoRoot, repoNameWithOwner, networkRemote, target === "project" ? inferProjectBase(options.projectRoot, "main") : "main");
7137
7138
  refreshRemoteBaseRef(options.projectRoot, repoRoot, base);
7138
7139
  let reviewer = (options.reviewer || "").trim();
7139
7140
  let reviewerSource = reviewer ? "flag" : undefined;
@@ -7169,6 +7170,7 @@ function gitOpenPr(options) {
7169
7170
  "",
7170
7171
  "## Task",
7171
7172
  `- beads: ${taskId || "n/a"}`,
7173
+ ...defaultPrRunLines(taskId, repoNameWithOwner),
7172
7174
  "",
7173
7175
  "## Review",
7174
7176
  "- Completion verification will run validation, verifier review, and PR policy checks.",
@@ -7260,6 +7262,29 @@ function gitOpenPr(options) {
7260
7262
  }
7261
7263
  return result;
7262
7264
  }
7265
+ function defaultPrRunLines(taskId, repoNameWithOwner) {
7266
+ const lines = [];
7267
+ const runId = process.env.RIG_SERVER_RUN_ID?.trim();
7268
+ if (runId) {
7269
+ lines.push(`- Run: ${runId}`);
7270
+ }
7271
+ const closeout = defaultPrCloseoutLine(taskId, repoNameWithOwner);
7272
+ if (closeout) {
7273
+ lines.push(`- ${closeout}`);
7274
+ }
7275
+ return lines;
7276
+ }
7277
+ function defaultPrCloseoutLine(taskId, repoNameWithOwner) {
7278
+ const sourceIssueId = loadRuntimeContextFromEnv()?.sourceTask?.sourceIssueId;
7279
+ if (sourceIssueId) {
7280
+ const match = sourceIssueId.match(/^([^#]+)#(\d+)$/);
7281
+ if (match) {
7282
+ const [, sourceRepo, issueNumber] = match;
7283
+ return sourceRepo.toLowerCase() === repoNameWithOwner.toLowerCase() ? `Closes #${issueNumber}` : `Closes ${sourceRepo}#${issueNumber}`;
7284
+ }
7285
+ }
7286
+ return /^\d+$/.test(taskId) ? `Closes #${taskId}` : "";
7287
+ }
7263
7288
  function readPrViewState(gh, repoRoot, repoNameWithOwner, prUrl) {
7264
7289
  const view = runCapture2(withGhRepo([
7265
7290
  gh,
@@ -7385,32 +7410,19 @@ function resolveGithubCliBinary(projectRoot) {
7385
7410
  if (explicit) {
7386
7411
  candidates.add(explicit);
7387
7412
  }
7413
+ for (const candidate of ["/usr/bin/gh", "/opt/homebrew/bin/gh", "/usr/local/bin/gh"]) {
7414
+ candidates.add(candidate);
7415
+ }
7388
7416
  const explicitPathEntries = (process.env.PATH || "").split(":").map((entry) => entry.trim()).filter(Boolean);
7389
7417
  for (const entry of explicitPathEntries) {
7390
7418
  candidates.add(resolve26(entry, "gh"));
7391
7419
  }
7392
- const hostProjectRoot = process.env.RIG_HOST_PROJECT_ROOT?.trim();
7393
- if (hostProjectRoot) {
7394
- candidates.add(resolve26(resolveHostRigBinDir(hostProjectRoot), "gh"));
7395
- }
7396
- candidates.add(resolve26(resolveHostRigBinDir(projectRoot), "gh"));
7397
- const runtimeContext = loadRuntimeContextFromEnv();
7398
- if (runtimeContext?.binDir) {
7399
- candidates.add(resolve26(runtimeContext.binDir, "gh"));
7400
- }
7401
- const runtimeHome = process.env.RIG_RUNTIME_HOME?.trim();
7402
- if (runtimeHome) {
7403
- candidates.add(resolve26(runtimeHome, "bin", "gh"));
7404
- }
7405
- for (const candidate of ["/opt/homebrew/bin/gh", "/usr/local/bin/gh", "/usr/bin/gh"]) {
7406
- candidates.add(candidate);
7407
- }
7408
7420
  const bunResolved = Bun.which("gh");
7409
7421
  if (bunResolved) {
7410
7422
  candidates.add(bunResolved);
7411
7423
  }
7412
7424
  for (const candidate of candidates) {
7413
- if (candidate && existsSync22(candidate)) {
7425
+ if (candidate && existsSync22(candidate) && !isRuntimeGatewayGhPath(candidate)) {
7414
7426
  return candidate;
7415
7427
  }
7416
7428
  }
@@ -7559,6 +7571,32 @@ function withGhRepo(command, repoNameWithOwner) {
7559
7571
  }
7560
7572
  return [command[0], command[1], command[2], ...ghRepoArgs(repoNameWithOwner), ...command.slice(3)];
7561
7573
  }
7574
+ function inferRepositoryDefaultBase(projectRoot, repoRoot, repoNameWithOwner, remoteName, fallback) {
7575
+ const remote = remoteName || "origin";
7576
+ const symbolic = runCapture2(gitCmd(projectRoot, repoRoot, "symbolic-ref", "--short", `refs/remotes/${remote}/HEAD`), projectRoot);
7577
+ if (symbolic.exitCode === 0) {
7578
+ const ref = symbolic.stdout.trim().replace(new RegExp(`^${escapeRegExp2(remote)}/`), "");
7579
+ if (ref && ref !== "HEAD") {
7580
+ return ref;
7581
+ }
7582
+ }
7583
+ const lsRemote = runCapture2(gitCmd(projectRoot, repoRoot, "ls-remote", "--symref", remote, "HEAD"), projectRoot);
7584
+ if (lsRemote.exitCode === 0) {
7585
+ const match = lsRemote.stdout.match(/^ref:\s+refs\/heads\/([^\t\r\n]+)\s+HEAD/m);
7586
+ if (match?.[1]) {
7587
+ return match[1];
7588
+ }
7589
+ }
7590
+ const gh = resolveGithubCliBinary(projectRoot);
7591
+ if (gh && repoNameWithOwner) {
7592
+ const api = runCapture2(withGhRepo([gh, "repo", "view", "--json", "defaultBranchRef", "--jq", ".defaultBranchRef.name"], repoNameWithOwner), repoRoot);
7593
+ const branch = api.exitCode === 0 ? api.stdout.trim() : "";
7594
+ if (branch) {
7595
+ return branch;
7596
+ }
7597
+ }
7598
+ return fallback;
7599
+ }
7562
7600
  function inferProjectBase(projectRoot, fallback) {
7563
7601
  const containing = runCapture2(gitCmd(projectRoot, projectRoot, "branch", "-r", "--contains", "HEAD"), projectRoot);
7564
7602
  if (containing.exitCode !== 0) {
@@ -7940,6 +7978,10 @@ function runtimeGitEnv(projectRoot) {
7940
7978
  }
7941
7979
  env[key] = value;
7942
7980
  }
7981
+ const rigGithubToken = process.env.RIG_GITHUB_TOKEN?.trim() || "";
7982
+ if (rigGithubToken && !env.GITHUB_TOKEN && !env.GH_TOKEN) {
7983
+ env.GITHUB_TOKEN = rigGithubToken;
7984
+ }
7943
7985
  if (!env.GITHUB_TOKEN && env.GH_TOKEN) {
7944
7986
  env.GITHUB_TOKEN = env.GH_TOKEN;
7945
7987
  }
@@ -7963,6 +8005,13 @@ function runtimeGitEnv(projectRoot) {
7963
8005
  if (!env.GH_TOKEN && env.GITHUB_TOKEN) {
7964
8006
  env.GH_TOKEN = env.GITHUB_TOKEN;
7965
8007
  }
8008
+ const gitHubToken = env.GITHUB_TOKEN || env.GH_TOKEN || env.RIG_GITHUB_TOKEN || rigGithubToken;
8009
+ if (gitHubToken) {
8010
+ env.RIG_GITHUB_TOKEN = gitHubToken;
8011
+ env.GITHUB_TOKEN = env.GITHUB_TOKEN || gitHubToken;
8012
+ env.GH_TOKEN = env.GH_TOKEN || gitHubToken;
8013
+ applyGitHubCredentialHelperEnv(env);
8014
+ }
7966
8015
  if (runtimeKnownHosts && existsSync22(runtimeKnownHosts)) {
7967
8016
  const sshParts = [
7968
8017
  "ssh",
@@ -7979,6 +8028,14 @@ function runtimeGitEnv(projectRoot) {
7979
8028
  }
7980
8029
  return Object.keys(env).length > 0 ? env : undefined;
7981
8030
  }
8031
+ function applyGitHubCredentialHelperEnv(env) {
8032
+ env.GIT_TERMINAL_PROMPT = "0";
8033
+ env.GIT_CONFIG_COUNT = "2";
8034
+ env.GIT_CONFIG_KEY_0 = "credential.helper";
8035
+ env.GIT_CONFIG_VALUE_0 = "";
8036
+ env.GIT_CONFIG_KEY_1 = "credential.helper";
8037
+ env.GIT_CONFIG_VALUE_1 = '!f() { test "$1" = get || exit 0; token="${GITHUB_TOKEN:-${GH_TOKEN:-${RIG_GITHUB_TOKEN:-}}}"; test -n "$token" || exit 0; echo username=x-access-token; echo password="$token"; }; f';
8038
+ }
7982
8039
  function loadPersistedRuntimeSecrets(runtimeRoot) {
7983
8040
  if (!runtimeRoot) {
7984
8041
  return {};