-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaskExecutor.ts
More file actions
611 lines (595 loc) Β· 20.4 KB
/
Copy pathaskExecutor.ts
File metadata and controls
611 lines (595 loc) Β· 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import type { Pool } from "pg";
import type { JobWithMetadata, PgBoss } from "pg-boss";
import type { Config } from "../../config.js";
import type { PrSurface } from "../../github/prSurface.js";
import { captureEvent } from "../../analytics/index.js";
import { runAskRun } from "../../agent/ask/askRun.js";
import { loadAskThreadTranscript } from "../../agent/ask/askThreadContext.js";
import { formatAskReply, sanitizeAskAnswerText } from "../../agent/ask/formatAskReply.js";
import {
askReplyBodyWithOperationMarker,
askReplyCommentIdFromIntentDetail,
findExistingAskReplyComment,
} from "../../agent/ask/recoverAskReply.js";
import {
classifyFailure,
classifiedFailureLogFields,
classifiedFailurePostHogProperties,
} from "../../errors/classifiedFailure.js";
import { getAppBotIdentity } from "../../github/appAuth.js";
import { isKnownNoAcceptanceMutationError } from "../../github/mutationErrorContract.js";
import { logWarn } from "../../evlog.js";
import { ASK_PUBLISH_LENS } from "../../settings/index.js";
import { withPrRepositoryView } from "../../prWorkspace/index.js";
import { resolveWorkItemHead, runDurableWorkItem } from "../durableJob.js";
import {
getOperationIntent,
mergeOperationIntentDetail,
persistOperationIntent,
reconcileOperationIntent,
} from "../operationIntentRepository.js";
import { hasCompletedPublishStep, recordAskPublishStep } from "../repository.js";
import {
askFailureReplyOperationKey,
askReplyOperationKey,
withOperationIntent,
} from "../withOperationIntent.js";
import { createAskExecutionId, recordAskProviderUsage } from "../askQuota.js";
import type { AskJobData, AskWorkItem } from "../types.js";
import { waitForReadySnapshot } from "../../codeIndex/repository.js";
import { buildRepositoryViewParams } from "./repositoryViewParams.js";
function replyTargetKindFromIntentDetail(
value: unknown,
fallback: AskWorkItem["payload"]["replyTarget"]["kind"],
): AskWorkItem["payload"]["replyTarget"]["kind"] {
return value === "inlineReviewThread" || value === "prConversation" ? value : fallback;
}
function askReplyLookupKeys(resourceKey: string, operationKey: string): readonly string[] {
const scopedKey = askReplyOperationKey(resourceKey);
if (operationKey === scopedKey) return [operationKey];
// Failure-reply keys must not adopt a legacy ask:reply comment as already published.
if (operationKey.startsWith(`ask:reply:${resourceKey}`)) {
return [operationKey, scopedKey];
}
return [operationKey];
}
async function findAskReplyOnAnyTarget(params: {
readonly prSurface: PrSurface;
readonly item: AskWorkItem;
readonly botLogin: string;
readonly operationKey: string;
readonly operationInstance: string;
}) {
const { prSurface, item, botLogin, operationKey, operationInstance } = params;
for (const key of askReplyLookupKeys(item.resourceKey, operationKey)) {
const primary = await findExistingAskReplyComment({
prSurface,
replyTarget: item.payload.replyTarget,
question: item.payload.question,
botLogin,
operationKey: key,
operationInstance,
});
if (primary != null) return primary;
if (item.payload.replyTarget.kind === "prConversation") continue;
const conversation = await findExistingAskReplyComment({
prSurface,
replyTarget: { kind: "prConversation", prNumber: item.prNumber },
question: item.payload.question,
botLogin,
operationKey: key,
operationInstance,
});
if (conversation != null) return conversation;
}
return null;
}
async function publishAskAnswer(
cfg: Config,
prSurface: PrSurface,
item: AskWorkItem,
answer: string,
operationKey: string,
alreadySanitized = false,
): Promise<{ commentId: number; targetKind: AskWorkItem["payload"]["replyTarget"]["kind"] }> {
const body = alreadySanitized ? answer : sanitizeAskAnswerText(answer);
const replyTarget = item.payload.replyTarget;
const markedBody = askReplyBodyWithOperationMarker(body, operationKey, item.id);
try {
const posted = await prSurface.replyAt(replyTarget, markedBody);
return { ...posted, targetKind: replyTarget.kind };
} catch (e) {
if (replyTarget.kind !== "inlineReviewThread") throw e;
const bot = await getAppBotIdentity(cfg);
let recovered = null;
for (const key of askReplyLookupKeys(item.resourceKey, operationKey)) {
recovered = await findExistingAskReplyComment({
prSurface,
replyTarget,
question: item.payload.question,
botLogin: bot.login,
operationKey: key,
operationInstance: item.id,
});
if (recovered != null) break;
}
if (recovered != null) {
return { commentId: recovered.commentId, targetKind: recovered.targetKind };
}
const failure = classifyFailure(e, { phase: "publish", toolName: "ask_inline_reply" });
logWarn("ask_inline_reply_failed", {
owner: item.owner,
repo: item.repo,
pr: replyTarget.prNumber,
inReplyToCommentId: replyTarget.inReplyToCommentId,
message: e instanceof Error ? e.message : String(e),
...classifiedFailureLogFields(failure),
});
if (!isKnownNoAcceptanceMutationError(e)) throw e;
const fallback = await prSurface.replyAt(
{ kind: "prConversation", prNumber: replyTarget.prNumber },
askReplyBodyWithOperationMarker(
["_Could not reply in the review thread; posting here instead._", "", body].join("\n"),
operationKey,
item.id,
),
);
return { ...fallback, targetKind: "prConversation" };
}
}
async function stashRecoveredAskReply(params: {
readonly pool: Pool;
readonly item: AskWorkItem;
readonly operationKey: string;
readonly commentId: number;
readonly targetKind: AskWorkItem["payload"]["replyTarget"]["kind"];
}): Promise<void> {
const { pool, item, operationKey, commentId, targetKind } = params;
const intent = await getOperationIntent(pool, item.id, operationKey);
const result = { commentId };
if (intent == null) {
await persistOperationIntent(pool, {
workItemId: item.id,
operationKey,
mutationKind: "github.ask_reply",
detail: {
step: "ask_reply",
resourceKey: item.resourceKey,
reviewLens: ASK_PUBLISH_LENS,
replyTargetKind: targetKind,
__result: result,
},
});
return;
}
if (askReplyCommentIdFromIntentDetail(intent.detail) != null) return;
if (intent.status === "outcome_unknown") {
// Evidence recovered from GitHub: finish the unknown outcome without remutating.
await reconcileOperationIntent(pool, {
workItemId: item.id,
operationKey,
status: "reconciled",
detail: {
__result: result,
replyTargetKind: targetKind,
recoveredAfterMutating: true,
},
});
return;
}
if (intent.status === "pending") {
await mergeOperationIntentDetail(pool, {
workItemId: item.id,
operationKey,
detail: { __result: result, replyTargetKind: targetKind },
});
}
}
/**
* Recover a GitHub ask reply that was accepted but not yet recorded locally.
* Returns the comment id when delivery can complete without remutation/model rerun.
*/
type AskReplyRecovery =
| {
readonly kind: "recovered";
readonly commentId: number;
readonly targetKind: AskWorkItem["payload"]["replyTarget"]["kind"];
}
| { readonly kind: "outcome_unknown" }
| null;
async function recoverDeliveredAskReplyCommentId(params: {
readonly cfg: Config;
readonly pool: Pool;
readonly prSurface: PrSurface;
readonly item: AskWorkItem;
}): Promise<AskReplyRecovery> {
const { cfg, pool, prSurface, item } = params;
const operationKey = askReplyOperationKey(item.resourceKey, item.payload.commentId);
const intent = await getOperationIntent(pool, item.id, operationKey);
const stashed = askReplyCommentIdFromIntentDetail(intent?.detail);
if (stashed != null) {
return {
kind: "recovered",
commentId: stashed,
targetKind: replyTargetKindFromIntentDetail(
intent?.detail.replyTargetKind,
item.payload.replyTarget.kind,
),
};
}
// Scan only while the mutation may still be in flight or outcome-unknown.
// Skip failed/reconciled so an older identical-question reply is not reused.
if (intent == null || (intent.status !== "pending" && intent.status !== "outcome_unknown")) {
return null;
}
const bot = await getAppBotIdentity(cfg);
const recovered = await findAskReplyOnAnyTarget({
prSurface,
item,
botLogin: bot.login,
operationKey,
operationInstance: item.id,
});
if (recovered == null) {
return intent.status === "outcome_unknown" || intent.detail.__mutating === true
? { kind: "outcome_unknown" }
: null;
}
await stashRecoveredAskReply({
pool,
item,
operationKey,
commentId: recovered.commentId,
targetKind: recovered.targetKind ?? item.payload.replyTarget.kind,
});
return {
kind: "recovered",
commentId: recovered.commentId,
targetKind: recovered.targetKind ?? item.payload.replyTarget.kind,
};
}
type AskFailureReplyDecision = "skip" | "publish";
/** Confirmed delivery only. An outcome_unknown answer mutation is not delivery. */
async function decideAskFailureReply(params: {
readonly cfg: Config;
readonly pool: Pool;
readonly prSurface: PrSurface;
readonly item: AskWorkItem;
}): Promise<AskFailureReplyDecision> {
const { cfg, pool, prSurface, item } = params;
if (
await hasCompletedPublishStep(pool, item.id, item.resourceKey, ASK_PUBLISH_LENS, "ask_reply")
) {
return "skip";
}
const recovered = await recoverDeliveredAskReplyCommentId({
cfg,
pool,
prSurface,
item,
});
return recovered?.kind === "recovered" ? "skip" : "publish";
}
async function finalizeAskReplyPublish(params: {
readonly pool: Pool;
readonly item: AskWorkItem;
readonly commentId: number;
readonly targetKind: AskWorkItem["payload"]["replyTarget"]["kind"];
readonly leaseEpoch: number | null;
}): Promise<"ok" | "degraded"> {
const { pool, item, commentId, targetKind, leaseEpoch } = params;
await withOperationIntent({
client: pool,
workItemId: item.id,
operationKey: askReplyOperationKey(item.resourceKey, item.payload.commentId),
mutationKind: "github.ask_reply",
leaseEpoch,
detail: {
step: "ask_reply",
resourceKey: item.resourceKey,
reviewLens: ASK_PUBLISH_LENS,
replyTargetKind: targetKind,
},
mutate: async () => ({ commentId }),
});
try {
await recordAskPublishStep(pool, {
workItemId: item.id,
resourceKey: item.resourceKey,
step: "ask_reply",
detail: {
replyTargetKind: targetKind,
commentId,
},
leaseEpoch,
});
return "ok";
} catch (e) {
const failure = classifyFailure(e, { phase: "publish" });
logWarn("ask_publish_record_failed", {
owner: item.owner,
repo: item.repo,
pr: item.prNumber,
workItemId: item.id,
message: e instanceof Error ? e.message : String(e),
...classifiedFailureLogFields(failure),
});
captureEvent({
distinctId: `installation:${item.installationId}`,
event: "ask failed",
properties: {
owner: item.owner,
repo: item.repo,
pr_number: item.prNumber,
reply_target_kind: item.payload.replyTarget.kind,
...classifiedFailurePostHogProperties(failure),
},
});
return "degraded";
}
}
export async function executeAskJob(
cfg: Config,
pool: Pool,
boss: PgBoss,
job: JobWithMetadata<AskJobData>,
): Promise<void> {
await runDurableWorkItem({
cfg,
pool,
boss,
job,
type: "ask",
resolveHeadSha: resolveWorkItemHead,
execute: async (item, env) => {
const { prSurface } = env;
const headSha = env.headSha;
const payload = item.payload;
const askReplyPublished = () =>
hasCompletedPublishStep(pool, item.id, item.resourceKey, ASK_PUBLISH_LENS, "ask_reply");
if (await askReplyPublished()) {
return { kind: "completed" };
}
const recoveredReply = await recoverDeliveredAskReplyCommentId({
cfg,
pool,
prSurface,
item,
});
if (recoveredReply?.kind === "recovered") {
const status = await finalizeAskReplyPublish({
pool,
item,
commentId: recoveredReply.commentId,
targetKind: recoveredReply.targetKind,
leaseEpoch: env.leaseEpoch,
});
return status === "degraded"
? { kind: "completed", degraded: true }
: { kind: "completed" };
}
if (recoveredReply?.kind === "outcome_unknown") {
// The provider may have accepted the reply, but no exact marker was
// found. Do not rerun the model or create a fallback reply.
return { kind: "completed", degraded: true };
}
return withPrRepositoryView(
buildRepositoryViewParams(
item,
{
gitCredentialAuth: () => prSurface.gitCredentialAuth(),
headSha,
pullRequest: env.pullRequest,
},
payload,
),
async (repositoryView) => {
const transcript = await loadAskThreadTranscript({
prSurface,
replyTarget: payload.replyTarget,
commentId: payload.commentId,
});
const ready =
cfg.codeIndexMode === "fts"
? await waitForReadySnapshot(
pool,
{
installationId: item.installationId,
owner: item.owner,
repo: item.repo,
headSha,
},
0,
)
: null;
const executionId = createAskExecutionId();
const result = await runAskRun({
cfg,
prSurface,
owner: item.owner,
repo: item.repo,
prNumber: item.prNumber,
headSha,
question: payload.question,
replyTarget: payload.replyTarget,
codeAnchor: payload.codeAnchor,
threadTranscript: transcript.text,
threadTranscriptTruncated: transcript.truncated,
cwd: repositoryView.agentCwd,
workspace: repositoryView.workspace,
durability: {
pool,
workItemId: item.id,
installationId: item.installationId,
},
pool,
codeIndexSnapshotId: ready?.id,
});
await recordAskProviderUsage(pool, {
workItemId: item.id,
executionId,
usage: result.usage,
});
if (!(await askReplyPublished())) {
const operationKey = askReplyOperationKey(item.resourceKey, payload.commentId);
let selectedTargetKind = payload.replyTarget.kind;
const posted = await withOperationIntent<{ readonly commentId: number }>({
client: pool,
workItemId: item.id,
operationKey,
mutationKind: "github.ask_reply",
leaseEpoch: env.leaseEpoch,
detail: {
step: "ask_reply",
resourceKey: item.resourceKey,
reviewLens: ASK_PUBLISH_LENS,
replyTargetKind: payload.replyTarget.kind,
},
recover: async () => {
const bot = await getAppBotIdentity(cfg);
const recovered = await findAskReplyOnAnyTarget({
prSurface,
item,
botLogin: bot.login,
operationKey,
operationInstance: item.id,
});
return recovered == null
? { kind: "absent" as const }
: {
kind: "reconciled" as const,
value: { commentId: recovered.commentId },
detail: { replyTargetKind: recovered.targetKind },
};
},
isKnownNoAcceptanceError: isKnownNoAcceptanceMutationError,
reconcileDetail: () => ({ replyTargetKind: selectedTargetKind }),
mutate: async () => {
const published = await publishAskAnswer(
cfg,
prSurface,
item,
result.answer,
operationKey,
true,
);
selectedTargetKind = published.targetKind;
return { commentId: published.commentId };
},
});
captureEvent({
distinctId: `installation:${item.installationId}`,
event: "ask answered",
properties: {
owner: item.owner,
repo: item.repo,
pr_number: item.prNumber,
reply_target_kind: payload.replyTarget.kind,
},
});
try {
await recordAskPublishStep(pool, {
workItemId: item.id,
resourceKey: item.resourceKey,
step: "ask_reply",
detail: {
replyTargetKind: selectedTargetKind,
commentId: posted.commentId,
},
leaseEpoch: env.leaseEpoch,
});
} catch (e) {
const failure = classifyFailure(e, { phase: "publish" });
logWarn("ask_publish_record_failed", {
owner: item.owner,
repo: item.repo,
pr: item.prNumber,
workItemId: item.id,
message: e instanceof Error ? e.message : String(e),
...classifiedFailureLogFields(failure),
});
captureEvent({
distinctId: `installation:${item.installationId}`,
event: "ask failed",
properties: {
owner: item.owner,
repo: item.repo,
pr_number: item.prNumber,
reply_target_kind: payload.replyTarget.kind,
...classifiedFailurePostHogProperties(failure),
},
});
return { kind: "completed", degraded: true };
}
}
return { kind: "completed" };
},
);
},
onTerminalFailure: async (item, prSurface, error) => {
const failure = classifyFailure(error ?? new Error("Ask failed after retries"), {
phase: "ask",
});
captureEvent({
distinctId: `installation:${item.installationId}`,
event: "ask failed",
properties: {
owner: item.owner,
repo: item.repo,
pr_number: item.prNumber,
reply_target_kind: item.payload.replyTarget.kind,
...classifiedFailurePostHogProperties(failure),
},
});
if (!prSurface) return;
if ((await decideAskFailureReply({ cfg, pool, prSurface, item })) === "skip") return;
const payload = item.payload;
const operationKey = askFailureReplyOperationKey(item.resourceKey, item.payload.commentId);
await withOperationIntent({
client: pool,
workItemId: item.id,
operationKey,
mutationKind: "github.ask_failure_reply",
detail: {
step: "ask_failure_reply",
resourceKey: item.resourceKey,
reviewLens: ASK_PUBLISH_LENS,
replyTargetKind: payload.replyTarget.kind,
},
recover: async () => {
const bot = await getAppBotIdentity(cfg);
const recovered = await findAskReplyOnAnyTarget({
prSurface,
item,
botLogin: bot.login,
operationKey,
operationInstance: item.id,
});
return recovered == null
? { kind: "absent" as const }
: {
kind: "reconciled" as const,
value: { commentId: recovered.commentId },
detail: { replyTargetKind: recovered.targetKind },
};
},
isKnownNoAcceptanceError: isKnownNoAcceptanceMutationError,
mutate: async () => {
const published = await publishAskAnswer(
cfg,
prSurface,
item,
formatAskReply({
question: payload.question,
answer: "PR Agent could not complete this ask after retries. Please try again later.",
replyTarget: payload.replyTarget,
}),
operationKey,
true,
);
return { commentId: published.commentId };
},
});
},
});
}