requence 0.8.3.dev122__tar.gz → 0.8.3.dev149__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 (81) hide show
  1. {requence-0.8.3.dev122/requence.egg-info → requence-0.8.3.dev149}/PKG-INFO +2 -2
  2. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/README.md +1 -1
  3. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/pyproject.toml +1 -1
  4. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/context.py +2 -2
  5. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/exceptions.py +12 -1
  6. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/internal_types.py +4 -0
  7. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/__init__.py +58 -17
  8. requence-0.8.3.dev149/requence/service/dev_session.py +51 -0
  9. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/ui.py +1 -1
  10. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/store.py +9 -6
  11. requence-0.8.3.dev149/requence/task/__init__.py +37 -0
  12. requence-0.8.3.dev149/requence/task/automations.py +131 -0
  13. {requence-0.8.3.dev122 → requence-0.8.3.dev149/requence.egg-info}/PKG-INFO +2 -2
  14. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence.egg-info/SOURCES.txt +2 -0
  15. requence-0.8.3.dev122/requence/task/__init__.py +0 -17
  16. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/MANIFEST.in +0 -0
  17. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/__init__.py +0 -0
  18. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/callbacks/__init__.py +0 -0
  19. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/callbacks/context.py +0 -0
  20. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/callbacks/handler.py +0 -0
  21. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/client_properties.py +0 -0
  22. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/__init__.py +0 -0
  23. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/errors.py +0 -0
  24. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/handlers/__init__.py +0 -0
  25. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/handlers/claim_check_handler.py +0 -0
  26. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/handlers/context.py +0 -0
  27. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/handlers/file_handler.py +0 -0
  28. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/handlers/stream_handler.py +0 -0
  29. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/pending_uploads.py +0 -0
  30. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/requence_file.py +0 -0
  31. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/requence_stream.py +0 -0
  32. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/sanitize_log_data.py +0 -0
  33. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/stream_framing.py +0 -0
  34. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/files/transport.py +0 -0
  35. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/logger.py +0 -0
  36. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/obfuscase.py +0 -0
  37. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/py.typed +0 -0
  38. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/reachable.py +0 -0
  39. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/requence_callback.py +0 -0
  40. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/requence_file.py +0 -0
  41. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/requence_stream.py +0 -0
  42. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/boundary.py +0 -0
  43. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/callback_growth.py +0 -0
  44. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/cli.py +0 -0
  45. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/durable_callback.py +0 -0
  46. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/surface_handler.py +0 -0
  47. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/transformer.py +0 -0
  48. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/types.py +0 -0
  49. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/service/ui_watch.py +0 -0
  50. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/abort_task.py +0 -0
  51. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/ack_task.py +0 -0
  52. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/cli.py +0 -0
  53. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/get_task.py +0 -0
  54. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/ingress.py +0 -0
  55. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/protect_task.py +0 -0
  56. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/task.py +0 -0
  57. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/task_watcher.py +0 -0
  58. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/types.py +0 -0
  59. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/updates.py +0 -0
  60. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/task/utils.py +0 -0
  61. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/transformer/__init__.py +0 -0
  62. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/transformer/context.py +0 -0
  63. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/transformer/errors.py +0 -0
  64. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/transformer/guards.py +0 -0
  65. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/transformer/message_handler.py +0 -0
  66. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/transformer/transform.py +0 -0
  67. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/transformer/value_handler.py +0 -0
  68. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/transformer/walk.py +0 -0
  69. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/utils.py +0 -0
  70. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/wire.py +0 -0
  71. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence/wire_codec.py +0 -0
  72. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence.egg-info/dependency_links.txt +0 -0
  73. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence.egg-info/entry_points.txt +0 -0
  74. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence.egg-info/requires.txt +0 -0
  75. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/requence.egg-info/top_level.txt +0 -0
  76. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/setup.cfg +0 -0
  77. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/tests/test_client_properties.py +0 -0
  78. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/tests/test_logger.py +0 -0
  79. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/tests/test_store_parity.py +0 -0
  80. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/tests/test_wire.py +0 -0
  81. {requence-0.8.3.dev122 → requence-0.8.3.dev149}/tests/test_wire_codec.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: requence
3
- Version: 0.8.3.dev122
3
+ Version: 0.8.3.dev149
4
4
  Summary: Requence Service
5
5
  Author: Torsten Blindert
6
6
  Requires-Python: >=3.12
