-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcontext.py
More file actions
567 lines (470 loc) · 21.4 KB
/
context.py
File metadata and controls
567 lines (470 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
"""
OrchestrationContext and ActivityContext for duroxide Python SDK.
OrchestrationContext methods return ScheduledTask descriptors (dicts) that the
user yields from their generator function. The Rust runtime receives these
descriptors and executes the corresponding DurableFutures.
ActivityContext provides tracing and cancellation for activity functions.
"""
import json
from typing import Any, Optional
from duroxide._duroxide import (
orchestration_trace_log,
orchestration_set_custom_status,
orchestration_reset_custom_status,
orchestration_get_custom_status,
orchestration_set_kv_value,
orchestration_get_kv_value,
orchestration_get_kv_all_values,
orchestration_get_kv_all_keys,
orchestration_get_kv_length,
orchestration_clear_kv_value,
orchestration_clear_all_kv_values,
orchestration_prune_kv_values,
activity_trace_log,
activity_is_cancelled,
activity_tag,
)
class ScheduledTask(dict):
"""A task descriptor that can be yielded from orchestration generators.
Extends dict so it's compatible with JSON serialization and the Rust handler,
while adding chainable builder methods like `.with_tag()`.
"""
def with_tag(self, tag: str) -> "ScheduledTask":
"""Set a routing tag for activity worker specialization.
Tagged activities are only processed by workers whose tag filter matches.
"""
self["tag"] = tag
return self
class OrchestrationContext:
"""Context object passed to orchestration generator functions.
Methods that schedule work return ScheduledTask descriptors to be yielded.
Logging methods are fire-and-forget (no yield needed).
"""
def __init__(self, ctx_info: dict):
self.instance_id: str = ctx_info["instanceId"]
self.execution_id: int = ctx_info["executionId"]
self.orchestration_name: str = ctx_info["orchestrationName"]
self.orchestration_version: str = ctx_info["orchestrationVersion"]
# ─── Scheduling (yield these) ──────────────────────────
def schedule_activity(self, name: str, input=None, session_id: str = None) -> ScheduledTask:
"""Schedule an activity. Yield the return value.
If session_id is provided, the activity will be routed to the worker
owning that session (session affinity).
Chain `.with_tag("gpu")` to route to a specialized worker.
"""
if session_id is not None:
return ScheduledTask({
"type": "activityWithSession",
"name": name,
"input": json.dumps(input),
"sessionId": session_id,
})
return ScheduledTask({
"type": "activity",
"name": name,
"input": json.dumps(input),
})
def schedule_activity_on_session(
self, name: str, input, session_id: str
) -> ScheduledTask:
"""Schedule an activity with session affinity. Yield the return value.
Activities with the same session_id are routed to the same worker.
Chain `.with_tag("gpu")` to route to a specialized worker.
"""
return ScheduledTask({
"type": "activityWithSession",
"name": name,
"input": json.dumps(input),
"sessionId": session_id,
})
def schedule_activity_with_retry(self, name: str, input, retry: dict) -> ScheduledTask:
"""Schedule an activity with retry policy. Yield the return value."""
return ScheduledTask({
"type": "activityWithRetry",
"name": name,
"input": json.dumps(input),
"retry": {
"maxAttempts": retry.get("max_attempts", retry.get("maxAttempts", 3)),
"timeoutMs": retry.get("timeout_ms", retry.get("timeoutMs")),
"totalTimeoutMs": retry.get(
"total_timeout_ms", retry.get("totalTimeoutMs")
),
"backoff": retry.get("backoff"),
},
})
def schedule_activity_with_retry_on_session(
self, name: str, input, retry: dict, session_id: str
) -> ScheduledTask:
"""Schedule an activity with retry policy and session affinity. Yield the return value.
All retry attempts are pinned to the same session_id, ensuring they
execute on the same worker.
"""
return ScheduledTask({
"type": "activityWithRetryOnSession",
"name": name,
"input": json.dumps(input),
"retry": {
"maxAttempts": retry.get("max_attempts", retry.get("maxAttempts", 3)),
"timeoutMs": retry.get("timeout_ms", retry.get("timeoutMs")),
"totalTimeoutMs": retry.get(
"total_timeout_ms", retry.get("totalTimeoutMs")
),
"backoff": retry.get("backoff"),
},
"sessionId": session_id,
})
def schedule_timer(self, delay_ms: int) -> dict:
"""Schedule a timer (delay in milliseconds). Yield the return value."""
return {"type": "timer", "delayMs": delay_ms}
def wait_for_event(self, name: str) -> dict:
"""Wait for an external event. Yield the return value."""
return {"type": "waitEvent", "name": name}
def dequeue_event(self, queue_name: str) -> dict:
"""Dequeue the next message from a named event queue (FIFO mailbox semantics).
Unlike wait_for_event, queued events use FIFO matching:
- Events that arrive before a subscription are buffered until consumed
- Events survive continue_as_new boundaries
- The caller enqueues messages with client.enqueue_event()
Yield the return value.
"""
return {"type": "dequeueEvent", "queueName": queue_name}
def schedule_sub_orchestration(self, name: str, input=None) -> dict:
"""Schedule a sub-orchestration. Yield the return value."""
return {
"type": "subOrchestration",
"name": name,
"input": json.dumps(input),
}
def schedule_sub_orchestration_with_id(
self, name: str, instance_id: str, input=None
) -> dict:
"""Schedule a sub-orchestration with a specific instance ID."""
return {
"type": "subOrchestrationWithId",
"name": name,
"instanceId": instance_id,
"input": json.dumps(input),
}
def schedule_sub_orchestration_versioned(
self, name: str, version: str = None, input=None
) -> dict:
"""Schedule a versioned sub-orchestration. Yield the return value."""
return {
"type": "subOrchestrationVersioned",
"name": name,
"version": version,
"input": json.dumps(input),
}
def schedule_sub_orchestration_versioned_with_id(
self, name: str, version: str, instance_id: str, input=None
) -> dict:
"""Schedule a versioned sub-orchestration with a specific instance ID."""
return {
"type": "subOrchestrationVersionedWithId",
"name": name,
"version": version,
"instanceId": instance_id,
"input": json.dumps(input),
}
def start_orchestration(self, name: str, instance_id: str, input=None) -> dict:
"""Start a detached orchestration (fire-and-forget). Yield the return value."""
return {
"type": "orchestration",
"name": name,
"instanceId": instance_id,
"input": json.dumps(input),
}
def start_orchestration_versioned(
self, name: str, version: str, instance_id: str, input=None
) -> dict:
"""Start a versioned detached orchestration (fire-and-forget)."""
return {
"type": "orchestrationVersioned",
"name": name,
"version": version,
"instanceId": instance_id,
"input": json.dumps(input),
}
def new_guid(self) -> dict:
"""Get a deterministic GUID. Yield the return value."""
return {"type": "newGuid"}
def utc_now(self) -> dict:
"""Get the current deterministic UTC time (ms). Yield the return value."""
return {"type": "utcNow"}
def continue_as_new(self, input=None) -> dict:
"""Continue the orchestration as a new instance with new input."""
return {
"type": "continueAsNew",
"input": json.dumps(input),
}
def continue_as_new_versioned(self, input, version: str = None) -> dict:
"""Continue as new with a specific version."""
return {
"type": "continueAsNewVersioned",
"input": json.dumps(input),
"version": version,
}
# ─── Typed scheduling (auto JSON serialization/deserialization) ──
def schedule_activity_typed(
self, name: str, input: Any = None, result_type: type = None
) -> ScheduledTask:
"""Schedule activity with auto JSON serialization/deserialization.
Input is auto-serialized via json.dumps if not already a string.
Result is auto-deserialized via json.loads.
Chain `.with_tag("gpu")` to route to a specialized worker.
"""
raw_input = input if isinstance(input, str) else json.dumps(input)
return ScheduledTask({
"type": "activity",
"name": name,
"input": raw_input,
"_typed": True,
})
def schedule_sub_orchestration_typed(
self, name: str, input: Any = None, result_type: type = None
) -> dict:
"""Schedule sub-orchestration with auto JSON serialization/deserialization.
Input is auto-serialized via json.dumps if not already a string.
Result is auto-deserialized via json.loads.
"""
raw_input = input if isinstance(input, str) else json.dumps(input)
return {
"type": "subOrchestration",
"name": name,
"input": raw_input,
"_typed": True,
}
def schedule_activity_on_session_typed(
self, name: str, input: Any = None, session_id: str = None
) -> ScheduledTask:
"""Schedule activity with session affinity and auto JSON serialization/deserialization.
Input is auto-serialized via json.dumps if not already a string.
Result is auto-deserialized via json.loads.
"""
raw_input = input if isinstance(input, str) else json.dumps(input)
return ScheduledTask({
"type": "activityWithSession",
"name": name,
"input": raw_input,
"sessionId": session_id,
"_typed": True,
})
def schedule_activity_with_retry_typed(
self, name: str, input: Any = None, retry: dict = None
) -> ScheduledTask:
"""Schedule activity with retry policy and auto JSON serialization/deserialization.
Input is auto-serialized via json.dumps if not already a string.
Result is auto-deserialized via json.loads.
"""
raw_input = input if isinstance(input, str) else json.dumps(input)
retry = retry or {}
return ScheduledTask({
"type": "activityWithRetry",
"name": name,
"input": raw_input,
"retry": {
"maxAttempts": retry.get("max_attempts", retry.get("maxAttempts", 3)),
"timeoutMs": retry.get("timeout_ms", retry.get("timeoutMs")),
"totalTimeoutMs": retry.get(
"total_timeout_ms", retry.get("totalTimeoutMs")
),
"backoff": retry.get("backoff"),
},
"_typed": True,
})
def schedule_activity_with_retry_on_session_typed(
self, name: str, input: Any = None, retry: dict = None, session_id: str = None
) -> ScheduledTask:
"""Schedule activity with retry policy, session affinity, and auto JSON serde.
Input is auto-serialized via json.dumps if not already a string.
Result is auto-deserialized via json.loads.
"""
raw_input = input if isinstance(input, str) else json.dumps(input)
retry = retry or {}
return ScheduledTask({
"type": "activityWithRetryOnSession",
"name": name,
"input": raw_input,
"retry": {
"maxAttempts": retry.get("max_attempts", retry.get("maxAttempts", 3)),
"timeoutMs": retry.get("timeout_ms", retry.get("timeoutMs")),
"totalTimeoutMs": retry.get(
"total_timeout_ms", retry.get("totalTimeoutMs")
),
"backoff": retry.get("backoff"),
},
"sessionId": session_id,
"_typed": True,
})
def wait_for_event_typed(self, name: str) -> dict:
"""Wait for an external event with auto JSON deserialization.
Result is auto-deserialized via json.loads.
"""
return {"type": "waitEvent", "name": name, "_typed": True}
def dequeue_event_typed(self, queue_name: str) -> dict:
"""Dequeue the next message from a named event queue with auto JSON deserialization.
Result is auto-deserialized via json.loads.
"""
return {"type": "dequeueEvent", "queueName": queue_name, "_typed": True}
def schedule_sub_orchestration_with_id_typed(
self, name: str, instance_id: str, input: Any = None
) -> dict:
"""Schedule sub-orchestration with a specific instance ID and auto JSON serde.
Input is auto-serialized via json.dumps if not already a string.
Result is auto-deserialized via json.loads.
"""
raw_input = input if isinstance(input, str) else json.dumps(input)
return {
"type": "subOrchestrationWithId",
"name": name,
"instanceId": instance_id,
"input": raw_input,
"_typed": True,
}
def start_orchestration_typed(
self, name: str, instance_id: str, input: Any = None
) -> dict:
"""Start a detached orchestration (fire-and-forget) with auto JSON input serialization.
Input is auto-serialized via json.dumps if not already a string.
"""
raw_input = input if isinstance(input, str) else json.dumps(input)
return {
"type": "orchestration",
"name": name,
"instanceId": instance_id,
"input": raw_input,
}
def start_orchestration_versioned_typed(
self, name: str, version: str, instance_id: str, input: Any = None
) -> dict:
"""Start a versioned detached orchestration (fire-and-forget) with auto JSON input serialization."""
raw_input = input if isinstance(input, str) else json.dumps(input)
return {
"type": "orchestrationVersioned",
"name": name,
"version": version,
"instanceId": instance_id,
"input": raw_input,
}
def continue_as_new_typed(self, input: Any = None) -> dict:
"""Continue the orchestration as a new instance with auto JSON input serialization."""
raw_input = input if isinstance(input, str) else json.dumps(input)
return {
"type": "continueAsNew",
"input": raw_input,
}
# ─── Composition helpers ───────────────────────────────
def all(self, tasks: list) -> dict:
"""Join multiple tasks (wait for all). Yield the return value."""
return {"type": "join", "tasks": tasks}
def race(self, *tasks) -> dict:
"""Select/race multiple tasks (wait for first). Yield the return value."""
return {"type": "select", "tasks": list(tasks)}
def all_typed(self, tasks: list) -> dict:
"""Like all() but auto-unwraps ok values from join results.
Returns a list of result values instead of [{"ok": val}, ...].
"""
return {"type": "join", "tasks": tasks, "_typed_all": True}
def race_typed(self, *tasks) -> dict:
"""Like race() but marks result for typed processing.
Returns {"index": N, "value": val} with value already parsed.
"""
return {"type": "select", "tasks": list(tasks), "_typed_race": True}
# ─── Custom Status (fire-and-forget, delegates to Rust ctx) ──
def set_custom_status(self, status: str):
"""Set a custom status string on this orchestration instance.
Fire-and-forget — no yield needed. Last write per turn wins.
Persistent across turns: if you don't call it on a later turn,
the provider keeps the previous value.
"""
orchestration_set_custom_status(self.instance_id, str(status))
def reset_custom_status(self):
"""Clear the custom status back to None.
Fire-and-forget — no yield needed.
"""
orchestration_reset_custom_status(self.instance_id)
def get_custom_status(self) -> Optional[str]:
"""Read the current custom status value.
Returns the status string or None if none has been set.
Reflects all set/reset calls made so far, including across turns
and continue-as-new boundaries.
"""
return orchestration_get_custom_status(self.instance_id)
def set_kv_value(self, key: str, value: str):
"""Set a key-value pair scoped to this orchestration instance."""
orchestration_set_kv_value(self.instance_id, str(key), str(value))
def get_kv_value(self, key: str) -> Optional[str]:
"""Get the current value for a key. Returns None if not set."""
return orchestration_get_kv_value(self.instance_id, str(key))
def get_kv_all_values(self) -> dict[str, str]:
"""Return a snapshot of all key-value pairs for this orchestration instance."""
return orchestration_get_kv_all_values(self.instance_id)
def get_kv_all_keys(self) -> list[str]:
"""Return the list of KV keys currently set on this orchestration instance."""
return orchestration_get_kv_all_keys(self.instance_id)
def get_kv_length(self) -> int:
"""Return the number of KV entries currently set on this orchestration instance."""
return orchestration_get_kv_length(self.instance_id)
def clear_kv_value(self, key: str):
"""Remove a single key from the KV store."""
orchestration_clear_kv_value(self.instance_id, str(key))
def clear_all_kv_values(self):
"""Clear ALL key-value pairs for this orchestration instance."""
orchestration_clear_all_kv_values(self.instance_id)
def prune_kv_values_updated_before(self, cutoff_ms: int) -> int:
"""Prune KV entries whose last persisted update is older than cutoff_ms."""
return orchestration_prune_kv_values(self.instance_id, cutoff_ms)
def get_kv_value_from_instance(self, instance_id: str, key: str) -> ScheduledTask:
"""Read a KV value from another orchestration instance via the built-in syscall activity."""
return ScheduledTask({
"type": "activity",
"name": "__duroxide_syscall:get_kv_value",
"input": json.dumps({"instance_id": instance_id, "key": key}),
})
# ─── Logging (fire-and-forget, delegates to Rust ctx.trace()) ───
def trace_info(self, message: str):
orchestration_trace_log(self.instance_id, "info", str(message))
def trace_warn(self, message: str):
orchestration_trace_log(self.instance_id, "warn", str(message))
def trace_error(self, message: str):
orchestration_trace_log(self.instance_id, "error", str(message))
def trace_debug(self, message: str):
orchestration_trace_log(self.instance_id, "debug", str(message))
class ActivityContext:
"""Context for activity execution."""
def __init__(self, ctx_info: dict):
self.instance_id: str = ctx_info["instanceId"]
self.execution_id: int = ctx_info["executionId"]
self.orchestration_name: str = ctx_info["orchestrationName"]
self.orchestration_version: str = ctx_info["orchestrationVersion"]
self.activity_name: str = ctx_info["activityName"]
self.worker_id: str = ctx_info["workerId"]
self.session_id: str = ctx_info.get("sessionId")
self._tag: Optional[str] = ctx_info.get("tag")
self._trace_token: str = ctx_info["_traceToken"]
def tag(self) -> Optional[str]:
"""Returns the routing tag if this activity was scheduled via .with_tag().
Returns None for activities scheduled without a tag.
"""
return self._tag
def trace_info(self, message: str):
activity_trace_log(self._trace_token, "info", str(message))
def trace_warn(self, message: str):
activity_trace_log(self._trace_token, "warn", str(message))
def trace_error(self, message: str):
activity_trace_log(self._trace_token, "error", str(message))
def trace_debug(self, message: str):
activity_trace_log(self._trace_token, "debug", str(message))
def is_cancelled(self) -> bool:
"""Check if this activity has been cancelled (e.g., lost a race/select)."""
return activity_is_cancelled(self._trace_token)
def get_client(self):
"""Get a Client from this activity's context (for starting orchestrations, etc.)."""
from duroxide._duroxide import activity_get_client
from duroxide import Client
native = activity_get_client(self._trace_token)
if native is None:
raise RuntimeError("Activity context not found")
client = Client.__new__(Client)
client._native = native
return client