@peer2peer/codex-bridge 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,177 @@
1
+ // Copyright 2026 Ix Infrastructure Inc.
2
+ //
3
+ // The mode sources the bridge trusts, read by the bridge itself:
4
+ // - this thread's own rollout file, `CODEX_HOME/sessions/**/rollout-*-<thread>.jsonl`.
5
+ // Codex writes it, outside the sandbox's writable roots, so a sandboxed
6
+ // command can't forge a turn context there. The thread id comes only
7
+ // from `_meta` on a tool call, which Codex builds (never from a hook).
8
+ // The file is read incrementally from where the last read stopped, so a
9
+ // turn context far back in a long session is still seen (Codex writes one
10
+ // only when the settings change). No turn context seen: the mode is unknown.
11
+ //
12
+ // A planted file is refused: more than one file for the thread, a first
13
+ // line that isn't this thread's `session_meta`, or timestamps that run
14
+ // backwards or into the future make the whole file untrusted. (The bridge
15
+ // also refuses it when CODEX_HOME is inside a writable root: Bridge.)
16
+ import { open, readdir } from "node:fs/promises";
17
+ import { join } from "node:path";
18
+ import { turnContextOf } from "./lib.js";
19
+ const CHUNK = 256 * 1024;
20
+ /** A turn context is a few KB; a longer line is skipped rather than buffered. */
21
+ const MAX_LINE = 1024 * 1024;
22
+ /** Clock skew allowed for a timestamp "in the future". */
23
+ const FUTURE_SLACK_MS = 5 * 60_000;
24
+ /** Every rollout file of `threadId` under `sessionsDir` (YYYY/MM/DD folders). */
25
+ export async function findRollouts(sessionsDir, threadId, depth = 4) {
26
+ if (!/^[0-9A-Za-z-]{8,80}$/.test(threadId))
27
+ return [];
28
+ const suffix = `-${threadId}.jsonl`;
29
+ const out = [];
30
+ const walk = async (dir, left) => {
31
+ let names;
32
+ try {
33
+ names = await readdir(dir);
34
+ }
35
+ catch {
36
+ return;
37
+ }
38
+ for (const name of names) {
39
+ if (name.startsWith("rollout-") && name.endsWith(suffix))
40
+ out.push(join(dir, name));
41
+ else if (left > 0 && /^\d+$/.test(name))
42
+ await walk(join(dir, name), left - 1);
43
+ }
44
+ };
45
+ await walk(sessionsDir, depth);
46
+ return out.sort();
47
+ }
48
+ /** The one rollout file of `threadId`, or undefined (none, or more than one: possibly planted). */
49
+ export async function findRollout(sessionsDir, threadId, depth = 4) {
50
+ const all = await findRollouts(sessionsDir, threadId, depth);
51
+ return all.length === 1 ? all[0] : undefined;
52
+ }
53
+ /** Reads one thread's rollout file as it grows, keeping its latest turn context. */
54
+ export class RolloutReader {
55
+ codexHome;
56
+ threadId;
57
+ now;
58
+ path;
59
+ offset = 0;
60
+ partial = "";
61
+ last = null;
62
+ seq = 0;
63
+ isSkipping = false;
64
+ isFirstLine = true;
65
+ lastAt = 0;
66
+ untrusted;
67
+ ever = new Set();
68
+ constructor(codexHome, threadId, now = Date.now) {
69
+ this.codexHome = codexHome;
70
+ this.threadId = threadId;
71
+ this.now = now;
72
+ }
73
+ distrust(why) {
74
+ this.untrusted ??= why;
75
+ this.last = { mode: {}, seq: this.seq, untrusted: this.untrusted };
76
+ return this.last;
77
+ }
78
+ async refresh() {
79
+ if (this.untrusted)
80
+ return this.last;
81
+ if (!this.path) {
82
+ const all = await findRollouts(join(this.codexHome, "sessions"), this.threadId);
83
+ if (all.length > 1)
84
+ return this.distrust(`${all.length} rollout files claim this thread`);
85
+ this.path = all[0];
86
+ }
87
+ if (!this.path)
88
+ return this.last;
89
+ let fh;
90
+ try {
91
+ fh = await open(this.path, "r");
92
+ }
93
+ catch {
94
+ return this.last;
95
+ }
96
+ try {
97
+ const { size } = await fh.stat();
98
+ if (size < this.offset)
99
+ return this.distrust("the rollout file shrank");
100
+ while (this.offset < size) {
101
+ const buf = Buffer.alloc(Math.min(CHUNK, size - this.offset));
102
+ const { bytesRead } = await fh.read(buf, 0, buf.length, this.offset);
103
+ if (bytesRead === 0)
104
+ break;
105
+ this.offset += bytesRead;
106
+ const lines = (this.partial + buf.subarray(0, bytesRead).toString("utf8")).split("\n");
107
+ this.partial = lines.pop() ?? "";
108
+ if (this.isSkipping && lines.length > 0) {
109
+ // The rest of a huge line (an image, a big tool output): never a turn context.
110
+ lines.shift();
111
+ this.isSkipping = false;
112
+ }
113
+ if (this.partial.length > MAX_LINE) {
114
+ this.partial = "";
115
+ this.isSkipping = true;
116
+ }
117
+ for (const line of lines) {
118
+ const bad = this.check(line);
119
+ if (bad)
120
+ return this.distrust(bad);
121
+ const ctx = turnContextOf(line);
122
+ if (ctx) {
123
+ this.seq += 1;
124
+ if (this.ever instanceof Set) {
125
+ if (ctx.writable === undefined)
126
+ this.ever = "unknown";
127
+ else if (ctx.writable === "all")
128
+ this.ever = "all";
129
+ else
130
+ for (const r of ctx.writable)
131
+ this.ever.add(r);
132
+ }
133
+ this.last = {
134
+ mode: ctx.mode,
135
+ seq: this.seq,
136
+ ...(ctx.cwd ? { cwd: ctx.cwd } : {}),
137
+ ...(ctx.writable !== undefined ? { writable: ctx.writable } : {}),
138
+ writableEver: this.ever instanceof Set ? [...this.ever] : this.ever,
139
+ };
140
+ }
141
+ }
142
+ }
143
+ }
144
+ finally {
145
+ await fh.close();
146
+ }
147
+ return this.last;
148
+ }
149
+ /** Why a line makes the file untrusted, or undefined: the first must be this thread's session_meta; times never run backwards or ahead. */
150
+ check(line) {
151
+ if (line.trim() === "")
152
+ return undefined;
153
+ const ts = /^\{"timestamp":"([^"]{10,40})"/.exec(line)?.[1];
154
+ if (this.isFirstLine) {
155
+ this.isFirstLine = false;
156
+ try {
157
+ const v = JSON.parse(line);
158
+ if (v.type !== "session_meta" || (v.payload?.id !== this.threadId && v.payload?.session_id !== this.threadId))
159
+ return "the rollout file doesn't start with this thread's session_meta";
160
+ }
161
+ catch {
162
+ return "the rollout file's first line isn't JSON";
163
+ }
164
+ }
165
+ if (ts !== undefined) {
166
+ const at = Date.parse(ts);
167
+ if (Number.isFinite(at)) {
168
+ if (at > this.now() + FUTURE_SLACK_MS)
169
+ return "a rollout timestamp is in the future";
170
+ if (at + 60_000 < this.lastAt)
171
+ return "rollout timestamps run backwards";
172
+ this.lastAt = Math.max(this.lastAt, at);
173
+ }
174
+ }
175
+ return undefined;
176
+ }
177
+ }
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@peer2peer/codex-bridge",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Peer2Peer for OpenAI Codex: an MCP server, Codex hooks and an installer over the `p2p` CLI",
5
+ "keywords": [
6
+ "peer2peer",
7
+ "p2p",
8
+ "codex",
9
+ "openai-codex",
10
+ "mcp",
11
+ "model-context-protocol",
12
+ "ai-agents"
13
+ ],
14
+ "homepage": "https://github.com/ix-infrastructure/Peer2Peer/tree/main/packages/codex-bridge#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/ix-infrastructure/Peer2Peer/issues"
17
+ },
18
+ "license": "Apache-2.0",
19
+ "author": "Ix Infrastructure",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/ix-infrastructure/Peer2Peer.git",
23
+ "directory": "packages/codex-bridge"
24
+ },
25
+ "type": "module",
26
+ "main": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "!dist/**/__tests__",
37
+ "!dist/**/*.test.*",
38
+ "!dist/**/*.map",
39
+ "README.md",
40
+ "LICENSE"
41
+ ],
42
+ "engines": {
43
+ "node": ">=22"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public",
47
+ "provenance": true,
48
+ "registry": "https://registry.npmjs.org/"
49
+ }
50
+ }