From b51455c6ef1fcc5e0ae6d30c01f29d8c47148c45 Mon Sep 17 00:00:00 2001 From: Sanduni Pavithra Date: Thu, 3 Dec 2026 14:59:37 +0530 Subject: [PATCH 1/6] fix(platform/tasktypes): start one agency flow per task and workflow Send - as the flowId, so one task can start several of the other side's workflows. --- platform/tasktypes/external_review.go | 26 +++++++++-- platform/tasktypes/external_review_test.go | 53 +++++++++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/platform/tasktypes/external_review.go b/platform/tasktypes/external_review.go index ebe86dbb..17c8f85d 100644 --- a/platform/tasktypes/external_review.go +++ b/platform/tasktypes/external_review.go @@ -144,6 +144,10 @@ func (p *ExternalReviewPlugin) dispatchRequest(ctx pluginContext, cfg externalRe if cfg.ServiceWorkflowID == "" { return "", nil, fmt.Errorf("external_review: service_workflow_id is required") } + // The workflow is part of the flow's ID, which the other side refuses with a "/". + if strings.Contains(cfg.ServiceWorkflowID, "/") { + return "", nil, configError(fmt.Errorf("external_review: service_workflow_id %q must not contain \"/\"", cfg.ServiceWorkflowID)) + } if ctx.Record.CaseID == "" { return "", nil, fmt.Errorf("external_review: the task has no case") } @@ -242,11 +246,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 +270,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..d64121d6 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,57 @@ 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"]) +} + +// A workflow with a "/" can't name a flow on the other side, which no retry changes. +func TestExternalReview_RefusesWorkflowWithSlash(t *testing.T) { + mgr, svc := newReviewService(t, http.StatusOK) + p := NewExternalReviewPlugin(mgr, tokenFor, noCaseAttributes{}) + ctx := reviewContext(map[string]any{"submission": map[string]any{}}) + + err := p.Execute(ctx, json.RawMessage(`{"service_id": "agency", "path": "/api/v1/internal/cases/{caseId}/flows", "service_workflow_id": "a/b"}`)) + + require.Error(t, err) + assert.NotErrorIs(t, err, ErrSuspended) + assert.True(t, plugins.IsPermanent(err)) + assert.Empty(t, svc.calls()) +} + // The case ID is escaped into its path segment. func TestExternalReview_EscapesTheCaseID(t *testing.T) { mgr, svc := newReviewService(t, http.StatusOK) From c91216b6dbb2d64aedfdf733ccd302a7f5c5df31 Mon Sep 17 00:00:00 2001 From: Sanduni Pavithra Date: Thu, 3 Dec 2026 15:05:54 +0530 Subject: [PATCH 2/6] fix(platform/caseflow): resume a flow started under the bare task ID A dispatch retried after the flowId gained the workflow resumes the flow it started. --- platform/caseflow/client_handler.go | 21 ++++- platform/caseflow/client_handler_test.go | 100 +++++++++++++++++++++++ platform/caseflow/service.go | 34 ++++++++ platform/caseflow/service_test.go | 8 +- 4 files changed, 159 insertions(+), 4 deletions(-) diff --git a/platform/caseflow/client_handler.go b/platform/caseflow/client_handler.go index db8951fb..c9474357 100644 --- a/platform/caseflow/client_handler.go +++ b/platform/caseflow/client_handler.go @@ -180,7 +180,7 @@ func (h *ClientHandler) HandleStart(w http.ResponseWriter, r *http.Request) { // It is recorded with the flow, so a flow that is recorded is owned, and a retry of a // failed start finds both. f, err := h.flows.StartFlow(ctx, StartRequest{ - CaseID: caseID, TemplateID: flow.ID, WorkflowID: req.FlowID, Vars: vars, Owner: h.list.Owner, + CaseID: caseID, TemplateID: flow.ID, WorkflowID: req.FlowID, PriorWorkflowID: priorFlowID(req), Vars: vars, Owner: h.list.Owner, Case: caseInfo(req.Attributes, h.list.CaseColumns), }) if err != nil { @@ -197,6 +197,25 @@ func (h *ClientHandler) HandleStart(w http.ResponseWriter, r *http.Request) { httputil.JSON(w, http.StatusOK, f) } +// priorFlowSep joins the caller's task ID and the workflow in the flow ID a dispatching +// side sends (dispatchFlowID in platform/tasktypes). +const priorFlowSep = "-" + +// priorFlowID is the ID the dispatching side named req's flow by before it added the +// workflow to the flow ID ("-", from ""), or "" when req's +// flow ID is not in that form. A dispatch started before the change and retried after it +// then resumes the flow it started, rather than starting a second. +// +// TODO(OpenNSW/nsw-srilanka#1316): remove once no task dispatched before the change is +// still open. +func priorFlowID(req ClientStartRequest) string { + prior, ok := strings.CutSuffix(req.FlowID, priorFlowSep+req.WorkflowID) + if !ok || prior == "" { + return "" + } + return prior +} + // allowed returns the listed flow workflowID names if the calling client may start it. It // logs why when not. func (h *ClientHandler) allowed(ctx context.Context, workflowID, caseID string) (ClientFlow, bool) { diff --git a/platform/caseflow/client_handler_test.go b/platform/caseflow/client_handler_test.go index 85351717..3f1f3bb4 100644 --- a/platform/caseflow/client_handler_test.go +++ b/platform/caseflow/client_handler_test.go @@ -376,6 +376,106 @@ func TestClientHandlerConflict(t *testing.T) { } } +// One caller task starts two workflows on the same case, each under a flow ID of its own +// (the task's ID and the workflow), as a dispatching side does for a payment check and +// then the officer's review. Both start, and a repeat of either starts nothing more. +func TestClientHandlerTwoWorkflowsFromOneTask(t *testing.T) { + list := clientFlows + list.Workflows = []ClientFlow{{ID: "notice", Clients: []string{"intake"}}, {ID: "receipt", Clients: []string{"intake"}}} + h, repo, wm := newClientHandler(t, list) + bodies := []string{ + `{"workflowId":"notice","flowId":"task-1-notice","callbackToken":"tok-1"}`, + `{"workflowId":"receipt","flowId":"task-1-receipt","callbackToken":"tok-2"}`, + } + for _, body := range append(bodies, bodies...) { + if rec := post(h, intake, body); rec.Code != http.StatusOK { + t.Fatalf("status = %d for %s, body %s", rec.Code, body, rec.Body) + } + } + for id, template := range map[string]string{"task-1-notice": "notice", "task-1-receipt": "receipt"} { + if f, ok := repo.flows[id]; !ok || f.TemplateID != template { + t.Errorf("flow %s = %+v, want one recorded for %s", id, f, template) + } + } + if len(wm.starts) != 2 || wm.starts[0].id != "task-1-notice" || wm.starts[1].id != "task-1-receipt" { + t.Errorf("starts = %v, want task-1-notice then task-1-receipt", wm.starts) + } +} + +// A dispatch started before the dispatching side added the workflow to the flow ID (flow +// "task-1") and retried after it ("task-1-notice") resumes the flow it started. A prior +// flow for another workflow or case is not that flow, so a new one starts under the new ID. +func TestClientHandlerPriorFlowID(t *testing.T) { + list := clientFlows + list.Workflows = []ClientFlow{{ID: "notice", Clients: []string{"intake"}}, {ID: "receipt", Clients: []string{"intake"}}} + prior := func(caseID, template string, status Status) Flow { + return Flow{WorkflowID: "task-1", CaseID: caseID, TemplateID: template, Status: status, ManifestVersion: 2} + } + for name, tc := range map[string]struct { + prior Flow + body string + wantFlow string // the flow the response names + wantStarts []string // the workflows started + }{ + "retry of a started prior flow": { + prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice"}`, + wantFlow: "task-1", + }, + "retry of a prior flow that failed to start": { + prior: prior("C1", "notice", StatusStarting), body: `{"workflowId":"notice","flowId":"task-1-notice"}`, + wantFlow: "task-1", wantStarts: []string{"task-1"}, + }, + "another workflow from the same task": { + prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"receipt","flowId":"task-1-receipt"}`, + wantFlow: "task-1-receipt", wantStarts: []string{"task-1-receipt"}, + }, + "prior flow on another case": { + prior: prior("C2", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice"}`, + wantFlow: "task-1-notice", wantStarts: []string{"task-1-notice"}, + }, + } { + t.Run(name, func(t *testing.T) { + repo, wm := newMemRepo(tc.prior), &startCapture{} + h := mustClientHandler(t, newService(t, repo, wm), list, testMaxRequestBytes) + for range 2 { // a repeat changes nothing + rec := post(h, intake, tc.body) + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", rec.Code, rec.Body) + } + var f Flow + if err := json.Unmarshal(rec.Body.Bytes(), &f); err != nil || f.WorkflowID != tc.wantFlow { + t.Fatalf("flow = %+v, err = %v, want %s", f, err, tc.wantFlow) + } + } + var started []string + for _, s := range wm.starts { + started = append(started, s.id) + } + if !reflect.DeepEqual(started, tc.wantStarts) { + t.Errorf("starts = %v, want %v", started, tc.wantStarts) + } + if tc.wantFlow == "task-1" && len(repo.flows) != 1 { + t.Errorf("flows = %v, want only the prior one", repo.flows) + } + }) + } +} + +func TestPriorFlowID(t *testing.T) { + for _, tc := range []struct{ flowID, workflowID, want string }{ + {"task-1-notice", "notice", "task-1"}, + {"0b6f3f6e-3c1a-4c55-9a1e-6d1f2f3a4b5c-lot-review", "lot-review", "0b6f3f6e-3c1a-4c55-9a1e-6d1f2f3a4b5c"}, + {"task-1", "notice", ""}, // the old form, or another caller's ID + {"task-1notice", "notice", ""}, // no separator + {"-notice", "notice", ""}, // no task ID before it + {"task-1-notice", "receipt", ""}, // another workflow's suffix + } { + if got := priorFlowID(ClientStartRequest{FlowID: tc.flowID, WorkflowID: tc.workflowID}); got != tc.want { + t.Errorf("priorFlowID(%q, %q) = %q, want %q", tc.flowID, tc.workflowID, got, tc.want) + } + } +} + // A failed start leaves the flow recorded with its owner, so the caller's retry starts it. // A failed record starts nothing. func TestClientHandlerFailures(t *testing.T) { diff --git a/platform/caseflow/service.go b/platform/caseflow/service.go index ce4210bf..c1f476d3 100644 --- a/platform/caseflow/service.go +++ b/platform/caseflow/service.go @@ -26,6 +26,7 @@ import ( "encoding/json" "errors" "fmt" + "log/slog" "strings" "github.com/OpenNSW/core/artifact" @@ -58,6 +59,12 @@ type StartRequest struct { // that can be asked twice for the same flow (a redelivered event) passes a stable ID // instead, so the repeat is recognised. WorkflowID string + // PriorWorkflowID, if set, is an ID an earlier version of the caller named the same + // flow by. When no flow is recorded under WorkflowID, a flow recorded under this one + // for the same case and template is the flow asked for, and is resumed instead of + // starting another. One recorded for another case or template is not, and a new flow + // starts under WorkflowID. + PriorWorkflowID string // Vars seeds the workflow variables. They are recorded with the flow, so a retried // start runs with the first request's variables. Vars map[string]any @@ -140,6 +147,15 @@ func (s *Service) StartFlow(ctx context.Context, req StartRequest) (*Flow, error case !errors.Is(err, ErrFlowNotFound): return nil, fmt.Errorf("caseflow: failed to read flow: %w", err) } + f, err = s.priorFlow(ctx, req) + if err != nil { + return nil, err + } + if f != nil { + slog.InfoContext(ctx, "caseflow: resuming the flow recorded under its prior ID", + "workflowId", newFlow.WorkflowID, "priorWorkflowId", f.WorkflowID, "caseId", req.CaseID) + return s.resume(ctx, req, f, nil) + } } // A new flow runs on the current version. Its template is checked before anything is @@ -167,6 +183,24 @@ func (s *Service) StartFlow(ctx context.Context, req StartRequest) (*Flow, error return s.resume(ctx, req, f, &def) } +// priorFlow returns the flow recorded under req.PriorWorkflowID if it is for req's case and +// template, and nil if there is none or it is for another. +func (s *Service) priorFlow(ctx context.Context, req StartRequest) (*Flow, error) { + if req.PriorWorkflowID == "" || req.PriorWorkflowID == req.WorkflowID { + return nil, nil + } + f, err := s.repo.Get(ctx, req.PriorWorkflowID) + switch { + case errors.Is(err, ErrFlowNotFound): + return nil, nil + case err != nil: + return nil, fmt.Errorf("caseflow: failed to read flow: %w", err) + case f.CaseID != req.CaseID || f.TemplateID != req.TemplateID: + return nil, nil + } + return f, nil +} + // resume starts the recorded flow f unless it has already started. def is f's definition // if the caller has loaded it; nil loads it from f's manifest version. func (s *Service) resume(ctx context.Context, req StartRequest, f *Flow, def *workflow.WorkflowDefinition) (*Flow, error) { diff --git a/platform/caseflow/service_test.go b/platform/caseflow/service_test.go index 57a2a2d9..49058f41 100644 --- a/platform/caseflow/service_test.go +++ b/platform/caseflow/service_test.go @@ -19,9 +19,9 @@ import ( // newArtifacts has two manifests; v2, the current one, points export at a new file, drops // legacy, and has notice's task read by reviewer before officer, where v1 has it read by -// officer alone. Notice has one task. In v2, pair has two, unread one whose template has -// no read.roles, stranger one read by a role the catalog lacks, and orphan one whose -// template the manifest lacks. +// officer alone. Notice has one task. In v2, receipt has one too, pair has two, unread +// one whose template has no read.roles, stranger one read by a role the catalog lacks, +// and orphan one whose template the manifest lacks. func newArtifacts() *artifact.Set { set, err := artifact.LoadSet(context.Background(), testutil.MemLoader{ "export.json": []byte(`{"id":"export","name":"Export v1","nodes":[],"edges":[]}`), @@ -29,6 +29,7 @@ func newArtifacts() *artifact.Set { "notice.json": []byte(`{"id":"notice","nodes":[` + taskNode("review", "notice-review") + `],"edges":[]}`), "notice-review.json": []byte(`{"id":"notice-review","read":{"roles":["officer"]}}`), "notice-review2.json": []byte(`{"id":"notice-review","read":{"roles":["reviewer","officer"]}}`), + "receipt.json": []byte(`{"id":"receipt","nodes":[` + taskNode("review", "notice-review") + `],"edges":[]}`), "legacy.json": []byte(`{"id":"legacy","nodes":[],"edges":[]}`), "pair.json": []byte(`{"id":"pair","nodes":[` + taskNode("a", "notice-review") + `,` + taskNode("b", "notice-review") + `],"edges":[]}`), "unread.json": []byte(`{"id":"unread","nodes":[` + taskNode("look", "unread-look") + `],"edges":[]}`), @@ -45,6 +46,7 @@ func newArtifacts() *artifact.Set { {"id":"export","kind":"workflow","path":"export_v2.json"}, {"id":"notice","kind":"workflow","path":"notice.json"}, {"id":"notice-review","kind":"task_template","path":"notice-review2.json"}, + {"id":"receipt","kind":"workflow","path":"receipt.json"}, {"id":"pair","kind":"workflow","path":"pair.json"}, {"id":"unread","kind":"workflow","path":"unread.json"}, {"id":"unread-look","kind":"task_template","path":"unread-look.json"}, From 20c5a398081a1869079dc5ff124a19f422123d53 Mon Sep 17 00:00:00 2001 From: Sanduni Pavithra Date: Thu, 3 Dec 2026 17:17:14 +0530 Subject: [PATCH 3/6] add deprecate comment --- platform/caseflow/client_handler.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/platform/caseflow/client_handler.go b/platform/caseflow/client_handler.go index c9474357..fc1cfc9f 100644 --- a/platform/caseflow/client_handler.go +++ b/platform/caseflow/client_handler.go @@ -207,7 +207,11 @@ const priorFlowSep = "-" // then resumes the flow it started, rather than starting a second. // // TODO(OpenNSW/nsw-srilanka#1316): remove once no task dispatched before the change is -// still open. +// still open, together with priorFlowSep and what it feeds in service.go: +// StartRequest.PriorWorkflowID, which only this sets, and priorFlow, the fallback in +// StartFlow that resumes a flow recorded under the prior ID for the same case and +// template instead of starting another under the new one. These can be deprecated once no +// task dispatched before the change is still open. func priorFlowID(req ClientStartRequest) string { prior, ok := strings.CutSuffix(req.FlowID, priorFlowSep+req.WorkflowID) if !ok || prior == "" { From b80f71139c3111d39f3a888607c5e59108a1ca6c Mon Sep 17 00:00:00 2001 From: Sanduni Pavithra Date: Fri, 4 Dec 2026 11:16:10 +0530 Subject: [PATCH 4/6] fix(platform): send the prior flow ID instead of deriving it An EXTERNAL_REVIEW dispatch now sends the bare task ID as priorFlowId, and caseflow resumes the flow recorded under it, instead of cutting "-" off the flowId. VarNames.PriorFlowID, optional, saves it with the flow's variables. TODOs no longer name the host repo. --- platform/caseflow/client_handler.go | 69 ++++++++-------- platform/caseflow/client_handler_test.go | 95 ++++++++++++++++------ platform/caseflow/service.go | 4 +- platform/tasktypes/external_review.go | 7 +- platform/tasktypes/external_review_test.go | 7 +- 5 files changed, 122 insertions(+), 60 deletions(-) diff --git a/platform/caseflow/client_handler.go b/platform/caseflow/client_handler.go index fc1cfc9f..cbf1c4f6 100644 --- a/platform/caseflow/client_handler.go +++ b/platform/caseflow/client_handler.go @@ -43,6 +43,16 @@ type ClientStartRequest struct { // them in cases.attributes, and fills the columns ClientFlows.CaseColumns maps from // them. Attributes map[string]any `json:"attributes,omitempty"` + // PriorFlowID, if set, is the ID the caller named this flow by before. When no flow is + // recorded under FlowID, a flow recorded under PriorFlowID for the same case and + // workflow is the one asked for, and is resumed instead of starting another (see + // StartRequest.PriorWorkflowID). + // + // TODO: the EXTERNAL_REVIEW dispatch sends its task ID here, so a dispatch started + // under the bare task ID and retried under "-" resumes its flow. + // Deprecate it, with VarNames.PriorFlowID, StartRequest.PriorWorkflowID and + // Service.priorFlow, once no task dispatched before that change is still open. + PriorFlowID string `json:"priorFlowId,omitempty"` } // ClientHandler serves the route another system starts a flow on: it creates the case if it @@ -79,6 +89,12 @@ type VarNames struct { // CallbackToken holds the token the caller expects the decision back on, when it sent // one. CallbackToken string + // PriorFlowID holds the request's priorFlowId, when it sent one: the ID the caller + // named the flow by before. It is saved with the flow's other variables. Optional: + // blank leaves it out. + // + // TODO: deprecate with ClientStartRequest.PriorFlowID. + PriorFlowID string } // DefaultVarNames names each variable after the start request field, or path parameter, @@ -89,21 +105,28 @@ var DefaultVarNames = VarNames{ CaseID: "caseId", Input: "input", CallbackToken: "callbackToken", + PriorFlowID: "priorFlowId", } // check refuses names that are blank, that two variables share, or that start with "_": // the engine reserves those names for the variables it sets itself, such as -// workflow.VarCaseID. +// workflow.VarCaseID. PriorFlowID may be blank. func (n VarNames) check() error { seen := map[string]string{} - for _, v := range []struct{ field, name string }{ - {"FlowID", n.FlowID}, - {"WorkflowID", n.WorkflowID}, - {"CaseID", n.CaseID}, - {"Input", n.Input}, - {"CallbackToken", n.CallbackToken}, + for _, v := range []struct { + field, name string + optional bool + }{ + {"FlowID", n.FlowID, false}, + {"WorkflowID", n.WorkflowID, false}, + {"CaseID", n.CaseID, false}, + {"Input", n.Input, false}, + {"CallbackToken", n.CallbackToken, false}, + {"PriorFlowID", n.PriorFlowID, true}, } { switch { + case v.optional && v.name == "": + continue case strings.TrimSpace(v.name) == "": return fmt.Errorf("caseflow: the %s variable name is required", v.field) case strings.HasPrefix(v.name, "_"): @@ -120,8 +143,8 @@ func (n VarNames) check() error { type ClientHandlerOption func(*ClientHandler) // WithVarNames names the variables a client flow starts with: the names the deployment's -// workflows read them by. Every name must be set, differ from the others, and not start -// with "_". +// workflows read them by. Every name but PriorFlowID must be set; each one set must differ +// from the others, and not start with "_". func WithVarNames(names VarNames) ClientHandlerOption { return func(h *ClientHandler) { h.vars = names } } @@ -180,7 +203,7 @@ func (h *ClientHandler) HandleStart(w http.ResponseWriter, r *http.Request) { // It is recorded with the flow, so a flow that is recorded is owned, and a retry of a // failed start finds both. f, err := h.flows.StartFlow(ctx, StartRequest{ - CaseID: caseID, TemplateID: flow.ID, WorkflowID: req.FlowID, PriorWorkflowID: priorFlowID(req), Vars: vars, Owner: h.list.Owner, + CaseID: caseID, TemplateID: flow.ID, WorkflowID: req.FlowID, PriorWorkflowID: req.PriorFlowID, Vars: vars, Owner: h.list.Owner, Case: caseInfo(req.Attributes, h.list.CaseColumns), }) if err != nil { @@ -197,29 +220,6 @@ func (h *ClientHandler) HandleStart(w http.ResponseWriter, r *http.Request) { httputil.JSON(w, http.StatusOK, f) } -// priorFlowSep joins the caller's task ID and the workflow in the flow ID a dispatching -// side sends (dispatchFlowID in platform/tasktypes). -const priorFlowSep = "-" - -// priorFlowID is the ID the dispatching side named req's flow by before it added the -// workflow to the flow ID ("-", from ""), or "" when req's -// flow ID is not in that form. A dispatch started before the change and retried after it -// then resumes the flow it started, rather than starting a second. -// -// TODO(OpenNSW/nsw-srilanka#1316): remove once no task dispatched before the change is -// still open, together with priorFlowSep and what it feeds in service.go: -// StartRequest.PriorWorkflowID, which only this sets, and priorFlow, the fallback in -// StartFlow that resumes a flow recorded under the prior ID for the same case and -// template instead of starting another under the new one. These can be deprecated once no -// task dispatched before the change is still open. -func priorFlowID(req ClientStartRequest) string { - prior, ok := strings.CutSuffix(req.FlowID, priorFlowSep+req.WorkflowID) - if !ok || prior == "" { - return "" - } - return prior -} - // allowed returns the listed flow workflowID names if the calling client may start it. It // logs why when not. func (h *ClientHandler) allowed(ctx context.Context, workflowID, caseID string) (ClientFlow, bool) { @@ -290,5 +290,8 @@ func clientVars(names VarNames, caseID string, req ClientStartRequest) (map[stri if req.CallbackToken != "" { vars[names.CallbackToken] = req.CallbackToken } + if names.PriorFlowID != "" && req.PriorFlowID != "" { + vars[names.PriorFlowID] = req.PriorFlowID + } return vars, nil } diff --git a/platform/caseflow/client_handler_test.go b/platform/caseflow/client_handler_test.go index 3f1f3bb4..7bc55f4a 100644 --- a/platform/caseflow/client_handler_test.go +++ b/platform/caseflow/client_handler_test.go @@ -31,7 +31,7 @@ const testMaxRequestBytes = 1 << 20 var catalogClients = map[string]string{"intake": "INTAKE_CLIENT", "lab": "LAB_CLIENT"} // testVarNames are the names the tests' handlers give a client flow's variables. -var testVarNames = VarNames{FlowID: "flowRef", WorkflowID: "flowKind", CaseID: "caseRef", Input: "payload", CallbackToken: "replyToken"} +var testVarNames = VarNames{FlowID: "flowRef", WorkflowID: "flowKind", CaseID: "caseRef", Input: "payload", CallbackToken: "replyToken", PriorFlowID: "priorRef"} // mustClientHandler builds a handler for list over flows, with the catalog's clients and // testVarNames. @@ -168,6 +168,56 @@ func TestClientHandlerWithoutCallbackToken(t *testing.T) { } } +// The request's priorFlowId is saved with the flow's variables, unless it sent none or +// the handler names no variable for it. +func TestClientHandlerSavesPriorFlowID(t *testing.T) { + body := `{"workflowId":"notice","flowId":"task-1-notice","priorFlowId":"task-1"}` + payload := func(repo *memRepo) map[string]any { + t.Helper() + var vars map[string]any + if err := json.Unmarshal(repo.flows["task-1-notice"].Payload, &vars); err != nil { + t.Fatal(err) + } + return vars + } + t.Run("saved", func(t *testing.T) { + h, repo, wm := newClientHandler(t, clientFlows) + if rec := post(h, intake, body); rec.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", rec.Code, rec.Body) + } + if got := wm.starts[0].vars[testVarNames.PriorFlowID]; got != "task-1" { + t.Errorf("started with %s = %v, want task-1", testVarNames.PriorFlowID, got) + } + if got := payload(repo)[testVarNames.PriorFlowID]; got != "task-1" { + t.Errorf("saved %s = %v, want task-1", testVarNames.PriorFlowID, got) + } + }) + t.Run("not sent", func(t *testing.T) { + h, repo, _ := newClientHandler(t, clientFlows) + if rec := post(h, intake, `{"workflowId":"notice","flowId":"task-1-notice"}`); rec.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", rec.Code, rec.Body) + } + if got, ok := payload(repo)[testVarNames.PriorFlowID]; ok { + t.Errorf("saved %s = %v without one in the request", testVarNames.PriorFlowID, got) + } + }) + t.Run("no variable named", func(t *testing.T) { + names := testVarNames + names.PriorFlowID = "" + repo, wm := newMemRepo(), &startCapture{} + h, err := NewClientHandler(newService(t, repo, wm), clientFlows, catalogClients, testMaxRequestBytes, WithVarNames(names)) + if err != nil { + t.Fatal(err) + } + if rec := post(h, intake, body); rec.Code != http.StatusOK { + t.Fatalf("status = %d, body %s", rec.Code, rec.Body) + } + if _, ok := payload(repo)[""]; ok || len(payload(repo)) != 4 { + t.Errorf("saved %v, want only the four required variables", payload(repo)) + } + }) +} + // A repeat of the same request is answered from the recorded flow, which has its owner. func TestClientHandlerRepeat(t *testing.T) { h, repo, wm := newClientHandler(t, clientFlows) @@ -278,7 +328,7 @@ func TestClientHandlerDefaultVarNames(t *testing.T) { // Each variable is named after the start request field, or path parameter, it holds. func TestDefaultVarNames(t *testing.T) { - want := VarNames{FlowID: "flowId", WorkflowID: "workflowId", CaseID: "caseId", Input: "input", CallbackToken: "callbackToken"} + want := VarNames{FlowID: "flowId", WorkflowID: "workflowId", CaseID: "caseId", Input: "input", CallbackToken: "callbackToken", PriorFlowID: "priorFlowId"} if DefaultVarNames != want { t.Errorf("DefaultVarNames = %+v, want %+v", DefaultVarNames, want) } @@ -301,6 +351,9 @@ func TestNewClientHandlerChecksVarNames(t *testing.T) { "case ID as flow ID": with(func(n *VarNames) { n.CaseID = n.FlowID }), "input as callback": with(func(n *VarNames) { n.Input = n.CallbackToken }), "underscore": with(func(n *VarNames) { n.Input = "_anything" }), + "spaces prior flow ID": with(func(n *VarNames) { n.PriorFlowID = " " }), + "prior as flow ID": with(func(n *VarNames) { n.PriorFlowID = n.FlowID }), + "underscore prior": with(func(n *VarNames) { n.PriorFlowID = "_prior" }), } for _, name := range []string{ workflow.VarCaseID, workflow.VarManifestVersion, workflow.VarSplitNodeID, @@ -314,7 +367,7 @@ func TestNewClientHandlerChecksVarNames(t *testing.T) { t.Errorf("%s: NewClientHandler(%+v) = %v, %v; want an error", label, names, h, err) } } - for _, names := range []VarNames{DefaultVarNames, testVarNames, with(func(n *VarNames) { n.CaseID = "case_id" })} { + for _, names := range []VarNames{DefaultVarNames, testVarNames, with(func(n *VarNames) { n.CaseID = "case_id" }), with(func(n *VarNames) { n.PriorFlowID = "" })} { repo, wm := newMemRepo(), &startCapture{} if _, err := NewClientHandler(newService(t, repo, wm), clientFlows, catalogClients, testMaxRequestBytes, WithVarNames(names)); err != nil { t.Errorf("NewClientHandler(%+v): %v", names, err) @@ -403,8 +456,9 @@ func TestClientHandlerTwoWorkflowsFromOneTask(t *testing.T) { } // A dispatch started before the dispatching side added the workflow to the flow ID (flow -// "task-1") and retried after it ("task-1-notice") resumes the flow it started. A prior -// flow for another workflow or case is not that flow, so a new one starts under the new ID. +// "task-1") and retried after it ("task-1-notice", priorFlowId "task-1") resumes the flow +// it started. A prior flow for another workflow or case is not that flow, and neither is +// one the request does not name, so a new one starts under the new ID. func TestClientHandlerPriorFlowID(t *testing.T) { list := clientFlows list.Workflows = []ClientFlow{{ID: "notice", Clients: []string{"intake"}}, {ID: "receipt", Clients: []string{"intake"}}} @@ -418,21 +472,29 @@ func TestClientHandlerPriorFlowID(t *testing.T) { wantStarts []string // the workflows started }{ "retry of a started prior flow": { - prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice"}`, + prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice","priorFlowId":"task-1"}`, wantFlow: "task-1", }, "retry of a prior flow that failed to start": { - prior: prior("C1", "notice", StatusStarting), body: `{"workflowId":"notice","flowId":"task-1-notice"}`, + prior: prior("C1", "notice", StatusStarting), body: `{"workflowId":"notice","flowId":"task-1-notice","priorFlowId":"task-1"}`, wantFlow: "task-1", wantStarts: []string{"task-1"}, }, "another workflow from the same task": { - prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"receipt","flowId":"task-1-receipt"}`, + prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"receipt","flowId":"task-1-receipt","priorFlowId":"task-1"}`, wantFlow: "task-1-receipt", wantStarts: []string{"task-1-receipt"}, }, "prior flow on another case": { - prior: prior("C2", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice"}`, + prior: prior("C2", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice","priorFlowId":"task-1"}`, wantFlow: "task-1-notice", wantStarts: []string{"task-1-notice"}, }, + "no priorFlowId": { + prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice"}`, + wantFlow: "task-1-notice", wantStarts: []string{"task-1-notice"}, + }, + "priorFlowId names no flow": { + prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-2-notice","priorFlowId":"task-2"}`, + wantFlow: "task-2-notice", wantStarts: []string{"task-2-notice"}, + }, } { t.Run(name, func(t *testing.T) { repo, wm := newMemRepo(tc.prior), &startCapture{} @@ -461,21 +523,6 @@ func TestClientHandlerPriorFlowID(t *testing.T) { } } -func TestPriorFlowID(t *testing.T) { - for _, tc := range []struct{ flowID, workflowID, want string }{ - {"task-1-notice", "notice", "task-1"}, - {"0b6f3f6e-3c1a-4c55-9a1e-6d1f2f3a4b5c-lot-review", "lot-review", "0b6f3f6e-3c1a-4c55-9a1e-6d1f2f3a4b5c"}, - {"task-1", "notice", ""}, // the old form, or another caller's ID - {"task-1notice", "notice", ""}, // no separator - {"-notice", "notice", ""}, // no task ID before it - {"task-1-notice", "receipt", ""}, // another workflow's suffix - } { - if got := priorFlowID(ClientStartRequest{FlowID: tc.flowID, WorkflowID: tc.workflowID}); got != tc.want { - t.Errorf("priorFlowID(%q, %q) = %q, want %q", tc.flowID, tc.workflowID, got, tc.want) - } - } -} - // A failed start leaves the flow recorded with its owner, so the caller's retry starts it. // A failed record starts nothing. func TestClientHandlerFailures(t *testing.T) { diff --git a/platform/caseflow/service.go b/platform/caseflow/service.go index c1f476d3..e247ad53 100644 --- a/platform/caseflow/service.go +++ b/platform/caseflow/service.go @@ -63,7 +63,9 @@ type StartRequest struct { // flow by. When no flow is recorded under WorkflowID, a flow recorded under this one // for the same case and template is the flow asked for, and is resumed instead of // starting another. One recorded for another case or template is not, and a new flow - // starts under WorkflowID. + // starts under WorkflowID. ClientHandler sets it from the request's priorFlowId. + // + // TODO: deprecate with ClientStartRequest.PriorFlowID. PriorWorkflowID string // Vars seeds the workflow variables. They are recorded with the flow, so a retried // start runs with the first request's variables. diff --git a/platform/tasktypes/external_review.go b/platform/tasktypes/external_review.go index 17c8f85d..e646958a 100644 --- a/platform/tasktypes/external_review.go +++ b/platform/tasktypes/external_review.go @@ -261,7 +261,9 @@ func dispatchFlowID(taskID, workflowID string) string { // startFlowBody is the body of the other side's client flows route: the workflow to // 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 +// priorFlowId is the ID this task's flow had before the workflow was added to it, so a +// dispatch started under that ID and retried under the new one resumes its flow on the +// other side. 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 @@ -273,5 +275,8 @@ func startFlowBody(record *store.TaskRecord, workflowID string, data any, callba "flowId": dispatchFlowID(record.TaskID, workflowID), "input": data, "callbackToken": callbackToken, + // TODO: stop sending priorFlowId once no task dispatched + // under the bare task ID is still open. The other side ignores a body without it. + "priorFlowId": record.TaskID, } } diff --git a/platform/tasktypes/external_review_test.go b/platform/tasktypes/external_review_test.go index d64121d6..10c2ca8a 100644 --- a/platform/tasktypes/external_review_test.go +++ b/platform/tasktypes/external_review_test.go @@ -106,6 +106,7 @@ func TestExternalReview_DispatchesWithoutReplyToken(t *testing.T) { assert.Equal(t, map[string]any{ "workflowId": "review", "flowId": reviewTaskID + "-review", + "priorFlowId": reviewTaskID, "callbackToken": ownToken(t), "input": map[string]any{"field": "v1"}, }, call.body) @@ -142,7 +143,8 @@ func TestDispatchFlowID(t *testing.T) { } // 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. +// the officer's review, starts each as its own flow. Both name the task's bare ID as the +// prior flow ID, which the other side resumes only for the same case and workflow. func TestExternalReview_FlowIDPerWorkflow(t *testing.T) { mgr, svc := newReviewService(t, http.StatusOK) p := NewExternalReviewPlugin(mgr, tokenFor, noCaseAttributes{}) @@ -157,6 +159,9 @@ func TestExternalReview_FlowIDPerWorkflow(t *testing.T) { 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"]) + for _, call := range calls { + assert.Equal(t, reviewTaskID, call.body["priorFlowId"]) + } } // A retried dispatch names the same flow, so the other side takes it for a retry. From ec20a3e57eb89e445c40e0a69f5c68c77a13888c Mon Sep 17 00:00:00 2001 From: Sanduni Pavithra Date: Fri, 4 Dec 2026 11:36:48 +0530 Subject: [PATCH 5/6] refactor(platform/tasktypes): drop the "/" check on service_workflow_id caseflow refuses a flow ID with "/" itself, and its 400 already parks the step, so the plugin need not repeat the rule. --- platform/tasktypes/external_review.go | 4 ---- platform/tasktypes/external_review_test.go | 14 -------------- 2 files changed, 18 deletions(-) diff --git a/platform/tasktypes/external_review.go b/platform/tasktypes/external_review.go index e646958a..7d413124 100644 --- a/platform/tasktypes/external_review.go +++ b/platform/tasktypes/external_review.go @@ -144,10 +144,6 @@ func (p *ExternalReviewPlugin) dispatchRequest(ctx pluginContext, cfg externalRe if cfg.ServiceWorkflowID == "" { return "", nil, fmt.Errorf("external_review: service_workflow_id is required") } - // The workflow is part of the flow's ID, which the other side refuses with a "/". - if strings.Contains(cfg.ServiceWorkflowID, "/") { - return "", nil, configError(fmt.Errorf("external_review: service_workflow_id %q must not contain \"/\"", cfg.ServiceWorkflowID)) - } if ctx.Record.CaseID == "" { return "", nil, fmt.Errorf("external_review: the task has no case") } diff --git a/platform/tasktypes/external_review_test.go b/platform/tasktypes/external_review_test.go index 10c2ca8a..f63d0e23 100644 --- a/platform/tasktypes/external_review_test.go +++ b/platform/tasktypes/external_review_test.go @@ -179,20 +179,6 @@ func TestExternalReview_RetryKeepsFlowID(t *testing.T) { assert.Equal(t, calls[0].body["flowId"], calls[1].body["flowId"]) } -// A workflow with a "/" can't name a flow on the other side, which no retry changes. -func TestExternalReview_RefusesWorkflowWithSlash(t *testing.T) { - mgr, svc := newReviewService(t, http.StatusOK) - p := NewExternalReviewPlugin(mgr, tokenFor, noCaseAttributes{}) - ctx := reviewContext(map[string]any{"submission": map[string]any{}}) - - err := p.Execute(ctx, json.RawMessage(`{"service_id": "agency", "path": "/api/v1/internal/cases/{caseId}/flows", "service_workflow_id": "a/b"}`)) - - require.Error(t, err) - assert.NotErrorIs(t, err, ErrSuspended) - assert.True(t, plugins.IsPermanent(err)) - assert.Empty(t, svc.calls()) -} - // The case ID is escaped into its path segment. func TestExternalReview_EscapesTheCaseID(t *testing.T) { mgr, svc := newReviewService(t, http.StatusOK) From 0433ac1b60c8cd6c2b3a78df3a0eab20183e69b0 Mon Sep 17 00:00:00 2001 From: Sanduni Pavithra Date: Fri, 4 Dec 2026 14:59:55 +0530 Subject: [PATCH 6/6] fix(agency): drop the priorFlowId fallback TNSW no longer sends priorFlowId; the replay mock trims the workflow off flowId again. --- platform/caseflow/client_handler.go | 46 ++----- platform/caseflow/client_handler_test.go | 153 +-------------------- platform/caseflow/service.go | 36 ----- platform/caseflow/service_test.go | 8 +- platform/tasktypes/external_review.go | 7 +- platform/tasktypes/external_review_test.go | 7 +- 6 files changed, 18 insertions(+), 239 deletions(-) diff --git a/platform/caseflow/client_handler.go b/platform/caseflow/client_handler.go index cbf1c4f6..db8951fb 100644 --- a/platform/caseflow/client_handler.go +++ b/platform/caseflow/client_handler.go @@ -43,16 +43,6 @@ type ClientStartRequest struct { // them in cases.attributes, and fills the columns ClientFlows.CaseColumns maps from // them. Attributes map[string]any `json:"attributes,omitempty"` - // PriorFlowID, if set, is the ID the caller named this flow by before. When no flow is - // recorded under FlowID, a flow recorded under PriorFlowID for the same case and - // workflow is the one asked for, and is resumed instead of starting another (see - // StartRequest.PriorWorkflowID). - // - // TODO: the EXTERNAL_REVIEW dispatch sends its task ID here, so a dispatch started - // under the bare task ID and retried under "-" resumes its flow. - // Deprecate it, with VarNames.PriorFlowID, StartRequest.PriorWorkflowID and - // Service.priorFlow, once no task dispatched before that change is still open. - PriorFlowID string `json:"priorFlowId,omitempty"` } // ClientHandler serves the route another system starts a flow on: it creates the case if it @@ -89,12 +79,6 @@ type VarNames struct { // CallbackToken holds the token the caller expects the decision back on, when it sent // one. CallbackToken string - // PriorFlowID holds the request's priorFlowId, when it sent one: the ID the caller - // named the flow by before. It is saved with the flow's other variables. Optional: - // blank leaves it out. - // - // TODO: deprecate with ClientStartRequest.PriorFlowID. - PriorFlowID string } // DefaultVarNames names each variable after the start request field, or path parameter, @@ -105,28 +89,21 @@ var DefaultVarNames = VarNames{ CaseID: "caseId", Input: "input", CallbackToken: "callbackToken", - PriorFlowID: "priorFlowId", } // check refuses names that are blank, that two variables share, or that start with "_": // the engine reserves those names for the variables it sets itself, such as -// workflow.VarCaseID. PriorFlowID may be blank. +// workflow.VarCaseID. func (n VarNames) check() error { seen := map[string]string{} - for _, v := range []struct { - field, name string - optional bool - }{ - {"FlowID", n.FlowID, false}, - {"WorkflowID", n.WorkflowID, false}, - {"CaseID", n.CaseID, false}, - {"Input", n.Input, false}, - {"CallbackToken", n.CallbackToken, false}, - {"PriorFlowID", n.PriorFlowID, true}, + for _, v := range []struct{ field, name string }{ + {"FlowID", n.FlowID}, + {"WorkflowID", n.WorkflowID}, + {"CaseID", n.CaseID}, + {"Input", n.Input}, + {"CallbackToken", n.CallbackToken}, } { switch { - case v.optional && v.name == "": - continue case strings.TrimSpace(v.name) == "": return fmt.Errorf("caseflow: the %s variable name is required", v.field) case strings.HasPrefix(v.name, "_"): @@ -143,8 +120,8 @@ func (n VarNames) check() error { type ClientHandlerOption func(*ClientHandler) // WithVarNames names the variables a client flow starts with: the names the deployment's -// workflows read them by. Every name but PriorFlowID must be set; each one set must differ -// from the others, and not start with "_". +// workflows read them by. Every name must be set, differ from the others, and not start +// with "_". func WithVarNames(names VarNames) ClientHandlerOption { return func(h *ClientHandler) { h.vars = names } } @@ -203,7 +180,7 @@ func (h *ClientHandler) HandleStart(w http.ResponseWriter, r *http.Request) { // It is recorded with the flow, so a flow that is recorded is owned, and a retry of a // failed start finds both. f, err := h.flows.StartFlow(ctx, StartRequest{ - CaseID: caseID, TemplateID: flow.ID, WorkflowID: req.FlowID, PriorWorkflowID: req.PriorFlowID, Vars: vars, Owner: h.list.Owner, + CaseID: caseID, TemplateID: flow.ID, WorkflowID: req.FlowID, Vars: vars, Owner: h.list.Owner, Case: caseInfo(req.Attributes, h.list.CaseColumns), }) if err != nil { @@ -290,8 +267,5 @@ func clientVars(names VarNames, caseID string, req ClientStartRequest) (map[stri if req.CallbackToken != "" { vars[names.CallbackToken] = req.CallbackToken } - if names.PriorFlowID != "" && req.PriorFlowID != "" { - vars[names.PriorFlowID] = req.PriorFlowID - } return vars, nil } diff --git a/platform/caseflow/client_handler_test.go b/platform/caseflow/client_handler_test.go index 7bc55f4a..85351717 100644 --- a/platform/caseflow/client_handler_test.go +++ b/platform/caseflow/client_handler_test.go @@ -31,7 +31,7 @@ const testMaxRequestBytes = 1 << 20 var catalogClients = map[string]string{"intake": "INTAKE_CLIENT", "lab": "LAB_CLIENT"} // testVarNames are the names the tests' handlers give a client flow's variables. -var testVarNames = VarNames{FlowID: "flowRef", WorkflowID: "flowKind", CaseID: "caseRef", Input: "payload", CallbackToken: "replyToken", PriorFlowID: "priorRef"} +var testVarNames = VarNames{FlowID: "flowRef", WorkflowID: "flowKind", CaseID: "caseRef", Input: "payload", CallbackToken: "replyToken"} // mustClientHandler builds a handler for list over flows, with the catalog's clients and // testVarNames. @@ -168,56 +168,6 @@ func TestClientHandlerWithoutCallbackToken(t *testing.T) { } } -// The request's priorFlowId is saved with the flow's variables, unless it sent none or -// the handler names no variable for it. -func TestClientHandlerSavesPriorFlowID(t *testing.T) { - body := `{"workflowId":"notice","flowId":"task-1-notice","priorFlowId":"task-1"}` - payload := func(repo *memRepo) map[string]any { - t.Helper() - var vars map[string]any - if err := json.Unmarshal(repo.flows["task-1-notice"].Payload, &vars); err != nil { - t.Fatal(err) - } - return vars - } - t.Run("saved", func(t *testing.T) { - h, repo, wm := newClientHandler(t, clientFlows) - if rec := post(h, intake, body); rec.Code != http.StatusOK { - t.Fatalf("status = %d, body %s", rec.Code, rec.Body) - } - if got := wm.starts[0].vars[testVarNames.PriorFlowID]; got != "task-1" { - t.Errorf("started with %s = %v, want task-1", testVarNames.PriorFlowID, got) - } - if got := payload(repo)[testVarNames.PriorFlowID]; got != "task-1" { - t.Errorf("saved %s = %v, want task-1", testVarNames.PriorFlowID, got) - } - }) - t.Run("not sent", func(t *testing.T) { - h, repo, _ := newClientHandler(t, clientFlows) - if rec := post(h, intake, `{"workflowId":"notice","flowId":"task-1-notice"}`); rec.Code != http.StatusOK { - t.Fatalf("status = %d, body %s", rec.Code, rec.Body) - } - if got, ok := payload(repo)[testVarNames.PriorFlowID]; ok { - t.Errorf("saved %s = %v without one in the request", testVarNames.PriorFlowID, got) - } - }) - t.Run("no variable named", func(t *testing.T) { - names := testVarNames - names.PriorFlowID = "" - repo, wm := newMemRepo(), &startCapture{} - h, err := NewClientHandler(newService(t, repo, wm), clientFlows, catalogClients, testMaxRequestBytes, WithVarNames(names)) - if err != nil { - t.Fatal(err) - } - if rec := post(h, intake, body); rec.Code != http.StatusOK { - t.Fatalf("status = %d, body %s", rec.Code, rec.Body) - } - if _, ok := payload(repo)[""]; ok || len(payload(repo)) != 4 { - t.Errorf("saved %v, want only the four required variables", payload(repo)) - } - }) -} - // A repeat of the same request is answered from the recorded flow, which has its owner. func TestClientHandlerRepeat(t *testing.T) { h, repo, wm := newClientHandler(t, clientFlows) @@ -328,7 +278,7 @@ func TestClientHandlerDefaultVarNames(t *testing.T) { // Each variable is named after the start request field, or path parameter, it holds. func TestDefaultVarNames(t *testing.T) { - want := VarNames{FlowID: "flowId", WorkflowID: "workflowId", CaseID: "caseId", Input: "input", CallbackToken: "callbackToken", PriorFlowID: "priorFlowId"} + want := VarNames{FlowID: "flowId", WorkflowID: "workflowId", CaseID: "caseId", Input: "input", CallbackToken: "callbackToken"} if DefaultVarNames != want { t.Errorf("DefaultVarNames = %+v, want %+v", DefaultVarNames, want) } @@ -351,9 +301,6 @@ func TestNewClientHandlerChecksVarNames(t *testing.T) { "case ID as flow ID": with(func(n *VarNames) { n.CaseID = n.FlowID }), "input as callback": with(func(n *VarNames) { n.Input = n.CallbackToken }), "underscore": with(func(n *VarNames) { n.Input = "_anything" }), - "spaces prior flow ID": with(func(n *VarNames) { n.PriorFlowID = " " }), - "prior as flow ID": with(func(n *VarNames) { n.PriorFlowID = n.FlowID }), - "underscore prior": with(func(n *VarNames) { n.PriorFlowID = "_prior" }), } for _, name := range []string{ workflow.VarCaseID, workflow.VarManifestVersion, workflow.VarSplitNodeID, @@ -367,7 +314,7 @@ func TestNewClientHandlerChecksVarNames(t *testing.T) { t.Errorf("%s: NewClientHandler(%+v) = %v, %v; want an error", label, names, h, err) } } - for _, names := range []VarNames{DefaultVarNames, testVarNames, with(func(n *VarNames) { n.CaseID = "case_id" }), with(func(n *VarNames) { n.PriorFlowID = "" })} { + for _, names := range []VarNames{DefaultVarNames, testVarNames, with(func(n *VarNames) { n.CaseID = "case_id" })} { repo, wm := newMemRepo(), &startCapture{} if _, err := NewClientHandler(newService(t, repo, wm), clientFlows, catalogClients, testMaxRequestBytes, WithVarNames(names)); err != nil { t.Errorf("NewClientHandler(%+v): %v", names, err) @@ -429,100 +376,6 @@ func TestClientHandlerConflict(t *testing.T) { } } -// One caller task starts two workflows on the same case, each under a flow ID of its own -// (the task's ID and the workflow), as a dispatching side does for a payment check and -// then the officer's review. Both start, and a repeat of either starts nothing more. -func TestClientHandlerTwoWorkflowsFromOneTask(t *testing.T) { - list := clientFlows - list.Workflows = []ClientFlow{{ID: "notice", Clients: []string{"intake"}}, {ID: "receipt", Clients: []string{"intake"}}} - h, repo, wm := newClientHandler(t, list) - bodies := []string{ - `{"workflowId":"notice","flowId":"task-1-notice","callbackToken":"tok-1"}`, - `{"workflowId":"receipt","flowId":"task-1-receipt","callbackToken":"tok-2"}`, - } - for _, body := range append(bodies, bodies...) { - if rec := post(h, intake, body); rec.Code != http.StatusOK { - t.Fatalf("status = %d for %s, body %s", rec.Code, body, rec.Body) - } - } - for id, template := range map[string]string{"task-1-notice": "notice", "task-1-receipt": "receipt"} { - if f, ok := repo.flows[id]; !ok || f.TemplateID != template { - t.Errorf("flow %s = %+v, want one recorded for %s", id, f, template) - } - } - if len(wm.starts) != 2 || wm.starts[0].id != "task-1-notice" || wm.starts[1].id != "task-1-receipt" { - t.Errorf("starts = %v, want task-1-notice then task-1-receipt", wm.starts) - } -} - -// A dispatch started before the dispatching side added the workflow to the flow ID (flow -// "task-1") and retried after it ("task-1-notice", priorFlowId "task-1") resumes the flow -// it started. A prior flow for another workflow or case is not that flow, and neither is -// one the request does not name, so a new one starts under the new ID. -func TestClientHandlerPriorFlowID(t *testing.T) { - list := clientFlows - list.Workflows = []ClientFlow{{ID: "notice", Clients: []string{"intake"}}, {ID: "receipt", Clients: []string{"intake"}}} - prior := func(caseID, template string, status Status) Flow { - return Flow{WorkflowID: "task-1", CaseID: caseID, TemplateID: template, Status: status, ManifestVersion: 2} - } - for name, tc := range map[string]struct { - prior Flow - body string - wantFlow string // the flow the response names - wantStarts []string // the workflows started - }{ - "retry of a started prior flow": { - prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice","priorFlowId":"task-1"}`, - wantFlow: "task-1", - }, - "retry of a prior flow that failed to start": { - prior: prior("C1", "notice", StatusStarting), body: `{"workflowId":"notice","flowId":"task-1-notice","priorFlowId":"task-1"}`, - wantFlow: "task-1", wantStarts: []string{"task-1"}, - }, - "another workflow from the same task": { - prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"receipt","flowId":"task-1-receipt","priorFlowId":"task-1"}`, - wantFlow: "task-1-receipt", wantStarts: []string{"task-1-receipt"}, - }, - "prior flow on another case": { - prior: prior("C2", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice","priorFlowId":"task-1"}`, - wantFlow: "task-1-notice", wantStarts: []string{"task-1-notice"}, - }, - "no priorFlowId": { - prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-1-notice"}`, - wantFlow: "task-1-notice", wantStarts: []string{"task-1-notice"}, - }, - "priorFlowId names no flow": { - prior: prior("C1", "notice", StatusStarted), body: `{"workflowId":"notice","flowId":"task-2-notice","priorFlowId":"task-2"}`, - wantFlow: "task-2-notice", wantStarts: []string{"task-2-notice"}, - }, - } { - t.Run(name, func(t *testing.T) { - repo, wm := newMemRepo(tc.prior), &startCapture{} - h := mustClientHandler(t, newService(t, repo, wm), list, testMaxRequestBytes) - for range 2 { // a repeat changes nothing - rec := post(h, intake, tc.body) - if rec.Code != http.StatusOK { - t.Fatalf("status = %d, body %s", rec.Code, rec.Body) - } - var f Flow - if err := json.Unmarshal(rec.Body.Bytes(), &f); err != nil || f.WorkflowID != tc.wantFlow { - t.Fatalf("flow = %+v, err = %v, want %s", f, err, tc.wantFlow) - } - } - var started []string - for _, s := range wm.starts { - started = append(started, s.id) - } - if !reflect.DeepEqual(started, tc.wantStarts) { - t.Errorf("starts = %v, want %v", started, tc.wantStarts) - } - if tc.wantFlow == "task-1" && len(repo.flows) != 1 { - t.Errorf("flows = %v, want only the prior one", repo.flows) - } - }) - } -} - // A failed start leaves the flow recorded with its owner, so the caller's retry starts it. // A failed record starts nothing. func TestClientHandlerFailures(t *testing.T) { diff --git a/platform/caseflow/service.go b/platform/caseflow/service.go index e247ad53..ce4210bf 100644 --- a/platform/caseflow/service.go +++ b/platform/caseflow/service.go @@ -26,7 +26,6 @@ import ( "encoding/json" "errors" "fmt" - "log/slog" "strings" "github.com/OpenNSW/core/artifact" @@ -59,14 +58,6 @@ type StartRequest struct { // that can be asked twice for the same flow (a redelivered event) passes a stable ID // instead, so the repeat is recognised. WorkflowID string - // PriorWorkflowID, if set, is an ID an earlier version of the caller named the same - // flow by. When no flow is recorded under WorkflowID, a flow recorded under this one - // for the same case and template is the flow asked for, and is resumed instead of - // starting another. One recorded for another case or template is not, and a new flow - // starts under WorkflowID. ClientHandler sets it from the request's priorFlowId. - // - // TODO: deprecate with ClientStartRequest.PriorFlowID. - PriorWorkflowID string // Vars seeds the workflow variables. They are recorded with the flow, so a retried // start runs with the first request's variables. Vars map[string]any @@ -149,15 +140,6 @@ func (s *Service) StartFlow(ctx context.Context, req StartRequest) (*Flow, error case !errors.Is(err, ErrFlowNotFound): return nil, fmt.Errorf("caseflow: failed to read flow: %w", err) } - f, err = s.priorFlow(ctx, req) - if err != nil { - return nil, err - } - if f != nil { - slog.InfoContext(ctx, "caseflow: resuming the flow recorded under its prior ID", - "workflowId", newFlow.WorkflowID, "priorWorkflowId", f.WorkflowID, "caseId", req.CaseID) - return s.resume(ctx, req, f, nil) - } } // A new flow runs on the current version. Its template is checked before anything is @@ -185,24 +167,6 @@ func (s *Service) StartFlow(ctx context.Context, req StartRequest) (*Flow, error return s.resume(ctx, req, f, &def) } -// priorFlow returns the flow recorded under req.PriorWorkflowID if it is for req's case and -// template, and nil if there is none or it is for another. -func (s *Service) priorFlow(ctx context.Context, req StartRequest) (*Flow, error) { - if req.PriorWorkflowID == "" || req.PriorWorkflowID == req.WorkflowID { - return nil, nil - } - f, err := s.repo.Get(ctx, req.PriorWorkflowID) - switch { - case errors.Is(err, ErrFlowNotFound): - return nil, nil - case err != nil: - return nil, fmt.Errorf("caseflow: failed to read flow: %w", err) - case f.CaseID != req.CaseID || f.TemplateID != req.TemplateID: - return nil, nil - } - return f, nil -} - // resume starts the recorded flow f unless it has already started. def is f's definition // if the caller has loaded it; nil loads it from f's manifest version. func (s *Service) resume(ctx context.Context, req StartRequest, f *Flow, def *workflow.WorkflowDefinition) (*Flow, error) { diff --git a/platform/caseflow/service_test.go b/platform/caseflow/service_test.go index 49058f41..57a2a2d9 100644 --- a/platform/caseflow/service_test.go +++ b/platform/caseflow/service_test.go @@ -19,9 +19,9 @@ import ( // newArtifacts has two manifests; v2, the current one, points export at a new file, drops // legacy, and has notice's task read by reviewer before officer, where v1 has it read by -// officer alone. Notice has one task. In v2, receipt has one too, pair has two, unread -// one whose template has no read.roles, stranger one read by a role the catalog lacks, -// and orphan one whose template the manifest lacks. +// officer alone. Notice has one task. In v2, pair has two, unread one whose template has +// no read.roles, stranger one read by a role the catalog lacks, and orphan one whose +// template the manifest lacks. func newArtifacts() *artifact.Set { set, err := artifact.LoadSet(context.Background(), testutil.MemLoader{ "export.json": []byte(`{"id":"export","name":"Export v1","nodes":[],"edges":[]}`), @@ -29,7 +29,6 @@ func newArtifacts() *artifact.Set { "notice.json": []byte(`{"id":"notice","nodes":[` + taskNode("review", "notice-review") + `],"edges":[]}`), "notice-review.json": []byte(`{"id":"notice-review","read":{"roles":["officer"]}}`), "notice-review2.json": []byte(`{"id":"notice-review","read":{"roles":["reviewer","officer"]}}`), - "receipt.json": []byte(`{"id":"receipt","nodes":[` + taskNode("review", "notice-review") + `],"edges":[]}`), "legacy.json": []byte(`{"id":"legacy","nodes":[],"edges":[]}`), "pair.json": []byte(`{"id":"pair","nodes":[` + taskNode("a", "notice-review") + `,` + taskNode("b", "notice-review") + `],"edges":[]}`), "unread.json": []byte(`{"id":"unread","nodes":[` + taskNode("look", "unread-look") + `],"edges":[]}`), @@ -46,7 +45,6 @@ func newArtifacts() *artifact.Set { {"id":"export","kind":"workflow","path":"export_v2.json"}, {"id":"notice","kind":"workflow","path":"notice.json"}, {"id":"notice-review","kind":"task_template","path":"notice-review2.json"}, - {"id":"receipt","kind":"workflow","path":"receipt.json"}, {"id":"pair","kind":"workflow","path":"pair.json"}, {"id":"unread","kind":"workflow","path":"unread.json"}, {"id":"unread-look","kind":"task_template","path":"unread-look.json"}, diff --git a/platform/tasktypes/external_review.go b/platform/tasktypes/external_review.go index 7d413124..249a027d 100644 --- a/platform/tasktypes/external_review.go +++ b/platform/tasktypes/external_review.go @@ -257,9 +257,7 @@ func dispatchFlowID(taskID, workflowID string) string { // startFlowBody is the body of the other side's client flows route: the workflow to // start, the flow's ID (see dispatchFlowID), the data as its input, and callbackToken. -// priorFlowId is the ID this task's flow had before the workflow was added to it, so a -// dispatch started under that ID and retried under the new one resumes its flow on the -// other side. callbackToken is opaque and names the step this dispatch is for; the reviewer calls +// 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 @@ -271,8 +269,5 @@ func startFlowBody(record *store.TaskRecord, workflowID string, data any, callba "flowId": dispatchFlowID(record.TaskID, workflowID), "input": data, "callbackToken": callbackToken, - // TODO: stop sending priorFlowId once no task dispatched - // under the bare task ID is still open. The other side ignores a body without it. - "priorFlowId": record.TaskID, } } diff --git a/platform/tasktypes/external_review_test.go b/platform/tasktypes/external_review_test.go index f63d0e23..5637794c 100644 --- a/platform/tasktypes/external_review_test.go +++ b/platform/tasktypes/external_review_test.go @@ -106,7 +106,6 @@ func TestExternalReview_DispatchesWithoutReplyToken(t *testing.T) { assert.Equal(t, map[string]any{ "workflowId": "review", "flowId": reviewTaskID + "-review", - "priorFlowId": reviewTaskID, "callbackToken": ownToken(t), "input": map[string]any{"field": "v1"}, }, call.body) @@ -143,8 +142,7 @@ func TestDispatchFlowID(t *testing.T) { } // 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. Both name the task's bare ID as the -// prior flow ID, which the other side resumes only for the same case and workflow. +// 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{}) @@ -159,9 +157,6 @@ func TestExternalReview_FlowIDPerWorkflow(t *testing.T) { 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"]) - for _, call := range calls { - assert.Equal(t, reviewTaskID, call.body["priorFlowId"]) - } } // A retried dispatch names the same flow, so the other side takes it for a retry.