patchrelay 0.36.8 → 0.36.9

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.
@@ -130,14 +130,14 @@ export class GitHubWebhookHandler {
130
130
  return;
131
131
  }
132
132
  // Route to issue via branch name
133
- const issue = this.db.getIssueByBranch(event.branchName);
133
+ const issue = this.db.issues.getIssueByBranch(event.branchName);
134
134
  if (!issue) {
135
135
  this.logger.debug({ branchName: event.branchName, triggerEvent: event.triggerEvent }, "GitHub webhook: no matching issue for branch");
136
136
  return;
137
137
  }
138
138
  const project = this.config.projects.find((p) => p.id === issue.projectId);
139
139
  // Update PR state on the issue
140
- this.db.upsertIssue({
140
+ this.db.issues.upsertIssue({
141
141
  projectId: issue.projectId,
142
142
  linearIssueId: issue.linearIssueId,
143
143
  ...(event.prNumber !== undefined ? { prNumber: event.prNumber } : {}),
@@ -158,7 +158,7 @@ export class GitHubWebhookHandler {
158
158
  const queueEvictionCheck = this.isQueueEvictionFailure(issue, event, project);
159
159
  if (!isMetadataOnlyCheckEvent(event) || queueEvictionCheck) {
160
160
  // Re-read issue after PR metadata upsert so guards see fresh prReviewState
161
- const afterMetadata = this.db.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
161
+ const afterMetadata = this.db.issues.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
162
162
  const newState = this.resolveFactoryStateForEvent(afterMetadata, event, project);
163
163
  // Only transition and notify when the state actually changes.
164
164
  // Multiple check_suite events can arrive for the same outcome.
@@ -169,13 +169,13 @@ export class GitHubWebhookHandler {
169
169
  factoryState: newState,
170
170
  });
171
171
  this.logger.info({ issueKey: issue.issueKey, from: afterMetadata.factoryState, to: newState, trigger: event.triggerEvent }, "Factory state transition from GitHub event");
172
- const transitionedIssue = this.db.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
172
+ const transitionedIssue = this.db.issues.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
173
173
  void this.emitLinearActivity(transitionedIssue, newState, event);
174
174
  void this.syncLinearSession(transitionedIssue);
175
175
  }
176
176
  }
177
177
  // Re-read issue after all upserts so reactive run logic sees current state
178
- const freshIssue = this.db.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
178
+ const freshIssue = this.db.issues.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
179
179
  // Reset repair counters on new push — but only when no repair run is active,
180
180
  // since Codex pushes during repair and resetting mid-run would bypass budgets.
181
181
  if (event.triggerEvent === "pr_synchronize" && !freshIssue.activeRunId) {
@@ -240,7 +240,7 @@ export class GitHubWebhookHandler {
240
240
  }
241
241
  async updateCiSnapshot(issue, event, project) {
242
242
  if (event.triggerEvent === "pr_merged") {
243
- this.db.upsertIssue({
243
+ this.db.issues.upsertIssue({
244
244
  projectId: issue.projectId,
245
245
  linearIssueId: issue.linearIssueId,
246
246
  lastGitHubCiSnapshotHeadSha: null,
@@ -252,7 +252,7 @@ export class GitHubWebhookHandler {
252
252
  return;
253
253
  }
254
254
  if (event.triggerEvent === "pr_synchronize") {
255
- this.db.upsertIssue({
255
+ this.db.issues.upsertIssue({
256
256
  projectId: issue.projectId,
257
257
  linearIssueId: issue.linearIssueId,
258
258
  lastGitHubCiSnapshotHeadSha: event.headSha ?? null,
@@ -279,7 +279,7 @@ export class GitHubWebhookHandler {
279
279
  gateCheckNames: this.getGateCheckNames(project),
280
280
  });
281
281
  if (!snapshot) {
282
- this.db.upsertIssue({
282
+ this.db.issues.upsertIssue({
283
283
  projectId: issue.projectId,
284
284
  linearIssueId: issue.linearIssueId,
285
285
  lastGitHubCiSnapshotHeadSha: event.headSha ?? issue.lastGitHubCiSnapshotHeadSha ?? null,
@@ -300,7 +300,7 @@ export class GitHubWebhookHandler {
300
300
  });
301
301
  return;
302
302
  }
303
- this.db.upsertIssue({
303
+ this.db.issues.upsertIssue({
304
304
  projectId: issue.projectId,
305
305
  linearIssueId: issue.linearIssueId,
306
306
  prCheckStatus: snapshot.gateCheckStatus,
@@ -341,7 +341,7 @@ export class GitHubWebhookHandler {
341
341
  return;
342
342
  }
343
343
  const hadPendingWake = this.db.issueSessions.hasPendingIssueSessionEvents(issue.projectId, issue.linearIssueId);
344
- this.db.upsertIssue({
344
+ this.db.issues.upsertIssue({
345
345
  projectId: issue.projectId,
346
346
  linearIssueId: issue.linearIssueId,
347
347
  lastGitHubFailureSource: "queue_eviction",
@@ -399,7 +399,7 @@ export class GitHubWebhookHandler {
399
399
  }
400
400
  const hadPendingWake = this.db.issueSessions.hasPendingIssueSessionEvents(issue.projectId, issue.linearIssueId);
401
401
  const snapshot = this.getRelevantCiSnapshot(issue, event);
402
- this.db.upsertIssue({
402
+ this.db.issues.upsertIssue({
403
403
  projectId: issue.projectId,
404
404
  linearIssueId: issue.linearIssueId,
405
405
  lastGitHubFailureSource: "branch_ci",
@@ -520,7 +520,7 @@ export class GitHubWebhookHandler {
520
520
  : "Pull request closed during active run",
521
521
  });
522
522
  }
523
- this.db.upsertIssue({
523
+ this.db.issues.upsertIssue({
524
524
  projectId: issue.projectId,
525
525
  linearIssueId: issue.linearIssueId,
526
526
  activeRunId: null,
@@ -535,7 +535,7 @@ export class GitHubWebhookHandler {
535
535
  this.db.transaction(commitTerminalUpdate);
536
536
  }
537
537
  this.db.issueSessions.releaseIssueSessionLeaseRespectingActiveLease(issue.projectId, issue.linearIssueId);
538
- const updatedIssue = this.db.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
538
+ const updatedIssue = this.db.issues.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
539
539
  if (event.triggerEvent === "pr_merged") {
540
540
  await this.completeLinearIssueAfterMerge(updatedIssue);
541
541
  }
@@ -554,7 +554,7 @@ export class GitHubWebhookHandler {
554
554
  }
555
555
  const normalizedCurrent = liveIssue.stateName?.trim().toLowerCase();
556
556
  if (normalizedCurrent === targetState.trim().toLowerCase()) {
557
- this.db.upsertIssue({
557
+ this.db.issues.upsertIssue({
558
558
  projectId: issue.projectId,
559
559
  linearIssueId: issue.linearIssueId,
560
560
  ...(liveIssue.stateName ? { currentLinearState: liveIssue.stateName } : {}),
@@ -563,7 +563,7 @@ export class GitHubWebhookHandler {
563
563
  return;
564
564
  }
565
565
  const updated = await linear.setIssueState(issue.linearIssueId, targetState);
566
- this.db.upsertIssue({
566
+ this.db.issues.upsertIssue({
567
567
  projectId: issue.projectId,
568
568
  linearIssueId: issue.linearIssueId,
569
569
  ...(updated.stateName ? { currentLinearState: updated.stateName } : {}),
@@ -587,7 +587,7 @@ export class GitHubWebhookHandler {
587
587
  const failureContext = source === "queue_eviction"
588
588
  ? this.buildQueueFailureContext(issue, event)
589
589
  : await this.resolveBranchFailureContext(issue, event, project);
590
- this.db.upsertIssue({
590
+ this.db.issues.upsertIssue({
591
591
  projectId: issue.projectId,
592
592
  linearIssueId: issue.linearIssueId,
593
593
  lastGitHubFailureSource: source,
@@ -614,7 +614,7 @@ export class GitHubWebhookHandler {
614
614
  if (event.triggerEvent === "check_passed" && !this.canClearFailureProvenance(issue, event, project)) {
615
615
  return;
616
616
  }
617
- this.db.upsertIssue({
617
+ this.db.issues.upsertIssue({
618
618
  projectId: issue.projectId,
619
619
  linearIssueId: issue.linearIssueId,
620
620
  lastGitHubFailureSource: null,
@@ -763,7 +763,7 @@ export class GitHubWebhookHandler {
763
763
  return !issue.lastGitHubFailureHeadSha || issue.lastGitHubFailureHeadSha === event.headSha;
764
764
  }
765
765
  getRelevantCiSnapshot(issue, event) {
766
- const snapshot = this.db.getLatestGitHubCiSnapshot(issue.projectId, issue.linearIssueId);
766
+ const snapshot = this.db.issues.getLatestGitHubCiSnapshot(issue.projectId, issue.linearIssueId);
767
767
  if (!snapshot)
768
768
  return undefined;
769
769
  if (snapshot.headSha !== event.headSha)
@@ -904,7 +904,7 @@ export class GitHubWebhookHandler {
904
904
  const prNumber = typeof issuePayload.number === "number" ? issuePayload.number : undefined;
905
905
  if (!prNumber)
906
906
  return;
907
- const issue = this.db.getIssueByPrNumber(prNumber);
907
+ const issue = this.db.issues.getIssueByPrNumber(prNumber);
908
908
  if (!issue)
909
909
  return;
910
910
  if (!this.isPatchRelayOwnedPr(issue))
@@ -110,7 +110,7 @@ export class IdleIssueReconciler {
110
110
  this.feed = feed;
111
111
  }
112
112
  async reconcile() {
113
- for (const issue of this.db.listIdleNonTerminalIssues()) {
113
+ for (const issue of this.db.issues.listIdleNonTerminalIssues()) {
114
114
  if (issue.prState === "merged") {
115
115
  this.advanceIdleIssue(issue, "done", { clearFailureProvenance: true });
116
116
  continue;
@@ -145,13 +145,13 @@ export class IdleIssueReconciler {
145
145
  await this.reconcileFromGitHub(issue);
146
146
  }
147
147
  }
148
- for (const issue of this.db.listIssues()) {
148
+ for (const issue of this.db.issues.listIssues()) {
149
149
  if (!this.shouldProbeTerminalIssueFromGitHub(issue))
150
150
  continue;
151
151
  await this.reconcileFromGitHub(issue);
152
152
  }
153
- for (const issue of this.db.listBlockedDelegatedIssues()) {
154
- const unresolved = this.db.countUnresolvedBlockers(issue.projectId, issue.linearIssueId);
153
+ for (const issue of this.db.issues.listBlockedDelegatedIssues()) {
154
+ const unresolved = this.db.issues.countUnresolvedBlockers(issue.projectId, issue.linearIssueId);
155
155
  if (unresolved === 0) {
156
156
  this.db.issueSessions.appendIssueSessionEventRespectingActiveLease(issue.projectId, issue.linearIssueId, {
157
157
  projectId: issue.projectId,
@@ -179,7 +179,7 @@ export class IdleIssueReconciler {
179
179
  return;
180
180
  }
181
181
  this.logger.info({ issueKey: issue.issueKey, from: issue.factoryState, to: newState, pendingRunType: options?.pendingRunType }, "Reconciliation: advancing idle issue");
182
- this.db.upsertIssue({
182
+ this.db.issues.upsertIssue({
183
183
  projectId: issue.projectId,
184
184
  linearIssueId: issue.linearIssueId,
185
185
  factoryState: newState,
@@ -206,7 +206,7 @@ export class IdleIssueReconciler {
206
206
  });
207
207
  const branchOwner = resolveBranchOwnerForStateTransition(newState, options?.pendingRunType);
208
208
  if (branchOwner) {
209
- this.db.setBranchOwner(issue.projectId, issue.linearIssueId, branchOwner);
209
+ this.db.issues.setBranchOwner(issue.projectId, issue.linearIssueId, branchOwner);
210
210
  }
211
211
  if (options?.pendingRunType) {
212
212
  this.appendWakeEvent(issue, options.pendingRunType, options.pendingRunContext, "idle_reconciliation");
@@ -309,7 +309,7 @@ export class IdleIssueReconciler {
309
309
  ?? (inferred === "queue_eviction" && failureHeadSha && checkName
310
310
  ? ["queue_eviction", failureHeadSha, checkName].join("::")
311
311
  : null);
312
- this.db.upsertIssue({
312
+ this.db.issues.upsertIssue({
313
313
  projectId: issue.projectId,
314
314
  linearIssueId: issue.linearIssueId,
315
315
  lastGitHubFailureSource: inferred,
@@ -317,7 +317,7 @@ export class IdleIssueReconciler {
317
317
  ...(checkName ? { lastGitHubFailureCheckName: checkName } : {}),
318
318
  ...(failureSignature ? { lastGitHubFailureSignature: failureSignature } : {}),
319
319
  });
320
- const refreshed = this.db.getIssue(issue.projectId, issue.linearIssueId);
320
+ const refreshed = this.db.issues.getIssue(issue.projectId, issue.linearIssueId);
321
321
  if (!refreshed)
322
322
  return issue;
323
323
  this.logger.info({ issueKey: issue.issueKey, prNumber: issue.prNumber, inferred, factoryState: issue.factoryState }, "Recovered missing failure provenance from GitHub state");
@@ -337,7 +337,7 @@ export class IdleIssueReconciler {
337
337
  const checkName = issue.lastGitHubFailureCheckName ?? protocol.evictionCheckName;
338
338
  const failureSignature = issue.lastGitHubFailureSignature
339
339
  ?? (failureHeadSha && checkName ? ["queue_eviction", failureHeadSha, checkName].join("::") : null);
340
- this.db.upsertIssue({
340
+ this.db.issues.upsertIssue({
341
341
  projectId: issue.projectId,
342
342
  linearIssueId: issue.linearIssueId,
343
343
  lastGitHubFailureSource: "queue_eviction",
@@ -345,7 +345,7 @@ export class IdleIssueReconciler {
345
345
  ...(checkName ? { lastGitHubFailureCheckName: checkName } : {}),
346
346
  ...(failureSignature ? { lastGitHubFailureSignature: failureSignature } : {}),
347
347
  });
348
- const refreshed = this.db.getIssue(issue.projectId, issue.linearIssueId);
348
+ const refreshed = this.db.issues.getIssue(issue.projectId, issue.linearIssueId);
349
349
  if (!refreshed)
350
350
  return issue;
351
351
  this.logger.info({ issueKey: issue.issueKey, prNumber: issue.prNumber }, "Reclassified stale branch failure as queue repair from GitHub state");
@@ -410,7 +410,7 @@ export class IdleIssueReconciler {
410
410
  const previousHeadSha = issue.prHeadSha;
411
411
  const gateCheckNames = getGateCheckNames(project);
412
412
  const gateCheckStatus = deriveGateCheckStatusFromRollup(pr.statusCheckRollup, gateCheckNames);
413
- this.db.upsertIssue({
413
+ this.db.issues.upsertIssue({
414
414
  projectId: issue.projectId,
415
415
  linearIssueId: issue.linearIssueId,
416
416
  ...(pr.headRefOid ? { prHeadSha: pr.headRefOid } : {}),
@@ -433,13 +433,13 @@ export class IdleIssueReconciler {
433
433
  : {}),
434
434
  });
435
435
  if (pr.state === "MERGED") {
436
- this.db.upsertIssue({ projectId: issue.projectId, linearIssueId: issue.linearIssueId, prState: "merged" });
436
+ this.db.issues.upsertIssue({ projectId: issue.projectId, linearIssueId: issue.linearIssueId, prState: "merged" });
437
437
  this.advanceIdleIssue(issue, "done", { clearFailureProvenance: true });
438
438
  return;
439
439
  }
440
440
  if (pr.state === "CLOSED") {
441
441
  this.logger.info({ issueKey: issue.issueKey, prNumber: issue.prNumber }, "Reconciliation: PR was closed, re-delegating for implementation");
442
- this.db.upsertIssue({ projectId: issue.projectId, linearIssueId: issue.linearIssueId, prState: "closed" });
442
+ this.db.issues.upsertIssue({ projectId: issue.projectId, linearIssueId: issue.linearIssueId, prState: "closed" });
443
443
  this.advanceIdleIssue(issue, "delegated", {
444
444
  pendingRunType: "implementation",
445
445
  clearFailureProvenance: true,
@@ -481,7 +481,7 @@ export class IdleIssueReconciler {
481
481
  }
482
482
  const downstreamOwned = issue.factoryState === "awaiting_queue" || issue.prReviewState === "approved" || pr.reviewDecision === "APPROVED";
483
483
  const mergeConflictDetected = pr.mergeable === "CONFLICTING" || pr.mergeStateStatus === "DIRTY";
484
- const refreshedIssue = this.db.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
484
+ const refreshedIssue = this.db.issues.getIssue(issue.projectId, issue.linearIssueId) ?? issue;
485
485
  const reactiveIntent = deriveIssueSessionReactiveIntent({
486
486
  prNumber: refreshedIssue.prNumber,
487
487
  prState: refreshedIssue.prState,
@@ -549,7 +549,7 @@ export class IdleIssueReconciler {
549
549
  return;
550
550
  }
551
551
  if (isReviewDecisionApproved(pr.reviewDecision)) {
552
- this.db.upsertIssue({
552
+ this.db.issues.upsertIssue({
553
553
  projectId: issue.projectId,
554
554
  linearIssueId: issue.linearIssueId,
555
555
  prReviewState: "approved",
@@ -0,0 +1,176 @@
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
+ function isRequestedChangesRunType(runType) {
5
+ return runType === "review_fix" || runType === "branch_upkeep";
6
+ }
7
+ function resolveRetryRunType(runType, context) {
8
+ if (runType === "branch_upkeep") {
9
+ return "branch_upkeep";
10
+ }
11
+ return context?.reviewFixMode === "branch_upkeep" || context?.branchUpkeepRequired === true
12
+ ? "branch_upkeep"
13
+ : "review_fix";
14
+ }
15
+ function resolvePostRunState(issue) {
16
+ if (ACTIVE_RUN_STATES.has(issue.factoryState) && issue.prNumber) {
17
+ if (issue.prState === "merged")
18
+ return "done";
19
+ if (issue.prReviewState === "approved")
20
+ return "awaiting_queue";
21
+ return "pr_open";
22
+ }
23
+ return undefined;
24
+ }
25
+ export function resolveRecoverablePostRunState(issue) {
26
+ if (!issue.prNumber) {
27
+ return resolvePostRunState(issue);
28
+ }
29
+ if (issue.prState === "merged")
30
+ return "done";
31
+ if (issue.prState === "open") {
32
+ const reactiveIntent = deriveIssueSessionReactiveIntent({
33
+ prNumber: issue.prNumber,
34
+ prState: issue.prState,
35
+ prReviewState: issue.prReviewState,
36
+ prCheckStatus: issue.prCheckStatus,
37
+ latestFailureSource: issue.lastGitHubFailureSource,
38
+ });
39
+ if (reactiveIntent)
40
+ return reactiveIntent.compatibilityFactoryState;
41
+ if (issue.prReviewState === "approved")
42
+ return "awaiting_queue";
43
+ return "pr_open";
44
+ }
45
+ return resolvePostRunState(issue);
46
+ }
47
+ export class InterruptedRunRecovery {
48
+ db;
49
+ logger;
50
+ linearSync;
51
+ withHeldLease;
52
+ releaseLease;
53
+ failRunAndClear;
54
+ restoreIdleWorktree;
55
+ completionPolicy;
56
+ enqueueIssue;
57
+ feed;
58
+ constructor(db, logger, linearSync, withHeldLease, releaseLease, failRunAndClear, restoreIdleWorktree, completionPolicy, enqueueIssue, feed) {
59
+ this.db = db;
60
+ this.logger = logger;
61
+ this.linearSync = linearSync;
62
+ this.withHeldLease = withHeldLease;
63
+ this.releaseLease = releaseLease;
64
+ this.failRunAndClear = failRunAndClear;
65
+ this.restoreIdleWorktree = restoreIdleWorktree;
66
+ this.completionPolicy = completionPolicy;
67
+ this.enqueueIssue = enqueueIssue;
68
+ this.feed = feed;
69
+ }
70
+ async handle(run, issue) {
71
+ this.logger.warn({ issueKey: issue.issueKey, runType: run.runType, threadId: run.threadId }, "Run has interrupted turn - marking as failed");
72
+ const repairedCounters = this.withHeldLease(issue.projectId, issue.linearIssueId, (lease) => {
73
+ if (run.runType === "ci_repair" && issue.ciRepairAttempts > 0) {
74
+ this.db.issueSessions.upsertIssueWithLease(lease, {
75
+ projectId: issue.projectId,
76
+ linearIssueId: issue.linearIssueId,
77
+ ciRepairAttempts: issue.ciRepairAttempts - 1,
78
+ });
79
+ }
80
+ else if (run.runType === "queue_repair" && issue.queueRepairAttempts > 0) {
81
+ this.db.issueSessions.upsertIssueWithLease(lease, {
82
+ projectId: issue.projectId,
83
+ linearIssueId: issue.linearIssueId,
84
+ queueRepairAttempts: issue.queueRepairAttempts - 1,
85
+ });
86
+ }
87
+ if (run.runType === "ci_repair" || run.runType === "queue_repair") {
88
+ this.db.issueSessions.upsertIssueWithLease(lease, {
89
+ projectId: issue.projectId,
90
+ linearIssueId: issue.linearIssueId,
91
+ lastAttemptedFailureHeadSha: null,
92
+ lastAttemptedFailureSignature: null,
93
+ });
94
+ }
95
+ return true;
96
+ });
97
+ if (!repairedCounters) {
98
+ this.logger.warn({ runId: run.id, issueId: run.linearIssueId }, "Skipping interrupted-run recovery after losing issue-session lease");
99
+ this.releaseLease(run.projectId, run.linearIssueId);
100
+ return;
101
+ }
102
+ if (isRequestedChangesRunType(run.runType)) {
103
+ await this.handleInterruptedRequestedChangesRun(run, issue);
104
+ return;
105
+ }
106
+ const recoveredState = resolveRecoverablePostRunState(this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? issue);
107
+ this.failRunAndClear(run, "Codex turn was interrupted", recoveredState);
108
+ await this.restoreIdleWorktree(issue);
109
+ const failedIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? issue;
110
+ if (recoveredState) {
111
+ this.feed?.publish({
112
+ level: "info",
113
+ kind: "stage",
114
+ issueKey: issue.issueKey,
115
+ projectId: run.projectId,
116
+ stage: recoveredState,
117
+ status: "reconciled",
118
+ summary: `Interrupted ${run.runType} recovered -> ${recoveredState}`,
119
+ });
120
+ }
121
+ else {
122
+ void this.linearSync.emitActivity(failedIssue, buildRunFailureActivity(run.runType, "The Codex turn was interrupted."));
123
+ }
124
+ void this.linearSync.syncSession(failedIssue, { activeRunType: run.runType });
125
+ this.releaseLease(run.projectId, run.linearIssueId);
126
+ }
127
+ async handleInterruptedRequestedChangesRun(run, issue) {
128
+ const freshIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? issue;
129
+ const refreshedIssue = await this.completionPolicy.refreshIssueAfterReactivePublish(run, freshIssue);
130
+ const retryContext = await this.completionPolicy.resolveRequestedChangesWakeContext(refreshedIssue, run.runType, run.runType === "branch_upkeep"
131
+ ? {
132
+ branchUpkeepRequired: true,
133
+ reviewFixMode: "branch_upkeep",
134
+ wakeReason: "branch_upkeep",
135
+ }
136
+ : undefined);
137
+ const retryRunType = resolveRetryRunType(run.runType, retryContext);
138
+ const recoveredState = resolveRecoverablePostRunState(refreshedIssue) ?? "failed";
139
+ const interruptedMessage = "Requested-changes run was interrupted before PatchRelay could verify that a new PR head was published";
140
+ this.failRunAndClear(run, interruptedMessage, recoveredState);
141
+ await this.restoreIdleWorktree(issue);
142
+ const recoveredIssue = this.db.issues.getIssue(run.projectId, run.linearIssueId) ?? refreshedIssue;
143
+ if (recoveredState === "changes_requested") {
144
+ this.db.issues.upsertIssue({
145
+ projectId: run.projectId,
146
+ linearIssueId: run.linearIssueId,
147
+ pendingRunType: retryRunType,
148
+ pendingRunContextJson: retryContext ? JSON.stringify(retryContext) : null,
149
+ });
150
+ this.feed?.publish({
151
+ level: "warn",
152
+ kind: "workflow",
153
+ issueKey: issue.issueKey,
154
+ projectId: run.projectId,
155
+ stage: run.runType,
156
+ status: "retry_queued",
157
+ summary: "Requested-changes run was interrupted; PatchRelay will retry from fresh GitHub truth",
158
+ });
159
+ this.enqueueIssue(run.projectId, run.linearIssueId);
160
+ }
161
+ else {
162
+ this.feed?.publish({
163
+ level: "error",
164
+ kind: "workflow",
165
+ issueKey: issue.issueKey,
166
+ projectId: run.projectId,
167
+ stage: run.runType,
168
+ status: "escalated",
169
+ summary: interruptedMessage,
170
+ });
171
+ }
172
+ void this.linearSync.emitActivity(recoveredIssue, buildRunFailureActivity(run.runType, interruptedMessage));
173
+ void this.linearSync.syncSession(recoveredIssue, { activeRunType: run.runType });
174
+ this.releaseLease(run.projectId, run.linearIssueId);
175
+ }
176
+ }
@@ -71,7 +71,7 @@ export class IssueQueryService {
71
71
  const legacy = this.db.getIssueOverview(issueKey);
72
72
  if (!legacy)
73
73
  return undefined;
74
- const issueRecord = this.db.getIssueByKey(issueKey);
74
+ const issueRecord = this.db.issues.getIssueByKey(issueKey);
75
75
  const activeStatus = await this.runStatusProvider.getActiveRunStatus(issueKey);
76
76
  const activeRun = activeStatus?.run ?? legacy.activeRun;
77
77
  const latestRun = this.db.runs.getLatestRunForIssue(legacy.issue.projectId, legacy.issue.linearIssueId);
@@ -123,8 +123,8 @@ export class IssueQueryService {
123
123
  : {}),
124
124
  };
125
125
  }
126
- const issueRecord = this.db.getIssueByKey(issueKey);
127
- const blockedBy = this.db.listIssueDependencies(session.projectId, session.linearIssueId);
126
+ const issueRecord = this.db.issues.getIssueByKey(issueKey);
127
+ const blockedBy = this.db.issues.listIssueDependencies(session.projectId, session.linearIssueId);
128
128
  const unresolvedBlockedBy = blockedBy.filter((entry) => (entry.blockerCurrentLinearStateType !== "completed"
129
129
  && entry.blockerCurrentLinearState?.trim().toLowerCase() !== "done"));
130
130
  const blockedByKeys = unresolvedBlockedBy.map((entry) => entry.blockerIssueKey ?? entry.blockerLinearIssueId);
@@ -231,7 +231,7 @@ export class IssueQueryService {
231
231
  const overview = await this.getIssueOverview(issueKey);
232
232
  if (!overview)
233
233
  return undefined;
234
- const issueRecord = this.db.getIssueByKey(issueKey);
234
+ const issueRecord = this.db.issues.getIssueByKey(issueKey);
235
235
  const latestRunReport = parseStageReport(overview.latestRun?.reportJson, overview.latestRun?.status ?? "unknown");
236
236
  const runs = (overview.runs ?? this.buildRuns(overview.issue.projectId, overview.issue.linearIssueId)).map((run) => ({
237
237
  run: {
@@ -0,0 +1,114 @@
1
+ import { isoNow } from "./db/shared.js";
2
+ import { buildTrackedIssueRecord } from "./tracked-issue-projector.js";
3
+ import { extractLatestAssistantSummary, } from "./issue-session-events.js";
4
+ import { deriveIssueSessionState, deriveIssueSessionWakeReason, } from "./issue-session.js";
5
+ export function syncIssueSessionFromIssue(params) {
6
+ const { connection, issues, issueSessions, runs, issue, options } = params;
7
+ const existing = issueSessions.getIssueSession(issue.projectId, issue.linearIssueId);
8
+ const latestRun = runs.getLatestRunForIssue(issue.projectId, issue.linearIssueId);
9
+ const latestRunType = options?.lastRunType ?? latestRun?.runType ?? existing?.lastRunType;
10
+ const summaryText = resolveIssueSessionSummary(issue, runs, latestRun, existing?.summaryText, options?.summaryText);
11
+ const activeThreadId = issue.threadId ?? existing?.activeThreadId;
12
+ const threadGeneration = activeThreadId && activeThreadId !== existing?.activeThreadId
13
+ ? (existing?.threadGeneration ?? 0) + 1
14
+ : (existing?.threadGeneration ?? (activeThreadId ? 1 : 0));
15
+ const sessionState = deriveIssueSessionState({
16
+ ...(issue.activeRunId !== undefined ? { activeRunId: issue.activeRunId } : {}),
17
+ factoryState: issue.factoryState,
18
+ });
19
+ const tracked = buildTrackedIssueRecord({
20
+ issue,
21
+ session: existing,
22
+ blockedBy: issues.listIssueDependencies(issue.projectId, issue.linearIssueId),
23
+ hasPendingWake: issueSessions.peekIssueSessionWake(issue.projectId, issue.linearIssueId) !== undefined,
24
+ latestRun,
25
+ latestEvent: issueSessions.listIssueSessionEvents(issue.projectId, issue.linearIssueId, { limit: 1 }).at(-1),
26
+ });
27
+ const lastWakeReason = options?.lastWakeReason
28
+ ?? deriveIssueSessionWakeReason({
29
+ pendingRunType: issue.pendingRunType,
30
+ factoryState: issue.factoryState,
31
+ prNumber: issue.prNumber,
32
+ prState: issue.prState,
33
+ prReviewState: issue.prReviewState,
34
+ prCheckStatus: issue.prCheckStatus,
35
+ latestFailureSource: issue.lastGitHubFailureSource,
36
+ })
37
+ ?? existing?.lastWakeReason;
38
+ const now = isoNow();
39
+ if (existing) {
40
+ connection.prepare(`
41
+ UPDATE issue_sessions SET
42
+ issue_key = ?,
43
+ repo_id = ?,
44
+ branch_name = ?,
45
+ worktree_path = ?,
46
+ pr_number = ?,
47
+ pr_head_sha = ?,
48
+ pr_author_login = ?,
49
+ session_state = ?,
50
+ waiting_reason = ?,
51
+ summary_text = ?,
52
+ active_thread_id = ?,
53
+ thread_generation = ?,
54
+ active_run_id = ?,
55
+ last_run_type = ?,
56
+ last_wake_reason = ?,
57
+ ci_repair_attempts = ?,
58
+ queue_repair_attempts = ?,
59
+ review_fix_attempts = ?,
60
+ updated_at = ?
61
+ WHERE project_id = ? AND linear_issue_id = ?
62
+ `).run(issue.issueKey ?? null, issue.projectId, issue.branchName ?? null, issue.worktreePath ?? null, issue.prNumber ?? null, issue.prHeadSha ?? null, issue.prAuthorLogin ?? null, sessionState, tracked.waitingReason ?? null, summaryText ?? null, activeThreadId ?? null, threadGeneration, issue.activeRunId ?? null, latestRunType ?? null, lastWakeReason ?? null, issue.ciRepairAttempts, issue.queueRepairAttempts, issue.reviewFixAttempts, now, issue.projectId, issue.linearIssueId);
63
+ return;
64
+ }
65
+ connection.prepare(`
66
+ INSERT INTO issue_sessions (
67
+ project_id, linear_issue_id, issue_key, repo_id, branch_name, worktree_path,
68
+ pr_number, pr_head_sha, pr_author_login, session_state, waiting_reason, summary_text,
69
+ active_thread_id, thread_generation, active_run_id, last_run_type, last_wake_reason,
70
+ ci_repair_attempts, queue_repair_attempts, review_fix_attempts,
71
+ created_at, updated_at
72
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
73
+ `).run(issue.projectId, issue.linearIssueId, issue.issueKey ?? null, issue.projectId, issue.branchName ?? null, issue.worktreePath ?? null, issue.prNumber ?? null, issue.prHeadSha ?? null, issue.prAuthorLogin ?? null, sessionState, tracked.waitingReason ?? null, summaryText ?? null, activeThreadId ?? null, threadGeneration, issue.activeRunId ?? null, latestRunType ?? null, lastWakeReason ?? null, issue.ciRepairAttempts, issue.queueRepairAttempts, issue.reviewFixAttempts, now, now);
74
+ }
75
+ function resolveIssueSessionSummary(issue, runs, latestRun, existingSummaryText, explicitSummaryText) {
76
+ if (explicitSummaryText?.trim()) {
77
+ return explicitSummaryText;
78
+ }
79
+ const latestSummary = extractLatestAssistantSummary(latestRun);
80
+ if (latestRun && (latestRun.status === "queued" || latestRun.status === "running")) {
81
+ return latestSummary;
82
+ }
83
+ if (shouldKeepPreviousIssueSummary(issue, latestRun)) {
84
+ return findLatestCompletedRunSummary(runs, issue.projectId, issue.linearIssueId)
85
+ ?? existingSummaryText
86
+ ?? latestSummary;
87
+ }
88
+ return latestSummary ?? existingSummaryText;
89
+ }
90
+ function shouldKeepPreviousIssueSummary(issue, latestRun) {
91
+ if (!latestRun || latestRun.status !== "failed") {
92
+ return false;
93
+ }
94
+ if (latestRun.summaryJson || latestRun.reportJson) {
95
+ return false;
96
+ }
97
+ return issue.factoryState === "pr_open"
98
+ || issue.factoryState === "awaiting_queue"
99
+ || issue.factoryState === "done";
100
+ }
101
+ function findLatestCompletedRunSummary(runs, projectId, linearIssueId) {
102
+ const issueRuns = runs.listRunsForIssue(projectId, linearIssueId);
103
+ for (let index = issueRuns.length - 1; index >= 0; index -= 1) {
104
+ const run = issueRuns[index];
105
+ if (!run || run.status !== "completed") {
106
+ continue;
107
+ }
108
+ const summary = extractLatestAssistantSummary(run);
109
+ if (summary?.trim()) {
110
+ return summary;
111
+ }
112
+ }
113
+ return undefined;
114
+ }
@@ -26,7 +26,7 @@ export class LinearSessionSync {
26
26
  if (!recoveredAgentSessionId)
27
27
  return issue;
28
28
  this.logger.info({ issueKey: issue.issueKey, agentSessionId: recoveredAgentSessionId }, "Recovered missing Linear agent session id from webhook history");
29
- return this.db.upsertIssue({
29
+ return this.db.issues.upsertIssue({
30
30
  projectId: issue.projectId,
31
31
  linearIssueId: issue.linearIssueId,
32
32
  agentSessionId: recoveredAgentSessionId,
@@ -99,7 +99,7 @@ export class LinearSessionSync {
99
99
  currentLinearState: liveIssue.stateName,
100
100
  currentLinearStateType: liveIssue.stateType,
101
101
  })) {
102
- this.db.upsertIssue({
102
+ this.db.issues.upsertIssue({
103
103
  projectId: issue.projectId,
104
104
  linearIssueId: issue.linearIssueId,
105
105
  ...(liveIssue.stateName ? { currentLinearState: liveIssue.stateName } : {}),
@@ -112,7 +112,7 @@ export class LinearSessionSync {
112
112
  return;
113
113
  const normalizedCurrent = liveIssue.stateName?.trim().toLowerCase();
114
114
  if (normalizedCurrent === targetState.trim().toLowerCase()) {
115
- this.db.upsertIssue({
115
+ this.db.issues.upsertIssue({
116
116
  projectId: issue.projectId,
117
117
  linearIssueId: issue.linearIssueId,
118
118
  ...(liveIssue.stateName ? { currentLinearState: liveIssue.stateName } : {}),
@@ -121,7 +121,7 @@ export class LinearSessionSync {
121
121
  return;
122
122
  }
123
123
  const updated = await linear.setIssueState(issue.linearIssueId, targetState);
124
- this.db.upsertIssue({
124
+ this.db.issues.upsertIssue({
125
125
  projectId: issue.projectId,
126
126
  linearIssueId: issue.linearIssueId,
127
127
  ...(updated.stateName ? { currentLinearState: updated.stateName } : {}),
@@ -174,7 +174,7 @@ export class LinearSessionSync {
174
174
  if (now - lastEmit < PROGRESS_THROTTLE_MS)
175
175
  return;
176
176
  this.progressThrottle.set(run.id, now);
177
- const issue = this.db.getIssue(run.projectId, run.linearIssueId);
177
+ const issue = this.db.issues.getIssue(run.projectId, run.linearIssueId);
178
178
  if (issue) {
179
179
  void this.emitActivity(issue, activity, { ephemeral: true });
180
180
  }
@@ -192,7 +192,7 @@ export class LinearSessionSync {
192
192
  body,
193
193
  });
194
194
  if (result.id !== issue.statusCommentId) {
195
- this.db.upsertIssue({
195
+ this.db.issues.upsertIssue({
196
196
  projectId: issue.projectId,
197
197
  linearIssueId: issue.linearIssueId,
198
198
  statusCommentId: result.id,