@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.
- package/README.md +833 -2
- package/adhoc/tracker.ts +360 -0
- package/aven/client.ts +996 -0
- package/aven/status.ts +395 -0
- package/brainstorm/args.ts +24 -0
- package/brainstorm/artifact.ts +67 -0
- package/brainstorm/paths.ts +43 -0
- package/brainstorm/prompt.ts +172 -0
- package/brainstorm/refs.ts +41 -0
- package/config.schema.json +161 -0
- package/config.ts +577 -0
- package/feedback/args.ts +21 -0
- package/feedback/prompt.ts +177 -0
- package/feedback/triage.ts +88 -0
- package/flow/agent-role.ts +46 -0
- package/flow/answer-wake.ts +212 -0
- package/flow/answer.ts +183 -0
- package/flow/args.ts +48 -0
- package/flow/ask-emitter.ts +331 -0
- package/flow/attribution.ts +145 -0
- package/flow/aven-resolve-exec.ts +95 -0
- package/flow/aven-resolve.ts +80 -0
- package/flow/awaiting-approval.ts +32 -0
- package/flow/binding-heal.ts +68 -0
- package/flow/cost-autopush.ts +162 -0
- package/flow/cost-command.ts +122 -0
- package/flow/cost-rows-journal.ts +296 -0
- package/flow/cost-rows-push.ts +112 -0
- package/flow/cost-rows-rows.ts +209 -0
- package/flow/cost-rows.ts +228 -0
- package/flow/cost.ts +173 -0
- package/flow/durable-refs.ts +287 -0
- package/flow/effect-gate.ts +137 -0
- package/flow/ensure-labels.ts +44 -0
- package/flow/frame.ts +172 -0
- package/flow/gates.ts +211 -0
- package/flow/jira-ref.ts +212 -0
- package/flow/label-gate.ts +149 -0
- package/flow/land-gate.ts +98 -0
- package/flow/lifecycle-policy.ts +162 -0
- package/flow/live-wait.ts +52 -0
- package/flow/liveness.ts +408 -0
- package/flow/loop-deps.ts +210 -0
- package/flow/loop-settle.ts +531 -0
- package/flow/loop.ts +437 -0
- package/flow/orchestrate.ts +45 -0
- package/flow/phase-command.ts +287 -0
- package/flow/phase-emoji.ts +13 -0
- package/flow/phase-prompts.ts +251 -0
- package/flow/picker.ts +307 -0
- package/flow/push.ts +475 -0
- package/flow/reconcile-command.ts +253 -0
- package/flow/reconcile-deps.ts +329 -0
- package/flow/reconcile.ts +738 -0
- package/flow/ref-kind.ts +80 -0
- package/flow/resume-picker.ts +222 -0
- package/flow/resume.ts +116 -0
- package/flow/run-health.ts +252 -0
- package/flow/run-liveness.ts +33 -0
- package/flow/state-metrics.ts +559 -0
- package/flow/status.ts +105 -0
- package/flow/step-output.ts +416 -0
- package/flow/steps.ts +73 -0
- package/flow/terminal-purge.ts +281 -0
- package/flow/turn-generation.ts +19 -0
- package/flow/v2-emission.ts +381 -0
- package/flow/v2-resume.ts +100 -0
- package/flow/vocabulary.ts +261 -0
- package/flow/widget.ts +448 -0
- package/flow/worktree-context.ts +136 -0
- package/flow-diagram/assemble.ts +27 -0
- package/flow-diagram/command.ts +252 -0
- package/flow-diagram/translate.ts +862 -0
- package/flow-diagram/vendor/harness-profiler/LICENSE +21 -0
- package/flow-diagram/vendor/harness-profiler/PIN.md +64 -0
- package/flow-diagram/vendor/harness-profiler/hp.js +960 -0
- package/flow-diagram/vendor/harness-profiler/load.ts +66 -0
- package/flow-diagram/vendor/harness-profiler/view.html +436 -0
- package/gates.ts +20 -0
- package/herdr/sync.ts +355 -0
- package/herdr/viewer.ts +432 -0
- package/implement/args.ts +21 -0
- package/implement/prompt.ts +98 -0
- package/implement/retry.ts +57 -0
- package/implement/routing.ts +31 -0
- package/index.ts +3206 -0
- package/mode-profile.ts +56 -0
- package/package.json +205 -4
- package/phase-state.ts +18 -0
- package/plan/args.ts +20 -0
- package/plan/prompt.ts +251 -0
- package/plan/size-check.ts +46 -0
- package/plan-tools/helpers.ts +177 -0
- package/plan-tools/jira-branch-tool.ts +54 -0
- package/plan-tools/jira-branch.ts +109 -0
- package/plan-tools/jira-issue.ts +85 -0
- package/plan-tools/resolve-feature-path.ts +30 -0
- package/plan-tools/resolve-notes-path.ts +37 -0
- package/plan-tools/resolve-spec-path.ts +31 -0
- package/pr-quality/index.ts +908 -0
- package/review/LICENSE +19 -0
- package/review/deviation.ts +252 -0
- package/review/review.ts +3053 -0
- package/review/sem-guidance.d.mts +8 -0
- package/review/sem-guidance.mjs +89 -0
- package/review/verdict.ts +13 -0
- package/run-state.ts +2046 -0
- package/run-title.ts +91 -0
- package/server/consumer.ts +146 -0
- package/server/daemon.ts +1094 -0
- package/server/lifecycle.ts +679 -0
- package/server/pending.ts +267 -0
- package/server/staleness.ts +320 -0
- package/server/writer.ts +86 -0
- package/shared/diff-base.ts +138 -0
- package/shared/flow-base.ts +210 -0
- package/shared/sonarqube-utils.ts +415 -0
- package/shared/stable-range.ts +102 -0
- package/slugify.ts +13 -0
- package/sonarqube/sonarqube.ts +821 -0
- package/spec-review/approve.ts +91 -0
- package/spec-review/args.ts +21 -0
- package/spec-review/aven-spec.ts +142 -0
- package/spec-review/commands.ts +336 -0
- package/spec-review/escape-regexp.ts +7 -0
- package/spec-review/open-in-pane.ts +166 -0
- package/spec-review/picker.ts +74 -0
- package/spec-review/prompt.ts +72 -0
- package/spec-review/publish-args.ts +33 -0
- package/spec-review/publish.ts +210 -0
- package/telemetry/attributes.ts +118 -0
- package/telemetry/content.ts +140 -0
- package/telemetry/dispatch.ts +521 -0
- package/telemetry/exporter.ts +124 -0
- package/telemetry/index.ts +776 -0
- package/telemetry/loki.ts +224 -0
- package/tools/code-review-parse.ts +518 -0
- package/tools/code-review.ts +312 -0
- package/tools/deviation.ts +737 -0
- package/tools/flow-submit.ts +302 -0
- package/tools/land.ts +608 -0
- package/tools/phase-complete.ts +476 -0
- package/tools/result.ts +18 -0
- package/tools/review-deps.ts +44 -0
- package/tools/run-attempt.ts +312 -0
- package/tools/spec-approve.ts +265 -0
- package/validate/args.ts +22 -0
- package/validate/deviation-gate.ts +229 -0
- package/validate/gate-target.ts +371 -0
- package/validate/gate.ts +79 -0
- package/validate/paths.ts +20 -0
- package/validate/personal.ts +104 -0
- package/validate/prompt.ts +525 -0
- package/validate/review-gate.ts +134 -0
- package/validate/subtask-range.ts +113 -0
- package/work-mode.ts +19 -0
- package/worktree/bootstrap.ts +243 -0
- package/worktree/branch.ts +168 -0
- package/worktree/herdr.ts +447 -0
- package/worktree/index.ts +1447 -0
- package/worktree/orphan.ts +384 -0
- package/worktree/remove-guards.ts +99 -0
- package/worktree/residue.ts +216 -0
- package/worktree/sweep.ts +526 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
// AIDEV-NOTE (PMR-MSDJ 2.3, plan PMR-V36Y): `/flow-reconcile-push` — the
|
|
2
|
+
// manual face of the tombstone reconcile. This module owns the ARGUMENT
|
|
3
|
+
// GRAMMAR and the USER-FACING RENDERING only; every decision (which refs are
|
|
4
|
+
// stale, what a tombstone body is, how many warnings a pass may emit) stays in
|
|
5
|
+
// `flow/reconcile.ts` and every io seam stays in `flow/reconcile-deps.ts`, so
|
|
6
|
+
// the whole surface is testable with injected deps + a capture sink
|
|
7
|
+
// (test/reconcile-command.test.ts) and `index.ts` stays registration wiring.
|
|
8
|
+
//
|
|
9
|
+
// Semantics table (orchestrator decision 2026-11-24; the plan's Done-when
|
|
10
|
+
// manual-smoke step REQUIRES the no-URL dry-run row to list refs, and a full
|
|
11
|
+
// `runReconcilePass` cannot do that — its dry-run still GETs every group to
|
|
12
|
+
// build the body it would have posted):
|
|
13
|
+
//
|
|
14
|
+
// | mode | PI_FLOW_PUSHGATEWAY_URL | behaviour |
|
|
15
|
+
// |----------|-------------------------|----------------------------------------|
|
|
16
|
+
// | help | anything | usage line, zero io |
|
|
17
|
+
// | real run | unset | one ERROR notify, zero io |
|
|
18
|
+
// | real run | set | runReconcilePass(deps, {dryRun:false}) |
|
|
19
|
+
// | dry-run | set | runReconcilePass(deps, {dryRun:true}) |
|
|
20
|
+
// | dry-run | unset | gateway-less candidate scan, zero POST |
|
|
21
|
+
//
|
|
22
|
+
// aven failure behaves identically in both dry-run flavors: exactly one
|
|
23
|
+
// warning (the engine's fail-closed line, via the injected `warn` sink) plus
|
|
24
|
+
// the report banner.
|
|
25
|
+
import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'
|
|
26
|
+
import type { FlowRunEvent } from '../run-state.ts'
|
|
27
|
+
// AIDEV-NOTE: PMR-7B8M — VALUE import is fine here: this module is
|
|
28
|
+
// session-side only (registered from index.ts, never reachable from the daemon
|
|
29
|
+
// graph), unlike flow/reconcile.ts which must keep its staleness edge
|
|
30
|
+
// type-only.
|
|
31
|
+
import {
|
|
32
|
+
type DaemonStaleness,
|
|
33
|
+
daemonStalenessMessage,
|
|
34
|
+
} from '../server/staleness.ts'
|
|
35
|
+
import {
|
|
36
|
+
AVEN_FAILED_WARNING,
|
|
37
|
+
computeStaleRefs,
|
|
38
|
+
livenessStatusOf,
|
|
39
|
+
type ReconcileDeps,
|
|
40
|
+
type ReconcileReport,
|
|
41
|
+
type ReconcileStaleEntry,
|
|
42
|
+
runReconcilePass,
|
|
43
|
+
} from './reconcile.ts'
|
|
44
|
+
import { reconcileSummaryLine } from './reconcile-deps.ts'
|
|
45
|
+
|
|
46
|
+
/** `/flow-reconcile-push` takes no ref: one pass covers every run journal. */
|
|
47
|
+
export type ParsedReconcileArgs =
|
|
48
|
+
| { kind: 'run' }
|
|
49
|
+
| { kind: 'dry-run' }
|
|
50
|
+
| { kind: 'help' }
|
|
51
|
+
|
|
52
|
+
export function parseReconcileArgs(args: string): ParsedReconcileArgs {
|
|
53
|
+
const tokens = args
|
|
54
|
+
.trim()
|
|
55
|
+
.split(/\s+/)
|
|
56
|
+
.filter((token) => token.length > 0)
|
|
57
|
+
let dryRun = false
|
|
58
|
+
for (const token of tokens) {
|
|
59
|
+
if (token === '--dry-run') {
|
|
60
|
+
dryRun = true
|
|
61
|
+
continue
|
|
62
|
+
}
|
|
63
|
+
// `--help`, a stray ref, anything else: show usage rather than guess.
|
|
64
|
+
return { kind: 'help' }
|
|
65
|
+
}
|
|
66
|
+
if (dryRun) return { kind: 'dry-run' }
|
|
67
|
+
return { kind: 'run' }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The notify channel: `ctx.ui.notify` in a session, an array in tests. */
|
|
71
|
+
export type ReconcileNotify = (
|
|
72
|
+
message: string,
|
|
73
|
+
level: 'info' | 'warning' | 'error',
|
|
74
|
+
) => void
|
|
75
|
+
|
|
76
|
+
export interface ReconcileCommandDeps {
|
|
77
|
+
/**
|
|
78
|
+
* The gateway-backed engine deps, or null when `PI_FLOW_PUSHGATEWAY_URL` is
|
|
79
|
+
* unset — the SAME env gate the daemon timer and the cost autopush use
|
|
80
|
+
* (`pushgatewayEnv`), so no surface can disagree with another about whether
|
|
81
|
+
* there is a gateway. `warn` is the COMMAND's sink: engine warnings become
|
|
82
|
+
* user-visible warnings here instead of `server.log` lines.
|
|
83
|
+
*/
|
|
84
|
+
createEngineDeps(warn: (message: string) => void): ReconcileDeps | null
|
|
85
|
+
/** Gateway-less scan seams: the journal + aven half only, used by `--dry-run`. */
|
|
86
|
+
listJournalRefs: () => Promise<string[]>
|
|
87
|
+
readRunEvents: (ref: string) => Promise<readonly FlowRunEvent[] | null>
|
|
88
|
+
listTicketStatuses: () => Promise<Map<string, string> | null>
|
|
89
|
+
/**
|
|
90
|
+
* AIDEV-NOTE: PMR-7B8M — REQUIRED, no default and no silent degradation
|
|
91
|
+
* (PMR-HRGJ precedent): a missing seam is a wiring bug, so it must fail
|
|
92
|
+
* loudly at the type level. SESSION-SIDE ONLY — the daemon's own timer pass
|
|
93
|
+
* runs `runReconcilePass` in flow/reconcile.ts and never calls this; only
|
|
94
|
+
* the `/flow-reconcile-push` command fills `report.daemonStaleness`.
|
|
95
|
+
*/
|
|
96
|
+
checkDaemonStaleness: () => Promise<DaemonStaleness>
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const RECONCILE_USAGE =
|
|
100
|
+
'Usage: /flow-reconcile-push [--dry-run] — tombstone Pushgateway liveness for runs whose aven ticket is not live (neither `active` nor `todo`). No ref: one pass covers every run journal.'
|
|
101
|
+
|
|
102
|
+
const NO_GATEWAY_MESSAGE =
|
|
103
|
+
'reconcile needs PI_FLOW_PUSHGATEWAY_URL set — nothing to push to. Export the pushgateway endpoint (and PI_FLOW_PUSHGATEWAY_TOKEN when it needs auth), then rerun /flow-reconcile-push.'
|
|
104
|
+
|
|
105
|
+
const NOT_INSPECTED_HEADER =
|
|
106
|
+
'PI_FLOW_PUSHGATEWAY_URL unset — candidates only, groups not inspected'
|
|
107
|
+
|
|
108
|
+
const AVEN_FAILED_BANNER =
|
|
109
|
+
'aven FAILED — ticket statuses unavailable: nothing was tombstoned (fail-closed)'
|
|
110
|
+
|
|
111
|
+
/** One pass, rendered. The handler body, ctx-free so tests drive it directly. */
|
|
112
|
+
export async function runReconcileCommand(
|
|
113
|
+
deps: ReconcileCommandDeps,
|
|
114
|
+
parsed: ParsedReconcileArgs,
|
|
115
|
+
out: ReconcileNotify,
|
|
116
|
+
): Promise<void> {
|
|
117
|
+
if (parsed.kind === 'help') {
|
|
118
|
+
out(RECONCILE_USAGE, 'info')
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
const dryRun = parsed.kind === 'dry-run'
|
|
122
|
+
const warn = (message: string): void => {
|
|
123
|
+
out(message, 'warning')
|
|
124
|
+
}
|
|
125
|
+
const engine = deps.createEngineDeps(warn)
|
|
126
|
+
if (engine === null) {
|
|
127
|
+
if (!dryRun) {
|
|
128
|
+
out(NO_GATEWAY_MESSAGE, 'error')
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
const scanned = await scanWithoutGateway(deps, warn)
|
|
132
|
+
// PMR-7B8M: filled on BOTH report paths, before rendering.
|
|
133
|
+
scanned.daemonStaleness = await deps.checkDaemonStaleness()
|
|
134
|
+
out(formatReconcileReport(scanned, 'no-gateway'), 'info')
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
const report = await runReconcilePass(engine, { dryRun })
|
|
138
|
+
// PMR-7B8M: the gateway half of the same advisory.
|
|
139
|
+
report.daemonStaleness = await deps.checkDaemonStaleness()
|
|
140
|
+
out(formatReconcileReport(report, 'gateway'), 'info')
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The gateway-less half of `--dry-run`: the engine's candidate filter and the
|
|
145
|
+
* engine's aven gate, with no gateway call at all. It mirrors
|
|
146
|
+
* `runReconcilePass`'s gates in the same order (no aven shell-out when there
|
|
147
|
+
* are no candidates, one warning when the listing fails, fail-closed) and
|
|
148
|
+
* shares the rules themselves (`livenessStatusOf`, `computeStaleRefs`,
|
|
149
|
+
* `AVEN_FAILED_WARNING`) instead of restating them.
|
|
150
|
+
*
|
|
151
|
+
* `tombstoned`/`skipped` stay EMPTY BY CONSTRUCTION: nothing was fetched, so
|
|
152
|
+
* no ref can claim a gateway-verified reason. That is why the rendered report
|
|
153
|
+
* says "groups not inspected" rather than implying a verified body.
|
|
154
|
+
*/
|
|
155
|
+
async function scanWithoutGateway(
|
|
156
|
+
deps: ReconcileCommandDeps,
|
|
157
|
+
warn: (message: string) => void,
|
|
158
|
+
): Promise<ReconcileReport> {
|
|
159
|
+
const refs = await deps.listJournalRefs()
|
|
160
|
+
const journalStates = new Map<string, string>()
|
|
161
|
+
for (const ref of refs) {
|
|
162
|
+
const events = await deps.readRunEvents(ref)
|
|
163
|
+
if (events === null) continue
|
|
164
|
+
const status = livenessStatusOf(events)
|
|
165
|
+
if (status === null) continue
|
|
166
|
+
journalStates.set(ref, status)
|
|
167
|
+
}
|
|
168
|
+
const report: ReconcileReport = {
|
|
169
|
+
scanned: refs.length,
|
|
170
|
+
candidates: journalStates.size,
|
|
171
|
+
stale: [],
|
|
172
|
+
tombstoned: [],
|
|
173
|
+
skipped: [],
|
|
174
|
+
avenFailed: false,
|
|
175
|
+
dryRun: true,
|
|
176
|
+
}
|
|
177
|
+
if (journalStates.size === 0) return report
|
|
178
|
+
const ticketStatus = await deps.listTicketStatuses()
|
|
179
|
+
if (ticketStatus === null) {
|
|
180
|
+
warn(AVEN_FAILED_WARNING)
|
|
181
|
+
report.avenFailed = true
|
|
182
|
+
return report
|
|
183
|
+
}
|
|
184
|
+
const stale: ReconcileStaleEntry[] = []
|
|
185
|
+
for (const ref of computeStaleRefs(journalStates, ticketStatus)) {
|
|
186
|
+
const journalStatus = journalStates.get(ref)
|
|
187
|
+
if (journalStatus === undefined) continue
|
|
188
|
+
stale.push({ ref, journalStatus, ticketStatus: ticketStatus.get(ref) })
|
|
189
|
+
}
|
|
190
|
+
report.stale = stale
|
|
191
|
+
return report
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The report as a user reads it: headers first, then the ONE summary line the
|
|
196
|
+
* daemon logs too (`reconcileSummaryLine`, so the two surfaces cannot drift),
|
|
197
|
+
* then one indented line per stale ref saying what happened to it. Candidates
|
|
198
|
+
* that stayed live are not listed — `candidates=N` is the statement.
|
|
199
|
+
*/
|
|
200
|
+
export function formatReconcileReport(
|
|
201
|
+
report: ReconcileReport,
|
|
202
|
+
inspected: 'gateway' | 'no-gateway',
|
|
203
|
+
): string {
|
|
204
|
+
const lines: string[] = []
|
|
205
|
+
if (report.dryRun) lines.push('DRY RUN — nothing pushed')
|
|
206
|
+
if (inspected === 'no-gateway') lines.push(NOT_INSPECTED_HEADER)
|
|
207
|
+
lines.push(reconcileSummaryLine(report))
|
|
208
|
+
// AIDEV-NOTE: PMR-7B8M — the staleness line is rendered HERE, after the
|
|
209
|
+
// shared summary, never inside `reconcileSummaryLine` (that line is the
|
|
210
|
+
// daemon's log line too and the daemon can never be the judge of its own
|
|
211
|
+
// staleness). `daemonStalenessMessage` returns null for the silent verdicts
|
|
212
|
+
// (fresh, unchecked — rev 1: equality against main HEAD leaves no
|
|
213
|
+
// "git could not judge" class), so a healthy or absent daemon adds nothing
|
|
214
|
+
// to the output.
|
|
215
|
+
if (report.daemonStaleness !== undefined) {
|
|
216
|
+
const stalenessLine = daemonStalenessMessage(report.daemonStaleness)
|
|
217
|
+
if (stalenessLine !== null) lines.push(stalenessLine)
|
|
218
|
+
}
|
|
219
|
+
if (report.avenFailed) lines.push(AVEN_FAILED_BANNER)
|
|
220
|
+
for (const entry of report.stale) {
|
|
221
|
+
lines.push(` ${formatStaleLine(report, entry)}`)
|
|
222
|
+
}
|
|
223
|
+
return lines.join('\n')
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function formatStaleLine(
|
|
227
|
+
report: ReconcileReport,
|
|
228
|
+
entry: ReconcileStaleEntry,
|
|
229
|
+
): string {
|
|
230
|
+
const head = `${entry.ref} (ticket ${entry.ticketStatus ?? 'missing'}) journal=${entry.journalStatus}`
|
|
231
|
+
const skipped = report.skipped.find((item) => item.ref === entry.ref)
|
|
232
|
+
if (skipped !== undefined) return `${head} → skipped: ${skipped.reason}`
|
|
233
|
+
if (report.tombstoned.includes(entry.ref)) return `${head} → tombstoned`
|
|
234
|
+
if (report.dryRun) return `${head} → would tombstone`
|
|
235
|
+
return `${head} → not tombstoned`
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function registerReconcileCommand(
|
|
239
|
+
pi: ExtensionAPI,
|
|
240
|
+
deps: ReconcileCommandDeps,
|
|
241
|
+
): void {
|
|
242
|
+
pi.registerCommand('flow-reconcile-push', {
|
|
243
|
+
description:
|
|
244
|
+
'Tombstone stale run-liveness metrics (halted/paused runs whose aven ticket is not live — neither `active` nor `todo`). Usage: /flow-reconcile-push [--dry-run]',
|
|
245
|
+
handler: async (args, ctx) => {
|
|
246
|
+
await runReconcileCommand(
|
|
247
|
+
deps,
|
|
248
|
+
parseReconcileArgs(args),
|
|
249
|
+
(message, level) => ctx.ui.notify(message, level),
|
|
250
|
+
)
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// AIDEV-NOTE (PMR-MSDJ 2.2, plan PMR-V36Y decision 4a): node-only io wiring for
|
|
2
|
+
// the tombstone reconcile — the single place where the daemon's world (agent
|
|
3
|
+
// dir + process env) is turned into the engine's injected `ReconcileDeps`, plus
|
|
4
|
+
// the tolerant global-config reader the timer re-reads every pass.
|
|
5
|
+
//
|
|
6
|
+
// AIDEV-NOTE (PMR-MSDJ fix round, P2-1): the bounded single-ref confirmation
|
|
7
|
+
// (`ticketStatusOf`) is wired HERE, from `aven/status.ts`, exactly like
|
|
8
|
+
// `listTicketStatuses` — same node-only runner discipline, same injectable
|
|
9
|
+
// seam for tests. Nothing on this path reads `avenWorkspace` from config: the
|
|
10
|
+
// probe itself is workspace-EXPLICIT inside `aven/status.ts` (see its P2-1
|
|
11
|
+
// note), which keeps the daemon node-only. The price: a workspace that is
|
|
12
|
+
// neither pinned nor routed from the daemon's cwd stays invisible to BOTH the
|
|
13
|
+
// listing and the probe, and such a ref still reads as missing.
|
|
14
|
+
//
|
|
15
|
+
// Home justification: this is flow-feature code, not daemon policy. The daemon
|
|
16
|
+
// half of the reconcile (timer posture, keepalive, scan-queue independence)
|
|
17
|
+
// lives in server/daemon.ts `createReconcileLoop`; everything here is
|
|
18
|
+
// reconcile-domain io that the manual `/flow-reconcile-push` command (2.3,
|
|
19
|
+
// in-session, `ctx.ui.notify` warn channel) can reuse by passing its own warn
|
|
20
|
+
// sink — which is why no function here reads `process.env` directly (the env is
|
|
21
|
+
// always a parameter, defaulting to `process.env` only at the boundary).
|
|
22
|
+
//
|
|
23
|
+
// Dependency contract (mirrors server/daemon.ts's header): node built-ins +
|
|
24
|
+
// ./reconcile.ts + ./push.ts + ../aven/status.ts + ../run-state.ts only. No
|
|
25
|
+
// config.ts: its `readConfigFile` THROWS on a malformed file (AGENTS.md
|
|
26
|
+
// boundary rule for the session side) and its loader pulls
|
|
27
|
+
// `@earendil-works/pi-coding-agent`; the daemon must degrade to defaults +
|
|
28
|
+
// warning instead. run-state.ts already runtime-imports pi-coding-agent for
|
|
29
|
+
// getAgentDir (run-state.ts:10) — a pre-existing condition the daemon accepts;
|
|
30
|
+
// nothing here adds a new one.
|
|
31
|
+
//
|
|
32
|
+
// AIDEV-NOTE: journal enumeration is the SAME rule the daemon's 1s scan uses
|
|
33
|
+
// (`journalRefsFromEntries`, which server/daemon.ts scanRuns also calls) — the
|
|
34
|
+
// reconcile must never see a different run set than the projections do. The
|
|
35
|
+
// runs dir is derived from the agent dir the spawner handed us
|
|
36
|
+
// (PI_FLOW_AGENT_DIR, server/lifecycle.ts), never from getAgentDir().
|
|
37
|
+
import { promises as fsp } from 'node:fs'
|
|
38
|
+
import path from 'node:path'
|
|
39
|
+
import {
|
|
40
|
+
listTicketStatuses as avenListTicketStatuses,
|
|
41
|
+
ticketStatusOf as avenTicketStatusOf,
|
|
42
|
+
type TicketProbe,
|
|
43
|
+
} from '../aven/status.ts'
|
|
44
|
+
import { readRunEvents, runsDir } from '../run-state.ts'
|
|
45
|
+
import {
|
|
46
|
+
deleteGroup,
|
|
47
|
+
fetchGroupBody,
|
|
48
|
+
type GroupBodyResult,
|
|
49
|
+
type PushResult,
|
|
50
|
+
pushToGateway,
|
|
51
|
+
} from './push.ts'
|
|
52
|
+
import {
|
|
53
|
+
parseReconcileSettings,
|
|
54
|
+
type ReconcileDeps,
|
|
55
|
+
type ReconcileReport,
|
|
56
|
+
runReconcilePass,
|
|
57
|
+
} from './reconcile.ts'
|
|
58
|
+
|
|
59
|
+
/** Daemon-side settings shape (`warning` is ours, not the engine's: a file
|
|
60
|
+
* that exists but cannot be read is defaults + warning, same discipline as
|
|
61
|
+
* `parseReconcileSettings` applying to malformed CONTENT). */
|
|
62
|
+
export interface ReconcileSettings {
|
|
63
|
+
enabled: boolean
|
|
64
|
+
intervalMinutes: number
|
|
65
|
+
warning?: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The single fetch shape both pushgateway calls need: `flow/push.ts`'s
|
|
69
|
+
* `GroupFetch` (GET, wants `text()`) and `PushFetch` (POST, wants status) are
|
|
70
|
+
* both structurally satisfied by this one, so wiring injects exactly one
|
|
71
|
+
* seam for tests. */
|
|
72
|
+
export type ReconcileFetch = (
|
|
73
|
+
url: string,
|
|
74
|
+
init: {
|
|
75
|
+
method: string
|
|
76
|
+
headers: Record<string, string>
|
|
77
|
+
body?: string
|
|
78
|
+
},
|
|
79
|
+
) => Promise<{
|
|
80
|
+
status: number
|
|
81
|
+
statusText: string
|
|
82
|
+
text: () => Promise<string>
|
|
83
|
+
}>
|
|
84
|
+
|
|
85
|
+
const JOURNAL_SUFFIX = '.journal.jsonl'
|
|
86
|
+
|
|
87
|
+
const defaultReadFile = (file: string): Promise<string> =>
|
|
88
|
+
fsp.readFile(file, 'utf8')
|
|
89
|
+
|
|
90
|
+
/** global config the daemon can locate without pi-coding-agent:
|
|
91
|
+
* `<agentDir>/pi-flow/config.json` — the same file `config.ts` treats as its
|
|
92
|
+
* global layer (config.ts:331), reached through the agent dir the spawner
|
|
93
|
+
* passed instead of `getAgentDir()`. Project overrides are invisible here BY
|
|
94
|
+
* DESIGN (a daemon has no session cwd; 2.1 note in config.ts). */
|
|
95
|
+
export function globalConfigPath(agentDir: string): string {
|
|
96
|
+
return path.join(agentDir, 'pi-flow', 'config.json')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** `*.journal.jsonl` basenames from a runs-dir listing — the scanRuns rule. */
|
|
100
|
+
export function journalRefsFromEntries(entries: readonly string[]): string[] {
|
|
101
|
+
const refs: string[] = []
|
|
102
|
+
for (const entry of entries) {
|
|
103
|
+
if (!entry.endsWith(JOURNAL_SUFFIX)) continue
|
|
104
|
+
refs.push(entry.slice(0, -JOURNAL_SUFFIX.length))
|
|
105
|
+
}
|
|
106
|
+
return refs
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Refs with journals under `runsDir(agentDir)`. A missing runs dir is an
|
|
110
|
+
* empty list, not an error: the daemon starts before the first journal write
|
|
111
|
+
* (see the KEEPALIVE RULE in server/daemon.ts). */
|
|
112
|
+
export async function listJournalRefs(
|
|
113
|
+
agentDir: string,
|
|
114
|
+
readdir: (dir: string) => Promise<string[]> = (dir) => fsp.readdir(dir),
|
|
115
|
+
): Promise<string[]> {
|
|
116
|
+
let entries: string[]
|
|
117
|
+
try {
|
|
118
|
+
entries = await readdir(runsDir(agentDir))
|
|
119
|
+
} catch {
|
|
120
|
+
return []
|
|
121
|
+
}
|
|
122
|
+
return journalRefsFromEntries(entries)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
interface PushgatewayEnv {
|
|
126
|
+
url: string
|
|
127
|
+
token: string | undefined
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The pushgateway env gate — the same rule as the cost autopush
|
|
131
|
+
* (`flow/cost-autopush.ts` `enabled`): no URL means nothing happens, not even
|
|
132
|
+
* a journal read. The token is optional (a gateway behind mTLS/none). */
|
|
133
|
+
export function pushgatewayEnv(
|
|
134
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
135
|
+
): PushgatewayEnv | null {
|
|
136
|
+
const url = env.PI_FLOW_PUSHGATEWAY_URL
|
|
137
|
+
if (url === undefined || url.length === 0) return null
|
|
138
|
+
const token = env.PI_FLOW_PUSHGATEWAY_TOKEN
|
|
139
|
+
if (token === undefined || token.length === 0)
|
|
140
|
+
return { url, token: undefined }
|
|
141
|
+
return { url, token }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface ReconcileDepsOptions {
|
|
145
|
+
/** agent dir from PI_FLOW_AGENT_DIR (the daemon) or getAgentDir() (command) */
|
|
146
|
+
agentDir: string
|
|
147
|
+
/** the daemon's log channel / the command's notify — engine warnings land here */
|
|
148
|
+
warn: (message: string) => void
|
|
149
|
+
env?: NodeJS.ProcessEnv
|
|
150
|
+
fetchImpl?: ReconcileFetch
|
|
151
|
+
listTicketStatuses?: () => Promise<Map<string, string> | null>
|
|
152
|
+
/** P2-1 single-ref confirmation seam (tests inject; default: aven shell-out) */
|
|
153
|
+
ticketStatusOf?: (ref: string) => Promise<TicketProbe | null>
|
|
154
|
+
readdir?: (dir: string) => Promise<string[]>
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Wire the engine's io. Returns null when the pushgateway env gate is closed
|
|
158
|
+
* — callers skip the pass silently (it is logged once, by the timer). */
|
|
159
|
+
export function createReconcileDeps(
|
|
160
|
+
options: ReconcileDepsOptions,
|
|
161
|
+
): ReconcileDeps | null {
|
|
162
|
+
const gateway = pushgatewayEnv(options.env ?? process.env)
|
|
163
|
+
if (gateway === null) return null
|
|
164
|
+
const fetchImpl: ReconcileFetch = options.fetchImpl ?? defaultReconcileFetch
|
|
165
|
+
const listStatuses = options.listTicketStatuses ?? avenListTicketStatuses
|
|
166
|
+
const probeStatus = options.ticketStatusOf ?? avenTicketStatusOf
|
|
167
|
+
const readdir = options.readdir ?? ((dir: string) => fsp.readdir(dir))
|
|
168
|
+
const { agentDir, warn } = options
|
|
169
|
+
return {
|
|
170
|
+
listJournalRefs: () => listJournalRefs(agentDir, readdir),
|
|
171
|
+
readRunEvents: (ref: string) => readRunEvents(agentDir, ref),
|
|
172
|
+
listTicketStatuses: () => listStatuses(),
|
|
173
|
+
ticketStatusOf: (ref: string) => probeStatus(ref),
|
|
174
|
+
fetchGroupBody: (trackRef: string, mode: 'work' | 'personal') =>
|
|
175
|
+
fetchGroupBodyOrError(gateway, trackRef, mode, fetchImpl),
|
|
176
|
+
deleteGroup: (trackRef: string, mode: 'work' | 'personal') =>
|
|
177
|
+
deleteGroupOrError(gateway, trackRef, mode, fetchImpl),
|
|
178
|
+
pushTombstone: (
|
|
179
|
+
trackRef: string,
|
|
180
|
+
mode: 'work' | 'personal',
|
|
181
|
+
body: string,
|
|
182
|
+
) => pushTombstoneOrError(gateway, trackRef, mode, body, fetchImpl),
|
|
183
|
+
warn,
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function defaultReconcileFetch(
|
|
188
|
+
url: string,
|
|
189
|
+
init: { method: string; headers: Record<string, string>; body?: string },
|
|
190
|
+
): Promise<{
|
|
191
|
+
status: number
|
|
192
|
+
statusText: string
|
|
193
|
+
text: () => Promise<string>
|
|
194
|
+
}> {
|
|
195
|
+
const response = await fetch(url, init)
|
|
196
|
+
return {
|
|
197
|
+
status: response.status,
|
|
198
|
+
statusText: response.statusText,
|
|
199
|
+
text: () => response.text(),
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// AIDEV-NOTE: the engine's `ReconcileDeps` contract is `{ok:false,error}` or a
|
|
204
|
+
// PushResult, never a throw — but `flow/push.ts` only guarantees that for its
|
|
205
|
+
// own io. These wrappers keep the promise for the whole call (a frozen DNS
|
|
206
|
+
// cache throwing synchronously inside `fetch` must cost one skip entry, not
|
|
207
|
+
// the daemon's pass): unexpected throws become ordinary failure results.
|
|
208
|
+
async function fetchGroupBodyOrError(
|
|
209
|
+
gateway: PushgatewayEnv,
|
|
210
|
+
trackRef: string,
|
|
211
|
+
mode: 'work' | 'personal',
|
|
212
|
+
fetchImpl: ReconcileFetch,
|
|
213
|
+
): Promise<GroupBodyResult> {
|
|
214
|
+
try {
|
|
215
|
+
return await fetchGroupBody(
|
|
216
|
+
gateway.url,
|
|
217
|
+
trackRef,
|
|
218
|
+
mode,
|
|
219
|
+
gateway.token,
|
|
220
|
+
fetchImpl,
|
|
221
|
+
)
|
|
222
|
+
} catch (err) {
|
|
223
|
+
return { ok: false, error: err instanceof Error ? err.message : `${err}` }
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function pushTombstoneOrError(
|
|
228
|
+
gateway: PushgatewayEnv,
|
|
229
|
+
trackRef: string,
|
|
230
|
+
mode: 'work' | 'personal',
|
|
231
|
+
body: string,
|
|
232
|
+
fetchImpl: ReconcileFetch,
|
|
233
|
+
): Promise<PushResult> {
|
|
234
|
+
try {
|
|
235
|
+
return await pushToGateway(
|
|
236
|
+
gateway.url,
|
|
237
|
+
trackRef,
|
|
238
|
+
body,
|
|
239
|
+
fetchImpl,
|
|
240
|
+
gateway.token,
|
|
241
|
+
mode,
|
|
242
|
+
)
|
|
243
|
+
} catch (err) {
|
|
244
|
+
return { ok: false, error: err instanceof Error ? err.message : `${err}` }
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// AIDEV-NOTE (PMR-2EGV 2.3): the group DELETE gets the identical never-throw
|
|
249
|
+
// wrapper as the GET/POST pair above — a synchronous throw inside `fetch`
|
|
250
|
+
// must cost one skip entry (no re-POST attempted), never the pass.
|
|
251
|
+
async function deleteGroupOrError(
|
|
252
|
+
gateway: PushgatewayEnv,
|
|
253
|
+
trackRef: string,
|
|
254
|
+
mode: 'work' | 'personal',
|
|
255
|
+
fetchImpl: ReconcileFetch,
|
|
256
|
+
): Promise<PushResult> {
|
|
257
|
+
try {
|
|
258
|
+
return await deleteGroup(
|
|
259
|
+
gateway.url,
|
|
260
|
+
trackRef,
|
|
261
|
+
fetchImpl,
|
|
262
|
+
gateway.token,
|
|
263
|
+
mode,
|
|
264
|
+
)
|
|
265
|
+
} catch (err) {
|
|
266
|
+
return { ok: false, error: err instanceof Error ? err.message : `${err}` }
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** The raw global config text, re-read on every cycle so `intervalMinutes` and
|
|
271
|
+
* `enabled` changes apply without a daemon restart (plan PMR-V36Y 2.2).
|
|
272
|
+
* ENOENT is the normal "no config yet" answer (defaults, no warning); any
|
|
273
|
+
* other read failure degrades the same way but says so. */
|
|
274
|
+
export async function readReconcileSettings(
|
|
275
|
+
agentDir: string,
|
|
276
|
+
readFile: (file: string) => Promise<string> = defaultReadFile,
|
|
277
|
+
): Promise<ReconcileSettings> {
|
|
278
|
+
const file = globalConfigPath(agentDir)
|
|
279
|
+
let raw: string | null = null
|
|
280
|
+
let warning: string | undefined
|
|
281
|
+
try {
|
|
282
|
+
raw = await readFile(file)
|
|
283
|
+
} catch (err) {
|
|
284
|
+
const code = (err as NodeJS.ErrnoException).code
|
|
285
|
+
if (code !== 'ENOENT') {
|
|
286
|
+
warning = `could not read ${file}: ${err instanceof Error ? err.message : `${err}`}`
|
|
287
|
+
}
|
|
288
|
+
raw = null
|
|
289
|
+
}
|
|
290
|
+
const settings = parseReconcileSettings(raw)
|
|
291
|
+
if (warning !== undefined) return { ...settings, warning }
|
|
292
|
+
return settings
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** One stable line per pass — the daemon logs it, and subtask 2.3's command
|
|
296
|
+
* notify reuses the same shape (plan PMR-V36Y behavior step 6). */
|
|
297
|
+
export function reconcileSummaryLine(report: ReconcileReport): string {
|
|
298
|
+
const parts = [
|
|
299
|
+
`reconcile pass: scanned=${report.scanned}`,
|
|
300
|
+
`candidates=${report.candidates}`,
|
|
301
|
+
`stale=${report.stale.length}`,
|
|
302
|
+
`tombstoned=${report.tombstoned.length}`,
|
|
303
|
+
`skipped=${report.skipped.length}`,
|
|
304
|
+
]
|
|
305
|
+
if (report.dryRun) parts.push('dry-run')
|
|
306
|
+
if (report.avenFailed) parts.push('aven=failed')
|
|
307
|
+
return parts.join(' ')
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export interface ReconcilePassOnceOptions extends ReconcileDepsOptions {
|
|
311
|
+
dryRun?: boolean
|
|
312
|
+
/** engine seam (tests assert the wired deps reach it; default real engine) */
|
|
313
|
+
engine?: (
|
|
314
|
+
deps: ReconcileDeps,
|
|
315
|
+
opts: { dryRun: boolean },
|
|
316
|
+
) => Promise<ReconcileReport>
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** One pass with wired deps. Returns null when the pushgateway env gate is
|
|
320
|
+
* closed, so the caller can distinguish "nothing to do" from a real report. */
|
|
321
|
+
export async function runReconcilePassOnce(
|
|
322
|
+
options: ReconcilePassOnceOptions,
|
|
323
|
+
): Promise<ReconcileReport | null> {
|
|
324
|
+
const deps = createReconcileDeps(options)
|
|
325
|
+
if (deps === null) return null
|
|
326
|
+
const dryRun = options.dryRun === true
|
|
327
|
+
const engine = options.engine ?? runReconcilePass
|
|
328
|
+
return engine(deps, { dryRun })
|
|
329
|
+
}
|