-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathoperation-manager.ts
More file actions
158 lines (139 loc) · 4.34 KB
/
operation-manager.ts
File metadata and controls
158 lines (139 loc) · 4.34 KB
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
import { createGitClient, type GitClient } from "./client";
import { removeLock, waitForUnlock } from "./lock-detector";
import { AsyncReaderWriterLock } from "./rw-lock";
/**
* Returns process.env with Electron/Chromium variables cleaned so that
* child processes spawned by git hooks (e.g. biome via lint-staged) don't
* crash trying to initialise GPU subsystems.
*
* The agent service symlinks `node → Electron binary` and prepends it to
* PATH. If ELECTRON_RUN_AS_NODE is missing, that binary starts as a full
* Chromium browser (GPU init → SIGTRAP crash). We strip most ELECTRON_/
* CHROME_ vars but explicitly keep ELECTRON_RUN_AS_NODE=1 so any such
* shim still behaves as plain Node.js.
*/
function getCleanEnv(): Record<string, string> {
const env: Record<string, string> = {};
for (const [key, value] of Object.entries(process.env)) {
if (value === undefined) continue;
if (key === "ELECTRON_RUN_AS_NODE") continue;
if (key.startsWith("ELECTRON_") || key.startsWith("CHROME_")) continue;
env[key] = value;
}
env.ELECTRON_RUN_AS_NODE = "1";
return env;
}
interface RepoState {
lock: AsyncReaderWriterLock;
client: GitClient;
lastAccess: number;
}
export interface ExecuteOptions {
signal?: AbortSignal;
timeoutMs?: number;
waitForExternalLock?: boolean;
}
class GitOperationManagerImpl {
private repoStates = new Map<string, RepoState>();
private cleanupInterval: ReturnType<typeof setInterval> | null = null;
private static readonly CLEANUP_INTERVAL_MS = 60000;
private static readonly IDLE_TIMEOUT_MS = 300000;
constructor() {
this.cleanupInterval = setInterval(
() => this.cleanupIdleRepos(),
GitOperationManagerImpl.CLEANUP_INTERVAL_MS,
);
}
private getRepoState(repoPath: string): RepoState {
let state = this.repoStates.get(repoPath);
if (!state) {
state = {
lock: new AsyncReaderWriterLock(),
client: createGitClient(repoPath),
lastAccess: Date.now(),
};
this.repoStates.set(repoPath, state);
}
state.lastAccess = Date.now();
return state;
}
private cleanupIdleRepos(): void {
const now = Date.now();
for (const [repoPath, state] of this.repoStates) {
if (now - state.lastAccess > GitOperationManagerImpl.IDLE_TIMEOUT_MS) {
this.repoStates.delete(repoPath);
}
}
}
async executeRead<T>(
repoPath: string,
operation: (git: GitClient) => Promise<T>,
options?: ExecuteOptions,
): Promise<T> {
const state = this.getRepoState(repoPath);
if (options?.signal) {
const scopedGit = createGitClient(repoPath, {
abortSignal: options.signal,
});
return operation(
scopedGit.env({ ...getCleanEnv(), GIT_OPTIONAL_LOCKS: "0" }),
);
}
const git = state.client.env({ ...getCleanEnv(), GIT_OPTIONAL_LOCKS: "0" });
return operation(git);
}
async executeWrite<T>(
repoPath: string,
operation: (git: GitClient) => Promise<T>,
options?: ExecuteOptions,
): Promise<T> {
const state = this.getRepoState(repoPath);
if (options?.waitForExternalLock !== false) {
const unlocked = await waitForUnlock(
repoPath,
options?.timeoutMs ?? 10000,
);
if (!unlocked) {
throw new Error(`Git repository is locked: ${repoPath}`);
}
}
await state.lock.acquireWrite();
try {
if (options?.signal) {
const scopedGit = createGitClient(repoPath, {
abortSignal: options.signal,
});
return await operation(scopedGit.env(getCleanEnv()));
}
return await operation(state.client.env(getCleanEnv()));
} catch (error) {
if (options?.signal?.aborted) {
await removeLock(repoPath).catch(() => {});
}
throw error;
} finally {
state.lock.releaseWrite();
}
}
destroy(): void {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
this.cleanupInterval = null;
}
this.repoStates.clear();
}
}
let instance: GitOperationManagerImpl | null = null;
export function getGitOperationManager(): GitOperationManagerImpl {
if (!instance) {
instance = new GitOperationManagerImpl();
}
return instance;
}
export function resetGitOperationManager(): void {
if (instance) {
instance.destroy();
instance = null;
}
}
export type GitOperationManager = GitOperationManagerImpl;