@attalabs/vinaya 0.33.0 → 0.35.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 (143) hide show
  1. package/LICENSE +104 -201
  2. package/README.md +1 -1
  3. package/aeg-root/enforcement.md +1 -1
  4. package/aeg-root/glossary.md +4 -0
  5. package/aeg-root/roles/developer.md +3 -0
  6. package/aeg-root/roles/operator.md +30 -10
  7. package/aeg-root/roles/planner/reference.md +3 -2
  8. package/aeg-root/roles/planner.md +3 -1
  9. package/aeg-root/templates/brief-template.md +9 -7
  10. package/aeg-root/templates/pr-report-template.md +3 -1
  11. package/dist/checks/bin/check-body-bare-digits.js +107 -28
  12. package/dist/checks/bin/check-branch-topology.js +94 -25
  13. package/dist/checks/bin/check-brief-shape.js +113 -33
  14. package/dist/checks/bin/check-changeset-coverage.js +369 -92
  15. package/dist/checks/bin/check-closes-n.js +94 -25
  16. package/dist/checks/bin/check-coherence.js +124 -29
  17. package/dist/checks/bin/check-dead-branch-push.js +79 -25
  18. package/dist/checks/bin/check-dispatch-readiness.js +122 -28
  19. package/dist/checks/bin/check-doc-coverage-push.js +365 -88
  20. package/dist/checks/bin/check-doc-coverage.js +367 -90
  21. package/dist/checks/bin/check-doctrine-no-procedures.js +107 -28
  22. package/dist/checks/bin/check-doctrine-portability.js +366 -89
  23. package/dist/checks/bin/check-evidence-fresh.js +338 -61
  24. package/dist/checks/bin/check-exec-bits.js +363 -86
  25. package/dist/checks/bin/check-first-push-dispatch.js +122 -28
  26. package/dist/checks/bin/check-issue-assignment.js +94 -25
  27. package/dist/checks/bin/check-issue-milestone-attach.js +79 -25
  28. package/dist/checks/bin/check-issue-objectives-numbering.js +606 -26
  29. package/dist/checks/bin/check-issue-parts-coverage.js +79 -25
  30. package/dist/checks/bin/check-issue-surface-globs.js +162 -34
  31. package/dist/checks/bin/check-issue-title-grammar.js +79 -25
  32. package/dist/checks/bin/check-issue-tranche-label.js +79 -25
  33. package/dist/checks/bin/check-main-branch-refusal.js +79 -25
  34. package/dist/checks/bin/check-no-disk-state.js +79 -25
  35. package/dist/checks/bin/check-pr-premise-reassert.js +107 -28
  36. package/dist/checks/bin/check-pr-report-density.js +79 -25
  37. package/dist/checks/bin/check-principal-test-plan-wait.js +79 -25
  38. package/dist/checks/bin/check-quoted-command.js +282 -39
  39. package/dist/checks/bin/check-reader-resolvable-prose.js +333 -47
  40. package/dist/checks/bin/check-registry-gates.js +79 -25
  41. package/dist/checks/bin/check-retired-vocabulary.js +282 -39
  42. package/dist/checks/bin/check-review-gate.js +111 -31
  43. package/dist/checks/bin/check-single-plan-pr.js +79 -25
  44. package/dist/checks/bin/check-surface-scope.js +94 -25
  45. package/dist/checks/bin/check-test-plan.js +79 -25
  46. package/dist/checks/bin/check-token-collection-wired.js +79 -25
  47. package/dist/checks/bin/check-token-report.js +79 -25
  48. package/dist/checks/bin/check-workspace-escape.js +364 -87
  49. package/dist/index.js +2713 -1609
  50. package/dist/lib/pre-push-cache-test-run.js +226 -16
  51. package/dist/lib/pre-push-select-tests.js +48 -5
  52. package/package.json +2 -2
  53. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/brief-render.ts +153 -37
  54. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/brief-validation.ts +87 -24
  55. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/coherence-checks.ts +9 -2
  56. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/control-store/migration.ts +13 -3
  57. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/dev-review-loop/assess-round.ts +7 -0
  58. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/dev-review-loop/index.ts +5 -2
  59. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/dev-review-loop/journal-reconstruction.ts +116 -20
  60. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/dev-review-loop/render-summary.ts +100 -4
  61. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/gate-cutovers.ts +49 -0
  62. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/index.ts +60 -4
  63. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/issue-validation.ts +138 -54
  64. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/log/envelope.ts +26 -3
  65. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/objectives-cutover-config.ts +31 -0
  66. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/objectives.ts +15 -6
  67. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/reader-resolvable-prose.ts +122 -23
  68. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/review-gate.ts +18 -3
  69. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/review-policy.ts +24 -0
  70. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/review-status.ts +15 -10
  71. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/ruling-ordinal.ts +16 -0
  72. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/task-phase-history.ts +194 -0
  73. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/task-tools.ts +312 -53
  74. package/studio-standalone/_node_modules/@attalabs/aeg-core/src/verdict-extraction.ts +62 -8
  75. package/studio-standalone/apps/vinaya-studio/web/.next/BUILD_ID +1 -1
  76. package/studio-standalone/apps/vinaya-studio/web/.next/build-manifest.json +3 -3
  77. package/studio-standalone/apps/vinaya-studio/web/.next/prerender-manifest.json +3 -3
  78. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_global-error.html +1 -1
  79. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_global-error.rsc +1 -1
  80. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_global-error.segments/__PAGE__.segment.rsc +1 -1
  81. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  82. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  83. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  84. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  85. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_not-found/page/server-reference-manifest.json +2 -2
  86. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_not-found/page.js.nft.json +1 -1
  87. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
  88. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/api/coherence/route.js.nft.json +1 -1
  89. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/page/server-reference-manifest.json +2 -2
  90. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/page.js.nft.json +1 -1
  91. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/page_client-reference-manifest.js +1 -1
  92. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/backlog/page/server-reference-manifest.json +2 -2
  93. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/backlog/page.js.nft.json +1 -1
  94. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/backlog/page_client-reference-manifest.js +1 -1
  95. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/page/server-reference-manifest.json +2 -2
  96. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/page.js.nft.json +1 -1
  97. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/page_client-reference-manifest.js +1 -1
  98. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/page/server-reference-manifest.json +2 -2
  99. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/page.js.nft.json +1 -1
  100. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/page_client-reference-manifest.js +1 -1
  101. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/tranches/[slug]/page/server-reference-manifest.json +3 -3
  102. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/tranches/[slug]/page.js.nft.json +1 -1
  103. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/tranches/[slug]/page_client-reference-manifest.js +1 -1
  104. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/tranches/[slug]/tasks/[taskId]/page/server-reference-manifest.json +2 -2
  105. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/tranches/[slug]/tasks/[taskId]/page.js.nft.json +1 -1
  106. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/[name]/tranches/[slug]/tasks/[taskId]/page_client-reference-manifest.js +1 -1
  107. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/page/server-reference-manifest.json +2 -2
  108. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/page.js.nft.json +1 -1
  109. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/projects/page_client-reference-manifest.js +1 -1
  110. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/tranches/page/server-reference-manifest.json +2 -2
  111. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/tranches/page.js.nft.json +1 -1
  112. package/studio-standalone/apps/vinaya-studio/web/.next/server/app/studio/tranches/page_client-reference-manifest.js +1 -1
  113. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/[root-of-the-server]__02a-3g8._.js +4 -4
  114. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/1q96_modules_@clerk_nextjs_dist_esm_app-router_client_keyless-creator-reader_0lom2js.js +1 -1
  115. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/[root-of-the-server]__074w9nf._.js +1 -1
  116. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/[root-of-the-server]__0ls23oh._.js +1 -1
  117. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/{[root-of-the-server]__18paley._.js → [root-of-the-server]__0zc_0gx._.js} +1 -1
  118. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/[root-of-the-server]__1gjqmhq._.js +1 -1
  119. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/[root-of-the-server]__1wc4-ip._.js +1 -1
  120. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_08onyya._.js +1 -1
  121. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_096f46i._.js +1 -1
  122. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_0af2one._.js +1 -1
  123. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_0e5tkdd._.js +1 -1
  124. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_0kf2g72._.js +1 -1
  125. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_0zf4d27._.js +1 -1
  126. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_1-wo68i._.js +1 -1
  127. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_15gdg48._.js +2 -2
  128. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/_1achbh9._.js +1 -1
  129. package/studio-standalone/apps/vinaya-studio/web/.next/server/chunks/ssr/node_modules_@clerk_nextjs_dist_esm_app-router_server-actions_1x7vfhn.js +2 -2
  130. package/studio-standalone/apps/vinaya-studio/web/.next/server/middleware-build-manifest.js +3 -3
  131. package/studio-standalone/apps/vinaya-studio/web/.next/server/pages/500.html +1 -1
  132. package/studio-standalone/apps/vinaya-studio/web/.next/server/server-reference-manifest.js +1 -1
  133. package/studio-standalone/apps/vinaya-studio/web/.next/server/server-reference-manifest.json +4 -4
  134. package/studio-standalone/apps/vinaya-studio/web/.next/static/chunks/0ko698wh8xx2g.css +1 -0
  135. package/studio-standalone/apps/vinaya-studio/web/.next/static/chunks/{22s44f5k3ecmj.js → 3goaig_rysc6p.js} +1 -1
  136. package/studio-standalone/apps/vinaya-studio/web/.next/static/chunks/{1ce2dj27_cgbj.js → 3ip5z3cf43bgo.js} +4 -4
  137. package/studio-standalone/apps/vinaya-studio/web/.next/static/chunks/{34hhdtbp20krb.js → 3u9emqjwpd0t9.js} +1 -1
  138. package/studio-standalone/apps/vinaya-studio/web/package.json +2 -2
  139. package/studio-standalone/apps/vinaya-studio/web/.next/static/chunks/0bm4nrrzwc7zi.css +0 -1
  140. /package/studio-standalone/_node_modules/@attalabs/vinaya/studio-standalone/_node_modules/@attalabs/vinaya/studio-standalone/_node_modules/@attalabs/vinaya/studio-standalone/_node_modules/@attalabs/vinaya/studio-standalone/_node_modules/@attalabs/vinaya/studio-standalone/_node_modules/@attalabs/{aeg-core → vinaya/studio-standalone/_node_modules/@attalabs/vinaya/studio-standalone/_node_modules/@attalabs/aeg-core}/bin/verify-coherence.ts +0 -0
  141. /package/studio-standalone/apps/vinaya-studio/web/.next/static/{vl7QVqELe1AQ-9kaOd9mR → ca66rAHI_w9D7S7ceXDvR}/_buildManifest.js +0 -0
  142. /package/studio-standalone/apps/vinaya-studio/web/.next/static/{vl7QVqELe1AQ-9kaOd9mR → ca66rAHI_w9D7S7ceXDvR}/_clientMiddlewareManifest.js +0 -0
  143. /package/studio-standalone/apps/vinaya-studio/web/.next/static/{vl7QVqELe1AQ-9kaOd9mR → ca66rAHI_w9D7S7ceXDvR}/_ssgManifest.js +0 -0