@@ -50,7 +50,7 @@ REQUENCE_SERVICE_ACCESS_TOKEN=your-token python main.py
50
50
  ## Your first service
51
51
 
52
52
  Create the service in the Requence UI first — its input schema, configuration schema and
53
- outputs. Then implement the version:
53
+ outputs. Then implement the variant:
54
54
 
55
55
  ```python
56
56
  from requence.service import Service
@@ -38,7 +38,7 @@ REQUENCE_SERVICE_ACCESS_TOKEN=your-token python main.py
38
38
  ## Your first service
39
39
 
40
40
  Create the service in the Requence UI first — its input schema, configuration schema and
41
- outputs. Then implement the version:
41
+ outputs. Then implement the variant:
42
42
 
43
43
  ```python
44
44
  from requence.service import Service
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "requence"
7
- version = "0.8.3.dev122"
7
+ version = "0.8.3.dev149"
8
8
  description = "Requence Service"
9
9
  readme = { file = "README.md", content-type = "text/markdown" }
10
10
  authors = [{ name = "Torsten Blindert" }]
@@ -100,8 +100,8 @@ class Context[I, C]:
100
100
  needs a generator handler (a non-generator's registry goes the moment it
101
101
  returns) and it dies with the message. One written in the UI's own
102
102
  defaults — ``create_ui(path, {"onSubmit": …})`` — closes over module
103
- scope, so any instance of this version can run it, and an invocation waits
104
- in the version's handler queue when none is up. That is the honest answer
103
+ scope, so any instance of this variant can run it, and an invocation waits
104
+ in the variant's handler queue when none is up. That is the honest answer
105
105
  to a wait measured in days: a form's submit handler belongs in the
106
106
  defaults, and the handler that renders it is free to defer and exit.
107
107
 
@@ -63,4 +63,15 @@ class SkipException(Exception):
63
63
  pass
64
64
 
65
65
  class StopException(Exception):
66
- pass
66
+ pass
67
+
68
+ class DevSessionException(Exception):
69
+ """A dev token the Requence instance refused, or could not be asked about.
70
+
71
+ A dev token moves the worker off the deployed version's queue and onto a
72
+ personal overlay queue, and the instance routes a task there only when the
73
+ overlay's key is one of its live personal tokens. Unchecked, an unknown
74
+ token started a worker on a queue nothing would ever send to — so the
75
+ service refuses to start instead. Mirrors ``DevSessionError`` in
76
+ ``packages/service/src/devSession.ts``.
77
+ """
@@ -3,6 +3,10 @@ from ssl import SSLContext
3
3
 
4
4
  class ServiceConfig(TypedDict, total=False):
5
5
  access_token: Optional[str]
6
+ # The service variant this worker implements.
7
+ variant: str
8
+ # Deprecated: the old name of ``variant``. Still accepted, with a
9
+ # DeprecationWarning; the wire still says ``version`` either way.
6
10
  version: str
7
11
  # Optional, author-chosen label announced in the AMQP client properties and
8
12
  # shown in the Requence UI next to the online count (mirrors
@@ -57,11 +57,13 @@ import ssl
57
57
  import threading
58
58
  import time
59
59
  import tomllib
60
+ import warnings
60
61
  from ..internal_types import ServiceConfig, Payload, ActorApi
61
62
  from ..files import RequenceFile, RequenceStream
62
63
  from ..requence_callback import Callback
63
64
  from .boundary import SUPPRESSED, create_inbound_boundary, create_outbound_boundary
64
65
  from .callback_growth import create_callback_growth_watch
