@myrialabs/clopen 0.4.38 → 0.4.40

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/backend/auth/permissions.ts +48 -0
  2. package/backend/chat/stream-manager.ts +1 -1
  3. package/backend/database/crypto/secret-columns.ts +9 -1
  4. package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
  5. package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
  6. package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
  7. package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -0
  8. package/backend/database/migrations/index.ts +28 -0
  9. package/backend/database/queries/db-client-connection-queries.ts +19 -0
  10. package/backend/database/queries/deploy-queries.ts +124 -0
  11. package/backend/database/queries/graph-queries.ts +291 -247
  12. package/backend/database/queries/index.ts +10 -0
  13. package/backend/database/queries/integration-db-link-queries.ts +168 -0
  14. package/backend/database/queries/integration-queries.ts +34 -6
  15. package/backend/database/queries/worktree-branch-queries.ts +331 -0
  16. package/backend/db-client/cloning.test.ts +161 -0
  17. package/backend/db-client/cloning.ts +672 -0
  18. package/backend/db-client/connection-url.test.ts +117 -0
  19. package/backend/db-client/connection-url.ts +126 -0
  20. package/backend/db-client/drivers/mongodb.ts +2 -14
  21. package/backend/db-client/drivers/mysql.ts +2 -8
  22. package/backend/db-client/drivers/postgres.ts +10 -13
  23. package/backend/db-client/drivers/redis.ts +2 -6
  24. package/backend/db-client/env/detect.ts +438 -0
  25. package/backend/db-client/env/diff.ts +94 -0
  26. package/backend/db-client/env/env.test.ts +574 -0
  27. package/backend/db-client/env/index.ts +15 -0
  28. package/backend/db-client/env/naming.ts +202 -0
  29. package/backend/db-client/env/service.ts +487 -0
  30. package/backend/db-client/integrations/index.ts +44 -0
  31. package/backend/db-client/integrations/links.ts +564 -0
  32. package/backend/db-client/integrations/projector.test.ts +280 -0
  33. package/backend/db-client/integrations/projector.ts +256 -0
  34. package/backend/db-client/integrations/supabase-service.ts +266 -0
  35. package/backend/db-client/providers/neon/adapter.ts +522 -0
  36. package/backend/db-client/providers/neon/api-types.ts +182 -0
  37. package/backend/db-client/providers/neon/client.ts +298 -0
  38. package/backend/db-client/providers/neon/endpoints.test.ts +201 -0
  39. package/backend/db-client/providers/neon/endpoints.ts +314 -0
  40. package/backend/db-client/providers/neon/list.test.ts +181 -0
  41. package/backend/db-client/providers/registry.ts +27 -0
  42. package/backend/db-client/providers/supabase/adapter.ts +523 -0
  43. package/backend/db-client/providers/supabase/api-types.ts +92 -0
  44. package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
  45. package/backend/db-client/providers/supabase/client.ts +259 -0
  46. package/backend/db-client/providers/supabase/features.ts +502 -0
  47. package/backend/db-client/providers/supabase/local.test.ts +78 -0
  48. package/backend/db-client/providers/supabase/local.ts +137 -0
  49. package/backend/db-client/providers/types.ts +194 -0
  50. package/backend/deployments/bindings.test.ts +43 -0
  51. package/backend/deployments/bindings.ts +216 -0
  52. package/backend/deployments/index.ts +53 -0
  53. package/backend/deployments/log-streams.ts +214 -0
  54. package/backend/deployments/prompts.ts +70 -0
  55. package/backend/deployments/providers/vercel/adapter.ts +757 -0
  56. package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
  57. package/backend/deployments/providers/vercel/build-logs.ts +211 -0
  58. package/backend/deployments/providers/vercel/client.ts +203 -0
  59. package/backend/deployments/providers/vercel/list.test.ts +624 -0
  60. package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
  61. package/backend/deployments/providers/vercel/normalise.ts +113 -0
  62. package/backend/deployments/providers/vercel/types.ts +104 -0
  63. package/backend/deployments/registry.ts +27 -0
  64. package/backend/deployments/service.ts +458 -0
  65. package/backend/deployments/types.ts +278 -0
  66. package/backend/env-files/discovery.ts +48 -0
  67. package/backend/env-files/edit.ts +213 -0
  68. package/backend/env-files/env-files.test.ts +515 -0
  69. package/backend/env-files/git.ts +27 -0
  70. package/backend/env-files/index.ts +44 -0
  71. package/backend/env-files/keys.ts +69 -0
  72. package/backend/env-files/parse.ts +157 -0
  73. package/backend/env-files/write.ts +229 -0
  74. package/backend/index.ts +4 -0
  75. package/backend/integrations/health.ts +18 -4
  76. package/backend/integrations/projections/index.ts +0 -0
  77. package/backend/integrations/projections/mcp-projector.ts +7 -3
  78. package/backend/integrations/projections/types.ts +28 -9
  79. package/backend/integrations/registry/index.ts +8 -1
  80. package/backend/integrations/registry/providers/context7.ts +1 -0
  81. package/backend/integrations/registry/providers/firecrawl.ts +1 -0
  82. package/backend/integrations/registry/providers/github.ts +1 -0
  83. package/backend/integrations/registry/providers/neon.ts +55 -0
  84. package/backend/integrations/registry/providers/supabase.ts +62 -0
  85. package/backend/integrations/registry/providers/vercel.ts +47 -0
  86. package/backend/integrations/registry/types.ts +2 -0
  87. package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
  88. package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
  89. package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
  90. package/backend/memory/README.md +57 -34
  91. package/backend/memory/compose.test.ts +3 -3
  92. package/backend/memory/compose.ts +6 -9
  93. package/backend/memory/config.ts +0 -5
  94. package/backend/memory/consolidate.ts +1 -2
  95. package/backend/memory/context.test.ts +8 -54
  96. package/backend/memory/context.ts +19 -25
  97. package/backend/memory/extract/episodic.ts +10 -12
  98. package/backend/memory/extract/index.ts +23 -52
  99. package/backend/memory/extract/scheduler.ts +3 -6
  100. package/backend/memory/index.ts +6 -5
  101. package/backend/memory/invalidate.ts +36 -48
  102. package/backend/memory/layout.ts +319 -130
  103. package/backend/memory/retention.ts +12 -42
  104. package/backend/memory/retrieval.eval.test.ts +2 -2
  105. package/backend/memory/retrieval.test.ts +228 -183
  106. package/backend/memory/retrieval.ts +22 -21
  107. package/backend/memory/revise.test.ts +62 -116
  108. package/backend/memory/revise.ts +0 -1
  109. package/backend/memory/view.ts +8 -5
  110. package/backend/snapshot/helpers.ts +35 -41
  111. package/backend/snapshot/snapshot-service.ts +90 -0
  112. package/backend/snapshot/turn-changes.test.ts +163 -0
  113. package/backend/snapshot/turn-changes.ts +131 -0
  114. package/backend/work/start-work.ts +4 -0
  115. package/backend/worktrees/branching/engine.ts +557 -0
  116. package/backend/worktrees/branching/index.ts +56 -0
  117. package/backend/worktrees/branching/naming.test.ts +118 -0
  118. package/backend/worktrees/branching/naming.ts +97 -0
  119. package/backend/worktrees/branching/projector.test.ts +229 -0
  120. package/backend/worktrees/branching/projector.ts +139 -0
  121. package/backend/worktrees/branching/providers/neon.ts +220 -0
  122. package/backend/worktrees/branching/registry.ts +27 -0
  123. package/backend/worktrees/branching/service.ts +620 -0
  124. package/backend/worktrees/branching/suggest.test.ts +154 -0
  125. package/backend/worktrees/branching/suggest.ts +202 -0
  126. package/backend/worktrees/branching/types.ts +88 -0
  127. package/backend/worktrees/index.ts +7 -0
  128. package/backend/worktrees/service.ts +48 -1
  129. package/backend/ws/db-client/accounts.ts +225 -0
  130. package/backend/ws/db-client/connections.ts +34 -3
  131. package/backend/ws/db-client/context.ts +37 -0
  132. package/backend/ws/db-client/env.ts +142 -0
  133. package/backend/ws/db-client/index.ts +22 -1
  134. package/backend/ws/db-client/supabase.ts +188 -0
  135. package/backend/ws/deployments/actions.ts +360 -0
  136. package/backend/ws/deployments/context.ts +43 -0
  137. package/backend/ws/deployments/crud.ts +152 -0
  138. package/backend/ws/deployments/index.ts +15 -0
  139. package/backend/ws/deployments/schemas.ts +183 -0
  140. package/backend/ws/index.ts +6 -2
  141. package/backend/ws/memory/crud.ts +4 -16
  142. package/backend/ws/projects/index.ts +6 -1
  143. package/backend/ws/projects/info.ts +70 -6
  144. package/backend/ws/projects/overview.test.ts +108 -0
  145. package/backend/ws/projects/overview.ts +300 -0
  146. package/backend/ws/snapshot/changes.ts +279 -0
  147. package/backend/ws/snapshot/index.ts +4 -3
  148. package/backend/ws/system/device-info.ts +307 -76
  149. package/backend/ws/worktrees/branching.ts +212 -0
  150. package/backend/ws/worktrees/crud.ts +33 -7
  151. package/backend/ws/worktrees/index.ts +14 -0
  152. package/dist/assets/{index-DSHIANSy.js → index-D4igjF6e.js} +1 -1
  153. package/dist/assets/{index-CyH7fSbR.js → index-DONNwiXm.js} +1 -1
  154. package/dist/assets/main-BrfXH1bL.js +1205 -0
  155. package/dist/assets/main-CT1PygsW.css +1 -0
  156. package/dist/index.html +2 -2
  157. package/package.json +1 -1
  158. package/shared/constants/tool-icons.ts +13 -0
  159. package/shared/types/db-client/connection.ts +6 -0
  160. package/shared/types/db-client/env.ts +192 -0
  161. package/shared/types/db-client/index.ts +3 -0
  162. package/shared/types/db-client/integrations.ts +204 -0
  163. package/shared/types/db-client/supabase.ts +131 -0
  164. package/shared/types/deployments.ts +382 -0
  165. package/shared/types/integrations.ts +9 -0
  166. package/shared/types/memory.ts +34 -51
  167. package/shared/types/ui/components.ts +1 -1
  168. package/shared/types/worktree-branching.ts +315 -0
  169. package/shared/utils/diff-calculator.test.ts +89 -0
  170. package/shared/utils/diff-calculator.ts +30 -65
  171. package/shared/utils/logger.ts +2 -0
  172. package/backend/memory/extract/languages.ts +0 -176
  173. package/backend/memory/extract/structural.test.ts +0 -215
  174. package/backend/memory/extract/structural.ts +0 -364
  175. package/backend/ws/snapshot/content.ts +0 -85
  176. package/dist/assets/main-D2IOjftb.css +0 -1
  177. package/dist/assets/main-DM-tHKbW.js +0 -1152
@@ -0,0 +1,315 @@
1
+ /**
2
+ * Worktree database branching — shared vocabulary.
3
+ *
4
+ * The worktree manager talks about a BRANCH DATABASE, not about Neon. Every
5
+ * provider that can offer the `worktree-branching` capability is normalised
6
+ * into these shapes by an adapter, and the manager never learns which one
7
+ * answered — the same test `Task 2` and `Task 3` set for their surfaces: if
8
+ * Turso needs a second dialog, these types are wrong and fixing them is the
9
+ * work.
10
+ *
11
+ * The word "branch" is deliberately NOT baked into the copy. Neon cuts a real
12
+ * copy-on-write branch; Turso seeds a whole new database from a parent. Both
13
+ * are "an isolated database derived from a parent", and `BranchProviderInfo`
14
+ * carries the noun each provider uses so the dialog can say what actually
15
+ * happens rather than calling a seeded database a branch.
16
+ *
17
+ * Nothing here carries a credential. The connection string a branch answers on
18
+ * is resolved on the server, sealed on the branch row, and written into the
19
+ * worktree's environment — it never crosses this line.
20
+ */
21
+
22
+ import type { DbDriver, DbSslMode } from './db-client';
23
+
24
+ /**
25
+ * Where a worktree's database comes from.
26
+ *
27
+ * `account` is a provider that branches natively — Neon cuts a copy-on-write
28
+ * branch through its API. `connection` is everything else: a database Clopen
29
+ * can already reach, copied with that engine's own primitives. The second is
30
+ * the general case and the first is the optimisation, which is the opposite of
31
+ * how this feature started.
32
+ */
33
+ export type BranchSourceKind = 'account' | 'connection';
34
+
35
+ /** How much of the parent a worktree's database starts with. */
36
+ export type BranchDataMode = 'schema-data' | 'schema' | 'empty';
37
+
38
+ /**
39
+ * A place worktree databases can come from, as the setup dialog lists them.
40
+ *
41
+ * Accounts and connections are one list rather than two tabs: the user is
42
+ * choosing WHICH DATABASE, and whether the copy happens through a vendor API or
43
+ * through `CREATE DATABASE` is an implementation detail they should be told
44
+ * about, not asked to navigate.
45
+ */
46
+ export interface BranchSourceOption {
47
+ kind: BranchSourceKind;
48
+ /** The account id, or the DB Client connection id. */
49
+ id: string;
50
+ label: string;
51
+ /** Integration provider id for an account, driver id for a connection. */
52
+ provider: string;
53
+ providerName: string;
54
+ /** What this source calls one — "branch" for Neon, "copy" for the rest. */
55
+ noun: string;
56
+ /** True when the provider branches natively rather than Clopen copying. */
57
+ isNative: boolean;
58
+ /** One sentence naming what a copy here does not carry, or null. */
59
+ notice: string | null;
60
+ /** Health, for the same strip the accounts list uses. */
61
+ status: string;
62
+ }
63
+
64
+ /** A parent database a worktree's branch is cut from. */
65
+ export interface BranchParent {
66
+ /** The provider's own id for the parent — a Neon project id. */
67
+ ref: string;
68
+ name: string;
69
+ /** Region, plan — whatever tells two similarly named parents apart. */
70
+ detail: string | null;
71
+ /** The owning container's name, so which organisation is billed is visible. */
72
+ group: string | null;
73
+ /** Whether a branch can be cut from it right now. The ADAPTER decides. */
74
+ isReady: boolean;
75
+ }
76
+
77
+ /** Where a created branch answers. The password is server-side only. */
78
+ export interface BranchConnection {
79
+ /** The full URI, which is what lands in the worktree's environment. */
80
+ uri: string;
81
+ host: string;
82
+ port: number;
83
+ username: string;
84
+ password: string;
85
+ database: string;
86
+ sslMode: DbSslMode;
87
+ }
88
+
89
+ /** What an adapter hands back after cutting one. */
90
+ export interface CreatedBranch {
91
+ /** The provider's own id for the branch. */
92
+ ref: string;
93
+ /** The name it was actually given, which may differ from the one requested. */
94
+ name: string;
95
+ connection: BranchConnection;
96
+ }
97
+
98
+ /** A branch as it exists at the provider, for the orphan sweep. */
99
+ export interface RemoteBranch {
100
+ ref: string;
101
+ name: string;
102
+ createdAt: string | null;
103
+ /** A provider's default branch is never ours and is never offered for delete. */
104
+ isDefault: boolean;
105
+ /** Logical size in bytes, when the provider reports one. */
106
+ sizeBytes: number | null;
107
+ }
108
+
109
+ /** A branching provider as the setup dialog renders it. */
110
+ export interface BranchProviderInfo {
111
+ /** Matches the integration provider id, so the brand mark is the same one. */
112
+ id: string;
113
+ name: string;
114
+ /**
115
+ * What this provider calls one, singular and lowercase — "branch" for Neon,
116
+ * "database" for Turso. Used in copy so a seeded Turso database is never
117
+ * described as a branch.
118
+ */
119
+ noun: string;
120
+ /** What the parent is called — "Neon project". */
121
+ parentLabel: string;
122
+ description: string;
123
+ /** One sentence naming what cutting one actually costs. */
124
+ notice: string;
125
+ /** True when the provider can hand back a pooled endpoint as well. */
126
+ supportsPooled: boolean;
127
+ }
128
+
129
+ /**
130
+ * Per-project settings for branching.
131
+ *
132
+ * `envFile` has NO default worth guessing, which is why it is asked for at
133
+ * setup. Next.js and Vite read `.env.local` in PREFERENCE to `.env`, so writing
134
+ * the wrong one produces the worst possible outcome: the branch is created, the
135
+ * variable is written, and nothing reads it.
136
+ */
137
+ export interface WorktreeBranchConfig {
138
+ /** The variable the connection URI is written to. */
139
+ envVar: string;
140
+ /** Which dotenv file inside the worktree receives the managed block. */
141
+ envFile: string;
142
+ /** Offer it, ticked, on every worktree create. */
143
+ autoCreate: boolean;
144
+ /** Ask the provider for a pooled endpoint instead of a direct one. */
145
+ pooled: boolean;
146
+ /**
147
+ * How much of the parent a new worktree's database starts with.
148
+ *
149
+ * Remembered per project and overridable per worktree, because the right
150
+ * answer depends on the database rather than on the feature: a seeded dev
151
+ * database is worth copying whole, and a forty-gigabyte one is not.
152
+ */
153
+ dataMode: BranchDataMode;
154
+ }
155
+
156
+ export const DEFAULT_WORKTREE_BRANCH_CONFIG: WorktreeBranchConfig = {
157
+ envVar: 'DATABASE_URL',
158
+ envFile: '.env',
159
+ autoCreate: true,
160
+ // Direct, not pooled, and the default is not a question to ask. A worktree is
161
+ // where an agent runs migrations: a transaction-mode pooler breaks prepared
162
+ // statements and some DDL, and the failure arrives as a confusing driver
163
+ // error rather than as a configuration one.
164
+ pooled: false,
165
+ // Copying the data is what makes a worktree feel like the project rather
166
+ // than like an empty shell of it — the app starts, the fixtures are there,
167
+ // and an agent can actually run what it just changed.
168
+ dataMode: 'schema-data'
169
+ };
170
+
171
+ /** The binding for one project, as the worktree manager sees it. */
172
+ export interface WorktreeBranchBindingInfo {
173
+ projectId: string;
174
+ sourceKind: BranchSourceKind;
175
+ /** The account id, or the DB Client connection id. */
176
+ sourceId: string;
177
+ provider: string;
178
+ providerName: string;
179
+ /** The account's label, or the connection's name. */
180
+ sourceLabel: string;
181
+ parentRef: string;
182
+ parentName: string;
183
+ isNative: boolean;
184
+ config: WorktreeBranchConfig;
185
+ updatedAt: string;
186
+ }
187
+
188
+ /**
189
+ * What happened when the connection string was written into the worktree.
190
+ *
191
+ * `skipped-tracked` is the one that matters. A dotenv file that git TRACKS
192
+ * would be carried back to the main project by "Apply to Main", overwriting the
193
+ * real connection string with one that points at a database this worktree's
194
+ * deletion destroys. So it is refused rather than written, and the refusal is
195
+ * reported instead of being logged and forgotten.
196
+ */
197
+ export type BranchEnvStatus = 'written' | 'skipped-tracked' | 'failed';
198
+
199
+ /** A live or orphaned branch, as the manager sees it. */
200
+ export interface WorktreeBranchInfo {
201
+ id: string;
202
+ /** Null once the worktree is gone — which is what makes a row an orphan. */
203
+ worktreeId: string | null;
204
+ worktreeName: string | null;
205
+ sourceKind: BranchSourceKind;
206
+ provider: string;
207
+ providerName: string;
208
+ /** What this source calls one, so copy reads right per source. */
209
+ noun: string;
210
+ /** The account id, or the DB Client connection id. */
211
+ sourceId: string;
212
+ sourceLabel: string;
213
+ /** Which mechanism made it — `template`, `row-copy`, `file-copy`, … */
214
+ strategy: string | null;
215
+ /** One sentence naming what the copy did not carry, or null. */
216
+ notice: string | null;
217
+ parentRef: string;
218
+ branchRef: string;
219
+ branchName: string;
220
+ envVar: string;
221
+ envFile: string;
222
+ envStatus: BranchEnvStatus;
223
+ envDetail: string | null;
224
+ status: 'active' | 'orphaned';
225
+ /** Why it is orphaned — the provider's own words for the failed delete. */
226
+ error: string | null;
227
+ /** The projected DB Client connection, so the branch can be inspected. */
228
+ connectionId: string | null;
229
+ createdAt: string;
230
+ }
231
+
232
+ /**
233
+ * A branch at the provider that Clopen has no row for.
234
+ *
235
+ * The second half of orphan detection. A crash between "the provider created
236
+ * it" and "the row was written" leaves a branch nothing here knows about, and
237
+ * the only thing tying it back is the name — which is why branch names are
238
+ * deterministic and prefixed.
239
+ */
240
+ export interface UnknownRemoteBranch {
241
+ sourceKind: BranchSourceKind;
242
+ /** The account id, or the DB Client connection id. */
243
+ sourceId: string;
244
+ provider: string;
245
+ parentRef: string;
246
+ branch: RemoteBranch;
247
+ }
248
+
249
+ /** Everything the worktree manager's branching dialog renders. */
250
+ export interface WorktreeBranchingState {
251
+ providers: BranchProviderInfo[];
252
+ /**
253
+ * Everywhere a worktree database could come from — accounts AND connections.
254
+ *
255
+ * One list, because "which database" is one question. See
256
+ * `BranchSourceOption`.
257
+ */
258
+ sources: BranchSourceOption[];
259
+ binding: WorktreeBranchBindingInfo | null;
260
+ /** Dotenv files that already exist in the project, most-precedent first. */
261
+ envFiles: string[];
262
+ branches: WorktreeBranchInfo[];
263
+ }
264
+
265
+ /** The result of attaching a branch to a freshly created worktree. */
266
+ export interface BranchAttachResult {
267
+ branch: WorktreeBranchInfo | null;
268
+ /** What the copy did not carry, when it carried less than everything. */
269
+ notice?: string | null;
270
+ /**
271
+ * Why no branch was attached, when one was expected.
272
+ *
273
+ * Fail-soft is the rule: a provider outage must never cost the user the
274
+ * worktree they asked for, so this is reported alongside a worktree that was
275
+ * created successfully rather than thrown.
276
+ */
277
+ error: string | null;
278
+ }
279
+
280
+ /** The branch-name prefix every source-side name carries. */
281
+ export const BRANCH_NAME_PREFIX = 'clopen';
282
+
283
+ /**
284
+ * The binding a project would get if it asked for one.
285
+ *
286
+ * Derived from the project's own `.env` and Clopen's connection list, so the
287
+ * dialog can offer "also give it its own database" without the user having
288
+ * configured anything first. Null when the URL in the project's environment
289
+ * corresponds to no saved connection — Clopen would have the address but not
290
+ * the password, and an offer that fails at the moment of use is worse than no
291
+ * offer.
292
+ */
293
+ export interface BranchSuggestion {
294
+ sourceKind: 'connection';
295
+ sourceId: string;
296
+ sourceLabel: string;
297
+ providerName: string;
298
+ /** The database to copy — a name, or a file path for SQLite. */
299
+ parentRef: string;
300
+ parentName: string;
301
+ /** The variable the copy's URL should be written to. */
302
+ envVar: string;
303
+ /** Which dotenv file that variable already lives in. */
304
+ envFile: string;
305
+ /** What a copy here does not carry, or null. */
306
+ notice: string | null;
307
+ }
308
+
309
+ /** A database on a connection that a worktree copy could be made from. */
310
+ export interface BranchConnectionParent {
311
+ /** The database name, its file path for SQLite, its index for Redis. */
312
+ ref: string;
313
+ name: string;
314
+ driver: DbDriver;
315
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Line counting for +/- badges.
3
+ *
4
+ * These numbers sit next to a file name in the checkpoint timeline, the
5
+ * Changes tab and every Edit tool row, where they are read against what git
6
+ * says about the same file. A file that ends with a newline — which is most
7
+ * source files — used to count one line more than it has, so the badges and
8
+ * git disagreed on every created and deleted file.
9
+ */
10
+
11
+ import { describe, test, expect } from 'bun:test';
12
+
13
+ import { countLineChanges, calculateFileChangeStats, isBinaryBuffer } from './diff-calculator';
14
+
15
+ describe('countLineChanges', () => {
16
+ test('counts a created file by its real lines, trailing newline and all', () => {
17
+ expect(countLineChanges('', 'one\ntwo\n')).toEqual({ additions: 2, deletions: 0 });
18
+ });
19
+
20
+ test('counts a file with no trailing newline the same way', () => {
21
+ expect(countLineChanges('', 'one\ntwo')).toEqual({ additions: 2, deletions: 0 });
22
+ });
23
+
24
+ test('counts a deleted file as deletions only', () => {
25
+ expect(countLineChanges('one\ntwo\nthree\n', '')).toEqual({ additions: 0, deletions: 3 });
26
+ });
27
+
28
+ test('counts only the lines that actually moved, not the context around them', () => {
29
+ expect(countLineChanges('a\nb\nc\n', 'a\nB\nc\n')).toEqual({ additions: 1, deletions: 1 });
30
+ });
31
+
32
+ test('reads CRLF as the same content as LF', () => {
33
+ expect(countLineChanges('a\r\nb\r\n', 'a\nb\n')).toEqual({ additions: 0, deletions: 0 });
34
+ });
35
+
36
+ test('answers nothing for two empty sides', () => {
37
+ expect(countLineChanges('', '')).toEqual({ additions: 0, deletions: 0 });
38
+ });
39
+ });
40
+
41
+ describe('isBinaryBuffer', () => {
42
+ test('calls a NUL byte binary and plain text not', () => {
43
+ expect(isBinaryBuffer(Buffer.from([0x89, 0x50, 0x00, 0x0a]))).toBe(true);
44
+ expect(isBinaryBuffer(Buffer.from('a\nb\n', 'utf8'))).toBe(false);
45
+ });
46
+ });
47
+
48
+ describe('calculateFileChangeStats', () => {
49
+ test('adds up across added, modified and deleted files', () => {
50
+ const before = {
51
+ 'keep.ts': Buffer.from('a\nb\n'),
52
+ 'gone.ts': Buffer.from('x\ny\n')
53
+ };
54
+ const after = {
55
+ 'keep.ts': Buffer.from('a\nB\n'),
56
+ 'new.ts': Buffer.from('n\n')
57
+ };
58
+
59
+ expect(calculateFileChangeStats(before, after)).toEqual({
60
+ filesChanged: 3,
61
+ insertions: 2,
62
+ deletions: 3
63
+ });
64
+ });
65
+
66
+ test('counts a binary file as changed but contributes none of its bytes as lines', () => {
67
+ // A PNG split on newline bytes yields a line count with no meaning: two
68
+ // screenshots once added +159 to a checkpoint whose source changes were +17.
69
+ const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00, 0x0a, 0x0a, 0x0a, 0x0a]);
70
+ const before = { 'src/a.ts': Buffer.from('a\n') };
71
+ const after = { 'src/a.ts': Buffer.from('b\n'), 'shot.png': png };
72
+
73
+ expect(calculateFileChangeStats(before, after)).toEqual({
74
+ filesChanged: 2,
75
+ insertions: 1,
76
+ deletions: 1
77
+ });
78
+ });
79
+
80
+ test('counts a deleted binary the same way', () => {
81
+ const png = Buffer.from([0x89, 0x50, 0x00, 0x0a, 0x0a]);
82
+
83
+ expect(calculateFileChangeStats({ 'shot.png': png }, {})).toEqual({
84
+ filesChanged: 1,
85
+ insertions: 0,
86
+ deletions: 0
87
+ });
88
+ });
89
+ });
@@ -2,6 +2,11 @@
2
2
  * Diff Calculator Utility
