Skip to content

Fix execute_bash tool to inherit environment variables from parent process - #245

Merged
xingyaoww merged 5 commits into
mainfrom
openhands/fix-execute-bash-env-inheritance
Sep 14, 2025
Merged

xingyaoww merged 5 commits into
mainfrom
openhands/fix-execute-bash-env-inheritance

Conversation

@xingyaoww

Copy link
Copy Markdown
Contributor

Summary

Fixes #243 - execute_bash tool should be able to inherit current env vars

Problem

Previously, when using the execute_bash tool, environment variables set in the parent process (e.g., export A=A) were not available in the new terminal session. This was because:

  1. TmuxTerminal: The new_session() and new_window() calls didn't pass the parent environment variables
  2. SubprocessTerminal: Already correctly inherited environment variables

Solution

Changes Made

  1. Modified TmuxTerminal (openhands/tools/execute_bash/terminal/tmux_terminal.py):

    • Added import os to access environment variables
    • Copy parent environment variables using env = os.environ.copy()
    • Pass environment=env parameter to both new_session() and new_window() calls
  2. Added comprehensive test (tests/tools/execute_bash/test_bash_session.py):

    • New test test_environment_variable_inheritance_from_parent()
    • Tests both TmuxTerminal and SubprocessTerminal
    • Verifies that environment variables set in the parent process are accessible in the terminal session
    • Includes proper cleanup to avoid test pollution

Technical Details

The fix leverages the existing environment parameter support in the libtmux library:

  • libtmux.Server.new_session(environment=env)
  • libtmux.Session.new_window(environment=env)

SubprocessTerminal already correctly inherited environment variables via subprocess.Popen(env=env) where env = os.environ.copy().

Testing

  • ✅ New test passes for both terminal types
  • ✅ Existing environment variable persistence tests still pass
  • ✅ All execute_bash tests pass
  • ✅ Pre-commit hooks pass (formatting, linting, type checking)

Verification

You can now:

  1. Set an environment variable: export TEST_VAR=hello
  2. Use execute_bash tool: echo $TEST_VAR
  3. The variable will be correctly inherited and displayed

This ensures that the execute_bash tool maintains the expected behavior of inheriting the parent process environment, making it more intuitive and consistent with standard shell behavior.

@xingyaoww can click here to continue refining the PR

…ocess

- Modified TmuxTerminal to pass environment variables to new_session() and new_window()
- Added comprehensive test to verify environment variable inheritance from parent process
- SubprocessTerminal already correctly inherited environment variables

Fixes #243

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Sep 13, 2025 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report
FileStmtsMissCoverMissing
openhands
   __init__.py10100% 
openhands/sdk
   __init__.py15286%23–24
   logger.py732171%33, 57, 64–67, 69–71, 124, 130–132, 135–136, 142–144, 151, 156–157
openhands/sdk/agent
   __init__.py30100% 
   agent.py1865769%56, 60, 67, 74, 81, 85, 96–97, 99–100, 102, 105, 116, 121–124, 127–130, 156–158, 203, 207–208, 215–216, 218–220, 222–224, 259, 273, 296, 301, 328, 333, 338–339, 362–364, 368–370, 377–378, 382, 386–387, 417, 424, 440
   base.py491079%46, 67, 87, 94–95, 101–102, 104, 107, 111
openhands/sdk/context
   __init__.py40100% 
   agent_context.py574029%57–58, 60–65, 76, 79, 84, 86, 92–95, 108–110, 112, 115, 117–120, 122–127, 132, 139–140, 145–147, 151–153
   manager.py330%1, 4–5
   view.py483429%31, 47–51, 53, 60–63, 65–67, 69, 77–78, 82–87, 89–90, 92, 106–112, 114
openhands/sdk/context/condenser
   __init__.py30100% 
   condenser.py17382%68–69, 73
   no_op_condenser.py6183%13
openhands/sdk/context/microagents
   __init__.py40100% 
   exceptions.py5180%11
   microagent.py14310030%48, 51–52, 59, 72, 75–76, 78, 82, 85–87, 90–91, 99–101, 103–105, 108, 111, 114–119, 128–131, 133–136, 143–144, 152, 154–155, 177, 184–187, 189, 215–221, 224, 228–229, 232, 251–252, 257–258, 260, 264, 271–273, 281–283, 299–300, 302–303, 306, 309–310, 313–317, 320–322, 325–330, 332–333, 335–337, 339–340, 342, 346
   types.py210100% 
openhands/sdk/context/prompts
   __init__.py20100% 
   prompt.py30583%12, 15, 24, 44–45
openhands/sdk/conversation
   __init__.py70100% 
   conversation.py1123965%85, 96, 105, 113, 121–123, 127–128, 173, 180, 184, 188–190, 198, 200, 202, 204–206, 208, 210, 216–217, 230–231, 233–237, 248–250, 260–261, 269–270
   event_store.py1014654%30–33, 37–41, 50–51, 55, 57, 60, 67, 72–73, 80–81, 104–107, 111–116, 118, 125–129, 133–134, 136–142, 148–149
   persistence_const.py50100% 
   secrets_manager.py412831%42, 53–57, 68, 70–71, 73, 75–79, 84, 86–89, 91–92, 106–107, 109, 112–113, 115
   serialization_diff.py00100% 
   state.py941584%84, 122, 125–126, 132, 135–138, 140, 145, 149, 185–187
   types.py30100% 
   visualizer.py911385%88, 106, 145, 166–167, 175–176, 185, 210, 212, 214, 217, 236
openhands/sdk/event
   __init__.py50100% 
   base.py742960%44–47, 51, 55, 75, 79–81, 83–88, 90–91, 93–95, 97–98, 100, 127–128, 150–151, 155
   condenser.py25772%26–27, 29, 33–35, 49
   llm_convertible.py1793679%53, 63–64, 69–70, 119–121, 246, 250, 257, 260–262, 280–281, 286, 294, 318–323, 326, 335–336, 341, 364–367, 370, 374–375, 380
   types.py30100% 
   user_action.py12466%15–17, 21
   utils.py12191%34
openhands/sdk/io
   __init__.py40100% 
   base.py14471%7, 11, 15, 19
   local.py513825%16–19, 22–24, 27–31, 33–34, 37–39, 42–44, 47–48, 51–53, 56–68
   memory.py432248%16, 20, 31–38, 40–44, 48–54
openhands/sdk/llm
   __init__.py60100% 
   exceptions.py361363%5–6, 9, 16, 23, 32, 47, 55, 72, 84, 91, 101, 108
   llm.py38615360%206, 212, 216, 224, 229, 238, 242–244, 248–249, 260, 262, 266, 281, 308, 314, 321, 325, 340, 346–347, 366–367, 375, 384–386, 442, 451, 455–456, 461–462, 464–465, 468–470, 475–477, 481–483, 487–488, 504–507, 512–517, 521–522, 531–533, 536–537, 557, 561, 565–568, 614, 628, 663, 668–669, 672–674, 681–682, 691, 698, 701, 705–707, 711, 713–718, 720–737, 740–744, 746–747, 753–762, 766–777, 789–790, 796–802, 804, 806, 809, 813
   llm_registry.py382144%38–41, 49, 57–61, 73–74, 80–83, 99–100, 105, 108, 116
   message.py1102577%23, 37, 41, 58–63, 90, 97, 100, 118, 124, 127, 130, 149, 151–156, 223–224
   metadata.py15473%17–18, 31, 33
openhands/sdk/llm/mixins
   fn_call_converter.py34330511%73–75, 336–349, 351–352, 354, 363–364, 366–369, 371–372, 374–375, 377–378, 380–383, 385–386, 388–389, 391, 398, 400, 421–428, 430–434, 438–444, 446–451, 455–463, 465–468, 470, 472–474, 477, 480–482, 484, 488, 490–491, 500, 502–503, 507–511, 515–520, 522, 524, 529, 532, 534–535, 538, 541, 543–547, 553, 575, 580, 590–593, 599–602, 607–613, 615, 617, 621, 624, 626–627, 629–632, 637, 639, 643–644, 648, 654, 656, 659, 667, 669–671, 673–675, 677–679, 685–688, 691–692, 699–704, 707–711, 716, 719, 723, 727, 732–733, 736–738, 741, 746–748, 750–751, 767, 779–781, 785–786, 788–791, 793–794, 796–798, 800, 803, 805, 807–809, 811–812, 815–816, 819–821, 823–826, 829, 833, 840–841, 844–845, 859, 865–867, 870–871, 875–876, 882–883, 886, 898, 901–908, 912–913, 918–919, 924, 930–933, 943–944, 949, 955–956, 961–962, 969, 972–974, 977–978, 980, 986, 991, 994, 998, 1006, 1008–1009, 1012–1014, 1016–1017, 1023–1025, 1027–1028, 1030, 1032, 1036, 1038, 1043, 1045–1046, 1049
   non_native_fc.py392438%45–46, 49–51, 54–55, 63–64, 69, 72, 74–75, 80–82, 85, 87–91, 93–94
openhands/sdk/llm/utils
   metrics.py1114460%16–18, 116–118, 147–150, 199, 205, 208–209, 211–213, 216–219, 225, 240–244, 261, 264, 267–269, 273, 276, 281, 284–285, 287–288, 303–304, 306, 308, 311
   model_features.py40977%21–22, 24, 40–42, 45, 61–62
   retry_mixin.py503824%40, 42–43, 46–50, 53–59, 64, 85–86, 90, 92–95, 98–101, 104–105, 107–111, 114–116, 118
   telemetry.py1366055%67, 71, 82, 88, 94, 99–100, 112–113, 120, 129, 134, 153, 157–158, 166–168, 174, 191–193, 196–199, 201, 205–209, 212–223, 228–229, 232, 234, 237–238, 240–245, 249–252
openhands/sdk/mcp
   __init__.py50100% 
   client.py635020%20–23, 28–30, 32, 34–36, 38–41, 43–45, 48–51, 53–59, 77–80, 82, 86–88, 94–95, 101–107, 110–113
   definition.py281353%40–46, 52, 55, 64–67
   tool.py402245%24–25, 32–33, 36–39, 43, 46, 49–52, 60, 76–77, 85, 90, 101–102, 107
   utils.py301840%23–24, 27, 30, 35, 37–41, 44, 52–56, 58–59
openhands/sdk/tool
   __init__.py40100% 
   schema.py1354566%24–31, 37–39, 123–124, 126–127, 129–132, 135, 139–141, 143–144, 146, 153, 191–193, 196–197, 199, 244–247, 267, 276–280, 282–283
   security_prompt.py30100% 
   tool.py952771%58, 99, 131, 141, 143, 159–160, 163, 166–169, 172–178, 183, 188–194
openhands/sdk/tool/builtins
   __init__.py40100% 
   finish.py26773%22–25, 33, 39, 58
   think.py321359%24, 27–28, 31, 33–37, 39, 51, 57, 74
openhands/sdk/utils
   __init__.py30100% 
   async_utils.py12120%7–9, 11, 14, 17, 26, 31–32, 34–36
   discriminated_union.py631477%80–82, 87, 116, 169, 172–173, 175, 178, 212, 217, 224, 227
   json.py28280%1–3, 5, 7–8, 11, 14–21, 25, 28, 30–31, 34, 37–38, 40, 43, 45–48
   protocol.py30100% 
   pydantic_diff.py575110%8–9, 11–15, 19–20, 23–24, 27–36, 38–39, 41, 44, 50–58, 60–62, 65, 69–77, 79–80, 84–85
   truncate.py10730%32–33, 36–37, 40–41, 44
openhands/tools
   __init__.py9277%49–50
openhands/tools/execute_bash
   __init__.py40100% 
   constants.py90100% 
   definition.py924551%37, 40, 43–44, 46, 49–51, 53–55, 57, 105, 108–110, 113, 115–117, 119, 123–124, 127–129, 131–132, 135–138, 142–144, 149, 153–155, 158–160, 164–165, 167
   impl.py40392%55, 58, 62
   metadata.py50394%95–96, 100
openhands/tools/execute_bash/terminal
   __init__.py60100% 
   factory.py491177%24–25, 30, 32, 35, 37–38, 44–46, 97
   interface.py691578%43, 52, 62, 71, 76, 85, 94, 99, 145, 157, 162, 171, 180, 191, 193
   subprocess_terminal.py2365975%68, 99–100, 126, 132, 139, 146–147, 157–158, 164–165, 179, 181, 185–187, 193, 209, 218–222, 257–259, 264, 276, 290, 314, 316, 325, 346, 362, 367, 373–375, 383–384, 388–389, 391–397, 401–402, 405–406, 408–409, 411–413
   terminal_session.py178895%92, 96–98, 235, 281, 297, 317
   tmux_terminal.py802173%36, 45, 108, 119, 133, 145–152, 160–161, 163–164, 166, 168–170
openhands/tools/execute_bash/utils
   command.py81495%48, 64–66
openhands/tools/str_replace_editor
   __init__.py30100% 
   definition.py62985%87, 99, 119, 122, 125, 132, 134, 136, 138
   editor.py2281195%131, 264, 340, 350, 401–402, 641, 648–649, 663, 668
   exceptions.py220100% 
   impl.py26292%31–32
openhands/tools/str_replace_editor/utils
   __init__.py00100% 
   config.py20100% 
   constants.py50100% 
   diff.py64198%115
   encoding.py54198%81
   file_cache.py95990%44–46, 49–50, 54, 59, 151, 154
   history.py66198%79
   shell.py230100% 
openhands/tools/task_tracker
   __init__.py20100% 
   definition.py1329428%48, 51–53, 55–56, 59–60, 62, 78, 83, 85, 87–88, 91, 94–96, 98–99, 102–108, 110–112, 115, 117–120, 122, 125, 128–129, 131–132, 134–135, 137, 150–151, 154–155, 159, 161, 163–165, 171, 173–174, 179–180, 184, 193–194, 196–198, 202–203, 205–208, 210, 214–215, 217–219, 221–225, 229, 233–234, 236–237, 239, 241–245, 407, 410
openhands/tools/utils
   __init__.py00100% 
TOTAL5184186164% 

…nt inheritance

- Added comment in SubprocessTerminal.initialize() to clarify environment variable inheritance
- Added specific test for subprocess terminal environment inheritance
- Both terminals now have clear documentation and comprehensive tests for environment variable inheritance

Co-authored-by: openhands <openhands@all-hands.dev>
@xingyaoww
xingyaoww enabled auto-merge (squash) September 14, 2025 02:08
@xingyaoww
xingyaoww merged commit 53ab7bd into main Sep 14, 2025
@xingyaoww
xingyaoww deleted the openhands/fix-execute-bash-env-inheritance branch September 14, 2025 02:13
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
…ocess (OpenHands#245)

Co-authored-by: openhands <openhands@all-hands.dev>
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.

execute_bash tool should be able to inherent current env vars

2 participants