gitCheckpointStore.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { getAppConfigDir, getWorkspaceHash } from "../../paths.js";
  4. import type { GitCheckpointMeta } from "@zcode/shared";
  5. function isGitCheckpointMeta(value: unknown): value is GitCheckpointMeta {
  6. if (typeof value !== "object" || value === null) {
  7. return false;
  8. }
  9. const candidate = value as Partial<GitCheckpointMeta>;
  10. return (
  11. typeof candidate.checkpointId === "string" &&
  12. typeof candidate.workspacePath === "string" &&
  13. typeof candidate.repoRoot === "string" &&
  14. typeof candidate.workspaceInRepoPath === "string" &&
  15. typeof candidate.createdAt === "number" &&
  16. typeof candidate.refName === "string" &&
  17. typeof candidate.commitOid === "string" &&
  18. candidate.scope === "workspace"
  19. );
  20. }
  21. export class GitCheckpointStore {
  22. constructor(
  23. private readonly options?: {
  24. rootDir?: string;
  25. },
  26. ) {}
  27. private readonly writeChains = new Map<string, Promise<void>>();
  28. private get checkpointsDir(): string {
  29. return this.options?.rootDir ?? join(getAppConfigDir(), "checkpoints");
  30. }
  31. private checkpointDir(workspacePath: string): string {
  32. return join(this.checkpointsDir, getWorkspaceHash(workspacePath));
  33. }
  34. private checkpointFilePath(workspacePath: string, checkpointId: string): string {
  35. return join(this.checkpointDir(workspacePath), `${checkpointId}.json`);
  36. }
  37. private async waitForPendingWrite(filePath: string): Promise<void> {
  38. await this.writeChains.get(filePath)?.catch(() => {});
  39. }
  40. private enqueueWrite<T>(filePath: string, operation: () => Promise<T>): Promise<T> {
  41. const previous = this.writeChains.get(filePath) ?? Promise.resolve();
  42. const result = previous.catch(() => {}).then(operation);
  43. const completion = result.then(
  44. () => undefined,
  45. () => undefined,
  46. );
  47. this.writeChains.set(filePath, completion);
  48. void completion.finally(() => {
  49. if (this.writeChains.get(filePath) === completion) {
  50. this.writeChains.delete(filePath);
  51. }
  52. });
  53. return result;
  54. }
  55. private async writeManifestAtomic(filePath: string, meta: GitCheckpointMeta): Promise<void> {
  56. const tempPath = `${filePath}.${process.pid}.${Date.now().toString(36)}.${Math.random()
  57. .toString(36)
  58. .slice(2, 8)}.tmp`;
  59. await writeFile(tempPath, `${JSON.stringify(meta, null, 2)}\n`, "utf-8");
  60. await rename(tempPath, filePath);
  61. }
  62. async save(meta: GitCheckpointMeta): Promise<void> {
  63. const filePath = this.checkpointFilePath(meta.workspacePath, meta.checkpointId);
  64. await this.enqueueWrite(filePath, async () => {
  65. await mkdir(this.checkpointDir(meta.workspacePath), { recursive: true });
  66. await this.writeManifestAtomic(filePath, meta);
  67. });
  68. }
  69. async load(workspacePath: string, checkpointId: string): Promise<GitCheckpointMeta | null> {
  70. const filePath = this.checkpointFilePath(workspacePath, checkpointId);
  71. try {
  72. await this.waitForPendingWrite(filePath);
  73. const raw = await readFile(filePath, "utf-8");
  74. const parsed = JSON.parse(raw) as unknown;
  75. return isGitCheckpointMeta(parsed) ? parsed : null;
  76. } catch {
  77. return null;
  78. }
  79. }
  80. async delete(workspacePath: string, checkpointId: string): Promise<void> {
  81. const filePath = this.checkpointFilePath(workspacePath, checkpointId);
  82. await this.enqueueWrite(filePath, async () => {
  83. await rm(filePath, { force: true });
  84. });
  85. }
  86. }