@kitman-sh/software-engineering 0.13.7 → 0.13.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.
package/src/plan.ts CHANGED
@@ -17,15 +17,23 @@ import {
17
17
  PlanApproval,
18
18
  PlanDeclined,
19
19
  type PlanReview,
20
- PlanTickets,
20
+ PlanTicket,
21
+ planTicketsOf,
21
22
  readPlanReview,
22
23
  planWaves,
23
- type IntegrationBranch,
24
- type PlanTicket
24
+ type IntegrationBranch
25
25
  } from '@kitman-sh/sdk/loop'
26
26
  import * as Effect from 'effect/Effect'
27
27
  import * as Schema from 'effect/Schema'
28
28
  import build from './build'
29
+ import {
30
+ DeclinedConcern,
31
+ disputedText,
32
+ outOfScopeText,
33
+ reviewPlan,
34
+ ticketText,
35
+ type DisputedConcern
36
+ } from './plan-review'
29
37
 
30
38
  const Input = Schema.Struct({
31
39
  /** What to plan. */
@@ -40,11 +48,19 @@ const Input = Schema.Struct({
40
48
  implement: Schema.optional(Schema.Boolean)
41
49
  })
42
50
 
51
+ const ScopedTickets = planTicketsOf(
52
+ Schema.Struct({
53
+ ...PlanTicket.fields,
54
+ promise: Schema.String.check(Schema.isPattern(/\S/)),
55
+ outOfScope: Schema.Array(Schema.String)
56
+ })
57
+ )
58
+
43
59
  const Plan = Schema.Struct({
44
60
  title: Schema.String,
45
61
  /** The spec, as Markdown. */
46
62
  spec: Schema.String,
47
- tickets: PlanTickets,
63
+ tickets: ScopedTickets,
48
64
  /** What the plan leaves out; each becomes a constraint on the Builds of its tickets. */
49
65
  outOfScope: Schema.Array(Schema.String),
50
66
  /** Outcomes only visible after merge (adoption, a metric moving); never a ticket's criterion. */
@@ -62,6 +78,18 @@ const Revised = Schema.Struct({
62
78
  declined: Schema.Array(PlanDeclined)
63
79
  })
64
80
 
81
+ const { docs: _docs, ...reworkedPlan } = Plan.fields
82
+
83
+ /** A revision the planner wrote from a plan review. */
84
+ const Reworked = Schema.Struct({ ...reworkedPlan, declined: Schema.Array(DeclinedConcern) })
85
+
86
+ /** A revision the planner wrote from a plan review, of a plan that answers a person's review. */
87
+ const ReworkedAnswer = Schema.Struct({
88
+ ...Reworked.fields,
89
+ /** The person's comments this revision answers and those it does not, in place of the earlier ones. */
90
+ comments: Schema.Struct({ addressed: Schema.Array(PlanAddressed), declined: Schema.Array(PlanDeclined) })
91
+ })
92
+
65
93
  /** The criteria a ticket names that the base branch already meets. */
66
94
  const CriteriaCheck = Schema.Struct({
67
95
  alreadyMet: Schema.Array(Schema.Struct({ key: Schema.String, criterion: Schema.String }))
@@ -69,8 +97,6 @@ const CriteriaCheck = Schema.Struct({
69
97
 
70
98
  const Rejected = Schema.Struct({ key: Schema.String, reason: Schema.String })
71
99
 
72
- const Opinion = Schema.Struct({ summary: Schema.String, concerns: Schema.Array(Schema.String) })
73
-
74
100
  const TicketResult = Schema.Struct({
75
101
  key: Schema.String,
76
102
  title: Schema.String,
@@ -100,19 +126,16 @@ const Output = Schema.Struct({
100
126
  docs: Schema.optional(ChangeRequestResult)
101
127
  })
102
128
 
103
- const menu = ['Implement now', 'File as Tasks', 'Second opinion', 'Refine', 'Finish'] as const
129
+ const menu = ['Implement now', 'File as Tasks', 'Refine', 'Finish'] as const
104
130
 
105
131
  type Choice = (typeof menu)[number]
106
132
 
107
133
  /** Menu rounds before the plan is filed as Tasks for a person to pick up. */
108
134
  const maxRounds = 6
109
135
 
110
- const maxOpinionsPerPlan = 2
111
-
112
136
  const menuDescriptions: Record<Choice, string> = {
113
137
  'Implement now': 'Builds the tickets in dependency order and delivers them.',
114
138
  'File as Tasks': 'Makes a Task of each ticket, to build later.',
115
- 'Second opinion': 'Another Agent reviews the plan.',
116
139
  Refine: 'You say what to change and the planner rewrites it.',
117
140
  Finish: 'Ends the Run with the plan as it is.'
118
141
  }
@@ -127,6 +150,7 @@ const onFailure = ['Retry with a steer', 'Skip it and its dependents', 'Stop her
127
150
 
128
151
  const ticketRules = [
129
152
  'Each ticket is one Build: a vertical slice an agent can implement, review and deliver as one ChangeRequest. Give it a short unique key, and list in blockedBy the keys of the tickets whose code it needs; tickets never block each other in a cycle. Set repoBindingId only when it changes another repository than the plan, keep one repository per ticket, and no cycle across repositories. Set outcome to the kind of change: feature, bug, refactor, docs, migration, investigation or performance.',
153
+ 'Give each ticket its promise: one sentence that says what is true once it is done. List in its outOfScope the cases it does not handle. Two promises are two tickets.',
130
154
  "Each acceptance criterion is something a Build can show before its ChangeRequest: a behaviour a test or a check proves. A text search is never a criterion, nor is something the code already does. Outcomes only visible after merge go under afterMerge, not under a ticket's criteria."
131
155
  ].join('\n\n')
132
156
 
@@ -150,27 +174,12 @@ const planText = (plan: Plan) =>
150
174
  ...plan.tickets.map(ticketText)
151
175
  ].join('\n\n')
152
176
 
153
- const outOfScopeText = ({ outOfScope = [] }: PlanTicket) =>
154
- outOfScope.length === 0 ? [] : ['Out of scope:', ...outOfScope.map(item => `- ${item}`)]
177
+ /** The plan as a person reads it, with what the plan reviewer still disputes. */
178
+ const documentText = (plan: Plan, disputed: readonly DisputedConcern[]) =>
179
+ [planText(plan), ...disputedText(disputed)].join('\n\n')
155
180
 
156
- const ticketText = (ticket: PlanTicket) =>
157
- [
158
- `### ${ticket.key}: ${ticket.title}`,
159
- ticket.body,
160
- ...(ticket.promise === undefined ? [] : [`Promise: ${ticket.promise}`]),
161
- ...ticket.acceptanceCriteria.map(criterion => `- [ ] ${criterion}`),
162
- ...outOfScopeText(ticket),
163
- ...(ticket.blockedBy.length === 0 ? [] : [`Blocked by: ${ticket.blockedBy.join(', ')}`])
164
- ].join('\n\n')
165
-
166
- const opinionPrompt = (plan: Plan) =>
167
- [
168
- 'Review this plan as a second opinion. Look for missing scope, tickets too big to review, wrong blockers and risks. Change nothing.',
169
- planText(plan)
170
- ].join('\n\n')
171
-
172
- const refinePrompt = (plan: Plan, words: string) =>
173
- [`Change the plan as the user asks: ${words}`, ticketRules, planText(plan)].join('\n\n')
181
+ const refinePrompt = (document: string, words: string) =>
182
+ [`Change the plan as the user asks: ${words}`, ticketRules, document].join('\n\n')
174
183
 
175
184
  const commentText = (comment: PlanReview['comments'][number]) => {
176
185
  const on =
@@ -181,12 +190,14 @@ const commentText = (comment: PlanReview['comments'][number]) => {
181
190
  return `${comment.id}, ${on}\n${comment.body}`
182
191
  }
183
192
 
184
- const revisePrompt = (plan: Plan, review: PlanReview) =>
193
+ const commentsText = (review: PlanReview) => review.comments.map(commentText).join('\n\n')
194
+
195
+ const revisePrompt = (document: string, review: PlanReview) =>
185
196
  [
186
197
  `A person reviewed revision ${review.revision} of the plan and requested changes. Each comment names its id, then the text it quotes and the line of the plan's Markdown below it is on, then what the person said. Revise the plan to answer each comment; list in addressed the id of each comment the new revision answers and how, in one sentence. List in declined the id of each comment you did not act on and why, in one sentence; keep that out of the plan itself.`,
187
- review.comments.map(commentText).join('\n\n'),
198
+ commentsText(review),
188
199
  ticketRules,
189
- planText(plan)
200
+ document
190
201
  ].join('\n\n')
191
202
 
192
203
  const criteriaPrompt = (plan: Plan) =>
@@ -230,13 +241,6 @@ const guarded = (plan: Plan, check: typeof CriteriaCheck.Type) => {
230
241
  return { plan: { ...plan, tickets }, rejected }
231
242
  }
232
243
 
233
- /** A plan revision whose criteria were checked against the base: a ticket with nothing left to do is rejected. */
234
- const checked = (plan: Plan, revision: number) =>
235
- Effect.map(
236
- agentWork(`criteria-check:${revision}`, { agent: 'reviewer', prompt: criteriaPrompt(plan), output: CriteriaCheck }),
237
- check => guarded(plan, check)
238
- )
239
-
240
244
  /** The review a revision answers, and which of its comments the planner says it addressed or declined. */
241
245
  type Answered = {
242
246
  readonly review: PlanReview
@@ -244,11 +248,71 @@ type Answered = {
244
248
  readonly declined: readonly PlanDeclined[]
245
249
  }
246
250
 
251
+ type Draft = { readonly plan: Plan; readonly answered?: Answered | undefined }
252
+
253
+ const answeredText = ({ addressed, declined }: Answered) =>
254
+ [
255
+ "The plan answers a person's review, whose comments are in what the person asked for. This is what the plan above did about each. Give comments the same for your revision: list in its addressed the id of each comment your revision answers and how, and in its declined the id of each it does not act on and why, each in one sentence.",
256
+ [
257
+ ...addressed.map(({ comment, how }) => `Comment ${comment}, addressed: ${how}`),
258
+ ...declined.map(({ comment, why }) => `Comment ${comment}, declined: ${why}`)
259
+ ].join('\n')
260
+ ].join('\n\n')
261
+
262
+ const rework = (step: string, prompt: string, answered: Answered | undefined) =>
263
+ answered === undefined
264
+ ? agentWorkOnce(step, { agent: 'planner', prompt: [prompt, ticketRules].join('\n\n'), output: Reworked }).pipe(
265
+ Effect.map(({ declined, ...plan }) => ({ plan: { plan }, declined }))
266
+ )
267
+ : agentWorkOnce(step, {
268
+ agent: 'planner',
269
+ prompt: [prompt, ticketRules, answeredText(answered)].join('\n\n'),
270
+ output: ReworkedAnswer
271
+ }).pipe(
272
+ Effect.map(({ declined, comments, ...plan }) => ({
273
+ plan: { plan, answered: { review: answered.review, ...comments } },
274
+ declined
275
+ }))
276
+ )
277
+
278
+ /**
279
+ * A plan revision ready for a person: the plan reviewer checked it, the planner answered its concerns, and its criteria
280
+ * were checked against the base, which rejects a ticket with nothing left to do.
281
+ */
282
+ const checked = (draft: Draft, revision: number, asked: readonly string[]) =>
283
+ Effect.gen(function* () {
284
+ const reviewed = yield* reviewPlan(`plan-review:${revision}`, {
285
+ plan: draft,
286
+ asked: asked.join('\n\n'),
287
+ text: ({ plan }) => planText(plan),
288
+ revise: (step, prompt) => rework(step, prompt, draft.answered)
289
+ })
290
+
291
+ const check = yield* agentWork(`criteria-check:${revision}`, {
292
+ agent: 'reviewer',
293
+ prompt: criteriaPrompt(reviewed.plan.plan),
294
+ output: CriteriaCheck
295
+ })
296
+
297
+ const { plan, rejected } = guarded(reviewed.plan.plan, check)
298
+
299
+ return {
300
+ plan,
301
+ rejected,
302
+ answered: reviewed.plan.answered,
303
+ disputed: reviewed.disputed.filter(entry => !rejected.some(({ key }) => key === entry.ticket))
304
+ }
305
+ })
306
+
247
307
  /**
248
308
  * A revision of the plan, as Run artifacts later Runs retrieve, and as the whole document a person reviews with the
249
309
  * review it answers.
250
310
  */
251
- const storePlan = (plan: Plan, suffix: string, revision: number, answered?: Answered) =>
311
+ const storePlan = (
312
+ { plan, disputed, answered }: Draft & { readonly disputed: readonly DisputedConcern[] },
313
+ suffix: string,
314
+ revision: number
315
+ ) =>
252
316
  Effect.gen(function* () {
253
317
  yield* artifact(`spec${suffix}`, {
254
318
  kind: 'plan_spec',
@@ -260,12 +324,11 @@ const storePlan = (plan: Plan, suffix: string, revision: number, answered?: Answ
260
324
  kind: 'plan_tickets',
261
325
  title: `Tickets for ${plan.title}`,
262
326
  text: plan.tickets.map(ticketText).join('\n\n'),
263
- data: plan.tickets.map(({ repoBindingId, outcome, promise, outOfScope, ...ticket }) => ({
327
+ data: plan.tickets.map(({ repoBindingId, outcome, ...ticket }) => ({
264
328
  ...ticket,
265
329
  acceptanceCriteria: [...ticket.acceptanceCriteria],
266
330
  blockedBy: [...ticket.blockedBy],
267
- ...(promise === undefined ? undefined : { promise }),
268
- ...(outOfScope === undefined ? undefined : { outOfScope: [...outOfScope] }),
331
+ outOfScope: [...ticket.outOfScope],
269
332
  ...(repoBindingId === undefined ? undefined : { repoBindingId }),
270
333
  ...(outcome === undefined ? undefined : { outcome })
271
334
  }))
@@ -273,7 +336,7 @@ const storePlan = (plan: Plan, suffix: string, revision: number, answered?: Answ
273
336
  yield* artifact(`document${suffix}`, {
274
337
  kind: 'plan_document',
275
338
  title: plan.title,
276
- text: planText(plan),
339
+ text: documentText(plan, disputed),
277
340
  data: {
278
341
  revision,
279
342
  ...(answered === undefined
@@ -606,20 +669,20 @@ const implement = (input: typeof Input.Type, plan: Plan, revision: number, filed
606
669
 
607
670
  /**
608
671
  * Plans from the goal and the code: the planner runs grill-with-docs, asking only the decisions that change the
609
- * tickets, and Plan opens a docs ChangeRequest for the ADR and glossary changes it left. Plan checks each ticket's
610
- * criteria against the base, stores the spec and tickets as Run artifacts, and waits for a person to approve each
611
- * revision, the planner revising it from their review until they do. Then it asks what next, at most six times: build
612
- * the tickets, file them as Tasks, ask another Agent's opinion (twice at most), refine the plan from the user's words,
613
- * or finish (ADR-0051). Nobody answering the menu files it as Tasks; started with `implement`, nobody answering
614
- * approves each revision and implements it.
672
+ * tickets, and Plan opens a docs ChangeRequest for the ADR and glossary changes it left. A plan reviewer checks each
673
+ * revision, and the planner answers its concerns once. Plan then checks each ticket's criteria against the base, stores
674
+ * the spec and tickets as Run artifacts, and waits for a person to approve the revision, the planner revising it from
675
+ * their review until they do. Then it asks what next, at most six times: build the tickets, file them as Tasks, refine
676
+ * the plan from the user's words, or finish (ADR-0051). Nobody answering the menu files it as Tasks; started with
677
+ * `implement`, nobody answering approves each revision and implements it.
615
678
  */
616
679
  export default defineLoop({
617
680
  id: 'plan',
618
- version: '2.2.0',
681
+ version: '3.0.0',
619
682
  title: 'Plan',
620
683
  input: Input,
621
684
  output: Output,
622
- agents: { planner: 'planner', reviewer: 'reviewer' },
685
+ agents: { planner: 'planner', reviewer: 'reviewer', 'plan-reviewer': 'plan-reviewer' },
623
686
  checkout: 'write',
624
687
  // A Child Run gets no more than its parent was granted, so this covers what each ticket's Build needs too.
625
688
  capabilities: [
@@ -650,11 +713,11 @@ export default defineLoop({
650
713
  : yield* changeRequest('docs', { title: drafted.docs.title, body: drafted.docs.summary })
651
714
 
652
715
  let revision = 1
653
- let { plan, rejected } = yield* checked(drafted, revision)
716
+ const asked = [input.words]
717
+ let { plan, rejected, disputed } = yield* checked({ plan: drafted }, revision, asked)
654
718
  let filed: Filed | undefined
655
- let opinions = 0
656
719
 
657
- yield* storePlan(plan, '', revision)
720
+ yield* storePlan({ plan, disputed }, '', revision)
658
721
 
659
722
  const project = input.projectId === undefined ? undefined : { projectId: input.projectId }
660
723
 
@@ -698,7 +761,14 @@ export default defineLoop({
698
761
  Effect.gen(function* () {
699
762
  for (;;) {
700
763
  const answer = yield* ask(`review:${revision}`, {
701
- prompt: `Review revision ${revision} of the plan "${plan.title}": approve it, or request changes by commenting on what should change.`,
764
+ prompt: [
765
+ `Review revision ${revision} of the plan "${plan.title}": approve it, or request changes by commenting on what should change.`,
766
+ ...(disputed.length === 0
767
+ ? []
768
+ : [
769
+ `The plan reviewer still disputes ${disputed.length} point${disputed.length === 1 ? '' : 's'}, listed at the end of the plan.`
770
+ ])
771
+ ].join(' '),
702
772
  freeText: true,
703
773
  recommended: PlanApproval,
704
774
  recommendationReason: 'Approve it once the plan reads right; until then the Run waits for you.',
@@ -708,11 +778,11 @@ export default defineLoop({
708
778
 
709
779
  if (isPlanApproval(answer)) return
710
780
 
711
- const review = readPlanReview(answer, revision, planText(plan))
781
+ const review = readPlanReview(answer, revision, documentText(plan, disputed))
712
782
 
713
783
  const revised = yield* agentWorkOnce(`revise-plan:${revision}`, {
714
784
  agent: 'planner',
715
- prompt: revisePrompt(plan, review),
785
+ prompt: revisePrompt(documentText(plan, disputed), review),
716
786
  output: Revised
717
787
  })
718
788
 
@@ -721,8 +791,12 @@ export default defineLoop({
721
791
  const count = review.comments.length
722
792
 
723
793
  revision++
724
- ;({ plan, rejected } = yield* checked(next, revision))
725
- yield* storePlan(plan, `:review-${revision}`, revision, { review, addressed, declined })
794
+ asked.push(commentsText(review))
795
+
796
+ const ready = yield* checked({ plan: next, answered: { review, addressed, declined } }, revision, asked)
797
+
798
+ ;({ plan, rejected, disputed } = ready)
799
+ yield* storePlan(ready, `:review-${revision}`, revision)
726
800
  yield* artifact(`changes:review-${revision}`, {
727
801
  kind: 'plan_changes',
728
802
  title: `Revision ${revision} of ${plan.title}`,
@@ -735,11 +809,7 @@ export default defineLoop({
735
809
  for (let round = 1; round <= maxRounds; round++) {
736
810
  const isFiled = filed?.revision === revision
737
811
 
738
- const options = menu.filter(
739
- option =>
740
- !(option === 'File as Tasks' && isFiled) &&
741
- !(option === 'Second opinion' && opinions >= maxOpinionsPerPlan)
742
- )
812
+ const options = menu.filter(option => !(option === 'File as Tasks' && isFiled))
743
813
 
744
814
  const fallback: Choice = input.implement === true ? 'Implement now' : isFiled ? 'Finish' : 'File as Tasks'
745
815
 
@@ -777,24 +847,6 @@ export default defineLoop({
777
847
  yield* file(`file-tasks:${round}`)
778
848
  break
779
849
 
780
- case 'Second opinion': {
781
- opinions++
782
-
783
- const opinion = yield* agentWork(`second-opinion:${round}`, {
784
- agent: 'reviewer',
785
- prompt: opinionPrompt(plan),
786
- output: Opinion
787
- })
788
-
789
- yield* artifact(`opinion:${round}`, {
790
- kind: 'plan_review',
791
- title: `Second opinion on ${plan.title}`,
792
- text: [opinion.summary, ...opinion.concerns.map(concern => `- ${concern}`)].join('\n\n')
793
- })
794
-
795
- break
796
- }
797
-
798
850
  case 'Refine': {
799
851
  const words = yield* ask(`refine:${round}`, {
800
852
  prompt: 'What should change in the plan?',
@@ -805,15 +857,16 @@ export default defineLoop({
805
857
 
806
858
  const refined = yield* agentWorkOnce(`refine-plan:${round}`, {
807
859
  agent: 'planner',
808
- prompt: refinePrompt(plan, words),
860
+ prompt: refinePrompt(documentText(plan, disputed), words),
809
861
  output: Plan
810
862
  })
811
863
 
812
864
  const before = plan
813
865
 
814
866
  revision++
815
- ;({ plan, rejected } = yield* checked(refined, revision))
816
- yield* storePlan(plan, `:${round}`, revision)
867
+ asked.push(words)
868
+ ;({ plan, rejected, disputed } = yield* checked({ plan: refined }, revision, asked))
869
+ yield* storePlan({ plan, disputed }, `:${round}`, revision)
817
870
  yield* artifact(`changes:${round}`, {
818
871
  kind: 'plan_changes',
819
872
  title: `Revision ${revision} of ${plan.title}`,