66
+ from .dev_session import assert_dev_session
65
67
  from .durable_callback import (
66
68
  DurableCallback,
67
69
  create_durable_callback,
@@ -270,7 +272,7 @@ class Service:
270
272
  one over — so a module-scope callable (a durable surface handler) or a
271
273
  webhook handler had nothing to call them on::
272
274
 
273
- service = Service.start({"version": "1.0.0"}, handler)
275
+ service = Service.start({"variant": "1.0.0"}, handler)
274
276
  ...
275
277
  service.act(message_key, actor) # reachable
276
278
 
@@ -320,7 +322,7 @@ class Service:
320
322
  if inspect.iscoroutinefunction(on_message_callback):
321
323
  raise TypeError("The on_message_callback must be a synchronous function, not an async function.")
322
324
 
323
- service_config: ServiceConfig = {"version": config} if isinstance(config, str) else config
325
+ service_config: ServiceConfig = {"variant": config} if isinstance(config, str) else config
324
326
 
325
327
  key = service_config.get("access_token") or os.getenv("REQUENCE_SERVICE_ACCESS_TOKEN") or os.getenv("REQUENCE_ACCESS_TOKEN")
326
328
  dev_key = dev_token or os.getenv("REQUENCE_SERVICE_DEV_TOKEN") or os.getenv("REQUENCE_DEV_TOKEN")
@@ -355,6 +357,13 @@ class Service:
355
357
  if instance_id and (len(dev_parts) < 3 or dev_parts[2] != instance_id):
356
358
  raise ValueError("Invalid dev token: access token and dev token belong to different requence instances")
357
359
  dev_overlay = dev_parts[1]
360
+ # Before the first connect: an unknown token would otherwise start a
361
+ # worker on an overlay queue nothing ever sends to (see dev_session).
362
+ if not instance_id:
363
+ raise ValueError(
364
+ "Invalid access token: it names no Requence API, so the dev token cannot be checked"
365
+ )
366
+ assert_dev_session(instance_id, url, dev_overlay)
358
367
 
359
368
  # A dev token is not a mode this process announces anywhere else: it
360
369
  # quietly moves the worker off the deployed version's queue and onto
@@ -370,9 +379,23 @@ class Service:
370
379
  "from pyproject.toml."
371
380
  )
372
381
 
373
- version = service_config.get("version")
374
- if not isinstance(version, str):
375
- raise ValueError("Version missing")
382
+ # ``version`` is the deprecated name of ``variant``. The wire still says
383
+ # ``version``, so a worker on an older SDK and one on this one are the
384
+ # same to the broker and the operator.
385
+ variant = service_config.get("variant")
386
+ deprecated_version = service_config.get("version")
387
+ if deprecated_version is not None:
388
+ warnings.warn(
389
+ 'The service option "version" is deprecated; use "variant".',
390
+ DeprecationWarning,
391
+ stacklevel=3,
392
+ )
393
+ if variant is not None and variant != deprecated_version:
394
+ raise ValueError('Conflicting service options: give "variant" only ("version" is its deprecated name)')
395
+ variant = deprecated_version
396
+ if not isinstance(variant, str):
397
+ raise ValueError("Variant missing")
398
+ version = variant
376
399
 
377
400
  prefetch = service_config.get("prefetch") or 1
378
401
 
@@ -490,9 +513,16 @@ class Service:
490
513
  self.__active_messages: list[str] = list()
491
514
 
492
515
  self.__generator_signals_lock = threading.Lock()
493
- self.__generator_signals: dict[str, threading.Event] = dict()
494
-
495
- # callback_id → {"message_id", "callback", "on_callback_invoke"}
516
+ # Keyed by ATTEMPT — (message id, epoch) — never by message id alone
517
+ # (#372). The connection id survives an AMQP reconnect, so a re-dispatch
518
+ # of a message can land here while the first attempt's generator still
519
+ # runs. Keyed by message id, the second attempt overwrote the first: the
520
+ # orphan became unstoppable, and the stop meant for it ended the new one.
521
+ self.__generator_signals: dict[tuple[str, str], threading.Event] = dict()
522
+
523
+ # callback_id → {"message_id", "epoch", "callback", "on_callback_invoke"}
524
+ # `epoch` is None for a callback registered by `act()`, which belongs to
525
+ # no dispatch attempt.
496
526
  self.__callbacks_lock = threading.Lock()
497
527
  self.__callbacks: dict[str, dict[str, Any]] = dict()
498
528
 
@@ -1409,12 +1439,13 @@ class Service:
1409
1439
  def __remove_active_message(self, message_id: str):
1410
1440
  self.__active_messages.remove(message_id)
1411
1441
 
1412
- def __register_callback(self, message_id: str, registered_callback_ids: List[str], on_callback_invoke: Callable[[dict], None], callback: Callable[[Any], None]) -> dict:
1442
+ def __register_callback(self, message_id: str, epoch: Optional[str], registered_callback_ids: List[str], on_callback_invoke: Callable[[dict], None], callback: Callable[[Any], None]) -> dict:
1413
1443
  callback_id = str(uuid4())
1414
1444
  registered_callback_ids.append(callback_id)
1415
1445
  with self.__callbacks_lock:
