@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,252 @@
1
+ // AIDEV-NOTE (PMR-PVT5 4.1): /flow-diagram command registration, mirroring
2
+ // flow/cost-command.ts. All data access is injected (FlowDiagramDeps) so the
3
+ // handler is testable purely; index.ts (4.2) owns the real dep wiring and
4
+ // filename/open side effects. Every degradation step aborts with a single
5
+ // warning notify — the written file path is always the deliverable.
6
+
7
+ import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'
8
+ import type { AssignmentRecord } from '@gtheys/pi-interactive-subagents'
9
+ import {
10
+ collectMainSessionFiles,
11
+ collectSpawnNotes,
12
+ parseSessionEntries,
13
+ } from '../flow/cost.ts'
14
+ import { parseRefKind } from '../flow/ref-kind.ts'
15
+ import { isValidRef } from '../herdr/viewer.ts'
16
+ import type { FlowRunEvent } from '../run-state.ts'
17
+ import { renderDiagramHtml } from './assemble.ts'
18
+ import { buildDiagram, selectSegment } from './translate.ts'
19
+ import type { HpRenderer } from './vendor/harness-profiler/load.ts'
20
+
21
+ export interface FlowDiagramDeps {
22
+ /** Journal events for a ref; null when no journal exists. */
23
+ readRunEvents: (ref: string) => Promise<FlowRunEvent[] | null>
24
+ /** Durable assignment records (subagents data dir). */
25
+ listAssignmentRecords: () => AssignmentRecord[]
26
+ /** Raw session jsonl body, or null when unreadable (counted, not fatal). */
27
+ readSessionFile: (file: string) => string | null
28
+ /** Vendored renderer; null aborts the command (fail-graceful load). */
29
+ loadRenderer: () => HpRenderer | null
30
+ /**
31
+ * Write the self-contained HTML as
32
+ * `pi-flow/diagrams/<ref>.run<k>.<YYYYMMDD-HHmmss>.html` under the agent
33
+ * dir; resolves the written file path.
34
+ */
35
+ writeDiagram: (ref: string, runIndex: number, html: string) => Promise<string>
36
+ /** Best-effort open (xdg-open/open fallback); false = user opens by hand. */
37
+ openDiagram: (path: string) => Promise<boolean>
38
+ }
39
+
40
+ export type ParsedDiagramArgs =
41
+ | { kind: 'ok'; ref: string; run: number | null }
42
+ | { kind: 'help' }
43
+
44
+ /**
45
+ * Filename stamp for diagram files: local-time `YYYYMMDD-HHmmss`. Pure so
46
+ * index.ts can reuse it in the writeDiagram wiring and tests can pin it.
47
+ */
48
+ export function diagramStamp(ms: number): string {
49
+ const d = new Date(ms)
50
+ const pad = (n: number): string => String(n).padStart(2, '0')
51
+ const date = `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}`
52
+ const time = `${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`
53
+ return `${date}-${time}`
54
+ }
55
+
56
+ /**
57
+ * First token = ref (aven:/jira: prefix stripped via parseRefKind), optional
58
+ * second token = 1-based positive-integer run selector; anything else = help.
59
+ */
60
+ export function parseDiagramArgs(args: string): ParsedDiagramArgs {
61
+ const tokens = args
62
+ .trim()
63
+ .split(/\s+/)
64
+ .filter((t) => t.length > 0)
65
+ if (tokens.length === 0 || tokens.length > 2) return { kind: 'help' }
66
+ const kinded = parseRefKind(tokens[0] as string)
67
+ let ref: string
68
+ if (kinded.kind === 'jira') {
69
+ ref = kinded.jiraId
70
+ } else {
71
+ ref = kinded.ref
72
+ }
73
+ if (tokens.length === 1) {
74
+ return { kind: 'ok', ref, run: null }
75
+ }
76
+ const runToken = tokens[1] as string
77
+ if (!/^\d+$/.test(runToken)) return { kind: 'help' }
78
+ const run = Number.parseInt(runToken, 10)
79
+ if (run < 1) return { kind: 'help' }
80
+ return { kind: 'ok', ref, run }
81
+ }
82
+
83
+ /**
84
+ * AIDEV-NOTE (PMR-PVT5): degradation seam. Ordering — help, invalid ref,
85
+ * renderer, journal, session reads, translate, write, open. Session files
86
+ * that fail to read are counted and summarized in ONE info notify (never one
87
+ * per file); a missing session only means that lane lacks token totals.
88
+ * AIDEV-NOTE (PMR-GPQJ): `events` is the SELECTED run segment, not the whole
89
+ * journal — reads and the unreadable count stay scoped to that run.
90
+ */
91
+ async function collectSessionEntries(
92
+ ref: string,
93
+ segment: readonly FlowRunEvent[],
94
+ deps: FlowDiagramDeps,
95
+ notify: (message: string, level: 'info' | 'warning') => void,
96
+ ): Promise<Map<string, readonly unknown[]>> {
97
+ const wanted = new Set<string>()
98
+ for (const file of collectMainSessionFiles(segment)) wanted.add(file)
99
+ const notes = collectSpawnNotes(segment)
100
+ for (const note of notes) {
101
+ if (note.sessionFile !== null) wanted.add(note.sessionFile)
102
+ }
103
+ const byChild = new Map<string, AssignmentRecord>()
104
+ for (const record of deps.listAssignmentRecords()) {
105
+ byChild.set(record.childId, record)
106
+ }
107
+ for (const note of notes) {
108
+ if (note.sessionFile !== null) continue
109
+ const record = byChild.get(note.childId)
110
+ if (record !== undefined && record.sessionFile.length > 0) {
111
+ wanted.add(record.sessionFile)
112
+ }
113
+ }
114
+ const entries = new Map<string, readonly unknown[]>()
115
+ let misses = 0
116
+ for (const file of wanted) {
117
+ const raw = deps.readSessionFile(file)
118
+ if (raw === null) {
119
+ misses += 1
120
+ continue
121
+ }
122
+ entries.set(file, parseSessionEntries(raw))
123
+ }
124
+ if (misses > 0) {
125
+ notify(
126
+ `/flow-diagram ${ref}: ${misses} session files unreadable — lanes may lack token totals.`,
127
+ 'info',
128
+ )
129
+ }
130
+ return entries
131
+ }
132
+
133
+ export async function runFlowDiagram(
134
+ ref: string,
135
+ run: number | null,
136
+ deps: FlowDiagramDeps,
137
+ notify: (message: string, level: 'info' | 'warning') => void,
138
+ ): Promise<void> {
139
+ const renderer = deps.loadRenderer()
140
+ if (renderer === null) {
141
+ notify(
142
+ `/flow-diagram ${ref}: harness-profiler renderer failed to load — aborting.`,
143
+ 'warning',
144
+ )
145
+ return
146
+ }
147
+ const events = await deps.readRunEvents(ref)
148
+ if (events === null) {
149
+ notify(
150
+ `/flow-diagram ${ref}: no run journal found — nothing to diagram.`,
151
+ 'warning',
152
+ )
153
+ return
154
+ }
155
+ // AIDEV-NOTE (PMR-GPQJ): select the run segment BEFORE collecting session
156
+ // entries so only the selected run's session files are read and the
157
+ // unreadable summary counts that subset (multi-run journals previously
158
+ // read every run's files journal-wide).
159
+ const selection = selectSegment(events, run)
160
+ if (selection.kind === 'out-of-range') {
161
+ notify(
162
+ `/flow-diagram ${ref}: run ${run} out of range — journal has ${selection.runCount} run(s).`,
163
+ 'warning',
164
+ )
165
+ return
166
+ }
167
+ if (selection.kind === 'no-records') {
168
+ notify(
169
+ `/flow-diagram ${ref}: nothing to diagram — no run segments or records in the journal.`,
170
+ 'warning',
171
+ )
172
+ return
173
+ }
174
+ const sessionEntriesByFile = await collectSessionEntries(
175
+ ref,
176
+ selection.segment,
177
+ deps,
178
+ notify,
179
+ )
180
+ const result = buildDiagram(
181
+ ref,
182
+ run,
183
+ events,
184
+ deps.listAssignmentRecords(),
185
+ sessionEntriesByFile,
186
+ )
187
+ if (result.kind === 'out-of-range') {
188
+ notify(
189
+ `/flow-diagram ${ref}: run ${run} out of range — journal has ${result.runCount} run(s).`,
190
+ 'warning',
191
+ )
192
+ return
193
+ }
194
+ if (result.kind === 'no-records') {
195
+ notify(
196
+ `/flow-diagram ${ref}: nothing to diagram — no run segments or records in the journal.`,
197
+ 'warning',
198
+ )
199
+ return
200
+ }
201
+ // AIDEV-NOTE (PMR-9CHQ): skipped-record degradation is ONE summary
202
+ // warning (same style as the unreadable-session seam), never a throw.
203
+ if (result.warnings.length > 0) {
204
+ notify(
205
+ `/flow-diagram ${ref}: ${result.warnings.length} record(s) skipped — unparseable or missing timestamps.`,
206
+ 'warning',
207
+ )
208
+ }
209
+ const html = renderDiagramHtml(
210
+ renderer,
211
+ result.header,
212
+ result.records,
213
+ `${ref} run ${result.header.runIndex}`,
214
+ )
215
+ const path = await deps.writeDiagram(ref, result.header.runIndex, html)
216
+ const opened = await deps.openDiagram(path)
217
+ if (opened) {
218
+ notify(`/flow-diagram ${ref}: diagram written to ${path}`, 'info')
219
+ } else {
220
+ notify(`/flow-diagram ${ref}: open failed — diagram at ${path}`, 'warning')
221
+ }
222
+ }
223
+
224
+ export function registerFlowDiagramCommand(
225
+ pi: ExtensionAPI,
226
+ deps: FlowDiagramDeps,
227
+ ): void {
228
+ pi.registerCommand('flow-diagram', {
229
+ description:
230
+ 'Render the per-ticket flow timeline as a self-contained HTML sequence diagram. Usage: /flow-diagram <ref> [run]',
231
+ handler: async (args, ctx) => {
232
+ const parsed = parseDiagramArgs(args)
233
+ if (parsed.kind === 'help') {
234
+ ctx.ui.notify(
235
+ 'Usage: /flow-diagram <ref> [run] — render the per-ticket flow timeline as a self-contained HTML sequence diagram.',
236
+ 'info',
237
+ )
238
+ return
239
+ }
240
+ if (!isValidRef(parsed.ref)) {
241
+ ctx.ui.notify(
242
+ `/flow-diagram ${parsed.ref}: invalid ref (must match [A-Za-z0-9][A-Za-z0-9._-]{0,99}) — aborting.`,
243
+ 'warning',
244
+ )
245
+ return
246
+ }
247
+ await runFlowDiagram(parsed.ref, parsed.run, deps, (message, level) => {
248
+ ctx.ui.notify(message, level)
249
+ })
250
+ },
251
+ })
252
+ }