feat: add exec shell snapshot cache · openclaw/openclaw@c389839
steipete
·
2026-05-31
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -104,6 +104,14 @@ Env var equivalents:
|
104 | 104 | - `OPENCLAW_LOAD_SHELL_ENV=1` |
105 | 105 | - `OPENCLAW_SHELL_ENV_TIMEOUT_MS=15000` |
106 | 106 | |
| 107 | +## Exec shell snapshots |
| 108 | + |
| 109 | +`OPENCLAW_EXEC_SHELL_SNAPSHOT=1` is an opt-in gateway exec optimization for bash and zsh. Set it in the Gateway |
| 110 | +process environment; per-call `exec.env` values cannot enable it or redirect its cache directory. When enabled, |
| 111 | +OpenClaw captures sourceable aliases/functions and a small safe environment set from shell startup files into |
| 112 | +`$OPENCLAW_STATE_DIR/cache/shell-snapshots/`, then sources that snapshot before each `exec` command. Secret-looking |
| 113 | +variables are excluded from the snapshot. Sandbox and node exec do not use it. |
| 114 | + |
107 | 115 | ## Runtime-injected env vars |
108 | 116 | |
109 | 117 | OpenClaw also injects context markers into spawned child processes: |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -80,6 +80,11 @@ Notes:
|
80 | 80 | from `PATH` to avoid fish-incompatible scripts, then falls back to `SHELL` if neither exists. |
81 | 81 | - On Windows hosts, exec prefers PowerShell 7 (`pwsh`) discovery (Program Files, ProgramW6432, then PATH), |
82 | 82 | then falls back to Windows PowerShell 5.1. |
| 83 | +- On non-Windows gateway hosts, setting `OPENCLAW_EXEC_SHELL_SNAPSHOT=1` in the Gateway process environment |
| 84 | + enables an opt-in startup snapshot for bash and zsh. OpenClaw captures sourceable aliases/functions and a small |
| 85 | + safe environment set from shell startup files into `$OPENCLAW_STATE_DIR/cache/shell-snapshots/`, then sources |
| 86 | + that snapshot before each exec command. Secret-looking variables are excluded; sandbox and node exec do not use |
| 87 | + this snapshot. |
83 | 88 | - Host execution (`gateway`/`node`) rejects `env.PATH` and loader overrides (`LD_*`/`DYLD_*`) to |
84 | 89 | prevent binary hijacking or injected code. |
85 | 90 | - OpenClaw sets `OPENCLAW_SHELL=exec` in the spawned command environment (including PTY and sandbox execution) so shell/profile rules can detect exec-tool context. |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,7 @@ import {
|
53 | 53 | readEnvInt, |
54 | 54 | } from "./bash-tools.shared.js"; |
55 | 55 | import { buildCursorPositionResponse, stripDsrRequests } from "./pty-dsr.js"; |
| 56 | +import { maybeWrapCommandWithShellSnapshot } from "./shell-snapshot.js"; |
56 | 57 | import { getShellConfig, sanitizeBinaryOutput } from "./shell-utils.js"; |
57 | 58 | |
58 | 59 | export { execSchema } from "./bash-tools.schemas.js"; |
@@ -822,12 +823,20 @@ export async function runExecProcess(opts: {
|
822 | 823 | shellRuntimeEnv, |
823 | 824 | opts.pathPrepend, |
824 | 825 | ); |
| 826 | +const commandWithShellSnapshot = await maybeWrapCommandWithShellSnapshot({ |
| 827 | +command: commandWithPathPrepend, |
| 828 | + shell, |
| 829 | + shellArgs, |
| 830 | +cwd: opts.workdir, |
| 831 | +env: shellRuntimeEnv, |
| 832 | +enabled: true, |
| 833 | +}); |
825 | 834 | |
826 | | -const childArgv = [shell, ...shellArgs, commandWithPathPrepend]; |
| 835 | +const childArgv = [shell, ...shellArgs, commandWithShellSnapshot]; |
827 | 836 | if (opts.usePty) { |
828 | 837 | return { |
829 | 838 | mode: "pty" as const, |
830 | | -ptyCommand: commandWithPathPrepend, |
| 839 | +ptyCommand: commandWithShellSnapshot, |
831 | 840 | childFallbackArgv: childArgv, |
832 | 841 | env: shellRuntimeEnv, |
833 | 842 | stdinMode: "pipe-open" as const, |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -7,8 +7,8 @@
|
7 | 7 | */ |
8 | 8 | |
9 | 9 | import type { WriteStream } from "node:fs"; |
| 10 | +import { sanitizeBinaryOutput } from "../shell-utils.js"; |
10 | 11 | import { stripAnsi } from "../utils/ansi.js"; |
11 | | -import { sanitizeBinaryOutput } from "../utils/shell.js"; |
12 | 12 | import type { BashOperations } from "./tools/bash-operations.js"; |
13 | 13 | import { createPrivateTempWriteStream } from "./tools/private-temp-file.js"; |
14 | 14 | import { DEFAULT_MAX_BYTES, truncateTail } from "./tools/truncate.js"; |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4,7 +4,7 @@
|
4 | 4 | */ |
5 | 5 | |
6 | 6 | import { execSync, spawnSync } from "node:child_process"; |
7 | | -import { getShellConfig } from "../utils/shell.js"; |
| 7 | +import { getBashShellConfig } from "../shell-utils.js"; |
8 | 8 | |
9 | 9 | // Cache for shell command results (persists for process lifetime) |
10 | 10 | const commandResultCache = new Map<string, string | undefined>(); |
@@ -27,7 +27,7 @@ function executeWithConfiguredShell(command: string): {
|
27 | 27 | value: string | undefined; |
28 | 28 | } { |
29 | 29 | try { |
30 | | -const { shell, args } = getShellConfig(); |
| 30 | +const { shell, args } = getBashShellConfig(); |
31 | 31 | const result = spawnSync(shell, [...args, command], { |
32 | 32 | encoding: "utf-8", |
33 | 33 | timeout: 10000, |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -7,8 +7,8 @@ import { keyHint } from "../../modes/interactive/components/keybinding-hints.js"
|
7 | 7 | import { truncateToVisualLines } from "../../modes/interactive/components/visual-truncate.js"; |
8 | 8 | import { theme } from "../../modes/interactive/theme/theme.js"; |
9 | 9 | import type { AgentTool } from "../../runtime/index.js"; |
| 10 | +import { getBashShellConfig, getShellEnv, killProcessTree } from "../../shell-utils.js"; |
10 | 11 | import { waitForChildProcess } from "../../utils/child-process.js"; |
11 | | -import { getShellConfig, getShellEnv, killProcessTree } from "../../utils/shell.js"; |
12 | 12 | import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js"; |
13 | 13 | import type { BashOperations } from "./bash-operations.js"; |
14 | 14 | import { OutputAccumulator } from "./output-accumulator.js"; |
@@ -48,7 +48,7 @@ export function createLocalBashOperations(options?: { shellPath?: string }): Bas
|
48 | 48 | return { |
49 | 49 | exec: (command, cwd, { onData, signal, timeout, env }) => { |
50 | 50 | return new Promise((resolve, reject) => { |
51 | | -const { shell, args } = getShellConfig(options?.shellPath); |
| 51 | +const { shell, args } = getBashShellConfig(options?.shellPath); |
52 | 52 | if (!existsSync(cwd)) { |
53 | 53 | reject( |
54 | 54 | new Error(`Working directory does not exist: ${cwd}\nCannot execute bash commands.`), |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2,8 +2,8 @@ import * as os from "node:os";
|
2 | 2 | import { getCapabilities, getImageDimensions, imageFallback } from "@earendil-works/pi-tui"; |
3 | 3 | import type { ImageContent, TextContent } from "../../../llm/types.js"; |
4 | 4 | import type { Theme } from "../../modes/interactive/theme/theme.js"; |
| 5 | +import { sanitizeBinaryOutput } from "../../shell-utils.js"; |
5 | 6 | import { stripAnsi } from "../../utils/ansi.js"; |
6 | | -import { sanitizeBinaryOutput } from "../../utils/shell.js"; |
7 | 7 | |
8 | 8 | export function shortenPath(path: unknown): string { |
9 | 9 | if (typeof path !== "string") { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。