Skip to content

Commit b7615e0

Browse files
authored
Exec/ACP: inject OPENCLAW_SHELL into child shell env (#31271)
* exec: mark runtime shell context in exec env * tests(exec): cover OPENCLAW_SHELL in gateway exec * tests(exec): cover OPENCLAW_SHELL in pty mode * acpx: mark runtime shell context for spawned process * tests(acpx): log OPENCLAW_SHELL in runtime fixture * tests(acpx): assert OPENCLAW_SHELL in runtime prompt * docs(env): document OPENCLAW_SHELL runtime markers * docs(exec): describe OPENCLAW_SHELL exec marker * docs(acp): document OPENCLAW_SHELL acp marker * docs(gateway): note OPENCLAW_SHELL for background exec * tui: tag local shell runs with OPENCLAW_SHELL * tests(tui): assert OPENCLAW_SHELL in local shell runner * acp client: tag spawned bridge env with OPENCLAW_SHELL * tests(acp): cover acp client OPENCLAW_SHELL env helper * docs(env): include acp-client and tui-local shell markers * docs(acp): document acp-client OPENCLAW_SHELL marker * docs(tui): document tui-local OPENCLAW_SHELL marker * exec: keep shell runtime env string-only for docker args * changelog: note OPENCLAW_SHELL runtime markers
1 parent aeb8173 commit b7615e0

16 files changed

Lines changed: 145 additions & 7 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Docs: https://docs.openclaw.ai
2121
- Tools/Diffs: add a new optional `diffs` plugin tool for read-only diff rendering from before/after text or unified patches, with gateway viewer URLs for canvas and PNG image output. Thanks @gumadeiras.
2222
- Memory/LanceDB: support custom OpenAI `baseUrl` and embedding dimensions for LanceDB memory. (#17874) Thanks @rish2jain and @vincentkoc.
2323
- ACP/ACPX streaming: pin ACPX plugin support to `0.1.15`, add configurable ACPX command/version probing, and streamline ACP stream delivery (`final_only` default + reduced tool-event noise) with matching runtime and test updates. (#30036) Thanks @osolmaz.
24+
- Shell env markers: set `OPENCLAW_SHELL` across shell-like runtimes (`exec`, `acp`, `acp-client`, `tui-local`) so shell startup/config rules can target OpenClaw contexts consistently, and document the markers in env/exec/acp/TUI docs. Thanks @vincentkoc.
2425
- Cron/Heartbeat light bootstrap context: add opt-in lightweight bootstrap mode for automation runs (`--light-context` for cron agent turns and `agents.*.heartbeat.lightContext` for heartbeat), keeping only `HEARTBEAT.md` for heartbeat runs and skipping bootstrap-file injection for cron lightweight runs. (#26064) Thanks @jose-velez.
2526
- OpenAI/WebSocket warm-up: add optional OpenAI Responses WebSocket warm-up (`response.create` with `generate:false`), enable it by default for `openai/*`, and expose `params.openaiWsWarmup` for per-model enable/disable control.
2627
- Agents/Subagents runtime events: replace ad-hoc subagent completion system-message handoff with typed internal completion events (`task_completion`) that are rendered consistently across direct and queued announce paths, with gateway/CLI plumbing for structured `internalEvents`.

‎docs/cli/acp.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ Security note:
179179

180180
- `--token` and `--password` can be visible in local process listings on some systems.
181181
- Prefer `--token-file`/`--password-file` or environment variables (`OPENCLAW_GATEWAY_TOKEN`, `OPENCLAW_GATEWAY_PASSWORD`).
182+
- ACP runtime backend child processes receive `OPENCLAW_SHELL=acp`, which can be used for context-specific shell/profile rules.
183+
- `openclaw acp client` sets `OPENCLAW_SHELL=acp-client` on the spawned bridge process.
182184

183185
### `acp client` options
184186

‎docs/gateway/background-process.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Behavior:
2828
- When backgrounded (explicit or timeout), the tool returns `status: "running"` + `sessionId` and a short tail.
2929
- Output is kept in memory until the session is polled or cleared.
3030
- If the `process` tool is disallowed, `exec` runs synchronously and ignores `yieldMs`/`background`.
31+
- Spawned exec commands receive `OPENCLAW_SHELL=exec` for context-aware shell/profile rules.
3132

3233
## Child process bridging
3334

‎docs/help/environment.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ Env var equivalents:
5656
- `OPENCLAW_LOAD_SHELL_ENV=1`
5757
- `OPENCLAW_SHELL_ENV_TIMEOUT_MS=15000`
5858

59+
## Runtime-injected env vars
60+
61+
OpenClaw also injects context markers into spawned child processes:
62+
63+
- `OPENCLAW_SHELL=exec`: set for commands run through the `exec` tool.
64+
- `OPENCLAW_SHELL=acp`: set for ACP runtime backend process spawns (for example `acpx`).
65+
- `OPENCLAW_SHELL=acp-client`: set for `openclaw acp client` when it spawns the ACP bridge process.
66+
- `OPENCLAW_SHELL=tui-local`: set for local TUI `!` shell commands.
67+
68+
These are runtime markers (not required user config). They can be used in shell/profile logic
69+
to apply context-specific rules.
70+
5971
## Env var substitution in config
6072

6173
You can reference env vars directly in config string values using `${VAR_NAME}` syntax:

‎docs/tools/exec.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Notes:
4040
then falls back to Windows PowerShell 5.1.
4141
- Host execution (`gateway`/`node`) rejects `env.PATH` and loader overrides (`LD_*`/`DYLD_*`) to
4242
prevent binary hijacking or injected code.
43+
- OpenClaw sets `OPENCLAW_SHELL=exec` in the spawned command environment (including PTY and sandbox execution) so shell/profile rules can detect exec-tool context.
4344
- Important: sandboxing is **off by default**. If sandboxing is off and `host=sandbox` is explicitly
4445
configured/requested, exec now fails closed instead of silently running on the gateway host.
4546
Enable sandboxing or use `host=gateway` with approvals.

‎docs/web/tui.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Other Gateway slash commands (for example, `/context`) are forwarded to the Gate
113113
- Prefix a line with `!` to run a local shell command on the TUI host.
114114
- The TUI prompts once per session to allow local execution; declining keeps `!` disabled for the session.
115115
- Commands run in a fresh, non-interactive shell in the TUI working directory (no persistent `cd`/env).
116+
- Local shell commands receive `OPENCLAW_SHELL=tui-local` in their environment.
116117
- A lone `!` is sent as a normal message; leading spaces do not trigger local exec.
117118

118119
## Tool output

‎extensions/acpx/src/runtime-internals/process.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function spawnWithResolvedCommand(
132132

133133
return spawn(resolved.command, resolved.args, {
134134
cwd: params.cwd,
135-
env: process.env,
135+
env: { ...process.env, OPENCLAW_SHELL: "acp" },
136136
stdio: ["pipe", "pipe", "pipe"],
137137
shell: resolved.shell,
138138
windowsHide: resolved.windowsHide,

‎extensions/acpx/src/runtime-internals/test-fixtures.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const fs = require("node:fs");
2020
2121
const args = process.argv.slice(2);
2222
const logPath = process.env.MOCK_ACPX_LOG;
23+
const openclawShell = process.env.OPENCLAW_SHELL || "";
2324
const writeLog = (entry) => {
2425
if (!logPath) return;
2526
fs.appendFileSync(logPath, JSON.stringify(entry) + "\n");
@@ -146,7 +147,14 @@ if (command === "sessions" && args[commandIndex + 1] === "close") {
146147
147148
if (command === "prompt") {
148149
const stdinText = fs.readFileSync(0, "utf8");
149-
writeLog({ kind: "prompt", agent, args, sessionName: sessionFromOption, stdinText });
150+
writeLog({
151+
kind: "prompt",
152+
agent,
153+
args,
154+
sessionName: sessionFromOption,
155+
stdinText,
156+
openclawShell,
157+
});
150158
const requestId = "req-1";
151159
152160
emitJson({

‎extensions/acpx/src/runtime.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe("AcpxRuntime", () => {
102102
const prompt = logs.find((entry) => entry.kind === "prompt");
103103
expect(ensure).toBeDefined();
104104
expect(prompt).toBeDefined();
105+
expect(prompt?.openclawShell).toBe("acp");
105106
expect(Array.isArray(prompt?.args)).toBe(true);
106107
const promptArgs = (prompt?.args as string[]) ?? [];
107108
expect(promptArgs).toContain("--ttl");

‎src/acp/client.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { RequestPermissionRequest } from "@agentclientprotocol/sdk";
22
import { describe, expect, it, vi } from "vitest";
3-
import { resolvePermissionRequest } from "./client.js";
3+
import { resolveAcpClientSpawnEnv, resolvePermissionRequest } from "./client.js";
44
import { extractAttachmentsFromPrompt, extractTextFromPrompt } from "./event-mapper.js";
55

66
function makePermissionRequest(
@@ -28,6 +28,26 @@ function makePermissionRequest(
2828
};
2929
}
3030

31+
describe("resolveAcpClientSpawnEnv", () => {
32+
it("sets OPENCLAW_SHELL marker and preserves existing env values", () => {
33+
const env = resolveAcpClientSpawnEnv({
34+
PATH: "/usr/bin",
35+
USER: "openclaw",
36+
});
37+
38+
expect(env.OPENCLAW_SHELL).toBe("acp-client");
39+
expect(env.PATH).toBe("/usr/bin");
40+
expect(env.USER).toBe("openclaw");
41+
});
42+
43+
it("overrides pre-existing OPENCLAW_SHELL to acp-client", () => {
44+
const env = resolveAcpClientSpawnEnv({
45+
OPENCLAW_SHELL: "wrong",
46+
});
47+
expect(env.OPENCLAW_SHELL).toBe("acp-client");
48+
});
49+
});
50+
3151
describe("resolvePermissionRequest", () => {
3252
it("auto-approves safe tools without prompting", async () => {
3353
const prompt = vi.fn(async () => true);

0 commit comments

Comments
 (0)