~cytrogen/gstack

ref: 9c5f479745acc90533a7ff75a00771b9056c43ef gstack/lib/worktree.ts -rw-r--r-- 9.2 KiB
9c5f4797 — Cytrogen fork: 频率分级路由 + 触发式描述符重写 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * Git worktree manager for isolated test execution with change harvesting.
 *
 * Creates git worktrees for test suites that need real repo context,
 * harvests any changes the test agent makes as patches, and provides
 * deduplication across runs.
 *
 * Reusable platform module — future /batch or /codex challenge skills
 * can import this directly.
 */

import { spawnSync } from 'child_process';
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';

// --- Interfaces ---

export interface WorktreeInfo {
  path: string;
  testName: string;
  originalSha: string;
  createdAt: number;
}

export interface HarvestResult {
  testName: string;
  worktreePath: string;
  diffStat: string;
  patchPath: string;
  changedFiles: string[];
  isDuplicate: boolean;
}

// --- Utility ---

/** Recursive directory copy (pure TypeScript, no external deps). */
function copyDirSync(src: string, dest: string): void {
  fs.mkdirSync(dest, { recursive: true });
  for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
    // Skip symlinks to avoid infinite recursion (e.g., .claude/skills/gstack → repo root)
    if (entry.isSymbolicLink()) continue;
    const srcPath = path.join(src, entry.name);
    const destPath = path.join(dest, entry.name);
    if (entry.isDirectory()) {
      copyDirSync(srcPath, destPath);
    } else {
      fs.copyFileSync(srcPath, destPath);
    }
  }
}

/** Run a git command and return stdout. Throws on failure unless tolerateFailure is set. */
function git(args: string[], cwd: string, tolerateFailure = false): string {
  const result = spawnSync('git', args, { cwd, stdio: 'pipe', timeout: 30_000 });
  const stdout = result.stdout?.toString().trim() ?? '';
  const stderr = result.stderr?.toString().trim() ?? '';
  if (result.status !== 0 && !tolerateFailure) {
    throw new Error(`git ${args.join(' ')} failed (exit ${result.status}): ${stderr || stdout}`);
  }
  return stdout;
}

// --- Dedup index ---

interface DedupIndex {
  hashes: Record<string, string>; // hash → first-seen runId
}

function getDedupPath(): string {
  return path.join(os.homedir(), '.gstack-dev', 'harvests', 'dedup.json');
}

function loadDedupIndex(): DedupIndex {
  try {
    const raw = fs.readFileSync(getDedupPath(), 'utf-8');
    return JSON.parse(raw);
  } catch {
    return { hashes: {} };
  }
}

function saveDedupIndex(index: DedupIndex): void {
  const dir = path.dirname(getDedupPath());
  fs.mkdirSync(dir, { recursive: true });
  const tmp = getDedupPath() + '.tmp';
  fs.writeFileSync(tmp, JSON.stringify(index, null, 2));
  fs.renameSync(tmp, getDedupPath());
}

// --- WorktreeManager ---

export class WorktreeManager {
  private repoRoot: string;
  private runId: string;
  private active: Map<string, WorktreeInfo> = new Map();
  private harvestResults: HarvestResult[] = [];

  constructor(repoRoot?: string) {
    if (repoRoot) {
      this.repoRoot = repoRoot;
    } else {
      this.repoRoot = git(['rev-parse', '--show-toplevel'], process.cwd());
    }
    this.runId = crypto.randomUUID();

    // Register cleanup on process exit
    process.on('exit', () => {
      this.cleanupAll();
    });
  }

  /** Create an isolated worktree. Returns the worktree path. Throws on failure. */
  create(testName: string): string {
    const originalSha = git(['rev-parse', 'HEAD'], this.repoRoot);

    const worktreeBase = path.join(this.repoRoot, '.gstack-worktrees', this.runId);
    fs.mkdirSync(worktreeBase, { recursive: true });

    const worktreePath = path.join(worktreeBase, testName);

    // Create detached worktree at current HEAD
    git(['worktree', 'add', '--detach', worktreePath, 'HEAD'], this.repoRoot);

    // Copy gitignored build artifacts that tests need
    const agentsSrc = path.join(this.repoRoot, '.agents');
    if (fs.existsSync(agentsSrc)) {
      copyDirSync(agentsSrc, path.join(worktreePath, '.agents'));
    }

    const browseDist = path.join(this.repoRoot, 'browse', 'dist');
    if (fs.existsSync(browseDist)) {
      copyDirSync(browseDist, path.join(worktreePath, 'browse', 'dist'));
    }

    const info: WorktreeInfo = {
      path: worktreePath,
      testName,
      originalSha,
      createdAt: Date.now(),
    };
    this.active.set(testName, info);

    return worktreePath;
  }

