@gtheys/pi-flow 0.0.0-stage → 0.2.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.
Files changed (164) hide show
  1. package/README.md +833 -2
  2. package/adhoc/tracker.ts +360 -0
  3. package/aven/client.ts +996 -0
  4. package/aven/status.ts +395 -0
  5. package/brainstorm/args.ts +24 -0
  6. package/brainstorm/artifact.ts +67 -0
  7. package/brainstorm/paths.ts +43 -0
  8. package/brainstorm/prompt.ts +172 -0
  9. package/brainstorm/refs.ts +41 -0
  10. package/config.schema.json +161 -0
  11. package/config.ts +577 -0
  12. package/feedback/args.ts +21 -0
  13. package/feedback/prompt.ts +177 -0
  14. package/feedback/triage.ts +88 -0
  15. package/flow/agent-role.ts +46 -0
  16. package/flow/answer-wake.ts +212 -0
  17. package/flow/answer.ts +183 -0
  18. package/flow/args.ts +48 -0
  19. package/flow/ask-emitter.ts +331 -0
  20. package/flow/attribution.ts +145 -0
  21. package/flow/aven-resolve-exec.ts +95 -0
  22. package/flow/aven-resolve.ts +80 -0
  23. package/flow/awaiting-approval.ts +32 -0
  24. package/flow/binding-heal.ts +68 -0
  25. package/flow/cost-autopush.ts +162 -0
  26. package/flow/cost-command.ts +122 -0
  27. package/flow/cost-rows-journal.ts +296 -0
  28. package/flow/cost-rows-push.ts +112 -0
  29. package/flow/cost-rows-rows.ts +209 -0
  30. package/flow/cost-rows.ts +228 -0
  31. package/flow/cost.ts +173 -0
  32. package/flow/durable-refs.ts +287 -0
  33. package/flow/effect-gate.ts +137 -0
  34. package/flow/ensure-labels.ts +44 -0
  35. package/flow/frame.ts +172 -0
  36. package/flow/gates.ts +211 -0
  37. package/flow/jira-ref.ts +212 -0
  38. package/flow/label-gate.ts +149 -0
  39. package/flow/land-gate.ts +98 -0
  40. package/flow/lifecycle-policy.ts +162 -0
  41. package/flow/live-wait.ts +52 -0
  42. package/flow/liveness.ts +408 -0
  43. package/flow/loop-deps.ts +210 -0
  44. package/flow/loop-settle.ts +531 -0
  45. package/flow/loop.ts +437 -0
  46. package/flow/orchestrate.ts +45 -0
  47. package/flow/phase-command.ts +287 -0
  48. package/flow/phase-emoji.ts +13 -0
  49. package/flow/phase-prompts.ts +251 -0
  50. package/flow/picker.ts +307 -0
  51. package/flow/push.ts +475 -0
  52. package/flow/reconcile-command.ts +253 -0
  53. package/flow/reconcile-deps.ts +329 -0
  54. package/flow/reconcile.ts +738 -0
  55. package/flow/ref-kind.ts +80 -0
  56. package/flow/resume-picker.ts +222 -0
  57. package/flow/resume.ts +116 -0
  58. package/flow/run-health.ts +252 -0
  59. package/flow/run-liveness.ts +33 -0
  60. package/flow/state-metrics.ts +559 -0
  61. package/flow/status.ts +105 -0
  62. package/flow/step-output.ts +416 -0
  63. package/flow/steps.ts +73 -0
  64. package/flow/terminal-purge.ts +281 -0
  65. package/flow/turn-generation.ts +19 -0
  66. package/flow/v2-emission.ts +381 -0
  67. package/flow/v2-resume.ts +100 -0
  68. package/flow/vocabulary.ts +261 -0
  69. package/flow/widget.ts +448 -0
  70. package/flow/worktree-context.ts +136 -0
  71. package/flow-diagram/assemble.ts +27 -0
  72. package/flow-diagram/command.ts +252 -0
  73. package/flow-diagram/translate.ts +862 -0
  74. package/flow-diagram/vendor/harness-profiler/LICENSE +21 -0
  75. package/flow-diagram/vendor/harness-profiler/PIN.md +64 -0
  76. package/flow-diagram/vendor/harness-profiler/hp.js +960 -0
  77. package/flow-diagram/vendor/harness-profiler/load.ts +66 -0
  78. package/flow-diagram/vendor/harness-profiler/view.html +436 -0
  79. package/gates.ts +20 -0
  80. package/herdr/sync.ts +355 -0
  81. package/herdr/viewer.ts +432 -0
  82. package/implement/args.ts +21 -0
  83. package/implement/prompt.ts +98 -0
  84. package/implement/retry.ts +57 -0
  85. package/implement/routing.ts +31 -0
  86. package/index.ts +3206 -0
  87. package/mode-profile.ts +56 -0
  88. package/package.json +205 -4
  89. package/phase-state.ts +18 -0
  90. package/plan/args.ts +20 -0
  91. package/plan/prompt.ts +251 -0
  92. package/plan/size-check.ts +46 -0
  93. package/plan-tools/helpers.ts +177 -0
  94. package/plan-tools/jira-branch-tool.ts +54 -0
  95. package/plan-tools/jira-branch.ts +109 -0
  96. package/plan-tools/jira-issue.ts +85 -0
  97. package/plan-tools/resolve-feature-path.ts +30 -0
  98. package/plan-tools/resolve-notes-path.ts +37 -0
  99. package/plan-tools/resolve-spec-path.ts +31 -0
  100. package/pr-quality/index.ts +908 -0
  101. package/review/LICENSE +19 -0
  102. package/review/deviation.ts +252 -0
  103. package/review/review.ts +3053 -0
  104. package/review/sem-guidance.d.mts +8 -0
  105. package/review/sem-guidance.mjs +89 -0
  106. package/review/verdict.ts +13 -0
  107. package/run-state.ts +2046 -0
  108. package/run-title.ts +91 -0
  109. package/server/consumer.ts +146 -0
  110. package/server/daemon.ts +1094 -0
  111. package/server/lifecycle.ts +679 -0
  112. package/server/pending.ts +267 -0
  113. package/server/staleness.ts +320 -0
  114. package/server/writer.ts +86 -0
  115. package/shared/diff-base.ts +138 -0
  116. package/shared/flow-base.ts +210 -0
  117. package/shared/sonarqube-utils.ts +415 -0
  118. package/shared/stable-range.ts +102 -0
  119. package/slugify.ts +13 -0
  120. package/sonarqube/sonarqube.ts +821 -0
  121. package/spec-review/approve.ts +91 -0
  122. package/spec-review/args.ts +21 -0
  123. package/spec-review/aven-spec.ts +142 -0
  124. package/spec-review/commands.ts +336 -0
  125. package/spec-review/escape-regexp.ts +7 -0
  126. package/spec-review/open-in-pane.ts +166 -0
  127. package/spec-review/picker.ts +74 -0
  128. package/spec-review/prompt.ts +72 -0
  129. package/spec-review/publish-args.ts +33 -0
  130. package/spec-review/publish.ts +210 -0
  131. package/telemetry/attributes.ts +118 -0
  132. package/telemetry/content.ts +140 -0
  133. package/telemetry/dispatch.ts +521 -0
  134. package/telemetry/exporter.ts +124 -0
  135. package/telemetry/index.ts +776 -0
  136. package/telemetry/loki.ts +224 -0
  137. package/tools/code-review-parse.ts +518 -0
  138. package/tools/code-review.ts +312 -0
  139. package/tools/deviation.ts +737 -0
  140. package/tools/flow-submit.ts +302 -0
  141. package/tools/land.ts +608 -0
  142. package/tools/phase-complete.ts +476 -0
  143. package/tools/result.ts +18 -0
  144. package/tools/review-deps.ts +44 -0
  145. package/tools/run-attempt.ts +312 -0
  146. package/tools/spec-approve.ts +265 -0
  147. package/validate/args.ts +22 -0
  148. package/validate/deviation-gate.ts +229 -0
  149. package/validate/gate-target.ts +371 -0
  150. package/validate/gate.ts +79 -0
  151. package/validate/paths.ts +20 -0
  152. package/validate/personal.ts +104 -0
  153. package/validate/prompt.ts +525 -0
  154. package/validate/review-gate.ts +134 -0
  155. package/validate/subtask-range.ts +113 -0
  156. package/work-mode.ts +19 -0
  157. package/worktree/bootstrap.ts +243 -0
  158. package/worktree/branch.ts +168 -0
  159. package/worktree/herdr.ts +447 -0
  160. package/worktree/index.ts +1447 -0
  161. package/worktree/orphan.ts +384 -0
  162. package/worktree/remove-guards.ts +99 -0
  163. package/worktree/residue.ts +216 -0
  164. package/worktree/sweep.ts +526 -0
