Repository navigation
Expand file tree
/
Copy pathstudio_workflows.py
More file actions
1111 lines (1038 loc) · 36.1 KB
/
Copy pathstudio_workflows.py
File metadata and controls
1111 lines (1038 loc) · 36.1 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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Workflow asset and mutable Draft routes for Studio."""
import json
import uuid
from fastapi import (
APIRouter,
Depends,
File,
Form,
Header,
HTTPException,
Query,
Request,
UploadFile,
status,
)
from pydantic import ValidationError
from sqlalchemy import func, or_, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession
from backend.api.v1.studio_helpers import (
LOCAL_USER_ID,
canonicalize_studio_graph,
get_project,
get_workflow,
)
from backend.api.v1.studio_schemas import (
DraftRead,
DraftUpdate,
ProjectRuntimeLogRead,
ProjectRuntimeSummaryRead,
ProjectRuntimeTraceRead,
PublishedWorkflowRunStart,
WorkflowCreate,
WorkflowRead,
)
from backend.api.v1.workflows import (
build_evidence_projection,
dispatch_materialized_image_jobs,
get_evidence_batch,
list_evidence_batches,
parse_projection_includes,
)
from backend.database import get_db, rollback_session
from backend.models.gaojixing_collection import GaojixingCollectionRun
from backend.models.studio import (
StudioProject,
StudioWorkflow,
StudioWorkflowDraft,
StudioWorkflowVersion,
)
from backend.models.workflow_run import WorkflowRun
from backend.schemas import workflow as workflow_schemas
from backend.schemas.common import ApiResponse, PaginationMeta
from backend.schemas.workflow_runtime import WorkflowRunStatus, WorkflowRunTraceResponse
from backend.services.agent_project_service import update_workflow_draft
from backend.services.gaojixing_collection_service import (
GaojixingCollectionConflictError,
resume_collection,
)
from backend.workflow.managed_gaojixing_question_batches import (
MAX_QUESTION_BANK_BYTES,
ManagedQuestionBatchConflictError,
ManagedQuestionBatchError,
UnsupportedQuestionBatchFormatError,
accepts_managed_question_batch,
cleanup_managed_question_batch,
stage_managed_question_batch,
)
from backend.workflow.opencli_hda_tracer import (
get_workflow_run_checkpoint,
get_workflow_run_projection,
list_workflow_run_events,
replay_downstream_from_persisted_gaojixing_source,
start_workflow_run,
)
router = APIRouter()
def _canonical_run_identity(*, inputs: dict, user: str) -> str:
return json.dumps(
{"inputs": inputs, "user": user},
sort_keys=True,
separators=(",", ":"),
)
def _runtime_log(
row: WorkflowRun,
*,
workflow_names: dict[str, str],
version_numbers: dict[str, int],
) -> ProjectRuntimeLogRead:
projection = row.projection or {}
request = row.request or {}
duration_ms = max(0, int((row.updated_at - row.created_at).total_seconds() * 1000))
return ProjectRuntimeLogRead(
run_id=row.id,
workflow_id=row.workflow_id,
workflow_name=workflow_names.get(row.workflow_id, "未知工作流"),
workflow_version=version_numbers.get(row.studio_workflow_version_id or ""),
trace_id=row.trace_id,
status=row.status,
trigger=str((request.get("trigger") or {}).get("kind") or "manual"),
response_mode=request.get("responseMode") or "async",
event_count=int(projection.get("eventCount", 0)),
node_count=len(projection.get("nodeStates", []) or []),
error_count=len(projection.get("errors", []) or []),
duration_ms=duration_ms,
started_at=row.created_at,
updated_at=row.updated_at,
)
def _default_published_trigger_kind(
project: workflow_schemas.WorkflowProject,
trigger_node_id: str | None,
) -> workflow_schemas.WorkflowRunTriggerKind:
"""Choose the graph's real trigger when Studio Run omits one.
The authoring UI historically posted ``manual`` unconditionally. That
silently makes schedule-only workflows fail before any node executes. A
direct Studio/CLI run is still an explicit run, but it must enter through
the trigger entry that actually exists in the published graph.
"""
nodes = project.nodes
if trigger_node_id:
selected = next((node for node in nodes if node.id == trigger_node_id), None)
if selected is not None:
if selected.kind == "webhook":
return "webhook"
if selected.kind == "schedule":
params = selected.params
builder = params.get("builder")
if params.get("mode") == "manual" or (
isinstance(builder, dict) and builder.get("nodeType") == "manual-trigger"
):
return "manual"
return "schedule"
for node in nodes:
if node.kind == "schedule" and (
node.params.get("mode") == "manual"
or (
isinstance(node.params.get("builder"), dict)
and node.params["builder"].get("nodeType") == "manual-trigger"
)
):
return "manual"
if any(node.kind == "schedule" for node in nodes):
return "schedule"
if any(node.kind == "webhook" for node in nodes):
return "webhook"
return "manual"
async def _project_runtime_scope(
db: AsyncSession,
*,
workspace_id: str,
project_id: str,
) -> tuple[dict[str, str], dict[str, int]]:
await get_project(db, workspace_id, project_id)
workflows = list(
(
await db.execute(
select(StudioWorkflow).where(
StudioWorkflow.project_id == project_id,
StudioWorkflow.archived.is_(False),
)
)
)
.scalars()
.all()
)
workflow_names = {workflow.id: workflow.name for workflow in workflows}
if not workflow_names:
return workflow_names, {}
versions = list(
(
await db.execute(
select(StudioWorkflowVersion).where(
StudioWorkflowVersion.workflow_id.in_(workflow_names)
)
)
)
.scalars()
.all()
)
return workflow_names, {version.id: version.version for version in versions}
async def _get_project_workflow_run(
db: AsyncSession,
*,
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
) -> WorkflowRun:
await get_workflow(db, workspace_id, project_id, workflow_id)
row = await db.get(WorkflowRun, run_id)
if row is None or row.workflow_id != workflow_id:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Workflow run not found")
return row
@router.get(
"/workspaces/{workspace_id}/projects/{project_id}/workflows",
response_model=ApiResponse[list[WorkflowRead]],
)
async def list_workflows(
workspace_id: str, project_id: str, db: AsyncSession = Depends(get_db)
) -> ApiResponse:
await get_project(db, workspace_id, project_id)
rows = (
(
await db.execute(
select(StudioWorkflow)
.where(
StudioWorkflow.project_id == project_id,
StudioWorkflow.archived.is_(False),
)
.order_by(StudioWorkflow.updated_at.desc())
)
)
.scalars()
.all()
)
return ApiResponse.ok([WorkflowRead.model_validate(row) for row in rows])
@router.get(
"/workspaces/{workspace_id}/projects/{project_id}/runtime-summary",
response_model=ApiResponse[ProjectRuntimeSummaryRead],
)
async def get_project_runtime_summary(
workspace_id: str,
project_id: str,
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
"""Summarize persisted workflow runs for one Studio Project."""
workflow_names, version_numbers = await _project_runtime_scope(
db,
workspace_id=workspace_id,
project_id=project_id,
)
if not workflow_names:
return ApiResponse.ok(
ProjectRuntimeSummaryRead(
total_runs=0,
successful_runs=0,
failed_runs=0,
blocked_runs=0,
running_runs=0,
total_events=0,
recent_logs=[],
)
)
# ponytail: scan compact projection rows here; add maintained counters only
# when project run volume makes this operator-page query measurable.
aggregate_rows = list(
(
await db.execute(
select(WorkflowRun.status, WorkflowRun.projection).where(
WorkflowRun.workflow_id.in_(workflow_names)
)
)
).all()
)
recent_rows = list(
(
await db.execute(
select(WorkflowRun)
.where(WorkflowRun.workflow_id.in_(workflow_names))
.order_by(WorkflowRun.created_at.desc())
.limit(8)
)
)
.scalars()
.all()
)
successful = {"completed", "partial_success"}
failed = {"failed"}
blocked = {"blocked"}
running = {"queued", "running", "waiting", "partial"}
return ApiResponse.ok(
ProjectRuntimeSummaryRead(
total_runs=len(aggregate_rows),
successful_runs=sum(1 for row in aggregate_rows if row.status in successful),
failed_runs=sum(1 for row in aggregate_rows if row.status in failed),
blocked_runs=sum(1 for row in aggregate_rows if row.status in blocked),
running_runs=sum(1 for row in aggregate_rows if row.status in running),
total_events=sum(
int((row.projection or {}).get("eventCount", 0)) for row in aggregate_rows
),
recent_logs=[
_runtime_log(
row,
workflow_names=workflow_names,
version_numbers=version_numbers,
)
for row in recent_rows
],
)
)
@router.get(
"/workspaces/{workspace_id}/projects/{project_id}/runtime-logs",
response_model=ApiResponse[list[ProjectRuntimeLogRead]],
)
async def list_project_runtime_logs(
workspace_id: str,
project_id: str,
run_status: WorkflowRunStatus | None = Query(
default=None,
alias="status",
),
search: str | None = Query(default=None, max_length=255),
page: int = Query(default=1, ge=1),
limit: int = Query(default=20, ge=1, le=100),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
"""List durable Workflow Runs owned by one Studio Project."""
workflow_names, version_numbers = await _project_runtime_scope(
db,
workspace_id=workspace_id,
project_id=project_id,
)
if not workflow_names:
return ApiResponse.ok(
[],
meta=PaginationMeta(total=0, page=page, limit=limit, pages=1),
)
filters = [WorkflowRun.workflow_id.in_(workflow_names)]
if run_status:
filters.append(WorkflowRun.status == run_status)
if search:
pattern = f"%{search.strip()}%"
filters.append(
or_(
WorkflowRun.id.ilike(pattern),
WorkflowRun.trace_id.ilike(pattern),
WorkflowRun.workflow_id.ilike(pattern),
)
)
total = int(await db.scalar(select(func.count()).select_from(WorkflowRun).where(*filters)) or 0)
rows = list(
(
await db.execute(
select(WorkflowRun)
.where(*filters)
.order_by(WorkflowRun.created_at.desc())
.offset((page - 1) * limit)
.limit(limit)
)
)
.scalars()
.all()
)
return ApiResponse.ok(
[
_runtime_log(
row,
workflow_names=workflow_names,
version_numbers=version_numbers,
)
for row in rows
],
meta=PaginationMeta(
total=total,
page=page,
limit=limit,
pages=max(1, -(-total // limit)),
),
)
async def _get_project_workflow_run(
db: AsyncSession,
*,
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
) -> tuple[WorkflowRun, int]:
"""Load one workflow run only through its owning project and version."""
await get_workflow(db, workspace_id, project_id, workflow_id)
result = await db.execute(
select(WorkflowRun, StudioWorkflowVersion.version)
.join(
StudioWorkflowVersion,
WorkflowRun.studio_workflow_version_id == StudioWorkflowVersion.id,
)
.where(
WorkflowRun.id == run_id,
WorkflowRun.workflow_id == workflow_id,
StudioWorkflowVersion.workflow_id == workflow_id,
)
)
scoped_run = result.one_or_none()
if scoped_run is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Workflow run not found")
return scoped_run
async def _published_workflow_version(
db: AsyncSession,
*,
workspace_id: str,
project_id: str,
workflow_id: str,
) -> StudioWorkflowVersion:
workflow = await get_workflow(db, workspace_id, project_id, workflow_id)
if workflow.current_published_version is None:
raise HTTPException(
status.HTTP_409_CONFLICT,
"Workflow must be published before API execution",
)
version = await db.scalar(
select(StudioWorkflowVersion).where(
StudioWorkflowVersion.workflow_id == workflow_id,
StudioWorkflowVersion.version == workflow.current_published_version,
)
)
if version is None:
raise HTTPException(
status.HTTP_409_CONFLICT,
"Published workflow version is unavailable",
)
return version
def _published_run_id(
*,
workspace_id: str,
project_id: str,
workflow_id: str,
version_id: str,
idempotency_key: str | None,
) -> str | None:
if not idempotency_key:
return None
return str(
uuid.uuid5(
uuid.NAMESPACE_URL,
(
"opencli-admin:studio-run:"
f"{workspace_id}:{project_id}:{workflow_id}:{version_id}:{idempotency_key}"
),
)
)
async def _existing_published_run_projection(
db: AsyncSession,
*,
run_id: str,
workflow_id: str,
version_id: str,
requested_identity: str,
) -> workflow_schemas.WorkflowRunProjection | None:
existing = await db.get(WorkflowRun, run_id)
if existing is None:
return None
existing_input = existing.request.get("input") if isinstance(existing.request, dict) else None
existing_payload = existing_input.get("payload") if isinstance(existing_input, dict) else None
existing_user = existing_input.get("sourceId") if isinstance(existing_input, dict) else None
identity_matches = (
isinstance(existing_payload, dict)
and isinstance(existing_user, str)
and _canonical_run_identity(inputs=existing_payload, user=existing_user)
== requested_identity
)
if (
existing.workflow_id != workflow_id
or existing.studio_workflow_version_id != version_id
or not identity_matches
):
raise HTTPException(
status.HTTP_409_CONFLICT,
"Idempotency key collides with another workflow run",
)
projection = await get_workflow_run_projection(run_id, session=db)
if projection is None:
raise HTTPException(
status.HTTP_409_CONFLICT,
"Stored idempotent workflow run is unavailable",
)
return projection
async def _start_published_version_run(
*,
db: AsyncSession,
workspace_id: str,
project_id: str,
workflow_id: str,
version: StudioWorkflowVersion,
run_input: workflow_schemas.WorkflowRunInput,
user: str,
request_id: str,
response_mode: workflow_schemas.WorkflowRunResponseMode,
trigger_kind: workflow_schemas.WorkflowRunTriggerKind | None = None,
trigger_node_id: str | None = None,
idempotency_key: str | None = None,
run_id: str | None = None,
plugins=None,
) -> ApiResponse:
version_id = version.id
resolved_run_id = run_id or _published_run_id(
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
version_id=version_id,
idempotency_key=idempotency_key,
)
requested_identity = _canonical_run_identity(
inputs=run_input.payload,
user=user,
)
if idempotency_key:
existing_projection = await _existing_published_run_projection(
db,
run_id=resolved_run_id,
workflow_id=workflow_id,
version_id=version_id,
requested_identity=requested_identity,
)
if existing_projection is not None:
return ApiResponse.ok(existing_projection)
project = workflow_schemas.WorkflowProject.model_validate(version.graph)
resolved_trigger_kind = trigger_kind or _default_published_trigger_kind(
project,
trigger_node_id,
)
try:
projection = await start_workflow_run(
workflow_schemas.WorkflowRunStartRequest(
project=project,
runId=resolved_run_id,
trigger=workflow_schemas.WorkflowRunTrigger(
kind=resolved_trigger_kind,
triggerNodeId=trigger_node_id,
requestId=request_id,
idempotencyKey=idempotency_key,
),
input=run_input,
responseMode=response_mode,
),
session=db,
studio_workflow_version_id=version_id,
plugins=plugins,
)
except IntegrityError:
if not idempotency_key:
raise
await rollback_session(db)
projection = await _existing_published_run_projection(
db,
run_id=resolved_run_id,
workflow_id=workflow_id,
version_id=version_id,
requested_identity=requested_identity,
)
if projection is None:
raise
return ApiResponse.ok(projection)
await dispatch_materialized_image_jobs(db, projection.runId)
return ApiResponse.ok(projection)
@router.post(
("/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}/runs"),
response_model=ApiResponse[workflow_schemas.WorkflowRunProjection],
status_code=202,
)
async def start_published_workflow_run(
workspace_id: str,
project_id: str,
workflow_id: str,
body: PublishedWorkflowRunStart,
request: Request,
idempotency_header: str | None = Header(default=None, alias="Idempotency-Key"),
request_id_header: str | None = Header(default=None, alias="X-Request-ID"),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
"""Run the immutable published graph without accepting graph replacement."""
version = await _published_workflow_version(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
)
request_id = body.request_id or request_id_header or str(uuid.uuid4())
idempotency_key = body.idempotency_key or idempotency_header
return await _start_published_version_run(
plugins=request.app.state.workflow_plugins,
db=db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
version=version,
run_input=workflow_schemas.WorkflowRunInput(
payload=body.inputs,
source="external",
sourceId=body.user,
),
user=body.user,
request_id=request_id,
idempotency_key=idempotency_key,
response_mode=body.response_mode,
trigger_kind=body.trigger_kind,
trigger_node_id=body.trigger_node_id,
)
@router.post(
("/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}/runs/question-bank"),
response_model=ApiResponse[workflow_schemas.WorkflowRunProjection],
status_code=202,
)
async def start_published_workflow_run_from_question_bank(
http_request: Request,
workspace_id: str,
project_id: str,
workflow_id: str,
question_bank: UploadFile = File(..., alias="questionBank"),
request: str = Form(...),
idempotency_header: str | None = Header(default=None, alias="Idempotency-Key"),
request_id_header: str | None = Header(default=None, alias="X-Request-ID"),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
"""Run the immutable published graph from one managed question package."""
version = await _published_workflow_version(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
)
project = workflow_schemas.WorkflowProject.model_validate(version.graph)
if not accepts_managed_question_batch(project):
raise HTTPException(
status.HTTP_422_UNPROCESSABLE_CONTENT,
"Question bank uploads require the governed Gaojixing workflow packages",
)
try:
body = PublishedWorkflowRunStart.model_validate_json(request)
request_id = body.request_id or request_id_header or str(uuid.uuid4())
idempotency_key = body.idempotency_key or idempotency_header
request_owns_run_directory = idempotency_key is None
run_id = _published_run_id(
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
version_id=version.id,
idempotency_key=idempotency_key,
) or str(uuid.uuid4())
payload = await question_bank.read(MAX_QUESTION_BANK_BYTES + 1)
staged = stage_managed_question_batch(
payload,
filename=question_bank.filename or "",
run_id=run_id,
)
except UnsupportedQuestionBatchFormatError as exc:
raise HTTPException(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, str(exc)) from exc
except ManagedQuestionBatchConflictError as exc:
raise HTTPException(status.HTTP_409_CONFLICT, str(exc)) from exc
except (ManagedQuestionBatchError, ValidationError, json.JSONDecodeError) as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc
finally:
await question_bank.close()
try:
return await _start_published_version_run(
db=db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
version=version,
run_input=workflow_schemas.WorkflowRunInput(
payload={"questionBatchRef": staged.question_batch_ref},
source="external",
sourceId=body.user,
),
user=body.user,
request_id=request_id,
idempotency_key=idempotency_key,
response_mode=body.response_mode,
trigger_kind=body.trigger_kind,
trigger_node_id=body.trigger_node_id,
run_id=run_id,
plugins=http_request.app.state.workflow_plugins,
)
except Exception:
if request_owns_run_directory and staged.created:
cleanup_managed_question_batch(
staged.question_batch_ref,
expected_run_id=run_id,
)
raise
@router.post(
(
"/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}"
"/runs/{run_id}/downstream-replay"
),
response_model=ApiResponse[workflow_schemas.WorkflowRunProjection],
status_code=202,
)
async def replay_persisted_gaojixing_source_downstream(
http_request: Request,
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
"""Replay normalization through sink from completed persisted Gaojixing evidence."""
workflow = await get_workflow(db, workspace_id, project_id, workflow_id)
if workflow.current_published_version is None:
raise HTTPException(
status.HTTP_409_CONFLICT, "Workflow must be published before downstream replay"
)
version = await db.scalar(
select(StudioWorkflowVersion).where(
StudioWorkflowVersion.workflow_id == workflow_id,
StudioWorkflowVersion.version == workflow.current_published_version,
)
)
if version is None:
raise HTTPException(status.HTTP_409_CONFLICT, "Published workflow version is unavailable")
await _get_project_workflow_run(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
run_id=run_id,
)
try:
projection = await replay_downstream_from_persisted_gaojixing_source(
run_id,
expected_workflow_id=workflow_id,
expected_studio_workflow_version_id=version.id,
session=db,
plugins=http_request.app.state.workflow_plugins,
)
except ValueError as exc:
raise HTTPException(status.HTTP_409_CONFLICT, str(exc)) from exc
return ApiResponse.ok(projection)
@router.get(
(
"/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}"
"/runs/{run_id}/trace"
),
response_model=ApiResponse[ProjectRuntimeTraceRead],
)
async def get_project_runtime_trace(
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
after_sequence: int | None = Query(default=None, ge=0, alias="afterSequence"),
limit: int | None = Query(default=None, ge=1, le=500),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
"""Return one project-owned run trace without opening generic run access."""
row, workflow_version = await _get_project_workflow_run(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
run_id=run_id,
)
projection = await get_workflow_run_projection(run_id, session=db)
checkpoint = await get_workflow_run_checkpoint(run_id, session=db)
events = await list_workflow_run_events(
run_id,
session=db,
after_sequence=after_sequence,
limit=limit,
)
if projection is None or checkpoint is None or events is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Workflow run not found")
request = row.request or {}
input_payload = request.get("input") or {}
return ApiResponse.ok(
ProjectRuntimeTraceRead(
workflow_version=workflow_version,
inputs=input_payload.get("payload") or {},
user=input_payload.get("sourceId"),
response_mode=request.get("responseMode") or "async",
trace=WorkflowRunTraceResponse(
projection=projection,
checkpoint=checkpoint,
events=events,
filters={"afterSequence": after_sequence, "limit": limit},
nextAfterSequence=max(
(event.sequence for event in events),
default=after_sequence or 0,
),
),
)
)
@router.get(
"/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}/runs/{run_id}",
response_model=ApiResponse[workflow_schemas.WorkflowRunProjection],
)
async def get_project_workflow_run_projection(
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
await _get_project_workflow_run(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
run_id=run_id,
)
projection = await get_workflow_run_projection(run_id, session=db)
if projection is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Workflow run not found")
return ApiResponse.ok(projection)
@router.get(
"/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}/runs/{run_id}/events",
response_model=ApiResponse[list[workflow_schemas.WorkflowNodeRunEvent]],
)
async def list_project_workflow_run_events(
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
after_sequence: int | None = Query(default=None, ge=0, alias="afterSequence"),
limit: int | None = Query(default=None, ge=1, le=500),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
await _get_project_workflow_run(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
run_id=run_id,
)
events = await list_workflow_run_events(
run_id, session=db, after_sequence=after_sequence, limit=limit
)
if events is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Workflow run not found")
return ApiResponse.ok(events)
@router.get(
"/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}/runs/{run_id}/evidence-batches",
response_model=ApiResponse[workflow_schemas.WorkflowEvidenceBatchListResponse],
)
async def list_project_workflow_evidence_batches(
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
node_id: str | None = Query(default=None),
source_group: str | None = Query(default=None),
cursor: str | None = Query(default=None),
limit: int = Query(default=50, ge=1, le=200),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
await _get_project_workflow_run(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
run_id=run_id,
)
projection = await get_workflow_run_projection(run_id, session=db)
if projection is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Workflow run not found")
try:
batches = list_evidence_batches(
projection,
node_id=node_id,
source_group=source_group,
cursor=cursor,
limit=limit,
)
except ValueError as exc:
raise HTTPException(status.HTTP_400_BAD_REQUEST, str(exc)) from exc
return ApiResponse.ok(batches)
@router.get(
"/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}/runs/{run_id}/evidence-batches/{batch_id}",
response_model=ApiResponse[workflow_schemas.WorkflowEvidenceBatchDetail],
)
async def get_project_workflow_evidence_batch(
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
batch_id: str,
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
await _get_project_workflow_run(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
run_id=run_id,
)
projection = await get_workflow_run_projection(run_id, session=db)
if projection is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Workflow run not found")
batch = get_evidence_batch(projection, batch_id)
if batch is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Evidence batch not found")
return ApiResponse.ok(batch)
@router.get(
"/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}/runs/{run_id}/projection",
response_model=ApiResponse[workflow_schemas.WorkflowEvidenceProjection],
)
async def get_project_workflow_evidence_projection(
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
node_id: str | None = Query(default=None),
source_group: str | None = Query(default=None),
include: str | None = Query(default=None),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
await _get_project_workflow_run(
db,
workspace_id=workspace_id,
project_id=project_id,
workflow_id=workflow_id,
run_id=run_id,
)
projection = await get_workflow_run_projection(run_id, session=db)
if projection is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Workflow run not found")
try:
includes = parse_projection_includes(include)
except ValueError as exc:
raise HTTPException(status.HTTP_400_BAD_REQUEST, str(exc)) from exc
return ApiResponse.ok(
build_evidence_projection(
projection,
node_id=node_id,
source_group=source_group,
includes=includes,
)
)
@router.post(
(
"/workspaces/{workspace_id}/projects/{project_id}/workflows/{workflow_id}"
"/runs/{run_id}/gaojixing/resume"
),
response_model=ApiResponse[workflow_schemas.WorkflowRunProjection],
status_code=202,
)
async def resume_published_gaojixing_run(
workspace_id: str,
project_id: str,
workflow_id: str,
run_id: str,
db: AsyncSession = Depends(get_db),
) -> ApiResponse[workflow_schemas.WorkflowRunProjection]:
"""Resume only a run owned by the requested Studio workflow scope."""
await get_workflow(db, workspace_id, project_id, workflow_id)