langgraph-runtime-inmem 0.39.0.dev2__tar.gz → 0.39.0.dev4__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.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/PKG-INFO +1 -1
  2. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/__init__.py +1 -1
  3. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/encryption.py +1 -0
  4. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/inmem_stream.py +34 -17
  5. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/ops.py +64 -57
  6. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/.gitignore +0 -0
  7. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/Makefile +0 -0
  8. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/README.md +0 -0
  9. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/_persistence.py +0 -0
  10. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/checkpoint.py +0 -0
  11. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/database.py +0 -0
  12. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/lifespan.py +0 -0
  13. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/metrics.py +0 -0
  14. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/queue.py +0 -0
  15. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/retry.py +0 -0
  16. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/routes.py +0 -0
  17. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/langgraph_runtime_inmem/store.py +0 -0
  18. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/pyproject.toml +0 -0
  19. {langgraph_runtime_inmem-0.39.0.dev2 → langgraph_runtime_inmem-0.39.0.dev4}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langgraph-runtime-inmem
3
- Version: 0.39.0.dev2
3
+ Version: 0.39.0.dev4
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.dev2"
14
+ __version__ = "0.39.0.dev4"
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()
@@ -1580,6 +1580,8 @@ class Threads(Authenticated):
1580
1580
  conn: InMemConnectionProto,
1581
1581
  thread_id: UUID,
1582
1582
  ctx: Auth.types.BaseAuthContext | None = None,
1583
+ *,
1584
+ sync_checkpoint_delete: bool = False, # deletes are always synchronous
1583
1585
  ) -> AsyncIterator[UUID]:
1584
1586
  """Delete a thread by ID and cascade delete all associated runs."""
1585
1587
  thread_list = conn.store["threads"]
@@ -1638,6 +1640,8 @@ class Threads(Authenticated):
1638
1640
  strategy: Literal["delete", "keep_latest"] = "delete",
1639
1641
  batch_size: int = 100,
1640
1642
  ctx: Auth.types.BaseAuthContext | None = None,
1643
+ *,
1644
+ sync_checkpoint_delete: bool = False,
1641
1645
  ) -> int:
1642
1646
  """Prune threads by ID (inmem implementation).
1643
1647
 
@@ -1646,6 +1650,7 @@ class Threads(Authenticated):
1646
1650
  strategy: Prune strategy ("delete" supported, "keep_latest" not supported)
1647
1651
  batch_size: Not used in inmem implementation
1648
1652
  ctx: Auth context for permission checks
1653
+ sync_checkpoint_delete: Not used; inmem deletes are always synchronous
1649
1654
 
1650
1655
  Returns:
1651
1656
  Number of threads successfully pruned
@@ -2405,17 +2410,22 @@ class Threads(Authenticated):
2405
2410
  "Joined thread stream",
2406
2411
  thread_id=str(thread_id),
2407
2412
  )
2413
+ # Before the replay, so what is published while it is sent reaches a queue.
2414
+ created_queues.extend(
2415
+ await Threads.Stream.subscribe(conn, thread_id, seen_runs)
2416
+ )
2417
+ # ``message_stores`` is keyed by ``UUID`` (see
2418
+ # :meth:`StreamManager.put`), and callers can hand us
2419
+ # ``thread_id`` as either ``str`` or ``UUID``.
2420
+ store_key = _ensure_uuid(thread_id)
2408
2421
 
2409
2422
  # Restore messages if resuming from a specific event
2410
2423
  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
2424
  all_events = []
2418
- for run_id in stream_manager.message_stores.get(store_key, []):
2425
+ # A copy: restore_messages drops expired runs from this dict.
2426
+ for run_id in list(
2427
+ stream_manager.message_stores.get(store_key, ())
2428
+ ):
2419
2429
  for message in stream_manager.restore_messages(
2420
2430
  run_id, thread_id, last_event_id
2421
2431
  ):
@@ -2463,61 +2473,49 @@ class Threads(Authenticated):
2463
2473
  new_queue_tuples = await Threads.Stream.subscribe(
2464
2474
  conn, thread_id, seen_runs
2465
2475
  )
2476
+ drained = []
2466
2477
  # Track new queues for cleanup
2467
2478
  for run_id, queue in new_queue_tuples:
2468
2479
  created_queues.append((run_id, queue))
2480
+ # A run that started after the join can store events
2481
+ # before this poll gives it a queue.
2482
+ drained.extend(
2483
+ (message, run_id)
2484
+ for message in stream_manager.message_stores.get(
2485
+ store_key, {}
2486
+ ).get(run_id, ())
2487
+ )
2469
2488
 
2470
- drained_any = False
2489
+ # Drain every queue before yielding, then send in id order: a
2490
+ # client resumes after the last id it got.
2471
2491
  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
2492
+ while not queue.empty():
2493
+ drained.append((queue.get_nowait(), run_id))
2494
+ drained.sort(key=lambda item: stream_id_key(item[0].id))
2495
+ drained_any = False
2496
+ for message, run_id in drained:
2497
+ try:
2498
+ decoded = decode_stream_message(
2499
+ message.data, channel=message.topic
2500
+ )
2501
+ except (ValueError, KeyError):
2502
+ continue
2503
+ event = decoded.event_bytes
2504
+ payload = decoded.message_bytes
2505
+ if event == b"control" and payload == b"done":
2506
+ event = b"metadata"
2507
+ payload = orjson.dumps(
2508
+ {"status": "run_done", "run_id": str(run_id)}
2509
+ )
2510
+ if not should_filter_event(event.decode("utf-8"), payload):
2511
+ # Only a stored event can be resumed from.
2512
+ yield (
2513
+ event,
2514
+ payload,
2515
+ message.id if message.resumable else None,
2516
+ str(run_id),
2517
+ )
2518
+ drained_any = True
2521
2519
 
2522
2520
  if drained_any:
2523
2521
  # Yield once so other tasks (worker, send) can
@@ -3675,9 +3673,11 @@ class Runs(Authenticated):
3675
3673
  )
3676
3674
  run = await anext(run_iter, None)
3677
3675
 
3676
+ last_replayed = None
3678
3677
  for message in get_stream_manager().restore_messages(
3679
3678
  run_id, thread_id, last_event_id
3680
3679
  ):
3680
+ last_replayed = stream_id_key(message.id)
3681
3681
  data, id = message.data, message.id
3682
3682
  decoded = decode_stream_message(data, channel=message.topic)
3683
3683
  mode = decoded.event_bytes.decode("utf-8")
@@ -3700,6 +3700,13 @@ class Runs(Authenticated):
3700
3700
  try:
3701
3701
  # Wait for messages with a timeout
3702
3702
  message = await asyncio.wait_for(queue.get(), timeout=0.5)
3703
+ # The queue was subscribed before the replay, so it can
3704
+ # repeat events the replay already sent.
3705
+ if (
3706
+ last_replayed is not None
3707
+ and stream_id_key(message.id) <= last_replayed
3708
+ ):
3709
+ continue
3703
3710
  data, id = message.data, message.id
3704
3711
  decoded = decode_stream_message(data, channel=message.topic)
3705
3712
  mode = decoded.event_bytes.decode("utf-8")