service.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { NodeChildProcessSpawner, NodeFileSystem, NodePath } from "@effect/platform-node"
  2. import { Cause, Duration, Effect, Layer, Schedule, ServiceMap, Stream } from "effect"
  3. import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
  4. import path from "path"
  5. import z from "zod"
  6. import { InstanceContext } from "@/effect/instance-context"
  7. import { AppFileSystem } from "@/filesystem"
  8. import { Config } from "../config/config"
  9. import { Global } from "../global"
  10. import { Log } from "../util/log"
  11. export namespace Snapshot {
  12. export const Patch = z.object({
  13. hash: z.string(),
  14. files: z.string().array(),
  15. })
  16. export type Patch = z.infer<typeof Patch>
  17. export const FileDiff = z
  18. .object({
  19. file: z.string(),
  20. before: z.string(),
  21. after: z.string(),
  22. additions: z.number(),
  23. deletions: z.number(),
  24. status: z.enum(["added", "deleted", "modified"]).optional(),
  25. })
  26. .meta({
  27. ref: "FileDiff",
  28. })
  29. export type FileDiff = z.infer<typeof FileDiff>
  30. const log = Log.create({ service: "snapshot" })
  31. const prune = "7.days"
  32. const core = ["-c", "core.longpaths=true", "-c", "core.symlinks=true"]
  33. const cfg = ["-c", "core.autocrlf=false", ...core]
  34. const quote = [...cfg, "-c", "core.quotepath=false"]
  35. interface GitResult {
  36. readonly code: ChildProcessSpawner.ExitCode
  37. readonly text: string
  38. readonly stderr: string
  39. }
  40. export interface Interface {
  41. readonly cleanup: () => Effect.Effect<void>
  42. readonly track: () => Effect.Effect<string | undefined>
  43. readonly patch: (hash: string) => Effect.Effect<Snapshot.Patch>
  44. readonly restore: (snapshot: string) => Effect.Effect<void>
  45. readonly revert: (patches: Snapshot.Patch[]) => Effect.Effect<void>
  46. readonly diff: (hash: string) => Effect.Effect<string>
  47. readonly diffFull: (from: string, to: string) => Effect.Effect<Snapshot.FileDiff[]>
  48. }
  49. export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/Snapshot") {}
  50. export const layer: Layer.Layer<
  51. Service,
  52. never,
  53. InstanceContext | AppFileSystem.Service | ChildProcessSpawner.ChildProcessSpawner
  54. > = Layer.effect(
  55. Service,
  56. Effect.gen(function* () {
  57. const ctx = yield* InstanceContext
  58. const fs = yield* AppFileSystem.Service
  59. const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
  60. const directory = ctx.directory
  61. const worktree = ctx.worktree
  62. const project = ctx.project
  63. const gitdir = path.join(Global.Path.data, "snapshot", project.id)
  64. const args = (cmd: string[]) => ["--git-dir", gitdir, "--work-tree", worktree, ...cmd]
  65. const git = Effect.fnUntraced(
  66. function* (cmd: string[], opts?: { cwd?: string; env?: Record<string, string> }) {
  67. const proc = ChildProcess.make("git", cmd, {
  68. cwd: opts?.cwd,
  69. env: opts?.env,
  70. extendEnv: true,
  71. })
  72. const handle = yield* spawner.spawn(proc)
  73. const [text, stderr] = yield* Effect.all(
  74. [Stream.mkString(Stream.decodeText(handle.stdout)), Stream.mkString(Stream.decodeText(handle.stderr))],
  75. { concurrency: 2 },
  76. )
  77. const code = yield* handle.exitCode
  78. return { code, text, stderr } satisfies GitResult
  79. },
  80. Effect.scoped,
  81. Effect.catch((err) =>
  82. Effect.succeed({
  83. code: ChildProcessSpawner.ExitCode(1),
  84. text: "",
  85. stderr: String(err),
  86. }),
  87. ),
  88. )
  89. // Snapshot-specific error handling on top of AppFileSystem
  90. const exists = (file: string) => fs.exists(file).pipe(Effect.orDie)
  91. const read = (file: string) => fs.readFileString(file).pipe(Effect.catch(() => Effect.succeed("")))
  92. const remove = (file: string) => fs.remove(file).pipe(Effect.catch(() => Effect.void))
  93. const enabled = Effect.fnUntraced(function* () {
  94. if (project.vcs !== "git") return false
  95. return (yield* Effect.promise(() => Config.get())).snapshot !== false
  96. })
  97. const excludes = Effect.fnUntraced(function* () {
  98. const result = yield* git(["rev-parse", "--path-format=absolute", "--git-path", "info/exclude"], {
  99. cwd: worktree,
  100. })
  101. const file = result.text.trim()
  102. if (!file) return
  103. if (!(yield* exists(file))) return
  104. return file
  105. })
  106. const sync = Effect.fnUntraced(function* () {
  107. const file = yield* excludes()
  108. const target = path.join(gitdir, "info", "exclude")
  109. yield* fs.ensureDir(path.join(gitdir, "info")).pipe(Effect.orDie)
  110. if (!file) {
  111. yield* fs.writeFileString(target, "").pipe(Effect.orDie)
  112. return
  113. }
  114. yield* fs.writeFileString(target, yield* read(file)).pipe(Effect.orDie)
  115. })
  116. const add = Effect.fnUntraced(function* () {
  117. yield* sync()
  118. yield* git([...cfg, ...args(["add", "."])], { cwd: directory })
  119. })
  120. const cleanup = Effect.fn("Snapshot.cleanup")(function* () {
  121. if (!(yield* enabled())) return
  122. if (!(yield* exists(gitdir))) return
  123. const result = yield* git(args(["gc", `--prune=${prune}`]), { cwd: directory })
  124. if (result.code !== 0) {
  125. log.warn("cleanup failed", {
  126. exitCode: result.code,
  127. stderr: result.stderr,
  128. })
  129. return
  130. }
  131. log.info("cleanup", { prune })
  132. })
  133. const track = Effect.fn("Snapshot.track")(function* () {
  134. if (!(yield* enabled())) return
  135. const existed = yield* exists(gitdir)
  136. yield* fs.ensureDir(gitdir).pipe(Effect.orDie)
  137. if (!existed) {
  138. yield* git(["init"], {
  139. env: { GIT_DIR: gitdir, GIT_WORK_TREE: worktree },
  140. })
  141. yield* git(["--git-dir", gitdir, "config", "core.autocrlf", "false"])
  142. yield* git(["--git-dir", gitdir, "config", "core.longpaths", "true"])
  143. yield* git(["--git-dir", gitdir, "config", "core.symlinks", "true"])
  144. yield* git(["--git-dir", gitdir, "config", "core.fsmonitor", "false"])
  145. log.info("initialized")
  146. }
  147. yield* add()
  148. const result = yield* git(args(["write-tree"]), { cwd: directory })
  149. const hash = result.text.trim()
  150. log.info("tracking", { hash, cwd: directory, git: gitdir })
  151. return hash
  152. })
  153. const patch = Effect.fn("Snapshot.patch")(function* (hash: string) {
  154. yield* add()
  155. const result = yield* git([...quote, ...args(["diff", "--no-ext-diff", "--name-only", hash, "--", "."])], {
  156. cwd: directory,
  157. })
  158. if (result.code !== 0) {
  159. log.warn("failed to get diff", { hash, exitCode: result.code })
  160. return { hash, files: [] }
  161. }
  162. return {
  163. hash,
  164. files: result.text
  165. .trim()
  166. .split("\n")
  167. .map((x) => x.trim())
  168. .filter(Boolean)
  169. .map((x) => path.join(worktree, x).replaceAll("\\", "/")),
  170. }
  171. })
  172. const restore = Effect.fn("Snapshot.restore")(function* (snapshot: string) {
  173. log.info("restore", { commit: snapshot })
  174. const result = yield* git([...core, ...args(["read-tree", snapshot])], { cwd: worktree })
  175. if (result.code === 0) {
  176. const checkout = yield* git([...core, ...args(["checkout-index", "-a", "-f"])], { cwd: worktree })
  177. if (checkout.code === 0) return
  178. log.error("failed to restore snapshot", {
  179. snapshot,
  180. exitCode: checkout.code,
  181. stderr: checkout.stderr,
  182. })
  183. return
  184. }
  185. log.error("failed to restore snapshot", {
  186. snapshot,
  187. exitCode: result.code,
  188. stderr: result.stderr,
  189. })
  190. })
  191. const revert = Effect.fn("Snapshot.revert")(function* (patches: Snapshot.Patch[]) {
  192. const seen = new Set<string>()
  193. for (const item of patches) {
  194. for (const file of item.files) {
  195. if (seen.has(file)) continue
  196. seen.add(file)
  197. log.info("reverting", { file, hash: item.hash })
  198. const result = yield* git([...core, ...args(["checkout", item.hash, "--", file])], { cwd: worktree })
  199. if (result.code !== 0) {
  200. const rel = path.relative(worktree, file)
  201. const tree = yield* git([...core, ...args(["ls-tree", item.hash, "--", rel])], { cwd: worktree })
  202. if (tree.code === 0 && tree.text.trim()) {
  203. log.info("file existed in snapshot but checkout failed, keeping", { file })
  204. } else {
  205. log.info("file did not exist in snapshot, deleting", { file })
  206. yield* remove(file)
  207. }
  208. }
  209. }
  210. }
  211. })
  212. const diff = Effect.fn("Snapshot.diff")(function* (hash: string) {
  213. yield* add()
  214. const result = yield* git([...quote, ...args(["diff", "--no-ext-diff", hash, "--", "."])], {
  215. cwd: worktree,
  216. })
  217. if (result.code !== 0) {
  218. log.warn("failed to get diff", {
  219. hash,
  220. exitCode: result.code,
  221. stderr: result.stderr,
  222. })
  223. return ""
  224. }
  225. return result.text.trim()
  226. })
  227. const diffFull = Effect.fn("Snapshot.diffFull")(function* (from: string, to: string) {
  228. const result: Snapshot.FileDiff[] = []
  229. const status = new Map<string, "added" | "deleted" | "modified">()
  230. const statuses = yield* git(
  231. [...quote, ...args(["diff", "--no-ext-diff", "--name-status", "--no-renames", from, to, "--", "."])],
  232. { cwd: directory },
  233. )
  234. for (const line of statuses.text.trim().split("\n")) {
  235. if (!line) continue
  236. const [code, file] = line.split("\t")
  237. if (!code || !file) continue
  238. status.set(file, code.startsWith("A") ? "added" : code.startsWith("D") ? "deleted" : "modified")
  239. }
  240. const numstat = yield* git(
  241. [...quote, ...args(["diff", "--no-ext-diff", "--no-renames", "--numstat", from, to, "--", "."])],
  242. {
  243. cwd: directory,
  244. },
  245. )
  246. for (const line of numstat.text.trim().split("\n")) {
  247. if (!line) continue
  248. const [adds, dels, file] = line.split("\t")
  249. if (!file) continue
  250. const binary = adds === "-" && dels === "-"
  251. const [before, after] = binary
  252. ? ["", ""]
  253. : yield* Effect.all(
  254. [
  255. git([...cfg, ...args(["show", `${from}:${file}`])]).pipe(Effect.map((item) => item.text)),
  256. git([...cfg, ...args(["show", `${to}:${file}`])]).pipe(Effect.map((item) => item.text)),
  257. ],
  258. { concurrency: 2 },
  259. )
  260. const additions = binary ? 0 : parseInt(adds)
  261. const deletions = binary ? 0 : parseInt(dels)
  262. result.push({
  263. file,
  264. before,
  265. after,
  266. additions: Number.isFinite(additions) ? additions : 0,
  267. deletions: Number.isFinite(deletions) ? deletions : 0,
  268. status: status.get(file) ?? "modified",
  269. })
  270. }
  271. return result
  272. })
  273. yield* cleanup().pipe(
  274. Effect.catchCause((cause) => {
  275. log.error("cleanup loop failed", { cause: Cause.pretty(cause) })
  276. return Effect.void
  277. }),
  278. Effect.repeat(Schedule.spaced(Duration.hours(1))),
  279. Effect.delay(Duration.minutes(1)),
  280. Effect.forkScoped,
  281. )
  282. return Service.of({ cleanup, track, patch, restore, revert, diff, diffFull })
  283. }),
  284. ).pipe(Layer.fresh)
  285. export const defaultLayer = layer.pipe(
  286. Layer.provide(NodeChildProcessSpawner.layer),
  287. Layer.provide(AppFileSystem.defaultLayer),
  288. Layer.provide(NodeFileSystem.layer), // needed by NodeChildProcessSpawner
  289. Layer.provide(NodePath.layer),
  290. )
  291. }