openclaw - 💡(How to fix) Fix Feature Request: Persistent Shell State [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#56909Fetched 2026-04-08 01:46:12
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

Error Message

Multi-step terminal workflows require cramming everything into single commands or using absolute paths everywhere. This is clunky and error-prone.

Code Example

Session Start → spawn persistent shell (bash --norc or with user's rc)
exec("cd /project && export FOO=bar") → routes to persistent shell
exec("echo $FOO && pwd") → outputs "bar" and "/project"
Session End → terminate shell process, cleanup
RAW_BUFFERClick to expand / collapse

Feature Request: Persistent Shell State

Problem Statement

Each exec tool call in OpenClaw spawns a fresh shell process. This means:

  • cd /project/dir in one call has zero effect on the next call.
  • Environment variables set with export are lost immediately.
  • Virtual environment activations (source venv/bin/activate) don't persist.
  • Aliases and shell functions vanish between calls.

Multi-step terminal workflows require cramming everything into single commands or using absolute paths everywhere. This is clunky and error-prone.

Proposed Solution

Long-Lived Shell Process

  • Spawn a persistent bash/zsh process at session start.
  • All exec calls route commands through this process.
  • Working directory, environment variables, aliases, and shell state persist across calls.

Lifecycle Management

  • Shell lifetime = session lifetime. New session = fresh shell.
  • If the persistent shell dies (crash, OOM), detect and respawn transparently.
  • User can reset shell state manually (/shell reset or similar).

Implementation Sketch

Session Start → spawn persistent shell (bash --norc or with user's rc)
exec("cd /project && export FOO=bar") → routes to persistent shell
exec("echo $FOO && pwd") → outputs "bar" and "/project"
Session End → terminate shell process, cleanup

Backward Compatibility

  • exec with explicit workdir parameter still overrides the persistent shell's cwd for that call.
  • One-shot mode available via flag for commands that should run in isolation.

User Impact

  • Developers: "cd into the project, activate venv, run tests, check output" becomes a natural multi-step flow instead of a single mega-command.
  • Sysadmins: SSH sessions, remote debugging, and multi-step server operations work naturally.
  • All users: Reduces the cognitive overhead of working with the terminal through an agent.

Hermes Agent's persistent shell is one of its most-praised developer features. Users report it makes the agent feel like a real terminal collaborator rather than a command executor.

Technical Considerations

  • Output capture: Reading stdout/stderr from a long-lived process requires careful stream handling (vs. simple subprocess.run).
  • PTY support: Interactive commands (vim, top, ssh) still need PTY mode; persistent shell should support both PTY and non-PTY.
  • Security: Persistent shells accumulate secrets in environment. Session cleanup must be thorough.
  • Concurrency: If multiple tool calls fire simultaneously, they need to be serialized through the shell or use separate shells.
  • Timeouts: A hung command in the persistent shell shouldn't block all subsequent commands; need timeout + kill support.

Priority

HIGH. Fundamental quality-of-life improvement for the developer/sysadmin use case, which is a large share of OpenClaw's user base.

extent analysis

Fix Plan

To implement a persistent shell state in OpenClaw, follow these steps:

  • Spawn a persistent bash/zsh process at session start using bash --norc or with the user's rc file.
  • Route all exec calls through this process.
  • Implement lifecycle management:
    • Shell lifetime equals session lifetime. New session = fresh shell.
    • Detect and respawn the shell transparently if it dies (crash, OOM).
    • Allow users to reset shell state manually (/shell reset or similar).

Example code snippet in Python to spawn a persistent shell:

import subprocess
import threading

class PersistentShell:
    def __init__(self):
        self.shell_process = subprocess.Popen(['bash', '--norc'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    def execute(self, command):
        self.shell_process.stdin.write(command.encode() + b'\n')
        self.shell_process.stdin.flush()
        output = self.shell_process.stdout.readline().decode()
        return output

# Create a persistent shell instance
persistent_shell = PersistentShell()

# Execute commands through the persistent shell
print(persistent_shell.execute('cd /project && export FOO=bar'))
print(persistent_shell.execute('echo $FOO && pwd'))

Verification

To verify that the fix worked:

  • Test multi-step terminal workflows without cramming everything into single commands.
  • Verify that environment variables set with export persist across calls.
  • Check that virtual environment activations (source venv/bin/activate) persist.
  • Test aliases and shell functions to ensure they don't vanish between calls.

Extra Tips

  • Handle output capture carefully when reading stdout/stderr from a long-lived process.
  • Support both PTY and non-PTY modes for interactive commands (vim, top, ssh).
  • Ensure thorough session cleanup to prevent secrets accumulation in environment variables.
  • Implement concurrency control to serialize multiple tool calls through the shell or use separate shells.
  • Add timeout and kill support to prevent hung commands from blocking subsequent commands.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix Feature Request: Persistent Shell State [1 participants]