patchrelay 0.75.3 → 0.77.0

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 (45) hide show
  1. package/dist/agent-input-service.js +40 -26
  2. package/dist/build-info.json +3 -3
  3. package/dist/cli/data.js +3 -1
  4. package/dist/db/issue-session-store.js +44 -9
  5. package/dist/db/issue-store.js +11 -2
  6. package/dist/db/migrations.js +3 -0
  7. package/dist/factory-state.js +23 -0
  8. package/dist/github-webhook-reactive-run.js +15 -11
  9. package/dist/github-webhook-stack-coordination.js +8 -4
  10. package/dist/github-webhook-state-projector.js +204 -139
  11. package/dist/github-webhook-terminal-handler.js +37 -27
  12. package/dist/idle-reconciliation.js +122 -66
  13. package/dist/implementation-outcome-policy.js +5 -1
  14. package/dist/issue-session-projection-invalidator.js +9 -0
  15. package/dist/linear-agent-session-client.js +16 -8
  16. package/dist/linear-issue-projection.js +15 -11
  17. package/dist/linear-status-comment-sync.js +8 -4
  18. package/dist/linear-workflow-state-sync.js +9 -5
  19. package/dist/merged-linear-completion-reconciler.js +39 -17
  20. package/dist/no-pr-completion-check.js +51 -29
  21. package/dist/orchestration-parent-wake.js +15 -8
  22. package/dist/queue-health-monitor.js +17 -8
  23. package/dist/reactive-run-policy.js +5 -1
  24. package/dist/run-budgets.js +40 -6
  25. package/dist/run-completion-policy.js +50 -9
  26. package/dist/run-failure-policy.js +463 -0
  27. package/dist/run-finalizer.js +68 -35
  28. package/dist/run-launcher.js +63 -12
  29. package/dist/run-notification-handler.js +19 -9
  30. package/dist/run-orchestrator.js +70 -78
  31. package/dist/run-reconciler.js +137 -64
  32. package/dist/run-settlement.js +57 -0
  33. package/dist/run-wake-planner.js +39 -29
  34. package/dist/service-issue-actions.js +45 -28
  35. package/dist/service-startup-recovery.js +61 -35
  36. package/dist/telemetry.js +9 -0
  37. package/dist/terminal-wake-reconciler.js +20 -3
  38. package/dist/webhooks/agent-session-handler.js +22 -12
  39. package/dist/webhooks/dependency-readiness-handler.js +17 -10
  40. package/dist/webhooks/desired-stage-recorder.js +32 -13
  41. package/dist/webhooks/issue-removal-handler.js +24 -13
  42. package/package.json +1 -1
  43. package/dist/interrupted-run-recovery.js +0 -227
  44. package/dist/run-recovery-service.js +0 -202
  45. package/dist/zombie-recovery.js +0 -13
@@ -1,227 +0,0 @@
1
- import { ACTIVE_RUN_STATES } from "./factory-state.js";
2
- import { buildRunFailureActivity } from "./linear-session-reporting.js";
3
- import { deriveIssueSessionReactiveIntent } from "./issue-session.js";
4
- import { isRequestedChangesRunType } from "./reactive-pr-state.js";
5
- function resolveRetryRunType(runType, context) {
6
- if (runType === "branch_upkeep") {
7
- return "branch_upkeep";
8
- }
9
- return context?.reviewFixMode === "branch_upkeep" || context?.branchUpkeepRequired === true
10
- ? "branch_upkeep"
11
- : "review_fix";
12
- }
13
- function resolvePostRunState(issue) {
14
- if (ACTIVE_RUN_STATES.has(issue.factoryState) && issue.prNumber) {
15
- if (issue.prState === "merged")
16
- return "done";
17
- if (issue.prReviewState === "approved")
18
- return "awaiting_queue";
19
- return "pr_open";
20
- }
21
- return undefined;
22
- }
23
- export function resolveRecoverablePostRunState(issue) {
24
- if (!issue.prNumber) {
25
- return resolvePostRunState(issue);
26
- }
27
- if (issue.prState === "merged")
28
- return "done";
29
- if (issue.prState === "open") {
30
- const reactiveIntent = deriveIssueSessionReactiveIntent({
31
- prNumber: issue.prNumber,
32
- prState: issue.prState,
33
- prReviewState: issue.prReviewState,
34
- prCheckStatus: issue.prCheckStatus,
35
- latestFailureSource: issue.lastGitHubFailureSource,
36
- });
37
- if (reactiveIntent)
38
- return reactiveIntent.compatibilityFactoryState;
39
- if (issue.prReviewState === "approved")
40
- return "awaiting_queue";
41
- return "pr_open";
42
- }
43
- return resolvePostRunState(issue);
44
- }
45
- export class InterruptedRunRecovery {
46
- db;
47
- logger;
48
- linearSync;
49
- withHeldLease;
50
- releaseLease;
51
- failRunAndClear;
52
- restoreIdleWorktree;
53
- completionPolicy;
54
- enqueueIssue;
55
- feed;
56
- constructor(db, logger, linearSync, withHeldLease, releaseLease, failRunAndClear, restoreIdleWorktree, completionPolicy, enqueueIssue, feed) {
57
- this.db = db;
58
- this.logger = logger;
59
- this.linearSync = linearSync;
60
- this.withHeldLease = withHeldLease;
61
- this.releaseLease = releaseLease;
62
- this.failRunAndClear = failRunAndClear;
63
- this.restoreIdleWorktree = restoreIdleWorktree;
64
- this.completionPolicy = completionPolicy;
65
- this.enqueueIssue = enqueueIssue;
66
- this.feed = feed;
67
- }
68
- async handle(run, issue) {
69
- this.logger.warn({ issueKey: issue.issueKey, runType: run.runType, threadId: run.threadId }, "Run has interrupted turn - marking as failed");
70
- const repairedCounters = this.withHeldLease(issue.projectId, issue.linearIssueId, (lease) => {
71
- if (run.runType === "ci_repair" && issue.ciRepairAttempts > 0) {
72
- this.db.issueSessions.upsertIssueWithLease(lease, {
73
- projectId: issue.projectId,
74
- linearIssueId: issue.linearIssueId,
75
- ciRepairAttempts: issue.ciRepairAttempts - 1,
76
- });
77
- }
78
- else if (run.runType === "queue_repair" && issue.queueRepairAttempts > 0) {
79
- this.db.issueSessions.upsertIssueWithLease(lease, {
80
- projectId: issue.projectId,
81
- linearIssueId: issue.linearIssueId,
82
- queueRepairAttempts: issue.queueRepairAttempts - 1,
83
- });
84
- }
85
- else if (isRequestedChangesRunType(run.runType) && issue.reviewFixAttempts > 0) {
86
- this.db.issueSessions.upsertIssueWithLease(lease, {
87
- projectId: issue.projectId,
88
- linearIssueId: issue.linearIssueId,
89
- reviewFixAttempts: issue.reviewFixAttempts - 1,
90
- });
91
- }
92
- if (run.runType === "ci_repair" || run.runType === "queue_repair") {
93
- this.db.issueSessions.upsertIssueWithLease(lease, {
94
- projectId: issue.projectId,
95
- linearIssueId: issue.linearIssueId,
96
- lastAttemptedFailureHeadSha: null,
97
- lastAttemptedFailureSignature: null,
98
- lastAttemptedFailureAt: null,
99
- });
100
- }
101
- return true;
102
- });
103
- if (!repairedCounters) {
104
- this.logger.warn({ runId: run.id, issueId: run.linearIssueId }, "Skipping interrupted-run recovery after losing issue-session lease");
105
- this.releaseLease(run.projectId, run.linearIssueId);
106
- return;
107
- }
108
- if (isRequestedChangesRunType(run.runType)) {
109
- await this.handleInterruptedRequestedChangesRun(run, issue);
110
- return;
111
- }
112
- if (run.runType === "implementation" && !issue.prNumber) {
113
- await this.handleInterruptedImplementationRun(run, issue);
114
- return;
115
- }
116
- const recoveredState = resolveRecoverablePostRunState(this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? issue);
117
- this.failRunAndClear(run, "Codex turn was interrupted", recoveredState);
118
- await this.restoreIdleWorktree(issue);
119
- const failedIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? issue;
120
- if (recoveredState) {
121
- this.feed?.publish({
122
- level: "info",
123
- kind: "stage",
124
- issueKey: issue.issueKey,
125
- projectId: run.projectId,
126
- stage: recoveredState,
127
- status: "reconciled",
128
- summary: `Interrupted ${run.runType} recovered -> ${recoveredState}`,
129
- });
130
- }
131
- else {
132
- void this.linearSync.emitActivity(failedIssue, buildRunFailureActivity(run.runType, "The Codex turn was interrupted."));
133
- }
134
- void this.linearSync.syncSession(failedIssue, { activeRunType: run.runType });
135
- this.releaseLease(run.projectId, run.linearIssueId);
136
- }
137
- async handleInterruptedImplementationRun(run, issue) {
138
- const interruptedMessage = "Implementation run was interrupted before PatchRelay could publish a PR";
139
- this.failRunAndClear(run, "Codex turn was interrupted", "delegated");
140
- await this.restoreIdleWorktree(issue);
141
- const refreshedIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? issue;
142
- this.db.issueSessions.appendIssueSessionEventRespectingActiveLease(run.projectId, run.linearIssueId, {
143
- projectId: run.projectId,
144
- linearIssueId: run.linearIssueId,
145
- eventType: "delegated",
146
- dedupeKey: `interrupted_implementation:implementation:${run.linearIssueId}`,
147
- });
148
- if (!this.db.workflowWakes.peekIssueWake(run.projectId, run.linearIssueId)) {
149
- const failedIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? refreshedIssue;
150
- this.feed?.publish({
151
- level: "error",
152
- kind: "workflow",
153
- issueKey: issue.issueKey,
154
- projectId: run.projectId,
155
- stage: run.runType,
156
- status: "escalated",
157
- summary: interruptedMessage,
158
- });
159
- void this.linearSync.emitActivity(failedIssue, buildRunFailureActivity(run.runType, interruptedMessage));
160
- void this.linearSync.syncSession(failedIssue, { activeRunType: run.runType });
161
- this.releaseLease(run.projectId, run.linearIssueId);
162
- return;
163
- }
164
- this.feed?.publish({
165
- level: "warn",
166
- kind: "workflow",
167
- issueKey: issue.issueKey,
168
- projectId: run.projectId,
169
- stage: run.runType,
170
- status: "retry_queued",
171
- summary: "Implementation run was interrupted; PatchRelay will retry automatically",
172
- });
173
- const recoveredIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? refreshedIssue;
174
- void this.linearSync.syncSession(recoveredIssue, { activeRunType: run.runType });
175
- this.enqueueIssue(run.projectId, run.linearIssueId);
176
- this.releaseLease(run.projectId, run.linearIssueId);
177
- }
178
- async handleInterruptedRequestedChangesRun(run, issue) {
179
- const freshIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? issue;
180
- const refreshedIssue = await this.completionPolicy.refreshIssueAfterReactivePublish(run, freshIssue);
181
- const retryContext = await this.completionPolicy.resolveRequestedChangesWakeContext(refreshedIssue, run.runType, run.runType === "branch_upkeep"
182
- ? {
183
- branchUpkeepRequired: true,
184
- reviewFixMode: "branch_upkeep",
185
- wakeReason: "branch_upkeep",
186
- }
187
- : undefined);
188
- const retryRunType = resolveRetryRunType(run.runType, retryContext);
189
- const recoveredState = resolveRecoverablePostRunState(refreshedIssue) ?? "failed";
190
- const interruptedMessage = "Requested-changes run was interrupted before PatchRelay could verify that a new PR head was published";
191
- this.failRunAndClear(run, interruptedMessage, recoveredState);
192
- await this.restoreIdleWorktree(issue);
193
- const recoveredIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? refreshedIssue;
194
- if (recoveredState === "changes_requested") {
195
- this.db.issues.upsertIssue({
196
- projectId: run.projectId,
197
- linearIssueId: run.linearIssueId,
198
- pendingRunType: retryRunType,
199
- pendingRunContextJson: retryContext ? JSON.stringify(retryContext) : null,
200
- });
201
- this.feed?.publish({
202
- level: "warn",
203
- kind: "workflow",
204
- issueKey: issue.issueKey,
205
- projectId: run.projectId,
206
- stage: run.runType,
207
- status: "retry_queued",
208
- summary: "Requested-changes run was interrupted; PatchRelay will retry from fresh GitHub truth",
209
- });
210
- this.enqueueIssue(run.projectId, run.linearIssueId);
211
- }
212
- else {
213
- this.feed?.publish({
214
- level: "error",
215
- kind: "workflow",
216
- issueKey: issue.issueKey,
217
- projectId: run.projectId,
218
- stage: run.runType,
219
- status: "escalated",
220
- summary: interruptedMessage,
221
- });
222
- }
223
- void this.linearSync.emitActivity(recoveredIssue, buildRunFailureActivity(run.runType, interruptedMessage));
224
- void this.linearSync.syncSession(recoveredIssue, { activeRunType: run.runType });
225
- this.releaseLease(run.projectId, run.linearIssueId);
226
- }
227
- }
@@ -1,202 +0,0 @@
1
- import { buildRunFailureActivity } from "./linear-session-reporting.js";
2
- import { getRemainingZombieRecoveryDelayMs } from "./zombie-recovery.js";
3
- const DEFAULT_ZOMBIE_RECOVERY_BUDGET = 5;
4
- export class RunRecoveryService {
5
- db;
6
- logger;
7
- linearSync;
8
- withHeldLease;
9
- getHeldLease;
10
- appendWakeEventWithLease;
11
- releaseLease;
12
- enqueueIssue;
13
- feed;
14
- constructor(db, logger, linearSync, withHeldLease, getHeldLease, appendWakeEventWithLease, releaseLease, enqueueIssue, feed) {
15
- this.db = db;
16
- this.logger = logger;
17
- this.linearSync = linearSync;
18
- this.withHeldLease = withHeldLease;
19
- this.getHeldLease = getHeldLease;
20
- this.appendWakeEventWithLease = appendWakeEventWithLease;
21
- this.releaseLease = releaseLease;
22
- this.enqueueIssue = enqueueIssue;
23
- this.feed = feed;
24
- }
25
- recoverOrEscalate(params) {
26
- const { issue, runType, reason } = params;
27
- const fresh = this.db.issues.getIssue(issue.projectId, issue.linearIssueId);
28
- if (!fresh)
29
- return;
30
- if (params.isRequestedChangesRunType(runType)) {
31
- const updated = this.withHeldLease(fresh.projectId, fresh.linearIssueId, (lease) => {
32
- this.db.issueSessions.clearPendingIssueSessionEventsWithLease(lease);
33
- this.db.issueSessions.upsertIssueWithLease(lease, {
34
- projectId: fresh.projectId,
35
- linearIssueId: fresh.linearIssueId,
36
- pendingRunType: null,
37
- pendingRunContextJson: null,
38
- factoryState: "escalated",
39
- });
40
- return true;
41
- });
42
- if (!updated) {
43
- this.logger.warn({ issueKey: fresh.issueKey, reason }, "Skipping review-fix recovery escalation after losing issue-session lease");
44
- this.releaseLease(fresh.projectId, fresh.linearIssueId);
45
- return;
46
- }
47
- this.logger.warn({ issueKey: fresh.issueKey, reason }, "Requested-changes run failed before a new head was published - escalating");
48
- this.feed?.publish({
49
- level: "error",
50
- kind: "workflow",
51
- issueKey: fresh.issueKey,
52
- projectId: fresh.projectId,
53
- stage: runType,
54
- status: "escalated",
55
- summary: `Requested-changes run failed before publishing a new head (${reason})`,
56
- });
57
- this.releaseLease(fresh.projectId, fresh.linearIssueId);
58
- return;
59
- }
60
- if (fresh.prState === "merged") {
61
- const updated = this.withHeldLease(fresh.projectId, fresh.linearIssueId, (lease) => {
62
- this.db.issueSessions.upsertIssueWithLease(lease, {
63
- projectId: fresh.projectId,
64
- linearIssueId: fresh.linearIssueId,
65
- factoryState: "done",
66
- zombieRecoveryAttempts: 0,
67
- lastZombieRecoveryAt: null,
68
- });
69
- return true;
70
- });
71
- if (!updated) {
72
- this.logger.warn({ issueKey: fresh.issueKey, reason }, "Skipping merged recovery completion after losing issue-session lease");
73
- this.releaseLease(fresh.projectId, fresh.linearIssueId);
74
- return;
75
- }
76
- this.logger.info({ issueKey: fresh.issueKey, reason }, "Recovery: PR already merged - transitioning to done");
77
- this.releaseLease(fresh.projectId, fresh.linearIssueId);
78
- return;
79
- }
80
- const attempts = fresh.zombieRecoveryAttempts + 1;
81
- if (attempts > DEFAULT_ZOMBIE_RECOVERY_BUDGET) {
82
- const updated = this.withHeldLease(fresh.projectId, fresh.linearIssueId, (lease) => {
83
- this.db.issueSessions.upsertIssueWithLease(lease, {
84
- projectId: fresh.projectId,
85
- linearIssueId: fresh.linearIssueId,
86
- factoryState: "escalated",
87
- });
88
- return true;
89
- });
90
- if (!updated) {
91
- this.logger.warn({ issueKey: fresh.issueKey, attempts, reason }, "Skipping recovery escalation after losing issue-session lease");
92
- this.releaseLease(fresh.projectId, fresh.linearIssueId);
93
- return;
94
- }
95
- this.logger.warn({ issueKey: fresh.issueKey, attempts, reason }, "Recovery: budget exhausted - escalating");
96
- this.feed?.publish({
97
- level: "error",
98
- kind: "workflow",
99
- issueKey: fresh.issueKey,
100
- projectId: fresh.projectId,
101
- stage: "escalated",
102
- status: "budget_exhausted",
103
- summary: `${reason} recovery failed after ${DEFAULT_ZOMBIE_RECOVERY_BUDGET} attempts`,
104
- });
105
- this.releaseLease(fresh.projectId, fresh.linearIssueId);
106
- return;
107
- }
108
- if (fresh.lastZombieRecoveryAt) {
109
- const remainingDelayMs = getRemainingZombieRecoveryDelayMs(fresh.lastZombieRecoveryAt, fresh.zombieRecoveryAttempts);
110
- if (remainingDelayMs > 0) {
111
- this.withHeldLease(fresh.projectId, fresh.linearIssueId, (lease) => {
112
- this.appendWakeEventWithLease(lease, fresh, runType, undefined, `recovery:${attempts}`);
113
- });
114
- this.logger.debug({ issueKey: fresh.issueKey, attempts: fresh.zombieRecoveryAttempts, remainingDelayMs }, "Recovery: backoff not elapsed, deferring retry");
115
- return;
116
- }
117
- }
118
- const requeued = this.withHeldLease(fresh.projectId, fresh.linearIssueId, (lease) => {
119
- this.db.issueSessions.upsertIssueWithLease(lease, {
120
- projectId: fresh.projectId,
121
- linearIssueId: fresh.linearIssueId,
122
- pendingRunType: null,
123
- pendingRunContextJson: null,
124
- zombieRecoveryAttempts: attempts,
125
- lastZombieRecoveryAt: new Date().toISOString(),
126
- });
127
- return this.appendWakeEventWithLease(lease, fresh, runType, undefined, `recovery:${attempts}`);
128
- });
129
- if (!requeued) {
130
- this.logger.warn({ issueKey: fresh.issueKey, attempts, reason }, "Skipping recovery re-enqueue after losing issue-session lease");
131
- this.releaseLease(fresh.projectId, fresh.linearIssueId);
132
- return;
133
- }
134
- this.enqueueIssue(fresh.projectId, fresh.linearIssueId);
135
- this.logger.info({ issueKey: fresh.issueKey, attempts, reason }, "Recovery: re-enqueued with backoff");
136
- }
137
- escalate(params) {
138
- const { issue, runType, reason } = params;
139
- this.logger.warn({ issueKey: issue.issueKey, runType, reason }, "Escalating to human");
140
- const escalated = this.withHeldLease(issue.projectId, issue.linearIssueId, (lease) => {
141
- if (issue.activeRunId) {
142
- this.db.issueSessions.finishRunWithLease(lease, issue.activeRunId, { status: "released" });
143
- }
144
- this.db.issueSessions.clearPendingIssueSessionEventsWithLease(lease);
145
- this.db.issueSessions.upsertIssueWithLease(lease, {
146
- projectId: issue.projectId,
147
- linearIssueId: issue.linearIssueId,
148
- pendingRunType: null,
149
- pendingRunContextJson: null,
150
- activeRunId: null,
151
- factoryState: "escalated",
152
- });
153
- return true;
154
- });
155
- if (!escalated) {
156
- this.logger.warn({ issueKey: issue.issueKey, runType }, "Skipping escalation write after losing issue-session lease");
157
- this.releaseLease(issue.projectId, issue.linearIssueId);
158
- return;
159
- }
160
- this.feed?.publish({
161
- level: "error",
162
- kind: "workflow",
163
- issueKey: issue.issueKey,
164
- projectId: issue.projectId,
165
- stage: runType,
166
- status: "escalated",
167
- summary: `Escalated: ${reason}`,
168
- });
169
- const escalatedIssue = this.db.issues.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
170
- void this.linearSync.emitActivity(escalatedIssue, {
171
- type: "error",
172
- body: `PatchRelay needs human help to continue.\n\n${reason}`,
173
- });
174
- void this.linearSync.syncSession(escalatedIssue);
175
- this.releaseLease(issue.projectId, issue.linearIssueId);
176
- }
177
- failRunAndClear(params) {
178
- const { run, message, nextState } = params;
179
- const updated = this.withHeldLease(run.projectId, run.linearIssueId, (lease) => {
180
- this.db.runs.finishRun(run.id, { status: "failed", failureReason: message });
181
- if (nextState === "failed" || nextState === "escalated" || nextState === "awaiting_input" || nextState === "done") {
182
- this.db.issueSessions.clearPendingIssueSessionEventsWithLease(lease);
183
- }
184
- this.db.issues.upsertIssue({
185
- projectId: run.projectId,
186
- linearIssueId: run.linearIssueId,
187
- activeRunId: null,
188
- factoryState: nextState,
189
- });
190
- return true;
191
- });
192
- if (!updated) {
193
- this.logger.warn({ runId: run.id, issueId: run.linearIssueId }, "Skipping failure cleanup after losing issue-session lease");
194
- }
195
- this.releaseLease(run.projectId, run.linearIssueId);
196
- }
197
- emitInterruptedFailure(runType, issue, message) {
198
- const latest = this.db.issues.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
199
- void this.linearSync.emitActivity(latest, buildRunFailureActivity(runType, message));
200
- void this.linearSync.syncSession(latest, { activeRunType: runType });
201
- }
202
- }
@@ -1,13 +0,0 @@
1
- const ZOMBIE_RECOVERY_BASE_DELAY_MS = 15_000;
2
- export function getZombieRecoveryDelayMs(recoveryAttempts) {
3
- return ZOMBIE_RECOVERY_BASE_DELAY_MS * Math.pow(2, recoveryAttempts);
4
- }
5
- export function getRemainingZombieRecoveryDelayMs(lastRecoveryAt, recoveryAttempts, now = Date.now()) {
6
- if (!lastRecoveryAt)
7
- return 0;
8
- const recoveredAtMs = Date.parse(lastRecoveryAt);
9
- if (!Number.isFinite(recoveredAtMs))
10
- return 0;
11
- const delay = getZombieRecoveryDelayMs(recoveryAttempts);
12
- return Math.max(0, recoveredAtMs + delay - now);
13
- }