@leitwerk-dev/coding 0.2.1 → 0.3.1

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 (85) hide show
  1. package/README.md +51 -12
  2. package/dist/actions.d.ts +0 -5
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/auto-work-branch-internal.d.ts +2 -18
  5. package/dist/auto-work-branch-internal.d.ts.map +1 -1
  6. package/dist/auto-work-branch.d.ts +1 -2
  7. package/dist/auto-work-branch.d.ts.map +1 -1
  8. package/dist/auto-work-branch.js +4 -102
  9. package/dist/auto-work-branch.js.map +1 -1
  10. package/dist/chunk-6E5VQPC4.js +28 -0
  11. package/dist/chunk-6E5VQPC4.js.map +1 -0
  12. package/dist/chunk-GO44KCUS.js +154 -0
  13. package/dist/chunk-GO44KCUS.js.map +1 -0
  14. package/dist/chunk-KFELB7ZC.js +70 -0
  15. package/dist/chunk-KFELB7ZC.js.map +1 -0
  16. package/dist/finalization-git-internal.d.ts +6 -45
  17. package/dist/finalization-git-internal.d.ts.map +1 -1
  18. package/dist/finalization-git.js +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +48 -91
  22. package/dist/index.js.map +1 -1
  23. package/dist/profile-bindings-internal.d.ts +3 -0
  24. package/dist/profile-bindings-internal.d.ts.map +1 -0
  25. package/dist/pull-request-publication-internal.d.ts +73 -0
  26. package/dist/pull-request-publication-internal.d.ts.map +1 -0
  27. package/dist/repository-change-launch-internal-Dd-AONUs.d.ts +67 -0
  28. package/dist/repository-change-launch-internal.d.ts +31 -82
  29. package/dist/repository-change-launch-internal.d.ts.map +1 -1
  30. package/dist/repository-change-launch.d.ts +4 -2
  31. package/dist/repository-change-launch.d.ts.map +1 -1
  32. package/dist/repository-change-launch.js +205 -133
  33. package/dist/repository-change-launch.js.map +1 -1
  34. package/dist/repository-change-process.d.ts +7 -19
  35. package/dist/repository-change-process.d.ts.map +1 -1
  36. package/dist/repository-change-publication.d.ts +229 -0
  37. package/dist/repository-change-publication.d.ts.map +1 -0
  38. package/dist/repository-change-publication.js +770 -0
  39. package/dist/repository-change-publication.js.map +1 -0
  40. package/dist/repository-change-state-internal.d.ts +2 -11
  41. package/dist/repository-change-state-internal.d.ts.map +1 -1
  42. package/dist/repository-change-state.d.ts +0 -1
  43. package/dist/repository-change-state.d.ts.map +1 -1
  44. package/dist/repository-change-state.js +0 -7
  45. package/dist/repository-change-ui-launcher-internal.d.ts +53 -0
  46. package/dist/repository-change-ui-launcher-internal.d.ts.map +1 -0
  47. package/dist/repository-rebase/git.d.ts +31 -0
  48. package/dist/repository-rebase/git.d.ts.map +1 -0
  49. package/dist/repository-rebase/index.d.ts +42 -0
  50. package/dist/repository-rebase/index.d.ts.map +1 -0
  51. package/dist/repository-rebase/index.js +16 -0
  52. package/dist/repository-rebase/policy.d.ts +37 -0
  53. package/dist/repository-rebase/policy.d.ts.map +1 -0
  54. package/dist/repository-rebase/prompt.d.ts +3 -0
  55. package/dist/repository-rebase/prompt.d.ts.map +1 -0
  56. package/package.json +14 -4
  57. package/src/actions.ts +0 -13
  58. package/src/auto-work-branch-internal.ts +22 -130
  59. package/src/auto-work-branch.ts +1 -7
  60. package/src/finalization-git-internal.ts +60 -697
  61. package/src/index.ts +0 -1
  62. package/src/profile-bindings-internal.ts +39 -0
  63. package/src/pull-request-publication-internal.ts +286 -0
  64. package/src/repository-change-launch-internal.ts +79 -267
  65. package/src/repository-change-launch.ts +10 -6
  66. package/src/repository-change-process.ts +8 -126
  67. package/src/repository-change-publication.ts +673 -0
  68. package/src/repository-change-state-internal.ts +4 -34
  69. package/src/repository-change-state.ts +0 -1
  70. package/src/repository-change-ui-launcher-internal.ts +225 -0
  71. package/src/repository-rebase/git.ts +219 -0
  72. package/src/repository-rebase/index.ts +144 -0
  73. package/src/repository-rebase/policy.ts +75 -0
  74. package/src/repository-rebase/prompt.ts +2 -0
  75. package/dist/chunk-HUVXPZSF.js +0 -61
  76. package/dist/chunk-HUVXPZSF.js.map +0 -1
  77. package/dist/chunk-MY6XFGHM.js +0 -614
  78. package/dist/chunk-MY6XFGHM.js.map +0 -1
  79. package/dist/chunk-OERAYSVI.js +0 -27
  80. package/dist/chunk-OERAYSVI.js.map +0 -1
  81. package/dist/turns/resolve-merge-conflict.d.ts +0 -4
  82. package/dist/turns/resolve-merge-conflict.d.ts.map +0 -1
  83. package/dist/turns/resolve-merge-conflict.js +0 -8
  84. package/src/turns/resolve-merge-conflict.ts +0 -26
  85. /package/dist/{turns/resolve-merge-conflict.js.map → repository-rebase/index.js.map} +0 -0
