ms-tau-sdk 3.0.3.dev104__tar.gz → 3.0.4.dev105__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/CHANGELOG.md +28 -0
  2. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/PKG-INFO +1 -1
  3. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/pyproject.toml +1 -1
  4. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/agent_skills/tau_security_and_access/SKILL.md +8 -2
  5. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/client.py +14 -2
  6. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/logging.py +87 -20
  7. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/protocols/caller_context.py +54 -3
  8. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/manager.py +23 -12
  9. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/observability.py +55 -11
  10. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/requester.py +8 -1
  11. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/.gitignore +0 -0
  12. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/README.md +0 -0
  13. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/__init__.py +0 -0
  14. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/app.py +0 -0
  15. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/cli.py +0 -0
  16. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/config.py +0 -0
  17. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/env_file.py +0 -0
  18. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/logs.py +0 -0
  19. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/proxy.py +0 -0
  20. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/state.py +0 -0
  21. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/static/BULMA-LICENSE.txt +0 -0
  22. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/static/app.js +0 -0
  23. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/static/board.css +0 -0
  24. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/static/bulma.min.css +0 -0
  25. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/packages/tau-board/src/ms_tau_board/static/index.html +0 -0
  26. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/__init__.py +0 -0
  27. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/agent_skills/design-agent-icons-and-motion/SKILL.md +0 -0
  28. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/agent_skills/design-agent-icons-and-motion/agents/openai.yaml +0 -0
  29. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/agent_skills/design-agent-icons-and-motion/references/metatables-example.html +0 -0
  30. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/agent_skills/tau_a2a_runtime_adapter/SKILL.md +0 -0
  31. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/agent_skills/tau_local_development/SKILL.md +0 -0
  32. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/agent_skills/tau_project_customization/SKILL.md +0 -0
  33. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/agent_skills/tau_repository_integration/SKILL.md +0 -0
  34. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/__init__.py +0 -0
  35. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/a2a.py +0 -0
  36. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/chat.py +0 -0
  37. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/conversations.py +0 -0
  38. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/dependencies.py +0 -0
  39. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/health.py +0 -0
  40. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/inspection.py +0 -0
  41. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/local_chat.py +0 -0
  42. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/models.py +0 -0
  43. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/request_identity.py +0 -0
  44. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/api/sessions.py +0 -0
  45. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/app.py +0 -0
  46. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/application.py +0 -0
  47. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/__init__.py +0 -0
  48. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/assertions.py +0 -0
  49. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/auth.py +0 -0
  50. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/hydrate_endpoint.py +0 -0
  51. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/local.py +0 -0
  52. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/mcp.py +0 -0
  53. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/models.py +0 -0
  54. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/release_access.py +0 -0
  55. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/release_readiness.py +0 -0
  56. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/routes.py +0 -0
  57. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/backend/team_memberships.py +0 -0
  58. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/cli.py +0 -0
  59. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/errors.py +0 -0
  60. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/model_relay/__init__.py +0 -0
  61. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/model_relay/component.py +0 -0
  62. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/model_relay/contract.py +0 -0
  63. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/protocols/__init__.py +0 -0
  64. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/protocols/a2a_failure.py +0 -0
  65. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/protocols/a2a_message.py +0 -0
  66. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/protocols/a2a_roles.py +0 -0
  67. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/protocols/assistant_ui.py +0 -0
  68. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/protocols/chat_history.py +0 -0
  69. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/protocols/strict_json.py +0 -0
  70. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/providers/__init__.py +0 -0
  71. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/providers/definitions.py +0 -0
  72. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/providers/factory.py +0 -0
  73. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/providers/tau_compat.py +0 -0
  74. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/resources/SYSTEM.md +0 -0
  75. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/resources/__init__.py +0 -0
  76. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/resources/context_compat.py +0 -0
  77. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/resources/loader.py +0 -0
  78. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/resources/prompts/review-code-repository.md +0 -0
  79. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/resources/selection.py +0 -0
  80. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/__init__.py +0 -0
  81. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/deployment_health.py +0 -0
  82. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/events.py +0 -0
  83. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/extensions.py +0 -0
  84. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/failures.py +0 -0
  85. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/live_turns.py +0 -0
  86. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/provenance.py +0 -0
  87. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/session.py +0 -0
  88. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/snapshots.py +0 -0
  89. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/runtime/task_context.py +0 -0
  90. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/sessions/__init__.py +0 -0
  91. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/sessions/storage.py +0 -0
  92. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/settings.py +0 -0
  93. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/skills.py +0 -0
  94. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/tools/__init__.py +0 -0
  95. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/tools/mainsequence_mcp.py +0 -0
  96. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/tools/mcp_applications.py +0 -0
  97. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/tools/mcp_connection.py +0 -0
  98. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/tools/skill_read.py +0 -0
  99. {ms_tau_sdk-3.0.3.dev104 → ms_tau_sdk-3.0.4.dev105}/src/ms_tau_sdk/tools/task_control.py +0 -0
