Repository: xonsh/xonsh

Issue:
Starship with the current main branch: KeyError: 'obj'
## Current Behavior
<!---
For general xonsh issues, please try to replicate the failure using `xonsh --no-rc --no-env`.
Short, reproducible code snippets are highly appreciated.
You can use `$XONSH_SHOW_TRACEBACK=1`, `$XONSH_TRACE_SUBPROC=2`, or `$XONSH_DEBUG=1`
to collect more information about the failure.
-->

On the latest git version of xonsh with starship enabled, after successfully capturing a subprocess, it shows the following traceback, and the starship prompt is temporarily gone until the next command is executed.

Traceback (if applicable):

<details>

```xsh
ERROR:root:'obj'
Traceback (most recent call last):
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 93, in __call__
    toks = self._format_prompt(template=template, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/ptk_shell/formatter.py", line 46, in _format_prompt
    toks = super()._format_prompt(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 107, in _format_prompt
    tmpl = template() if callable(template) else template
           ^^^^^^^^^^
  File "<stdin>", line 9, in starship_prompt
  File "<stdin>", line 9, in <genexpr>
KeyError: 'obj'
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 93, in __call__
    toks = self._format_prompt(template=template, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/ptk_shell/formatter.py", line 46, in _format_prompt
    toks = super()._format_prompt(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 107, in _format_prompt
    tmpl = template() if callable(template) else template
           ^^^^^^^^^^
  File "<stdin>", line 9, in starship_prompt
  File "<stdin>", line 9, in <genexpr>
KeyError: 'obj'
Failed to format prompt `<function starship_prompt at 0x7f72fdebfce0>`-> <class 'KeyError'>:'obj'
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 93, in __call__
    toks = self._format_prompt(template=template, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/ptk_shell/formatter.py", line 46, in _format_prompt
    toks = super()._format_prompt(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 107, in _format_prompt
    tmpl = template() if callable(template) else template
           ^^^^^^^^^^
  File "<stdin>", line 9, in starship_prompt
  File "<stdin>", line 9, in <genexpr>
KeyError: 'obj'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 172, in _failover_template_format
    return template()
           ^^^^^^^^^^
  File "<stdin>", line 9, in starship_prompt
  File "<stdin>", line 9, in <genexpr>
KeyError: 'obj'
ERROR:root:'obj'
Traceback (most recent call last):
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 93, in __call__
    toks = self._format_prompt(template=template, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/ptk_shell/formatter.py", line 46, in _format_prompt
    toks = super()._format_prompt(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 107, in _format_prompt
    tmpl = template() if callable(template) else template
           ^^^^^^^^^^
  File "<stdin>", line 18, in starship_rprompt
  File "<stdin>", line 18, in <genexpr>
KeyError: 'obj'
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 93, in __call__
    toks = self._format_prompt(template=template, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/ptk_shell/formatter.py", line 46, in _format_prompt
    toks = super()._format_prompt(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 107, in _format_prompt
    tmpl = template() if callable(template) else template
           ^^^^^^^^^^
  File "<stdin>", line 18, in starship_rprompt
  File "<stdin>", line 18, in <genexpr>
KeyError: 'obj'
Failed to format prompt `<function starship_rprompt at 0x7f72fdebfe20>`-> <class 'KeyError'>:'obj'
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 93, in __call__
    toks = self._format_prompt(template=template, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/ptk_shell/formatter.py", line 46, in _format_prompt
    toks = super()._format_prompt(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 107, in _format_prompt
    tmpl = template() if callable(template) else template
           ^^^^^^^^^^
  File "<stdin>", line 18, in starship_rprompt
  File "<stdin>", line 18, in <genexpr>
KeyError: 'obj'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yes/.virtualenvs/xonsh-test/lib/python3.12/site-packages/xonsh/prompt/base.py", line 172, in _failover_template_format
    return template()
           ^^^^^^^^^^
  File "<stdin>", line 18, in starship_rprompt
  File "<stdin>", line 18, in <genexpr>
KeyError: 'obj'
```

