ms-tau-sdk 2.0.32.dev95__tar.gz → 2.0.33.dev96__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 (96) hide show
  1. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/CHANGELOG.md +28 -0
  2. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/PKG-INFO +1 -1
  3. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/pyproject.toml +1 -1
  4. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/a2a.py +9 -2
  5. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/hydrate_endpoint.py +32 -41
  6. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/models.py +3 -0
  7. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/release_access.py +8 -1
  8. ms_tau_sdk-2.0.33.dev96/src/ms_tau_sdk/backend/release_readiness.py +236 -0
  9. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/errors.py +8 -1
  10. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/requester.py +48 -38
  11. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/settings.py +3 -1
  12. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/tools/mcp_applications.py +40 -5
  13. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/.gitignore +0 -0
  14. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/README.md +0 -0
  15. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/__init__.py +0 -0
  16. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/app.py +0 -0
  17. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/cli.py +0 -0
  18. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/config.py +0 -0
  19. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/env_file.py +0 -0
  20. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/logs.py +0 -0
  21. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/proxy.py +0 -0
  22. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/state.py +0 -0
  23. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/static/BULMA-LICENSE.txt +0 -0
  24. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/static/app.js +0 -0
  25. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/static/board.css +0 -0
  26. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/static/bulma.min.css +0 -0
  27. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/packages/tau-board/src/ms_tau_board/static/index.html +0 -0
  28. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/__init__.py +0 -0
  29. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/agent_skills/design-agent-icons-and-motion/SKILL.md +0 -0
  30. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/agent_skills/design-agent-icons-and-motion/agents/openai.yaml +0 -0
  31. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/agent_skills/design-agent-icons-and-motion/references/metatables-example.html +0 -0
  32. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/agent_skills/tau_a2a_runtime_adapter/SKILL.md +0 -0
  33. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/agent_skills/tau_local_development/SKILL.md +0 -0
  34. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/agent_skills/tau_project_customization/SKILL.md +0 -0
  35. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/agent_skills/tau_repository_integration/SKILL.md +0 -0
  36. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/agent_skills/tau_security_and_access/SKILL.md +0 -0
  37. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/__init__.py +0 -0
  38. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/chat.py +0 -0
  39. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/conversations.py +0 -0
  40. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/dependencies.py +0 -0
  41. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/health.py +0 -0
  42. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/inspection.py +0 -0
  43. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/local_chat.py +0 -0
  44. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/models.py +0 -0
  45. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/request_identity.py +0 -0
  46. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/api/sessions.py +0 -0
  47. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/app.py +0 -0
  48. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/application.py +0 -0
  49. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/__init__.py +0 -0
  50. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/assertions.py +0 -0
  51. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/auth.py +0 -0
  52. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/client.py +0 -0
  53. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/local.py +0 -0
  54. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/mcp.py +0 -0
  55. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/routes.py +0 -0
  56. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/backend/team_memberships.py +0 -0
  57. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/cli.py +0 -0
  58. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/logging.py +0 -0
  59. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/protocols/__init__.py +0 -0
  60. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/protocols/a2a_failure.py +0 -0
  61. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/protocols/a2a_message.py +0 -0
  62. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/protocols/a2a_roles.py +0 -0
  63. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/protocols/assistant_ui.py +0 -0
  64. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/protocols/caller_context.py +0 -0
  65. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/protocols/chat_history.py +0 -0
  66. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/protocols/strict_json.py +0 -0
  67. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/providers/__init__.py +0 -0
  68. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/providers/definitions.py +0 -0
  69. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/providers/factory.py +0 -0
  70. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/providers/tau_compat.py +0 -0
  71. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/resources/SYSTEM.md +0 -0
  72. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/resources/__init__.py +0 -0
  73. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/resources/context_compat.py +0 -0
  74. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/resources/loader.py +0 -0
  75. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/resources/prompts/review-code-repository.md +0 -0
  76. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/resources/selection.py +0 -0
  77. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/__init__.py +0 -0
  78. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/deployment_health.py +0 -0
  79. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/events.py +0 -0
  80. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/extensions.py +0 -0
  81. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/failures.py +0 -0
  82. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/live_turns.py +0 -0
  83. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/manager.py +0 -0
  84. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/observability.py +0 -0
  85. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/provenance.py +0 -0
  86. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/session.py +0 -0
  87. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/snapshots.py +0 -0
  88. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/runtime/task_context.py +0 -0
  89. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/sessions/__init__.py +0 -0
  90. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/sessions/storage.py +0 -0
  91. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/skills.py +0 -0
  92. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/tools/__init__.py +0 -0
  93. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/tools/mainsequence_mcp.py +0 -0
  94. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/tools/mcp_connection.py +0 -0
  95. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/tools/skill_read.py +0 -0
  96. {ms_tau_sdk-2.0.32.dev95 → ms_tau_sdk-2.0.33.dev96}/src/ms_tau_sdk/tools/task_control.py +0 -0
