@kitman-sh/software-engineering 0.14.8 → 0.15.0

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/errand.ts CHANGED
@@ -1,30 +1,37 @@
1
1
  import {
2
- agentWorkOnce,
3
2
  ask,
4
3
  call,
5
- changeRequest,
6
4
  ChangeRequestResult,
7
5
  defineLoop,
6
+ step,
8
7
  testsNotRunReasons,
9
8
  verify,
10
- writeBack
9
+ type VerifyResult
11
10
  } from '@kitman-sh/sdk/loop'
12
11
  import * as Effect from 'effect/Effect'
13
12
  import * as Schema from 'effect/Schema'
14
- import build, { implementAdaptation, SuggestedCheck } from './build'
13
+ import { implementer } from './agents'
14
+ import build from './build'
15
15
  import { evidenceTable } from './change-request-body'
16
+ import { implementAdaptation } from './implemented'
17
+ import { opened } from './opened'
18
+ import { SuggestedCheck } from './suggested-check'
16
19
  import { ChangeRequestSummary } from './text-limits'
17
20
 
18
21
  const Input = Schema.Struct({
19
- /** What to just do. */
20
- words: Schema.String,
21
- taskId: Schema.optional(Schema.String),
22
- repoBindingId: Schema.optional(Schema.String),
23
- /** The base branch; the repository's default branch when left out. */
24
- branch: Schema.optional(Schema.String)
22
+ words: Schema.String.annotate({ description: 'What to just do.' }),
23
+ taskId: Schema.optional(Schema.String).annotate({
24
+ description: 'The Task the Errand does; the words say what when left out.'
25
+ }),
26
+ repoBindingId: Schema.optional(Schema.String).annotate({
27
+ description: "The repository to work in; the Task's, else the Team's default, when left out."
28
+ }),
29
+ branch: Schema.optional(Schema.String).annotate({
30
+ description: "The base branch; the repository's default branch when left out."
31
+ })
25
32
  })
26
33
 