1416
1446
  self.__callbacks[callback_id] = {
1417
1447
  "message_id": message_id,
1448
+ "epoch": epoch,
1418
1449
  "callback": callback,
1419
1450
  "on_callback_invoke": on_callback_invoke,
1420
1451
  }
@@ -1426,6 +1457,8 @@ class Service:
1426
1457
 
1427
1458
  message_id = str(properties.correlation_id)
1428
1459
  registered_callback_ids: List[str] = []
1460
+ # This attempt's own generator signal, for the cleanup in `finally` (#372).
1461
+ generator_key: Optional[tuple[str, str]] = None
1429
1462
 
1430
1463
  # Never assume a codec: `ui/backend` bakes the choice per service node, so
1431
1464
  # two nodes of one task can legitimately arrive under different codecs,
@@ -1555,7 +1588,7 @@ class Service:
1555
1588
  growth = self.__callback_growth.for_delivery(task_id=task_id)
1556
1589
 
1557
1590
  def register_callback(callback: Callable[[Any], None], path: List[str | int]) -> dict:
1558
- registration = self.__register_callback(message_id, registered_callback_ids, dispatch_invocation, callback)
1591
+ registration = self.__register_callback(message_id, epoch, registered_callback_ids, dispatch_invocation, callback)
1559
1592
  # After the entry is in, so the connection count a warning states is
1560
1593
  # the number this process is holding. Never refuses and never
1561
1594
  # evicts: a cap kills a legitimate long-lived handle at an arbitrary
@@ -1716,8 +1749,9 @@ class Service:
1716
1749
  self.__schedule(functools.partial(self.__ack_message, delivery_tag, generation))
1717
1750
  acked = True
1718
1751
 
1752
+ generator_key = (message_id, epoch)
1719
1753
  with self.__generator_signals_lock:
1720
- self.__generator_signals[message_id] = terminated
1754
+ self.__generator_signals[generator_key] = terminated
1721
1755
 
1722
1756
  for result in response:
1723
1757
 
@@ -1821,9 +1855,11 @@ class Service:
1821
1855
  if not acked:
1822
1856
  self.__schedule(functools.partial(self.__handle_exception, body, properties, delivery_tag, generation))
1823
1857
  finally:
1824
- with self.__generator_signals_lock:
1825
- if message_id in self.__generator_signals:
1826
- del self.__generator_signals[message_id]
1858
+ # Only this attempt's own signal: another attempt of the same message
1859
+ # may still be running in this process (#372).
1860
+ if generator_key is not None:
1861
+ with self.__generator_signals_lock:
1862
+ self.__generator_signals.pop(generator_key, None)
1827
1863
 
1828
1864
  with self.__callbacks_lock:
1829
1865
  for callback_id in registered_callback_ids:
@@ -1841,14 +1877,19 @@ class Service:
1841
1877
  message_id = properties.correlation_id
1842
1878
 
1843
1879
  if (payload_data["type"] == "stop"):
1880
+ # An addressed stop ends exactly its attempt (#372); one without an
1881
+ # epoch ends the node or the task, so it ends every attempt.
1882
+ epoch = payload_data.get("epoch")
1844
1883
  with self.__generator_signals_lock:
1845
- if message_id in self.__generator_signals:
1846
- self.__generator_signals[message_id].set()
1884
+ for (signal_message_id, signal_epoch), signal in self.__generator_signals.items():
1885
+ if signal_message_id == message_id and (epoch is None or signal_epoch == epoch):
1886
+ signal.set()
1847
1887
  with self.__callbacks_lock:
1848
1888
  stopped = [
1849
1889
  (callback_id, entry)
1850
1890
  for callback_id, entry in self.__callbacks.items()
1851
1891
  if entry["message_id"] == message_id
1892
+ and (epoch is None or entry["epoch"] == epoch)
1852
1893
  ]
1853
1894
  for callback_id, _entry in stopped:
1854
1895
  del self.__callbacks[callback_id]
@@ -2010,7 +2051,7 @@ class Service:
2010
2051
  growth = self.__callback_growth.for_delivery(task_id=task_id)
2011
2052
 
2012
2053
  def register_callback(callback: Callable[[Any], None], path: List[str | int]) -> dict:
2013
- registration = self.__register_callback(message_id, registered_callback_ids, dispatch_invocation, callback)
2054
+ registration = self.__register_callback(message_id, None, registered_callback_ids, dispatch_invocation, callback)
2014
2055
  growth.record(path)
