Skip to content

feat(up): send a machine's traffic through Phase with phase up - #332

Open
nimish-ks wants to merge 45 commits into
feat--agentsfrom
feat--phase-proto-relay
Open

nimish-ks wants to merge 45 commits into
feat--agentsfrom
feat--phase-proto-relay

Conversation

@nimish-ks

Copy link
Copy Markdown
Member

Stacked on #322 (feat--agents), and includes #331 as merged there.

What

phase up sends a Mac's traffic through Phase with no proxy settings in any program. phase down steps aside and stops it, and phase status shows what is captured.

  • A privileged helper, installed once with one password prompt, owns the tunnel device and routes and never sees a credential. Its packets reach an unprivileged engine over WireGuard on loopback.
  • In the engine, a user-space network stack sends your Connections' hosts to the existing credential proxy, which swaps decoys for real credentials. Everything else passes through unread.
  • The model traffic of the AI coding tools Phase knows (Claude Code, Codex, Cursor's command-line agent and others) is read only for those tools' own processes, found by the program behind each connection. Leak protection replaces a secret value Phase handed out before it reaches a model, and commands register values with the running engine's proxy.
  • Only this user's own programs get Connection credentials. Traffic whose program can't be found, such as a virtual machine's on macOS's shared network, goes through without them.
  • The helper steps aside the moment the engine stops or traffic stops getting through, checks the internet before taking routes, and gives the physical interface a scoped default route. Local networks and other VPNs, such as Tailscale, keep their own routes.
  • DNS asks the network's own servers first and public ones last, keeps names under a VPN's own domain off public resolvers, and re-reads servers when the way out changes or after 30 seconds.
  • UDP to port 443 is refused while traffic is captured, so tools fall back from QUIC to TCP, which can be read.
  • phase users logout --purge stops the engine.

Hooks, masking and phase ai are unchanged.

Limits

  • macOS only. On other platforms the helper reports that it is unsupported.
  • Tools that send model traffic through another proxy (HTTPS_PROXY, HTTP_PROXY, ALL_PROXY) are relayed unread, and phase up says so when one is set.
  • New dependencies: golang.zx2c4.com/wireguard and gvisor.dev/gvisor, with their indirect ones.

Checked

  • Build, vet (darwin, linux, windows), full tests, race.
  • The tunnel engine's tests run model traffic, registration and leak protection through a real engine and WireGuard peer in process.

Not checked here: a run on a Mac with the helper installed.

A command registered what it hands out with the first proxy it found: the
session's own, else the one the harness hooks keep running. Each proxy reads
model traffic the others never see, so a value handed out inside a session
never reached the hook-managed proxy. And a source added after these two
would have its proxy told only when neither of them found one.

Every source is asked now, and every proxy found is registered with, one
after another. The session's own proxy comes first and decides as before: an
Agent login is refused when it fails, before any other proxy is spoken to,
and any other login is warned. A failure of any other proxy warns once and
refuses nobody, and when the command found several proxies the line names
the one that failed. A proxy nothing listens at is passed over in silence,
and only when none runs is there no proxy. A proxy two sources find, as in a
shell that loads the hook-managed proxy's environment, is registered with
once.

A value has the same placeholder at each proxy: the type and the id are the
command's, and only the hashes are made with a proxy's key, which is now
kept for each proxy. The first registration has its usual bounds. Each one
after it has what is left of the second since the first began and never less
than one dial timeout, so a proxy that is away or does not answer costs the
ones after it a quarter of a second each at the most.
phase up captures a Mac's traffic with no proxy settings in any program.
A privileged helper, installed once, owns the tunnel device and routes
and never sees a credential. Its packets reach an unprivileged engine
over WireGuard on loopback, where a user-space network stack sends
Connection hosts to the existing credential proxy and passes everything
else through unread.

The helper steps aside the moment the engine stops or traffic stops
getting through, checks the internet before taking the routes, and on
macOS gives the physical interface a scoped default route, without
which the engine's pinned sockets have no way out.

phase down steps aside and stops the engine; phase status shows what is
captured. Hooks, masking and phase ai are unchanged.
The Phase AI certificate authority's key now lives in the system
keystore or, where there is none, only in the memory of each proxy
process. Programs behind the tunnel get no certificate settings of their
own: they trust the certificate authority through the trust store, so
one that is new in every process would fail every Connection host on
the machine.

phase up now uses the same store as phase ai enable, and stops before
it changes anything when the key can't be kept. The engine stops too if
a later session finds the keystore gone, and the helper steps aside.
The tunnel sees every program's traffic, so anything decided per program
has to know which program opened a connection. A flow reaches the engine
with the program's own source port. On macOS the engine asks lsof which
process of this user holds a socket from that port to that destination,
then reads from the kernel the path and arguments the process was
started with. Neither step needs privileges. A lookup takes about 50 ms
and never more than 500 ms, and it never answers with the engine itself.

A process is recognised as an AI coding tool from its executable, and
for a script runtime from its arguments, by a table that is plain data
so that a server can supply it later. The chat apps that ship some of
these tools are their own programs, and are not matched.

Linux and Windows report that the lookup isn't supported yet.
Leak protection reads the model traffic of known AI coding tools. Under
phase up every program's traffic reaches the router, a browser's to the
same hosts included, so the router now decides per connection, when it
opens, by the program that opened it.

For a TLS flow whose hello names a host that is read for some tool, the
router finds the program behind the flow's source port and the tool it
is. If the host is read for that tool, the router tells the proxy so and
hands the flow over by CONNECT, as it does for a Connection host, with
the hello it read sent on. Any other program's flow goes on unread, and
so does one whose program can't be found within half a second: not
knowing means not reading. A host nobody reads costs no lookup. A
Connection host that is also a model host still goes to the proxy, with
the tool or its absence noted. A flow the proxy can't take goes on
unread rather than fail, unless it needs a Connection's credential.

The router asks through a small Reader interface, which the proxy is to
implement. Until one is set, no model host is read.

A hidden --routes flag sends only the given networks through Phase, for
trying phase up on part of a machine's traffic first. Model hosts are
then captured by name as well, as Connection hosts are, or they would
never reach the router.

phase status says whether everything or part of the network goes through
Phase, which tools' model hosts are read, and how many flows were read,
left unread for other programs, or left unread because the program
wasn't found in time. It names no program, and no host outside that
table.

The engine now stops writing its status before it returns, and the test
Console answers a session's refresh, without which the proxy stopped
serving the session after fifteen seconds.
A custom Connection can be added, given other hosts or removed without
a new session. The tunnel learnt a session's hosts only when the session
started, so a Connection approved while phase up was running passed
unread until the session was next reopened.

The proxy now reports where a running session's Connections go whenever
that changes, with no credential in the report, and the engine passes it
on to the router and to the helper's DNS entries. A change that needs a
new session still ends the old one, and the new one starts as before.
Purging deleted the engine's pid file along with the rest of ~/.phase
and left the engine running: still holding a session and its
credentials, and still carrying the machine's traffic. It now stops the
way phase down stops it. The helper steps aside first, so traffic goes
direct again, and then the engine is stopped. With no engine running,
the helper isn't spoken to.

The engine's state directory and the helper's socket are now variables
that the tests point at throwaway places, so no test can read the real
engine's state or reach the helper that holds this machine's routes.
…xy environment

A CONNECT nobody noted is a client of the proxy environment, for which
every listed host is read. A tunnel's flow can arrive that way as well.
Its note is refused when too many wait, and the flow is handed over all
the same because it needs a Connection's credential. Or the note is
forgotten before a slow CONNECT is read. Another program's traffic to a
model host would then be read as a tool's.

Engine.TunnelConnection lets a tunnel say which connections to the proxy
are its own, for as long as each is open. A CONNECT from one of them
that finds no note is a tunnel's flow from no known tool: a Connection's
credential is attached as before, and nothing is read. A note that is
there still says whose flow it is.

What a tunnel calls when it is done with a connection also drops the
note that connection never used, so the note cannot name the next
program to connect from the same address and port. That is all
Engine.ForgetFlow did, by address alone and for no caller, so it is
removed: one call opens what the proxy knows of a connection and one
ends it.
The tunnel decided which flows to hand over through a Reader that
nothing implemented, so under phase up no model host was read. phase up
now sets the proxy's own: the hosts leak protection reads, each for the
processes of the tool it is listed under, with the tools being the ones
the tunnel can recognise a process as.

Every CONNECT the router sends is told of first, as a tunnel's flow and
with its tool, or with none when it isn't to be read. A CONNECT the
proxy knows nothing about is taken for a program that was given the
proxy through its environment, where any listed host is read: a
Connection's host that is a model host too was read for every program
that way. The engine tells the proxy even when no Reader is set.

When the proxy can't keep what it is told, a flow that was only there
to be read goes on unread and is counted. A Connection's flow still
goes for its credential, on a connection the proxy knows to be the
tunnel's, and nothing of it is read.

Upstream.OnlyOverrides refuses every address a test has no stand-in
for, so tests that use the proxy's real host names can't leave the
machine.
… engine

With only phase up running there is no session and no hook-managed
proxy, so a secrets command had nothing to register with, and the
values it handed out went to a model unreplaced.

While its proxy serves a session the engine now keeps where the proxy
listens, with the capability its control API asks for, in a file of its
own that only its owner can read. The status still holds no credential.
The file goes when the session ends, when the engine stops, and when a
new engine starts in the same place.

leakguard gets a third source that reads it. The engine's proxy is the
one that reads what the tools on this machine send through the tunnel,
so it is registered with whatever else runs: beside the hook-managed
proxy, and inside a session beside that session's own. The policy is
the one for any proxy that is not the session's: with no engine running
a command pays one file check and makes no call; state that isn't a
regular file of the owner's alone, is of another version or names a
dead process isn't used; a refused connection means no proxy runs there
and is silent; any other failure warns once, pointing at phase status
and naming the engine when another proxy was found too, and never
refuses the command.
A failed phase up left the engine and the helper trying, so Phase could
start capturing later with nobody told. phase up now stops the tunnel as
phase down does when the wait for capture fails or is interrupted, and
says what is true afterwards. It also refuses to start a second engine
beside one that is attached already.

With another VPN down, the range the engine leaves to that VPN's routes
is routed into the tunnel, and what the engine sent there came straight
back to be sent again. A destination that is left to its route, and
whose route leads into the tunnel, is refused for TCP, UDP and pings,
and counted.

The helper removed every route it was asked for, added or not, and the
system removes a route by where it goes to: a failed add over another
VPN's identical route took that VPN's route away. It now keeps what it
added and gives back only that. On macOS the routing table is read
around each step, since route(8) doesn't always say when it failed, and
a capture is all of its routes or none. The helper itself refuses the
default route in either family, and loopback, link-local, multicast and
its own range, whatever the command line let through.

The check for something else using Phase's range looked only at routes
that start 198.19.248. It reads both families now, and finds a route
anywhere in 198.19.248.0/22, in the block around it, or at Phase's IPv6
address.

The control address presents the proxy's password for whoever reaches
it, and answered whatever the request named: a web page whose name was
pointed at it could read a Connection's environment. It answers only as
phase.invalid, or that name with the port the program came to.

One Connection host with a name a DNS entry can't hold made the helper
refuse the whole attach. Such a name is left out, and status counts it.

phase up said that everything on the Mac goes through Phase. It says
that new connections do, and that the ones already open stay as they
are. phase status says the same.

phase down didn't last when an engine it had no pid for was attached:
that engine attached again within a second. The helper now says which
process is attached, and phase down stops it too when it is this user's
phase binary running the engine. Anything else is left alone and named.
The proxy knows each of the tunnel's connections by the address it
comes from, and keeps two things until the tunnel says it is done: that
the connection is the tunnel's, and the note for a CONNECT that never
came. The router said so after it had closed the connection, so for a
moment the port was free while the proxy still took a connection from
it for that one.

It says so first now. Every way a connection to the proxy ends, in the
router before its CONNECT is answered or once the flow is over, goes
through the one Close.
cursor-agent runs as the copy of node kept beside each of its releases,
under ~/.local/share/cursor-agent/versions, and is started through a
link in ~/.local/bin. To the tunnel it was a node program like any
other. A node binary under a cursor-agent folder is Cursor now.
A tunnel's two halves need not share a machine: the half that captures
may run somewhere the proxy does not trust, with one proxy serving many
of them. What the proxy remembered of traffic it remembered per tool and
host, so two capture peers with the same tool on the same host would
have shared it. One peer's refused certificates would have left another
peer's flows unread.

FlowContext gains Peer, an opaque id of the capture peer a flow came
from. The capture layer gives it with the note it leaves for a flow, and
nothing a proxied program sends is read into it. PeerLocal is this
machine, the proxy environment and its own tunnel, and is what an empty
Peer means. An id that is not short and plain is refused with the note.

Each peer has a memory of its own, with its own bounds: the
pass-through windows, the limit on unread reports, the placeholders
already reported, the hellos kept for a host whose hello is copied, and
the count of connections copied or passed through unread. What the
table says of a host is looked up per peer as well. Status tells of the
local peer only.

An event names the peer in detail.peer when it is not the local one, so
the events of everything that runs today are unchanged.
The tunnel's two halves will not always share a machine: the half that
captures may be a sandbox, with one engine serving many. The router took
every flow for this machine's, looked for the program behind it here,
and told the proxy nothing of where it came from.

A Flow now carries its peer. The stack names it by the range the flow's
source address is in, and those ranges are the ones WireGuard is given
as each peer's allowed addresses, so the name is the peer WireGuard took
the packets from. Today there is one peer, the helper, which is the
local one and sends from any address. A source no peer sends from is
refused.

The router passes the peer on when it tells the proxy whose flow a
CONNECT carries, and the proxy keeps what it remembers per peer.

Which tool a flow belongs to is asked of the peer's own rule. The local
peer's rule is the one there was: the program that opened the flow on
this machine. Another kind of peer can bring its own with SetPeerRule,
such as a sandbox enrolled for one tool, and the router needs no change
for it. A peer with no rule is read for no tool.
…r came

A flow to port 443 was held 300 ms for its TLS hello. One whose hello
came later was taken for a flow with no name and relayed as it was for
as long as the program kept it: a tool's model flow went on unread, a
kept connection with every request after the first, and status counted
it as an ordinary flow.

The program speaks first on port 443, so until its hello comes there is
nothing to relay. The flow is now held up to five seconds. What isn't a
hello, and a program that leaves, are still known at once.

When the wait does run out the flow goes on unread as before, and is
counted as unread_no_hello. phase status says how many there were, apart
from the flows that passed through as they should.
The router counted a tool's model flow as read as soon as the proxy
answered its CONNECT. The proxy answers 200 for a tunnel it only relays
as well: for a while after a tool has twice refused its certificate at a
host, that tool's flows there are passed through so the tool keeps
working. Those flows reached the model unread and phase status counted
them as read.

The proxy's answer now says when the tunnel is its own to serve, with an
X-Phase-Tunnel: terminated header. A tunnel it only relays is answered
as before. The router counts a handed-over flow as read when the header
is there and as unread_by_proxy when it isn't, and phase status says how
many went through the proxy unread.

In the proxy this is the one line that writes the answer of a tunnel it
terminates. The header's name is in pkg/proxy, where both sides read it.
Two engine tests hand the proxy a model host through a stand-in reader.
The proxy's own table doesn't read that host, so it only relays the
tool's flow, which the last commit counts as unread_by_proxy and no
longer as read. Their expectations still said read, and failed.
The engine resolves names itself, against the network's DNS servers.
1.1.1.1 and 8.8.8.8 were kept in the same list as those and every
lookup moved on to the next server in it, so about half the queries for
a private or split-horizon Connection host went to a public resolver.
Its answer that the name doesn't exist ended the lookup, or it gave the
public address of a host that has another one inside.

The network's servers are now asked in the system's order, each in its
turn, and the next only when one doesn't answer. An answer that a name
doesn't exist is an answer. The public resolvers are asked only when the
network has no server of its own or none of them answers. Names the
tunnel's resolver passes on are asked the same way.

On macOS the servers the system keeps for one domain, as another VPN's
split DNS sets up, are read as well, and a name under such a domain is
asked of them first. The tunnel's own entries are left out as before.
The engine read the network's DNS servers once, when it started. After
a move to another network it went on asking the old network's servers:
every lookup waited for them to time out, and a Connection host only
the new network's servers know never resolved.

The watch on the default route already follows the machine to another
interface. It now counts each change it sees: another interface, the
same interface with another gateway, and a default route that went away
and came back, which is what a move between two networks on the same
interface looks like. The servers are read again before the next lookup
after a change, and again while a read finds none, since the network
they belong to may not be up yet.

Reading them runs scutil, so it is given five seconds and no more.
With everything routed into the tunnel, a host Phase should have is
caught by the name in its TLS hello. UDP was relayed to any real address
with nothing asked, so a program speaking HTTP/3 to its model host, or
to a Connection's host, went past both the reading and the proxy.

UDP to port 443 at a real address is now refused while everything is
captured. The datagram starts no conversation, and the tunnel's stack
answers it as a host answers one for a closed port, with a port
unreachable message from the address the program sent to. A program
holding a connected socket is told at once and falls back to TCP, where
its hello names the host.

Each refused datagram is counted as udp_443_refused, and phase status
says how many there were. All other UDP is relayed as before, and so is
UDP to port 443 when only part of the network is captured: hosts are
caught by their names then, and their tunnel addresses already give UDP
no answer.
When the helper stepped aside it asked for its capture routes to be
removed, took no answer, and forgot them all. A route whose removal
failed stayed in place with nothing on record: half the address space
still led into a tunnel nobody answered, and the helper never tried
again.

Removing routes now reports the ones that are still there, by the
routing table where it can be read and by route's own word where it
can't. The helper keeps those on record, so the next step aside tries
again, and an engine that comes back takes them up with the rest. Its
log and its status name them, and phase status says which routes still
lead into the tunnel and that phase down removes them: closing the
device takes its routes with it.
… starts

An engine removed whatever proxy state it found as the first thing it
did, since an engine that was killed leaves its state behind. A second
engine started beside a running one did the same, then stopped because
the port was taken: the running engine's state was gone, and no command
could register a secret value with its proxy until the session was next
reopened.

The state is now removed once the engine holds the port the helper
reaches it on. An engine that is still running holds that port, so a
second one stops before it touches anything.
…is written

Two things were left saying more than was true.

When the proxy's state couldn't be written, the engine logged it and
went on to say it was ready. A command finds the proxy through that
state, so no secret value it handed out was registered with leak
protection. The engine now records the failure as its error, stays not
ready, and hands the helper nothing to capture for that session.

When a session stopped, only the state file went. The router kept the
proxy's address and the capability it presents there, and the engine
stayed ready. Until the next session started, a Connection's flow was
sent to that port with the capability, for whatever listened there. The
router now forgets both when the session stops and the engine is not
ready until it serves one again. In between a Connection's flow fails,
and a model host's goes on unread, as with any proxy that isn't there.
phase up looked for a running engine, started one if it found none, and
only then put the engine's process on record. Nothing kept a second run
from looking in between: two runs at once each started an engine, and a
phase down in that moment found no engine to stop.

Both now take one lock in the tunnel's state directory. phase up holds
it from before it looks for an engine until the one it started is on
record, and phase down while it stops the tunnel. Each waits for the
other, up to half a minute.

When the lock can't be had, phase up says another run is still at work
and starts nothing. phase down goes ahead without it: stepping aside is
never refused.
… from

Two entries of the table took other programs for a tool, and their model
traffic was then read as the tool's.

A node or bun process counted as a tool when any of its arguments
mentioned the tool's package, so a script that was only told the name,
node deps.js @openai/codex, was Codex. The entry now names the folder
the tool is installed in, and holds when an argument is a path that goes
through that folder: the script the runtime was started with.

Any executable whose name starts with codex- was Codex. That is how the
package names its binary for each platform, so the entry now holds only
for such a file inside the package's own folder.

bun x opencode-ai@latest is no longer OpenCode by its arguments: they
name a package and no path. What it starts is still recognised, by its
own executable or by the script it runs.
When an engine sent the helper more names while it ran, the helper kept
the whole list as the names it captures, and then tried to write their
entries. Writing stops at the first name that already has an entry of
someone else's, so the helper's status named hosts it had no entry for.

Installing the entries now returns the ones that are in place, read back
from the directory, and the helper reports those: after an attach, after
an update, and none once it has stepped aside. What is written, and what
is left alone, is as before.

phase status names any name the engine sent that the helper has no entry
for. Such a host is still caught by the name in its TLS hello.
The test of the names a helper reports slept half a second after it sent
more names. On a slow run the helper might not have handled them by
then, and the test would have passed on the state from before. It now
waits until the helper has asked for its entries to be written again.
The router counts a tool's flow as read when the proxy's answer to its
CONNECT says the proxy serves the tunnel. A pass-through window now
belongs to the client that refused the certificate, which the proxy
knows by the client's TLS hello, and the hello comes after the answer.
So the proxy answers that it serves the tunnel, reads the hello, and
only then hands the flow to its service unread. The flow was counted as
read.

The proxy now tells the tunnel when it hands one of the tunnel's
connections on after answering it: for a client in a window of its own,
and for a hello that has to be copied and can't be. A tunnel asks to be
told with Engine.OnTunnelUnread, for a connection it has said is its
own, and the router's reader passes the word on. A flow that was counted
as read by the answer moves to unread_by_proxy, and one the proxy is
heard from before its answer was read is never counted as read.
With everything captured the engine refuses UDP to port 443, so every
program that speaks HTTP/3 falls back to TCP: browsers, and anything
else on the machine. phase up said nothing of it. It now says so in one
line, and only when it captures everything, since UDP is left as it was
when part of the network is captured.
Every user's traffic on the Mac reaches the tunnel. A flow from this machine now goes to the proxy, the Postgres adapter or the control address only when its program is found and runs as the engine's user. Another user's flow, or one whose program can't be found in time, goes to the real host unread and is counted apart.
phase up and phase status warn when HTTPS_PROXY, HTTP_PROXY or ALL_PROXY is set where phase up was started, and the help says so. Only whether a variable is set is looked at; its value is never kept or shown.
…lper

A program named sudo earlier on PATH would otherwise be run, and handed the password.
sudo runs phase as root to install or remove the helper, and the helper is copied from it. Refuse when the program or its folder can be changed by anyone but root and the current user, and say how to fix it.
A control message is read up to 256 KiB, so a line with no end can't grow without limit. The helper's accept loop pauses and tries again after an error such as running out of file descriptors, and stops only when its socket is closed.
A socket is pinned to the physical interface when its route leads into the tunnel, already leaves by that interface, or can't be told. One whose route goes by another VPN is left on it, so that VPN's resolver and hosts stay reachable.
A VPN that sets up servers for its own domain changes no route out, so its names kept going to the old servers. A read made only because of age that finds no servers keeps the ones that worked.
…vers

When the domain's servers and the network's own don't answer, such a name fails rather than go to 1.1.1.1 or 8.8.8.8.
… too

A tool doing its own DNS can reach a model host at a real address inside the captured networks. Its HTTP/3 is now refused there as well, so it falls back to TCP where its TLS name decides.
…sing it

A program may hold a DNS answer for its 10 seconds. With more names than the pool holds inside that time, a new name now gets SERVFAIL rather than an address another name may still be using.
A program named ps earlier on PATH could otherwise pass any process off as the engine for phase down to stop.
…edentials

Only this user's own programs get Connection credentials. The help says so, and phase status says it when Connection flows went on without a program of yours behind them.
…roto-relay

# Conflicts:
#	src/cmd/leak_protection.go
#	src/cmd/leak_protection_test.go
#	src/cmd/users_logout.go
#	src/cmd/users_logout_test.go
#	src/internal/aiproxy/CONTROL_API.md
#	src/internal/aiproxy/engine.go
#	src/internal/aiproxy/leak.go
#	src/internal/airuntime/runtime.go
#	src/internal/leakguard/leakguard.go
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Deploying phase-cli-install-script with  Cloudflare Pages  Cloudflare Pages

Latest commit: f1ee745
Status: ✅  Deploy successful!
Preview URL: https://3c67a345.phase-cli-install-script.pages.dev
Branch Preview URL: https://feat--phase-proto-relay.phase-cli-install-script.pages.dev

View logs

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant