agents-relay 1.0.164 → 1.0.166

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.
@@ -228,9 +228,17 @@ export class Reconciler extends ReconcilerReviewBase {
228
228
  }
229
229
  }
230
230
  async recoverLostExecutions(job, now) {
231
- for (const task of job.tasks.filter(item => item.state === 'RUNNING' || (item.state === 'QUEUED' && item.executionId !== null))) {
232
- const expired = task.leaseExpiresAt !== null && new Date(task.leaseExpiresAt).getTime() <= now;
233
- if (!expired)
231
+ const candidates = job.tasks.filter(item => item.state === 'RUNNING' || (item.state === 'QUEUED' && item.executionId !== null));
232
+ for (const staleTask of candidates) {
233
+ if (staleTask.leaseExpiresAt === null || new Date(staleTask.leaseExpiresAt).getTime() > now)
234
+ continue;
235
+ // Re-read durable state before applying lease recovery: another reconciler may have accepted a terminal event or launched a newer execution.
236
+ const task = (await this.store.load(job.id)).tasks.find(item => item.id === staleTask.id);
237
+ if (!task)
238
+ continue;
239
+ const sameGeneration = task.executionId === staleTask.executionId && task.leaseOwner === staleTask.leaseOwner && task.leaseExpiresAt === staleTask.leaseExpiresAt;
240
+ const recoverable = task.state === 'RUNNING' || (task.state === 'QUEUED' && task.executionId !== null);
241
+ if (!sameGeneration || !recoverable || task.leaseExpiresAt === null || new Date(task.leaseExpiresAt).getTime() > now)
234
242
  continue;
235
243
  this.live.get(task.id)?.execution.cancel();
236
244
  const planner = this.plannerLive.get(task.id);
@@ -239,8 +247,7 @@ export class Reconciler extends ReconcilerReviewBase {
239
247
  planner.controller.abort();
240
248
  this.plannerLive.delete(task.id);
241
249
  }
242
- const failedEvent = task.state === 'RUNNING' ? 'task.failed' : 'fail';
243
- await transitionTaskEvent(task, failedEvent);
250
+ await transitionTaskEvent(task, task.state === 'RUNNING' ? 'task.failed' : 'fail');
244
251
  task.error = 'Lease expired';
245
252
  task.leaseOwner = null;
246
253
  task.leaseExpiresAt = null;
@@ -248,12 +255,11 @@ export class Reconciler extends ReconcilerReviewBase {
248
255
  if (task.attempt < task.maxAttempts) {
249
256
  await transitionTaskEvent(task, 'retry');
250
257
  await this.store.saveTask(task);
258
+ continue;
251
259
  }
252
- else {
253
- task.error += ' after maximum attempts';
254
- await this.store.saveTask(task);
255
- await this.emit(eventFor(job.id, task.id, task.parentTaskId, 'task.failed', 'failed', task.error, 'user', { reason: 'lease_expired' }));
256
- }
260
+ task.error += ' after maximum attempts';
261
+ await this.store.saveTask(task);
262
+ await this.emit(eventFor(job.id, task.id, task.parentTaskId, 'task.failed', 'failed', task.error, 'user', { reason: 'lease_expired' }));
257
263
  }
258
264
  }
259
265
  async planJob(jobId, guidance) {
package/dist/relayd.js CHANGED
@@ -19,7 +19,9 @@ import { deriveJobRuntimeStatus } from './runtime-status.js';
19
19
  import { decisionMode, LayaCliDecisionEngine } from './decision.js';
20
20
  import { JsonlDecisionAuditStore } from './decision-audit.js';
21
21
  import { summarizeRuntime } from './runtime-summary.js';
22
+ import { dueBuckets, processDueScheduledJobs } from './scheduled-jobs.js';
22
23
  const PR_REFRESH_COALESCE_MS = 1000;
24
+ export const SCHEDULE_HEARTBEAT_MS = 600_000;
23
25
  export function shouldCoalescePullRequestRefresh(completedAt, now = Date.now(), force = false) {
24
26
  return !force && completedAt !== null && now >= completedAt && now - completedAt < PR_REFRESH_COALESCE_MS;
25
27
  }
@@ -280,13 +282,29 @@ export async function runDaemon(argv) {
280
282
  process.stderr.write(`warning: workspace reconcile unavailable: ${error instanceof Error ? error.message : String(error)}\n`);
281
283
  }
282
284
  };
285
+ const scheduleHeartbeatSafely = async () => {
286
+ if (client.rateLimitStatus().limited)
287
+ return;
288
+ const now = new Date();
289
+ for (const repository of repositories) {
290
+ try {
291
+ await processDueScheduledJobs({ client, repository, trustedAuthors: trusted, buckets: dueBuckets(now, 36), now, engine: job => ({ reconcile: async () => { await pool.reconcileTarget(repository, job.prNumber, job.id); }, idle: async () => { } }) });
292
+ }
293
+ catch (error) {
294
+ process.stderr.write(`warning: scheduled heartbeat unavailable for ${repository}: ${error instanceof Error ? error.message : String(error)}\n`);
295
+ }
296
+ }
297
+ };
283
298
  const watchdogDefault = webhook ? SERVICE_DEFAULTS.webhookWatchdogMs : SERVICE_DEFAULTS.watchdogMs;
284
299
  const interval = setInterval(() => void reconcileSafely(), Number(value(argv, '--interval', String(watchdogDefault))));
285
300
  interval.unref();
286
- const stop = () => { clearInterval(interval); void stopWatch().finally(() => server.close(() => process.exit(0))); };
301
+ const scheduleInterval = setInterval(() => void scheduleHeartbeatSafely(), SCHEDULE_HEARTBEAT_MS);
302
+ scheduleInterval.unref();
303
+ const stop = () => { clearInterval(interval); clearInterval(scheduleInterval); void stopWatch().finally(() => server.close(() => process.exit(0))); };
287
304
  process.once('SIGINT', stop);
288
305
  process.once('SIGTERM', stop);
289
306
  console.log('Dashboard listening; repository worker pool active');
307
+ void scheduleHeartbeatSafely();
290
308
  void refreshAll(true)
291
309
  .then(items => pool.reconcileKnown(items))
292
310
  .catch(error => process.stderr.write(`warning: startup reconciliation unavailable: ${error instanceof Error ? error.message : String(error)}\n`));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agents-relay",
3
- "version": "1.0.164",
3
+ "version": "1.0.166",
4
4
  "description": "Durable async agent jobs coordinated through GitHub pull requests",
5
5
  "type": "module",
6
6
  "bin": {
@@ -109,7 +109,9 @@ SHA-bound final reviewer. For example, `video-director + market + style ->
109
109
  review-chair -> unified revision plan` is a project pattern, not a hard-coded
110
110
  Relay reviewer list.
111
111
 
112
- A scheduled autonomous job adds cron/timezone/next-run and missed-run/concurrency policy to the trusted job marker, plus the `agents-relay:autonomous` label and one bounded time-of-day `agents-relay:next-run:*` index label. The GitHub heartbeat discovers only indexed due candidates, validates the marker's exact `nextRunAt`, claims the slot idempotently, advances the durable next run, and pulses `agents-relay:schedule-wake`. GitHub webhook delivery then wakes local Relay for planner/worker execution. While the schedule is active, the durable Job remains `ACTIVE`; between runs its derived runtime status is `WAITING`. Scheduled recurring Jobs do not enter one-shot final-review/auto-merge after each satisfied run; older-cycle failures are historical evidence and do not block a later slot.
112
+ A scheduled autonomous job adds cron/timezone/next-run and missed-run/concurrency policy to the trusted job marker, plus the `agents-relay:autonomous` label and one bounded time-of-day `agents-relay:next-run:*` index label. The always-on `agents-relayd` process is the authoritative heartbeat for repositories it manages: it evaluates due schedule buckets on startup and every ten minutes and reconciles claimed work through the local worker pool. A repository may also install `.github/workflows/autonomous-scheduler.yml` as an optional cloud fallback; GitHub `schedule` delivery is best-effort and must not be relied on for precise cadence. Unscheduled autonomous Jobs and fixed Jobs do not use the scheduler heartbeat.
113
+
114
+ The scheduler heartbeat discovers only indexed due candidates, validates the marker's exact `nextRunAt`, claims the slot idempotently, advances the durable next run, and pulses `agents-relay:schedule-wake`. GitHub webhook delivery then wakes local Relay for planner/worker execution. While the schedule is active, the durable Job remains `ACTIVE`; between runs its derived runtime status is `WAITING`. Scheduled recurring Jobs do not enter one-shot final-review/auto-merge after each satisfied run; older-cycle failures are historical evidence and do not block a later slot.
113
115
 
114
116
 
115
117
  ## Task evidence and head identity