@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
@@ -0,0 +1,261 @@
1
+ // AIDEV-NOTE: PMR-ZV9Y 1.1 — THE v2 state-machine vocabulary (plan PMR-ZV9Y,
2
+ // "The v2 vocabulary"). One closed enum per dimension plus the two derived
3
+ // tables (wait_kind, expected action). This module is deliberately PURE with
4
+ // ZERO imports so every consumer can reach it: the run journal (run-state.ts
5
+ // validates its v2 events against these sets), the daemon-side Prometheus
6
+ // derivation (flow/state-metrics.ts, node-only closure), the Loki pusher
7
+ // labels, the desktop-notify body, and the Grafana value mappings.
8
+ //
9
+ // Cardinality rule (ISC-A-1): nothing in this file is free text and nothing
10
+ // outside it may ever become a metric/log LABEL value. If a new state or
11
+ // reason is needed, add it here, mirror it in the Grafana value mappings, and
12
+ // extend the run-state tests — the deepEqual pins in test/vocabulary.test.ts
13
+ // are the drift alarm.
14
+ //
15
+ // Vocabulary history (PMR-0NRH): `human_ask_pending` was added here as the
16
+ // HUMAN wait for a mid-turn plain ask in the main session — the generic ask
17
+ // emitter journals it, so every current and future prompt-instructed ask
18
+ // becomes visible on the Waiting-on-me board with no per-gate reason and no
19
+ // prompt edit. `SELF_CLOSING_HUMAN_WAITS` was added in the same feature's
20
+ // review round (PMR-422Q): the per-reason "its owning seam journals its own
21
+ // `running` close" property that lets flow/v2-emission.ts hold the board for a live
22
+ // ask without latching on a wait nobody ever closes.
23
+ //
24
+ // v2 state is DERIVED, never a rename: LoopRunStatus ('running' | 'paused' |
25
+ // 'halted' | 'done') stays as-is because resume (flow/resume.ts), the
26
+ // phase-3 pending protocol and old journals depend on it (ISC-A-2).
27
+
28
+ /** The v2 run state (supersedes the free-text halt-note vocabulary). */
29
+ export type FlowState =
30
+ | 'running'
31
+ | 'waiting_human'
32
+ | 'waiting_machine'
33
+ | 'halted'
34
+ | 'done'
35
+ | 'stale'
36
+
37
+ /**
38
+ * The closed v2 reason enum. `answer_pending` was dropped from the brainstorm
39
+ * list (no emission site produces it — dead vocabulary); `prompt_send_failed`
40
+ * and `aven_read_failed` were added (real halt paths in flow/loop.ts the
41
+ * brainstorm missed); `subagent_ask_pending` was added by the worker-ask-wait
42
+ * fix (an in-flight child parked on ask_owner is a HUMAN wait —
43
+ * flow/loop-settle.ts emits it instead of `subagent_in_flight` when a pending
44
+ * ask exists);
45
+ * `human_ask_pending` was added by the mid-turn-ask fix PMR-0NRH (a mid-turn
46
+ * plain ask in the MAIN session is a HUMAN wait — the generic ask emitter
47
+ * journals it, so a prompt-instructed `ask_user_question` becomes visible on
48
+ * the Waiting-on-me board without any per-gate reason or prompt edit);
49
+ * `label_reset_failed` was added by PMR-B033 (the blocker route-back's gate
50
+ * reset — removing the stale implement completion label before re-injecting
51
+ * implement — failed, so the run halts fail-closed instead of auto-advancing
52
+ * on the stale label);
53
+ * `landing_check_failed` (PMR-5THZ) and `landing_continuation_exhausted`
54
+ * (PMR-HG75) were removed by PMR-V31Z (personal mode delivers via PR —
55
+ * feedback entry is label-ordered, so the merge-main landing gate and its
56
+ * continuation budget died with the vocabulary they emitted);
57
+ * `closeout_check_failed` was added by PMR-VE27 and RE-BASED by PMR-V31Z
58
+ * (the close-out policy — flow/lifecycle-policy.ts — could not verify the
59
+ * journaled gh-pr-create effect at a feedback close-out seam — missing,
60
+ * journal unreadable, or the probe dep absent — so the run halts
61
+ * fail-closed instead of advancing past feedback; every mode verifies the
62
+ * same PR-effect leg now, the ancestry leg the VE27 text described is
63
+ * superseded).
64
+ */
65
+ export type FlowReason =
66
+ | 'spec_approval_pending'
67
+ | 'code_review_pending'
68
+ | 'escalation'
69
+ | 'retry_budget_exhausted'
70
+ | 'blocker_budget_exhausted'
71
+ | 'label_miss'
72
+ | 'label_reset_failed'
73
+ | 'deviation_gate_rerun'
74
+ | 'subagent_in_flight'
75
+ | 'subagent_ask_pending'
76
+ | 'human_ask_pending'
77
+ | 'prompt_send_failed'
78
+ | 'aven_read_failed'
79
+ | 'closeout_check_failed'
80
+
81
+ /** Who can unblock a wait: the operator (human) or the machine itself. */
82
+ export type WaitKind = 'human' | 'machine'
83
+
84
+ /** Guard set for `FlowState` — mirrors the type, pinned by tests. */
85
+ export const FLOW_STATES: readonly FlowState[] = [
86
+ 'running',
87
+ 'waiting_human',
88
+ 'waiting_machine',
89
+ 'halted',
90
+ 'done',
91
+ 'stale',
92
+ ]
93
+
94
+ /** Guard set for `FlowReason` — mirrors the type, pinned by tests. */
95
+ export const FLOW_REASONS: readonly FlowReason[] = [
96
+ 'spec_approval_pending',
97
+ 'code_review_pending',
98
+ 'escalation',
99
+ 'retry_budget_exhausted',
100
+ 'blocker_budget_exhausted',
101
+ 'label_miss',
102
+ 'label_reset_failed',
103
+ 'deviation_gate_rerun',
104
+ 'subagent_in_flight',
105
+ 'subagent_ask_pending',
106
+ 'human_ask_pending',
107
+ 'prompt_send_failed',
108
+ 'aven_read_failed',
109
+ 'closeout_check_failed',
110
+ ]
111
+
112
+ /**
113
+ * reason -> wait_kind. The human reasons are the ones an operator can
114
+ * resolve; everything else clears itself (or needs a code/label fix, which is
115
+ * still not an interactive wait).
116
+ *
117
+ * AIDEV-NOTE (worker-ask-wait fix): `subagent_ask_pending` is human because the
118
+ * blocker is the OPERATOR answering an ask_owner question in a child pane — the
119
+ * machine half (`subagent_in_flight`, the child still working) is a different
120
+ * wait. Before this reason existed, that case was labelled machine and the
121
+ * "Waiting on me — act now" board missed it.
122
+ *
123
+ * AIDEV-NOTE (PMR-0NRH): `human_ask_pending` is human for the same reason one
124
+ * level up — the blocker is the OPERATOR answering an `ask_user_question` in the
125
+ * main session. Before it existed, a mid-turn ask in the main session was
126
+ * invisible: only child asks (subagent_ask_pending) and registered gate waits
127
+ * showed up on the board.
128
+ */
129
+ export const WAIT_KIND: Readonly<Record<FlowReason, WaitKind>> = {
130
+ spec_approval_pending: 'human',
131
+ code_review_pending: 'human',
132
+ escalation: 'human',
133
+ retry_budget_exhausted: 'machine',
134
+ blocker_budget_exhausted: 'machine',
135
+ label_miss: 'machine',
136
+ // AIDEV-NOTE: PMR-B033 — machine wait: the blocker route-back could not
137
+ // reset the stale implement completion label (removal dep absent, returned
138
+ // false, or threw). Nothing interactive to answer; recovery is fixing aven
139
+ // reachability/the ticket labels and rerunning /flow <ref>.
140
+ label_reset_failed: 'machine',
141
+ deviation_gate_rerun: 'machine',
142
+ subagent_in_flight: 'machine',
143
+ subagent_ask_pending: 'human',
144
+ human_ask_pending: 'human',
145
+ prompt_send_failed: 'machine',
146
+ aven_read_failed: 'machine',
147
+ // AIDEV-NOTE: PMR-VE27, re-based by PMR-V31Z — machine wait: the close-out
148
+ // policy could not verify the journaled gh-pr-create effect at a feedback
149
+ // close-out seam (effect missing, journal unreadable, or the probe dep
150
+ // absent — every mode verifies the same PR-effect leg now; the ancestry
151
+ // leg PMR-VE27 also dispatched on is superseded). Nothing interactive to
152
+ // answer; recovery is gathering the missing evidence (the gh-pr-create
153
+ // effect declared by flow_land <ref> during the feedback PR delivery)
154
+ // and rerunning /flow <ref> — the label-ordered mapping re-derives the
155
+ // phase from the labels.
156
+ closeout_check_failed: 'machine',
157
+ }
158
+
159
+ /**
160
+ * reason -> the command that unblocks the run, with `{ref}` as the placeholder.
161
+ * Only the human wait kinds have one; the Waiting-on-me board mirrors these
162
+ * strings as value mappings and the desktop notification uses them verbatim.
163
+ *
164
+ * AIDEV-NOTE (worker-ask-wait fix): `subagent_ask_pending` is the one human wait
165
+ * with NO slash command — the operator answers the child in its pane (or tells
166
+ * the parent to run `answer_subagent`), so its action is plain prose and
167
+ * deliberately carries no `{ref}` placeholder.
168
+ *
169
+ * AIDEV-NOTE (PMR-0NRH): `human_ask_pending` is the second human wait with no
170
+ * slash command — the operator answers the ask in the session it was asked in,
171
+ * so its action is prose with no `{ref}` placeholder either.
172
+ */
173
+ export const EXPECTED_ACTION: Readonly<Partial<Record<FlowReason, string>>> = {
174
+ spec_approval_pending: '/flow answer {ref} approve',
175
+ // AIDEV-NOTE: PMR-V31Z 7.2 — the old '/flow answer {ref} pass|fail' action
176
+ // died with the review gate (7.1); code_review_pending is now only the
177
+ // AD-HOC code_review tool's in-session ask, so the action is prose like
178
+ // the other ask-owned waits.
179
+ code_review_pending: 'answer the code_review ask (pass/fail in this session)',
180
+ escalation: 'intervene: /flow {ref} (or fix forward)',
181
+ subagent_ask_pending: 'answer the subagent (its pane or answer_subagent)',
182
+ human_ask_pending: 'answer the agent (the ask in this session)',
183
+ }
184
+
185
+ /** Rendered when nothing unblocks the run (no human action registered). */
186
+ export const NO_EXPECTED_ACTION = '—'
187
+
188
+ /**
189
+ * AIDEV-NOTE: PMR-422Q (PMR-0NRH review blocker 2) — the HUMAN waits whose
190
+ * OWNING SEAM journals its own `running` close, i.e. the wait is guaranteed a
191
+ * release by EITHER route: the owning seam's external `state-change{running}`
192
+ * close, OR the flow/v2-emission.ts live-wait LEASE expiring (flow/live-wait.ts —
193
+ * arming stamps it, the owning seam heartbeats it via
194
+ * `loop.renewLiveWaitLease`, and a wait that stops beating releases itself
195
+ * within `LIVE_WAIT_LEASE_SECONDS`). PMR-422Q made the second route real: the
196
+ * hold went wide (non-terminal states INCLUDING `running`) precisely because
197
+ * the lease bounds it, so "guaranteed release" no longer means "a close record
198
+ * will arrive" alone. flow/v2-emission.ts arms its live-wait hold ONLY for these
199
+ * reasons: a hold that nothing can release is a latch, and a latched hold
200
+ * silently drops every later loop-emitted wait for the rest of the process's
201
+ * run.
202
+ *
203
+ * Membership is by EMISSION SITE, not by wait kind:
204
+ * * `human_ask_pending` — IN. flow/ask-emitter.ts journals the close on
205
+ * tool_result / tool_execution_end for the toolCallId it opened.
206
+ * * `code_review_pending` — IN. tools/code-review.ts `closeReviewGate` journals
207
+ * step-stop + `state-change{running}` on a pass/fail answer.
208
+ * * `escalation` — OUT. `run_attempt` escalates through the shared fan-out
209
+ * writer and NEVER journals the running return; the loop's own
210
+ * snapshot('running') is the only thing that unsticks that journal (the
211
+ * PMR-ZV9Y fix-round P1 invariant), so arming here is exactly the latch.
212
+ * * `spec_approval_pending` — OUT. A loop-owned pause (`snapshot('paused', ...)`)
213
+ * whose close is the loop's own next snapshot; holding the loop against its
214
+ * own pause would deadlock the run.
215
+ * * `subagent_ask_pending` — OUT. Emitted by the loop's own settle branch; the
216
+ * child's answer is not an external state-change, so no external close ever
217
+ * arrives for it.
218
+ * * every machine reason — OUT by definition: the hold exists to protect a
219
+ * HUMAN wait against a competing loop emission.
220
+ */
221
+ export const SELF_CLOSING_HUMAN_WAITS: readonly FlowReason[] = [
222
+ 'code_review_pending',
223
+ 'human_ask_pending',
224
+ ]
225
+
226
+ /**
227
+ * True when a reason's owning seam journals its own `running` close, so a
228
+ * hold armed on it is guaranteed to be released. The empty reason (a state
229
+ * that carries none) is never self-closing.
230
+ */
231
+ export function reasonOwnsExternalClose(reason: FlowReason | ''): boolean {
232
+ if (reason === '') return false
233
+ return SELF_CLOSING_HUMAN_WAITS.includes(reason)
234
+ }
235
+
236
+ /**
237
+ * The wait kind for a reason. Out-of-vocabulary input (a leniently parsed
238
+ * journal, a future writer) degrades to `machine` so a drifted value can
239
+ * never fabricate a "waiting on you" row.
240
+ */
241
+ export function reasonWaitKind(reason: FlowReason): WaitKind {
242
+ if (!FLOW_REASONS.includes(reason)) return 'machine'
243
+ return WAIT_KIND[reason]
244
+ }
245
+
246
+ /**
247
+ * The single source for "what do I type to unblock this run?". Only a
248
+ * `waiting_human` state has an action; every other state/reason pair renders
249
+ * as NO_EXPECTED_ACTION.
250
+ */
251
+ export function expectedAction(
252
+ state: FlowState,
253
+ reason: FlowReason | '',
254
+ ref: string,
255
+ ): string {
256
+ if (state !== 'waiting_human') return NO_EXPECTED_ACTION
257
+ if (reason === '') return NO_EXPECTED_ACTION
258
+ const template = EXPECTED_ACTION[reason]
259
+ if (template === undefined) return NO_EXPECTED_ACTION
260
+ return template.replaceAll('{ref}', ref)
261
+ }