@@ -1,5 +1,33 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.0.4 — 2026-11-26
4
+
5
+ - **The Agent's hops are spans of the caller's trace.** A trace is the set of runtime log lines
6
+ that share one W3C trace id, and each lifecycle's terminal line is its span
7
+ ([ADR 0031](docs/adrs/0031-the-agents-hops-in-a-platform-trace.md)). The runtime used to forward
8
+ the caller's `traceparent` unchanged and give a run, its turn and its model and tool calls the
9
+ request's `span_id`, so a trace showed the Agent as one hop. Now:
10
+ - **The request's span is the current span.** The calls made for a request name the runtime's
11
+ own request span as their parent, and the request's `traceparent` is only that span's recorded
12
+ `parent_span_id`. A request without a `traceparent` starts a trace that is sent on with the
13
+ sampled flag `01`, and its `http.request.*` line says `trace_sampled: true`.
14
+ - **Each lifecycle is its own span.** The run, the turn, each model call attempt, each tool call,
15
+ each handoff and each platform or application call attempt gets its own `span_id`, with the
16
+ enclosing span as its `parent_span_id`. A tool's Main Sequence MCP calls, A2A sends included,
17
+ name the tool call's span, and each platform call names its own. A Task attempt and other
18
+ detached work stay in the span that started them, since they write no terminal line of their
19
+ own: a dispatched Task's run is a child of the dispatch request.
20
+ - `agent.model.completed` and `.failed` also carry `duration_ms`, equal to `model_duration_ms`.
21
+ - **Nothing else changes.** No log line is added, every uid and everything persisted is
22
+ unchanged, and there is still no exporter, `TracerProvider` or setting. Model providers, the
23
+ model relay, provider credential hydration and lease renewals are still sent no `traceparent`
24
+ or `baggage`.
25
+ - **The `logging.googleapis.com/trace` fields are gone.** They were written only when
26
+ `GOOGLE_CLOUD_PROJECT` was set and a line held an `otelTraceID`, which nothing set.
27
+
28
+ See [Distributed tracing](docs/reference/runtime-contract.md#distributed-tracing). Tested with the
29
+ runtime's own requests, turns and calls; not tried against the platform's trace view.
30
+
3
31
  ## 3.0.3 — 2026-11-20
4
32
 
5
33
  - **Where the platform's model relay is.** The runtime contract, Security and access and
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: ms-tau-sdk
3
- Version: 3.0.3.dev104
3
+ Version: 3.0.4.dev105
4
4
  Summary: Workspace-bound Tau application primitives for Main Sequence projects
5
5
  Project-URL: Changelog, https://github.com/mainsequence-sdk/ms-tau-sdk/blob/development/CHANGELOG.md
6
6
  Project-URL: Documentation, https://github.com/mainsequence-sdk/ms-tau-sdk/tree/development/docs
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "ms-tau-sdk"
7
- version = "3.0.3.dev104"
7
+ version = "3.0.4.dev105"
8
8
  description = "Workspace-bound Tau application primitives for Main Sequence projects"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.13"
@@ -362,8 +362,14 @@ person = baggage.get_baggage("mainsequence.advisor.person") # None when the wor
362
362
  - It is public. The runtime passes it on with every platform and application call, so never put a
363
363
  secret in it, and know that an application that trusts this Agent's context trusts everyone who
364
364
  may give the Agent work.
365
-
366
- The SDK Security and access guide's **Caller context** section has the full rules.
365
+ - The Agent's hops are spans of the caller's trace. The runtime records the request, run, turn,
366
+ and each model, tool and platform call as its own log lines, and a call a tool makes names the
367
+ tool call's span as its parent. A tool adds nothing for that, and a `traceparent` it sets on a
368
+ `platform_client()` call replaces the runtime's, so leave it unset. Model calls carry no
369
+ `traceparent` or `baggage`.
370
+
371
+ The SDK Security and access guide's **Caller context** section has the full rules, and the runtime
372
+ contract's **Distributed tracing** section lists the spans.
367
373
 
368
374
  An application that owns sessions for its own people can name each session's person on the
369
375
  platform, and the runtime then tells the application which session a tool call serves. If you are
@@ -24,6 +24,7 @@ from ms_tau_sdk.errors import (
24
24
  LocalModeUnsupportedError,
25
25
  SessionNotFoundError,
26
26
  )
27
+ from ms_tau_sdk.logging import TraceSpan
27
28
  from ms_tau_sdk.protocols.a2a_roles import platform_message_to_binding
28
29
  from ms_tau_sdk.protocols.caller_context import outbound_headers
29
30
  from ms_tau_sdk.settings import TauSDKSettings
@@ -374,6 +375,10 @@ class MainSequenceClient:
374
375
  attempt_started_at = time.monotonic()
375
376
  attempt_number = transport_attempt + 1
376
377
  operation = _dependency_operation(method, path)
378
+ # Each attempt is a span, the parent it names to the platform (ADR 0031).
379
+ span = TraceSpan()
380
+ with span:
381
+ caller_context = outbound_headers(headers or ())
377
382
  try:
378
383
  response = await self._client.request(
379
384
  method,
@@ -381,7 +386,7 @@ class MainSequenceClient:
381
386
  # The caller context of the work goes on with the call; a header the call
382
387
  # sets wins.
383
388
  headers={
384
- **outbound_headers(headers or ()),
389
+ **caller_context,
385
390
  **(headers or {}),
386
391
  **await self.auth.headers(force=force_auth),
387
392
  },
@@ -398,6 +403,7 @@ class MainSequenceClient:
398
403
  logger.info(
399
404
  "dependency.call.completed",
400
405
  message="Backend dependency call completed",
406
+ **span.log_fields,
401
407
  dependency_operation=operation,
402
408
  target_system="mainsequence_backend",
403
409
  dependency_attempt=attempt_number,
@@ -454,6 +460,7 @@ class MainSequenceClient:
454
460
  logger.warning(
455
461
  "dependency.call.failed",
456
462
  message="Backend dependency call failed",
463
+ **span.log_fields,
457
464
  dependency_operation=operation,
458
465
  target_system="mainsequence_backend",
459
466
  dependency_attempt=attempt_number,
@@ -822,6 +829,9 @@ class MainSequenceClient:
822
829
  while True:
823
830
  started_at = time.monotonic()
824
831
  failure = ""
832
+ span = TraceSpan()
833
+ with span:
834
+ caller_context = outbound_headers(headers or ())
825
835
  try:
826
836
  response = await self._client.request(
827
837
  method,
@@ -832,7 +842,7 @@ class MainSequenceClient:
832
842
  data=data,
833
843
  files=files,
834
844
  headers={
835
- **outbound_headers(headers or ()),
845
+ **caller_context,
836
846
  **dict(headers or {}),
837
847
  **delegation,
838
848
  **await self.auth.headers(force=force_auth),
@@ -845,6 +855,7 @@ class MainSequenceClient:
845
855
  logger.warning(
846
856
  "dependency.call.failed",
847
857
  message="Platform call failed",
858
+ **span.log_fields,
848
859
  dependency_operation=operation,
849
860
  target_system="mainsequence_backend",
850
861
  requester_bound=proof is not None,
@@ -857,6 +868,7 @@ class MainSequenceClient:
857
868
  logger.info(
858
869
  "dependency.call.completed",
859
870
  message="Platform call completed",
871
+ **span.log_fields,
860
872
  dependency_operation=operation,
861
873
  target_system="mainsequence_backend",
862
874
  requester_bound=proof is not None,
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import asyncio
6
+ import contextlib
6
7
  import json
7
8
  import logging
8
9
  import os
@@ -23,12 +24,18 @@ from structlog.contextvars import (
23
24
  bind_contextvars,
24
25
  clear_contextvars,
25
26
  get_contextvars,
27
+ reset_contextvars,
26
28
  unbind_contextvars,
27
29
  )
28
30
  from structlog.typing import EventDict, Processor, WrappedLogger
29
31
 
30
32
  from ms_tau_sdk import __version__
31
- from ms_tau_sdk.protocols.caller_context import recorded_baggage
33
+ from ms_tau_sdk.protocols.caller_context import (
34
+ caller_context_scope,
35
+ recorded_baggage,
36
+ span_scope,
37
+ trace_parent_context,
38
+ )
32
39
 
33
40
  SENSITIVE_EXACT_KEYS = {
34
41
  "api_key",
@@ -162,6 +169,73 @@ def safe_log(
162
169
  return
163
170
 
164
171
 
172
+ def new_span_id() -> str:
173
+ """A W3C span id: 16 lowercase hex digits."""
174
+
175
+ return uuid.uuid4().hex[:16]
176
+
177
+
178
+ class TraceSpan:
179
+ """One span of the current trace, a child of the span the work is in (ADR 0031).
180
+
181
+ A span exists only inside a trace: work whose log context holds no ``trace_id`` and
182
+ ``span_id``, such as a lease renewal, gets none, and its ``log_fields`` are empty. While the
183
+ span is open, the work's log lines carry it as ``span_id`` with the enclosing span as
184
+ ``parent_span_id``, and it is the current OpenTelemetry span, so the calls made inside it name
185
+ it as their parent. The terminal log line of the lifecycle it covers records it.
186
+ """
187
+
188
+ __slots__ = ("_sampled", "_scope", "_tokens", "parent_span_id", "span_id", "trace_id")
189
+
190
+ def __init__(self) -> None:
191
+ context = get_contextvars()
192
+ trace_id = context.get("trace_id")
193
+ parent_span_id = context.get("span_id")
194
+ traced = isinstance(trace_id, str) and isinstance(parent_span_id, str)
195
+ self.trace_id = trace_id if traced else None
196
+ self.parent_span_id = parent_span_id if traced else None
197
+ self.span_id = new_span_id() if traced else None
198
+ self._sampled = context.get("trace_sampled") is not False
199
+ self._scope: contextlib.ExitStack | None = None
200
+ self._tokens: Mapping[str, Any] | None = None
201
+
202
+ @property
203
+ def log_fields(self) -> dict[str, str]:
204
+ """The span's ``span_id`` and ``parent_span_id``, for a line written outside the span."""
205
+
206
+ if self.span_id is None or self.parent_span_id is None:
207
+ return {}
208
+ return {"span_id": self.span_id, "parent_span_id": self.parent_span_id}
209
+
210
+ def open(self) -> TraceSpan:
211
+ """Make the span the one the work is in, until ``close()``."""
212
+
213
+ fields = self.log_fields
214
+ if not fields or self._scope is not None or self.trace_id is None:
215
+ return self
216
+ scope = contextlib.ExitStack()
217
+ scope.enter_context(span_scope(self.trace_id, fields["span_id"], sampled=self._sampled))
218
+ self._scope = scope
219
+ self._tokens = bind_contextvars(**fields)
220
+ return self
221
+
222
+ def close(self) -> None:
223
+ """Return the work to the enclosing span. Closing twice, or unopened, does nothing."""
224
+
225
+ scope, tokens = self._scope, self._tokens
226
+ self._scope = self._tokens = None
227
+ if tokens is not None:
228
+ reset_contextvars(**tokens)
229
+ if scope is not None:
230
+ scope.close()
231
+
232
+ def __enter__(self) -> TraceSpan:
233
+ return self.open()
234
+
235
+ def __exit__(self, *_exc_info: object) -> None:
236
+ self.close()
237
+
238
+
165
239
  def _bind_context(
166
240
  _logger: WrappedLogger,
167
241
  _method_name: str,
@@ -188,22 +262,6 @@ def _add_caller_context(
188
262
  return event_dict
189
263
 
190
264
 
191
- def _add_google_cloud_trace_fields(
192
- _logger: WrappedLogger,
193
- _method_name: str,
194
- event_dict: EventDict,
195
- ) -> EventDict:
196
- trace_id = event_dict.pop("otelTraceID", None)
197
- span_id = event_dict.pop("otelSpanID", None)
198
- sampled = event_dict.pop("otelTraceSampled", False)
199
- project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
200
- if project_id and trace_id and trace_id != "0" and span_id and span_id != "0":
201
- event_dict["logging.googleapis.com/trace"] = f"projects/{project_id}/traces/{trace_id}"
202
- event_dict["logging.googleapis.com/spanId"] = span_id
203
- event_dict["logging.googleapis.com/trace_sampled"] = sampled
204
- return event_dict
205
-
206
-
207
265
  def _add_source(
208
266
  _logger: WrappedLogger,
209
267
  _method_name: str,
@@ -365,7 +423,6 @@ def _common_processors() -> list[Processor]:
365
423
  structlog.processors.TimeStamper(fmt="iso", utc=True),
366
424
  _bind_context,
367
425
  _add_caller_context,
368
- _add_google_cloud_trace_fields,
369
426
  structlog.dev.set_exc_info,
370
427
  _safe_exception,
371
428
  structlog.processors.StackInfoRenderer(),
@@ -529,7 +586,8 @@ def _trace_context(scope: Scope) -> tuple[str, str | None, bool]:
529
586
  match = TRACEPARENT_PATTERN.fullmatch(raw)
530
587
  if match and match.group(1) != "0" * 32 and match.group(2) != "0" * 16:
531
588
  return match.group(1), match.group(2), bool(int(match.group(3), 16) & 1)
532
- return uuid.uuid4().hex, None, False
589
+ # A trace the runtime starts is sampled: it records every span of it (ADR 0031).
590
+ return uuid.uuid4().hex, None, True
533
591
 
534
592
 
535
593
  def _route_path(scope: Scope) -> str:
@@ -636,7 +694,7 @@ class RequestContextMiddleware:
636
694
  clear_contextvars()
637
695
  request_id = _request_id(scope)
638
696
  trace_id, parent_span_id, trace_sampled = _trace_context(scope)
639
- span_id = uuid.uuid4().hex[:16]
697
+ span_id = new_span_id()
640
698
  state = scope.setdefault("state", {})
641
699
  state["request_id"] = request_id
642
700
  state["trace_id"] = trace_id
@@ -841,6 +899,12 @@ class RequestContextMiddleware:
841
899
 
842
900
  await send(message)
843
901
 
902
+ # The request's span is the current span, so the calls made for it name it as their parent
903
+ # and the request's ``traceparent`` is its parent (ADR 0031).
904
+ request_span = contextlib.ExitStack()
905
+ headers = scope.get("headers", ())
906
+ request_span.enter_context(caller_context_scope(trace_parent_context(headers)))
907
+ request_span.enter_context(span_scope(trace_id, span_id, sampled=trace_sampled))
844
908
  try:
845
909
  await self.app(scope, receive_with_context, send_with_context)
846
910
  log_terminal("http.request.completed", terminal_fields())
@@ -861,14 +925,17 @@ class RequestContextMiddleware:
861
925
  raise
862
926
  finally:
863
927
  self.active_requests -= 1
928
+ request_span.close()
864
929
  clear_contextvars()
865
930
 
866
931
 
867
932
  __all__ = [
868
933
  "RequestContextMiddleware",
934
+ "TraceSpan",
869
935
  "bind_request_identity_log_fields",
870
936
  "bind_request_log_fields",
871
937
  "configure_logging",
872
938
  "conversation_log_fields",
939
+ "new_span_id",
873
940
  "safe_log",
874
941
  ]
@@ -10,6 +10,10 @@ The context of the request or Task the runtime serves is OpenTelemetry's current
10
10
  code reads it with ``opentelemetry.baggage``, and the runtime's calls to the platform and to
11
11
  applications carry it on. It is a claim, worth as much as the trust in the caller that delivered
12
12
  it, and it is never Main Sequence identity.
13
+
14
+ The current span is the runtime's own: the span of the request, run, turn, tool call or call the
15
+ work is in (ADR 0031). It is a non-recording span, because the runtime records its spans as the
16
+ terminal log lines it writes, and the calls made inside it name it as their parent.
13
17
  """
14
18
 
15
19
  from __future__ import annotations
@@ -23,6 +27,7 @@ from opentelemetry import baggage, trace
23
27
  from opentelemetry import context as otel_context
24
28
  from opentelemetry.baggage.propagation import W3CBaggagePropagator
25
29
  from opentelemetry.context import Context
30
+ from opentelemetry.trace import NonRecordingSpan, SpanContext, TraceFlags
26
31
  from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
27
32
 
28
33
  _baggage_propagator = W3CBaggagePropagator()
@@ -79,18 +84,64 @@ def request_context(headers: Iterable[tuple[bytes, bytes]]) -> Context:
79
84
  """The caller context one inbound request or Task dispatch carries, from its headers.
80
85
 
81
86
  Its baggage is exactly the request's ``baggage`` header: never the process's or another
82
- request's, and none without the header. Its trace parent is the request's ``traceparent``,
83
- unless instrumentation already records a span for the request.
87
+ request's, and none without the header. Its span is the current span, which is the request's
88
+ own: the request's ``traceparent`` is then only that span's recorded parent. With no current
89
+ span, as for a WebSocket, the ``traceparent`` is the trace parent.
84
90
  """
85
91
 
86
92
  carrier = _carrier(headers)
87
93
  context = baggage.clear(context=otel_context.get_current())
88
94
  context = _with_baggage_header(carrier.get("baggage"), context)
89
- if not trace.get_current_span(context).is_recording():
95
+ if not trace.get_current_span(context).get_span_context().is_valid:
90
96
  context = _trace_propagator.extract(carrier, context=context)
91
97
  return context
92
98
 
93
99
 
100
+ def trace_parent_context(headers: Iterable[tuple[bytes, bytes]]) -> Context:
101
+ """The current context, with the request's ``traceparent`` and ``tracestate`` as its span.
102
+
103
+ Instrumentation that already records a span for the request keeps it.
104
+ """
105
+
106
+ context = otel_context.get_current()
107
+ if trace.get_current_span(context).is_recording():
108
+ return context
109
+ return _trace_propagator.extract(_carrier(headers), context=context)
110
+
111
+
112
+ @contextmanager
113
+ def span_scope(trace_id: str, span_id: str, *, sampled: bool = True) -> Iterator[None]:
114
+ """Make span ``span_id`` of trace ``trace_id`` the current span until the block ends.
115
+
116
+ The span keeps the sampled flag and ``tracestate`` of the current span when that one is in the
117
+ same trace, and otherwise is sampled as ``sampled`` says. A span that instrumentation records
118
+ stays current, and ids that are not W3C trace and span ids change nothing.
119
+ """
120
+
121
+ current = trace.get_current_span()
122
+ try:
123
+ trace_value, span_value = int(trace_id, 16), int(span_id, 16)
124
+ except (TypeError, ValueError):
125
+ trace_value = span_value = 0
126
+ if current.is_recording() or not 0 < trace_value < 2**128 or not 0 < span_value < 2**64:
127
+ yield
128
+ return
129
+ enclosing = current.get_span_context()
130
+ if enclosing.is_valid and enclosing.trace_id == trace_value:
131
+ flags, state = enclosing.trace_flags, enclosing.trace_state
132
+ else:
133
+ flags = TraceFlags(TraceFlags.SAMPLED if sampled else TraceFlags.DEFAULT)
134
+ state = None
135
+ span = NonRecordingSpan(
136
+ SpanContext(trace_value, span_value, is_remote=False, trace_flags=flags, trace_state=state)
137
+ )
138
+ token = otel_context.attach(trace.set_span_in_context(span))
139
+ try:
140
+ yield
141
+ finally:
142
+ otel_context.detach(token)
143
+
144
+
94
145
  def outbound_headers(present: Iterable[str] = ()) -> dict[str, str]:
95
146
  """The W3C ``traceparent``, ``tracestate`` and ``baggage`` headers of the current context.
96
147
 
@@ -57,7 +57,7 @@ from ms_tau_sdk.errors import (
57
57
  ToolValidationError,
58
58
  ToolWorkbenchError,
59
59
  )
60
- from ms_tau_sdk.logging import conversation_log_fields
60
+ from ms_tau_sdk.logging import TraceSpan, conversation_log_fields
61
61
  from ms_tau_sdk.protocols.caller_context import caller_context_scope
62
62
  from ms_tau_sdk.providers.factory import ProviderFactory
63
63
  from ms_tau_sdk.resources.loader import tau_resource_paths
@@ -1482,12 +1482,16 @@ class SessionRuntimeManager:
1482
1482
  agent_uid = await self._session_agent_uid(session_uid)
1483
1483
  agent_run_uid = str(uuid.uuid4())
1484
1484
  resolved_turn_uid = turn_uid or str(uuid.uuid4())
1485
- with bound_contextvars(
1486
- session_uid=session_uid,
1487
- agent_session_uid=session_uid,
1488
- agent_run_uid=agent_run_uid,
1489
- turn_uid=resolved_turn_uid,
1490
- agent_uid=agent_uid,
1485
+ with (
1486
+ bound_contextvars(
1487
+ session_uid=session_uid,
1488
+ agent_session_uid=session_uid,
1489
+ agent_run_uid=agent_run_uid,
1490
+ turn_uid=resolved_turn_uid,
1491
+ agent_uid=agent_uid,
1492
+ ),
1493
+ # The run's span: its ``agent.run.*`` lines record it (ADR 0031).
1494
+ TraceSpan(),
1491
1495
  ):
1492
1496
  async for event in self._prompt_with_context(
1493
1497
  session_uid,
@@ -1749,6 +1753,11 @@ class SessionRuntimeManager:
1749
1753
  model=runtime.model,
1750
1754
  is_streaming=True,
1751
1755
  )
1756
+ limit_at = asyncio.get_running_loop().time() + self.settings.turn_timeout_seconds
1757
+ turn_limit = asyncio.timeout_at(limit_at if deadline is None else min(limit_at, deadline))
1758
+ requester_token = bind_turn_requester(requester_binding)
1759
+ # The turn's span, inside the run's: its ``agent.turn.*`` lines record it (ADR 0031).
1760
+ turn_span = TraceSpan().open()
1752
1761
  logger.info(
1753
1762
  "agent.turn.started",
1754
1763
  message="Started Tau turn",
@@ -1758,9 +1767,6 @@ class SessionRuntimeManager:
1758
1767
  model=runtime.model,
1759
1768
  is_streaming=True,
1760
1769
  )
1761
- limit_at = asyncio.get_running_loop().time() + self.settings.turn_timeout_seconds
1762
- turn_limit = asyncio.timeout_at(limit_at if deadline is None else min(limit_at, deadline))
1763
- requester_token = bind_turn_requester(requester_binding)
1764
1770
  try:
1765
1771
  try:
1766
1772
  async with turn_limit:
@@ -1848,11 +1854,13 @@ class SessionRuntimeManager:
1848
1854
  ),
1849
1855
  **observer.terminal_fields(),
1850
1856
  }
1857
+ observer.close()
1851
1858
  log(
1852
1859
  f"agent.turn.{terminal_event}",
1853
1860
  message="Finished Tau turn",
1854
1861
  **terminal_fields,
1855
1862
  )
1863
+ turn_span.close()
1856
1864
  log(
1857
1865
  f"agent.run.{terminal_event}",
1858
1866
  message="Finished Tau agent run",
@@ -2193,8 +2201,11 @@ class SessionRuntimeManager:
2193
2201
  operation_uid=operation_uid or str(uuid.uuid4()),
2194
2202
  causation_event_id=parent.get("causation_event_id"),
2195
2203
  origin_request_id=parent.get("request_id"),
2196
- parent_span_id=parent.get("span_id"),
2197
- span_id=uuid.uuid4().hex[:16],
2204
+ # Detached work writes no terminal line of its own, so it is no span of its own: the
2205
+ # spans inside it are children of the span that started it (ADR 0031).
2206
+ span_id=parent.get("span_id"),
2207
+ parent_span_id=parent.get("parent_span_id"),
2208
+ trace_sampled=parent.get("trace_sampled"),
2198
2209
  )
2199
2210
 
2200
2211
  async def run_detached() -> object:
@@ -1,4 +1,10 @@
1
- """Content-free lifecycle instrumentation for translated Tau events."""
1
+ """Content-free lifecycle instrumentation for translated Tau events.
2
+
3
+ Each model call attempt, tool call and handoff is its own span, a child of the span it starts in,
4
+ which is the turn's (ADR 0031). Its terminal line records it. A tool call's span is also the
5
+ current span while the tool runs, so the platform, MCP and A2A calls the tool makes name it as
6
+ their parent. A model call's span names nothing: model calls carry no caller context.
7
+ """
2
8
 
3
9
  from __future__ import annotations
4
10
 
@@ -10,7 +16,7 @@ from typing import Any
10
16
 
11
17
  import structlog
12
18
 
13
- from ms_tau_sdk.logging import safe_log
19
+ from ms_tau_sdk.logging import TraceSpan, safe_log
14
20
 
15
21
  from .events import TauRuntimeEvent
16
22
  from .failures import (
@@ -89,16 +95,18 @@ class TauTurnObserver:
89
95
  self.handoffs = 0
90
96
  self.usage: dict[str, int] = {}
91
97
  self.terminal_failure: TerminalAssistantFailure | None = None
92
- self._model: tuple[str, float, int] | None = None
98
+ self._model: tuple[str, float, int, TraceSpan] | None = None
93
99
  self._next_model_attempt = 1
94
100
  self._tools: dict[
95
101
  str,
96
- tuple[float, str, str, int, str | None, str, bool, str],
102
+ tuple[float, str, str, int, str | None, str, bool, str, TraceSpan],
97
103
  ] = {}
98
104
  self._tool_attempts: dict[str, int] = {}
105
+ # The one tool call whose span is current. Tau runs a turn's tool calls one at a time.
106
+ self._current_tool: str | None = None
99
107
  self._handoffs: dict[
100
108
  str,
101
- tuple[float, str, str, str | None, str | None, str],
109
+ tuple[float, str, str, str | None, str | None, str, TraceSpan],
102
110
  ] = {}
103
111
 
104
112
  def observe(self, event: TauRuntimeEvent) -> None:
@@ -173,13 +181,15 @@ class TauTurnObserver:
173
181
  return
174
182
  call_uid = str(uuid.uuid4())
175
183
  attempt = self._next_model_attempt
176
- self._model = (call_uid, time.monotonic(), attempt)
184
+ span = TraceSpan()
185
+ self._model = (call_uid, time.monotonic(), attempt, span)
177
186
  self.model_calls += 1
178
187
  safe_log(
179
188
  self.logger,
180
189
  "info",
181
190
  "agent.model.started",
182
191
  message="Model call started",
192
+ **span.log_fields,
183
193
  model_call_uid=call_uid,
184
194
  model_provider=self.provider,
185
195
  model_name=self.model,
@@ -190,24 +200,29 @@ class TauTurnObserver:
190
200
  def _finish_model(self, data: Mapping[str, Any]) -> None:
191
201
  if self._model is None:
192
202
  self._start_model()
193
- call_uid, started, attempt = self._model or (
203
+ call_uid, started, attempt, span = self._model or (
194
204
  str(uuid.uuid4()),
195
205
  time.monotonic(),
196
206
  self._next_model_attempt,
207
+ TraceSpan(),
197
208
  )
198
209
  usage = _usage(data)
199
210
  self.usage.update(usage)
211
+ model_duration_ms = round((time.monotonic() - started) * 1000, 3)
200
212
  safe_log(
201
213
  self.logger,
202
214
  "info",
203
215
  "agent.model.completed",
204
216
  message="Model call completed",
217
+ **span.log_fields,
205
218
  model_call_uid=call_uid,
206
219
  model_provider=self.provider,
207
220
  model_name=self.model,
208
221
  model_operation="response",
209
222
  model_attempt=attempt,
210
- model_duration_ms=round((time.monotonic() - started) * 1000, 3),
223
+ model_duration_ms=model_duration_ms,
224
+ # A span's duration, under the name every terminal line gives it (ADR 0031).
225
+ duration_ms=model_duration_ms,
211
226
  model_first_token_ms=self.first_token_ms,
212
227
  finish_reason=_bounded(_first(data, "stopReason", "stop_reason", "finish_reason")),
213
228
  provider_request_id=_bounded(_first(data, "provider_request_id", "response_id")),
@@ -221,10 +236,11 @@ class TauTurnObserver:
221
236
  def _fail_model(self, data: Mapping[str, Any], *, event_type: str) -> None:
222
237
  if self._model is None:
223
238
  self._start_model()
224
- call_uid, started, attempt = self._model or (
239
+ call_uid, started, attempt, span = self._model or (
225
240
  str(uuid.uuid4()),
226
241
  time.monotonic(),
227
242
  self._next_model_attempt,
243
+ TraceSpan(),
228
244
  )
229
245
  error_type = (
230
246
  _bounded(_first(data, "provider_error_type", "error_type", "errorType")) or "ModelError"
@@ -263,12 +279,14 @@ class TauTurnObserver:
263
279
  "warning" if retryable else "error",
264
280
  "agent.model.failed",
265
281
  message="Model call failed",
282
+ **span.log_fields,
266
283
  model_call_uid=call_uid,
267
284
  model_provider=self.provider,
268
285
  model_name=self.model,
269
286
  model_operation="response",
270
287
  model_attempt=attempt,
271
288
  model_duration_ms=model_duration_ms,
289
+ duration_ms=model_duration_ms,
272
290
  model_error_type=error_type,
273
291
  status_code=status_code,
274
292
  provider_request_id=_bounded(_first(data, "provider_request_id", "response_id")),
@@ -306,6 +324,7 @@ class TauTurnObserver:
306
324
  if approval_required
307
325
  else "not_required"
308
326
  )
327
+ span = TraceSpan()
309
328
  self._tools[call_uid] = (
310
329
  time.monotonic(),
311
330
  name,
@@ -315,13 +334,18 @@ class TauTurnObserver:
315
334
  side_effect_class,
316
335
  approval_required,
317
336
  approval_outcome,
337
+ span,
318
338
  )
339
+ if self._current_tool is None:
340
+ self._current_tool = call_uid
341
+ span.open()
319
342
  self.tool_calls += 1
320
343
  safe_log(
321
344
  self.logger,
322
345
  "info",
323
346
  "agent.tool.started",
324
347
  message="Tool call started",
348
+ **span.log_fields,
325
349
  tool_call_uid=call_uid,
326
350
  tool_name=name,
327
351
  tool_category=category,
@@ -343,6 +367,7 @@ class TauTurnObserver:
343
367
  side_effect_class,
344
368
  approval_required,
345
369
  approval_outcome,
370
+ span,
346
371
  ) = self._tools.pop(
347
372
  call_uid,
348
373
  (
@@ -354,6 +379,7 @@ class TauTurnObserver:
354
379
  "unknown",
355
380
  False,
356
381
  "not_required",
382
+ TraceSpan(),
357
383
  ),
358
384
  )
359
385
  error_type = _bounded(_first(data, "errorType", "error_type"))
@@ -368,6 +394,7 @@ class TauTurnObserver:
368
394
  "error" if error_type else "info",
369
395
  "agent.tool.failed" if error_type else "agent.tool.completed",
370
396
  message="Tool call completed" if not error_type else "Tool call failed",
397
+ **span.log_fields,
371
398
  tool_call_uid=call_uid,
372
399
  tool_name=name,
373
400
  tool_category=category,
@@ -383,6 +410,9 @@ class TauTurnObserver:
383
410
  outcome=outcome,
384
411
  duration_ms=round((time.monotonic() - started) * 1000, 3),
385
412
  )
413
+ if self._current_tool == call_uid:
414
+ self._current_tool = None
415
+ span.close()
386
416
 
387
417
  def _start_handoff(self, data: Mapping[str, Any]) -> None:
388
418
  handoff_uid = _bounded(data.get("handoff_uid")) or str(uuid.uuid4())
@@ -396,6 +426,7 @@ class TauTurnObserver:
396
426
  if requested_reason in {"delegation", "specialist", "supervisor", "retry"}
397
427
  else "tau_subagent_event"
398
428
  )
429
+ span = TraceSpan()
399
430
  self._handoffs[handoff_uid] = (
400
431
  time.monotonic(),
401
432
  source,
@@ -403,6 +434,7 @@ class TauTurnObserver:
403
434
  parent_session,
404
435
  child_session,
405
436
  handoff_reason,
437
+ span,
406
438
  )
407
439
  self.handoffs += 1
408
440
  safe_log(
@@ -410,6 +442,7 @@ class TauTurnObserver:
410
442
  "info",
411
443
  "agent.handoff.started",
412
444
  message="Agent handoff started",
445
+ **span.log_fields,
413
446
  handoff_uid=handoff_uid,
414
447
  source_agent_uid=source,
415
448
  target_agent_uid=target,
@@ -433,6 +466,7 @@ class TauTurnObserver:
433
466
  _parent_session,
434
467
  _child_session,
435
468
  _reason,
469
+ _span,
436
470
  ) in reversed(self._handoffs.items())
437
471
  if candidate_source == source and candidate_target == target
438
472
  ),
@@ -441,14 +475,15 @@ class TauTurnObserver:
441
475
  if handoff_uid is None or handoff_uid not in self._handoffs:
442
476
  self._start_handoff(data)
443
477
  handoff_uid = next(reversed(self._handoffs))
444
- started, source, target, parent_session, child_session, handoff_reason = self._handoffs.pop(
445
- handoff_uid
478
+ started, source, target, parent_session, child_session, handoff_reason, span = (
479
+ self._handoffs.pop(handoff_uid)
446
480
  )
447
481
  safe_log(
448
482
  self.logger,
449
483
  "warning" if failed else "info",
450
484
  "agent.handoff.completed" if not failed else "agent.handoff.failed",
451
485
  message="Agent handoff completed" if not failed else "Agent handoff failed",
486
+ **span.log_fields,
452
487
  handoff_uid=handoff_uid,
453
488
  source_agent_uid=source,
454
489
  target_agent_uid=target,
@@ -460,6 +495,15 @@ class TauTurnObserver:
460
495
  duration_ms=round((time.monotonic() - started) * 1000, 3),
461
496
  )
462
497
 
498
+ def close(self) -> None:
499
+ """End the span of a tool call that never finished, when the turn ends."""
500
+
501
+ if self._current_tool is not None:
502
+ entry = self._tools.get(self._current_tool)
503
+ self._current_tool = None
504
+ if entry is not None:
505
+ entry[-1].close()
506
+
463
507
  def terminal_fields(self) -> dict[str, object]:
464
508
  fields: dict[str, object] = {
465
509
  "execution_duration_ms": round(
@@ -39,6 +39,7 @@ from ms_tau_sdk.errors import (
39
39
  BackendTimeoutError,
40
40
  TauSDKError,
41
41
  )
42
+ from ms_tau_sdk.logging import TraceSpan
42
43
  from ms_tau_sdk.protocols.caller_context import outbound_headers
43
44
  from ms_tau_sdk.settings import TauSDKSettings
44
45
 
@@ -346,6 +347,10 @@ class TurnRequesterBinding:
346
347
  session_headers = (
347
348
  {AGENT_SESSION_HEADER: self.session_uid} if self.session_uid is not None else {}
348
349
  )
350
+ # The call is a span, the parent it names to the application (ADR 0031).
351
+ span = TraceSpan()
352
+ with span:
353
+ caller_context = outbound_headers(own_headers)
349
354
  request = httpx.Request(
350
355
  method,
351
356
  url,
@@ -355,7 +360,7 @@ class TurnRequesterBinding:
355
360
  data=options.get("data"),
356
361
  files=options.get("files"),
357
362
  headers={
358
- **outbound_headers(own_headers),
363
+ **caller_context,
359
364
  **own_headers,
360
365
  **session_headers,
361
366
  "Authorization": f"Bearer {token}",
@@ -380,6 +385,7 @@ class TurnRequesterBinding:
380
385
  logger.warning(
381
386
  "dependency.call.failed",
382
387
  message="Application call failed",
388
+ **span.log_fields,
383
389
  target_system="resource_release",
384
390
  resource_release_uid=release_uid,
385
391
  requester_bound=delegate,
@@ -392,6 +398,7 @@ class TurnRequesterBinding:
392
398
  logger.info(
393
399
  "dependency.call.completed",
394
400
  message="Application call completed",
401
+ **span.log_fields,
395
402
  target_system="resource_release",
396
403
  resource_release_uid=release_uid,
397
404
  requester_bound=delegate,