@bevel-software/platform-core-backend 0.21.0 → 0.22.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 (177) hide show
  1. package/dist/core/create-core-server.d.ts.map +1 -1
  2. package/dist/core/create-core-server.js +32 -2
  3. package/dist/core/create-core-server.js.map +1 -1
  4. package/dist/core/create-core-services.d.ts +13 -1
  5. package/dist/core/create-core-services.d.ts.map +1 -1
  6. package/dist/core/create-core-services.js +51 -5
  7. package/dist/core/create-core-services.js.map +1 -1
  8. package/dist/modules/access/access-requests.contract.d.ts +75 -0
  9. package/dist/modules/access/access-requests.contract.d.ts.map +1 -0
  10. package/dist/modules/access/access-requests.contract.js +20 -0
  11. package/dist/modules/access/access-requests.contract.js.map +1 -0
  12. package/dist/modules/access/access-requests.routes.d.ts +35 -0
  13. package/dist/modules/access/access-requests.routes.d.ts.map +1 -0
  14. package/dist/modules/access/access-requests.routes.js +237 -0
  15. package/dist/modules/access/access-requests.routes.js.map +1 -0
  16. package/dist/modules/access/access-requests.service.d.ts +123 -0
  17. package/dist/modules/access/access-requests.service.d.ts.map +1 -0
  18. package/dist/modules/access/access-requests.service.js +337 -0
  19. package/dist/modules/access/access-requests.service.js.map +1 -0
  20. package/dist/modules/access-model/access-grammar.d.ts +12 -0
  21. package/dist/modules/access-model/access-grammar.d.ts.map +1 -1
  22. package/dist/modules/access-model/access-grammar.js +24 -0
  23. package/dist/modules/access-model/access-grammar.js.map +1 -1
  24. package/dist/modules/auth/auth.service.d.ts +26 -1
  25. package/dist/modules/auth/auth.service.d.ts.map +1 -1
  26. package/dist/modules/auth/auth.service.js +11 -2
  27. package/dist/modules/auth/auth.service.js.map +1 -1
  28. package/dist/modules/github-app/github-app.client.d.ts +101 -0
  29. package/dist/modules/github-app/github-app.client.d.ts.map +1 -0
  30. package/dist/modules/github-app/github-app.client.js +222 -0
  31. package/dist/modules/github-app/github-app.client.js.map +1 -0
  32. package/dist/modules/github-app/github-app.connection.d.ts +102 -0
  33. package/dist/modules/github-app/github-app.connection.d.ts.map +1 -0
  34. package/dist/modules/github-app/github-app.connection.js +185 -0
  35. package/dist/modules/github-app/github-app.connection.js.map +1 -0
  36. package/dist/modules/github-app/github-app.routes.d.ts +59 -0
  37. package/dist/modules/github-app/github-app.routes.d.ts.map +1 -0
  38. package/dist/modules/github-app/github-app.routes.js +323 -0
  39. package/dist/modules/github-app/github-app.routes.js.map +1 -0
  40. package/dist/modules/github-app/index.d.ts +4 -0
  41. package/dist/modules/github-app/index.d.ts.map +1 -0
  42. package/dist/modules/github-app/index.js +4 -0
  43. package/dist/modules/github-app/index.js.map +1 -0
  44. package/dist/modules/kb-fs/remote-url.d.ts +22 -0
  45. package/dist/modules/kb-fs/remote-url.d.ts.map +1 -0
  46. package/dist/modules/kb-fs/remote-url.js +35 -0
  47. package/dist/modules/kb-fs/remote-url.js.map +1 -0
  48. package/dist/modules/plugins/join-proposals.d.ts +17 -5
  49. package/dist/modules/plugins/join-proposals.d.ts.map +1 -1
  50. package/dist/modules/plugins/join-proposals.js +76 -22
  51. package/dist/modules/plugins/join-proposals.js.map +1 -1
  52. package/dist/modules/plugins/join-requests.service.d.ts +111 -18
  53. package/dist/modules/plugins/join-requests.service.d.ts.map +1 -1
  54. package/dist/modules/plugins/join-requests.service.js +173 -29
  55. package/dist/modules/plugins/join-requests.service.js.map +1 -1
  56. package/dist/modules/plugins/plugins.routes.d.ts.map +1 -1
  57. package/dist/modules/plugins/plugins.routes.js +3 -2
  58. package/dist/modules/plugins/plugins.routes.js.map +1 -1
  59. package/dist/modules/settings/deployment-settings.service.d.ts +22 -1
  60. package/dist/modules/settings/deployment-settings.service.d.ts.map +1 -1
  61. package/dist/modules/settings/deployment-settings.service.js +93 -4
  62. package/dist/modules/settings/deployment-settings.service.js.map +1 -1
  63. package/dist/modules/settings/managed-repository.d.ts +43 -0
  64. package/dist/modules/settings/managed-repository.d.ts.map +1 -0
  65. package/dist/modules/settings/managed-repository.js +60 -0
  66. package/dist/modules/settings/managed-repository.js.map +1 -0
  67. package/dist/modules/settings/repository-source.d.ts +128 -0
  68. package/dist/modules/settings/repository-source.d.ts.map +1 -0
  69. package/dist/modules/settings/repository-source.js +150 -0
  70. package/dist/modules/settings/repository-source.js.map +1 -0
  71. package/dist/modules/settings/setup.routes.d.ts +27 -4
  72. package/dist/modules/settings/setup.routes.d.ts.map +1 -1
  73. package/dist/modules/settings/setup.routes.js +187 -13
  74. package/dist/modules/settings/setup.routes.js.map +1 -1
  75. package/dist/modules/skills/skill-access-requests.routes.d.ts +6 -8
  76. package/dist/modules/skills/skill-access-requests.routes.d.ts.map +1 -1
  77. package/dist/modules/skills/skill-access-requests.routes.js +24 -63
  78. package/dist/modules/skills/skill-access-requests.routes.js.map +1 -1
  79. package/dist/modules/tool-helpers/tool-context.d.ts.map +1 -1
  80. package/dist/modules/tool-helpers/tool-context.js +15 -0
  81. package/dist/modules/tool-helpers/tool-context.js.map +1 -1
  82. package/dist/modules/workflow/agent-tools/workflow.tools.d.ts.map +1 -1
  83. package/dist/modules/workflow/agent-tools/workflow.tools.js +38 -2
  84. package/dist/modules/workflow/agent-tools/workflow.tools.js.map +1 -1
  85. package/dist/modules/workflow/git/git.service.d.ts +3 -1
  86. package/dist/modules/workflow/git/git.service.d.ts.map +1 -1
  87. package/dist/modules/workflow/git/git.service.js +17 -4
  88. package/dist/modules/workflow/git/git.service.js.map +1 -1
  89. package/dist/modules/workflow/git/node-git-runner.d.ts.map +1 -1
  90. package/dist/modules/workflow/git/node-git-runner.js +5 -0
  91. package/dist/modules/workflow/git/node-git-runner.js.map +1 -1
  92. package/dist/modules/workflow/git/pull-request.service.d.ts +45 -0
  93. package/dist/modules/workflow/git/pull-request.service.d.ts.map +1 -1
  94. package/dist/modules/workflow/git/pull-request.service.js +99 -0
  95. package/dist/modules/workflow/git/pull-request.service.js.map +1 -1
  96. package/dist/modules/workflow/workflow.routes.d.ts.map +1 -1
  97. package/dist/modules/workflow/workflow.routes.js +3 -1
  98. package/dist/modules/workflow/workflow.routes.js.map +1 -1
  99. package/dist/modules/workflow/workflow.service.d.ts +34 -7
  100. package/dist/modules/workflow/workflow.service.d.ts.map +1 -1
  101. package/dist/modules/workflow/workflow.service.js +148 -48
  102. package/dist/modules/workflow/workflow.service.js.map +1 -1
  103. package/dist/modules/workspace/startup/kb-startup-runner.d.ts +66 -0
  104. package/dist/modules/workspace/startup/kb-startup-runner.d.ts.map +1 -1
  105. package/dist/modules/workspace/startup/kb-startup-runner.js +148 -0
  106. package/dist/modules/workspace/startup/kb-startup-runner.js.map +1 -1
  107. package/dist/modules/workspace/workspace.service.d.ts.map +1 -1
  108. package/dist/modules/workspace/workspace.service.js +17 -1
  109. package/dist/modules/workspace/workspace.service.js.map +1 -1
  110. package/dist/modules/workspace/workspace.tools.d.ts.map +1 -1
  111. package/dist/modules/workspace/workspace.tools.js +28 -2
  112. package/dist/modules/workspace/workspace.tools.js.map +1 -1
  113. package/dist/shared/domain-errors.d.ts +39 -0
  114. package/dist/shared/domain-errors.d.ts.map +1 -1
  115. package/dist/shared/domain-errors.js +55 -0
  116. package/dist/shared/domain-errors.js.map +1 -1
  117. package/dist/shared/git.contract.d.ts +10 -0
  118. package/dist/shared/git.contract.d.ts.map +1 -1
  119. package/dist/shared/git.contract.js.map +1 -1
  120. package/package.json +3 -3
  121. package/src/core/create-core-server.ts +37 -1
  122. package/src/core/create-core-services.ts +65 -8
  123. package/src/modules/access/__tests__/access-requests.recut.test.ts +134 -0
  124. package/src/modules/access/__tests__/access-requests.routes.test.ts +610 -0
  125. package/src/modules/access/access-requests.contract.ts +93 -0
  126. package/src/modules/access/access-requests.routes.ts +286 -0
  127. package/src/modules/access/access-requests.service.ts +420 -0
  128. package/src/modules/access-model/access-grammar.ts +22 -0
  129. package/src/modules/auth/__tests__/auth.service.test.ts +38 -0
  130. package/src/modules/auth/auth.service.ts +28 -1
  131. package/src/modules/github-app/__tests__/github-app.test.ts +848 -0
  132. package/src/modules/github-app/github-app.client.ts +268 -0
  133. package/src/modules/github-app/github-app.connection.ts +204 -0
  134. package/src/modules/github-app/github-app.routes.ts +359 -0
  135. package/src/modules/github-app/index.ts +19 -0
  136. package/src/modules/kb-fs/__tests__/remote-url.test.ts +38 -0
  137. package/src/modules/kb-fs/remote-url.ts +35 -0
  138. package/src/modules/plugins/__tests__/join-proposals.test.ts +100 -19
  139. package/src/modules/plugins/__tests__/join-requests.service.test.ts +27 -11
  140. package/src/modules/plugins/__tests__/join-requests.settlement.test.ts +371 -0
  141. package/src/modules/plugins/__tests__/plugins.routes.test.ts +1 -1
  142. package/src/modules/plugins/join-proposals.ts +87 -19
  143. package/src/modules/plugins/join-requests.service.ts +199 -39
  144. package/src/modules/plugins/plugins.routes.ts +3 -2
  145. package/src/modules/settings/__tests__/repository-source.test.ts +191 -0
  146. package/src/modules/settings/__tests__/setup.routes.git-mode.test.ts +353 -0
  147. package/src/modules/settings/__tests__/setup.routes.github-app.test.ts +282 -0
  148. package/src/modules/settings/__tests__/setup.routes.managed-phase.test.ts +233 -0
  149. package/src/modules/settings/deployment-settings.service.ts +109 -3
  150. package/src/modules/settings/managed-repository.ts +68 -0
  151. package/src/modules/settings/repository-source.ts +197 -0
  152. package/src/modules/settings/setup.routes.ts +214 -10
  153. package/src/modules/skills/__tests__/skill-access-requests.routes.test.ts +5 -1
  154. package/src/modules/skills/skill-access-requests.routes.ts +25 -75
  155. package/src/modules/tool-helpers/tool-context.ts +15 -0
  156. package/src/modules/workflow/__tests__/apply-failure.test.ts +9 -1
  157. package/src/modules/workflow/__tests__/workflow.service.facade.test.ts +159 -8
  158. package/src/modules/workflow/__tests__/workflow.service.update-from-target.test.ts +146 -40
  159. package/src/modules/workflow/agent-tools/__tests__/workflow.tools.test.ts +141 -1
  160. package/src/modules/workflow/agent-tools/workflow.tools.ts +49 -3
  161. package/src/modules/workflow/git/__tests__/git.service.pull.test.ts +121 -0
  162. package/src/modules/workflow/git/__tests__/pull-request.service.getPrDetail.test.ts +145 -2
  163. package/src/modules/workflow/git/__tests__/pull-request.service.viewer-can-delete.test.ts +166 -0
  164. package/src/modules/workflow/git/git.service.ts +25 -4
  165. package/src/modules/workflow/git/node-git-runner.ts +6 -0
  166. package/src/modules/workflow/git/pull-request.service.ts +119 -0
  167. package/src/modules/workflow/workflow.routes.ts +3 -1
  168. package/src/modules/workflow/workflow.service.ts +170 -52
  169. package/src/modules/workspace/__tests__/workspace.service.unknown-branch.test.ts +43 -0
  170. package/src/modules/workspace/__tests__/workspace.tools.branch-errors.test.ts +233 -7
  171. package/src/modules/workspace/__tests__/workspace.tools.test.ts +35 -3
  172. package/src/modules/workspace/startup/__tests__/kb-startup-runner.test.ts +211 -0
  173. package/src/modules/workspace/startup/kb-startup-runner.ts +159 -0
  174. package/src/modules/workspace/workspace.service.ts +17 -1
  175. package/src/modules/workspace/workspace.tools.ts +35 -0
  176. package/src/shared/domain-errors.ts +58 -0
  177. package/src/shared/git.contract.ts +10 -0