2015
2056
  return registration
2016
2057
 
@@ -0,0 +1,51 @@
1
+ """The dev-token check a service makes before it serves as a dev overlay.
2
+
3
+ Mirrors ``packages/service/src/devSession.ts``: the same endpoint, the same two
4
+ headers and the same messages. The difference is where it runs — the Python
5
+ service configures synchronously, so the check runs BEFORE the first connect and
6
+ a refusal raises out of ``Service(...)`` / ``Service.start(...)`` directly.
7
+ """
8
+
9
+ from typing import Any, Callable
10
+
11
+ import requests
12
+
13
+ from ..exceptions import DevSessionException
14
+
15
+ # Long enough for a dev server that is busy restarting, short enough that a
16
+ # wrong URL does not look like a hang.
17
+ DEV_SESSION_TIMEOUT_SECONDS = 10
18
+
19
+
20
+ def assert_dev_session(
21
+ server_url: str,
22
+ connection_string: str,
23
+ dev_key: str,
24
+ get: Callable[..., Any] = requests.get,
25
+ ) -> dict:
26
+ """Returns ``{"branchId", "branchName"}`` when the instance accepts both
27
+ tokens; raises :class:`DevSessionException` with its reason otherwise."""
28
+ url = f"{server_url}/service/dev-session"
29
+ try:
30
+ response = get(
31
+ url,
32
+ headers={
33
+ "Authorization": f"Bearer {connection_string}",
34
+ "x-dev-authorization": dev_key,
35
+ },
36
+ timeout=DEV_SESSION_TIMEOUT_SECONDS,
37
+ )
38
+ except requests.RequestException as error:
39
+ raise DevSessionException(
40
+ f"Requence: the dev token cannot be checked — {url} is not reachable "
41
+ f"({error}). A dev overlay needs the Requence API."
42
+ ) from None
43
+
44
+ if response.ok:
45
+ return response.json()
46
+
47
+ reason = (response.text or "").strip()
48
+ raise DevSessionException(
49
+ f"Requence: the dev token was refused by {server_url} "
50
+ f"({response.status_code}){f': {reason}' if reason else ''}"
51
+ )
@@ -92,7 +92,7 @@ def create_ui[Props](
92
92
  later render that stays silent about it.
93
93
 
94
94
  A **callable** among them is a *durable* handler (D24): written at module
95
- scope, it closes over nothing one message owns, so any instance of the version
95
+ scope, it closes over nothing one message owns, so any instance of the variant
96
96
  can run it — including one started after the process that rendered has exited.
97
97
  That is the opposite of a callable written inline at ``context.render``, which
98
98
  dies with the message that published it. Where you write it is the whole
@@ -25,13 +25,13 @@ from typing import Any, Callable, Generic, Literal, Optional, TypeVar
25
25
 
26
26
  import requests
27
27
 
28
- StoreBinding = Literal["task", "taskTemplate"]
28
+ StoreBinding = Literal["task", "taskTemplate", "automation"]
29
29
 
30
- # The two bindings, as values, so `at` can refuse a third. Python cannot enforce
30
+ # The bindings, as values, so `at` can refuse a third. Python cannot enforce
31
31
  # the union the way TypeScript does, so the check is at call time instead — and
32
32
  # the list is written out here for the same reason the logic runtime writes it
33
33
  # out: the store's own declaration is TypeScript.
34
- STORE_BINDINGS: tuple[str, ...] = ("task", "taskTemplate")
34
+ STORE_BINDINGS: tuple[str, ...] = ("task", "taskTemplate", "automation")
35
35
 
36
36
  StoreOwnership = Literal["private", "shared"]
37
37
 
@@ -217,6 +217,8 @@ class Store:
217
217
  context.store.private('cursor', 'taskTemplate') # this node, survives it
218
218
  context.store.shared('cursor', 'task') # every node, this run
219
219
  context.store.shared('cursor', 'taskTemplate') # every node, survives it
220
+ context.store.shared('cursor', 'automation') # every node, every run
221
+ # of this automation
220
222
 
221
223
  Both verbs are LOCAL and do no I/O — they return a handle, so a handler that
222
224
  never reads or writes a slot makes zero calls.
@@ -274,9 +276,10 @@ class Store:
274
276
  if binding not in STORE_BINDINGS:
275
277
  raise ValueError(
276
278
  f"context.store.{ownership}({key!r}, {binding!r}): the binding "
277
- 'must be "task" (forgotten when this task ends) or '
278
- '"taskTemplate" (kept for the next one). Omitting it means '
279
- '"task" — the one that forgets.'
279
+ 'must be "task" (forgotten when this task ends), '
280
+ '"taskTemplate" (kept for the next run of this template) or '
281
+ '"automation" (kept for the next run of this automation). '
282
+ 'Omitting it means "task" — the one that forgets.'
280
283
  )
281
284
  return StoreSlot(self, ownership, key, binding)
282
285
 
@@ -0,0 +1,37 @@
1
+ from .task import Task
2
+ from .get_task import get_task
3
+ from .abort_task import abort_task, TaskAbortException
4
+ from .protect_task import protect_task, TaskProtectException
5
+ from .task_watcher import TaskWatcher
6
+ from .automations import (
7
+ Automation,
8
+ AutomationException,
9
+ AutomationExistsException,
10
+ AutomationDeletedException,
11
+ create_automation,
12
+ get_automation,
13
+ start_automation,
14
+ stop_automation,
15
+ delete_automation,
16
+ )
17
+ from ..exceptions import RequenceUnreachableException
18
+
19
+ __all__ = [
20
+ "Task",
21
+ "get_task",
22
+ "abort_task",
23
+ "TaskAbortException",
24
+ "protect_task",
25
+ "TaskProtectException",
26
+ "TaskWatcher",
27
+ "Automation",
28
+ "AutomationException",
29
+ "AutomationExistsException",
30
+ "AutomationDeletedException",
31
+ "create_automation",
32
+ "get_automation",
33
+ "start_automation",
34
+ "stop_automation",
35
+ "delete_automation",
36
+ "RequenceUnreachableException",
37
+ ]
@@ -0,0 +1,131 @@
1
+ """The automation routes of the access-token API (#364). Mirrors
2
+ packages/task/src/automations.ts: one function per route, and every refusal is
3
+ an AutomationException carrying the API's stable `code`. The two refusals a
4
+ retried create must tell apart have their own classes.
5
+ """
6
+
7
+ from typing import Any, Literal, Optional, TypedDict
8
+
9
+ from .utils import get_key, reachable_request
10
+
11
+ AutomationStatus = Literal["IDLE", "RUNNING", "STOPPED", "FAILED"]
12
+
13
+
14
+ class Automation(TypedDict):
15
+ id: str
16
+ name: str
17
+ status: AutomationStatus
18
+ activeTaskId: Optional[str]
19
+ previousTaskIds: list[str]
20
+ triggerTemplateId: str
21
+ actionTemplateId: str
22
+ failureReason: Optional[str]
23
+ outdatedReason: Optional[str]
24
+ createdAt: str
25
+ updatedAt: Optional[str]
26
+
27
+
28
+ class AutomationException(Exception):
29
+ """A refusal of an automation route, with the API's status and `code`."""
30
+ def __init__(self, message: str, status: int, code: str):
31
+ super().__init__(message)
32
+ self.status = status
33
+ self.code = code
34
+
35
+
36
+ class AutomationExistsException(AutomationException):
37
+ """The id given to create_automation already has an automation. For a
38
+ retried create this means the first attempt succeeded: treat it as done.
39
+ """
40
+ def __init__(self, message: str, status: int):
41
+ super().__init__(message, status, "AUTOMATION_EXISTS")
42
+
43
+
44
+ class AutomationDeletedException(AutomationException):
45
+ """The id given to create_automation belongs to a deleted automation. A
46
+ deleted id can never be used again: mint a new one.
47
+ """
48
+ def __init__(self, message: str, status: int):
49
+ super().__init__(message, status, "AUTOMATION_DELETED")
50
+
51
+
52
+ def _to_exception(response: Any) -> AutomationException:
53
+ code = "UNKNOWN"
54
+ message = response.reason or ""
55
+ try:
56
+ body = response.json()
57
+ code = body.get("code") or code
58
+ message = body.get("message") or message
59
+ except Exception:
60
+ # Not a JSON refusal (an auth middleware's plain text); keep the status.
61
+ pass
62
+
63
+ if code == "AUTOMATION_EXISTS":
64
+ return AutomationExistsException(message, response.status_code)
65
+ if code == "AUTOMATION_DELETED":
66
+ return AutomationDeletedException(message, response.status_code)
67
+ return AutomationException(message, response.status_code, code)
68
+
69
+
70
+ def _request(access_token: Optional[str], method: str, path: str, body: Optional[dict[str, Any]] = None):
71
+ token, api_base_url = get_key(access_token)
72
+
73
+ response = reachable_request(
74
+ method=method,
75
+ url=f"{api_base_url}/automation{path}",
76
+ headers={"Authorization": f"Bearer {token}"},
77
+ json=body,
78
+ )
79
+
80
+ if not response.ok:
81
+ raise _to_exception(response)
82
+
83
+ return response
84
+
85
+
86
+ def create_automation(
87
+ scope: str,
88
+ name: str,
89
+ trigger_template_id: str,
90
+ action_template_id: str,
91
+ id: Optional[str] = None,
92
+ mapping_function: Optional[str] = None,
93
+ input: Any = None,
94
+ access_token: Optional[str] = None,
95
+ ) -> Automation:
96
+ """Creates an automation.
97
+
98
+ `id` is the id to create it under. Mint a new random uuid for each new
99
+ automation and save it in your own store BEFORE you call this; a retry then
100
+ names the same id, and AutomationExistsException means it is done. Never
101
+ derive it from your own user id: once that automation is deleted, the id can
102
+ never be used again.
103
+ """
104
+ body: dict[str, Any] = {
105
+ "scope": scope,
106
+ "name": name,
107
+ "triggerTemplateId": trigger_template_id,
108
+ "actionTemplateId": action_template_id,
109
+ "mappingFunction": mapping_function,
110
+ "input": input,
111
+ }
112
+ if id is not None:
113
+ body["id"] = id
114
+
115
+ return _request(access_token, "POST", "", body).json()
116
+
117
+
118
+ def get_automation(automation_id: str, access_token: Optional[str] = None) -> Automation:
119
+ return _request(access_token, "GET", f"/{automation_id}").json()
120
+
121
+
122
+ def start_automation(automation_id: str, access_token: Optional[str] = None) -> Automation:
123
+ return _request(access_token, "POST", f"/{automation_id}/start").json()
124
+
125
+
126
+ def stop_automation(automation_id: str, access_token: Optional[str] = None) -> Automation:
127
+ return _request(access_token, "POST", f"/{automation_id}/stop").json()
128
+
129
+
130
+ def delete_automation(automation_id: str, access_token: Optional[str] = None) -> None:
131
+ _request(access_token, "DELETE", f"/{automation_id}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: requence
3
- Version: 0.8.3.dev122
3
+ Version: 0.8.3.dev149
4
4
  Summary: Requence Service
5
5
  Author: Torsten Blindert
6
6
  Requires-Python: >=3.12
@@ -50,7 +50,7 @@ REQUENCE_SERVICE_ACCESS_TOKEN=your-token python main.py
50
50
  ## Your first service
51
51
 
52
52
  Create the service in the Requence UI first — its input schema, configuration schema and
53
- outputs. Then implement the version:
53
+ outputs. Then implement the variant:
54
54
 
55
55
  ```python
56
56
  from requence.service import Service
@@ -43,6 +43,7 @@ requence/service/__init__.py
43
43
  requence/service/boundary.py
44
44
  requence/service/callback_growth.py
45
45
  requence/service/cli.py
46
+ requence/service/dev_session.py
46
47
  requence/service/durable_callback.py
47
48
  requence/service/surface_handler.py
48
49
  requence/service/transformer.py
@@ -52,6 +53,7 @@ requence/service/ui_watch.py
52
53
  requence/task/__init__.py
53
54
  requence/task/abort_task.py
54
55
  requence/task/ack_task.py
56
+ requence/task/automations.py
55
57
  requence/task/cli.py
56
58
  requence/task/get_task.py
57
59
  requence/task/ingress.py
@@ -1,17 +0,0 @@
1
- from .task import Task
2
- from .get_task import get_task
3
- from .abort_task import abort_task, TaskAbortException
4
- from .protect_task import protect_task, TaskProtectException
5
- from .task_watcher import TaskWatcher
6
- from ..exceptions import RequenceUnreachableException
7
-
8
- __all__ = [
9
- "Task",
10
- "get_task",
11
- "abort_task",
12
- "TaskAbortException",
13
- "protect_task",
14
- "TaskProtectException",
15
- "TaskWatcher",
16
- "RequenceUnreachableException",
17
- ]