@@ -75,7 +75,13 @@
75
75
 
76
76
  import { isPrincipal, isWaiverLabelActorVerified, PRINCIPAL_ALLOWLIST, WAIVER_LABEL_REVIEW } from './waiver-label'
77
77
  import { extractCodeReviewVerdict, extractSecurityReviewVerdict } from './verdict-extraction'
78
- import { DEFAULT_REVIEW_POLICY, evaluateCodeReview, evaluateSecurityReview, type ReviewPolicy } from './review-policy'
78
+ import {
79
+ consequentialFindings,
80
+ DEFAULT_REVIEW_POLICY,
81
+ evaluateCodeReview,
82
+ evaluateSecurityReview,
83
+ type ReviewPolicy
84
+ } from './review-policy'
79
85
  import {
80
86
  compareManifest,
81
87
  policyDigest as computePolicyDigest,
@@ -311,11 +317,20 @@ export function checkReviewGate(input: ReviewGateInput): ReviewGateResult {
311
317
  // returns a named `fail` result; an uncaught throw here would crash the
312
318
  // check-run instead, on nothing worse than a typo in a manually-posted
313
319
  // comment. Caught and failed closed the same way.
320
+ //
321
+ // O1/O4: only CONSEQUENTIAL findings are evaluated — a finding the reviewer
322
+ // marked `resolved` keeps its severity in the record but no longer blocks,
323
+ // the SAME `consequentialFindings` filter `deriveCodeReviewVerdict` applies
324
+ // before it derives APPROVE/PASS, so an APPROVE whose only blocking findings
325
+ // are `resolved` passes the gate exactly as it made derivation approve. A
326
+ // finding at or above the threshold in any other state — `open`,
327
+ // `fix-claimed`, `reproduced`, or no state token — is untouched by the
328
+ // filter and still blocks (O2).
314
329
  let codeReviewPolicyEvaluation: ReturnType<typeof evaluateCodeReview>
315
330
  let securityPolicyEvaluation: ReturnType<typeof evaluateSecurityReview>
316
331
  try {
317
- codeReviewPolicyEvaluation = evaluateCodeReview(codeReview.findingSeverities, policy)
318
- securityPolicyEvaluation = evaluateSecurityReview(security.findingSeverities, policy)
332
+ codeReviewPolicyEvaluation = evaluateCodeReview(consequentialFindings(codeReview.findingSeverities), policy)
333
+ securityPolicyEvaluation = evaluateSecurityReview(consequentialFindings(security.findingSeverities), policy)
319
334
  } catch (err) {
320
335
  return {
321
336
  verdict: 'fail',
@@ -146,6 +146,30 @@ export function evaluateReviewFindings<F extends PolicyFinding>(
146
146
  return { outcome: blockingFindings.length > 0 ? 'blocked' : 'clean', blockingFindings }
147
147
  }
148
148
 
149
+ /**
150
+ * O4: the SINGLE definition of "does this finding count toward policy." A
151
+ * finding is consequential — evaluated by `evaluateReviewFindings` and able to
152
+ * block — unless the reviewer marked it `resolved`. Any other state (`open`,
153
+ * `fix-claimed`, `reproduced`) and a finding carrying no state token at all
154
+ * stay consequential and still block (O2, fail-closed): only an explicit
155
+ * `resolved` clears a finding from the gate, so a reviewer cannot clear a real
156
+ * blocker by relabelling it anything else. Imported by verdict derivation
157
+ * (`deriveCodeReviewVerdict`/`deriveSecurityVerdict`), the merge gate
158
+ * (`checkReviewGate`), and the loop's publication self-check (`publishRound`)
159
+ * alike, so the three can never disagree about the same verdict comment. The
160
+ * re-review-state grammar itself is parsed once, by `parseFindingState`
161
+ * (`verdict-extraction.ts`); this rule only decides what a parsed state means
162
+ * for policy.
163
+ */
164
+ export function isConsequentialFinding(finding: { state?: string | null }): boolean {
165
+ return finding.state !== 'resolved'
166
+ }
167
+
168
+ /** Every consequential finding (see `isConsequentialFinding`), preserving order and element type. */
169
+ export function consequentialFindings<F extends { state?: string | null }>(findings: readonly F[]): F[] {
170
+ return findings.filter((f) => isConsequentialFinding(f))
171
+ }
172
+
149
173
  /** `evaluateReviewFindings` fixed to the code-review scale and `policy.codeReviewThreshold`. */
150
174
  export function evaluateCodeReview<F extends PolicyFinding>(
151
175
  findings: readonly F[],
@@ -18,7 +18,7 @@
18
18
  * head it judged. There is no second `Judged head:` regex in this file.
19
19
  */
20
20
 
21
- import { extractCodeReviewVerdict, extractSecurityReviewVerdict } from './verdict-extraction'
21
+ import { extractCodeReviewVerdict, extractSecurityReviewVerdict, parseFindingState } from './verdict-extraction'
22
22
  import { isPrincipal } from './waiver-label'
23
23
 
24
24
  /**
@@ -31,14 +31,16 @@ const DEVELOPER_ROUND_MARKER = /<!--\s*aeg:developer:round-(\d+)\s*-->/i
31
31
 
32
32
  /**
33
33
  * A rendered finding line as `review-post.ts`'s `renderFindingsSection`
34
- * writes it — `<n>. [SEVERITY] <location> — F<id> <class> <state>: <text>` —
35
- * read for its id and its re-review state together. The id half deliberately
36
- * mirrors that renderer's own output rather than inventing a second finding
37
- * grammar; the state half is what round-over-round convergence is measured
38
- * from, and no exported parser carries it.
34
+ * writes it — `<n>. [SEVERITY] <location> — <description>` — matched only far
35
+ * enough to isolate the DESCRIPTION after the ` — ` separator; the id and
36
+ * re-review state are then read from that description by the shared
37
+ * `parseFindingState` (`verdict-extraction.ts`), the single definition of the
38
+ * `F<n> <class>[ <state>]:` state grammar this package carries (Traps to
39
+ * avoid — no second copy of the state token list). The `\S+` location match
40
+ * is unchanged from before this shared-parse refactor, so which lines this
41
+ * loop tracks is byte-for-byte what it tracked before.
39
42
  */
40
- const FINDING_LINE =
41
- /^\d+\.\s+\[[A-Z]+\]\s+\S+\s+—\s+F(\d+)(?:\s+\S+)?(?:\s+(open|fix-claimed|reproduced|resolved))?:/gm
43
+ const FINDING_LINE = /^\d+\.\s+\[[A-Z]+\]\s+\S+\s+—\s+(.*)$/gm
42
44
 
43
45
  export type ReviewStatus =
44
46
  | { state: 'CONTINUE' }
@@ -87,8 +89,11 @@ export type VerdictComment = {
87
89
  export function findingStates(body: string): Map<string, string | null> {
88
90
  const out = new Map<string, string | null>()
89
91
  for (const m of body.matchAll(FINDING_LINE)) {
90
- const id = `F${m[1]}`
91
- const state = m[2] ?? null
92
+ const { id, state } = parseFindingState(m[1] as string)
93
+ // A description with no `F<n>` head is not a tracked finding — the prior
94
+ // regex required `F(\d+)` on the line, so a line without one was never
95
+ // matched at all; skipping it here keeps that behaviour exactly.
96
+ if (id === null) continue
92
97
  // A round that lists the same id twice keeps the first state it declared;
93
98
  // a later id-less repeat must never erase a real `resolved`/`reproduced`.
94
99
  if (!out.has(id) || out.get(id) === null) out.set(id, state)
@@ -18,6 +18,22 @@ import { isPrincipal } from './waiver-label'
18
18
  /** The marker `apps/cli/src/commands/pr-rule.ts` posts: `<!-- aeg:principal:ruling:<pr>-<k> -->`, one line, nothing else on it. */
19
19
  const RULING_MARKER_ORDINAL = /^<!-- aeg:principal:ruling:\d+-(\d+) -->$/
20
20
 
21
+ /**
22
+ * The marker line a principal ruling carries, built from the two numbers it
23
+ * encodes — `<ref>-<ordinal>`, where `ref` is the pull request or Issue the
24
+ * ruling is about and `ordinal` is the strictly-increasing `k` every reader
25
+ * here compares. It lives beside `RULING_MARKER_ORDINAL` because the two
26
+ * cannot be allowed to disagree about which slot the ordinal sits in: a
27
+ * renderer that swapped them emitted a marker that parsed as ordinal `1`
28
+ * forever, so a ruling written exactly as instructed failed the very
29
+ * freshness gate it was posted to satisfy. Any caller that WRITES a marker a
30
+ * reader here will parse should build it with this rather than by
31
+ * interpolating its own template.
32
+ */
33
+ export function principalRulingMarker(ref: number, ordinal: number): string {
34
+ return `<!-- aeg:principal:ruling:${ref}-${ordinal} -->`
35
+ }
36
+
21
37
  export type RulingComment = {
22
38
  body: string
23
39
  /** The comment author's GitHub login, or `null` when the caller could not resolve one. */
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Where a run is, and what that place usually takes in this repository.
3
+ *
4
+ * Two pure concerns, both read by the task-status reader and the `task_status`
5
+ * tool:
6
+ *
7
+ * - **The phase vocabulary.** The dev-review-loop's own recovery record
8
+ * (`loop_state`) carries a `phase` string mirroring the driver's own
9
+ * `Decision['type']`. `TASK_PHASE_LABELS` maps each recorded phase to the
10
+ * one word a reader sees, one-to-one — never a phase invented from a
11
+ * second source, and never one recorded phase split into two.
12
+ * - **Typical phase times, from history.** `phaseSamplesFromPrComments`
13
+ * reads durations off a merged task pull request's own principal-authored
14
+ * comments: a developer round marker, then that round's verdict comments,
15
+ * then the next round's marker. The interval marker → verdicts is time
16
+ * spent reviewing; verdicts → next marker is time spent developing.
17
+ * `summarizePhaseSamples` reduces a set of those to a MEDIAN and a sample
18
+ * count, and refuses to answer at all below `MIN_PHASE_HISTORY_SAMPLES`.
19
+ *
20
+ * Every figure this module produces is history — what already happened, on
21
+ * merged work — and is named that way (`typicalPhaseMinutes`, never an ETA,
22
+ * never a remaining time, never a deadline). Nothing here predicts when a run
23
+ * in flight will finish.
24
+ *
25
+ * Only principal-authored comments contribute a sample: the same trust
26
+ * boundary every other forge read in this loop applies, so a non-principal
27
+ * commenter cannot post a round-marker- or verdict-shaped comment and shift
28
+ * what the repository reports as typical.
29
+ */
30
+
31
+ import { parseDeveloperRoundMarker } from './review-status'
32
+ import type { TaskPhaseHistory } from './task-tools'
33
+ import { extractCodeReviewVerdict, extractSecurityReviewVerdict } from './verdict-extraction'
34
+ import { isPrincipal } from './waiver-label'
35
+
36
+ // --- the phase vocabulary ---------------------------------------------------
37
+
38
+ /**
39
+ * The phases the loop actually records, and the word each reads as. One entry
40
+ * per `Decision['type']` the driver persists into `loop_state.phase`:
41
+ *
42
+ * | recorded phase | shown as |
43
+ * | -------------------- | -------------------- |
44
+ * | `dispatch_developer` | `developing` |
45
+ * | `ask_confidence` | `awaiting confidence`|
46
+ * | `dispatch_reviewers` | `reviewing` |
47
+ * | `publish` | `publishing` |
48
+ * | `pause` | `paused` |
49
+ *
50
+ * There is deliberately no separate `waiting for CI` phase: the driver polls
51
+ * the head's checks while the record still reads `dispatch_developer`, so a
52
+ * distinct CI-wait row would have to be inferred from a second source rather
53
+ * than read from the record. `developing` therefore spans the developer's own
54
+ * turn AND the gate wait that follows its push — stated here rather than left
55
+ * for a reader to assume.
56
+ */
57
+ export const TASK_PHASE_LABELS = {
58
+ dispatch_developer: 'developing',
59
+ ask_confidence: 'awaiting confidence',
60
+ dispatch_reviewers: 'reviewing',
61
+ publish: 'publishing',
62
+ pause: 'paused'
63
+ } as const
64
+
65
+ export type RecordedLoopPhase = keyof typeof TASK_PHASE_LABELS
66
+ export type TaskPhaseLabel = (typeof TASK_PHASE_LABELS)[RecordedLoopPhase]
67
+
68
+ export function isRecordedLoopPhase(phase: string): phase is RecordedLoopPhase {
69
+ return Object.hasOwn(TASK_PHASE_LABELS, phase)
70
+ }
71
+
72
+ /** The shown phase for a recorded one. A phase this vocabulary does not know (a decision type added after this mapping) reads back VERBATIM — the record's own word, never a guess at which known phase it resembles. */
73
+ export function taskPhaseLabel(phase: string): string {
74
+ return isRecordedLoopPhase(phase) ? TASK_PHASE_LABELS[phase] : phase
75
+ }
76
+
77
+ /**
78
+ * Which history class a recorded phase compares against — `null` for a phase
79
+ * the pull-request record carries no interval for. `publish` and `pause` have
80
+ * none by nature (publishing is one comment post; a pause ends when a person
81
+ * acts), and `ask_confidence` has none because it is not separable from the
82
+ * developer turn it interrupts.
83
+ */
84
+ export type PhaseHistoryClass = 'developing' | 'reviewing'
85
+
86
+ export function phaseHistoryClassFor(phase: string): PhaseHistoryClass | null {
87
+ if (phase === 'dispatch_developer') return 'developing'
88
+ if (phase === 'dispatch_reviewers') return 'reviewing'
89
+ return null
90
+ }
91
+
92
+ // --- typical phase times, from merged pull requests -------------------------
93
+
94
+ /** One comment as a history read sees it: its body, its author, and the forge's own creation timestamp. */
95
+ export type HistoryComment = { body: string; author: string | null; createdAt: string }
96
+
97
+ /** Minutes, one entry per interval the record actually carried — never a filled-in or interpolated value. */
98
+ export type PhaseSamples = { [K in PhaseHistoryClass]: number[] }
99
+
100
+ export function emptyPhaseSamples(): PhaseSamples {
101
+ return { developing: [], reviewing: [] }
102
+ }
103
+
104
+ /** A verdict comment is one either merge-gate extractor parses clean — the same "did this cast a verdict" test the gate itself applies, so this read can never disagree with it about which comment ended a round. */
105
+ function isVerdictComment(body: string): boolean {
106
+ return (
107
+ extractCodeReviewVerdict([body]).danglingNote === null || extractSecurityReviewVerdict([body]).danglingNote === null
108
+ )
109
+ }
110
+
111
+ type PhaseEvent = { kind: 'marker'; at: number } | { kind: 'verdict'; at: number }
112
+
113
+ function minutesBetween(fromMs: number, toMs: number): number | null {
114
+ const ms = toMs - fromMs
115
+ return ms >= 0 ? ms / 60_000 : null
116
+ }
117
+
118
+ /**
119
+ * The phase intervals one pull request's comments record. Ordered by the
120
+ * forge's own timestamps rather than by the order the read returned them, so
121
+ * a paginated or re-ordered read measures the same intervals.
122
+ *
123
+ * A round whose verdicts were never posted as comments contributes NOTHING —
124
+ * neither a reviewing nor a developing sample — because the record does not
125
+ * say where that round's time went. Today the driver posts a round's verdict
126
+ * comments when it publishes, so a pull request that took several rounds
127
+ * carries one reviewing interval and no developing interval at all; that is
128
+ * a thin history, and `summarizePhaseSamples` below refuses to turn a thin
129
+ * history into a number.
130
+ */
131
+ export function phaseSamplesFromPrComments(
132
+ comments: readonly HistoryComment[],
133
+ allowlist: readonly string[]
134
+ ): PhaseSamples {
135
+ const events: PhaseEvent[] = []
136
+ for (const comment of comments) {
137
+ if (!isPrincipal(comment.author, allowlist as string[])) continue
138
+ const at = Date.parse(comment.createdAt)
139
+ if (!Number.isFinite(at)) continue
140
+ if (parseDeveloperRoundMarker(comment.body) !== null) events.push({ kind: 'marker', at })
141
+ else if (isVerdictComment(comment.body)) events.push({ kind: 'verdict', at })
142
+ }
143
+ events.sort((a, b) => a.at - b.at)
144
+
145
+ const samples = emptyPhaseSamples()
146
+ let markerAt: number | null = null
147
+ let lastVerdictAt: number | null = null
148
+ const closeRound = (): void => {
149
+ if (markerAt === null || lastVerdictAt === null) return
150
+ const minutes = minutesBetween(markerAt, lastVerdictAt)
151
+ if (minutes !== null) samples.reviewing.push(minutes)
152
+ }
153
+ for (const event of events) {
154
+ if (event.kind === 'marker') {
155
+ closeRound()
156
+ if (lastVerdictAt !== null) {
157
+ const minutes = minutesBetween(lastVerdictAt, event.at)
158
+ if (minutes !== null) samples.developing.push(minutes)
159
+ }
160
+ markerAt = event.at
161
+ lastVerdictAt = null
162
+ continue
163
+ }
164
+ // A round posts more than one verdict (a code review and a security
165
+ // pass); the round's review ends at the LAST of them.
166
+ if (markerAt !== null) lastVerdictAt = lastVerdictAt === null ? event.at : Math.max(lastVerdictAt, event.at)
167
+ }
168
+ closeRound()
169
+ return samples
170
+ }
171
+
172
+ /** Below this many past intervals, a phase reports no typical time at all: two samples from two pull requests describe those two pull requests, not this repository. */
173
+ export const MIN_PHASE_HISTORY_SAMPLES = 3
174
+
175
+ // The median of a phase's past intervals, with the count it was computed
176
+ // from, is `TaskPhaseHistory` (`task-tools.ts`) — the shape the `task_status`
177
+ // result schema declares. One name for the fact, derived from the schema
178
+ // itself, so the reader and the published contract cannot drift apart.
179
+
180
+ export function medianOf(values: readonly number[]): number | null {
181
+ if (values.length === 0) return null
182
+ const sorted = [...values].sort((a, b) => a - b)
183
+ const mid = Math.floor(sorted.length / 2)
184
+ if (sorted.length % 2 === 1) return sorted[mid] as number
185
+ return ((sorted[mid - 1] as number) + (sorted[mid] as number)) / 2
186
+ }
187
+
188
+ /** `null` — no typical time, never an invented one — for a phase with fewer than `MIN_PHASE_HISTORY_SAMPLES` past intervals, and for one with none at all. */
189
+ export function summarizePhaseSamples(minutes: readonly number[]): TaskPhaseHistory | null {
190
+ if (minutes.length < MIN_PHASE_HISTORY_SAMPLES) return null
191
+ const median = medianOf(minutes)
192
+ if (median === null) return null
193
+ return { typicalPhaseMinutes: Math.round(median), typicalPhaseSamples: minutes.length }
194
+ }