Repository navigation
fix(env): reload .env credential changes without a gateway restart (#22450) - #23836
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: Notes
[DESIGN-REVIEWED] c61b937 Description read[DESCRIPTION-READ] 4ecd84df4740e12204d55bdcf5a7529018324e1b1cbdd8f48115635a727aced6 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. The Windows case-folding added to the two dashboard writers rides along, and so does the Weixin rollback rewrite it forces. Notes
What this change shipsInventory (8 items) — 5 justifiedIntent: an operator's
[FIRST-PRINCIPLES-REVIEWED] c61b937 Description read[DESCRIPTION-READ] dd0675ac22fa58f50fdef4258cf106749c685cb9bd2b54f16f8e641412599a5b That sha256 names the PR description this verdict read. Recompute it with |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
910a698 to
a8f7eeb
Compare
6a0423d to
6f25202
Compare
6f25202 to
d60449a
Compare
2b40ed5 to
07b44ff
Compare
3a40c71 to
a502a7b
Compare
|
HOLD at a502a7b: this needs a human reviewer. The PR changes how Three current-head AI notes have no reply:
To clear: a maintainer reviews the credential paths and Sensitive Change Review records that check, and the author answers each note (fix, rebut, or a linked issue). — crew-pr-reviewer (r126) |
…22450) Edits to $KIROCREW_HOME/.env took effect only after a full gateway restart: the live config watcher fingerprinted config.json only. The watcher now reconciles .env in the same poll cycle, applies the diff to os.environ through one applier (honouring the Docker credentials scrub and the PATH carve-out), pops only keys .env itself owns, and restarts only the channel whose credential changed. Slack, which has no in-process restart, logs a "run kirocrew restart" notice. The .env ownership record is carried across an in-app re-exec through the existing keep_for_reexec channel, so a post-restart .env removal still pops what .env owned. The dashboard channel writer and the Weixin sign-in writer match .env lines by canonical os.environ entry name, so on Windows a lowercase spelling of a saved key cannot keep an old token in force. POSIX stays case-sensitive. Supersedes #23569. Fixes #22450
a502a7b to
c61b937
Compare
|
Recommend HOLD at c61b937. This needs a human reviewer. The PR changes how One current-head note has no reply. First Principles item 7 says the Windows case-variant collapse in both writers, and the Weixin rollback rewrite it forces, ride along without a linked report, and suggests deferring them to their own issue. Please keep it with a short rationale, or split it out. To clear: a maintainer reviews the credential and Teams admission paths, and the author answers item 7. — crew-pr-reviewer (r131) |
|
|
Recommend HOLD (policy: human review) at The PR changes how Since r131 the author has answered First Principles item 7 with an evidence-backed rebuttal. Without spelling-aware writers, a save on Windows would append a second spelling, and the new live apply would keep the old token while reporting success. The cited tests fail against the old writers. I accept that reply. Every AI lane passes or was skipped at this head, all checks are green, and the branch merges cleanly with main. In the parts I read I found no defect, but I didn't audit all 45 files. To clear: a maintainer reviews the credential, re-exec ownership and Teams admission paths. — crew-pr-reviewer (r134) |
Problem / Motivation
Goal: An edit to
$KIROCREW_HOME/.envtakes effect on a running gateway, restarting only the affected channel, without a full gateway restart.Edits to
$KIROCREW_HOME/.envtook effect only after a full gateway restart. The live config watcher fingerprintedconfig.jsononly, and nothing re-read.envafter boot. The dashboard's own credential writer applied its changes live, but a hand edit or any other writer did not, so a rotated token kept reading as the old value until someone restarted the gateway.Why it matters
Operators rotate channel credentials by editing
.env. Today the edit silently does nothing until a restart, so a revoked token keeps a channel failing to authenticate, and a new token looks "saved" but is not in force. Requiring a full restart for a credential change also drops every running session.Not a goal
kirocrew restart" notice instead..envsupports or how boot reads it.What changed (motivation → approach → change)
Symptom: a
.envedit is ignored until restart. Root cause:ConfigWatchnever watched.env. Change:.envis reconciled in the same poll cycle that already reloadsconfig.json.load_credentials(propagate=False), so a worker-thread read cannot reseed a credential the watcher just removed.apply_env_credential_changesinstead of its own set/pop loop, so it also respects the Docker credentials scrub..envPATHline is expanded and validated off the event loop (env.prepare_env_path_additions, run viaasyncio.to_thread) and handed to the applier aspath_additions. A~otheruserentry callspwd.getpwnam/ the SAM database, and that lookup no longer runs on the loop.os.path.expandusersemantics are unchanged..envownership only when the write takes the entry over (a new key, a rotation, an overwrite of a foreign value, or a key.envalready owns). A line whose value merely matches what the process inherited from a service unit or shell is not owned, so removing it later leaves the inherited credential in place, as boot'ssetdefaultalready does..envownership record survives an in-app restart through one environment marker,_KIROCREW_ENV_OWNED_KEYS. The record is Python module state, andos.execvkeepsos.environbut resets module state, so without it the restarted gateway would find every.envkey already set, treat each one as inherited, and never pop any of them on a later removal. The marker rides the existing exec-successor channel,platform_compat.keep_for_reexec, the same one the managed-service launch marker uses; no new pre-exec hook is added. Every change to the record (note_env_owned_key,discard_env_owned_key,reset_env_owned_keys, and the boot-timeadopt_env_owned_keys_from_reexec) handskeep_for_reexecthe owned key NAMES (comma-joined, no values; an empty marker when nothing is owned). The marker is never in this process's ownos.environ:reexec_launcher/reexec_python_modulewrite it there only on the line that already applies_KEPT_FOR_REEXEC, right before the exec. In the successor, the firstload_credentials(propagate=True)(at the latest the one inrun_gateway, which runs before the orchestrator is built and before anything in that function spawns a process) callsadopt_env_owned_keys_from_reexec, which pops the marker fromos.environand restores the record, so spawned children never inherit it. The only callers of those two seams are the gateway's update/restart paths, and a failed exec there exits the process (exit_after_failed_restart_exec) rather than leaving it running with the marker set.os.environENTRY rather than per.envspelling. On Windows (case-insensitiveos.environ) every spelling of a key (HTTPS_PROXY/https_proxy) is grouped and resolved once, to the first non-empty spelling in.envorder (boot'ssetdefaultprecedence), so a rename, a partial duplicate removal, or both spellings edited in one save cannot leave the entry depending on iteration order. On POSIX each entry has one spelling, so nothing changes there.changed_env_keys) also compares each entry's effective value, so a Windows save that only reorders two spellings with different values is applied instead of silently adopted as the new baseline.apply_env_credential_changesas the dashboard channel writer, so it records.envownership (a later removal of the line revokes it in-process) and keeps the Docker scrub guard..env+config.jsoncommit runs underlive.hold()(like every sibling channel saver) and throughdrained_to_thread, so the watcher never restarts Weixin between the two writes and a cancelled request never releases the hold mid-write..envrecord is tied to the file it was read from: the watcher adopts it only for the same.env, so a record from another data home is never taken as its baseline.restart_credentialsdescriptor field. The tenant gets its own descriptor field rather than joiningcredentialsbecause readiness (channels.pychannel_readiness) reports any absentcredentialskey as missing, and an unset tenant is the supported multi-tenant mode.state.teams_on_activity) is bound only once a client is the accepted handle, after boot'sstart_channelsand after a restart passes its generation check.maybe_start_teamsno longer binds it before the connect completes, so a restart superseded mid-connect never exposes its handler (the route answers 503 until then).registry.changed_credential_keys, so a lowercase.envSlack key on Windows still gets the notice..envwriters match lines by canonical entry name (_env_name_key), not literal spelling: the dashboard channel writer (_write_env_updates_locked) and the Weixin sign-in's_write_env_secret/_read_env_value/_delete_env_key. Before this, on Windows a hand-typedslack_bot_token=line was left in place and the savedSLACK_BOT_TOKEN=was appended after it, so the first-non-empty-spelling rule above put the OLD token back in force while the save reported success. Now a save rewrites the first case-equivalent spelling under the declared name and drops the rest, a clear removes every spelling, and the rollback read returns the spelling boot actually propagates. On POSIX the canonical name is the name itself, so a lowercase line is a different variable and is left alone.This supersedes #23569, and before it #23027, #23470, #23491 and #23514 (all closed). It is one commit on current
main(the final #23569 diff plus the First Principles fix below, squashed on a new branch), plus a second commit for the GPT 6.1 WeChat rollback finding below, so PR Hygiene's two-commit limit is met without a force-push. It folds in every review fix from those PRs, including the structural-guard registrations, the Weixin sign-in hold, and scoping the boot.envrecord.Carried over from #23569
609fb58fbc. The HOLD found no definite defect. It reserves the credential and Teams webhook admission changes for a human reviewer, and it was waiting on Sensitive Change Review. The disposition posted there explains both changes in plain words, answers the two advisory notes (the_KIROCREW_ENV_OWNED_KEYSmarker is described above, andrestart_credentialsis explained under Teams), and says the HOLD still stands. This PR does not clear it: a maintainer still needs to review the credential and Teams admission changes, and Sensitive Change Review still needs to complete here..envfinding (channel_config_writes.py): fixed. The writer bullet above covers it. GPT 6.1 reported no findings at fix(env): reload .env credential changes without a gateway restart #23569's last head,5e779ea66e.383ae6675e(weixin_qr.py:182): fixed in the second commit. A failedconfig.jsonwrite rolled back by restoring one snapshotted value, but the sign-in had already collapsed every Windows case spelling of the key into one line. So an empty lowercase alias ahead of the working token (boot skips the empty one) came back as the only line, and the working token was lost. The rollback now restores the exact.envtext, BOM included, snapshotted under the shared.envlock the commit already holds._read_env_valuenow returns the first non-empty spelling, the one boot propagates.5e779ea66e(a second exec-successor hook besidekeep_for_reexec): fixed here as the verdict's Subtraction asked._carry_env_ownership_across_reexecand its three call sites are deleted, andserialize_env_owned_keys_for_reexecis gone with them. The ownership record now hands its marker tokeep_for_reexec, soplatform_compat_runtime.pyandplatform_compat.pyare byte-identical tomain. The verdict's other note, thatrestart_credentialshas one consumer, is answered under Teams above.Backwards compatibility
Compatible: boot still seeds
.envwithsetdefault, every key.envaccepted before is still accepted, andPATHentries resolve exactly as before.Removes nothing: the diff adds a watcher input and routes existing credential writes through one applier; no key, kind, public function or stored data is removed or renamed.
Reader: src/kiro_crew/dashboard/messaging_api/channel_config_writes.py:write_channel_env -- crew page -- under
_KIROCREW_CREDS_SCRUBBEDa dashboard save no longer writes credential keys intoos.environ, matching the boot loop; children read them from.env(test_a_landed_write_honours_the_docker_creds_scrub)Tests
Proof: PROVEN test/test_channel_boot_hot_reload.py::TestTeamsCloseClearsWebhookHandler::test_closing_teams_clears_the_late_bound_webhook_handler, test/test_channel_boot_hot_reload.py::TestTeamsWebhookPublishedOnlyForTheAcceptedClient::test_an_accepted_start_binds_its_handler, test/test_channel_boot_hot_reload.py::TestCredentialChangeRestartsChannel::test_a_credential_change_restarts_the_owning_channel, test/test_channel_boot_hot_reload.py::TestCredentialChangeRestartsChannel::test_a_credential_change_for_a_degraded_channel_is_deferred_not_dropped, test/test_channel_boot_hot_reload.py::TestSlackCredentialChangeWarns::test_a_slack_token_change_logs_a_restart_needed_notice, test/test_slack_config_handlers.py::test_windows_save_replaces_a_lowercase_spelling_of_the_key (head a502a7b)
test/test_env_credential_hot_reload.py: an env-only edit dispatchescredentials.<KEY>and appliesos.environ; a failed apply leaves the baseline so a repair re-applies the rotation; an undecodable or wide-encoded.envis retried rather than read as every key cleared; a removal pops only what.envpropagated; a.envPATH edit is expanded on a worker thread, not the loop thread; a Windows case-duplicate keeps its entry when one spelling is removed; a live add that matches an inherited value does not take ownership, so deleting the line keeps the inherited token, while an add that writes a new value does own it and is popped on removal; both spellings edited in one save resolve to boot's precedence in either iteration order, modelled with a case-foldingos.environ; a reorder of two Windows spellings with different values is a diff and installs the new winner, while a reorder of equal values is not.test/test_dotenv_path_extension.py: a.envPATH line appends absolute directories behind the existing PATH, deduped; relative and unexpanded entries are dropped with a warning;~and~otheruserstill expand throughos.path.expanduser.test/test_channel_boot_hot_reload.py: a credential change restarts only the owning channel; a degraded channel's change is deferred, not dropped; a Slack token change logs the restart notice; closing Teams clears the late-bound webhook handler; a Teams restart superseded mid-connect never binds its webhook handler while an accepted one does; a lowercase Slack key on Windows still logs the notice.test/test_weixin_qr.py: a first sign-in records ownership, so deleting the line afterwards revokes the token in-process; a confirmed sign-in persists while the watcher is held and a cancelled one keeps the hold until the write finishes; a boot record from another.envfile is refused; on Windows a sign-in replaces a lowercaseweixin_tokenline and the rollback delete removes every spelling, while on POSIX both leave the lowercase name alone; a failed config write restores.envbyte for byte on both platforms, including an empty lowercase alias ahead of the working token and a UTF-8 BOM, and_read_env_valueskips an empty case spelling on Windows. The Windows rollback cases fail against the previous rollback.test/test_slack_config_handlers.py: on Windows a dashboard save replaces a lowercase spelling of the key, collapses several spellings to one line where the first was, and a clear removes every spelling; on POSIX a lowercase name is left untouched. The five Windows cases fail against the previous writers.test/test_env_credential_hot_reload.py(re-exec): the realreexec_launcherseam, withos.execvstubbed, hands the successor a marker naming both owned keys, the marker is never in the live process's own environment, and the successor's firstload_credentialsadopts it and pops both keys on a later removal; a re-exec with nothing owned, or after an owned key was discarded, still carries an empty marker. Both tests fail if the ownership record stops callingkeep_for_reexec.test/conftest_resets.py: the boot.envrecord and the ownership set are reset around every test, so neither leaks across an xdist worker. The_KEPT_FOR_REEXECreset now binds one fresh dict in bothplatform_compatandplatform_compat_runtime. Before, it rebound only the facade name, whilekeep_for_reexecand thereexec_*seams write to the runtime module's dict, so a value kept in one test could reach a later test's stubbed exec.test/test_teams_gateway.py:maybe_start_teamsregisters the transport but leaves the webhook handler unbound for the host.test/test_channel_env_write_off_loop.py: the dashboard writer honours the Docker credentials scrub.On head
a502a7be15(Python 3.12): squashed to one commit and rebased onto currentmain. Fixes three CI failures from the previous head: added_keep_env_owned_keys_for_reexec_lockedtoconfig.loader's facade manifest; the re-exec marker test now patchesIS_WINDOWSthrough theplatform_compatfacade instead of one owner module;main'stest_the_badge_never_breaks_bootnow expectsload_credentials(propagate=False), the post-boot read this PR introduces. Locally:TestUnreadyChannelBadgeand both facade-mirror guards pass; black/isort/flake8 clean; black gate passes. The earlier full local runs on383ae6675e(1555 passed) and46233c0224(177 passed) covered the same production code.Also on
a502a7be15: the WeChat sign-in rollback no longer overwrites the whole.envwith its snapshot. If nothing else touched the file since the sign-in's write, it puts the snapshot back byte for byte; otherwise it restores only theWEIXIN_TOKENentry's prior lines into the current file, keeping unrelated edits, and refuses (logged) if that entry itself was changed meanwhile. New tests:test_rollback_keeps_an_unrelated_edit_saved_after_the_token_write,test_rollback_refuses_when_the_token_entry_was_changed_independently; all 47 tests intest/test_weixin_qr.pypass.And on
a502a7be15: the rollback snapshot and the published text now both come from_write_env_secret's own single read (it returns(prior, published)), so the snapshot holds every edit the write kept and no read of.envruns between the token write and the rollback-protected config write. New tests:test_write_env_secret_returns_its_own_read_and_what_it_published,test_write_env_secret_on_an_absent_file_reports_no_prior,test_a_read_failure_after_the_token_write_cannot_escape_the_commit; all 50 tests intest/test_weixin_qr.pypass, and the 104 intest/test_env_file_bom.pyandtest/test_env_staged_publish.py.Fix on
a502a7be15: reworded one test docstring intest/test_weixin_qr.pyto present tense so the Comment History Gate passes. No code change.And on
a502a7be15: a.envedit to the Slack owner (KIROCREW_OWNER_ID) now logs the same restart-needed notice as a Slack token edit. The owner is read once at start into_owner_idand the owner allowlist, so a replaced or removed owner stays authorized until restart (GPT 6.1 finding atchannel_lifecycle.py:644). Covered bytest/test_channel_boot_hot_reload.py::TestSlackCredentialChangeWarns::test_an_owner_change_logs_the_restart_needed_notice.Rebased on
a502a7be15: onto currentmain(includes #23870, the macOStest_acp_elicitation_runtimefix). Still one commit; no change to this PR's diff.Fix on
a502a7be15: the WeChat sign-in.envwrites publish UTF-8 bytes instead of text, so Windows no longer newline-translates them. The rollback compares the file with what it published and restores the snapshot byte for byte; on Windows the text-mode write turned\ninto\r\n, so the comparison never matched and a CRLF snapshot would be written back as\r\r\n. Covered bytest/test_weixin_qr.py::test_atomic_write_secret_lands_byte_for_byteand::test_a_failed_commit_restores_crlf_line_endings_byte_for_byte; also rebased onto currentmain.Manual verification
N/A — the watcher cycle, applier, and channel restart dispatch are each driven end to end by the
ConfigWatch._cycle()tests above against a real temp.env, which is the path a hand edit takes.Related Issues
Fixes #22450
Sibling sweep
Swept: git grep -nE "load_credentials(|apply_env_credential_changes|os.environ[.*] *=" -- src/
Sibling: src/kiro_crew/dashboard/messaging_api/channel_config_writes.py -- changed here
Sibling: src/kiro_crew/slack/gateway_runtime/channel_lifecycle.py -- changed here
Sibling: src/kiro_crew/dashboard/source_providers/jira.py -- changed here
Sibling: src/kiro_crew/dashboard/upload_destination.py -- changed here
Swept (case-equivalent
.envspellings): git grep -nE 'split("=", 1)[0]|partition("=")' -- src/Sibling:
src/kiro_crew/dashboard/handlers/weixin_qr.py-- changed hereSibling:
src/kiro_crew/cli_setup.py-- tracked in #23768Swept (exec-successor hand-off): git grep -nE "keep_for_reexec|_KEPT_FOR_REEXEC|os.execv" -- src/
Sibling:
src/kiro_crew/config/loader_builders.py-- not affected -- already useskeep_for_reexec, the channel the ownership marker now sharesSibling:
src/kiro_crew/platform_compat_runtime.py-- not affected -- identical tomain; the only pre-exec write is the existingos.environ.update(_KEPT_FOR_REEXEC)Pattern harvest
Not generalizable: the defect was one missing watcher input (
.envalongsideconfig.json), not a repeated code shape a rule could catch.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)docs/system-specs/modules/config.mdContribution License Agreement
Contributed under the project's Apache-2.0 license.