preserve prefect.cli.* modules in reset_sys_modules fixture
The `reset_sys_modules` autouse fixture deletes any modules added to
`sys.modules` during a test. With lazy CLI loading, command modules
like `prefect.cli.events` are imported on-demand during test
invocations and then deleted after the test. However, cyclopts
caches the resolved App internally (`CommandSpec._resolved`), so
subsequent tests get a stale App whose function references point to
the deleted module. When monkeypatch patches the freshly re-imported
module, the stale cached App still uses the old (unpatched) functions.
Fix: skip `prefect.cli.*` modules during cleanup so cyclopts' cached
references remain valid.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
perf: gate EventsWorker flush on existing instance
Only call wait_until_empty() when an EventsWorker instance already
exists (i.e. events were actually emitted during the task). This avoids
instantiating a new EventsWorker and its associated orchestration client
for tasks that never emit events.
Co-authored-by: bot_apk <apk@cognition.ai>