@bevel-software/platform-core-backend 0.23.0 → 0.25.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 (238) hide show
  1. package/dist/core/create-core-server.d.ts.map +1 -1
  2. package/dist/core/create-core-server.js +17 -3
  3. package/dist/core/create-core-server.js.map +1 -1
  4. package/dist/core/create-core-services.d.ts +3 -0
  5. package/dist/core/create-core-services.d.ts.map +1 -1
  6. package/dist/core/create-core-services.js +25 -1
  7. package/dist/core/create-core-services.js.map +1 -1
  8. package/dist/core/lifecycle.d.ts +12 -0
  9. package/dist/core/lifecycle.d.ts.map +1 -1
  10. package/dist/core/lifecycle.js +10 -0
  11. package/dist/core/lifecycle.js.map +1 -1
  12. package/dist/core-config.d.ts +16 -0
  13. package/dist/core-config.d.ts.map +1 -1
  14. package/dist/core-config.js +17 -0
  15. package/dist/core-config.js.map +1 -1
  16. package/dist/index.d.ts +3 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +13 -2
  19. package/dist/index.js.map +1 -1
  20. package/dist/modules/access/access.routes.d.ts.map +1 -1
  21. package/dist/modules/access/access.routes.js +4 -1
  22. package/dist/modules/access/access.routes.js.map +1 -1
  23. package/dist/modules/access/directory-sync-bot.d.ts.map +1 -1
  24. package/dist/modules/access/directory-sync-bot.js +7 -3
  25. package/dist/modules/access/directory-sync-bot.js.map +1 -1
  26. package/dist/modules/agent-instructions/agent-instructions.routes.d.ts +8 -1
  27. package/dist/modules/agent-instructions/agent-instructions.routes.d.ts.map +1 -1
  28. package/dist/modules/agent-instructions/agent-instructions.routes.js +8 -2
  29. package/dist/modules/agent-instructions/agent-instructions.routes.js.map +1 -1
  30. package/dist/modules/agent-instructions/compose.d.ts +38 -6
  31. package/dist/modules/agent-instructions/compose.d.ts.map +1 -1
  32. package/dist/modules/agent-instructions/compose.js +39 -6
  33. package/dist/modules/agent-instructions/compose.js.map +1 -1
  34. package/dist/modules/agent-instructions/index.d.ts +2 -1
  35. package/dist/modules/agent-instructions/index.d.ts.map +1 -1
  36. package/dist/modules/agent-instructions/index.js +2 -1
  37. package/dist/modules/agent-instructions/index.js.map +1 -1
  38. package/dist/modules/agent-instructions/shared-file-rules.d.ts +115 -0
  39. package/dist/modules/agent-instructions/shared-file-rules.d.ts.map +1 -0
  40. package/dist/modules/agent-instructions/shared-file-rules.js +272 -0
  41. package/dist/modules/agent-instructions/shared-file-rules.js.map +1 -0
  42. package/dist/modules/audit/agent-audit.service.d.ts.map +1 -1
  43. package/dist/modules/audit/agent-audit.service.js +4 -2
  44. package/dist/modules/audit/agent-audit.service.js.map +1 -1
  45. package/dist/modules/auth/account-erasure.service.d.ts.map +1 -1
  46. package/dist/modules/auth/account-erasure.service.js +57 -16
  47. package/dist/modules/auth/account-erasure.service.js.map +1 -1
  48. package/dist/modules/auth/auth.service.d.ts.map +1 -1
  49. package/dist/modules/auth/auth.service.js +25 -15
  50. package/dist/modules/auth/auth.service.js.map +1 -1
  51. package/dist/modules/code-mode/code-mode.tool.d.ts +20 -2
  52. package/dist/modules/code-mode/code-mode.tool.d.ts.map +1 -1
  53. package/dist/modules/code-mode/code-mode.tool.js +66 -35
  54. package/dist/modules/code-mode/code-mode.tool.js.map +1 -1
  55. package/dist/modules/database/connection.d.ts +16 -0
  56. package/dist/modules/database/connection.d.ts.map +1 -1
  57. package/dist/modules/database/connection.js +117 -0
  58. package/dist/modules/database/connection.js.map +1 -1
  59. package/dist/modules/database/core-schema.d.ts +296 -96
  60. package/dist/modules/database/core-schema.d.ts.map +1 -1
  61. package/dist/modules/database/core-schema.js +81 -31
  62. package/dist/modules/database/core-schema.js.map +1 -1
  63. package/dist/modules/database/migrate.d.ts +95 -1
  64. package/dist/modules/database/migrate.d.ts.map +1 -1
  65. package/dist/modules/database/migrate.js +390 -2
  66. package/dist/modules/database/migrate.js.map +1 -1
  67. package/dist/modules/kb-fs/locking-filesystem.d.ts +17 -0
  68. package/dist/modules/kb-fs/locking-filesystem.d.ts.map +1 -1
  69. package/dist/modules/kb-fs/locking-filesystem.js +29 -0
  70. package/dist/modules/kb-fs/locking-filesystem.js.map +1 -1
  71. package/dist/modules/mcp/mcp.service.d.ts +8 -0
  72. package/dist/modules/mcp/mcp.service.d.ts.map +1 -1
  73. package/dist/modules/mcp/mcp.service.js +38 -8
  74. package/dist/modules/mcp/mcp.service.js.map +1 -1
  75. package/dist/modules/plugins/join-request-records.store.d.ts.map +1 -1
  76. package/dist/modules/plugins/join-request-records.store.js +7 -4
  77. package/dist/modules/plugins/join-request-records.store.js.map +1 -1
  78. package/dist/modules/tool-auth/external-api-key.service.d.ts.map +1 -1
  79. package/dist/modules/tool-auth/external-api-key.service.js +8 -2
  80. package/dist/modules/tool-auth/external-api-key.service.js.map +1 -1
  81. package/dist/modules/tool-manuals/tool-manuals.service.d.ts.map +1 -1
  82. package/dist/modules/tool-manuals/tool-manuals.service.js +30 -0
  83. package/dist/modules/tool-manuals/tool-manuals.service.js.map +1 -1
  84. package/dist/modules/tool-manuals/tool-manuals.tools.d.ts.map +1 -1
  85. package/dist/modules/tool-manuals/tool-manuals.tools.js +42 -31
  86. package/dist/modules/tool-manuals/tool-manuals.tools.js.map +1 -1
  87. package/dist/modules/tool-registry/description-length.d.ts +80 -0
  88. package/dist/modules/tool-registry/description-length.d.ts.map +1 -0
  89. package/dist/modules/tool-registry/description-length.js +108 -0
  90. package/dist/modules/tool-registry/description-length.js.map +1 -0
  91. package/dist/modules/workflow/git/git.service.d.ts +25 -0
  92. package/dist/modules/workflow/git/git.service.d.ts.map +1 -1
  93. package/dist/modules/workflow/git/git.service.js +40 -1
  94. package/dist/modules/workflow/git/git.service.js.map +1 -1
  95. package/dist/modules/workflow/pending-commits.service.d.ts.map +1 -1
  96. package/dist/modules/workflow/pending-commits.service.js +5 -1
  97. package/dist/modules/workflow/pending-commits.service.js.map +1 -1
  98. package/dist/modules/workflow/recovery-bot.d.ts.map +1 -1
  99. package/dist/modules/workflow/recovery-bot.js +7 -3
  100. package/dist/modules/workflow/recovery-bot.js.map +1 -1
  101. package/dist/modules/workflow/review-workflow/review-workflow.service.d.ts.map +1 -1
  102. package/dist/modules/workflow/review-workflow/review-workflow.service.js +12 -3
  103. package/dist/modules/workflow/review-workflow/review-workflow.service.js.map +1 -1
  104. package/dist/modules/workflow/workflow.service.d.ts.map +1 -1
  105. package/dist/modules/workflow/workflow.service.js +4 -1
  106. package/dist/modules/workflow/workflow.service.js.map +1 -1
  107. package/dist/modules/workspace/agent-upload.routes.d.ts +77 -0
  108. package/dist/modules/workspace/agent-upload.routes.d.ts.map +1 -0
  109. package/dist/modules/workspace/agent-upload.routes.js +210 -0
  110. package/dist/modules/workspace/agent-upload.routes.js.map +1 -0
  111. package/dist/modules/workspace/agent-upload.store.d.ts +284 -0
  112. package/dist/modules/workspace/agent-upload.store.d.ts.map +1 -0
  113. package/dist/modules/workspace/agent-upload.store.js +553 -0
  114. package/dist/modules/workspace/agent-upload.store.js.map +1 -0
  115. package/dist/modules/workspace/startup/steps/seed-tree.d.ts.map +1 -1
  116. package/dist/modules/workspace/startup/steps/seed-tree.js +3 -3
  117. package/dist/modules/workspace/startup/steps/seed-tree.js.map +1 -1
  118. package/dist/modules/workspace/startup/steps/template-source.d.ts +40 -0
  119. package/dist/modules/workspace/startup/steps/template-source.d.ts.map +1 -1
  120. package/dist/modules/workspace/startup/steps/template-source.js +46 -4
  121. package/dist/modules/workspace/startup/steps/template-source.js.map +1 -1
  122. package/dist/modules/workspace/upload-limits.d.ts +13 -0
  123. package/dist/modules/workspace/upload-limits.d.ts.map +1 -0
  124. package/dist/modules/workspace/upload-limits.js +13 -0
  125. package/dist/modules/workspace/upload-limits.js.map +1 -0
  126. package/dist/modules/workspace/workspace.routes.d.ts.map +1 -1
  127. package/dist/modules/workspace/workspace.routes.js +1 -1
  128. package/dist/modules/workspace/workspace.routes.js.map +1 -1
  129. package/dist/modules/workspace/workspace.service.d.ts +13 -0
  130. package/dist/modules/workspace/workspace.service.d.ts.map +1 -1
  131. package/dist/modules/workspace/workspace.service.js +61 -33
  132. package/dist/modules/workspace/workspace.service.js.map +1 -1
  133. package/dist/modules/workspace/workspace.tools.d.ts +11 -9
  134. package/dist/modules/workspace/workspace.tools.d.ts.map +1 -1
  135. package/dist/modules/workspace/workspace.tools.js +570 -134
  136. package/dist/modules/workspace/workspace.tools.js.map +1 -1
  137. package/dist/modules/workspace/write-denial.d.ts +0 -6
  138. package/dist/modules/workspace/write-denial.d.ts.map +1 -1
  139. package/dist/modules/workspace/write-denial.js +0 -6
  140. package/dist/modules/workspace/write-denial.js.map +1 -1
  141. package/dist/modules/workspace/zip-entry-rules.d.ts +114 -0
  142. package/dist/modules/workspace/zip-entry-rules.d.ts.map +1 -0
  143. package/dist/modules/workspace/zip-entry-rules.js +154 -0
  144. package/dist/modules/workspace/zip-entry-rules.js.map +1 -0
  145. package/dist/shared/column-crypto.d.ts +194 -0
  146. package/dist/shared/column-crypto.d.ts.map +1 -0
  147. package/dist/shared/column-crypto.js +144 -0
  148. package/dist/shared/column-crypto.js.map +1 -0
  149. package/dist/shared/token-crypto.d.ts.map +1 -1
  150. package/dist/shared/token-crypto.js +25 -1
  151. package/dist/shared/token-crypto.js.map +1 -1
  152. package/dist/tenancy/static-tenant-source.d.ts.map +1 -1
  153. package/dist/tenancy/static-tenant-source.js +1 -0
  154. package/dist/tenancy/static-tenant-source.js.map +1 -1
  155. package/dist/tenancy/tenant-secrets.d.ts +5 -1
  156. package/dist/tenancy/tenant-secrets.d.ts.map +1 -1
  157. package/dist/tenancy/tenant-secrets.js +4 -0
  158. package/dist/tenancy/tenant-secrets.js.map +1 -1
  159. package/kb-template/AGENTS.md +42 -0
  160. package/migrations/0016_pii_encryption.sql +20 -0
  161. package/migrations/meta/0016_snapshot.json +2327 -0
  162. package/migrations/meta/_journal.json +7 -0
  163. package/package.json +3 -3
  164. package/src/core/__tests__/lifecycle.test.ts +71 -4
  165. package/src/core/create-core-server.ts +21 -3
  166. package/src/core/create-core-services.ts +27 -1
  167. package/src/core/lifecycle.ts +19 -0
  168. package/src/core-config.ts +18 -0
  169. package/src/index.ts +25 -0
  170. package/src/modules/access/__tests__/users-db-double.ts +21 -12
  171. package/src/modules/access/access.routes.ts +4 -1
  172. package/src/modules/access/directory-sync-bot.ts +7 -3
  173. package/src/modules/agent-instructions/__tests__/agent-instructions.route.test.ts +8 -4
  174. package/src/modules/agent-instructions/__tests__/compose.test.ts +19 -10
  175. package/src/modules/agent-instructions/__tests__/shared-file-rules.test.ts +238 -0
  176. package/src/modules/agent-instructions/agent-instructions.routes.ts +12 -2
  177. package/src/modules/agent-instructions/compose.ts +50 -7
  178. package/src/modules/agent-instructions/index.ts +12 -0
  179. package/src/modules/agent-instructions/shared-file-rules.ts +314 -0
  180. package/src/modules/audit/agent-audit.service.ts +4 -2
  181. package/src/modules/auth/__tests__/account-deactivation.test.ts +2 -1
  182. package/src/modules/auth/__tests__/account-erasure.approval-gate.test.ts +6 -2
  183. package/src/modules/auth/__tests__/account.routes.test.ts +6 -3
  184. package/src/modules/auth/__tests__/auth.service.test.ts +3 -1
  185. package/src/modules/auth/account-erasure.service.ts +69 -18
  186. package/src/modules/auth/auth.service.ts +33 -23
  187. package/src/modules/code-mode/__tests__/chain-runtime.e2e.test.ts +335 -0
  188. package/src/modules/code-mode/__tests__/code-mode.tool.test.ts +46 -2
  189. package/src/modules/code-mode/code-mode.tool.ts +80 -34
  190. package/src/modules/database/__tests__/connection.test.ts +12 -0
  191. package/src/modules/database/__tests__/pii-backfill.pg.test.ts +780 -0
  192. package/src/modules/database/connection.ts +117 -0
  193. package/src/modules/database/core-schema.ts +81 -31
  194. package/src/modules/database/migrate.ts +540 -2
  195. package/src/modules/kb-fs/__tests__/locking-filesystem.test.ts +93 -0
  196. package/src/modules/kb-fs/locking-filesystem.ts +37 -0
  197. package/src/modules/mcp/__tests__/mcp.e2e.test.ts +4 -3
  198. package/src/modules/mcp/__tests__/mcp.service.test.ts +47 -7
  199. package/src/modules/mcp/mcp.service.ts +46 -7
  200. package/src/modules/plugins/join-request-records.store.ts +7 -4
  201. package/src/modules/tool-auth/external-api-key.service.ts +8 -2
  202. package/src/modules/tool-manuals/__tests__/tool-manuals.service.test.ts +151 -0
  203. package/src/modules/tool-manuals/tool-manuals.service.ts +37 -0
  204. package/src/modules/tool-manuals/tool-manuals.tools.ts +44 -31
  205. package/src/modules/tool-registry/__tests__/tool-description-length.test.ts +378 -0
  206. package/src/modules/tool-registry/description-length.ts +111 -0
  207. package/src/modules/workflow/git/__tests__/git.service.prFetchFailure.test.ts +170 -0
  208. package/src/modules/workflow/git/git.service.ts +47 -1
  209. package/src/modules/workflow/pending-commits.service.ts +5 -1
  210. package/src/modules/workflow/recovery-bot.ts +7 -3
  211. package/src/modules/workflow/review-workflow/__tests__/carry-approvals-forward.test.ts +4 -0
  212. package/src/modules/workflow/review-workflow/__tests__/erase-approver.test.ts +25 -3
  213. package/src/modules/workflow/review-workflow/review-workflow.service.ts +12 -3
  214. package/src/modules/workflow/workflow.service.ts +4 -1
  215. package/src/modules/workspace/__tests__/agent-uploads.test.ts +1604 -0
  216. package/src/modules/workspace/__tests__/escape-sequences.routes.test.ts +24 -14
  217. package/src/modules/workspace/__tests__/workspace.service.any-workspace-credentials.test.ts +156 -0
  218. package/src/modules/workspace/__tests__/workspace.service.replaced-repository.test.ts +77 -1
  219. package/src/modules/workspace/__tests__/workspace.service.test.ts +57 -0
  220. package/src/modules/workspace/__tests__/workspace.tools.agents-file.test.ts +39 -36
  221. package/src/modules/workspace/__tests__/workspace.tools.test.ts +196 -46
  222. package/src/modules/workspace/agent-upload.routes.ts +214 -0
  223. package/src/modules/workspace/agent-upload.store.ts +668 -0
  224. package/src/modules/workspace/startup/steps/__tests__/steps.test.ts +9 -9
  225. package/src/modules/workspace/startup/steps/seed-tree.ts +3 -6
  226. package/src/modules/workspace/startup/steps/template-source.ts +53 -5
  227. package/src/modules/workspace/upload-limits.ts +12 -0
  228. package/src/modules/workspace/workspace.routes.ts +1 -2
  229. package/src/modules/workspace/workspace.service.ts +63 -37
  230. package/src/modules/workspace/workspace.tools.ts +647 -148
  231. package/src/modules/workspace/write-denial.ts +0 -8
  232. package/src/modules/workspace/zip-entry-rules.ts +173 -0
  233. package/src/shared/__tests__/column-crypto.test.ts +217 -0
  234. package/src/shared/column-crypto.ts +218 -0
  235. package/src/shared/token-crypto.ts +28 -1
  236. package/src/tenancy/__tests__/static-tenant-source.test.ts +4 -0
  237. package/src/tenancy/static-tenant-source.ts +1 -0
  238. package/src/tenancy/tenant-secrets.ts +5 -1