package/flow/picker.ts ADDED
@@ -0,0 +1,307 @@
1
+ // AIDEV-NOTE (PMR-ZGPY 4.1, D3/D10; 2.1 adds the ticket sentinel row and
2
+ // the 'tickets' plan kind): pure picker helpers over OpenGate[] —
3
+ // menu options, per-kind choice vocabularies (with a "show command instead"
4
+ // escape), and numbered fallback lines. The picker only ANSWERS gates via
5
+ // `/flow answer <ref> <choice>`; resuming paused runs is human-only and
6
+ // explicitly NOT this module's job (independent of T6). No IO here; the
7
+ // ctx.ui.select dispatch wiring lives in 4.2.
8
+
9
+ import type { AnswerChoice, ReadyTicket } from '../aven/client.ts'
10
+ import type { OpenGate } from './gates.ts'
11
+
12
+ // PMR-V31Z 7.1 — /flow answer serves the spec gate only (approve/reject);
13
+ // the pass/fail choices went with the deleted review gate.
14
+ const ANSWER_CHOICES: readonly AnswerChoice[] = ['approve', 'reject']
15
+
16
+ function isAnswerChoice(value: string): value is AnswerChoice {
17
+ return (ANSWER_CHOICES as readonly string[]).includes(value)
18
+ }
19
+
20
+ export interface PickerOption {
21
+ label: string
22
+ value: string
23
+ }
24
+
25
+ // AIDEV-NOTE (fix round 2): menu option values are the composite
26
+ // `${ref}|${kind}` — deriveRunGates can emit two gates with the SAME ref
27
+ // (e.g. flow-paused + answer-gate; PMR-V31Z 7.2 deleted the code-review
28
+ // kind), and a bare ref value would make the menu
29
+ // resolve first-match, presenting row B's vocabulary for row A. Refs cannot
30
+ // contain '|' (runFileName charset), so '|' is an unambiguous separator.
31
+ const GATE_VALUE_SEPARATOR = '|'
32
+
33
+ /** Split a composite menu value back into its ref + kind parts. A value
34
+ * without a separator yields an empty kind — ref+kind lookups then simply
35
+ * miss (fail-closed), never first-match another row. */
36
+ export function splitGateValue(value: string): { ref: string; kind: string } {
37
+ const idx = value.lastIndexOf(GATE_VALUE_SEPARATOR)
38
+ if (idx === -1) {
39
+ return { ref: value, kind: '' }
40
+ }
41
+ return { ref: value.slice(0, idx), kind: value.slice(idx + 1) }
42
+ }
43
+
44
+ // AIDEV-NOTE (fix round 2, /flow dispatch): the zero-arg /flow picker must not
45
+ // swallow other arg shapes — /flow X Y etc. keep parseFlowArgs's usage notify.
46
+ // The dispatch condition is this pure predicate so tests drive it directly.
47
+ export function isPickerInvocation(args: string): boolean {
48
+ return args.trim().length === 0
49
+ }
50
+
51
+ /** Escape value the caller (4.2) turns into printing the exact command. */
52
+ export const COMMAND_ESCAPE_VALUE = '__command__'
53
+
54
+ const COMMAND_ESCAPE_OPTION: PickerOption = {
55
+ label: 'show command instead',
56
+ value: COMMAND_ESCAPE_VALUE,
57
+ }
58
+
59
+ /** Sentinel value marking the ready-ticket picker row (2.1); the caller
60
+ * (2.2+) turns this selection into the ticket-picker flow. */
61
+ export const TICKET_ROW_VALUE = '__tickets__'
62
+
63
+ export const TICKET_ROW_OPTION: PickerOption = {
64
+ label: 'Start a flow on a ready ticket…',
65
+ value: TICKET_ROW_VALUE,
66
+ }
67
+
68
+ // AIDEV-NOTE (D5/D6; PMR-V31Z 7.2): the one remaining choice vocabulary —
69
+ // plan-review, flow-paused and answer-gate all answer with approve/reject
70
+ // (flow-paused answers close the gate; resuming stays human-only). The
71
+ // per-kind table died with the code-review leg: the ad-hoc code_review tool
72
+ // records its verdict itself through the blocking ask, so there is no picker
73
+ // vocabulary for it anymore.
74
+ const GATE_CHOICE_VALUES: readonly string[] = ['approve', 'reject']
75
+
76
+ /** One select option per gate; label is the gate's single-line title, value
77
+ * the composite ref|kind (fix round 2 — same-ref rows stay distinct). */
78
+ export function buildGateMenuOptions(
79
+ gates: readonly OpenGate[],
80
+ ): PickerOption[] {
81
+ const options: PickerOption[] = []
82
+ for (const gate of gates) {
83
+ options.push({
84
+ label: gate.title,
85
+ value: `${gate.ref}${GATE_VALUE_SEPARATOR}${gate.kind}`,
86
+ })
87
+ }
88
+ return options
89
+ }
90
+
91
+ /** Choice options for the second select (the answer step); escape option last. */
92
+ export function gateChoices(_gate: OpenGate): PickerOption[] {
93
+ const choices: PickerOption[] = []
94
+ for (const value of GATE_CHOICE_VALUES) {
95
+ choices.push({ label: value, value })
96
+ }
97
+ choices.push(COMMAND_ESCAPE_OPTION)
98
+ return choices
99
+ }
100
+
101
+ /** Numbered plain-text fallback: one title line + one answer-with line per gate. */
102
+ export function buildGateFallbackLines(gates: readonly OpenGate[]): string[] {
103
+ const lines: string[] = []
104
+ for (let i = 0; i < gates.length; i++) {
105
+ const gate = gates[i]
106
+ lines.push(`${i + 1}. ${gate.title}`)
107
+ const values = GATE_CHOICE_VALUES.join('|')
108
+ lines.push(` answer with: /flow answer ${gate.ref} ${values}`)
109
+ }
110
+ return lines
111
+ }
112
+
113
+ // ---------------------------------------------------------------------------
114
+ // PMR-ZGPY 4.2 (PMR-XQA1): pure dispatch table for the zero-arg /flow picker.
115
+ // index.ts stays thin: collect gates → planPickerOutcome → act on the plan.
116
+ // ---------------------------------------------------------------------------
117
+
118
+ /** Which step of the two-select interaction produced this state. gateKind
119
+ * threads the composite value's kind half so ref+kind lookups can never
120
+ * resolve a different row sharing the same ref (fix round 2). */
121
+ export type PickerSelection =
122
+ | { stage: 'gate-cancelled' }
123
+ | { stage: 'ticket-row-picked' }
124
+ | { stage: 'gate-picked'; gateRef: string; gateKind: string }
125
+ | { stage: 'choice-cancelled'; gateRef: string; gateKind: string }
126
+ | { stage: 'answered'; gateRef: string; gateKind: string; choice: string }
127
+
128
+ /**
129
+ * The action the /flow handler should take. Pure — tests drive the full
130
+ * matrix without any UI. 'answer' ref/choice feed executeFlowAnswer
131
+ * directly (D10); 'print-command' is the cancel/escape escape hatch that
132
+ * hands the user the exact /flow answer command instead.
133
+ */
134
+ export type PickerPlan =
135
+ | { kind: 'fallback'; lines: string[] }
136
+ | { kind: 'empty' }
137
+ | { kind: 'tickets' }
138
+ | { kind: 'menu'; options: PickerOption[] }
139
+ | {
140
+ kind: 'choice-menu'
141
+ gateRef: string
142
+ gateKind: string
143
+ options: PickerOption[]
144
+ }
145
+ | { kind: 'answer'; ref: string; choice: AnswerChoice }
146
+ | { kind: 'print-command'; ref: string; choices: string[] }
147
+
148
+ export function planPickerOutcome(
149
+ hasUI: boolean,
150
+ gates: readonly OpenGate[],
151
+ selection?: PickerSelection,
152
+ ): PickerPlan {
153
+ // Zero gates → 'tickets' (PMR-ZGPY 2.1): with no ready gates the bare
154
+ // /flow picker jumps straight to the ready-ticket picker (built in 2.2).
155
+ // Supersedes the fix-round-2 'empty' behavior; a selection can never
156
+ // coexist with zero gates (the menu requires gates to render).
157
+ if (gates.length === 0) {
158
+ return { kind: 'tickets' }
159
+ }
160
+ if (!hasUI) {
161
+ return { kind: 'fallback', lines: buildGateFallbackLines(gates) }
162
+ }
163
+ if (selection === undefined) {
164
+ // Zero gates cannot reach here: the hoisted guard above returns 'tickets'
165
+ // before any selection branch, and a selection can never coexist with
166
+ // zero gates (the menu requires gates to render).
167
+ return {
168
+ kind: 'menu',
169
+ options: [...buildGateMenuOptions(gates), TICKET_ROW_OPTION],
170
+ }
171
+ }
172
+ if (selection.stage === 'ticket-row-picked') {
173
+ // Sentinel row chosen: hand off to the ready-ticket picker (2.2).
174
+ return { kind: 'tickets' }
175
+ }
176
+ if (selection.stage === 'gate-cancelled') {
177
+ // Cancel at the first select: notify nothing.
178
+ return { kind: 'empty' }
179
+ }
180
+ // Ref+kind lookup (fix round 2): the kind half guarantees the vocabulary
181
+ // always matches the exact row the user picked, even across same-ref rows.
182
+ const gate = gates.find(
183
+ (g) => g.ref === selection.gateRef && g.kind === selection.gateKind,
184
+ )
185
+ if (gate === undefined) {
186
+ return { kind: 'empty' }
187
+ }
188
+ if (selection.stage === 'gate-picked') {
189
+ return {
190
+ kind: 'choice-menu',
191
+ gateRef: gate.ref,
192
+ gateKind: gate.kind,
193
+ options: gateChoices(gate),
194
+ }
195
+ }
196
+ if (selection.stage === 'choice-cancelled') {
197
+ // Cancel at the second select: print the exact command instead.
198
+ return {
199
+ kind: 'print-command',
200
+ ref: gate.ref,
201
+ choices: [...GATE_CHOICE_VALUES],
202
+ }
203
+ }
204
+ if (
205
+ selection.choice === COMMAND_ESCAPE_VALUE ||
206
+ !isAnswerChoice(selection.choice)
207
+ ) {
208
+ return {
209
+ kind: 'print-command',
210
+ ref: gate.ref,
211
+ choices: [...GATE_CHOICE_VALUES],
212
+ }
213
+ }
214
+ return { kind: 'answer', ref: gate.ref, choice: selection.choice }
215
+ }
216
+
217
+ // AIDEV-NOTE (PMR-3E1Z 2.2; PMR-W60H / ISC-A-1 picker invariants): pure
218
+ // decision table for the ready-ticket picker — the 'run' kind only RETURNS a
219
+ // ref for the handler to start the flow with; this module never resumes a
220
+ // paused run or emits messages (PMR-W60H / ISC-A-1 picker invariants).
221
+
222
+ // ---------------------------------------------------------------------------
223
+ // PMR-3E1Z 2.2: ready-ticket picker (the 'tickets' handoff from above).
224
+
225
+ export type TicketSelection =
226
+ | { stage: 'ticket-cancelled' }
227
+ | { stage: 'ticket-picked'; ref: string }
228
+
229
+ export type TicketPickerPlan =
230
+ | { kind: 'fallback'; lines: string[] }
231
+ | { kind: 'none'; line: string }
232
+ | { kind: 'failed' }
233
+ | { kind: 'menu'; options: PickerOption[] }
234
+ | { kind: 'silent' }
235
+ | { kind: 'run'; ref: string }
236
+
237
+ /** One select option per ready ticket; value is the ref itself so the
238
+ * handler's Map resolves label→ref without slicing (refs are unique here —
239
+ * unlike gates, tickets have no per-ref kind collision). */
240
+ export function buildTicketMenuOptions(
241
+ tickets: readonly ReadyTicket[],
242
+ ): PickerOption[] {
243
+ const options: PickerOption[] = []
244
+ for (const ticket of tickets) {
245
+ options.push({
246
+ label: `${ticket.ref} — ${ticket.title}`,
247
+ value: ticket.ref,
248
+ })
249
+ }
250
+ return options
251
+ }
252
+
253
+ /** Numbered plain-text fallback mirroring buildGateFallbackLines: one title
254
+ * line + one start-with line per ticket. */
255
+ export function buildTicketFallbackLines(
256
+ tickets: readonly ReadyTicket[],
257
+ ): string[] {
258
+ const lines: string[] = []
259
+ for (let i = 0; i < tickets.length; i++) {
260
+ const ticket = tickets[i]
261
+ lines.push(`${i + 1}. ${ticket.ref} — ${ticket.title}`)
262
+ lines.push(` start with: /flow ${ticket.ref}`)
263
+ }
264
+ return lines
265
+ }
266
+
267
+ /** Pure decision table for the ready-ticket picker. Null/empty checks come
268
+ * FIRST — a null/empty list has no menu to render regardless of selection
269
+ * state; the handler re-calls with the selection only after a valid menu
270
+ * resolved. Unknown-ref picked → 'silent', mirroring the gate lookup-miss
271
+ * rule above. */
272
+ export function planTicketPickerOutcome(
273
+ hasUI: boolean,
274
+ hadGates: boolean,
275
+ tickets: ReadyTicket[] | null,
276
+ selection?: TicketSelection,
277
+ ): TicketPickerPlan {
278
+ // classifyReadyTickets signals aven failure with null — the handler warns.
279
+ if (tickets === null) {
280
+ return { kind: 'failed' }
281
+ }
282
+ if (tickets.length === 0) {
283
+ // Notify once; the message depends on whether the user came from an
284
+ // empty gate menu or a bare /flow invocation.
285
+ if (hadGates) {
286
+ return { kind: 'none', line: 'No ready tickets.' }
287
+ }
288
+ return { kind: 'none', line: 'No open gates or ready tickets.' }
289
+ }
290
+ if (!hasUI) {
291
+ return { kind: 'fallback', lines: buildTicketFallbackLines(tickets) }
292
+ }
293
+ if (selection === undefined) {
294
+ return { kind: 'menu', options: buildTicketMenuOptions(tickets) }
295
+ }
296
+ if (selection.stage === 'ticket-cancelled') {
297
+ // Cancel at the ticket select: notify nothing.
298
+ return { kind: 'silent' }
299
+ }
300
+ const ticket = tickets.find((t) => t.ref === selection.ref)
301
+ if (ticket === undefined) {
302
+ // Lookup miss (stale list between menu render and pick): stay silent,
303
+ // same fail-closed rule as the gate picker.
304
+ return { kind: 'silent' }
305
+ }
306
+ return { kind: 'run', ref: ticket.ref }
307
+ }