Skip to content

Latest commit

 

History

History
246 lines (193 loc) · 9.82 KB

SHELL_PROFILES.md

File metadata and controls

246 lines (193 loc) · 9.82 KB

Shell Profiles (Prepared Environment Snapshots)

English | 简体中文

By default, run_shell and run_job do not keep a persistent shell session. They prepare an environment snapshot once per project/profile and then run each command as an independent process with that snapshot. The explicit Workflow Session persistent-shell tools described in section 9 are the only long-lived exception. This page describes how both paths apply shell profiles and their safety boundaries.

Applies to webcodex-runner (the host agent that executes shell commands). The server never reads or stores shell env values, init_script bodies, or tokens.


1. What is a prepared shell env snapshot

When a command runs for a project, the agent:

  1. Resolves the effective profile (project.shell_profile, else shell.default_profile, else plain shell config).
  2. Prepares a one-time environment snapshot for that project/cwd + profile pair: it starts the profile program with env_clear, applies the profile env, runs the profile init_script (if any), and captures the resulting environment via env -0 behind a unique marker.
  3. Caches the snapshot keyed by project/cwd + profile name.
  4. Runs every subsequent command for that pair as a fresh process with the cached snapshot applied — there is no long-lived shell, no source per command, and no .bashrc/.profile loaded by default.

Because the snapshot is keyed by project path + profile name, the same profile can be reused by multiple projects, and a project-relative init_script (e.g. source .venv/bin/activate) is resolved from each project's own root.

2. Why .bashrc / .profile are not sourced by default

WebCodex intentionally does not source ~/.bashrc or ~/.profile when preparing a snapshot:

  • .bashrc can be slow (network calls, prompts, completions).
  • It may contain interactive-only commands (stty, echo, prompts) that break non-interactive capture or hang the prepare step.
  • It can leak or pollute the environment (secrets, aliases, functions).
  • It is non-reproducible across hosts and users.

Use an explicit shell profile instead. Explicit profiles are auditable, fast, and do not depend on the interactive shell setup of the agent's user.

3. Rust / Cargo example

[shell]
default_profile = "rust"

[shell.profiles.rust]
program = "sh"
args = ["-c"]

[shell.profiles.rust.env]
PATH = "/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
CARGO_HOME = "/root/.cargo"
RUSTUP_HOME = "/root/.rustup"

This profile needs no init_script: the env block already sets PATH, CARGO_HOME, and RUSTUP_HOME, so cargo / rustc are directly available.

4. Python venv example

[shell.profiles.py-venv]
program = "bash"
args = ["-lc"]
init_script = '''
source .venv/bin/activate
'''

The init_script is project-relative: .venv/bin/activate is resolved from the project root, so each project activates its own venv. The snapshot is captured once per project/profile pair.

5. Conda example

[shell.profiles.conda-ml]
program = "bash"
args = ["-lc"]
init_script = '''
source /opt/miniconda3/etc/profile.d/conda.sh
conda activate ml
'''

6. Binding a project to a profile

A project TOML (projects.d/<id>.toml) can pin a profile:

id = "paper-exp"
path = "/root/git/paper-exp"
shell_profile = "conda-ml"

7. Resolution rules

The effective profile for a command is chosen as:

  1. project.shell_profile (if set), else
  2. shell.default_profile (if set), else
  3. fallback to the plain shell config (no prepared snapshot).

