langgraph-runtime-inmem 0.37.2__tar.gz → 0.38.0__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 (19) hide show
  1. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/PKG-INFO +1 -1
  2. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/__init__.py +1 -1
  3. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/database.py +33 -3
  4. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/inmem_stream.py +38 -21
  5. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/lifespan.py +14 -0
  6. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/ops.py +220 -28
  7. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/pyproject.toml +5 -1
  8. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/uv.lock +10 -10
  9. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/.gitignore +0 -0
  10. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/Makefile +0 -0
  11. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/README.md +0 -0
  12. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/_persistence.py +0 -0
  13. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/checkpoint.py +0 -0
  14. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/encryption.py +0 -0
  15. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/metrics.py +0 -0
  16. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/queue.py +0 -0
  17. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/retry.py +0 -0
  18. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/routes.py +0 -0
  19. {langgraph_runtime_inmem-0.37.2 → langgraph_runtime_inmem-0.38.0}/langgraph_runtime_inmem/store.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langgraph-runtime-inmem
3
- Version: 0.37.2
3
+ Version: 0.38.0
4
4
  Summary: Inmem implementation for the LangGraph API server.
5
5
  Author-email: Will Fu-Hinthorn <will@langchain.dev>
6
6
  License: Elastic-2.0
@@ -11,7 +11,7 @@ from langgraph_runtime_inmem import (
11
11
  store,
12
12
  )
13
13
 
