@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/run-title.ts ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Run-title resolution for the pi-flow journal (PMR-9DZ0).
3
+ *
4
+ * AIDEV-NOTE: PMR-9DZ0 — sanitizeTitle MUST run at journal-write time, not at
5
+ * render time. The run journal is JSONL: one event object per line, so a
6
+ * multi-line title would corrupt the line format and every consumer of the
7
+ * journal. The render path (flow/run-health.ts) trusts the journal and only
8
+ * exposition-escapes label values — this sanitizer is the single guard.
9
+ *
10
+ * AIDEV-NOTE: session-side module (imports fetchJiraIssue, which shells out to
11
+ * acli via pi.exec). Imported only by index.ts; never add it to the four
12
+ * node-only closure roots guarded by test/cost-rows-node-only.test.ts.
13
+ *
14
+ * AIDEV-NOTE: resolveRunTitle NEVER throws by design — the title is a cosmetic
15
+ * enrichment of a run-start event; a failing acli lookup must not abort the
16
+ * flow run. Every failure path returns a warning string instead.
17
+ */
18
+
19
+ import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'
20
+ import { fetchJiraIssue } from './plan-tools/jira-issue.ts'
21
+ import type { WorkMode } from './work-mode.ts'
22
+
23
+ /** Hard cap for journaled/rendered titles (Grafana table/barchart cells). */
24
+ export const TITLE_MAX = 120
25
+
26
+ // AIDEV-NOTE: order matters (pinned by test/run-title.test.ts): control chars
27
+ // → space, collapse whitespace, trim, empty→undefined, THEN cap. Capping last
28
+ // means the cap sees a normalized string; capping first could leave a trailing
29
+ // space inside the 120-char window.
30
+ export function sanitizeTitle(raw: string): string | undefined {
31
+ let value = raw.replace(/[\u0000-\u001f\u007f-\u009f]/g, ' ')
32
+ value = value.replace(/\s+/g, ' ').trim()
33
+ if (!value) {
34
+ return undefined
35
+ }
36
+ return value.slice(0, TITLE_MAX)
37
+ }
38
+
39
+ export interface RunTitleInput {
40
+ mode: WorkMode
41
+ /** Personal mode only: the title from the aven snapshot read (zero extra calls). */
42
+ avenTitle?: string
43
+ /** Work mode only: typed Jira ID, else the epic's jira-ref metadata, else null. */
44
+ jiraKey: string | null
45
+ }
46
+
47
+ export interface RunTitleResult {
48
+ title?: string
49
+ warning: string | null
50
+ }
51
+
52
+ /**
53
+ * Resolve the run title best-effort. Never throws: every failure becomes a
54
+ * warning string so the caller can journal run-start without a title and keep
55
+ * the flow running.
56
+ */
57
+ export async function resolveRunTitle(
58
+ pi: ExtensionAPI,
59
+ input: RunTitleInput,
60
+ ): Promise<RunTitleResult> {
61
+ if (input.mode === 'personal') {
62
+ const avenTitle = sanitizeTitle(input.avenTitle ?? '')
63
+ if (avenTitle === undefined) {
64
+ return { warning: null }
65
+ }
66
+ return { title: avenTitle, warning: null }
67
+ }
68
+ if (input.jiraKey === null) {
69
+ return { warning: null }
70
+ }
71
+ try {
72
+ const issue = await fetchJiraIssue(pi, input.jiraKey)
73
+ const title = sanitizeTitle(issue.summary)
74
+ if (title === undefined) {
75
+ return { warning: null }
76
+ }
77
+ return { title, warning: null }
78
+ } catch (error) {
79
+ return { warning: lookupWarning(input.jiraKey, error) }
80
+ }
81
+ }
82
+
83
+ function lookupWarning(jiraKey: string, error: unknown): string {
84
+ let message: string
85
+ if (error instanceof Error) {
86
+ message = error.message
87
+ } else {
88
+ message = String(error)
89
+ }
90
+ return `title lookup failed for ${jiraKey} — continuing without a title (${message})`
91
+ }
@@ -0,0 +1,146 @@
1
+ // AIDEV-NOTE: PMR-XTQQ — pending-record consumer (durable run server,
2
+ // subtask 3.3). Called from index.ts on session_start (after the binding
3
+ // restore) and on agent_settled when the loop is inactive: if the restored
4
+ // binding's ref has a pending record, consume it (rename-first,
5
+ // exactly-once — see server/pending.ts) and re-present `/flow <ref>` to the
6
+ // user.
7
+ //
8
+ // AIDEV-NOTE: PMR-T9ZQ — GATE-AT-CALL-SITES CONTRACT: this module stays
9
+ // consent-free by design. Cross-session resume consent (the parked-run
10
+ // picker) is enforced at the index.ts call sites, strictly BEFORE
11
+ // resumeParkedRun is reached, so a session that declined, has no UI, or hit
12
+ // a picker error never gets here and therefore never renames/consumes a
13
+ // pending record. The OWN-session PMR-PJM0 restore keeps calling this
14
+ // automatically (design decision 1: the session already owns the run). Do
15
+ // NOT add a consent check inside the consumer — it has no ctx/ui and the
16
+ // gate must precede the rename.
17
+ //
18
+ // DEGRADE PATH (documented, not a bug): if sendUserMessage throws — e.g.
19
+ // because a run is already streaming, where a plain triggerTurn would be
20
+ // rejected and a deliverAs would be REQUIRED — consumption has already
21
+ // happened (correct: no duplicates), so we catch and notify the manual
22
+ // `/flow <ref>` fallback. session_start / settled timing makes plain
23
+ // injection the expected path.
24
+ //
25
+ // LATCH: an in-process per-ref Set owned by the caller (index.ts). It is
26
+ // added on a successful consumption attempt (before the send) and cleared by
27
+ // the /flow start path so a real rerun re-arms the consumer. agent_settled
28
+ // fires often; the latch plus the ENOENT-on-consumed rename contract makes
29
+ // repeated settles no-ops.
30
+ //
31
+ // The module is pure-ish: everything effectful (sender, fs, journal read)
32
+ // is injected, so tests use a fake sender + fake io and never need a live
33
+ // pi process.
34
+
35
+ import type { FlowRunEvent } from '../run-state.ts'
36
+ import { projectRunState, readRunEvents } from '../run-state.ts'
37
+ import {
38
+ type ConsumePendingResult,
39
+ consumePending,
40
+ defaultPendingIo,
41
+ isParkedLoop,
42
+ type PendingIo,
43
+ } from './pending.ts'
44
+
45
+ export interface ResumeConsumerDeps {
46
+ /** The flow ref from the restored binding. */
47
+ ref: string
48
+ agentDir: string
49
+ pendingDir: string
50
+ /** runServer.enabled kill switch — false short-circuits everything. */
51
+ enabled: () => boolean
52
+ /** True while a /flow loop is running in this process — no injection then. */
53
+ loopActive: () => boolean
54
+ /** In-process per-ref latch; caller clears entries on /flow start. */
55
+ latch: Set<string>
56
+ notify: (message: string, level: 'info' | 'warning' | 'error') => void
57
+ /** pi.sendUserMessage seam. */
58
+ send: (message: string) => void
59
+ readRunEvents?: (
60
+ agentDir: string,
61
+ ref: string,
62
+ ) => Promise<FlowRunEvent[] | null>
63
+ consumePending?: (
64
+ pendingDir: string,
65
+ ref: string,
66
+ verify: () => Promise<boolean> | boolean,
67
+ io?: PendingIo,
68
+ ) => Promise<ConsumePendingResult>
69
+ io?: PendingIo
70
+ }
71
+
72
+ export function resumedMessage(ref: string): string {
73
+ return `pi-flow: resumed parked run ${ref} — re-presented /flow ${ref}.`
74
+ }
75
+
76
+ /** Dropped/verify-fail fallback: today's manual-rerun wording. */
77
+ export function droppedMessage(ref: string): string {
78
+ return `pi-flow: restored flow binding for ${ref}. The parked run record failed verification and was dropped — rerun /flow ${ref} to continue phases.`
79
+ }
80
+
81
+ /** sendUserMessage threw — manual rerun is the fallback. */
82
+ export function sendFailedMessage(ref: string): string {
83
+ return `pi-flow: could not re-present /flow ${ref} automatically — run /flow ${ref} to resume the parked run.`
84
+ }
85
+
86
+ /**
87
+ * Verify closure over the journal: parked = latest loop-state snapshot has
88
+ * awaitingOutcome=true and status='running' (see server/pending.ts).
89
+ */
90
+ async function isStillParked(
91
+ deps: Pick<ResumeConsumerDeps, 'agentDir' | 'readRunEvents'>,
92
+ ref: string,
93
+ ): Promise<boolean> {
94
+ const read = deps.readRunEvents ?? readRunEvents
95
+ const events = await read(deps.agentDir, ref)
96
+ if (events === null) return false
97
+ const state = projectRunState(events)
98
+ if (state === null) return false
99
+ return isParkedLoop(state.loop)
100
+ }
101
+
102
+ /**
103
+ * Maybe consume the pending record for `ref` and re-present `/flow <ref>`.
104
+ * No-op when the kill switch is off, the loop is active, the latch holds, or
105
+ * there is nothing pending. Never throws on consumption failure paths.
106
+ */
107
+ export async function resumeParkedRun(deps: ResumeConsumerDeps): Promise<void> {
108
+ if (!deps.enabled()) return
109
+ if (deps.loopActive()) return
110
+ if (deps.latch.has(deps.ref)) return
111
+ const consume = deps.consumePending ?? consumePending
112
+ const io = deps.io ?? defaultPendingIo
113
+ let result: ConsumePendingResult
114
+ try {
115
+ result = await consume(
116
+ deps.pendingDir,
117
+ deps.ref,
118
+ () => isStillParked(deps, deps.ref),
119
+ io,
120
+ )
121
+ } catch (err) {
122
+ deps.notify(
123
+ `pi-flow: pending-record check for ${deps.ref} failed: ${String(err)}`,
124
+ 'warning',
125
+ )
126
+ return
127
+ }
128
+ if (result.consumed) {
129
+ // Latch BEFORE the send: a streaming sendUserMessage throw must not cause
130
+ // a second injection attempt on the next settle.
131
+ deps.latch.add(deps.ref)
132
+ deps.notify(resumedMessage(deps.ref), 'info')
133
+ // NOTE: during streaming a deliverAs would be REQUIRED; the settle/startup
134
+ // timing makes plain injection the expected path, and a throw is caught
135
+ // above with the manual-fallback notify (documented degrade).
136
+ try {
137
+ deps.send(`/flow ${deps.ref}`)
138
+ } catch (err) {
139
+ deps.notify(`${sendFailedMessage(deps.ref)} (${String(err)})`, 'warning')
140
+ }
141
+ return
142
+ }
143
+ if ('dropped' in result && result.dropped) {
144
+ deps.notify(droppedMessage(deps.ref), 'info')
145
+ }
146
+ }