@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,138 @@
1
+ // AIDEV-NOTE: PMR-R4AP — ONE upstream-aware merge-base resolver shared by the
2
+ // review gate (review/review.ts) and the deviation gate (tools/deviation.ts),
3
+ // so the two gates can never disagree on the diff range (PMR-KKDC). Takes a
4
+ // narrow `exec` seam (pi.exec is compatible) so it is testable without Pi.
5
+
6
+ export interface ExecResult {
7
+ stdout: string
8
+ stderr?: string
9
+ code: number
10
+ }
11
+
12
+ // AIDEV-NOTE: timeoutMs is the optional per-call override for bounded process
13
+ // runners; callers that omit it retain the runner's own default.
14
+ export const EXEC_TIMEOUT_MARKER = 'git subprocess timed out'
15
+
16
+ export type Exec = (
17
+ cmd: string,
18
+ args: string[],
19
+ options?: { cwd?: string; timeoutMs?: number },
20
+ ) => Promise<ExecResult>
21
+
22
+ export interface FlowRange {
23
+ baseBranch: string
24
+ baseSha: string
25
+ headSha: string
26
+ /** `<baseSha>..HEAD` */
27
+ range: string
28
+ commitCount: number
29
+ }
30
+
31
+ export type FlowRangeResult =
32
+ | { ok: true; value: FlowRange }
33
+ | { ok: false; reason: string }
34
+
35
+ /**
36
+ * Merge base between HEAD and a branch: prefers `<branch>@{upstream}`, falls
37
+ * back to the local branch. Null when neither resolves.
38
+ */
39
+ export async function getMergeBase(
40
+ exec: Exec,
41
+ branch: string,
42
+ cwd?: string,
43
+ ): Promise<string | null> {
44
+ try {
45
+ const upstream = await exec(
46
+ 'git',
47
+ ['rev-parse', '--abbrev-ref', `${branch}@{upstream}`],
48
+ { cwd },
49
+ )
50
+ if (upstream.code === 0 && upstream.stdout.trim()) {
51
+ const mb = await exec(
52
+ 'git',
53
+ ['merge-base', 'HEAD', upstream.stdout.trim()],
54
+ { cwd },
55
+ )
56
+ if (mb.code === 0 && mb.stdout.trim()) {
57
+ return mb.stdout.trim()
58
+ }
59
+ }
60
+ const mb = await exec('git', ['merge-base', 'HEAD', branch], { cwd })
61
+ if (mb.code === 0 && mb.stdout.trim()) {
62
+ return mb.stdout.trim()
63
+ }
64
+ return null
65
+ } catch {
66
+ return null
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Resolve the review range for a flow worktree. Errors (never an empty
72
+ * result) when the base is unresolvable or HEAD == merge-base.
73
+ */
74
+ export async function resolveFlowRange(
75
+ exec: Exec,
76
+ cwd: string,
77
+ baseBranch: string,
78
+ baseRef: string = baseBranch,
79
+ ): Promise<FlowRangeResult> {
80
+ // AIDEV-NOTE: PMR-R4AP — deliberately NO @{upstream} lookup here. Flow work
81
+ // is local-first: origin/<base> can be behind local <base> (unpushed local
82
+ // merges), which would drag unrelated commits into the range. baseRef is
83
+ // the exact SHA recorded at worktree create (or the local base branch for
84
+ // legacy worktrees).
85
+ const mb = await exec('git', ['merge-base', 'HEAD', baseRef], { cwd })
86
+ const baseSha = mb.stdout.trim()
87
+ if (mb.code !== 0 || baseSha === '') {
88
+ return {
89
+ ok: false,
90
+ reason: `cannot resolve merge-base of HEAD and '${baseRef}' (base '${baseBranch}') in ${cwd}`,
91
+ }
92
+ }
93
+ const head = await exec('git', ['rev-parse', 'HEAD'], { cwd })
94
+ const headSha = head.stdout.trim()
95
+ if (head.code !== 0 || headSha === '') {
96
+ return { ok: false, reason: `git rev-parse HEAD failed in ${cwd}` }
97
+ }
98
+ if (headSha === baseSha) {
99
+ return {
100
+ ok: false,
101
+ reason: `empty range: HEAD == merge-base with '${baseBranch}' (${baseSha.slice(0, 9)}) in ${cwd}`,
102
+ }
103
+ }
104
+ const range = `${baseSha}..HEAD`
105
+ const count = await exec('git', ['rev-list', '--count', range], { cwd })
106
+ const commitCount = Number.parseInt(count.stdout.trim(), 10)
107
+ if (count.code !== 0 || Number.isNaN(commitCount)) {
108
+ return { ok: false, reason: `git rev-list failed for ${range} in ${cwd}` }
109
+ }
110
+ return {
111
+ ok: true,
112
+ value: { baseBranch, baseSha, headSha, range, commitCount },
113
+ }
114
+ }
115
+
116
+ export async function diffForRange(
117
+ exec: Exec,
118
+ cwd: string,
119
+ range: string,
120
+ ): Promise<string> {
121
+ const r = await exec('git', ['diff', range], { cwd })
122
+ if (r.code !== 0) {
123
+ throw new Error(`git diff ${range} failed in ${cwd}: ${r.stderr ?? ''}`)
124
+ }
125
+ return r.stdout
126
+ }
127
+
128
+ export async function commitMessagesForRange(
129
+ exec: Exec,
130
+ cwd: string,
131
+ range: string,
132
+ ): Promise<string> {
133
+ const r = await exec('git', ['log', range], { cwd })
134
+ if (r.code !== 0) {
135
+ throw new Error(`git log ${range} failed in ${cwd}: ${r.stderr ?? ''}`)
136
+ }
137
+ return r.stdout
138
+ }
@@ -0,0 +1,210 @@
1
+ // AIDEV-NOTE: PMR-R4AP — the flow's diff base is the branch the MAIN checkout
2
+ // was on when the worktree was created (main/develop/staging/...). It is
3
+ // stored as git config `branch.<feature>.flowbase` (repo-shared config, so
4
+ // the worktree can read it). No recorded base => callers error, never guess.
5
+ import type { Exec, ExecResult } from './diff-base.ts'
6
+
7
+ export type BaseResult =
8
+ | { ok: true; branch: string }
9
+ | { ok: false; reason: string }
10
+
11
+ /** Current branch of `cwd`; error on detached HEAD. */
12
+ export async function currentBranch(
13
+ exec: Exec,
14
+ cwd: string,
15
+ ): Promise<BaseResult> {
16
+ const r = await exec('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd })
17
+ const branch = r.stdout.trim()
18
+ if (r.code !== 0 || branch === '') {
19
+ return { ok: false, reason: `git rev-parse failed in ${cwd}` }
20
+ }
21
+ if (branch === 'HEAD') {
22
+ return {
23
+ ok: false,
24
+ reason: `detached HEAD in ${cwd} — check out the base branch (main/develop/...) first`,
25
+ }
26
+ }
27
+ return { ok: true, branch }
28
+ }
29
+
30
+ export interface FlowBase {
31
+ branch: string
32
+ /** Exact commit the worktree branch started from; null for legacy worktrees. */
33
+ sha: string | null
34
+ }
35
+
36
+ export type FlowBaseResult =
37
+ | { ok: true; value: FlowBase }
38
+ | { ok: false; reason: string }
39
+
40
+ /** Resolve a branch to its exact commit SHA in `cwd`. */
41
+ export async function resolveBaseSha(
42
+ exec: Exec,
43
+ cwd: string,
44
+ branch: string,
45
+ ): Promise<BaseResult> {
46
+ const r = await exec('git', ['rev-parse', '--verify', `${branch}^{commit}`], {
47
+ cwd,
48
+ })
49
+ const sha = r.stdout.trim()
50
+ if (r.code !== 0 || sha === '') {
51
+ return { ok: false, reason: `cannot resolve '${branch}' in ${cwd}` }
52
+ }
53
+ return { ok: true, branch: sha }
54
+ }
55
+
56
+ export type MergeBaseResult =
57
+ | { ok: true; sha: string }
58
+ | { ok: false; reason: string }
59
+
60
+ // AIDEV-NOTE: PMR-1KAE — merge-base is the rebase-aware staleness anchor:
61
+ // for an unrebased flow branch it equals the recorded
62
+ // branch.<branch>.flowbasesha (the pre-rebase base), and after the branch
63
+ // is rebased onto the base it equals the base tip — so staleness detection
64
+ // self-heals where the recorded config value cannot (no rebase ever updates
65
+ // flowbasesha). Fail-closed: any git error or empty output is an error
66
+ // result, never a guess.
67
+ export async function resolveMergeBase(
68
+ exec: Exec,
69
+ cwd: string,
70
+ refA: string,
71
+ refB: string,
72
+ ): Promise<MergeBaseResult> {
73
+ const r = await exec('git', ['merge-base', refA, refB], { cwd })
74
+ const sha = r.stdout.trim()
75
+ if (r.code !== 0 || sha === '') {
76
+ return {
77
+ ok: false,
78
+ reason: `git merge-base ${refA} ${refB} failed in ${cwd}`,
79
+ }
80
+ }
81
+ return { ok: true, sha }
82
+ }
83
+
84
+ export type AncestryResult =
85
+ | { ok: true; value: boolean }
86
+ | { ok: false; reason: string }
87
+
88
+ // AIDEV-NOTE: PMR-1KAE — exit-code ancestry probe: git merge-base
89
+ // --is-ancestor exits 0 when ancestorRef is an ancestor of descendantRef
90
+ // (equality included), 1 when it is not, anything else is a git error. The
91
+ // distinction matters: a failure must refuse the landing fail-closed, a "no"
92
+ // must not.
93
+ export async function refIsAncestor(
94
+ exec: Exec,
95
+ cwd: string,
96
+ ancestorRef: string,
97
+ descendantRef: string,
98
+ ): Promise<AncestryResult> {
99
+ const r = await exec(
100
+ 'git',
101
+ ['merge-base', '--is-ancestor', ancestorRef, descendantRef],
102
+ { cwd },
103
+ )
104
+ if (r.code === 0) return { ok: true, value: true }
105
+ if (r.code === 1) return { ok: true, value: false }
106
+ return {
107
+ ok: false,
108
+ reason: `git merge-base --is-ancestor ${ancestorRef} ${descendantRef} failed in ${cwd} (${(r.stderr ?? '').trim()})`,
109
+ }
110
+ }
111
+
112
+ const CONFIG_WRITE_ATTEMPTS = 4
113
+ const CONFIG_WRITE_DELAY_MS = 50
114
+
115
+ async function writeConfigWithRetry(
116
+ exec: Exec,
117
+ cwd: string,
118
+ key: string,
119
+ value: string,
120
+ delayMs: number,
121
+ ): Promise<ExecResult> {
122
+ // AIDEV-NOTE: PMR-R4AP — concurrent sessions share .git/config; git takes
123
+ // config.lock and a simultaneous writer fails immediately. Retry with a short
124
+ // linear backoff, then surface the last error.
125
+ let last: ExecResult = { stdout: '', code: 1 }
126
+ for (let attempt = 1; attempt <= CONFIG_WRITE_ATTEMPTS; attempt++) {
127
+ last = await exec('git', ['config', key, value], { cwd })
128
+ if (last.code === 0) return last
129
+ if (attempt < CONFIG_WRITE_ATTEMPTS) {
130
+ await new Promise((resolve) => setTimeout(resolve, delayMs * attempt))
131
+ }
132
+ }
133
+ return last
134
+ }
135
+
136
+ /** Record base branch + exact SHA as branch.<feature>.flowbase / .flowbasesha. */
137
+ export async function recordFlowBase(
138
+ exec: Exec,
139
+ cwd: string,
140
+ featureBranch: string,
141
+ baseBranch: string,
142
+ baseSha: string,
143
+ delayMs: number = CONFIG_WRITE_DELAY_MS,
144
+ ): Promise<BaseResult> {
145
+ for (const [key, value] of [
146
+ ['flowbase', baseBranch],
147
+ ['flowbasesha', baseSha],
148
+ ] as const) {
149
+ const r = await writeConfigWithRetry(
150
+ exec,
151
+ cwd,
152
+ `branch.${featureBranch}.${key}`,
153
+ value,
154
+ delayMs,
155
+ )
156
+ if (r.code !== 0) {
157
+ return {
158
+ ok: false,
159
+ reason: `git config branch.${featureBranch}.${key} failed after ${CONFIG_WRITE_ATTEMPTS} attempts: ${r.stderr ?? ''}`,
160
+ }
161
+ }
162
+ }
163
+ return { ok: true, branch: baseBranch }
164
+ }
165
+
166
+ export async function clearFlowBase(
167
+ exec: Exec,
168
+ cwd: string,
169
+ featureBranch: string,
170
+ ): Promise<void> {
171
+ for (const key of ['flowbase', 'flowbasesha']) {
172
+ await exec('git', ['config', '--unset', `branch.${featureBranch}.${key}`], {
173
+ cwd,
174
+ })
175
+ }
176
+ }
177
+
178
+ /** Read the recorded base of the branch checked out in `cwd`. */
179
+ export async function readFlowBase(
180
+ exec: Exec,
181
+ cwd: string,
182
+ ): Promise<FlowBaseResult> {
183
+ const cur = await currentBranch(exec, cwd)
184
+ if (!cur.ok) return cur
185
+ const b = await exec('git', ['config', `branch.${cur.branch}.flowbase`], {
186
+ cwd,
187
+ })
188
+ const branch = b.stdout.trim()
189
+ if (b.code !== 0 || branch === '') {
190
+ return {
191
+ ok: false,
192
+ // AIDEV-NOTE: PMR-182S — unset flowbase usually means the session is on
193
+ // the main checkout (flow worktree not bound), not a legacy worktree.
194
+ // Never suggest setting flowbase on the current branch: it masks that.
195
+ // PMR-81SX: the success path now clears these keys — cleanupFlowWorktree
196
+ // (worktree/index.ts) calls clearFlowBase on the main checkout after
197
+ // every successful removal, so an unset flowbase after cleanup is the
198
+ // expected end state, not a stale-config gap.
199
+ reason: `no recorded flow base for '${cur.branch}' — the flow worktree is not bound (cwd is '${cwd}', not the flow worktree). Resume: run /flow <ref> to re-enter the flow, then worktree action=bind with the worktree path and branch, and re-run. Do NOT set branch.${cur.branch}.flowbase by hand.`,
200
+ }
201
+ }
202
+ const s = await exec('git', ['config', `branch.${cur.branch}.flowbasesha`], {
203
+ cwd,
204
+ })
205
+ const sha = s.stdout.trim()
206
+ return {
207
+ ok: true,
208
+ value: { branch, sha: s.code === 0 && sha !== '' ? sha : null },
209
+ }
210
+ }