14
- __version__ = "0.37.2"
14
+ __version__ = "0.38.0"
15
15
  __all__ = [
16
16
  "ops",
17
17
  "database",
@@ -185,12 +185,27 @@ def _load_ops_file() -> None:
185
185
  orphaned = [r for r in GLOBAL_STORE.get("runs") or [] if r["status"] == "running"]
186
186
  now = datetime.now(UTC)
187
187
  for run in orphaned:
188
- run["status"] = "pending"
188
+ # An interrupt recorded before the crash stands, so that run is not retried. A
189
+ # rollback needs the checkpointer, which starts later: the worker that picks the
190
+ # run up applies it before the graph runs.
191
+ run["status"] = (
192
+ "interrupted" if run.get("cancel_action") == "interrupt" else "pending"
193
+ )
189
194
  run["updated_at"] = now
190
- if orphaned:
195
+ # No worker will finish those runs, so nothing else moves their threads off busy.
196
+ stopped = {r["thread_id"] for r in orphaned if r["status"] == "interrupted"}
197
+ if stopped:
198
+ from langgraph_runtime_inmem.ops import _thread_status # noqa: PLC0415
199
+
200
+ conn = InMemConnectionProto()
201
+ for thread in GLOBAL_STORE.get("threads") or []:
202
+ if thread["thread_id"] in stopped:
203
+ thread["status"] = _thread_status(conn, thread["thread_id"])
204
+ retried = [str(r["run_id"]) for r in orphaned if r["status"] == "pending"]
205
+ if retried:
191
206
  logger.warning(
192
207
  "Retrying runs left running by a previous process that did not shut down cleanly",
193
- run_ids=[str(r["run_id"]) for r in orphaned],
208
+ run_ids=retried,
194
209
  )
195
210
 
196
211
 
@@ -206,6 +221,19 @@ def upgrade_thread_status_facts(store: dict[str, Any]) -> None:
206
221
  store.setdefault("thread_failed", {})
207
222
 
208
223
 
224
+ def upgrade_thread_priority_settings(store: dict[str, Any]) -> None:
225
+ """Give threads saved by older versions, before priority settings, none yet."""
226
+ for thread in store.get("threads") or ():
227
+ for key in ("run_priority", "fairness_group"):
228
+ thread.setdefault(key, None)
229
+
230
+
231
+ def upgrade_run_priorities(store: dict[str, Any]) -> None:
232
+ """Give runs saved by older versions, before runs had a priority, the default level."""
233
+ for run in store.get("runs") or ():
234
+ run.setdefault("priority", 0)
235
+
236
+
209
237
  def _set_aside_ops_file(reason: Exception) -> None:
210
238
  """Rename an unloadable ops file to ``<file>.bak-<UTC timestamp>`` and log why."""
211
239
  from langgraph_api.graph import graph_file_module_names # noqa: PLC0415
@@ -293,6 +321,8 @@ async def start_pool() -> None:
293
321
  if not GLOBAL_STORE.get(k):
294
322
  GLOBAL_STORE[k] = []
295
323
  upgrade_thread_status_facts(GLOBAL_STORE)
324
+ upgrade_run_priorities(GLOBAL_STORE)
325
+ upgrade_thread_priority_settings(GLOBAL_STORE)
296
326
  # Remove agents that were created by the system to avoid removed graphs
297
327
  if GLOBAL_STORE.get("assistants"):
298
328
  for a in GLOBAL_STORE["assistants"]:
@@ -13,12 +13,19 @@ def _ensure_uuid(id: str | UUID) -> UUID:
13
13
  return UUID(id) if isinstance(id, str) else id
14
14
 
15
15
 
16
- def _generate_ms_seq_id() -> str:
17
- """Generate a Redis-like millisecond-sequence ID (e.g., '1234567890123-0')"""
18
- # Get current time in milliseconds
19
- ms = int(time.time() * 1000)
20
- # For simplicity, always use sequence 0 since we're not handling high throughput
21
- return f"{ms}-0"
16
+ def stream_id_key(stream_id: str | bytes) -> tuple[int, int]:
17
+ """Numeric order of a `<ms>-<seq>` stream id, for comparing resume cursors.
18
+
19
+ Clients send `-` or a negative id such as `-1` to replay everything, so
20
+ those sort first, and `<ms>` or `<ms>-*` to replay that whole millisecond.
21
+ Raises ValueError for an id without a numeric `<ms>`.
22
+ """
23
+ if isinstance(stream_id, bytes):
24
+ stream_id = stream_id.decode()
25
+ if stream_id.startswith("-"):
26
+ return (-1, -1)
27
+ ms, _, seq = stream_id.partition("-")
28
+ return int(ms), int(seq) if seq.isdigit() else -1
22
29
 
23
30
 
24
31
  @dataclass
@@ -65,6 +72,14 @@ class StreamManager:
65
72
  self.message_stores = defaultdict(
66
73
  lambda: defaultdict(list[Message])
67
74
  ) # Dict[str, List[Message]]
75
+ self._last_id = (0, -1)
76
+
77
+ def _next_id(self) -> bytes:
78
+ """Strictly increasing, as a resume replays only the ids above its cursor."""
79
+ ms = int(time.time() * 1000)
80
+ last_ms, last_seq = self._last_id
81
+ self._last_id = (ms, 0) if ms > last_ms else (last_ms, last_seq + 1)
82
+ return f"{self._last_id[0]}-{self._last_id[1]}".encode()
68
83
 
69
84
  def get_queues(
70
85
  self, run_id: UUID | str, thread_id: UUID | str | None
@@ -109,8 +124,7 @@ class StreamManager:
109
124
  else:
110
125
  thread_id = _ensure_uuid(thread_id)
111
126
 
112
- message.id = _generate_ms_seq_id().encode()
113
- # For resumable run streams, embed the generated message ID into the frame
127
+ message.id = self._next_id()
114
128
  topic = message.topic.decode()
115
129
  if resumable:
116
130
  self.message_stores[thread_id][run_id].append(message)
@@ -131,8 +145,8 @@ class StreamManager:
131
145
  message: Message,
132
146
  ) -> None:
133
147
  thread_id = _ensure_uuid(thread_id)
134
- message.id = _generate_ms_seq_id().encode()
135
- queues = self.thread_streams[thread_id]
148
+ message.id = self._next_id()
149
+ queues = self.thread_streams.get(thread_id, ())
136
150
  coros = [queue.put(message) for queue in queues]
137
151
  results = await asyncio.gather(*coros, return_exceptions=True)
138
152
  for result in results:
@@ -182,6 +196,13 @@ class StreamManager:
182
196
  if not self.queues[thread_id][run_id]:
183
197
  del self.queues[thread_id][run_id]
184
198
 
199
+ async def remove_thread_stream(self, thread_id: UUID | str, queue: asyncio.Queue):
200
+ thread_id = _ensure_uuid(thread_id)
201
+ if thread_id in self.thread_streams:
202
+ self.thread_streams[thread_id].remove(queue)
203
+ if not self.thread_streams[thread_id]:
204
+ del self.thread_streams[thread_id]
205
+
185
206
  async def remove_control_queue(
186
207
  self, run_id: UUID | str, thread_id: UUID | str | None, queue: asyncio.Queue
187
208
  ):
@@ -207,19 +228,15 @@ class StreamManager:
207
228
  thread_id = THREADLESS_KEY
208
229
  else:
209
230
  thread_id = _ensure_uuid(thread_id)
210
- if message_id is None:
231
+ if message_id is None or thread_id not in self.message_stores:
211
232
  return
212
233
  try:
213
- # Handle ms-seq format (e.g., "1234567890123-0")
214
- if thread_id in self.message_stores:
215
- for message in self.message_stores[thread_id][run_id]:
216
- if message.id.decode() > message_id:
217
- yield message
218
- except TypeError:
219
- # Try integer format if ms-seq fails
220
- message_idx = int(message_id) + 1
221
- if run_id in self.message_stores:
222
- yield from self.message_stores[thread_id][run_id][message_idx:]
234
+ after = stream_id_key(message_id)
235
+ except ValueError:
236
+ return
237
+ for message in self.message_stores[thread_id][run_id]:
238
+ if stream_id_key(message.id) > after:
239
+ yield message
223
240
 
224
241
  def get_queues_by_thread_id(self, thread_id: UUID | str) -> list[asyncio.Queue]:
225
242
  """Get all queues for a specific thread_id across all runs."""
@@ -136,6 +136,20 @@ async def lifespan(
136
136
  _LAST_LIFESPAN_ERROR = exc
137
137
  await _log_graph_load_failure(exc)
138
138
  raise
139
+ try:
140
+ from langgraph_api.priority_settings import ( # noqa: PLC0415
141
+ get_priority_callback,
142
+ )
143
+ except ImportError:
144
+ # An older langgraph-api has no priority callback.
145
+ pass
146
+ else:
147
+ # Loaded now so a bad path fails startup, not every run create.
148
+ if get_priority_callback() is not None:
149
+ await logger.awarning(
150
+ "The in-memory runtime stores each thread's priority settings"
151
+ " but starts runs in the order they were created"
152
+ )
139
153
  if config.N_JOBS_PER_WORKER > 0:
140
154
  tg.create_task(queue_with_signal())
141
155
 
@@ -32,6 +32,7 @@ from langgraph_runtime_inmem.inmem_stream import (
32
32
  ContextQueue,
33
33
  Message,
34
34
  get_stream_manager,
35
+ stream_id_key,
35
36
  )
36
37
 
37
38
  if typing.TYPE_CHECKING:
@@ -54,6 +55,7 @@ if typing.TYPE_CHECKING:
54
55
  PoolStats,
55
56
  QueueStats,
56
57
  Run,
58
+ RunCancelProgress,
57
59
  RunSelectField,
58
60
  RunStatus,
59
61
  StreamMode,
@@ -263,6 +265,18 @@ def _ensure_uuid(id_: str | uuid.UUID | None) -> uuid.UUID:
263
265
  return id_
264
266
 
265
267
 
268
+ _EPOCH = datetime(1970, 1, 1, tzinfo=UTC)
269
+
270
+
271
+ def _accepted_at_or_before(run_id: uuid.UUID, boundary_ms: int) -> bool:
272
+ """Whether a run was accepted at or before a Unix-millisecond boundary.
273
+
274
+ Run IDs are UUIDv7, whose first 48 bits are the millisecond they were
275
+ generated. A run whose ID isn't a UUIDv7 counts as accepted before.
276
+ """
277
+ return run_id.version != 7 or run_id.int >> 80 <= boundary_ms
278
+
279
+
266
280
  def _ensure_datetime(value: datetime | str | None) -> datetime | None:
267
281
  if value is None or isinstance(value, datetime):
268
282
  return value
@@ -1391,6 +1405,8 @@ class Threads(Authenticated):
1391
1405
  "status": "idle",
1392
1406
  "config": {},
1393
1407
  "values": None,
1408
+ "run_priority": None,
1409
+ "fairness_group": None,
1394
1410
  }
1395
1411
 
1396
1412
  # Add to store
@@ -1547,6 +1563,9 @@ class Threads(Authenticated):
1547
1563
 
1548
1564
  now = datetime.now(UTC)
1549
1565
 
1566
+ if run.get("cancel_action") == "rollback":
1567
+ # A rollback recorded while the run ran wins over the signal its worker saw first, as in Go.
1568
+ run_status, checkpoint, exception = "rollback", None, None
1550
1569
  if run_status == "rollback":
1551
1570
  await Runs.delete(conn, run_id, thread_id=run["thread_id"])
1552
1571
  else:
@@ -1758,6 +1777,9 @@ class Threads(Authenticated):
1758
1777
  "values": copy.deepcopy(original_thread.get("values")),
1759
1778
  "interrupts": copy.deepcopy(original_thread.get("interrupts", {})),
1760
1779
  "error": copy.deepcopy(original_thread.get("error")),
1780
+ # A copy gets its own priority settings at its first run.
1781
+ "run_priority": None,
1782
+ "fairness_group": None,
1761
1783
  }
