@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/review/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Derived from mitsuhiko/agent-stuff (https://github.com/mitsuhiko/agent-stuff)
2
+
3
+ Apache License
4
+ Version 2.0, January 2004
5
+ http://www.apache.org/licenses/
6
+
7
+ Copyright Armin Ronacher
8
+
9
+ Licensed under the Apache License, Version 2.0 (the "License");
10
+ you may not use this file except in compliance with the License.
11
+ You may obtain a copy of the License at
12
+
13
+ http://www.apache.org/licenses/LICENSE-2.0
14
+
15
+ Unless required by applicable law or agreed to in writing, software
16
+ distributed under the License is distributed on an "AS IS" BASIS,
17
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ See the License for the specific language governing permissions and
19
+ limitations under the License.
@@ -0,0 +1,252 @@
1
+ // AIDEV-NOTE: Deviation reviewer (PMR-M64D) — fresh-context subagent that
2
+ // compares the resolved spec against the git diff. Same split as verdict.ts:
3
+ // validate/deviation-gate.ts owns the artifact contract (DEVIATION_VERDICT_
4
+ // HEADING, DEVIATION_CONTRACT_PROMPT) and parses the artifact; this module
5
+ // only composes the reviewer prompt from those shared constants and spawns
6
+ // via the existing launchSubagent path (no new spawn infrastructure). The
7
+ // caller (validate prompt integration, subtask 3) resolves spec/diff/why
8
+ // inputs as strings — this module never reads git or aven itself.
9
+
10
+ import type { ExtensionContext } from '@earendil-works/pi-coding-agent'
11
+ import type { SubagentResult } from '@gtheys/pi-interactive-subagents'
12
+ import { launchSubagent, watchSubagent } from '@gtheys/pi-interactive-subagents'
13
+ import {
14
+ DEVIATION_CONTRACT_PROMPT,
15
+ DEVIATION_VERDICT_HEADING,
16
+ } from '../validate/deviation-gate.ts'
17
+
18
+ // AIDEV-NOTE: Below this length a spec section cannot meaningfully constrain
19
+ // a deviation comparison — the reviewer would be inventing requirements, so
20
+ // the prompt degrades to "insufficient spec to compare".
21
+ export const DEVIATION_MIN_SPEC_LENGTH = 40
22
+
23
+ export interface DeviationReviewInput {
24
+ mode: 'work' | 'personal'
25
+ spec: string
26
+ diff: string
27
+ commitMessages: string
28
+ avenNotes: string
29
+ /**
30
+ * AIDEV-NOTE: PMR-6EEW gate integrity — pinned range the reviewer must
31
+ * echo back. When present the prompt requires a reviewed_range line and
32
+ * the harness compares it against the pin character by character.
33
+ */
34
+ pinnedRange?: { baseSha: string; headSha: string }
35
+ /**
36
+ * AIDEV-NOTE: When set, the diff is NOT inlined: the tool wrote it to this
37
+ * file and the reviewer reads it with the read tool. Inlined multi-100KB
38
+ * diffs overflowed pi's Markdown renderer (stack overflow) on the user
39
+ * message, and rtk may condense `git diff` output — a file is exact.
40
+ */
41
+ diffPath?: string
42
+ }
43
+
44
+ // AIDEV-NOTE: Diffs above this size go to a file instead of the prompt.
45
+ export const DEVIATION_INLINE_DIFF_MAX = 30_000
46
+
47
+ // AIDEV-NOTE: file-mode read proof — the reviewer must echo the diff's line
48
+ // count; the tool compares it against the real count and rejects mismatches.
49
+ export const DIFF_LINES_PREFIX = 'diff_lines_read:'
50
+
51
+ /**
52
+ * AIDEV-NOTE: PMR-Q23K — count lines with `wc -l` semantics on purpose: a
53
+ * trailing newline TERMINATES the last line, it does not open a phantom
54
+ * one. The old split('\n').length counted one line too many on every git
55
+ * diff (they all end in a newline), so the gate demanded a 1143rd line
56
+ * that did not exist and no verdict could ever pass. A final unterminated
57
+ * segment still counts as a line.
58
+ * AIDEV-NOTE: PMR-Z2RA — this wc -l count is deliberately NOT what the
59
+ * reviewer's read tool reports. Read tools follow the split-on-newline
60
+ * convention and show expected + 1 on newline-terminated files, which is
61
+ * why checkDiffLinesRead accepts both convention counts.
62
+ */
63
+ export function countDiffLines(diff: string): number {
64
+ if (diff.length === 0) {
65
+ return 0
66
+ }
67
+ const segments = diff.split('\n')
68
+ if (segments.at(-1) === '') {
69
+ return segments.length - 1
70
+ }
71
+ return segments.length
72
+ }
73
+
74
+ /** LAST `diff_lines_read: <N>` line in the artifact, or null. */
75
+ export function extractDiffLinesRead(artifact: string): number | null {
76
+ const matches = [...artifact.matchAll(/^diff_lines_read:\s*(\d+)\s*$/gm)]
77
+ const last = matches.at(-1)
78
+ return last === undefined ? null : Number(last[1])
79
+ }
80
+
81
+ export type DiffLinesReadCheck =
82
+ | { ok: true }
83
+ | { ok: false; expected: number; got: number | null }
84
+
85
+ /**
86
+ * AIDEV-NOTE: The file-mode read-proof gate as one testable decision: the
87
+ * verdict passes only when the echoed `diff_lines_read` equals the real
88
+ * line count of the pinned diff. Extracted from the tool executor so the
89
+ * accept/reject contract is testable at this seam (PMR-Q23K).
90
+ * AIDEV-NOTE: PMR-Z2RA — readers count lines by convention, and two
91
+ * conventions are in circulation: wc -l (a trailing newline TERMINATES the
92
+ * last line, what countDiffLines reports) and split-on-newline (a trailing
93
+ * newline OPENS a phantom final segment, what many read tools report as the
94
+ * total). Every git diff ends in a newline, so a reviewer faithfully
95
+ * echoing its read tool's line total sends expected + 1 — that exact
96
+ * mismatch hard-blocked the PMR-ZGPY validate range. Accept precisely the
97
+ * two convention counts (and the split one ONLY for newline-terminated
98
+ * diffs, where the extra segment is phantom rather than real); a skipped
99
+ * line or an arbitrary skim count still fails, so the anti-skim guarantee
100
+ * is intact.
101
+ */
102
+ export function checkDiffLinesRead(
103
+ diff: string,
104
+ artifact: string,
105
+ ): DiffLinesReadCheck {
106
+ const expected = countDiffLines(diff)
107
+ const got = extractDiffLinesRead(artifact)
108
+ if (got === expected) {
109
+ return { ok: true }
110
+ }
111
+ if (diff.endsWith('\n') && got === expected + 1) {
112
+ return { ok: true }
113
+ }
114
+ return { ok: false, expected, got }
115
+ }
116
+
117
+ /** Fence text with a backtick run longer than any run inside it. */
118
+ export function fenceBlock(text: string, lang = ''): string {
119
+ const longest = Math.max(2, ...(text.match(/`+/g) ?? []).map((m) => m.length))
120
+ const f = '`'.repeat(longest + 1)
121
+ return `${f}${lang}\n${text}\n${f}`
122
+ }
123
+
124
+ // AIDEV-NOTE: PMR-MH1Y — PMR-C0YV burned 3 consecutive reviewer runs on
125
+ // artifacts whose declared count did not match the parseable lines: the
126
+ // reviewer enumerated findings as narrative prose (numbered lists quoting
127
+ // spec/notes text with backticks, regexes and angle brackets) and never
128
+ // restated them as contract lines. The contract below is therefore explicit
129
+ // about the section shape (ONLY contract lines, no prose, restate findings,
130
+ // no literal pipes inside fields) and ends with a mandatory self-check the
131
+ // reviewer performs before responding. The parser stays strict — this prompt
132
+ // is the emitter-side half of the fix, the raw-artifact excerpt in
133
+ // tools/deviation.ts is the diagnosis half.
134
+ const DEVIATION_EMITTER_INSTRUCTIONS = `End the review with a machine-readable deviation verdict section:
135
+ ${DEVIATION_VERDICT_HEADING}
136
+ deviations_count: <N>
137
+ ${DEVIATION_CONTRACT_PROMPT}
138
+ where N is the number of deviations, 0 when the diff matches the spec.
139
+
140
+ Verdict-section rules (the parser is STRICT — any violation fails the gate):
141
+ - The verdict section contains ONLY contract lines: the heading, the reviewed_range / diff_lines_read lines when required above, exactly one deviations_count line, and exactly N "- deviation:" lines. No prose, no summaries, no numbered lists, no other bullets inside the section.
142
+ - Emit the count line EXACTLY ONCE, directly under the heading (after reviewed_range / diff_lines_read when those are required).
143
+ - One deviation per line, each line starting exactly with "- deviation:". Never wrap a deviation across multiple lines. No markdown formatting (no **bold**, no \`code\`) inside a deviation line.
144
+ - Fields are separated by " | " — never use a "|" character inside the what/why/evidence fields themselves (write ";" or "/" instead).
145
+ - If you described deviations in prose earlier in your reply, restate EVERY one as a "- deviation:" line here — prose findings do not count.
146
+ - SELF-CHECK before responding: count the "- deviation:" lines in your verdict section. The number MUST equal N. If it does not, fix the section until it does.`
147
+
148
+ const ECHO_INSTRUCTIONS = `Pinned range you are reviewing (set by the harness — do NOT recompute it):
149
+ reviewed_range: <baseSha>..<headSha>
150
+ Copy that line EXACTLY into your verdict section, directly under the heading, before deviations_count. It is compared character by character against the pin; any difference invalidates your review.`
151
+
152
+ export function buildDeviationReviewPrompt(
153
+ input: DeviationReviewInput,
154
+ ): string {
155
+ const trimmedSpec = input.spec.trim()
156
+ let specSection: string
157
+ if (trimmedSpec.length >= DEVIATION_MIN_SPEC_LENGTH) {
158
+ specSection = `## Resolved spec (authoritative — the diff is judged against ONLY this text)
159
+
160
+ ${fenceBlock(trimmedSpec, 'markdown')}`
161
+ } else {
162
+ specSection = `## Resolved spec
163
+
164
+ The resolved spec is too thin to compare against (${trimmedSpec.length} chars, minimum ${DEVIATION_MIN_SPEC_LENGTH}). This is an INSUFFICIENT SPEC case: do not invent or infer requirements. Report "insufficient spec to compare" as your finding summary and emit deviations_count: 0 — the caller handles the insufficient-spec route.`
165
+ }
166
+
167
+ const commits = input.commitMessages.trim()
168
+ const notes = input.avenNotes.trim()
169
+ let commitsSection: string
170
+ if (commits.length > 0) {
171
+ commitsSection = fenceBlock(commits)
172
+ } else {
173
+ commitsSection = '(none recorded)'
174
+ }
175
+ let notesSection: string
176
+ if (notes.length > 0) {
177
+ notesSection = fenceBlock(notes, 'markdown')
178
+ } else {
179
+ notesSection = '(none recorded)'
180
+ }
181
+
182
+ let pinnedLine: string
183
+ if (input.pinnedRange !== undefined) {
184
+ pinnedLine = ` reviewed_range: ${input.pinnedRange.baseSha}..${input.pinnedRange.headSha}`
185
+ } else {
186
+ pinnedLine = '(no pin supplied — plain legacy mode)'
187
+ }
188
+
189
+ let diffSection: string
190
+ if (input.diffPath !== undefined) {
191
+ diffSection = `The diff is large, so it is in a file: ${input.diffPath}
192
+ Read ALL of it with the read tool (use offset/limit to page). Do NOT use git diff: the file is the exact pinned diff. Judging without reading every line of the file invalidates your review.
193
+ Proof of reading: in your verdict section, directly under the reviewed_range line, add the line \`${DIFF_LINES_PREFIX} <N>\` where N is the file's total line count, which you find by reading it to the end. A missing or wrong N invalidates your review.`
194
+ } else {
195
+ diffSection = fenceBlock(input.diff, 'diff')
196
+ }
197
+
198
+ return `You are a fresh-context spec-deviation reviewer. You have no knowledge of the work beyond the inputs below.
199
+
200
+ Mode: ${input.mode}. Compare the resolved spec against the git diff and report every place the implementation DEVIATES from the spec. Do not review code quality — deviation from the spec is the only finding type. Do not invent deviations: if the diff matches the spec, report 0.
201
+
202
+ Ground rules:
203
+ - Judge the diff against the spec text ONLY, never against unstated assumptions.
204
+ - Every deviation's "why" rationale MUST be labelled inferred, with the concrete evidence that led to the inference, e.g. (inferred: commit message says X but spec says Y).
205
+ - Bounded inputs: judge only the spec section and diff below — do not explore the repository history.
206
+ - Empty why-input sections simply carry no extra context.
207
+
208
+ ${specSection}
209
+
210
+ ## Pinned review target (PMR-6EEW gate integrity)
211
+
212
+ ${ECHO_INSTRUCTIONS}
213
+ ${pinnedLine}
214
+
215
+ ## Git diff vs base
216
+
217
+ ${diffSection}
218
+
219
+ ## Why-inputs (mined context — commit messages, aven notes: interview note, worktree note, oneshot outcome, escalation notes)
220
+
221
+ ### Commit messages
222
+ ${commitsSection}
223
+
224
+ ### Aven notes
225
+ ${notesSection}
226
+
227
+ ${DEVIATION_EMITTER_INSTRUCTIONS}`
228
+ }
229
+
230
+ /**
231
+ * AIDEV-NOTE: Spawn the deviation reviewer via launchSubagent (the same
232
+ * subagent path as startSubagentReview) and return its result. Fire-and-await
233
+ * rather than fire-and-steer: the validate flow (subtask 3) needs the
234
+ * artifact synchronously to parse and route. Spawn failure propagates to the
235
+ * caller — the validate flow decides how to degrade.
236
+ */
237
+ export async function startDeviationReview(
238
+ ctx: ExtensionContext,
239
+ prompt: string,
240
+ hint: string,
241
+ ): Promise<SubagentResult> {
242
+ const running = await launchSubagent(
243
+ {
244
+ agent: 'reviewer',
245
+ name: `Deviation check: ${hint}`,
246
+ task: prompt,
247
+ cwd: ctx.cwd,
248
+ },
249
+ ctx,
250
+ )
251
+ return watchSubagent(running, new AbortController().signal)
252
+ }