Skip to content

feat: Replace terminal dangerous-command block with HITL approval and add Safe Mode in Settings#1310

Open
spider-yamet wants to merge 47 commits intoeigent-ai:mainfrom
spider-yamet:feat/replace-strict-command-prohibition
Open

feat: Replace terminal dangerous-command block with HITL approval and add Safe Mode in Settings#1310
spider-yamet wants to merge 47 commits intoeigent-ai:mainfrom
spider-yamet:feat/replace-strict-command-prohibition

Conversation

@spider-yamet
Copy link
Contributor

Related issue

Closes #1306

Summary

Replaces the Terminal Toolkit’s hard block of “dangerous” commands with a Human-in-the-Loop (HITL) approval flow and adds a Safe Mode setting so users can opt in.

Changes

HITL for dangerous commands

  • When a dangerous command is detected and Safe Mode is on, the user is offered three choices:
    • Yes – approve this command once
    • All Yes in this task – approve all subsequent dangerous commands in the current task
    • No – reject the command
  • Frontend shows the prompt and calls /chat/{id}/terminal-approval with the chosen option; backend enforces approval before running the command and supports “approve all in task” per task.

Safe Mode in Settings

  • Settings → Permissions: new Safe Mode toggle with hint:
    “With Safe Mode active, Eigent will pause and seek explicit approval whenever high-risk system operations are detected.”
  • Default: off. When on, the HITL flow above is used for the configured dangerous-command list.

Backend

  • Dangerous command list (triggers HITL when Safe Mode is on): system (e.g. sudo, su, reboot, shutdown), file (e.g. rm, chown, mount), disk (e.g. dd, mkfs, fdisk), process (e.g. service, systemctl), network (e.g. iptables, ifconfig), cron (e.g. crontab, at), user/kernel (e.g. useradd, modprobe), and related commands as specified.
  • Non-Docker mode: cd is validated so the agent cannot leave the designated working_directory.
  • Task lock includes approved_all_dangerous_commands and a queue for terminal approval; reset on new task.

Frontend

  • Permissions tab and Safe Mode UI; preference stored in localStorage and sent as safe_mode in the start-task request.

Other

  • Pre-commit: skip backend checks when uv is not installed so commits succeed without uv (message suggests installing uv for backend checks).

Testing

  • Verified Safe Mode off: dangerous commands run without approval.
  • Verified Safe Mode on: dangerous commands trigger the three-option prompt; Yes / All Yes in task / No behave as described.
  • Verified Permissions UI: toggle and hint render; state persists and is sent to backend.
  • Verified cd outside working_directory in non-Docker mode is rejected.

@spider-yamet
Copy link
Contributor Author

@bytecii @Wendong-Fan Could you please review my PR?

Copy link
Collaborator

@bytecii bytecii left a comment

Choose a reason for hiding this comment

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

IMO we can move this to camel to make a special safe mode such as strict. cc @Wendong-Fan

@Wendong-Fan
Copy link
Contributor

IMO we can move this to camel to make a special safe mode such as strict. cc @Wendong-Fan

thanks @bytecii , the idea behind this issue is to implement a human-in-the-loop mechanism for safer code execution, which would involve the HumanToolkit. Since CAMEL serves more as a modular framework, it might be more appropriate to implement this feature in Eigent as an application layer. What do you think?

@Wendong-Fan Wendong-Fan requested review from 4pmtong and fengju0213 and removed request for 4pmtong February 21, 2026 15:37
@Wendong-Fan Wendong-Fan added this to the Sprint 16 milestone Feb 21, 2026
@spider-yamet
Copy link
Contributor Author

spider-yamet commented Feb 22, 2026

@Wendong-Fan @bytecii could you please review this pr?
I see this PR remains opening for several days.

Regards

bytecii and others added 8 commits February 23, 2026 00:09
- Move HitlOptions and ApprovalRequest models from chat.py to app/hitl/__init__.py
- Remove unnecessary hasattr guard on auto_approve (always initialized)
- Remove issue reference comment and docstring comments
- Update all imports across controllers, services, and tests
- Move _request_user_approval from AbstractToolkit to TerminalToolkit
  (only terminal uses approval today)
- Remove debug print statements from shell_exec
- Move import time to top-level in terminal_toolkit
- Fix approval endpoint docstring
- Remove module docstring from terminal_command.py
- Add comment for effective executable check
- Update test to subclass TerminalToolkit directly
options.project_id,
Agents.browser_agent,
working_directory=working_directory,
safe_mode=True,
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove this as this is not configured. And by default if the HITL not specified, we still use the safe_mode=True for the terminaltoolkit

return None
return "Operation rejected by user. The task is being stopped."

async def shell_exec(
Copy link
Collaborator

Choose a reason for hiding this comment

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

changed to async

- Remove unused logger from abstract_toolkit
- Move _thread_local from AbstractToolkit to TerminalToolkit
- Add missing threading.local() definition
- Add Args/Returns to terminal_command.py functions
- Use ApprovalAction.reject enum instead of raw string
- Fix ApprovalAction and ActionCommandApprovalData docstrings
- Add comment for auto_approve reset at task start
Copy link
Collaborator

@fengju0213 fengju0213 left a comment

Choose a reason for hiding this comment

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

thanks !@spider-yamet and @bytecii left some comments below and pushed a commit
overall,looks good

)
return future

def resolve_approval(self, approval_id: str, action: str):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Approval callbacks may execute outside the Future owner loop/thread; the current restore paths here have a cross-thread wake-up risk, which can lead to “API succeeds but command execution does not continue”.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We fixed the cross-thread wake-up risk by changing how approval resolution works:

  1. No resolution from the API thread – resolve_approval (and the other resolve entrypoints) no longer call future.set_result(...) directly. They only call _resolve_future_threadsafe.
  2. Resolution on the Future’s loop – _resolve_future_threadsafe gets the event loop that owns the Future via future.get_loop(), then schedules a callback on that loop with loop.call_soon_threadsafe(self._set_future_result_if_pending, future, action). The callback runs on the loop where the command is awaiting approval and only then calls future.set_result(action), so the wake-up always happens on the correct loop.
  3. Defensive checks and logging – We check future.done() and loop.is_closed(), handle RuntimeError from get_loop() and from call_soon_threadsafe (e.g. if the loop closes between the check and scheduling), and log warnings so “API succeeds but command execution does not continue” is visible if something goes wrong.

Comment on lines +243 to +258
# cd with no args or "cd ~" -> home; treat as potential escape
if not target or target == "~":
target = os.path.expanduser("~")
elif target == "-":
# "cd -" is previous dir; cannot validate statically, allow it
continue
try:
work_real = os.path.realpath(os.path.abspath(working_directory))
if os.path.isabs(target):
resolved = os.path.realpath(os.path.abspath(target))
else:
resolved = os.path.realpath(
os.path.abspath(os.path.join(work_real, target))
)
if os.path.commonpath([resolved, work_real]) != work_real:
return (
Copy link
Collaborator

Choose a reason for hiding this comment

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

If relative cd in compound commands is not resolved against step-by-step directory state, valid sequences (e.g., cd sub && cd ..) can be incorrectly flagged as escaping the workspace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We’ve addressed this by validating compound cd commands against a step-by-step directory state:

  1. State per sub-command – We iterate over each sub-command from split_compound_command and keep a simulated current_dir and previous_dir (both initially set to the canonical workspace path).
  2. Relative cd uses current dir – For each cd sub-command we resolve the target: if it’s relative we resolve it with os.path.join(current_dir, target) and then os.path.realpath(os.path.abspath(...)), so cd sub and cd .. are evaluated in the context of the directory left by the previous step.
  3. cd - – We treat cd - as “go to previous directory” by setting the target to previous_dir in the simulated state.
  4. State updates – After a valid cd we set previous_dir = current_dir and current_dir = resolved before the next sub-command, so a sequence like cd sub && cd .. is validated step-by-step and correctly stays inside the workspace instead of being flagged as escaping.

@spider-yamet
Copy link
Contributor Author

@fengju0213 @Wendong-Fan @bytecii I updated all the details by following the feedback, ready for review. :)

@spider-yamet
Copy link
Contributor Author

spider-yamet commented Mar 6, 2026

could you please check @fengju0213 ? Hope we can complete this task ASAP...

@spider-yamet
Copy link
Contributor Author

Could you let me know your plan, @fengju0213 ?

@spider-yamet
Copy link
Contributor Author

Still waiting for review, @Wendong-Fan @bytecii.. Regards

@spider-yamet
Copy link
Contributor Author

@fengju0213 Could you please review current PR? or hope you can let me know about any timeline for this, remaining for review status for several weeks.. Regards.

@fengju0213
Copy link
Collaborator

@fengju0213 Could you please review current PR? or hope you can let me know about any timeline for this, remaining for review status for several weeks.. Regards.

Sorry for the delay. Our team has been extremely busy over the past few weeks. On the code side, we don't have any major issues at the moment. However, since this involves UI and user experience, we need our product team to review the UI part. I will ask them to provide feedback as soon as possible.

Once again, apologies for the delay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Replace strict command prohibition in Terminal Toolkit with structured Human-in-the-Loop verification

4 participants