@@ -1,8 +1,10 @@
1
1
  import { migrate } from 'drizzle-orm/node-postgres/migrator';
2
+ import { sql } from 'drizzle-orm';
2
3
  import { logger } from '../../shared/logging.js';
3
4
  const log = logger('database');
4
- import { DEFAULT_DB_SCHEMA, dbSchemaOf } from './connection.js';
5
+ import { DEFAULT_DB_SCHEMA, dbSchemaOf, piiKeysOf } from './connection.js';
5
6
  import { AdvisoryLock, withAdvisoryLock } from './advisory-lock.js';
7
+ import { PII_SEALED_SHAPE_SQL_REGEX, isEncryptedBlob } from '../../shared/column-crypto.js';
6
8
  /*
7
9
  * ── Per-tier migration folders ──────────────────────────────────────────────
8
10
  *
@@ -62,14 +64,24 @@ export async function runCoreMigrations(db, folder) {
62
64
  log.info('Running core database migrations...');
63
65
  await migrate(db, { migrationsFolder: folder, migrationsTable: '__drizzle_migrations_core', migrationsSchema });
64
66
  log.info('Core migrations complete.');
67
+ // Under the same lock, before anything reads or writes a PII column:
68
+ // the data half of migration 0016, which SQL cannot do (see below).
69
+ await runPiiEncryptionBackfill(db);
65
70
  }, { tenantKey });
66
71
  }
67
72
  /**
68
73
  * Apply the ENTERPRISE migration history from `folder`, tracked in
69
74
  * `__drizzle_migrations_enterprise`. Run AFTER {@link runCoreMigrations} —
70
75
  * enterprise tables FK into core tables.
76
+ *
77
+ * `piiBackfill` is the overlay's own personal data, for the overlay that has
78
+ * sealed columns of its own (`encryptedText` / `blindIndexText` in its
79
+ * schema): the rows written before those columns were sealed are sealed here,
80
+ * right after the history that adds the index columns and under the same
81
+ * lock, exactly as {@link runCoreMigrations} does for core's tables. See
82
+ * {@link runPiiBackfill}.
71
83
  */
