Files
DeepTutor/deeptutor/services/subagent/process.py
Bingxi Zhao (Frank) bd80a4d28a release: v1.5.17
Partners stop being a deployment-wide admin resource and become
per-user: three nested rights (manage / use / neither) replace the
blanket admin gate on the router, each member can build and own their
own, and every person's conversations with a partner stay private to
their account. Grants keep working and now offer only admin-managed
partners. Channel runtime state moves from one directory per channel to
one per partner — copied, not moved, and only when its owner is
unambiguous — so two partners can finally run the same channel.

Around that: a knowledge base can track GitHub repositories and re-sync
their Markdown (#903); Antigravity CLI joins My Agents now that Gemini
CLI is retired (#828); personal WeChat signs in from a QR code drawn in
the browser instead of the server log (#951); a chat account can be
linked to a DeepTutor account with /link; containers re-apply the extras
they declare instead of losing them at compose down (#762); and
`deeptutor doctor` answers whether a workspace is ready to run (#959).

Reliability, largely from community PRs: embeddings survive a
rate-limited provider through key rotation, a retry budget and a global
throttle (#962); MinerU parsing and code execution work on Windows
(#844, #964); indexing fails bounded instead of hanging (#946); MinerU
page-layout blocks stop being indexed as content (#949); voice uploads
carry a MIME the transcription API accepts (#945); and reasoning effort
can be overridden per conversation (#641).

Release plumbing: notes in assets/releases/ver1-5-17.md, v1.5.13
archived, __version__ 1.5.17, README Releases row and all eleven
translations synced.

Two defects found by the release gate and fixed here:

- Six partner routes — the four channel-onboarding endpoints and both
  WeChat QR endpoints — kept the _auth-only mount they had inherited
  from the blanket admin gate, so any signed-in account could start a
  QR login for someone else's partner and write a bot token into its
  channel config. All six now declare _MANAGEABLE, and a sweep test
  asserts no {partner_id} route is left ungated.
- A configured api_key became `str | list[str]` when key rotation
  landed, but ten call sites pass it somewhere that takes one key; a
  list would have rendered into an Authorization header verbatim.
  keypool.primary_api_key reduces at each boundary.

Gates: pre-commit (all 15 hooks), ruff check + format, pytest
5276 passed, web 605 node tests, docs build.
2026-08-25 02:24:35 +08:00

188 lines
6.9 KiB
Python

"""Stream a child process's stdout/stderr line-by-line as it runs.
The single low-level primitive the subagent backends share: spawn a command and
yield every stdout and stderr line the moment it arrives — so a long, multi-step
agent run surfaces live in the sidebar instead of all at once at the end — then
guarantee the process is torn down when the consumer stops early or the turn is
cancelled.
There is deliberately **no timeout** on the wait: per the product contract,
DeepTutor waits unconditionally for the subagent's own logic to finish; only the
subagent exiting (cleanly or with an error) ends the stream. Cancellation (the
user aborting the turn) propagates as ``CancelledError`` and the ``finally``
block terminates the child so no orphaned agent process is left behind.
"""
from __future__ import annotations
import asyncio
from collections.abc import AsyncIterator, Mapping, Sequence
import contextlib
import logging
import os
import shutil
logger = logging.getLogger(__name__)
def resolve_cli_command(cmd: Sequence[str], *, path: str | None = None) -> list[str]:
"""Resolve ``cmd[0]`` to a full path via PATH/PATHEXT before spawning.
On Windows, ``asyncio.create_subprocess_exec`` (and the underlying
``CreateProcess``) only appends ``.exe`` when resolving a bare command
name, so CLIs installed by npm ship as ``.cmd``/``.bat``/``.ps1`` shims
(e.g. ``claude``/``codex``) are invisible and the spawn raises
``FileNotFoundError`` — the backend then reports "not installed" even
though the CLI is on PATH. ``shutil.which`` honors ``PATHEXT`` (and a
caller-supplied PATH) and returns the shim's full path, so the spawn
finds it.
Returns the command unchanged when ``cmd`` is empty, when ``cmd[0]`` is
already an absolute path (resolved back to itself), or when nothing on
PATH matches — in that last case the OS spawn behaves exactly as before,
preserving the existing ``FileNotFoundError`` semantics callers handle.
"""
if not cmd:
return list(cmd)
resolved = shutil.which(cmd[0], path=path)
if resolved:
return [resolved, *list(cmd[1:])]
return list(cmd)
# (channel, text) where channel is "stdout", "stderr", or "exit" (the final
# item, whose text is the integer return code as a string).
ProcessLine = tuple[str, str]
_TERMINATE_GRACE_SECONDS = 5.0
async def stream_process_lines(
cmd: Sequence[str],
*,
cwd: str | None = None,
env: Mapping[str, str] | None = None,
) -> AsyncIterator[ProcessLine]:
"""Yield ``(channel, line)`` for each stdout/stderr line until the process exits.
The final item is always ``("exit", "<returncode>")`` so callers can tell a
clean finish from an early break. stdout and stderr are interleaved in
arrival order via a shared queue.
"""
full_env = {**os.environ, **(env or {})}
resolved_cmd = resolve_cli_command(cmd, path=full_env.get("PATH"))
process = await asyncio.create_subprocess_exec(
*resolved_cmd,
cwd=cwd or None,
env=full_env,
stdin=asyncio.subprocess.DEVNULL,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
queue: asyncio.Queue[ProcessLine | None] = asyncio.Queue()
async def _pump(stream: asyncio.StreamReader | None, channel: str) -> None:
if stream is None:
await queue.put(None)
return
try:
while True:
raw = await stream.readline()
if not raw:
break
await queue.put((channel, raw.decode("utf-8", "replace").rstrip("\r\n")))
except Exception: # pragma: no cover - defensive: a broken pipe must not hang the queue
logger.debug("subagent %s pump failed", channel, exc_info=True)
finally:
await queue.put(None) # sentinel: this channel is drained
readers = [
asyncio.create_task(_pump(process.stdout, "stdout")),
asyncio.create_task(_pump(process.stderr, "stderr")),
]
drained = 0
try:
while drained < len(readers):
item = await queue.get()
if item is None:
drained += 1
continue
yield item
returncode = await process.wait()
yield "exit", str(returncode)
finally:
for task in readers:
task.cancel()
with contextlib.suppress(Exception):
await asyncio.gather(*readers, return_exceptions=True)
await _terminate(process)
async def _terminate(process: asyncio.subprocess.Process) -> None:
"""Best-effort teardown: terminate, wait briefly, then kill."""
if process.returncode is not None:
return
with contextlib.suppress(ProcessLookupError):
process.terminate()
try:
await asyncio.wait_for(process.wait(), timeout=_TERMINATE_GRACE_SECONDS)
return
except (TimeoutError, asyncio.TimeoutError):
pass
except ProcessLookupError: # pragma: no cover
return
with contextlib.suppress(ProcessLookupError):
process.kill()
with contextlib.suppress(Exception):
await process.wait()
def not_found_detail(probe_text: str, fallback: str) -> str:
"""What to show a reader when a CLI probe failed.
:func:`probe_version` reports a bare ``"not installed"`` for a missing
binary, which repeats what the status chip already says and crowds out the
backend's own guidance — every caller writing ``version or "<cli> not found
on PATH"`` was in fact unreachable for the common case. Real probe output (a
version banner, a permission error) is more informative and still wins.
"""
text = (probe_text or "").strip()
return fallback if text in ("", "not installed") else text
async def probe_version(cmd: Sequence[str], *, timeout: float = 8.0) -> tuple[bool, str]:
"""Run a fast ``--version``-style probe; return ``(ok, stdout-or-error)``.
Used by backend ``detect`` to answer "is this CLI installed here?" without
the no-timeout consult semantics — a probe that hangs is a failed probe.
"""
try:
resolved_cmd = resolve_cli_command(cmd)
process = await asyncio.create_subprocess_exec(
*resolved_cmd,
stdin=asyncio.subprocess.DEVNULL,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
)
except FileNotFoundError:
return False, "not installed"
except Exception as exc: # pragma: no cover - defensive
return False, str(exc)
try:
out, _ = await asyncio.wait_for(process.communicate(), timeout=timeout)
except (TimeoutError, asyncio.TimeoutError):
await _terminate(process)
return False, "probe timed out"
text = (out or b"").decode("utf-8", "replace").strip()
return process.returncode == 0, text
__all__ = [
"ProcessLine",
"not_found_detail",
"probe_version",
"resolve_cli_command",
"stream_process_lines",
]