This repository was archived by the owner on Sep 23, 2026. It is now read-only.
Repository navigation
feat(telemetry): align events with TS schema, add trace_id and missing events - #2500
Merged
Merged
Conversation
…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)
Contributor
There was a problem hiding this comment.
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-idfrom provider responses throughkosonggenerate/step results intokimi-clitelemetry, plus add earlyon_trace_idcallback support. - Update
kimi-clitelemetry event shapes (e.g.,api_error.retryable,tool_callenum-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 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 on lines
+777
to
+778
| if self._current_turn_id: | ||
| _ended_kwargs["turn_id"] = self._current_turn_id |
This was referenced Jul 15, 2026
This was referenced Jul 16, 2026
NLPark-Cran
referenced
this pull request
in NLPark-Cran/cran-code
Aug 18, 2026
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Related Issue
N/A — no tracking issue. This aligns the Python telemetry surface with the
TS rewrite's event registry (
agent-core-v2events.ts).Description
kosong
x-trace-idresponse header viawith_raw_response(stream + non-stream);trace_idis carried onAPIStatusError(openai + httpx conversion paths)StreamedMessage/GenerateResult/StepResultthreadtrace_idthroughon_trace_idcallback fired as soon as response headers arrive(before streaming), so events emitted mid-stream see the current request
kimi-cli
UI) feeding
trace_idintoapi_error,compaction_*,turn_interrupted,turn_ended,cancel,tool_call,tool_call_dedup_detected,tool_call_repeat,permission_approval_result,question_answered/dismissedcompaction_finished/failedrenamed to TS property names(
source/tokens_before/tokens_after/input_tokens/output_tokens),gain
round/thinking_effort/compacted_countapi_errorgainsretryable(TSisRetryableGenerateErrortable, incl.408/409/529),
provider_type/protocol, andoverloaded(529); the non-TSapierror_type folds intoothertool_callgainstool_call_id,cancelledoutcome,trace_id;error_typebecomes the TS enum ('error'|'cancelled') with the exceptionclass moved to
error_classtool_call_dedup_detectedre-added at same-step/cross-step detection(
args_hashover canonical args)turn_interruptedgainsinterrupt_reason;cancelgainsfrom(
streaming/compacting);question_answeredgainsansweredturn_ended(unconditional at turn end) andpermission_approval_result(all approval decision points; existingtool_approved/tool_rejectedkept for compatibility)Open questions for reviewers:
turn_idis intentionally NOT added: Python currently has uuid-hex turnids while TS uses a per-session monotonic integer — needs a data-side
decision before unifying the property.
permission_approval_result(TS only emits when an approval promptresolves); flagged for data-team sign-off.
Checklist
make gen-changelogto update the changelog.make gen-docsto update the user documentation.