diff --git a/platform/tasktypes/external_review.go b/platform/tasktypes/external_review.go index ebe86dbb..249a027d 100644 --- a/platform/tasktypes/external_review.go +++ b/platform/tasktypes/external_review.go @@ -242,11 +242,23 @@ func replyTokenFrom(inputs map[string]any) (string, error) { return token, nil } +// flowIDSep joins a task's ID and the other side's workflow in the ID of the flow the +// task starts there. +const flowIDSep = "-" + +// dispatchFlowID is the ID of the flow taskID starts on the other side to run +// workflowID: one per task and workflow, so a task can start several of the other +// side's workflows, and a retried dispatch names the same flow. A step that runs again +// replies on the other side's token: a second dispatch of the same workflow from the +// same task names the flow already started. +func dispatchFlowID(taskID, workflowID string) string { + return taskID + flowIDSep + workflowID +} + // startFlowBody is the body of the other side's client flows route: the workflow to -// start, this task's ID as the flow's, so a retried dispatch is recognised as the same -// flow, the data as its input, and callbackToken. callbackToken is opaque and names the -// step this dispatch is for; the reviewer calls back on it, so a late or repeated -// callback can't complete a later step. +// start, the flow's ID (see dispatchFlowID), the data as its input, and callbackToken. +// callbackToken is opaque and names the step this dispatch is for; the reviewer calls +// back on it, so a late or repeated callback can't complete a later step. // // data carries only the values declared by the workflow node's input_mapping // — not the full record state — so the external reviewer sees the explicit @@ -254,7 +266,7 @@ func replyTokenFrom(inputs map[string]any) (string, error) { func startFlowBody(record *store.TaskRecord, workflowID string, data any, callbackToken string) map[string]any { return map[string]any{ "workflowId": workflowID, - "flowId": record.TaskID, + "flowId": dispatchFlowID(record.TaskID, workflowID), "input": data, "callbackToken": callbackToken, } diff --git a/platform/tasktypes/external_review_test.go b/platform/tasktypes/external_review_test.go index 05b3be72..5637794c 100644 --- a/platform/tasktypes/external_review_test.go +++ b/platform/tasktypes/external_review_test.go @@ -105,7 +105,7 @@ func TestExternalReview_DispatchesWithoutReplyToken(t *testing.T) { assert.Equal(t, "/api/v1/internal/cases/case-1/flows", call.path) assert.Equal(t, map[string]any{ "workflowId": "review", - "flowId": reviewTaskID, + "flowId": reviewTaskID + "-review", "callbackToken": ownToken(t), "input": map[string]any{"field": "v1"}, }, call.body) @@ -137,6 +137,43 @@ func TestExternalReview_DispatchNeeds(t *testing.T) { } } +func TestDispatchFlowID(t *testing.T) { + assert.Equal(t, reviewTaskID+"-officer_review", dispatchFlowID(reviewTaskID, "officer_review")) +} + +// A task that starts two of the other side's workflows, such as a payment check and then +// the officer's review, starts each as its own flow. +func TestExternalReview_FlowIDPerWorkflow(t *testing.T) { + mgr, svc := newReviewService(t, http.StatusOK) + p := NewExternalReviewPlugin(mgr, tokenFor, noCaseAttributes{}) + + for _, workflowID := range []string{"payment_confirm", "officer_review"} { + ctx := reviewContext(map[string]any{"submission": map[string]any{}}) + cfg := fmt.Sprintf(`{"service_id": "agency", "path": "/api/v1/internal/cases/{caseId}/flows", "service_workflow_id": %q}`, workflowID) + require.ErrorIs(t, p.Execute(ctx, json.RawMessage(cfg)), ErrSuspended) + } + + calls := svc.calls() + require.Len(t, calls, 2) + assert.Equal(t, reviewTaskID+"-payment_confirm", calls[0].body["flowId"]) + assert.Equal(t, reviewTaskID+"-officer_review", calls[1].body["flowId"]) +} + +// A retried dispatch names the same flow, so the other side takes it for a retry. +func TestExternalReview_RetryKeepsFlowID(t *testing.T) { + mgr, svc := newReviewService(t, http.StatusOK) + p := NewExternalReviewPlugin(mgr, tokenFor, noCaseAttributes{}) + cfg := json.RawMessage(`{"service_id": "agency", "path": "/api/v1/internal/cases/{caseId}/flows", "service_workflow_id": "review"}`) + + for range 2 { + require.ErrorIs(t, p.Execute(reviewContext(map[string]any{"submission": map[string]any{}}), cfg), ErrSuspended) + } + + calls := svc.calls() + require.Len(t, calls, 2) + assert.Equal(t, calls[0].body["flowId"], calls[1].body["flowId"]) +} + // The case ID is escaped into its path segment. func TestExternalReview_EscapesTheCaseID(t *testing.T) { mgr, svc := newReviewService(t, http.StatusOK)