72
- export async function runEnterpriseMigrations(db, folder) {
84
+ export async function runEnterpriseMigrations(db, folder, opts = {}) {
73
85
  const { migrationsSchema, tenantKey } = ledgerOptions(db);
74
86
  await withAdvisoryLock(db, AdvisoryLock.Migrations, async () => {
75
87
  log.info('Running enterprise database migrations...');
@@ -79,6 +91,382 @@ export async function runEnterpriseMigrations(db, folder) {
79
91
  migrationsSchema,
80
92
  });
81
93
  log.info('Enterprise migrations complete.');
94
+ if (opts.piiBackfill)
95
+ await runPiiBackfill(db, opts.piiBackfill);
82
96
  }, { tenantKey });
83
97
  }
98
+ const PII_BACKFILL_TABLES = [
99
+ { table: 'users', key: ['id'], encrypted: ['email', 'name', 'avatar_url'], bidx: { source: 'email', column: 'email_bidx' } },
100
+ { table: 'pr_file_approvals', key: ['id'], encrypted: ['approver_email', 'approver_name'], bidx: { source: 'approver_email', column: 'approver_email_bidx' } },
101
+ { table: 'pr_merge_log', key: ['id'], encrypted: ['triggered_by_email', 'triggered_by_name', 'error'], bidx: { source: 'triggered_by_email', column: 'triggered_by_email_bidx' } },
102
+ { table: 'pr_comments', key: ['id'], encrypted: ['author_email', 'author_name', 'body'], bidx: { source: 'author_email', column: 'author_email_bidx' } },
103
+ { table: 'change_requests', key: ['id'], encrypted: ['author_email', 'author_name', 'title', 'body', 'apply_failure_reason', 'apply_failed_by_name'], bidx: { source: 'author_email', column: 'author_email_bidx' } },
104
+ { table: 'pending_commits', key: ['id'], encrypted: ['author_email', 'author_name', 'last_error'], bidx: { source: 'author_email', column: 'author_email_bidx' } },
105
+ { table: 'file_locks', key: ['workspace_id', 'branch', 'path'], encrypted: ['holder_name'] },
106
+ { table: 'plugin_join_requests', key: ['id'], encrypted: ['requester_email', 'requester_name', 'failure_reason'], bidx: { source: 'requester_email', column: 'requester_email_bidx' } },
107
+ ];
108
+ const ident = (name) => sql.raw(`"${name}"`);
109
+ /** A table's blind indexes, however many it declares. */
110
+ const indexesOf = (t) => (t.bidx === undefined ? [] : Array.isArray(t.bidx) ? t.bidx : [t.bidx]);
111
+ /** The name the refusals call the key by: the operator's own, with core's tenant note when it is core's. */
112
+ const keyNameOf = (spec) => spec.keyName ?? 'SECRETS_ENC_KEY (for a tenant: the one derived from TENANT_MASTER_KEY)';
113
+ /** A text column as it is stored: bytes, so the handle's connection does not open it. */
114
+ const asStored = (col) => sql `convert_to(${ident(col)}, 'UTF8') AS ${ident(col)}`;
115
+ /** What {@link asStored} selected, back as text; anything else as it came. */
116
+ const storedText = (value) => (Buffer.isBuffer(value) ? value.toString('utf8') : value);
117
+ /**
118
+ * A column "needs sealing" when it holds non-empty text that isn't a blob.
119
+ * The shape regex is deliberately used as a NEGATIVE filter: anything failing
120
+ * it — including plaintext that merely BEGINS with the prefix — is selected
121
+ * and sealed, and `isEncryptedBlob` is the same pattern, so the two can never
122
+ * disagree about a value. Sealed rows all match, keeping the steady-state scan
123
+ * an empty-result query.
124
+ *
125
+ * `trustShape` is false on the FIRST backfill of a database (see
126
+ * {@link isFirstBackfill}): nothing has been sealed yet, so a value in this
127
+ * shape is plaintext somebody typed, and every non-empty value is selected.
128
+ */
129
+ function needsSealing(col, trustShape) {
130
+ if (!trustShape)
131
+ return sql `(${ident(col)} IS NOT NULL AND ${ident(col)} <> '')`;
132
+ return sql `(${ident(col)} IS NOT NULL AND ${ident(col)} <> '' AND ${ident(col)} !~ ${PII_SEALED_SHAPE_SQL_REGEX})`;
133
+ }
134
+ /**
135
+ * Whether this database has never been through the backfill: the spec's
136
+ * marker column (for core, the blind index of `users`) is still nullable.
137
+ * The backfill, the duplicate collapse and the constraints run in ONE
138
+ * transaction, so a nullable column means no backfill has ever committed
139
+ * here — and therefore that nothing in it is sealed, whatever it looks like.
140
+ *
141
+ * That is what lets a first backfill trust no shape at all. A blob is
142
+ * recognised by its shape, and a title or a display name is text a person
143
+ * chose: one typed in that shape before the upgrade would be read as sealed,
144
+ * skipped, and left in clear for good — and, sampled by
145
+ * {@link assertKeyOpensSealedRows}, would refuse the start for a key that is
146
+ * perfectly right. Once the first backfill has committed, every write goes
147
+ * through a handle that seals it, so no such value can arrive again.
148
+ *
149
+ * A marker that is not there at all is the SQL history not having run (or a
150
+ * spec naming a column it does not add): refused, rather than read as "not
151
+ * the first run", which would trust every shape in a database nobody sealed.
152
+ */
153
+ async function isFirstBackfill(tx, spec) {
154
+ const { table, column } = spec.marker;
155
+ const result = await tx.execute(sql `
156
+ SELECT is_nullable FROM information_schema.columns
157
+ WHERE table_schema = current_schema() AND table_name = ${table} AND column_name = ${column}
158
+ `);
159
+ const nullable = result.rows[0]?.is_nullable;
160
+ if (nullable === undefined) {
161
+ throw new Error(`PII encryption backfill (${spec.name}): the marker column ${table}.${column} does not exist. ` +
162
+ 'Apply the migration that adds the blind-index columns before the backfill runs.');
163
+ }
164
+ return nullable === 'YES';
165
+ }
166
+ /** How many rows one backfill query reads: a table is walked in batches, never loaded whole. */
167
+ const BACKFILL_BATCH_ROWS = 500;
168
+ /**
169
+ * The configured key must open what earlier starts sealed. A rotated or
170
+ * mistyped `SECRETS_ENC_KEY` would otherwise go unnoticed here — sealed rows
171
+ * are exactly the ones the scan skips — and surface only as every login
172
+ * failing and every lookup by email missing, because the blind indexes would
173
+ * be computed under the new key. One sealed value per table is enough: all
174
+ * rows of a deployment are sealed under one key, so any one of them answers
175
+ * for the rest. Refusing the start is the loud failure; re-keying a database
176
+ * is a deliberate operation, not a boot.
177
+ *
178
+ * The sample is a sealed value from WHICHEVER column has one, not from a
179
+ * column chosen to stand for its table. A column may hold nothing in any
180
+ * row (an optional address nobody gave, an error text that never occurred),
181
+ * and a check that looked only there would find no sample, say nothing, and
182
+ * let a wrong key through to a table whose other columns are full of values
183
+ * it cannot open.
184
+ */
185
+ async function assertKeyOpensSealedRows(tx, keys, spec) {
186
+ for (const t of spec.tables) {
187
+ // ONE pass over the table, which ends at the first row that has a sealed
188
+ // value in ANY of its sealed columns: on a sealed database, the first
189
+ // row. Whatever that row holds sealed is tried. (A value in it that is
190
+ // not sealed opens as itself, so it is no evidence either way.)
191
+ const sealedSomewhere = sql.join(t.encrypted.map((col) => sql `${ident(col)} ~ ${PII_SEALED_SHAPE_SQL_REGEX}`), sql ` OR `);
192
+ const sample = await tx.execute(sql `SELECT ${sql.join(t.encrypted.map(asStored), sql `, `)} FROM ${ident(t.table)} WHERE ${sealedSomewhere} LIMIT 1`);
193
+ const row = (sample.rows[0] ?? {});
194
+ for (const col of t.encrypted) {
195
+ const value = storedText(row[col]);
196
+ if (typeof value === 'string' && !keys.open(value).ok) {
197
+ throw new Error(`PII encryption: ${t.table}.${col} is sealed with a key the configured ${keyNameOf(spec)} ` +
198
+ 'does not open — refusing to start. ' +
199
+ 'Restore the key that sealed it; changing the key is a re-keying of the database, not a configuration change.');
200
+ }
201
+ }
202
+ }
203
+ }
204
+ async function backfillTable(tx, keys, t, trustShape, keyName) {
205
+ const indexes = indexesOf(t);
206
+ // The key columns are read as text beside themselves, so the next batch can
207
+ // be asked for by value whatever their type is (a uuid, a serial, a path).
208
+ const keyText = (k) => `${k}__key`;
209
+ const cols = [
210
+ ...t.key.map(ident),
211
+ ...t.key.map((k) => sql `${ident(k)}::text AS ${ident(keyText(k))}`),
212
+ ...t.encrypted.map(asStored),
213
+ ...indexes.map((index) => ident(index.column)),
214
+ ];
215
+ // Only rows with work left: the ciphertext prefix makes "unsealed" a plain
216
+ // SQL predicate, so a fully-sealed table costs one empty-result query.
217
+ // AN INDEX IS THERE EXACTLY WHERE ITS SOURCE IS. A row is owed work when
218
+ // the two disagree, either way: a source with no index beside it, and an
219
+ // index left beside a source that has since been emptied to NULL (which
220
+ // would go on answering for an address the row no longer holds). A source
221
+ // that is NULL with no index is how an optional address looks, and is not
222
+ // asked for: it would come back on every start for nothing.
223
+ const pending = [
224
+ ...t.encrypted.map((col) => needsSealing(col, trustShape)),
225
+ ...indexes.map((index) => sql `((${ident(index.column)} IS NULL) <> (${ident(index.source)} IS NULL))`),
226
+ ];
227
+ const keyTuple = sql `(${sql.join(t.key.map((k) => sql `${ident(k)}::text`), sql `, `)})`;
228
+ // A blob, as far as this pass may believe one. On a first backfill nothing
229
+ // is sealed yet, so nothing is believed (see `isFirstBackfill`).
230
+ const sealedAlready = (value) => trustShape && isEncryptedBlob(value);
231
+ let rewritten = 0;
232
+ /** The key of the last row read, as text: the next batch starts after it. */
233
+ let after = null;
234
+ // IN BATCHES, in key order. A table is never loaded whole: a deployment with
235
+ // years of change requests holds every title and body it ever had, and
236
+ // reading them into one result held the first start after the upgrade — and
237
+ // the memory of the process making it — for as long as that took. Walking by
238
+ // key rather than re-asking for "what is left" also ends: a row this pass
239
+ // leaves as it found it (a write overtook it) is passed, not met again.
240
+ for (;;) {
241
+ const past = after ? sql ` AND ${keyTuple} > (${sql.join(after.map((v) => sql `${v}`), sql `, `)})` : sql ``;
242
+ const result = await tx.execute(sql `SELECT ${sql.join(cols, sql `, `)} FROM ${ident(t.table)} WHERE (${sql.join(pending, sql ` OR `)})${past} ORDER BY ${keyTuple} LIMIT ${BACKFILL_BATCH_ROWS}`);
243
+ const batch = result.rows;
244
+ for (const read of batch) {
245
+ const row = Object.fromEntries(Object.entries(read).map(([col, value]) => [col, storedText(value)]));
246
+ const sets = [];
247
+ // Compare-and-swap: pin every value this row was read with, so a write
248
+ // that lands between scan and update makes this UPDATE match zero rows
249
+ // instead of clobbering the newer value.
250
+ const where = t.key.map((k) => sql `${ident(k)} = ${row[k]}`);
251
+ for (const col of t.encrypted) {
252
+ const value = row[col];
253
+ if (typeof value === 'string' && value !== '' && !sealedAlready(value)) {
254
+ sets.push(sql `${ident(col)} = ${keys.seal(value)}`);
255
+ where.push(sql `${ident(col)} = ${value}`);
256
+ }
257
+ }
258
+ for (const index of indexes) {
259
+ // The blind index is derived from its source, so it is (re)computed
260
+ // whenever the source is being sealed in this pass — a legacy writer
261
+ // that put a NEW plaintext email on an already-indexed row left a stale
262
+ // index behind, and sealing the email alone would freeze that mismatch
263
+ // — and whenever it was never filled. The source may already be
264
+ // ciphertext (an index that was never filled beside a sealed address);
265
+ // the index is always computed over the plaintext, never over a blob
266
+ // the key cannot open.
267
+ const source = row[index.source];
268
+ const stored = row[index.column];
269
+ // NULL is "no value", and has no index: not the index of the empty
270
+ // string, which every row without an address would then share where
271
+ // a plain unique index let any number of NULLs stand. An index found
272
+ // beside a NULL source is taken away, pinned to what was read like
273
+ // every other write here.
274
+ //
275
+ // The EMPTY STRING is a value, and keeps its index: it is what the
276
+ // handle writes for one (`blindIndexText`), so a row is found by it
277
+ // whether the application wrote it or this did, and two empty
278
+ // strings are equal under a unique index, as they were in clear.
279
+ if (typeof source !== 'string') {
280
+ if (stored != null) {
281
+ sets.push(sql `${ident(index.column)} = NULL`);
282
+ where.push(sql `${ident(index.column)} = ${stored}`);
283
+ where.push(sql `${ident(index.source)} IS NULL`);
284
+ }
285
+ continue;
286
+ }
287
+ const text = source;
288
+ const sealingSource = text !== '' && !sealedAlready(text);
289
+ if (sealingSource || stored == null) {
290
+ const opened = sealedAlready(text) ? keys.open(text) : { ok: true, plain: text };
291
+ if (!opened.ok) {
292
+ throw new Error(`PII encryption backfill: ${t.table}.${index.source} cannot be decrypted with the ` +
293
+ `configured ${keyName} — refusing to derive a blind index from ciphertext. ` +
294
+ 'Restore the key that sealed it, then restart.');
295
+ }
296
+ sets.push(sql `${ident(index.column)} = ${keys.index(opened.plain)}`);
297
+ // Pin the index AND its source: if a concurrent writer replaces the
298
+ // email between scan and write, the CAS must not attach the OLD
299
+ // email's blind index to the NEW value.
300
+ where.push(sql `${ident(index.column)} IS NOT DISTINCT FROM ${stored ?? null}`);
301
+ where.push(sql `${ident(index.source)} IS NOT DISTINCT FROM ${source ?? null}`);
302
+ }
303
+ }
304
+ if (sets.length === 0)
305
+ continue;
306
+ const updated = await tx.execute(sql `UPDATE ${ident(t.table)} SET ${sql.join(sets, sql `, `)} WHERE ${sql.join(where, sql ` AND `)}`);
307
+ rewritten += updated.rowCount ?? 0;
308
+ }
309
+ if (batch.length < BACKFILL_BATCH_ROWS)
310
+ return rewritten;
311
+ const last = batch[batch.length - 1];
312
+ after = t.key.map((k) => String(last[keyText(k)]));
313
+ }
314
+ }
315
+ /**
316
+ * The name on a refused apply that was recorded BEFORE this release: the
317
+ * address of whoever it names was never kept, so no blind index stands beside
318
+ * it and an account erasure has nothing to find it by.
319
+ *
320
+ * Matching it by display name instead got both mistakes at once: a person
321
+ * who had since changed their name kept the old one on the request for good,
322
+ * and somebody else of the same name had theirs taken away. So the name goes,
323
+ * once, here. The refusal itself stays, with its reason and its time; it
324
+ * reads "could not apply" without saying who. A refusal recorded from this
325
+ * release on carries its index and is never touched by this.
326
+ */
327
+ async function clearUnindexedRefusalNames(tx) {
328
+ const cleared = await tx.execute(sql.raw(`
329
+ UPDATE "change_requests" SET "apply_failed_by_name" = NULL
330
+ WHERE "apply_failed_by_name" IS NOT NULL AND "apply_failed_by_email_bidx" IS NULL
331
+ `));
332
+ return cleared.rowCount ?? 0;
333
+ }
334
+ /**
335
+ * Legacy uniqueness was on the RAW email, so rows differing only by case or
336
+ * whitespace could coexist; under the normalized blind index they collide and
337
+ * the unique-index creation below would abort the start. Approval rows and
338
+ * join-request rows are the same logical row — collapse them, keeping the
339
+ * earliest. Colliding USER rows are distinct accounts; refuse loudly with the
340
+ * ids so an operator resolves the conflict deliberately instead of the
341
+ * upgrade guessing.
342
+ */
343
+ async function resolveBidxCollisions(tx) {
344
+ await tx.execute(sql.raw(`
345
+ DELETE FROM "pr_file_approvals" a USING "pr_file_approvals" b
346
+ WHERE a."pr_number" = b."pr_number" AND a."path" = b."path"
347
+ AND a."approver_email_bidx" = b."approver_email_bidx" AND a."head_sha" = b."head_sha"
348
+ AND (a."approved_at" > b."approved_at" OR (a."approved_at" = b."approved_at" AND a."id" > b."id"))
349
+ `));
350
+ await tx.execute(sql.raw(`
351
+ DELETE FROM "plugin_join_requests" a USING "plugin_join_requests" b
352
+ WHERE a."plugin_key" = b."plugin_key" AND a."requester_email_bidx" = b."requester_email_bidx"
353
+ AND (a."created_at" > b."created_at" OR (a."created_at" = b."created_at" AND a."id" > b."id"))
354
+ `));
355
+ const dupes = await tx.execute(sql.raw(`
356
+ SELECT array_agg("id") AS ids FROM "users" GROUP BY "email_bidx" HAVING count(*) > 1
357
+ `));
358
+ if (dupes.rows.length > 0) {
359
+ const groups = dupes.rows.map((r) => r.ids.join(', '));
360
+ throw new Error('PII encryption backfill: multiple user rows share the same email after normalization ' +
361
+ '(case/whitespace variants of one address). Merge or delete the duplicates, then restart. ' +
362
+ `Conflicting user ids: [${groups.join('], [')}]`);
363
+ }
364
+ }
365
+ const PII_FINALIZE_STATEMENTS = [
366
+ 'ALTER TABLE "users" ALTER COLUMN "email_bidx" SET NOT NULL',
367
+ 'CREATE UNIQUE INDEX IF NOT EXISTS "users_email_bidx_unq" ON "users" ("email_bidx")',
368
+ 'ALTER TABLE "users" DROP CONSTRAINT IF EXISTS "users_email_unique"',
369
+ 'ALTER TABLE "pr_file_approvals" ALTER COLUMN "approver_email_bidx" SET NOT NULL',
370
+ 'CREATE UNIQUE INDEX IF NOT EXISTS "pr_file_approvals_bidx_unq" ON "pr_file_approvals" ("pr_number","path","approver_email_bidx","head_sha")',
371
+ 'DROP INDEX IF EXISTS "pr_file_approvals_unq"',
372
+ 'ALTER TABLE "pr_merge_log" ALTER COLUMN "triggered_by_email_bidx" SET NOT NULL',
373
+ 'ALTER TABLE "pr_comments" ALTER COLUMN "author_email_bidx" SET NOT NULL',
374
+ 'ALTER TABLE "change_requests" ALTER COLUMN "author_email_bidx" SET NOT NULL',
375
+ 'ALTER TABLE "pending_commits" ALTER COLUMN "author_email_bidx" SET NOT NULL',
376
+ 'ALTER TABLE "plugin_join_requests" ALTER COLUMN "requester_email_bidx" SET NOT NULL',
377
+ 'CREATE UNIQUE INDEX IF NOT EXISTS "plugin_join_requests_requester_bidx_plugin_unq" ON "plugin_join_requests" ("requester_email_bidx","plugin_key")',
378
+ 'DROP INDEX IF EXISTS "plugin_join_requests_requester_plugin_unq"',
379
+ ];
380
+ /** Core's own personal data: the tables above, and what migration 0016 deferred. */
381
+ const CORE_PII_BACKFILL = {
382
+ name: 'core',
383
+ tables: PII_BACKFILL_TABLES,
384
+ marker: { table: 'users', column: 'email_bidx' },
385
+ afterSealing: async (tx, { first }) => {
386
+ const cleared = await clearUnindexedRefusalNames(tx);
387
+ if (cleared > 0) {
388
+ log.info(`PII encryption backfill: took the name off ${cleared} refused apply(ies) recorded before this release.`);
389
+ }
390
+ // Only where the unique indexes are not up yet. Once they are, they are
391
+ // what keeps two rows from sharing an index, so there is nothing to
392
+ // collapse — and the collapse is two self-joins and an aggregate over
393
+ // whole tables, which every later start paid for nothing.
394
+ if (first)
395
+ await resolveBidxCollisions(tx);
396
+ },
397
+ finalize: PII_FINALIZE_STATEMENTS,
398
+ };
399
+ /**
400
+ * Encrypt pre-existing plaintext PII rows, fill the blind-index columns, and
401
+ * apply the constraints migration 0016 deferred. Idempotent; `runCoreMigrations`
402
+ * runs it under the migrations lock right after the history. The handle must
403
+ * hold the knowledge base's key (`createDb(url, { piiKey })`; the composition
404
+ * root's does): one that holds none is refused before anything is read.
405
+ */
406
+ export async function runPiiEncryptionBackfill(db) {
407
+ await runPiiBackfill(db, CORE_PII_BACKFILL);
408
+ }
409
+ /**
410
+ * Seal the rows `spec` names that were written before their columns were
411
+ * sealed, fill their blind indexes, and apply `spec.finalize` — the DATA half
412
+ * of a migration that adds `encryptedText` / `blindIndexText` columns, which
413
+ * SQL cannot do because it holds no key. ONE implementation for every schema
414
+ * on a handle: core's own tables ({@link runPiiEncryptionBackfill}) and an
415
+ * overlay's, so the rules above (one transaction, a first run that trusts no
416
+ * shape, a key that must open what is sealed, columns read as stored, writes
417
+ * that pin what they read) are not rewritten per schema and cannot drift.
418
+ *
419
+ * Idempotent, and to be run on every start under the lock that guards the
420
+ * schema's history: {@link runEnterpriseMigrations} does that for an overlay
421
+ * that passes its spec. The handle must hold the key the rows are sealed
422
+ * with; one that holds none is refused before anything is read.
423
+ */
424
+ export async function runPiiBackfill(db, spec) {
425
+ const keys = piiKeysOf(db);
426
+ assertSpecIsWhole(spec);
427
+ const label = spec.name === CORE_PII_BACKFILL.name ? 'PII encryption backfill' : `PII encryption backfill (${spec.name})`;
428
+ await db.transaction(async (tx) => {
429
+ const first = await isFirstBackfill(tx, spec);
430
+ // Nothing is sealed before the first backfill, so there is no sealed row
431
+ // for the key to be checked against — and a value that only LOOKS sealed
432
+ // must not be taken for one (see `isFirstBackfill`).
433
+ if (!first)
434
+ await assertKeyOpensSealedRows(tx, keys, spec);
435
+ let rewritten = 0;
436
+ for (const t of spec.tables) {
437
+ rewritten += await backfillTable(tx, keys, t, !first, keyNameOf(spec));
438
+ }
439
+ if (rewritten > 0)
440
+ log.info(`${label}: rewrote ${rewritten} row(s).`);
441
+ await spec.afterSealing?.(tx, { first });
442
+ for (const statement of spec.finalize) {
443
+ await tx.execute(sql.raw(statement));
444
+ }
445
+ // The marker is what the NEXT start reads. Left nullable, that start
446
+ // would be a "first" one again: it would believe no shape, take every
447
+ // sealed value for plaintext and seal it a second time, and nothing
448
+ // would open afterwards. So a spec that does not close its own marker
449
+ // does not commit.
450
+ if (await isFirstBackfill(tx, spec)) {
451
+ throw new Error(`${label}: finalize left the marker column ${spec.marker.table}.${spec.marker.column} nullable. ` +
452
+ 'It must set it NOT NULL, or the next start would seal the sealed rows again. Nothing was changed.');
453
+ }
454
+ });
455
+ }
456
+ /** A spec is refused for what would otherwise fail halfway: a marker that is no index of its tables, an index of a column that is not sealed. */
457
+ function assertSpecIsWhole(spec) {
458
+ let markerIsAnIndex = false;
459
+ for (const t of spec.tables) {
460
+ for (const index of indexesOf(t)) {
461
+ if (!t.encrypted.includes(index.source)) {
462
+ throw new Error(`PII encryption backfill (${spec.name}): ${t.table}.${index.column} indexes "${index.source}", which is not one of the table's encrypted columns.`);
463
+ }
464
+ if (t.table === spec.marker.table && index.column === spec.marker.column)
465
+ markerIsAnIndex = true;
466
+ }
467
+ }
468
+ if (!markerIsAnIndex) {
469
+ throw new Error(`PII encryption backfill (${spec.name}): the marker ${spec.marker.table}.${spec.marker.column} is not a blind-index column of the spec's tables.`);
470
+ }
471
+ }
84
472
  //# sourceMappingURL=migrate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"migrate.js","sourceRoot":"","sources":["../../../src/modules/database/migrate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oCAAoC,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEjD,MAAM,GAAG,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAiB,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH;;;;;;;;;;;;;GAaG;AACH,SAAS,aAAa,CAAC,EAAY;IACjC,MAAM,MAAM,GAAG,UAAU,CAAC,EAAE,CAAC,CAAC;IAC9B,OAAO,MAAM,KAAK,iBAAiB;QACjC,CAAC,CAAC,EAAE,SAAS,EAAE,EAAE,EAAE;QACnB,CAAC,CAAC,EAAE,gBAAgB,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC;AACtD,CAAC;AAED,8FAA8F;AAC9F,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,EAAY,EAAE,MAAc;IAClE,MAAM,EAAE,gBAAgB,EAAE,SAAS,EAAE,GAAG,aAAa,CAAC,EAAE,CAAC,CAAC;IAC1D,MAAM,gBAAgB,CACpB,EAAE,EACF,YAAY,CAAC,UAAU,EACvB,KAAK,IAAI,EAAE;QACT,GAAG,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC;QAChD,MAAM,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AAQ,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;gBACxC,MAAM,IAAI,KAAK,CACb,4BAA4B,IAAI,CAAC,IAAI,MAAM,CAAC,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,aAAa,KAAK,CAAC,MAAM,uDAAuD,CACnJ,CAAC;YACJ,CAAC;YACD,IAAI,CAAC,CAAC,KAAK,KAAK,IAAI,CAAC,MAAM,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,CAAC,MAAM;gBAAE,eAAe,GAAG,IAAI,CAAC;QACnG,CAAC;IACH,CAAC;IACD,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CACb,4BAA4B,IAAI,CAAC,IAAI,iBAAiB,IAAI,CAAC,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,oDAAoD,CAClJ,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -114,6 +114,23 @@ export declare class LockingFilesystem extends GitGuardedFilesystem {
114
114
  * the write and releases the lock untouched; the caller gets what it threw.
115
115
  */
116
116
  writeFile(inputPath: string, content: FileContent, options?: WriteOptions, check?: () => void | Promise<void>): Promise<void>;
117
+ /**
118
+ * Write a file whose new content DEPENDS on what it holds: the read, the
119
+ * computing of the new bytes and the write all happen with the path's lock
120
+ * held, so what lands is derived from the very bytes it replaces.
121
+ *
122
+ * `writeFile` cannot give that. Its content is fixed before the lock is
123
+ * taken, so a caller that read the file first is writing over a state
124
+ * anyone may have moved in between: an `edit_file` replacing an empty owner
125
+ * field with one name would silently overwrite another name written a
126
+ * moment earlier, and both callers would be told their edit landed.
127
+ *
128
+ * `rewrite` receives the file as it is under the lock (null when nothing is
129
+ * there) and answers the bytes to write. Throwing refuses: nothing is
130
+ * written, the lock is released untouched, and the caller gets what was
131
+ * thrown.
132
+ */
133
+ rewriteFile(inputPath: string, rewrite: (current: Buffer | null) => FileContent | Promise<FileContent>): Promise<void>;
117
134
  appendFile(inputPath: string, content: FileContent): Promise<void>;