@@ -1,5 +1,33 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.33 — 2026-11-10
4
+
5
+ - **A call to an application that scaled to zero no longer spends its time limit on the start.**
6
+ Tau kept an application's token for the turn and used it without asking the platform, so a
7
+ call to an application that had since scaled to zero was held while it started and failed as a
8
+ tool timeout. Now the kept token is used only while the application is known warm: it answered,
9
+ or the platform reported it ready, within its idle window (60 seconds until the platform
10
+ states the application's own in `scaling.idle_timeout_seconds`, or always when
11
+ `scaling.min_instances` is at least 1). Otherwise Tau asks the platform again, which wakes the
12
+ application, and waits under the readiness limit before MCP opens. The provider credential
13
+ hydrate release follows the same rule and the same wait.
14
+ - **The readiness limit follows the platform's expectation.** When
15
+ `TAU_MCP_APPLICATION_READY_TIMEOUT_SECONDS` is not set, the limit is twice
16
+ `scaling.expected_cold_start_seconds`, at least 30 seconds, and `300` seconds when the
17
+ platform states none. The default was `120`. Set, the variable is the limit as before.
18
+ - **A timeout during a start says so.** After a sent tool call times out, Tau asks the platform
19
+ once; when the application is `waking` the result carries `failure: cold_start_timeout`,
20
+ `phase: request` and `runtime_access_state: waking`, and the message says the call may or may
21
+ not have run. It is never sent again.
22
+ - **The time left travels with the call.** A tool call sends
23
+ `Mainsequence-Request-Timeout-Ms`, and `message:send` reads it from its caller: the shorter
24
+ of it and the platform's Message limit bounds the request.
25
+
26
+ The contract is mainsequence-sdk's ADR 0041; see the amendment to
27
+ [ADR 0022](docs/adrs/0022-mcp-application-tool-call-limits-and-failures.md). The platform does
28
+ not return `scaling` yet as far as this SDK can tell, and nothing was tried against a deployed
29
+ application.
30
+
3
31
  ## 2.0.32 — 2026-11-10
4
32
 
5
33
  - **A model Tau does not list runs from the provider control.** A session on a model that the
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: ms-tau-sdk
3
- Version: 2.0.32.dev95
3
+ Version: 2.0.33.dev96
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 = "2.0.32.dev95"
7
+ version = "2.0.33.dev96"
8
8
  description = "Workspace-bound Tau application primitives for Main Sequence projects"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.13"
@@ -27,6 +27,7 @@ from ms_tau_sdk.backend.models import (
27
27
  AgentTaskCreateResult,
28
28
  AgentTaskSnapshot,
29
29
  )
30
+ from ms_tau_sdk.backend.release_readiness import DEADLINE_HEADER, caller_timeout_seconds
30
31
  from ms_tau_sdk.errors import BackendError, LeaseLostError, TaskTerminalizationUnknownError
31
32
  from ms_tau_sdk.logging import bind_request_log_fields