27
- const Done = Schema.Struct({
34
+ const Answer = Schema.Struct({
28
35
  /** done: finished; blocked: something stopped it; too_big: it is a feature, not an errand, and nothing changed. */
29
36
  status: Schema.Literals(['done', 'blocked', 'too_big']),
30
37
  /** What it did or found, or what stopped it, for the person who asked. */
@@ -43,9 +50,13 @@ const budget = '1 hour'
43
50
  const choices = ['Open a ChangeRequest', 'Hand to Build for a full review', 'Discard'] as const
44
51
 
45
52
  const Output = Schema.Struct({
46
- status: Schema.Literals(['done', 'blocked', 'too_big', 'opened', 'handed_to_build', 'discarded']),
47
- summary: Schema.String,
48
- changeRequest: Schema.optional(ChangeRequestResult)
53
+ status: Schema.Literals(['done', 'blocked', 'too_big', 'opened', 'handed_to_build', 'discarded']).annotate({
54
+ description: 'done, blocked, too big for an Errand, opened a ChangeRequest, handed to Build, or discarded.'
55
+ }),
56
+ summary: Schema.String.annotate({ description: 'What the Errand did, in a line.' }),
57
+ changeRequest: Schema.optional(ChangeRequestResult).annotate({
58
+ description: 'The ChangeRequest it opened, when it opened one.'
59
+ })
49
60
  })
50
61
 
51
62
  const prompt = (words: string) =>
@@ -57,6 +68,87 @@ const prompt = (words: string) =>
57
68
  'Answer with a summary for the person who asked, whether you changed files, a ChangeRequest title if you did, and checks to run if the repository has none approved.'
58
69
  ].join('\n\n')
59
70
 
71
+ export class Done extends Schema.TaggedClass<Done>()('Done', {
72
+ summary: Schema.String,
73
+ changed: Schema.Boolean,
74
+ title: Schema.optional(Schema.String),
75
+ checks: Schema.optional(Schema.Array(SuggestedCheck))
76
+ }) {}
77
+
78
+ /** Something stopped the errand, as `summary` says. */
79
+ export class Blocked extends Schema.TaggedClass<Blocked>()('Blocked', { summary: Schema.String }) {}
80
+
81
+ /** It is a feature, not an errand, as `summary` says why. */
82
+ export class TooBig extends Schema.TaggedClass<TooBig>()('TooBig', { summary: Schema.String }) {}
83
+
84
+ /** One agent just does what the words say. The Loop that uses it names an `implementer` agent. */
85
+ export const done = step('done', {
86
+ payload: { words: Schema.String.annotate({ description: 'What to just do.' }) },
87
+ success: Schema.Union([Done, Blocked, TooBig]),
88
+ run: ({ words }) =>
89
+ Effect.gen(function* () {
90
+ const { status, summary, changed, title, checks } = yield* implementer('errand', {
91
+ skill: 'implement',
92
+ prompt: prompt(words),
93
+ output: Answer,
94
+ timeout: budget
95
+ })
96
+
97
+ if (status === 'blocked') return new Blocked({ summary })
98
+
99
+ if (status === 'too_big') return new TooBig({ summary })
100
+
101
+ return new Done({ summary, changed, title, checks })
102
+ }).pipe(
103
+ // Kitman fails a Step that runs past its time budget with this reason.
104
+ Effect.catchIf(
105
+ error => error._tag === 'StepFailed' && error.reason.includes('time budget'),
106
+ () =>
107
+ Effect.succeed(
108
+ new TooBig({
109
+ summary: `It took longer than an errand's ${budget}, so it is a feature: Build can do it with a full review.`
110
+ })
111
+ )
112
+ )
113
+ )
114
+ })
115
+
116
+ const chosen = (summary: string, head: VerifyResult) => {
117
+ const notRun = testsNotRunReasons(head)
118
+ const trusted = head.passed && notRun.length === 0
119
+
120
+ const checksSay = !head.passed
121
+ ? 'The checks fail.'
122
+ : notRun.length === 0
123
+ ? 'The checks pass.'
124
+ : `The checks pass, but verify never ran some tests:\n${notRun.map(item => `- ${item}`).join('\n')}`
125
+
126
+ return ask('chosen', {
127
+ prompt: `${summary}\n\n${checksSay}\n\nWhat next?`,
128
+ options: [
129
+ { value: 'Open a ChangeRequest', description: 'Deliver it as it is, unreviewed.' },
130
+ { value: 'Hand to Build for a full review', description: 'Build reviews and verifies it before delivering.' },
131
+ { value: 'Discard', description: 'Drop the change.' }
132
+ ] satisfies { readonly value: (typeof choices)[number]; readonly description: string }[],
133
+ recommended: trusted ? 'Open a ChangeRequest' : 'Hand to Build for a full review',
134
+ recommendationReason: trusted
135
+ ? 'The checks pass, and an errand is small enough to review on the ChangeRequest.'
136
+ : head.passed
137
+ ? 'Verify skipped tests it should have run, so it needs the review a Build gives before anyone merges it.'
138
+ : 'The checks fail, so it needs the fixes and review a Build gives before anyone merges it.',
139
+ default: 'Open a ChangeRequest',
140
+ timeout: '1 hour'
141
+ })
142
+ }
143
+
144
+ const unreviewedBody = (summary: string, head: VerifyResult) =>
145
+ [
146
+ summary,
147
+ '> **Unreviewed errand.** One agent did this with no review; ask for a Build or a Review if it needs one.',
148
+ '## Evidence',
149
+ evidenceTable(head)
150
+ ].join('\n\n')
151
+
60
152
  /**
61
153
  * An errand: one agent just does a small thing with the words, with no review rally. A change is verified and then,
62
154
  * by answer or by default, opened as a ChangeRequest marked unreviewed, handed to Build for a full review, or
@@ -65,8 +157,9 @@ const prompt = (words: string) =>
65
157
  */
66
158
  export default defineLoop({
67
159
  id: 'errand',
68
- version: '1.0.0',
160
+ version: '2.0.0',
69
161
  title: 'Errand',
162
+ description: 'One agent does a small change with no review rally, then opens it as an unreviewed ChangeRequest.',
70
163
  input: Input,
71
164
  output: Output,
72
165
  agents: { implementer: 'implementer' },
@@ -76,58 +169,20 @@ export default defineLoop({
76
169
  calls: () => [build],
77
170
  run: input =>
78
171
  Effect.gen(function* () {
79
- const done = yield* agentWorkOnce('errand', {
80
- agent: 'implementer',
81
- skill: 'implement',
82
- prompt: prompt(input.words),
83
- output: Done,
84
- timeout: budget
85
- }).pipe(
86
- // Kitman fails a Step that runs past its time budget with this reason.
87
- Effect.catchIf(
88
- error => error._tag === 'StepFailed' && error.reason.includes('time budget'),
89
- () =>
90
- Effect.succeed({
91
- status: 'too_big' as const,
92
- summary: `It took longer than an errand's ${budget}, so it is a feature: Build can do it with a full review.`,
93
- changed: false
94
- })
95
- )
96
- )
172
+ const errand = yield* done({ words: input.words })
97
173
 
98
- if (done.status !== 'done' || !done.changed) return { status: done.status, summary: done.summary }
99
-
100
- const head = yield* verify('verify', { suggested: done.checks ?? [] })
101
-
102
- const notRun = testsNotRunReasons(head)
103
- const trusted = head.passed && notRun.length === 0
104
-
105
- const checksSay = !head.passed
106
- ? 'The checks fail.'
107
- : notRun.length === 0
108
- ? 'The checks pass.'
109
- : `The checks pass, but verify never ran some tests:\n${notRun.map(item => `- ${item}`).join('\n')}`
110
-
111
- const answer = yield* ask('next', {
112
- prompt: `${done.summary}\n\n${checksSay}\n\nWhat next?`,
113
- options: [
114
- { value: 'Open a ChangeRequest', description: 'Deliver it as it is, unreviewed.' },
115
- { value: 'Hand to Build for a full review', description: 'Build reviews and verifies it before delivering.' },
116
- { value: 'Discard', description: 'Drop the change.' }
117
- ] satisfies { readonly value: (typeof choices)[number]; readonly description: string }[],
118
- recommended: trusted ? 'Open a ChangeRequest' : 'Hand to Build for a full review',
119
- recommendationReason: trusted
120
- ? 'The checks pass, and an errand is small enough to review on the ChangeRequest.'
121
- : head.passed
122
- ? 'Verify skipped tests it should have run, so it needs the review a Build gives before anyone merges it.'
123
- : 'The checks fail, so it needs the fixes and review a Build gives before anyone merges it.',
124
- default: 'Open a ChangeRequest',
125
- timeout: '1 hour'
126
- })
174
+ if (errand._tag === 'TooBig') return { status: 'too_big' as const, summary: errand.summary }
175
+
176
+ if (errand._tag === 'Blocked') return { status: 'blocked' as const, summary: errand.summary }
127
177
 
128
- if (answer === 'Discard') return { status: 'discarded' as const, summary: done.summary }
178
+ if (!errand.changed) return { status: 'done' as const, summary: errand.summary }
129
179
 
130
- if (answer === 'Hand to Build for a full review') {
180
+ const head = yield* verify('verify', { suggested: errand.checks ?? [] })
181
+ const choice = yield* chosen(errand.summary, head)
182
+
183
+ if (choice === 'Discard') return { status: 'discarded' as const, summary: errand.summary }
184
+
185
+ if (choice === 'Hand to Build for a full review') {
131
186
  const built = yield* call('build', build, {
132
187
  words: input.words,
133
188
  revision: head.revision,
@@ -135,21 +190,14 @@ export default defineLoop({
135
190
  ...(input.branch === undefined ? undefined : { branch: input.branch })
136
191
  })
137
192
 
138
- return { status: 'handed_to_build' as const, summary: done.summary, changeRequest: built.changeRequest }
193
+ return { status: 'handed_to_build' as const, summary: errand.summary, changeRequest: built.changeRequest }
139
194
  }
140
195
 
141
- const opened = yield* changeRequest('change-request', {
142
- title: done.title ?? input.words.slice(0, 120),
143
- body: [
144
- done.summary,
145
- '> **Unreviewed errand.** One agent did this with no review; ask for a Build or a Review if it needs one.',
146
- '## Evidence',
147
- evidenceTable(head)
148
- ].join('\n\n')
196
+ const { changeRequest } = yield* opened({
197
+ title: errand.title ?? input.words.slice(0, 120),
198
+ body: unreviewedBody(errand.summary, head)
149
199
  })
150
200
 
151
- yield* writeBack('write-back', { milestone: 'change_request_opened', changeRequestUrl: opened.url })
152
-
153
- return { status: 'opened' as const, summary: done.summary, changeRequest: opened }
201
+ return { status: 'opened' as const, summary: errand.summary, changeRequest }
154
202
  })
155
203
  })
package/src/file-tasks.ts CHANGED
@@ -4,18 +4,24 @@ import * as Schema from 'effect/Schema'
4
4
  import { reviewTickets, ScopedTickets, TicketConcern } from './plan-review'
5
5
 
6
6
  export const FileTasksInput = Schema.Struct({
7
- tickets: ScopedTickets,
8
- /** The Project the Tasks land in; the Project of the Run's Task when left out. */
9
- projectId: Schema.optional(Schema.String),
10
- /** The repository the tickets are checked against, and filed for unless a ticket names its own. */
11
- repoBindingId: Schema.optional(Schema.String)
7
+ tickets: ScopedTickets.annotateKey({
8
+ description: 'The tickets to file, each with its title, body, Promise, Out of scope and Acceptance Criteria.'
9
+ }),
10
+ projectId: Schema.optional(Schema.String).annotate({
11
+ description: "The Project the Tasks land in; the Project of the Run's Task when left out."
12
+ }),
13
+ repoBindingId: Schema.optional(Schema.String).annotate({
14
+ description: 'The repository the tickets are checked against, and filed for unless a ticket names its own.'
15
+ })
12
16
  })
13
17
 
14
18
  const Output = Schema.Struct({
15
- /** None when the review has concerns. */
16
- tasks: Schema.Array(Schema.Struct({ key: Schema.String, taskId: Schema.String })),
17
- /** What to revise before starting it again, each by its ticket. */
18
- concerns: Schema.Array(TicketConcern)
19
+ tasks: Schema.Array(Schema.Struct({ key: Schema.String, taskId: Schema.String })).annotate({
20
+ description: "The Task filed for each ticket, by the ticket's key; none when the review has concerns."
21
+ }),
22
+ concerns: Schema.Array(TicketConcern).annotate({
23
+ description: 'What to revise before starting it again, each by its ticket.'
24
+ })
19
25
  })
20
26
 
21
27
  /**
@@ -24,8 +30,9 @@ const Output = Schema.Struct({
24
30
  */
25
31
  export default defineLoop({
26
32
  id: 'file-tasks',
27
- version: '2.1.0',
33
+ version: '2.2.0',
28
34
  title: 'File Tasks',
35
+ description: 'Files reviewed tickets as Tasks, with their blockers as dependencies.',
29
36
  input: FileTasksInput,
30
37
  output: Output,
31
38
  agents: { 'plan-reviewer': 'plan-reviewer' },
package/src/firewatch.ts CHANGED
@@ -1,22 +1,28 @@
1
- import { agentWork, alertTask, artifact, ask, call, defineLoop, note } from '@kitman-sh/sdk/loop'
1
+ import { alertTask, artifact, ask, call, defineLoop, note, step } from '@kitman-sh/sdk/loop'
2
2
  import * as Effect from 'effect/Effect'
3
3
  import * as Predicate from 'effect/Predicate'
4
4
  import * as Schema from 'effect/Schema'
5
+ import { investigator } from './agents'
5
6
  import build from './build'
6
7
  import { OneSentence } from './text-limits'
7
8
 
8
9
  const Input = Schema.Struct({
9
- /** The alert as the monitoring Connection received it. */
10
- alert: Schema.Record(Schema.String, Schema.Json),
11
- /** Where a fix is built; the Team's default repository when left out. */
12
- repoBindingId: Schema.optional(Schema.String),
13
- /** How long a conclusion stands: a repeat within it is folded in, not investigated again; 6 when left out. */
14
- windowHours: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(720))),
15
- /**
16
- * Starts a fix unasked when the cause is in code and the conclusion is confident. A Team opts in with `values` on the
17
- * When Clause of the Kit that declares Firewatch for it.
18
- */
19
- autoFix: Schema.optional(Schema.Boolean)
10
+ alert: Schema.Record(Schema.String, Schema.Json).annotate({
11
+ description: 'The alert as the monitoring Connection received it.'
12
+ }),
13
+ repoBindingId: Schema.optional(Schema.String).annotate({
14
+ description: "Where a fix is built; the Team's default repository when left out."
15
+ }),
16
+ windowHours: Schema.optional(
17
+ Schema.Int.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(720))
18
+ ).annotate({
19
+ description:
20
+ 'How long a conclusion stands: a repeat within it is folded in, not investigated again; 6 when left out.'
21
+ }),
22
+ autoFix: Schema.optional(Schema.Boolean).annotate({
23
+ description:
24
+ 'Starts a fix unasked when the cause is in code and the conclusion is confident. A Team opts in with `values` on the When Clause of the Kit that declares Firewatch for it.'
25
+ })
20
26
  })
21
27
 
22
28
  type Alert = (typeof Input.Type)['alert']
@@ -43,18 +49,26 @@ type Conclusion = typeof Conclusion.Type
43
49
  const FixRequest = Schema.Struct({ words: Schema.String, outcome: Schema.Literal('bug') })
44
50
 
45
51
  const Output = Schema.Struct({
46
- /** folded: a repeat joined earlier work; resolved: it resolved with nothing to investigate; concluded. */
47
- status: Schema.Literals(['folded', 'resolved', 'concluded']),
48
- taskId: Schema.optional(Schema.String),
49
- /** The Run this alert was folded into. */
50
- joinedRunId: Schema.optional(Schema.String),
51
- conclusionArtifactId: Schema.optional(Schema.String),
52
- /** It resolved on its own while it was being investigated. */
53
- resolvedDuring: Schema.Boolean,
54
- fix: Schema.Literals(['none', 'declined', 'started', 'failed']),
55
- changeRequestUrl: Schema.optional(Schema.String),
56
- /** The fix a person can still start from the Run's offer. */
57
- fixRequest: Schema.optional(FixRequest)
52
+ status: Schema.Literals(['folded', 'resolved', 'concluded']).annotate({
53
+ description: 'folded: a repeat joined earlier work; resolved: it resolved with nothing to investigate; concluded.'
54
+ }),
55
+ taskId: Schema.optional(Schema.String).annotate({
56
+ description: 'The Task that tracks the alert, when there is one.'
57
+ }),
58
+ joinedRunId: Schema.optional(Schema.String).annotate({ description: 'The Run this alert was folded into.' }),
59
+ conclusionArtifactId: Schema.optional(Schema.String).annotate({
60
+ description: "The Artifact holding the investigation's conclusion."
61
+ }),
62
+ resolvedDuring: Schema.Boolean.annotate({ description: 'It resolved on its own while it was being investigated.' }),
63
+ fix: Schema.Literals(['none', 'declined', 'started', 'failed']).annotate({
64
+ description: 'Whether a fix was started: none was offered, a person declined it, it started, or starting it failed.'
65
+ }),
66
+ changeRequestUrl: Schema.optional(Schema.String).annotate({
67
+ description: "The fix's ChangeRequest, when a fix opened one."
68
+ }),
69
+ fixRequest: Schema.optional(FixRequest).annotate({
70
+ description: "The fix a person can still start from the Run's offer."
71
+ })
58
72
  })
59
73
 
60
74
  type Output = typeof Output.Type
@@ -125,6 +139,127 @@ const conclusionText = (conclusion: Conclusion, resolvedDuring: boolean) =>
125
139
  ...(conclusion.fix === undefined ? [] : [`Fix: ${conclusion.fix}`])
126
140
  ].join('\n')
127
141
 
142
+ const AlertRequest = {
143
+ fingerprint: Schema.String,
144
+ title: Schema.String,
145
+ windowHours: Schema.Int,
146
+ priority: Schema.Literals(['urgent', 'high', 'medium', 'low'])
147
+ }
148
+
149
+ /** A repeat joined earlier work on the alert's Task. */
150
+ export class Folded extends Schema.TaggedClass<Folded>()('Folded', {
151
+ taskId: Schema.optional(Schema.String),
152
+ joinedRunId: Schema.String
153
+ }) {}
154
+
155
+ /** It resolved with nothing open to investigate. */
156
+ export class AlertResolved extends Schema.TaggedClass<AlertResolved>()('AlertResolved', {
157
+ taskId: Schema.optional(Schema.String)
158
+ }) {}
159
+
160
+ /** The alert's Task, found or made, for an investigation. */
161
+ export class Placed extends Schema.TaggedClass<Placed>()('Placed', { taskId: Schema.optional(Schema.String) }) {}
162
+
163
+ /** Finds the alert's Task by fingerprint or makes one, and folds a repeat into the Run already on it. */
164
+ export const folded = step('folded', {
165
+ payload: { ...AlertRequest, resolved: Schema.Boolean },
166
+ success: Schema.Union([Folded, AlertResolved, Placed]),
167
+ run: ({ resolved, ...request }) =>
168
+ Effect.gen(function* () {
169
+ const placed = yield* alertTask('task', { ...request, ...(resolved ? { resolved } : undefined) })
170
+ const taskId = placed.taskId === undefined ? undefined : { taskId: placed.taskId }
171
+
172
+ if (placed.joined !== undefined) return new Folded({ ...taskId, joinedRunId: placed.joined.runId })
173
+
174
+ return resolved ? new AlertResolved({ ...taskId }) : new Placed({ ...taskId })
175
+ })
176
+ })
177
+
178
+ /** The investigator's conclusion on the alert. The Loop that uses it names an `investigator` agent. */
179
+ export const investigated = step('investigated', {
180
+ payload: { alert: Input.fields.alert },
181
+ success: Conclusion,
182
+ run: ({ alert }) => investigator('investigate', { prompt: investigatePrompt(alert), output: Conclusion })
183
+ })
184
+
185
+ export class Concluded extends Schema.TaggedClass<Concluded>()('Concluded', {
186
+ artifactId: Schema.String,
187
+ resolvedDuring: Schema.Boolean
188
+ }) {}
189
+
190
+ /** Stores the conclusion and notes it on the Task, saying when the alert resolved meanwhile. */
191
+ export const concluded = step('concluded', {
192
+ payload: { alert: Input.fields.alert, request: Schema.Struct(AlertRequest), conclusion: Conclusion },
193
+ success: Concluded,
194
+ run: ({ alert, request, conclusion }) =>
195
+ Effect.gen(function* () {
196
+ // A resolve that arrived while investigating was folded into this Run's Task.
197
+ const resolvedDuring = (yield* alertTask('task', request)).resolved === true
198
+
199
+ const stored = yield* artifact('conclusion', {
200
+ kind: 'investigation_conclusion',
201
+ title: request.title,
202
+ text: conclusionText(conclusion, resolvedDuring),
203
+ data: { alert, sources: conclusion.sources, confidence: conclusion.confidence }
204
+ })
205
+
206
+ yield* note('summary', { text: conclusion.summary, artifactId: stored.artifactId })
207
+
208
+ return new Concluded({ artifactId: stored.artifactId, resolvedDuring })
209
+ })
210
+ })
211
+
212
+ export class FixDeclined extends Schema.TaggedClass<FixDeclined>()('FixDeclined', {}) {}
213
+
214
+ export class FixStarted extends Schema.TaggedClass<FixStarted>()('FixStarted', { changeRequestUrl: Schema.String }) {}
215
+
216
+ /** The Build it started failed. */
217
+ export class FixFailed extends Schema.TaggedClass<FixFailed>()('FixFailed', {}) {}
218
+
219
+ /** Asks whether to start a Build that fixes the cause as a bug, and starts it when the answer says so. */
220
+ export const fixOffered = step('fixOffered', {
221
+ payload: {
222
+ summary: Schema.String,
223
+ fix: Schema.String,
224
+ fixRequest: FixRequest,
225
+ confident: Schema.Boolean,
226
+ resolvedDuring: Schema.Boolean,
227
+ autoFix: Schema.Boolean,
228
+ taskId: Schema.optional(Schema.String),
229
+ repoBindingId: Schema.optional(Schema.String)
230
+ },
231
+ success: Schema.Union([FixDeclined, FixStarted, FixFailed]),
232
+ run: ({ summary, fix, fixRequest, confident, resolvedDuring, autoFix, taskId, repoBindingId }) =>
233
+ Effect.gen(function* () {
234
+ const answer = yield* ask('start-fix', {
235
+ prompt: `${summary}\n\nStart a Build that fixes it as a bug: ${fix}?`,
236
+ options: [
237
+ { value: 'Start a fix', description: 'A Build fixes it as a bug and opens a ChangeRequest.' },
238
+ { value: 'Not now', description: 'Keeps the finding on the Task.' }
239
+ ],
240
+ recommended: confident ? 'Start a fix' : 'Not now',
241
+ recommendationReason: confident
242
+ ? 'Firewatch is confident of the cause and the alert is still firing.'
243
+ : resolvedDuring
244
+ ? 'The alert resolved while Firewatch looked, so a fix can wait for a person to judge.'
245
+ : 'Firewatch is not confident of the cause, so a fix could aim at the wrong thing.',
246
+ default: autoFix && confident ? 'Start a fix' : 'Not now',
247
+ timeout: '1 hour'
248
+ })
249
+
250
+ if (answer === 'Not now') return new FixDeclined()
251
+
252
+ return yield* call('fix', build, {
253
+ ...fixRequest,
254
+ ...(taskId === undefined ? undefined : { taskId }),
255
+ ...(repoBindingId === undefined ? undefined : { repoBindingId })
256
+ }).pipe(
257
+ Effect.map(fixed => new FixStarted({ changeRequestUrl: fixed.changeRequest.url })),
258
+ Effect.catchTag('ChildFailed', () => Effect.succeed(new FixFailed()))
259
+ )
260
+ })
261
+ })
262
+
128
263
  /**
129
264
  * Turns an alert into a cited cause on its Bug Task, found by fingerprint or made. A repeat while the alert is being
130
265
  * investigated, or soon after, is folded into that Run as another occurrence. A resolve during the investigation is
@@ -133,8 +268,9 @@ const conclusionText = (conclusion: Conclusion, resolvedDuring: boolean) =>
133
268
  */
134
269
  export default defineLoop({
135
270
  id: 'firewatch',
136
- version: '1.0.0',
271
+ version: '2.0.0',
137
272
  title: 'Firewatch',
273
+ description: 'Investigates an alert and writes its cited cause on a Bug Task, offering a fix.',
138
274
  input: Input,
139
275
  output: Output,
140
276
  outcome: 'investigation',
@@ -146,88 +282,61 @@ export default defineLoop({
146
282
  calls: () => [build],
147
283
  run: input =>
148
284
  Effect.gen(function* () {
149
- const fingerprint = fingerprintOf(input.alert)
150
- const resolved = isResolved(input.alert)
151
- const windowHours = input.windowHours ?? 6
152
- const request = { fingerprint, title: alertTitle(input.alert), windowHours, priority: priorityOf(input.alert) }
285
+ const request = {
286
+ fingerprint: fingerprintOf(input.alert),
287
+ title: alertTitle(input.alert),
288
+ windowHours: input.windowHours ?? 6,
289
+ priority: priorityOf(input.alert)
290
+ }
153
291
 
154
- const placed = yield* alertTask('task', { ...request, ...(resolved ? { resolved } : undefined) })
292
+ const placed = yield* folded({ ...request, resolved: isResolved(input.alert) })
155
293
  const taskId = placed.taskId === undefined ? undefined : { taskId: placed.taskId }
156
294
 
157
- if (placed.joined !== undefined)
295
+ if (placed._tag === 'Folded')
158
296
  return {
159
297
  status: 'folded',
160
298
  ...taskId,
161
- joinedRunId: placed.joined.runId,
299
+ joinedRunId: placed.joinedRunId,
162
300
  resolvedDuring: false,
163
301
  fix: 'none'
164
302
  } satisfies Output
165
303
 
166
- if (resolved) return { status: 'resolved', ...taskId, resolvedDuring: false, fix: 'none' } satisfies Output
167
-
168
- const conclusion = yield* agentWork('investigate', {
169
- agent: 'investigator',
170
- prompt: investigatePrompt(input.alert),
171
- output: Conclusion
172
- })
304
+ if (placed._tag === 'AlertResolved')
305
+ return { status: 'resolved', ...taskId, resolvedDuring: false, fix: 'none' } satisfies Output
173
306
 
174
- // A resolve that arrived while investigating was folded into this Run's Task.
175
- const resolvedDuring = (yield* alertTask('task:check', request)).resolved === true
307
+ const conclusion = yield* investigated({ alert: input.alert })
308
+ const { artifactId, resolvedDuring } = yield* concluded({ alert: input.alert, request, conclusion })
176
309
 
177
- const stored = yield* artifact('conclusion', {
178
- kind: 'investigation_conclusion',
179
- title: request.title,
180
- text: conclusionText(conclusion, resolvedDuring),
181
- data: { alert: input.alert, sources: conclusion.sources, confidence: conclusion.confidence }
182
- })
183
-
184
- yield* note('summary', { text: conclusion.summary, artifactId: stored.artifactId })
185
-
186
- const concluded = {
310
+ const ended = {
187
311
  status: 'concluded',
188
312
  ...taskId,
189
- conclusionArtifactId: stored.artifactId,
313
+ conclusionArtifactId: artifactId,
190
314
  resolvedDuring
191
315
  } as const
192
316
 
193
- if (conclusion.fix === undefined || !conclusion.inCode) return { ...concluded, fix: 'none' } satisfies Output
317
+ if (conclusion.fix === undefined || !conclusion.inCode) return { ...ended, fix: 'none' } satisfies Output
194
318
 
195
319
  const fixRequest = {
196
320
  words: `${conclusion.fix}\n\n${conclusionText(conclusion, resolvedDuring)}`,
197
321
  outcome: 'bug' as const
198
322
  }
199
323
 
200
- const confident = conclusion.confidence === 'high' && !resolvedDuring
201
-
202
- const answer = yield* ask('start-fix', {
203
- prompt: `${conclusion.summary}\n\nStart a Build that fixes it as a bug: ${conclusion.fix}?`,
204
- options: [
205
- { value: 'Start a fix', description: 'A Build fixes it as a bug and opens a ChangeRequest.' },
206
- { value: 'Not now', description: 'Keeps the finding on the Task.' }
207
- ],
208
- recommended: confident ? 'Start a fix' : 'Not now',
209
- recommendationReason: confident
210
- ? 'Firewatch is confident of the cause and the alert is still firing.'
211
- : resolvedDuring
212
- ? 'The alert resolved while Firewatch looked, so a fix can wait for a person to judge.'
213
- : 'Firewatch is not confident of the cause, so a fix could aim at the wrong thing.',
214
- default: input.autoFix === true && confident ? 'Start a fix' : 'Not now',
215
- timeout: '1 hour'
324
+ const offered = yield* fixOffered({
325
+ summary: conclusion.summary,
326
+ fix: conclusion.fix,
327
+ fixRequest,
328
+ confident: conclusion.confidence === 'high' && !resolvedDuring,
329
+ resolvedDuring,
330
+ autoFix: input.autoFix === true,
331
+ ...taskId,
332
+ ...(input.repoBindingId === undefined ? undefined : { repoBindingId: input.repoBindingId })
216
333
  })
217
334
 
218
- if (answer === 'Not now') return { ...concluded, fix: 'declined', fixRequest } satisfies Output
335
+ if (offered._tag === 'FixStarted')
336
+ return { ...ended, fix: 'started', changeRequestUrl: offered.changeRequestUrl } satisfies Output
219
337
 
220
- return yield* call('fix', build, {
221
- ...fixRequest,
222
- ...taskId,
223
- ...(input.repoBindingId === undefined ? undefined : { repoBindingId: input.repoBindingId })
224
- }).pipe(
225
- Effect.map(
226
- fixed => ({ ...concluded, fix: 'started', changeRequestUrl: fixed.changeRequest.url }) satisfies Output
227
- ),
228
- Effect.catchTag('ChildFailed', () =>
229
- Effect.succeed({ ...concluded, fix: 'failed', fixRequest } satisfies Output)
230
- )
231
- )
338
+ if (offered._tag === 'FixFailed') return { ...ended, fix: 'failed', fixRequest } satisfies Output
339
+
340
+ return { ...ended, fix: 'declined', fixRequest } satisfies Output
232
341
  })
233
342
  })