</details>

I found this issue when investigating https://github.com/xonsh/xonsh/issues/5003, reproduced and bisected in my Arch Linux container. The first bad commit is 0f25a5a348e5569982d2b579a1bfa34d68df2418.

## Expected Behavior
<!--- What you expect -->
There shouldn't be any errors after using !(), and the starship prompt should show up as normal afterwards.

## xonfig

<details>

```xsh
+-----------------------------+----------------------+
| xonsh                       | 0.16.0.dev111        |
| Git SHA                     | 0f25a5a3             |
| Commit Date                 | May 22 23:45:39 2024 |
| Python                      | 3.12.3               |
| PLY                         | 3.11                 |
| have readline               | True                 |
| prompt toolkit              | 3.0.39               |
| shell type                  | prompt_toolkit       |
| history backend             | json                 |
| pygments                    | 2.18.0               |
| on posix                    | True                 |
| on linux                    | True                 |
| distro                      | arch                 |
| on wsl                      | False                |
| on darwin                   | False                |
| on windows                  | False                |
| on cygwin                   | False                |
| on msys2                    | False                |
| is superuser                | False                |
| default encoding            | utf-8                |
| xonsh encoding              | utf-8                |
| encoding errors             | surrogateescape      |
| xontrib 1                   | vox                  |
| xontrib 2                   | voxapi               |
| RC file 1                   | /home/yes/.xonshrc   |
| UPDATE_OS_ENVIRON           | False                |
| XONSH_CAPTURE_ALWAYS        | False                |
| XONSH_SUBPROC_OUTPUT_FORMAT | stream_lines         |
| THREAD_SUBPROCS             | True                 |
| XONSH_CACHE_SCRIPTS         | True                 |
+-----------------------------+----------------------+
```

</details>

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**



Discussion:

Resolving patch:
diff --git a/xonsh/jobs.py b/xonsh/jobs.py
--- a/xonsh/jobs.py
+++ b/xonsh/jobs.py
@@ -210,7 +210,7 @@ def _continue(job):
 
     def _kill(job):
         subprocess.check_output(
-            ["taskkill", "/F", "/T", "/PID", str(job["proc"].pid)],
+            ["taskkill", "/F", "/T", "/PID", str(job["obj"].pid)],
             stderr=subprocess.STDOUT,
         )
 
@@ -231,7 +231,7 @@ def wait_for_active_job(last_task=None, backgrounded=False, return_error=False):
         # Return when there are no foreground active task
         if active_task is None:
             return last_task
-        proc = active_task["proc"]
+        proc = active_task["obj"]
         _continue(active_task)
         while proc.returncode is None:
             try:
@@ -344,7 +344,7 @@ def wait_for_active_job(last_task=None, backgrounded=False, return_error=False):
         # Return when there are no foreground active task
         if active_task is None:
             return last_task
-        proc = active_task["proc"]
+        proc = active_task["obj"]
         info = {"backgrounded": False}
 
         try:
@@ -402,7 +402,7 @@ def _clear_dead_jobs():
     to_remove = set()
     tasks = get_tasks()
     for tid in tasks:
-        proc = get_task(tid)["proc"]
+        proc = get_task(tid)["obj"]
         if proc is None or proc.poll() is not None:
             to_remove.add(tid)
     for job in to_remove:
diff --git a/xonsh/procs/specs.py b/xonsh/procs/specs.py
--- a/xonsh/procs/specs.py
+++ b/xonsh/procs/specs.py
@@ -956,7 +956,7 @@ def _run_command_pipeline(specs, cmds):
                 "cmds": cmds,
                 "pids": [i.pid for i in cp.procs],
                 "status": "suspended" if cp.suspended else "running",
-                "proc": proc,
+                "obj": proc,
                 "bg": background,
                 "pipeline": cp,
                 "pgrp": cp.term_pgid,


Test patch: