@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
package/flow/loop.ts
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
// AIDEV-NOTE: Extension-owned /flow loop (replaces the old "driver agent"
|
|
2
|
+
// handoff prompt — an agent cannot invoke slash commands, so a prompt asking
|
|
3
|
+
// it to do so just made the agent wander; PMR follow-up, see docs/rpiv-flow.md
|
|
4
|
+
// §5). The loop injects ONE phase prompt at a time via the same
|
|
5
|
+
// buildPhasePrompt the phase commands use, then advances on the
|
|
6
|
+
// agent_settled event: label landed -> next phase prompt; label missing,
|
|
7
|
+
// abort, or aven failure -> deterministic stop-on-fail (aven note + halt).
|
|
8
|
+
// Exception: a settle while a subagent is still in flight is a mid-phase
|
|
9
|
+
// async wait (fire-and-forget scouts/planners steer back as a later run),
|
|
10
|
+
// not a phase outcome — the loop stays armed and re-checks on the next
|
|
11
|
+
// settle (hasInFlightSubagent guard). The model never sequences phases —
|
|
12
|
+
// extension code does.
|
|
13
|
+
// AIDEV-NOTE: PMR-XTQQ — the loop is journaled: every transition emits a
|
|
14
|
+
// loop-state snapshot (flow/resume.ts reads them back), and /flow re-entry
|
|
15
|
+
// seeds `resume` from the journal so a restart restores the blocker-retry
|
|
16
|
+
// budget and the validate blockers-note cutoff exactly.
|
|
17
|
+
// AIDEV-NOTE: PMR-H1GA — split decision (module map). loop.ts is the
|
|
18
|
+
// run-LIFECYCLE driver and the public surface: FlowLoop + FlowLoopImpl
|
|
19
|
+
// (start/halt/advanceAfter), createFlowLoop, and re-exports of everything
|
|
20
|
+
// that moved. The moved machinery lives in:
|
|
21
|
+
// - flow/v2-emission.ts — V2EmissionGuard: the guarded v2 emission +
|
|
22
|
+
// live-wait state machine (dedup, recency watermark, reason-aware
|
|
23
|
+
// lease-bound hold) and the two-dimension master rationale's single
|
|
24
|
+
// authoritative home.
|
|
25
|
+
// - flow/v2-resume.ts — the pure journal-tail v2 resume/seed helpers
|
|
26
|
+
// (resumeV2StateFromEvents, resumePlanApprovalOpenFromEvents,
|
|
27
|
+
// v2StateForStatus).
|
|
28
|
+
// - flow/loop-deps.ts — the DI contract (FlowLoopDeps, FlowStartInput).
|
|
29
|
+
// - flow/loop-settle.ts — FlowSettleEngine: settle evaluation + prompt
|
|
30
|
+
// injection; owns the mutable run state and calls back into the loop
|
|
31
|
+
// through the narrow SettleHost adapter (halt/advanceAfter closures, so
|
|
32
|
+
// advanceAfter stays private — see the SettleHost note there).
|
|
33
|
+
// Rationale: aislop's file-too-large rule counts TOTAL lines and flags above
|
|
34
|
+
// 441, so each module stays ≤441. packages/pi-flow/index.ts is deliberately
|
|
35
|
+
// NOT split: its wiring contracts span separate seams (telemetry fan-out,
|
|
36
|
+
// ask-emitter, deviation tool) and would broaden risk without sharing loop
|
|
37
|
+
// hold-state machinery.
|
|
38
|
+
import { clearFlowEnv, type FlowEnv, stampFlowStart } from './attribution.ts'
|
|
39
|
+
import type {
|
|
40
|
+
FlowLoopDeps,
|
|
41
|
+
FlowLoopState,
|
|
42
|
+
FlowStartInput,
|
|
43
|
+
} from './loop-deps.ts'
|
|
44
|
+
import { FlowSettleEngine } from './loop-settle.ts'
|
|
45
|
+
import { nextPhase, PHASE_LABEL, type Phase } from './orchestrate.ts'
|
|
46
|
+
import { V2EmissionGuard } from './v2-emission.ts'
|
|
47
|
+
import type { FlowReason, FlowState } from './vocabulary.ts'
|
|
48
|
+
import { getFlowWorktree } from './worktree-context.ts'
|
|
49
|
+
|
|
50
|
+
// AIDEV-NOTE: PMR-H1GA — the loop's DI contracts (FlowLoopDeps,
|
|
51
|
+
// FlowLoopState, FlowStartInput) live in flow/loop-deps.ts; the public two
|
|
52
|
+
// are re-exported
|
|
53
|
+
// here so index.ts and every loop test harness keep resolving the types from
|
|
54
|
+
// flow/loop.ts unchanged.
|
|
55
|
+
export type { FlowLoopDeps, FlowStartInput } from './loop-deps.ts'
|
|
56
|
+
// AIDEV-NOTE: PMR-XVKZ 1.1 — the pure v2 resume/seed helpers live in
|
|
57
|
+
// flow/v2-resume.ts (PMR-H1GA split); re-exported here so index.ts and the
|
|
58
|
+
// loop-state-change suite keep resolving them from flow/loop.ts.
|
|
59
|
+
export {
|
|
60
|
+
resumePlanApprovalOpenFromEvents,
|
|
61
|
+
resumeV2StateFromEvents,
|
|
62
|
+
} from './v2-resume.ts'
|
|
63
|
+
|
|
64
|
+
export interface FlowLoop {
|
|
65
|
+
/** Start (or restart) the run at the given phase. Sends the phase prompt. */
|
|
66
|
+
start(input: FlowStartInput): Promise<void>
|
|
67
|
+
/** Evaluate the settled run: advance, halt, or finish. Ignored when idle. */
|
|
68
|
+
onSettled(): Promise<void>
|
|
69
|
+
/** Whether a flow run is awaiting a phase outcome in this session. */
|
|
70
|
+
isActive(): boolean
|
|
71
|
+
/** The active flow's ref, or null when no flow run is in flight (PMR-06RH). */
|
|
72
|
+
activeRef(): string | null
|
|
73
|
+
/**
|
|
74
|
+
* AIDEV-NOTE: PMR-ZV9Y fix round (P1 blocker) — invalidate the loop's guarded
|
|
75
|
+
* v2 emission (`lastEmittedV2`) when a v2 `state-change` for the ACTIVE ref is
|
|
76
|
+
* journaled by a SEAM the loop does not run through itself (the tools).
|
|
77
|
+
* `run_attempt` escalate (`waiting_human{escalation}`) and
|
|
78
|
+
* `deviation_review` rerun (`waiting_machine{deviation_gate_rerun}`) journal a
|
|
79
|
+
* wait through the shared fan-out writer but never journal the running return;
|
|
80
|
+
* the loop's instance field would stay pinned at `running`, so the next
|
|
81
|
+
* `snapshot('running')` (sendPhasePrompt / retryAfterBlockers) is
|
|
82
|
+
* guard-suppressed and `pi_flow_state` stays stuck at the wait while the flow
|
|
83
|
+
* is actively working (false waiting-on-human page, phantom Waiting-on-me row,
|
|
84
|
+
* wait-seconds mis-attribution). index.ts calls this from the fan-out writer's
|
|
85
|
+
* `onV2StateChangeObserved` observer — THE single choke point that sees every
|
|
86
|
+
* journaled v2 event (see the telemetry/dispatch.ts seam map), so no journal
|
|
87
|
+
* re-read. Setting `lastEmittedV2` to the value the loop just emitted is a
|
|
88
|
+
* harmless no-op (the loop's own emissions pass the same observer): the update
|
|
89
|
+
* is guarded by ref (a foreign run never touches this run's guard) and is
|
|
90
|
+
* idempotent.
|
|
91
|
+
*
|
|
92
|
+
* AIDEV-NOTE: PMR-ZV9Y fix-round-2 (P2a) — the observer is MONOTONIC on the
|
|
93
|
+
* event timestamp `at`. Tool v2 writes bypass `journalSerial` (run-attempt /
|
|
94
|
+
* deviation go straight to `writer.record`), so an already-enqueued loop
|
|
95
|
+
* `running` event can route AFTER a tool's later `waiting_human`: without a
|
|
96
|
+
* recency guard route(waiting_human) sets the guard, then the late
|
|
97
|
+
* route(running echo) resets it to `running` and the next differing snapshot
|
|
98
|
+
* is suppressed again — the original P1 symptom in a narrow window. So an
|
|
99
|
+
* external update whose `at` is older-or-equal than the last applied
|
|
100
|
+
* observation is ignored. `at` is always `new Date().toISOString()` (loop
|
|
101
|
+
* `now()`, the tools, run-state.recordRunEvent) — a fixed-width UTC-`Z`
|
|
102
|
+
* format, so a plain lexicographic string compare is a correct chronological
|
|
103
|
+
* compare (no timezone/locale drift, same precision everywhere). `lastV2At`
|
|
104
|
+
* is set by BOTH this method and the loop's own `emitStateChange`; the loop's
|
|
105
|
+
* own emission is never recency-blocked (it writes the guard unconditionally),
|
|
106
|
+
* so a stale future-dated external write can never lock the loop out.
|
|
107
|
+
*
|
|
108
|
+
* AIDEV-NOTE: PMR-422Q (PMR-0NRH review blocker 2) — the OPTIONAL `reason`
|
|
109
|
+
* parameter makes the live-wait hold REASON-AWARE. `state` alone cannot tell a
|
|
110
|
+
* wait that closes itself (the ask emitter, the code-review gate — each journals
|
|
111
|
+
* its own `state-change{running}`) from one that NEVER journals the running
|
|
112
|
+
* return (`escalation` — a `waiting_human` nobody closes), and only the former
|
|
113
|
+
* may arm the hold: a hold nothing can release is a latch. See
|
|
114
|
+
* SELF_CLOSING_HUMAN_WAITS in flow/vocabulary.ts (ISC-A-1 — the property lives
|
|
115
|
+
* with the other reason semantics, not in a second list here). The parameter is
|
|
116
|
+
* optional so the PMR-ZV9Y call sites keep working unchanged: an ABSENT reason
|
|
117
|
+
* can never arm the hold, it only clears it. index.ts threads `event.reason`.
|
|
118
|
+
* `deviation_gate_rerun` is NEITHER an arming case NOR a WIDTH case: the gate
|
|
119
|
+
* journals `waiting_machine` (tools/deviation.ts, WAIT_KIND machine) STRAIGHT
|
|
120
|
+
* to the fan-out writer (index.ts: registerDeviationReviewTool), so it never
|
|
121
|
+
* reaches emitStateChange (flow/v2-emission.ts, PMR-H1GA) where the hold
|
|
122
|
+
* lives — no predicate, wide or narrow, could suppress it. It is the
|
|
123
|
+
* PMR-ZV9Y P1 invalidation case documented just
|
|
124
|
+
* above (a tool journals a wait with no running return, so the observer must
|
|
125
|
+
* un-pin lastEmittedV2). Its observation also CLEARS the flag (and the lease
|
|
126
|
+
* with it, PMR-422Q), and the clearing mechanism is the STATE dimension: the
|
|
127
|
+
* observed state is `waiting_machine`, not `waiting_human`, so the
|
|
128
|
+
* `state === 'waiting_human'` guard in the body short-circuits and the reason
|
|
129
|
+
* is never consulted (PMR-FM0F — a not-self-closing reason is a secondary
|
|
130
|
+
* property here, not the operative mechanism, since SELF_CLOSING_HUMAN_WAITS
|
|
131
|
+
* is human-only anyway). See the skip-rule note in emitStateChange
|
|
132
|
+
* (flow/v2-emission.ts — its single authoritative home, PMR-H1GA).
|
|
133
|
+
*/
|
|
134
|
+
noteExternalV2State(
|
|
135
|
+
ref: string,
|
|
136
|
+
state: FlowState,
|
|
137
|
+
at: string,
|
|
138
|
+
reason?: FlowReason | '',
|
|
139
|
+
): void
|
|
140
|
+
/**
|
|
141
|
+
* AIDEV-NOTE: PMR-422Q — renew the live-wait lease for an ARMED self-closing
|
|
142
|
+
* wait (the ask emitter's heartbeat; tunables in flow/live-wait.ts). An
|
|
143
|
+
* IN-PROCESS ping only — never a journal write (the lease is in-memory, and
|
|
144
|
+
* a 60s beat would spam the journal with ~480 records per overnight wait).
|
|
145
|
+
* Guards, each load-bearing: IDLE (state null — nothing is armed for a run
|
|
146
|
+
* that is not in flight), REF (a foreign ref must never extend this run's
|
|
147
|
+
* hold; same rule as noteExternalV2State), and ARMED (a beat racing the
|
|
148
|
+
* external `running` close is a no-op — it must NOT resurrect a released
|
|
149
|
+
* hold, or a cleared wait would silently re-arm). The deadline moves as
|
|
150
|
+
* `max(deadline, leaseNow + window)`: MONOTONIC, so a beat can never shorten
|
|
151
|
+
* a lease an earlier beat (or the arming stamp) already granted, and the
|
|
152
|
+
* observation-`at` clock and the lease clock may differ without corruption.
|
|
153
|
+
*/
|
|
154
|
+
renewLiveWaitLease(ref: string): void
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
class FlowLoopImpl implements FlowLoop {
|
|
158
|
+
private readonly deps: FlowLoopDeps
|
|
159
|
+
private readonly env: FlowEnv
|
|
160
|
+
// AIDEV-NOTE: PMR-H1GA — the guarded v2 emission/live-wait state machine
|
|
161
|
+
// (lastEmittedV2 dedup, lastV2At recency watermark, the reason-aware +
|
|
162
|
+
// lease-bound live-wait flag/deadline) and the v2 emission trio
|
|
163
|
+
// (emitStateChange / emitStep / emitV2) live in flow/v2-emission.ts
|
|
164
|
+
// (V2EmissionGuard) with their full AIDEV notes; the two-dimension
|
|
165
|
+
// live-wait master rationale's single authoritative home is the hold
|
|
166
|
+
// predicate's note there. The public methods below are thin stubs that
|
|
167
|
+
// own the IDLE/REF run-lifecycle guards and delegate.
|
|
168
|
+
private readonly v2: V2EmissionGuard
|
|
169
|
+
// AIDEV-NOTE: PMR-H1GA — the mutable run state and settle evaluation
|
|
170
|
+
// (onSettled, retryAfterBlockers, sendPhasePrompt, snapshot, the
|
|
171
|
+
// spec-approval pause and restart-close) live in flow/loop-settle.ts
|
|
172
|
+
// (FlowSettleEngine). The loop keeps run LIFECYCLE: start, halt,
|
|
173
|
+
// advanceAfter and the done path; the engine reaches them through the
|
|
174
|
+
// SettleHost adapter below.
|
|
175
|
+
private readonly settle: FlowSettleEngine
|
|
176
|
+
|
|
177
|
+
constructor(deps: FlowLoopDeps) {
|
|
178
|
+
this.deps = deps
|
|
179
|
+
this.env = deps.env ?? process.env
|
|
180
|
+
// AIDEV-NOTE: PMR-H1GA / PMR-422Q — both closures read `deps` AT CALL
|
|
181
|
+
// TIME: tests mutate h.deps.now (and wire journalV2) after
|
|
182
|
+
// createFlowLoop, so capturing values here would freeze them.
|
|
183
|
+
// leaseClock drives LEASE ARITHMETIC ONLY (deps.now when injected,
|
|
184
|
+
// else the module now()); journalV2 is the raw optional seam — the
|
|
185
|
+
// guard's emitV2 wraps it so a throwing/absent seam never propagates
|
|
186
|
+
// (ISC-A-5).
|
|
187
|
+
this.v2 = new V2EmissionGuard(
|
|
188
|
+
() => {
|
|
189
|
+
if (deps.now !== undefined) return deps.now()
|
|
190
|
+
return now()
|
|
191
|
+
},
|
|
192
|
+
(event) => {
|
|
193
|
+
deps.journalV2?.(event)
|
|
194
|
+
},
|
|
195
|
+
)
|
|
196
|
+
// AIDEV-NOTE: PMR-H1GA — the SettleHost is an object literal with
|
|
197
|
+
// halt/advanceAfter CLOSURES, not `implements SettleHost`: advanceAfter
|
|
198
|
+
// is private lifecycle machinery, and a private method cannot satisfy a
|
|
199
|
+
// public interface member without widening the internal class surface.
|
|
200
|
+
// The arrows capture the private methods; see the SettleHost note in
|
|
201
|
+
// flow/loop-settle.ts.
|
|
202
|
+
this.settle = new FlowSettleEngine(deps, this.v2, this.env, {
|
|
203
|
+
halt: (phase, reason, v2reason) => this.halt(phase, reason, v2reason),
|
|
204
|
+
advanceAfter: (phase, labels) => this.advanceAfter(phase, labels),
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
isActive(): boolean {
|
|
209
|
+
return this.settle.state !== null
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
activeRef(): string | null {
|
|
213
|
+
return this.settle.state?.ref ?? null
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// AIDEV-NOTE: PMR-ZV9Y fix round (P1 blocker) + fix-round-2 (P2a) — see the
|
|
217
|
+
// FlowLoop method doc. PMR-H1GA: the IDLE/REF guards are run lifecycle and
|
|
218
|
+
// stay here; vocabulary/recency/arming/lease-stamping live in
|
|
219
|
+
// V2EmissionGuard.noteExternal (flow/v2-emission.ts) with the full
|
|
220
|
+
// rationale note.
|
|
221
|
+
noteExternalV2State(
|
|
222
|
+
ref: string,
|
|
223
|
+
state: FlowState,
|
|
224
|
+
at: string,
|
|
225
|
+
reason?: FlowReason | '',
|
|
226
|
+
): void {
|
|
227
|
+
if (this.settle.state === null) return
|
|
228
|
+
if (this.settle.state.ref !== ref) return
|
|
229
|
+
this.v2.noteExternal(state, at, reason)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// AIDEV-NOTE: PMR-422Q — heartbeat entry point (see the FlowLoop method
|
|
233
|
+
// note for the guards). Callers: the ask emitter's in-process beat timer
|
|
234
|
+
// (index.ts wiring). Nothing else may touch the lease. PMR-H1GA: the
|
|
235
|
+
// IDLE/REF guards stay here; the ARMED guard + monotonic renewal live in
|
|
236
|
+
// V2EmissionGuard.renewLease (flow/v2-emission.ts).
|
|
237
|
+
renewLiveWaitLease(ref: string): void {
|
|
238
|
+
if (this.settle.state === null) return
|
|
239
|
+
if (this.settle.state.ref !== ref) return
|
|
240
|
+
this.v2.renewLease()
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async start(input: FlowStartInput): Promise<void> {
|
|
244
|
+
// AIDEV-NOTE: PMR-ZRMQ — yolo is PERSONAL-mode only. The prompt layer
|
|
245
|
+
// already keeps work prompts gate-full, but the receipt-gate exemptions
|
|
246
|
+
// (phase-complete.ts / spec-approve.ts) fire on the journaled yolo bit
|
|
247
|
+
// regardless of mode; loop start is the single choke point covering the
|
|
248
|
+
// --yolo flag, config yolo, and journaled-seed resumes alike, so refuse
|
|
249
|
+
// loudly here instead of letting a work-mode run silently skip the
|
|
250
|
+
// human receipt gates.
|
|
251
|
+
if (input.yolo === true && input.promptInput.mode === 'work') {
|
|
252
|
+
this.deps.notify(
|
|
253
|
+
`/flow ${input.ref}: --yolo is personal-mode only — refusing to start a work-mode run without the receipt gates. Remove --yolo (or the yolo config) to run here.`,
|
|
254
|
+
'warning',
|
|
255
|
+
)
|
|
256
|
+
return
|
|
257
|
+
}
|
|
258
|
+
if (this.settle.state !== null) {
|
|
259
|
+
this.deps.notify(
|
|
260
|
+
`/flow: superseding active flow for ${this.settle.state.ref} with ${input.ref}`,
|
|
261
|
+
'info',
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
// AIDEV-NOTE: PMR-HFK1 — stamp attribution env BEFORE any prompt is
|
|
265
|
+
// sent: every child spawned from here on carries PI_FLOW_* into its tab
|
|
266
|
+
// env. A fresh traceparent root per invocation (see attribution.ts) —
|
|
267
|
+
// superseding stamps over the old run's values. Invalid ref = abort
|
|
268
|
+
// fail-closed: the journal filename would be unusable anyway.
|
|
269
|
+
const stamped = stampFlowStart(
|
|
270
|
+
this.env,
|
|
271
|
+
input.ref,
|
|
272
|
+
input.phase,
|
|
273
|
+
undefined,
|
|
274
|
+
{
|
|
275
|
+
trackRef: input.trackRef,
|
|
276
|
+
mode: input.promptInput.mode,
|
|
277
|
+
yolo: input.yolo,
|
|
278
|
+
},
|
|
279
|
+
)
|
|
280
|
+
this.deps.onFlowEnvChange?.()
|
|
281
|
+
if (!stamped.ok) {
|
|
282
|
+
this.deps.notify(
|
|
283
|
+
`/flow ${input.ref}: ${stamped.reason} — aborting.`,
|
|
284
|
+
'warning',
|
|
285
|
+
)
|
|
286
|
+
this.settle.clearRun()
|
|
287
|
+
this.deps.setActiveRef?.(null)
|
|
288
|
+
return
|
|
289
|
+
}
|
|
290
|
+
const resume = input.resume
|
|
291
|
+
const resumed = resume !== undefined
|
|
292
|
+
// AIDEV-NOTE: PMR-ZV9Y 2.1 — seed the guarded v2 state from the journal
|
|
293
|
+
// tail (dedup state, recency watermark reset, live-wait flag + lease
|
|
294
|
+
// reset); the seeding semantics and their notes live on
|
|
295
|
+
// V2EmissionGuard.seed (flow/v2-emission.ts, PMR-H1GA).
|
|
296
|
+
this.v2.seed(input.resumeState)
|
|
297
|
+
const state: FlowLoopState = {
|
|
298
|
+
ref: input.ref,
|
|
299
|
+
currentPhase: input.phase,
|
|
300
|
+
awaitingOutcome: false,
|
|
301
|
+
blockerRetries: 0,
|
|
302
|
+
validateStartedAt: now(),
|
|
303
|
+
promptInput: input.promptInput,
|
|
304
|
+
}
|
|
305
|
+
if (resumed) {
|
|
306
|
+
state.blockerRetries = resume.blockerRetries
|
|
307
|
+
state.validateStartedAt = resume.validateStartedAt
|
|
308
|
+
}
|
|
309
|
+
// AIDEV-NOTE: PMR-ZV9Y 2.1 — beginRun seeds the plan.spec-approval
|
|
310
|
+
// pairing flag (overridden ONLY when supplied, so a same-instance
|
|
311
|
+
// restart keeps the earlier pause's open step) and pendingResume; the
|
|
312
|
+
// seeding notes live on beginRun in flow/loop-settle.ts (PMR-H1GA).
|
|
313
|
+
this.settle.beginRun(state, {
|
|
314
|
+
pendingResume: resumed,
|
|
315
|
+
specApprovalOpen: input.resumePlanApprovalOpen,
|
|
316
|
+
})
|
|
317
|
+
// AIDEV-NOTE: PMR-ZV9Y 2.1 — close a still-open plan.spec-approval step
|
|
318
|
+
// BEFORE the running snapshot (journal ORDER: step-stop THEN
|
|
319
|
+
// state-change{running}); the full rationale lives on
|
|
320
|
+
// closeSpecApprovalStepIfOpen in flow/loop-settle.ts (PMR-H1GA).
|
|
321
|
+
this.settle.closeSpecApprovalStepIfOpen()
|
|
322
|
+
// AIDEV-NOTE: PMR-XTQQ — loop-state snapshot journaling: budget and
|
|
323
|
+
// cutoff live on the journal so a restart restores them; cleared only
|
|
324
|
+
// by terminal snapshots.
|
|
325
|
+
this.settle.snapshot('running')
|
|
326
|
+
this.deps.setActiveRef?.(input.ref)
|
|
327
|
+
this.deps.resetLandingState?.()
|
|
328
|
+
await this.settle.sendPhasePrompt(input.phase)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async onSettled(): Promise<void> {
|
|
332
|
+
return this.settle.onSettled()
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// AIDEV-NOTE: PMR-ZV9Y 2.1 — `v2reason` is the closed-enum halt reason
|
|
336
|
+
// mapped onto the `state-change{halted, ...}` emission (default
|
|
337
|
+
// `label_miss` — the generic halt). The free-text `reason` stays the
|
|
338
|
+
// journal-note/halt-record text; it is NEVER a label value (ISC-A-1).
|
|
339
|
+
private async halt(
|
|
340
|
+
phase: Phase,
|
|
341
|
+
reason: string,
|
|
342
|
+
v2reason: FlowReason = 'label_miss',
|
|
343
|
+
): Promise<void> {
|
|
344
|
+
const ref = this.settle.state?.ref ?? ''
|
|
345
|
+
const text = `flow halted at ${phase}: ${reason}`
|
|
346
|
+
await this.deps.avenNote(ref, text)
|
|
347
|
+
// AIDEV-NOTE: yolo 5.1 — halted yolo runs get the `error` type label,
|
|
348
|
+
// best-effort and BEFORE the terminal snapshot (the journal is the
|
|
349
|
+
// durable halt record; the label marks the outcome). `error` is a type
|
|
350
|
+
// label, deliberately outside lifecycleLabelMutation, and only applied
|
|
351
|
+
// for yolo runs. A failing/throwing label write never masks the halt:
|
|
352
|
+
// it is journaled and the loop continues tearing down.
|
|
353
|
+
if (this.settle.state?.promptInput.yolo === true && this.deps.avenLabel) {
|
|
354
|
+
let labeled = false
|
|
355
|
+
try {
|
|
356
|
+
labeled = await this.deps.avenLabel(ref, 'error')
|
|
357
|
+
} catch {
|
|
358
|
+
labeled = false
|
|
359
|
+
}
|
|
360
|
+
if (!labeled) {
|
|
361
|
+
const note = `failed to apply the "error" label on ${ref} (non-fatal)`
|
|
362
|
+
this.deps.journal({ kind: 'note', at: now(), ref, text: note })
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
this.deps.journal({ kind: 'note', at: now(), ref, text })
|
|
366
|
+
this.deps.notify(`/flow ${ref}: ${text}`, 'warning')
|
|
367
|
+
this.settle.snapshot('halted', v2reason)
|
|
368
|
+
this.settle.clearRun()
|
|
369
|
+
// AIDEV-NOTE: PMR-HFK1 — terminal transition: no child spawned from now
|
|
370
|
+
// on may attribute to this (dead) run.
|
|
371
|
+
clearFlowEnv(this.env)
|
|
372
|
+
this.deps.onFlowEnvChange?.()
|
|
373
|
+
this.deps.setActiveRef?.(null)
|
|
374
|
+
if (ref.length > 0) this.deps.onRunEnd?.(ref)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// AIDEV-NOTE: advanceAfter runs after a settled phase turn whose label
|
|
378
|
+
// landed: journal completion, resolve the next phase from the SAME label
|
|
379
|
+
// table /flow starts from (labels stay the run state), and inject the next
|
|
380
|
+
// phase prompt. All labels present = flow done.
|
|
381
|
+
private async advanceAfter(phase: Phase, labels: string[]): Promise<void> {
|
|
382
|
+
const ref = this.settle.state?.ref ?? ''
|
|
383
|
+
this.deps.journal({
|
|
384
|
+
kind: 'phase-complete',
|
|
385
|
+
at: now(),
|
|
386
|
+
ref,
|
|
387
|
+
phase,
|
|
388
|
+
label: PHASE_LABEL[phase],
|
|
389
|
+
})
|
|
390
|
+
// AIDEV-NOTE: PMR-ZV9Y 2.1 / PMR-F79K — there is deliberately NO
|
|
391
|
+
// plan.spec-approval step-stop here. An earlier "idempotent fallback" close
|
|
392
|
+
// in this method was unreachable (the pause nulls the run state, onSettled
|
|
393
|
+
// bails on a null state, and start() clears the pairing flag on every
|
|
394
|
+
// re-entry) and its comment described a pause→advance path that cannot
|
|
395
|
+
// exist; keeping it invited a simplification that would re-break
|
|
396
|
+
// fix-round-3 deviation #1. start() owns the close.
|
|
397
|
+
const next = nextPhase(labels, this.settle.state?.promptInput.mode)
|
|
398
|
+
if (next.kind === 'done') {
|
|
399
|
+
this.deps.journal({
|
|
400
|
+
kind: 'note',
|
|
401
|
+
at: now(),
|
|
402
|
+
ref,
|
|
403
|
+
text: 'flow complete — all phase labels present',
|
|
404
|
+
})
|
|
405
|
+
this.deps.notify(`/flow ${ref}: all phases complete.`, 'info')
|
|
406
|
+
const doneState = this.settle.state
|
|
407
|
+
if (doneState === null) return
|
|
408
|
+
const doneRef = doneState.ref
|
|
409
|
+
const doneMode = doneState.promptInput.mode
|
|
410
|
+
const doneWorktree = getFlowWorktree() ?? undefined
|
|
411
|
+
this.settle.snapshot('done')
|
|
412
|
+
this.settle.clearRun()
|
|
413
|
+
clearFlowEnv(this.env)
|
|
414
|
+
this.deps.onFlowEnvChange?.()
|
|
415
|
+
this.deps.setActiveRef?.(null)
|
|
416
|
+
this.deps.onRunEnd?.(ref)
|
|
417
|
+
if (this.deps.onFlowDone) {
|
|
418
|
+
try {
|
|
419
|
+
await this.deps.onFlowDone(doneRef, doneMode, doneWorktree)
|
|
420
|
+
} catch {
|
|
421
|
+
// AIDEV-NOTE: PMR-81SX — cleanup is best-effort; a failure here must
|
|
422
|
+
// never surface as a run failure. The worktree simply stays.
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return
|
|
426
|
+
}
|
|
427
|
+
await this.settle.sendPhasePrompt(next.phase)
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export function createFlowLoop(deps: FlowLoopDeps): FlowLoop {
|
|
432
|
+
return new FlowLoopImpl(deps)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function now(): string {
|
|
436
|
+
return new Date().toISOString()
|
|
437
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// AIDEV-NOTE: Pure next-phase resolver for /flow. Aven labels ARE the run
|
|
2
|
+
// state: the first phase whose completion label is missing is the one to run
|
|
3
|
+
// — that single rule gives resume (kill mid-phase -> label absent -> re-run
|
|
4
|
+
// the same phase) and no-duplicated-commits (label present -> phase skipped).
|
|
5
|
+
// The table below is the ONLY place the phase<->label lifecycle is mapped;
|
|
6
|
+
// reconcile with phase-state.ts LIFECYCLE_LABELS before renaming labels.
|
|
7
|
+
// Lifecycle is PIV (PMR-Z2K7): plan -> implement -> validate -> feedback.
|
|
8
|
+
// Research is no longer a lifecycle phase — the /brainstorm command
|
|
9
|
+
// (renamed from /research, PMR-H7RW) still exists outside the gates.
|
|
10
|
+
// AIDEV-NOTE: PMR-V31Z 2.1 — labels are the run state for phase ordering
|
|
11
|
+
// AND for feedback entry again: the personal validate->feedback landing
|
|
12
|
+
// guard that used to reconcile the mapping at the call sites (the validate
|
|
13
|
+
// settle in flow/loop-settle.ts and the /flow start reconciliation in
|
|
14
|
+
// index.ts, via the deleted flow/landing-guard.ts) is gone — personal mode
|
|
15
|
+
// delivers via PR, so the label mapping alone advances every phase.
|
|
16
|
+
|
|
17
|
+
import { MODE_PROFILE } from '../mode-profile.ts'
|
|
18
|
+
import type { WorkMode } from '../work-mode.ts'
|
|
19
|
+
|
|
20
|
+
export type Phase = 'plan' | 'implement' | 'validate' | 'feedback'
|
|
21
|
+
|
|
22
|
+
// phase -> the aven label that marks this phase complete
|
|
23
|
+
export const PHASE_LABEL: Record<Phase, string> = {
|
|
24
|
+
plan: 'planned',
|
|
25
|
+
implement: 'active',
|
|
26
|
+
validate: 'review-passed',
|
|
27
|
+
feedback: 'done',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type NextPhase = { kind: 'phase'; phase: Phase } | { kind: 'done' }
|
|
31
|
+
|
|
32
|
+
// AIDEV-NOTE: PMR-GFM5/PMR-81SX — the phase list is mode-keyed
|
|
33
|
+
// (MODE_PROFILE.phases); both modes now run all four phases (personal
|
|
34
|
+
// feedback = wrap-up). Default 'work' keeps the full PIV order for callers
|
|
35
|
+
// that don't pass a mode.
|
|
36
|
+
export function nextPhase(
|
|
37
|
+
labels: readonly string[],
|
|
38
|
+
mode: WorkMode = 'work',
|
|
39
|
+
): NextPhase {
|
|
40
|
+
const first = MODE_PROFILE[mode].phases.find(
|
|
41
|
+
(phase) => !labels.includes(PHASE_LABEL[phase]),
|
|
42
|
+
)
|
|
43
|
+
if (first === undefined) return { kind: 'done' }
|
|
44
|
+
return { kind: 'phase', phase: first }
|
|
45
|
+
}
|