|
| 1 | +// Copyright 2026 AxonFlow |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// WCP retry_context + idempotency_key E2E example (Issue #1673 Phase 1 + 2). |
| 5 | +// |
| 6 | +// Exercises the new Java SDK surface end-to-end against a running v7.3.0 |
| 7 | +// enterprise stack. Every assertion fails the process with System.exit(1). |
| 8 | +// |
| 9 | +// Run from this directory: |
| 10 | +// mvn install -DskipTests=true # from the SDK root, first time only |
| 11 | +// source /tmp/axonflow-e2e-env.sh |
| 12 | +// export AXONFLOW_BASE_URL=http://localhost:8080 |
| 13 | +// mvn -q compile exec:java |
| 14 | +package com.getaxonflow.examples; |
| 15 | + |
| 16 | +import com.getaxonflow.sdk.AxonFlow; |
| 17 | +import com.getaxonflow.sdk.AxonFlowConfig; |
| 18 | +import com.getaxonflow.sdk.exceptions.IdempotencyKeyMismatchException; |
| 19 | +import com.getaxonflow.sdk.types.workflow.WorkflowTypes; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +public class WcpRetryIdempotency { |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + String endpoint = envOrDefault("AXONFLOW_BASE_URL", "http://localhost:8080"); |
| 28 | + String clientId = mustEnv("AXONFLOW_CLIENT_ID"); |
| 29 | + String clientSecret = mustEnv("AXONFLOW_CLIENT_SECRET"); |
| 30 | + |
| 31 | + AxonFlow client = AxonFlow.create( |
| 32 | + AxonFlowConfig.builder() |
| 33 | + .endpoint(endpoint) |
| 34 | + .clientId(clientId) |
| 35 | + .clientSecret(clientSecret) |
| 36 | + .build()); |
| 37 | + |
| 38 | + banner("Act 1 — retry_context (Java SDK)"); |
| 39 | + act1(client); |
| 40 | + |
| 41 | + banner("Act 2 — idempotency_key (Java SDK)"); |
| 42 | + act2(client); |
| 43 | + |
| 44 | + banner("All assertions passed ✔"); |
| 45 | + } |
| 46 | + |
| 47 | + private static void act1(AxonFlow client) { |
| 48 | + WorkflowTypes.CreateWorkflowResponse wf = client.createWorkflow( |
| 49 | + WorkflowTypes.CreateWorkflowRequest.builder() |
| 50 | + .workflowName("java-sdk-retry-context") |
| 51 | + .build()); |
| 52 | + System.out.println("workflow: " + wf.getWorkflowId()); |
| 53 | + |
| 54 | + // 1) First gate — first-call invariants |
| 55 | + WorkflowTypes.StepGateResponse first = client.stepGate( |
| 56 | + wf.getWorkflowId(), |
| 57 | + "step-1", |
| 58 | + WorkflowTypes.StepGateRequest.builder() |
| 59 | + .stepName("first-step") |
| 60 | + .stepType(WorkflowTypes.StepType.TOOL_CALL) |
| 61 | + .build()); |
| 62 | + WorkflowTypes.RetryContext rc = first.getRetryContext(); |
| 63 | + assertTrue("retry_context not null", rc != null); |
| 64 | + assertEqInt("first gate_count", 1, rc.getGateCount()); |
| 65 | + assertEqInt("first completion_count", 0, rc.getCompletionCount()); |
| 66 | + assertEqStr("first prior_completion_status", |
| 67 | + WorkflowTypes.PriorCompletionStatus.NONE.name(), |
| 68 | + rc.getPriorCompletionStatus().name()); |
| 69 | + assertTrue("first !prior_output_available", !rc.isPriorOutputAvailable()); |
| 70 | + assertEqStr("first last_decision (first-call invariant)", |
| 71 | + first.getDecision().name(), rc.getLastDecision().name()); |
| 72 | + assertTrue("first FirstAttemptAt == LastAttemptAt", |
| 73 | + rc.getFirstAttemptAt().equals(rc.getLastAttemptAt())); |
| 74 | + System.out.println(" first gate invariants ✔"); |
| 75 | + |
| 76 | + // 2) Complete, then re-gate |
| 77 | + Map<String, Object> output = new HashMap<>(); |
| 78 | + output.put("transfer_id", "TXN-java-1"); |
| 79 | + output.put("amount", 500); |
| 80 | + client.markStepCompleted( |
| 81 | + wf.getWorkflowId(), |
| 82 | + "step-1", |
| 83 | + WorkflowTypes.MarkStepCompletedRequest.builder().output(output).build()); |
| 84 | + WorkflowTypes.StepGateResponse reGate = client.stepGate( |
| 85 | + wf.getWorkflowId(), |
| 86 | + "step-1", |
| 87 | + WorkflowTypes.StepGateRequest.builder() |
| 88 | + .stepType(WorkflowTypes.StepType.TOOL_CALL) |
| 89 | + .build()); |
| 90 | + assertEqInt("re-gate post-complete gate_count", 2, |
| 91 | + reGate.getRetryContext().getGateCount()); |
| 92 | + assertEqInt("re-gate post-complete completion_count", 1, |
| 93 | + reGate.getRetryContext().getCompletionCount()); |
| 94 | + assertEqStr("re-gate post-complete prior_completion_status", |
| 95 | + WorkflowTypes.PriorCompletionStatus.COMPLETED.name(), |
| 96 | + reGate.getRetryContext().getPriorCompletionStatus().name()); |
| 97 | + assertTrue("re-gate post-complete prior_output_available", |
| 98 | + reGate.getRetryContext().isPriorOutputAvailable()); |
| 99 | + assertTrue("re-gate post-complete prior_output omitted by default", |
| 100 | + reGate.getRetryContext().getPriorOutput() == null); |
| 101 | + assertTrue("re-gate post-complete cached==true", reGate.isCached()); |
| 102 | + System.out.println(" re-gate post-complete ✔"); |
| 103 | + |
| 104 | + // 3) Gate on step-2 without completion (agent-crash simulation) |
| 105 | + client.stepGate( |
| 106 | + wf.getWorkflowId(), |
| 107 | + "step-2", |
| 108 | + WorkflowTypes.StepGateRequest.builder() |
| 109 | + .stepName("second-step") |
| 110 | + .stepType(WorkflowTypes.StepType.TOOL_CALL) |
| 111 | + .build()); |
| 112 | + WorkflowTypes.StepGateResponse reGate2 = client.stepGate( |
| 113 | + wf.getWorkflowId(), |
| 114 | + "step-2", |
| 115 | + WorkflowTypes.StepGateRequest.builder() |
| 116 | + .stepType(WorkflowTypes.StepType.TOOL_CALL) |
| 117 | + .build()); |
| 118 | + assertEqStr("gated_not_completed status", |
| 119 | + WorkflowTypes.PriorCompletionStatus.GATED_NOT_COMPLETED.name(), |
| 120 | + reGate2.getRetryContext().getPriorCompletionStatus().name()); |
| 121 | + assertEqInt("gated_not_completed completion_count", 0, |
| 122 | + reGate2.getRetryContext().getCompletionCount()); |
| 123 | + System.out.println(" gated_not_completed ✔"); |
| 124 | + |
| 125 | + // 4) include_prior_output=true recovers the payload |
| 126 | + WorkflowTypes.StepGateResponse withPrior = client.stepGate( |
| 127 | + wf.getWorkflowId(), |
| 128 | + "step-1", |
| 129 | + WorkflowTypes.StepGateRequest.builder() |
| 130 | + .stepType(WorkflowTypes.StepType.TOOL_CALL) |
| 131 | + .build(), |
| 132 | + WorkflowTypes.StepGateOptions.includePriorOutput()); |
| 133 | + assertTrue("prior_output populated", |
| 134 | + withPrior.getRetryContext().getPriorOutput() != null); |
| 135 | + assertEqStr("prior_output[transfer_id]", "TXN-java-1", |
| 136 | + String.valueOf(withPrior.getRetryContext().getPriorOutput().get("transfer_id"))); |
| 137 | + System.out.println(" prior_output recovery ✔"); |
| 138 | + } |
| 139 | + |
| 140 | + private static void act2(AxonFlow client) { |
| 141 | + WorkflowTypes.CreateWorkflowResponse wf = client.createWorkflow( |
| 142 | + WorkflowTypes.CreateWorkflowRequest.builder() |
| 143 | + .workflowName("java-sdk-idempotency-key") |
| 144 | + .build()); |
| 145 | + System.out.println("workflow: " + wf.getWorkflowId()); |
| 146 | + |
| 147 | + String originalKey = "payment:wire:java-sdk-invoice-1"; |
| 148 | + |
| 149 | + // 5) Gate with key — retry_context.idempotency_key echoes |
| 150 | + WorkflowTypes.StepGateResponse first = client.stepGate( |
| 151 | + wf.getWorkflowId(), |
| 152 | + "step-1", |
| 153 | + WorkflowTypes.StepGateRequest.builder() |
| 154 | + .stepName("wire") |
| 155 | + .stepType(WorkflowTypes.StepType.TOOL_CALL) |
| 156 | + .idempotencyKey(originalKey) |
| 157 | + .build()); |
| 158 | + assertEqStr("retry_context.idempotency_key echo", |
| 159 | + originalKey, first.getRetryContext().getIdempotencyKey()); |
| 160 | + System.out.println(" key round-trip ✔"); |
| 161 | + |
| 162 | + // 6) Re-gate with different key → IdempotencyKeyMismatchException |
| 163 | + try { |
| 164 | + client.stepGate( |
| 165 | + wf.getWorkflowId(), |
| 166 | + "step-1", |
| 167 | + WorkflowTypes.StepGateRequest.builder() |
| 168 | + .stepType(WorkflowTypes.StepType.TOOL_CALL) |
| 169 | + .idempotencyKey("payment:wire:different-2") |
| 170 | + .build()); |
| 171 | + fail("expected IdempotencyKeyMismatchException on gate with different key"); |
| 172 | + } catch (IdempotencyKeyMismatchException e) { |
| 173 | + assertEqStr("mismatch expected_key", originalKey, e.getExpectedIdempotencyKey()); |
| 174 | + assertEqStr("mismatch received_key", "payment:wire:different-2", |
| 175 | + e.getReceivedIdempotencyKey()); |
| 176 | + assertTrue("mismatch workflow_id populated", |
| 177 | + e.getWorkflowId() != null && e.getWorkflowId().startsWith("wf_")); |
| 178 | + assertEqStr("mismatch step_id", "step-1", e.getStepId()); |
| 179 | + } |
| 180 | + System.out.println(" typed 409 error ✔"); |
| 181 | + |
| 182 | + // 7) Complete with matching key |
| 183 | + Map<String, Object> output = new HashMap<>(); |
| 184 | + output.put("transfer_id", "TXN-K1"); |
| 185 | + client.markStepCompleted( |
| 186 | + wf.getWorkflowId(), |
| 187 | + "step-1", |
| 188 | + WorkflowTypes.MarkStepCompletedRequest.builder() |
| 189 | + .output(output) |
| 190 | + .idempotencyKey(originalKey) |
| 191 | + .build()); |
| 192 | + System.out.println(" complete with matching key ✔"); |
| 193 | + } |
| 194 | + |
| 195 | + // --- helpers --- |
| 196 | + |
| 197 | + private static String envOrDefault(String name, String fallback) { |
| 198 | + String v = System.getenv(name); |
| 199 | + return v == null || v.isEmpty() ? fallback : v; |
| 200 | + } |
| 201 | + |
| 202 | + private static String mustEnv(String name) { |
| 203 | + String v = System.getenv(name); |
| 204 | + if (v == null || v.isEmpty()) { |
| 205 | + fail("missing env: " + name); |
| 206 | + } |
| 207 | + return v; |
| 208 | + } |
| 209 | + |
| 210 | + private static void assertTrue(String label, boolean cond) { |
| 211 | + if (!cond) fail("assertion failed: " + label); |
| 212 | + } |
| 213 | + |
| 214 | + private static void assertEqStr(String label, String want, String got) { |
| 215 | + if (want == null ? got != null : !want.equals(got)) { |
| 216 | + fail(label + ": want \"" + want + "\", got \"" + got + "\""); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + private static void assertEqInt(String label, int want, int got) { |
| 221 | + if (want != got) fail(label + ": want " + want + ", got " + got); |
| 222 | + } |
| 223 | + |
| 224 | + private static void fail(String msg) { |
| 225 | + System.err.println("FAIL: " + msg); |
| 226 | + System.exit(1); |
| 227 | + } |
| 228 | + |
| 229 | + private static void banner(String s) { |
| 230 | + System.out.println(); |
| 231 | + System.out.println("━━━ " + s + " ━━━"); |
| 232 | + } |
| 233 | +} |
0 commit comments