@eloxiv/cli 0.0.0-stage → 0.1.1

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 (294) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -2
  3. package/dist/AgentSkills.d.ts +51 -0
  4. package/dist/AgentSkills.d.ts.map +1 -0
  5. package/dist/AgentSkills.js +264 -0
  6. package/dist/AgentSkills.js.map +1 -0
  7. package/dist/AgentWeb.d.ts +83 -0
  8. package/dist/AgentWeb.d.ts.map +1 -0
  9. package/dist/AgentWeb.js +436 -0
  10. package/dist/AgentWeb.js.map +1 -0
  11. package/dist/App.d.ts +213 -0
  12. package/dist/App.d.ts.map +1 -0
  13. package/dist/App.js +357 -0
  14. package/dist/App.js.map +1 -0
  15. package/dist/Blueprint.d.ts +47 -0
  16. package/dist/Blueprint.d.ts.map +1 -0
  17. package/dist/Blueprint.js +241 -0
  18. package/dist/Blueprint.js.map +1 -0
  19. package/dist/Brand.d.ts +42 -0
  20. package/dist/Brand.d.ts.map +1 -0
  21. package/dist/Brand.js +370 -0
  22. package/dist/Brand.js.map +1 -0
  23. package/dist/Changelog.d.ts +22 -0
  24. package/dist/Changelog.d.ts.map +1 -0
  25. package/dist/Changelog.js +117 -0
  26. package/dist/Changelog.js.map +1 -0
  27. package/dist/Cli.d.ts +17 -0
  28. package/dist/Cli.d.ts.map +1 -0
  29. package/dist/Cli.js +810 -0
  30. package/dist/Cli.js.map +1 -0
  31. package/dist/DbAdmin.d.ts +18 -0
  32. package/dist/DbAdmin.d.ts.map +1 -0
  33. package/dist/DbAdmin.js +91 -0
  34. package/dist/DbAdmin.js.map +1 -0
  35. package/dist/Dependencies.d.ts +53 -0
  36. package/dist/Dependencies.d.ts.map +1 -0
  37. package/dist/Dependencies.js +132 -0
  38. package/dist/Dependencies.js.map +1 -0
  39. package/dist/Deploy.d.ts +39 -0
  40. package/dist/Deploy.d.ts.map +1 -0
  41. package/dist/Deploy.js +458 -0
  42. package/dist/Deploy.js.map +1 -0
  43. package/dist/Docs.d.ts +71 -0
  44. package/dist/Docs.d.ts.map +1 -0
  45. package/dist/Docs.js +240 -0
  46. package/dist/Docs.js.map +1 -0
  47. package/dist/Doctor.d.ts +56 -0
  48. package/dist/Doctor.d.ts.map +1 -0
  49. package/dist/Doctor.js +368 -0
  50. package/dist/Doctor.js.map +1 -0
  51. package/dist/Dossier.d.ts +23 -0
  52. package/dist/Dossier.d.ts.map +1 -0
  53. package/dist/Dossier.js +92 -0
  54. package/dist/Dossier.js.map +1 -0
  55. package/dist/Emit.d.ts +26 -0
  56. package/dist/Emit.d.ts.map +1 -0
  57. package/dist/Emit.js +51 -0
  58. package/dist/Emit.js.map +1 -0
  59. package/dist/Errors.d.ts +26 -0
  60. package/dist/Errors.d.ts.map +1 -0
  61. package/dist/Errors.js +17 -0
  62. package/dist/Errors.js.map +1 -0
  63. package/dist/Evals.d.ts +52 -0
  64. package/dist/Evals.d.ts.map +1 -0
  65. package/dist/Evals.js +126 -0
  66. package/dist/Evals.js.map +1 -0
  67. package/dist/Files.d.ts +47 -0
  68. package/dist/Files.d.ts.map +1 -0
  69. package/dist/Files.js +145 -0
  70. package/dist/Files.js.map +1 -0
  71. package/dist/Generate.d.ts +80 -0
  72. package/dist/Generate.d.ts.map +1 -0
  73. package/dist/Generate.js +310 -0
  74. package/dist/Generate.js.map +1 -0
  75. package/dist/Guards.d.ts +45 -0
  76. package/dist/Guards.d.ts.map +1 -0
  77. package/dist/Guards.js +530 -0
  78. package/dist/Guards.js.map +1 -0
  79. package/dist/Inspect.d.ts +125 -0
  80. package/dist/Inspect.d.ts.map +1 -0
  81. package/dist/Inspect.js +270 -0
  82. package/dist/Inspect.js.map +1 -0
  83. package/dist/Keychain.d.ts +57 -0
  84. package/dist/Keychain.d.ts.map +1 -0
  85. package/dist/Keychain.js +73 -0
  86. package/dist/Keychain.js.map +1 -0
  87. package/dist/Lock.d.ts +13 -0
  88. package/dist/Lock.d.ts.map +1 -0
  89. package/dist/Lock.js +145 -0
  90. package/dist/Lock.js.map +1 -0
  91. package/dist/Mcp.d.ts +91 -0
  92. package/dist/Mcp.d.ts.map +1 -0
  93. package/dist/Mcp.js +128 -0
  94. package/dist/Mcp.js.map +1 -0
  95. package/dist/Merge.d.ts +18 -0
  96. package/dist/Merge.d.ts.map +1 -0
  97. package/dist/Merge.js +43 -0
  98. package/dist/Merge.js.map +1 -0
  99. package/dist/Migrate.d.ts +78 -0
  100. package/dist/Migrate.d.ts.map +1 -0
  101. package/dist/Migrate.js +288 -0
  102. package/dist/Migrate.js.map +1 -0
  103. package/dist/Plugins.d.ts +36 -0
  104. package/dist/Plugins.d.ts.map +1 -0
  105. package/dist/Plugins.js +304 -0
  106. package/dist/Plugins.js.map +1 -0
  107. package/dist/Postgres.d.ts +73 -0
  108. package/dist/Postgres.d.ts.map +1 -0
  109. package/dist/Postgres.js +294 -0
  110. package/dist/Postgres.js.map +1 -0
  111. package/dist/Publish.d.ts +23 -0
  112. package/dist/Publish.d.ts.map +1 -0
  113. package/dist/Publish.js +59 -0
  114. package/dist/Publish.js.map +1 -0
  115. package/dist/Registry.d.ts +539 -0
  116. package/dist/Registry.d.ts.map +1 -0
  117. package/dist/Registry.js +340 -0
  118. package/dist/Registry.js.map +1 -0
  119. package/dist/Remote.d.ts +90 -0
  120. package/dist/Remote.d.ts.map +1 -0
  121. package/dist/Remote.js +1006 -0
  122. package/dist/Remote.js.map +1 -0
  123. package/dist/Run.d.ts +120 -0
  124. package/dist/Run.d.ts.map +1 -0
  125. package/dist/Run.js +477 -0
  126. package/dist/Run.js.map +1 -0
  127. package/dist/SyncGen.d.ts +118 -0
  128. package/dist/SyncGen.d.ts.map +1 -0
  129. package/dist/SyncGen.js +392 -0
  130. package/dist/SyncGen.js.map +1 -0
  131. package/dist/Update.d.ts +301 -0
  132. package/dist/Update.d.ts.map +1 -0
  133. package/dist/Update.js +627 -0
  134. package/dist/Update.js.map +1 -0
  135. package/dist/bin.d.ts +3 -0
  136. package/dist/bin.d.ts.map +1 -0
  137. package/dist/bin.js +42 -0
  138. package/dist/bin.js.map +1 -0
  139. package/dist/docs/backlog.md +56 -0
  140. package/dist/docs/ci.md +277 -0
  141. package/dist/docs/cli.md +696 -0
  142. package/dist/docs/connectors/google.md +115 -0
  143. package/dist/docs/connectors/http-openapi.md +7 -0
  144. package/dist/docs/connectors/mcp.md +8 -0
  145. package/dist/docs/connectors/microsoft-graph.md +75 -0
  146. package/dist/docs/connectors/openai.md +25 -0
  147. package/dist/docs/connectors/slack.md +162 -0
  148. package/dist/docs/connectors/workos.md +39 -0
  149. package/dist/docs/contracts/README.md +173 -0
  150. package/dist/docs/contracts/action-consent.md +92 -0
  151. package/dist/docs/contracts/action-registry.md +85 -0
  152. package/dist/docs/contracts/actor.md +100 -0
  153. package/dist/docs/contracts/agent-resources.md +90 -0
  154. package/dist/docs/contracts/agent-runtime.md +257 -0
  155. package/dist/docs/contracts/approvals.md +126 -0
  156. package/dist/docs/contracts/audit.md +75 -0
  157. package/dist/docs/contracts/auth-provider.md +74 -0
  158. package/dist/docs/contracts/calendar-provider.md +46 -0
  159. package/dist/docs/contracts/client-sdk.md +245 -0
  160. package/dist/docs/contracts/collab-document.md +151 -0
  161. package/dist/docs/contracts/command.md +44 -0
  162. package/dist/docs/contracts/connections.md +235 -0
  163. package/dist/docs/contracts/context-accounting.md +50 -0
  164. package/dist/docs/contracts/daemon.md +243 -0
  165. package/dist/docs/contracts/db-tx.md +53 -0
  166. package/dist/docs/contracts/delegation.md +68 -0
  167. package/dist/docs/contracts/device-control.md +103 -0
  168. package/dist/docs/contracts/directory-provider.md +36 -0
  169. package/dist/docs/contracts/directory.md +161 -0
  170. package/dist/docs/contracts/document-source.md +33 -0
  171. package/dist/docs/contracts/files.md +115 -0
  172. package/dist/docs/contracts/flags.md +83 -0
  173. package/dist/docs/contracts/host.md +68 -0
  174. package/dist/docs/contracts/http-api.md +43 -0
  175. package/dist/docs/contracts/ingestion.md +130 -0
  176. package/dist/docs/contracts/ingress.md +141 -0
  177. package/dist/docs/contracts/invocation.md +260 -0
  178. package/dist/docs/contracts/local-client.md +110 -0
  179. package/dist/docs/contracts/locale.md +56 -0
  180. package/dist/docs/contracts/machine-identity.md +117 -0
  181. package/dist/docs/contracts/mail-provider.md +49 -0
  182. package/dist/docs/contracts/memory.md +53 -0
  183. package/dist/docs/contracts/messaging.md +40 -0
  184. package/dist/docs/contracts/metering.md +139 -0
  185. package/dist/docs/contracts/model-provider.md +84 -0
  186. package/dist/docs/contracts/module-manifest.md +213 -0
  187. package/dist/docs/contracts/notification-channel.md +90 -0
  188. package/dist/docs/contracts/notifications.md +123 -0
  189. package/dist/docs/contracts/outbox.md +80 -0
  190. package/dist/docs/contracts/privacy.md +110 -0
  191. package/dist/docs/contracts/progress.md +52 -0
  192. package/dist/docs/contracts/query-surface.md +108 -0
  193. package/dist/docs/contracts/readiness.md +63 -0
  194. package/dist/docs/contracts/release.md +142 -0
  195. package/dist/docs/contracts/result-rendering.md +61 -0
  196. package/dist/docs/contracts/sandbox.md +186 -0
  197. package/dist/docs/contracts/scheduler.md +148 -0
  198. package/dist/docs/contracts/screen-context.md +81 -0
  199. package/dist/docs/contracts/search.md +120 -0
  200. package/dist/docs/contracts/secrets.md +74 -0
  201. package/dist/docs/contracts/settings.md +109 -0
  202. package/dist/docs/contracts/sharing.md +77 -0
  203. package/dist/docs/contracts/slot.md +48 -0
  204. package/dist/docs/contracts/space.md +67 -0
  205. package/dist/docs/contracts/storage-adapter.md +45 -0
  206. package/dist/docs/contracts/storage-backend.md +56 -0
  207. package/dist/docs/contracts/sync.md +213 -0
  208. package/dist/docs/contracts/telemetry.md +80 -0
  209. package/dist/docs/contracts/theme-tokens.md +50 -0
  210. package/dist/docs/contracts/tool-source.md +58 -0
  211. package/dist/docs/contracts/versioning.md +99 -0
  212. package/dist/docs/contracts/view.md +65 -0
  213. package/dist/docs/contracts/work-items.md +99 -0
  214. package/dist/docs/contracts/workflow.md +82 -0
  215. package/dist/docs/decisions.md +163 -0
  216. package/dist/docs/example-app.md +192 -0
  217. package/dist/docs/getting-started.md +927 -0
  218. package/dist/docs/goal.md +54 -0
  219. package/dist/docs/parity.md +29 -0
  220. package/dist/docs/performance.md +129 -0
  221. package/dist/docs/registry/README.md +74 -0
  222. package/dist/docs/registry/app.md +38 -0
  223. package/dist/docs/registry/approvals-ui-foldkit.md +28 -0
  224. package/dist/docs/registry/calendar-ui-foldkit.md +27 -0
  225. package/dist/docs/registry/calendar.md +35 -0
  226. package/dist/docs/registry/chat-ui-foldkit.md +30 -0
  227. package/dist/docs/registry/desktop-rune.md +50 -0
  228. package/dist/docs/registry/desktop.md +32 -0
  229. package/dist/docs/registry/eloxiv-action.md +25 -0
  230. package/dist/docs/registry/eloxiv-connector.md +25 -0
  231. package/dist/docs/registry/eloxiv-module.md +25 -0
  232. package/dist/docs/registry/eloxiv-policy.md +25 -0
  233. package/dist/docs/registry/eloxiv-view.md +25 -0
  234. package/dist/docs/registry/hello-ui-foldkit.md +29 -0
  235. package/dist/docs/registry/hello.md +39 -0
  236. package/dist/docs/registry/home-ui-foldkit.md +27 -0
  237. package/dist/docs/registry/home.md +29 -0
  238. package/dist/docs/registry/inbox-ui-foldkit.md +27 -0
  239. package/dist/docs/registry/mail-ui-foldkit.md +27 -0
  240. package/dist/docs/registry/mail.md +34 -0
  241. package/dist/docs/registry/meeting-prep.md +26 -0
  242. package/dist/docs/registry/members-ui-foldkit.md +28 -0
  243. package/dist/docs/registry/mobile.md +37 -0
  244. package/dist/docs/registry/notes-ui-foldkit.md +27 -0
  245. package/dist/docs/registry/notes.md +26 -0
  246. package/dist/docs/registry/people-ui-foldkit.md +28 -0
  247. package/dist/docs/registry/people.md +38 -0
  248. package/dist/docs/registry/shell-foldkit.md +35 -0
  249. package/dist/docs/registry/theme.md +25 -0
  250. package/dist/docs/registry/ui-conformance.md +30 -0
  251. package/dist/docs/release.md +100 -0
  252. package/dist/docs/scaffold.md +826 -0
  253. package/dist/docs/security/threat-model.md +299 -0
  254. package/dist/docs/upstream/zero-cache-socket-handoff.md +105 -0
  255. package/dist/index.d.ts +8 -0
  256. package/dist/index.d.ts.map +1 -0
  257. package/dist/index.js +8 -0
  258. package/dist/index.js.map +1 -0
  259. package/package.json +48 -3
  260. package/src/AgentSkills.ts +329 -0
  261. package/src/AgentWeb.ts +507 -0
  262. package/src/App.ts +435 -0
  263. package/src/Blueprint.ts +276 -0
  264. package/src/Brand.ts +415 -0
  265. package/src/Changelog.ts +136 -0
  266. package/src/Cli.ts +1458 -0
  267. package/src/DbAdmin.ts +115 -0
  268. package/src/Dependencies.ts +170 -0
  269. package/src/Deploy.ts +563 -0
  270. package/src/Docs.ts +309 -0
  271. package/src/Doctor.ts +424 -0
  272. package/src/Dossier.ts +148 -0
  273. package/src/Emit.ts +68 -0
  274. package/src/Errors.ts +18 -0
  275. package/src/Evals.ts +153 -0
  276. package/src/Files.ts +152 -0
  277. package/src/Generate.ts +402 -0
  278. package/src/Guards.ts +541 -0
  279. package/src/Inspect.ts +358 -0
  280. package/src/Keychain.ts +107 -0
  281. package/src/Lock.ts +174 -0
  282. package/src/Mcp.ts +159 -0
  283. package/src/Merge.ts +70 -0
  284. package/src/Migrate.ts +331 -0
  285. package/src/Plugins.ts +347 -0
  286. package/src/Postgres.ts +353 -0
  287. package/src/Publish.ts +64 -0
  288. package/src/Registry.ts +405 -0
  289. package/src/Remote.ts +1158 -0
  290. package/src/Run.ts +541 -0
  291. package/src/SyncGen.ts +570 -0
  292. package/src/Update.ts +873 -0
  293. package/src/bin.ts +46 -0
  294. package/src/index.ts +7 -0
package/src/Update.ts ADDED
@@ -0,0 +1,873 @@
1
+ /**
2
+ * `eloxiv diff`, `eloxiv update`, `eloxiv update --continue` and `eloxiv eject`.
3
+ *
4
+ * Every installed item has a base: the exact version the app installed, which the registry keeps
5
+ * immutably at `<name>/<version>.json` and the lock records (version, content hash, a sha256 per file as
6
+ * published). A working file whose hash differs from the lock's is a local edit; a registry whose latest
7
+ * content hash differs from the lock's has upstream changes. `update` merges the two per file (base,
8
+ * ours = the app's file, theirs = the new version).
9
+ *
10
+ * The app's files are read as bytes. A file is merged line by line only when it is valid UTF-8 without NUL
11
+ * on every side; otherwise it is binary and compared byte for byte.
12
+ */
13
+ import type { ProvenanceConflict, ProvenanceEntry } from "@eloxiv/contracts"
14
+ import {
15
+ applyMigrations,
16
+ formatMigrationChanges,
17
+ migrationsBetween,
18
+ type Migration,
19
+ type MigrationChange,
20
+ } from "./Migrate.ts"
21
+ import { Effect } from "effect"
22
+ import {
23
+ CliError,
24
+ installItems,
25
+ lockPath,
26
+ publishedFiles,
27
+ readConfig,
28
+ readLock,
29
+ readPackage,
30
+ regenerate,
31
+ writeLock,
32
+ writePackage,
33
+ type MutableLock,
34
+ } from "./App.ts"
35
+ import {
36
+ applyChanges,
37
+ entriesOf,
38
+ fieldLabel,
39
+ formatNpmChange,
40
+ PACKAGE_FIELDS,
41
+ planAdded,
42
+ planMerge,
43
+ writesPackage,
44
+ type NpmChange,
45
+ type PackageField,
46
+ } from "./Dependencies.ts"
47
+ import {
48
+ asText,
49
+ bytesEqualText,
50
+ isMergeableText,
51
+ readAppBytes,
52
+ removeAppFile,
53
+ sha256Bytes,
54
+ writablePath,
55
+ writeAppFile,
56
+ } from "./Files.ts"
57
+ import { hasConflictMarkers, mergeText, unifiedDiff } from "./Merge.ts"
58
+ import {
59
+ contentHash,
60
+ decodeItem,
61
+ deprecationText,
62
+ fetchDeprecations,
63
+ fetchItem,
64
+ readRegistryText,
65
+ redactToken,
66
+ RegistryError,
67
+ resolveItems,
68
+ targetOf,
69
+ verifyImmutable,
70
+ versionPath,
71
+ type Deprecation,
72
+ type ItemKind,
73
+ type RegistryItem,
74
+ } from "./Registry.ts"
75
+
76
+ export type { NpmChange } from "./Dependencies.ts"
77
+
78
+ export interface VersionRef {
79
+ readonly version: string
80
+ readonly contentHash: string
81
+ }
82
+
83
+ const fromRegistry = (e: RegistryError) => new CliError({ message: e.message })
84
+
85
+ /** The entries for `names` (every installed item when empty), refusing names that are not installed. */
86
+ export const selectEntries = (lock: MutableLock, names: ReadonlyArray<string>) =>
87
+ Effect.gen(function* () {
88
+ if (names.length === 0) return Object.values(lock.items)
89
+ const missing = names.filter((name) => lock.items[name] === undefined)
90
+ if (missing.length > 0)
91
+ return yield* new CliError({ message: `Not installed: ${missing.join(", ")} (use \`eloxiv add\`)` })
92
+ return names.map((name) => lock.items[name]!)
93
+ })
94
+
95
+ export const kindOf = (entry: ProvenanceEntry): ItemKind => (entry.meta?.["kind"] as ItemKind | undefined) ?? "app"
96
+
97
+ /** Fetch the exact version the app installed, and check it is what the lock says was installed. */
98
+ export const fetchBase = Effect.fn("Update.fetchBase")(function* (registry: string, entry: ProvenanceEntry) {
99
+ const relative = entry.item ?? versionPath(entry.name, entry.registryVersion)
100
+ const text = yield* readRegistryText(registry, relative).pipe(Effect.option)
101
+ if (text._tag === "None") return undefined
102
+ const item = yield* decodeItem(text.value, `${entry.name}@${entry.registryVersion}`).pipe(
103
+ Effect.mapError(fromRegistry),
104
+ )
105
+ const hash = yield* contentHash(item)
106
+ if (hash !== entry.contentHash) {
107
+ return yield* new CliError({
108
+ message: `The registry's ${relative} (sha256 ${hash.slice(0, 12)}) is not what the lock says was installed (${entry.contentHash.slice(0, 12)}); a published version must never change`,
109
+ })
110
+ }
111
+ return item
112
+ })
113
+
114
+ const contents = (item: RegistryItem | undefined) =>
115
+ new Map((item?.files ?? []).map((file) => [targetOf(file), file.content] as const))
116
+
117
+ /** Where the new version of a binary file changed on both sides is written, beside the app's file. */
118
+ export const upstreamCopy = (path: string) => `${path}.upstream`
119
+
120
+ /**
121
+ * Files of an item holding conflict markers the published version did not have: recorded content
122
+ * conflicts, and any locally edited file (an update that stopped before it could record them).
123
+ */
124
+ const filesWithMarkers = Effect.fn("Update.filesWithMarkers")(function* (dir: string, entry: ProvenanceEntry) {
125
+ const recorded = new Set(
126
+ (entry.conflicts ?? []).filter((c) => c.reason === "content").map((conflict) => lockPath(conflict.path)),
127
+ )
128
+ const published = new Map(entry.files.map((file) => [lockPath(file.path), file.sha256] as const))
129
+ const found: Array<string> = []
130
+ for (const path of new Set([...recorded, ...published.keys()])) {
131
+ const bytes = yield* readAppBytes(dir, path)
132
+ if (bytes === undefined) continue
133
+ const text = asText(bytes)
134
+ if (text === undefined || !hasConflictMarkers(text)) continue
135
+ if (recorded.has(path) || (yield* sha256Bytes(bytes)) !== published.get(path)) found.push(path)
136
+ }
137
+ return found
138
+ })
139
+
140
+ // ---------------------------------------------------------------------------------------------------------
141
+ // diff
142
+
143
+ export interface LocalChange {
144
+ readonly path: string
145
+ readonly change: "modified" | "deleted"
146
+ /** Unified diff from the installed base to the app's file (absent when the base is not available or the file is binary). */
147
+ readonly diff?: string
148
+ /** The app's file is not UTF-8 text. */
149
+ readonly binary?: true
150
+ }
151
+
152
+ export interface UpstreamChange {
153
+ readonly path: string
154
+ readonly change: "added" | "removed" | "modified"
155
+ readonly diff: string
156
+ }
157
+
158
+ export interface DependencyChange {
159
+ readonly field: PackageField
160
+ readonly name: string
161
+ readonly from?: string
162
+ readonly to?: string
163
+ }
164
+
165
+ export interface ItemDiff {
166
+ readonly name: string
167
+ readonly kind: ItemKind
168
+ readonly owner: "registry" | "app"
169
+ readonly installed: VersionRef
170
+ /** The registry's latest version, or `null` when the registry no longer has the item. */
171
+ readonly latest: VersionRef | null
172
+ readonly conflicts: ReadonlyArray<ProvenanceConflict>
173
+ readonly local: ReadonlyArray<LocalChange>
174
+ readonly upstream: ReadonlyArray<UpstreamChange>
175
+ readonly dependencies: ReadonlyArray<DependencyChange>
176
+ readonly registryDependencies: { readonly added: ReadonlyArray<string>; readonly removed: ReadonlyArray<string> }
177
+ /** Whether the installed version could be fetched again (unified diffs of local edits need it). */
178
+ readonly baseAvailable: boolean
179
+ /** Why the registry retired the item, and what to use instead, when it has. */
180
+ readonly deprecated?: string
181
+ }
182
+
183
+ export interface DiffReport {
184
+ readonly items: ReadonlyArray<ItemDiff>
185
+ /**
186
+ * Whether anything differs: local edits, upstream changes (files, dependencies or metadata) or unresolved
187
+ * conflicts (app-owned items aside).
188
+ */
189
+ readonly differences: boolean
190
+ }
191
+
192
+ const dependencyChanges = (base: RegistryItem, theirs: RegistryItem) => {
193
+ const changes: Array<DependencyChange> = []
194
+ for (const field of PACKAGE_FIELDS) {
195
+ const before = entriesOf(base, field)
196
+ const after = entriesOf(theirs, field)
197
+ for (const [name, to] of after) {
198
+ const from = before.get(name)
199
+ if (from !== to) changes.push(from === undefined ? { field, name, to } : { field, name, from, to })
200
+ }
201
+ for (const [name, from] of before) if (!after.has(name)) changes.push({ field, name, from })
202
+ }
203
+ return changes
204
+ }
205
+
206
+ const registryDependencyChanges = (base: RegistryItem, theirs: RegistryItem) => {
207
+ const before = new Set(base.registryDependencies ?? [])
208
+ const after = new Set(theirs.registryDependencies ?? [])
209
+ return { added: [...after].filter((d) => !before.has(d)), removed: [...before].filter((d) => !after.has(d)) }
210
+ }
211
+
212
+ const diffItem = Effect.fn("Update.diffItem")(function* (
213
+ dir: string,
214
+ registry: string,
215
+ entry: ProvenanceEntry,
216
+ deprecations: Readonly<Record<string, Deprecation>>,
217
+ ) {
218
+ const deprecation = deprecations[entry.name]
219
+ const deprecated = deprecation === undefined ? {} : { deprecated: deprecationText(entry.name, deprecation) }
220
+ const installed = { version: entry.registryVersion, contentHash: entry.contentHash }
221
+ const empty = { added: [], removed: [] }
222
+ const owner = entry.owner ?? "registry"
223
+ if (owner === "app") {
224
+ return {
225
+ name: entry.name,
226
+ kind: kindOf(entry),
227
+ owner,
228
+ installed,
229
+ latest: null,
230
+ conflicts: [],
231
+ local: [],
232
+ upstream: [],
233
+ dependencies: [],
234
+ registryDependencies: empty,
235
+ baseAvailable: false,
236
+ ...deprecated,
237
+ } satisfies ItemDiff
238
+ }
239
+ const base = yield* fetchBase(registry, entry)
240
+ const baseFiles = contents(base)
241
+ const label = `${entry.name}@${entry.registryVersion}`
242
+ const local: Array<LocalChange> = []
243
+ for (const file of entry.files) {
244
+ const path = lockPath(file.path)
245
+ const bytes = yield* readAppBytes(dir, path)
246
+ if (bytes !== undefined && (yield* sha256Bytes(bytes)) === file.sha256) continue
247
+ if (bytes === undefined) {
248
+ local.push({ path, change: "deleted" })
249
+ continue
250
+ }
251
+ const current = asText(bytes)
252
+ const before = baseFiles.get(path)
253
+ if (current === undefined) local.push({ path, change: "modified", binary: true })
254
+ else if (before === undefined) local.push({ path, change: "modified" })
255
+ else
256
+ local.push({
257
+ path,
258
+ change: "modified",
259
+ diff: unifiedDiff(path, before, current, { before: label, after: "app" }),
260
+ })
261
+ }
262
+ const latestItem = yield* fetchItem(registry, entry.name).pipe(
263
+ Effect.map((item) => item as RegistryItem | undefined),
264
+ Effect.catchTag("RegistryError", () => Effect.succeed(undefined)),
265
+ )
266
+ const latestHash = latestItem === undefined ? undefined : yield* contentHash(latestItem)
267
+ const latest =
268
+ latestItem === undefined || latestHash === undefined
269
+ ? null
270
+ : { version: latestItem.meta.eloxiv.version, contentHash: latestHash }
271
+ const upstream: Array<UpstreamChange> = []
272
+ let dependencies: Array<DependencyChange> = []
273
+ let registryDependencies: ItemDiff["registryDependencies"] = empty
274
+ // Without the base, only the new version number is known: per-file upstream changes need the base.
275
+ if (base !== undefined && latestItem !== undefined && latest !== null && latest.contentHash !== entry.contentHash) {
276
+ const after = contents(latestItem)
277
+ const theirsLabel = `${entry.name}@${latest.version}`
278
+ for (const path of [...new Set([...baseFiles.keys(), ...after.keys()])].sort()) {
279
+ const before = baseFiles.get(path)
280
+ const next = after.get(path)
281
+ if (before === next) continue
282
+ upstream.push({
283
+ path,
284
+ change: before === undefined ? "added" : next === undefined ? "removed" : "modified",
285
+ diff: unifiedDiff(path, before, next, { before: label, after: theirsLabel }),
286
+ })
287
+ }
288
+ dependencies = dependencyChanges(base, latestItem)
289
+ registryDependencies = registryDependencyChanges(base, latestItem)
290
+ }
291
+ return {
292
+ name: entry.name,
293
+ kind: kindOf(entry),
294
+ owner,
295
+ installed,
296
+ latest,
297
+ conflicts: entry.conflicts ?? [],
298
+ local,
299
+ upstream,
300
+ dependencies,
301
+ registryDependencies,
302
+ baseAvailable: base !== undefined,
303
+ ...deprecated,
304
+ } satisfies ItemDiff
305
+ })
306
+
307
+ const itemDiffers = (item: ItemDiff) =>
308
+ item.owner === "registry" &&
309
+ (item.local.length > 0 ||
310
+ item.conflicts.length > 0 ||
311
+ item.upstream.length > 0 ||
312
+ item.dependencies.length > 0 ||
313
+ item.registryDependencies.added.length > 0 ||
314
+ item.registryDependencies.removed.length > 0 ||
315
+ (item.latest !== null && item.latest.contentHash !== item.installed.contentHash))
316
+
317
+ /** Compare installed items with their installed base (local edits) and the registry's latest (upstream). */
318
+ export const diff = Effect.fn("Update.diff")(function* (dir: string, names: ReadonlyArray<string>) {
319
+ const { registry } = yield* readConfig(dir)
320
+ const lock = yield* readLock(dir)
321
+ const items: Array<ItemDiff> = []
322
+ const deprecations = yield* fetchDeprecations(registry).pipe(Effect.mapError(fromRegistry))
323
+ for (const entry of yield* selectEntries(lock, names)) items.push(yield* diffItem(dir, registry, entry, deprecations))
324
+ return { items, differences: items.some(itemDiffers) } satisfies DiffReport
325
+ })
326
+
327
+ export const formatDiff = (report: DiffReport, options: { readonly patches: boolean }) => {
328
+ const out: Array<string> = []
329
+ for (const item of report.items) {
330
+ const head = `${item.name} (${item.kind}) ${item.installed.version}`
331
+ if (item.owner === "app") {
332
+ out.push(`${head}: app-owned (eloxiv eject); not compared`)
333
+ continue
334
+ }
335
+ const behind = item.latest !== null && item.latest.contentHash !== item.installed.contentHash
336
+ const state = [
337
+ item.local.length > 0 ? `${item.local.length} locally edited` : "no local edits",
338
+ item.latest === null
339
+ ? "not in the registry any more"
340
+ : behind
341
+ ? `${item.latest.version} available`
342
+ : "up to date",
343
+ ...(item.conflicts.length > 0 ? [`${item.conflicts.length} unresolved conflicts`] : []),
344
+ ]
345
+ out.push(`${head}: ${state.join(", ")}`)
346
+ if (item.deprecated !== undefined) out.push(` deprecated ${item.deprecated}`)
347
+ for (const conflict of item.conflicts) out.push(` conflict ${conflict.path} (${conflict.reason})`)
348
+ for (const change of item.local)
349
+ out.push(` local ${change.change.padEnd(9)}${change.path}${change.binary === true ? " (binary)" : ""}`)
350
+ for (const change of item.upstream) out.push(` upstream ${change.change.padEnd(9)}${change.path}`)
351
+ for (const dep of item.dependencies)
352
+ out.push(` upstream ${fieldLabel(dep.field)} ${dep.name} ${dep.from ?? "(none)"} → ${dep.to ?? "(removed)"}`)
353
+ for (const dep of item.registryDependencies.added) out.push(` upstream needs registry item ${dep}`)
354
+ for (const dep of item.registryDependencies.removed) out.push(` upstream no longer needs registry item ${dep}`)
355
+ if (!item.baseAvailable)
356
+ out.push(
357
+ ` (the installed version is not published in the registry: local edits are shown without diffs, upstream changes not at all, and update cannot merge)`,
358
+ )
359
+ if (!options.patches) continue
360
+ for (const change of item.local)
361
+ if (change.diff !== undefined && change.diff !== "") out.push(change.diff.trimEnd())
362
+ for (const change of item.upstream) if (change.diff !== "") out.push(change.diff.trimEnd())
363
+ }
364
+ if (report.items.length === 0) out.push("no items installed")
365
+ return out.join("\n")
366
+ }
367
+
368
+ // ---------------------------------------------------------------------------------------------------------
369
+ // update
370
+
371
+ export type FileOutcome =
372
+ /** Unchanged locally, changed upstream: replaced with the new version. */
373
+ | "updated"
374
+ /** Changed locally, unchanged upstream: the app's file is kept. */
375
+ | "kept"
376
+ /** Changed on both sides, merged cleanly: the merge is written. */
377
+ | "merged"
378
+ /** Changed on both sides, overlapping: written with conflict markers. */
379
+ | "conflict"
380
+ /** A binary file changed on both sides: the app's file is kept, the new version written beside it. */
381
+ | "conflict-binary"
382
+ /** Changed locally to exactly the new version. */
383
+ | "already-updated"
384
+ /** New upstream: written. */
385
+ | "added"
386
+ /** Removed upstream, unchanged locally: deleted. */
387
+ | "removed"
388
+ /** Removed upstream, changed locally: kept and no longer part of the item (the app owns it). */
389
+ | "kept-removed-upstream"
390
+ /** Deleted locally, unchanged upstream: stays deleted. */
391
+ | "deleted-locally"
392
+ /** Deleted locally, changed upstream: nothing written; a conflict for the developer to decide. */
393
+ | "conflict-deleted-locally"
394
+ /** New upstream, but an unrelated file of the same path is in the way: the item is not updated. */
395
+ | "blocked"
396
+
397
+ export interface FilePlan {
398
+ readonly path: string
399
+ readonly outcome: FileOutcome
400
+ /** What is written (updated, merged, conflict, conflict-binary, added). */
401
+ readonly content?: string
402
+ /** For conflict-binary: where `content` (the new version) is written, beside the app's file. */
403
+ readonly upstream?: string
404
+ /** For blocked files: whose file is in the way. */
405
+ readonly detail?: string
406
+ }
407
+
408
+ export interface ItemUpdate {
409
+ readonly name: string
410
+ readonly kind: ItemKind
411
+ readonly status: "up-to-date" | "updated" | "conflicts" | "blocked" | "app-owned" | "unresolved-conflicts"
412
+ readonly from: VersionRef
413
+ readonly to?: VersionRef
414
+ readonly files: ReadonlyArray<FilePlan>
415
+ /** Registry items the new version depends on that the app did not have: installed with it. */
416
+ readonly installs: ReadonlyArray<string>
417
+ /** The item's npm dependency changes, then those of the registry items installed with it (`item` set). */
418
+ readonly npm: ReadonlyArray<NpmChange>
419
+ /** The patch files its installed or new version names (`meta.eloxiv.patchedDependencies`). */
420
+ readonly patches?: ReadonlyArray<string>
421
+ /** For unresolved-conflicts: files still holding conflict markers. */
422
+ readonly markers?: ReadonlyArray<string>
423
+ /** Why the registry retired the item, and what to use instead, when it has. */
424
+ readonly deprecated?: string
425
+ }
426
+
427
+ export interface UpdateReport {
428
+ readonly dryRun: boolean
429
+ readonly items: ReadonlyArray<ItemUpdate>
430
+ /** What the updated items' compatibility migrations changed (or would) in the app's own files. */
431
+ readonly migrations: ReadonlyArray<MigrationChange>
432
+ /** The migrations' notes: what a person or agent must still do. */
433
+ readonly notes: ReadonlyArray<string>
434
+ /** Whether the update left anything for the developer: conflicts, blocked items or earlier conflicts. */
435
+ readonly needsAttention: boolean
436
+ }
437
+
438
+ const CONFLICTS: ReadonlySet<FileOutcome> = new Set(["conflict", "conflict-binary", "conflict-deleted-locally"])
439
+
440
+ const planFiles = Effect.fn("Update.planFiles")(function* (
441
+ dir: string,
442
+ entry: ProvenanceEntry,
443
+ base: RegistryItem,
444
+ theirs: RegistryItem,
445
+ ownedElsewhere: ReadonlyMap<string, string>,
446
+ ) {
447
+ const before = contents(base)
448
+ const after = contents(theirs)
449
+ const labels = {
450
+ ours: "app",
451
+ base: `${entry.name}@${entry.registryVersion}`,
452
+ theirs: `${entry.name}@${theirs.meta.eloxiv.version}`,
453
+ }
454
+ const plans: Array<FilePlan> = []
455
+ for (const path of [...new Set([...before.keys(), ...after.keys()])].sort()) {
456
+ // Every path the update may write or delete: refused through a symbolic link before anything is written.
457
+ yield* writablePath(dir, path)
458
+ const b = before.get(path)
459
+ const t = after.get(path)
460
+ const o = yield* readAppBytes(dir, path)
461
+ const is = (text: string) => o !== undefined && bytesEqualText(o, text)
462
+ if (b !== undefined && t !== undefined) {
463
+ if (o === undefined) {
464
+ if (b !== t) plans.push({ path, outcome: "conflict-deleted-locally" })
465
+ else plans.push({ path, outcome: "deleted-locally" })
466
+ } else if (is(b)) {
467
+ if (t !== b) plans.push({ path, outcome: "updated", content: t })
468
+ } else if (t === b) plans.push({ path, outcome: "kept" })
469
+ else if (is(t)) plans.push({ path, outcome: "already-updated" })
470
+ else {
471
+ const ours = asText(o)
472
+ if (ours !== undefined && isMergeableText(b) && isMergeableText(t)) {
473
+ const merged = mergeText(ours, b, t, labels)
474
+ plans.push({ path, outcome: merged.clean ? "merged" : "conflict", content: merged.text })
475
+ } else {
476
+ // Binary: never line-merged. The app's file stays; the new version goes beside it.
477
+ const upstream = upstreamCopy(path)
478
+ yield* writablePath(dir, upstream)
479
+ const existing = yield* readAppBytes(dir, upstream)
480
+ const owner = ownedElsewhere.get(upstream) ?? (after.has(upstream) ? entry.name : undefined)
481
+ if (owner !== undefined) plans.push({ path, outcome: "blocked", detail: `${upstream} (of ${owner})` })
482
+ else if (existing !== undefined && !bytesEqualText(existing, t))
483
+ plans.push({ path, outcome: "blocked", detail: `an unrelated local file at ${upstream}` })
484
+ else plans.push({ path, outcome: "conflict-binary", content: t, upstream })
485
+ }
486
+ }
487
+ } else if (b !== undefined) {
488
+ // Removed upstream.
489
+ if (o === undefined) continue
490
+ plans.push({ path, outcome: is(b) ? "removed" : "kept-removed-upstream" })
491
+ } else if (t !== undefined) {
492
+ // Added upstream.
493
+ const owner = ownedElsewhere.get(path)
494
+ if (owner !== undefined && !is(t)) plans.push({ path, outcome: "blocked", detail: `belongs to ${owner}` })
495
+ else if (o === undefined) plans.push({ path, outcome: "added", content: t })
496
+ else if (is(t)) plans.push({ path, outcome: "already-updated" })
497
+ else plans.push({ path, outcome: "blocked", detail: "an unrelated local file" })
498
+ }
499
+ }
500
+ return plans
501
+ })
502
+
503
+ interface Planned {
504
+ readonly report: ItemUpdate
505
+ readonly theirs?: RegistryItem
506
+ readonly theirsHash?: string
507
+ }
508
+
509
+ const planItem = Effect.fn("Update.planItem")(function* (
510
+ dir: string,
511
+ registry: string,
512
+ lock: MutableLock,
513
+ entry: ProvenanceEntry,
514
+ pkg: Record<string, any>,
515
+ deprecations: Readonly<Record<string, Deprecation>>,
516
+ ) {
517
+ const from = { version: entry.registryVersion, contentHash: entry.contentHash }
518
+ const deprecation = deprecations[entry.name]
519
+ const unchanged = {
520
+ name: entry.name,
521
+ kind: kindOf(entry),
522
+ from,
523
+ files: [],
524
+ installs: [],
525
+ npm: [],
526
+ ...(deprecation === undefined ? {} : { deprecated: deprecationText(entry.name, deprecation) }),
527
+ }
528
+ if (entry.owner === "app") return { report: { ...unchanged, status: "app-owned" } } satisfies Planned
529
+ if ((entry.conflicts ?? []).length > 0)
530
+ return { report: { ...unchanged, status: "unresolved-conflicts" } } satisfies Planned
531
+ const theirs = yield* fetchItem(registry, entry.name).pipe(Effect.mapError(fromRegistry))
532
+ const theirsHash = yield* contentHash(theirs)
533
+ const to = { version: theirs.meta.eloxiv.version, contentHash: theirsHash }
534
+ if (theirsHash === entry.contentHash && to.version === entry.registryVersion)
535
+ return { report: { ...unchanged, status: "up-to-date" } } satisfies Planned
536
+ if (to.version === entry.registryVersion) {
537
+ return yield* new CliError({
538
+ message: `The registry republished ${entry.name}@${to.version} with different content (${theirsHash.slice(0, 12)}, installed ${entry.contentHash.slice(0, 12)}); refusing to merge against a changed version`,
539
+ })
540
+ }
541
+ // The new version becomes the next update's base: it must be exactly its immutable copy.
542
+ yield* verifyImmutable(registry, theirs).pipe(Effect.mapError(fromRegistry))
543
+ // Conflict markers the last update could not record: resolve them before merging anything else in.
544
+ const markers = yield* filesWithMarkers(dir, entry)
545
+ if (markers.length > 0) return { report: { ...unchanged, status: "unresolved-conflicts", markers } } satisfies Planned
546
+ const base = yield* fetchBase(registry, entry)
547
+ if (base === undefined) {
548
+ return yield* new CliError({
549
+ message: `${entry.name}@${entry.registryVersion}, the version this app installed, is not published in ${redactToken(registry)} (expected ${entry.item ?? versionPath(entry.name, entry.registryVersion)}); cannot merge without it`,
550
+ })
551
+ }
552
+ const ownedElsewhere = new Map(
553
+ Object.values(lock.items)
554
+ .filter((other) => other.name !== entry.name)
555
+ .flatMap((other) => other.files.map((file) => [lockPath(file.path), other.name] as const)),
556
+ )
557
+ const files = yield* planFiles(dir, entry, base, theirs, ownedElsewhere)
558
+ const missing = (theirs.registryDependencies ?? []).filter((dep) => lock.items[dep] === undefined)
559
+ const installed =
560
+ missing.length === 0
561
+ ? []
562
+ : (yield* resolveItems(registry, missing).pipe(Effect.mapError(fromRegistry))).filter(
563
+ (item) => lock.items[item.name] === undefined,
564
+ )
565
+ // A dependency this item drops goes only if no other installed item asks for it (at its installed version, or
566
+ // being installed with it); with an item the registry cannot describe (an app-owned one), everything stays.
567
+ const others: Array<RegistryItem> = [...installed]
568
+ let known = true
569
+ for (const other of Object.values(lock.items)) {
570
+ if (other.name === entry.name) continue
571
+ const item = other.owner === "app" ? undefined : yield* fetchBase(registry, other)
572
+ if (item === undefined) known = false
573
+ else others.push(item)
574
+ }
575
+ const needed = (field: PackageField, name: string) =>
576
+ !known || others.some((item) => entriesOf(item, field).has(name))
577
+ // One merge into package.json for the item and the items installed with it, keeping the app's pins.
578
+ const own = planMerge(pkg, base, theirs, needed)
579
+ const npm = [...own, ...planAdded(applyChanges(structuredClone(pkg), own), installed, true)]
580
+ const blocked = files.some((file) => file.outcome === "blocked")
581
+ const conflicted = files.some((file) => CONFLICTS.has(file.outcome))
582
+ const status = blocked ? "blocked" : conflicted ? "conflicts" : "updated"
583
+ return {
584
+ report: {
585
+ name: entry.name,
586
+ kind: kindOf(entry),
587
+ status,
588
+ from,
589
+ to,
590
+ files,
591
+ installs: installed.map((item) => item.name),
592
+ npm,
593
+ patches: [
594
+ ...new Set([base, theirs].flatMap((item) => Object.values(item.meta.eloxiv.patchedDependencies ?? {}))),
595
+ ],
596
+ },
597
+ theirs,
598
+ theirsHash,
599
+ } satisfies Planned
600
+ })
601
+
602
+ /** The file outcomes that change what is on disk. */
603
+ const WRITTEN = new Set<FileOutcome>(["updated", "merged", "conflict", "added", "removed"])
604
+
605
+ /**
606
+ * Whether the package manager must run after an update: an item installed with it, an npm dependency added or
607
+ * changed, or a pnpm patch whose entry or file changed or went (pnpm applies a patch only when it installs).
608
+ */
609
+ export const needsInstall = (report: UpdateReport) =>
610
+ report.items.some(
611
+ (item) =>
612
+ item.installs.length > 0 ||
613
+ item.npm.some(
614
+ (change) =>
615
+ change.outcome === "added" ||
616
+ change.outcome === "changed" ||
617
+ (change.field === "patchedDependencies" && change.outcome === "removed"),
618
+ ) ||
619
+ item.files.some((file) => WRITTEN.has(file.outcome) && (item.patches ?? []).includes(file.path)),
620
+ )
621
+
622
+ export interface UpdateOptions {
623
+ readonly dryRun: boolean
624
+ }
625
+
626
+ const conflictOf = (file: FilePlan): ProvenanceConflict => ({
627
+ path: file.path,
628
+ reason: file.outcome === "conflict" ? "content" : file.outcome === "conflict-binary" ? "binary" : "deleted-locally",
629
+ })
630
+
631
+ /**
632
+ * Merge the registry's latest version of `names` (every installed item when empty) into the app. Nothing is
633
+ * written with `dryRun`. An item with a blocked file is not touched at all.
634
+ *
635
+ * Writes are not transactional. An item's conflicts are recorded in the lock before any of its files is
636
+ * written, so an update that stops half-way leaves the item waiting for `update --continue` rather than
637
+ * conflict markers nobody knows about; `--continue` also finds markers that were never recorded.
638
+ */
639
+ export const update = Effect.fn("Update.update")(function* (
640
+ dir: string,
641
+ names: ReadonlyArray<string>,
642
+ options: UpdateOptions,
643
+ ) {
644
+ const { config, registry } = yield* readConfig(dir)
645
+ let lock = yield* readLock(dir)
646
+ const reports: Array<ItemUpdate> = []
647
+ const deprecations = yield* fetchDeprecations(registry).pipe(Effect.mapError(fromRegistry))
648
+ const pending: Array<{ readonly item: string; readonly migration: Migration }> = []
649
+ for (const selected of yield* selectEntries(lock, names)) {
650
+ // An earlier item's update may have installed this one's dependencies: plan against the current lock.
651
+ const entry = lock.items[selected.name]!
652
+ const pkg = yield* readPackage(dir)
653
+ const planned: Planned = yield* planItem(dir, registry, lock, entry, pkg, deprecations)
654
+ reports.push(planned.report)
655
+ const { report, theirs, theirsHash } = planned
656
+ if (theirs !== undefined && (report.status === "updated" || report.status === "conflicts")) {
657
+ for (const migration of migrationsBetween(
658
+ theirs.meta.eloxiv.migrations ?? [],
659
+ entry.registryVersion,
660
+ theirs.meta.eloxiv.version,
661
+ ))
662
+ pending.push({ item: entry.name, migration })
663
+ }
664
+ if (options.dryRun || theirs === undefined || theirsHash === undefined) continue
665
+ if (report.status !== "updated" && report.status !== "conflicts") continue
666
+ const conflicts = report.files.filter((file) => CONFLICTS.has(file.outcome)).map(conflictOf)
667
+ if (conflicts.length > 0) {
668
+ // Intent first: if anything below fails, the item still waits for these to be resolved.
669
+ lock.items[entry.name] = { ...entry, conflicts }
670
+ yield* writeLock(dir, lock)
671
+ }
672
+ if (report.installs.length > 0) {
673
+ yield* installItems(dir, report.installs, false, { packageJson: false })
674
+ lock = yield* readLock(dir)
675
+ }
676
+ for (const file of report.files) {
677
+ if (file.content !== undefined) yield* writeAppFile(dir, file.upstream ?? file.path, file.content)
678
+ else if (file.outcome === "removed") yield* removeAppFile(dir, file.path)
679
+ }
680
+ if (writesPackage(report.npm)) yield* writePackage(dir, applyChanges(yield* readPackage(dir), report.npm))
681
+ const { conflicts: _previous, ...rest } = entry
682
+ lock.items[entry.name] = {
683
+ ...rest,
684
+ type: theirs.type,
685
+ registry: config.registry,
686
+ registryVersion: theirs.meta.eloxiv.version,
687
+ contentHash: theirsHash,
688
+ files: yield* publishedFiles(theirs),
689
+ meta: theirs.meta.eloxiv,
690
+ item: versionPath(theirs.name, theirs.meta.eloxiv.version),
691
+ updatedFrom: { registryVersion: entry.registryVersion, contentHash: entry.contentHash },
692
+ updatedAt: new Date().toISOString(),
693
+ ...(conflicts.length > 0 ? { conflicts } : {}),
694
+ }
695
+ yield* writeLock(dir, lock)
696
+ }
697
+ if (!options.dryRun && reports.some((r) => r.status === "updated" || r.status === "conflicts"))
698
+ yield* regenerate(dir, lock)
699
+ // Compatibility migrations, over the app's own files: not those the update itself writes (they are the new
700
+ // version's already), nor the composition files the CLI generates.
701
+ const written = new Set(
702
+ reports.flatMap((r) =>
703
+ r.files.filter((f) => WRITTEN.has(f.outcome) && f.outcome !== "conflict").map((f) => f.path),
704
+ ),
705
+ )
706
+ for (const file of ["eloxiv.modules.ts", "eloxiv.server.ts", "eloxiv.schema.ts", "eloxiv.sync.ts"]) written.add(file)
707
+ const migrations = applyMigrations(dir, pending, { dryRun: options.dryRun, exclude: written })
708
+ return {
709
+ dryRun: options.dryRun,
710
+ items: reports,
711
+ migrations,
712
+ notes: pending.flatMap((p) =>
713
+ p.migration.note === undefined ? [] : [`${p.item} ${p.migration.since}: ${p.migration.note}`],
714
+ ),
715
+ needsAttention:
716
+ reports.some((r) => r.status === "conflicts" || r.status === "blocked" || r.status === "unresolved-conflicts") ||
717
+ migrations.some((m) => m.kind === "manual"),
718
+ } satisfies UpdateReport
719
+ })
720
+
721
+ const OUTCOME_LABEL: Record<FileOutcome, string> = {
722
+ updated: "updated",
723
+ kept: "kept",
724
+ merged: "merged",
725
+ conflict: "CONFLICT",
726
+ "conflict-binary": "CONFLICT",
727
+ "already-updated": "same",
728
+ added: "added",
729
+ removed: "removed",
730
+ "kept-removed-upstream": "kept",
731
+ "deleted-locally": "deleted",
732
+ "conflict-deleted-locally": "CONFLICT",
733
+ blocked: "BLOCKED",
734
+ }
735
+
736
+ const OUTCOME_NOTE: Partial<Record<FileOutcome, string>> = {
737
+ kept: "local edits; unchanged upstream",
738
+ merged: "local edits merged with upstream changes",
739
+ conflict: "overlapping changes: conflict markers",
740
+ "already-updated": "already matches the new version",
741
+ "kept-removed-upstream": "modified locally, removed upstream: now the app's own file",
742
+ "deleted-locally": "deleted locally; unchanged upstream",
743
+ "conflict-deleted-locally": "deleted locally, changed upstream: restore it or leave it deleted",
744
+ }
745
+
746
+ const noteOf = (file: FilePlan) =>
747
+ file.outcome === "blocked"
748
+ ? `${file.detail} is in the way; move it and retry`
749
+ : file.outcome === "conflict-binary"
750
+ ? `binary, changed on both sides: yours is kept, the new version is in ${file.upstream}`
751
+ : OUTCOME_NOTE[file.outcome]
752
+
753
+ export const formatUpdate = (report: UpdateReport) => {
754
+ const out: Array<string> = []
755
+ const would = report.dryRun ? "would " : ""
756
+ for (const item of report.items) {
757
+ const head = `${item.name} (${item.kind})`
758
+ switch (item.status) {
759
+ case "up-to-date":
760
+ out.push(`${head} ${item.from.version}: up to date`)
761
+ if (item.deprecated !== undefined) out.push(` deprecated ${item.deprecated}`)
762
+ continue
763
+ case "app-owned":
764
+ out.push(`${head} ${item.from.version}: app-owned (eloxiv eject); not updated`)
765
+ continue
766
+ case "unresolved-conflicts":
767
+ out.push(
768
+ `${head}: has unresolved conflicts from the last update; resolve them, then run eloxiv update --continue`,
769
+ )
770
+ for (const path of item.markers ?? []) out.push(` CONFLICT ${path} (conflict markers)`)
771
+ continue
772
+ }
773
+ const verb =
774
+ item.status === "blocked"
775
+ ? "not updated (blocked)"
776
+ : item.status === "conflicts"
777
+ ? `${would}update with conflicts`
778
+ : `${would}update`
779
+ out.push(`${head} ${item.from.version} → ${item.to?.version}: ${verb}`)
780
+ for (const file of item.files) {
781
+ const note = noteOf(file)
782
+ out.push(` ${OUTCOME_LABEL[file.outcome].padEnd(9)}${file.path}${note === undefined ? "" : ` (${note})`}`)
783
+ }
784
+ for (const name of item.installs) out.push(` ${would}install registry item ${name}`)
785
+ for (const change of item.npm) out.push(` ${formatNpmChange(change, would, item.name)}`)
786
+ }
787
+ const conflicted = report.items.filter((item) => item.status === "conflicts")
788
+ if (conflicted.length > 0 && !report.dryRun)
789
+ out.push(
790
+ "Resolve the conflicts (edit out the <<<<<<< ||||||| ======= >>>>>>> markers; for a binary file keep yours or replace it with <file>.upstream, then delete <file>.upstream), then run eloxiv update --continue.",
791
+ )
792
+ if (report.migrations.length > 0) {
793
+ out.push(`compatibility migrations:`)
794
+ out.push(...formatMigrationChanges(report.migrations, report.dryRun))
795
+ }
796
+ for (const note of report.notes) out.push(`note: ${note}`)
797
+ if (report.migrations.length > 0) {
798
+ out.push(`compatibility migrations:`)
799
+ out.push(...formatMigrationChanges(report.migrations, report.dryRun))
800
+ }
801
+ for (const note of report.notes) out.push(`note: ${note}`)
802
+ if (report.items.length === 0) out.push("no items installed")
803
+ return out.join("\n")
804
+ }
805
+
806
+ // ---------------------------------------------------------------------------------------------------------
807
+ // update --continue
808
+
809
+ export interface ContinueReport {
810
+ readonly resolved: ReadonlyArray<string>
811
+ readonly unresolved: ReadonlyArray<{ readonly name: string; readonly paths: ReadonlyArray<string> }>
812
+ }
813
+
814
+ /**
815
+ * Record that the conflicts of the last update are resolved. A content conflict is resolved once its file
816
+ * no longer holds conflict markers, a binary one once `<file>.upstream` is gone, and a file deleted locally
817
+ * and changed upstream by whatever the developer chose (restored or still deleted). Any locally edited file
818
+ * of an item that holds conflict markers, recorded or not, keeps the item unresolved. The lock's per-file
819
+ * hashes already describe the new version as published, so a resolved file simply shows as a local edit.
820
+ */
821
+ export const continueUpdate = Effect.fn("Update.continueUpdate")(function* (dir: string, names: ReadonlyArray<string>) {
822
+ const lock = yield* readLock(dir)
823
+ const resolved: Array<string> = []
824
+ const unresolved: Array<{ name: string; paths: Array<string> }> = []
825
+ for (const entry of yield* selectEntries(lock, names)) {
826
+ if (entry.owner === "app" && (entry.conflicts ?? []).length === 0) continue
827
+ const conflicts = entry.conflicts ?? []
828
+ const remaining = new Set(yield* filesWithMarkers(dir, entry))
829
+ for (const conflict of conflicts)
830
+ if (conflict.reason === "binary" && (yield* readAppBytes(dir, upstreamCopy(conflict.path))) !== undefined)
831
+ remaining.add(lockPath(conflict.path))
832
+ if (remaining.size > 0) {
833
+ unresolved.push({ name: entry.name, paths: [...remaining].sort() })
834
+ continue
835
+ }
836
+ if (conflicts.length === 0) continue
837
+ const { conflicts: _resolved, ...rest } = entry
838
+ lock.items[entry.name] = rest
839
+ resolved.push(entry.name)
840
+ }
841
+ if (resolved.length > 0) yield* writeLock(dir, lock)
842
+ return { resolved, unresolved } satisfies ContinueReport
843
+ })
844
+
845
+ export const formatContinue = (report: ContinueReport) => {
846
+ const out = report.resolved.map((name) => `${name}: conflicts resolved`)
847
+ for (const item of report.unresolved)
848
+ for (const path of item.paths)
849
+ out.push(
850
+ `${item.name}: ${path} is not resolved yet (it still has conflict markers, or ${upstreamCopy(path)} is still there)`,
851
+ )
852
+ if (out.length === 0) out.push("no unresolved conflicts")
853
+ return out.join("\n")
854
+ }
855
+
856
+ // ---------------------------------------------------------------------------------------------------------
857
+ // eject
858
+
859
+ /**
860
+ * Take items over (`owner: "app"`): diff and update leave them alone and report them as app-owned. The
861
+ * provenance stays in the lock, so `undo` hands an item back to the registry and the next update merges
862
+ * from the version it was ejected at.
863
+ */
864
+ export const eject = Effect.fn("Update.eject")(function* (dir: string, names: ReadonlyArray<string>, undo: boolean) {
865
+ const lock = yield* readLock(dir)
866
+ const entries = yield* selectEntries(lock, names)
867
+ for (const entry of entries) {
868
+ const { owner: _owner, ...rest } = entry
869
+ lock.items[entry.name] = undo ? rest : { ...rest, owner: "app" }
870
+ }
871
+ yield* writeLock(dir, lock)
872
+ return entries.map((entry) => entry.name)
873
+ })