Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs-site/src/content/docs/guides/remote-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ To disconnect a Child-initiated link, run `ocx disconnect` on the Child. It disc

On a full OpenCodex Child, `ocx disconnect` also restarts a client runtime that is still running, so the Child is serving standalone OpenCodex when the command returns. If that restart does not complete, the command exits non-zero and prints advice for the state it observed: `ocx restart` on the Child when the same runtime still serves the old connection; `ocx service restart` when a service that cannot be shown to relaunch the runtime refused the restart; and inspection with `ocx status` and `ocx connect status` when a new connection is in use, the client state cannot be read, or a different or unknown runtime answers. A client runtime whose connection was removed accepts `ocx restart` when its owner can relaunch it: the OpenCodex desktop app, or a background service that is enabled and running, starts the standalone replacement, so the same command works over SSH with nobody at the Child's screen. A service that cannot be shown to relaunch it (for example a unit started by hand while disabled) keeps the runtime and the command says to run `ocx service restart`. A runtime that carries only `OCX_SERVICE=1` (a service installed before 2.49, or a proxy started by `ocx claude`, `ocx codex` or `ocx opencode`) is not changed by this: `ocx disconnect` does not restart it, and `ocx restart` refuses while its service is viable, as before. The same holds for a client runtime started by an OpenCodex version before this change: `ocx disconnect` restarts a runtime only after it has been restarted once on the new version (an update's own restart, the desktop app restarting it, `ocx service restart`, or `ocx restart`).

To see how much a link carries, run `ocx link status --json`. Each link has a `traffic` block: the bytes of request bodies sent toward the Home (`toHomeBytes`) and of response bodies sent back (`fromHomeBytes`), the number of requests and of requests that ended early (`incomplete`), all counted since that link's counting last started (`since`, with its `epoch` and `generation`). Compare two readings only when both their `epoch` and `generation` are the same; a new value in either means counting started again for that link, and a restart always gives a new `epoch`. On a Child the numbers are the bytes its link transport carried, including its own reads of the Home; a request also counts as `incomplete` there when its body upload did not finish, and when `incomplete` is above zero `toHomeBytes` can miss part of a request body that was cut off, so it is a lower bound. A request body still being sent counts only once its write completes, so a reading taken mid-upload can trail what the Home already received; the difference between two readings counts completed writes. A Child shows them on its dashboard's status read, since `ocx link status` on a Child reads only local files. On a Home the response side is counted as it is sent and the request side is each request's declared length (`toHomeSource: "content-length"`); requests that declared none are counted in `toHomeUncounted`. OpenCodex keeps counters for up to 256 links at a time; a request it had no room to measure is counted in `untrackedRequests`. Headers, SSH overhead and anything a relay between the machines adds are not included, and a restart starts the count again. OpenCodex does not guess whether a route is direct or goes through a relay. A link with one route uses its alias; for a link with several routes, `ocx link status --json` names the route in use and since when (`route.name`, `route.since`), and the route's own name tells you which kind it is. Each row of `ocx logs --json` also carries `requestBody`, the request body's size as received (`wireBytes`), its `encoding`, and its size after decoding (`decodedBytes`).
To see how much a link carries, run `ocx link status --json`. Each link has a `traffic` block: the bytes of request bodies sent toward the Home (`toHomeBytes`) and of response bodies sent back (`fromHomeBytes`), the number of requests and of requests that ended early (`incomplete`), all counted since that link's counting last started (`since`, with its `epoch` and `generation`). Compare two readings only when both their `epoch` and `generation` are the same; a new value in either means counting started again for that link, and a restart always gives a new `epoch`. On a Child the numbers are the bytes its link transport carried, including its own reads of the Home; a request also counts as `incomplete` there when its body upload did not finish, and when `incomplete` is above zero `toHomeBytes` can miss part of a request body that was cut off, so it is a lower bound. A request body still being sent counts only once its write completes, so a reading taken mid-upload can trail what the Home already received; the difference between two readings counts completed writes. A write completes once the operating system accepts it, and Windows can accept a whole large request body at once, so on a Windows Child a body can count in full as soon as it is sent, even if the Home reads only part of it. A Child shows them on its dashboard's status read, since `ocx link status` on a Child reads only local files. On a Home the response side is counted as it is sent and the request side is each request's declared length (`toHomeSource: "content-length"`); requests that declared none are counted in `toHomeUncounted`. OpenCodex keeps counters for up to 256 links at a time; a request it had no room to measure is counted in `untrackedRequests`. Headers, SSH overhead and anything a relay between the machines adds are not included, and a restart starts the count again. OpenCodex does not guess whether a route is direct or goes through a relay. A link with one route uses its alias; for a link with several routes, `ocx link status --json` names the route in use and since when (`route.name`, `route.since`), and the route's own name tells you which kind it is. Each row of `ocx logs --json` also carries `requestBody`, the request body's size as received (`wireBytes`), its `encoding`, and its size after decoding (`decodedBytes`).

