@firefunc-agent/runner 0.9.37 → 0.9.40
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 +21 -0
- package/dist/activity.d.ts +2 -0
- package/dist/activity.js +3 -0
- package/dist/alive.d.ts +8 -0
- package/dist/alive.js +29 -0
- package/dist/api.d.ts +32 -0
- package/dist/api.js +18 -2
- package/dist/ask-replace.d.ts +6 -0
- package/dist/ask-replace.js +49 -0
- package/dist/awake-timer.d.ts +8 -0
- package/dist/awake-timer.js +30 -0
- package/dist/brief.js +2 -2
- package/dist/build-info.json +2 -2
- package/dist/clean-up.d.ts +29 -0
- package/dist/clean-up.js +131 -0
- package/dist/cli.js +57 -6
- package/dist/config.d.ts +7 -0
- package/dist/cut.d.ts +2 -0
- package/dist/cut.js +20 -3
- package/dist/daemon.js +115 -5
- package/dist/disk-reclaim.d.ts +40 -0
- package/dist/disk-reclaim.js +95 -0
- package/dist/drives.d.ts +29 -0
- package/dist/drives.js +270 -0
- package/dist/engine-account.d.ts +1 -1
- package/dist/engine-account.js +12 -1
- package/dist/engines/types.d.ts +1 -1
- package/dist/exec.js +5 -6
- package/dist/firefunc/automations.js +3 -3
- package/dist/firefunc/main.js +1 -1
- package/dist/firefunc/oauth-login.js +1 -1
- package/dist/gh-repos.d.ts +41 -0
- package/dist/gh-repos.js +68 -0
- package/dist/git-auth.d.ts +1 -0
- package/dist/git-auth.js +1 -1
- package/dist/held.d.ts +4 -2
- package/dist/held.js +19 -5
- package/dist/job.d.ts +1 -0
- package/dist/job.js +148 -33
- package/dist/keep-local.d.ts +1 -0
- package/dist/keep-local.js +63 -0
- package/dist/local-tools.d.ts +31 -0
- package/dist/local-tools.js +178 -0
- package/dist/machine-logins.d.ts +4 -0
- package/dist/machine-logins.js +31 -0
- package/dist/mcp-login.d.ts +3 -0
- package/dist/mcp-login.js +34 -4
- package/dist/mcp-tools.d.ts +23 -0
- package/dist/mcp-tools.js +122 -0
- package/dist/mcp.d.ts +8 -0
- package/dist/mcp.js +37 -3
- package/dist/pool.d.ts +1 -0
- package/dist/pool.js +1 -0
- package/dist/pr-title.d.ts +1 -0
- package/dist/pr-title.js +7 -0
- package/dist/repo-path.d.ts +15 -0
- package/dist/repo-path.js +56 -0
- package/dist/resources.d.ts +38 -0
- package/dist/resources.js +47 -1
- package/dist/restart-idle.d.ts +1 -0
- package/dist/restart-idle.js +20 -5
- package/dist/run-folders.d.ts +25 -0
- package/dist/run-folders.js +89 -0
- package/dist/viewer.d.ts +1 -1
- package/dist/viewer.js +3 -1
- package/dist/work-dir.d.ts +30 -0
- package/dist/work-dir.js +153 -22
- package/dist/worktrees.d.ts +7 -0
- package/dist/worktrees.js +26 -5
- package/package.json +1 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, statSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { expandHome } from './config.js';
|
|
5
|
+
export function expandPath(p, env = process.env) {
|
|
6
|
+
const out = p
|
|
7
|
+
.trim()
|
|
8
|
+
.replace(/^"(.*)"$/, '$1')
|
|
9
|
+
.replace(/%([A-Za-z_][A-Za-z0-9_]*)%/g, (m, n) => env[n] ?? env[n.toUpperCase()] ?? m)
|
|
10
|
+
.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (m, n) => env[n] ?? m)
|
|
11
|
+
.replace(/\$([A-Za-z_][A-Za-z0-9_]*)/g, (m, n) => env[n] ?? m);
|
|
12
|
+
return expandHome(out);
|
|
13
|
+
}
|
|
14
|
+
export function repoOfRemote(url) {
|
|
15
|
+
const m = /[:/]([^/:]+\/[^/]+?)(?:\.git)?\/?$/.exec(url.trim());
|
|
16
|
+
return m ? m[1] : null;
|
|
17
|
+
}
|
|
18
|
+
export function checkRepoPath(repo, typed, run = (args, cwd) => {
|
|
19
|
+
const r = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
|
20
|
+
return { code: r.status ?? 1, out: `${r.stdout ?? ''}`.trim() };
|
|
21
|
+
}) {
|
|
22
|
+
const path = expandPath(typed);
|
|
23
|
+
const unexpanded = /%[A-Za-z_]+%|\$\{?[A-Za-z_]/.exec(path);
|
|
24
|
+
if (unexpanded)
|
|
25
|
+
return {
|
|
26
|
+
ok: false,
|
|
27
|
+
path,
|
|
28
|
+
reason: `${unexpanded[0]} in the path is not set on this machine.`,
|
|
29
|
+
fix: 'Type the full path instead, e.g. "C:\\Users\\you\\src\\repo".',
|
|
30
|
+
};
|
|
31
|
+
if (!existsSync(path) || !statSync(path).isDirectory())
|
|
32
|
+
return {
|
|
33
|
+
ok: false,
|
|
34
|
+
path,
|
|
35
|
+
reason: `No folder at ${path}.`,
|
|
36
|
+
fix: `Clone it there first (git clone https://github.com/${repo} "${path}"), or give the folder it is in.`,
|
|
37
|
+
};
|
|
38
|
+
if (!existsSync(join(path, '.git')))
|
|
39
|
+
return {
|
|
40
|
+
ok: false,
|
|
41
|
+
path,
|
|
42
|
+
reason: `${path} is not a git checkout.`,
|
|
43
|
+
fix: `Give the folder the repository was cloned into (it holds a .git folder).`,
|
|
44
|
+
};
|
|
45
|
+
const remote = run(['remote', 'get-url', 'origin'], path);
|
|
46
|
+
const named = remote.code === 0 ? repoOfRemote(remote.out) : null;
|
|
47
|
+
if (named && named.toLowerCase() !== repo.toLowerCase())
|
|
48
|
+
return {
|
|
49
|
+
ok: false,
|
|
50
|
+
path,
|
|
51
|
+
reason: `${path} is a checkout of ${named}, not ${repo}.`,
|
|
52
|
+
fix: `Give the folder that holds ${repo}, or map ${named} instead.`,
|
|
53
|
+
};
|
|
54
|
+
return { ok: true, path };
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=repo-path.js.map
|
package/dist/resources.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { type DeviceReport } from './device-lease.js';
|
|
2
2
|
import { type MachineProfile } from './machine-profile.js';
|
|
3
|
+
import { type WorkRootSource } from './work-dir.js';
|
|
4
|
+
import { type DriveReport } from './drives.js';
|
|
5
|
+
import { type CleanUpReport } from './clean-up.js';
|
|
6
|
+
import { type ReclaimReport } from './disk-reclaim.js';
|
|
3
7
|
export type ResourceReading = {
|
|
4
8
|
freeBytes: number;
|
|
5
9
|
totalBytes: number | null;
|
|
@@ -11,6 +15,11 @@ export type ResourcesReport = {
|
|
|
11
15
|
timeLimitMinutes: number;
|
|
12
16
|
devices?: DeviceReport;
|
|
13
17
|
workDir?: WorkDirReport;
|
|
18
|
+
workRoot?: WorkRootReport;
|
|
19
|
+
drives?: DriveReport[];
|
|
20
|
+
localTools?: string[];
|
|
21
|
+
cleanUp?: CleanUpReport | null;
|
|
22
|
+
reclaim?: ReclaimReport | null;
|
|
14
23
|
cpus?: number;
|
|
15
24
|
load1PerCore?: number;
|
|
16
25
|
maxHeavyRuns?: number;
|
|
@@ -28,6 +37,16 @@ export type WorkDirReport = {
|
|
|
28
37
|
set: boolean;
|
|
29
38
|
usable: boolean;
|
|
30
39
|
};
|
|
40
|
+
export type WorkRootReport = {
|
|
41
|
+
path: string;
|
|
42
|
+
usable: boolean;
|
|
43
|
+
reason?: string;
|
|
44
|
+
fix?: string;
|
|
45
|
+
bytes: number | null;
|
|
46
|
+
freeBytes: number | null;
|
|
47
|
+
totalBytes: number | null;
|
|
48
|
+
source: WorkRootSource;
|
|
49
|
+
};
|
|
31
50
|
export declare const DEFAULT_MIN_FREE_DISK_GB = 5;
|
|
32
51
|
export declare const DEFAULT_MIN_FREE_MEMORY_GB = 1;
|
|
33
52
|
export declare const DEFAULT_JOB_TIMEOUT_MINUTES = 20;
|
|
@@ -47,6 +66,7 @@ export declare function macAvailableFromVmStat(out: string): number | null;
|
|
|
47
66
|
export declare function linuxAvailableFromMeminfo(text: string): number | null;
|
|
48
67
|
export declare function availableMemory(os?: NodeJS.Platform): number;
|
|
49
68
|
export type ResourceCfg = {
|
|
69
|
+
localTools?: Record<string, unknown>;
|
|
50
70
|
workDir?: string;
|
|
51
71
|
minFreeDiskGB?: number;
|
|
52
72
|
minFreeMemoryGB?: number;
|
|
@@ -66,14 +86,32 @@ export declare function readResources(cfg: ResourceCfg, runnerDir: string, deps?
|
|
|
66
86
|
memory?: () => number;
|
|
67
87
|
totalMemory?: () => number;
|
|
68
88
|
workDir?: () => WorkDirReport;
|
|
89
|
+
workRoot?: () => WorkRootReport;
|
|
90
|
+
drives?: () => DriveReport[];
|
|
91
|
+
cleanUp?: () => CleanUpReport | null;
|
|
69
92
|
devices?: () => DeviceReport;
|
|
70
93
|
processor?: () => {
|
|
71
94
|
cpus: number;
|
|
72
95
|
load1PerCore: number | null;
|
|
73
96
|
};
|
|
74
97
|
profile?: () => MachineProfile | null;
|
|
98
|
+
localTools?: () => string[];
|
|
75
99
|
}): ResourcesReport;
|
|
76
100
|
export declare function workDirReport(): WorkDirReport;
|
|
101
|
+
export declare function workRootReport(read?: {
|
|
102
|
+
check?: () => {
|
|
103
|
+
root: string;
|
|
104
|
+
usable: boolean;
|
|
105
|
+
reason?: string;
|
|
106
|
+
fix?: string;
|
|
107
|
+
};
|
|
108
|
+
source?: () => WorkRootSource;
|
|
109
|
+
disk?: (dir: string) => {
|
|
110
|
+
freeBytes: number;
|
|
111
|
+
totalBytes: number;
|
|
112
|
+
} | null;
|
|
113
|
+
bytes?: (dir: string) => number | null;
|
|
114
|
+
}): WorkRootReport;
|
|
77
115
|
export declare function shortOf(r: ResourcesReport): Array<'disk' | 'memory'>;
|
|
78
116
|
export declare function resourceWatch(deps: {
|
|
79
117
|
read: () => ResourcesReport;
|
package/dist/resources.js
CHANGED
|
@@ -5,7 +5,11 @@ import { deviceReport } from './device-lease.js';
|
|
|
5
5
|
import { deviceLockDir } from './locks.js';
|
|
6
6
|
import { commandOnPath, runPath } from './machine-commands.js';
|
|
7
7
|
import { machineProfile } from './machine-profile.js';
|
|
8
|
-
import { workDir, workDirFailure } from './work-dir.js';
|
|
8
|
+
import { workDir, workDirFailure, workRootCheck, workRootSource, } from './work-dir.js';
|
|
9
|
+
import { driveReport, rootBytes } from './drives.js';
|
|
10
|
+
import { localToolNames } from './local-tools.js';
|
|
11
|
+
import { lastCleanUp } from './clean-up.js';
|
|
12
|
+
import { lastReclaim } from './disk-reclaim.js';
|
|
9
13
|
export function heavyRunBudget(setting, memBytes = totalmem()) {
|
|
10
14
|
if (typeof setting === 'number' && Number.isInteger(setting) && setting >= 1) {
|
|
11
15
|
return Math.min(16, setting);
|
|
@@ -101,9 +105,18 @@ export function hasDeviceTools(cfg, has = (n, p) => commandOnPath(n, p)) {
|
|
|
101
105
|
return has('adb', runPath(cfg));
|
|
102
106
|
}
|
|
103
107
|
export function readResources(cfg, runnerDir, deps = {}) {
|
|
108
|
+
const root = (() => {
|
|
109
|
+
try {
|
|
110
|
+
return deps.workRoot ? deps.workRoot() : workRootReport();
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
})();
|
|
104
116
|
const dirs = [
|
|
105
117
|
tmpdir(),
|
|
106
118
|
...(cfg.workDir ? [cfg.workDir] : []),
|
|
119
|
+
...(root?.usable ? [root.path] : []),
|
|
107
120
|
runnerDir,
|
|
108
121
|
...Object.values(cfg.repos ?? {}),
|
|
109
122
|
];
|
|
@@ -144,6 +157,26 @@ export function readResources(cfg, runnerDir, deps = {}) {
|
|
|
144
157
|
: null,
|
|
145
158
|
timeLimitMinutes: Math.round(limit),
|
|
146
159
|
workDir: deps.workDir ? deps.workDir() : workDirReport(),
|
|
160
|
+
...(root ? { workRoot: root } : {}),
|
|
161
|
+
drives: (() => {
|
|
162
|
+
try {
|
|
163
|
+
return deps.drives ? deps.drives() : driveReport();
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
})(),
|
|
169
|
+
cleanUp: deps.cleanUp ? deps.cleanUp() : lastCleanUp(),
|
|
170
|
+
reclaim: lastReclaim(),
|
|
171
|
+
...(() => {
|
|
172
|
+
try {
|
|
173
|
+
const names = deps.localTools ? deps.localTools() : localToolNames(cfg);
|
|
174
|
+
return names.length ? { localTools: names } : {};
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return {};
|
|
178
|
+
}
|
|
179
|
+
})(),
|
|
147
180
|
devices: (() => {
|
|
148
181
|
try {
|
|
149
182
|
if (deps.devices)
|
|
@@ -172,6 +205,19 @@ export function workDirReport() {
|
|
|
172
205
|
const set = workDir() !== null;
|
|
173
206
|
return { set, usable: !set || workDirFailure() === null };
|
|
174
207
|
}
|
|
208
|
+
export function workRootReport(read = {}) {
|
|
209
|
+
const c = (read.check ?? (() => workRootCheck()))();
|
|
210
|
+
const d = c.usable ? (read.disk ?? diskOf)(c.root) : null;
|
|
211
|
+
return {
|
|
212
|
+
path: c.root,
|
|
213
|
+
usable: c.usable,
|
|
214
|
+
...(c.usable ? {} : { reason: c.reason, fix: c.fix }),
|
|
215
|
+
bytes: c.usable ? (read.bytes ?? ((r) => rootBytes(r)))(c.root) : null,
|
|
216
|
+
freeBytes: d ? d.freeBytes : null,
|
|
217
|
+
totalBytes: d ? d.totalBytes : null,
|
|
218
|
+
source: (read.source ?? workRootSource)(),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
175
221
|
export function shortOf(r) {
|
|
176
222
|
const out = [];
|
|
177
223
|
if (r.disk && r.disk.floorBytes > 0 && r.disk.freeBytes < r.disk.floorBytes)
|
package/dist/restart-idle.d.ts
CHANGED
package/dist/restart-idle.js
CHANGED
|
@@ -19,11 +19,17 @@ export async function restartWhenIdle(d) {
|
|
|
19
19
|
const started = d.now();
|
|
20
20
|
d.log('Draining: the runner finishes the runs it holds and takes nothing new; it restarts once none is left.');
|
|
21
21
|
let told = -1;
|
|
22
|
+
let toldSettle = -1;
|
|
22
23
|
for (;;) {
|
|
23
24
|
const held = d.held();
|
|
24
25
|
const waited = d.now() - started;
|
|
25
|
-
|
|
26
|
+
const seen = d.drainSeen?.() ?? null;
|
|
27
|
+
const confirmed = !!seen && (drainedBefore || Date.parse(seen) >= started - 1_000);
|
|
28
|
+
if (held.length === 0 && (confirmed || waited >= settle)) {
|
|
29
|
+
if (confirmed && waited < settle)
|
|
30
|
+
d.log('The runner confirmed it takes nothing new.');
|
|
26
31
|
break;
|
|
32
|
+
}
|
|
27
33
|
if (d.maxWaitMs != null && waited >= d.maxWaitMs) {
|
|
28
34
|
lift();
|
|
29
35
|
return {
|
|
@@ -35,11 +41,20 @@ export async function restartWhenIdle(d) {
|
|
|
35
41
|
'Run "firefunc-runner restart --when-idle" again without --max-wait to wait for them.',
|
|
36
42
|
};
|
|
37
43
|
}
|
|
38
|
-
if (held.length !== told) {
|
|
44
|
+
if (held.length > 0 && held.length !== told) {
|
|
39
45
|
told = held.length;
|
|
40
|
-
d.log(held.length
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
d.log(`waiting for ${held.length} run${held.length === 1 ? '' : 's'} to finish: ${held.join(', ')}`);
|
|
47
|
+
}
|
|
48
|
+
else if (held.length === 0) {
|
|
49
|
+
const left = Math.max(0, Math.ceil((settle - waited) / 1000));
|
|
50
|
+
const step = Math.floor(left / 10);
|
|
51
|
+
if (told !== 0 || step !== toldSettle) {
|
|
52
|
+
d.log(told !== 0
|
|
53
|
+
? `No run held. Waiting up to ${left} s for the runner to confirm it takes nothing new, so a run being picked up right now cannot be cut short.`
|
|
54
|
+
: ` …up to ${left} s left`);
|
|
55
|
+
told = 0;
|
|
56
|
+
toldSettle = step;
|
|
57
|
+
}
|
|
43
58
|
}
|
|
44
59
|
await d.sleep(poll);
|
|
45
60
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const RUN_MARKER = ".firefunc-run";
|
|
2
|
+
export declare const GROUP_MARKER = ".firefunc-project";
|
|
3
|
+
export declare const RESERVED_FOLDERS: Set<string>;
|
|
4
|
+
export declare const NO_PROJECT_FOLDER = "runs";
|
|
5
|
+
export type RunProject = {
|
|
6
|
+
id: string;
|
|
7
|
+
slug: string | null;
|
|
8
|
+
name: string | null;
|
|
9
|
+
} | null;
|
|
10
|
+
export declare function safeSegment(text: string | null | undefined, max?: number): string;
|
|
11
|
+
export declare function projectFolder(project: RunProject | undefined): string | null;
|
|
12
|
+
export declare function runFolderName(externalId: string | null | undefined, runId: string): string;
|
|
13
|
+
export declare function runFolderPath(root: string, job: {
|
|
14
|
+
runId: string;
|
|
15
|
+
externalId?: string | null;
|
|
16
|
+
project?: RunProject;
|
|
17
|
+
}): string;
|
|
18
|
+
export declare function makeRunFolder(root: string, job: {
|
|
19
|
+
runId: string;
|
|
20
|
+
externalId?: string | null;
|
|
21
|
+
project?: RunProject;
|
|
22
|
+
}): string;
|
|
23
|
+
export declare function writeRunMarker(folder: string, runId: string): boolean;
|
|
24
|
+
export declare function runMarkerOf(folder: string): string | null;
|
|
25
|
+
export declare function checkoutOfWorktree(folder: string): string | null;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
export const RUN_MARKER = '.firefunc-run';
|
|
4
|
+
export const GROUP_MARKER = '.firefunc-project';
|
|
5
|
+
export const RESERVED_FOLDERS = new Set(['_tmp', '_cache', 'kept', 'tmp']);
|
|
6
|
+
export const NO_PROJECT_FOLDER = 'runs';
|
|
7
|
+
export function safeSegment(text, max = 40) {
|
|
8
|
+
const s = (text ?? '')
|
|
9
|
+
.replace(/[^A-Za-z0-9._-]+/g, '-')
|
|
10
|
+
.replace(/-{2,}/g, '-')
|
|
11
|
+
.replace(/^[.-]+/, '')
|
|
12
|
+
.slice(0, max)
|
|
13
|
+
.replace(/[.-]+$/, '');
|
|
14
|
+
return s === '.' || s === '..' ? '' : s;
|
|
15
|
+
}
|
|
16
|
+
export function projectFolder(project) {
|
|
17
|
+
if (!project)
|
|
18
|
+
return null;
|
|
19
|
+
const seg = safeSegment(project.slug) || safeSegment(project.id, 8);
|
|
20
|
+
if (!seg)
|
|
21
|
+
return null;
|
|
22
|
+
const lower = seg.toLowerCase();
|
|
23
|
+
return RESERVED_FOLDERS.has(lower) || lower === NO_PROJECT_FOLDER ? `p-${seg}` : seg;
|
|
24
|
+
}
|
|
25
|
+
export function runFolderName(externalId, runId) {
|
|
26
|
+
const ref = safeSegment(externalId) || 'task';
|
|
27
|
+
const short = runId.replace(/[^A-Za-z0-9]/g, '').slice(0, 8) || 'run';
|
|
28
|
+
return `${ref}-${short}`;
|
|
29
|
+
}
|
|
30
|
+
export function runFolderPath(root, job) {
|
|
31
|
+
return join(root, projectFolder(job.project) ?? NO_PROJECT_FOLDER, runFolderName(job.externalId, job.runId));
|
|
32
|
+
}
|
|
33
|
+
export function makeRunFolder(root, job) {
|
|
34
|
+
const base = runFolderPath(root, job);
|
|
35
|
+
const group = join(base, '..');
|
|
36
|
+
mkdirSync(group, { recursive: true });
|
|
37
|
+
try {
|
|
38
|
+
writeFileSync(join(group, GROUP_MARKER), 'firefunc\n', { flag: 'wx' });
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
}
|
|
42
|
+
for (let n = 1; n < 1000; n++) {
|
|
43
|
+
const p = n === 1 ? base : `${base}-${n}`;
|
|
44
|
+
if (existsSync(p))
|
|
45
|
+
continue;
|
|
46
|
+
try {
|
|
47
|
+
mkdirSync(p);
|
|
48
|
+
return p;
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (err.code === 'EEXIST')
|
|
52
|
+
continue;
|
|
53
|
+
throw err;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
throw new Error(`could not find a free folder name for ${base}`);
|
|
57
|
+
}
|
|
58
|
+
export function writeRunMarker(folder, runId) {
|
|
59
|
+
try {
|
|
60
|
+
writeFileSync(join(folder, RUN_MARKER), `${JSON.stringify({ runId, at: new Date().toISOString() })}\n`);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function runMarkerOf(folder) {
|
|
68
|
+
try {
|
|
69
|
+
const v = JSON.parse(readFileSync(join(folder, RUN_MARKER), 'utf8'));
|
|
70
|
+
return typeof v.runId === 'string' && v.runId ? v.runId : null;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export function checkoutOfWorktree(folder) {
|
|
77
|
+
try {
|
|
78
|
+
const text = readFileSync(join(folder, '.git'), 'utf8');
|
|
79
|
+
const gitdir = /^gitdir:\s*(.+)$/m.exec(text)?.[1]?.trim();
|
|
80
|
+
if (!gitdir)
|
|
81
|
+
return null;
|
|
82
|
+
const m = /^(.*)[\\/]\.git[\\/]worktrees[\\/][^\\/]+$/.exec(gitdir);
|
|
83
|
+
return m?.[1] ?? null;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=run-folders.js.map
|
package/dist/viewer.d.ts
CHANGED
package/dist/viewer.js
CHANGED
|
@@ -51,7 +51,9 @@ export function createSessionViewer(opts) {
|
|
|
51
51
|
});
|
|
52
52
|
server.on('error', (err) => {
|
|
53
53
|
url = null;
|
|
54
|
-
opts.onError?.(
|
|
54
|
+
opts.onError?.(err.code === 'EADDRINUSE'
|
|
55
|
+
? `EADDRINUSE: ${err.message}`
|
|
56
|
+
: err.message, port);
|
|
55
57
|
});
|
|
56
58
|
server.listen(port, '127.0.0.1', () => {
|
|
57
59
|
url = `http://127.0.0.1:${port}`;
|
package/dist/work-dir.d.ts
CHANGED
|
@@ -1,11 +1,41 @@
|
|
|
1
|
+
export type WorkRootSource = 'local' | 'workspace' | 'default';
|
|
2
|
+
export type WorkRootCheck = {
|
|
3
|
+
root: string;
|
|
4
|
+
usable: boolean;
|
|
5
|
+
reason?: string;
|
|
6
|
+
fix?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function defaultWorkRoot(): string;
|
|
1
9
|
export declare function setWorkDir(dir: string | null | undefined): void;
|
|
10
|
+
export declare function setWorkspaceRoot(root: string | null | undefined): boolean;
|
|
11
|
+
export declare function workspaceRoot(): string | null;
|
|
2
12
|
export declare function workDir(): string | null;
|
|
13
|
+
export declare function workRootSource(): WorkRootSource;
|
|
14
|
+
export declare function chosenWorkRoot(): string;
|
|
15
|
+
export declare function checkRoot(root: string, source?: WorkRootSource, fresh?: boolean): WorkRootCheck;
|
|
16
|
+
export declare function workRootCheck(fresh?: boolean): WorkRootCheck;
|
|
3
17
|
export declare function workRoot(): string;
|
|
4
18
|
export declare function workDirFailure(): string | null;
|
|
5
19
|
export declare function workDirProblem(): string | null;
|
|
20
|
+
export declare function workRootProblem(): string | null;
|
|
21
|
+
export declare function runRoots(): string[];
|
|
6
22
|
export declare function workRoots(): string[];
|
|
7
23
|
export declare function workTempEnv(): Record<string, string>;
|
|
24
|
+
export declare function scratchRoot(): string;
|
|
25
|
+
export declare function workspaceRootFile(runnerDir: string): string;
|
|
26
|
+
export declare function saveWorkspaceRoot(runnerDir: string, root: string | null): void;
|
|
27
|
+
export declare function loadWorkspaceRoot(runnerDir: string): string | null;
|
|
8
28
|
export declare function workDirWords(dir: string | undefined, disk: (dir: string) => {
|
|
9
29
|
freeBytes: number;
|
|
10
30
|
totalBytes: number;
|
|
11
31
|
} | null, problem?: string | null): string;
|
|
32
|
+
export declare function workRootWords(r: {
|
|
33
|
+
path: string;
|
|
34
|
+
usable: boolean;
|
|
35
|
+
reason?: string;
|
|
36
|
+
fix?: string;
|
|
37
|
+
bytes: number | null;
|
|
38
|
+
freeBytes: number | null;
|
|
39
|
+
source: WorkRootSource;
|
|
40
|
+
}): string;
|
|
41
|
+
export declare function isTooBroad(root: string): boolean;
|
package/dist/work-dir.js
CHANGED
|
@@ -1,33 +1,103 @@
|
|
|
1
|
-
import { mkdirSync } from 'node:fs';
|
|
2
|
-
import { tmpdir } from 'node:os';
|
|
3
|
-
import { join } from 'node:path';
|
|
1
|
+
import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { homedir, tmpdir } from 'node:os';
|
|
3
|
+
import { join, parse, resolve } from 'node:path';
|
|
4
4
|
let configured = null;
|
|
5
|
-
let
|
|
5
|
+
let workspace = null;
|
|
6
|
+
const used = new Set();
|
|
7
|
+
const checks = new Map();
|
|
8
|
+
const CHECK_TTL_MS = 30_000;
|
|
9
|
+
const clean = (dir) => typeof dir === 'string' && dir.trim() ? dir.trim() : null;
|
|
10
|
+
export function defaultWorkRoot() {
|
|
11
|
+
return clean(process.env.FIREFUNC_WORK_ROOT) ?? join(homedir(), 'FireFunc');
|
|
12
|
+
}
|
|
6
13
|
export function setWorkDir(dir) {
|
|
7
|
-
configured =
|
|
8
|
-
|
|
14
|
+
configured = clean(dir);
|
|
15
|
+
}
|
|
16
|
+
export function setWorkspaceRoot(root) {
|
|
17
|
+
const next = clean(root);
|
|
18
|
+
const changed = next !== workspace;
|
|
19
|
+
workspace = next;
|
|
20
|
+
return changed;
|
|
21
|
+
}
|
|
22
|
+
export function workspaceRoot() {
|
|
23
|
+
return workspace;
|
|
9
24
|
}
|
|
10
25
|
export function workDir() {
|
|
11
26
|
return configured;
|
|
12
27
|
}
|
|
13
|
-
export function
|
|
14
|
-
|
|
15
|
-
|
|
28
|
+
export function workRootSource() {
|
|
29
|
+
return configured ? 'local' : workspace ? 'workspace' : 'default';
|
|
30
|
+
}
|
|
31
|
+
export function chosenWorkRoot() {
|
|
32
|
+
return configured ?? workspace ?? defaultWorkRoot();
|
|
33
|
+
}
|
|
34
|
+
function fixFor(source) {
|
|
35
|
+
if (source === 'local') {
|
|
36
|
+
return 'Connect the drive or pick a folder this user can write, with connect --work-dir <dir>.';
|
|
37
|
+
}
|
|
38
|
+
if (source === 'workspace') {
|
|
39
|
+
return "Connect the drive or pick a folder this user can write in the machine's settings on FireFunc.";
|
|
40
|
+
}
|
|
41
|
+
return "Connect the drive or pick a folder this user can write, with connect --work-dir <dir>, or in the machine's settings on FireFunc.";
|
|
42
|
+
}
|
|
43
|
+
function plainReason(err, root) {
|
|
44
|
+
const e = err;
|
|
45
|
+
const code = e?.code;
|
|
46
|
+
if (code === 'EACCES' || code === 'EPERM')
|
|
47
|
+
return `this user may not write to ${root}`;
|
|
48
|
+
if (code === 'ENOENT')
|
|
49
|
+
return `${root} is on a drive or folder that is not there`;
|
|
50
|
+
if (code === 'ENOTDIR' || code === 'EEXIST')
|
|
51
|
+
return `a file is in the way of the folder ${root}`;
|
|
52
|
+
if (code === 'EROFS')
|
|
53
|
+
return `${root} is on a read-only drive`;
|
|
54
|
+
if (code === 'ENOSPC')
|
|
55
|
+
return `the drive holding ${root} is full`;
|
|
56
|
+
return e instanceof Error ? e.message : String(err);
|
|
57
|
+
}
|
|
58
|
+
export function checkRoot(root, source = workRootSource(), fresh = false) {
|
|
59
|
+
const seen = checks.get(root);
|
|
60
|
+
if (!fresh && seen && Date.now() - seen.at < CHECK_TTL_MS)
|
|
61
|
+
return seen.check;
|
|
62
|
+
let check;
|
|
63
|
+
if (isTooBroad(root)) {
|
|
64
|
+
check = {
|
|
65
|
+
root,
|
|
66
|
+
usable: false,
|
|
67
|
+
reason: `${root} is a whole drive or your home folder, not a folder of its own.`,
|
|
68
|
+
fix: `Pick a folder inside it, such as ${join(root, 'FireFunc')}.`,
|
|
69
|
+
};
|
|
70
|
+
checks.set(root, { at: Date.now(), check });
|
|
71
|
+
return check;
|
|
72
|
+
}
|
|
16
73
|
try {
|
|
17
|
-
mkdirSync(
|
|
18
|
-
|
|
19
|
-
|
|
74
|
+
mkdirSync(root, { recursive: true });
|
|
75
|
+
const probe = join(root, `.firefunc-probe-${process.pid}`);
|
|
76
|
+
writeFileSync(probe, 'ok');
|
|
77
|
+
rmSync(probe, { force: true });
|
|
78
|
+
check = { root, usable: true };
|
|
20
79
|
}
|
|
21
80
|
catch (err) {
|
|
22
|
-
|
|
23
|
-
return tmpdir();
|
|
81
|
+
check = { root, usable: false, reason: plainReason(err, root), fix: fixFor(source) };
|
|
24
82
|
}
|
|
83
|
+
checks.set(root, { at: Date.now(), check });
|
|
84
|
+
return check;
|
|
85
|
+
}
|
|
86
|
+
export function workRootCheck(fresh = false) {
|
|
87
|
+
return checkRoot(chosenWorkRoot(), workRootSource(), fresh);
|
|
88
|
+
}
|
|
89
|
+
export function workRoot() {
|
|
90
|
+
const c = workRootCheck();
|
|
91
|
+
if (!c.usable)
|
|
92
|
+
return tmpdir();
|
|
93
|
+
used.add(c.root);
|
|
94
|
+
return c.root;
|
|
25
95
|
}
|
|
26
96
|
export function workDirFailure() {
|
|
27
97
|
if (!configured)
|
|
28
98
|
return null;
|
|
29
|
-
|
|
30
|
-
return
|
|
99
|
+
const c = workRootCheck(true);
|
|
100
|
+
return c.usable ? null : (c.reason ?? 'it cannot be used');
|
|
31
101
|
}
|
|
32
102
|
export function workDirProblem() {
|
|
33
103
|
const why = workDirFailure();
|
|
@@ -35,26 +105,65 @@ export function workDirProblem() {
|
|
|
35
105
|
? `the work folder ${configured} cannot be made (${why}), so runs work in the OS temp folder ${tmpdir()} instead. Fix: connect the drive or pick a folder this user can write, with connect --work-dir <dir>.`
|
|
36
106
|
: null;
|
|
37
107
|
}
|
|
108
|
+
export function workRootProblem() {
|
|
109
|
+
const c = workRootCheck(true);
|
|
110
|
+
return c.usable
|
|
111
|
+
? null
|
|
112
|
+
: `the work folder ${c.root} (${workRootSource()}) cannot be used (${c.reason}), so runs work in the OS temp folder ${tmpdir()} instead. Fix: ${c.fix}`;
|
|
113
|
+
}
|
|
114
|
+
export function runRoots() {
|
|
115
|
+
const roots = [chosenWorkRoot(), defaultWorkRoot(), ...used];
|
|
116
|
+
if (configured)
|
|
117
|
+
roots.push(configured);
|
|
118
|
+
if (workspace)
|
|
119
|
+
roots.push(workspace);
|
|
120
|
+
return [...new Set(roots)];
|
|
121
|
+
}
|
|
38
122
|
export function workRoots() {
|
|
39
123
|
const roots = [tmpdir()];
|
|
40
|
-
|
|
41
|
-
roots.push(
|
|
124
|
+
for (const r of runRoots())
|
|
125
|
+
roots.push(r, join(r, '_tmp'), join(r, 'tmp'));
|
|
42
126
|
return [...new Set(roots)];
|
|
43
127
|
}
|
|
44
128
|
export function workTempEnv() {
|
|
45
|
-
|
|
129
|
+
const c = workRootCheck();
|
|
130
|
+
if (!c.usable)
|
|
46
131
|
return {};
|
|
47
|
-
const tmp = join(
|
|
132
|
+
const tmp = join(c.root, '_tmp');
|
|
48
133
|
try {
|
|
49
134
|
mkdirSync(tmp, { recursive: true });
|
|
135
|
+
mkdirSync(join(c.root, '_cache'), { recursive: true });
|
|
50
136
|
}
|
|
51
137
|
catch {
|
|
52
138
|
return {};
|
|
53
139
|
}
|
|
140
|
+
used.add(c.root);
|
|
54
141
|
return { TMPDIR: tmp, TEMP: tmp, TMP: tmp };
|
|
55
142
|
}
|
|
143
|
+
export function scratchRoot() {
|
|
144
|
+
return workTempEnv().TMPDIR ?? workRoot();
|
|
145
|
+
}
|
|
146
|
+
export function workspaceRootFile(runnerDir) {
|
|
147
|
+
return join(runnerDir, 'work-root.json');
|
|
148
|
+
}
|
|
149
|
+
export function saveWorkspaceRoot(runnerDir, root) {
|
|
150
|
+
try {
|
|
151
|
+
writeFileSync(workspaceRootFile(runnerDir), JSON.stringify({ workRoot: clean(root) }));
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
export function loadWorkspaceRoot(runnerDir) {
|
|
157
|
+
try {
|
|
158
|
+
const v = JSON.parse(readFileSync(workspaceRootFile(runnerDir), 'utf8'));
|
|
159
|
+
return typeof v.workRoot === 'string' ? clean(v.workRoot) : null;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
56
165
|
export function workDirWords(dir, disk, problem = null) {
|
|
57
|
-
const where = dir?.trim() ? dir.trim() :
|
|
166
|
+
const where = dir?.trim() ? dir.trim() : chosenWorkRoot();
|
|
58
167
|
if (dir?.trim() && problem) {
|
|
59
168
|
const t = disk(tmpdir());
|
|
60
169
|
const tFree = t ? `${(t.freeBytes / 1024 ** 3).toFixed(1)} GB free` : 'free space unknown';
|
|
@@ -64,6 +173,28 @@ export function workDirWords(dir, disk, problem = null) {
|
|
|
64
173
|
const free = d ? `${(d.freeBytes / 1024 ** 3).toFixed(1)} GB free` : 'free space unknown';
|
|
65
174
|
return dir?.trim()
|
|
66
175
|
? `${where} (${free}) — worktrees and the engine's temp folder`
|
|
67
|
-
: `${where} (${free}) — the
|
|
176
|
+
: `${where} (${free}) — the ${workRootSource() === 'workspace' ? "workspace's" : 'default'} root; set one with connect --work-dir <dir>`;
|
|
177
|
+
}
|
|
178
|
+
export function workRootWords(r) {
|
|
179
|
+
const by = r.source === 'local'
|
|
180
|
+
? "this machine's --work-dir"
|
|
181
|
+
: r.source === 'workspace'
|
|
182
|
+
? "the workspace's setting"
|
|
183
|
+
: 'the default';
|
|
184
|
+
if (!r.usable) {
|
|
185
|
+
return `${r.path} (${by}) cannot be used: ${r.reason}. Runs work in the OS temp folder instead. Fix: ${r.fix}`;
|
|
186
|
+
}
|
|
187
|
+
const gb = (b) => `${(b / 1024 ** 3).toFixed(1)} GB`;
|
|
188
|
+
const size = r.bytes === null ? 'size not measured' : `holds ${gb(r.bytes)}`;
|
|
189
|
+
const free = r.freeBytes === null ? 'free space unknown' : `${gb(r.freeBytes)} free`;
|
|
190
|
+
return `${r.path} (${by}) — usable, ${size}, ${free}`;
|
|
191
|
+
}
|
|
192
|
+
export function isTooBroad(root) {
|
|
193
|
+
const r = resolve(root);
|
|
194
|
+
if (r === resolve(homedir()))
|
|
195
|
+
return true;
|
|
196
|
+
if (r === parse(r).root)
|
|
197
|
+
return true;
|
|
198
|
+
return /^\\\\[^\\]+\\[^\\]+\\?$/.test(r);
|
|
68
199
|
}
|
|
69
200
|
//# sourceMappingURL=work-dir.js.map
|
package/dist/worktrees.d.ts
CHANGED
|
@@ -4,6 +4,13 @@ export declare function isRunnerWorktree(path: string, opts?: {
|
|
|
4
4
|
platform?: NodeJS.Platform;
|
|
5
5
|
tmp?: string;
|
|
6
6
|
roots?: readonly string[];
|
|
7
|
+
layoutRoots?: readonly string[];
|
|
8
|
+
hasMarker?: (path: string) => boolean;
|
|
9
|
+
}): boolean;
|
|
10
|
+
export declare function isLayoutRunFolder(path: string, opts?: {
|
|
11
|
+
platform?: NodeJS.Platform;
|
|
12
|
+
layoutRoots?: readonly string[];
|
|
13
|
+
hasMarker?: (path: string) => boolean;
|
|
7
14
|
}): boolean;
|
|
8
15
|
export type WorktreeEntry = {
|
|
9
16
|
path: string;
|