DeepTutor icon indicating copy to clipboard operation
DeepTutor copied to clipboard

[Bug]: Launcher imports a stale source tree from the runtime home, so a zip-based 1.6.x → 1.7.x upgrade keeps running the old code

Open panghufattiger996-maker opened this issue 1 month ago • 5 comments

Do you need to file an issue?

  • [x] I have searched the existing issues and this bug is not already listed.
  • [x] I believe this is a legitimate bug, not just a question or feature request.

Describe the bug

This is a second, independent defect next to #2330. #2330 covers the updater installing only core. This one is why the app keeps failing after you fix that, and why it reports the wrong version while doing it.

deeptutor start --detach re-launches itself with:

# deeptutor/runtime/launcher.py:1331-1349
command = [sys.executable, "-m", "deeptutor_cli.main", "start", "--home", str(runtime_home.resolve())]
kwargs = {"cwd": str(runtime_home), ...}

and deeptutor/runtime/update_worker.py:49,84 restarts the same way (-m deeptutor_cli.main, cwd=home).

For python -m, CPython puts the current working directory at sys.path[0]. Since cwd is the runtime home, any importable deeptutor/ or deeptutor_cli/ directory sitting in the home shadows the venv copy. The documented zip install leaves exactly that: the release archive is extracted to <home> and then pip-installed from it, so <home>/deeptutor/ and <home>/deeptutor_cli/ remain on disk permanently and are imported on every start.

Consequences, all observed on one machine after upgrading 1.6.15 → 1.7.1:

  1. deeptutor start keeps executing the old 1.6.15 code even though 1.7.1 is the installed distribution.
  2. The startup banner reads the shadowed deeptutor/__version__.py, so it prints v1.6.15 forever — the version badge lies.
  3. After installing the missing module wheels by hand, startup still fails, because the shadowed 1.6.15 tree has no deeptutor/modules/ at all. The ModuleUnavailable error from #2330 persists even though the modules are now genuinely installed and deeptutor module list reports them ready.
  4. It is cwd-dependent, so the failure is invisible when you start from any other directory.

catalog.source_checkout() does not catch this. It is defined as:

# deeptutor/modules/catalog.py:80-84
root = Path(__file__).resolve().parents[2]
return (root / "pyproject.toml").is_file() and (root / "scripts/build_modules.py").is_file()

A GitHub release zip has no .git, and a 1.6.x archive has no scripts/build_modules.py (that file arrives in 1.7.0), so the helper reports source_checkout() == False while the import system simultaneously resolves to the source tree. The two answers disagree, and no code reconciles them.

Steps to reproduce

  1. Download the DeepTutor 1.6.15 source zip, extract it to D:\DT, and python -m pip install -e . (or install 1.6.15 from PyPI into a venv and then extract the 1.6.15 zip to the same directory — either way <home> ends up containing deeptutor/ and deeptutor_cli/).
  2. deeptutor start --detach --no-browser --home D:\DT. Confirm the banner says v1.6.15.
  3. Trigger the in-app update to 1.7.1 (or pip install deeptutor==1.7.1). Install the modules so #2330 is out of the picture: pip install "deeptutor[full]==1.7.1".
  4. deeptutor module list — reports core, server, web, etc. as installed: true, ready: true.
  5. deeptutor start --detach --no-browser --home D:\DT — still fails with ModuleUnavailable: Module 'server' is unavailable, and the banner still prints v1.6.15.
  6. Run the same command with the working directory anywhere else: it starts fine and prints v1.7.1.

Expected Behavior

The launcher-managed restart should execute the distribution that was just installed, regardless of what Python files happen to exist in the runtime home. A stale source tree left in <home> is a normal consequence of the documented zip install and must not win over the venv.

The same rule should hold for the update_worker restart path, which is where the mismatch is most damaging: the worker installs 1.7.x, then restarts the old tree, so the upgrade appears to have no effect.

Related Module

API/Backend

Configuration Used

  • DeepTutor 1.6.15 → 1.7.1
  • Install method: GitHub source zip extracted to the runtime home, pip install -e . into <home>\.venv (this is the layout that triggers it; a plain PyPI install into a venv with a neutral home does not)
  • Launcher-managed: deeptutor start --detach --no-browser --home <home>
  • Windows 11 build 26100, Python 3.13.5, Node 26.12.1, pip 26.2.2

Logs and screenshots

worker.log shows the upgrade succeeding:

  Attempting uninstall: deeptutor
    Found existing installation: deeptutor 1.6.15
    Uninstalling deeptutor-1.6.15: Successfully uninstalled deeptutor-1.6.15