@@ -29,7 +29,7 @@ import { promises as fs } from 'node:fs';
29
29
  import { execFile } from 'node:child_process';
30
30
  import { promisify } from 'node:util';
31
31
  import { isFolderPlaceholder, folderPlaceholderPath, isPlatformRestoreShape, } from '@bevel-software/platform-shared';
32
- import { and, eq, inArray, isNotNull, isNull, lt, or } from 'drizzle-orm';
32
+ import { and, desc, eq, inArray, isNotNull, isNull, lt, ne, or } from 'drizzle-orm';
33
33
  import { changeRequests } from '../database/schema.js';
34
34
  import { redactSecret } from '../../shared/redact-secret.js';
35
35
  import { workspaceIdForBranch, branchForWorkspaceId } from '../../shared/workspace-id.js';
@@ -470,8 +470,8 @@ export class WorkflowService {
470
470
  * and nothing else. The pull itself reports the change, since it is the
471
471
  * only code that can observe it under the workspace mutex.
472
472
  */
473
- async pullWorkspace(workspaceId) {
474
- const { treeChanged } = await this.git.pull(workspaceId);
473
+ async pullWorkspace(workspaceId, opts = {}) {
474
+ const { treeChanged } = await this.git.pull(workspaceId, opts);
475
475
  const defaultBranch = this.kb.defaultBranch;
476
476
  if (treeChanged && branchForWorkspaceId(workspaceId) === defaultBranch) {
477
477
  this.events?.emit({ kind: 'fs-tree-changed', workspaceId, branch: defaultBranch });
@@ -1427,6 +1427,30 @@ export class WorkflowService {
1427
1427
  getChangeRequest(number) {
1428
1428
  return this.prs.getPr(number);
1429
1429
  }
1430
+ /**
1431
+ * The newest non-open change request from `sourceBranch` by `authorEmail`.
1432
+ *
1433
+ * Read straight off `change_requests` rather than through the PR listing,
1434
+ * which serves OPEN requests only: the whole point here is the closed ones.
1435
+ * `author_email` is stored lowercased at insert, so the needle is too.
1436
+ * Ordered by `closed_at` with `created_at` as the tiebreak, because a row
1437
+ * closed before the column existed (or by a path that forgot to stamp it)
1438
+ * still has a creation time and must not sort as the oldest.
1439
+ */
1440
+ async latestClosedChangeRequest(authorEmail, sourceBranch) {
1441
+ const email = authorEmail.trim().toLowerCase();
1442
+ if (!email || !sourceBranch)
1443
+ return null;
1444
+ const [row] = await this.db
1445
+ .select({ number: changeRequests.number, state: changeRequests.state })
1446
+ .from(changeRequests)
1447
+ .where(and(eq(changeRequests.authorEmail, email), eq(changeRequests.sourceBranch, sourceBranch), ne(changeRequests.state, 'open')))
1448
+ .orderBy(desc(changeRequests.closedAt), desc(changeRequests.createdAt))
1449
+ .limit(1);
1450
+ if (!row)
1451
+ return null;
1452
+ return { number: row.number, state: row.state };
1453
+ }
1430
1454
  getChangeRequestDetail(number, opts) {
1431
1455
  return this.prs.getPrDetail(number, opts);
1432
1456
  }
@@ -1594,9 +1618,25 @@ export class WorkflowService {
1594
1618
  * the enforcement read one predicate. A conflicting merge is aborted
1595
1619
  * inside `mergeFromOrigin`: nothing is committed or pushed, and the branch
1596
1620
  * is exactly what it was.
1621
+ *
1622
+ * ONE detail read after the merge. The read before it exists only to
1623
+ * decide authority and to name the two branches, so it asks for no patches
1624
+ * — that is one git subprocess per changed file it does not spend. The
1625
+ * approvals carry-forward runs BEFORE the final read rather than after it,
1626
+ * against a head resolved with `resolvePrShas` (two rev-parses) instead of
1627
+ * a whole second detail, so the detail that goes back already describes the
1628
+ * approvals it carried. What used to be three detail reads is now one.
1597
1629
  */
1598
1630
  async updateFromTarget(workspaceId, user, number) {
1599
- const detail = await this.prs.getPrDetail(number, { fresh: true, workspaceId, viewerEmail: user.email });
1631
+ // No patches: all this read decides is authority, state and the two
1632
+ // branch names. Asking for patch text here would diff every changed file
1633
+ // in a request that is about to be merged and read in full anyway.
1634
+ const detail = await this.prs.getPrDetail(number, {
1635
+ fresh: true,
1636
+ workspaceId,
1637
+ viewerEmail: user.email,
1638
+ patches: false,
1639
+ });
1600
1640
  if (!detail) {
1601
1641
  throw new WorkflowValidationError(`Change request #${number} not found.`, { kind: 'change-request-not-found', number });
1602
1642
  }
@@ -1615,8 +1655,15 @@ export class WorkflowService {
1615
1655
  // merge commit that then cannot push, stranding it in the workspace. A
1616
1656
  // rebase conflict hands the stranded saves to recovery, as every other
1617
1657
  // pull does, before the refusal reaches the caller.
1658
+ //
1659
+ // `preserveMerges`: this is the one pull whose unpushed commit may be a
1660
+ // MERGE — the update that ran before this one and could not push. A plain
1661
+ // rebase replays it as cherry-picks of the target's commits, which drops
1662
+ // the target as a parent, so the branch never contains the target's head
1663
+ // however many times the update runs. Replayed with `--rebase-merges` it
1664
+ // stays a merge and the next push ends the loop.
1618
1665
  try {
1619
- await this.pullWorkspace(workspaceId);
1666
+ await this.pullWorkspace(workspaceId, { preserveMerges: true });
1620
1667
  }
1621
1668
  catch (err) {
1622
1669
  if (err instanceof PullRebaseConflictError) {
@@ -1648,21 +1695,47 @@ export class WorkflowService {
1648
1695
  if (outcome.kind === 'conflicts') {
1649
1696
  throw new ChangeRequestConflictsError(detail.branch, detail.base, outcome.paths);
1650
1697
  }
1651
- // If a new merge commit landed, push so the CR picks it up. When
1652
- // already up to date there's nothing to share — short-circuit the push.
1653
- if (!outcome.alreadyUpToDate) {
1698
+ // Push whenever the CLONE holds a commit origin has not seen — not when
1699
+ // THIS merge authored one. The two differ in exactly the case that keeps
1700
+ // a request behind for good: an earlier update merged, its push failed,
1701
+ // and the merge is sitting in the workspace. That merge already contains
1702
+ // the target, so this call's `mergeFromOrigin` reports
1703
+ // `alreadyUpToDate` and the old gate skipped the push — leaving the
1704
+ // request behind on the remote, and the next open with the same work to
1705
+ // do. Asking git what is unpushed retries it instead.
1706
+ //
1707
+ // A failed probe falls back to the old gate rather than to "push
1708
+ // anyway": `trackedPush` on a clone with nothing to push is a wasted
1709
+ // round trip on every single update.
1710
+ const hasUnpushed = await this.git
1711
+ .hasUnpushedCommits(workspaceId)
1712
+ .catch((err) => {
1713
+ log.warn(`could not tell whether change request #${number} has unpushed commits; falling back to whether this merge authored one`, { err });
1714
+ return !outcome.alreadyUpToDate;
1715
+ });
1716
+ if (hasUnpushed) {
1654
1717
  await this.trackedPush(workspaceId, user);
1655
- // The source head moved: the refusal described a revision that is gone.
1656
- await this.clearApplyFailure(number, { recordedBefore: headMovedAfter });
1657
1718
  }
1658
- this.prs.invalidateDetailCache(number);
1659
- const refreshed = await this.prs.getPrDetail(number, {
1660
- fresh: true,
1661
- workspaceId,
1662
- viewerEmail: user.email,
1663
- });
1664
- if (!refreshed) {
1665
- throw new WorkflowValidationError(`Refreshed change request #${number} but could not re-fetch its detail.`);
1719
+ // Where the branch ended up, as PUBLISHED — two rev-parses, not a second
1720
+ // detail read. Everything below needs only the sha, and the detail that
1721
+ // goes back to the caller is read once, at the end, after the approvals
1722
+ // it should describe have been carried.
1723
+ let headAfterMerge = headBeforeMerge;
1724
+ try {
1725
+ const at = await this.git.resolvePrShas(workspaceId, detail.base, detail.branch);
1726
+ headAfterMerge = at.headSha;
1727
+ }
1728
+ catch (err) {
1729
+ log.warn(`could not resolve the published head of change request #${number} after updating it from target`, { err });
1730
+ }
1731
+ const headMoved = Boolean(headBeforeMerge) && headAfterMerge !== headBeforeMerge;
1732
+ // The source head moved: the refusal described a revision that is gone.
1733
+ // `hasUnpushed` counts as well as the sha comparison, and deliberately —
1734
+ // a push that just landed moved the published head by definition, and
1735
+ // reading the sha to confirm it is a call that can fail. A refusal left
1736
+ // standing over a revision nobody can apply any more is the worse error.
1737
+ if (headMoved || hasUnpushed) {
1738
+ await this.clearApplyFailure(number, { recordedBefore: headMovedAfter });
1666
1739
  }
1667
1740
  // The merge is a commit the APPROVERS did not make. Re-pin their
1668
1741
  // approvals onto the new head for every file whose bytes it left alone —
@@ -1671,35 +1744,48 @@ export class WorkflowService {
1671
1744
  // than the bookkeeping, so a failure here is logged and the fresh detail
1672
1745
  // still goes back.
1673
1746
  //
1674
- // `alreadyUpToDate` is the real gate: no merge commit, no head to compare,
1675
- // nothing moved under anyone. The other two conditions are belt-and-braces
1676
- // over a sha that is not a sha — a resolve that failed onto a detail with
1677
- // no head, or two heads that somehow read equal — both of which
1678
- // `pathsChangedBetween` and `carryApprovalsForward` would refuse anyway.
1679
- // A merge that authored a commit cannot leave the head where it was.
1680
- if (!outcome.alreadyUpToDate && headBeforeMerge && refreshed.headSha !== headBeforeMerge) {
1747
+ // The head moving is the real gate: nothing moved, nothing moved under
1748
+ // anyone. It also covers the sha that is not a sha — a resolve that
1749
+ // failed onto a detail with no head — which `pathsChangedBetween` and
1750
+ // `carryApprovalsForward` would refuse anyway.
1751
+ //
1752
+ // This runs BEFORE the detail read, not after it. The rows it writes are
1753
+ // what the caller's `approvals` must describe, so doing it first means
1754
+ // the one read at the end is already correct — where carrying afterwards
1755
+ // needed a THIRD read to correct a detail assembled before the rows
1756
+ // existed.
1757
+ //
1758
+ // The same list answers the caller's `updatedPaths`: "which files did
1759
+ // this merge change" is one question, and the two answers must not be
1760
+ // allowed to differ — a file whose approval was carried (so: untouched)
1761
+ // that the dialog then re-read would be the dialog discarding content the
1762
+ // server just called unchanged.
1763
+ let updatedPaths = [];
1764
+ if (headMoved) {
1681
1765
  try {
1682
- const changedPaths = await this.git.pathsChangedBetween(workspaceId, headBeforeMerge, refreshed.headSha);
1683
- const carried = await this.reviewWorkflow.carryApprovalsForward(number, headBeforeMerge, refreshed.headSha, changedPaths);
1684
- // The detail above was assembled before those rows existed, so its
1685
- // `approvals` (and the merge gate derived from them) still describe
1686
- // the pre-merge head. Only re-read when something actually carried.
1687
- if (carried > 0) {
1688
- this.prs.invalidateDetailCache(number);
1689
- const withApprovals = await this.prs.getPrDetail(number, {
1690
- fresh: true,
1691
- workspaceId,
1692
- viewerEmail: user.email,
1693
- });
1694
- if (withApprovals)
1695
- return withApprovals;
1696
- }
1766
+ updatedPaths = await this.git.pathsChangedBetween(workspaceId, headBeforeMerge, headAfterMerge);
1767
+ await this.reviewWorkflow.carryApprovalsForward(number, headBeforeMerge, headAfterMerge, updatedPaths);
1697
1768
  }
1698
1769
  catch (err) {
1699
1770
  log.warn(`could not carry approvals forward on change request #${number} after updating from target`, { err });
1771
+ // The diff is what failed, so which files moved is unknown. Say so as
1772
+ // "every file this request had", which is every file a caller can
1773
+ // already be showing: slow beats a dialog that goes on presenting
1774
+ // pre-merge text as current. Anything the merge ADDED is not on the
1775
+ // list and does not need to be — no caller has read it yet.
1776
+ updatedPaths = (detail.files ?? []).map((f) => f.path);
1700
1777
  }
1701
1778
  }
1702
- return refreshed;
1779
+ this.prs.invalidateDetailCache(number);
1780
+ const refreshed = await this.prs.getPrDetail(number, {
1781
+ fresh: true,
1782
+ workspaceId,
1783
+ viewerEmail: user.email,
1784
+ });
1785
+ if (!refreshed) {
1786
+ throw new WorkflowValidationError(`Refreshed change request #${number} but could not re-fetch its detail.`);
1787
+ }
1788
+ return { ...refreshed, updatedPaths };
1703
1789
  }
1704
1790
  listComments(number) {
1705
1791
  return this.reviewWorkflow.listComments(number);
@@ -2126,11 +2212,16 @@ export class WorkflowService {
2126
2212
  /**
2127
2213
  * DELETE a change request outright: close it (whatever its diff says) and
2128
2214
  * retire its source branch — the request, its proposal, and the branch that
2129
- * carried it are gone in one verb. Admin-only, resolved the same way every
2130
- * other admin check here is (write on `roles.yaml` at `origin/<base>`,
2131
- * never a local ref): this is the moderation verb for a shared deployment,
2132
- * stronger than reject (which the author or the files' owners can do, and
2133
- * which leaves the branch for a second round).
2215
+ * carried it are gone in one verb. The request's AUTHOR or an admin, and
2216
+ * nobody else: it is the author's own proposal and their own branch to throw
2217
+ * away, and it is the moderation verb for a shared deployment. Admin rights
2218
+ * resolve the same way every other admin check here does (write on
2219
+ * `roles.yaml` at `origin/<base>`, never a local ref); authorship is the
2220
+ * STORED `authorId` hash, never anything the caller sends.
2221
+ *
2222
+ * Stronger than reject, which the changed files' owners may also do and which
2223
+ * leaves the branch for a second round. They keep reject and do not get this:
2224
+ * an owner must not be able to destroy someone else's text and branch.
2134
2225
  */
2135
2226
  async deleteChangeRequest(number, user) {
2136
2227
  const summary = await this.prs.getPr(number);
@@ -2180,8 +2271,17 @@ export class WorkflowService {
2180
2271
  this.kickDeletedBranchSweep();
2181
2272
  throw new WorkflowDomainError(`The base branch "${summary.base}" could not be resolved — it may have been deleted, or origin was unreachable. A background check is running; retry in a moment.`, 409);
2182
2273
  }
2183
- if (isAdmin !== true) {
2184
- throw new WorkflowDomainError('Only an admin can delete a change request.', 403);
2274
+ // Authorship is decided from the STORED author hash, never from anything
2275
+ // the caller sends. Note what is deliberately NOT short-circuited for an
2276
+ // author: the strict fetch and the `null` check above still run. An
2277
+ // author's grant needs no `roles.yaml` read, so skipping them would look
2278
+ // free — but it would let a delete close the request while the branch
2279
+ // removal silently failed against an unreachable origin, and "the shared
2280
+ // repository was down, so nothing changed" is the one refusal that has to
2281
+ // stay honest.
2282
+ const callerIsAuthor = !!(summary.authorId && summary.authorId === hashEmail(user.email));
2283
+ if (!callerIsAuthor && isAdmin !== true) {
2284
+ throw new WorkflowDomainError("Only the request's author or an admin can delete it.", 403);
2185
2285
  }
2186
2286
  // Close first (idempotent: an already-closed request just skips to the
2187
2287
  // branch retirement). Guarded on `open` so a concurrent merge wins.