|
| 1 | +/* |
| 2 | + * runtime-e2e/decision_context_transfer_basis/DecisionContextTransferBasisTest.java |
| 3 | + * |
| 4 | + * Real-wire test of the v8.4.0 SDK surface (platform #2509, epic #2508) |
| 5 | + * against a running AxonFlow agent: |
| 6 | + * |
| 7 | + * 1. DecisionSummary.getContext() / DecisionExplanation.getContext() surface |
| 8 | + * the sanitized request context a PEP attaches to a Decision Mode call. We |
| 9 | + * act as the PEP via a raw POST /api/v1/decide (that endpoint is not |
| 10 | + * SDK-wrapped per ADR-056), then read the decision back through the SDK's |
| 11 | + * listDecisions + explainDecision and assert getContext() is populated. |
| 12 | + * 2. AuditLogEntry transfer_basis = "pasal_56b_dpa" round-trips through |
| 13 | + * Jackson serialize -> deserialize verbatim. |
| 14 | + * |
| 15 | + * Run: |
| 16 | + * mvn -DskipTests dependency:build-classpath -Dmdep.outputFile=/tmp/cp.txt -q |
| 17 | + * mvn -DskipTests -q package # build the SDK jar |
| 18 | + * SDK_JAR=$(ls target/axonflow-sdk-*.jar | head -1) |
| 19 | + * CP="$SDK_JAR:$(cat /tmp/cp.txt)" |
| 20 | + * AXONFLOW_AGENT_URL=http://localhost:8080 \ |
| 21 | + * AXONFLOW_TENANT_ID=buku-e-java-e2e AXONFLOW_TENANT_SECRET=buku-e-secret \ |
| 22 | + * java -cp "$CP" \ |
| 23 | + * runtime-e2e/decision_context_transfer_basis/DecisionContextTransferBasisTest.java |
| 24 | + */ |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 27 | +import com.getaxonflow.sdk.AxonFlow; |
| 28 | +import com.getaxonflow.sdk.AxonFlowConfig; |
| 29 | +import com.getaxonflow.sdk.types.AuditLogEntry; |
| 30 | +import com.getaxonflow.sdk.types.DecisionExplanation; |
| 31 | +import com.getaxonflow.sdk.types.DecisionSummary; |
| 32 | +import com.getaxonflow.sdk.types.ListDecisionsOptions; |
| 33 | +import java.net.URI; |
| 34 | +import java.net.http.HttpClient; |
| 35 | +import java.net.http.HttpRequest; |
| 36 | +import java.net.http.HttpResponse; |
| 37 | +import java.util.Base64; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | +public class DecisionContextTransferBasisTest { |
| 42 | + |
| 43 | + static void fail(String msg) { |
| 44 | + System.err.println("FAIL: " + msg); |
| 45 | + System.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + String endpoint = System.getenv().getOrDefault("AXONFLOW_AGENT_URL", "http://localhost:8080"); |
| 50 | + String clientId = System.getenv().getOrDefault("AXONFLOW_TENANT_ID", "buku-e-java-e2e"); |
| 51 | + String secret = System.getenv().getOrDefault("AXONFLOW_TENANT_SECRET", "buku-e-secret"); |
| 52 | + Map<String, String> want = |
| 53 | + Map.of( |
| 54 | + "x_ai_agent", "refund-bot", |
| 55 | + "x_session_id", "sess-buku-42", |
| 56 | + "x_leader_identity", "ops-lead"); |
| 57 | + |
| 58 | + // 1. PEP: create a decision carrying request context (body 'context' map). |
| 59 | + String decisionId = createDecision(endpoint, clientId, secret); |
| 60 | + System.out.println("PEP decide -> decision_id=" + decisionId); |
| 61 | + |
| 62 | + AxonFlow client = |
| 63 | + AxonFlow.create( |
| 64 | + AxonFlowConfig.builder() |
| 65 | + .endpoint(endpoint) |
| 66 | + .clientId(clientId) |
| 67 | + .clientSecret(secret) |
| 68 | + .build()); |
| 69 | + |
| 70 | + // 2. Read it back through the SDK. |
| 71 | + List<DecisionSummary> rows = |
| 72 | + client.listDecisions(ListDecisionsOptions.builder().limit(5).build()); |
| 73 | + DecisionSummary found = |
| 74 | + rows.stream().filter(r -> decisionId.equals(r.getDecisionId())).findFirst().orElse(null); |
| 75 | + if (found == null) { |
| 76 | + fail("listDecisions did not return " + decisionId + " (got " + rows.size() + " rows)"); |
| 77 | + } |
| 78 | + System.out.println("SDK listDecisions -> context=" + found.getContext()); |
| 79 | + if (found.getContext() == null || !found.getContext().entrySet().containsAll(want.entrySet())) { |
| 80 | + fail("listDecisions context = " + found.getContext() + ", want superset of " + want); |
| 81 | + } |
| 82 | + System.out.println( |
| 83 | + "PASS: listDecisions DecisionSummary.getContext() populated with " |
| 84 | + + found.getContext().size() |
| 85 | + + " PEP-forwarded keys"); |
| 86 | + |
| 87 | + DecisionExplanation exp = client.explainDecision(decisionId); |
| 88 | + System.out.println( |
| 89 | + "SDK explainDecision -> context=" |
| 90 | + + exp.getContext() |
| 91 | + + " contextTruncated=" |
| 92 | + + exp.isContextTruncated()); |
| 93 | + if (exp.getContext() == null || !exp.getContext().entrySet().containsAll(want.entrySet())) { |
| 94 | + fail("explainDecision context = " + exp.getContext()); |
| 95 | + } |
| 96 | + System.out.println( |
| 97 | + "PASS: explainDecision returned full context (contextTruncated=" |
| 98 | + + exp.isContextTruncated() |
| 99 | + + ")"); |
| 100 | + |
| 101 | + // 3. transfer_basis = pasal_56b_dpa round-trip (Pasal 56(b)). |
| 102 | + ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); |
| 103 | + String json = |
| 104 | + "{\"id\":\"e2e-audit\",\"timestamp\":\"2026-05-30T10:00:00Z\"," |
| 105 | + + "\"data_residency\":\"ID\",\"transfer_basis\":\"" |
| 106 | + + AuditLogEntry.TRANSFER_BASIS_PASAL_56B_DPA |
| 107 | + + "\"}"; |
| 108 | + AuditLogEntry entry = mapper.readValue(json, AuditLogEntry.class); |
| 109 | + String reserialized = mapper.writeValueAsString(entry); |
| 110 | + AuditLogEntry back = mapper.readValue(reserialized, AuditLogEntry.class); |
| 111 | + if (!"pasal_56b_dpa".equals(back.getTransferBasis())) { |
| 112 | + fail("transfer_basis round-trip = " + back.getTransferBasis() + ", want pasal_56b_dpa"); |
| 113 | + } |
| 114 | + System.out.println("SDK AuditLogEntry round-trip -> " + reserialized); |
| 115 | + System.out.println( |
| 116 | + "PASS: AuditLogEntry.getTransferBasis() = \"" + back.getTransferBasis() + "\" round-trips verbatim"); |
| 117 | + |
| 118 | + System.out.println("ALL PASS: v8.4.0 context + pasal_56b_dpa verified through SDK runtime"); |
| 119 | + } |
| 120 | + |
| 121 | + /** Acts as the PEP: the request context lives in the body's 'context' map. */ |
| 122 | + static String createDecision(String endpoint, String clientId, String secret) throws Exception { |
| 123 | + String body = |
| 124 | + "{\"stage\":\"llm\",\"query\":\"summarize this support ticket\"," |
| 125 | + + "\"target\":{\"type\":\"llm\",\"model\":\"gpt-4\",\"provider\":\"openai\"}," |
| 126 | + + "\"context\":{\"x-ai-agent\":\"refund-bot\",\"x-session-id\":\"sess-buku-42\"," |
| 127 | + + "\"x-leader-identity\":\"ops-lead\"}}"; |
| 128 | + String auth = Base64.getEncoder().encodeToString((clientId + ":" + secret).getBytes()); |
| 129 | + HttpRequest req = |
| 130 | + HttpRequest.newBuilder(URI.create(endpoint + "/api/v1/decide")) |
| 131 | + .header("Content-Type", "application/json") |
| 132 | + .header("X-Client-ID", clientId) |
| 133 | + .header("Authorization", "Basic " + auth) |
| 134 | + .POST(HttpRequest.BodyPublishers.ofString(body)) |
| 135 | + .build(); |
| 136 | + HttpResponse<String> resp = |
| 137 | + HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString()); |
| 138 | + if (resp.statusCode() != 200) { |
| 139 | + fail("decide HTTP " + resp.statusCode() + ": " + resp.body()); |
| 140 | + } |
| 141 | + System.out.println("server /decide response: " + resp.body()); |
| 142 | + String marker = "\"decision_id\":\""; |
| 143 | + int i = resp.body().indexOf(marker); |
| 144 | + if (i < 0) { |
| 145 | + fail("no decision_id in response: " + resp.body()); |
| 146 | + } |
| 147 | + int start = i + marker.length(); |
| 148 | + return resp.body().substring(start, resp.body().indexOf('"', start)); |
| 149 | + } |
| 150 | +} |
0 commit comments