  /** Harvest changes from a worktree. Returns null if clean or on error. */
  harvest(testName: string): HarvestResult | null {
    const info = this.active.get(testName);
    if (!info) return null;

    try {
      // Check if worktree directory still exists (agent may have deleted it)
      if (!fs.existsSync(info.path)) {
        process.stderr.write(`  HARVEST [${testName}]: worktree dir deleted, skipping\n`);
        return null;
      }

      // Stage everything including untracked files
      git(['-C', info.path, 'add', '-A'], info.path, true);

      // Get diff against original SHA (captures both committed and uncommitted changes)
      const patch = git(['-C', info.path, 'diff', info.originalSha, '--cached'], info.path, true);

      if (!patch) return null;

      // Get diff stat for human-readable output
      const diffStat = git(['-C', info.path, 'diff', info.originalSha, '--cached', '--stat'], info.path, true);

      // Get changed file names
      const nameOnly = git(['-C', info.path, 'diff', info.originalSha, '--cached', '--name-only'], info.path, true);
      const changedFiles = nameOnly.split('\n').filter(Boolean);

      // Dedup check
      const hash = crypto.createHash('sha256').update(patch).digest('hex');
      const dedupIndex = loadDedupIndex();
      const isDuplicate = hash in dedupIndex.hashes;

      let patchPath = '';

      if (!isDuplicate) {
        // Save patch
        const harvestDir = path.join(os.homedir(), '.gstack-dev', 'harvests', this.runId);
        fs.mkdirSync(harvestDir, { recursive: true });
        patchPath = path.join(harvestDir, `${testName}.patch`);
        fs.writeFileSync(patchPath, patch);

        // Update dedup index
        dedupIndex.hashes[hash] = this.runId;
        saveDedupIndex(dedupIndex);
      }

      const result: HarvestResult = {
        testName,
        worktreePath: info.path,
        diffStat,
        patchPath,
        changedFiles,
        isDuplicate,
      };

      this.harvestResults.push(result);
      return result;
    } catch (err) {
      process.stderr.write(`  HARVEST [${testName}]: error — ${err}\n`);
      return null;
    }
  }

  /** Remove a worktree. Non-fatal on error. */
  cleanup(testName: string): void {
    const info = this.active.get(testName);
    if (!info) return;

    try {
      git(['worktree', 'remove', '--force', info.path], this.repoRoot, true);
    } catch {
      // Force remove the directory if git worktree remove fails
      try {
        fs.rmSync(info.path, { recursive: true, force: true });
        git(['worktree', 'prune'], this.repoRoot, true);
      } catch { /* non-fatal */ }
    }

    this.active.delete(testName);
  }

  /** Force-remove all active worktrees (for process exit handler). */
  cleanupAll(): void {
    for (const testName of [...this.active.keys()]) {
      this.cleanup(testName);
    }

    // Clean up the run directory if empty
    const runDir = path.join(this.repoRoot, '.gstack-worktrees', this.runId);
    try {
      const entries = fs.readdirSync(runDir);
      if (entries.length === 0) {
        fs.rmdirSync(runDir);
      }
    } catch { /* non-fatal */ }
  }

  /** Remove worktrees from previous runs that weren't cleaned up. */
  pruneStale(): void {
    try {
      git(['worktree', 'prune'], this.repoRoot, true);

      const worktreeBase = path.join(this.repoRoot, '.gstack-worktrees');
      if (!fs.existsSync(worktreeBase)) return;

      for (const entry of fs.readdirSync(worktreeBase)) {
        // Don't prune our own run
        if (entry === this.runId) continue;

        const entryPath = path.join(worktreeBase, entry);
        try {
          fs.rmSync(entryPath, { recursive: true, force: true });
        } catch { /* non-fatal */ }
      }
    } catch {
      process.stderr.write('  WORKTREE: prune failed (non-fatal)\n');
    }
  }

  /** Print harvest report summary. */
  printReport(): void {
    if (this.harvestResults.length === 0) return;

    const nonDuplicates = this.harvestResults.filter(r => !r.isDuplicate);
    process.stderr.write('\n=== HARVEST REPORT ===\n');
    process.stderr.write(`${nonDuplicates.length} of ${this.harvestResults.length} test suites produced new changes:\n\n`);

    for (const result of this.harvestResults) {
      if (result.isDuplicate) {
        process.stderr.write(`  ${result.testName}: duplicate patch (skipped)\n`);
      } else {
        process.stderr.write(`  ${result.testName}: ${result.changedFiles.length} files changed\n`);
        process.stderr.write(`    Patch: ${result.patchPath}\n`);
        process.stderr.write(`    Apply: git apply ${result.patchPath}\n`);
        if (result.diffStat) {
          process.stderr.write(`    ${result.diffStat}\n`);
        }
      }
      process.stderr.write('\n');
    }
  }

  /** Get the run ID (for testing). */
  getRunId(): string {
    return this.runId;
  }

  /** Get active worktree info (for testing). */
  getInfo(testName: string): WorktreeInfo | undefined {
    return this.active.get(testName);
  }
}