Skip to content
This repository was archived by the owner on Sep 23, 2026. It is now read-only.

feat(telemetry): align events with TS schema, add trace_id and missing events - #2500

Merged
7Sageer merged 7 commits into
MoonshotAI:mainfrom
7Sageer:feat/telemetry-ts-alignment
Jul 16, 2026
Merged

7Sageer merged 7 commits into
MoonshotAI:mainfrom
7Sageer:feat/telemetry-ts-alignment

Conversation

@7Sageer

@7Sageer 7Sageer commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

N/A — no tracking issue. This aligns the Python telemetry surface with the
TS rewrite's event registry (agent-core-v2 events.ts).

Description

kosong

  • Kimi provider captures the x-trace-id response header via
    with_raw_response (stream + non-stream); trace_id is carried on
    APIStatusError (openai + httpx conversion paths)
  • StreamedMessage / GenerateResult / StepResult thread trace_id through
  • New on_trace_id callback fired as soon as response headers arrive
    (before streaming), so events emitted mid-stream see the current request

kimi-cli

  • Two-level current-trace holder (ContextVar per turn task + root mirror for
    UI) feeding trace_id into api_error, compaction_*, turn_interrupted,
    turn_ended, cancel, tool_call, tool_call_dedup_detected,
    tool_call_repeat, permission_approval_result, question_answered/dismissed
  • compaction_finished/failed renamed to TS property names
    (source/tokens_before/tokens_after/input_tokens/output_tokens),
    gain round/thinking_effort/compacted_count
  • api_error gains retryable (TS isRetryableGenerateError table, incl.
    408/409/529), provider_type/protocol, and overloaded (529); the non-TS
    api error_type folds into other
  • tool_call gains tool_call_id, cancelled outcome, trace_id;
    error_type becomes the TS enum ('error'|'cancelled') with the exception
    class moved to error_class
  • tool_call_dedup_detected re-added at same-step/cross-step detection
    (args_hash over canonical args)
  • turn_interrupted gains interrupt_reason; cancel gains from
    (streaming/compacting); question_answered gains answered
  • New events: turn_ended (unconditional at turn end) and
    permission_approval_result (all approval decision points; existing
    tool_approved/tool_rejected kept for compatibility)

Open questions for reviewers:

  • turn_id is intentionally NOT added: Python currently has uuid-hex turn
    ids while TS uses a per-session monotonic integer — needs a data-side
    decision before unifying the property.
  • Auto-approve paths (yolo/afk/session-cache hit) also emit
    permission_approval_result (TS only emits when an approval prompt
    resolves); flagged for data-team sign-off.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run make gen-changelog to update the changelog.
  • I have run make gen-docs to update the user documentation.

…g events

Align the Python telemetry surface with the TS rewrite's event registry
(agent-core-v2 events.ts), and attach the KFC x-trace-id response header
to request-scoped events so they can join with server request logs.

kosong:
- Kimi provider captures x-trace-id via with_raw_response (stream and
  non-stream); trace_id carried on APIStatusError (openai + httpx paths)
- StreamedMessage/GenerateResult/StepResult thread trace_id through
- New on_trace_id callback fired when response headers arrive (before
  streaming, so events emitted mid-stream see the current request)

kimi-cli:
- Two-level current-trace holder (ContextVar per turn task + root mirror
  for UI) feeding trace_id into api_error, compaction_*, turn_interrupted,
  turn_ended, cancel, tool_call, tool_call_dedup_detected, tool_call_repeat,
  permission_approval_result, question_answered/dismissed
- compaction_finished/failed renamed to TS property names
  (source/tokens_before/tokens_after/input_tokens/output_tokens) and gain
  round/thinking_effort/compacted_count
- api_error gains retryable (TS isRetryableGenerateError table, incl.
  408/409/529), provider_type/protocol and overloaded(529); the non-TS
  'api' error_type folds into 'other'
- tool_call gains tool_call_id, cancelled outcome and trace_id;
  error_type becomes the TS enum ('error'|'cancelled') with the exception
  class moved to error_class
- tool_call_dedup_detected re-added at same-step/cross-step detection
- turn_interrupted gains interrupt_reason; cancel gains from
  (streaming/compacting); question_answered gains answered
- New events: turn_ended (unconditional at turn end) and
  permission_approval_result (all approval decision points; existing
  tool_approved/tool_rejected kept for compatibility)
Copilot AI review requested due to automatic review settings July 15, 2026 03:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aligns the Python telemetry surface (in both kimi-cli and the kosong workspace package) with the TS rewrite’s event/schema expectations by adding trace_id propagation, enriching error/tool telemetry properties, and introducing TS-parity events (e.g., turn_ended, permission_approval_result).

Changes:

  • Thread x-trace-id from provider responses through kosong generate/step results into kimi-cli telemetry, plus add early on_trace_id callback support.
  • Update kimi-cli telemetry event shapes (e.g., api_error.retryable, tool_call enum-like error typing, compaction property renames) and add missing TS-aligned events.
  • Add/extend tests covering trace-id propagation, retryable classification, permission approval telemetry, and tool-call dedup telemetry.

