aglio-worker 0.74.1 → 0.77.1

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/agent.js +153 -111
  2. package/package.json +1 -1
package/agent.js CHANGED
@@ -72510,7 +72510,7 @@ async function cleanupDockerDisk(input2) {
72510
72510
 
72511
72511
  // src/job-runner.ts
72512
72512
  import { spawn } from "node:child_process";
72513
- import { access, mkdir, readFile, stat } from "node:fs/promises";
72513
+ import { access, mkdir, readFile, rm, stat } from "node:fs/promises";
72514
72514
  import { homedir as homedir2 } from "node:os";
72515
72515
  import { isAbsolute, join as join3, relative as relative2, resolve as resolve3 } from "node:path";
72516
72516
 
@@ -72705,6 +72705,89 @@ var JobLogBatcher = class {
72705
72705
  }
72706
72706
  };
72707
72707
 
72708
+ // src/remote-git-branch.ts
72709
+ var DIVERGED_PREFIX = "branch diverged";
72710
+ function remoteTrackingRef(branch) {
72711
+ return `refs/remotes/origin/${branch}`;
72712
+ }
72713
+ function branchRefspec(branch) {
72714
+ return `+refs/heads/${branch}:${remoteTrackingRef(branch)}`;
72715
+ }
72716
+ function quoted(args) {
72717
+ return `git ${args.join(" ")}`;
72718
+ }
72719
+ function tail(output2) {
72720
+ const text = output2.trim();
72721
+ return text.length > 800 ? `\u2026${text.slice(-800)}` : text;
72722
+ }
72723
+ function ok(result, args) {
72724
+ if (result.code !== 0) {
72725
+ throw new Error(`${quoted(args)} failed with code ${result.code}${result.output.trim() ? `: ${tail(result.output)}` : ""}`);
72726
+ }
72727
+ return result;
72728
+ }
72729
+ async function remoteBranchExists(git, branch) {
72730
+ const args = ["ls-remote", "--heads", "origin", `refs/heads/${branch}`];
72731
+ const result = ok(await git(args), args);
72732
+ return result.output.split("\n").some((line) => line.trim().endsWith(` refs/heads/${branch}`));
72733
+ }
72734
+ async function fetchBranch(git, branch, options = {}) {
72735
+ const args = [
72736
+ "fetch",
72737
+ ...options.depth ? ["--depth", String(options.depth)] : [],
72738
+ "origin",
72739
+ branchRefspec(branch)
72740
+ ];
72741
+ ok(await git(args), args);
72742
+ const verify = ["rev-parse", "--verify", remoteTrackingRef(branch)];
72743
+ ok(await git(verify), verify);
72744
+ }
72745
+ async function prepareBranchCheckout(git, input2) {
72746
+ const { branch, base } = input2;
72747
+ const exists = await remoteBranchExists(git, branch);
72748
+ const start = exists ? `origin/${branch}` : `origin/${base}`;
72749
+ if (exists) {
72750
+ await fetchBranch(git, branch, { depth: 1 });
72751
+ } else if (!input2.baseAlreadyFetched) {
72752
+ await fetchBranch(git, base, { depth: 1 });
72753
+ }
72754
+ const checkout = ["checkout", "-B", branch, start];
72755
+ ok(await git(checkout), checkout);
72756
+ return exists ? "remote" : "base";
72757
+ }
72758
+ function isRejected(output2) {
72759
+ return /non-fast-forward|fetch first|\[rejected\]/i.test(output2);
72760
+ }
72761
+ async function pushBranchWithRecovery(git, branch) {
72762
+ const pushArgs = ["push", "-u", "origin", branch];
72763
+ const first2 = await git(pushArgs);
72764
+ if (first2.code === 0) return "pushed";
72765
+ if (!isRejected(first2.output)) {
72766
+ throw new Error(
72767
+ `${quoted(pushArgs)} failed with code ${first2.code}${first2.output.trim() ? `: ${tail(first2.output)}` : ""}`
72768
+ );
72769
+ }
72770
+ if (!await remoteBranchExists(git, branch)) {
72771
+ throw new Error(`${quoted(pushArgs)} failed with code ${first2.code}: ${tail(first2.output)}`);
72772
+ }
72773
+ await fetchBranch(git, branch);
72774
+ const rebaseArgs = ["rebase", `origin/${branch}`];
72775
+ const rebase = await git(rebaseArgs);
72776
+ if (rebase.code !== 0) {
72777
+ await git(["rebase", "--abort"]).catch(() => void 0);
72778
+ throw new Error(
72779
+ `${DIVERGED_PREFIX}: ${branch} cannot be rebased onto origin/${branch}${rebase.output.trim() ? `: ${tail(rebase.output)}` : ""}`
72780
+ );
72781
+ }
72782
+ const retry = await git(pushArgs);
72783
+ if (retry.code !== 0) {
72784
+ throw new Error(
72785
+ `${DIVERGED_PREFIX}: ${branch} push rejected after rebase${retry.output.trim() ? `: ${tail(retry.output)}` : ""}`
72786
+ );
72787
+ }
72788
+ return "rebased";
72789
+ }
72790
+
72708
72791
  // src/job-runner.ts
72709
72792
  var JobLeaseLostError = class extends Error {
72710
72793
  constructor(message) {
@@ -72916,30 +72999,58 @@ async function checkoutRepos(job, workspace, write, signal) {
72916
72999
  if (hasGit) {
72917
73000
  write(`[aglio] updating ${repo.name} (${branch})
72918
73001
  `);
72919
- await gitEcho(write, ["-C", target, "fetch", "--depth", "1", "origin", branch], target, signal);
72920
- await gitEcho(write, ["-C", target, "checkout", "-B", branch, `origin/${branch}`], target, signal);
72921
- } else {
72922
- if (!repo.cloneUrl) continue;
72923
- await mkdir(workspace, { recursive: true });
72924
- write(`[aglio] cloning ${repo.owner}/${repo.repo} (${branch})
73002
+ try {
73003
+ await gitEcho(
73004
+ write,
73005
+ ["-C", target, "fetch", "--depth", "1", "origin", branchRefspec(branch)],
73006
+ target,
73007
+ signal
73008
+ );
73009
+ await gitEcho(
73010
+ write,
73011
+ ["-C", target, "checkout", "-B", branch, `origin/${branch}`],
73012
+ target,
73013
+ signal
73014
+ );
73015
+ continue;
73016
+ } catch (error63) {
73017
+ write(`[aglio] ${repo.name} checkout failed: ${errorText(error63)}
72925
73018
  `);
72926
- await gitEcho(
72927
- write,
72928
- ["clone", "--depth", "1", "--branch", branch, repo.cloneUrl, target],
72929
- workspace,
72930
- signal
72931
- );
73019
+ write(`[aglio] re-cloning ${repo.owner}/${repo.repo} (${branch})
73020
+ `);
73021
+ await rm(target, { recursive: true, force: true });
73022
+ }
72932
73023
  }
73024
+ if (!repo.cloneUrl) continue;
73025
+ await mkdir(workspace, { recursive: true });
73026
+ if (!hasGit) write(`[aglio] cloning ${repo.owner}/${repo.repo} (${branch})
73027
+ `);
73028
+ await gitEcho(
73029
+ write,
73030
+ ["clone", "--depth", "1", "--branch", branch, repo.cloneUrl, target],
73031
+ workspace,
73032
+ signal
73033
+ );
72933
73034
  }
72934
73035
  }
72935
73036
  async function gitEcho(write, args, cwd, signal) {
73037
+ let output2 = "";
72936
73038
  const code = await runShell({
72937
73039
  command: `git ${args.map(shellArg).join(" ")}`,
72938
73040
  cwd,
72939
73041
  signal,
72940
- onOutput: (chunk) => write(chunk.toString("utf8"))
73042
+ onOutput: (chunk) => {
73043
+ const text = chunk.toString("utf8");
73044
+ output2 += text;
73045
+ write(text);
73046
+ }
72941
73047
  });
72942
- if (code !== 0) throw new Error(`git ${args[0]} failed with code ${code}`);
73048
+ if (code !== 0) {
73049
+ const detail = output2.trim();
73050
+ throw new Error(
73051
+ `git ${args[0]} failed with code ${code}${detail ? `: ${detail.slice(-800)}` : ""}`
73052
+ );
73053
+ }
72943
73054
  }
72944
73055
  function shellArg(value) {
72945
73056
  return /^[A-Za-z0-9_./:@-]+$/.test(value) ? value : `'${value.replace(/'/g, `'\\''`)}'`;
@@ -73039,7 +73150,7 @@ import { dirname as dirname4 } from "node:path";
73039
73150
 
73040
73151
  // src/remote-card-run.ts
73041
73152
  import { randomUUID } from "node:crypto";
73042
- import { mkdir as mkdir2, readFile as readFile2, rm as rm2, stat as stat3, writeFile } from "node:fs/promises";
73153
+ import { mkdir as mkdir2, readFile as readFile2, rm as rm3, stat as stat3, writeFile } from "node:fs/promises";
73043
73154
  import { dirname as dirname3, isAbsolute as isAbsolute4, join as join5, relative as relative4, resolve as resolve4 } from "node:path";
73044
73155
 
73045
73156
  // ../../packages/agents/src/drivers.ts
@@ -85266,7 +85377,7 @@ async function shutdownPostHogAi() {
85266
85377
  }
85267
85378
 
85268
85379
  // src/remote-cleanup.ts
85269
- import { access as access2, readdir, rm, stat as stat2 } from "node:fs/promises";
85380
+ import { access as access2, readdir, rm as rm2, stat as stat2 } from "node:fs/promises";
85270
85381
  import { join as join4 } from "node:path";
85271
85382
  import Dockerode3 from "dockerode";
85272
85383
  var JOB_WORKSPACE_RETENTION_MS = 7 * 24 * 60 * 60 * 1e3;
@@ -85317,7 +85428,7 @@ async function cleanupRemoteWorkspaces(input2) {
85317
85428
  continue;
85318
85429
  }
85319
85430
  if (await workspaceHasResumeMarker(jobDir)) continue;
85320
- await rm(jobDir, { recursive: true, force: true }).catch(() => void 0);
85431
+ await rm2(jobDir, { recursive: true, force: true }).catch(() => void 0);
85321
85432
  removed += 1;
85322
85433
  }
85323
85434
  }
@@ -85352,89 +85463,6 @@ async function cleanupRemoteContainers(input2) {
85352
85463
  return { removed, scanned };
85353
85464
  }
85354
85465
 
85355
- // src/remote-git-branch.ts
85356
- var DIVERGED_PREFIX = "branch diverged";
85357
- function remoteTrackingRef(branch) {
85358
- return `refs/remotes/origin/${branch}`;
85359
- }
85360
- function branchRefspec(branch) {
85361
- return `+refs/heads/${branch}:${remoteTrackingRef(branch)}`;
85362
- }
85363
- function quoted(args) {
85364
- return `git ${args.join(" ")}`;
85365
- }
85366
- function tail(output2) {
85367
- const text = output2.trim();
85368
- return text.length > 800 ? `\u2026${text.slice(-800)}` : text;
85369
- }
85370
- function ok(result, args) {
85371
- if (result.code !== 0) {
85372
- throw new Error(`${quoted(args)} failed with code ${result.code}${result.output.trim() ? `: ${tail(result.output)}` : ""}`);
85373
- }
85374
- return result;
85375
- }
85376
- async function remoteBranchExists(git, branch) {
85377
- const args = ["ls-remote", "--heads", "origin", `refs/heads/${branch}`];
85378
- const result = ok(await git(args), args);
85379
- return result.output.split("\n").some((line) => line.trim().endsWith(` refs/heads/${branch}`));
85380
- }
85381
- async function fetchBranch(git, branch, options = {}) {
85382
- const args = [
85383
- "fetch",
85384
- ...options.depth ? ["--depth", String(options.depth)] : [],
85385
- "origin",
85386
- branchRefspec(branch)
85387
- ];
85388
- ok(await git(args), args);
85389
- const verify = ["rev-parse", "--verify", remoteTrackingRef(branch)];
85390
- ok(await git(verify), verify);
85391
- }
85392
- async function prepareBranchCheckout(git, input2) {
85393
- const { branch, base } = input2;
85394
- const exists = await remoteBranchExists(git, branch);
85395
- const start = exists ? `origin/${branch}` : `origin/${base}`;
85396
- if (exists) {
85397
- await fetchBranch(git, branch, { depth: 1 });
85398
- } else if (!input2.baseAlreadyFetched) {
85399
- await fetchBranch(git, base, { depth: 1 });
85400
- }
85401
- const checkout = ["checkout", "-B", branch, start];
85402
- ok(await git(checkout), checkout);
85403
- return exists ? "remote" : "base";
85404
- }
85405
- function isRejected(output2) {
85406
- return /non-fast-forward|fetch first|\[rejected\]/i.test(output2);
85407
- }
85408
- async function pushBranchWithRecovery(git, branch) {
85409
- const pushArgs = ["push", "-u", "origin", branch];
85410
- const first2 = await git(pushArgs);
85411
- if (first2.code === 0) return "pushed";
85412
- if (!isRejected(first2.output)) {
85413
- throw new Error(
85414
- `${quoted(pushArgs)} failed with code ${first2.code}${first2.output.trim() ? `: ${tail(first2.output)}` : ""}`
85415
- );
85416
- }
85417
- if (!await remoteBranchExists(git, branch)) {
85418
- throw new Error(`${quoted(pushArgs)} failed with code ${first2.code}: ${tail(first2.output)}`);
85419
- }
85420
- await fetchBranch(git, branch);
85421
- const rebaseArgs = ["rebase", `origin/${branch}`];
85422
- const rebase = await git(rebaseArgs);
85423
- if (rebase.code !== 0) {
85424
- await git(["rebase", "--abort"]).catch(() => void 0);
85425
- throw new Error(
85426
- `${DIVERGED_PREFIX}: ${branch} cannot be rebased onto origin/${branch}${rebase.output.trim() ? `: ${tail(rebase.output)}` : ""}`
85427
- );
85428
- }
85429
- const retry = await git(pushArgs);
85430
- if (retry.code !== 0) {
85431
- throw new Error(
85432
- `${DIVERGED_PREFIX}: ${branch} push rejected after rebase${retry.output.trim() ? `: ${tail(retry.output)}` : ""}`
85433
- );
85434
- }
85435
- return "rebased";
85436
- }
85437
-
85438
85466
  // src/remote-card-run.ts
85439
85467
  var GIT_USER = { name: "Aglio Agent", email: "agent@aglio.local" };
85440
85468
  function shellArg2(value) {
@@ -85471,7 +85499,7 @@ async function runRemoteCardJob(deps) {
85471
85499
  () => false
85472
85500
  );
85473
85501
  context = await deps.api.fetchContext({ ...lease, workspaceRoot: runRoot, workspaceReady });
85474
- await rm2(join5(workspace, ".aglio", SESSION_RESUME_MARKER), { force: true }).catch(() => void 0);
85502
+ await rm3(join5(workspace, ".aglio", SESSION_RESUME_MARKER), { force: true }).catch(() => void 0);
85475
85503
  } catch (error63) {
85476
85504
  return { state: "failed", error: `Could not load run context: ${errorText2(error63)}` };
85477
85505
  }
@@ -85537,7 +85565,7 @@ async function runRemoteCardJob(deps) {
85537
85565
  });
