@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.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Ix Infrastructure, Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,215 @@
1
- # Temporary Holding Version
1
+ # Peer2Peer for Codex (the Codex bridge)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This connects OpenAI Codex CLI sessions to [Peer2Peer v3](https://github.com/ix-infrastructure/Peer2Peer/blob/v0.1.0/docs/protocol.md). A Codex session can then see what your friends', circle-mates' and other devices' agent sessions (Claude Code or Codex) are working on, message them end-to-end encrypted, and get flagged when two sessions change the same files. There is no account and no login.
4
+
5
+ It is the Codex counterpart of the [Claude Code mod](https://github.com/ix-infrastructure/Peer2Peer/blob/v0.1.0/mods/claude/peer2peer/README.md) and keeps the same rules. Both run over the same `p2p` CLI and the same relays, so a Claude session and a Codex session see and message each other with no extra setup.
6
+
7
+ ## Install
8
+
9
+ You need Node.js 22 or newer and Codex CLI with hooks (0.156 or later; `hooks` is a stable feature there).
10
+
11
+ The bridge ships inside the single-file `p2p` CLI, `mods/claude/peer2peer/bin/p2p.mjs`, so there is nothing to clone or build. Put that file somewhere it can stay, then run:
12
+
13
+ ```sh
14
+ mkdir -p ~/.local/share/peer2peer
15
+ curl -fsSLo ~/.local/share/peer2peer/p2p.mjs https://raw.githubusercontent.com/ix-infrastructure/Peer2Peer/main/mods/claude/peer2peer/bin/p2p.mjs
16
+ curl -fsSLo ~/.local/share/peer2peer/defaults.json https://raw.githubusercontent.com/ix-infrastructure/Peer2Peer/main/mods/claude/peer2peer/bin/defaults.json
17
+
18
+ node ~/.local/share/peer2peer/p2p.mjs codex install # prints what it would add
19
+ node ~/.local/share/peer2peer/p2p.mjs codex install --write # adds it
20
+ ```
21
+
22
+ While the repository is private, `raw.githubusercontent.com` needs authentication. With the GitHub CLI signed in, fetch the same two files through the API instead:
23
+
24
+ ```sh
25
+ mkdir -p ~/.local/share/peer2peer
26
+ gh api -H "Accept: application/vnd.github.raw" repos/ix-infrastructure/Peer2Peer/contents/mods/claude/peer2peer/bin/p2p.mjs > ~/.local/share/peer2peer/p2p.mjs
27
+ gh api -H "Accept: application/vnd.github.raw" repos/ix-infrastructure/Peer2Peer/contents/mods/claude/peer2peer/bin/defaults.json > ~/.local/share/peer2peer/defaults.json
28
+ ```
29
+
30
+ If the Claude Code mod is installed, its bundled `p2p.mjs` works too.
31
+
32
+ `p2p codex install` prints three things, each pointing at the script you ran:
33
+
34
+ 1. an `[mcp_servers.peer2peer]` table for `~/.codex/config.toml` (`command = "node"`, `args = ["…/p2p.mjs", "codex", "mcp"]`). It includes an `env_vars` line (CODEX_HOME and P2P_SHARE_STATUS only), plus an `[mcp_servers.peer2peer.env]` table holding the guarded settings set in your environment when you run it (`P2P_HOME`, `P2P_AUTO_DELIVER`, …; see "Where settings come from"). Existing entries in that table are kept when you install again;
35
+ 2. five hooks for `~/.codex/hooks.json`: SessionStart, UserPromptSubmit, PreToolUse (shell commands only), PostToolUse and Stop. Each runs `node …/p2p.mjs codex hook <Event>`;
36
+ 3. a few lines for `~/.codex/AGENTS.md` on how to treat peers.
37
+
38
+ `--write` applies all three. It backs up each file first (`<file>.p2p-backup-<time>`), writes atomically, and keeps everything else in those files as it was. It is idempotent: a second run changes nothing, and a moved script replaces its own entries instead of adding more. It also refuses to touch a `config.toml` that already defines `peer2peer` itself, or a `hooks.json` that isn't valid JSON. `--write --remove` takes out exactly what it added. `--codex-home <dir>` (or `CODEX_HOME`) points it at another Codex home. Its hook entries carry `--managed-by=p2p-codex-install`, and only entries with that marker are ever replaced or removed. A symlinked `config.toml` or `hooks.json` (say, from a dotfiles repo) stays a symlink: its target is backed up and written.
39
+
40
+ Then:
41
+
42
+ - **Restart Codex and trust the hooks once with `/hooks`.** Codex runs non-managed hooks only after you approve them, and again after any change to them.
43
+ - **Make your identity** in a terminal, once per machine: `node …/p2p.mjs setup`. Then join a friend with `node …/p2p.mjs join '<link or code>'`, or make a code for them with `invite`. A code on a relay on a private network (a Tailscale name, a LAN address) needs `--allow-private-relay` on the join (`node …/p2p.mjs join --allow-private-relay '<link>'`), unless that relay is already one of yours. Put the script on your `PATH` as `p2p` if you like.
44
+ - **Share your Codex sessions.** A session follows its project (the repository it works in): private until you share the project with `p2p project share [<circle>…] [--person <friend>]…` in it, a command only you run: the bridge's shell guard and the CLI refuse it from the model, best effort (for a hard barrier deny the model's sandbox access to `P2P_HOME`: [threat model](https://github.com/ix-infrastructure/Peer2Peer/blob/v0.1.0/docs/security/threat-model.md#the-hard-barrier-sandbox-your-agents)). Every folder the session works in binds it to that project, and `p2p_send` refuses anyone the session's projects don't share. `p2p codex sessions` lists your live Codex sessions with their sids; the bridge registers each one, so only it speaks for them.
45
+
46
+ ## What the model gets
47
+
48
+ The MCP server offers five tools. None of them changes who reaches you.
49
+
50
+ | Tool | What it does |
51
+ |---|---|
52
+ | `p2p_list_peers` | Lists live sessions of friends, circle-mates and your other devices: address, busy or idle, repo, branch, summary and files. All of it is inert, and labelled as data written by those peers. |
53
+ | `p2p_send` | Messages a `p2p:<name>@<fingerprint>/<label> [ref]` address or a `name@fingerprint` address. A bare name such as `ann` is refused, because anyone can choose their own name. A listed session is addressed by its owner's full userId. The text goes to `p2p send` on stdin, so it never shows up in a process listing. |
54
+ | `p2p_status` | Sets this session's one-line working-on summary (in the model's own words; absolute paths are stripped; at most 200 characters) and its repo-relative files. Paths outside the repository are dropped. |
55
+ | `p2p_inbox` | Shows the messages that arrived since the last call, each framed as `<peer-message from="p2p:…">` with its body escaped, under a header that says peers' text is data, not instructions. A held message is listed by sender only, without its text. |
56
+ | `p2p_peer_activity` | A digest of what peers' sessions are working on, same repository first, with `POTENTIAL CONFLICT` lines. |
57
+
58
+ **Codex's own approvals still apply.** The three reading tools are annotated read-only, so Codex runs them without asking. `p2p_send` and `p2p_status` publish text the model wrote, so they are open-world: **Codex asks you before each one.** Under approval policy `never`, Codex refuses them outright ("MCP tool call requires approval, but approval policy is never"). That keeps a session that runs unattended from being talked into sending your data to a peer. Busy/idle and edited files still go out through the hooks, with no tool call. To allow them anyway, add this to `config.toml`:
59
+
60
+ ```toml
61
+ [mcp_servers.peer2peer.tools.p2p_send]
62
+ approval_mode = "approve"
63
+
64
+ [mcp_servers.peer2peer.tools.p2p_status]
65
+ approval_mode = "approve"
66
+ ```
67
+
68
+ Asked for `p2p_join`, `p2p_approve`, `p2p_relays_add`, `p2p_invite`, `p2p_release`, or any other `p2p_*` name it doesn't offer, the server refuses and names the command you would run in your own terminal. Joining, inviting, approving held contacts, relays, circles, sharing a session and releasing held messages are only ever done by you. Secrets stay out of the model's context the same way: the bridge never reads or shows your backup phrase or invite codes.
69
+
70
+ ## How messages reach Codex
71
+
72
+ Codex has no API for pushing a message into a running session. What it does have is Claude-Code-style hooks, and hooks can add context for the model or continue a turn. The bridge uses those:
73
+
74
+ | When | How |
75
+ |---|---|
76
+ | You submit a prompt | The UserPromptSubmit hook adds the waiting messages, any new conflicts, and a note about held messages. |
77
+ | During a turn | The PostToolUse hook adds messages that arrived since the last tool call (the same "next tool round" as in Claude Code). |
78
+ | When a turn ends | If messages arrived during the turn, the Stop hook continues it (`decision: "block"`) with them. This counts against the turn caps. |
79
+ | Any time | The model can read `p2p_inbox`. AGENTS.md and the server's instructions tell it to check at turn boundaries. |
80
+ | While idle (opt-in) | With `P2P_CODEX_QUEUE=1`, the bridge starts a turn in an idle thread with `codex queue --thread <id>`. The queued text is the bridge's own pointer ("N new messages from …: call p2p_inbox"), never the peer's text, which would otherwise sit in a process listing and arrive as a user message. This is off by default: `codex queue` is experimental and reaches a running TUI only through Codex's shared app-server daemon. |
81
+
82
+ Without the hooks, the tools still work and the model can read `p2p_inbox`, but nothing arrives on its own. Status then counts the session as busy for two minutes after each tool call.
83
+
84
+ The MCP server lives exactly as long as the Codex session. It runs `p2p watch --session <sid> --jsonl` and restarts it with backoff. Nothing runs after the session ends, and there is no daemon.
85
+
86
+ Hooks reach the server over a local socket (0600, in a 0700 folder under `P2P_HOME`; on Windows a named pipe whose random name is recorded only in that folder). **The socket is untrusted:** anything running as you can talk to it, including the model's own commands (Codex's sandbox allows Unix sockets). So:
87
+
88
+ - a hook report can only make the mode stricter. It can't loosen it, it never binds the server to a thread, its cwd is ignored, and the files it names must exist in the repo;
89
+ - held messages come out of it as metadata only (who, why, when), never their text;
90
+ - a release or decline must come from the person (see below).
91
+
92
+ A hook finds its server by the thread the server is bound to. The server binds only to the thread id in a tool call's `_meta`, which Codex builds. Before that, a hook goes by the Codex process both descend from, and only when exactly one unbound server is there. With more than one, the hook does nothing rather than guess.
93
+
94
+ ## Who may start work in a Codex session (the hold rule)
95
+
96
+ Only this session's own mode counts; nothing a peer claims does. The bridge reads the mode from Codex itself, from sources a sandboxed command can't forge:
97
+
98
+ - the latest turn context (approval policy, sandbox, approvals reviewer) in **this thread's own rollout file**, `CODEX_HOME/sessions/**/rollout-*-<thread>.jsonl`. The bridge reads it incrementally, so a turn context far back in a long session still counts (Codex records one only when the settings change);
99
+ - the sandbox and auto-review in each tool call's `_meta`.
100
+
101
+ A hook's `permission_mode` can only make the mode stricter: `bypassPermissions` holds until Codex records a new turn context. Codex may run UserPromptSubmit before it records the turn's context (it does on the compaction path), so a mode-dependent message never goes out at the prompt. It goes out at the first tool round or at the turn's end instead.
102
+
103
+ - **The session asks before acting** when approvals go to you (`untrusted` or `on-request`, not `never`, not auto-review) **and** the sandbox is `read-only`. Messages from friends and circle-mates are then delivered.
104
+ - **Anything else counts as auto-approving.** That covers `workspace-write` (Codex edits files and runs sandboxed commands without asking), `danger-full-access`, `never`, `--yolo`, `--approve-for-me` and `granular`. **An unknown mode holds too**: no tool call has named the thread yet, or there is no turn context. A message held only because the mode was unknown goes out once Codex confirms the session asks. Every message from another person is otherwise **held**. There are two exceptions:
105
+ - senders listed in `P2P_AUTO_DELIVER`, by full userId (`p2p:…`) or `name@fingerprint`. Bare names and petnames are ignored, since names are self-chosen;
106
+ - messages from your own devices, which are always delivered.
107
+ - **The mode is never published** in a status.
108
+ - **A message sent to you rather than to a session** is never delivered by itself. Your most recently used Codex session holds it.
109
+ - **Held messages** wait up to 10 minutes. Their text never reaches the model. Review them in your own terminal:
110
+
111
+ ```sh
112
+ p2p codex held # who, why and when (never the text)
113
+ p2p codex release <id> # hand it to that session's model at its next prompt, tool call or turn end
114
+ p2p codex decline <id> # drop it; the sender gets one delivery notice
115
+ ```
116
+
117
+ A release (or decline) rests on the sandbox, not on process ancestry, because a daemon can escape the process tree with `setsid` and a double fork. Codex's seatbelt or landlock confinement survives that. Three things are needed:
118
+ - **a nonce file.** Before each request, `p2p codex release` writes a file into `P2P_HOME/codex/confirm/` (folder 0700, file 0600). The file's name is the hash of a secret the command keeps and sends only over the socket, so a sandboxed process that can read the folder still can't present it, or spend it. The bridge accepts only a fresh (under 60 s), matching, regular file owned by you, in real folders (`codex/` and `confirm/` must not be symbolic links), and deletes it;
119
+ - **`P2P_HOME` out of the sandbox's reach.** The bridge checks that `P2P_HOME` (resolved) is outside every writable root of the session's trusted turn context: the workspace, extra writable roots, `TMPDIR` and `/tmp`. If it is inside one, if the session runs under `danger-full-access`, or if the roots aren't known yet (no tool call has named the thread), releases over the socket are refused outright. Release from a separate terminal while the session's sandbox can't write `P2P_HOME`, or leave the auto-approving mode, or list the sender in `P2P_AUTO_DELIVER`. A session started in your home folder has `~/.p2p` inside its workspace: point `P2P_HOME` elsewhere;
120
+ - **a one-time code**, as a second factor, when the session's Codex window is a terminal. The bridge prints it there with the message's sender and first line: "type this into `p2p codex release` in a separate terminal; never paste it into Codex". With no terminal (an IDE extension, the desktop app, `codex exec` with pipes), the nonce file and the writable-roots check are what's required.
121
+
122
+ Requests are rate-limited: 5 of yours a minute, with an exponential backoff after wrong codes (2 s, 4 s, 8 s, up to 15 minutes). Requests without a valid nonce file, or from a Codex descendant, are counted apart (30 a minute, with their own backoff), so they can never push your own releases into backoff. A caller the OS shows descends from Codex (`lsof` on macOS, `ss` on Linux) is refused; that check is cheap, but never enough on its own. The writable-roots check runs both ways: a writable root inside `P2P_HOME` or its `codex/confirm` folder refuses too, as does a symlinked `codex/` or `confirm/`.
123
+
124
+ **Under `danger-full-access` nothing can be enforced:** the model's commands can write anywhere and run as you. That's why releases are refused in that mode. Messages from other people are held there anyway.
125
+
126
+ An expired message also sends its sender one notice. At most 5 held messages per sender and 50 in all are kept; the oldest go first.
127
+ - **Turn caps:** messages that arrive while a turn runs are gathered into one continuation. Each sender can start at most 3 turns per 10 minutes, and all peers together at most 10. Past that, messages are held for you.
128
+ - **First contacts** (friend invites, circle invites and join requests) are never approved by the bridge. The model is told that they wait for you (`p2p held`, `p2p held approve <id>`).
129
+
130
+ Defence in depth, not the protection itself: `held`, `release`, `decline` and `install --write` also refuse to run inside a Codex tool call (where `CODEX_THREAD_ID` is set). The PreToolUse hook refuses model shell commands that run mutating `p2p` verbs (`join`, `accept`, `invite`, `held approve`, `relays add|remove`, `circle …`, `session roster`, `codex release`, …), including commands wrapped in `sh -c` or `eval`. It also refuses commands that touch `~/.p2p` (keys, backup phrase, and `~/.p2p/codex/`, the servers' registry and sockets). These guards parse conservatively, like the Claude mod's, and a determined command can spell things another way. What protects you is that the tools never do these things, held text never leaves the bridge, and a release needs the checks above.
131
+
132
+ ## Status: what a Codex session shares
133
+
134
+ A Codex session's status goes, end-to-end encrypted, only to the people its roster allows. It carries busy or idle, the label (the repository's folder name), the repo and the branch. Unless you opt out, it also carries:
135
+
136
+ - the one-line summary the model gives `p2p_status`. If the model hasn't given one, the summary is made from the changed files ("Changing src/a.ts in web.");
137
+ - the repo-relative files the session changes (at most 25). These come from apply_patch and edit tool calls seen by PostToolUse, and from `p2p_status`. Either way, only files that exist in the repo are shared (or, for `p2p_status`, files an edit already reported).
138
+
139
+ It never carries absolute paths, your prompt or the mode. Prompts stay inside the hook process and are never sent to the bridge.
140
+
141
+ Conflicts: when a peer's session in the same repo changes a file this session changed, the model gets a `POTENTIAL CONFLICT` note (at its next prompt or tool call, and in `p2p_peer_activity`). The bridge also sends that session one short message per (session, file).
142
+
143
+ | Setting | Effect |
144
+ |---|---|
145
+ | `P2P_SHARE_STATUS=0` | No summary, no files and no conflict messages. Only busy/idle, label, repo and branch are shared. |
146
+ | `P2P_AUTO_DELIVER=p2p:…,name@xxxx-xxxx-xxxx-xxxx` | These people's messages are delivered even in an auto-approving session |
147
+ | `P2P_CODEX_QUEUE=1` | Push messages into an idle thread with `codex queue` (experimental) |
148
+ | `P2P_HOME`, `P2P_BIN` | As for `p2p` |
149
+ | `CODEX_HOME` | Where the bridge finds this thread's rollout file (default `~/.codex`) |
150
+ | `P2P_CODEX_CONFIRM_RATE`, `P2P_CODEX_CONFIRM_TTY` | Release requests a minute (default 5), and where the one-time code is printed (default the Codex window, `/dev/tty`). Set them only in the server's `env` in `config.toml`; the tests use them |
151
+
152
+ ### Where settings come from
153
+
154
+ Codex merges config layers in order: user (`CODEX_HOME/config.toml`), profile, then the `.codex/config.toml` files of projects you marked trusted (cwd, its parents, the repo root), then `-c` flags. Codex keeps a project-local denylist (`codex-rs/config/src/loader/mod.rs`, `PROJECT_LOCAL_CONFIG_DENYLIST`: base URLs, model providers, `notify`, profiles, …), but **`mcp_servers` isn't on it**. So a trusted repo's `.codex/config.toml` can add to `mcp_servers.peer2peer.env`, for example `P2P_AUTO_DELIVER = "p2p:<someone>"` or a `P2P_CODEX_CONFIRM_TTY` pointing into the repo. A repo is content, not you, so the bridge takes these settings **only from the user-level config's `[mcp_servers.peer2peer.env]` table**:
155
+
156
+ `P2P_HOME`, `P2P_AUTO_DELIVER`, `P2P_CODEX_CONFIRM_TTY`, `P2P_CODEX_CONFIRM_RATE`, `P2P_CODEX_QUEUE`, `P2P_CODEX_BIN`, `P2P_CODEX_META_LOG`, `P2P_BIN`, `P2P_RELAY_TRANSPORT`, `P2P_DEFAULT_RELAYS`.
157
+
158
+ An environment value that differs from the table's is ignored, with a warning in Codex's MCP log and in `p2p codex sessions`. Values from your shell count only once they are in the table (the installer copies them in). `P2P_SHARE_STATUS=0` only tightens, so it is honoured from either place.
159
+
160
+ **A trusted repo can replace this server outright.** Codex merges `mcp_servers` from trusted project layers key by key, so a repo's `.codex/config.toml` can change `mcp_servers.peer2peer.command`, `args`, `cwd` or `env.PATH`, and then whatever runs isn't this bridge. Everything below is defence in depth against a repo that only adds settings. As a backstop, the bridge reads the `.codex/config.toml` of the cwd and each parent up to the repo root (for its own cwd and for Codex's), and if any of them touches `mcp_servers.peer2peer` at all, it holds everything and refuses every release, with a warning on stderr. It runs the same scan for every working folder it learns later (from the rollout or hooks). Keys are matched decoded (quoted, literal or `\u`-escaped spellings alike), and a file it can't read with confidence counts as touching it. The bridge runs its helpers (`git`, `ps`, `lsof`, `ss`, `getconf`) by absolute path from the system folders with a scrubbed environment (a fixed system `PATH`, no `GIT_*`), `P2P_BIN` and `P2P_CODEX_BIN` only as absolute paths, and nothing through the `PATH` in its own env. The programs it starts (`p2p`, `p2p watch`, `codex queue`) get an environment built from an allowlist: a fixed `PATH` (the node running the bridge, then the system folders), `HOME` (`USERPROFILE` on Windows) from the user database, `P2P_*` (except the extension and defaults-file variables), `LANG`, `LC_*`, `TZ`, `TERM`, and on Windows `SYSTEMROOT`, `APPDATA` and `LOCALAPPDATA`; `codex queue` also gets the bridge's `CODEX_HOME`. `IX_BIN` (the Ix CLI the Ix extension runs) is a guarded setting, taken only from `[mcp_servers.peer2peer.env]` (a project-set `IX_BIN=/bin/sh` would run the repo's scripts), and passed on only as an absolute path to an existing regular file outside temp folders, Codex-granted folders and the project, owned by you or root, writable by no group or others, in folders nobody else can write. Nothing else is passed on, so `NODE_OPTIONS`, `NODE_TLS_REJECT_UNAUTHORIZED`, proxy variables, `NODE_ICU_DATA`, `GCONV_PATH`, `LD_*` and `DYLD_*` set by a project config never reach them.
161
+
162
+ The bridge takes your home folder from the user database, never from `HOME` (a project config can set `HOME` in the server's env, which would move `~/.codex`, `~/.p2p` and the pin file). If `HOME` is set to anything else, the bridge holds everything and refuses every release. The same happens, whether or not `CODEX_HOME` was given, when the Codex home or `P2P_HOME` in effect lies in a temp folder, in a folder Codex lets the sandbox write (`memories`, `memories_v2` under a Codex home), or in the project, or isn't private to you (not yours, world-writable, or a symlinked `config.toml`). Temp folders are `/tmp`, `/private/tmp`, `/var/tmp`, `/dev/shm` and your own (on macOS from `getconf DARWIN_USER_TEMP_DIR`, which `TMPDIR` can't change). The project is the server's cwd and Codex's own (read from the OS, since a project config can move the server's `cwd`), each with its repo root, plus every cwd a trusted rollout declares. When the cwd is exactly your home folder, `~/.codex` and `~/.p2p` aren't counted as project files (they still have to pass the privacy checks); everything else under `~` is. That exemption holds only while the sandbox can't reach them: whenever a turn context declares writable a folder holding the Codex home or its `config.toml`, or overlapping `P2P_HOME` (a `workspace-write` session started in `~`, say), the bridge holds everything and refuses every release. Read-only and on-request sessions in `~` work as usual. Once a turn context exists but can't be relied on (its writable folders can't be read, or the rollout file isn't trusted), the bridge holds everything and refuses releases too; before the first turn context, only the static checks apply. A home folder that is itself a git repo (dotfiles) doesn't count as a project root, though: only the cwd's own subtree does. When the OS doesn't show Codex's own working folder (Windows, `lsof` missing), the project can't be confirmed, so releases are refused, with a warning. When it does and the server runs somewhere else, which a `cwd` override does, the bridge holds everything unless your user-level `[mcp_servers.peer2peer]` table sets that `cwd`. A symlinked `config.toml` (stow, chezmoi, Nix) is judged by its target: a regular file outside temp folders, Codex-granted folders and the project, owned by you (or by root, as in `/nix/store`), and writable by no group or others, in folders that are each owned by you or root and not group- or world-writable unless sticky.
163
+
164
+ `CODEX_HOME` locates that user config, so it is checked on its own. A value in any of those places, not owned by you or world-writable is always refused, with symlinks resolved. Otherwise the bridge counts the environment's value only when Codex itself (its parent process) runs with the same `CODEX_HOME`. It checks through `/proc` on Linux and `/bin/ps -E` on macOS (reading only the environment after the command line, and giving up when `CODEX_HOME=` appears twice). Where that can't be seen (Windows, a non-dumpable Codex on Linux, a platform-binary parent on macOS), it counts the value only when `p2p codex install --write` pinned it: the installer records a non-default Codex home in `~/.p2p/codex/codex-homes`, a fixed place outside any project. Otherwise it uses `~/.codex`. Every refusal is logged on stderr. A trusted project can still define its own MCP servers and commands, or replace this one: Codex runs those as you, and no bridge can defend against that. Trust only repos you'd let run code. The installer writes `command` as the absolute path of the node it runs under, so Codex's `PATH` doesn't pick the binary. `p2p codex held|release|decline` and the hooks read `P2P_HOME` from the same user-level table, so they find the bridges.
165
+
166
+ ## What Codex supports (October 2026, codex-cli 0.156.1)
167
+
168
+ | Capability | Status | Used for |
169
+ |---|---|---|
170
+ | MCP stdio servers (`[mcp_servers.<name>]`: command, args, env, `env_vars`, cwd, timeouts, enabled/disabled tools) | Stable | The tools |
171
+ | Env passed to MCP servers | Allowlist only (HOME, PATH, USER, …) plus `env` / `env_vars` | `env_vars` in the snippet |
172
+ | MCP client capabilities | `elicitation`, with no roots and no sampling. Each `tools/call` carries `_meta["x-codex-turn-metadata"]`: session/thread id, turn id, `sandbox_mode`, `auto_review_enabled` (confirmed on the wire by the smoke test) | The thread id and sandbox |
173
+ | MCP tool approval | Per server and per tool: `approval_mode` auto/prompt/writes/approve. Under `auto`, read-only tools run freely; otherwise a tool needs approval unless it is annotated non-destructive and not open-world. Under approval `never`, a tool that needs approval is refused | Tool annotations |
174
+ | Hooks (`hooks.json` or `[hooks]`): SessionStart, UserPromptSubmit, PreToolUse, PermissionRequest, PostToolUse, Stop, SessionEnd, compaction, subagent and interrupt events | Stable (`features.hooks`). They need trust via `/hooks` | Delivery, status, mode, guard |
175
+ | Hook payload | `session_id`, `cwd`, `transcript_path`, `model`, `turn_id`, `permission_mode` (`never` maps to `bypassPermissions`, everything else to `default`; no sandbox), plus per-event fields | Pairing and mode |
176
+ | Hook output | `additionalContext` (SessionStart, UserPromptSubmit, PreToolUse, PostToolUse), `decision: "block"` + `reason` (UserPromptSubmit, PostToolUse, Stop; a Stop block continues the turn), PreToolUse `permissionDecision: "deny"` | Delivery and guard |
177
+ | `notify` (`agent-turn-complete` as an argv JSON) | Legacy, slated for removal | Not used: Stop replaces it |
178
+ | AGENTS.md (`~/.codex/AGENTS.md`, then the repo root down to cwd, `AGENTS.override.md` first) | Stable | Guidance lines |
179
+ | Approval policies `untrusted`, `on-request`, `granular`, `never` (`on-failure` is an alias); sandboxes `read-only`, `workspace-write`, `danger-full-access`; `--yolo`, `--approve-for-me` | Stable | The hold rule |
180
+ | Injecting into a running session | No direct API. `codex queue --thread` (experimental; through the app-server daemon; starts a turn when the thread is idle) and the app-server's `turn/steer` / `thread/queue/*` | Opt-in `P2P_CODEX_QUEUE` |
181
+ | Plugins (`.codex-plugin/plugin.json`, which may bundle MCP servers, hooks and skills) | Stable | Not yet: see Limits |
182
+
183
+ Sources: [Codex MCP docs](https://learn.chatgpt.com/docs/extend/mcp?surface=cli), [hooks](https://learn.chatgpt.com/docs/hooks), [AGENTS.md](https://developers.openai.com/codex/guides/agents-md), [plugins](https://learn.chatgpt.com/docs/plugins), and the openai/codex source: `codex-rs/rmcp-client/src/utils.rs` (env allowlist), `codex-rs/core/src/mcp_tool_call.rs` (`_meta`, tool approval) and `turn_metadata.rs`, `config/src/mcp_types.rs` (`approval_mode`), `codex-rs/hooks/src/schema.rs` and `core/src/hook_runtime.rs` (hook payloads and `permission_mode`), `codex-rs/hooks/src/legacy_notify.rs`, `ext/queue/src/service.rs` (`codex queue`), `protocol/src/protocol.rs` (approval policies). The hook input and output JSON schemas embedded in the 0.156.1 binary were also checked.
184
+
185
+ ## Limits
186
+
187
+ - **No push into an idle session by default.** A message that arrives while Codex sits idle waits for your next prompt, unless you opt in to `P2P_CODEX_QUEUE=1`. That path needs Codex's app-server daemon. It queues only the bridge's pointer to `p2p_inbox`.
188
+ - **Held messages need a terminal**, outside Codex, with `P2P_HOME` out of the session's sandbox. Codex has no buttons for a bridge to add above the prompt, so release and decline are `p2p codex release|decline`. MCP elicitation could serve as a Deliver button, but whether an auto-approving Codex auto-resolves elicitations is undocumented, so the bridge doesn't rely on it. The one-time code is printed straight into the Codex window and stays visible until the TUI redraws.
189
+ - **The summary comes from the model** (`p2p_status`). Nothing else turns a prompt into a status line, and prompts never leave the hook process.
190
+ - **The mode and the writable roots depend on Codex's rollout format** (its turn context) and on a tool call having named the thread. Until both are there, messages from other people are held and releases are refused. `codex exec --ephemeral` writes no rollout, so it always holds. The rollout file is distrusted, so the mode counts as unknown, when `CODEX_HOME` is inside a folder the sandbox can write, when two files claim the thread, when its first line isn't the thread's `session_meta`, or when its timestamps run backwards or into the future. Whatever the turn context declares writable, a Codex home or `P2P_HOME` in a temp folder, a Codex-granted folder or the project makes the bridge hold everything (your own devices, `P2P_AUTO_DELIVER` senders and messages already released included) and refuse every release (see "Where settings come from").
191
+ - **Pairing a hook with its bridge:** when several unbound sessions run under one Codex process (one app-server), hooks do nothing until a tool call binds each bridge to its thread. They never guess.
192
+ - **Guards are a backstop.** As in the Claude mod, the shell-command guard parses conservatively and a determined command can get around it.
193
+ - **A sandbox escape defeats the nonce gate.** The release checks rest on Codex's sandbox keeping `P2P_HOME` out of a model command's reach. A process that escapes the sandbox runs as you: it can write the nonce file, read `~/.p2p` (keys included), and talk to any socket. On Linux the sandbox can usually still reach user daemons over their sockets (D-Bus with `systemd-run --user`, tmux, docker), and any of them can start a process outside the sandbox. As far as the Codex source shows, the only control over Unix sockets is the macOS network proxy's `[permissions.<profile>.network.unix_sockets]` allowlist; there's no equivalent on Linux. Keep `network_access` off (the default), never set `dangerously_allow_all_unix_sockets`, and run untrusted work in a container or VM without those sockets. Under `danger-full-access` there is no sandbox at all, and releases are refused.
194
+ - **Settings from the user-level config only** (see "Where settings come from"). A profile (`-p name`, `<name>.config.toml`) isn't read for them, and Codex's parent-environment check uses `ps -E`, which macOS hides for platform binaries: started under `/bin/sh`, an environment `CODEX_HOME` counts only when the installer pinned it.
195
+ - **No Codex plugin package yet.** A `.codex-plugin` could bundle the MCP server and hooks, but its paths would point into the plugin cache, and plugin hooks need trust too. The installer covers the same ground today.
196
+ - **Windows:** releases are always refused there. Windows doesn't show another process's working folder, so the bridge can't confirm the project Codex runs in, and it fails closed: messages it would hold stay held (they expire unread), while everything else is delivered as usual. The socket is a named pipe with a random name. Hook pairing falls back to "the only unbound session". There is no caller check; the nonce file and the writable-roots check still apply. Hook commands are quoted for `cmd`. It is untested there.
197
+ - **A real sandboxed attack isn't in the smoke test.** It would mean asking a live Codex model to run a socket client under `workspace-write`. The integration test does the same thing from two processes: a direct descendant of the server's parent (like a model's command), and a daemon that escaped the process tree. Both read only metadata, and their releases are refused, since neither can produce the nonce file a sandboxed process can't write. That the sandbox really can't write `P2P_HOME` is Codex's guarantee (seatbelt on macOS, landlock on Linux); the bridge only checks that `P2P_HOME` is outside the writable roots Codex recorded.
198
+
199
+ ## Develop
200
+
201
+ ```sh
202
+ (cd packages/core && npm ci && npm run build)
203
+ (cd packages/relay && npm ci && npm run build)
204
+ (cd packages/codex-bridge && npm ci && npm run build)
205
+ (cd packages/p2p && npm ci && npm run build)
206
+ cd packages/codex-bridge
207
+ npm run typecheck && npm run lint && npm test && npm run build
208
+ node ../../scripts/check-boundaries.mjs
209
+ ```
210
+
211
+ The build order matters: core, relay, then this package, then `packages/p2p`, which imports this package's `dist`. `npm test` builds them in that order first (`scripts/pretest.mjs`), running `npm ci` in any of them that has no `node_modules` yet, so it works in a fresh checkout. The integration test (`src/__tests__/integration.test.ts`) starts a relay in-process and gives two temporary `P2P_HOME`s to the real CLI. It drives one Codex session through the MCP server's stdio JSON-RPC and a Codex hook, and the other person through the plain CLI.
212
+
213
+ A real Codex smoke test runs only on request: `P2P_CODEX_SMOKE=1 npm test` (`src/__tests__/codex-smoke.test.ts`). It needs `codex` on `PATH` and logged in. It starts a relay and two identities, then runs `codex exec --ephemeral -s read-only` with the MCP server given as `-c` overrides, plus `tools.p2p_send.approval_mode = "approve"`. The server is started through `env CODEX_HOME=<throwaway>` and a small node parent, so it reads its guarded settings from a throwaway user-level config. With `CODEX_API_KEY` set it uses a throwaway `CODEX_HOME`; otherwise `--ignore-user-config`, which reads only your login. It never writes any Codex config. It checks that the Codex model lists a CLI peer's status, that its `p2p_send` reaches that peer's `p2p watch`, and that each tool call's `_meta` carries the thread id and `sandbox_mode`. The hooks are not part of the smoke test: running them in `codex exec` would need `--dangerously-bypass-hook-trust`, which would also run your own user hooks. The integration test runs them the way Codex does instead (JSON on stdin, JSON on stdout).
214
+
215
+ After changing the bridge, rebuild the bundled CLI the Claude mod ships: `cd packages/p2p && npm run bundle`, then `scripts/check-bundle.sh`.