Between a Child and a Home that both run this version, the Child compresses large model requests (above 1 KiB, up to 16 MiB, with no encoding of their own) with zstd before they cross the link, each request on its own. It does so only after the Home has said, on the link itself, that it accepts them, so a Child linked to an older Home sends requests as before. The Home refuses a compressed request that would expand more than 200 times past 8 MiB, which protects it from compression bombs. The SSH connection itself is not compressed.

Expand Down
6 changes: 5 additions & 1 deletion src/link/traffic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
* `incomplete` > 0 `toHomeBytes` is a lower bound of what crossed, and otherwise every request that
* ended had its body counted in full. A write still in progress counts only once it completes, so a
* read taken mid-write lags (it can show `incomplete` 0 while the Home already holds a prefix) and a
* delta between two reads measures completed writes. A Home's hub-link listener (`src/server/index/link-relay-sessions.ts`) counts the
* delta between two reads measures completed writes. A write completes once the operating system
* accepts it, and how much it accepts at once is the OS's choice: Linux and macOS accept what fits
* their send buffer, while Windows can take a whole large nonblocking send into its buffer (it took
* 48 MiB bodies at once on CI), so a body can count in full, and its upload as finished, as soon as
* it is written, even when the Home reads only part of it. A Home's hub-link listener (`src/server/index/link-relay-sessions.ts`) counts the
* response-body bytes it streams back and, for requests, each declared Content-Length (it cannot
* wrap Bun's native request without losing its identity); a request body without one is counted in
* `toHomeUncounted` instead. Neither counts HTTP headers, the relay-auth challenge, SSH or TCP
Expand Down
2 changes: 1 addition & 1 deletion structure/remote-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ The explicit `ocx link revoke --force --yes` branch in `src/cli/link.ts` sends o

### Traffic counters

`GET /api/link/status?traffic=1` adds `traffic` to every link row (#3023); without the query the document is unchanged, so an older `ocx link status` that validates rows key by key keeps working. `ocx link status` sends the query, validates the block exactly (`src/cli/link-status-traffic.ts`) and accepts its absence from an older runtime. `src/link/traffic.ts` owns the book: per link `since`, `epoch`, `generation`, `requests`, `incomplete` (ended before the response body did, or on a Child before its upload finished), `toHomeBytes`, `fromHomeBytes`, `toHomeSource` and `toHomeUncounted`, plus the book-level `untrackedRequests` repeated on every row. Counting is a plain integer addition per body chunk on the JavaScript thread, with no lock, log line, timer or disk write, and no request scans the book. A book holds at most 256 links and never more: a new link takes the place of the link idle longest (the first key of an insertion-ordered map of links with no request in flight, so making room is O(1)); when every tracked link has a request in flight there is no place, and that request is not measured and counts only in `untrackedRequests`. Telemetry never refuses or slows a request. Every time a link's counters start from zero, at its first request or its first after the book forgot it, it takes a new `generation` from one monotonic counter per book; a link the book does not hold reads zeros under the generation of the last forgetting. The book lives in memory; a restart starts a new one with a fresh random `epoch`, so generations from two process lifetimes never compare equal under it. A consumer subtracts two reads only when both `epoch` and `generation` match; `since` is the wall-clock time of that start, for people, and can repeat or go backward with the clock. A Child counts in its connection-bound transport (`fetchBoundLinkRelay` in `src/client/link-relay-transport.ts`) into the process book `childLinkTraffic`, so relayed requests and the Child's own reads of the Home (`linkBoundFetch` and `fetchLinkHomeManagement` in `src/client/link-home.ts`) count alike: a request from the moment its data request starts on the authenticated socket, each request-body chunk or buffered body once its socket write completed, and each response-body chunk as it is read off the socket's stream, before any adapter queue or gzip, deflate or Brotli decoding, so bytes read before a cancel still count. A refused challenge, an expired preflight or a refused connection sent no data and counts nothing (`toHomeSource: "relayed"`). The transport ends a request only once both its response and its upload settled, and counts it complete only when both finished, so a Home that answers in full before reading the whole body still leaves it incomplete. A write still pending when a request ends is not counted, even if part of it reached the Home, so while `incomplete` > 0 `toHomeBytes` is a lower bound of the request bytes that crossed, and with `incomplete` at 0 every request that has ended had its body counted in full. A write still in progress counts only once it completes, so a read taken mid-write lags (it can show `incomplete` 0 while the Home already holds a prefix of that body), and a delta between two reads measures completed writes; response bytes are counted as they arrive and need no such caveat. The Home's hub-link listener lifecycle owns its own book (`createLinkTrafficBook("content-length")` in `src/server/index/link-listener.ts`), and `src/server/index/link-relay-sessions.ts` counts only requests on a verified relay session: response-body bytes as they stream (a request aborted before its response headers stays incomplete), and on the request side each request's declared Content-Length, because wrapping Bun's native request would lose the identity `requestIP` and `timeout` need; a body without Content-Length adds to `toHomeUncounted`. A legacy key admission without a relay session is not attributed to a link. Neither side counts HTTP headers, relay-auth challenges, SSH or TCP framing, or bytes a relay between the machines adds.
`GET /api/link/status?traffic=1` adds `traffic` to every link row (#3023); without the query the document is unchanged, so an older `ocx link status` that validates rows key by key keeps working. `ocx link status` sends the query, validates the block exactly (`src/cli/link-status-traffic.ts`) and accepts its absence from an older runtime. `src/link/traffic.ts` owns the book: per link `since`, `epoch`, `generation`, `requests`, `incomplete` (ended before the response body did, or on a Child before its upload finished), `toHomeBytes`, `fromHomeBytes`, `toHomeSource` and `toHomeUncounted`, plus the book-level `untrackedRequests` repeated on every row. Counting is a plain integer addition per body chunk on the JavaScript thread, with no lock, log line, timer or disk write, and no request scans the book. A book holds at most 256 links and never more: a new link takes the place of the link idle longest (the first key of an insertion-ordered map of links with no request in flight, so making room is O(1)); when every tracked link has a request in flight there is no place, and that request is not measured and counts only in `untrackedRequests`. Telemetry never refuses or slows a request. Every time a link's counters start from zero, at its first request or its first after the book forgot it, it takes a new `generation` from one monotonic counter per book; a link the book does not hold reads zeros under the generation of the last forgetting. The book lives in memory; a restart starts a new one with a fresh random `epoch`, so generations from two process lifetimes never compare equal under it. A consumer subtracts two reads only when both `epoch` and `generation` match; `since` is the wall-clock time of that start, for people, and can repeat or go backward with the clock. A Child counts in its connection-bound transport (`fetchBoundLinkRelay` in `src/client/link-relay-transport.ts`) into the process book `childLinkTraffic`, so relayed requests and the Child's own reads of the Home (`linkBoundFetch` and `fetchLinkHomeManagement` in `src/client/link-home.ts`) count alike: a request from the moment its data request starts on the authenticated socket, each request-body chunk or buffered body once its socket write completed, and each response-body chunk as it is read off the socket's stream, before any adapter queue or gzip, deflate or Brotli decoding, so bytes read before a cancel still count. A refused challenge, an expired preflight or a refused connection sent no data and counts nothing (`toHomeSource: "relayed"`). The transport ends a request only once both its response and its upload settled, and counts it complete only when both finished, so a Home that answers in full before reading the whole body still leaves it incomplete. A write still pending when a request ends is not counted, even if part of it reached the Home, so while `incomplete` > 0 `toHomeBytes` is a lower bound of the request bytes that crossed, and with `incomplete` at 0 every request that has ended had its body counted in full. A write still in progress counts only once it completes, so a read taken mid-write lags (it can show `incomplete` 0 while the Home already holds a prefix of that body), and a delta between two reads measures completed writes. A write completes once the operating system accepts it, and how much it accepts at once is the OS's choice: Linux and macOS accept what fits their send buffer, while Windows can take a whole large nonblocking send into its buffer (it took 48 MiB bodies at once on CI), so a body can count in full, and its upload as finished, as soon as it is written, even when the Home reads only part of it. Response bytes are counted as they arrive and need no such caveat. The Home's hub-link listener lifecycle owns its own book (`createLinkTrafficBook("content-length")` in `src/server/index/link-listener.ts`), and `src/server/index/link-relay-sessions.ts` counts only requests on a verified relay session: response-body bytes as they stream (a request aborted before its response headers stays incomplete), and on the request side each request's declared Content-Length, because wrapping Bun's native request would lose the identity `requestIP` and `timeout` need; a body without Content-Length adds to `toHomeUncounted`. A legacy key admission without a relay session is not attributed to a link. Neither side counts HTTP headers, relay-auth challenges, SSH or TCP framing, or bytes a relay between the machines adds.

A connected Child serves status from its machine listener (`src/client/machine-listener.ts`) to its dashboard session only, and `?traffic=1` there adds the Child's own link as a row (`projectClientLinkRow` in `src/client/link-status.ts`) with its counters: a sidecar for this link names the alias, direction and tunnel state; an unreadable sidecar, one for another link, or a missing sidecar while the durable `child-initiated.json` marker names this link (or cannot be read) makes the row `failed` (`sidecar_invalid`, `sidecar_mismatch`, `sidecar_missing`) with alias `unknown`; otherwise no sidecar is a Home-initiated link that reads idle since it connected, under the Home's recorded name (`linkHomeName`) or `unknown`, never the loopback URL the Child connects to; `ocx link status` on a Child reads local files and cannot show them. Coverage: `tests/clients/link-traffic.test.ts` (real Home sessions and the real bound transport: a streamed request and SSE response, an aborted response, a request without Content-Length, refused requests, a refused challenge and connection, a gzip response, a Child's own Home read, a Home request aborted before its headers, the hard bound with links in flight and `untrackedRequests`, eviction never touching a link in flight, a new generation under a repeating or backward clock, a new epoch per book, a buffered body whose write never completes, a Home that read a prefix before the cancel, one that read a prefix and then answered in full, and a read taken while a large write is still pending), `tests/clients/link-traffic-child-status.test.ts` (the Child's machine listener), `tests/server/link-traffic-status.test.ts` (the plain document's literal bytes, the opt-in document, CLI validation).

Expand Down
Loading
Loading