Successfully installed deeptutor-1.7.1

Then the restart banner still says v1.6.15, because runtime/banner.py reads deeptutor.__version__ from the shadowed tree.

launcher.log repeats 智能体原生的个性化辅导 · v1.6.15 on every start while metadata.version("deeptutor") is 1.7.1.

Module state was verified good before the failing start:

{ "schema_version": 1, "enabled": { "server": true, "web": true } }

Additional Information

Controlled proof of the resolution-order defect. A minimal deeptutor_cli/ package containing only a print statement, placed in the working directory:

console script deeptutor.exe, cwd=dir with the fake package  -> real CLI   (script dir, not cwd, is sys.path[0])
python -m deeptutor_cli.main, cwd=dir with the fake package  -> fake package
python -m deeptutor_cli.main, cwd=neutral                   -> real CLI

The deeptutor.exe console script is immune because its sys.path[0] is the script directory; the -m invocations used by _launch_detached and update_worker are not. So deeptutor start typed by hand can behave differently from the detached process it spawns.

Same test with the real 1.6.15 tree copied beside a 1.7.1 venv:

cwd = stale 1.6.15 tree:  deeptutor     -> <stale>\deeptutor\__init__.py
                          deeptutor_cli -> <stale>\deeptutor_cli\__init__.py
                          `--help` lists no `module` command

cwd = neutral:            deeptutor     -> .venv\Lib\site-packages\deeptutor\__init__.py
                          deeptutor_cli -> .venv\Lib\site-packages\deeptutor_cli\__init__.py
                          `--help` lists `module`

Possible fixes, in rough order of scope:

  1. Spawn the detached launcher and the update-worker restart with -P (Python 3.11+, and Requires-Python is <3.15,>=3.11) or PYTHONSAFEPATH=1, at least when source_checkout() is false. Verified locally: both make the stale tree stop winning while leaving genuine source checkouts alone if applied conditionally.
  2. Invoke the console script / sys.argv[0] path instead of -m, matching what a hand-typed deeptutor start already does correctly.
  3. Make source_checkout() agree with the import resolver, so the two views cannot diverge — but this alone would not stop the stale import.
  4. Purely diagnostic, worth doing regardless: fail loudly when the loaded deeptutor.__version__ disagrees with metadata.version("deeptutor"), instead of silently printing the old version in the banner.

Why #2357 does not cover this. That PR makes the updater pin the installed module set. It does not change how the spawned process resolves imports, so on this layout the restart still executes the stale tree and the modules it just installed are never seen. #2330's scope note already says 1.6.x → 1.7.x cannot be fixed from 1.7.x; this issue affects every restart on an affected layout, including future 1.7.x → 1.7.y updates.

Immediate workaround for anyone hitting it: move or delete the leftover source trees out of the runtime home, keeping data/ untouched:

mkdir _legacy
move deeptutor,deeptutor_cli,deeptutor_web,deeptutor.egg-info,web _legacy\

After that the same deeptutor start runs 1.7.1 with the banner and modules correct. Note that web/ also has to move, because <home>/web/package.json makes the launcher pick the source frontend build instead of the packaged one.

panghufattiger996-maker avatar Nov 05 '26 03:11 panghufattiger996-maker

Happy to send a small PR for the launcher side of this if that direction is acceptable.

Scope I have in mind: only _launch_detached in deeptutor/runtime/launcher.py, spawning with -P (or PYTHONSAFEPATH=1 in the child env) when source_checkout() is false, so a genuine source checkout keeps its current import behaviour. Both were verified locally against a stale 1.6.15 tree beside a 1.7.1 venv.

I would deliberately leave update_worker.py out: #2357 is already changing _launch_restart there (the 5 s early-exit window), and touching it from a second PR would just create a conflict. If the direction is fine, that restart path could follow once #2357 lands.

Not asking you to commit to anything — just flagging intent so two people do not write the same patch in parallel. If you would rather this stayed out of scope for now, that is a fine answer too and I will leave it as a report only.

panghufattiger996-maker avatar Nov 05 '26 03:11 panghufattiger996-maker

One additional acceptance boundary for the proposed -P / PYTHONSAFEPATH=1 change: safe-path stops the unsafe cwd from being prepended, but it does not ignore an inherited PYTHONPATH. A poisoned or installer-left PYTHONPATH could therefore still select a different tree even after the cwd case is fixed. I would avoid treating “the banner now says 1.7.1” as the sole readiness signal.

A useful regression matrix would cover:

  • packaged install vs. intentional editable/source checkout;
  • neutral cwd vs. runtime home containing the stale deeptutor/, deeptutor_cli/, and web/ trees;
  • clean environment vs. a deliberately conflicting PYTHONPATH;
  • both _launch_detached and the update-worker restart, including a restart immediately after pip reports success.

For packaged mode, either sanitize PYTHONPATH in the child environment or explicitly document why it is trusted; if stronger isolation such as -I is considered, check first that no required Python-path configuration is lost.

Most importantly, have the restarted child return a small runtime-identity receipt before the update is marked ready: deeptutor.__file__, deeptutor_cli.__file__, loaded __version__, importlib.metadata.version("deeptutor"), resolved module distributions, and the selected frontend asset origin. Assert that core/CLI import origins belong to the intended environment (or the approved source checkout), and that loaded and installed versions agree. That closes the gap where pip, module list, the banner, and the process actually serving traffic can otherwise report four different views of the upgrade. This is a proposed acceptance gate; I have not independently rerun the Windows upgrade scenario.

zeweihan avatar Nov 05 '26 06:11 zeweihan

Re-checking this after the 2026-11-05 merge batch: the base fix landed via #2447 (PYTHONSAFEPATH is now set in launcher.py:1327 and update_worker.py:91), and the install-plan half landed via #2357 (catalog.installed_distributions() at catalog.py:118, pinning the whole installed set in one transaction). Two of the review points raised above are still open, so I would keep this issue open rather than close it — just narrowing it to what remains.

Still uncovered, verified against current dev:

  1. PYTHONPATH is not sanitized. The merged code is env = os.environ.copy() followed by env["PYTHONSAFEPATH"] = "1", so an inherited PYTHONPATH survives into the child. This is the case zeweihan raised, and I confirmed it on Windows / Python 3.13.5 with a fake deeptutor_cli/ package, both vectors live:

    python -m deeptutor_cli.main        -> poisoned
    python -P -m deeptutor_cli.main     -> poisoned      (safe-path does not ignore PYTHONPATH)
    python -I -m deeptutor_cli.main     -> real CLI, but sys.stdout.encoding becomes gbk
    python -P -m ... with PYTHONPATH removed from the child env -> real CLI, encoding stays utf-8
    

    The -I / -E route is not a safe drop-in here: both discard PYTHONIOENCODING, which launcher.py:1652 sets to utf-8:replace and which the console-relaxation helper (deeptutor/utils/console_encoding.py, wired in at launcher.py:1522) depends on to avoid the Windows legacy-code-page UnicodeEncodeError tracked as #702. So the remaining fix is sanitizing PYTHONPATH in the child environment, not isolating the interpreter.

  2. No runtime identity check. Nothing compares the loaded deeptutor.__version__ / deeptutor.__file__ against importlib.metadata.version("deeptutor"), so the original failure signature stays silent. In the reported case pip said 1.7.1, deeptutor module list reported ready: true for every module, the banner printed v1.6.15, and the process serving traffic was the 1.6.15 tree — four views, all consistent with the code, all different. banner.py on dev still imports the shadowed __version__ at line 15 and prints it at line 868 without cross-checking anything.

Optional 3 — source_checkout() is still not consulted when choosing the spawn flags, so an intentional editable run gets the same treatment as a packaged install. That reads as a policy question rather than a defect, and I do not feel strongly about it.

I have a small local patch for point 2 — the banner reports both versions and the loaded origin when they disagree, with 15 tests passing against current dev. I opened no PR because this issue was filed as an environment/layout report and I did not want to widen it unilaterally. Happy to send it if a maintainer wants the diagnostic half, either here or as its own issue.

The 1.6.x → 1.7.x path itself still needs the manual upgrade, as #2357's own scope note says; nothing in this batch changes that.

panghufattiger996-maker avatar Nov 06 '26 05:11 panghufattiger996-maker

Opened #2481 with the diagnostic half of point 2 above: the banner now compares the loaded deeptutor.version with importlib.metadata.version("deeptutor") and, when they disagree, names both in the tagline and prints the loaded origin as a warning. 15 tests, dev targets. It changes no import resolution, so it does not overlap #2447.

panghufattiger996-maker avatar Nov 06 '26 05:11 panghufattiger996-maker

Verified fixed on the current local dev branch (50968922d3f6), with regression checks.

will be fixed in the next releases!

pancacake avatar Nov 14 '26 16:11 pancacake