fix(update): make self-update safe on Windows#530
Merged
Conversation
padak
marked this pull request as ready for review
July 23, 2026 21:11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Design: Make kbagent self-update safe on Windows
Issue: #528
Status: Proposed
Target:
kbagent updateand startup auto-updateProblem
Both self-update entry points currently mutate the
uv toolenvironment fromwhich the running
kbagentprocess was launched:_perform_update(), then continues in the oldprocess long enough to write the cache and re-exec;
kbagent updateupdates kbagent first and then performs MCPdetection, a PyPI request, and an MCP update from the already-mutated
process.
On Windows, a running executable or dependency can be locked. Even without a
lock, a failed in-place dependency swap can leave the tool environment with a
mixture of old and new distributions. The reporter observed both failure
classes:
richandtyperafter a failed startup update;certifi/cacert.pemafter explicit update removed the olddistribution and the same process subsequently attempted the MCP HTTPS
check.
The updater must treat mutation of its own environment as a terminal action,
and it must use
uv tool installas a full reinstall/re-resolution rather thanan incremental upgrade.
Goals
work after the kbagent environment starts changing.
artifact when
uvmanages the install.[server]extras and the beta channel.abort the requested command.
Non-goals
uv's package resolver or tool-environment transaction logic.Windows environment. A locked executable remains a reported, recoverable
failure.
kbagent's environment.
Design
1. Separate planning from mutation
Introduce small immutable plan/result dataclasses in
services/version_service.py. The planning phase performs every operation thatcan touch the network or inspect the current environment:
@v<version>git fallback;[server]extras and build the complete install command;The apply phase receives only these prepared values. It must not call
httpx,importlib, package metadata probes, or command builders again.2. Apply MCP before kbagent
For explicit
kbagent update, execute the independent MCP stage first.Re-probing the MCP version is allowed immediately after that stage because the
kbagent environment is still untouched.
The kbagent reinstall is always the final mutation. Its result is assembled
from the prepared plan and subprocess return value; no post-install version
probe or network request is made.
This removes the exact ordering bug behind the missing certifi CA bundle.
3. Recreate the uv tool environment
For every uv-managed kbagent path, build:
The preferred spec is the versioned release wheel. The fallback source must
also be pinned to
@v<target_version>for stable and beta releases; updatingfrom mutable
mainis not sufficiently reproducible for a recovery operation.When server extras are present, encode them in the primary PEP 508 requirement
(
keboola-cli[server] @ ...) so one resolution recreates the completeenvironment.
uv tool installis the supported operation for recreating a tool environment;--reinstallensures every package is materialized again and--forceallowsuv-owned entry points to be replaced. Pip remains a best-effort fallback, but
its result and recovery limitation must be explicit.
4. Make startup mutation terminal
Reorder startup auto-update:
KBAGENT_SKIP_UPDATE=1.There must be no cache write or MCP stage between steps 4 and 5. The
already-prepared cache lets the re-exec'd process skip all network work for
that cycle.
If the kbagent install subprocess fails or times out, emit a concise failure
and the exact forced-reinstall recovery command. Do not report a successful
update and do not run further update work from the potentially inconsistent
process.
5. Preserve the command contract
kbagent updatekeeps the current top-level result shape. Add enough fields tomake partial outcomes unambiguous:
planned,updated,up_to_date, andmessage;recovery_command;updatedremains true when at least one stage changed;The existing
--betabehavior,[server]preservation, timeouts, andhuman/JSON rendering remain supported.
Implementation map
src/keboola_agent_cli/services/version_service.pyuv tool install --force --reinstallthe self-updatecommand;
src/keboola_agent_cli/auto_update.py_perform_update;src/keboola_agent_cli/commands/version.pyaccessing package metadata after the self-update stage.
src/keboola_agent_cli/changelog.pytests/test_version_service.pymutation;
tests/test_auto_update.pyAcceptance criteria
updater.
the kbagent install command begins.
tool install --force --reinstall.[server]and beta installs retain their current behavior.immediately re-execs after success.
includes an exact recovery command.
post-mutation HTTP call fail; the fixed flow performs no such call.
pass.
Validation
Run locally:
Before merge, exercise a release wheel on Windows 11:
[server]viauv tool install;a working
kbagent --version;kbagent serveopen and verify the lock failure is clear and does notclaim success.
Risks and follow-up
uv; this change reduces ourmutation to a full, exact reinstall and eliminates all post-mutation work.
remain reproducible with current uv. That is intentionally separate because
process detachment/relaunch semantics need dedicated Windows integration
coverage.
guarantee. Its failure message must recommend reinstalling with uv.