agents-relay 2.0.207 → 2.0.208

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.
@@ -0,0 +1,52 @@
1
+ import { open } from 'node:fs/promises';
2
+ /** Reverse-read newline-delimited records without loading the complete audit file. */
3
+ export async function* readJsonlNewestFirst(path) {
4
+ let file;
5
+ try {
6
+ file = await open(path, 'r');
7
+ }
8
+ catch (error) {
9
+ if (error.code === 'ENOENT')
10
+ return;
11
+ throw error;
12
+ }
13
+ try {
14
+ const { size } = await file.stat();
15
+ let offset = size;
16
+ let prefix = Buffer.alloc(0);
17
+ while (offset > 0) {
18
+ const length = Math.min(64 * 1024, offset);
19
+ offset -= length;
20
+ const chunk = Buffer.allocUnsafe(length);
21
+ const { bytesRead } = await file.read(chunk, 0, length, offset);
22
+ if (bytesRead !== length)
23
+ throw new Error('Short reverse audit read');
24
+ const block = Buffer.concat([chunk, prefix]);
25
+ let end = block.length;
26
+ for (let i = block.length - 1; i >= 0; i--) {
27
+ if (block[i] !== 10)
28
+ continue;
29
+ const line = block.subarray(i + 1, end);
30
+ end = i;
31
+ if (!line.length)
32
+ continue;
33
+ try {
34
+ yield JSON.parse(line.toString('utf8'));
35
+ }
36
+ catch {
37
+ // A concurrent writer may leave an incomplete trailing line. Ignore it.
38
+ }
39
+ }
40
+ prefix = block.subarray(0, end);
41
+ }
42
+ if (prefix.length) {
43
+ try {
44
+ yield JSON.parse(prefix.toString('utf8'));
45
+ }
46
+ catch { }
47
+ }
48
+ }
49
+ finally {
50
+ await file.close();
51
+ }
52
+ }
@@ -1,13 +1,155 @@
1
1
  import { appendFile, mkdir, readFile } from 'node:fs/promises';
2
2
  import { dirname } from 'node:path';
3
+ import { readJsonlNewestFirst } from './audit-tail.js';
3
4
  import { randomUUID, createHash } from 'node:crypto';
4
5
  import { modelChoiceId } from './model-routing.js';
5
6
  export const DECISION_AUDIT_SCHEMA_VERSION = 5;
7
+ function textField(value) {
8
+ return typeof value === 'string' && value.trim() ? value : null;
9
+ }
10
+ /** Avoid passing long worker logs, inline credentials or private task content to the decision engine. */
11
+ function routeReason(value) {
12
+ return value.replace(/\s+/g, ' ')
13
+ .replace(/\b(Bearer\s+)[A-Za-z0-9._~-]+/gi, '$1[redacted]')
14
+ .replace(/\b((?:api[_ -]?key|secret|password|access[_ -]?token|token|credential|authorization)\s*[:=]\s*)[^\s,;]+/gi, '$1[redacted]')
15
+ .slice(0, 240);
16
+ }
17
+ /**
18
+ * Router facts cross Job boundaries. Never copy arbitrary worker error messages
19
+ * into a later Job's Laya prompt: only deterministic, content-free reason codes
20
+ * may leave this private audit store.
21
+ */
22
+ function routerReasonCode(kind, summary) {
23
+ if (kind === 'success')
24
+ return 'task_completed';
25
+ if (kind === 'cancelled')
26
+ return 'task_cancelled';
27
+ if (kind === 'blocked')
28
+ return 'task_blocked';
29
+ if (kind === 'outcome_unknown')
30
+ return 'execution_outcome_unknown';
31
+ if (kind === 'auth_config_failure')
32
+ return 'authentication_rejected';
33
+ if (kind === 'timeout')
34
+ return 'worker_timeout';
35
+ if (kind === 'launch_failure')
36
+ return 'worker_launch_failed';
37
+ if (kind === 'provider_failure') {
38
+ if (/stream disconnected|response\.completed/i.test(summary))
39
+ return 'provider_stream_disconnected';
40
+ if (/rate[_ .-]?limit(?:[_ .-]?exceeded)?|HTTP 429/i.test(summary))
41
+ return 'provider_rate_limited';
42
+ if (/quota|credits? exhausted/i.test(summary))
43
+ return 'provider_quota_exhausted';
44
+ return 'provider_failure';
45
+ }
46
+ if (kind === 'runtime_failure')
47
+ return 'worker_runtime_failure';
48
+ if (kind === 'semantic_failure')
49
+ return 'task_semantic_failure';
50
+ return 'task_reported_failure';
51
+ }
52
+ function routeResult(record, route) {
53
+ const worker = textField(route.worker);
54
+ const adapter = textField(route.adapter);
55
+ const provider = textField(route.provider);
56
+ const model = textField(route.model);
57
+ if (!worker || !adapter || !provider || !model)
58
+ return null;
59
+ return {
60
+ timestamp: record.timestamp,
61
+ worker, adapter, provider, model,
62
+ profile: textField(route.profile),
63
+ reasoning: textField(route.reasoning),
64
+ result: record.kind === 'success' ? 'success' : record.kind === 'cancelled' ? 'cancelled'
65
+ : record.kind === 'blocked' ? 'blocked' : record.kind === 'outcome_unknown' ? 'unknown' : 'failure',
66
+ failure_kind: ['success', 'cancelled', 'blocked', 'outcome_unknown'].includes(record.kind) ? null : record.kind,
67
+ reason: routerReasonCode(record.kind, record.summary),
68
+ };
69
+ }
70
+ function outcomeAttemptKey(record) {
71
+ const evidence = record.evidence ?? {};
72
+ const attempt = typeof evidence.attempt === 'number' && Number.isSafeInteger(evidence.attempt) ? String(evidence.attempt) : null;
73
+ const execution = textField(evidence.execution_id);
74
+ return JSON.stringify([record.decision_id, execution ?? (attempt ? 'attempt:' + attempt : 'legacy')]);
75
+ }
76
+ /**
77
+ * Join durable decision and outcome records by decision ID, retaining only the
78
+ * latest terminal outcome per execution attempt. History is global across Jobs and sorted
79
+ * by outcome time, not by when a route was chosen. Only completed attempts count.
80
+ */
81
+ export async function recentExecutionRouteResults(store, limit = 10) {
82
+ if (store.readRecentRoutes)
83
+ return store.readRecentRoutes(limit);
84
+ const records = await store.read();
85
+ const decisions = new Map();
86
+ const outcomes = new Map();
87
+ records.forEach((record, order) => {
88
+ if (record.record_type === 'decision') {
89
+ decisions.set(record.decision_id, record);
90
+ }
91
+ else if (record.kind !== 'task_started') {
92
+ // Reused decisions from an authorized retry are separate executions.
93
+ const key = outcomeAttemptKey(record);
94
+ const current = outcomes.get(key);
95
+ if (!current || record.timestamp > current.record.timestamp || (record.timestamp === current.record.timestamp && order > current.order)) {
96
+ outcomes.set(key, { record, order });
97
+ }
98
+ }
99
+ });
100
+ const results = [];
101
+ for (const { record, order } of outcomes.values()) {
102
+ const decision = decisions.get(record.decision_id);
103
+ const route = decision?.selected;
104
+ const worker = route?.worker && typeof route.worker === 'object' ? route.worker : undefined;
105
+ const model = route?.model && typeof route.model === 'object' ? route.model : undefined;
106
+ const result = routeResult(record, {
107
+ worker: worker?.id, adapter: worker?.adapter, provider: model?.provider, model: model?.model,
108
+ profile: model?.profile, reasoning: model?.reasoning,
109
+ });
110
+ if (result)
111
+ results.push({ ...result, order });
112
+ }
113
+ return results.sort((a, b) => b.timestamp.localeCompare(a.timestamp) || b.order - a.order)
114
+ .slice(0, Math.max(0, Math.min(10, Math.trunc(limit))))
115
+ .map(({ order: _order, ...result }) => result);
116
+ }
6
117
  export class JsonlDecisionAuditStore {
7
118
  path;
8
119
  constructor(path) {
9
120
  this.path = path;
10
121
  }
122
+ /** The normal path reads backward only until ten distinct completed attempts.
123
+ * Legacy outcomes without embedded routing facts use a one-time compatibility
124
+ * fallback to the full audit reader; new outcomes are self-contained. */
125
+ async readRecentRoutes(limit = 10) {
126
+ const cap = Math.max(0, Math.min(10, Math.trunc(limit)));
127
+ if (cap === 0)
128
+ return [];
129
+ const results = [];
130
+ const seen = new Set();
131
+ let needsLegacyJoin = false;
132
+ for await (const record of readJsonlNewestFirst(this.path)) {
133
+ if (record.record_type !== 'outcome' || record.kind === 'task_started')
134
+ continue;
135
+ const key = outcomeAttemptKey(record);
136
+ if (seen.has(key))
137
+ continue;
138
+ seen.add(key);
139
+ const route = record.evidence?.route;
140
+ const result = route && typeof route === 'object' && !Array.isArray(route)
141
+ ? routeResult(record, route) : null;
142
+ if (result)
143
+ results.push(result);
144
+ else
145
+ needsLegacyJoin = true;
146
+ if (seen.size >= cap)
147
+ break;
148
+ }
149
+ if (needsLegacyJoin)
150
+ return recentExecutionRouteResults({ read: () => this.read() }, cap);
151
+ return results.sort((a, b) => b.timestamp.localeCompare(a.timestamp)).slice(0, cap);
152
+ }
11
153
  async append(record) { await mkdir(dirname(this.path), { recursive: true }); await appendFile(this.path, `${JSON.stringify(sanitize(record))}\n`, 'utf8'); }
12
154
  async read() {
13
155
  try {
@@ -29,7 +171,7 @@ function sanitize(value, key = '') {
29
171
  }
30
172
  export function decisionAuditId() { return randomUUID(); }
31
173
  export function auditInputHash(record) { return createHash('sha256').update(JSON.stringify(record)).digest('hex'); }
32
- export function outcomeForDecision(decisionId, kind, summary, evidence) { return { record_type: 'outcome', schema_version: DECISION_AUDIT_SCHEMA_VERSION, outcome_id: randomUUID(), decision_id: decisionId, timestamp: new Date().toISOString(), kind, summary, evidence }; }
174
+ export function outcomeForDecision(decisionId, kind, summary, evidence) { return { record_type: 'outcome', schema_version: DECISION_AUDIT_SCHEMA_VERSION, outcome_id: randomUUID(), decision_id: decisionId, timestamp: new Date().toISOString(), kind, summary: routeReason(summary), evidence }; }
33
175
  export function taskAuditFacts(task) {
34
176
  if (!task)
35
177
  return {};
@@ -1,4 +1,4 @@
1
- import { makeExecutionDecisionAudit } from './decision-audit.js';
1
+ import { makeExecutionDecisionAudit, recentExecutionRouteResults } from './decision-audit.js';
2
2
  import { discoverModelCandidates } from './model-routing.js';
3
3
  import { loadExecutionRouterRules, loadRoutingPolicy, policyAllows } from './routing-policy.js';
4
4
  import { DEFAULT_CHATGPT_PREFERRED_CONCURRENCY } from './worker-routing.js';
@@ -274,6 +274,10 @@ export async function routeExecution(workers, requirements, options = {}) {
274
274
  const chatgptRunning = options.chatgptRunning ?? 0;
275
275
  const taskFacts = options.taskFacts ?? {};
276
276
  const taskShape = executionTaskShape(taskFacts);
277
+ const historyStore = options.historyStore ?? options.auditStore;
278
+ const recentRouteResults = historyStore
279
+ ? await recentExecutionRouteResults(historyStore).catch(() => []) // Advisory history cannot block execution.
280
+ : [];
277
281
  const discoveredDefaults = new Map();
278
282
  for (const worker of workers) {
279
283
  const discovered = await discoverModelCandidates(worker, options.registered ?? [], options.usage, undefined, options.rulesPath);
@@ -318,8 +322,9 @@ export async function routeExecution(workers, requirements, options = {}) {
318
322
  chatgpt_running: chatgptRunning,
319
323
  preferred_chatgpt_concurrency: preferredChatgptConcurrency,
320
324
  },
325
+ recent_route_results: recentRouteResults,
321
326
  }));
322
- const question = appendRoutingInstruction('User routing instructions have highest priority over default preferences, task size, runtime and resource signals. Candidate order is arbitrary and conveys no priority, ranking or preference. Do not favor earlier choices. Select an offered route that obeys the user instructions.\nUser routing instructions:\n' + routingInstruction, 'For vision, image generation, visual review, and final review tasks, prefer ChatGPT Worker when available and within capacity; for coding, CLI and filesystem tasks, prefer Codex. These are soft preferences subordinate to user rules. Choose exactly one listed route; do not invent routes.');
327
+ const question = appendRoutingInstruction('User routing instructions have highest priority over default preferences, task size, runtime and resource signals. Candidate order is arbitrary and conveys no priority, ranking or preference. Do not favor earlier choices. Select an offered route that obeys the user instructions.\nUser routing instructions:\n' + routingInstruction, 'Use recent_route_results (up to the last 10 confirmed execution outcomes) to weigh route reliability, including success, failure and the recorded reason. Distinguish provider/runtime faults from task-specific failures and cancellations; historical failures are evidence, not automatic hard exclusions. For vision, image generation, visual review, and final review tasks, prefer ChatGPT Worker when available and within capacity; for coding, CLI and filesystem tasks, prefer Codex. These are soft preferences subordinate to user rules. Choose exactly one listed route; do not invent routes.');
323
328
  let selected = fallback;
324
329
  let evidence = { mode: options.decisionMode ?? 'off', fallback: true };
325
330
  const decisionStages = [];
@@ -380,6 +385,7 @@ export async function routeExecution(workers, requirements, options = {}) {
380
385
  candidateRoutes: routes.map(route => ({ id: route.id, workerId: route.worker.workerId, adapter: route.worker.adapter })),
381
386
  effectiveChoices,
382
387
  routingInstruction,
388
+ recentRouteResults,
383
389
  },
384
390
  policy,
385
391
  routing,
@@ -5,6 +5,7 @@ import { implementationComplete, implementationTasks } from './runtime-status.js
5
5
  import { eventFor } from './events.js';
6
6
  import { workerCandidates } from './worker-routing.js';
7
7
  import { routeExecution } from './execution-routing.js';
8
+ import { recordRouteOutcome } from './route-outcome-recording.js';
8
9
  import { ReconcilerRuntimeBase } from './reconciler-runtime.js';
9
10
  import { dispatchReviewLifecycle, reviewOutcome } from './workflow/review-v2.js';
10
11
  import { dispatchTaskLifecycleSync } from './workflow/lifecycle.js';
@@ -260,6 +261,7 @@ export class ReconcilerReviewBase extends ReconcilerRuntimeBase {
260
261
  registered,
261
262
  chatgptRunning: runtimeSummary?.chatgptRunning ?? 0,
262
263
  routingConstraints: configured?.routing,
264
+ auditStore: this.options.decisionAuditStore,
263
265
  });
264
266
  return {
265
267
  candidate: routed.candidate,
@@ -479,6 +481,11 @@ export class ReconcilerReviewBase extends ReconcilerRuntimeBase {
479
481
  if (outcome === 'Failed')
480
482
  throw Error('reviewer start postcondition violated');
481
483
  }
484
+ // A changes-requested verdict is a successfully delivered review, not a failed model route.
485
+ if (event.type === 'review.approved' || event.type === 'review.changes_required') {
486
+ const committedReviewer = job.tasks.find(item => item.id === task.id) ?? task;
487
+ await recordRouteOutcome(this.options.decisionAuditStore, committedReviewer, 'success', 'review.' + outcome, { event_type: event.type });
488
+ }
482
489
  // Review approval records evidence only. The subsequent Graph reconcile
483
490
  // drives durable local Git merge delivery and terminalization.
484
491
  if (outcome === 'approved')
@@ -7,6 +7,7 @@ import { classifyChatGptPlatformFailure, classifyWorkerRejection, reroutableWork
7
7
  import { applyTaskRetryAction } from './task-actions.js';
8
8
  import { DurableTaskTransitionRuntime } from './workflow/durable-transition.js';
9
9
  import { eventFor } from './events.js';
10
+ import { recordTerminalRouteOutcome, recordLaunchRouteOutcome, recordWatchdogRouteOutcome } from './route-outcome-recording.js';
10
11
  import { WorkflowEffectExecutor } from './workflow/effects.js';
11
12
  import { ReconcileCoordinator } from './reconcile-coordinator.js';
12
13
  import { acceptedReportedTag, eventDataWithoutTag } from './task-tag.js';
@@ -294,6 +295,7 @@ export class ReconcilerRuntimeBase {
294
295
  await this.emit(eventFor(jobId, taskId, transition.task.parentTaskId, 'task.process.delivery_failed', 'failed', `Worker ${phase} reported after lifecycle moved to ${transition.from}: ${message}`, 'orchestrator', { phase, authoritative: false, executionId, workflowEvent }, { agent: 'relay', component: 'agents-relay' }));
295
296
  return;
296
297
  }
298
+ await recordLaunchRouteOutcome(this.options.decisionAuditStore, transition.task, message, phase, executionId, unknownOutcome);
297
299
  if (executionId) {
298
300
  const live = this.live.get(taskId);
299
301
  if (live?.executionId === executionId) {
@@ -362,6 +364,7 @@ export class ReconcilerRuntimeBase {
362
364
  this.armInactivityWatchdog(latest, executionId);
363
365
  return;
364
366
  }
367
+ await recordWatchdogRouteOutcome(this.options.decisionAuditStore, transition.task, { executionId, failedExit, rawError, failureReason, failureMessage, exitCode: exit?.exitCode ?? null });
365
368
  if (live?.executionId === executionId) {
366
369
  live.controller.abort();
367
370
  live.execution.cancel();
@@ -477,6 +480,8 @@ export class ReconcilerRuntimeBase {
477
480
  }));
478
481
  if (!transition?.decision.accepted)
479
482
  return { applied: false, reconcile: false, reason: `${event.type}.ignored` };
483
+ if (lifecycleEvent !== 'interactive.waiting')
484
+ await recordTerminalRouteOutcome(this.options.decisionAuditStore, transition.task, event, terminalEventExecutionId ?? null);
480
485
  if (transition.task.adapter === 'codex' && liveAtTerminal && !liveAtTerminal.processSettled && liveAtTerminal.jobId === event.job_id && (!terminalEventExecutionId || liveAtTerminal.executionId === terminalEventExecutionId)) {
481
486
  liveAtTerminal.controller.abort();
482
487
  liveAtTerminal.execution.cancel();
package/dist/relayd.js CHANGED
@@ -504,7 +504,7 @@ export async function runDaemon(argv) {
504
504
  storySize: task.storySize ?? 'M',
505
505
  },
506
506
  usage,
507
- chatgptRunning: runtime.workers.chatgpt,
507
+ chatgptRunning: runtime.workers.chatgpt, historyStore: decisionAuditStore,
508
508
  });
509
509
  return {
510
510
  selected: {
@@ -0,0 +1,51 @@
1
+ import { outcomeForDecision } from './decision-audit.js';
2
+ /** Preserve the source classification except when the failure text gives clear evidence. */
3
+ export function classifyRoutingFailure(message, fallback) {
4
+ if (/invalid (?:api|public) key|(?:authentication|authorization) failed|401 unauthorized/i.test(message))
5
+ return 'auth_config_failure';
6
+ if (/stream disconnected|response\.completed|rate[_ .-]?limit(?:[_ .-]?exceeded)?|quota exceeded|provider[_ .-]?unavailable|model[_ .-]?(?:unavailable|not[_ .-]?found)|service unavailable/i.test(message))
7
+ return 'provider_failure';
8
+ if (/timeout|timed out|deadline exceeded/i.test(message))
9
+ return 'timeout';
10
+ return fallback;
11
+ }
12
+ /** One best-effort audit effect after an authoritative Graph/SQLite transition. */
13
+ export async function recordRouteOutcome(store, task, kind, reason, evidence = {}) {
14
+ if (!store || !task.routing?.decisionId)
15
+ return;
16
+ try {
17
+ await store.append(outcomeForDecision(task.routing.decisionId, kind, reason, {
18
+ job_id: task.jobId, task_id: task.id, attempt: task.attempt,
19
+ route: {
20
+ worker: task.workerSelection?.workerId ?? task.adapter,
21
+ adapter: task.adapter,
22
+ provider: task.routing.provider,
23
+ model: task.routing.model,
24
+ profile: task.routing.profile ?? null,
25
+ reasoning: task.routing.reasoning ?? null,
26
+ },
27
+ ...evidence,
28
+ }));
29
+ }
30
+ catch (error) {
31
+ // A failed audit append must not undo an already committed SQLite transition.
32
+ process.stderr.write('[agents-relay] route outcome audit unavailable: ' + (error instanceof Error ? error.message : String(error)) + '\n');
33
+ }
34
+ }
35
+ /** Accepted terminal Worker event; reviewer/human gates remain distinct outcomes. */
36
+ export async function recordTerminalRouteOutcome(store, task, event, executionId) {
37
+ const kind = event.type === 'task.completed' ? 'success'
38
+ : event.type === 'task.cancelled' ? 'cancelled' : event.type === 'task.blocked' ? 'blocked'
39
+ : classifyRoutingFailure([event.message, event.data?.reason].join(' '), 'task_failure');
40
+ const reason = event.type === 'task.completed' ? 'task.completed'
41
+ : [typeof event.data?.reason === 'string' ? event.data.reason : null, event.message].filter(Boolean).join(': ') || event.type;
42
+ await recordRouteOutcome(store, task, kind, reason, { execution_id: executionId, event_type: event.type });
43
+ }
44
+ /** Launcher Graph accepted a failure or safe pre-submission fallback. */
45
+ export async function recordLaunchRouteOutcome(store, task, message, phase, executionId, unknownOutcome) {
46
+ await recordRouteOutcome(store, task, unknownOutcome ? 'outcome_unknown' : classifyRoutingFailure(message, phase === 'launch' ? 'launch_failure' : 'runtime_failure'), 'worker_' + phase + '_failure: ' + message, { phase, execution_id: executionId, target_state: task.state, outcome_unknown: unknownOutcome });
47
+ }
48
+ /** Watchdog Graph applies its durable state before emitting a non-authoritative event. */
49
+ export async function recordWatchdogRouteOutcome(store, task, evidence) {
50
+ await recordRouteOutcome(store, task, evidence.failedExit ? classifyRoutingFailure(evidence.rawError, 'runtime_failure') : 'timeout', (evidence.failureReason ?? 'watchdog_failed') + ': ' + evidence.failureMessage, { execution_id: evidence.executionId, watchdog: true, exit_code: evidence.exitCode });
51
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agents-relay",
3
- "version": "2.0.207",
3
+ "version": "2.0.208",
4
4
  "description": "Durable async agent jobs coordinated through GitHub pull requests",
5
5
  "repository": {
6
6
  "type": "git",