Skip to content

fix(agent-observability): degrade gracefully when opentelemetry is missing - #29

Merged
alberthaolihai merged 1 commit into
mainfrom
claude/loopforge-issue-17-otel-fix
Sep 17, 2026
Merged

alberthaolihai merged 1 commit into
mainfrom
claude/loopforge-issue-17-otel-fix

Conversation

@alberthaolihai

Copy link
Copy Markdown
Collaborator

Summary

  • core/agentlens/runtime.py imported opentelemetry at module scope with no guard, so any environment without the (optional) opentelemetry package crashed every agent-observability hook phase (SessionStart/UserPromptSubmit/PreToolUse/PostToolUse/Stop) at import time — before the existing try/except error handling in main()/runtime.main() ever got a chance to run.
  • Fresh installs hit this reliably: run_hook.sh falls back to system python3 whenever the project .venv lacks zhiyanllm, and system python3 typically doesn't have opentelemetry installed — matching the traceback reported in [Bug]: ⚠ Hook SessionStart [warning] #17.
  • Wrapped the import in try/except, consistent with how the rest of agentlens already treats opentelemetry/zhiyanllm as optional (bootstrap.load_runtime, tracing.generate_turn_carrier). The two call sites for _otel_trace are only reached once bootstrap.ensure_initialized has confirmed the runtime is available, so this is a no-op on the happy path.
  • Regenerated the .cursor/.claude Classic host bundles via scripts/build-classic-hosts.py --write so all three editions stay in sync with the .codebuddy source.

This supersedes #28, which was opened from a stale branch that also carried unrelated, already-superseded commits and conflicted with main. This branch is based directly on current main and only touches the 3 files above.

Fixes #17

Test plan

  • Reproduced the exact issue [Bug]: ⚠ Hook SessionStart [warning] #17 traceback against the pre-fix code using a clean venv with no opentelemetry installed.
  • Confirmed the fix: same clean venv, all 5 hook phases (session-start/user-prompt-submit/pre/post/stop) now exit 0 with no traceback.
  • pytest tests/ — 123 passed; the 6 pre-existing failures (test_cls_sink.py, test_collector.py) reproduce identically on the pre-fix code, confirming they're unrelated environment-dependent baseline failures, not regressions.
  • python3 scripts/build-classic-hosts.py --check — Classic bundles match generator.
  • bash scripts/validate.sh — passed (6/6).
  • bash scripts/smoke-install.sh — passed.
  • bash scripts/scan-secrets.sh — blocked; the bundled helper only supports Linux x86_64 and this was run on macOS.

🤖 Generated with Claude Code

…ssing

core/agentlens/runtime.py imported opentelemetry at module scope with no
guard, so any environment without the (optional) opentelemetry package
crashed every agent-observability hook phase at import time, before the
existing try/except-based error handling in main()/runtime.main() could
ever run. Fresh installs typically hit this because run_hook.sh falls
back to system python3 when the project .venv lacks zhiyanllm, and
system python3 rarely has opentelemetry installed.

Wrap the import in try/except, consistent with how the rest of the
agentlens package already treats opentelemetry/zhiyanllm as optional
(bootstrap.load_runtime, tracing.generate_turn_carrier). The two call
sites for _otel_trace are only reached once bootstrap.ensure_initialized
has confirmed the runtime is available, so this is a no-op on the happy
path.

Regenerated the .cursor/.claude Classic host bundles via
scripts/build-classic-hosts.py --write to keep them in sync with the
.codebuddy source.

Fixes #17

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@alberthaolihai
alberthaolihai merged commit 60af49f into main Sep 17, 2026
5 checks passed
@alberthaolihai
alberthaolihai deleted the claude/loopforge-issue-17-otel-fix branch September 17, 2026 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: ⚠ Hook SessionStart [warning]

1 participant