farming-code 2.2.28 → 2.2.30
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.
- package/README.md +27 -90
- package/README.zh_cn.md +17 -90
- package/THIRD_PARTY_NOTICES.md +25 -6
- package/backend/acp/packaged-claude-acp.js +13 -0
- package/backend/acp/packaged-codex-acp.js +2 -2
- package/backend/acp-runtime.js +38 -17
- package/backend/acp-session-state.js +78 -88
- package/backend/acp-transcript.js +174 -9
- package/backend/agent-manager.js +27 -2
- package/backend/agent-session-history.js +45 -12
- package/backend/codex-session-history.js +1 -1
- package/backend/codex-transcript.js +0 -202
- package/backend/config-manager.js +44 -16
- package/backend/data/LICENSE.agent-browser +201 -0
- package/backend/data/runtime-dependency-manifest.json +199 -0
- package/backend/data/runtime-dependency-sources.json +5 -0
- package/backend/executable-discovery.js +7 -0
- package/backend/farming-agent-bootstrap.zh_cn.md +4 -2
- package/backend/farming-app-cli.js +161 -65
- package/backend/farming-cli.js +7 -59
- package/backend/index-html.js +31 -0
- package/backend/main-agent-skills.js +7 -28
- package/backend/npm-update-helper.js +43 -6
- package/backend/provider-adapters.js +10 -0
- package/backend/runtime-dependency-manager.js +759 -0
- package/backend/runtime-executable-invocation.js +20 -0
- package/backend/server-process-identity.js +47 -0
- package/backend/server.js +145 -204
- package/backend/slash-command-discovery.js +138 -1
- package/backend/storage-layout.js +30 -0
- package/backend/update-service.js +60 -914
- package/config/farming.deploy.env.example +1 -1
- package/config/farming.install.env.example +5 -1
- package/dist/acp/LICENSE.claude-agent-acp +191 -0
- package/dist/acp/LICENSE.claude-agent-sdk +1 -0
- package/dist/acp/claude-agent-acp-0.59.0.mjs +41606 -0
- package/dist/acp/{codex-acp-1.1.4.js → codex-acp-1.1.4.mjs} +387 -7
- package/dist/assets/{App-CM7Cy-l4.css → App-BKNUILuL.css} +1 -1
- package/dist/assets/App-Z1N7T0eM.js +695 -0
- package/dist/assets/{FileEditorMarkdownPreview-DYOMm-b1.js → FileEditorMarkdownPreview-Cs9cLv8a.js} +1 -1
- package/dist/assets/FileEditorPane-CNtuGjQ3.js +2 -0
- package/dist/assets/IconGlyphs-BClgag6E.js +1 -0
- package/dist/assets/ProjectFilesSection-K3GHuQ4U.js +12 -0
- package/dist/assets/ReviewPage-BRuxFaH5.js +3 -0
- package/dist/assets/code-dark-kLsQLvpf.css +1 -0
- package/dist/assets/{index-BzQO3pUr.js → index-D-zetSol.js} +2 -2
- package/dist/assets/main-DUTz-Rd2.css +1 -0
- package/dist/assets/{workspace-editor-monaco-tXA-udGx.js → workspace-editor-monaco-CJNwbpCy.js} +1 -1
- package/dist/assets/workspace-editor-monaco-CO9w8YV0.js +1 -0
- package/dist/assets/workspace-view-state-BjKPYp0E.js +7 -0
- package/dist/index.html +33 -3
- package/extensions/browser/backend/agent-browser-runtime.js +1041 -0
- package/extensions/browser/backend/browser-mcp-server.js +352 -0
- package/extensions/browser/backend/browser-resource-manager.js +921 -114
- package/extensions/browser/backend/browser-resource-store.js +47 -6
- package/extensions/browser/backend/browser-router.js +15 -2
- package/extensions/browser/backend/executable-discovery.js +164 -17
- package/extensions/browser/backend/managed-chromium-installer.js +593 -0
- package/extensions/browser/bin/farming-browser +384 -30
- package/extensions/browser/frontend/BrowserSidebarPortals.tsx +17 -19
- package/extensions/browser/frontend/BrowserViewer.tsx +217 -46
- package/extensions/browser/frontend/browser.css +68 -17
- package/extensions/browser/frontend/types.ts +13 -0
- package/extensions/browser/frontend/useBrowserResources.ts +18 -3
- package/index.html +32 -2
- package/package.json +10 -6
- package/shared/backend-connection-status.d.ts +12 -0
- package/shared/backend-connection-status.js +35 -4
- package/backend/agent-activity.js +0 -35
- package/backend/agent-memory-report.js +0 -583
- package/backend/bundle-update-helper.js +0 -113
- package/backend/codex-rollout-follower.js +0 -326
- package/dist/assets/App-CPG4WcjO.js +0 -658
- package/dist/assets/FileEditorPane-DXXq82aM.js +0 -2
- package/dist/assets/IconGlyphs-DrdPjYXr.js +0 -1
- package/dist/assets/ProjectFilesSection-CP66XVPl.js +0 -12
- package/dist/assets/ReviewPage-w21MEPD6.js +0 -3
- package/dist/assets/code-dark-BDjwj7Um.css +0 -1
- package/dist/assets/main-BlVUXfxa.css +0 -1
- package/dist/assets/workspace-editor-monaco-CFEfFNcA.js +0 -1
- package/dist/assets/workspace-view-state-BS_V8JVs.js +0 -7
- package/extensions/browser/backend/cdp-browser-runtime.js +0 -648
- package/extensions/browser/backend/cdp-client.js +0 -167
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { execFile, spawn } = require('child_process');
|
|
5
|
+
const storageLayout = require('../../../backend/storage-layout');
|
|
6
|
+
const {
|
|
7
|
+
runtimePlatformKey,
|
|
8
|
+
verifyExecutable,
|
|
9
|
+
} = require('../../../backend/runtime-dependency-manager');
|
|
10
|
+
const {
|
|
11
|
+
runtimeExecutableInvocation,
|
|
12
|
+
} = require('../../../backend/runtime-executable-invocation');
|
|
13
|
+
const {
|
|
14
|
+
AGENT_BROWSER_VERSION,
|
|
15
|
+
} = require('./agent-browser-runtime');
|
|
16
|
+
const {
|
|
17
|
+
managedAgentBrowserPath,
|
|
18
|
+
} = require('./executable-discovery');
|
|
19
|
+
|
|
20
|
+
const MANIFEST_FORMAT = 'farming-managed-chromium-v1';
|
|
21
|
+
const INSTALL_TIMEOUT_MS = 15 * 60_000;
|
|
22
|
+
const LOCK_TIMEOUT_MS = 16 * 60_000;
|
|
23
|
+
const LOCK_STALE_MS = 20 * 60_000;
|
|
24
|
+
const LOCK_POLL_MS = 200;
|
|
25
|
+
const MAX_INSTALL_OUTPUT_BYTES = 256 * 1024;
|
|
26
|
+
|
|
27
|
+
function delay(ms) {
|
|
28
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function readJson(filePath) {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function writeJsonAtomic(filePath, value) {
|
|
40
|
+
const temporary = `${filePath}.${process.pid}.${crypto.randomUUID()}.tmp`;
|
|
41
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
42
|
+
fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, {
|
|
43
|
+
flag: 'wx',
|
|
44
|
+
mode: 0o600,
|
|
45
|
+
});
|
|
46
|
+
try {
|
|
47
|
+
try {
|
|
48
|
+
fs.renameSync(temporary, filePath);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (!['EEXIST', 'EPERM'].includes(error?.code) || !fs.existsSync(filePath)) throw error;
|
|
51
|
+
fs.rmSync(filePath, { force: true });
|
|
52
|
+
fs.renameSync(temporary, filePath);
|
|
53
|
+
}
|
|
54
|
+
} finally {
|
|
55
|
+
fs.rmSync(temporary, { force: true });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function processRunning(pid) {
|
|
60
|
+
const value = Number(pid);
|
|
61
|
+
if (!Number.isSafeInteger(value) || value <= 0) return false;
|
|
62
|
+
try {
|
|
63
|
+
process.kill(value, 0);
|
|
64
|
+
return true;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return error?.code === 'EPERM' || error?.code === 'EACCES';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function pathInside(root, candidate) {
|
|
71
|
+
const relative = path.relative(path.resolve(root), path.resolve(candidate));
|
|
72
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function browserExecutableName(platform) {
|
|
76
|
+
if (platform === 'darwin') return 'Google Chrome for Testing';
|
|
77
|
+
if (platform === 'win32') return 'chrome.exe';
|
|
78
|
+
return 'chrome';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function findBrowserExecutable(rootDir, options = {}) {
|
|
82
|
+
const platform = options.platform || process.platform;
|
|
83
|
+
const expectedName = browserExecutableName(platform);
|
|
84
|
+
const stack = [{ directory: rootDir, depth: 0 }];
|
|
85
|
+
let visited = 0;
|
|
86
|
+
while (stack.length) {
|
|
87
|
+
const { directory, depth } = stack.pop();
|
|
88
|
+
if (depth > 8 || visited > 30_000) continue;
|
|
89
|
+
let entries;
|
|
90
|
+
try {
|
|
91
|
+
entries = fs.readdirSync(directory, { withFileTypes: true });
|
|
92
|
+
} catch {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
for (const entry of entries) {
|
|
96
|
+
visited += 1;
|
|
97
|
+
const candidate = path.join(directory, entry.name);
|
|
98
|
+
if (entry.isDirectory()) {
|
|
99
|
+
if (!['.home', '.xdg'].includes(entry.name)) {
|
|
100
|
+
stack.push({ directory: candidate, depth: depth + 1 });
|
|
101
|
+
}
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (!entry.isFile() || entry.name !== expectedName) continue;
|
|
105
|
+
if (platform === 'darwin' && !candidate.includes('.app/Contents/MacOS/')) continue;
|
|
106
|
+
try {
|
|
107
|
+
fs.accessSync(candidate, fs.constants.X_OK);
|
|
108
|
+
return candidate;
|
|
109
|
+
} catch {
|
|
110
|
+
// Keep looking for a complete executable.
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return '';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function appendBounded(current, chunk) {
|
|
118
|
+
const next = `${current}${String(chunk || '')}`;
|
|
119
|
+
return next.length > MAX_INSTALL_OUTPUT_BYTES
|
|
120
|
+
? next.slice(next.length - MAX_INSTALL_OUTPUT_BYTES)
|
|
121
|
+
: next;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function killInstallProcess(child, platform) {
|
|
125
|
+
if (!child?.pid) return;
|
|
126
|
+
try {
|
|
127
|
+
if (platform !== 'win32') process.kill(-child.pid, 'SIGKILL');
|
|
128
|
+
else child.kill('SIGKILL');
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (error?.code !== 'ESRCH') child.kill?.('SIGKILL');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function defaultRunInstallCommand(executablePath, args, options = {}) {
|
|
135
|
+
const env = options.env || process.env;
|
|
136
|
+
const platform = options.platform || process.platform;
|
|
137
|
+
const invocation = runtimeExecutableInvocation(executablePath, args, env, platform);
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
const child = spawn(invocation.command, invocation.args, {
|
|
140
|
+
detached: platform !== 'win32',
|
|
141
|
+
env,
|
|
142
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
143
|
+
windowsHide: true,
|
|
144
|
+
});
|
|
145
|
+
let stdout = '';
|
|
146
|
+
let stderr = '';
|
|
147
|
+
let settled = false;
|
|
148
|
+
let timedOut = false;
|
|
149
|
+
let exitProofTimeout = null;
|
|
150
|
+
let ownershipError = null;
|
|
151
|
+
const timeoutMs = options.timeoutMs || INSTALL_TIMEOUT_MS;
|
|
152
|
+
const timeout = setTimeout(() => {
|
|
153
|
+
if (settled) return;
|
|
154
|
+
timedOut = true;
|
|
155
|
+
killInstallProcess(child, platform);
|
|
156
|
+
exitProofTimeout = setTimeout(() => {
|
|
157
|
+
if (settled) return;
|
|
158
|
+
const error = new Error(
|
|
159
|
+
`Chromium installation timed out and process ${child.pid} exit could not be proven`,
|
|
160
|
+
);
|
|
161
|
+
error.code = 'CHROMIUM_INSTALL_EXIT_UNPROVEN';
|
|
162
|
+
error.cleanupUnproven = true;
|
|
163
|
+
settled = true;
|
|
164
|
+
reject(error);
|
|
165
|
+
}, 30_000);
|
|
166
|
+
exitProofTimeout.unref?.();
|
|
167
|
+
}, timeoutMs);
|
|
168
|
+
timeout.unref?.();
|
|
169
|
+
child.stdout?.on('data', chunk => {
|
|
170
|
+
stdout = appendBounded(stdout, chunk);
|
|
171
|
+
options.onOutput?.(String(chunk));
|
|
172
|
+
});
|
|
173
|
+
child.stderr?.on('data', chunk => {
|
|
174
|
+
stderr = appendBounded(stderr, chunk);
|
|
175
|
+
options.onOutput?.(String(chunk));
|
|
176
|
+
});
|
|
177
|
+
child.once('error', error => {
|
|
178
|
+
if (settled) return;
|
|
179
|
+
settled = true;
|
|
180
|
+
clearTimeout(timeout);
|
|
181
|
+
clearTimeout(exitProofTimeout);
|
|
182
|
+
reject(ownershipError || error);
|
|
183
|
+
});
|
|
184
|
+
child.once('exit', (code, signal) => {
|
|
185
|
+
if (settled) return;
|
|
186
|
+
settled = true;
|
|
187
|
+
clearTimeout(timeout);
|
|
188
|
+
clearTimeout(exitProofTimeout);
|
|
189
|
+
if (ownershipError) {
|
|
190
|
+
reject(ownershipError);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (timedOut) {
|
|
194
|
+
const error = new Error(`Chromium installation timed out after ${timeoutMs} ms`);
|
|
195
|
+
error.code = 'CHROMIUM_INSTALL_TIMEOUT';
|
|
196
|
+
reject(error);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (code === 0) {
|
|
200
|
+
resolve({ stdout, stderr });
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const detail = String(stderr || stdout || '').trim().slice(-2_000);
|
|
204
|
+
const error = new Error(
|
|
205
|
+
detail || `agent-browser install exited with ${signal || `code ${code}`}`,
|
|
206
|
+
);
|
|
207
|
+
error.code = 'CHROMIUM_INSTALL_FAILED';
|
|
208
|
+
reject(error);
|
|
209
|
+
});
|
|
210
|
+
try {
|
|
211
|
+
options.onSpawn?.(child.pid);
|
|
212
|
+
} catch (error) {
|
|
213
|
+
ownershipError = error;
|
|
214
|
+
killInstallProcess(child, platform);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function defaultVerifyBrowser(executablePath, options = {}) {
|
|
220
|
+
return new Promise((resolve, reject) => {
|
|
221
|
+
execFile(executablePath, ['--version'], {
|
|
222
|
+
encoding: 'utf8',
|
|
223
|
+
env: options.env || process.env,
|
|
224
|
+
timeout: 10_000,
|
|
225
|
+
maxBuffer: 64 * 1024,
|
|
226
|
+
windowsHide: true,
|
|
227
|
+
}, (error, stdout, stderr) => {
|
|
228
|
+
if (error) {
|
|
229
|
+
reject(new Error(
|
|
230
|
+
String(stderr || '').trim()
|
|
231
|
+
|| error.message
|
|
232
|
+
|| 'The installed Chromium executable could not start',
|
|
233
|
+
{ cause: error },
|
|
234
|
+
));
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const version = String(stdout || stderr || '').trim();
|
|
238
|
+
if (!/Chrom(?:e|ium)/i.test(version)) {
|
|
239
|
+
reject(new Error('The installed Chromium executable did not report a browser version'));
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
resolve(version);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
class ManagedChromiumInstaller {
|
|
248
|
+
constructor(options = {}) {
|
|
249
|
+
this.configDir = options.configDir;
|
|
250
|
+
this.platform = options.platform || process.platform;
|
|
251
|
+
this.arch = options.arch || process.arch;
|
|
252
|
+
this.platformKey = options.platformKey || runtimePlatformKey({
|
|
253
|
+
platform: this.platform,
|
|
254
|
+
arch: this.arch,
|
|
255
|
+
musl: options.musl,
|
|
256
|
+
});
|
|
257
|
+
this.agentBrowserVersion = options.agentBrowserVersion || AGENT_BROWSER_VERSION;
|
|
258
|
+
this.env = options.env || process.env;
|
|
259
|
+
this.agentBrowserPath = options.agentBrowserPath
|
|
260
|
+
|| (() => managedAgentBrowserPath({ env: this.env }));
|
|
261
|
+
this.runInstallCommand = options.runInstallCommand || defaultRunInstallCommand;
|
|
262
|
+
this.verifyBrowser = options.verifyBrowser || defaultVerifyBrowser;
|
|
263
|
+
this.verifyAgentBrowser = options.verifyAgentBrowser
|
|
264
|
+
|| (executablePath => verifyExecutable(executablePath, this.agentBrowserVersion, {
|
|
265
|
+
env: this.env,
|
|
266
|
+
platform: this.platform,
|
|
267
|
+
useConfiguredLoader: true,
|
|
268
|
+
}));
|
|
269
|
+
this.wait = options.wait || delay;
|
|
270
|
+
this.installPromise = null;
|
|
271
|
+
this.lastFailure = '';
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
rootDir() {
|
|
275
|
+
return storageLayout.managedChromiumRootDir(this.configDir);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
targetDir() {
|
|
279
|
+
return storageLayout.managedChromiumVersionDir(
|
|
280
|
+
this.configDir,
|
|
281
|
+
this.agentBrowserVersion,
|
|
282
|
+
this.platformKey,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
manifestFile(directory = this.targetDir()) {
|
|
287
|
+
return path.join(directory, 'install.json');
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
readValidManifest(directory = this.targetDir()) {
|
|
291
|
+
const manifest = readJson(this.manifestFile(directory));
|
|
292
|
+
if (
|
|
293
|
+
manifest?.format !== MANIFEST_FORMAT
|
|
294
|
+
|| manifest.agentBrowserVersion !== this.agentBrowserVersion
|
|
295
|
+
|| manifest.platformKey !== this.platformKey
|
|
296
|
+
|| typeof manifest.executableRelativePath !== 'string'
|
|
297
|
+
) return null;
|
|
298
|
+
const executablePath = path.resolve(directory, manifest.executableRelativePath);
|
|
299
|
+
if (!pathInside(directory, executablePath)) return null;
|
|
300
|
+
try {
|
|
301
|
+
fs.accessSync(executablePath, fs.constants.X_OK);
|
|
302
|
+
} catch {
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
return { ...manifest, executablePath };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
installedOlderVersion() {
|
|
309
|
+
let versions = [];
|
|
310
|
+
try {
|
|
311
|
+
versions = fs.readdirSync(this.rootDir(), { withFileTypes: true })
|
|
312
|
+
.filter(entry => entry.isDirectory() && !entry.name.startsWith('.'))
|
|
313
|
+
.map(entry => entry.name)
|
|
314
|
+
.filter(version => version !== this.agentBrowserVersion);
|
|
315
|
+
} catch {
|
|
316
|
+
return '';
|
|
317
|
+
}
|
|
318
|
+
return versions.find(version => {
|
|
319
|
+
const directory = storageLayout.managedChromiumVersionDir(
|
|
320
|
+
this.configDir,
|
|
321
|
+
version,
|
|
322
|
+
this.platformKey,
|
|
323
|
+
);
|
|
324
|
+
const manifest = readJson(path.join(directory, 'install.json'));
|
|
325
|
+
if (
|
|
326
|
+
manifest?.format !== MANIFEST_FORMAT
|
|
327
|
+
|| manifest.agentBrowserVersion !== version
|
|
328
|
+
|| manifest.platformKey !== this.platformKey
|
|
329
|
+
|| typeof manifest.executableRelativePath !== 'string'
|
|
330
|
+
) return false;
|
|
331
|
+
const executablePath = path.resolve(directory, manifest.executableRelativePath);
|
|
332
|
+
if (!pathInside(directory, executablePath)) return false;
|
|
333
|
+
try {
|
|
334
|
+
fs.accessSync(executablePath, fs.constants.X_OK);
|
|
335
|
+
return true;
|
|
336
|
+
} catch {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
}) || '';
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
status() {
|
|
343
|
+
const current = this.readValidManifest();
|
|
344
|
+
const installedVersion = current?.agentBrowserVersion || this.installedOlderVersion();
|
|
345
|
+
const updateAvailable = Boolean(installedVersion && !current);
|
|
346
|
+
if (this.installPromise) {
|
|
347
|
+
return {
|
|
348
|
+
state: 'installing',
|
|
349
|
+
agentBrowserVersion: this.agentBrowserVersion,
|
|
350
|
+
installedVersion: installedVersion || '',
|
|
351
|
+
updateAvailable,
|
|
352
|
+
error: '',
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
if (current) {
|
|
356
|
+
return {
|
|
357
|
+
state: 'ready',
|
|
358
|
+
agentBrowserVersion: this.agentBrowserVersion,
|
|
359
|
+
installedVersion: current.agentBrowserVersion,
|
|
360
|
+
updateAvailable: false,
|
|
361
|
+
error: '',
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
state: this.lastFailure ? 'failed' : 'absent',
|
|
366
|
+
agentBrowserVersion: this.agentBrowserVersion,
|
|
367
|
+
installedVersion: installedVersion || '',
|
|
368
|
+
updateAvailable,
|
|
369
|
+
error: this.lastFailure,
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
browserOption() {
|
|
374
|
+
const manifest = this.readValidManifest();
|
|
375
|
+
return manifest
|
|
376
|
+
? { kind: 'managed-chromium', path: manifest.executablePath }
|
|
377
|
+
: null;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
async acquireLock() {
|
|
381
|
+
const lockDir = storageLayout.managedChromiumInstallLockDir(this.configDir);
|
|
382
|
+
const startedAt = Date.now();
|
|
383
|
+
const token = crypto.randomUUID();
|
|
384
|
+
let childPid = null;
|
|
385
|
+
const writeOwner = () => writeJsonAtomic(path.join(lockDir, 'owner.json'), {
|
|
386
|
+
pid: process.pid,
|
|
387
|
+
childPid,
|
|
388
|
+
token,
|
|
389
|
+
createdAt: new Date().toISOString(),
|
|
390
|
+
});
|
|
391
|
+
while (true) {
|
|
392
|
+
try {
|
|
393
|
+
fs.mkdirSync(lockDir, { recursive: false, mode: 0o700 });
|
|
394
|
+
try {
|
|
395
|
+
writeOwner();
|
|
396
|
+
} catch (error) {
|
|
397
|
+
fs.rmSync(lockDir, { recursive: true, force: true });
|
|
398
|
+
throw error;
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
childStarted(pid) {
|
|
402
|
+
childPid = Number(pid) || null;
|
|
403
|
+
writeOwner();
|
|
404
|
+
},
|
|
405
|
+
abandon() {
|
|
406
|
+
const owner = readJson(path.join(lockDir, 'owner.json'));
|
|
407
|
+
if (owner?.token !== token) return;
|
|
408
|
+
writeJsonAtomic(path.join(lockDir, 'owner.json'), {
|
|
409
|
+
pid: null,
|
|
410
|
+
childPid,
|
|
411
|
+
token,
|
|
412
|
+
createdAt: owner.createdAt,
|
|
413
|
+
});
|
|
414
|
+
},
|
|
415
|
+
release() {
|
|
416
|
+
const owner = readJson(path.join(lockDir, 'owner.json'));
|
|
417
|
+
if (owner?.token === token) fs.rmSync(lockDir, { recursive: true, force: true });
|
|
418
|
+
},
|
|
419
|
+
};
|
|
420
|
+
} catch (error) {
|
|
421
|
+
if (error?.code !== 'EEXIST') {
|
|
422
|
+
if (error?.code === 'ENOENT') {
|
|
423
|
+
fs.mkdirSync(path.dirname(lockDir), { recursive: true, mode: 0o700 });
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
throw error;
|
|
427
|
+
}
|
|
428
|
+
const owner = readJson(path.join(lockDir, 'owner.json'));
|
|
429
|
+
let ageMs = 0;
|
|
430
|
+
try {
|
|
431
|
+
ageMs = Date.now() - fs.statSync(lockDir).mtimeMs;
|
|
432
|
+
} catch {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const ownerAlive = processRunning(owner?.pid);
|
|
436
|
+
const childAlive = processRunning(owner?.childPid);
|
|
437
|
+
if ((!ownerAlive && !childAlive) || (!owner && ageMs >= LOCK_STALE_MS)) {
|
|
438
|
+
const stale = `${lockDir}.stale-${Date.now()}-${crypto.randomUUID()}`;
|
|
439
|
+
try {
|
|
440
|
+
fs.renameSync(lockDir, stale);
|
|
441
|
+
fs.rmSync(stale, { recursive: true, force: true });
|
|
442
|
+
continue;
|
|
443
|
+
} catch {
|
|
444
|
+
// Another installer recovered the abandoned lock.
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
if (Date.now() - startedAt >= LOCK_TIMEOUT_MS) {
|
|
448
|
+
const timeout = new Error('Timed out waiting for another Chromium installation');
|
|
449
|
+
timeout.code = 'CHROMIUM_INSTALL_LOCK_TIMEOUT';
|
|
450
|
+
throw timeout;
|
|
451
|
+
}
|
|
452
|
+
await this.wait(LOCK_POLL_MS);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
async install() {
|
|
458
|
+
if (this.readValidManifest()) return this.status();
|
|
459
|
+
if (this.installPromise) return this.installPromise;
|
|
460
|
+
this.lastFailure = '';
|
|
461
|
+
this.installPromise = (async () => {
|
|
462
|
+
try {
|
|
463
|
+
await this.performInstall();
|
|
464
|
+
} catch (error) {
|
|
465
|
+
this.lastFailure = error?.message || String(error);
|
|
466
|
+
throw error;
|
|
467
|
+
} finally {
|
|
468
|
+
this.installPromise = null;
|
|
469
|
+
}
|
|
470
|
+
return this.status();
|
|
471
|
+
})();
|
|
472
|
+
return this.installPromise;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async performInstall() {
|
|
476
|
+
const agentBrowserPath = String(
|
|
477
|
+
typeof this.agentBrowserPath === 'function'
|
|
478
|
+
? this.agentBrowserPath()
|
|
479
|
+
: this.agentBrowserPath,
|
|
480
|
+
).trim();
|
|
481
|
+
if (!agentBrowserPath) {
|
|
482
|
+
const error = new Error(
|
|
483
|
+
`agent-browser ${this.agentBrowserVersion} is not prepared; restart Farming through its launcher`,
|
|
484
|
+
);
|
|
485
|
+
error.code = 'AGENT_BROWSER_NOT_FOUND';
|
|
486
|
+
throw error;
|
|
487
|
+
}
|
|
488
|
+
const agentBrowser = await this.verifyAgentBrowser(agentBrowserPath);
|
|
489
|
+
if (!agentBrowser?.valid) {
|
|
490
|
+
const error = new Error(`Farming requires agent-browser ${this.agentBrowserVersion} to install Chromium`);
|
|
491
|
+
error.code = 'AGENT_BROWSER_VERSION_MISMATCH';
|
|
492
|
+
throw error;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const lock = await this.acquireLock();
|
|
496
|
+
let stagingDir = '';
|
|
497
|
+
let cleanupSafe = true;
|
|
498
|
+
try {
|
|
499
|
+
if (this.readValidManifest()) return this.status();
|
|
500
|
+
const targetDir = this.targetDir();
|
|
501
|
+
const parentDir = path.dirname(targetDir);
|
|
502
|
+
fs.mkdirSync(parentDir, { recursive: true, mode: 0o700 });
|
|
503
|
+
for (const entry of fs.readdirSync(parentDir, { withFileTypes: true })) {
|
|
504
|
+
if (entry.isDirectory() && entry.name.startsWith(`.staging-${this.platformKey}-`)) {
|
|
505
|
+
fs.rmSync(path.join(parentDir, entry.name), { recursive: true, force: true });
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
stagingDir = path.join(
|
|
509
|
+
parentDir,
|
|
510
|
+
`.staging-${this.platformKey}-${process.pid}-${crypto.randomUUID()}`,
|
|
511
|
+
);
|
|
512
|
+
fs.mkdirSync(stagingDir, { recursive: false, mode: 0o700 });
|
|
513
|
+
const installHome = path.join(stagingDir, '.home');
|
|
514
|
+
const xdgRoot = path.join(stagingDir, '.xdg');
|
|
515
|
+
fs.mkdirSync(installHome, { recursive: true, mode: 0o700 });
|
|
516
|
+
fs.mkdirSync(xdgRoot, { recursive: true, mode: 0o700 });
|
|
517
|
+
const installEnv = {
|
|
518
|
+
...this.env,
|
|
519
|
+
HOME: installHome,
|
|
520
|
+
USERPROFILE: installHome,
|
|
521
|
+
LOCALAPPDATA: path.join(installHome, 'AppData', 'Local'),
|
|
522
|
+
XDG_CACHE_HOME: path.join(xdgRoot, 'cache'),
|
|
523
|
+
XDG_CONFIG_HOME: path.join(xdgRoot, 'config'),
|
|
524
|
+
XDG_STATE_HOME: path.join(xdgRoot, 'state'),
|
|
525
|
+
PLAYWRIGHT_BROWSERS_PATH: stagingDir,
|
|
526
|
+
};
|
|
527
|
+
await this.runInstallCommand(agentBrowserPath, ['install'], {
|
|
528
|
+
env: installEnv,
|
|
529
|
+
platform: this.platform,
|
|
530
|
+
timeoutMs: INSTALL_TIMEOUT_MS,
|
|
531
|
+
onSpawn: pid => lock.childStarted(pid),
|
|
532
|
+
});
|
|
533
|
+
const executablePath = findBrowserExecutable(stagingDir, { platform: this.platform });
|
|
534
|
+
if (!executablePath) {
|
|
535
|
+
const error = new Error('agent-browser finished without installing a usable Chromium executable');
|
|
536
|
+
error.code = 'CHROMIUM_EXECUTABLE_NOT_FOUND';
|
|
537
|
+
throw error;
|
|
538
|
+
}
|
|
539
|
+
const browserVersion = await this.verifyBrowser(executablePath, { env: installEnv });
|
|
540
|
+
fs.rmSync(installHome, { recursive: true, force: true });
|
|
541
|
+
fs.rmSync(xdgRoot, { recursive: true, force: true });
|
|
542
|
+
writeJsonAtomic(this.manifestFile(stagingDir), {
|
|
543
|
+
format: MANIFEST_FORMAT,
|
|
544
|
+
agentBrowserVersion: this.agentBrowserVersion,
|
|
545
|
+
platformKey: this.platformKey,
|
|
546
|
+
browserVersion,
|
|
547
|
+
executableRelativePath: path.relative(stagingDir, executablePath),
|
|
548
|
+
installedAt: new Date().toISOString(),
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
let displacedDir = '';
|
|
552
|
+
if (fs.existsSync(targetDir)) {
|
|
553
|
+
displacedDir = `${targetDir}.invalid-${crypto.randomUUID()}`;
|
|
554
|
+
fs.renameSync(targetDir, displacedDir);
|
|
555
|
+
}
|
|
556
|
+
try {
|
|
557
|
+
fs.renameSync(stagingDir, targetDir);
|
|
558
|
+
stagingDir = '';
|
|
559
|
+
} catch (error) {
|
|
560
|
+
if (displacedDir && !fs.existsSync(targetDir)) fs.renameSync(displacedDir, targetDir);
|
|
561
|
+
throw error;
|
|
562
|
+
}
|
|
563
|
+
if (displacedDir) {
|
|
564
|
+
try {
|
|
565
|
+
fs.rmSync(displacedDir, { recursive: true, force: true });
|
|
566
|
+
} catch (error) {
|
|
567
|
+
console.warn('Managed Chromium installed, but the invalid previous cache could not be removed:', error);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
this.lastFailure = '';
|
|
571
|
+
return this.status();
|
|
572
|
+
} catch (error) {
|
|
573
|
+
cleanupSafe = error?.cleanupUnproven !== true;
|
|
574
|
+
throw error;
|
|
575
|
+
} finally {
|
|
576
|
+
if (cleanupSafe) {
|
|
577
|
+
if (stagingDir) fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
578
|
+
lock.release();
|
|
579
|
+
} else {
|
|
580
|
+
lock.abandon();
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
module.exports = {
|
|
587
|
+
INSTALL_TIMEOUT_MS,
|
|
588
|
+
MANIFEST_FORMAT,
|
|
589
|
+
ManagedChromiumInstaller,
|
|
590
|
+
defaultRunInstallCommand,
|
|
591
|
+
defaultVerifyBrowser,
|
|
592
|
+
findBrowserExecutable,
|
|
593
|
+
};
|