@leitwerk-dev/coding 0.1.2 → 0.1.3

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 (57) hide show
  1. package/dist/actions.d.ts +18 -0
  2. package/dist/actions.d.ts.map +1 -0
  3. package/dist/actions.js +45 -0
  4. package/dist/actions.js.map +1 -0
  5. package/dist/auto-work-branch.d.ts +10 -11
  6. package/dist/auto-work-branch.d.ts.map +1 -0
  7. package/dist/auto-work-branch.js +90 -90
  8. package/dist/auto-work-branch.js.map +1 -1
  9. package/dist/finalization-git.d.ts +9 -11
  10. package/dist/finalization-git.d.ts.map +1 -0
  11. package/dist/finalization-git.js +532 -11
  12. package/dist/finalization-git.js.map +1 -1
  13. package/dist/index.d.ts +7 -61
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +6 -785
  16. package/dist/index.js.map +1 -1
  17. package/dist/repository-change-launch.d.ts +15 -17
  18. package/dist/repository-change-launch.d.ts.map +1 -0
  19. package/dist/repository-change-launch.js +182 -17
  20. package/dist/repository-change-launch.js.map +1 -1
  21. package/dist/repository-change-process.d.ts +38 -0
  22. package/dist/repository-change-process.d.ts.map +1 -0
  23. package/dist/repository-change-process.js +751 -0
  24. package/dist/repository-change-process.js.map +1 -0
  25. package/dist/repository-change-state.d.ts +8 -10
  26. package/dist/repository-change-state.d.ts.map +1 -0
  27. package/dist/repository-change-state.js +50 -12
  28. package/dist/repository-change-state.js.map +1 -1
  29. package/dist/turns/generate-commit-message.d.ts +4 -6
  30. package/dist/turns/generate-commit-message.d.ts.map +1 -0
  31. package/dist/turns/generate-commit-message.js +38 -9
  32. package/dist/turns/generate-commit-message.js.map +1 -1
  33. package/dist/turns/generate-plan.d.ts +3 -5
  34. package/dist/turns/generate-plan.d.ts.map +1 -0
  35. package/dist/turns/generate-plan.js +19 -7
  36. package/dist/turns/generate-plan.js.map +1 -1
  37. package/dist/turns/implement.d.ts +3 -5
  38. package/dist/turns/implement.d.ts.map +1 -0
  39. package/dist/turns/implement.js +17 -7
  40. package/dist/turns/implement.js.map +1 -1
  41. package/dist/turns/resolve-merge-conflict.d.ts +3 -5
  42. package/dist/turns/resolve-merge-conflict.d.ts.map +1 -0
  43. package/dist/turns/resolve-merge-conflict.js +21 -7
  44. package/dist/turns/resolve-merge-conflict.js.map +1 -1
  45. package/dist/turns/review-implementation.d.ts +4 -7
  46. package/dist/turns/review-implementation.d.ts.map +1 -0
  47. package/dist/turns/review-implementation.js +27 -9
  48. package/dist/turns/review-implementation.js.map +1 -1
  49. package/dist/turns/review-plan.d.ts +4 -7
  50. package/dist/turns/review-plan.d.ts.map +1 -0
  51. package/dist/turns/review-plan.js +21 -9
  52. package/dist/turns/review-plan.js.map +1 -1
  53. package/dist/turns/simplify-implementation.d.ts +3 -5
  54. package/dist/turns/simplify-implementation.d.ts.map +1 -0
  55. package/dist/turns/simplify-implementation.js +7 -7
  56. package/dist/turns/simplify-implementation.js.map +1 -1
  57. package/package.json +3 -3
