Module: MCPClient::Client::TaskLifetimes

Included in:
TaskRegistry
Defined in:
lib/mcp_client/client/task_lifetimes.rb

Overview

Which task a task id names, over the life of one server session.

A task id is unique within a session, so a server that answers with an id it has already handed out has ended that task and started another: every CreateTaskResult begins a lifetime of its own, and a wait, a hold, an answer or a cancel of one lifetime never reaches another. A lifetime is a number drawn from the session's own counter, never reused and never restarted, so two lifetimes stay distinguishable even after the counters of the ids created longest ago are pruned.

A request is bound to the lifetime it is about (see #task_lifetime_pin) rather than cleared by a preflight: the binding is checked at the wire, where a CreateTaskResult a concurrent call records is already visible, and again before the answer is acted on. Mixed into TaskRegistry, whose task states and in-flight key registry it reads to decide what a prune may forget.

Constant Summary collapse

MAX_TRACKED_TASK_LIFETIMES =

How many task ids one client keeps a lifetime counter for. The counter has to outlive the task's own bookkeeping — that is the point: a creation under an id this client has already seen is a different task, whether or not anything of the previous one is still around — so on a sessionless connection, where the epoch never moves and nothing else prunes it, the map would otherwise grow with every task ever created. It bounds the ids of tasks that are over: a task still running keeps its lifetime however many ids follow it (see #prune_task_lifetimes).

4096
TRACKED_TASK_LIFETIMES_LOW_WATER =

How far a prune goes once the cap is passed: dropping a batch keeps the scan amortized instead of walking the map on every creation.

(MAX_TRACKED_TASK_LIFETIMES * 3) / 4