1762
1784
  # Add new thread to store
1763
1785
  conn.store["threads"].append(
@@ -2399,8 +2421,7 @@ class Threads(Authenticated):
2399
2421
  ):
2400
2422
  all_events.append((message, run_id))
2401
2423
 
2402
- # Sort by message ID (which is ms-seq format)
2403
- all_events.sort(key=lambda x: x[0].id.decode())
2424
+ all_events.sort(key=lambda x: stream_id_key(x[0].id))
2404
2425
 
2405
2426
  # Yield sorted events
2406
2427
  for message, run_id in all_events:
@@ -2522,9 +2543,14 @@ class Threads(Authenticated):
2522
2543
  raise
2523
2544
  finally:
2524
2545
  # Clean up all created queues
2546
+ thread_key = _ensure_uuid(thread_id)
2525
2547
  for run_id, queue in created_queues:
2526
2548
  try:
2527
- await stream_manager.remove_queue(run_id, thread_id, queue)
2549
+ # subscribe() files the thread-level queue under the thread id.
2550
+ if run_id == thread_key:
2551
+ await stream_manager.remove_thread_stream(thread_id, queue)
2552
+ else:
2553
+ await stream_manager.remove_queue(run_id, thread_id, queue)
2528
2554
  except Exception:
2529
2555
  # Ignore cleanup errors