@@ -0,0 +1,751 @@
1
+ import { createReviewSubject, normalizeOptionalMarkdown } from "@leitwerk-dev/domain";
2
+ import { acceptedReviewHandoffAction, createEmptyStructuralProcessState, flow, humanTurn, revisionAction, } from "@leitwerk-dev/process-sdk";
3
+ import { acceptReviewForm, codingActionIds, requestReviewChangesForm, requestRevisionForm, } from "./actions.js";
4
+ import { runDeterministicFinalization } from "./finalization-git.js";
5
+ import { clearReviewRefs, createEmptyRepositoryChangeFinalizationState, repositoryChangeStateCodec, resetRepositoryChangeFinalizationState, } from "./repository-change-state.js";
6
+ import { buildGenerateCommitMessagePrompt, normalizeGeneratedCommitMessage, } from "./turns/generate-commit-message.js";
7
+ import { buildGeneratePlanPrompt } from "./turns/generate-plan.js";
8
+ import { buildImplementPrompt } from "./turns/implement.js";
9
+ import { buildResolveMergeConflictPrompt } from "./turns/resolve-merge-conflict.js";
10
+ import { buildReviewImplementationPrompt } from "./turns/review-implementation.js";
11
+ import { buildReviewPlanPrompt } from "./turns/review-plan.js";
12
+ import { buildSimplifyImplementationPrompt } from "./turns/simplify-implementation.js";
13
+ export function createRepositoryChangeProcess(config) {
14
+ const planReviewSubject = createReviewSubject("plan");
15
+ const implementationReviewSubject = createReviewSubject("implementation");
16
+ const turnIds = {
17
+ importPlan: "import_plan",
18
+ generatePlan: "generate_plan",
19
+ planDecision: "plan_decision",
20
+ reviewPlan: "review_plan",
21
+ planReviewFeedback: "plan_review_feedback",
22
+ implement: "implement",
23
+ implementationDecision: "implementation_decision",
24
+ reviewImplementation: "review_implementation",
25
+ implementationReviewFeedback: "implementation_review_feedback",
26
+ simplifyImplementation: "simplify_implementation",
27
+ simplificationDecision: "simplification_decision",
28
+ generateCommitMessage: "generate_commit_message",
29
+ commitAndMerge: "commit_and_merge",
30
+ resolveMergeConflict: "resolve_merge_conflict",
31
+ };
32
+ const products = {
33
+ plan: "plan",
34
+ review: "review",
35
+ simplificationPlan: "simplification-plan",
36
+ implementationSummary: "implementation-summary",
37
+ mergeResolutionSummary: "merge-resolution-summary",
38
+ commitMessage: "commit-message",
39
+ };
40
+ const implementationTurnAvailableTools = ["read", "bash", "edit", "write"];
41
+ function normalizeMessage(value) {
42
+ return typeof value === "string" ? value.trim() : "";
43
+ }
44
+ function resetFinalizationAfterMessage(state, overrides = {}) {
45
+ return resetRepositoryChangeFinalizationState({
46
+ generatedCommitMessage: state.finalization.generatedCommitMessage,
47
+ ...overrides,
48
+ });
49
+ }
50
+ function hasResolvedTurnRecordRef(ref) {
51
+ return typeof ref?.turnRecordId === "string" && ref.turnRecordId.trim() !== "";
52
+ }
53
+ function requireResolvedPlanRef(plan, usage) {
54
+ if (!hasResolvedTurnRecordRef(plan)) {
55
+ throw new Error(`No plan is available for ${usage}`);
56
+ }
57
+ }
58
+ function buildImplementationReviewTurnState(ctx) {
59
+ return patchRepositoryChangeState(ctx.state, {
60
+ reviewSubject: implementationReviewSubject,
61
+ clearReviewRefs: true,
62
+ clearProductRefs: [products.simplificationPlan],
63
+ });
64
+ }
65
+ function buildAcceptedReviewFollowUpMessage(input) {
66
+ const adjustment = normalizeMessage(input.adjustment);
67
+ if (input.reviewSubject.kind === "plan") {
68
+ return [
69
+ "Revise the plan according to this review:",
70
+ input.reviewMarkdown,
71
+ adjustment
72
+ ? ["Also follow this adjustment where it conflicts with the review:", adjustment].join("\n\n")
73
+ : null,
74
+ ]
75
+ .filter((section) => typeof section === "string" && section !== "")
76
+ .join("\n\n");
77
+ }
78
+ return buildAcceptedImplementationFollowUpMessage({
79
+ kind: "review",
80
+ implementationMarkdown: input.reviewMarkdown,
81
+ adjustment,
82
+ });
83
+ }
84
+ function buildAcceptedImplementationFollowUpMessage(input) {
85
+ const adjustment = normalizeMessage(input.adjustment);
86
+ const isSimplification = input.kind === "simplification";
87
+ const subject = isSimplification ? "simplification plan" : "review";
88
+ return [
89
+ `Implement according to this ${subject}:`,
90
+ input.implementationMarkdown,
91
+ adjustment
92
+ ? [`Also follow this adjustment where it conflicts with the ${subject}:`, adjustment].join("\n\n")
93
+ : null,
94
+ [
95
+ "This is now an implementation turn. Ignore earlier review-only or read-only instructions.",
96
+ "You may change files in the repository workspace.",
97
+ `Use the available tools (${implementationTurnAvailableTools.join(", ")}) to inspect, edit, build, and test the change.`,
98
+ "When finished, return an implementation summary.",
99
+ ].join("\n"),
100
+ ]
101
+ .filter((section) => typeof section === "string" && section !== "")
102
+ .join("\n\n");
103
+ }
104
+ function buildAcceptedSimplificationFollowUpMessage(input) {
105
+ return buildAcceptedImplementationFollowUpMessage({
106
+ kind: "simplification",
107
+ implementationMarkdown: input.simplificationMarkdown,
108
+ adjustment: input.adjustment,
109
+ });
110
+ }
111
+ function patchRepositoryChangeState(state, input) {
112
+ const nextState = {
113
+ ...state,
114
+ ...("reviewSubject" in input ? { reviewSubject: input.reviewSubject ?? null } : {}),
115
+ ...("finalization" in input ? { finalization: input.finalization } : {}),
116
+ ...("semanticEntryRefs" in input ? { semanticEntryRefs: input.semanticEntryRefs } : {}),
117
+ };
118
+ const productRefsToClear = new Set(input.clearProductRefs ?? []);
119
+ if (input.clearReviewRefs) {
120
+ productRefsToClear.add(products.review);
121
+ }
122
+ if (!input.clearReviewRefs && productRefsToClear.size === 0) {
123
+ return nextState;
124
+ }
125
+ const productRefs = { ...nextState.productRefs };
126
+ for (const productName of productRefsToClear) {
127
+ delete productRefs[productName];
128
+ }
129
+ return {
130
+ ...nextState,
131
+ semanticEntryRefs: input.clearReviewRefs
132
+ ? clearReviewRefs(nextState.semanticEntryRefs)
133
+ : nextState.semanticEntryRefs,
134
+ productRefs,
135
+ };
136
+ }
137
+ const planDecisionSpec = humanTurn({
138
+ description: "Review plan",
139
+ reviewSubject: planReviewSubject,
140
+ reviewSemanticRef: "plan",
141
+ notesFields: [
142
+ {
143
+ id: "message",
144
+ label: "Revision notes",
145
+ placeholder: "Describe what the next plan revision should improve",
146
+ required: true,
147
+ },
148
+ ],
149
+ commentary: "Approve this plan, request a revision, or run an automated review.",
150
+ actions: {
151
+ [codingActionIds.approvePlan]: {
152
+ label: "Approve plan",
153
+ description: "Accept the current plan and continue into implementation.",
154
+ acceptanceState: "accepted",
155
+ to: turnIds.implement,
156
+ effect: ({ ctx }) => {
157
+ const plan = ctx.state.semanticEntryRefs.plan;
158
+ if (!hasResolvedTurnRecordRef(plan)) {
159
+ throw new Error("No plan is available for approval");
160
+ }
161
+ return {
162
+ state: patchRepositoryChangeState(ctx.state, {
163
+ reviewSubject: null,
164
+ clearReviewRefs: true,
165
+ clearProductRefs: [products.simplificationPlan],
166
+ }),
167
+ emit: [
168
+ {
169
+ type: "plan_approved",
170
+ data: {
171
+ externalId: ctx.process.externalId,
172
+ planRevision: ctx.process.planRevision,
173
+ },
174
+ },
175
+ ],
176
+ };
177
+ },
178
+ },
179
+ [codingActionIds.requestRevision]: revisionAction({
180
+ label: "Request revision",
181
+ description: "Send the plan back for another draft revision.",
182
+ acceptanceState: "requires_changes",
183
+ form: requestRevisionForm,
184
+ schedulable: true,
185
+ to: turnIds.generatePlan,
186
+ queueTarget: { semanticRef: "currentPrimaryPathLeaf" },
187
+ effect: ({ ctx, input }) => {
188
+ const message = normalizeMessage(input.message);
189
+ return {
190
+ state: patchRepositoryChangeState(ctx.state, {
191
+ reviewSubject: null,
192
+ clearReviewRefs: true,
193
+ clearProductRefs: [products.simplificationPlan],
194
+ }),
195
+ emit: [{ type: "plan_revision_requested", data: { message } }],
196
+ };
197
+ },
198
+ }),
199
+ [codingActionIds.runReview]: {
200
+ label: "Run automated review",
201
+ description: "Send the current plan to the LLM reviewer.",
202
+ acceptanceState: "neutral",
203
+ schedulable: true,
204
+ to: turnIds.reviewPlan,
205
+ effect: ({ ctx }) => {
206
+ const plan = ctx.state.semanticEntryRefs.plan;
207
+ if (!hasResolvedTurnRecordRef(plan)) {
208
+ throw new Error("No plan is available for review");
209
+ }
210
+ return {
211
+ state: patchRepositoryChangeState(ctx.state, {
212
+ reviewSubject: planReviewSubject,
213
+ clearReviewRefs: true,
214
+ clearProductRefs: [products.simplificationPlan],
215
+ }),
216
+ };
217
+ },
218
+ },
219
+ },
220
+ });
221
+ const planReviewFeedbackSpec = humanTurn({
222
+ description: "Review plan feedback",
223
+ reviewSubject: planReviewSubject,
224
+ reviewSemanticRef: "review",
225
+ notesFields: [
226
+ {
227
+ id: "message",
228
+ label: "Review notes",
229
+ placeholder: "Describe how the next review pass should change",
230
+ required: true,
231
+ },
232
+ ],
233
+ commentary: "Apply this feedback to the next plan revision, request a revised review, or dismiss it.",
234
+ actions: {
235
+ [codingActionIds.acceptReview]: acceptedReviewHandoffAction({
236
+ label: "Accept review",
237
+ description: "Apply the current review outcome and continue with the plan flow.",
238
+ acceptanceState: "accepted",
239
+ form: acceptReviewForm,
240
+ to: turnIds.generatePlan,
241
+ resolveBodyMarkdown: ({ ctx, input }) => {
242
+ const reviewMarkdown = normalizeOptionalMarkdown(ctx.readSemanticTurnResultMarkdown("review"));
243
+ if (!reviewMarkdown) {
244
+ throw new Error("No review output markdown is available to accept");
245
+ }
246
+ return buildAcceptedReviewFollowUpMessage({
247
+ reviewSubject: planReviewSubject,
248
+ reviewMarkdown,
249
+ adjustment: normalizeMessage(input.message),
250
+ });
251
+ },
252
+ effect: ({ ctx }) => ({
253
+ state: patchRepositoryChangeState(ctx.state, {
254
+ reviewSubject: null,
255
+ }),
256
+ }),
257
+ }),
258
+ [codingActionIds.requestReviewChanges]: revisionAction({
259
+ label: "Request review changes",
260
+ description: "Ask the LLM reviewer to revise its plan review.",
261
+ acceptanceState: "requires_changes",
262
+ form: requestReviewChangesForm,
263
+ to: turnIds.reviewPlan,
264
+ queueTarget: { semanticRef: "review" },
265
+ effect: ({ ctx }) => ({
266
+ state: patchRepositoryChangeState(ctx.state, {
267
+ reviewSubject: planReviewSubject,
268
+ }),
269
+ }),
270
+ }),
271
+ [codingActionIds.dismissReview]: {
272
+ label: "Dismiss review",
273
+ description: "Return to the plan decision without applying the review.",
274
+ acceptanceState: "neutral",
275
+ to: turnIds.planDecision,
276
+ effect: ({ ctx }) => ({
277
+ state: patchRepositoryChangeState(ctx.state, {
278
+ reviewSubject: planReviewSubject,
279
+ }),
280
+ }),
281
+ },
282
+ },
283
+ });
284
+ const implementationDecisionSpec = humanTurn({
285
+ description: "Review implementation",
286
+ reviewSubject: implementationReviewSubject,
287
+ reviewSemanticRef: "currentPrimaryPathLeaf",
288
+ notesFields: [
289
+ {
290
+ id: "message",
291
+ label: "Revision notes",
292
+ placeholder: "Describe what the next implementation turn should improve",
293
+ required: true,
294
+ },
295
+ ],
296
+ commentary: "Merge this implementation, request another pass, simplify it, or run an automated review.",
297
+ actions: {
298
+ [codingActionIds.finalizeChange]: {
299
+ label: config.finalizeLabel,
300
+ acceptanceState: "accepted",
301
+ form: config.finalizeForm,
302
+ schedulable: true,
303
+ to: turnIds.generateCommitMessage,
304
+ effect: ({ ctx }) => ({
305
+ state: patchRepositoryChangeState(ctx.state, {
306
+ reviewSubject: null,
307
+ finalization: createEmptyRepositoryChangeFinalizationState(),
308
+ clearReviewRefs: true,
309
+ clearProductRefs: [products.simplificationPlan],
310
+ }),
311
+ }),
312
+ },
313
+ [codingActionIds.requestRevision]: revisionAction({
314
+ label: "Request revision",
315
+ acceptanceState: "requires_changes",
316
+ form: requestRevisionForm,
317
+ schedulable: true,
318
+ to: turnIds.implement,
319
+ queueTarget: { semanticRef: "currentPrimaryPathLeaf" },
320
+ effect: ({ ctx }) => ({
321
+ state: patchRepositoryChangeState(ctx.state, {
322
+ reviewSubject: null,
323
+ clearReviewRefs: true,
324
+ clearProductRefs: [products.simplificationPlan],
325
+ }),
326
+ }),
327
+ }),
328
+ [codingActionIds.simplify]: {
329
+ label: "Simplify",
330
+ description: "Ask for a focused simplification review before another implementation pass.",
331
+ acceptanceState: "neutral",
332
+ schedulable: true,
333
+ to: turnIds.simplifyImplementation,
334
+ effect: ({ ctx }) => {
335
+ requireResolvedPlanRef(ctx.state.semanticEntryRefs.plan, "simplification review");
336
+ return { state: buildImplementationReviewTurnState(ctx) };
337
+ },
338
+ },
339
+ [codingActionIds.runReview]: {
340
+ label: "Run automated review",
341
+ acceptanceState: "neutral",
342
+ schedulable: true,
343
+ to: turnIds.reviewImplementation,
344
+ effect: ({ ctx }) => {
345
+ requireResolvedPlanRef(ctx.state.semanticEntryRefs.plan, "implementation review");
346
+ return { state: buildImplementationReviewTurnState(ctx) };
347
+ },
348
+ },
349
+ },
350
+ });
351
+ const implementationReviewFeedbackSpec = humanTurn({
352
+ description: "Review implementation feedback",
353
+ reviewSubject: implementationReviewSubject,
354
+ reviewSemanticRef: "review",
355
+ notesFields: [
356
+ {
357
+ id: "message",
358
+ label: "Review notes",
359
+ placeholder: "Describe how the next implementation review pass should change",
360
+ required: true,
361
+ },
362
+ ],
363
+ commentary: "Apply this feedback to the next implementation pass, request a revised review, or dismiss it.",
364
+ actions: {
365
+ [codingActionIds.acceptReview]: acceptedReviewHandoffAction({
366
+ label: "Accept review",
367
+ acceptanceState: "accepted",
368
+ form: acceptReviewForm,
369
+ schedulable: true,
370
+ to: turnIds.implement,
371
+ queueTarget: { semanticRef: "review" },
372
+ resolveBodyMarkdown: ({ ctx, input }) => {
373
+ const reviewMarkdown = normalizeOptionalMarkdown(ctx.readSemanticTurnResultMarkdown("review"));
374
+ if (!reviewMarkdown) {
375
+ throw new Error("No review output markdown is available to accept");
376
+ }
377
+ return buildAcceptedReviewFollowUpMessage({
378
+ reviewSubject: implementationReviewSubject,
379
+ reviewMarkdown,
380
+ adjustment: normalizeMessage(input.message),
381
+ });
382
+ },
383
+ effect: ({ ctx }) => ({
384
+ state: patchRepositoryChangeState(ctx.state, {
385
+ reviewSubject: null,
386
+ clearProductRefs: [products.simplificationPlan],
387
+ }),
388
+ }),
389
+ }),
390
+ [codingActionIds.requestReviewChanges]: revisionAction({
391
+ label: "Request review changes",
392
+ acceptanceState: "requires_changes",
393
+ form: requestReviewChangesForm,
394
+ schedulable: true,
395
+ to: turnIds.reviewImplementation,
396
+ queueTarget: { semanticRef: "review" },
397
+ effect: ({ ctx }) => ({
398
+ state: patchRepositoryChangeState(ctx.state, {
399
+ reviewSubject: implementationReviewSubject,
400
+ }),
401
+ }),
402
+ }),
403
+ [codingActionIds.dismissReview]: {
404
+ label: "Dismiss review",
405
+ acceptanceState: "neutral",
406
+ to: turnIds.implementationDecision,
407
+ effect: ({ ctx }) => ({
408
+ state: patchRepositoryChangeState(ctx.state, {
409
+ reviewSubject: implementationReviewSubject,
410
+ }),
411
+ }),
412
+ },
413
+ },
414
+ });
415
+ const simplificationDecisionSpec = humanTurn({
416
+ description: "Review simplification plan",
417
+ reviewSubject: implementationReviewSubject,
418
+ reviewProduct: products.simplificationPlan,
419
+ notesFields: [
420
+ {
421
+ id: "message",
422
+ label: "Simplification notes",
423
+ placeholder: "Describe how the simplification plan should change",
424
+ required: true,
425
+ },
426
+ ],
427
+ commentary: "Apply this simplification plan, request changes to it, or reject it.",
428
+ actions: {
429
+ [codingActionIds.acceptReview]: acceptedReviewHandoffAction({
430
+ label: "Accept simplification plan",
431
+ acceptanceState: "accepted",
432
+ form: acceptReviewForm,
433
+ schedulable: true,
434
+ to: turnIds.implement,
435
+ queueTarget: { productName: products.simplificationPlan },
436
+ resolveBodyMarkdown: ({ ctx, input }) => {
437
+ const simplificationMarkdown = normalizeOptionalMarkdown(ctx.readProductTurnResultMarkdown(products.simplificationPlan));
438
+ if (!simplificationMarkdown) {
439
+ throw new Error("No simplification plan markdown is available to accept");
440
+ }
441
+ return buildAcceptedSimplificationFollowUpMessage({
442
+ simplificationMarkdown,
443
+ adjustment: normalizeMessage(input.message),
444
+ });
445
+ },
446
+ effect: ({ ctx }) => ({
447
+ state: patchRepositoryChangeState(ctx.state, {
448
+ reviewSubject: null,
449
+ clearReviewRefs: true,
450
+ }),
451
+ }),
452
+ }),
453
+ [codingActionIds.requestReviewChanges]: revisionAction({
454
+ label: "Request simplification changes",
455
+ acceptanceState: "requires_changes",
456
+ form: requestReviewChangesForm,
457
+ to: turnIds.simplifyImplementation,
458
+ queueTarget: { productName: products.simplificationPlan },
459
+ effect: ({ ctx }) => ({
460
+ state: patchRepositoryChangeState(ctx.state, {
461
+ reviewSubject: implementationReviewSubject,
462
+ }),
463
+ }),
464
+ }),
465
+ [codingActionIds.dismissReview]: {
466
+ label: "Reject simplification plan",
467
+ description: "Return to the implementation decision without applying the simplification plan.",
468
+ acceptanceState: "neutral",
469
+ to: turnIds.implementationDecision,
470
+ effect: ({ ctx }) => ({
471
+ state: patchRepositoryChangeState(ctx.state, {
472
+ reviewSubject: implementationReviewSubject,
473
+ clearReviewRefs: true,
474
+ clearProductRefs: [products.simplificationPlan],
475
+ }),
476
+ }),
477
+ },
478
+ },
479
+ });
480
+ const importPlanTurn = flow
481
+ .serverAutomatic(turnIds.importPlan)
482
+ .description("Import handoff plan")
483
+ .run((ctx) => {
484
+ const planMarkdown = ctx.params.launchKind === "imported_plan"
485
+ ? normalizeOptionalMarkdown(ctx.params.importedPlanMarkdown)
486
+ : null;
487
+ if (!planMarkdown) {
488
+ throw new Error("No importedPlanMarkdown is available to import");
489
+ }
490
+ return {
491
+ outcome: "imported",
492
+ params: { plan: planMarkdown },
493
+ markdown: planMarkdown,
494
+ state: patchRepositoryChangeState(ctx.state, {
495
+ reviewSubject: null,
496
+ clearReviewRefs: true,
497
+ clearProductRefs: [products.simplificationPlan],
498
+ }),
499
+ };
500
+ })
501
+ .outcome("imported", (outcome) => outcome
502
+ .description("Imported a handoff plan and skipped replanning")
503
+ .markdown("plan", { description: "Imported handoff plan", publish: true })
504
+ .to(turnIds.implement)
505
+ .effect(({ ctx }) => ({ processPatch: { planRevision: ctx.process.planRevision + 1 } })));
506
+ const generatePlanTurn = flow
507
+ .llm(turnIds.generatePlan)
508
+ .description("Draft plan")
509
+ .tools("read", "bash")
510
+ .askQuestions()
511
+ .freshPrimary()
512
+ .buildPrompt(buildGeneratePlanPrompt)
513
+ .outcomeTool("plan_saved", (tool) => tool
514
+ .description("Save the finished plan for review. Its Markdown may include Mermaid diagrams or uploaded images.")
515
+ .requiredString("summary", "Short summary of the proposed plan")
516
+ .requiredStringArray("acceptanceCriteria", "Acceptance criteria for the requested repository change")
517
+ .to(turnIds.planDecision)
518
+ .effect(({ ctx, event }) => {
519
+ const planRevision = ctx.process.planRevision + 1;
520
+ const summary = normalizeMessage(event.params.summary);
521
+ const acceptanceCriteria = Array.isArray(event.params.acceptanceCriteria)
522
+ ? event.params.acceptanceCriteria.filter((entry) => typeof entry === "string" && entry.trim() !== "")
523
+ : [];
524
+ const planMarkdown = ctx.output?.content ?? "";
525
+ return {
526
+ state: patchRepositoryChangeState(ctx.state, {
527
+ reviewSubject: planReviewSubject,
528
+ clearReviewRefs: true,
529
+ clearProductRefs: [products.simplificationPlan],
530
+ }),
531
+ processPatch: { planRevision },
532
+ broadcasts: [
533
+ {
534
+ type: "plan.updated",
535
+ payload: {
536
+ planRevision,
537
+ reviewState: "awaiting_approval",
538
+ approved: false,
539
+ summary,
540
+ },
541
+ },
542
+ ],
543
+ emit: [
544
+ {
545
+ type: "plan_saved",
546
+ data: {
547
+ planRevision,
548
+ summary,
549
+ planMarkdown,
550
+ acceptanceCriteria,
551
+ },
552
+ },
553
+ ],
554
+ };
555
+ }))
556
+ .publish(products.plan);
557
+ const reviewPlanTurn = flow
558
+ .llm(turnIds.reviewPlan)
559
+ .description("Review plan")
560
+ .tools("read", "bash")
561
+ .rootBranchReview()
562
+ .startFromReviewBranch()
563
+ .reviews(planReviewSubject)
564
+ .consume(products.plan)
565
+ .buildPrompt(buildReviewPlanPrompt)
566
+ .outcomeTool("no_issues", (tool) => tool
567
+ .description("Approve the plan and publish the review. Its Markdown may include Mermaid diagrams or uploaded images.")
568
+ .to(turnIds.planDecision)
569
+ .state(({ ctx }) => patchRepositoryChangeState(ctx.state, {
570
+ reviewSubject: planReviewSubject,
571
+ })))
572
+ .outcomeTool("request_changes", (tool) => tool
573
+ .description("Request plan changes and publish actionable feedback. Its Markdown may include Mermaid diagrams or uploaded images.")
574
+ .to(turnIds.planReviewFeedback)
575
+ .state(({ ctx }) => patchRepositoryChangeState(ctx.state, {
576
+ reviewSubject: planReviewSubject,
577
+ })))
578
+ .publish(products.review);
579
+ const implementTurn = flow
580
+ .llm(turnIds.implement)
581
+ .description("Implement change")
582
+ .tools(...implementationTurnAvailableTools)
583
+ .freshSeededPrimary()
584
+ .continueFromProductBranch(products.simplificationPlan, {
585
+ kind: "semantic_ref",
586
+ ref: "review",
587
+ fallback: { kind: "session_root" },
588
+ })
589
+ .consume(products.plan)
590
+ .buildPrompt(buildImplementPrompt)
591
+ .publish(products.implementationSummary)
592
+ .to(turnIds.implementationDecision)
593
+ .state(({ ctx }) => patchRepositoryChangeState(ctx.state, {
594
+ reviewSubject: implementationReviewSubject,
595
+ clearReviewRefs: true,
596
+ clearProductRefs: [products.simplificationPlan],
597
+ }));
598
+ const reviewImplementationTurn = flow
599
+ .llm(turnIds.reviewImplementation)
600
+ .description("Review implementation")
601
+ .tools("read", "bash")
602
+ .rootBranchReview()
603
+ .startFromReviewBranch()
604
+ .reviews(implementationReviewSubject)
605
+ .buildPrompt(buildReviewImplementationPrompt)
606
+ .outcomeTool("no_issues", (tool) => tool
607
+ .description("Approve the implementation and publish the review. Its Markdown may include Mermaid diagrams or uploaded images.")
608
+ .to(turnIds.implementationDecision)
609
+ .state(({ ctx }) => patchRepositoryChangeState(ctx.state, {
610
+ reviewSubject: implementationReviewSubject,
611
+ })))
612
+ .outcomeTool("request_changes", (tool) => tool
613
+ .description("Request implementation changes and publish actionable feedback. Its Markdown may include Mermaid diagrams or uploaded images.")
614
+ .to(turnIds.implementationReviewFeedback)
615
+ .state(({ ctx }) => patchRepositoryChangeState(ctx.state, {
616
+ reviewSubject: implementationReviewSubject,
617
+ })))
618
+ .publish(products.review);
619
+ const simplifyImplementationTurn = flow
620
+ .llm(turnIds.simplifyImplementation)
621
+ .description("Simplify implementation")
622
+ .tools("read", "bash")
623
+ .rootBranchReview()
624
+ .startFromProductBranch(products.simplificationPlan)
625
+ .buildPrompt(buildSimplifyImplementationPrompt)
626
+ .publish(products.simplificationPlan)
627
+ .to(turnIds.simplificationDecision)
628
+ .state(({ ctx }) => patchRepositoryChangeState(ctx.state, {
629
+ reviewSubject: implementationReviewSubject,
630
+ }));
631
+ const generateCommitMessageTurn = flow
632
+ .llm(turnIds.generateCommitMessage)
633
+ .description("Generate commit message")
634
+ .modelPurpose("process_title_generation")
635
+ .rootBranchReview()
636
+ .startFromRoot()
637
+ .consume(products.plan)
638
+ .buildPrompt(buildGenerateCommitMessagePrompt)
639
+ .publish(products.commitMessage)
640
+ .to(turnIds.commitAndMerge)
641
+ .state(({ ctx }) => patchRepositoryChangeState(ctx.state, {
642
+ finalization: {
643
+ ...ctx.state.finalization,
644
+ generatedCommitMessage: normalizeGeneratedCommitMessage(ctx.output?.content),
645
+ },
646
+ }));
647
+ const commitAndMergeTurn = flow
648
+ .automatic(turnIds.commitAndMerge)
649
+ .description(config.finalizationDescription)
650
+ .run(runDeterministicFinalization)
651
+ .outcome("merge_conflict", (outcome) => outcome
652
+ .description("Deterministic merge hit conflicts and needs conflict resolution")
653
+ .requiredString("headSha", "The current HEAD sha while the merge conflict is active")
654
+ .stringArray("conflictedFiles", "Conflicted files that need resolution")
655
+ .string("fetchedBaseSha", "The fetched origin/<baseBranch> sha when available")
656
+ .to(turnIds.resolveMergeConflict)
657
+ .state(({ ctx }) => patchRepositoryChangeState(ctx.state, {
658
+ finalization: resetFinalizationAfterMessage(ctx.state),
659
+ })))
660
+ .outcome("finalized", (outcome) => outcome
661
+ .description("Deterministic finalization succeeded")
662
+ .requiredString("headSha", "The final HEAD sha")
663
+ .requiredString("mergeMode", "How origin/<baseBranch> was integrated")
664
+ .requiredString("pushTarget", "The final base-branch publish target")
665
+ .requiredBoolean("usedConflictResolution", "Whether the merge required conflict resolution")
666
+ .complete()
667
+ .state(({ ctx, event }) => patchRepositoryChangeState(ctx.state, {
668
+ reviewSubject: null,
669
+ finalization: resetFinalizationAfterMessage(ctx.state, {
670
+ usedConflictResolution: event.params.usedConflictResolution === true ||
671
+ ctx.state.finalization.usedConflictResolution,
672
+ finalizationSummaryMarkdown: normalizeOptionalMarkdown(ctx.output?.content),
673
+ finalizedHeadSha: normalizeMessage(event.params.headSha),
674
+ }),
675
+ })));
676
+ const resolveMergeConflictTurn = flow
677
+ .llm(turnIds.resolveMergeConflict)
678
+ .description("Resolve merge conflict")
679
+ .tools("read", "bash", "edit", "write")
680
+ .fullPrimary()
681
+ .startFromRoot()
682
+ .buildPrompt(buildResolveMergeConflictPrompt)
683
+ .outcomeTool("clean", (tool) => tool
684
+ .description("The merge conflict was resolved and the repository is clean")
685
+ .requiredString("headSha", "The post-resolution HEAD sha")
686
+ .to(turnIds.commitAndMerge)
687
+ .state(({ ctx, event }) => patchRepositoryChangeState(ctx.state, {
688
+ finalization: resetFinalizationAfterMessage(ctx.state, {
689
+ expectedPostConflictHeadSha: normalizeMessage(event.params.headSha),
690
+ usedConflictResolution: true,
691
+ }),
692
+ })))
693
+ .publish(products.mergeResolutionSummary);
694
+ const planFlow = flow
695
+ .fragment("plan")
696
+ .turn(importPlanTurn)
697
+ .turn(generatePlanTurn)
698
+ .turn({ id: turnIds.planDecision, definition: planDecisionSpec })
699
+ .turn(reviewPlanTurn)
700
+ .turn({ id: turnIds.planReviewFeedback, definition: planReviewFeedbackSpec });
701
+ const implementationFlow = flow
702
+ .fragment("implementation")
703
+ .turn(implementTurn)
704
+ .turn({ id: turnIds.implementationDecision, definition: implementationDecisionSpec })
705
+ .turn(reviewImplementationTurn)
706
+ .turn({
707
+ id: turnIds.implementationReviewFeedback,
708
+ definition: implementationReviewFeedbackSpec,
709
+ })
710
+ .turn(simplifyImplementationTurn)
711
+ .turn({ id: turnIds.simplificationDecision, definition: simplificationDecisionSpec });
712
+ const finalizationFlow = flow
713
+ .fragment("finalization")
714
+ .turn(generateCommitMessageTurn)
715
+ .turn(commitAndMergeTurn)
716
+ .turn(resolveMergeConflictTurn);
717
+ const builder = flow
718
+ .process(config.processId)
719
+ .displayName(config.displayName)
720
+ .entry(turnIds.generatePlan)
721
+ .alternateEntry(turnIds.importPlan)
722
+ .happyPath(turnIds.generatePlan, turnIds.implement, turnIds.generateCommitMessage, turnIds.commitAndMerge)
723
+ .piConfig({ sessionCwdTemplate: "{{{projectKey}}}" })
724
+ .codecs({
725
+ params: config.paramsCodec,
726
+ state: repositoryChangeStateCodec,
727
+ })
728
+ .initialState(() => ({
729
+ ...createEmptyStructuralProcessState(),
730
+ finalization: createEmptyRepositoryChangeFinalizationState(),
731
+ }))
732
+ .use(planFlow)
733
+ .use(implementationFlow)
734
+ .use(finalizationFlow);
735
+ if (config.repositoryCredentials)
736
+ builder.repositoryCredentials(config.repositoryCredentials);
737
+ const process = builder.launcher(config.launcher).define();
738
+ const decision = (id) => ({
739
+ id,
740
+ ...process.turns.get(id)?.definition,
741
+ });
742
+ return {
743
+ process,
744
+ planDecision: decision(turnIds.planDecision),
745
+ planReviewFeedback: decision(turnIds.planReviewFeedback),
746
+ implementationDecision: decision(turnIds.implementationDecision),
747
+ implementationReviewFeedback: decision(turnIds.implementationReviewFeedback),
748
+ simplificationDecision: decision(turnIds.simplificationDecision),
749
+ };
750
+ }
751
+ //# sourceMappingURL=repository-change-process.js.map