@firefunc-agent/runner 0.9.14 → 0.9.34
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 +167 -10
- package/dist/alive.d.ts +40 -0
- package/dist/alive.js +138 -0
- package/dist/api-fallback.d.ts +4 -2
- package/dist/api-fallback.js +26 -1
- package/dist/api.d.ts +65 -3
- package/dist/api.js +164 -3
- package/dist/brief-file.d.ts +15 -0
- package/dist/brief-file.js +36 -0
- package/dist/brief.d.ts +3 -0
- package/dist/brief.js +14 -4
- package/dist/build-caches.d.ts +7 -0
- package/dist/build-caches.js +63 -0
- package/dist/build-info.json +2 -2
- package/dist/checkpoint.d.ts +40 -0
- package/dist/checkpoint.js +98 -0
- package/dist/cli.js +184 -18
- package/dist/cloud-boot.d.ts +41 -0
- package/dist/cloud-boot.js +239 -0
- package/dist/command-activity.d.ts +23 -0
- package/dist/command-activity.js +89 -0
- package/dist/command-step.d.ts +58 -0
- package/dist/command-step.js +248 -0
- package/dist/config.d.ts +38 -0
- package/dist/config.js +23 -0
- package/dist/cut.d.ts +1 -1
- package/dist/cut.js +13 -3
- package/dist/daemon.js +364 -15
- package/dist/device-lease.d.ts +15 -0
- package/dist/device-lease.js +608 -0
- package/dist/editor/fs-server.d.ts +31 -0
- package/dist/editor/fs-server.js +159 -0
- package/dist/editor/relay-crypto.d.ts +68 -0
- package/dist/editor/relay-crypto.js +191 -0
- package/dist/editor/sessions.d.ts +50 -0
- package/dist/editor/sessions.js +279 -0
- package/dist/editor/worktree-of.d.ts +1 -0
- package/dist/editor/worktree-of.js +23 -0
- package/dist/editor/ws-client.d.ts +26 -0
- package/dist/editor/ws-client.js +227 -0
- package/dist/engine-account.d.ts +24 -0
- package/dist/engine-account.js +118 -0
- package/dist/engine-login.d.ts +12 -0
- package/dist/engine-login.js +88 -0
- package/dist/engine-models.d.ts +13 -0
- package/dist/engine-models.js +71 -0
- package/dist/engine-watch.d.ts +1 -0
- package/dist/engine-watch.js +22 -6
- package/dist/engines/antigravity.js +19 -4
- package/dist/engines/claude.js +10 -9
- package/dist/engines/codex.js +16 -2
- package/dist/engines/copilot.js +12 -0
- package/dist/engines/crush.d.ts +2 -0
- package/dist/engines/crush.js +58 -0
- package/dist/engines/cursor.js +12 -0
- package/dist/engines/gemini.js +14 -9
- package/dist/engines/index.d.ts +8 -0
- package/dist/engines/index.js +73 -1
- package/dist/engines/kilo.d.ts +2 -0
- package/dist/engines/kilo.js +63 -0
- package/dist/engines/opencode.d.ts +9 -0
- package/dist/engines/opencode.js +234 -0
- package/dist/engines/openhands.d.ts +10 -0
- package/dist/engines/openhands.js +271 -0
- package/dist/engines/pi.d.ts +10 -0
- package/dist/engines/pi.js +248 -0
- package/dist/engines/qwen.d.ts +8 -0
- package/dist/engines/qwen.js +153 -0
- package/dist/engines/sessions.d.ts +6 -0
- package/dist/engines/sessions.js +68 -0
- package/dist/engines/types.d.ts +3 -3
- package/dist/exec.d.ts +1 -0
- package/dist/exec.js +10 -4
- package/dist/firefunc/agents.d.ts +8 -1
- package/dist/firefunc/agents.js +53 -3
- package/dist/firefunc/auth.js +72 -19
- package/dist/firefunc/cli.js +25 -0
- package/dist/firefunc/client.d.ts +5 -1
- package/dist/firefunc/client.js +12 -2
- package/dist/firefunc/config.d.ts +11 -1
- package/dist/firefunc/config.js +6 -4
- package/dist/firefunc/io.d.ts +1 -0
- package/dist/firefunc/main.js +8 -1
- package/dist/firefunc/oauth-login.d.ts +23 -0
- package/dist/firefunc/oauth-login.js +304 -0
- package/dist/firefunc/tasks.d.ts +4 -0
- package/dist/firefunc/tasks.js +89 -2
- package/dist/fleet-words.d.ts +2 -0
- package/dist/fleet-words.js +7 -0
- package/dist/git-auth.d.ts +15 -0
- package/dist/git-auth.js +39 -1
- package/dist/held.d.ts +3 -0
- package/dist/job.d.ts +26 -3
- package/dist/job.js +752 -152
- package/dist/keep-awake.d.ts +17 -0
- package/dist/keep-awake.js +90 -0
- package/dist/keep-local.d.ts +1 -1
- package/dist/keep-local.js +5 -3
- package/dist/limit.d.ts +9 -1
- package/dist/limit.js +260 -3
- package/dist/local-checks.d.ts +19 -0
- package/dist/local-checks.js +79 -0
- package/dist/locks.d.ts +18 -1
- package/dist/locks.js +249 -69
- package/dist/machine-commands.d.ts +21 -0
- package/dist/machine-commands.js +107 -0
- package/dist/machine-profile.d.ts +18 -0
- package/dist/machine-profile.js +69 -0
- package/dist/mcp-init.d.ts +2 -0
- package/dist/mcp-init.js +28 -0
- package/dist/mcp.d.ts +13 -1
- package/dist/mcp.js +23 -2
- package/dist/network.js +3 -2
- package/dist/orphans.d.ts +4 -0
- package/dist/orphans.js +96 -4
- package/dist/pool.d.ts +1 -0
- package/dist/pool.js +25 -2
- package/dist/pr-title.d.ts +10 -0
- package/dist/pr-title.js +15 -0
- package/dist/recover.d.ts +3 -1
- package/dist/recover.js +27 -7
- package/dist/release-draft.d.ts +79 -0
- package/dist/release-draft.js +301 -0
- package/dist/resources.d.ts +86 -0
- package/dist/resources.js +217 -0
- package/dist/restart-idle.d.ts +23 -0
- package/dist/restart-idle.js +67 -0
- package/dist/run-attachments.d.ts +5 -0
- package/dist/run-attachments.js +14 -9
- package/dist/run-stops.d.ts +15 -0
- package/dist/run-stops.js +90 -0
- package/dist/run-temp.d.ts +12 -0
- package/dist/run-temp.js +57 -0
- package/dist/secret-scan.d.ts +5 -0
- package/dist/secret-scan.js +30 -0
- package/dist/self-upgrade.d.ts +36 -0
- package/dist/self-upgrade.js +78 -0
- package/dist/service.d.ts +7 -1
- package/dist/service.js +50 -7
- package/dist/sign-in.d.ts +41 -0
- package/dist/sign-in.js +239 -0
- package/dist/tool-call.d.ts +59 -0
- package/dist/tool-call.js +375 -0
- package/dist/transport.d.ts +26 -0
- package/dist/transport.js +118 -0
- package/dist/work-dir.d.ts +11 -0
- package/dist/work-dir.js +69 -0
- package/dist/worktree-report.d.ts +10 -0
- package/dist/worktree-report.js +17 -0
- package/dist/worktrees.d.ts +16 -1
- package/dist/worktrees.js +62 -17
- package/package.json +4 -4
- package/skill/firefunc/SKILL.md +4 -3
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { lstatSync, readdirSync, realpathSync, statSync, watch, openSync, readSync, closeSync, } from 'node:fs';
|
|
2
|
+
import { join, sep } from 'node:path';
|
|
3
|
+
export const CHUNK = 12_288;
|
|
4
|
+
const fail = (code, message) => ({
|
|
5
|
+
ok: false,
|
|
6
|
+
error: { code, message },
|
|
7
|
+
});
|
|
8
|
+
export function pathParts(raw) {
|
|
9
|
+
if (typeof raw !== 'string' || raw.length > 4096)
|
|
10
|
+
return null;
|
|
11
|
+
if (raw.includes('\0') || raw.includes('\\'))
|
|
12
|
+
return null;
|
|
13
|
+
if (/^[A-Za-z]:/.test(raw))
|
|
14
|
+
return null;
|
|
15
|
+
const parts = raw.split('/').filter((p) => p !== '' && p !== '.');
|
|
16
|
+
if (parts.some((p) => p === '..'))
|
|
17
|
+
return null;
|
|
18
|
+
return parts;
|
|
19
|
+
}
|
|
20
|
+
export class FolderServer {
|
|
21
|
+
root;
|
|
22
|
+
rootReal;
|
|
23
|
+
constructor(root) {
|
|
24
|
+
this.root = root;
|
|
25
|
+
this.rootReal = realpathSync(root);
|
|
26
|
+
}
|
|
27
|
+
resolve(raw) {
|
|
28
|
+
const parts = pathParts(raw);
|
|
29
|
+
if (!parts)
|
|
30
|
+
return { refuse: fail('NoPermissions', 'That path is outside the agent’s folder.') };
|
|
31
|
+
if (parts[0] === '.git')
|
|
32
|
+
return { refuse: fail('NoPermissions', 'The repository’s .git folder is not shared.') };
|
|
33
|
+
const file = parts.length ? join(this.rootReal, ...parts) : this.rootReal;
|
|
34
|
+
let real;
|
|
35
|
+
try {
|
|
36
|
+
real = realpathSync(file);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return { refuse: fail('FileNotFound', 'No such file in the agent’s folder.') };
|
|
40
|
+
}
|
|
41
|
+
if (real !== this.rootReal && !real.startsWith(this.rootReal + sep)) {
|
|
42
|
+
return { refuse: fail('NoPermissions', 'That file points outside the agent’s folder.') };
|
|
43
|
+
}
|
|
44
|
+
const rel = real.slice(this.rootReal.length + 1).split(sep);
|
|
45
|
+
if (rel[0] === '.git')
|
|
46
|
+
return { refuse: fail('NoPermissions', 'The repository’s .git folder is not shared.') };
|
|
47
|
+
return { file: real };
|
|
48
|
+
}
|
|
49
|
+
handle(req) {
|
|
50
|
+
const r = this.resolve(req.path);
|
|
51
|
+
if ('refuse' in r)
|
|
52
|
+
return r.refuse;
|
|
53
|
+
try {
|
|
54
|
+
if (req.op === 'stat') {
|
|
55
|
+
const s = statSync(r.file);
|
|
56
|
+
return {
|
|
57
|
+
ok: true,
|
|
58
|
+
result: {
|
|
59
|
+
type: s.isDirectory() ? 'dir' : 'file',
|
|
60
|
+
size: s.size,
|
|
61
|
+
mtime: Math.floor(s.mtimeMs),
|
|
62
|
+
ctime: Math.floor(s.ctimeMs),
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (req.op === 'readDirectory') {
|
|
67
|
+
const out = [];
|
|
68
|
+
for (const name of readdirSync(r.file)) {
|
|
69
|
+
if (r.file === this.rootReal && name === '.git')
|
|
70
|
+
continue;
|
|
71
|
+
let kind = 'file';
|
|
72
|
+
try {
|
|
73
|
+
const l = lstatSync(join(r.file, name));
|
|
74
|
+
kind = l.isSymbolicLink() ? 'symlink' : l.isDirectory() ? 'dir' : 'file';
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
out.push([name, kind]);
|
|
80
|
+
}
|
|
81
|
+
return { ok: true, result: out };
|
|
82
|
+
}
|
|
83
|
+
if (req.op === 'readFile') {
|
|
84
|
+
const s = statSync(r.file);
|
|
85
|
+
if (s.isDirectory())
|
|
86
|
+
return fail('FileNotFound', 'That is a folder, not a file.');
|
|
87
|
+
const offset = typeof req.offset === 'number' && req.offset >= 0 ? Math.floor(req.offset) : 0;
|
|
88
|
+
const want = typeof req.length === 'number' && req.length > 0
|
|
89
|
+
? Math.min(Math.floor(req.length), CHUNK)
|
|
90
|
+
: CHUNK;
|
|
91
|
+
const fd = openSync(r.file, 'r');
|
|
92
|
+
try {
|
|
93
|
+
const buf = Buffer.alloc(Math.max(0, Math.min(want, s.size - offset)));
|
|
94
|
+
const n = buf.length ? readSync(fd, buf, 0, buf.length, offset) : 0;
|
|
95
|
+
return {
|
|
96
|
+
ok: true,
|
|
97
|
+
result: {
|
|
98
|
+
data: buf.subarray(0, n).toString('base64'),
|
|
99
|
+
size: s.size,
|
|
100
|
+
eof: offset + n >= s.size,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
closeSync(fd);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return fail('NoPermissions', `“${String(req.op)}” is not something this folder answers: it is read-only.`);
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
const code = err.code;
|
|
112
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
113
|
+
return fail('FileNotFound', 'No such file in the agent’s folder.');
|
|
114
|
+
if (code === 'EACCES' || code === 'EPERM')
|
|
115
|
+
return fail('NoPermissions', 'The runner cannot read that file.');
|
|
116
|
+
return fail('Unavailable', 'The runner could not read that file just now.');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
watch(onChanged, debounceMs = 300) {
|
|
120
|
+
let pending = new Set();
|
|
121
|
+
let timer = null;
|
|
122
|
+
const flush = () => {
|
|
123
|
+
timer = null;
|
|
124
|
+
const paths = [...pending];
|
|
125
|
+
pending = new Set();
|
|
126
|
+
if (paths.length)
|
|
127
|
+
onChanged(paths.slice(0, 200));
|
|
128
|
+
};
|
|
129
|
+
const onEvent = (_e, name) => {
|
|
130
|
+
if (!name)
|
|
131
|
+
return;
|
|
132
|
+
const rel = String(name).split(sep).join('/');
|
|
133
|
+
if (rel === '.git' || rel.startsWith('.git/'))
|
|
134
|
+
return;
|
|
135
|
+
pending.add(`/${rel}`);
|
|
136
|
+
if (!timer)
|
|
137
|
+
timer = setTimeout(flush, debounceMs);
|
|
138
|
+
};
|
|
139
|
+
let w = null;
|
|
140
|
+
try {
|
|
141
|
+
w = watch(this.rootReal, { recursive: true }, onEvent);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
try {
|
|
145
|
+
w = watch(this.rootReal, onEvent);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
w = null;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
w?.on('error', () => { });
|
|
152
|
+
return () => {
|
|
153
|
+
if (timer)
|
|
154
|
+
clearTimeout(timer);
|
|
155
|
+
w?.close();
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=fs-server.js.map
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type KeyObject } from 'node:crypto';
|
|
2
|
+
export declare const RELAY_PROTOCOL = "firefunc-relay-v1";
|
|
3
|
+
export declare const REPLAY_WINDOW = 1024;
|
|
4
|
+
export declare const MAX_PLAINTEXT: number;
|
|
5
|
+
export type Direction = 'e2r' | 'r2e';
|
|
6
|
+
export type Side = 'editor' | 'runner';
|
|
7
|
+
export type SessionKeyPair = {
|
|
8
|
+
publicKey: string;
|
|
9
|
+
privateKey: KeyObject;
|
|
10
|
+
};
|
|
11
|
+
export declare function newSessionKey(): SessionKeyPair;
|
|
12
|
+
export declare function newIdentityKey(): {
|
|
13
|
+
publicKey: string;
|
|
14
|
+
privateKey: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function identityPrivateKey(pkcs8B64: string): KeyObject;
|
|
17
|
+
export declare function identityPublicKey(privateKey: KeyObject): string;
|
|
18
|
+
export declare function keyIdOf(spkiB64: string): string;
|
|
19
|
+
export declare function joinText(grantId: string, editorPublicKey: string): string;
|
|
20
|
+
export declare function acceptText(grantId: string, editorPublicKey: string, runnerPublicKey: string): string;
|
|
21
|
+
export declare function signWith(privateKey: KeyObject, text: string): string;
|
|
22
|
+
export declare function verifyWith(publicKeySpkiB64: string, text: string, signature: string): boolean;
|
|
23
|
+
export type SessionKeys = {
|
|
24
|
+
grantId: string;
|
|
25
|
+
side: Side;
|
|
26
|
+
sendKey: Buffer;
|
|
27
|
+
recvKey: Buffer;
|
|
28
|
+
sendDir: Direction;
|
|
29
|
+
recvDir: Direction;
|
|
30
|
+
check: string;
|
|
31
|
+
};
|
|
32
|
+
export declare function deriveSession(input: {
|
|
33
|
+
grantId: string;
|
|
34
|
+
side: Side;
|
|
35
|
+
own: SessionKeyPair;
|
|
36
|
+
editorPublicKey: string;
|
|
37
|
+
runnerPublicKey: string;
|
|
38
|
+
}): SessionKeys | null;
|
|
39
|
+
export declare function seal(key: Buffer, grantId: string, dir: Direction, seq: number, plaintext: string): string;
|
|
40
|
+
export declare function open(key: Buffer, grantId: string, dir: Direction, frame: {
|
|
41
|
+
s: number;
|
|
42
|
+
c: string;
|
|
43
|
+
}): string | null;
|
|
44
|
+
export declare function readFrame(text: string): {
|
|
45
|
+
s: number;
|
|
46
|
+
c: string;
|
|
47
|
+
} | null;
|
|
48
|
+
export declare function relayControl(text: string): string | null;
|
|
49
|
+
export declare class ReplayWindow {
|
|
50
|
+
private highest;
|
|
51
|
+
private seen;
|
|
52
|
+
accept(seq: number): boolean;
|
|
53
|
+
}
|
|
54
|
+
export declare class SecureChannel {
|
|
55
|
+
readonly keys: SessionKeys;
|
|
56
|
+
private sendSeq;
|
|
57
|
+
private window;
|
|
58
|
+
constructor(keys: SessionKeys);
|
|
59
|
+
send(message: unknown): string;
|
|
60
|
+
receive(text: string): {
|
|
61
|
+
kind: 'message';
|
|
62
|
+
message: unknown;
|
|
63
|
+
} | {
|
|
64
|
+
kind: 'skip';
|
|
65
|
+
} | {
|
|
66
|
+
kind: 'fail';
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, hkdfSync, sign as edSign, verify as edVerify, } from 'node:crypto';
|
|
2
|
+
export const RELAY_PROTOCOL = 'firefunc-relay-v1';
|
|
3
|
+
export const REPLAY_WINDOW = 1024;
|
|
4
|
+
export const MAX_PLAINTEXT = 20 * 1024;
|
|
5
|
+
export function newSessionKey() {
|
|
6
|
+
const { publicKey, privateKey } = generateKeyPairSync('x25519');
|
|
7
|
+
return {
|
|
8
|
+
publicKey: publicKey.export({ format: 'der', type: 'spki' }).toString('base64'),
|
|
9
|
+
privateKey,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function newIdentityKey() {
|
|
13
|
+
const { publicKey, privateKey } = generateKeyPairSync('ed25519');
|
|
14
|
+
return {
|
|
15
|
+
publicKey: publicKey.export({ format: 'der', type: 'spki' }).toString('base64'),
|
|
16
|
+
privateKey: privateKey.export({ format: 'der', type: 'pkcs8' }).toString('base64'),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function identityPrivateKey(pkcs8B64) {
|
|
20
|
+
return createPrivateKey({ key: Buffer.from(pkcs8B64, 'base64'), format: 'der', type: 'pkcs8' });
|
|
21
|
+
}
|
|
22
|
+
export function identityPublicKey(privateKey) {
|
|
23
|
+
return createPublicKey(privateKey).export({ format: 'der', type: 'spki' }).toString('base64');
|
|
24
|
+
}
|
|
25
|
+
function publicKeyOf(spkiB64, type) {
|
|
26
|
+
try {
|
|
27
|
+
const k = createPublicKey({ key: Buffer.from(spkiB64, 'base64'), format: 'der', type: 'spki' });
|
|
28
|
+
return k.asymmetricKeyType === type ? k : null;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function keyIdOf(spkiB64) {
|
|
35
|
+
return createHash('sha256').update(Buffer.from(spkiB64, 'base64')).digest('hex').slice(0, 32);
|
|
36
|
+
}
|
|
37
|
+
export function joinText(grantId, editorPublicKey) {
|
|
38
|
+
return `firefunc-editor-join/v1\n${grantId}\n${editorPublicKey}`;
|
|
39
|
+
}
|
|
40
|
+
export function acceptText(grantId, editorPublicKey, runnerPublicKey) {
|
|
41
|
+
return `firefunc-editor-accept/v1\n${grantId}\n${editorPublicKey}\n${runnerPublicKey}`;
|
|
42
|
+
}
|
|
43
|
+
export function signWith(privateKey, text) {
|
|
44
|
+
return edSign(null, Buffer.from(text, 'utf8'), privateKey).toString('base64');
|
|
45
|
+
}
|
|
46
|
+
export function verifyWith(publicKeySpkiB64, text, signature) {
|
|
47
|
+
const key = publicKeyOf(publicKeySpkiB64, 'ed25519');
|
|
48
|
+
if (!key || typeof signature !== 'string')
|
|
49
|
+
return false;
|
|
50
|
+
try {
|
|
51
|
+
return edVerify(null, Buffer.from(text, 'utf8'), key, Buffer.from(signature, 'base64'));
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function deriveSession(input) {
|
|
58
|
+
const peerB64 = input.side === 'editor' ? input.runnerPublicKey : input.editorPublicKey;
|
|
59
|
+
const ownB64 = input.side === 'editor' ? input.editorPublicKey : input.runnerPublicKey;
|
|
60
|
+
if (ownB64 !== input.own.publicKey)
|
|
61
|
+
return null;
|
|
62
|
+
const peer = publicKeyOf(peerB64, 'x25519');
|
|
63
|
+
if (!peer)
|
|
64
|
+
return null;
|
|
65
|
+
const shared = diffieHellman({ privateKey: input.own.privateKey, publicKey: peer });
|
|
66
|
+
const salt = createHash('sha256').update(input.grantId, 'utf8').digest();
|
|
67
|
+
const info = Buffer.from(`${RELAY_PROTOCOL}|${input.editorPublicKey}|${input.runnerPublicKey}`, 'utf8');
|
|
68
|
+
const okm = Buffer.from(hkdfSync('sha256', shared, salt, info, 76));
|
|
69
|
+
const kE2R = okm.subarray(0, 32);
|
|
70
|
+
const kR2E = okm.subarray(32, 64);
|
|
71
|
+
const check = String(okm.readUInt32BE(64) % 1_000_000).padStart(6, '0');
|
|
72
|
+
const editor = input.side === 'editor';
|
|
73
|
+
return {
|
|
74
|
+
grantId: input.grantId,
|
|
75
|
+
side: input.side,
|
|
76
|
+
sendKey: Buffer.from(editor ? kE2R : kR2E),
|
|
77
|
+
recvKey: Buffer.from(editor ? kR2E : kE2R),
|
|
78
|
+
sendDir: editor ? 'e2r' : 'r2e',
|
|
79
|
+
recvDir: editor ? 'r2e' : 'e2r',
|
|
80
|
+
check,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function nonceOf(seq) {
|
|
84
|
+
const n = Buffer.alloc(12);
|
|
85
|
+
n.writeBigUInt64BE(BigInt(seq), 4);
|
|
86
|
+
return n;
|
|
87
|
+
}
|
|
88
|
+
function aadOf(grantId, dir) {
|
|
89
|
+
return Buffer.from(`${grantId}|${dir}`, 'utf8');
|
|
90
|
+
}
|
|
91
|
+
export function seal(key, grantId, dir, seq, plaintext) {
|
|
92
|
+
const cipher = createCipheriv('chacha20-poly1305', key, nonceOf(seq), { authTagLength: 16 });
|
|
93
|
+
cipher.setAAD(aadOf(grantId, dir), { plaintextLength: Buffer.byteLength(plaintext, 'utf8') });
|
|
94
|
+
const ct = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final(), cipher.getAuthTag()]);
|
|
95
|
+
return JSON.stringify({ s: seq, c: ct.toString('base64') });
|
|
96
|
+
}
|
|
97
|
+
export function open(key, grantId, dir, frame) {
|
|
98
|
+
try {
|
|
99
|
+
const raw = Buffer.from(frame.c, 'base64');
|
|
100
|
+
if (raw.length < 16)
|
|
101
|
+
return null;
|
|
102
|
+
const decipher = createDecipheriv('chacha20-poly1305', key, nonceOf(frame.s), {
|
|
103
|
+
authTagLength: 16,
|
|
104
|
+
});
|
|
105
|
+
decipher.setAAD(aadOf(grantId, dir), { plaintextLength: raw.length - 16 });
|
|
106
|
+
decipher.setAuthTag(raw.subarray(raw.length - 16));
|
|
107
|
+
return Buffer.concat([
|
|
108
|
+
decipher.update(raw.subarray(0, raw.length - 16)),
|
|
109
|
+
decipher.final(),
|
|
110
|
+
]).toString('utf8');
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export function readFrame(text) {
|
|
117
|
+
try {
|
|
118
|
+
const o = JSON.parse(text);
|
|
119
|
+
if (typeof o.s !== 'number' || !Number.isSafeInteger(o.s) || o.s < 0)
|
|
120
|
+
return null;
|
|
121
|
+
if (typeof o.c !== 'string')
|
|
122
|
+
return null;
|
|
123
|
+
return { s: o.s, c: o.c };
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export function relayControl(text) {
|
|
130
|
+
try {
|
|
131
|
+
const o = JSON.parse(text);
|
|
132
|
+
return typeof o.relay === 'string' ? o.relay : null;
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export class ReplayWindow {
|
|
139
|
+
highest = -1;
|
|
140
|
+
seen = new Set();
|
|
141
|
+
accept(seq) {
|
|
142
|
+
if (!Number.isSafeInteger(seq) || seq < 0)
|
|
143
|
+
return false;
|
|
144
|
+
if (seq <= this.highest - REPLAY_WINDOW)
|
|
145
|
+
return false;
|
|
146
|
+
if (this.seen.has(seq))
|
|
147
|
+
return false;
|
|
148
|
+
this.seen.add(seq);
|
|
149
|
+
if (seq > this.highest) {
|
|
150
|
+
this.highest = seq;
|
|
151
|
+
const floor = this.highest - REPLAY_WINDOW;
|
|
152
|
+
if (this.seen.size > REPLAY_WINDOW * 2) {
|
|
153
|
+
for (const s of this.seen)
|
|
154
|
+
if (s <= floor)
|
|
155
|
+
this.seen.delete(s);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export class SecureChannel {
|
|
162
|
+
keys;
|
|
163
|
+
sendSeq = 0;
|
|
164
|
+
window = new ReplayWindow();
|
|
165
|
+
constructor(keys) {
|
|
166
|
+
this.keys = keys;
|
|
167
|
+
}
|
|
168
|
+
send(message) {
|
|
169
|
+
const text = JSON.stringify(message);
|
|
170
|
+
if (Buffer.byteLength(text, 'utf8') > MAX_PLAINTEXT)
|
|
171
|
+
throw new Error('message too large');
|
|
172
|
+
return seal(this.keys.sendKey, this.keys.grantId, this.keys.sendDir, this.sendSeq++, text);
|
|
173
|
+
}
|
|
174
|
+
receive(text) {
|
|
175
|
+
const frame = readFrame(text);
|
|
176
|
+
if (!frame)
|
|
177
|
+
return { kind: 'skip' };
|
|
178
|
+
const plain = open(this.keys.recvKey, this.keys.grantId, this.keys.recvDir, frame);
|
|
179
|
+
if (plain === null)
|
|
180
|
+
return { kind: 'fail' };
|
|
181
|
+
if (!this.window.accept(frame.s))
|
|
182
|
+
return { kind: 'skip' };
|
|
183
|
+
try {
|
|
184
|
+
return { kind: 'message', message: JSON.parse(plain) };
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return { kind: 'fail' };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=relay-crypto.js.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { KeyObject } from 'node:crypto';
|
|
2
|
+
import { type WsConnection, type WsHandlers } from './ws-client.js';
|
|
3
|
+
export declare const MAX_EDITOR_SESSIONS = 4;
|
|
4
|
+
export type EditorSessionOffer = {
|
|
5
|
+
grantId: string;
|
|
6
|
+
runId: string;
|
|
7
|
+
taskId: string;
|
|
8
|
+
state: 'joined' | 'open' | 'ended';
|
|
9
|
+
editor: string;
|
|
10
|
+
by: string | null;
|
|
11
|
+
editorPublicKey: string;
|
|
12
|
+
editorSignature: string;
|
|
13
|
+
editorIdentityKey: string;
|
|
14
|
+
};
|
|
15
|
+
export type RelayTicket = {
|
|
16
|
+
url: string;
|
|
17
|
+
ticket: string;
|
|
18
|
+
expiresAt: string | null;
|
|
19
|
+
};
|
|
20
|
+
export type RefuseReason = 'editor_off' | 'no_folder' | 'bad_signature' | 'busy';
|
|
21
|
+
export type EditorSessionApi = {
|
|
22
|
+
acceptEditorGrant(grantId: string, body: {
|
|
23
|
+
publicKey: string;
|
|
24
|
+
signature: string;
|
|
25
|
+
machineKey: string;
|
|
26
|
+
}): Promise<RelayTicket | null>;
|
|
27
|
+
editorTicket(grantId: string): Promise<RelayTicket | null>;
|
|
28
|
+
refuseEditorGrant(grantId: string, reason: RefuseReason): Promise<boolean>;
|
|
29
|
+
endEditorGrant(grantId: string, reason: 'runner_ended' | 'task_ended'): Promise<boolean>;
|
|
30
|
+
};
|
|
31
|
+
export type EditorSessionDeps = {
|
|
32
|
+
api: EditorSessionApi;
|
|
33
|
+
allowed: () => boolean;
|
|
34
|
+
worktreeOf: (runId: string) => string | null;
|
|
35
|
+
machineKey: () => {
|
|
36
|
+
publicKey: string;
|
|
37
|
+
privateKey: KeyObject;
|
|
38
|
+
};
|
|
39
|
+
machineName: string;
|
|
40
|
+
log: (line: string) => void;
|
|
41
|
+
dial?: (url: string, handlers: WsHandlers) => Promise<WsConnection>;
|
|
42
|
+
now?: () => number;
|
|
43
|
+
};
|
|
44
|
+
export declare function readOffer(raw: unknown): EditorSessionOffer | null;
|
|
45
|
+
export declare function createEditorSessions(deps: EditorSessionDeps): {
|
|
46
|
+
take(list: unknown[]): void;
|
|
47
|
+
active(): number;
|
|
48
|
+
checkOf(grantId: string): string | null;
|
|
49
|
+
closeAll(why?: string): void;
|
|
50
|
+
};
|