@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,210 @@
1
+ // AIDEV-NOTE: PMR-H1GA — the /flow loop's DI contract (FlowLoopDeps +
2
+ // FlowStartInput), moved verbatim from flow/loop.ts so loop.ts stays a
3
+ // lifecycle driver. Type-only module: every import is `import type`, so
4
+ // nothing here reaches a runtime closure. loop.ts re-exports both types
5
+ // (`export type { ... } from './loop-deps.ts'`) so index.ts and every loop
6
+ // test harness keep resolving them from flow/loop.ts unchanged.
7
+ import type { FlowRunEvent } from '../run-state.ts'
8
+ import type { FlowEnv } from './attribution.ts'
9
+ import type { PrEffectProbe } from './lifecycle-policy.ts'
10
+ import type { Phase } from './orchestrate.ts'
11
+ import type { PhasePromptInput } from './phase-prompts.ts'
12
+ import type { FlowState } from './vocabulary.ts'
13
+ import type { FlowWorktree } from './worktree-context.ts'
14
+
15
+ export interface FlowLoopState {
16
+ ref: string
17
+ currentPhase: Phase
18
+ awaitingOutcome: boolean
19
+ /** PMR-69V8: implement re-injections used this run (budget = MAX_ATTEMPTS - 1). */
20
+ blockerRetries: number
21
+ /** ISO time the current validate prompt was sent (blockers-note cutoff). */
22
+ validateStartedAt: string
23
+ promptInput: Omit<PhasePromptInput, 'labels'>
24
+ }
25
+
26
+ export interface FlowLoopDeps {
27
+ notify(message: string, level: 'info' | 'warning'): void
28
+ /** Best-effort journal write; must never throw. */
29
+ journal(event: FlowRunEvent): void
30
+ /**
31
+ * AIDEV-NOTE: PMR-ZV9Y 2.1 — best-effort v2 state-machine emission seam
32
+ * (`state-change`/`step-start`/`step-stop`). Optional and routed in index.ts
33
+ * to the SAME journal() writer as `journal`, so the durable run journal and
34
+ * the later telemetry fan-out (PMR-ZV9Y phase 4) see every v2 event. It is a
35
+ * SEPARATE dep name on purpose: the existing loop.test.ts / flow.test.ts
36
+ * harnesses never set it, so their exact `h.events` kind-sequence assertions
37
+ * stay byte-identical (ISC-A-2 — the v1 loop-state stream is untouched).
38
+ * The loop wraps the call in a try/catch, so a throwing seam must NEVER
39
+ * propagate into a phase (ISC-A-5).
40
+ */
41
+ journalV2?(event: FlowRunEvent): void
42
+ /** Current aven labels on the ref; null = aven unavailable/failed. */
43
+ readLabels(ref: string): Promise<string[] | null>
44
+ /**
45
+ * Aven `plan-state` metadata value on the ref (e.g. 'review', 'approved').
46
+ * null = aven failed OR the field is absent — the two are deliberately not
47
+ * distinguished; a null never triggers the awaiting-approval gate.
48
+ */
49
+ readPlanState(ref: string): Promise<string | null>
50
+ /** Write an aven note on the ref; false = write failed. */
51
+ avenNote(ref: string, text: string): Promise<boolean>
52
+ /**
53
+ * yolo 5.1 — apply the `error` TYPE label on the ref (bootstrap included);
54
+ * false = write failed. Optional so existing tests/consumers need no
55
+ * changes. AIDEV-NOTE: `error` is a type label, deliberately outside
56
+ * lifecycleLabelMutation, applied only for yolo runs on halt.
57
+ */
58
+ avenLabel?(ref: string, label: string): Promise<boolean>
59
+ /**
60
+ * PMR-69V8: true when an aven review-blockers note ("reviewed-at:") was
61
+ * written at/after sinceIso. Optional + fail-closed: absent or false keeps
62
+ * the plain halt, so a validate failure without blockers never retries.
63
+ */
64
+ hasFreshBlockers?(ref: string, sinceIso: string): Promise<boolean>
65
+ /**
66
+ * PMR-B033: remove a lifecycle label from the ref (the two-gate reset on
67
+ * blocker route-back — PMR-5THZ widened it from the implement gate alone
68
+ * to 'active' AND 'review-passed'). false = removal failed. Optional for
69
+ * type compatibility only — ABSENT IS FAIL-CLOSED: the reset is
70
+ * load-bearing (unlike the best-effort avenLabel), so a missing dep halts
71
+ * the run with label_reset_failed instead of silently re-introducing the
72
+ * premature advance the stale label causes.
73
+ */
74
+ removeAvenLabel?(ref: string, label: string): Promise<boolean>
75
+ /**
76
+ * AIDEV-NOTE: PMR-VE27 — the journaled gh-pr-create effect probe for the
77
+ * feedback CLOSE-OUT gate at the settle (all modes read this leg — PMR-V31Z superseded the ancestry leg:
78
+ * personal delivers via PR and journals the gh-pr-create effect too).
79
+ * Tri-state per lifecycle-policy.ts's PrEffectProbe: `missing` = a
80
+ * readable run journal with no declaration, `unreadable` = the journal
81
+ * read failed. Production: the prLandingEffect closure in index.ts
82
+ * (readRunEvents pre-check + createEffectJournal().find, PMR-VDVJ — the
83
+ * effect `flow_land` declares before authorizing). Optional for
84
+ * type compatibility only — ABSENT IS FAIL-CLOSED: the proof is
85
+ * load-bearing, so a missing dep maps to
86
+ * a null proof and checkCloseout refuses (the settle halts
87
+ * closeout_check_failed) instead of advancing done without PR-landing
88
+ * evidence. (PMR-YZ4D: the `route=pr` flag and the isFlowBranchLanded
89
+ * comparison are dead vocabulary from the deleted merge-main landing.)
90
+ */
91
+ prEffectFn?(ref: string): Promise<PrEffectProbe>
92
+
93
+ /** True when the ref is an epic with open children (implement routing guard). */
94
+ epicHasOpenChildren(ref: string): Promise<boolean>
95
+ /** True while this session has an in-flight subagent (mid-phase async wait). */
96
+ hasInFlightSubagent(): boolean
97
+ /**
98
+ * AIDEV-NOTE (worker-ask-wait fix): true when one of this session's in-flight
99
+ * subagents is parked on an ask_owner question (`asks/<childId>.json` exists
100
+ * for a live, non-lost assignment). The /flow loop uses it ONLY inside the
101
+ * hasInFlightSubagent branch to split that wait into the two situations the
102
+ * board must show apart: child working (waiting_machine{subagent_in_flight})
103
+ * vs child waiting for a human answer (waiting_human{subagent_ask_pending}).
104
+ * Optional and fail-closed — absent or false keeps the machine label, so
105
+ * existing fixtures and consumers need no changes.
106
+ */
107
+ hasPendingSubagentAsk?(): boolean
108
+ /**
109
+ * AIDEV-NOTE: PMR-2H3R — session-monotonic count of `turn_start` events.
110
+ * The loop stamps this at prompt injection and ignores a settle until a
111
+ * later turn starts, so an already-open turn cannot become the new phase's
112
+ * outcome. Optional for existing standalone harnesses; production wires it.
113
+ */
114
+ getTurnGeneration?(): number
115
+ sendPrompt(prompt: string): void
116
+ // AIDEV-NOTE: PMR-1CCT — optional active-ref sink so the review tool can
117
+ // resolve the flow worktree (flow/worktree-context.ts). Called with the
118
+ // ref on start and with null on every terminal transition (halt, pause,
119
+ // done). Optional so existing tests/consumers need no changes.
120
+ setActiveRef?(ref: string | null): void
121
+ // AIDEV-NOTE: PMR-HFK1 — attribution env target, stamped at run start,
122
+ // re-stamped per phase, cleared on terminal transitions. Optional +
123
+ // defaults to process.env so existing tests/consumers need no changes.
124
+ env?: FlowEnv
125
+ /**
126
+ * Called after every flow-env stamp/re-stamp/clear (start, phase change,
127
+ * halt, pause, done) so observers (telemetry) can close the phase that
128
+ * just ended without waiting for another span.
129
+ */
130
+ onFlowEnvChange?: () => void
131
+ /**
132
+ * Current main pi session file (null/absent = unknown). Journaled at every
133
+ * phase start so /flow-cost can attribute the main session's cost to
134
+ * phases (resumed flows may span several session files).
135
+ */
136
+ getSessionFile?: () => string | null
137
+ /**
138
+ * Called after every terminal transition (run complete, halt, pause for
139
+ * approval) with the ref that just ended, AFTER the flow env was cleared.
140
+ * Used to push cost metrics automatically.
141
+ */
142
+ onRunEnd?: (ref: string) => void
143
+ /**
144
+ * AIDEV-NOTE: PMR-81SX — best-effort post-success cleanup, called ONCE in
145
+ * advanceAfter's done branch AFTER loop-state teardown (snapshot done, state
146
+ * null, env cleared, active ref cleared). Never called on halt/pause (those
147
+ * fire onRunEnd — do not piggyback). The loop defensively try/catches this:
148
+ * a throwing implementation must never break the run end. Optional so
149
+ * existing tests/consumers need no changes. worktree is the binding captured
150
+ * BEFORE teardown (getFlowWorktree()) — it is already cleared by the time
151
+ * this runs; the explicit ref is also what cleanup's effects declaration
152
+ * must carry (never the 'worktree-ops' fallback).
153
+ */
154
+ onFlowDone?: (
155
+ ref: string,
156
+ mode: 'work' | 'personal',
157
+ worktree: FlowWorktree | undefined,
158
+ ) => Promise<void>
159
+ // AIDEV-NOTE: PMR-VZEV — clears the landing state (authorized;
160
+ // PMR-V31Z 7.2 deleted the humanReviewPassed marker it used to also
161
+ // clear) on every start so stale authorization from a
162
+ // superseded/prior flow cannot authorize a later landing.
163
+ resetLandingState?(): void
164
+ /**
165
+ * AIDEV-NOTE: PMR-422Q — LEASE-ARITHMETIC CLOCK ONLY (live-wait lease: see
166
+ * flow/live-wait.ts). `renewLiveWaitLease` and the hold's expiry check read
167
+ * this; it defaults to the module-local `now()` so production needs no
168
+ * wiring. It MUST NOT be used for event stamps: every journal `at` keeps the
169
+ * module `now()`, so the existing recency tests (which date external
170
+ * observations via `atOffset` against the REAL clock) stay real-clock-based.
171
+ * Tests inject a fake clock here to expire or renew a lease deterministically.
172
+ */
173
+ now?: () => string
174
+ }
175
+
176
+ export interface FlowStartInput {
177
+ ref: string
178
+ /** Ref the run is tracked by (Jira key in work mode); defaults to `ref`. */
179
+ trackRef?: string
180
+ phase: Phase
181
+ /** Per-run inputs for buildPhasePrompt (mode, gitRoot, workRoots, ...). */
182
+ promptInput: Omit<PhasePromptInput, 'labels'>
183
+ /** /flow --yolo: propagate the mode onto the prompt input + env stamp. */
184
+ yolo?: boolean
185
+ /**
186
+ * AIDEV-NOTE: PMR-XTQQ phase 2 — resume seed from the journal tail
187
+ * (flow/resume.ts). Restores blockerRetries and validateStartedAt exactly;
188
+ * absent → today's zero-defaults (back-compat). Phase position is never
189
+ * seeded — aven labels re-derive it.
190
+ */
191
+ resume?: {
192
+ blockerRetries: number
193
+ validateStartedAt: string
194
+ }
195
+ /**
196
+ * AIDEV-NOTE: PMR-ZV9Y 2.1 — v2 emission guard seed. The last state this
197
+ * run emitted (journal tail) so a restart/resume does NOT re-emit it. When
198
+ * absent the loop seeds from the journal itself in start()/resume (see
199
+ * resumeV2StateFromEvents). Explicitly seeded on the index.ts resume path.
200
+ */
201
+ resumeState?: FlowState
202
+ /**
203
+ * AIDEV-NOTE: PMR-ZV9Y 2.1 — seed the plan.spec-approval pairing flag from
204
+ * the journal tail on a cross-process resume (true when a step-start for
205
+ * plan.spec-approval was journaled without a matching step-stop). Absent on
206
+ * a fresh start; on a same-instance restart index.ts supplies the derived
207
+ * value so the stop stays paired with the start. See resumeV2StateFromEvents.
208
+ */
209
+ resumePlanApprovalOpen?: boolean
210
+ }