agents-relay 1.0.58 → 1.0.59

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.
package/dist/pool.js CHANGED
@@ -25,7 +25,6 @@ export class RepositoryWorkerPool {
25
25
  running = false;
26
26
  pending = false;
27
27
  jobTargets = new Map();
28
- jobQueues = new Map();
29
28
  constructor(client, repository, trustedAuthors, concurrency, makeReconciler) {
30
29
  this.client = client;
31
30
  this.trustedAuthors = trustedAuthors;
@@ -34,44 +33,27 @@ export class RepositoryWorkerPool {
34
33
  this.repositories = [...new Set(typeof repository === 'string' ? [repository] : repository)];
35
34
  }
36
35
  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
- }
50
36
  async reconcileDiscovered(discovered) {
51
37
  for (const item of discovered)
52
38
  this.jobTargets.set(item.job.id, { repository: item.job.repository, prNumber: item.job.prNumber });
53
39
  // Terminal PRs must be reconciled before OPEN-job scheduling. Otherwise the
54
40
  // OPEN-only runnable filter can strand durable jobs after GitHub merges/closes them.
55
41
  for (const job of terminalManagedJobs(discovered)) {
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
- });
42
+ const store = new GitHubStore(this.client, job.repository, job.prNumber, this.trustedAuthors);
43
+ await this.makeReconciler(store, 0).reconcile(job.id);
60
44
  }
61
45
  const jobs = runnableManagedJobs(discovered);
62
46
  let capacity = Math.max(1, this.concurrency);
63
47
  for (const job of jobs) {
64
48
  if (capacity <= 0)
65
49
  break;
50
+ const store = new GitHubStore(this.client, job.repository, job.prNumber, this.trustedAuthors);
66
51
  const ready = job.tasks.filter(task => task.state === 'QUEUED' || (task.state === 'DRAFT' && task.dependencies.length > 0)).length;
67
52
  const expiredRunning = job.tasks.some(task => task.state === 'RUNNING' && task.leaseExpiresAt !== null && new Date(task.leaseExpiresAt).getTime() <= Date.now());
68
53
  if (!needsLifecycleReconciliation(job))
69
54
  continue;
70
55
  const allowance = Math.max(1, Math.min(capacity, ready || (expiredRunning ? 1 : 0) || 1));
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
- });
56
+ await this.makeReconciler(store, allowance).reconcile(job.id);
75
57
  capacity -= allowance;
76
58
  }
77
59
  }
@@ -96,10 +78,8 @@ export class RepositoryWorkerPool {
96
78
  if (!this.repositories.includes(repository))
97
79
  return;
98
80
  this.jobTargets.set(jobId, { repository, prNumber });
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
- });
81
+ const store = new GitHubStore(this.client, repository, prNumber, this.trustedAuthors);
82
+ await this.makeReconciler(store, Math.max(1, this.concurrency)).reconcile(jobId);
103
83
  }
104
84
  async handleTerminalEvent(event) {
105
85
  const target = this.jobTargets.get(event.job_id);
@@ -107,10 +87,8 @@ export class RepositoryWorkerPool {
107
87
  process.stderr.write(`warning: terminal event target unknown for job ${event.job_id}\n`);
108
88
  return;
109
89
  }
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
- });
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);
114
92
  }
115
93
  async watch(bus) {
116
94
  return bus.subscribeAll(async (event) => {
@@ -61,6 +61,7 @@ The ChatGPT/Codex conversation or process exit is never the task result.
61
61
  Before your final return, verify that a terminal event for this exact Job/Task was published.
62
62
  If no terminal event has been published, publish task.failed before exiting.`;
63
63
  }
64
+ const taskLaunchClaims = new Set();
64
65
  export class Reconciler {
65
66
  store;
66
67
  options;
@@ -468,15 +469,34 @@ export class Reconciler {
468
469
  await this.store.saveTask(task);
469
470
  return;
470
471
  }
471
- const leaseMs = Math.max(this.options.leaseMs ?? 300000, task.timeoutMs);
472
- const startedAt = (this.options.now ?? new Date()).getTime();
473
- transitionTask(task, 'RUNNING');
474
- task.attempt += 1;
475
- task.leaseOwner = this.options.owner;
476
- task.leaseExpiresAt = new Date(startedAt + leaseMs).toISOString();
477
- task.executionId = randomUUID();
472
+ const claimKey = `${job.repository}#${job.prNumber}:${task.id}`;
473
+ if (taskLaunchClaims.has(claimKey))
474
+ return;
475
+ taskLaunchClaims.add(claimKey);
476
+ let claimedTask = null;
477
+ try {
478
+ const freshJob = await this.store.load(job.id);
479
+ const freshTask = freshJob.tasks.find(item => item.id === task.id);
480
+ if (!freshTask || freshTask.state !== 'QUEUED' || !dependencyReady(freshTask, new Map(freshJob.tasks.map(item => [item.id, item]))))
481
+ return;
482
+ const leaseMs = Math.max(this.options.leaseMs ?? 300000, freshTask.timeoutMs);
483
+ const startedAt = (this.options.now ?? new Date()).getTime();
484
+ transitionTask(freshTask, 'RUNNING');
485
+ freshTask.attempt += 1;
486
+ freshTask.leaseOwner = this.options.owner;
487
+ freshTask.leaseExpiresAt = new Date(startedAt + leaseMs).toISOString();
488
+ freshTask.executionId = randomUUID();
489
+ await this.store.saveTask(freshTask);
490
+ claimedTask = freshTask;
491
+ }
492
+ finally {
493
+ taskLaunchClaims.delete(claimKey);
494
+ }
495
+ if (!claimedTask)
496
+ return;
497
+ task = claimedTask;
498
+ const startedAt = Date.now();
478
499
  const durableExecutionId = task.executionId;
479
- await this.store.saveTask(task);
480
500
  const controller = new AbortController();
481
501
  let execution;
482
502
  const workerTask = { ...task, input: workerInput, routing: task.routing ? { ...task.routing, cwd: task.routing.cwd ?? executionContext.project.path } : task.routing };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agents-relay",
3
- "version": "1.0.58",
3
+ "version": "1.0.59",
4
4
  "description": "Durable async agent jobs coordinated through GitHub pull requests",
5
5
  "type": "module",
6
6
  "bin": {