-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_hitl.py
More file actions
571 lines (385 loc) · 19 KB
/
test_hitl.py
File metadata and controls
571 lines (385 loc) · 19 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
568
569
570
571
"""
Unit tests for Human-in-the-Loop (HITL) from Tasks and Workflows Guide.
Tests ctx.request_feedback() feature with various feedback types and patterns.
Uses threading and mocking to simulate human responses.
"""
import threading
import time
from typing import Optional
import pytest
from graflow.core.context import TaskExecutionContext
from graflow.core.decorators import task
from graflow.core.workflow import workflow
from graflow.exceptions import GraflowWorkflowCanceledError
from graflow.hitl.types import FeedbackResponse, FeedbackTimeoutError, FeedbackType
def provide_feedback_after_delay(
context: TaskExecutionContext,
approved: bool = True,
text: Optional[str] = None,
selected: Optional[str] = None,
reason: Optional[str] = None,
delay: float = 0.5,
):
"""Helper to provide feedback after a delay in a separate thread"""
def _provide():
time.sleep(delay)
manager = context.execution_context.feedback_manager
pending = manager.list_pending_requests(session_id=context.session_id)
if pending:
request = next((item for item in pending if item.task_id == context.task_id), pending[0])
feedback_id = request.feedback_id
response_type = request.feedback_type
response = FeedbackResponse(
feedback_id=feedback_id,
response_type=response_type,
approved=approved if response_type == FeedbackType.APPROVAL else None,
reason=reason if response_type == FeedbackType.APPROVAL else None,
text=text if response_type == FeedbackType.TEXT else None,
selected=selected if response_type == FeedbackType.SELECTION else None,
responded_by="test_user",
)
manager.provide_feedback(feedback_id, response)
thread = threading.Thread(target=_provide, daemon=True)
thread.start()
class TestBasicApproval:
"""Tests for basic approval feedback"""
def test_approval_approved(self):
"""Test approval request that gets approved"""
with workflow("approval_test") as wf:
@task(inject_context=True)
def request_approval(ctx: TaskExecutionContext):
# Provide feedback in background
provide_feedback_after_delay(ctx, approved=True)
response = ctx.request_feedback(feedback_type="approval", prompt="Approve this action?", timeout=5.0)
return response.approved
result = wf.execute()
assert result is True
def test_approval_rejected(self):
"""Test approval request that gets rejected"""
with workflow("rejection_test") as wf:
@task(inject_context=True)
def request_approval(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, approved=False)
response = ctx.request_feedback(feedback_type="approval", prompt="Approve this action?", timeout=5.0)
return response.approved
result = wf.execute()
assert result is False
def test_approval_with_reason(self):
"""Test approval with reason field"""
with workflow("approval_reason") as wf:
@task(inject_context=True)
def request_approval(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, approved=True, reason="Looks good to me!")
response = ctx.request_feedback(feedback_type="approval", prompt="Approve deployment?", timeout=5.0)
return {"approved": response.approved, "reason": response.reason}
result = wf.execute()
assert isinstance(result, dict)
assert result["approved"] is True
assert result["reason"] == "Looks good to me!"
class TestTextInput:
"""Tests for text input feedback"""
def test_text_input_basic(self):
"""Test basic text input"""
with workflow("text_input") as wf:
@task(inject_context=True)
def request_comment(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, text="This is my comment")
response = ctx.request_feedback(feedback_type="text", prompt="Enter your comment:", timeout=5.0)
return response.text
result = wf.execute()
assert result == "This is my comment"
def test_text_input_with_metadata(self):
"""Test text input with custom metadata"""
with workflow("text_metadata") as wf:
@task(inject_context=True)
def request_input(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, text="User input")
response = ctx.request_feedback(
feedback_type="text",
prompt="Provide feedback:",
metadata={"task": "review", "version": "1.0"},
timeout=5.0,
)
return response.text
result = wf.execute()
assert result == "User input"
class TestSelection:
"""Tests for selection feedback"""
def test_selection_basic(self):
"""Test basic selection from options"""
with workflow("selection") as wf:
@task(inject_context=True)
def request_selection(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, selected="option2")
response = ctx.request_feedback(
feedback_type="selection",
prompt="Choose an option:",
options=["option1", "option2", "option3"],
timeout=5.0,
)
return response.selected
result = wf.execute()
assert result == "option2"
def test_selection_with_options(self):
"""Test selection with descriptive options"""
with workflow("selection_modes") as wf:
@task(inject_context=True)
def choose_mode(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, selected="balanced")
response = ctx.request_feedback(
feedback_type="selection",
prompt="Choose execution mode:",
options=["fast", "balanced", "thorough"],
timeout=5.0,
)
return response.selected
result = wf.execute()
assert result == "balanced"
class TestChannelIntegration:
"""Tests for feedback with channel integration"""
def test_write_to_channel(self):
"""Test automatic writing of feedback response to channel"""
with workflow("channel_feedback") as wf:
@task(inject_context=True)
def request_with_channel(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, approved=True)
response = ctx.request_feedback(
feedback_type="approval",
prompt="Approve?",
channel_key="deployment_approved",
write_to_channel=True,
timeout=5.0,
)
# Response should be automatically written to channel
from_channel = ctx.get_channel().get("deployment_approved")
return {"response": response.approved, "from_channel": from_channel}
result = wf.execute()
assert isinstance(result, dict)
assert result["response"] is True
# Channel should contain the approval status
assert result["from_channel"] is not None
def test_manual_channel_write(self):
"""Test manually writing feedback to channel"""
with workflow("manual_channel") as wf:
@task(inject_context=True)
def request_and_store(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, text="Important note")
response = ctx.request_feedback(feedback_type="text", prompt="Enter note:", timeout=5.0)
# Manually write to channel
ctx.get_channel().set("user_note", response.text)
return response.text
@task(inject_context=True)
def use_feedback(ctx: TaskExecutionContext):
# Retrieve feedback from channel
note = ctx.get_channel().get("user_note")
return f"Processed: {note}"
request_and_store >> use_feedback # type: ignore
result = wf.execute()
assert result == "Processed: Important note"
class TestTimeoutBehavior:
"""Tests for feedback timeout scenarios"""
def test_quick_response_within_timeout(self):
"""Test response received well within timeout"""
with workflow("quick_response") as wf:
@task(inject_context=True)
def quick_feedback(ctx: TaskExecutionContext):
# Provide feedback almost immediately
provide_feedback_after_delay(ctx, approved=True, delay=0.1)
response = ctx.request_feedback(feedback_type="approval", prompt="Quick approval?", timeout=10.0)
return response.approved
result = wf.execute()
assert result is True
def test_timeout_error_handling(self):
"""Test handling of timeout errors"""
with workflow("timeout_test") as wf:
@task(inject_context=True)
def timeout_feedback(ctx: TaskExecutionContext):
# Don't provide feedback - let it timeout
try:
ctx.request_feedback(
feedback_type="approval",
prompt="This will timeout",
timeout=1.0, # Short timeout
)
return "Should not reach here"
except FeedbackTimeoutError:
return "Timeout handled"
result = wf.execute()
assert result == "Timeout handled"
class TestWorkflowIntegration:
"""Tests for HITL integration in complex workflows"""
def test_approval_gates_in_pipeline(self):
"""Test multiple approval gates in a pipeline"""
with workflow("approval_pipeline") as wf:
@task(inject_context=True)
def stage1(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, approved=True)
response = ctx.request_feedback(feedback_type="approval", prompt="Approve stage 1?", timeout=5.0)
if not response.approved:
ctx.cancel_workflow("Stage 1 rejected")
return "Stage 1 complete"
@task(inject_context=True)
def stage2(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, approved=True, delay=0.6)
response = ctx.request_feedback(feedback_type="approval", prompt="Approve stage 2?", timeout=5.0)
if not response.approved:
ctx.cancel_workflow("Stage 2 rejected")
return "Stage 2 complete"
@task
def final_stage():
return "Pipeline complete"
stage1 >> stage2 >> final_stage # type: ignore
result = wf.execute()
assert result == "Pipeline complete"
def test_rejected_approval_cancels_workflow(self):
"""Test that rejected approval can cancel workflow"""
with workflow("cancel_on_reject") as wf:
@task(inject_context=True)
def gate_task(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, approved=False)
response = ctx.request_feedback(feedback_type="approval", prompt="Approve?", timeout=5.0)
if not response.approved:
ctx.cancel_workflow("User rejected")
return "Approved"
@task
def should_not_run():
return "Should not execute"
gate_task >> should_not_run # type: ignore
with pytest.raises(GraflowWorkflowCanceledError):
wf.execute()
def test_conditional_branching_with_feedback(self):
"""Test conditional workflow branching based on feedback"""
with workflow("conditional_feedback") as wf:
@task(inject_context=True)
def choose_path(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, selected="fast")
response = ctx.request_feedback(
feedback_type="selection",
prompt="Choose processing mode:",
options=["fast", "thorough"],
timeout=5.0,
)
ctx.get_channel().set("mode", response.selected)
return response.selected
@task(inject_context=True)
def process(ctx: TaskExecutionContext):
mode = ctx.get_channel().get("mode")
if mode == "fast":
return "Fast processing complete"
else:
return "Thorough processing complete"
choose_path >> process # type: ignore
result = wf.execute()
assert result == "Fast processing complete"
class TestFeedbackHandler:
"""Tests for custom feedback handlers"""
def test_feedback_with_custom_handler(self):
"""Test feedback with custom handler callbacks"""
handler_calls = {"created": False, "received": False, "timeout": False}
class CustomHandler:
def on_request_created(self, request):
handler_calls["created"] = True
def on_response_received(self, request, response):
handler_calls["received"] = True
def on_request_timeout(self, request):
handler_calls["timeout"] = True
with workflow("custom_handler") as wf:
@task(inject_context=True)
def with_handler(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, approved=True)
handler = CustomHandler()
response = ctx.request_feedback(
feedback_type="approval", prompt="Test handler", timeout=5.0, handler=handler
)
return response.approved
result = wf.execute()
assert result is True
assert handler_calls["created"] is True
assert handler_calls["received"] is True
assert handler_calls["timeout"] is False
class TestMultipleFeedbackTypes:
"""Tests for different feedback types in same workflow"""
def test_mixed_feedback_types(self):
"""Test workflow with approval, text, and selection"""
with workflow("mixed_feedback") as wf:
@task(inject_context=True)
def approval_step(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, approved=True)
response = ctx.request_feedback(feedback_type="approval", prompt="Start process?", timeout=5.0)
return response.approved
@task(inject_context=True)
def text_step(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, text="Process notes", delay=0.6)
response = ctx.request_feedback(feedback_type="text", prompt="Enter notes:", timeout=5.0)
ctx.get_channel().set("notes", response.text)
return response.text
@task(inject_context=True)
def selection_step(ctx: TaskExecutionContext):
provide_feedback_after_delay(ctx, selected="medium", delay=0.7)
response = ctx.request_feedback(
feedback_type="selection", prompt="Choose priority:", options=["low", "medium", "high"], timeout=5.0
)
return response.selected
approval_step >> text_step >> selection_step # type: ignore
_, ctx = wf.execute(ret_context=True)
assert ctx.get_result("approval_step") is True
assert ctx.get_result("text_step") == "Process notes"
assert ctx.get_result("selection_step") == "medium"
class TestRealWorldPatterns:
"""Tests for real-world HITL patterns"""
def test_deployment_approval_pattern(self):
"""Test deployment approval pattern with metadata"""
with workflow("deployment") as wf:
@task
def prepare_deployment():
return {"version": "v1.2.3", "environment": "production", "changes": ["feature-x", "bugfix-y"]}
@task(inject_context=True)
def request_approval(ctx: TaskExecutionContext):
deployment_info = ctx.get_channel().get("prepare_deployment.__result__")
provide_feedback_after_delay(ctx, approved=True)
response = ctx.request_feedback(
feedback_type="approval",
prompt=f"Approve deployment {deployment_info['version']} to {deployment_info['environment']}?",
metadata=deployment_info,
timeout=5.0,
)
if not response.approved:
ctx.cancel_workflow("Deployment cancelled by user")
return response.approved
@task(inject_context=True)
def deploy(ctx: TaskExecutionContext):
approved = ctx.get_channel().get("request_approval.__result__")
if approved:
return {"status": "deployed", "timestamp": time.time()}
return {"status": "cancelled"}
prepare_deployment >> request_approval >> deploy # type: ignore
result = wf.execute()
assert isinstance(result, dict)
assert result["status"] == "deployed"
def test_data_validation_pattern(self):
"""Test human validation of processed data"""
with workflow("data_validation") as wf:
@task
def process_data():
return {"records_processed": 1000, "errors": 5, "success_rate": 0.995}
@task(inject_context=True)
def validate_results(ctx: TaskExecutionContext):
results = ctx.get_channel().get("process_data.__result__")
provide_feedback_after_delay(ctx, text="Errors are acceptable, proceed")
response = ctx.request_feedback(
feedback_type="text",
prompt=f"Processed {results['records_processed']} records with {results['errors']} errors. Comments?",
metadata=results,
timeout=5.0,
)
ctx.get_channel().set("validation_comment", response.text)
return "validated"
@task(inject_context=True)
def finalize(ctx: TaskExecutionContext):
comment = ctx.get_channel().get("validation_comment")
return {"status": "complete", "comment": comment}
process_data >> validate_results >> finalize # type: ignore
result = wf.execute()
assert isinstance(result, dict)
assert result["status"] == "complete"
assert "acceptable" in result["comment"]