@kitman-sh/software-engineering 0.13.8 → 0.13.10

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
+ type PlanTicket,
21
21
  readPlanReview,
22
22
  planWaves,
23
- type IntegrationBranch,
24
- type PlanTicket
23
+ type IntegrationBranch
25
24
  } from '@kitman-sh/sdk/loop'
26
25
  import * as Effect from 'effect/Effect'
27
26
  import * as Schema from 'effect/Schema'
28
27
  import build from './build'
28
+ import {
29
+ DeclinedConcern,
30
+ disputedText,
31
+ outOfScopeText,
32
+ reviewPlan,
33
+ ScopedTickets,
34
+ ticketText,
35
+ type DisputedConcern
36
+ } from './plan-review'
29
37
 
30
38
  const Input = Schema.Struct({
31
39
  /** What to plan. */
@@ -44,7 +52,7 @@ const Plan = Schema.Struct({
44
52
  title: Schema.String,
45
53
  /** The spec, as Markdown. */
46
54
  spec: Schema.String,
47
- tickets: PlanTickets,
55
+ tickets: ScopedTickets,
48
56
  /** What the plan leaves out; each becomes a constraint on the Builds of its tickets. */
49
57
  outOfScope: Schema.Array(Schema.String),
50
58
  /** Outcomes only visible after merge (adoption, a metric moving); never a ticket's criterion. */
@@ -62,6 +70,18 @@ const Revised = Schema.Struct({
62
70
  declined: Schema.Array(PlanDeclined)
63
71
  })
64
72
 
73
+ const { docs: _docs, ...reworkedPlan } = Plan.fields
74
+
75
+ /** A revision the planner wrote from a plan review. */
76
+ const Reworked = Schema.Struct({ ...reworkedPlan, declined: Schema.Array(DeclinedConcern) })
77
+
78
+ /** A revision the planner wrote from a plan review, of a plan that answers a person's review. */
79
+ const ReworkedAnswer = Schema.Struct({
80
+ ...Reworked.fields,
81
+ /** The person's comments this revision answers and those it does not, in place of the earlier ones. */
82
+ comments: Schema.Struct({ addressed: Schema.Array(PlanAddressed), declined: Schema.Array(PlanDeclined) })
83
+ })
84
+
65
85
  /** The criteria a ticket names that the base branch already meets. */
66
86
  const CriteriaCheck = Schema.Struct({
67
87
  alreadyMet: Schema.Array(Schema.Struct({ key: Schema.String, criterion: Schema.String }))
@@ -69,8 +89,6 @@ const CriteriaCheck = Schema.Struct({
69
89
 
70
90
  const Rejected = Schema.Struct({ key: Schema.String, reason: Schema.String })
71
91
 
72
- const Opinion = Schema.Struct({ summary: Schema.String, concerns: Schema.Array(Schema.String) })
73
-
74
92
  const TicketResult = Schema.Struct({
75
93
  key: Schema.String,
76
94
  title: Schema.String,
@@ -100,19 +118,16 @@ const Output = Schema.Struct({
100
118
  docs: Schema.optional(ChangeRequestResult)
101
119
  })
102
120
 
103
- const menu = ['Implement now', 'File as Tasks', 'Second opinion', 'Refine', 'Finish'] as const
121
+ const menu = ['Implement now', 'File as Tasks', 'Refine', 'Finish'] as const
104
122
 
105
123
  type Choice = (typeof menu)[number]
106
124
 
107
125
  /** Menu rounds before the plan is filed as Tasks for a person to pick up. */
108
126
  const maxRounds = 6
109
127
 
110
- const maxOpinionsPerPlan = 2
111
-
112
128
  const menuDescriptions: Record<Choice, string> = {
113
129
  'Implement now': 'Builds the tickets in dependency order and delivers them.',
114
130
  'File as Tasks': 'Makes a Task of each ticket, to build later.',
115
- 'Second opinion': 'Another Agent reviews the plan.',
116
131
  Refine: 'You say what to change and the planner rewrites it.',
117
132
  Finish: 'Ends the Run with the plan as it is.'
118
133
  }
@@ -127,6 +142,7 @@ const onFailure = ['Retry with a steer', 'Skip it and its dependents', 'Stop her
127
142
 
128
143
  const ticketRules = [
129
144
  '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.',
145
+ '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
146
  "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
147
  ].join('\n\n')
132
148
 
@@ -150,27 +166,12 @@ const planText = (plan: Plan) =>
150
166
  ...plan.tickets.map(ticketText)
151
167
  ].join('\n\n')
152
168
 
153
- const outOfScopeText = ({ outOfScope = [] }: PlanTicket) =>
154
- outOfScope.length === 0 ? [] : ['Out of scope:', ...outOfScope.map(item => `- ${item}`)]
155
-
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')
169
+ /** The plan as a person reads it, with what the plan reviewer still disputes. */
170
+ const documentText = (plan: Plan, disputed: readonly DisputedConcern[]) =>
171
+ [planText(plan), ...disputedText(disputed)].join('\n\n')
171
172
 
172
- const refinePrompt = (plan: Plan, words: string) =>
173
- [`Change the plan as the user asks: ${words}`, ticketRules, planText(plan)].join('\n\n')
173
+ const refinePrompt = (document: string, words: string) =>
174
+ [`Change the plan as the user asks: ${words}`, ticketRules, document].join('\n\n')
174
175
 
175
176
  const commentText = (comment: PlanReview['comments'][number]) => {
176
177
  const on =
@@ -181,12 +182,14 @@ const commentText = (comment: PlanReview['comments'][number]) => {
181
182
  return `${comment.id}, ${on}\n${comment.body}`
182
183
  }
183
184
 
184
- const revisePrompt = (plan: Plan, review: PlanReview) =>
185
+ const commentsText = (review: PlanReview) => review.comments.map(commentText).join('\n\n')
186
+
187
+ const revisePrompt = (document: string, review: PlanReview) =>
185
188
  [
186
189
  `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'),
190
+ commentsText(review),
188
191
  ticketRules,
189
- planText(plan)
192
+ document
190
193
  ].join('\n\n')
191
194
 
192
195
  const criteriaPrompt = (plan: Plan) =>
@@ -230,13 +233,6 @@ const guarded = (plan: Plan, check: typeof CriteriaCheck.Type) => {
230
233
  return { plan: { ...plan, tickets }, rejected }
231
234
  }
232
235
 
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
236
  /** The review a revision answers, and which of its comments the planner says it addressed or declined. */
241
237
  type Answered = {
242
238
  readonly review: PlanReview
@@ -244,11 +240,71 @@ type Answered = {
244
240
  readonly declined: readonly PlanDeclined[]
245
241
  }
246
242
 
243
+ type Draft = { readonly plan: Plan; readonly answered?: Answered | undefined }
244
+
245
+ const answeredText = ({ addressed, declined }: Answered) =>
246
+ [
247
+ "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.",
248
+ [
249
+ ...addressed.map(({ comment, how }) => `Comment ${comment}, addressed: ${how}`),
250
+ ...declined.map(({ comment, why }) => `Comment ${comment}, declined: ${why}`)
251
+ ].join('\n')
252
+ ].join('\n\n')
253
+
254
+ const rework = (step: string, prompt: string, answered: Answered | undefined) =>
255
+ answered === undefined
256
+ ? agentWorkOnce(step, { agent: 'planner', prompt: [prompt, ticketRules].join('\n\n'), output: Reworked }).pipe(
257
+ Effect.map(({ declined, ...plan }) => ({ plan: { plan }, declined }))
258
+ )
259
+ : agentWorkOnce(step, {
260
+ agent: 'planner',
261
+ prompt: [prompt, ticketRules, answeredText(answered)].join('\n\n'),
262
+ output: ReworkedAnswer
263
+ }).pipe(
264
+ Effect.map(({ declined, comments, ...plan }) => ({
265
+ plan: { plan, answered: { review: answered.review, ...comments } },
266
+ declined
267
+ }))
268
+ )
269
+
270
+ /**
271
+ * A plan revision ready for a person: the plan reviewer checked it, the planner answered its concerns, and its criteria
272
+ * were checked against the base, which rejects a ticket with nothing left to do.
273
+ */
274
+ const checked = (draft: Draft, revision: number, asked: readonly string[]) =>
275
+ Effect.gen(function* () {
276
+ const reviewed = yield* reviewPlan(`plan-review:${revision}`, {
277
+ plan: draft,
278
+ asked: asked.join('\n\n'),
279
+ text: ({ plan }) => planText(plan),
280
+ revise: (step, prompt) => rework(step, prompt, draft.answered)
281
+ })
282
+
283
+ const check = yield* agentWork(`criteria-check:${revision}`, {
284
+ agent: 'reviewer',
285
+ prompt: criteriaPrompt(reviewed.plan.plan),
286
+ output: CriteriaCheck
287
+ })
288
+
289
+ const { plan, rejected } = guarded(reviewed.plan.plan, check)
290
+
291
+ return {
292
+ plan,
293
+ rejected,
294
+ answered: reviewed.plan.answered,
295
+ disputed: reviewed.disputed.filter(entry => !rejected.some(({ key }) => key === entry.ticket))
296
+ }
297
+ })
298
+
247
299
  /**
248
300
  * A revision of the plan, as Run artifacts later Runs retrieve, and as the whole document a person reviews with the
249
301
  * review it answers.
250
302
  */
251
- const storePlan = (plan: Plan, suffix: string, revision: number, answered?: Answered) =>
303
+ const storePlan = (
304
+ { plan, disputed, answered }: Draft & { readonly disputed: readonly DisputedConcern[] },
305
+ suffix: string,
306
+ revision: number
307
+ ) =>
252
308
  Effect.gen(function* () {
253
309
  yield* artifact(`spec${suffix}`, {
254
310
  kind: 'plan_spec',
@@ -260,12 +316,11 @@ const storePlan = (plan: Plan, suffix: string, revision: number, answered?: Answ
260
316
  kind: 'plan_tickets',
261
317
  title: `Tickets for ${plan.title}`,
262
318
  text: plan.tickets.map(ticketText).join('\n\n'),
263
- data: plan.tickets.map(({ repoBindingId, outcome, promise, outOfScope, ...ticket }) => ({
319
+ data: plan.tickets.map(({ repoBindingId, outcome, ...ticket }) => ({
264
320
  ...ticket,
265
321
  acceptanceCriteria: [...ticket.acceptanceCriteria],
266
322
  blockedBy: [...ticket.blockedBy],
267
- ...(promise === undefined ? undefined : { promise }),
268
- ...(outOfScope === undefined ? undefined : { outOfScope: [...outOfScope] }),
323
+ outOfScope: [...ticket.outOfScope],
269
324
  ...(repoBindingId === undefined ? undefined : { repoBindingId }),
270
325
  ...(outcome === undefined ? undefined : { outcome })
271
326
  }))
@@ -273,7 +328,7 @@ const storePlan = (plan: Plan, suffix: string, revision: number, answered?: Answ
273
328
  yield* artifact(`document${suffix}`, {
274
329
  kind: 'plan_document',
275
330
  title: plan.title,
276
- text: planText(plan),
331
+ text: documentText(plan, disputed),
277
332
  data: {
278
333
  revision,
279
334
  ...(answered === undefined
@@ -606,20 +661,20 @@ const implement = (input: typeof Input.Type, plan: Plan, revision: number, filed
606
661
 
607
662
  /**
608
663
  * 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.
664
+ * tickets, and Plan opens a docs ChangeRequest for the ADR and glossary changes it left. A plan reviewer checks each
665
+ * revision, and the planner answers its concerns once. Plan then checks each ticket's criteria against the base, stores
666
+ * the spec and tickets as Run artifacts, and waits for a person to approve the revision, the planner revising it from
667
+ * their review until they do. Then it asks what next, at most six times: build the tickets, file them as Tasks, refine
668
+ * the plan from the user's words, or finish (ADR-0051). Nobody answering the menu files it as Tasks; started with
669
+ * `implement`, nobody answering approves each revision and implements it.
615
670
  */
616
671
  export default defineLoop({
617
672
  id: 'plan',
618
- version: '2.2.0',
673
+ version: '3.0.0',
619
674
  title: 'Plan',
620
675
  input: Input,
621
676
  output: Output,
622
- agents: { planner: 'planner', reviewer: 'reviewer' },
677
+ agents: { planner: 'planner', reviewer: 'reviewer', 'plan-reviewer': 'plan-reviewer' },
623
678
  checkout: 'write',
624
679
  // A Child Run gets no more than its parent was granted, so this covers what each ticket's Build needs too.
625
680
  capabilities: [
@@ -650,11 +705,11 @@ export default defineLoop({
650
705
  : yield* changeRequest('docs', { title: drafted.docs.title, body: drafted.docs.summary })
651
706
 
652
707
  let revision = 1
653
- let { plan, rejected } = yield* checked(drafted, revision)
708
+ const asked = [input.words]
709
+ let { plan, rejected, disputed } = yield* checked({ plan: drafted }, revision, asked)
654
710
  let filed: Filed | undefined
655
- let opinions = 0
656
711
 
657
- yield* storePlan(plan, '', revision)
712
+ yield* storePlan({ plan, disputed }, '', revision)
658
713
 
659
714
  const project = input.projectId === undefined ? undefined : { projectId: input.projectId }
660
715
 
@@ -698,7 +753,14 @@ export default defineLoop({
698
753
  Effect.gen(function* () {
699
754
  for (;;) {
700
755
  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.`,
756
+ prompt: [
757
+ `Review revision ${revision} of the plan "${plan.title}": approve it, or request changes by commenting on what should change.`,
758
+ ...(disputed.length === 0
759
+ ? []
760
+ : [
761
+ `The plan reviewer still disputes ${disputed.length} point${disputed.length === 1 ? '' : 's'}, listed at the end of the plan.`
762
+ ])
763
+ ].join(' '),
702
764
  freeText: true,
703
765
  recommended: PlanApproval,
704
766
  recommendationReason: 'Approve it once the plan reads right; until then the Run waits for you.',
@@ -708,11 +770,11 @@ export default defineLoop({
708
770
 
709
771
  if (isPlanApproval(answer)) return
710
772
 
711
- const review = readPlanReview(answer, revision, planText(plan))
773
+ const review = readPlanReview(answer, revision, documentText(plan, disputed))
712
774
 
713
775
  const revised = yield* agentWorkOnce(`revise-plan:${revision}`, {
714
776
  agent: 'planner',
715
- prompt: revisePrompt(plan, review),
777
+ prompt: revisePrompt(documentText(plan, disputed), review),
716
778
  output: Revised
717
779
  })
718
780
 
@@ -721,8 +783,12 @@ export default defineLoop({
721
783
  const count = review.comments.length
722
784
 
723
785
  revision++
724
- ;({ plan, rejected } = yield* checked(next, revision))
725
- yield* storePlan(plan, `:review-${revision}`, revision, { review, addressed, declined })
786
+ asked.push(commentsText(review))
787
+
788
+ const ready = yield* checked({ plan: next, answered: { review, addressed, declined } }, revision, asked)
789
+
790
+ ;({ plan, rejected, disputed } = ready)
791
+ yield* storePlan(ready, `:review-${revision}`, revision)
726
792
  yield* artifact(`changes:review-${revision}`, {
727
793
  kind: 'plan_changes',
728
794
  title: `Revision ${revision} of ${plan.title}`,
@@ -735,11 +801,7 @@ export default defineLoop({
735
801
  for (let round = 1; round <= maxRounds; round++) {
736
802
  const isFiled = filed?.revision === revision
737
803
 
738
- const options = menu.filter(
739
- option =>
740
- !(option === 'File as Tasks' && isFiled) &&
741
- !(option === 'Second opinion' && opinions >= maxOpinionsPerPlan)
742
- )
804
+ const options = menu.filter(option => !(option === 'File as Tasks' && isFiled))
743
805
 
744
806
  const fallback: Choice = input.implement === true ? 'Implement now' : isFiled ? 'Finish' : 'File as Tasks'
745
807
 
@@ -777,24 +839,6 @@ export default defineLoop({
777
839
  yield* file(`file-tasks:${round}`)
778
840
  break
779
841
 
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
842
  case 'Refine': {
799
843
  const words = yield* ask(`refine:${round}`, {
800
844
  prompt: 'What should change in the plan?',
@@ -805,15 +849,16 @@ export default defineLoop({
805
849
 
806
850
  const refined = yield* agentWorkOnce(`refine-plan:${round}`, {
807
851
  agent: 'planner',
808
- prompt: refinePrompt(plan, words),
852
+ prompt: refinePrompt(documentText(plan, disputed), words),
809
853
  output: Plan
810
854
  })
811
855
 
812
856
  const before = plan
813
857
 
814
858
  revision++
815
- ;({ plan, rejected } = yield* checked(refined, revision))
816
- yield* storePlan(plan, `:${round}`, revision)
859
+ asked.push(words)
860
+ ;({ plan, rejected, disputed } = yield* checked({ plan: refined }, revision, asked))
861
+ yield* storePlan({ plan, disputed }, `:${round}`, revision)
817
862
  yield* artifact(`changes:${round}`, {
818
863
  kind: 'plan_changes',
819
864
  title: `Revision ${revision} of ${plan.title}`,
package/src/research.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { agentWork, artifact, defineLoop, LoopOutcome, note, PlanTickets } from '@kitman-sh/sdk/loop'
1
+ import { agentWork, artifact, defineLoop, LoopOutcome, note } from '@kitman-sh/sdk/loop'
2
2
  import * as Effect from 'effect/Effect'
3
3
  import * as Schema from 'effect/Schema'
4
+ import { ScopedTickets } from './plan-review'
4
5
  import { OneSentence } from './text-limits'
5
6
 
6
7
  export class ResearchQuestionMissing extends Schema.TaggedError<ResearchQuestionMissing>()('ResearchQuestionMissing', {
@@ -24,7 +25,7 @@ const Fix = Schema.Struct({
24
25
  outcome: LoopOutcome
25
26
  })
26
27
 
27
- const NextSteps = Schema.Struct({ tickets: PlanTickets })
28
+ const NextSteps = Schema.Struct({ tickets: ScopedTickets })
28
29
 
29
30
  const Report = Schema.Struct({
30
31
  /** The Task's title, or a short title for the question. */
@@ -77,7 +78,7 @@ const researchPrompt = (words: string | undefined) =>
77
78
  'If your context has an earlier research report on this, extend it: keep what still holds, check what may have changed, and answer what it left open. Do not start over.',
78
79
  'Cite primary sources for every finding: the code, official documentation, specifications and first-party APIs. You may run things to reproduce behaviour; change nothing in the repository. Mark each finding observed (you ran it and saw it), read (a source says so) or inferred (you concluded it).',
79
80
  'List every source you needed and could not reach under `unreached`, with what you tried, and lower your conclusions to what the sources you did reach show.',
80
- 'If the question turns out to be a bug report, say so in the summary and give `fix` with outcome bug. If the report proposes any other change, give `fix` with its outcome. Give next steps worth doing as `nextStepTasks` tickets too.',
81
+ "If the question turns out to be a bug report, say so in the summary and give `fix` with outcome bug. If the report proposes any other change, give `fix` with its outcome. Give next steps worth doing as `nextStepTasks` tickets too, each scoped as skills({ name: 'scope-tasks' }) from the Kitman MCP says: its `promise` is one sentence that says what is true once it is done, and its `outOfScope` lists the cases it does not handle.",
81
82
  'Return the report as your Output. Do not write it into the repository.'
82
83
  ].join('\n\n')
83
84
 
@@ -153,7 +154,7 @@ const sample = {
153
154
  */
154
155
  export default defineLoop({
155
156
  id: 'research',
156
- version: '2.0.0',
157
+ version: '2.1.0',
157
158
  title: 'Research',
158
159
  input: Input,
159
160
  output: Output,