Reviewed changes

Copilot reviewed 29 out of 29 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/telemetry/test_instrumentation.py Extends telemetry tests for 529 overload mapping, retryable classification, compaction property renames, trace id holder behavior, and turn_ended shape.
tests/core/test_toolset.py Adds tests for tool_call_dedup_detected telemetry and TS-aligned tool_call error/cancel outcomes.
tests/core/test_notifications.py Updates test provider stub to include new trace_id property.
tests/core/test_kimisoul_steer.py Updates test provider stub to include new trace_id property.
tests/core/test_kimisoul_retry_recovery.py Updates test provider stubs to include new trace_id property.
tests/core/test_kimisoul_repeat.py Updates test provider stub to include new trace_id property.
tests/core/test_kimisoul_ralph_loop.py Updates test provider stub to include new trace_id property.
tests/core/test_auth_error_handling.py Updates test provider stub to include new trace_id property.
tests/core/test_approval_telemetry.py New tests for permission_approval_result across yolo/afk/manual/session-cache paths.
src/kimi_cli/ui/shell/visualize/_live_view.py Emits trace_id on UI-surface telemetry (question_answered, question_dismissed, cancel) via root trace mirror.
src/kimi_cli/telemetry/init.py Introduces two-level trace id holder (ContextVar + root mirror) with getters/setters.
src/kimi_cli/soul/toolset.py Adds trace_id injection into tool telemetry, implements args_hash, and emits dedup/cancel/error fields aligned to TS.
src/kimi_cli/soul/kimisoul.py Adds 529 overload classification, is_retryable_api_error, turn_ended event, trace-id capture via on_trace_id, and compaction telemetry property shape changes.
src/kimi_cli/soul/compaction.py Threads trace_id through CompactionResult.
src/kimi_cli/soul/approval.py Emits TS-aligned permission_approval_result with duration/surface/mode/result fields (plus trace id when available).
packages/kosong/tests/test_trace_id.py New tests verifying x-trace-id capture and propagation (success, streaming, error, and early callback).
packages/kosong/src/kosong/contrib/chat_provider/openai_responses.py Adds trace_id property (None) for streamed message compatibility.
packages/kosong/src/kosong/contrib/chat_provider/openai_legacy.py Adds trace_id property (None) for streamed message compatibility.
packages/kosong/src/kosong/contrib/chat_provider/google_genai.py Adds trace_id property (None) for streamed message compatibility.
packages/kosong/src/kosong/contrib/chat_provider/anthropic.py Adds trace_id property (None) for streamed message compatibility.
packages/kosong/src/kosong/chat_provider/openai_common.py Captures x-trace-id into APIStatusError when converting OpenAI SDK errors.
packages/kosong/src/kosong/chat_provider/mock.py Adds trace_id property (None) to mock streamed message.
packages/kosong/src/kosong/chat_provider/kimi.py Uses with_raw_response to capture headers early and stores x-trace-id on KimiStreamedMessage.
packages/kosong/src/kosong/chat_provider/echo/scripted_echo.py Adds trace_id property (None) to echo provider streamed message.
packages/kosong/src/kosong/chat_provider/echo/echo.py Adds trace_id property (None) to echo provider streamed message.
packages/kosong/src/kosong/chat_provider/chaos.py Forwards trace_id from wrapped provider stream.
packages/kosong/src/kosong/chat_provider/init.py Extends StreamedMessage protocol with trace_id and adds trace_id to APIStatusError + httpx conversion.
packages/kosong/src/kosong/_generate.py Adds on_trace_id callback and propagates trace_id into GenerateResult.
packages/kosong/src/kosong/init.py Threads on_trace_id and trace_id through step() into StepResult.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/kimi_cli/soul/kimisoul.py Outdated
Comment on lines 1180 to 1182
from kimi_cli.telemetry import set_current_trace_id

return await kosong.step(
Comment on lines +1532 to +1536
thinking_effort=(
self._runtime.llm.chat_provider.thinking_effort or "unknown"
if self._runtime.llm is not None
else "unknown"
),
Comment on lines +1592 to +1596
thinking_effort=(
self._runtime.llm.chat_provider.thinking_effort or "unknown"
if self._runtime.llm is not None
else "unknown"
),
Comment thread src/kimi_cli/soul/kimisoul.py Outdated
Comment on lines +777 to +778
if self._current_turn_id:
_ended_kwargs["turn_id"] = self._current_turn_id
@7Sageer
7Sageer added this pull request to the merge queue Jul 16, 2026
Merged via the queue into MoonshotAI:main with commit 7c52e76 Jul 16, 2026
17 checks passed
@7Sageer
7Sageer deleted the feat/telemetry-ts-alignment branch July 16, 2026 09:03
NLPark-Cran referenced this pull request in NLPark-Cran/cran-code Aug 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants