langgraph-runtime-inmem 0.39.0.dev1__tar.gz → 0.39.0.dev3__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.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/PKG-INFO +1 -1
  2. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/__init__.py +1 -1
  3. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/encryption.py +1 -0
  4. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/inmem_stream.py +34 -17
  5. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/ops.py +59 -57
  6. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/uv.lock +3 -3
  7. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/.gitignore +0 -0
  8. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/Makefile +0 -0
  9. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/README.md +0 -0
  10. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/_persistence.py +0 -0
  11. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/checkpoint.py +0 -0
  12. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/database.py +0 -0
  13. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/lifespan.py +0 -0
  14. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/metrics.py +0 -0
  15. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/queue.py +0 -0
  16. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/retry.py +0 -0
  17. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/routes.py +0 -0
  18. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/langgraph_runtime_inmem/store.py +0 -0
  19. {langgraph_runtime_inmem-0.39.0.dev1 → langgraph_runtime_inmem-0.39.0.dev3}/pyproject.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langgraph-runtime-inmem
3
- Version: 0.39.0.dev1
3
+ Version: 0.39.0.dev3
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.39.0.dev1"
14
+ __version__ = "0.39.0.dev3"
15
15
  __all__ = [
16
16
  "ops",
17
17
  "database",
@@ -55,6 +55,7 @@ NEVER_ENCRYPT_FIELDS = frozenset(
55
55
  "langgraph_plan",
56
56
  "langgraph_host",
57
57
  "langgraph_api_url",
58
+ "langgraph_graph_load_ms",
58
59
  "langgraph_request_id",
59
60
  "langgraph_auth_user_id",
60
61
  "langgraph_auth_permissions",
@@ -1,4 +1,5 @@
1
1
  import asyncio
2
+ import bisect
2
3
  import logging
3
4
  import time
4
5
  from collections import defaultdict
@@ -35,6 +36,8 @@ class Message:
35
36
  id: bytes | None = None
36
37
  # Control messages only: server-generated explanation of an interrupt/rollback
37
38
  reason: str | None = None
39
+ # Stored for replay, so a client can resume after its id.
40
+ resumable: bool = False
38
41
 
39
42
 
40
43
  class ContextQueue(asyncio.Queue):
@@ -127,6 +130,8 @@ class StreamManager:
127
130
  message.id = self._next_id()
128
131
  topic = message.topic.decode()
129
132
  if resumable:
133
+ message.resumable = True
134
+ self._drop_expired()
130
135
  self.message_stores[thread_id][run_id].append(message)
131
136
  if "control" in topic:
132
137
  self.control_keys[thread_id][run_id] = message
@@ -146,6 +151,10 @@ class StreamManager:
146
151
  ) -> None:
147
152
  thread_id = _ensure_uuid(thread_id)
148
153
  message.id = self._next_id()
154
+ # Thread-level events (state updates) replay too, filed under the thread id.
155
+ message.resumable = True
156
+ self._drop_expired()
157
+ self.message_stores[thread_id][thread_id].append(message)
149
158
  queues = self.thread_streams.get(thread_id, ())
150
159
  coros = [queue.put(message) for queue in queues]
151
160
  results = await asyncio.gather(*coros, return_exceptions=True)
@@ -228,16 +237,36 @@ class StreamManager:
228
237
  thread_id = THREADLESS_KEY
229
238
  else:
230
239
  thread_id = _ensure_uuid(thread_id)
231
- if message_id is None or thread_id not in self.message_stores:
240
+ if message_id is None:
241
+ return
242
+ self._drop_expired()
243
+ if thread_id not in self.message_stores:
232
244
  return
233
245
  try:
234
246
  after = stream_id_key(message_id)
235
247
  except ValueError:
236
248
  return
237
- for message in self.message_stores[thread_id][run_id]:
249
+ # A copy: a write while the replay is sent can drop expired entries from the front.
250
+ for message in list(self.message_stores[thread_id].get(run_id, ())):
238
251
  if stream_id_key(message.id) > after:
239
252
  yield message
240
253
 
254
+ def _drop_expired(self) -> None:
255
+ """Forget stored events older than the resumable TTL, as the Redis cache does."""
256
+ from langgraph_api.config import RESUMABLE_STREAM_TTL_SECONDS # noqa: PLC0415
257
+
258
+ oldest_ms = int(time.time() * 1000) - RESUMABLE_STREAM_TTL_SECONDS * 1000
259
+ for thread_id, runs in list(self.message_stores.items()):
260
+ for run_id, messages in list(runs.items()):
261
+ expired = bisect.bisect_left(
262
+ messages, oldest_ms, key=lambda m: stream_id_key(m.id)[0]
263
+ )
264
+ del messages[:expired]
265
+ if not messages:
266
+ del runs[run_id]
267
+ if not runs:
268
+ del self.message_stores[thread_id]
269
+
241
270
  def get_queues_by_thread_id(self, thread_id: UUID | str) -> list[asyncio.Queue]:
242
271
  """Get all queues for a specific thread_id across all runs."""
243
272
  all_queues = []
@@ -263,21 +292,9 @@ async def start_stream() -> None:
263
292
 
264
293
 
265
294
  async def stop_stream() -> None:
266
- """Clean up the queue system.
267
- Clear all queues and stored control messages."""
268
- global stream_manager
269
-
270
- # Send 'done' message to all active queues before clearing
271
- for run_id in list(stream_manager.queues.keys()):
272
- control_message = Message(topic=f"run:{run_id}:control".encode(), data=b"done")
273
-
274
- for queue in stream_manager.queues[run_id]:
275
- try:
276
- await queue.put(control_message)
277
- except (Exception, RuntimeError):
278
- pass # Ignore errors during shutdown
279
-
280
- # Clear all stored data
295
+ """Drop the run stream queues, control queues and stored stream messages."""
296
+ # Sends no `done`: every request has ended or been cancelled by the time this
297
+ # runs, and a thread stream would report a run cut short as finished.
281
298
  stream_manager.queues.clear()
282
299
  stream_manager.control_queues.clear()
283
300
  stream_manager.message_stores.clear()
@@ -2405,17 +2405,22 @@ class Threads(Authenticated):
2405
2405
  "Joined thread stream",
2406
2406
  thread_id=str(thread_id),
2407
2407
  )
2408
+ # Before the replay, so what is published while it is sent reaches a queue.
2409
+ created_queues.extend(
2410
+ await Threads.Stream.subscribe(conn, thread_id, seen_runs)
2411
+ )
2412
+ # ``message_stores`` is keyed by ``UUID`` (see
2413
+ # :meth:`StreamManager.put`), and callers can hand us
2414
+ # ``thread_id`` as either ``str`` or ``UUID``.
2415
+ store_key = _ensure_uuid(thread_id)
2408
2416
 
2409
2417
  # Restore messages if resuming from a specific event
2410
2418
  if last_event_id is not None:
2411
- # ``message_stores`` is keyed by ``UUID`` (see
2412
- # :meth:`StreamManager.put`). Callers can hand us
2413
- # ``thread_id`` as either ``str`` or ``UUID``, so
2414
- # normalize before the lookup — otherwise replay
2415
- # always misses and yields nothing.
2416
- store_key = _ensure_uuid(thread_id)
2417
2419
  all_events = []
2418
- for run_id in stream_manager.message_stores.get(store_key, []):
2420
+ # A copy: restore_messages drops expired runs from this dict.
2421
+ for run_id in list(
2422
+ stream_manager.message_stores.get(store_key, ())
2423
+ ):
2419
2424
  for message in stream_manager.restore_messages(
2420
2425
  run_id, thread_id, last_event_id
2421
2426
  ):
@@ -2463,61 +2468,49 @@ class Threads(Authenticated):
2463
2468
  new_queue_tuples = await Threads.Stream.subscribe(
2464
2469
  conn, thread_id, seen_runs
2465
2470
  )
2471
+ drained = []
2466
2472
  # Track new queues for cleanup
2467
2473
  for run_id, queue in new_queue_tuples:
2468
2474
  created_queues.append((run_id, queue))
2475
+ # A run that started after the join can store events
2476
+ # before this poll gives it a queue.
2477
+ drained.extend(
2478
+ (message, run_id)
2479
+ for message in stream_manager.message_stores.get(
2480
+ store_key, {}
2481
+ ).get(run_id, ())
2482
+ )
2469
2483
 
2470
- drained_any = False
2484
+ # Drain every queue before yielding, then send in id order: a
2485
+ # client resumes after the last id it got.
2471
2486
  for run_id, queue in created_queues:
2472
- while True:
2473
- try:
2474
- message = queue.get_nowait()
2475
- except asyncio.QueueEmpty:
2476
- break
2477
- try:
2478
- decoded = decode_stream_message(
2479
- message.data, channel=message.topic
2480
- )
2481
- except (ValueError, KeyError):
2482
- continue
2483
- event = decoded.event_bytes
2484
- event_name = event.decode("utf-8")
2485
- payload = decoded.message_bytes
2486
-
2487
- if event == b"control" and payload == b"done":
2488
- # Don't shadow the queue-iteration
2489
- # ``run_id`` with the topic-extracted
2490
- # string — non-control events later in
2491
- # this drain pass would yield the
2492
- # rebound value. Wire output is
2493
- # identical (``str(UUID)`` matches the
2494
- # topic suffix), but the rebinding is
2495
- # fragile if the topic format moves.
2496
- topic = message.topic.decode()
2497
- done_run_id = topic.split("run:")[1].split(":")[0]
2498
- meta_event = b"metadata"
2499
- meta_payload = orjson.dumps(
2500
- {"status": "run_done", "run_id": done_run_id}
2501
- )
2502
- if not should_filter_event(
2503
- "metadata", meta_payload
2504
- ):
2505
- yield (
2506
- meta_event,
2507
- meta_payload,
2508
- message.id,
2509
- done_run_id,
2510
- )
2511
- drained_any = True
2512
- else:
2513
- if not should_filter_event(event_name, payload):
2514
- yield (
2515
- event,
2516
- payload,
2517
- message.id,
2518
- str(run_id),
2519
- )
2520
- drained_any = True
2487
+ while not queue.empty():
2488
+ drained.append((queue.get_nowait(), run_id))
2489
+ drained.sort(key=lambda item: stream_id_key(item[0].id))
2490
+ drained_any = False
2491
+ for message, run_id in drained:
2492
+ try:
2493
+ decoded = decode_stream_message(
2494
+ message.data, channel=message.topic
2495
+ )
2496
+ except (ValueError, KeyError):
2497
+ continue
2498
+ event = decoded.event_bytes
2499
+ payload = decoded.message_bytes
2500
+ if event == b"control" and payload == b"done":
2501
+ event = b"metadata"
2502
+ payload = orjson.dumps(
2503
+ {"status": "run_done", "run_id": str(run_id)}
2504
+ )
2505
+ if not should_filter_event(event.decode("utf-8"), payload):
2506
+ # Only a stored event can be resumed from.
2507
+ yield (
2508
+ event,
2509
+ payload,
2510
+ message.id if message.resumable else None,
2511
+ str(run_id),
2512
+ )
2513
+ drained_any = True
2521
2514
 
2522
2515
  if drained_any:
2523
2516
  # Yield once so other tasks (worker, send) can
@@ -3675,9 +3668,11 @@ class Runs(Authenticated):
3675
3668
  )
3676
3669
  run = await anext(run_iter, None)
3677
3670
 
3671
+ last_replayed = None
3678
3672
  for message in get_stream_manager().restore_messages(
3679
3673
  run_id, thread_id, last_event_id
3680
3674
  ):
3675
+ last_replayed = stream_id_key(message.id)
3681
3676
  data, id = message.data, message.id
3682
3677
  decoded = decode_stream_message(data, channel=message.topic)
3683
3678
  mode = decoded.event_bytes.decode("utf-8")
@@ -3700,6 +3695,13 @@ class Runs(Authenticated):
3700
3695
  try:
3701
3696
  # Wait for messages with a timeout
3702
3697
  message = await asyncio.wait_for(queue.get(), timeout=0.5)
3698
+ # The queue was subscribed before the replay, so it can
3699
+ # repeat events the replay already sent.
3700
+ if (
3701
+ last_replayed is not None
3702
+ and stream_id_key(message.id) <= last_replayed
3703
+ ):
3704
+ continue
3703
3705
  data, id = message.data, message.id
3704
3706
  decoded = decode_stream_message(data, channel=message.topic)
3705
3707
  mode = decoded.event_bytes.decode("utf-8")
@@ -38,11 +38,11 @@ wheels = [
38
38
 
39
39
  [[package]]
40
40
  name = "certifi"
41
- version = "2026.10.19"
41
+ version = "2026.10.23"
42
42
  source = { registry = "https://pypi.org/simple" }
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" }
43
+ sdist = { url = "https://files.pythonhosted.org/packages/fd/11/b524cbd9bbdaca58608e4cf4053fd463417549e1959fa8d3aa18319f9555/certifi-2026.10.23.tar.gz", hash = "sha256:a9895e6e2758b371e0545e321a3a8fd1fcfd9829ada8824a936dfb16aaaf36cd", size = 136225, upload-time = "2026-10-23T11:36:16.478Z" }
44
44
  wheels = [
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" },
45
+ { url = "https://files.pythonhosted.org/packages/32/d1/a3652fe2c19134d0d15922177821d863cbd7e842d53a21bbe7e8e3a1b5ac/certifi-2026.10.23-py3-none-any.whl", hash = "sha256:e9b54de76e530bb6cf0ccd036787103acb58fd42ba930ea8bf948ddbaa2f61d1", size = 135082, upload-time = "2026-10-23T11:36:14.763Z" },
46
46
  ]
47
47
 
48
48
  [[package]]