118
135
  deleteFile(inputPath: string, options?: RemoveOptions): Promise<void>;
119
136
  copyFile(src: string, dest: string, options?: CopyOptions): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"locking-filesystem.d.ts","sourceRoot":"","sources":["../../../src/modules/kb-fs/locking-filesystem.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAOH,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,YAAY,EAClB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAE1F,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAIpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,KAAK,EAAqB,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAgBpF;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG;IAC5F,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;CACvC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,QAAQ,CAAC;IACf;;;;;;;;OAQG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB;;;;;;;;;;;OAWG;IACH,aAAa,CAAC,EAAE,cAAc,CAAC;IAC/B;;;;OAIG;IACH,WAAW,CAAC,EAAE,kBAA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AAU;IAcxB;;;;;;;;;;;;;OAaG;YACW,QAAQ;IAYtB;;;;OAIG;YACW,WAAW;CAwE1B"}
@@ -98,6 +98,35 @@ export class LockingFilesystem extends GitGuardedFilesystem {
98
98
  await validate?.(inputPath, toWrite);
99
99
  }));
100
100
  }
101
+ /**
102
+ * Write a file whose new content DEPENDS on what it holds: the read, the
103
+ * computing of the new bytes and the write all happen with the path's lock
104
+ * held, so what lands is derived from the very bytes it replaces.
105
+ *
106
+ * `writeFile` cannot give that. Its content is fixed before the lock is
107
+ * taken, so a caller that read the file first is writing over a state
108
+ * anyone may have moved in between: an `edit_file` replacing an empty owner
109
+ * field with one name would silently overwrite another name written a
110
+ * moment earlier, and both callers would be told their edit landed.
111
+ *
112
+ * `rewrite` receives the file as it is under the lock (null when nothing is
113
+ * there) and answers the bytes to write. Throwing refuses: nothing is
114
+ * written, the lock is released untouched, and the caller gets what was
115
+ * thrown.
116
+ */
117
+ async rewriteFile(inputPath, rewrite) {
118
+ await this.assertNotGitInternals(inputPath);
119
+ this.assertInsideRepo(inputPath);
120
+ const validate = this.lockContext.validateWrite;
121
+ let toWrite = null;
122
+ return this.withLock(inputPath, () => super.writeFile(inputPath, toWrite), async () => {
123
+ const next = await rewrite(await this.readIfExists(inputPath));
124
+ // The pre-disk gate judges the bytes that will land, as it does for
125
+ // every other write, and here those exist only now.
126
+ await validate?.(inputPath, next);
127
+ toWrite = next;
128
+ });
129
+ }
101
130
  async appendFile(inputPath, content) {
102
131
  await this.assertNotGitInternals(inputPath);
103
132
  this.assertInsideRepo(inputPath);