2530
2556
  pass
@@ -2864,6 +2890,17 @@ class Runs(Authenticated):
2864
2890
  from langgraph_api.schema import Run, Thread # noqa: PLC0415
2865
2891
  from langgraph_api.utils import uuid7 # noqa: PLC0415
2866
2892
 
2893
+ try:
2894
+ from langgraph_api.priority_settings import ( # noqa: PLC0415
2895
+ assign_priority_settings,
2896
+ get_priority_callback,
2897
+ )
2898
+ except ImportError:
2899
+ # An older langgraph-api has no priority callback.
2900
+ priority_callback = None
2901
+ else:
2902
+ priority_callback = get_priority_callback()
2903
+
2867
2904
  assistant_id = _ensure_uuid(assistant_id)
2868
2905
  assistant = next(
2869
2906
  (a for a in conn.store["assistants"] if a["assistant_id"] == assistant_id),
@@ -2936,6 +2973,53 @@ class Runs(Authenticated):
2936
2973
 
2937
2974
  _reject_existing_run(conn.store["runs"], run_id)
2938
2975
 
2976
+ # A thread gets its priority settings at its first run, from the deployment's
2977
+ # priority callback, before the thread is created or updated. They're kept as
2978
+ # the callback returned them; inmem never orders runs by them.
2979
+ priority_settings = None
2980
+ creates_thread = not existing_thread and (
2981
+ thread_id is None or if_not_exists == "create"
2982
+ )
2983
+ if priority_callback is not None and (
2984
+ creates_thread
2985
+ or (
2986
+ existing_thread is not None
2987
+ and existing_thread.get("run_priority") is None
2988
+ )
2989
+ ):
2990
+ ids = {"graph_id": assistant["graph_id"], "assistant_id": str(assistant_id)}
2991
+ priority_settings = await assign_priority_settings(
2992
+ priority_callback,
2993
+ thread_id=str(thread_id) if thread_id is not None else None,
2994
+ thread_metadata=(
2995
+ {**ids, **(config.get("metadata") or {}), **metadata}
2996
+ if creates_thread
2997
+ else Runs._merge_jsonb(existing_thread["metadata"], ids)
2998
+ ),
2999
+ assistant_id=str(assistant_id),
3000
+ graph_id=assistant["graph_id"],
3001
+ assistant_metadata=assistant.get("metadata") or {},
3002
+ run_metadata=metadata,
3003
+ run_kwargs=kwargs,
3004
+ )
3005
+ # Another first run on the thread may have gone ahead while the callback
3006
+ # ran: read the thread again, and keep its settings if it has them now.
3007
+ if thread_id is not None:
3008
+ existing_thread = next(
3009
+ (t for t in conn.store["threads"] if t["thread_id"] == thread_id),
3010
+ None,
3011
+ )
3012
+ if encryption_enabled and existing_thread:
3013
+ existing_thread = await _decrypt_from_storage(
3014
+ existing_thread, "thread"
3015
+ )
3016
+ if (
3017
+ existing_thread
3018
+ and filters
3019
+ and not _check_filter_match(existing_thread["metadata"], filters)
3020
+ ):
3021
+ return _empty_generator()
3022
+
2939
3023
  if not existing_thread and (thread_id is None or if_not_exists == "create"):
2940
3024
  # Create new thread
2941
3025
  if thread_id is None:
@@ -2962,10 +3046,18 @@ class Runs(Authenticated):
2962
3046
  created_at=datetime.now(UTC),
2963
3047
  updated_at=datetime.now(UTC),
2964
3048
  values=None,
3049
+ run_priority=priority_settings["priority"]
3050
+ if priority_settings
3051
+ else None,
3052
+ fairness_group=(
3053
+ priority_settings["fairness_group"] if priority_settings else None
3054
+ ),
2965
3055
  )
2966
3056
 
2967
- await logger.ainfo("Creating thread", thread_id=thread_id)
3057
+ # Append before the first await, so a concurrent first run that read the
3058
+ # thread after its priority callback sees this one instead of adding another.
2968
3059
  conn.store["threads"].append(thread)
3060
+ await logger.ainfo("Creating thread", thread_id=thread_id)
2969
3061
  elif existing_thread:
2970
3062
  # Update existing thread
2971
3063
  if existing_thread["status"] != "busy":
@@ -3011,6 +3103,30 @@ class Runs(Authenticated):
3011
3103
 
3012
3104
  return _return_inflight()
3013
3105
 
3106
+ # As in Go core, an existing thread gets the callback's result only once a run
3107
+ # is created on it, and only if it still has no settings; while the deployment
3108
+ # has a callback, every run takes the thread's priority. The check reads the
3109
+ # stored thread, not a decrypted copy, which a concurrent first run's settings
3110
+ # may have reached first.
3111
+ if existing_thread:
3112
+ stored = next(
3113
+ (t for t in conn.store["threads"] if t["thread_id"] == thread_id),
3114
+ existing_thread,
3115
+ )
3116
+ if priority_settings and stored.get("run_priority") is None:
3117
+ stored.update(
3118
+ run_priority=priority_settings["priority"],
3119
+ fairness_group=priority_settings["fairness_group"],
3120
+ )
3121
+ existing_thread.update(
3122
+ {key: stored.get(key) for key in ("run_priority", "fairness_group")}
3123
+ )
3124
+ thread_priority = (
3125
+ (existing_thread or thread).get("run_priority")
3126
+ if priority_callback is not None
3127
+ else None
3128
+ )
3129
+
3014
3130
  # Create new run
3015
3131
  configurable = Runs._merge_jsonb(
3016
3132
  Runs._get_configurable(assistant["config"]),
@@ -3072,6 +3188,7 @@ class Runs(Authenticated):
3072
3188
  updated_at=datetime.now(UTC),
3073
3189
  langsmith_session_name=langsmith_session_name,
3074
3190
  cancel_reason=None,
3191
+ priority=thread_priority if thread_priority is not None else 0,
3075
3192
  )
3076
3193
  stored_run = new_run
3077
3194
  encrypted_thread = None
@@ -3293,12 +3410,88 @@ class Runs(Authenticated):
3293
3410
  return
3294
3411
  raise HTTPException(status_code=404, detail="No runs found to cancel.")
3295
3412
 
3413
+ cancelable_runs = await Runs._cancel_runs(
3414
+ conn, candidate_runs, action=action, reason=reason
3415
+ )
3416
+ if not cancelable_runs:
3417
+ # When cancelling by assistant_id, it's valid to have no cancelable runs.
3418
+ # An interrupt that finds a rollback under way leaves it alone and
3419
+ # succeeds, as in the Go runtime.
3420
+ if assistant_id is not None or any(
3421
+ run["status"] == "running" and run.get("cancel_action") == "rollback"
3422
+ for run in candidate_runs
3423
+ ):
3424
+ return
3425
+ raise HTTPException(
3426
+ status_code=404,
3427
+ detail="No matching runs to cancel. Please verify the thread ID and run IDs are correct, and the runs haven't been deleted or completed.",
3428
+ )
3429
+
3430
+ await logger.ainfo(
3431
+ "Cancelled runs",
3432
+ run_ids=[str(r["run_id"]) for r in cancelable_runs],
3433
+ thread_id=str(thread_id) if thread_id else None,
3434
+ status=status,
3435
+ action=action,
3436
+ )
3437
+
3438
+ @staticmethod
3439
+ async def cancel_accepted_before(
3440
+ conn: InMemConnectionProto,
3441
+ accepted_before: datetime,
3442
+ *,
3443
+ reason: str,
3444
+ ) -> RunCancelProgress:
3445
+ """Interrupt every in-flight run accepted at or before `accepted_before`.
3446
+
3447
+ Applies no auth filters, so only the operator route may call it. As with
3448
+ `cancel`, a signalled running run stays running until its worker stops it,
3449
+ so `finishing` counts the running runs still being stopped.
3450
+ """
3451
+ boundary_ms = (accepted_before - _EPOCH) // timedelta(milliseconds=1)
3452
+
3453
+ def in_flight_targets() -> list[dict]:
3454
+ return [
3455
+ run
3456
+ for run in conn.store["runs"]
3457
+ if run["status"] in ("pending", "running")
3458
+ and _accepted_at_or_before(_ensure_uuid(run["run_id"]), boundary_ms)
3459
+ ]
3460
+
3461
+ cancelled = await Runs._cancel_runs(
3462
+ conn, in_flight_targets(), action="interrupt", reason=reason
3463
+ )
3464
+ if cancelled:
3465
+ await logger.ainfo(
3466
+ "Cancelled runs accepted before boundary",
3467
+ run_ids=[str(r["run_id"]) for r in cancelled],
3468
+ accepted_before=accepted_before.isoformat(),
3469
+ )
3470
+ remaining = in_flight_targets()
3471
+ finishing = sum(1 for run in remaining if run.get("cancel_action"))
3472
+ return {"not_yet_reached": len(remaining) - finishing, "finishing": finishing}
3473
+
3474
+ @staticmethod
3475
+ async def _cancel_runs(
3476
+ conn: InMemConnectionProto,
3477
+ candidate_runs: list[dict],
3478
+ *,
3479
+ action: Literal["interrupt", "rollback"],
3480
+ reason: str | None,
3481
+ ) -> list[dict]:
3482
+ """Signal each candidate run and cancel the ones still in flight.
3483
+
3484
+ Returns the runs that were in flight. When none were, nothing is changed.
3485
+ """
3296
3486
  stream_manager = get_stream_manager()
3297
3487
  coros = []
3298
3488
  cancelable_runs = []
3299
3489
  pending_thread_ids: set[UUID] = set()
3300
3490
 
3301
3491
  for run in candidate_runs:
3492
+ if action == "interrupt" and run.get("cancel_action") == "rollback":
3493
+ # A rollback already under way wins; its worker finalizes it.
3494
+ continue
3302
3495
  run_id = run["run_id"]
3303
3496
  control_message = Message(
3304
3497
  topic=f"run:{run_id}:control".encode(),
@@ -3314,9 +3507,13 @@ class Runs(Authenticated):
3314
3507
  cancelable_runs.append(run)
3315
3508
  if run["status"] == "pending":
3316
3509
  pending_thread_ids.add(run["thread_id"])
3317
- if action == "rollback" and run["status"] == "running":
3318
- # Its worker finalizes the rollback; staying "running" holds the thread's next run until then.
3319
- pass
3510
+ if run["status"] == "running":
3511
+ # Its worker finalizes the cancel; staying "running" holds the thread's next run until then.
3512
+ # A later interrupt keeps the first one's reason, which names who stopped the run.
3513
+ if action == "rollback" or not run.get("cancel_action"):
3514
+ run["cancel_reason"] = reason
3515
+ run["cancel_action"] = action
3516
+ run["updated_at"] = datetime.now(tz=UTC)
3320
3517
  elif queues or action != "rollback":
3321
3518
  run["status"] = "interrupted"
3322
3519
  run["cancel_reason"] = reason
@@ -3335,13 +3532,7 @@ class Runs(Authenticated):
3335
3532
  )
3336
3533
 
3337
3534
  if not cancelable_runs:
3338
- # When cancelling by assistant_id, it's valid to have no cancelable runs
3339
- if assistant_id is not None:
3340
- return
3341
- raise HTTPException(
3342
- status_code=404,
3343
- detail="No matching runs to cancel. Please verify the thread ID and run IDs are correct, and the runs haven't been deleted or completed.",
3344
- )
3535
+ return []
3345
3536
 
3346
3537
  if coros:
3347
3538
  await asyncio.gather(*coros)
@@ -3357,13 +3548,7 @@ class Runs(Authenticated):
3357
3548
  )
3358
3549
  thread["updated_at"] = datetime.now(tz=UTC)
3359
3550
 
3360
- await logger.ainfo(
3361
- "Cancelled runs",
3362
- run_ids=[str(r["run_id"]) for r in cancelable_runs],
3363
- thread_id=str(thread_id) if thread_id else None,
3364
- status=status,
3365
- action=action,
3366
- )
3551
+ return cancelable_runs
3367
3552
 
3368
3553
  @staticmethod
3369
3554
  async def search(
@@ -3640,15 +3825,22 @@ async def listen_for_cancellation(
3640
3825
 
3641
3826
  if control_key := stream_manager.get_control_key(run_id, thread_id):
3642
3827
  _handle_control_message(control_key)
3828
+ else:
3829
+ # Control messages do not survive a restart; a cancel recorded on the run does.
3830
+ runs = InMemConnectionProto().store["runs"]
3831
+ run = next((r for r in runs if r["run_id"] == run_id), None)
3832
+ if run and run.get("cancel_action"):
3833
+ _handle_control_message(
3834
+ Message(
3835
+ topic=f"run:{run_id}:control".encode(),
3836
+ data=run["cancel_action"].encode(),
3837
+ reason=run.get("cancel_reason"),
3838
+ )
3839
+ )
3643
3840
 
3841
+ # Runs.enter cancels this task when the run ends.
3644
3842
  while not done.is_set():
3645
- try:
3646
- # This task gets cancelled when Runs.enter exits anyway,
3647
- # so we can have a pretty lengthy timeout here
3648
- message = await asyncio.wait_for(queue.get(), timeout=240)
3649
- if _handle_control_message(message):
3650
- break
3651
- except TimeoutError:
3843
+ if _handle_control_message(await queue.get()):
3652
3844
  break
3653
3845
 
3654
3846
 
@@ -65,6 +65,10 @@ exclude = [
65
65
 
66
66
 
67
67
  [tool.ruff]
68
- lint.select = ["E", "F", "UP", "B", "I", "PLC0415"]
68
+ lint.select = ["E", "F", "UP", "B", "I", "PLC0415", "TID251"]
69
69
  lint.ignore = ["E501", "B008"]
70
70
  target-version = "py311"
71
+
72
+ [tool.ruff.lint.flake8-tidy-imports.banned-api]
73
+ "asyncio.set_event_loop_policy".msg = "Pass a loop_factory to asyncio.Runner instead."
74
+ "uvloop.install".msg = "Use langgraph_api.asyncio.run_main_loop instead."
@@ -1,5 +1,5 @@
1
1
  version = 1
2
- revision = 3
2
+ revision = 5
3
3
  requires-python = ">=3.11.0"
4
4
 
5
5
  [[package]]
@@ -38,11 +38,11 @@ wheels = [
38
38
 
39
39
  [[package]]
40
40
  name = "certifi"
41
- version = "2026.4.22"
41
+ version = "2026.10.19"
42
42
  source = { registry = "https://pypi.org/simple" }
43
- sdist = { url = "https://files.pythonhosted.org/packages/25/ee/6caf7a40c36a1220410afe15a1cc64993a1f864871f698c0f93acb72842a/certifi-2026.4.22.tar.gz", hash = "sha256:8d455352a37b71bf76a79caa83a3d6c25afee4a385d632127b6afb3963f1c580", size = 137077, upload-time = "2026-04-22T11:26:11.191Z" }
43
+ sdist = { url = "https://files.pythonhosted.org/packages/a6/bd/1436c50cc46699de0230fbaacc1d1de7bfec413176099949e9d5112e88f2/certifi-2026.10.19.tar.gz", hash = "sha256:035c1e85572b141bf1c3a3eff0e33a8eb74917522f2559075ff8d2fea780a1ae", size = 137598, upload-time = "2026-10-20T02:24:16.604Z" }
44
44
  wheels = [
45
- { url = "https://files.pythonhosted.org/packages/22/30/7cd8fdcdfbc5b869528b079bfb76dcdf6056b1a2097a662e5e8c04f42965/certifi-2026.4.22-py3-none-any.whl", hash = "sha256:3cb2210c8f88ba2318d29b0388d1023c8492ff72ecdde4ebdaddbb13a31b1c4a", size = 135707, upload-time = "2026-04-22T11:26:09.372Z" },
45
+ { url = "https://files.pythonhosted.org/packages/c4/51/29046e48f693c54e5b7412216d447a1d8362c9653148d98c02cdf65fa733/certifi-2026.10.19-py3-none-any.whl", hash = "sha256:42d35340cc5b9ba78e818ddbff0c5b1c2b779de448a340fd1b989f5fa8670e1b", size = 136464, upload-time = "2026-10-20T02:24:15.072Z" },
46
46
  ]
47
47
 
48
48
  [[package]]
@@ -388,7 +388,7 @@ dev = [
388
388
 
389
389
  [[package]]
390
390
  name = "langgraph-sdk"
391
- version = "0.4.2"
391
+ version = "0.4.4"
392
392
  source = { registry = "https://pypi.org/simple" }
393
393
  dependencies = [
394
394
  { name = "httpx" },
@@ -397,9 +397,9 @@ dependencies = [
397
397
  { name = "orjson" },
398
398
  { name = "websockets" },
399
399
  ]
400
- sdist = { url = "https://files.pythonhosted.org/packages/b4/2b/bd8ac26d4e97f6df88ef05ce5b6a38945a3903e1025d926f4752aa88aa97/langgraph_sdk-0.4.2.tar.gz", hash = "sha256:b88f0f5f6328ac0680d6790614a905b2bcfa257f2276dba4e38f0e86db0aa738", size = 348327, upload-time = "2026-06-01T17:51:19.856Z" }
400
+ sdist = { url = "https://files.pythonhosted.org/packages/3f/ae/91446c1fffa04a2dc1f81afbfb5bfff3590452891a72bd9098a656ab2657/langgraph_sdk-0.4.4.tar.gz", hash = "sha256:4e651ffa09de695681579396375377bdde23bedbc8e35b070e615cbd5af7da8b", size = 345789, upload-time = "2026-08-27T21:25:22.593Z" }
401
401
  wheels = [
402
- { url = "https://files.pythonhosted.org/packages/a0/05/aac507337cceae773c2cc9ab91eb6301963af7aeeb55b4217a00e15aff17/langgraph_sdk-0.4.2-py3-none-any.whl", hash = "sha256:75fa5096c1177ce39c847096a8fe3745ffd480ddb412995f836e9f5f884c43dd", size = 160521, upload-time = "2026-06-01T17:51:18.849Z" },
402
+ { url = "https://files.pythonhosted.org/packages/43/d5/2ad7f3a835c6fcebf58b6669552536ecd52d2dfe4cf49c6c36648fd83572/langgraph_sdk-0.4.4-py3-none-any.whl", hash = "sha256:39afe416c91742925e6f8a93715f566d499b36e1b636b804a4ffe3190e4f4e64", size = 162270, upload-time = "2026-08-27T21:25:21.072Z" },
403
403
  ]
404
404
 
405
405
  [[package]]
@@ -1199,14 +1199,14 @@ wheels = [
1199
1199
 
1200
1200
  [[package]]
1201
1201
  name = "werkzeug"
1202
- version = "3.1.8"
1202
+ version = "3.1.9"
1203
1203
  source = { registry = "https://pypi.org/simple" }
1204
1204
  dependencies = [
1205
1205
  { name = "markupsafe" },
1206
1206
  ]
1207
- sdist = { url = "https://files.pythonhosted.org/packages/dd/b2/381be8cfdee792dd117872481b6e378f85c957dd7c5bca38897b08f765fd/werkzeug-3.1.8.tar.gz", hash = "sha256:9bad61a4268dac112f1c5cd4630a56ede601b6ed420300677a869083d70a4c44", size = 875852, upload-time = "2026-04-02T18:49:14.268Z" }
1207
+ sdist = { url = "https://files.pythonhosted.org/packages/a4/34/4dd12fc8bb7d61c91467ec3efe415ffa7d5456f799954b40c5bbaeae470e/werkzeug-3.1.9.tar.gz", hash = "sha256:55ca7c70a75689be937aa27f8ff4b018f06ff4838fc73045560bf0f5a1291060", size = 940188, upload-time = "2026-09-27T18:33:41.637Z" }
1208
1208
  wheels = [
1209
- { url = "https://files.pythonhosted.org/packages/93/8c/2e650f2afeb7ee576912636c23ddb621c91ac6a98e66dc8d29c3c69446e1/werkzeug-3.1.8-py3-none-any.whl", hash = "sha256:63a77fb8892bf28ebc3178683445222aa500e48ebad5ec77b0ad80f8726b1f50", size = 226459, upload-time = "2026-04-02T18:49:12.72Z" },
1209
+ { url = "https://files.pythonhosted.org/packages/a1/38/df03f564f43cec2684823f3cccae1a652ee7face1cbaa76fb223096e64d7/werkzeug-3.1.9-py3-none-any.whl", hash = "sha256:6392e50c78460ba618e5b21f08a71f59c99ce99cdc6cf6e3dd7e6ccca8754fab", size = 228700, upload-time = "2026-09-27T18:33:39.685Z" },
1210
1210
  ]
1211
1211
 
1212
1212
  [[package]]