Skip to content

Shell state persistence - #900

Closed
W-OK-E wants to merge 0 commit into
SREGym:mainfrom
W-OK-E:shell-state-persistence
Closed

W-OK-E wants to merge 0 commit into
SREGym:mainfrom
W-OK-E:shell-state-persistence

Conversation

@W-OK-E

@W-OK-E W-OK-E commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

This PR emerges from the discussions in #883 and #898 to enable shell-state persistence across SREGym> cli commands used for debugging.

What Changed

Shell.exec now persists exported variables, shell functions, and the working directory across successive SREGym> CLI commands, so a sequence of commands behaves like one interactive shell session:

export REGISTRY_IP=$(kubectl get svc ... -o jsonpath='{.spec.clusterIP}')
echo "$REGISTRY_IP"          # <- still set on the next command

Previously each command ran in a fresh bash with no memory of the last one, so export, functions, and cd were lost between commands.

How it works

After each command we snapshot the exported environment (declare -px), function (declare -pf), and $PWD to a per-process state file (one per CLI PID, the temp dir), and source it before the next command. Each command still runs in
its own short-lived bash process; the state file is what carries context forward.

Scope

Dev/debug CLI only — cli.py is unchanged; it simply calls Shell.exec. No behavior change for main.py / agent runs. The change lives entirely in `shell.py

One simple thing to point out - The changes only take place AFTER the debug shell has entered into a problem session i.e. after start <problem-id has executed since cli.py holds the session in a while loop checking for the start and exit commands.

Let me know if this seems fine or if any changes are required.

Also, apologies for the delay in #883 @Saadmrp1038, I had hooked up a RAID HDD to run the local evaluations and it failed so kind of working things out there, but it will be done soon too.

@munimthahmid munimthahmid left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@W-OK-E I tested this through the SREGym CLI, and the intended persistence works well for exported variables, shell functions, and the working directory. I found two issues and left inline comments about them. Could you please address them? Otherwise, the feature looks good.

Comment thread sregym/service/shell.py Resolved
Comment thread sregym/service/shell.py Outdated
Comment on lines +22 to +28
# If a cwd is explicitly requested, it wins over the persisted directory.
start_dir = shlex.quote(cwd) if cwd else "."
# Restore prior state -> run the user's command -> snapshot exported vars, functions,
# and cwd for the next call. The user command's own exit code is preserved via exit.
wrapped = (
f"[ -f {state} ] && source {state} 2>/dev/null; "
f"cd {start_dir} 2>/dev/null; "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue will not block the current SREGym CLI, so it is minor, but it should still be fixed.

For example, after Shell.exec("cd /tmp"), calling Shell.exec("pwd", cwd="tests") should run in <repo>/tests, but it returns /tmp. The persisted /tmp state is restored before the relative cwd is applied, so tests is resolved as /tmp/tests. Since that directory does not exist and the cd error is suppressed, the command remains in /tmp.

This also contradicts the comment above, which says that an explicitly requested cwd wins over the persisted directory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patched - resolves to absolute cwd not relative.

@W-OK-E

W-OK-E commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Hello @munimthahmid !

Thank you for reviewing the PR and the suggestions, I have made a few changes w.r.t the points raised. Kindly review them and let me know if it seems fine.

@munimthahmid munimthahmid left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@W-OK-E The persistence feature works well, and the relative cwd issue is fixed. I left two comments about the unused state-file helper and changes that belong in #883

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you mistakenly added the problem from #883 here. Can you remove it from this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, removed.

Comment thread sregym/service/shell.py Resolved
@W-OK-E

W-OK-E commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

Sincere apologies @munimthahmid again! Will be careful handling multiple work-trees hereon.

@munimthahmid munimthahmid left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@W-OK-E Sorry for the confusion. My earlier comment was attached to the oracle file, but I meant all changes from #883. The problem implementation and registry changes are still included here. Could you remove those as well?

@W-OK-E W-OK-E closed this Jun 22, 2026
@W-OK-E
W-OK-E force-pushed the shell-state-persistence branch from 68e7a93 to 93ab29b Compare June 22, 2026 19:05
@W-OK-E

W-OK-E commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

I should be apologizing @munimthahmid. Have closed this since the current worktree is a mess. Primarily due to the hacks I was doing to test #883 on my NAS. I will create a new PR. Apologies for the inconvenience.

@W-OK-E
W-OK-E deleted the shell-state-persistence branch June 22, 2026 19:10
@W-OK-E W-OK-E mentioned this pull request Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants