Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

docs(agent): document the rpc channel and the turn-limit exit code

thinhlpgb71d7e8dbe2b
diff --git a/docs/src/pages/docs/agent/cli.mdx b/docs/src/pages/docs/agent/cli.mdx
index 1de322e4d..a3aece92e 100644
--- a/docs/src/pages/docs/agent/cli.mdx
+++ b/docs/src/pages/docs/agent/cli.mdx
@@ -104,7 +104,7 @@ jan cli agent run --max-turns 5 --max-session-tokens 20000 "triage the build fai
| `--fork-session` | Open the resumed session as a new thread, leaving the original resumable |
| `--worktree` | Work in a dedicated git worktree instead of the project directory. Off by default |
| `--no-worktree` | Work in the project directory, overriding a persistent `worktree` setting |
-| `--max-turns <N>` | Hard cap on agentic turns. If the model is still calling tools at N the run fails with `reached the N-turn limit...` and a non-zero exit; with `--output-format json` the partial answer is still in `result`. Bounds the parent run only - subagent turns are not counted. `0` means unbounded, which is the default |
+| `--max-turns <N>` | Hard cap on agentic turns. If the model is still calling tools at N the run fails with `reached the N-turn limit...` and exits `53`; with `--output-format json` the partial answer is still in `result`. Bounds the parent run only - subagent turns are not counted. `0` means unbounded, which is the default |
| `--max-session-tokens <N>` | Advisory token ceiling for this run, overriding `[budget].max_tokens`. Triggers compaction and a recorded note when passed; it does not terminate the run. `0` means no ceiling |
| `--max-budget-usd <USD>` | Stop the run once it has spent this much, overriding `[budget].max_usd`. Priced from the provider's published rates, so a model with no published price is **refused** rather than run uncapped - including a subagent whose definition names its own model. Subagents inherit the ceiling. Compaction is billed against it like any other request. Stopping this way exits `0` with `stop_reason: budget_exceeded`; `0` as a limit stops at the first billed request |
| `--provider`, `--api-key` | Credential overrides for this run |
@@ -181,15 +181,14 @@ had said before it broke as `result`:
route that reports a zero read is - the prefix was written every turn and never reused. Piped
consumers should branch on the difference rather than coalescing with `0`.
-The process exit code is unchanged: `0` on success, `1` on failure.
+One-shot exit codes: `0` on completion, `1` on failure, `2` on bad CLI arguments, `53` when `--max-turns` is exhausted while the model is still calling tools. `--max-session-tokens` is advisory and does not produce exit `53`.
A run stopped by `--max-budget-usd` exits **`0`**: the turns it took are real work with a real
answer, and the ceiling did what it was asked to do. Hitting `--max-turns` while the model is still
-calling tools exits **`1`**, because that one ends the run with no answer at all. So a plain-text
-consumer cannot tell a budget stop from a finished task by the exit code alone - the answer may be
-knowingly unfinished. A CI job that must distinguish them should read `stop_reason` from
-`--output-format json` and branch on `budget_exceeded`; in text mode the run records a
-`[cost ceiling reached]` note instead.
+calling tools exits **`53`**: it has no final answer. A plain-text consumer cannot tell a budget
+stop from a finished task by exit code alone - the answer may be knowingly unfinished. CI can
+read `stop_reason` from `--output-format json` and branch on `budget_exceeded`; in text mode the run
+records a `[cost ceiling reached]` note instead.
#### Streaming JSON, in both directions
@@ -286,6 +285,30 @@ jan cli agent status
jan cli agent status --project ~/code/app
```
+### `rpc`
+
+Run one long-lived Jan process with multiple addressable sessions over LF-delimited JSON-RPC on stdin/stdout. `jan cli agent rpc` prints **protocol lines only** to stdout. The first request is `initialize` with `protocolVersion: 1` and a `clientInfo` name/version; wait for its response, then send the `initialized` notification before any other method. Calling a method early returns `-32002`.
+
+```bash
+jan cli agent rpc
+jan cli agent rpc-schema # generated request/event schemas on stdout
+```
+
+One client flow:
+
+```text
+initialize {protocolVersion:1,clientInfo:{name,version}} -> version and serverInfo
+initialized -> notification
+session/start {cwd,model?,ephemeral?} -> {sessionId}
+turn/start {sessionId,input:"Hello"} -> {turnId} immediately
+item/<stream-json tag> -> {sessionId,turnId,event}
+turn/completed -> {sessionId,turnId,stopReason,error?}
+```
+
+The `item/*` payload contains the existing `StreamEvent` object unchanged. A client can also call `session/list`, `session/resume`, `session/fork`, `session/archive`, `turn/steer`, `turn/interrupt` and `permission/respond {requestId,decision}`. Session ids address this **process**, not another RPC process. `turn/interrupt` emits one `turn/completed` with `stopReason: "interrupted"` and acknowledges the request; a client that closes stdin ends the run, and an outstanding permission request is never granted - the gate treats a dropped request as a denial. Only one turn is active per process; a second `turn/start` returns `-32001` with `data.retryable: true` rather than silently queueing it. Transport errors (`-32700` malformed JSON, `-32600` invalid request, `-32601` unknown method, `-32602` bad params) differ from an agent failure reported by `turn/completed` with `stopReason: "error"`.
+
+Input frames are capped at 16 MiB and the writer queue holds at most 1025 records. If a client stops reading, the run ends with a terminal overload outcome instead of growing memory without bound. `--max-turns` is a one-shot flag rather than a session setting: an RPC turn runs until it finishes or a client sends `turn/interrupt`. `ephemeral: true` skips thread persistence. Host tool callbacks are **not implemented** on this RPC surface yet; `session/start` refuses a `tools` field rather than advertising it as supported. The `schema` command below documents the older `stream-json` channel, while `rpc-schema` describes this RPC envelope, accepted requests and event types. RPC clients use `turn/completed`, not the process exit code, to classify an individual turn.
+
### `schema`
Print the protocol as a JSON Schema document, generated from the Rust types that define the channel