Notes:

  • The snapshot cache key is project/cwd + profile name.
  • The same profile can be reused by multiple projects.
  • .venv (and any project-relative init_script) is prepared in the project root.
  • listProjects exposes shell_profile (the project's setting), resolved_shell_profile (the actually-used name), and shell_profile_status (configured / missing / not_configured / unknown).

8. Dialect reporting

Runner registration reports shell dialect facts so the server and agents never have to guess the remote shell:

  • The shell profiles summary carries default_dialect (sh | bash | custom) — the dialect of the runner's default shell program — and available_dialects, which always contains sh and bash, plus custom when custom profiles exist.
  • Each profile entry carries its own dialect.
  • A custom profile whose program does not map to sh or bash reports custom. Agents that need deterministic command syntax must select an explicit shell instead of relying on the profile dialect.
  • An explicit shell=sh|bash on run_shell / run_job always overrides the default; the server never guesses the remote shell.
  • Dialect reporting is metadata only: no PATH, env values, or init_script contents are ever sent to the server.

9. Workflow Session execution defaults

On the full operator runtime, a registered-project-bound Workflow Session can store an optional project-relative default_cwd and an optional default_shell (sh or bash). These are execution defaults, not a prepared environment snapshot or implicit process reuse.

For run_shell, run_job, and open_session_shell, resolution is:

  1. explicit per-call cwd / shell;
  2. the exact active project-matched Workflow Session defaults;
  3. the existing project root / configured shell-profile behavior.

Omitting a Session keeps the existing behavior. A Session from another project never contributes defaults. Invalid, missing, or outside-root directories fail through the existing cwd safety checks without silently retrying at the project root. run_shell and run_job remain independent processes; a command such as cd sub does not affect later one-shot calls.

start_coding_task(execution_context=...) can set or replace the context. Omission during continuation preserves it, while {} clears it. update_session_context performs a full replacement for an explicit active Workflow Session only after its required project is authorized and resolves to the exact Session project; cross-project escape is rejected. The context and event commit together in memory, while the JSON ledger is queued to the background writer, so success is not a synchronous durability guarantee. The context cannot store env values, tokens, arbitrary options, or shell state.

open_session_shell is the explicit exception: it creates one real long-lived sh/bash process for that Workflow Session. The selected profile environment and initialization execute once at open, so later session_shell_exec calls retain cwd, exports, functions, umask, and shell variables. Profile args remain the one-shot command invocation contract and are not reused for the long-lived process; persistent bash starts with --noprofile --norc, while persistent sh starts without command-mode arguments. Updating Session context does not alter an already-open process; close and reopen it to apply a changed cwd, dialect, or profile default. Persistent shells are rejected for read_only/inspect Sessions and for a Session with an SSH resource.

10. Changing config

After editing agent.toml, reload the Runner service. A valid hot reload moves new one-shot commands to a new profile-cache generation, so their snapshots are prepared lazily again. Project TOML changes are refreshed independently.

An already-open persistent shell is never silently restarted or rewritten by a reload. Current policy and project/profile selection are rechecked before later exec/status operations, but the environment and initialization already inside that process remain unchanged. Explicitly close and reopen it to apply changed profile contents, cwd, or dialect defaults. Configuration fields outside the documented hot-reload boundary still require a Runner restart; see DEPLOYMENT.md.

11. Security notes

  • Never put tokens in init_script.
  • Never echo/printf secrets in init_script — anything the script writes to stdout is parsed as part of the env snapshot capture.
  • Status / runtime_status / listAgents / listProjects expose only sanitized metadata: profile name, has_init_script (boolean), env_keys_count (count), program, args_count, per-profile dialect, and the summary default_dialect / available_dialects. They never expose init_script bodies, env values, tokens, the Authorization header, the full agent.toml, or the full env snapshot.
  • A prepare failure may surface a safe error summary (e.g. "failed to prepare shell profile 'x' at "), but never the init_script body or stderr tail.
  • The WebCodex agent token (WEBCODEX_TOKEN / WEBCODEX_AGENT_TOKEN / WEBCODEX_USER_TOKEN / AUTHORIZATION) is stripped from the child process environment and is never passed to commands.
  • prepare runs with env_clear + an explicit allowlist of inherited keys; profiles must declare the env they need.

12. Troubleshooting

For a canonical project, run the shared read-only readiness checks:

webcodex doctor

For an advanced enrolled profile, combine Agent status with operator status:

webcodex agent status \
  --profile workstation \
  --server-url https://example.test
webcodex ops status \
  --server-url https://example.test \
  --token-file ~/.config/webcodex/user.token \
  --strict

Profile preparation failures remain sanitized in Agent status/logs: neither init_script bodies nor environment values are exposed. Fleet/project roundtrips belong to explicit operator smoke checks, not ordinary onboarding.

See also: