kimi-code/packages/kosong
7Sageer 70211a727f
feat: attach trace_id to telemetry events (#1724)
* feat: attach KFC trace_id to telemetry events

Capture the x-trace-id response header in both kimi providers (kosong
and agent-core-v2 llmProtocol) via the openai SDK withResponse(), and
thread it through the request result chain into telemetry so events can
be joined with server-side request logs by trace id instead of
session_id + time window.

- kosong / llmProtocol: StreamedMessage/GenerateResult.traceId,
  GenerateOptions.onTraceId early capture (available mid-stream),
  APIStatusError.traceId on the error path
- v1 (agent-core): per-turn traceIdByTurn on TurnFlow; attach trace_id
  to turn_ended/turn_interrupted/api_error/cancel/tool_call(+dedup,
  repeat)/permission_approval_result/question_*/compaction_*
- v2 (agent-core-v2): AgentTelemetryContext carries turn_id/trace_id
  updated by llmRequester; registry gains trace_id on 13 events plus
  turn_id on turn_* and turn_id/step_no on api_error

* fix: attribute trace_id to the failed request on error paths

Review follow-ups for trace_id attribution on failure paths:

- v1: chatWithRetry feeds a failed attempt's APIStatusError trace id into
  the turn's traceIdByTurn before the loop dispatches turn.interrupted,
  so turn_ended/turn_interrupted on error turns attribute to the failed
  request instead of the previous successful step.
- v2: llmRequester keeps the onTraceId-captured trace in per-request
  state and trackApiError prefers it over error extraction, so failures
  after response headers arrived (empty response, mid-stream decode
  errors) no longer clear the ambient trace_id.
- v2: compaction_failed falls back to the ambient trace_id once a
  summarizer request actually hit the wire, covering mid-stream failures
  whose error carries no trace.
- kosong: parseTraceId filters empty x-trace-id headers and both kimi
  providers read the header through it.
- v2 test harness forwards onTraceId to GenerateFn so tests can simulate
  the early header capture.

* fix: attribute v1 api_error to the in-flight request on post-headers failures

- v1: a failure after response headers arrived (mid-stream decode error,
  empty response) carries no trace on the error itself, so api_error lost
  the trace the client had already captured via onTraceId. Add a per-step
  in-flight capture (written by onTraceId, cleared at step begin/end) and
  fall back to it when the error carries no trace, aligning with v2's
  per-request requestTraceId. Failures before any response headers
  (network errors, local aborts) still report no trace rather than
  leaking a previous request's.
- v2: document that ambient trace_id distribution assumes serialized LLM
  requests per agent, naming the two supported paths that break it
  (after-step compaction below the block ratio; inject-path turns during
  a manual compaction).

* fix: isolate telemetry trace attribution

* fix: isolate telemetry trace by request

* fix: declare @moonshot-ai/protocol dependency of agent-core-v2

The trace-by-request refactor imports types from @moonshot-ai/protocol
in toolContract and toolExecutorService, but the package was not
declared, failing clean CI installs (TS2307). Also drop three type
imports that are unused after the merge with main.
2026-07-16 09:14:12 +08:00
..
src feat: attach trace_id to telemetry events (#1724) 2026-07-16 09:14:12 +08:00
test feat: attach trace_id to telemetry events (#1724) 2026-07-16 09:14:12 +08:00
CHANGELOG.md ci: release packages (#1546) 2026-07-12 20:28:38 +08:00
package.json chore: drop #/ import array fallbacks and custom resolution plugins (#1594) 2026-07-13 16:37:35 +08:00
README.md Kimi For Coding 2026-05-22 15:54:50 +08:00
tsconfig.json Kimi For Coding 2026-05-22 15:54:50 +08:00
tsconfig.type-negative.json Kimi For Coding 2026-05-22 15:54:50 +08:00
tsdown.config.ts fix: align Anthropic-compatible model capabilities (#1746) 2026-07-16 01:31:38 +08:00
vitest.config.ts Kimi For Coding 2026-05-22 15:54:50 +08:00

@moonshot-ai/kosong

LLM abstraction layer used by Kimi Code.

Part of the Kimi Code monorepo.

See the main repository for documentation, issues, and contribution guidelines.

License

MIT