85538
85566
  const repoPaths = await cloneRepos();
85539
85567
  if (context.run.kind === "workspace_consult") {
85540
- await rm2(gitConfigPath, { force: true }).catch(() => void 0);
85568
+ await rm3(gitConfigPath, { force: true }).catch(() => void 0);
85541
85569
  }
85542
85570
  const env = await agentEnv();
85543
85571
  for (const command2 of driverSetupCommands(context.agent.driver, mcpUrl, extraServers)) {
@@ -85572,16 +85600,16 @@ async function runRemoteCardJob(deps) {
85572
85600
  return { state: "failed", error: errorText2(error63).slice(0, 4e3) };
85573
85601
  } finally {
85574
85602
  deps.mcp?.stopRun(context.run.id);
85575
- await rm2(homeHost, { recursive: true, force: true }).catch(() => void 0);
85576
- await rm2(gitConfigPath, { force: true }).catch(() => void 0);
85577
- await rm2(join5(toolDir, "models.json"), { force: true }).catch(() => void 0);
85603
+ await rm3(homeHost, { recursive: true, force: true }).catch(() => void 0);
85604
+ await rm3(gitConfigPath, { force: true }).catch(() => void 0);
85605
+ await rm3(join5(toolDir, "models.json"), { force: true }).catch(() => void 0);
85578
85606
  if (aiToken) {
85579
85607
  await deps.api.revokeAiToken?.({ ...lease, runId: context.run.id }).catch((error63) => {
85580
85608
  deps.log?.("AI token revoke failed", { error: errorText2(error63) });
85581
85609
  });
85582
85610
  }
85583
85611
  if (mcpRelativePath) {
85584
- await rm2(join5(toolDir, mcpRelativePath), { force: true }).catch(() => void 0);
85612
+ await rm3(join5(toolDir, mcpRelativePath), { force: true }).catch(() => void 0);
85585
85613
  }
85586
85614
  }
85587
85615
  async function prepareWorkspace() {
@@ -85728,8 +85756,22 @@ async function runRemoteCardJob(deps) {
85728
85756
  baseAlreadyFetched = true;
85729
85757
  }
85730
85758
  if (context.run.kind === "job") {
85731
- if (!baseAlreadyFetched) await git(["fetch", "origin", base], workspace);
85732
- await git(["checkout", "-B", base, `origin/${base}`], workspace);
85759
+ const repoCommands = await repoGit(targetRun);
85760
+ try {
85761
+ if (!baseAlreadyFetched) await fetchBranch(repoCommands, base, { depth: 1 });
85762
+ const checkout = await repoCommands(["checkout", "-B", base, `origin/${base}`]);
85763
+ if (checkout.code !== 0) {
85764
+ throw new Error(
85765
+ `git checkout -B ${base} failed with code ${checkout.code}${checkout.output.trim() ? `: ${checkout.output.trim().slice(-800)}` : ""}`
85766
+ );
85767
+ }
85768
+ } catch (error63) {
85769
+ if (!hasGit) throw error63;
85770
+ write(`[aglio] ${repo.name} checkout failed, re-cloning
85771
+ `);
85772
+ await rm3(target, { recursive: true, force: true });
85773
+ await git(["clone", "--depth", "1", "--branch", base, repo.cloneUrl, targetRun], workspace);
85774
+ }
85733
85775
  write(`[aglio] ${repo.name} starts from origin/${base} (job run)
85734
85776
  `);
85735
85777
  continue;
@@ -86191,7 +86233,7 @@ async function runRemoteCardJob(deps) {
86191
86233
  }
86192
86234
 
86193
86235
  // src/remote-provider-login.ts
86194
- import { mkdir as mkdir3, readFile as readFile3, rm as rm3 } from "node:fs/promises";
86236
+ import { mkdir as mkdir3, readFile as readFile3, rm as rm4 } from "node:fs/promises";
86195
86237
  import { join as join6, resolve as resolve5, sep as sep3 } from "node:path";
86196
86238
  var FINAL = ["completed", "failed", "cancelled", "expired"];
86197
86239
  var POLL_MS = 700;
@@ -86349,7 +86391,7 @@ ${tail2}` : "");
86349
86391
  } finally {
86350
86392
  await container?.kill().catch(() => void 0);
86351
86393
  await container?.remove().catch(() => void 0);
86352
- await rm3(root, { recursive: true, force: true }).catch(() => void 0);
86394
+ await rm4(root, { recursive: true, force: true }).catch(() => void 0);
86353
86395
  }
86354
86396
  }
86355
86397
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aglio-worker",
3
- "version": "0.74.1",
3
+ "version": "0.77.1",
4
4
  "description": "Host machine agent for Aglio: claims jobs over the API and starts aglio-runner containers.",
5
5
  "type": "module",
6
6
  "bin": {