@firefunc-agent/runner 0.9.0 → 0.9.3
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 +90 -28
- package/dist/activity.d.ts +4 -1
- package/dist/activity.js +94 -8
- package/dist/api.d.ts +24 -0
- package/dist/api.js +74 -5
- package/dist/brief.d.ts +5 -0
- package/dist/brief.js +21 -0
- package/dist/build-info.json +2 -2
- package/dist/cli.js +10 -2
- package/dist/config.d.ts +2 -0
- package/dist/cut.d.ts +45 -0
- package/dist/cut.js +105 -0
- package/dist/daemon.d.ts +16 -0
- package/dist/daemon.js +156 -35
- package/dist/declined.d.ts +11 -0
- package/dist/declined.js +27 -0
- package/dist/engines/claude.js +129 -1
- package/dist/engines/types.d.ts +10 -0
- package/dist/exec.d.ts +3 -0
- package/dist/exec.js +48 -5
- package/dist/firefunc/agents.js +59 -7
- package/dist/firefunc/client.d.ts +1 -0
- package/dist/firefunc/client.js +3 -0
- package/dist/firefunc/configure.d.ts +6 -0
- package/dist/firefunc/configure.js +409 -0
- package/dist/firefunc/main.js +45 -7
- package/dist/firefunc/output.js +4 -1
- package/dist/firefunc/version.js +2 -17
- package/dist/held.d.ts +18 -0
- package/dist/held.js +95 -0
- package/dist/job.d.ts +3 -0
- package/dist/job.js +437 -76
- package/dist/keep-local.d.ts +6 -1
- package/dist/keep-local.js +171 -4
- package/dist/limit.d.ts +30 -0
- package/dist/limit.js +258 -0
- package/dist/locks.d.ts +14 -0
- package/dist/locks.js +164 -0
- package/dist/mcp-proxy.d.ts +21 -0
- package/dist/mcp-proxy.js +180 -0
- package/dist/mcp.d.ts +2 -0
- package/dist/mcp.js +31 -2
- package/dist/orphans.d.ts +40 -0
- package/dist/orphans.js +235 -0
- package/dist/pool.d.ts +7 -0
- package/dist/pool.js +57 -9
- package/dist/pr-title.d.ts +11 -0
- package/dist/pr-title.js +63 -0
- package/dist/recover.d.ts +10 -0
- package/dist/recover.js +104 -0
- package/dist/session-stream.d.ts +8 -1
- package/dist/session-stream.js +9 -1
- package/dist/skills.d.ts +11 -0
- package/dist/skills.js +164 -0
- package/dist/stopped.d.ts +3 -0
- package/dist/stopped.js +10 -0
- package/dist/usage.d.ts +49 -0
- package/dist/usage.js +389 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +17 -0
- package/dist/worktrees.d.ts +15 -0
- package/dist/worktrees.js +96 -0
- package/package.json +1 -1
- package/skill/firefunc/SKILL.md +57 -8
package/dist/locks.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
|
|
3
|
+
import { hostname } from 'node:os';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
1
5
|
const noop = () => { };
|
|
2
6
|
export function createKeyedMutex() {
|
|
3
7
|
const tails = new Map();
|
|
@@ -10,4 +14,164 @@ export function createKeyedMutex() {
|
|
|
10
14
|
},
|
|
11
15
|
};
|
|
12
16
|
}
|
|
17
|
+
export function repoLockKey(repo) {
|
|
18
|
+
const slug = repo
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/[^a-z0-9._-]+/g, '_')
|
|
21
|
+
.replace(/^[._]+/, '')
|
|
22
|
+
.slice(0, 80);
|
|
23
|
+
const hash = createHash('sha256').update(repo.toLowerCase()).digest('hex').slice(0, 8);
|
|
24
|
+
return `${slug || 'repo'}-${hash}`;
|
|
25
|
+
}
|
|
26
|
+
export function heavyLockDir(runnerDir, repo) {
|
|
27
|
+
return join(runnerDir, 'locks', `heavy-${repoLockKey(repo)}.lock`);
|
|
28
|
+
}
|
|
29
|
+
function isAlive(pid) {
|
|
30
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
31
|
+
return false;
|
|
32
|
+
try {
|
|
33
|
+
process.kill(pid, 0);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
return e.code === 'EPERM';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function holderOf(lockDir) {
|
|
41
|
+
try {
|
|
42
|
+
const first = readFileSync(join(lockDir, 'owner'), 'utf8').split('\n', 1)[0] ?? '';
|
|
43
|
+
const pid = Number(first.trim());
|
|
44
|
+
return Number.isInteger(pid) && pid > 0 ? pid : null;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function breakIfStale(lockDir, now) {
|
|
51
|
+
const holder = holderOf(lockDir);
|
|
52
|
+
let stale = holder !== null && !isAlive(holder);
|
|
53
|
+
if (holder === null) {
|
|
54
|
+
try {
|
|
55
|
+
stale = now() - statSync(lockDir).mtimeMs > 60_000;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!stale)
|
|
62
|
+
return false;
|
|
63
|
+
const away = `${lockDir}.stale-${process.pid}-${Math.random().toString(36).slice(2, 8)}`;
|
|
64
|
+
try {
|
|
65
|
+
renameSync(lockDir, away);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
rmSync(away, { recursive: true, force: true });
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
export async function acquireHeavyLock(lockDir, opts = {}) {
|
|
74
|
+
const now = opts.now ?? (() => Date.now());
|
|
75
|
+
const sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
76
|
+
const started = now();
|
|
77
|
+
let told = false;
|
|
78
|
+
mkdirSync(dirname(lockDir), { recursive: true, mode: 0o700 });
|
|
79
|
+
for (;;) {
|
|
80
|
+
try {
|
|
81
|
+
mkdirSync(lockDir);
|
|
82
|
+
writeFileSync(join(lockDir, 'owner'), `${process.pid}\n${hostname()}\n${new Date(now()).toISOString()}\n${opts.label ?? 'runner'}\n`);
|
|
83
|
+
let held = true;
|
|
84
|
+
return () => {
|
|
85
|
+
if (!held)
|
|
86
|
+
return;
|
|
87
|
+
held = false;
|
|
88
|
+
if (holderOf(lockDir) === process.pid)
|
|
89
|
+
rmSync(lockDir, { recursive: true, force: true });
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
if (e.code !== 'EEXIST')
|
|
94
|
+
return noop;
|
|
95
|
+
}
|
|
96
|
+
if (breakIfStale(lockDir, now))
|
|
97
|
+
continue;
|
|
98
|
+
if (!told) {
|
|
99
|
+
told = true;
|
|
100
|
+
opts.onWait?.(holderOf(lockDir));
|
|
101
|
+
}
|
|
102
|
+
if (opts.maxWaitMs !== undefined && now() - started >= opts.maxWaitMs)
|
|
103
|
+
return noop;
|
|
104
|
+
await sleep(opts.pollMs ?? 1000);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const heavyMutex = createKeyedMutex();
|
|
108
|
+
export async function withHeavyLock(lockDir, fn, opts = {}) {
|
|
109
|
+
return heavyMutex.run(lockDir, async () => {
|
|
110
|
+
const release = await acquireHeavyLock(lockDir, opts);
|
|
111
|
+
try {
|
|
112
|
+
return await fn();
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
release();
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const HEAVY_SCRIPT_VERSION = 1;
|
|
120
|
+
export function heavyScript() {
|
|
121
|
+
return `#!/bin/sh
|
|
122
|
+
# firefunc-heavy v${HEAVY_SCRIPT_VERSION} — written by the FireFunc runner (locks.ts).
|
|
123
|
+
# Runs ONE heavy command at a time per repository on this machine:
|
|
124
|
+
# "$FIREFUNC_HEAVY" pnpm test
|
|
125
|
+
# It waits for any other heavy command in the same repository, then runs yours.
|
|
126
|
+
lock="\${FIREFUNC_HEAVY_LOCK:-}"
|
|
127
|
+
if [ -z "$lock" ] || [ "\${FIREFUNC_HEAVY_HELD:-}" = "$lock" ]; then
|
|
128
|
+
exec "$@"
|
|
129
|
+
fi
|
|
130
|
+
mkdir -p "$(dirname "$lock")" 2>/dev/null
|
|
131
|
+
told=0
|
|
132
|
+
while ! mkdir "$lock" 2>/dev/null; do
|
|
133
|
+
holder=$(sed -n 1p "$lock/owner" 2>/dev/null)
|
|
134
|
+
if [ -n "$holder" ] && ! kill -0 "$holder" 2>/dev/null; then
|
|
135
|
+
away="$lock.stale-$$"
|
|
136
|
+
mv "$lock" "$away" 2>/dev/null && rm -rf "$away"
|
|
137
|
+
continue
|
|
138
|
+
fi
|
|
139
|
+
if [ -z "$holder" ] && [ -n "$(find "$lock" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then
|
|
140
|
+
away="$lock.stale-$$"
|
|
141
|
+
mv "$lock" "$away" 2>/dev/null && rm -rf "$away"
|
|
142
|
+
continue
|
|
143
|
+
fi
|
|
144
|
+
if [ "$told" = 0 ]; then
|
|
145
|
+
echo "firefunc-heavy: another heavy command is running in this repository (pid \${holder:-?}) — waiting for it to finish first" >&2
|
|
146
|
+
told=1
|
|
147
|
+
fi
|
|
148
|
+
sleep 2
|
|
149
|
+
done
|
|
150
|
+
printf '%s\\n%s\\n%s\\n%s\\n' "$$" "$(hostname)" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "engine" > "$lock/owner"
|
|
151
|
+
trap 'rm -rf "$lock"' EXIT
|
|
152
|
+
trap 'rm -rf "$lock"; exit 130' INT
|
|
153
|
+
trap 'rm -rf "$lock"; exit 143' TERM
|
|
154
|
+
FIREFUNC_HEAVY_HELD="$lock"
|
|
155
|
+
export FIREFUNC_HEAVY_HELD
|
|
156
|
+
"$@"
|
|
157
|
+
`;
|
|
158
|
+
}
|
|
159
|
+
export function ensureHeavyScript(runnerDir) {
|
|
160
|
+
if (process.platform === 'win32')
|
|
161
|
+
return null;
|
|
162
|
+
const dir = join(runnerDir, 'bin');
|
|
163
|
+
const file = join(dir, 'firefunc-heavy');
|
|
164
|
+
const text = heavyScript();
|
|
165
|
+
try {
|
|
166
|
+
if (!existsSync(file) || readFileSync(file, 'utf8') !== text) {
|
|
167
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
168
|
+
writeFileSync(file, text, { mode: 0o755 });
|
|
169
|
+
}
|
|
170
|
+
chmodSync(file, 0o755);
|
|
171
|
+
return file;
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
13
177
|
//# sourceMappingURL=locks.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type RelayCredential = {
|
|
2
|
+
token: string;
|
|
3
|
+
expiresAt?: string;
|
|
4
|
+
};
|
|
5
|
+
export type CredentialRelay = {
|
|
6
|
+
url: string;
|
|
7
|
+
readonly renewals: number;
|
|
8
|
+
close(): void;
|
|
9
|
+
};
|
|
10
|
+
export type RelayOptions = {
|
|
11
|
+
upstream: string;
|
|
12
|
+
name: string;
|
|
13
|
+
initial: RelayCredential;
|
|
14
|
+
renew: () => Promise<RelayCredential | null>;
|
|
15
|
+
maxAgeMs?: number;
|
|
16
|
+
earlyMs?: number;
|
|
17
|
+
now?: () => number;
|
|
18
|
+
log?: (line: string) => void;
|
|
19
|
+
maxBodyBytes?: number;
|
|
20
|
+
};
|
|
21
|
+
export declare function startCredentialRelay(opts: RelayOptions): Promise<CredentialRelay>;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { createServer, request as httpRequest, } from 'node:http';
|
|
3
|
+
import { request as httpsRequest } from 'node:https';
|
|
4
|
+
const HOP_BY_HOP = new Set([
|
|
5
|
+
'connection',
|
|
6
|
+
'keep-alive',
|
|
7
|
+
'proxy-authenticate',
|
|
8
|
+
'proxy-authorization',
|
|
9
|
+
'proxy-connection',
|
|
10
|
+
'te',
|
|
11
|
+
'trailer',
|
|
12
|
+
'transfer-encoding',
|
|
13
|
+
'upgrade',
|
|
14
|
+
]);
|
|
15
|
+
function requestHeaders(from, upstream, token, length) {
|
|
16
|
+
const out = {};
|
|
17
|
+
for (const [k, v] of Object.entries(from)) {
|
|
18
|
+
const key = k.toLowerCase();
|
|
19
|
+
if (HOP_BY_HOP.has(key) || key === 'host' || key === 'authorization')
|
|
20
|
+
continue;
|
|
21
|
+
if (key === 'content-length' || v === undefined)
|
|
22
|
+
continue;
|
|
23
|
+
out[key] = v;
|
|
24
|
+
}
|
|
25
|
+
out.host = upstream.host;
|
|
26
|
+
out.authorization = `Bearer ${token}`;
|
|
27
|
+
if (length > 0)
|
|
28
|
+
out['content-length'] = String(length);
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
function responseHeaders(from) {
|
|
32
|
+
const out = {};
|
|
33
|
+
for (const [k, v] of Object.entries(from)) {
|
|
34
|
+
if (HOP_BY_HOP.has(k.toLowerCase()) || v === undefined)
|
|
35
|
+
continue;
|
|
36
|
+
out[k] = v;
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
function targetFor(upstream, rest, search) {
|
|
41
|
+
const t = new URL(upstream.toString());
|
|
42
|
+
if (rest)
|
|
43
|
+
t.pathname = `${t.pathname.replace(/\/+$/, '')}/${rest}`;
|
|
44
|
+
t.search = search;
|
|
45
|
+
return t;
|
|
46
|
+
}
|
|
47
|
+
export async function startCredentialRelay(opts) {
|
|
48
|
+
const upstream = new URL(opts.upstream);
|
|
49
|
+
const loopback = ['127.0.0.1', 'localhost', '[::1]', '::1'].includes(upstream.hostname);
|
|
50
|
+
if (upstream.protocol !== 'https:' && !(upstream.protocol === 'http:' && loopback)) {
|
|
51
|
+
throw new Error('a token is only relayed over https');
|
|
52
|
+
}
|
|
53
|
+
const now = opts.now ?? (() => Date.now());
|
|
54
|
+
const log = opts.log ?? (() => { });
|
|
55
|
+
const maxAgeMs = opts.maxAgeMs ?? 55 * 60_000;
|
|
56
|
+
const earlyMs = opts.earlyMs ?? 5 * 60_000;
|
|
57
|
+
const maxBody = opts.maxBodyBytes ?? 10 * 1024 * 1024;
|
|
58
|
+
const key = randomBytes(24).toString('hex');
|
|
59
|
+
const keyBuf = Buffer.from(key);
|
|
60
|
+
let token = opts.initial.token;
|
|
61
|
+
let issuedAt = now();
|
|
62
|
+
let expiresAt = opts.initial.expiresAt ? Date.parse(opts.initial.expiresAt) : Number.NaN;
|
|
63
|
+
let renewals = 0;
|
|
64
|
+
let renewing = null;
|
|
65
|
+
let lastRenewAt = 0;
|
|
66
|
+
let refusedAt = Number.NEGATIVE_INFINITY;
|
|
67
|
+
const due = () => Number.isFinite(expiresAt) ? now() >= expiresAt - earlyMs : now() - issuedAt >= maxAgeMs;
|
|
68
|
+
const renewOnce = (why) => {
|
|
69
|
+
if (renewing)
|
|
70
|
+
return renewing;
|
|
71
|
+
if (now() - lastRenewAt < 30_000 || now() - refusedAt < 5 * 60_000) {
|
|
72
|
+
return Promise.resolve(false);
|
|
73
|
+
}
|
|
74
|
+
renewing = (async () => {
|
|
75
|
+
try {
|
|
76
|
+
const fresh = await opts.renew();
|
|
77
|
+
if (!fresh?.token) {
|
|
78
|
+
refusedAt = now();
|
|
79
|
+
log(`mcp: FireFunc did not renew the ${opts.name} credential (${why}) — its calls keep the old token until it expires, then fail. ` +
|
|
80
|
+
'Fix: nothing on this machine; a run is allowed a few MCP credentials, and a run that needs more should be split. The API log names the refusal.');
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
token = fresh.token;
|
|
84
|
+
issuedAt = now();
|
|
85
|
+
lastRenewAt = issuedAt;
|
|
86
|
+
expiresAt = fresh.expiresAt ? Date.parse(fresh.expiresAt) : Number.NaN;
|
|
87
|
+
renewals += 1;
|
|
88
|
+
log(`mcp: renewed the ${opts.name} credential (${why}) — the run keeps reading ${opts.name}`);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
refusedAt = now();
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
renewing = null;
|
|
97
|
+
}
|
|
98
|
+
})();
|
|
99
|
+
return renewing;
|
|
100
|
+
};
|
|
101
|
+
const send = (target, method, headers, body) => new Promise((resolve, reject) => {
|
|
102
|
+
const req = (target.protocol === 'https:' ? httpsRequest : httpRequest)(target, { method, headers: requestHeaders(headers, target, token, body.length) }, resolve);
|
|
103
|
+
req.on('error', reject);
|
|
104
|
+
req.end(body.length > 0 ? body : undefined);
|
|
105
|
+
});
|
|
106
|
+
const sockets = new Set();
|
|
107
|
+
const server = createServer((req, res) => {
|
|
108
|
+
void (async () => {
|
|
109
|
+
try {
|
|
110
|
+
const u = new URL(req.url ?? '/', 'http://127.0.0.1');
|
|
111
|
+
const [, first = '', ...rest] = u.pathname.split('/');
|
|
112
|
+
const given = Buffer.from(first);
|
|
113
|
+
if (given.length !== keyBuf.length || !timingSafeEqual(given, keyBuf)) {
|
|
114
|
+
res.writeHead(404).end();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const chunks = [];
|
|
118
|
+
let size = 0;
|
|
119
|
+
for await (const chunk of req) {
|
|
120
|
+
size += chunk.length;
|
|
121
|
+
if (size > maxBody) {
|
|
122
|
+
res.writeHead(413).end();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
chunks.push(chunk);
|
|
126
|
+
}
|
|
127
|
+
const body = Buffer.concat(chunks);
|
|
128
|
+
const target = targetFor(upstream, rest.join('/'), u.search);
|
|
129
|
+
const method = req.method ?? 'GET';
|
|
130
|
+
if (due())
|
|
131
|
+
await renewOnce('before it expires');
|
|
132
|
+
let up = await send(target, method, req.headers, body);
|
|
133
|
+
if (up.statusCode === 401 && (await renewOnce('the server answered 401'))) {
|
|
134
|
+
up.resume();
|
|
135
|
+
up = await send(target, method, req.headers, body);
|
|
136
|
+
}
|
|
137
|
+
res.writeHead(up.statusCode ?? 502, responseHeaders(up.headers));
|
|
138
|
+
up.pipe(res);
|
|
139
|
+
up.on('error', () => res.destroy());
|
|
140
|
+
res.on('close', () => up.destroy());
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
if (!res.headersSent)
|
|
144
|
+
res.writeHead(502).end();
|
|
145
|
+
else
|
|
146
|
+
res.destroy();
|
|
147
|
+
}
|
|
148
|
+
})();
|
|
149
|
+
});
|
|
150
|
+
server.on('connection', (s) => {
|
|
151
|
+
sockets.add(s);
|
|
152
|
+
s.on('close', () => sockets.delete(s));
|
|
153
|
+
});
|
|
154
|
+
server.on('clientError', (_err, socket) => socket.destroy());
|
|
155
|
+
await new Promise((resolve, reject) => {
|
|
156
|
+
server.once('error', reject);
|
|
157
|
+
server.listen(0, '127.0.0.1', () => {
|
|
158
|
+
server.off('error', reject);
|
|
159
|
+
resolve();
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
server.on('error', () => { });
|
|
163
|
+
const port = server.address().port;
|
|
164
|
+
let closed = false;
|
|
165
|
+
return {
|
|
166
|
+
url: `http://127.0.0.1:${port}/${key}${upstream.pathname.endsWith('/') ? '/' : ''}`,
|
|
167
|
+
get renewals() {
|
|
168
|
+
return renewals;
|
|
169
|
+
},
|
|
170
|
+
close() {
|
|
171
|
+
if (closed)
|
|
172
|
+
return;
|
|
173
|
+
closed = true;
|
|
174
|
+
server.close();
|
|
175
|
+
for (const s of sockets)
|
|
176
|
+
s.destroy();
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=mcp-proxy.js.map
|
package/dist/mcp.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ export type PreparedMcp = {
|
|
|
32
32
|
configPath: string | null;
|
|
33
33
|
serverIds: string[];
|
|
34
34
|
toolNames: string[];
|
|
35
|
+
writeToolNames: string[];
|
|
35
36
|
deniedToolNames: string[];
|
|
36
37
|
env: Record<string, string>;
|
|
37
38
|
inlineOverrides: string[];
|
|
@@ -46,6 +47,7 @@ export type PrepareMcpOptions = {
|
|
|
46
47
|
runId: string;
|
|
47
48
|
api: McpCredentialClient;
|
|
48
49
|
log?: (msg: string) => void;
|
|
50
|
+
relay?: boolean;
|
|
49
51
|
};
|
|
50
52
|
export declare function mcpFormatForEngine(engine: string | null | undefined): McpFormat | null;
|
|
51
53
|
export type CachedMcpLogin = {
|
package/dist/mcp.js
CHANGED
|
@@ -3,6 +3,8 @@ import { dirname, isAbsolute, join } from 'node:path';
|
|
|
3
3
|
import { configPath } from './config.js';
|
|
4
4
|
import { CATALOG_ONLY, claudeCodeSignInFix, claudeCodeSignsFor } from './machine-logins.js';
|
|
5
5
|
import { execOut } from './exec.js';
|
|
6
|
+
import { startCredentialRelay } from './mcp-proxy.js';
|
|
7
|
+
const RELAYED_SERVERS = new Set(['github']);
|
|
6
8
|
const SERVER_ID_PATTERN = /^[a-z][a-z0-9_-]*$/;
|
|
7
9
|
const HEADER_NAME_PATTERN = /^[A-Za-z0-9!#$%&'*+.^_`|~-]+$/;
|
|
8
10
|
const HEADER_VALUE_PATTERN = /^[^\x00-\x1f\x7f]*$/;
|
|
@@ -430,6 +432,12 @@ export async function prepareMcp(opts) {
|
|
|
430
432
|
}
|
|
431
433
|
const now = Date.now();
|
|
432
434
|
const resolved = [];
|
|
435
|
+
const relays = [];
|
|
436
|
+
const relayedIds = new Set();
|
|
437
|
+
const closeRelays = () => {
|
|
438
|
+
for (const r of relays)
|
|
439
|
+
r.close();
|
|
440
|
+
};
|
|
433
441
|
for (const spec of usable) {
|
|
434
442
|
if (spec.auth === 'none') {
|
|
435
443
|
resolved.push({ spec, cred: null });
|
|
@@ -500,10 +508,28 @@ export async function prepareMcp(opts) {
|
|
|
500
508
|
'Fix: the API log names the reason; a 403 usually means GitHub is not connected to this workspace, and a 503 that the GitHub App is not configured. The context pack still carries the repository context.');
|
|
501
509
|
continue;
|
|
502
510
|
}
|
|
511
|
+
if (opts.relay && RELAYED_SERVERS.has(spec.id) && spec.transport !== 'stdio' && spec.url) {
|
|
512
|
+
const relay = await startCredentialRelay({
|
|
513
|
+
upstream: spec.url,
|
|
514
|
+
name: spec.id,
|
|
515
|
+
initial: minted,
|
|
516
|
+
renew: () => opts.api.mcpCredential(opts.runId, spec.id).catch(() => null),
|
|
517
|
+
log,
|
|
518
|
+
}).catch(() => null);
|
|
519
|
+
if (relay) {
|
|
520
|
+
relays.push(relay);
|
|
521
|
+
relayedIds.add(spec.id);
|
|
522
|
+
resolved.push({ spec: { ...spec, url: relay.url }, cred: null });
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
log(`mcp: the ${spec.id} relay could not start — the token is written into the config instead, and ${spec.id} calls fail once it expires (an hour after it was issued).`);
|
|
526
|
+
}
|
|
503
527
|
resolved.push({ spec, cred: { kind: 'bearer', scheme: 'Bearer', token: minted.token } });
|
|
504
528
|
}
|
|
505
|
-
if (!resolved.length)
|
|
529
|
+
if (!resolved.length) {
|
|
530
|
+
closeRelays();
|
|
506
531
|
return null;
|
|
532
|
+
}
|
|
507
533
|
const written = resolved.map((r) => r.spec);
|
|
508
534
|
let inlineOverrides = [];
|
|
509
535
|
let env = {};
|
|
@@ -519,6 +545,7 @@ export async function prepareMcp(opts) {
|
|
|
519
545
|
}
|
|
520
546
|
catch (e) {
|
|
521
547
|
log(`mcp: could not write ${relative} (${e.message}) — MCP is off for this run.`);
|
|
548
|
+
closeRelays();
|
|
522
549
|
return null;
|
|
523
550
|
}
|
|
524
551
|
}
|
|
@@ -533,6 +560,7 @@ export async function prepareMcp(opts) {
|
|
|
533
560
|
if (cleaned)
|
|
534
561
|
return;
|
|
535
562
|
cleaned = true;
|
|
563
|
+
closeRelays();
|
|
536
564
|
if (!writtenPath)
|
|
537
565
|
return;
|
|
538
566
|
try {
|
|
@@ -551,7 +579,7 @@ export async function prepareMcp(opts) {
|
|
|
551
579
|
const tools = s.writeTools
|
|
552
580
|
? `${total - writes} read + ${writes} write`
|
|
553
581
|
: `${total} tool${total === 1 ? '' : 's'} (unclassified)`;
|
|
554
|
-
return `${s.id} (${s.transport}, ${s.auth}, ${tools})`;
|
|
582
|
+
return `${s.id} (${s.transport}, ${s.auth}${relayedIds.has(s.id) ? ', through the renewing relay' : ''}, ${tools})`;
|
|
555
583
|
})
|
|
556
584
|
.join(', ')}; ${budget}`;
|
|
557
585
|
return {
|
|
@@ -559,6 +587,7 @@ export async function prepareMcp(opts) {
|
|
|
559
587
|
configPath: target,
|
|
560
588
|
serverIds: written.map((s) => s.id),
|
|
561
589
|
toolNames: toolNamesFor(written),
|
|
590
|
+
writeToolNames: toolNamesFor(written.map((s) => ({ ...s, allowedTools: s.writeTools ?? [] }))),
|
|
562
591
|
deniedToolNames: deniedNamesFor(written),
|
|
563
592
|
env,
|
|
564
593
|
inlineOverrides,
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type ProcessRow = {
|
|
2
|
+
pid: number;
|
|
3
|
+
ppid: number;
|
|
4
|
+
pgid: number;
|
|
5
|
+
command: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function listProcesses(): Promise<ProcessRow[]>;
|
|
8
|
+
export declare function workingDirs(pids: number[]): Promise<Map<number, string>>;
|
|
9
|
+
export type EngineGroup = {
|
|
10
|
+
pgid: number;
|
|
11
|
+
startedAt: number;
|
|
12
|
+
leaderExited: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type OrphanScope = {
|
|
15
|
+
roots: string[];
|
|
16
|
+
reviewCopiesOf?: string;
|
|
17
|
+
groups?: EngineGroup[];
|
|
18
|
+
self?: number;
|
|
19
|
+
};
|
|
20
|
+
export type SelectScope = Omit<OrphanScope, 'groups'> & {
|
|
21
|
+
groups?: number[];
|
|
22
|
+
};
|
|
23
|
+
export declare function groupsStillOurs(groups: EngineGroup[], rows: ProcessRow[]): Promise<number[]>;
|
|
24
|
+
export declare function selectOrphans(rows: ProcessRow[], cwds: Map<number, string>, scope: SelectScope): number[];
|
|
25
|
+
export declare function isAlive(pid: number): boolean;
|
|
26
|
+
export type OrphanSweep = {
|
|
27
|
+
stopped: Array<{
|
|
28
|
+
pid: number;
|
|
29
|
+
program: string;
|
|
30
|
+
}>;
|
|
31
|
+
survivors: number[];
|
|
32
|
+
};
|
|
33
|
+
export declare function killOrphans(scope: OrphanScope, opts?: {
|
|
34
|
+
graceMs?: number;
|
|
35
|
+
log?: (line: string) => void;
|
|
36
|
+
}): Promise<OrphanSweep>;
|
|
37
|
+
export declare function reviewCopiesOf(worktree: string): string[];
|
|
38
|
+
export declare function trackEngineGroup(pgid: number): void;
|
|
39
|
+
export declare function untrackEngineGroup(pgid: number): void;
|
|
40
|
+
export declare function signalEngineGroups(sig: NodeJS.Signals): number;
|