agents-relay 1.0.57 → 1.0.58

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/pool.js +30 -8
  2. package/package.json +1 -1
package/dist/pool.js CHANGED
@@ -25,6 +25,7 @@ export class RepositoryWorkerPool {
25
25
  running = false;
26
26
  pending = false;
27
27
  jobTargets = new Map();
28
+ jobQueues = new Map();
28
29
  constructor(client, repository, trustedAuthors, concurrency, makeReconciler) {
29
30
  this.client = client;
30
31
  this.trustedAuthors = trustedAuthors;
@@ -33,27 +34,44 @@ export class RepositoryWorkerPool {
33
34
  this.repositories = [...new Set(typeof repository === 'string' ? [repository] : repository)];
34
35
  }
35
36
  repositories;
37
+ async withJobQueue(jobId, work) {
38
+ const previous = this.jobQueues.get(jobId) ?? Promise.resolve();
39
+ const run = previous.catch(() => undefined).then(work);
40
+ const settled = run.then(() => undefined, () => undefined);
41
+ this.jobQueues.set(jobId, settled);
42
+ try {
43
+ return await run;
44
+ }
45
+ finally {
46
+ if (this.jobQueues.get(jobId) === settled)
47
+ this.jobQueues.delete(jobId);
48
+ }
49
+ }
36
50
  async reconcileDiscovered(discovered) {
37
51
  for (const item of discovered)
38
52
  this.jobTargets.set(item.job.id, { repository: item.job.repository, prNumber: item.job.prNumber });
39
53
  // Terminal PRs must be reconciled before OPEN-job scheduling. Otherwise the
40
54
  // OPEN-only runnable filter can strand durable jobs after GitHub merges/closes them.
41
55
  for (const job of terminalManagedJobs(discovered)) {
42
- const store = new GitHubStore(this.client, job.repository, job.prNumber, this.trustedAuthors);
43
- await this.makeReconciler(store, 0).reconcile(job.id);
56
+ await this.withJobQueue(job.id, async () => {
57
+ const store = new GitHubStore(this.client, job.repository, job.prNumber, this.trustedAuthors);
58
+ await this.makeReconciler(store, 0).reconcile(job.id);
59
+ });
44
60
  }
45
61
  const jobs = runnableManagedJobs(discovered);
46
62
  let capacity = Math.max(1, this.concurrency);
47
63
  for (const job of jobs) {
48
64
  if (capacity <= 0)
49
65
  break;
50
- const store = new GitHubStore(this.client, job.repository, job.prNumber, this.trustedAuthors);
51
66
  const ready = job.tasks.filter(task => task.state === 'QUEUED' || (task.state === 'DRAFT' && task.dependencies.length > 0)).length;
52
67
  const expiredRunning = job.tasks.some(task => task.state === 'RUNNING' && task.leaseExpiresAt !== null && new Date(task.leaseExpiresAt).getTime() <= Date.now());
53
68
  if (!needsLifecycleReconciliation(job))
54
69
  continue;
55
70
  const allowance = Math.max(1, Math.min(capacity, ready || (expiredRunning ? 1 : 0) || 1));
56
- await this.makeReconciler(store, allowance).reconcile(job.id);
71
+ await this.withJobQueue(job.id, async () => {
72
+ const store = new GitHubStore(this.client, job.repository, job.prNumber, this.trustedAuthors);
73
+ await this.makeReconciler(store, allowance).reconcile(job.id);
74
+ });
57
75
  capacity -= allowance;
58
76
  }
59
77
  }
@@ -78,8 +96,10 @@ export class RepositoryWorkerPool {
78
96
  if (!this.repositories.includes(repository))
79
97
  return;
80
98
  this.jobTargets.set(jobId, { repository, prNumber });
81
- const store = new GitHubStore(this.client, repository, prNumber, this.trustedAuthors);
82
- await this.makeReconciler(store, Math.max(1, this.concurrency)).reconcile(jobId);
99
+ await this.withJobQueue(jobId, async () => {
100
+ const store = new GitHubStore(this.client, repository, prNumber, this.trustedAuthors);
101
+ await this.makeReconciler(store, Math.max(1, this.concurrency)).reconcile(jobId);
102
+ });
83
103
  }
84
104
  async handleTerminalEvent(event) {
85
105
  const target = this.jobTargets.get(event.job_id);
@@ -87,8 +107,10 @@ export class RepositoryWorkerPool {
87
107
  process.stderr.write(`warning: terminal event target unknown for job ${event.job_id}\n`);
88
108
  return;
89
109
  }
90
- const store = new GitHubStore(this.client, target.repository, target.prNumber, this.trustedAuthors);
91
- await this.makeReconciler(store, Math.max(1, this.concurrency)).handleEvent(event);
110
+ await this.withJobQueue(event.job_id, async () => {
111
+ const store = new GitHubStore(this.client, target.repository, target.prNumber, this.trustedAuthors);
112
+ await this.makeReconciler(store, Math.max(1, this.concurrency)).handleEvent(event);
113
+ });
92
114
  }
93
115
  async watch(bus) {
94
116
  return bus.subscribeAll(async (event) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agents-relay",
3
- "version": "1.0.57",
3
+ "version": "1.0.58",
4
4
  "description": "Durable async agent jobs coordinated through GitHub pull requests",
5
5
  "type": "module",
6
6
  "bin": {