@@ -0,0 +1,673 @@
1
+ import { asUnknownRecord } from "@leitwerk-dev/domain";
2
+ import {
3
+ type AutomaticOutcomeBuilder,
4
+ type ExternalActionSource,
5
+ type FlowAutomaticRunContext,
6
+ flow,
7
+ } from "@leitwerk-dev/process-sdk";
8
+ import { commitAndPushWorkBranch, type GitIdentity } from "./finalization-git.js";
9
+ import type { RepositoryChangeState } from "./repository-change-state.js";
10
+ import { publishRebase, startRebase } from "./repository-rebase/git.js";
11
+ import { type ConflictEvidence, conflictKey, validateConflict } from "./repository-rebase/index.js";
12
+ import { rebasePrompt } from "./repository-rebase/prompt.js";
13
+
14
+ export {
15
+ pullRequestPublicationCallbacks,
16
+ pullRequestPublicationSources,
17
+ readPullRequestFeedback,
18
+ reconcilePullRequestSourceIssue,
19
+ resolvePullRequestGitIdentity,
20
+ } from "./pull-request-publication-internal.js";
21
+
22
+ /** @public */
23
+ export interface PublicationRequest {
24
+ /** @public */
25
+ number: number;
26
+ /** @public */
27
+ html_url: string;
28
+ /** @public */
29
+ merged: boolean;
30
+ /** @public */
31
+ merge_commit_sha?: string | null;
32
+ }
33
+ /** @public */
34
+ export interface PublicationPipeline {
35
+ /** @public */
36
+ number: number;
37
+ /** @public */
38
+ status: string;
39
+ /** @public */
40
+ [key: string]: unknown;
41
+ }
42
+ /** @public */
43
+ export interface PublicationParams {
44
+ /** @public */
45
+ owner: string;
46
+ /** @public */
47
+ repo: string;
48
+ /** @public */
49
+ workBranch: string;
50
+ /** @public */
51
+ baseBranch: string;
52
+ }
53
+ /** @public */
54
+ export type PublicationContext<P> = FlowAutomaticRunContext<P, RepositoryChangeState>;
55
+ /** @public */
56
+ export type PublicationEvidence = (
57
+ | {
58
+ /** @public */
59
+ kind: "feedback";
60
+ /** @public */
61
+ feedbackIds: PublicationFeedbackId[];
62
+ /** @public */
63
+ conversationCursor: number;
64
+ /** @public */
65
+ reviewCursor: number;
66
+ /** @public */
67
+ inlineCursor: number;
68
+ }
69
+ | {
70
+ /** @public */
71
+ kind: "failure";
72
+ /** @public */
73
+ pipeline: PublicationPipeline;
74
+ }
75
+ | {
76
+ /** @public */
77
+ kind: "conflict";
78
+ /** @public */
79
+ conflict: ConflictEvidence;
80
+ }
81
+ | {
82
+ /** @public */
83
+ kind: "terminal";
84
+ /** @public */
85
+ request: PublicationRequest;
86
+ }
87
+ | {
88
+ /** @public */
89
+ kind: "cancelled";
90
+ }
91
+ | {
92
+ /** @public */
93
+ kind: "observed";
94
+ }
95
+ ) & {
96
+ /** @public */
97
+ observationKey?: string;
98
+ };
99
+ /** @public */
100
+ export interface PublicationSource<P> {
101
+ /** @public */
102
+ id: string;
103
+ /** @public */
104
+ kind: "feedback" | "failure" | "conflict" | "terminal" | "cancelled" | "observation";
105
+ /** @public */
106
+ operatorId?: string;
107
+ /** @public */
108
+ label: string;
109
+ /** @public */
110
+ source: ExternalActionSource<P, RepositoryChangeState, unknown>;
111
+ /** @public */
112
+ enabled?(params: P): boolean;
113
+ /** @public */
114
+ read(input: {
115
+ /** @public */
116
+ params: P;
117
+ /** @public */
118
+ state: RepositoryChangeState;
119
+ /** @public */
120
+ event: unknown;
121
+ }): PublicationEvidence;
122
+ }
123
+ /** @public */
124
+ export interface RepositoryChangePublicationAdapter<P extends PublicationParams> {
125
+ /** @public */
126
+ namespace: string;
127
+ /** @public */
128
+ label: string;
129
+ /** @public */
130
+ ids: {
131
+ /** @public */
132
+ deliver: string;
133
+ /** @public */
134
+ feedback: string;
135
+ /** @public */
136
+ ciRepair: string;
137
+ /** @public */
138
+ operator: string;
139
+ };
140
+ /** @public */
141
+ tools: {
142
+ /** @public */
143
+ delivery: readonly string[];
144
+ /** @public */
145
+ feedback: readonly string[];
146
+ /** @public */
147
+ ci: readonly string[];
148
+ };
149
+ /** @public */
150
+ sources: readonly PublicationSource<P>[];
151
+ /** @public */
152
+ identity(ctx: PublicationContext<P>): Promise<GitIdentity>;
153
+ /** @public */
154
+ ensureRequest(ctx: PublicationContext<P>): Promise<PublicationRequest>;
155
+ /** @public */
156
+ reconcileTerminal(
157
+ ctx: PublicationContext<P>,
158
+ current: PublicationState,
159
+ request: PublicationRequest,
160
+ ): Promise<void>;
161
+ /** @public */
162
+ linkIssue(ctx: PublicationContext<P>, current: PublicationState): Promise<void>;
163
+ /** @public */
164
+ acknowledge(ctx: PublicationContext<P>, current: PublicationState): Promise<void>;
165
+ /** @public */
166
+ reply(ctx: PublicationContext<P>, current: PublicationState): Promise<void>;
167
+ /** @public */
168
+ afterRebase(ctx: PublicationContext<P>, current: PublicationState): Promise<void>;
169
+ /** @public */
170
+ head(ctx: PublicationContext<P>, current: PublicationState): Promise<string>;
171
+ /** @public */
172
+ prompt(kind: "feedback" | "ci", current: PublicationState): string;
173
+ /** @public */
174
+ commitMessage(kind: "feedback" | "ci"): string;
175
+ }
176
+ /** @public */
177
+ export type PublicationFeedbackId = {
178
+ /** @public */
179
+ kind: "conversation" | "review" | "inline";
180
+ /** @public */
181
+ id: number;
182
+ /** @public */
183
+ discussionId?: string;
184
+ };
185
+ /** @public */
186
+ type AdjustmentInvocation = {
187
+ /** @public */
188
+ origin: "feedback" | "ci" | "rebase";
189
+ /** @public */
190
+ publishRequired: boolean;
191
+ };
192
+
193
+ /** @public */
194
+ export interface DeliveryState {
195
+ /** @public */
196
+ stage: "not_started" | "branch_published" | "pull_request_ready" | "awaiting";
197
+ /** @public */
198
+ issueLinked: boolean;
199
+ /** @public */
200
+ adjustment: AdjustmentInvocation | null;
201
+ /** @public */
202
+ terminalPullRequest: PublicationRequest | null;
203
+ }
204
+
205
+ /** @public */
206
+ export interface PublicationState {
207
+ /** @public */
208
+ lastConflictKey?: string | null;
209
+ /** @public */
210
+ conflict?: ConflictEvidence | null;
211
+ /** @public */
212
+ repairReason?: "feedback" | "ci" | "rebase";
213
+ /** @public */
214
+ headSha: string | null;
215
+ /** @public */
216
+ prNumber: number | null;
217
+ /** @public */
218
+ prUrl: string | null;
219
+ /** @public */
220
+ conversationCursor: number;
221
+ /** @public */
222
+ reviewCursor: number;
223
+ /** @public */
224
+ inlineCursor: number;
225
+ /** @public */
226
+ feedbackIds: PublicationFeedbackId[];
227
+ /** @public */
228
+ pipeline: PublicationPipeline | null;
229
+ /** @public */
230
+ ciRecoveryCycles: number;
231
+ /** @public */
232
+ delivery: DeliveryState;
233
+ /** @public */
234
+ observationKey?: string;
235
+ /** @public */
236
+ pendingEvidence?: PublicationEvidence | null;
237
+ }
238
+
239
+ const initialDeliveryState: DeliveryState = {
240
+ stage: "not_started",
241
+ issueLinked: false,
242
+ adjustment: null,
243
+ terminalPullRequest: null,
244
+ };
245
+
246
+ const initialPublicationState: PublicationState = {
247
+ headSha: null,
248
+ prNumber: null,
249
+ prUrl: null,
250
+ conversationCursor: 0,
251
+ reviewCursor: 0,
252
+ inlineCursor: 0,
253
+ feedbackIds: [],
254
+ pipeline: null,
255
+ ciRecoveryCycles: 0,
256
+ delivery: initialDeliveryState,
257
+ };
258
+
259
+ /** @public */
260
+ export const publicationObject = (value: unknown) => asUnknownRecord(value) ?? {};
261
+
262
+ /** @public */
263
+ export function readPublicationState(
264
+ state: RepositoryChangeState,
265
+ namespace: string,
266
+ ): PublicationState {
267
+ const stored = publicationObject(publicationObject(state.extensionState)[namespace]);
268
+ return {
269
+ ...initialPublicationState,
270
+ ...stored,
271
+ delivery: {
272
+ ...initialDeliveryState,
273
+ ...publicationObject(stored.delivery),
274
+ },
275
+ } as PublicationState;
276
+ }
277
+
278
+ /** @public */
279
+ export function patchPublicationState(
280
+ state: RepositoryChangeState,
281
+ namespace: string,
282
+ patch: Partial<Omit<PublicationState, "delivery">> & {
283
+ /** @public */
284
+ delivery?: Partial<DeliveryState>;
285
+ },
286
+ ): RepositoryChangeState {
287
+ const current = readPublicationState(state, namespace);
288
+ return {
289
+ ...state,
290
+ extensionState: {
291
+ ...state.extensionState,
292
+ [namespace]: {
293
+ ...current,
294
+ ...patch,
295
+ delivery: { ...current.delivery, ...patch.delivery },
296
+ },
297
+ },
298
+ };
299
+ }
300
+
301
+ /** @public */
302
+ export function applyPublicationEvidence(
303
+ params: PublicationParams,
304
+ current: PublicationState,
305
+ evidence: PublicationEvidence,
306
+ countFailure = true,
307
+ ): PublicationState {
308
+ if (evidence.observationKey !== undefined)
309
+ current = { ...current, observationKey: evidence.observationKey };
310
+ switch (evidence.kind) {
311
+ case "feedback": {
312
+ const { kind: _, ...batch } = evidence;
313
+ return { ...current, ...batch, repairReason: "feedback" };
314
+ }
315
+ case "failure":
316
+ return {
317
+ ...current,
318
+ repairReason: "ci",
319
+ pipeline: evidence.pipeline,
320
+ ciRecoveryCycles: current.ciRecoveryCycles + (countFailure ? 1 : 0),
321
+ };
322
+ case "conflict": {
323
+ const conflict = validateConflict(
324
+ evidence.conflict,
325
+ {
326
+ owner: params.owner,
327
+ repo: params.repo,
328
+ prNumber: current.prNumber ?? 0,
329
+ headSha: current.headSha ?? "",
330
+ headBranch: params.workBranch,
331
+ baseBranch: params.baseBranch,
332
+ },
333
+ current.lastConflictKey,
334
+ );
335
+ return {
336
+ ...current,
337
+ conflict,
338
+ lastConflictKey: conflictKey(conflict),
339
+ repairReason: "rebase",
340
+ };
341
+ }
342
+ case "terminal":
343
+ return {
344
+ ...current,
345
+ delivery: { ...current.delivery, terminalPullRequest: evidence.request },
346
+ };
347
+ default:
348
+ return current;
349
+ }
350
+ }
351
+ function deliveryProgress(value: PublicationState, label: string, activeStepId?: string) {
352
+ const delivery = value.delivery;
353
+ const step = (id: string, label: string, done: boolean) => ({
354
+ id,
355
+ label,
356
+ status: (activeStepId === id ? "in_progress" : done ? "completed" : "incomplete") as
357
+ | "in_progress"
358
+ | "completed"
359
+ | "incomplete",
360
+ });
361
+ const adjustmentHandled =
362
+ delivery.adjustment === null && delivery.stage === "awaiting" && value.headSha !== null;
363
+ return {
364
+ title: "Delivery progress",
365
+ steps: [
366
+ step("publish_branch", "Publish work branch", value.headSha !== null),
367
+ step("open_pr", "Create or find pull request", value.prNumber !== null),
368
+ step("link_issue", "Link source issue when applicable", delivery.issueLinked),
369
+ step("publish_adjustment", "Publish an adjustment when applicable", adjustmentHandled),
370
+ step(
371
+ "reply_feedback",
372
+ "Acknowledge or reply to feedback when applicable",
373
+ value.feedbackIds.length === 0,
374
+ ),
375
+ step("await_evidence", "Await external evidence", delivery.stage === "awaiting"),
376
+ step(
377
+ "reconcile_terminal",
378
+ "Reconcile terminal pull request",
379
+ delivery.terminalPullRequest !== null,
380
+ ),
381
+ ],
382
+ ...(value.prNumber && value.prUrl
383
+ ? {
384
+ links: [
385
+ {
386
+ id: `${label}-pr-${value.prNumber}`,
387
+ label: `${label} #${value.prNumber}`,
388
+ url: value.prUrl,
389
+ kind: "pull_request" as const,
390
+ },
391
+ ],
392
+ }
393
+ : {}),
394
+ };
395
+ }
396
+
397
+ /** @public */
398
+ export function createRepositoryChangePublication<P extends PublicationParams>(
399
+ adapter: RepositoryChangePublicationAdapter<P>,
400
+ ) {
401
+ const ids = adapter.ids;
402
+ const publication = flow.fragment<P, RepositoryChangeState>(`${adapter.namespace}-publication`);
403
+ const remote = (state: RepositoryChangeState) => readPublicationState(state, adapter.namespace);
404
+ const patchRemote = (
405
+ state: RepositoryChangeState,
406
+ patch: Partial<Omit<PublicationState, "delivery">> & { delivery?: Partial<DeliveryState> },
407
+ ) => patchPublicationState(state, adapter.namespace, patch);
408
+ async function commitWorkBranch(ctx: PublicationContext<P>, commitMessage: string) {
409
+ const repo = ctx.repo.get("repo");
410
+ return commitAndPushWorkBranch({
411
+ repoPath: repo.fsPath,
412
+ workBranch: repo.workBranch,
413
+ commitMessage,
414
+ gitIdentity: await adapter.identity(ctx),
415
+ });
416
+ }
417
+ const delivery = flow
418
+ .automatic<P, RepositoryChangeState>(ids.deliver)
419
+ .description("Deliver")
420
+ .integrationTools(...adapter.tools.delivery)
421
+ .run(async (ctx) => {
422
+ let current = remote(ctx.state);
423
+ const update = (patch: Parameters<typeof patchRemote>[1]) => {
424
+ current = { ...current, ...patch, delivery: { ...current.delivery, ...patch.delivery } };
425
+ };
426
+ const result = (
427
+ outcome:
428
+ | "awaiting"
429
+ | "feedback_ready"
430
+ | "completed"
431
+ | "aborted"
432
+ | "ci_ready"
433
+ | "conflict_ready"
434
+ | "operator_action",
435
+ ) => ({
436
+ outcome,
437
+ params: { nextState: patchRemote(ctx.state, current) },
438
+ });
439
+ if (current.pendingEvidence) {
440
+ const evidence = current.pendingEvidence;
441
+ const needsOperator = evidence.kind === "failure" && current.ciRecoveryCycles >= 3;
442
+ current = applyPublicationEvidence(ctx.params, current, evidence, !needsOperator);
443
+ current.pendingEvidence = null;
444
+ if (evidence.kind === "cancelled") return result("aborted");
445
+ if (evidence.kind === "failure")
446
+ return result(needsOperator ? "operator_action" : "ci_ready");
447
+ if (evidence.kind === "conflict") return result("conflict_ready");
448
+ }
449
+ ctx.reportProgress(deliveryProgress(current, adapter.label));
450
+
451
+ if (current.delivery.terminalPullRequest) {
452
+ ctx.reportProgress(deliveryProgress(current, adapter.label, "reconcile_terminal"));
453
+ const pr = current.delivery.terminalPullRequest;
454
+ await adapter.reconcileTerminal(ctx, current, pr);
455
+ ctx.reportProgress(deliveryProgress(current, adapter.label));
456
+ return result(pr.merged ? "completed" : "aborted");
457
+ }
458
+
459
+ if (!current.headSha) {
460
+ ctx.reportProgress(deliveryProgress(current, adapter.label, "publish_branch"));
461
+ const published = await commitWorkBranch(
462
+ ctx,
463
+ ctx.state.finalization.generatedCommitMessage ?? "",
464
+ );
465
+ update({ headSha: published.headSha, pipeline: null });
466
+ }
467
+
468
+ if (!current.prNumber || !current.prUrl) {
469
+ ctx.reportProgress(deliveryProgress(current, adapter.label, "open_pr"));
470
+ const pr = await adapter.ensureRequest(ctx);
471
+ update({ prNumber: pr.number, prUrl: pr.html_url });
472
+ }
473
+
474
+ if (!current.delivery.issueLinked) {
475
+ ctx.reportProgress(deliveryProgress(current, adapter.label, "link_issue"));
476
+ await adapter.linkIssue(ctx, current);
477
+ update({ delivery: { issueLinked: true } });
478
+ }
479
+
480
+ if (current.delivery.adjustment) {
481
+ const adjustment = current.delivery.adjustment;
482
+ if (adjustment.publishRequired || adjustment.origin === "rebase") {
483
+ ctx.reportProgress(deliveryProgress(current, adapter.label, "publish_adjustment"));
484
+ const published =
485
+ adjustment.origin === "rebase"
486
+ ? publishRebase(rebaseInput(ctx))
487
+ : await commitWorkBranch(ctx, adapter.commitMessage(adjustment.origin));
488
+ if (adjustment.origin === "rebase") {
489
+ const head = await adapter.head(ctx, current);
490
+ if (head !== published.headSha)
491
+ throw new Error("Pull request head has not caught up with rebase publication");
492
+ }
493
+ update({ headSha: published.headSha, pipeline: null });
494
+ }
495
+ if (adjustment.origin === "rebase") await adapter.afterRebase(ctx, current);
496
+ if (adjustment.origin === "feedback") {
497
+ ctx.reportProgress(deliveryProgress(current, adapter.label, "reply_feedback"));
498
+ await adapter.reply(ctx, current);
499
+ }
500
+ update({
501
+ ...(adjustment.origin === "feedback" ? { feedbackIds: [] } : {}),
502
+ delivery: { adjustment: null },
503
+ });
504
+ }
505
+
506
+ if (current.feedbackIds.length > 0) {
507
+ ctx.reportProgress(deliveryProgress(current, adapter.label, "reply_feedback"));
508
+ await adapter.acknowledge(ctx, current);
509
+ return result("feedback_ready");
510
+ }
511
+
512
+ update({ delivery: { stage: "awaiting" } });
513
+ ctx.reportProgress(deliveryProgress(current, adapter.label, "await_evidence"));
514
+ return result("awaiting");
515
+ });
516
+ const outcome = (o: AutomaticOutcomeBuilder<P, RepositoryChangeState>, description: string) =>
517
+ o
518
+ .description(description)
519
+ .object("nextState")
520
+ .state(({ event }) => event.params.nextState as RepositoryChangeState);
521
+ delivery
522
+ .outcome("awaiting", (o) => outcome(o, "Delivery is waiting for external evidence").wait())
523
+ .outcome("feedback_ready", (o) =>
524
+ outcome(o, "Pull request feedback is acknowledged and ready for revision").to(ids.feedback),
525
+ )
526
+ .outcome("completed", (o) => outcome(o, "Merged pull request was reconciled").complete())
527
+ .outcome("aborted", (o) =>
528
+ outcome(o, "Closed pull request was reconciled").lifecycleStatus("aborted"),
529
+ );
530
+ for (const [name, target] of [
531
+ ["ci_ready", ids.ciRepair],
532
+ ["conflict_ready", ids.feedback],
533
+ ["operator_action", ids.operator],
534
+ ] as const) {
535
+ delivery.outcome(name, (o) => outcome(o, "Route observed delivery evidence").to(target));
536
+ }
537
+ for (const source of adapter.sources) {
538
+ const attach = (id: string, operator: boolean) =>
539
+ delivery.externalAction(id, source.source, (external) => {
540
+ external
541
+ .label(source.label)
542
+ .when(
543
+ ({ params, state }) =>
544
+ (!source.enabled || source.enabled(params)) &&
545
+ (source.kind !== "failure" || remote(state).ciRecoveryCycles >= 3 === operator),
546
+ );
547
+ if (source.kind === "cancelled") return external.lifecycleStatus("aborted");
548
+ external.to(
549
+ source.kind === "conflict"
550
+ ? ids.feedback
551
+ : source.kind === "failure"
552
+ ? operator
553
+ ? ids.operator
554
+ : ids.ciRepair
555
+ : ids.deliver,
556
+ );
557
+ return external.effect((input) => {
558
+ const evidence = source.read(input);
559
+ const current = remote(input.state);
560
+ return {
561
+ state: patchRemote(
562
+ input.state,
563
+ source.kind === "observation"
564
+ ? { pendingEvidence: evidence }
565
+ : applyPublicationEvidence(input.params, current, evidence, !operator),
566
+ ),
567
+ };
568
+ });
569
+ });
570
+ attach(source.id, false);
571
+ if (source.kind === "failure") attach(source.operatorId ?? `${source.id}_operator`, true);
572
+ }
573
+ publication.turn(delivery);
574
+ function rebaseInput(ctx: {
575
+ state: RepositoryChangeState;
576
+ repo: { get(key: string): { fsPath: string; workBranch: string } };
577
+ }) {
578
+ const project = ctx.repo.get("repo");
579
+ const conflict = remote(ctx.state).conflict;
580
+ if (!conflict) throw new Error("Missing conflict evidence");
581
+ return { projectKey: "repo", path: project.fsPath, workBranch: project.workBranch, conflict };
582
+ }
583
+ function repairReason(state: RepositoryChangeState, kind: "feedback" | "ci") {
584
+ return kind === "feedback" && remote(state).repairReason === "rebase" ? "rebase" : kind;
585
+ }
586
+
587
+ function revisionPrompt(kind: "feedback" | "ci") {
588
+ return (ctx: { state: RepositoryChangeState }) => {
589
+ const current = remote(ctx.state);
590
+ if (repairReason(ctx.state, kind) === "rebase")
591
+ return `${rebasePrompt} Evidence: ${JSON.stringify(current.conflict)}. Call changes_ready or no_changes when verified complete, cannot_repair when blocked.`;
592
+ return adapter.prompt(kind, current);
593
+ };
594
+ }
595
+
596
+ function revisionTurn(id: string, kind: "feedback" | "ci") {
597
+ const turn = flow
598
+ .llm<P, RepositoryChangeState>(id)
599
+ .description(kind === "feedback" ? "Address Feedback" : "Fix CI")
600
+ .tools("read", "bash", "edit", "write")
601
+ .resolveIntegrationTools((_params, state) =>
602
+ repairReason(state, kind) === "rebase" ? [] : adapter.tools[kind],
603
+ )
604
+ .prepare((ctx) =>
605
+ repairReason(ctx.state, kind) === "rebase" ? startRebase(rebaseInput(ctx)) : {},
606
+ )
607
+ .freshPrimary()
608
+ .prompt(revisionPrompt(kind));
609
+ for (const [name, description, publishRequired] of [
610
+ ["changes_ready", "Repository changes are ready to publish", true],
611
+ ["no_changes", "No repository change is required", false],
612
+ ] as const) {
613
+ turn.outcomeTool(name, (tool) =>
614
+ tool
615
+ .resultSummary()
616
+ .description(description)
617
+ .to(ids.deliver)
618
+ .state(({ ctx }) =>
619
+ patchRemote(ctx.state, {
620
+ delivery: { adjustment: { origin: repairReason(ctx.state, kind), publishRequired } },
621
+ }),
622
+ ),
623
+ );
624
+ }
625
+ return turn.outcomeTool("cannot_repair", (tool) =>
626
+ tool.description("The adjustment requires operator action").to(ids.operator),
627
+ );
628
+ }
629
+
630
+ publication.turn(revisionTurn(ids.feedback, "feedback"));
631
+ publication.turn(revisionTurn(ids.ciRepair, "ci"));
632
+ publication.turn({
633
+ id: ids.operator,
634
+ definition: {
635
+ kind: "human",
636
+ description: "Repository repair needs operator action",
637
+ operatorAttention: "required",
638
+ actions: {
639
+ retry_repair: {
640
+ label: "Retry repair",
641
+ acceptanceState: "accepted",
642
+ branches: { feedback: { to: ids.feedback }, ci: { to: ids.ciRepair } },
643
+ choose: ({ ctx }) =>
644
+ remote(ctx.state).repairReason === "rebase" ||
645
+ remote(ctx.state).repairReason === "feedback"
646
+ ? "feedback"
647
+ : "ci",
648
+ },
649
+ resume_waiting: {
650
+ label: "Resume waiting",
651
+ acceptanceState: "accepted",
652
+ to: ids.deliver,
653
+ effect: ({ ctx }) => ({
654
+ state: patchRemote(ctx.state, {
655
+ feedbackIds: [],
656
+ delivery: { adjustment: null },
657
+ }),
658
+ }),
659
+ },
660
+ abort: { label: "Abort process", acceptanceState: "neutral", lifecycleStatus: "aborted" },
661
+ },
662
+ },
663
+ });
664
+
665
+ return {
666
+ /** @public */
667
+ entryTurnId: ids.deliver,
668
+ /** @public */
669
+ fragment: publication,
670
+ /** @public */
671
+ happyPath: [ids.deliver],
672
+ };
673
+ }