3
3
  * Calculates git-like line-level differences between file contents
4
4
  * Accepts Buffer for binary-safe handling — converts to string internally for line diffing.
5
+ *
6
+ * "Binary-safe" means binary files are not line-counted at all. Splitting a PNG
7
+ * on newline bytes yields a line count with no meaning, and two screenshots
8
+ * once reported +159 lines into a checkpoint's total — dwarfing the source
9
+ * changes the number was there to describe.
5
10
  */
6
11
 
7
12
  export interface FileChangeStats {
@@ -10,11 +15,16 @@ export interface FileChangeStats {
10
15
  deletions: number;
11
16
  }
12
17
 
13
- export interface FileDiff {
14
- filepath: string;
15
- insertions: number;
16
- deletions: number;
17
- status: 'added' | 'modified' | 'deleted';
18
+ /**
19
+ * Whether these bytes are binary, and so cannot be counted or diffed as lines.
20
+ * A NUL byte in the first few KB is the same heuristic git uses.
21
+ */
22
+ export function isBinaryBuffer(buf: Buffer): boolean {
23
+ const limit = Math.min(buf.length, 8000);
24
+ for (let i = 0; i < limit; i++) {
25
+ if (buf[i] === 0) return true;
26
+ }
27
+ return false;
18
28
  }
19
29
 
20
30
  /**
@@ -29,31 +39,30 @@ export function calculateFileChangeStats(
29
39
  let totalDeletions = 0;
30
40
  const changedFiles = new Set<string>();
31
41
 
32
- // Check added and modified files
42
+ // Check added and modified files. A binary file still counts as changed — it
43
+ // just contributes no lines, because it has none.
33
44
  for (const [filepath, newContent] of Object.entries(currentSnapshot)) {
34
45
  const oldContent = previousSnapshot[filepath];
35
46
 
36
47
  if (!oldContent) {
37
- // File was added - count all lines as insertions
38
- const lines = countLines(newContent.toString('utf-8'));
39
- totalInsertions += lines;
40
48
  changedFiles.add(filepath);
49
+ if (isBinaryBuffer(newContent)) continue;
50
+ totalInsertions += countLines(newContent.toString('utf-8'));
41
51
  } else if (!oldContent.equals(newContent)) {
42
- // File was modified - calculate line diff
52
+ changedFiles.add(filepath);
53
+ if (isBinaryBuffer(newContent) || isBinaryBuffer(oldContent)) continue;
43
54
  const diff = calculateLineDiff(oldContent.toString('utf-8'), newContent.toString('utf-8'));
44
55
  totalInsertions += diff.insertions;
45
56
  totalDeletions += diff.deletions;
46
- changedFiles.add(filepath);
47
57
  }
48
58
  }
49
59
 
50
60
  // Check deleted files
51
61
  for (const [filepath, oldContent] of Object.entries(previousSnapshot)) {
52
62
  if (!currentSnapshot[filepath]) {
53
- // File was deleted - count all lines as deletions
54
- const lines = countLines(oldContent.toString('utf-8'));
55
- totalDeletions += lines;
56
63
  changedFiles.add(filepath);
64
+ if (isBinaryBuffer(oldContent)) continue;
65
+ totalDeletions += countLines(oldContent.toString('utf-8'));
57
66
  }
58
67
  }
59
68
 
@@ -64,56 +73,6 @@ export function calculateFileChangeStats(
64
73
  };
65
74
  }
66
75
 
67
- /**
68
- * Get detailed diff for each file
69
- * Useful for displaying individual file changes
70
- */
71
- export function getDetailedFileDiffs(
72
- previousSnapshot: Record<string, Buffer>,
73
- currentSnapshot: Record<string, Buffer>
74
- ): FileDiff[] {
75
- const diffs: FileDiff[] = [];
76
-
77
- // Check added and modified files
78
- for (const [filepath, newContent] of Object.entries(currentSnapshot)) {
79
- const oldContent = previousSnapshot[filepath];
80
-
81
- if (!oldContent) {
82
- // File was added
83
- diffs.push({
84
- filepath,
85
- insertions: countLines(newContent.toString('utf-8')),
86
- deletions: 0,
87
- status: 'added'
88
- });
89
- } else if (!oldContent.equals(newContent)) {
90
- // File was modified
91
- const diff = calculateLineDiff(oldContent.toString('utf-8'), newContent.toString('utf-8'));
92
- diffs.push({
93
- filepath,
94
- insertions: diff.insertions,
95
- deletions: diff.deletions,
96
- status: 'modified'
97
- });
98
- }
99
- }
100
-
101
- // Check deleted files
102
- for (const [filepath, oldContent] of Object.entries(previousSnapshot)) {
103
- if (!currentSnapshot[filepath]) {
104
- // File was deleted
105
- diffs.push({
106
- filepath,
107
- insertions: 0,
108
- deletions: countLines(oldContent.toString('utf-8')),
109
- status: 'deleted'
110
- });
111
- }
112
- }
113
-
114
- return diffs;
115
- }
116
-
117
76
  /**
118
77
  * Count only the lines that actually changed between two strings, using the
119
78
  * same LCS-based diff as git — identical (context) lines are excluded. Use this
@@ -180,7 +139,13 @@ function longestCommonSubsequence(arr1: string[], arr2: string[]): number {
180
139
  function splitLines(content: string): string[] {
181
140
  if (!content) return [];
182
141
  // Normalize line endings and split
183
- return content.replace(/\r\n/g, '\n').split('\n');
142
+ const lines = content.replace(/\r\n/g, '\n').split('\n');
143
+ // A trailing newline terminates the last line, it does not open a new one.
144
+ // Counting the empty string it leaves behind inflated every added or deleted
145
+ // file by exactly one line — a 2-line new file was reported as +3, which is
146
+ // not what git says and not what the user counts in the editor.
147
+ if (lines.length > 1 && lines[lines.length - 1] === '') lines.pop();
148
+ return lines;
184
149
  }
185
150
 
186
151
  /**
@@ -30,6 +30,7 @@ export type LogLabel =
30
30
  | 'mcp'
31
31
  | 'integrations'
32
32
  | 'work'
33
+ | 'deployments'
33
34
  | 'skills'
34
35
  | 'artifacts'
35
36
  | 'commands'
@@ -47,6 +48,7 @@ export type LogLabel =
47
48
  | 'notes'
48
49
  | 'settings'
49
50
  | 'engine'
51
+ | 'env-files'
50
52
  | 'tunnel'
51
53
  | 'remote-access'
52
54
  | 'db-client'