@peer2peer/codex-bridge 0.0.0-stage → 0.1.1
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/LICENSE +201 -0
- package/README.md +216 -2
- package/dist/bridge.d.ts +423 -0
- package/dist/bridge.js +1218 -0
- package/dist/commands.d.ts +47 -0
- package/dist/commands.js +141 -0
- package/dist/confirm.d.ts +68 -0
- package/dist/confirm.js +248 -0
- package/dist/hook.d.ts +33 -0
- package/dist/hook.js +242 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +18 -0
- package/dist/install.d.ts +76 -0
- package/dist/install.js +264 -0
- package/dist/ipc.d.ts +69 -0
- package/dist/ipc.js +302 -0
- package/dist/knobs.d.ts +266 -0
- package/dist/knobs.js +1020 -0
- package/dist/lib.d.ts +438 -0
- package/dist/lib.js +1690 -0
- package/dist/mcp.d.ts +66 -0
- package/dist/mcp.js +181 -0
- package/dist/p2p.d.ts +176 -0
- package/dist/p2p.js +149 -0
- package/dist/server.d.ts +41 -0
- package/dist/server.js +278 -0
- package/dist/tools.d.ts +22 -0
- package/dist/tools.js +277 -0
- package/dist/trusted.d.ts +59 -0
- package/dist/trusted.js +217 -0
- package/package.json +48 -4
package/dist/ipc.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type Server, type Socket } from "node:net";
|
|
2
|
+
export type Entry = {
|
|
3
|
+
v: 1;
|
|
4
|
+
/** The bridge (MCP server) process. */
|
|
5
|
+
pid: number;
|
|
6
|
+
/** Its parent: the Codex process (or app-server) that started it. */
|
|
7
|
+
codexPid: number;
|
|
8
|
+
sock: string;
|
|
9
|
+
sid: string;
|
|
10
|
+
ref?: string;
|
|
11
|
+
threadId?: string;
|
|
12
|
+
cwd: string;
|
|
13
|
+
label?: string;
|
|
14
|
+
startedAt: number;
|
|
15
|
+
activeAt: number;
|
|
16
|
+
};
|
|
17
|
+
export declare function registryDir(home: string): string;
|
|
18
|
+
/** Writes this bridge's entry atomically (0600). */
|
|
19
|
+
export declare function writeEntry(home: string, entry: Entry): void;
|
|
20
|
+
export declare function removeEntry(home: string, pid: number): void;
|
|
21
|
+
export declare function isAlive(pid: number): boolean;
|
|
22
|
+
/** Live entries; files a crashed bridge left behind are removed. */
|
|
23
|
+
export declare function readEntries(home: string, alive?: (pid: number) => boolean): Entry[];
|
|
24
|
+
/** A socket path for this bridge: under P2P_HOME when short enough, else a private temp folder. */
|
|
25
|
+
export declare function socketPath(home: string, pid: number, platform?: string): string;
|
|
26
|
+
/** Serves one JSON request per connection. */
|
|
27
|
+
export declare function serve(sock: string, handle: (req: Record<string, unknown>, conn: Socket) => Promise<unknown>): Promise<Server>;
|
|
28
|
+
/** One request to a bridge; rejects on timeout or a closed socket. */
|
|
29
|
+
export declare function ask<T = Record<string, unknown>>(sock: string, req: Record<string, unknown>, timeoutMs?: number): Promise<T>;
|
|
30
|
+
/** This process's ancestors, nearest first (at most `depth`). */
|
|
31
|
+
export declare function ancestors(depth?: number): Promise<number[]>;
|
|
32
|
+
/**
|
|
33
|
+
* Which bridge a hook belongs to:
|
|
34
|
+
* 1. the one bound to the hook's `session_id` (a bridge binds only to the
|
|
35
|
+
* thread a tool call's _meta names, never to what a hook says);
|
|
36
|
+
* 2. else the one unbound bridge under the same Codex process. With more
|
|
37
|
+
* than one there, none: a guess could hand one session's messages to
|
|
38
|
+
* another, so the hook does nothing until a tool call binds them;
|
|
39
|
+
* 3. on Windows only (no process tree to go by), the one unbound bridge
|
|
40
|
+
* there is.
|
|
41
|
+
* Undefined when none fits: the hook then does nothing.
|
|
42
|
+
*/
|
|
43
|
+
export declare function findEntry(entries: readonly Entry[], o: {
|
|
44
|
+
sessionId?: string;
|
|
45
|
+
ancestors: readonly number[];
|
|
46
|
+
cwd?: string;
|
|
47
|
+
platform?: string;
|
|
48
|
+
}): Entry | undefined;
|
|
49
|
+
/** The process on the other end of an accepted local socket, where the OS says (macOS lsof, Linux ss); else undefined. */
|
|
50
|
+
export declare function peerPid(conn: Socket, run?: (cmd: string, args: string[]) => Promise<string>): Promise<number | undefined>;
|
|
51
|
+
/**
|
|
52
|
+
* macOS `lsof -U -F pfdn`: our accepted socket (this pid, this fd) has a
|
|
53
|
+
* kernel address `d`; the peer's record names it as `n->0x…`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function parseLsofPeer(out: string, pid: number, fd: number): number | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Linux `ss -xpn`: the line whose peer inode is ours names the peer process
|
|
58
|
+
* (`pid=…`). Socket paths may contain spaces, so columns aren't counted from
|
|
59
|
+
* the left: the process column (`users:(…)`) is cut off the end, and the
|
|
60
|
+
* peer's inode is then the last field.
|
|
61
|
+
*/
|
|
62
|
+
export declare function parseSsPeer(out: string, inode: string): number | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Whether `pid` was started (at any depth) by the Codex process that runs
|
|
65
|
+
* this bridge, by the bridge itself, or by any process named `codex`. Such
|
|
66
|
+
* a caller may be the model's own (sandboxed) command, so it can't stand in
|
|
67
|
+
* for the person.
|
|
68
|
+
*/
|
|
69
|
+
export declare function descendsFromCodex(pid: number, codexPids: readonly number[], depth?: number): Promise<boolean>;
|
package/dist/ipc.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// How short-lived processes reach the bridge of their Codex session:
|
|
4
|
+
// - every bridge (MCP server process) writes a registry entry,
|
|
5
|
+
// `<P2P_HOME>/codex/sessions/<pid>.json` (0600, in a 0700 folder), and
|
|
6
|
+
// listens on a local socket (a Unix socket, or a named pipe on Windows);
|
|
7
|
+
// - a Codex hook finds its session's entry by the hook's `session_id`
|
|
8
|
+
// (once a bridge knows its thread), else by the Codex process both
|
|
9
|
+
// descend from, and asks over the socket;
|
|
10
|
+
// - `p2p codex held|release|decline|sessions`, run by the person in their
|
|
11
|
+
// own terminal, reach every live bridge the same way.
|
|
12
|
+
// One JSON line each way per connection.
|
|
13
|
+
import { execFile } from "node:child_process";
|
|
14
|
+
import { chmodSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { randomBytes } from "node:crypto";
|
|
16
|
+
import { createConnection, createServer } from "node:net";
|
|
17
|
+
import { tmpdir } from "node:os";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
import { safeBinary, safeExecEnv } from "./knobs.js";
|
|
20
|
+
export function registryDir(home) {
|
|
21
|
+
return join(home, "codex", "sessions");
|
|
22
|
+
}
|
|
23
|
+
function ensurePrivateDir(dir) {
|
|
24
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
25
|
+
try {
|
|
26
|
+
chmodSync(dir, 0o700);
|
|
27
|
+
}
|
|
28
|
+
catch { }
|
|
29
|
+
}
|
|
30
|
+
/** Writes this bridge's entry atomically (0600). */
|
|
31
|
+
export function writeEntry(home, entry) {
|
|
32
|
+
const dir = registryDir(home);
|
|
33
|
+
ensurePrivateDir(dir);
|
|
34
|
+
const file = join(dir, `${entry.pid}.json`);
|
|
35
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
36
|
+
writeFileSync(tmp, JSON.stringify(entry), { mode: 0o600 });
|
|
37
|
+
renameSync(tmp, file);
|
|
38
|
+
}
|
|
39
|
+
export function removeEntry(home, pid) {
|
|
40
|
+
rmSync(join(registryDir(home), `${pid}.json`), { force: true });
|
|
41
|
+
}
|
|
42
|
+
export function isAlive(pid) {
|
|
43
|
+
try {
|
|
44
|
+
process.kill(pid, 0);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
return e.code === "EPERM";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Live entries; files a crashed bridge left behind are removed. */
|
|
52
|
+
export function readEntries(home, alive = isAlive) {
|
|
53
|
+
const dir = registryDir(home);
|
|
54
|
+
let names;
|
|
55
|
+
try {
|
|
56
|
+
names = readdirSync(dir).filter((n) => /^\d+\.json$/.test(n));
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
const out = [];
|
|
62
|
+
for (const name of names) {
|
|
63
|
+
try {
|
|
64
|
+
const e = JSON.parse(readFileSync(join(dir, name), "utf8"));
|
|
65
|
+
if (e.v !== 1 || typeof e.pid !== "number" || typeof e.sock !== "string" || typeof e.sid !== "string")
|
|
66
|
+
continue;
|
|
67
|
+
if (!alive(e.pid)) {
|
|
68
|
+
rmSync(join(dir, name), { force: true });
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
out.push(e);
|
|
72
|
+
}
|
|
73
|
+
catch { }
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
/** A socket path for this bridge: under P2P_HOME when short enough, else a private temp folder. */
|
|
78
|
+
export function socketPath(home, pid, platform = process.platform) {
|
|
79
|
+
// A random suffix, recorded only in the 0600 registry: a pipe name nobody can predict and squat first.
|
|
80
|
+
if (platform === "win32")
|
|
81
|
+
return `\\\\.\\pipe\\p2p-codex-${pid}-${randomBytes(16).toString("hex")}`;
|
|
82
|
+
const dir = registryDir(home);
|
|
83
|
+
ensurePrivateDir(dir);
|
|
84
|
+
const p = join(dir, `${pid}.sock`);
|
|
85
|
+
// sun_path is 104 bytes on macOS, 108 on Linux.
|
|
86
|
+
if (Buffer.byteLength(p) < 100)
|
|
87
|
+
return p;
|
|
88
|
+
return join(mkdtempSync(join(tmpdir(), "p2p-codex-")), "s.sock");
|
|
89
|
+
}
|
|
90
|
+
/** Serves one JSON request per connection. */
|
|
91
|
+
export function serve(sock, handle) {
|
|
92
|
+
if (process.platform !== "win32")
|
|
93
|
+
rmSync(sock, { force: true });
|
|
94
|
+
const server = createServer((conn) => {
|
|
95
|
+
let buf = "";
|
|
96
|
+
conn.setEncoding("utf8");
|
|
97
|
+
conn.on("error", () => { });
|
|
98
|
+
conn.on("data", (chunk) => {
|
|
99
|
+
buf += chunk;
|
|
100
|
+
if (buf.length > 1_000_000)
|
|
101
|
+
return conn.destroy();
|
|
102
|
+
const nl = buf.indexOf("\n");
|
|
103
|
+
if (nl < 0)
|
|
104
|
+
return;
|
|
105
|
+
const line = buf.slice(0, nl);
|
|
106
|
+
buf = "";
|
|
107
|
+
let req;
|
|
108
|
+
try {
|
|
109
|
+
req = JSON.parse(line);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
conn.end(JSON.stringify({ ok: false, reason: "bad request" }) + "\n");
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
handle(req, conn)
|
|
116
|
+
.then((res) => conn.end(JSON.stringify(res) + "\n"))
|
|
117
|
+
.catch((e) => conn.end(JSON.stringify({ ok: false, reason: String(e?.message ?? e) }) + "\n"));
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
server.once("error", reject);
|
|
122
|
+
server.listen(sock, () => {
|
|
123
|
+
if (process.platform !== "win32") {
|
|
124
|
+
try {
|
|
125
|
+
chmodSync(sock, 0o600);
|
|
126
|
+
}
|
|
127
|
+
catch { }
|
|
128
|
+
}
|
|
129
|
+
server.unref();
|
|
130
|
+
resolve(server);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/** One request to a bridge; rejects on timeout or a closed socket. */
|
|
135
|
+
export function ask(sock, req, timeoutMs = 5_000) {
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
const conn = createConnection(sock);
|
|
138
|
+
let buf = "";
|
|
139
|
+
const timer = setTimeout(() => {
|
|
140
|
+
conn.destroy();
|
|
141
|
+
reject(new Error("timed out"));
|
|
142
|
+
}, timeoutMs);
|
|
143
|
+
conn.setEncoding("utf8");
|
|
144
|
+
conn.on("connect", () => conn.write(JSON.stringify(req) + "\n"));
|
|
145
|
+
conn.on("data", (chunk) => (buf += chunk));
|
|
146
|
+
conn.on("error", (e) => {
|
|
147
|
+
clearTimeout(timer);
|
|
148
|
+
reject(e);
|
|
149
|
+
});
|
|
150
|
+
conn.on("close", () => {
|
|
151
|
+
clearTimeout(timer);
|
|
152
|
+
try {
|
|
153
|
+
resolve(JSON.parse(buf.trim()));
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
reject(new Error("no answer"));
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/** The parent of `pid` (POSIX `ps`), or undefined. */
|
|
162
|
+
function parentOf(pid) {
|
|
163
|
+
return new Promise((resolve) => {
|
|
164
|
+
execFile("/bin/ps", ["-o", "ppid=", "-p", String(pid)], { timeout: 2_000, env: safeExecEnv() }, (err, stdout) => {
|
|
165
|
+
const n = Number(String(stdout).trim());
|
|
166
|
+
resolve(err || !Number.isInteger(n) || n <= 1 ? undefined : n);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/** This process's ancestors, nearest first (at most `depth`). */
|
|
171
|
+
export async function ancestors(depth = 8) {
|
|
172
|
+
const out = [];
|
|
173
|
+
let pid = process.ppid;
|
|
174
|
+
while (pid && pid > 1 && out.length < depth) {
|
|
175
|
+
out.push(pid);
|
|
176
|
+
if (process.platform === "win32")
|
|
177
|
+
break;
|
|
178
|
+
pid = await parentOf(pid);
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Which bridge a hook belongs to:
|
|
184
|
+
* 1. the one bound to the hook's `session_id` (a bridge binds only to the
|
|
185
|
+
* thread a tool call's _meta names, never to what a hook says);
|
|
186
|
+
* 2. else the one unbound bridge under the same Codex process. With more
|
|
187
|
+
* than one there, none: a guess could hand one session's messages to
|
|
188
|
+
* another, so the hook does nothing until a tool call binds them;
|
|
189
|
+
* 3. on Windows only (no process tree to go by), the one unbound bridge
|
|
190
|
+
* there is.
|
|
191
|
+
* Undefined when none fits: the hook then does nothing.
|
|
192
|
+
*/
|
|
193
|
+
export function findEntry(entries, o) {
|
|
194
|
+
if (o.sessionId) {
|
|
195
|
+
const bound = entries.find((e) => e.threadId === o.sessionId);
|
|
196
|
+
if (bound)
|
|
197
|
+
return bound;
|
|
198
|
+
}
|
|
199
|
+
const free = entries.filter((e) => !e.threadId);
|
|
200
|
+
const related = free.filter((e) => o.ancestors.includes(e.codexPid) || o.ancestors.includes(e.pid));
|
|
201
|
+
if (related.length > 0)
|
|
202
|
+
return related.length === 1 ? related[0] : undefined;
|
|
203
|
+
if ((o.platform ?? process.platform) === "win32" && free.length === 1)
|
|
204
|
+
return free[0];
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
// ── who is calling: the person, or something Codex started? ────────────────
|
|
208
|
+
/** The process on the other end of an accepted local socket, where the OS says (macOS lsof, Linux ss); else undefined. */
|
|
209
|
+
export async function peerPid(conn, run = runOut) {
|
|
210
|
+
const fd = conn._handle?.fd;
|
|
211
|
+
if (typeof fd !== "number" || fd < 0)
|
|
212
|
+
return undefined;
|
|
213
|
+
try {
|
|
214
|
+
if (process.platform === "darwin")
|
|
215
|
+
return parseLsofPeer(await run("lsof", ["-nP", "-U", "-F", "pfdn"]), process.pid, fd);
|
|
216
|
+
if (process.platform === "linux") {
|
|
217
|
+
const link = await run("readlink", [`/proc/self/fd/${fd}`]);
|
|
218
|
+
const inode = /socket:\[(\d+)\]/.exec(link)?.[1];
|
|
219
|
+
return inode ? parseSsPeer(await run("ss", ["-xpn"]), inode) : undefined;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
catch { }
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* macOS `lsof -U -F pfdn`: our accepted socket (this pid, this fd) has a
|
|
227
|
+
* kernel address `d`; the peer's record names it as `n->0x…`.
|
|
228
|
+
*/
|
|
229
|
+
export function parseLsofPeer(out, pid, fd) {
|
|
230
|
+
const recs = [];
|
|
231
|
+
let cur = null;
|
|
232
|
+
let p = "";
|
|
233
|
+
for (const line of out.split("\n")) {
|
|
234
|
+
if (!line)
|
|
235
|
+
continue;
|
|
236
|
+
const k = line[0];
|
|
237
|
+
const v = line.slice(1);
|
|
238
|
+
if (k === "p")
|
|
239
|
+
p = v;
|
|
240
|
+
else if (k === "f")
|
|
241
|
+
recs.push((cur = { pid: p, fd: v }));
|
|
242
|
+
else if (cur && (k === "d" || k === "n"))
|
|
243
|
+
cur[k] = v;
|
|
244
|
+
}
|
|
245
|
+
const mine = recs.find((r) => r.pid === String(pid) && r.fd === String(fd));
|
|
246
|
+
if (!mine?.d)
|
|
247
|
+
return undefined;
|
|
248
|
+
const peer = recs.find((r) => r.n === `->${mine.d}` && r.pid !== String(pid));
|
|
249
|
+
const n = peer ? Number(peer.pid) : NaN;
|
|
250
|
+
return Number.isInteger(n) && n > 0 ? n : undefined;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Linux `ss -xpn`: the line whose peer inode is ours names the peer process
|
|
254
|
+
* (`pid=…`). Socket paths may contain spaces, so columns aren't counted from
|
|
255
|
+
* the left: the process column (`users:(…)`) is cut off the end, and the
|
|
256
|
+
* peer's inode is then the last field.
|
|
257
|
+
*/
|
|
258
|
+
export function parseSsPeer(out, inode) {
|
|
259
|
+
for (const line of out.split("\n")) {
|
|
260
|
+
const at = line.indexOf(" users:((");
|
|
261
|
+
if (at < 0)
|
|
262
|
+
continue;
|
|
263
|
+
const fields = line.slice(0, at).trim().split(/\s+/);
|
|
264
|
+
if (fields.length < 8 || fields[fields.length - 1] !== inode)
|
|
265
|
+
continue;
|
|
266
|
+
const m = /pid=(\d+)/.exec(line.slice(at));
|
|
267
|
+
if (m)
|
|
268
|
+
return Number(m[1]);
|
|
269
|
+
}
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
function runOut(cmd, args) {
|
|
273
|
+
return new Promise((resolve, reject) => {
|
|
274
|
+
// By absolute path from the system folders, with a scrubbed environment: never the project's PATH.
|
|
275
|
+
const bin = cmd.startsWith("/") ? cmd : safeBinary(cmd);
|
|
276
|
+
if (!bin)
|
|
277
|
+
return reject(new Error(`${cmd} not found in the system folders`));
|
|
278
|
+
execFile(bin, args, { timeout: 5_000, maxBuffer: 20_000_000, env: safeExecEnv() }, (err, stdout) => (err ? reject(err) : resolve(String(stdout))));
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/** The command name of `pid` (POSIX `ps`), or undefined. */
|
|
282
|
+
function commOf(pid) {
|
|
283
|
+
return runOut("ps", ["-o", "comm=", "-p", String(pid)]).then((s) => s.trim() || undefined, () => undefined);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Whether `pid` was started (at any depth) by the Codex process that runs
|
|
287
|
+
* this bridge, by the bridge itself, or by any process named `codex`. Such
|
|
288
|
+
* a caller may be the model's own (sandboxed) command, so it can't stand in
|
|
289
|
+
* for the person.
|
|
290
|
+
*/
|
|
291
|
+
export async function descendsFromCodex(pid, codexPids, depth = 24) {
|
|
292
|
+
let cur = pid;
|
|
293
|
+
for (let i = 0; cur && cur > 1 && i < depth; i++) {
|
|
294
|
+
if (codexPids.includes(cur))
|
|
295
|
+
return true;
|
|
296
|
+
const comm = await commOf(cur);
|
|
297
|
+
if (comm && /(^|\/)codex(\.exe)?$/i.test(comm))
|
|
298
|
+
return true;
|
|
299
|
+
cur = await parentOf(cur);
|
|
300
|
+
}
|
|
301
|
+
return false;
|
|
302
|
+
}
|
package/dist/knobs.d.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/** Knobs taken only from the user-level config. */
|
|
2
|
+
export declare const GUARDED_KNOBS: readonly ["P2P_AUTO_DELIVER", "P2P_CODEX_CONFIRM_TTY", "P2P_CODEX_CONFIRM_RATE", "P2P_CODEX_QUEUE", "P2P_CODEX_BIN", "P2P_CODEX_META_LOG", "P2P_BIN", "P2P_HOME", "P2P_RELAY_TRANSPORT", "P2P_DEFAULT_RELAYS", "IX_BIN"];
|
|
3
|
+
/** Knobs that only tighten: honoured from the environment too. */
|
|
4
|
+
export declare const TIGHTEN_ONLY: readonly ["P2P_SHARE_STATUS", "IX_PEERS_SHARE_STATUS"];
|
|
5
|
+
export type Env = Record<string, string | undefined>;
|
|
6
|
+
/** Where nothing trusted may live: temp folders, Codex-granted folders, the project. */
|
|
7
|
+
export type Where = {
|
|
8
|
+
projectDirs: readonly string[];
|
|
9
|
+
temp: readonly string[];
|
|
10
|
+
granted?: readonly string[];
|
|
11
|
+
/** The person's home folder (default userHome): a project dir that is exactly it doesn't cover ~/.codex or ~/.p2p. */
|
|
12
|
+
home?: string;
|
|
13
|
+
};
|
|
14
|
+
/** The default P2P home's folder name under the person's home. */
|
|
15
|
+
export declare const DEFAULT_P2P_DIR = ".p2p";
|
|
16
|
+
/**
|
|
17
|
+
* The string values under `table` (a dotted path such as
|
|
18
|
+
* `mcp_servers.peer2peer.env`) in a TOML document: from `[table]`, from an
|
|
19
|
+
* inline `env = { … }` in the parent table, and from dotted keys. Only what
|
|
20
|
+
* Codex's config can hold there (string values) is read; null when the text
|
|
21
|
+
* can't be read with confidence (the caller then trusts nothing from it).
|
|
22
|
+
* Values that span lines (multi-line strings, arrays, inline tables) are
|
|
23
|
+
* skipped whole, so their contents are never read as keys; one under our
|
|
24
|
+
* table means null.
|
|
25
|
+
*/
|
|
26
|
+
export declare function readTomlTable(text: string, table: string[], touched?: () => void): Record<string, string> | null;
|
|
27
|
+
/**
|
|
28
|
+
* Whether a TOML document sets anything under `table` (a header, a key, an
|
|
29
|
+
* inline table, a value spanning lines), matched on decoded keys (quoted,
|
|
30
|
+
* literal and \u-escaped spellings alike). Fails closed: true whenever the
|
|
31
|
+
* text can't be read with confidence, for any reason (a value the reader
|
|
32
|
+
* doesn't take, such as a `"""` string or an array under the table, or text
|
|
33
|
+
* it can't parse at all).
|
|
34
|
+
*/
|
|
35
|
+
export declare function touchesTable(text: string, table: string[]): boolean;
|
|
36
|
+
/** The parent process's value of `name`, where the OS shows it (Linux /proc, macOS ps -E); undefined when it can't be told. */
|
|
37
|
+
export declare function parentEnvValue(name: string, ppid?: number): Promise<string | undefined | null>;
|
|
38
|
+
/**
|
|
39
|
+
* The working folder of Codex itself (this server's parent), where the OS
|
|
40
|
+
* shows it (Linux /proc, macOS lsof): a project config can move the
|
|
41
|
+
* server's own cwd (`mcp_servers.peer2peer.cwd`), not Codex's.
|
|
42
|
+
*/
|
|
43
|
+
export declare function parentCwd(ppid?: number): Promise<string | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* One variable from macOS `ps -E -o command=` output, given the plain
|
|
46
|
+
* `ps -o command=` output of the same process: ps appends the environment
|
|
47
|
+
* after the command and its arguments, so only what follows that prefix is
|
|
48
|
+
* read (a ` CODEX_HOME=` in an argument never counts). A value runs to the
|
|
49
|
+
* next ` NAME=` or the end, so paths with spaces survive. undefined ("can't
|
|
50
|
+
* tell") when the prefix doesn't match, the environment is hidden (no HOME=;
|
|
51
|
+
* macOS hides it for platform binaries such as /bin/sh), or the variable
|
|
52
|
+
* appears more than once; null when it is plainly not set.
|
|
53
|
+
*/
|
|
54
|
+
export declare function envFromPs(withEnv: string, command: string, name: string): string | undefined | null;
|
|
55
|
+
/**
|
|
56
|
+
* P2P_* variables a child never gets: they load code (extensions), point
|
|
57
|
+
* the CLI at other files, or change what this session tells peers
|
|
58
|
+
* (P2P_STATUS_CAPS, review L2: which status fields go on the wire), and a
|
|
59
|
+
* project config can set them in the server's env. (The guarded knobs come
|
|
60
|
+
* from the user-level config; status caps from `"statusCaps"` in the p2p
|
|
61
|
+
* home's config.json.)
|
|
62
|
+
*/
|
|
63
|
+
export declare const CHILD_P2P_DENY: readonly ["P2P_PERSON_TOKEN", "P2P_EXTENSION_PATH", "P2P_EXTENSIONS", "P2P_EXTENSION_EXCLUDE", "P2P_DEFAULTS_FILE", "P2P_WRITE_VECTORS", "P2P_GO_VECTORS", "P2P_STATUS_CAPS"];
|
|
64
|
+
/**
|
|
65
|
+
* The environment for the programs the bridge runs (`p2p`, `p2p watch`,
|
|
66
|
+
* `codex queue`), built from an allowlist: a fixed PATH (the node running
|
|
67
|
+
* this server, then the system folders), HOME (USERPROFILE on Windows)
|
|
68
|
+
* from the user database, P2P_* (minus CHILD_P2P_DENY), LANG, LC_*, TZ,
|
|
69
|
+
* TERM, and SYSTEMROOT/APPDATA/LOCALAPPDATA on Windows, plus `extra`.
|
|
70
|
+
* Nothing else: no NODE_OPTIONS, NODE_TLS_REJECT_UNAUTHORIZED, proxies,
|
|
71
|
+
* NODE_ICU_DATA, GCONV_PATH, LD_* or DYLD_*. Names compare
|
|
72
|
+
* case-insensitively, as on Windows.
|
|
73
|
+
*/
|
|
74
|
+
export declare function childEnv(env: Env, extra?: Env): NodeJS.ProcessEnv;
|
|
75
|
+
/** System folders only: never the PATH a project config may have put in the server's env. */
|
|
76
|
+
export declare const SAFE_PATH_DIRS: string[];
|
|
77
|
+
/**
|
|
78
|
+
* A scrubbed environment for helper programs (git, ps, lsof, ss, getconf):
|
|
79
|
+
* a fixed system PATH, the person's home from the user database, and
|
|
80
|
+
* nothing else (no GIT_*, no project-set PATH, no LD_* or DYLD_*).
|
|
81
|
+
*/
|
|
82
|
+
export declare function safeExecEnv(): NodeJS.ProcessEnv;
|
|
83
|
+
/**
|
|
84
|
+
* A program by absolute path, looked up in fixed folders (the system ones,
|
|
85
|
+
* then `extra`), never through the environment's PATH; undefined when none
|
|
86
|
+
* has it.
|
|
87
|
+
*/
|
|
88
|
+
export declare function safeBinary(name: string, extra?: readonly string[]): string | undefined;
|
|
89
|
+
/** git by absolute path: the system's, else Homebrew's or /usr/local's, else Git for Windows'. */
|
|
90
|
+
export declare function gitBinary(): string | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* Folders an installed `codex` usually lives in (npm, Homebrew, cargo, a
|
|
93
|
+
* user bin), for a bare `codex` when P2P_CODEX_BIN isn't an absolute path.
|
|
94
|
+
*/
|
|
95
|
+
export declare function codexBinDirs(home?: string): string[];
|
|
96
|
+
/**
|
|
97
|
+
* The person's home folder from the user database, never $HOME (a project
|
|
98
|
+
* config can set HOME in the server's env, which would move ~/.codex,
|
|
99
|
+
* the P2P home and the pin file).
|
|
100
|
+
*/
|
|
101
|
+
export declare function userHome(): string;
|
|
102
|
+
/** Why the environment's HOME (USERPROFILE on Windows) isn't the person's home folder; undefined when it is or isn't set. */
|
|
103
|
+
export declare function homeRefusal(env: Env, home?: string): string | undefined;
|
|
104
|
+
/** macOS's per-user temp folder (/var/folders/…/T) from confstr, never from TMPDIR; undefined elsewhere. */
|
|
105
|
+
export declare function darwinUserTempDir(): string | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* Folders anyone, or a sandboxed command, can write: the system temp
|
|
108
|
+
* folders and the person's own (macOS confstr, Windows' AppData\Local\Temp),
|
|
109
|
+
* which TMPDIR can't take away. TMPDIR values given (and os.tmpdir()) are
|
|
110
|
+
* added too: they only ever add folders.
|
|
111
|
+
*/
|
|
112
|
+
export declare function tempRoots(tmp?: readonly (string | undefined)[]): string[];
|
|
113
|
+
/** Folders Codex itself lets the sandbox write under a Codex home (its memories), for each Codex home given and ~/.codex. */
|
|
114
|
+
export declare function codexGranted(codexHomes: readonly string[], home?: string): string[];
|
|
115
|
+
/**
|
|
116
|
+
* An absolute path with symlinks resolved (macOS's /var is /private/var, and
|
|
117
|
+
* git reports the resolved toplevel). A file not written yet resolves by its
|
|
118
|
+
* folder.
|
|
119
|
+
*/
|
|
120
|
+
export declare function realish(path: string): string;
|
|
121
|
+
/**
|
|
122
|
+
* Why `path` lies where a project config or a sandboxed command could put
|
|
123
|
+
* files (a temp folder, the project), symlinks resolved on both sides; else
|
|
124
|
+
* undefined.
|
|
125
|
+
*/
|
|
126
|
+
export declare function unsafeLocation(path: string, o: Where): string | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* Why a folder (and its config.toml, when there) may hold someone else's
|
|
129
|
+
* files: not a folder, not this user's, or world-writable; else undefined.
|
|
130
|
+
* A symlinked config.toml (stow, chezmoi, Nix) is judged by its target: a
|
|
131
|
+
* regular file outside temp folders, Codex-granted folders and the project
|
|
132
|
+
* (`where`), owned by the person (or by root, as in /nix/store), and
|
|
133
|
+
* writable by no group or others. A missing folder counts only without
|
|
134
|
+
* `allowMissing`.
|
|
135
|
+
*/
|
|
136
|
+
export declare function notPrivate(dir: string, o?: {
|
|
137
|
+
allowMissing?: boolean;
|
|
138
|
+
where?: Where;
|
|
139
|
+
}): string | undefined;
|
|
140
|
+
/**
|
|
141
|
+
* Why someone other than the person (or root) could replace `file`: a
|
|
142
|
+
* folder above it isn't owned by the person or root, or is group- or
|
|
143
|
+
* world-writable without the sticky bit; else undefined. `file` is taken as
|
|
144
|
+
* resolved (no symlinks on its path). Not checked on Windows.
|
|
145
|
+
*/
|
|
146
|
+
export declare function foldersAboveProblem(file: string): string | undefined;
|
|
147
|
+
/**
|
|
148
|
+
* The CODEX_HOMEs `p2p codex install --write` pinned, one per line, in a
|
|
149
|
+
* file at a fixed place outside any project (codex/codex-homes in the
|
|
150
|
+
* default P2P home, with ~ from the user database): not under the
|
|
151
|
+
* environment's P2P_HOME or HOME, which a project config can set too.
|
|
152
|
+
*/
|
|
153
|
+
export declare function pinFile(home?: string): string;
|
|
154
|
+
/**
|
|
155
|
+
* The pinned CODEX_HOMEs: only from a regular file of this user that nobody
|
|
156
|
+
* else can write, opened without following a symlink and checked on the
|
|
157
|
+
* open descriptor, in folders (the P2P home and its codex/) of this user
|
|
158
|
+
* that nobody else can write; [] otherwise.
|
|
159
|
+
*/
|
|
160
|
+
export declare function readPins(file?: string): string[];
|
|
161
|
+
/**
|
|
162
|
+
* Records (or with `remove`, drops) a CODEX_HOME in the pin file; the
|
|
163
|
+
* person's `p2p codex install --write` only. Its folders are made (or
|
|
164
|
+
* tightened to) 0700; the new file is written through a fresh temp file
|
|
165
|
+
* (O_EXCL, no symlink followed) and renamed into place.
|
|
166
|
+
*/
|
|
167
|
+
export declare function writePin(codexHome: string, o?: {
|
|
168
|
+
file?: string;
|
|
169
|
+
remove?: boolean;
|
|
170
|
+
}): boolean;
|
|
171
|
+
export type EffectiveEnv = {
|
|
172
|
+
env: Env;
|
|
173
|
+
codexHome: string;
|
|
174
|
+
/** `cwd` in the user-level `[mcp_servers.peer2peer]` table, when set (the person moved the server on purpose). */
|
|
175
|
+
serverCwd?: string;
|
|
176
|
+
/** The P2P_HOME the bridge uses (also set in `env`, so every `p2p` it runs uses it, whatever HOME says). */
|
|
177
|
+
p2pHome: string;
|
|
178
|
+
warnings: string[];
|
|
179
|
+
/** Why the bridge must hold everything and refuse every release (see locationPolicy), or undefined. */
|
|
180
|
+
refusal?: string;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* The environment the bridge (and every `p2p` it runs) uses: guarded knobs
|
|
184
|
+
* from the user-level config only, tighten-only knobs from either place,
|
|
185
|
+
* everything else as given.
|
|
186
|
+
*/
|
|
187
|
+
export declare function effectiveEnv(o: {
|
|
188
|
+
env: Env;
|
|
189
|
+
/** The project a project config lives in (cwd, and the repo root when known). */
|
|
190
|
+
projectDirs: readonly string[];
|
|
191
|
+
readConfig?: (path: string) => Promise<string | null>;
|
|
192
|
+
parentValue?: (name: string) => Promise<string | undefined | null>;
|
|
193
|
+
home?: string;
|
|
194
|
+
/** Temp folders (default: tempRoots with the environment's TMPDIR). */
|
|
195
|
+
temp?: readonly string[];
|
|
196
|
+
/** Why a CODEX_HOME folder isn't private to this user (default notPrivate). */
|
|
197
|
+
privacy?: (dir: string, where?: Where) => string | undefined;
|
|
198
|
+
/** The installer's pinned CODEX_HOMEs (default readPins). */
|
|
199
|
+
pins?: () => string[];
|
|
200
|
+
/** Folders whose `.codex/config.toml` is a project config Codex may merge (cwd up to the repo root). */
|
|
201
|
+
projectConfigDirs?: readonly string[];
|
|
202
|
+
}): Promise<EffectiveEnv>;
|
|
203
|
+
/**
|
|
204
|
+
* IX_BIN, resolved (the real path is what is checked and forwarded), when
|
|
205
|
+
* it is an absolute path to an existing regular file outside temp folders, Codex-granted folders and the project,
|
|
206
|
+
* owned by the person or root, writable by no group or others, in folders
|
|
207
|
+
* nobody else can write (foldersAboveProblem); else why not.
|
|
208
|
+
*/
|
|
209
|
+
export declare function ixBinOf(raw: string | undefined, where: Where): {
|
|
210
|
+
value?: string;
|
|
211
|
+
warning?: string;
|
|
212
|
+
};
|
|
213
|
+
/** Whether `dir` is the person's home folder itself (symlinks resolved). */
|
|
214
|
+
export declare function isHomeFolder(dir: string, home?: string): boolean;
|
|
215
|
+
/**
|
|
216
|
+
* What Codex's own working folder says about the server's. Unknown (the OS
|
|
217
|
+
* doesn't show it: Windows, lsof missing): the project can't be confirmed,
|
|
218
|
+
* so releases are refused, with a warning. Known but different from the
|
|
219
|
+
* server's cwd: a cwd override moved the server; unless the user-level
|
|
220
|
+
* config set that cwd, a project config did, and the bridge holds
|
|
221
|
+
* everything.
|
|
222
|
+
*/
|
|
223
|
+
export declare function cwdPolicy(o: {
|
|
224
|
+
serverCwd: string;
|
|
225
|
+
codexCwd: string | undefined;
|
|
226
|
+
configuredCwd?: string | undefined;
|
|
227
|
+
}): {
|
|
228
|
+
refusal?: string;
|
|
229
|
+
releaseRefusal?: string;
|
|
230
|
+
warning?: string;
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* Why the bridge must hold everything and refuse every release, checked on
|
|
234
|
+
* the EFFECTIVE Codex home and P2P_HOME every time (whether or not
|
|
235
|
+
* CODEX_HOME was given): HOME isn't the person's home folder; either lies
|
|
236
|
+
* in a temp folder, a folder Codex lets the sandbox write, or the project;
|
|
237
|
+
* either isn't private to the person; or a project config touches
|
|
238
|
+
* `mcp_servers.peer2peer` at all (a trusted project can replace this
|
|
239
|
+
* server's command, args, cwd and env outright: Codex merges
|
|
240
|
+
* `mcp_servers` from project layers).
|
|
241
|
+
*/
|
|
242
|
+
export declare function locationPolicy(o: {
|
|
243
|
+
env: Env;
|
|
244
|
+
home: string;
|
|
245
|
+
codexHome: string;
|
|
246
|
+
p2pHome: string;
|
|
247
|
+
projectDirs: readonly string[];
|
|
248
|
+
temp: readonly string[];
|
|
249
|
+
privacy: (dir: string, where?: Where) => string | undefined;
|
|
250
|
+
projectConfigDirs: readonly string[];
|
|
251
|
+
readConfig: (path: string) => Promise<string | null>;
|
|
252
|
+
}): Promise<string | undefined>;
|
|
253
|
+
/**
|
|
254
|
+
* Why a project config among `dirs` (each one's `.codex/config.toml`)
|
|
255
|
+
* replaces or changes this server: it touches `mcp_servers.peer2peer` at
|
|
256
|
+
* all. The person's own Codex home (cwd = ~) is the user layer, skipped.
|
|
257
|
+
*/
|
|
258
|
+
export declare function projectOverride(dirs: readonly string[], codexHome: string, readConfig: (path: string) => Promise<string | null>): Promise<string | undefined>;
|
|
259
|
+
/** The folders whose `.codex/config.toml` Codex reads as project layers for `cwd`: it and each parent up to the repo root (just `cwd` outside a repo). */
|
|
260
|
+
export declare function projectConfigDirsOf(cwd: string, top: string | undefined): string[];
|
|
261
|
+
/**
|
|
262
|
+
* The P2P_HOME the bridges use, for the person's `p2p codex …` commands and
|
|
263
|
+
* the hooks: the user-level config's value when it sets one (that's what the
|
|
264
|
+
* bridge takes), else the environment's, else ~/.p2p.
|
|
265
|
+
*/
|
|
266
|
+
export declare function bridgeHome(env: Env, home?: string): string;
|