32
33
  from ms_tau_sdk.protocols.a2a_failure import (
@@ -2567,14 +2568,20 @@ async def _message_send(
2567
2568
  ) -> dict[str, Any]:
2568
2569
  """Answer one ``message:send``, for REST and JSON-RPC alike.
2569
2570
 
2570
- When the platform sets a Message limit, it runs from the moment the request arrives. It
2571
+ When the platform sets a Message limit, or the caller states how long it will still wait
2572
+ (``Mainsequence-Request-Timeout-Ms``), the shorter runs from the moment the request arrives. It
2571
2573
  bounds a Message-mode request as a whole, including output-contract repair turns, and it ends
2572
2574
  the wait of a Task-mode request that does not return immediately. A Message-mode request that
2573
2575
  reaches its limit, or its turn's own limit, raises ``_MessageLimitExceeded``, which each
2574
2576
  transport answers in its own form. A Task-mode request that fails after its Task was accepted
2575
2577
  raises ``_AcceptedTaskUnanswered``, which each transport answers with the Task named.
2576
2578
  """
2577
- platform_limit = config.a2a_message_limit_seconds
2579
+ # The caller's own remaining wait, when it states one, can only shorten the platform's limit.
2580
+ caller_limit = caller_timeout_seconds(request.headers.get(DEADLINE_HEADER))
2581
+ limits = [
2582
+ limit for limit in (config.a2a_message_limit_seconds, caller_limit) if limit is not None
2583
+ ]
2584
+ platform_limit = min(limits, default=None)
2578
2585
  deadline = (
2579
2586
  None if platform_limit is None else asyncio.get_running_loop().time() + platform_limit
2580
2587
  )
@@ -26,8 +26,6 @@ import httpx
26
26
  import structlog
27
27
 
28
28
  from ms_tau_sdk.errors import (
29
- ApplicationReadinessTimeoutError,
30
- ApplicationWakingError,
31
29
  BackendConflictError,
32
30
  BackendError,
33
31
  BackendTimeoutError,
@@ -36,6 +34,7 @@ from ms_tau_sdk.settings import TauSDKSettings
36
34
 
37
35
  from .models import ReleaseRuntimeAccess
38
36
  from .release_access import ReleaseAccess, release_access_from_answer
37
+ from .release_readiness import release_warmth, wait_until_ready
39
38
  from .routes import model_provider_credentials
40
39
 
41
40
  logger = structlog.get_logger(__name__)
@@ -260,16 +259,25 @@ class ReleaseHydrateEndpoint(CustomHydrateEndpoint):
260
259
  return f"ReleaseHydrateEndpoint(release_uid={self.release_uid!r})"
261
260
 
262
261
  async def hydrate(self, payload: Mapping[str, Any]) -> dict[str, Any]:
263
- access = await self._release_access(stale=None)
264
- response = await self._send(self._url(access), payload, authorization=access.token.value)
265
- if response.status_code == 401:
266
- # The token may have expired early or been revoked: obtain a new one, once.
267
- access = await self._release_access(stale=access)
262
+ try:
263
+ access = await self._release_access(stale=None)
268
264
  response = await self._send(
269
265
  self._url(access), payload, authorization=access.token.value
270
266
  )
271
267
  if response.status_code == 401:
272
- raise BackendError(f"{self.description} refused its token", status_code=401)
268
+ # The token may have expired early or been revoked: obtain a new one, once.
269
+ access = await self._release_access(stale=access)
270
+ response = await self._send(
271
+ self._url(access), payload, authorization=access.token.value
272
+ )
273
+ if response.status_code == 401:
274
+ raise BackendError(f"{self.description} refused its token", status_code=401)
275
+ except BackendTimeoutError:
276
+ # The request may have been held by a start: ask the platform before the next.
277
+ release_warmth.forget(self.release_uid)
278
+ raise
279
+ if response.status_code < 500:
280
+ release_warmth.served(self.release_uid)
273
281
  return self._answer(response)
274
282
 
275
283
  def _url(self, access: ReleaseAccess) -> httpx.URL:
@@ -285,7 +293,13 @@ class ReleaseHydrateEndpoint(CustomHydrateEndpoint):
285
293
  async def _release_access(self, *, stale: ReleaseAccess | None) -> ReleaseAccess:
286
294
  async with self._lock:
287
295
  cached = self._access
288
- if cached is not None and cached is not stale and not cached.token.needs_refresh():
296
+ if (
297
+ cached is not None
298
+ and cached is not stale
299
+ and not cached.token.needs_refresh()
300
+ # A release that may have scaled to zero is asked about again, which wakes it.
301
+ and release_warmth.is_warm(self.release_uid)
302
+ ):
289
303
  return cached
290
304
  self._access = None
291
305
  access = await self._ready_access()
@@ -295,38 +309,15 @@ class ReleaseHydrateEndpoint(CustomHydrateEndpoint):
295
309
  async def _ready_access(self) -> ReleaseAccess:
296
310
  """Obtain token access, waiting while the release wakes, within the ready timeout."""
297
311
 
298
- timeout_seconds = self.settings.mcp_application_ready_timeout_seconds
299
- started = asyncio.get_running_loop().time()
300
- deadline = asyncio.timeout(None)
301
- waiting = False
302
- try:
303
- async with deadline:
304
- while True:
305
- answer = await self._runtime_access()
306
- try:
307
- access = release_access_from_answer(answer)
308
- except ApplicationWakingError as error:
309
- if not waiting:
310
- waiting = True
311
- deadline.reschedule(started + timeout_seconds)
312
- logger.info(
313
- "runtime.provider_credential_release.waiting",
314
- resource_release_uid=self.release_uid,
315
- runtime_access_state="waking",
316
- retry_after_seconds=error.retry_after_seconds,
317
- )
318
- await asyncio.sleep(error.retry_after_seconds)
319
- else:
320
- if waiting:
321
- logger.info(
322
- "runtime.provider_credential_release.ready",
323
- resource_release_uid=self.release_uid,
324
- )
325
- return access
326
- except TimeoutError:
327
- if not deadline.expired():
328
- raise
329
- raise ApplicationReadinessTimeoutError(timeout_seconds=timeout_seconds)
312
+ async def obtain() -> ReleaseAccess:
313
+ return release_access_from_answer(await self._runtime_access())
314
+
315
+ return await wait_until_ready(
316
+ obtain,
317
+ settings=self.settings,
318
+ release_uid=self.release_uid,
319
+ event="runtime.provider_credential_release",
320
+ )
330
321
 
331
322
  async def _runtime_access(self) -> ReleaseRuntimeAccess:
332
323
  failure: BackendError
@@ -504,6 +504,9 @@ class ReleaseRuntimeAccess(BackendModel):
504
504
  resource_release_uid: str
505
505
  access: ReleaseAccessGrant | None = None
506
506
  runtime_access: dict[str, Any] = Field(default_factory=dict)
507
+ # How the release scales, when the platform states it: min_instances, idle_timeout_seconds,
508
+ # expected_cold_start_seconds, serving_since.
509
+ scaling: dict[str, Any] | None = None
507
510
 
508
511
 
509
512
  class AgentTask(BackendModel):
@@ -11,6 +11,7 @@ from ms_tau_sdk.errors import ApplicationWakingError, BackendError
11
11
 
12
12
  from .auth import AccessToken, _jwt_expiry
13
13
  from .models import ReleaseRuntimeAccess
14
+ from .release_readiness import expected_cold_start_seconds, release_warmth
14
15
 
15
16
 
16
17
  @dataclass(slots=True)
@@ -42,7 +43,11 @@ def release_access_from_answer(answer: ReleaseRuntimeAccess) -> ReleaseAccess:
42
43
  seconds = float("inf")
43
44
  if math.isfinite(seconds) and seconds >= 0:
44
45
  delay = max(seconds, 0.1)
45
- raise ApplicationWakingError(retry_after_seconds=delay)
46
+ release_warmth.forget(answer.resource_release_uid)
47
+ raise ApplicationWakingError(
48
+ retry_after_seconds=delay,
49
+ expected_cold_start_seconds=expected_cold_start_seconds(answer.scaling),
50
+ )
46
51
  raise BackendError(
47
52
  f"Release {answer.resource_release_uid} is not ready to be called"
48
53
  + (f" (runtime access: {state})" if isinstance(state, str) else ""),
@@ -67,6 +72,8 @@ def release_access_from_answer(answer: ReleaseRuntimeAccess) -> ReleaseAccess:
67
72
  expires_at = (
68
73
  grant.expires_at.timestamp() if grant.expires_at is not None else _jwt_expiry(token)
69
74
  )
75
+ # A ready answer is a sign that the release serves now.
76
+ release_warmth.served(answer.resource_release_uid, answer.scaling)
70
77
  return ReleaseAccess(
71
78
  rpc_url=rpc_url,
72
79
  token=AccessToken(
@@ -0,0 +1,236 @@
1
+ """Readiness of the releases the runtime calls: one wait, one limit rule, one warm rule.
2
+
3
+ The contract is the one mainsequence-sdk follows (its ADR 0041). Readiness is asked of the
4
+ platform, never guessed: a release that is starting is waited for under a readiness limit, apart
5
+ from the limit of the call that follows. What this process remembers only spares a platform call
6
+ while a release is known warm.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import asyncio
12
+ import math
13
+ import threading
14
+ import time
15
+ from collections.abc import Awaitable, Callable, Mapping
16
+ from dataclasses import dataclass
17
+
18
+ import structlog
19
+
20
+ from ms_tau_sdk.errors import ApplicationReadinessTimeoutError, ApplicationWakingError
21
+ from ms_tau_sdk.settings import TauSDKSettings
22
+
23
+ logger = structlog.get_logger(__name__)
24
+
25
+ DEADLINE_HEADER = "Mainsequence-Request-Timeout-Ms"
26
+ DEFAULT_READY_TIMEOUT_SECONDS = 300.0
27
+ MIN_READY_TIMEOUT_SECONDS = 30.0
28
+ # Used until the platform states a release's own idle window.
29
+ DEFAULT_IDLE_TIMEOUT_SECONDS = 60.0
30
+ MIN_IDLE_MARGIN_SECONDS = 10.0
31
+ IDLE_MARGIN_FRACTION = 0.2
32
+
33
+
34
+ def _positive(value: object) -> float | None:
35
+ if isinstance(value, bool) or not isinstance(value, int | float):
36
+ return None
37
+ try:
38
+ seconds = float(value)
39
+ except OverflowError:
40
+ return None
41
+ return seconds if math.isfinite(seconds) and seconds > 0 else None
42
+
43
+
44
+ def expected_cold_start_seconds(scaling: object) -> float | None:
45
+ """The start time the platform expects for a release, when it states one."""
46
+
47
+ if not isinstance(scaling, Mapping):
48
+ return None
49
+ return _positive(scaling.get("expected_cold_start_seconds"))
50
+
51
+
52
+ def ready_timeout_seconds(
53
+ settings: TauSDKSettings | None, expected_cold_start: float | None
54
+ ) -> float:
55
+ """How long a starting release is waited for.
56
+
57
+ A limit set in the environment wins. Otherwise it is twice the start time the platform
58
+ expects, at least 30 seconds, and the setting's default when the platform states none.
59
+ """
60
+
61
+ configured = (
62
+ settings.mcp_application_ready_timeout_seconds
63
+ if settings is not None
64
+ else DEFAULT_READY_TIMEOUT_SECONDS
65
+ )
66
+ if (
67
+ settings is not None
68
+ and "mcp_application_ready_timeout_seconds" in settings.model_fields_set
69
+ ):
70
+ return configured
71
+ if expected_cold_start is not None:
72
+ return max(MIN_READY_TIMEOUT_SECONDS, 2 * expected_cold_start)
73
+ return configured
74
+
75
+
76
+ @dataclass
77
+ class _Warmth:
78
+ # Monotonic time of the last sign that the release was serving.
79
+ served: float | None = None
80
+ min_instances: int | None = None
81
+ idle_timeout_seconds: float | None = None
82
+ last_cold_start_seconds: float | None = None
83
+
84
+
85
+ class ReleaseWarmth:
86
+ """What this process has seen of each release it calls. In memory, never the truth.
87
+
88
+ Another caller may keep a release warm and a deployment may restart it. A wrong "warm" costs
89
+ one held request, which is then classified; a wrong "cold" costs one platform call.
90
+ """
91
+
92
+ def __init__(self) -> None:
93
+ self._lock = threading.Lock()
94
+ self._releases: dict[str, _Warmth] = {}
95
+
96
+ def served(self, release_uid: str, scaling: object = None) -> None:
97
+ """Record a sign that the release serves: a ready answer, or a response below 500."""
98
+
99
+ with self._lock:
100
+ entry = self._releases.setdefault(release_uid, _Warmth())
101
+ entry.served = time.monotonic()
102
+ if isinstance(scaling, Mapping):
103
+ minimum = scaling.get("min_instances")
104
+ if isinstance(minimum, int) and not isinstance(minimum, bool) and minimum >= 0:
105
+ entry.min_instances = minimum
106
+ entry.idle_timeout_seconds = (
107
+ _positive(scaling.get("idle_timeout_seconds")) or entry.idle_timeout_seconds
108
+ )
109
+
110
+ def cold_start(self, release_uid: str, seconds: float) -> None:
111
+ with self._lock:
112
+ self._releases.setdefault(release_uid, _Warmth()).last_cold_start_seconds = seconds
113
+
114
+ def forget(self, release_uid: str) -> None:
115
+ """Stop treating the release as warm; what the platform stated about it is kept."""
116
+
117
+ with self._lock:
118
+ entry = self._releases.get(release_uid)
119
+ if entry is not None:
120
+ entry.served = None
121
+
122
+ def clear(self) -> None:
123
+ with self._lock:
124
+ self._releases.clear()
125
+
126
+ def is_warm(self, release_uid: str) -> bool:
127
+ """Whether a request sent now is expected to be served without a start."""
128
+
129
+ with self._lock:
130
+ entry = self._releases.get(release_uid)
131
+ if entry is None or entry.served is None:
132
+ return False
133
+ if entry.min_instances is not None and entry.min_instances >= 1:
134
+ return True
135
+ idle = entry.idle_timeout_seconds or DEFAULT_IDLE_TIMEOUT_SECONDS
136
+ margin = max(MIN_IDLE_MARGIN_SECONDS, IDLE_MARGIN_FRACTION * idle)
137
+ return time.monotonic() - entry.served < idle - margin
138
+
139
+ def last_cold_start_seconds(self, release_uid: str) -> float | None:
140
+ with self._lock:
141
+ entry = self._releases.get(release_uid)
142
+ return entry.last_cold_start_seconds if entry is not None else None
143
+
144
+
145
+ release_warmth = ReleaseWarmth()
146
+
147
+
148
+ async def wait_until_ready[T](
149
+ obtain: Callable[[], Awaitable[T]],
150
+ *,
151
+ settings: TauSDKSettings | None,
152
+ release_uid: str,
153
+ event: str,
154
+ still_wanted: Callable[[], None] | None = None,
155
+ ) -> T:
156
+ """Call ``obtain`` until it stops raising ``ApplicationWakingError``.
157
+
158
+ The readiness limit starts with the first call and is fixed by the first waking answer.
159
+ ``still_wanted`` runs before every call and raises to end the wait. Log events are
160
+ ``<event>.waiting`` and ``<event>.ready``. An expired wait raises
161
+ ``ApplicationReadinessTimeoutError``, which keeps no cause.
162
+ """
163
+
164
+ started = asyncio.get_running_loop().time()
165
+ deadline = asyncio.timeout(None)
166
+ limit: float | None = None
167
+ try:
168
+ async with deadline:
169
+ while True:
170
+ if still_wanted is not None:
171
+ still_wanted()
172
+ try:
173
+ access = await obtain()
174
+ except ApplicationWakingError as error:
175
+ if limit is None:
176
+ limit = ready_timeout_seconds(settings, error.expected_cold_start_seconds)
177
+ deadline.reschedule(started + limit)
178
+ logger.info(
179
+ f"{event}.waiting",
180
+ resource_release_uid=release_uid,
181
+ runtime_access_state="waking",
182
+ retry_after_seconds=error.retry_after_seconds,
183
+ ready_timeout_seconds=limit,
184
+ )
185
+ await asyncio.sleep(error.retry_after_seconds)
186
+ else:
187
+ if limit is not None:
188
+ seconds = asyncio.get_running_loop().time() - started
189
+ release_warmth.cold_start(release_uid, seconds)
190
+ logger.info(
191
+ f"{event}.ready",
192
+ resource_release_uid=release_uid,
193
+ cold_start_seconds=round(seconds, 3),
194
+ )
195
+ return access
196
+ except TimeoutError:
197
+ if not deadline.expired():
198
+ raise
199
+ raise ApplicationReadinessTimeoutError(timeout_seconds=limit or 0.0)
200
+
201
+
202
+ def deadline_headers(deadline: float | None) -> dict[str, str]:
203
+ """The header that tells a release how long its caller will still wait.
204
+
205
+ ``deadline`` is the monotonic instant by which the call must be answered; ``None`` sends
206
+ nothing.
207
+ """
208
+
209
+ if deadline is None:
210
+ return {}
211
+ remaining = deadline - time.monotonic()
212
+ return {DEADLINE_HEADER: str(max(1, math.floor(remaining * 1000)))}
213
+
214
+
215
+ def caller_timeout_seconds(value: str | None) -> float | None:
216
+ """Seconds from a caller's deadline header; ``None`` when absent or unusable."""
217
+
218
+ if value is None:
219
+ return None
220
+ try:
221
+ milliseconds = int(value.strip())
222
+ except ValueError:
223
+ return None
224
+ return milliseconds / 1000 if milliseconds > 0 else None
225
+
226
+
227
+ __all__ = [
228
+ "DEADLINE_HEADER",
229
+ "ReleaseWarmth",
230
+ "caller_timeout_seconds",
231
+ "deadline_headers",
232
+ "expected_cold_start_seconds",
233
+ "ready_timeout_seconds",
234
+ "release_warmth",
235
+ "wait_until_ready",
236
+ ]
@@ -48,9 +48,16 @@ class BackendTimeoutError(BackendError):
48
48
  class ApplicationWakingError(BackendError):
49
49
  """The platform accepted access resolution but the application is still starting."""
50
50
 
51
- def __init__(self, *, retry_after_seconds: float) -> None:
51
+ def __init__(
52
+ self,
53
+ *,
54
+ retry_after_seconds: float,
55
+ expected_cold_start_seconds: float | None = None,
56
+ ) -> None:
52
57
  super().__init__("Application is waking", detail={"runtime_access_state": "waking"})
53
58
  self.retry_after_seconds = retry_after_seconds
59
+ # The start time the platform expects, when it states one.
60
+ self.expected_cold_start_seconds = expected_cold_start_seconds
54
61
 
55
62
 
56
63
  class ApplicationReadinessTimeoutError(BackendError):
@@ -31,15 +31,16 @@ from ms_tau_sdk.backend.client import LeaseProof, answer_without_request_credent
31
31
  from ms_tau_sdk.backend.models import ReleaseRuntimeAccess
32
32
  from ms_tau_sdk.backend.release_access import ReleaseAccess as _ReleaseAccess
33
33
  from ms_tau_sdk.backend.release_access import release_access_from_answer as _token_access
34
+ from ms_tau_sdk.backend.release_readiness import release_warmth, wait_until_ready
34
35
  from ms_tau_sdk.backend.team_memberships import member_team_uids
35
36
  from ms_tau_sdk.errors import (
36
- ApplicationReadinessTimeoutError,
37
37
  ApplicationWakingError,
38
38
  BackendError,
39
39
  BackendTimeoutError,
40
40
  TauSDKError,
41
41
  )
42
42
  from ms_tau_sdk.protocols.caller_context import outbound_headers
43
+ from ms_tau_sdk.settings import TauSDKSettings
43
44
 
44
45
  logger = structlog.get_logger(__name__)
45
46
 
@@ -289,7 +290,13 @@ class TurnRequesterBinding:
289
290
  proof = self._delegation(delegate)
290
291
  key = (release_uid, delegate)
291
292
  cached = self._release_access.get(key)
292
- if cached is not None and not refresh and not cached.token.needs_refresh():
293
+ if (
294
+ cached is not None
295
+ and not refresh
296
+ and not cached.token.needs_refresh()
297
+ # A release that may have scaled to zero is asked about again, which wakes it.
298
+ and release_warmth.is_warm(release_uid)
299
+ ):
293
300
  return cached
294
301
  self._release_access.pop(key, None)
295
302
  failure: BackendError | None = None
@@ -361,6 +368,12 @@ class TurnRequesterBinding:
361
368
  response = await client.send(request, follow_redirects=False)
362
369
  except httpx.HTTPError as error:
363
370
  failure = type(error).__name__
371
+ if isinstance(error, httpx.TimeoutException):
372
+ # The request may have been held by a start: ask the platform before the next.
373
+ release_warmth.forget(release_uid)
374
+ else:
375
+ if response.status_code < 500:
376
+ release_warmth.served(release_uid)
364
377
  finally:
365
378
  client.cookies.clear()
366
379
  if failure:
@@ -608,7 +621,7 @@ def _current_turn_binding() -> TurnRequesterBinding | None:
608
621
  async def _turn_application_access(
609
622
  release_uid: str,
610
623
  *,
611
- ready_timeout_seconds: float = 120,
624
+ settings: TauSDKSettings | None = None,
612
625
  cancelled: Callable[[], bool] | None = None,
613
626
  ) -> _TurnApplicationAccess:
614
627
  """SDK-private: the current turn's access to one application.
@@ -628,42 +641,18 @@ async def _turn_application_access(
628
641
  async def resolve(*, refresh: bool) -> _ReleaseAccess:
629
642
  # Capture delegation once, including for renewal; an ended turn never falls back to the
630
643
  # Agent's own access. Each poll reacquires the binding lock and current lease proof.
631
- started = asyncio.get_running_loop().time()
632
- deadline = asyncio.timeout(None)
633
- waiting = False
634
- try:
635
- async with deadline:
636
- while True:
637
- if not binding.active:
638
- raise RequesterBindingError(
639
- "The turn ended while obtaining application access."
640
- )
641
- try:
642
- access = await binding._release_access_for(
643
- uid, refresh=refresh, delegate=delegate
644
- )
645
- except ApplicationWakingError as error:
646
- if not waiting:
647
- waiting = True
648
- deadline.reschedule(started + ready_timeout_seconds)
649
- logger.info(
650
- "runtime.application_access.waiting",
651
- resource_release_uid=uid,
652
- runtime_access_state="waking",
653
- retry_after_seconds=error.retry_after_seconds,
654
- )
655
- await asyncio.sleep(error.retry_after_seconds)
656
- else:
657
- if waiting:
658
- logger.info(
659
- "runtime.application_access.ready", resource_release_uid=uid
660
- )
661
- return access
662
- except TimeoutError:
663
- if not deadline.expired():
664
- raise
644
+ def still_wanted() -> None:
645
+ if not binding.active:
646
+ raise RequesterBindingError("The turn ended while obtaining application access.")
647
+
665
648
  # No transport error or credential-bearing exception is retained as the cause.
666
- raise ApplicationReadinessTimeoutError(timeout_seconds=ready_timeout_seconds)
649
+ return await wait_until_ready(
650
+ lambda: binding._release_access_for(uid, refresh=refresh, delegate=delegate),
651
+ settings=settings,
652
+ release_uid=uid,
653
+ event="runtime.application_access",
654
+ still_wanted=still_wanted,
655
+ )
667
656
 
668
657
  async def obtain(*, refresh: bool) -> _ReleaseAccess:
669
658
  # ToolCancellationToken exposes only is_cancelled(). Poll it while both the readiness
@@ -698,6 +687,27 @@ async def _turn_application_access(
698
687
  )
699
688
 
700
689
 
690
+ async def _turn_application_starting(release_uid: str) -> bool:
691
+ """SDK-private: whether the platform now reports the application as starting.
692
+
693
+ Asked once after a call timed out, to tell a request held by a start from a slow application.
694
+ Asking also wakes the application. Any failure to learn it reads as "not starting".
695
+ """
696
+
697
+ binding = _TURN_BINDING.get()
698
+ if binding is None or not binding.active:
699
+ return False
700
+ uid = _canonical_uid(release_uid)
701
+ release_warmth.forget(uid)
702
+ try:
703
+ await binding._release_access_for(uid, refresh=True, delegate=binding.serving)
704
+ except ApplicationWakingError:
705
+ return True
706
+ except Exception:
707
+ return False
708
+ return False
709
+
710
+
701
711
  def _canonical_uid(value: str) -> str:
702
712
  uid = canonical_requester_uid(value)
703
713
  if uid is None:
@@ -335,8 +335,10 @@ class TauSDKSettings(BaseSettings):
335
335
  gt=0,
336
336
  validation_alias="TAU_MCP_TOOL_TIMEOUT_SECONDS",
337
337
  )
338
+ # How long a starting release is waited for when this is not set and the platform states no
339
+ # expected start time. Set, it is the limit; unset, the platform's expectation decides.
338
340
  mcp_application_ready_timeout_seconds: float = Field(
339
- default=120.0,
341
+ default=300.0,
340
342
  gt=0,
341
343
  allow_inf_nan=False,
342
344
  validation_alias="TAU_MCP_APPLICATION_READY_TIMEOUT_SECONDS",
@@ -26,6 +26,7 @@ import binascii
26
26
  import hashlib
27
27
  import json
28
28
  import re
29
+ import time
29
30
  from collections.abc import AsyncGenerator, Mapping, Sequence
30
31
  from typing import Protocol
31
32
 
@@ -50,6 +51,7 @@ from ms_tau_sdk.backend.mcp import (
50
51
  raise_for_renewed_401,
51
52
  )
52
53
  from ms_tau_sdk.backend.models import MCPApplication
54
+ from ms_tau_sdk.backend.release_readiness import deadline_headers, release_warmth
53
55
  from ms_tau_sdk.errors import (
54
56
  ApplicationReadinessTimeoutError,
55
57
  ApplicationWakingError,
@@ -61,6 +63,7 @@ from ms_tau_sdk.runtime.requester import (
61
63
  AGENT_SESSION_HEADER,
62
64
  RequesterBindingError,
63
65
  _turn_application_access,
66
+ _turn_application_starting,
64
67
  _TurnApplicationAccess,
65
68
  )
66
69
  from ms_tau_sdk.settings import TauSDKSettings
@@ -82,10 +85,13 @@ class ApplicationConnector(Protocol):
82
85
  class _ApplicationTokenAuth(httpx2.Auth):
83
86
  """Send the turn's application token; obtain a new one once when the application says 401."""
84
87
 
85
- def __init__(self, access: _TurnApplicationAccess) -> None:
88
+ def __init__(self, access: _TurnApplicationAccess, release_uid: str | None = None) -> None:
86
89
  self._token = access.token
87
90
  self._renew = access.renew
88
91
  self._session_uid = access.session_uid
92
+ self._release_uid = release_uid
93
+ # The monotonic instant by which the tool call in flight must be answered, once known.
94
+ self.deadline: float | None = None
89
95
 
90
96
  async def async_auth_flow(
91
97
  self,
@@ -95,11 +101,15 @@ class _ApplicationTokenAuth(httpx2.Auth):
95
101
  # The turn's session, for the application to resolve the person it serves (ADR 0028).
96
102
  request.headers[AGENT_SESSION_HEADER] = self._session_uid
97
103
  request.headers["Authorization"] = f"Bearer {self._token}"
104
+ request.headers.update(deadline_headers(self.deadline))
98
105
  response = yield request
99
106
  if response.status_code == 401:
100
107
  self._token = await self._renew()
101
108
  request.headers["Authorization"] = f"Bearer {self._token}"
102
- raise_for_renewed_401((yield request))
109
+ response = yield request
110
+ raise_for_renewed_401(response)
111
+ if response.status_code < 500 and self._release_uid is not None:
112
+ release_warmth.served(self._release_uid)
103
113
 
104
114
 
105
115
  def application_connector(settings: TauSDKSettings) -> ApplicationConnector:
@@ -110,14 +120,14 @@ def application_connector(settings: TauSDKSettings) -> ApplicationConnector:
110
120
  ) -> MCPConnectionClient:
111
121
  access = await _turn_application_access(
112
122
  application.resource_release_uid,
113
- ready_timeout_seconds=settings.mcp_application_ready_timeout_seconds,
123
+ settings=settings,
114
124
  cancelled=signal.is_cancelled if signal is not None else None,
115
125
  )
116
126
  return await MCPConnectionClient(
117
127
  name=application.name,
118
128
  display_name=application.name,
119
129
  url=f"{str(access.rpc_url).rstrip('/')}/mcp",
120
- auth=_ApplicationTokenAuth(access),
130
+ auth=_ApplicationTokenAuth(access, application.resource_release_uid),
121
131
  # The connection applies the tool's response deadline to its call; these HTTP limits
122
132
  # also bound initialization and catalog requests.
123
133
  timeout=httpx2.Timeout(
@@ -337,6 +347,7 @@ def _application_tools(
337
347
  *,
338
348
  mcp_tool: str | None = None,
339
349
  call_timeout_seconds: float | None = None,
350
+ held_by_start: bool = False,
340
351
  ) -> AgentToolResult:
341
352
  # A failure after the call was sent, which has a time limit by then, is the tool's;
342
353
  # a failure while the session opens is the application's.
@@ -348,6 +359,17 @@ def _application_tools(
348
359
  call_timeout_seconds if call_timeout_seconds is not None else open_timeout_seconds
349
360
  ),
350
361
  )
362
+ if held_by_start:
363
+ text = (
364
+ f"The {name} application was starting and the tool {mcp_tool} did not answer "
365
+ f"within {call_timeout_seconds:g} seconds. The call may or may not have run."
366
+ )
367
+ failure = {
368
+ "failure": "cold_start_timeout",
369
+ "phase": "request",
370
+ "runtime_access_state": "waking",
371
+ "timeout_seconds": call_timeout_seconds,
372
+ }
351
373
  if mcp_tool is not None:
352
374
  failure["mcp_tool"] = mcp_tool
353
375
  logger.warning(
@@ -556,14 +578,27 @@ def _application_tools(
556
578
  None,
557
579
  )
558
580
  call_timeout_seconds = declared if declared is not None else tool_timeout_seconds
581
+ auth = getattr(client, "_http_auth", None)
582
+ if isinstance(auth, _ApplicationTokenAuth):
583
+ auth.deadline = time.monotonic() + call_timeout_seconds
559
584
  result = await client.call_tool(
560
585
  tool_name, dict(tool_arguments), timeout_seconds=call_timeout_seconds
561
586
  )
562
587
  finally:
563
588
  await client.aclose()
564
589
  except Exception as error:
590
+ held_by_start = (
591
+ call_timeout_seconds is not None
592
+ and _timed_out(error)
593
+ # Asked once, after the fact: a request held by a start is not the tool's fault.
594
+ and await _turn_application_starting(application.resource_release_uid)
595
+ )
565
596
  return failed(
566
- "call_tool", error, mcp_tool=tool_name, call_timeout_seconds=call_timeout_seconds
597
+ "call_tool",
598
+ error,
599
+ mcp_tool=tool_name,
600
+ call_timeout_seconds=call_timeout_seconds,
601
+ held_by_start=held_by_start,
567
602
  )
568
603
  projected = mcp_tool_result(canonical_name=tool_name, result=result, display_name=name)
569
604
  details = dict(projected.details) if isinstance(projected.details, dict) else {}