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
1,038 changes: 1,001 additions & 37 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ name = "rustscript-agent-gateway"
path = "src/bin/rustscript-agent-gateway.rs"

[dependencies]
# Stable ACP v1 process/stdin-stdout proxy boundary. Direct features are limited
# to process and stdio: draft v2, unstable MCP-over-ACP, HTTP/auth surfaces, and
# rmcp client features stay off. Every released v5 SDK unconditionally enables
# the schema's `unstable_proxy_chains` because Proxy/Conductor is its stable
# proxy surface; no v5 feature selection can disable that internal prerequisite.
agent-client-protocol = { version = "5.6.1", default-features = false, features = ["process", "stdio"] }
axum = "0.8"
blocking = "1"
bytes = "1"
futures-util = "0.3"
http-body-util = "0.1"
hyper = { version = "1", features = ["client", "http1"] }
hyper-util = { version = "0.1", features = ["client-legacy", "http1", "tokio"] }
parking_lot = "0.12"
rmcp = { version = "3.11.0", default-features = false, features = ["server", "macros", "transport-io"] }
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
rustscript-vm = { package = "pd-vm", git = "https://github.com/rustscript-lang/rustscript.git", rev = "11c9ee908a29365bd02573311f56e9308eb7dbe6", default-features = false, features = ["runtime", "http-client", "sqlite", "cranelift-jit"] }
pd-host-function = { git = "https://github.com/rustscript-lang/rustscript.git", rev = "11c9ee908a29365bd02573311f56e9308eb7dbe6" }
Expand All @@ -39,6 +47,7 @@ tokio = { version = "1", features = ["full"] }
tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] }
tower = { version = "0.5", features = ["util"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
url = "2"
uuid = { version = "1", features = ["v4"] }
webpki-roots = "1"
Expand Down Expand Up @@ -73,3 +82,10 @@ required-features = ["config-fixture"]
name = "provider_config_fixture_tests"
path = "tests/provider_config_fixture_tests.rs"
required-features = ["config-fixture"]

# The retained-disposition wire test needs the real publisher seam to hold a
# permit across the guest commit decision.
[[test]]
name = "mcp_tool_retained_tests"
path = "tests/mcp_tool_retained_tests.rs"
required-features = ["workspace-io-fault"]
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# rustscript-agent

`rustscript-agent` is a standalone agent runtime with **agent policy in RustScript and authority/durability in Rust**. RSS programs own the model loop, provider adaptation, tool routing, approval policy, and compaction decisions; the Rust host owns admission, capabilities, durable state, effect commits, and transport.
`rustscript-agent` is a standalone agent runtime with **agent policy in RustScript and authority/durability in Rust**. In the bundled runtime, RSS programs own the model loop, provider adaptation, tool routing, approval policy, and compaction decisions; the Rust host owns admission, capabilities, durable state, effect commits, and transport.

## Core properties

Expand Down Expand Up @@ -33,6 +33,13 @@ HTTP / Telegram
-> durable messages, events, receipts, and publications
```

### Use an external harness from an ACP editor

In [ACP mode](docs/acp-mode.md), RustScript Agent is an ACP v1 stdio proxy and
MCP tool provider. Configure your editor to launch the proxy in front of a
compatible harness. The harness owns the model, provider login, and usage charges;
RustScript supplies tools scoped to the ACP session's working directory.

## Documentation

- [Architecture](docs/architecture.md) — RSS/native ownership, typed boundaries, lifecycle, effects, replay, and capability fences
Expand Down
136 changes: 136 additions & 0 deletions docs/acp-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# ACP mode: use an external harness with RustScript tools

RustScript Agent acts as the **ACP proxy and MCP tool provider**. Your editor
speaks Agent Client Protocol (ACP) to the proxy, and the external harness
calls RustScript tools over Model Context Protocol (MCP). The harness owns the
LLM API calls, credentials, model selection, reasoning loop, permissions, and
any usage charges. No RustScript Agent provider login is required in this
mode. Use the harness's own authentication flow; keep credentials out of the
launch arguments and editor configuration.

## Launch from an editor

Configure an ACP client to launch `rustscript-agent acp proxy`. Put proxy
options before `--` and the harness executable and arguments after it. The
proxy launches the harness directly, without a shell. If your editor cannot
find either executable on its `PATH`, use its absolute path instead.

**Gemini CLI:** [Gemini CLI's ACP guide](https://geminicli.com/docs/cli/acp-mode/)
documents `gemini --acp`. Configure the command as follows:

```sh
rustscript-agent acp proxy --read-only -- gemini --acp
```

For an editor with separate executable and argument fields, use the values
below. The field names in your editor may differ.

```json
{
"command": "rustscript-agent",
"args": ["acp", "proxy", "--read-only", "--", "gemini", "--acp"]
}
```

**Claude Agent SDK adapter:** Install the executable using the
[Claude Agent ACP adapter instructions](https://github.com/agentclientprotocol/claude-agent-acp).
The [official package manifest](https://github.com/agentclientprotocol/claude-agent-acp/blob/main/package.json)
lists the package `@agentclientprotocol/claude-agent-acp` and the executable
`claude-agent-acp`. Launch the adapter, not the interactive `claude` command:

```sh
rustscript-agent acp proxy --read-only -- claude-agent-acp
```

These are launch syntax examples, not results from authenticated end-to-end
compatibility tests. Use the harness's supported version and check its MCP and
permission settings if RustScript tools do not appear.

## Compatibility and session setup

The initial release supports **ACP v1 over local stdio** on both sides of the
proxy. ACP v2, remote ACP transports, and native MCP-over-ACP are out of scope.
The harness needs to accept and launch stdio MCP servers declared during ACP
session setup. A working ACP handshake alone does not establish tool support.

The session's `cwd` must be an absolute path to an existing local directory.
It defines the RustScript tool workspace, regardless of where the editor
launched the proxy. The proxy resolves it to a canonical path for the MCP
server and leaves the original `cwd` in the forwarded ACP request.

On `session/new`, the proxy appends one `rustscript-tools` MCP declaration to
`mcpServers`, preserving the caller's existing declarations in order. It does
likewise on `session/load` and `session/resume` only when the harness advertises
the corresponding capability. A caller declaration named `rustscript-tools`
conflicts with the injected server and is rejected. The declaration uses the
running RustScript Agent executable with `tools serve --stdio --workspace` and
the canonical path. You do not need to configure a second binary or add the
same server manually. The harness controls when it starts and stops the MCP
process; an appended declaration does not guarantee a distinct process.

```text
ACP client/editor <-- ACP v1 stdio --> rustscript-agent acp proxy
|
ACP v1 stdio
|
v
External harness <---> LLM API
|
MCP stdio
|
v
rustscript-agent tools serve
|
v
RSS registry + capability hosts
```

```text
Editor -> Proxy -> Harness: initialize and negotiate ACP v1
Editor -> Proxy: session/new with cwd and mcpServers
Proxy -> Harness: session/new with rustscript-tools appended
Harness -> Tool server: start tools serve; MCP initialize and tools/list
Editor -> Proxy -> Harness: session/prompt
Harness <-> LLM API: inference and tool selection
Harness <-> Tool server: tools/call and tool result, when selected
Harness -> Proxy -> Editor: session updates and prompt result
```

## Tool access and lifecycle

- **Read-only (`--read-only`):** RustScript advertises only `read_file` and
`search_files` and rejects writes and process calls before execution. Files
read by the tools can still become part of the context that the harness
sends to its model.
- **Coding (default):** RustScript also exposes `write_file`, `patch`,
`terminal`, and `process`. These tools can change workspace files and start
commands with the local user's privileges. Use coding mode only with a
trusted harness and a workspace you intend it to modify. The RustScript
capability bounds are not an OS sandbox for commands.

The flag only applies to injected RustScript tools. It does not restrict the
harness, its built-in tools, or other MCP servers, and it does not make the
session offline. Review the harness's own permission settings before using
it. This mode does not use the RustScript gateway's provider loop, durable
approvals, or replay.

The proxy owns its direct harness child. Client EOF or a termination signal
initiates bounded shutdown: the proxy attempts to cancel active sessions and
terminates its child if needed. This is not a guarantee about arbitrary
harness grandchildren or other processes started outside RustScript's control.
Keep protocol frames on stdout and diagnostics on stderr for both the ACP and
MCP stdio connections.

## Troubleshooting

| Symptom | What to check |
| --- | --- |
| No ACP handshake or `acp_harness_unavailable` | Check the executable path and enter the harness's ACP mode explicitly. A normal interactive CLI or a v2-only peer is not an ACP v1 stdio agent. |
| MCP declaration rejected or RustScript tools missing | Use `mcpServers: []` in `session/new` or `session/load` if there are no caller servers. `invalid_mcp_servers` means the list is missing or malformed for that method; `mcp_server_name_conflict` means a caller entry is already named `rustscript-tools`. Check that the harness accepts session-supplied stdio MCP servers and that its policy permits starting the injected executable. |
| `invalid_workspace` | Set the editor's session `cwd` to a resolvable absolute directory on the machine running the proxy. Changing the proxy's launch directory does not fix an invalid session `cwd`. |
| Write or process tool absent or denied | With `--read-only`, the RustScript server does not offer those tools and refuses explicit calls. Change the proxy launch profile and reconnect only if you intend to allow writes or execution. A denial from the harness itself must be handled through the harness's permissions. |
| `acp_harness_exited` or the connection drops | Check the harness for startup, authentication, or runtime failures using its own diagnostics. The proxy reports a fixed error code, not the harness's stderr output. Reconnect from the client after resolving the issue. |
| JSON-RPC parse error or startup banner in the stream | Ensure the client, harness, and any launcher keep banners and debug messages off protocol stdout. Do not merge stderr into stdout. |

See the [configuration reference](configuration.md) for the direct MCP launch
syntax and tool-server diagnostics.
94 changes: 81 additions & 13 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# rustscript-agent configuration reference

This document is the canonical configuration reference for this revision of
`rustscript-agent`. It lists **only** configuration that exists in this
revision: every environment variable the binaries read, every native
`rustscript-agent`. It lists **only** inputs that exist in this
revision: environment variables read by binaries or test code, every native
`AgentGatewayConfig` field, and the capability-policy defaults the gateway
passes through to the `pd-vm` core. Reserved-but-unimplemented configuration
is called out explicitly in [Reserved configuration](#reserved-configuration)
and must not be treated as real.

A drift guard (`tests/cases/docs_consistency_tests.rs`) compares this document
against `src/` in both directions: a variable that the binaries read but this
document misses, or a variable this document advertises that no binary reads,
against `src/` in both directions: a variable accounted for in source but
missing here, or one documented here without a corresponding source entry,
fails the test suite.

## Configuration sources

| Source | Owns | Read by |
| --- | --- | --- |
| Gateway environment variables (`RUSTSCRIPT_AGENT_*`, except the separately listed home override) | gateway process settings | `rustscript-agent-gateway` binary (`src/bin/rustscript-agent-gateway.rs`) |
| [Gateway environment variables](#environment-variables-gateway-binary) | gateway process settings | `rustscript-agent-gateway` binary (`src/bin/rustscript-agent-gateway.rs`) |
| [MCP tool server environment](#mcp-tool-server-environment) | stderr tracing and internal readiness/diagnostic inputs | `rustscript-agent tools serve` |
| Agent home (`RUSTSCRIPT_AGENT_HOME`) | library config/auth path resolver, always-attached gateway provider operator auth home, and optional gateway workspace host (only when explicitly set) | library path resolver and gateway home wiring |
| CLI arguments (`--script`, `--allow-host`) | one run | `rustscript-agent` binary (`src/bin/rustscript-agent.rs`) |
| Runner CLI arguments (`--script`, `--allow-host`) | one script run | `rustscript-agent` binary (`src/bin/rustscript-agent.rs`) |
| [ACP and MCP CLI arguments](acp-mode.md) | harness launch and RustScript tool profile or workspace | `rustscript-agent acp proxy` and `rustscript-agent tools serve` |
| Native `AgentGatewayConfig` fields | embedding code | library API; the gateway binary maps a fixed subset from environment variables |

RSS programs never read ambient configuration: all bounds reach the VM
Expand Down Expand Up @@ -48,7 +50,12 @@ YAML field path), never the full Display prose.
A loader sample of both documents (an empty-credential config/auth pair decoded
by the bounded loaders) ships under [docs/examples/](examples/).

## Environment variables (gateway binary)
## Environment variables

### Environment variables (gateway binary)

The gateway reads the agent-home override, managed-workspace settings,
plugin-control settings, and the gateway variables listed in the table below.

### Agent home (library bootstrap and gateway workspace host)

Expand Down Expand Up @@ -191,6 +198,8 @@ RUSTSCRIPT_AGENT_STATE_DB=/var/lib/rustscript-agent/state.db \
cargo run --release --bin rustscript-agent-gateway
```

### Test scratch inputs

`RUSTSCRIPT_AGENT_TEST_TMP` is an **internal test input, not a production
setting**: it is read only by `#[cfg(test)]` code and test fixtures in this
repository to select a writable scratch directory, no binary reads it, and it
Expand All @@ -203,10 +212,69 @@ setting**. The workspace Telegram unit fixture requires this input to select
isolated test scratch storage. It is confined to `#[cfg(test)]` code and does
not configure a packaged gateway.

### MCP tool server environment

`RUSTSCRIPT_AGENT_TOOLS_TRACE` enables tool-server tracing on stderr when
present (even if empty). When unset, the tracing threshold is `WARN`. Raw
MCP payload logging stays disabled. This variable has no gateway alias.

The tool server also reads two internal test inputs in default builds:
`RUSTSCRIPT_AGENT_TEST_READY_MARKER` enables a readiness marker on stderr, and
`RUSTSCRIPT_AGENT_TEST_READY_FILE` requests a file-based readiness signal.
Leave both unset in normal use.

`RUSTSCRIPT_AGENT_TEST_DIAGNOSTIC_FLOOD` exercises the tool server's stderr
diagnostic path in test or `workspace-io-fault` builds only.

### Shared capability test hooks

The process and execution-gate inputs below belong to the shared capability
code, not to gateway or MCP configuration. They are available only in test or
`workspace-io-fault` builds on platforms that compile the corresponding code
paths. They are not deployment settings:

- Process launch: `RUSTSCRIPT_AGENT_TEST_SPAWN_PARK_FILE`,
`RUSTSCRIPT_AGENT_TEST_SPAWN_RELEASE_FILE`,
`RUSTSCRIPT_AGENT_TEST_SPAWN_BEFORE_RECORD_PARK_FILE`,
`RUSTSCRIPT_AGENT_TEST_SPAWN_BEFORE_RECORD_RELEASE_FILE`,
`RUSTSCRIPT_AGENT_TEST_SPAWN_BEFORE_RELEASE_PARK_FILE`, and
`RUSTSCRIPT_AGENT_TEST_SPAWN_BEFORE_RELEASE_RELEASE_FILE`.
- Execution gate: `RUSTSCRIPT_AGENT_TEST_GATE_FORCE_AMBIGUOUS`,
`RUSTSCRIPT_AGENT_TEST_GATE_RELEASE_FAILURE_MARKER`,
`RUSTSCRIPT_AGENT_TEST_GATE_AFTER_RELEASE_PARK_FILE`, and
`RUSTSCRIPT_AGENT_TEST_GATE_AFTER_RELEASE_RELEASE_FILE`.
- Process cleanup: `RUSTSCRIPT_AGENT_TEST_OWNED_CLEANUP_PARK_FILE`,
`RUSTSCRIPT_AGENT_TEST_OWNED_CLEANUP_RELEASE_FILE`,
`RUSTSCRIPT_AGENT_TEST_RETAINED_ATTEMPT_MARKER`,
`RUSTSCRIPT_AGENT_TEST_RETAINED_DECISION_PARK_FILE`,
`RUSTSCRIPT_AGENT_TEST_RETAINED_DECISION_RELEASE_FILE`,
`RUSTSCRIPT_AGENT_TEST_SWEEP_MARKER`,
`RUSTSCRIPT_AGENT_TEST_SWEEP_REAP_PARK_FILE`, and
`RUSTSCRIPT_AGENT_TEST_SWEEP_REAP_RELEASE_FILE`.

## ACP proxy and MCP tool server

```text
rustscript-agent acp proxy [--read-only] -- HARNESS [ARG ...]
rustscript-agent tools serve --stdio --workspace ABSOLUTE_PATH [--read-only]
```

The proxy passes the harness executable and arguments directly, without a
shell. The ACP client's session `cwd` determines the workspace for injected
RustScript tools. `--workspace` is for direct MCP launches; the proxy fills it
in automatically. The default coding profile permits writes and process
execution. `--read-only` limits the RustScript MCP server to file reads and
searches; it does not limit the harness or its other tools.

ACP mode does not need a RustScript Agent provider login or the gateway's
provider settings. Configure authentication, model selection, and permissions
in the external harness. See [ACP mode](acp-mode.md) for editor launch examples,
compatibility, and troubleshooting.

## CLI arguments (runner binary)

`rustscript-agent` is the single-run runner: it compiles one script and
drives its exported `run(context)` to completion with no gateway.
In single-run mode, `rustscript-agent` compiles one script and drives its
exported `run(context)` to completion with no gateway.

| Argument | Type | Default | Notes |
| --- | --- | --- | --- |
Expand Down Expand Up @@ -501,9 +569,9 @@ The following configuration does **not** exist in this revision. It is
listed only to reserve the namespace and to make the roadmap explicit; no
binary reads these names and setting them has no effect.

- **A6 parallel tools / subagents (排除)** remains out of scope and defines
no configuration. Tool approval is implemented through the `approvals`
settings above and durable approval records; a general-purpose harness layer
beyond tool approval is not part of the current product.
- **A6 parallel tools / subagents (排除)** remains out of scope for the bundled
gateway run loop and defines no configuration. The gateway uses the
`approvals` settings above and durable approval records; an external ACP
harness uses its own permission policy.
- A job **scheduler** is not implemented: job CRUD/pause/resume/latest-output
routes exist, but scheduled execution defines no configuration.
Loading
Loading