@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,224 @@
1
+ // AIDEV-NOTE (PMR-ZV9Y 4.2): minimal Loki log-push client. Mirrors
2
+ // telemetry/exporter.ts (PMR-R8AG) on purpose — hand-rolled JSON POST, no
3
+ // collector dependency, no growing buffers, no retry queue. Fire-and-forget
4
+ // with a short AbortSignal timeout; failures are dropped silently after AT
5
+ // MOST ONE journal note per process (never a rejected promise into the host),
6
+ // and the once-per-process guard is the caller-owned `state` object (a
7
+ // per-LokiPusher singleton threaded through subtask 4.3's fan-out). Loki's
8
+ // push receiver accepts this JSON at <endpoint>/loki/api/v1/push.
9
+ //
10
+ // LABEL HYGIENE (ISC-A-1): the Loki stream carries ONLY the closed v2 enum set
11
+ // plus identity (job, flow_ref, mode, phase, state, reason, wait_kind, kind).
12
+ // Free text — note prose, halt reasons, deviation text — lives in the JSON
13
+ // `line` only, NEVER a label. buildLokiStream is the fixed label-builder so a
14
+ // caller structurally cannot inject an arbitrary label, and it drops
15
+ // empty/absent values so a reasonless state never emits `reason=""`.
16
+ //
17
+ // SESSION-SIDE ONLY: like the OTLP path, this pushes from the live session
18
+ // (subtask 4.3 wires the journal-writer fan-out that calls it). The daemon
19
+ // never pushes Loki — see the AIDEV-NOTE on the `telemetry.loki` block in
20
+ // config.ts (single strict reader).
21
+
22
+ import type { FlowReason, FlowState, WaitKind } from '../flow/vocabulary.ts'
23
+ import type { FlowPhase } from '../run-state.ts'
24
+
25
+ /** Closed Loki label set: job (fixed) + identity + v2 enums. No free text. */
26
+ export interface LokiLabels {
27
+ flow_ref?: string
28
+ mode?: string
29
+ phase?: FlowPhase
30
+ state?: FlowState
31
+ reason?: FlowReason | ''
32
+ wait_kind?: WaitKind
33
+ kind?: string
34
+ }
35
+
36
+ /** Resolved Loki push settings; null (caller) = disabled, push zero events. */
37
+ export interface LokiSettings {
38
+ endpoint: string
39
+ headers?: Record<string, string>
40
+ }
41
+
42
+ export interface LokiOutcome {
43
+ ok: boolean
44
+ error?: string
45
+ }
46
+
47
+ /**
48
+ * At-most-one-note-per-process guard (ISC-A-5). Held by the caller and shared
49
+ * across every push in a process; pushLokiEvent flips `noted` on first failure
50
+ * so later failures are silent. Mirrors the OTLP path's `state.noted`.
51
+ */
52
+ export interface LokiNoteState {
53
+ noted: boolean
54
+ }
55
+
56
+ /** Pusher dependencies, injected so tests run without a live endpoint (D2). */
57
+ export interface LokiPushDeps {
58
+ fetchImpl: LokiFetch
59
+ /** Wall-clock fallback (ms epoch) when the event carries no parseable `at`. */
60
+ now: () => number
61
+ /** Shared per-process once-guard; the caller owns and threads it (4.3). */
62
+ state: LokiNoteState
63
+ /** Journal-note hook, invoked at most once per process on first failure. */
64
+ onNote: (error: string) => void
65
+ timeoutMs?: number
66
+ }
67
+
68
+ /** Injectable fetch seam: returns at least the HTTP status. */
69
+ export type LokiFetch = (
70
+ url: string,
71
+ init: {
72
+ method: string
73
+ headers: Record<string, string>
74
+ body: string
75
+ signal: AbortSignal
76
+ },
77
+ ) => Promise<{ status: number }>
78
+
79
+ /** Fixed Loki job label — every pi-flow stream is `{job="pi-flow"}`. */
80
+ export const LOKI_JOB = 'pi-flow'
81
+
82
+ /** Loki push endpoint path (D2): the config stores only the base URL. */
83
+ export const LOKI_PUSH_PATH = '/loki/api/v1/push'
84
+
85
+ /** Default push timeout in ms — same posture as the OTLP exporter. */
86
+ export const LOKI_TIMEOUT_MS = 2500
87
+
88
+ /** The closed label keys that may ride a Loki stream, in a stable order. */
89
+ const LOKI_LABEL_KEYS = [
90
+ 'flow_ref',
91
+ 'mode',
92
+ 'phase',
93
+ 'state',
94
+ 'reason',
95
+ 'wait_kind',
96
+ 'kind',
97
+ ] as const satisfies readonly (keyof LokiLabels)[]
98
+
99
+ /**
100
+ * Build the fixed label map: `job` always, then each closed-set key present
101
+ * AND non-empty. Unknown keys are never copied (the key list is the guard) and
102
+ * an empty value (e.g. reasonless state's `reason: ''`) is dropped so it
103
+ * cannot inflate cardinality.
104
+ */
105
+ export function buildLokiStream(labels: LokiLabels): Record<string, string> {
106
+ const stream: Record<string, string> = { job: LOKI_JOB }
107
+ for (const key of LOKI_LABEL_KEYS) {
108
+ const value = labels[key]
109
+ if (typeof value === 'string' && value.length > 0) stream[key] = value
110
+ }
111
+ return stream
112
+ }
113
+
114
+ /**
115
+ * Build the Loki push request body (pure): one stream, one entry. `timestampNs`
116
+ * is an epoch-nanoseconds STRING — see lokiTimestampNs.
117
+ */
118
+ export function buildLokiPushBody(
119
+ labels: LokiLabels,
120
+ line: string,
121
+ timestampNs: string,
122
+ ): string {
123
+ return JSON.stringify({
124
+ streams: [
125
+ { stream: buildLokiStream(labels), values: [[timestampNs, line]] },
126
+ ],
127
+ })
128
+ }
129
+
130
+ /**
131
+ * Epoch-nanoseconds STRING for the Loki entry. Derives from the event's own
132
+ * `at` (ISO-8601) when it parses, else the caller's `now()` fallback — this
133
+ * keeps the Loki entry timestamp aligned with the journal event time even
134
+ * when a push is retried or delayed within the session. Nanoseconds EXCEED
135
+ * Number.MAX_SAFE_INTEGER at current epoch (~1.7e18), so the value is built by
136
+ * appending the six nano digits to the integer millisecond epoch rather than
137
+ * `ms * 1_000_000` (which would silently truncate low-order digits to float).
138
+ */
139
+ export function lokiTimestampNs(at: string | undefined, nowMs: number): string {
140
+ const eventMs = at === undefined ? Number.NaN : Date.parse(at)
141
+ const ms = Number.isNaN(eventMs) ? nowMs : eventMs
142
+ return `${Math.trunc(ms)}000000`
143
+ }
144
+
145
+ /** Pull the event's ISO `at` off a JSON line so pushLokiEvent can timestamp it. */
146
+ function eventAt(line: string): string | undefined {
147
+ let parsed: unknown
148
+ try {
149
+ parsed = JSON.parse(line)
150
+ } catch {
151
+ return undefined
152
+ }
153
+ if (parsed === null || typeof parsed !== 'object') return undefined
154
+ const at = (parsed as Record<string, unknown>).at
155
+ return typeof at === 'string' ? at : undefined
156
+ }
157
+
158
+ /**
159
+ * POST one event. Timeout via AbortSignal (default LOKI_TIMEOUT_MS); any
160
+ * failure (throw, abort, non-2xx) maps to {ok:false, error} — the caller
161
+ * decides whether to note it once, then drops it. Never throws.
162
+ */
163
+ export async function pushLoki(
164
+ endpoint: string,
165
+ headers: Record<string, string>,
166
+ labels: LokiLabels,
167
+ line: string,
168
+ timestampNs: string,
169
+ fetchImpl: LokiFetch,
170
+ timeoutMs = LOKI_TIMEOUT_MS,
171
+ ): Promise<LokiOutcome> {
172
+ try {
173
+ const base = endpoint.replace(/\/+$/, '')
174
+ const response = await fetchImpl(`${base}${LOKI_PUSH_PATH}`, {
175
+ method: 'POST',
176
+ headers: { 'content-type': 'application/json', ...headers },
177
+ body: buildLokiPushBody(labels, line, timestampNs),
178
+ // AbortSignal.timeout is unref'd — a dead endpoint never pins the loop.
179
+ signal: AbortSignal.timeout(timeoutMs),
180
+ })
181
+ if (response.status < 200 || response.status >= 300) {
182
+ return { ok: false, error: `Loki endpoint responded ${response.status}` }
183
+ }
184
+ return { ok: true }
185
+ } catch (err) {
186
+ const message = err instanceof Error ? err.message : `${err}`
187
+ return { ok: false, error: message }
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Fire-and-forget push (the 4.3 fan-out seam). `settings === null` or an empty
193
+ * endpoint → disabled, ZERO fetch calls and no note. Otherwise: void the POST
194
+ * promise (no-throw contract — the flow loop must never await or crash on it)
195
+ * and route any failure through `state`, so the FIRST failure in the process
196
+ * calls `onNote` once and every later one is silent.
197
+ *
198
+ * The entry timestamp comes from the line's own `at` when parseable, else the
199
+ * caller's `now()` — see lokiTimestampNs.
200
+ */
201
+ export function pushLokiEvent(
202
+ settings: LokiSettings | null,
203
+ labels: LokiLabels,
204
+ line: string,
205
+ deps: LokiPushDeps,
206
+ ): void {
207
+ if (settings === null) return
208
+ if (settings.endpoint.length === 0) return
209
+ const timestampNs = lokiTimestampNs(eventAt(line), deps.now())
210
+ void pushLoki(
211
+ settings.endpoint,
212
+ settings.headers ?? {},
213
+ labels,
214
+ line,
215
+ timestampNs,
216
+ deps.fetchImpl,
217
+ deps.timeoutMs ?? LOKI_TIMEOUT_MS,
218
+ ).then((outcome) => {
219
+ if (outcome.ok) return
220
+ if (deps.state.noted) return
221
+ deps.state.noted = true
222
+ deps.onNote(outcome.error ?? 'loki push failed')
223
+ })
224
+ }