From 233f8250968a92b3140d4f55e1b62e9a3b183446 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Fri, 6 Nov 2026 15:16:06 -0800 Subject: [PATCH 01/20] start: - remove context from TemporalOperationStartContext. - add service, operation, deadline, and read-only cancellation token to NexusOperationInfo. --- .../internal/nexus/NexusInfoImpl.java | 41 ++++++++++++++++++- .../nexus/TemporalInterceptorMiddleware.java | 11 ++++- .../nexus/TemporalOperationProcessor.java | 19 +++------ .../io/temporal/nexus/NexusOperationInfo.java | 17 ++++++++ .../nexus/TemporalOperationHandler.java | 8 ++-- 5 files changed, 77 insertions(+), 19 deletions(-) diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java index 279628b04f..4e177e0ce2 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java @@ -1,16 +1,35 @@ package io.temporal.internal.nexus; +import io.temporal.common.CancellationToken; import io.temporal.nexus.NexusOperationInfo; +import java.time.Instant; +import java.util.concurrent.CancellationException; +import javax.annotation.Nullable; class NexusInfoImpl implements NexusOperationInfo { private final String namespace; private final String taskQueue; private final String endpoint; + private final String service; + private final String operation; + private final @Nullable Instant deadline; + private final CancellationToken cancellationToken; - NexusInfoImpl(String namespace, String taskQueue, String endpoint) { + NexusInfoImpl( + String namespace, + String taskQueue, + String endpoint, + String service, + String operation, + @Nullable Instant deadline, + CancellationToken cancellationToken) { this.namespace = namespace; this.taskQueue = taskQueue; this.endpoint = endpoint; + this.service = service; + this.operation = operation; + this.deadline = deadline; + this.cancellationToken = cancellationToken; } @Override @@ -27,4 +46,24 @@ public String getTaskQueue() { public String getEndpoint() { return endpoint; } + + @Override + public String getService() { + return service; + } + + @Override + public String getOperation() { + return operation; + } + + @Override + public @Nullable Instant getDeadline() { + return deadline; + } + + @Override + public CancellationToken getCancellationToken() { + return cancellationToken; + } } diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java index be76b8a91a..11e1b7e071 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java @@ -4,6 +4,8 @@ import io.nexusrpc.handler.*; import io.temporal.common.interceptors.NexusOperationInboundCallsInterceptor; import io.temporal.common.interceptors.WorkerInterceptor; +import io.temporal.internal.concurrent.structured.CancelSource; +import java.util.concurrent.CancellationException; public class TemporalInterceptorMiddleware implements OperationMiddleware { private final WorkerInterceptor[] interceptors; @@ -24,6 +26,9 @@ public OperationHandler intercept( } InternalNexusOperationContext temporalNexusContext = CurrentNexusOperationContext.get(); + CancelSource cancellationSource = + new CancelSource<>(() -> new CancellationException(context.getMethodCancellationReason())); + context.addMethodCancellationListener(cancellationSource::cancel); inboundCallsInterceptor.init( new RootNexusOperationOutboundCallsInterceptor( temporalNexusContext.getMetricsScope(), @@ -31,7 +36,11 @@ public OperationHandler intercept( new NexusInfoImpl( temporalNexusContext.getNamespace(), temporalNexusContext.getTaskQueue(), - temporalNexusContext.getEndpoint()))); + temporalNexusContext.getEndpoint(), + context.getService(), + context.getOperation(), + context.getDeadline(), + cancellationSource.token()))); return new OperationInterceptorConverter(inboundCallsInterceptor); } diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalOperationProcessor.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalOperationProcessor.java index 4edfa72083..8f13a67a37 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalOperationProcessor.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalOperationProcessor.java @@ -13,7 +13,6 @@ import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.reflect.Method; @@ -62,7 +61,7 @@ private static final class TemporalOperationExtension implements MethodExtension } TemporalOperationHandler.StartHandler startHandler = - (ctx, client, input) -> invokeStartHandler(handle, ctx, client, input); + (client, input) -> invokeStartHandler(handle, client, input); return new TemporalOperationHandler(startHandler) {}; } @@ -78,13 +77,11 @@ private static void validateSignature(Method method) { "@TemporalOperation method " + method.getName() + " must not be static"); } Class[] paramTypes = method.getParameterTypes(); - if (paramTypes.length != 3 - || !TemporalOperationStartContext.class.equals(paramTypes[0]) - || !TemporalNexusClient.class.equals(paramTypes[1])) { + if (paramTypes.length != 2 || !TemporalNexusClient.class.equals(paramTypes[0])) { throw new IllegalArgumentException( "@TemporalOperation method " + method.getName() - + " must accept (TemporalOperationStartContext, TemporalNexusClient, I); got " + + " must accept (TemporalNexusClient, I); got " + describeSignature(method)); } if (!TemporalOperationResult.class.equals(method.getReturnType())) { @@ -112,7 +109,7 @@ private static void validateSignature(Method method) { private static void validateTypes(Method method, OperationDefinition operationDefinition) { Type expectedInputType = operationDefinition.getInputType(); - Type declaredInputType = method.getGenericParameterTypes()[2]; + Type declaredInputType = method.getGenericParameterTypes()[1]; if (!typesMatch(declaredInputType, expectedInputType)) { throw new IllegalArgumentException( "@TemporalOperation method " @@ -144,13 +141,9 @@ private static void validateTypes(Method method, OperationDefinition operationDe // Package-private for testing. @SuppressWarnings("unchecked") static TemporalOperationResult invokeStartHandler( - MethodHandle handle, - TemporalOperationStartContext ctx, - TemporalNexusClient client, - Object input) - throws OperationException { + MethodHandle handle, TemporalNexusClient client, Object input) throws OperationException { try { - return (TemporalOperationResult) handle.invoke(ctx, client, input); + return (TemporalOperationResult) handle.invoke(client, input); } catch (RuntimeException | Error | OperationException e) { // HandlerException is a RuntimeException and falls into the RuntimeException arm. throw e; diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index a3ee0e9a76..025248a861 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -1,5 +1,10 @@ package io.temporal.nexus; +import io.temporal.common.CancellationToken; +import java.time.Instant; +import java.util.concurrent.CancellationException; +import javax.annotation.Nullable; + /** * Temporal information about the Nexus Operation. Use {@link NexusOperationContext#getInfo()} from * a Nexus Operation implementation to access. @@ -20,4 +25,16 @@ public interface NexusOperationInfo { * Supported from server v1.30.0. */ String getEndpoint(); + + // :>) add comment + String getService(); + + // :>) add comment + String getOperation(); + + // :>) add comment + @Nullable + Instant getDeadline(); + + CancellationToken getCancellationToken(); } diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java index 3ea9bf6139..1ff54b0370 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java @@ -60,8 +60,7 @@ public class TemporalOperationHandler implements OperationHandler { */ @FunctionalInterface public interface StartHandler { - TemporalOperationResult apply( - TemporalOperationStartContext context, TemporalNexusClient client, T input) + TemporalOperationResult apply(TemporalNexusClient client, T input) throws OperationException, HandlerException; } @@ -92,8 +91,9 @@ public final OperationStartResult start( TemporalNexusClient client = new TemporalNexusClientImpl(nexusCtx.getWorkflowClient(), ctx, details); - TemporalOperationStartContext startContext = new TemporalOperationStartContext(ctx, details); - TemporalOperationResult result = startHandler.apply(startContext, client, input); + // :<) remove + // TemporalOperationStartContext startContext = new TemporalOperationStartContext(ctx, details); + TemporalOperationResult result = startHandler.apply(client, input); if (result.isSync()) { return OperationStartResult.newSyncBuilder(result.getSyncResult()).build(); From 7525d3eae8bc27b1a58dfd8f4284e5360d9eea79 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Fri, 6 Nov 2026 15:40:26 -0800 Subject: [PATCH 02/20] chore: make tests great again --- .../nexus/TemporalOperationHandler.java | 2 +- .../temporal/client/nexus/NexusClientTest.java | 2 +- .../nexus/TemporalOperationProcessorTest.java | 18 +++++++----------- .../ActivityHandleFailOnConflictTest.java | 2 +- ...ctivityHandleUseExistingOnConflictTest.java | 2 +- .../nexus/AsyncActivityOperationTest.java | 4 ++-- .../CancelActivityAsyncOperationTest.java | 8 ++++---- ...xusSerializationContextPropagationTest.java | 8 ++++---- .../nexus/TemporalOperationAnnotationTest.java | 2 +- .../nexus/UpdateWorkflowOperationTest.java | 2 +- 10 files changed, 23 insertions(+), 27 deletions(-) diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java index 1ff54b0370..4da3ae7aa4 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java @@ -29,7 +29,7 @@ * @OperationImpl * public OperationHandler startTransfer() { * return new TemporalOperationHandler( - * (context, client, input) -> + * (client, input) -> * client.startWorkflow( * TransferWorkflow.class, * TransferWorkflow::transfer, diff --git a/temporal-sdk/src/test/java/io/temporal/client/nexus/NexusClientTest.java b/temporal-sdk/src/test/java/io/temporal/client/nexus/NexusClientTest.java index 8e32eadc1b..a8cb3a3754 100644 --- a/temporal-sdk/src/test/java/io/temporal/client/nexus/NexusClientTest.java +++ b/temporal-sdk/src/test/java/io/temporal/client/nexus/NexusClientTest.java @@ -390,7 +390,7 @@ public class ActivityNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, activityId) -> + (client, activityId) -> client.startActivity( LinkingActivity.class, LinkingActivity::execute, diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java index 8900000b92..6c869ff22e 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java @@ -95,12 +95,11 @@ public void invokeStartHandler_readsInstanceField() throws Exception { // read constructor-injected dependencies (workflow client, config, etc.) off `this`. StatefulHandler instance = new StatefulHandler("injected"); java.lang.reflect.Method m = - StatefulHandler.class.getMethod( - "op", TemporalOperationStartContext.class, TemporalNexusClient.class, String.class); + StatefulHandler.class.getMethod("op", TemporalNexusClient.class, String.class); java.lang.invoke.MethodHandle handle = java.lang.invoke.MethodHandles.lookup().unreflect(m).bindTo(instance); TemporalOperationResult result = - TemporalOperationProcessor.invokeStartHandler(handle, null, null, "input"); + TemporalOperationProcessor.invokeStartHandler(handle, null, "input"); Assert.assertEquals("injected:input", result.getSyncResult()); } @@ -111,14 +110,13 @@ public void invokeStartHandler_propagatesRuntimeExceptionUnwrapped() throws Exce // dispatch path. ThrowingHandler instance = new ThrowingHandler(); java.lang.reflect.Method m = - ThrowingHandler.class.getMethod( - "op", TemporalOperationStartContext.class, TemporalNexusClient.class, String.class); + ThrowingHandler.class.getMethod("op", TemporalNexusClient.class, String.class); java.lang.invoke.MethodHandle handle = java.lang.invoke.MethodHandles.lookup().unreflect(m).bindTo(instance); IllegalStateException thrown = Assert.assertThrows( IllegalStateException.class, - () -> TemporalOperationProcessor.invokeStartHandler(handle, null, null, "in")); + () -> TemporalOperationProcessor.invokeStartHandler(handle, null, "in")); Assert.assertEquals("user-thrown", thrown.getMessage()); // First user frame is at the top — no reflective wrapper class in between. Assert.assertEquals(ThrowingHandler.class.getName(), thrown.getStackTrace()[0].getClassName()); @@ -277,8 +275,7 @@ public StatefulHandler(String prefix) { } @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(prefix + ":" + input); } } @@ -286,8 +283,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class ThrowingHandler { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult op(TemporalNexusClient client, String input) { throw new IllegalStateException("user-thrown"); } } @@ -337,7 +333,7 @@ public static class DualAnnotated { @OperationImpl public OperationHandler op() { return new TemporalOperationHandler( - (ctx, client, input) -> TemporalOperationResult.sync(input)) {}; + (client, input) -> TemporalOperationResult.sync(input)) {}; } } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleFailOnConflictTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleFailOnConflictTest.java index 51cb14b703..ab96a42b51 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleFailOnConflictTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleFailOnConflictTest.java @@ -130,7 +130,7 @@ public class TestNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, activityId) -> { + (client, activityId) -> { nexusInvocationCount.incrementAndGet(); return client.startActivity( BlockingActivity.class, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleUseExistingOnConflictTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleUseExistingOnConflictTest.java index 5e01ec9ea2..45c0a94f5c 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleUseExistingOnConflictTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityHandleUseExistingOnConflictTest.java @@ -157,7 +157,7 @@ public class TestNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, activityId) -> + (client, activityId) -> client.startActivity( BlockingActivity.class, BlockingActivity::execute, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java index 776b36c3ec..92a7530bb6 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java @@ -93,13 +93,13 @@ public class TestNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( TestActivity.class, TestActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + context.getRequestId()) + .setId("act-" + input) .setStartToCloseTimeout(Duration.ofSeconds(10)) .build())); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java index d15ec85d4c..b1a652f3a9 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java @@ -189,13 +189,13 @@ public class DefaultCancelNexusServiceImpl { @OperationImpl public OperationHandler operation() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( HeartbeatingActivity.class, HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + context.getRequestId()) + .setId("act-" + input) .setStartToCloseTimeout(Duration.ofSeconds(60)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) @@ -243,13 +243,13 @@ public class OverriddenCancelNexusServiceImpl { @OperationImpl public OperationHandler operation() { return new TemporalOperationHandler( - (context, client, input) -> + (client, input) -> client.startActivity( HeartbeatingActivity.class, HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-override-" + context.getRequestId()) + .setId("act-override-" + input) .setStartToCloseTimeout(Duration.ofSeconds(5)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java index 2db5c243e0..24ef842c1e 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java @@ -472,7 +472,7 @@ public OperationHandler startWorkflow() { @OperationImpl public OperationHandler startActivity() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( BackingActivity.class, BackingActivity::process, @@ -491,7 +491,7 @@ public OperationHandler startActivity() { @OperationImpl public OperationHandler startAsyncActivity() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( AsyncActivity.class, AsyncActivity::process, @@ -505,7 +505,7 @@ public OperationHandler startAsyncActivity() { @OperationImpl public OperationHandler startStallingActivity() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startActivity( StallingActivity.class, StallingActivity::process, @@ -521,7 +521,7 @@ public OperationHandler startStallingActivity() { @OperationImpl public OperationHandler startUpdate() { return TemporalOperationHandler.create( - (context, client, input) -> + (client, input) -> client.startWorkflowUpdate( UpdateTargetWorkflow.class, backingId(input), diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java index a98df3399e..023f0c3d06 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java @@ -174,7 +174,7 @@ public TemporalOperationResult workflowOp( @OperationImpl public OperationHandler legacyOp() { return new TemporalOperationHandler( - (ctx, client, input) -> TemporalOperationResult.sync("legacy:" + input)) {}; + (client, input) -> TemporalOperationResult.sync("legacy:" + input)) {}; } } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/UpdateWorkflowOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/UpdateWorkflowOperationTest.java index 143a9fd7fe..6f070e54dd 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/UpdateWorkflowOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/UpdateWorkflowOperationTest.java @@ -229,7 +229,7 @@ public static class TestNexusServiceImpl { @OperationImpl public OperationHandler update() { return TemporalOperationHandler.create( - (context, client, input) -> { + (client, input) -> { UpdateOptions.Builder optionsBuilder = UpdateOptions.newBuilder(String.class) .setUpdateName("setValue") From 7161e89bd26ae587e3371e3f8d9912c69980f043 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Mon, 9 Nov 2026 16:21:22 -0800 Subject: [PATCH 03/20] feat: add direct Nexus operation info accessor --- .../src/main/java/io/temporal/nexus/Nexus.java | 10 ++++++++++ .../java/io/temporal/nexus/NexusOperationInfo.java | 4 ++-- .../client/nexus/StandaloneNexusSignalLinkingTest.java | 2 +- .../workflow/nexus/ActivityOperationLinkingTest.java | 4 ++-- .../nexus/GenericHandlerSignalDeduplicationTest.java | 2 +- .../workflow/nexus/NexusOperationInfoTest.java | 2 +- .../NexusSerializationContextPropagationTest.java | 2 +- 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/Nexus.java b/temporal-sdk/src/main/java/io/temporal/nexus/Nexus.java index 1d4899b31e..349d69a12c 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/Nexus.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/Nexus.java @@ -5,6 +5,16 @@ /** This class contains methods exposing Temporal APIs for Nexus Operations */ public final class Nexus { + /** + * Get information about the current Nexus Operation. + * + *

This static method relies on a thread-local variable and works only in the original Nexus + * thread. + */ + public static NexusOperationInfo getInfo() { + return getOperationContext().getInfo(); + } + /** * Can be used to get information about a Nexus Operation. This static method relies on a * thread-local variable and works only in the original Nexus thread. diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index 025248a861..8105d2dd6e 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -6,8 +6,8 @@ import javax.annotation.Nullable; /** - * Temporal information about the Nexus Operation. Use {@link NexusOperationContext#getInfo()} from - * a Nexus Operation implementation to access. + * Temporal information about the Nexus Operation. Use {@link Nexus#getInfo()} from a Nexus + * Operation implementation to access. */ public interface NexusOperationInfo { /** diff --git a/temporal-sdk/src/test/java/io/temporal/client/nexus/StandaloneNexusSignalLinkingTest.java b/temporal-sdk/src/test/java/io/temporal/client/nexus/StandaloneNexusSignalLinkingTest.java index 00587b802e..e0837d77ad 100644 --- a/temporal-sdk/src/test/java/io/temporal/client/nexus/StandaloneNexusSignalLinkingTest.java +++ b/temporal-sdk/src/test/java/io/temporal/client/nexus/StandaloneNexusSignalLinkingTest.java @@ -176,7 +176,7 @@ public OperationHandler operation() { io.nexusrpc.handler.OperationStartDetails details, String input) -> { WorkflowClient client = Nexus.getOperationContext().getWorkflowClient(); - String tq = Nexus.getOperationContext().getInfo().getTaskQueue(); + String tq = Nexus.getInfo().getTaskQueue(); SanoSignalCalleeWorkflow startStub = client.newWorkflowStub( SanoSignalCalleeWorkflow.class, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityOperationLinkingTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityOperationLinkingTest.java index 9b2fbe123f..49381b729a 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityOperationLinkingTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityOperationLinkingTest.java @@ -165,9 +165,9 @@ public OperationHandler operation() { ActivityClient.newInstance( Nexus.getOperationContext().getWorkflowClient().getWorkflowServiceStubs(), ActivityClientOptions.newBuilder() - .setNamespace(Nexus.getOperationContext().getInfo().getNamespace()) + .setNamespace(Nexus.getInfo().getNamespace()) .build()); - String taskQueue = Nexus.getOperationContext().getInfo().getTaskQueue(); + String taskQueue = Nexus.getInfo().getTaskQueue(); ActivityHandle first = activityClient.start( diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java index 379fbea2a1..92da5e8e0f 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java @@ -198,7 +198,7 @@ public TemporalOperationResult signalRetry( "CountSignalsWorkflow", WorkflowOptions.newBuilder() .setWorkflowId(targetId) - .setTaskQueue(Nexus.getOperationContext().getInfo().getTaskQueue()) + .setTaskQueue(Nexus.getInfo().getTaskQueue()) .build()) .signalWithStart("mySignal", new Object[] {targetId}, new Object[] {}); diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusOperationInfoTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusOperationInfoTest.java index aeb639c9f1..8276cbcf0e 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusOperationInfoTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusOperationInfoTest.java @@ -52,7 +52,7 @@ public static class TestNexusServiceImpl { public OperationHandler operation() { return OperationHandler.sync( (context, details, input) -> { - NexusOperationInfo info = Nexus.getOperationContext().getInfo(); + NexusOperationInfo info = Nexus.getInfo(); return info.getNamespace() + ":" + info.getTaskQueue() + ":" + info.getEndpoint(); }); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java index 24ef842c1e..064ba9e210 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java @@ -464,7 +464,7 @@ public OperationHandler startWorkflow() { .setMemo(Collections.singletonMap("started", input)) .setStaticSummary("summary-" + input) .setStaticDetails("details-" + input) - .setTaskQueue(Nexus.getOperationContext().getInfo().getTaskQueue()) + .setTaskQueue(Nexus.getInfo().getTaskQueue()) .build()) ::execute); } From e819eb3127ee8d5e1bc469f0084902d9b769cc9b Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Mon, 9 Nov 2026 16:26:29 -0800 Subject: [PATCH 04/20] feat: expose Nexus request ID through operation info --- .../internal/nexus/NexusInfoImpl.java | 8 ++++++++ .../nexus/TemporalInterceptorMiddleware.java | 1 + .../io/temporal/nexus/NexusOperationInfo.java | 6 ++++++ .../nexus/AsyncActivityOperationTest.java | 3 ++- .../CancelActivityAsyncOperationTest.java | 5 +++-- ...GenericHandlerSignalDeduplicationTest.java | 7 +++---- .../TemporalOperationAnnotationTest.java | 19 ++++++++++--------- 7 files changed, 33 insertions(+), 16 deletions(-) diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java index 4e177e0ce2..fad80ac612 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java @@ -12,6 +12,7 @@ class NexusInfoImpl implements NexusOperationInfo { private final String endpoint; private final String service; private final String operation; + private final String requestId; private final @Nullable Instant deadline; private final CancellationToken cancellationToken; @@ -21,6 +22,7 @@ class NexusInfoImpl implements NexusOperationInfo { String endpoint, String service, String operation, + String requestId, @Nullable Instant deadline, CancellationToken cancellationToken) { this.namespace = namespace; @@ -28,6 +30,7 @@ class NexusInfoImpl implements NexusOperationInfo { this.endpoint = endpoint; this.service = service; this.operation = operation; + this.requestId = requestId; this.deadline = deadline; this.cancellationToken = cancellationToken; } @@ -57,6 +60,11 @@ public String getOperation() { return operation; } + @Override + public String getRequestId() { + return requestId; + } + @Override public @Nullable Instant getDeadline() { return deadline; diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java index 11e1b7e071..f98f2899e1 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java @@ -39,6 +39,7 @@ public OperationHandler intercept( temporalNexusContext.getEndpoint(), context.getService(), context.getOperation(), + temporalNexusContext.getRequestId(), context.getDeadline(), cancellationSource.token()))); return new OperationInterceptorConverter(inboundCallsInterceptor); diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index 8105d2dd6e..b976e9cf89 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -32,6 +32,12 @@ public interface NexusOperationInfo { // :>) add comment String getOperation(); + /** + * @return Request ID for the current Nexus start request. The ID remains stable across redelivery + * of the same request. + */ + String getRequestId(); + // :>) add comment @Nullable Instant getDeadline(); diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java index 92a7530bb6..d1e1faeb4e 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java @@ -11,6 +11,7 @@ import io.temporal.internal.nexus.OperationToken; import io.temporal.internal.nexus.OperationTokenType; import io.temporal.internal.nexus.OperationTokenUtil; +import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; @@ -99,7 +100,7 @@ public OperationHandler operation() { TestActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + input) + .setId("act-" + Nexus.getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(10)) .build())); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java index b1a652f3a9..3f9c92ab16 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java @@ -16,6 +16,7 @@ import io.temporal.failure.CanceledFailure; import io.temporal.failure.NexusOperationFailure; import io.temporal.nexus.CancelActivityExecutionInput; +import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationCancelContext; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; @@ -195,7 +196,7 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + input) + .setId("act-" + Nexus.getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(60)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) @@ -249,7 +250,7 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-override-" + input) + .setId("act-override-" + Nexus.getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(5)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java index 92da5e8e0f..a657f49b82 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java @@ -13,7 +13,6 @@ import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -170,7 +169,7 @@ public interface TestNexusService { public static class TestNexusServiceImpl { @TemporalOperation public TemporalOperationResult doubleSignal( - TemporalOperationStartContext context, TemporalNexusClient client, String targetId) { + TemporalNexusClient client, String targetId) { client.getWorkflowClient().newUntypedWorkflowStub(targetId).signal("mySignal", targetId); // The first signal uses up the invocation's only signal, so a second one is rejected even // though it targets a different workflow. @@ -191,7 +190,7 @@ public TemporalOperationResult doubleSignal( @TemporalOperation public TemporalOperationResult signalRetry( - TemporalOperationStartContext context, TemporalNexusClient client, String targetId) { + TemporalNexusClient client, String targetId) { client .getWorkflowClient() .newUntypedWorkflowStub( @@ -204,7 +203,7 @@ public TemporalOperationResult signalRetry( List requestIds = retryRequestIds.computeIfAbsent(targetId, k -> new CopyOnWriteArrayList<>()); - requestIds.add(context.getRequestId()); + requestIds.add(Nexus.getInfo().getRequestId()); if (requestIds.size() == 1) { throw new HandlerException( HandlerException.ErrorType.INTERNAL, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java index 023f0c3d06..4dfd11c85d 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java @@ -6,11 +6,11 @@ import io.nexusrpc.handler.OperationImpl; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -140,21 +140,21 @@ public interface MixedService { @ServiceImpl(service = SugarService.class) public static class SugarServiceImpl { @TemporalOperation - public TemporalOperationResult start( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult start(TemporalNexusClient client, String input) { return client.startWorkflow( SugarTargetWorkflow.class, SugarTargetWorkflow::run, input, - WorkflowOptions.newBuilder().setWorkflowId("sugar-" + ctx.getRequestId()).build()); + WorkflowOptions.newBuilder() + .setWorkflowId("sugar-" + Nexus.getInfo().getRequestId()) + .build()); } } @ServiceImpl(service = SyncSugarService.class) public static class SyncSugarServiceImpl { @TemporalOperation - public TemporalOperationResult greet( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult greet(TemporalNexusClient client, String input) { return TemporalOperationResult.sync("sync:" + input); } } @@ -162,13 +162,14 @@ public TemporalOperationResult greet( @ServiceImpl(service = MixedService.class) public static class MixedServiceImpl { @TemporalOperation - public TemporalOperationResult workflowOp( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult workflowOp(TemporalNexusClient client, String input) { return client.startWorkflow( SugarTargetWorkflow.class, SugarTargetWorkflow::run, input, - WorkflowOptions.newBuilder().setWorkflowId("mixed-" + ctx.getRequestId()).build()); + WorkflowOptions.newBuilder() + .setWorkflowId("mixed-" + Nexus.getInfo().getRequestId()) + .build()); } @OperationImpl From 9fd257b27761e16ad468962531c2a7fa05051ff6 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Mon, 9 Nov 2026 16:59:25 -0800 Subject: [PATCH 05/20] test: migrate Temporal operation handlers to context-free signature --- .../nexus/TemporalOperationProcessorTest.java | 46 ++++++------------- .../nexus/GenericHandlerCancelTest.java | 9 ++-- .../nexus/GenericHandlerDoubleStartTest.java | 11 +++-- .../nexus/GenericHandlerSyncResultTest.java | 4 +- .../nexus/GenericHandlerTypedProcTest.java | 10 ++-- .../GenericHandlerTypedStartWorkflowTest.java | 10 ++-- ...enericHandlerUntypedStartWorkflowTest.java | 9 ++-- 7 files changed, 43 insertions(+), 56 deletions(-) diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java index 6c869ff22e..f41b862dad 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java @@ -9,7 +9,6 @@ import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import org.junit.Assert; import org.junit.Test; @@ -134,10 +133,7 @@ public void rejects_badReturnType() { public void rejects_badArity() { IllegalArgumentException e = assertInvalid(() -> TemporalOperationProcessor.process(new BadArity())); - Assert.assertTrue( - e.getMessage(), - e.getMessage() - .contains("must accept (TemporalOperationStartContext, TemporalNexusClient, I)")); + Assert.assertTrue(e.getMessage(), e.getMessage().contains("TemporalNexusClient, I)")); } @Test @@ -181,8 +177,7 @@ private static IllegalArgumentException assertInvalid(Runnable action) { @ServiceImpl(service = SingleOpService.class) public static class ValidSugar { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(input); } } @@ -190,7 +185,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class BadReturnType { @TemporalOperation - public String op(TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public String op(TemporalNexusClient client, String input) { return input; } } @@ -198,7 +193,7 @@ public String op(TemporalOperationStartContext ctx, TemporalNexusClient client, @ServiceImpl(service = SingleOpService.class) public static class BadArity { @TemporalOperation - public TemporalOperationResult op(TemporalNexusClient client, String input) { + public TemporalOperationResult op(String input) { return TemporalOperationResult.sync(input); } } @@ -207,9 +202,7 @@ public TemporalOperationResult op(TemporalNexusClient client, String inp public static class CompositeOk { @TemporalOperation public TemporalOperationResult> compose( - TemporalOperationStartContext ctx, - TemporalNexusClient client, - java.util.Map input) { + TemporalNexusClient client, java.util.Map input) { return TemporalOperationResult.sync(java.util.Collections.emptyList()); } } @@ -219,9 +212,7 @@ public static class CompositeBadInput { // Map value type is String instead of Integer. @TemporalOperation public TemporalOperationResult> compose( - TemporalOperationStartContext ctx, - TemporalNexusClient client, - java.util.Map input) { + TemporalNexusClient client, java.util.Map input) { return TemporalOperationResult.sync(java.util.Collections.emptyList()); } } @@ -231,9 +222,7 @@ public static class CompositeBadOutput { // Result list element type is Integer instead of String. @TemporalOperation public TemporalOperationResult> compose( - TemporalOperationStartContext ctx, - TemporalNexusClient client, - java.util.Map input) { + TemporalNexusClient client, java.util.Map input) { return TemporalOperationResult.sync(java.util.Collections.emptyList()); } } @@ -242,8 +231,7 @@ public TemporalOperationResult> compose( @ServiceImpl(service = SingleOpService.class) public static class RawReturnType { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(input); } } @@ -251,8 +239,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class NonPublicMethod { @TemporalOperation - TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(input); } } @@ -260,8 +247,7 @@ TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class StaticMethod { @TemporalOperation - public static TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) { + public static TemporalOperationResult op(TemporalNexusClient client, String input) { return TemporalOperationResult.sync(input); } } @@ -291,8 +277,7 @@ public TemporalOperationResult op(TemporalNexusClient client, String inp @ServiceImpl(service = VoidIoService.class) public static class VoidIo { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, Void input) { + public TemporalOperationResult op(TemporalNexusClient client, Void input) { return TemporalOperationResult.sync(null); } } @@ -300,8 +285,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class DeclaresCheckedException { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) + public TemporalOperationResult op(TemporalNexusClient client, String input) throws java.io.IOException { return TemporalOperationResult.sync(input); } @@ -310,8 +294,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class DeclaresRuntimeException { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) + public TemporalOperationResult op(TemporalNexusClient client, String input) throws IllegalStateException { return TemporalOperationResult.sync(input); } @@ -320,8 +303,7 @@ public TemporalOperationResult op( @ServiceImpl(service = SingleOpService.class) public static class DeclaresNexusrpcCheckedExceptions { @TemporalOperation - public TemporalOperationResult op( - TemporalOperationStartContext ctx, TemporalNexusClient client, String input) + public TemporalOperationResult op(TemporalNexusClient client, String input) throws io.nexusrpc.OperationException, io.nexusrpc.handler.HandlerException { return TemporalOperationResult.sync(input); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java index 50d0223416..d2f7a18ace 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java @@ -9,10 +9,11 @@ import io.temporal.client.WorkflowStub; import io.temporal.failure.CanceledFailure; import io.temporal.internal.Signal; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -128,14 +129,14 @@ public interface TestNexusCancelService { @ServiceImpl(service = TestNexusCancelService.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, String input) { + public TemporalOperationResult operation(TemporalNexusClient client, String input) { + NexusOperationInfo info = Nexus.getInfo(); return client.startWorkflow( WaitForCancelWorkflowInterface.class, WaitForCancelWorkflowInterface::execute, input, WorkflowOptions.newBuilder() - .setWorkflowId("generic-cancel-test-" + context.getService()) + .setWorkflowId("generic-cancel-test-" + info.getService()) .build()); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java index b319bc8c8a..88eccd0794 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java @@ -8,10 +8,11 @@ import io.temporal.client.WorkflowOptions; import io.temporal.failure.ApplicationFailure; import io.temporal.failure.NexusOperationFailure; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -101,15 +102,15 @@ public interface TestNexusServiceDoubleStart { @ServiceImpl(service = TestNexusServiceDoubleStart.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, String input) { + public TemporalOperationResult operation(TemporalNexusClient client, String input) { // First start should succeed but the workflow blocks indefinitely + NexusOperationInfo info = Nexus.getInfo(); client.startWorkflow( BlockingWorkflow.class, BlockingWorkflow::execute, input, WorkflowOptions.newBuilder() - .setWorkflowId("double-start-first-" + context.getService()) + .setWorkflowId("double-start-first-" + info.getService()) .build()); // Second start should throw return client.startWorkflow( @@ -117,7 +118,7 @@ public TemporalOperationResult operation( BlockingWorkflow::execute, input, WorkflowOptions.newBuilder() - .setWorkflowId("double-start-second-" + context.getService()) + .setWorkflowId("double-start-second-" + info.getService()) .build()); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSyncResultTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSyncResultTest.java index 8390a0f0a4..8e7205a730 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSyncResultTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSyncResultTest.java @@ -6,7 +6,6 @@ import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -61,8 +60,7 @@ public interface TestNexusSyncService { @ServiceImpl(service = TestNexusSyncService.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, String input) { + public TemporalOperationResult operation(TemporalNexusClient client, String input) { return TemporalOperationResult.sync("sync-" + input); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java index d3c23c0d7c..93ec457e30 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java @@ -4,10 +4,11 @@ import io.nexusrpc.Service; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -67,10 +68,11 @@ public interface TestNexusServiceProc { @ServiceImpl(service = TestNexusServiceProc.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, Integer input) { + public TemporalOperationResult operation(TemporalNexusClient client, Integer input) { + + NexusOperationInfo info = Nexus.getInfo(); String prefix = "generic-handler-test-proc" + input + "-"; - String workflowId = prefix + context.getService() + "-" + context.getOperation(); + String workflowId = prefix + info.getService() + "-" + info.getOperation(); WorkflowOptions options = WorkflowOptions.newBuilder().setWorkflowId(workflowId).build(); switch (input) { case 0: diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java index 31eec88d06..1cd05240fc 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java @@ -4,10 +4,11 @@ import io.nexusrpc.Service; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -68,10 +69,11 @@ public interface TestNexusServiceGeneric { @ServiceImpl(service = TestNexusServiceGeneric.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, Integer input) { + public TemporalOperationResult operation(TemporalNexusClient client, Integer input) { String prefix = "generic-handler-test-func" + input + "-"; - String workflowId = prefix + context.getService() + "-" + context.getOperation(); + NexusOperationInfo info = Nexus.getInfo(); + + String workflowId = prefix + info.getService() + "-" + info.getOperation(); WorkflowOptions options = WorkflowOptions.newBuilder().setWorkflowId(workflowId).build(); switch (input) { case 0: diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java index 71eb71ff07..359a568708 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java @@ -4,10 +4,11 @@ import io.nexusrpc.Service; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationResult; -import io.temporal.nexus.TemporalOperationStartContext; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; import io.temporal.testing.internal.SDKTestWorkflowRule; @@ -64,14 +65,14 @@ public interface TestNexusServiceUntyped { @ServiceImpl(service = TestNexusServiceUntyped.class) public class TestNexusServiceImpl { @TemporalOperation - public TemporalOperationResult operation( - TemporalOperationStartContext context, TemporalNexusClient client, String input) { + public TemporalOperationResult operation(TemporalNexusClient client, String input) { + NexusOperationInfo info = Nexus.getInfo(); return client.startWorkflow( "func1", String.class, WorkflowOptions.newBuilder() .setWorkflowId( - "generic-handler-untyped-" + context.getService() + "-" + context.getOperation()) + "generic-handler-untyped-" + info.getService() + "-" + info.getOperation()) .build(), input); } From 3eae86d42ca74d4269b01a51b488e3352c03ffc0 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Mon, 9 Nov 2026 17:44:05 -0800 Subject: [PATCH 06/20] test: cover ambient Nexus cancellation and deadline --- .../TemporalInterceptorMiddlewareTest.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java new file mode 100644 index 0000000000..20453b8e01 --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -0,0 +1,89 @@ +package io.temporal.internal.nexus; + +import io.nexusrpc.handler.OperationContext; +import io.nexusrpc.handler.OperationHandler; +import io.nexusrpc.handler.OperationMethodCanceller; +import io.temporal.common.CancellationToken; +import io.temporal.common.interceptors.WorkerInterceptor; +import io.temporal.nexus.Nexus; +import java.time.Instant; +import java.util.concurrent.CancellationException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; + +public class TemporalInterceptorMiddlewareTest { + + @After + public void tearDown() { + CurrentNexusOperationContext.unset(); + } + + @Test + public void methodCancellationReachesNexusOperationInfo() { + OperationMethodCanceller canceller = new OperationMethodCanceller(); + OperationContext operationContext = + OperationContext.newBuilder() + .setService("service") + .setOperation("operation") + .setMethodCanceller(canceller) + .build(); + installOperationContext(operationContext); + + CancellationToken token = Nexus.getInfo().getCancellationToken(); + Assert.assertFalse(token.isCancellationRequested()); + + canceller.cancel("test reason"); + + Assert.assertTrue(token.isCancellationRequested()); + CancellationException exception = + Assert.assertThrows(CancellationException.class, token::throwIfCancellationRequested); + Assert.assertEquals("test reason", exception.getMessage()); + } + + @Test + public void cancellationBeforeInterceptionReachesNexusOperationInfo() { + OperationMethodCanceller canceller = new OperationMethodCanceller(); + canceller.cancel("early reason"); + OperationContext operationContext = + OperationContext.newBuilder() + .setService("service") + .setOperation("operation") + .setMethodCanceller(canceller) + .build(); + installOperationContext(operationContext); + + CancellationToken token = Nexus.getInfo().getCancellationToken(); + + Assert.assertTrue(token.isCancellationRequested()); + CancellationException exception = + Assert.assertThrows(CancellationException.class, token::throwIfCancellationRequested); + Assert.assertEquals("early reason", exception.getMessage()); + } + + @Test + public void deadlineReachesNexusOperationInfo() { + Instant deadline = Instant.parse("2030-01-02T03:04:05Z"); + OperationContext operationContext = + OperationContext.newBuilder() + .setService("service") + .setOperation("operation") + .setDeadline(deadline) + .build(); + installOperationContext(operationContext); + + Assert.assertEquals(deadline, Nexus.getInfo().getDeadline()); + } + + private static void installOperationContext(OperationContext operationContext) { + InternalNexusOperationContext temporalContext = + new InternalNexusOperationContext("namespace", "taskQueue", "endpoint", null, null); + temporalContext.setRequestId("request-id"); + CurrentNexusOperationContext.set(temporalContext); + + TemporalInterceptorMiddleware middleware = + new TemporalInterceptorMiddleware(new WorkerInterceptor[0]); + middleware.intercept( + operationContext, OperationHandler.sync((context, details, input) -> input)); + } +} From 1793ab1ef32dc5106a066e409ea8e0d635a2c3c4 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Mon, 9 Nov 2026 17:49:56 -0800 Subject: [PATCH 07/20] fix: keep Nexus request ID internal --- .../internal/nexus/NexusInfoImpl.java | 8 ------- .../nexus/TemporalInterceptorMiddleware.java | 1 - .../io/temporal/nexus/NexusOperationInfo.java | 6 ----- .../nexus/AsyncActivityOperationTest.java | 3 +-- .../CancelActivityAsyncOperationTest.java | 5 ++-- ...GenericHandlerSignalDeduplicationTest.java | 24 +++++++++---------- .../TemporalOperationAnnotationTest.java | 9 ++----- 7 files changed, 16 insertions(+), 40 deletions(-) diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java index fad80ac612..4e177e0ce2 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java @@ -12,7 +12,6 @@ class NexusInfoImpl implements NexusOperationInfo { private final String endpoint; private final String service; private final String operation; - private final String requestId; private final @Nullable Instant deadline; private final CancellationToken cancellationToken; @@ -22,7 +21,6 @@ class NexusInfoImpl implements NexusOperationInfo { String endpoint, String service, String operation, - String requestId, @Nullable Instant deadline, CancellationToken cancellationToken) { this.namespace = namespace; @@ -30,7 +28,6 @@ class NexusInfoImpl implements NexusOperationInfo { this.endpoint = endpoint; this.service = service; this.operation = operation; - this.requestId = requestId; this.deadline = deadline; this.cancellationToken = cancellationToken; } @@ -60,11 +57,6 @@ public String getOperation() { return operation; } - @Override - public String getRequestId() { - return requestId; - } - @Override public @Nullable Instant getDeadline() { return deadline; diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java index f98f2899e1..11e1b7e071 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java @@ -39,7 +39,6 @@ public OperationHandler intercept( temporalNexusContext.getEndpoint(), context.getService(), context.getOperation(), - temporalNexusContext.getRequestId(), context.getDeadline(), cancellationSource.token()))); return new OperationInterceptorConverter(inboundCallsInterceptor); diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index b976e9cf89..8105d2dd6e 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -32,12 +32,6 @@ public interface NexusOperationInfo { // :>) add comment String getOperation(); - /** - * @return Request ID for the current Nexus start request. The ID remains stable across redelivery - * of the same request. - */ - String getRequestId(); - // :>) add comment @Nullable Instant getDeadline(); diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java index d1e1faeb4e..92a7530bb6 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java @@ -11,7 +11,6 @@ import io.temporal.internal.nexus.OperationToken; import io.temporal.internal.nexus.OperationTokenType; import io.temporal.internal.nexus.OperationTokenUtil; -import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; @@ -100,7 +99,7 @@ public OperationHandler operation() { TestActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + Nexus.getInfo().getRequestId()) + .setId("act-" + input) .setStartToCloseTimeout(Duration.ofSeconds(10)) .build())); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java index 3f9c92ab16..b1a652f3a9 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java @@ -16,7 +16,6 @@ import io.temporal.failure.CanceledFailure; import io.temporal.failure.NexusOperationFailure; import io.temporal.nexus.CancelActivityExecutionInput; -import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationCancelContext; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; @@ -196,7 +195,7 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + Nexus.getInfo().getRequestId()) + .setId("act-" + input) .setStartToCloseTimeout(Duration.ofSeconds(60)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) @@ -250,7 +249,7 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-override-" + Nexus.getInfo().getRequestId()) + .setId("act-override-" + input) .setStartToCloseTimeout(Duration.ofSeconds(5)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java index a657f49b82..2c3399901e 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java @@ -18,11 +18,8 @@ import io.temporal.testing.internal.SDKTestWorkflowRule; import io.temporal.workflow.*; import java.time.Duration; -import java.util.List; -import java.util.Map; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -38,13 +35,13 @@ public class GenericHandlerSignalDeduplicationTest { private static final String ONE_SIGNAL_MESSAGE = "Only one signal can be sent per Nexus operation invocation."; - private static final Map> retryRequestIds = new ConcurrentHashMap<>(); + private final AtomicInteger retryAttempts = new AtomicInteger(); @Rule public SDKTestWorkflowRule testWorkflowRule = SDKTestWorkflowRule.newBuilder() .setWorkflowTypes(CallerWorkflowImpl.class, CountSignalsWorkflowImpl.class) - .setNexusServiceImplementation(new TestNexusServiceImpl()) + .setNexusServiceImplementation(new TestNexusServiceImpl(retryAttempts)) .build(); @Test @@ -75,10 +72,8 @@ public void signalDedupedAcrossHandlerRetry() { Assert.assertEquals("signaled-" + targetId, newCaller().execute("signalRetry", targetId)); - List requestIds = retryRequestIds.get(targetId); Assert.assertEquals( - "handler should fail once and then be redelivered once", 2, requestIds.size()); - Assert.assertEquals(requestIds.get(0), requestIds.get(1)); + "handler should fail once and then be redelivered once", 2, retryAttempts.get()); CountSignalsWorkflow target = testWorkflowRule.getWorkflowClient().newWorkflowStub(CountSignalsWorkflow.class, targetId); @@ -167,6 +162,12 @@ public interface TestNexusService { @ServiceImpl(service = TestNexusService.class) public static class TestNexusServiceImpl { + private final AtomicInteger retryAttempts; + + public TestNexusServiceImpl(AtomicInteger retryAttempts) { + this.retryAttempts = retryAttempts; + } + @TemporalOperation public TemporalOperationResult doubleSignal( TemporalNexusClient client, String targetId) { @@ -201,10 +202,7 @@ public TemporalOperationResult signalRetry( .build()) .signalWithStart("mySignal", new Object[] {targetId}, new Object[] {}); - List requestIds = - retryRequestIds.computeIfAbsent(targetId, k -> new CopyOnWriteArrayList<>()); - requestIds.add(Nexus.getInfo().getRequestId()); - if (requestIds.size() == 1) { + if (retryAttempts.incrementAndGet() == 1) { throw new HandlerException( HandlerException.ErrorType.INTERNAL, "force handler redelivery after signalling", diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java index 4dfd11c85d..0451eb87ce 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java @@ -6,7 +6,6 @@ import io.nexusrpc.handler.OperationImpl; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; -import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationHandler; @@ -145,9 +144,7 @@ public TemporalOperationResult start(TemporalNexusClient client, String SugarTargetWorkflow.class, SugarTargetWorkflow::run, input, - WorkflowOptions.newBuilder() - .setWorkflowId("sugar-" + Nexus.getInfo().getRequestId()) - .build()); + WorkflowOptions.newBuilder().setWorkflowId("sugar-" + input).build()); } } @@ -167,9 +164,7 @@ public TemporalOperationResult workflowOp(TemporalNexusClient client, St SugarTargetWorkflow.class, SugarTargetWorkflow::run, input, - WorkflowOptions.newBuilder() - .setWorkflowId("mixed-" + Nexus.getInfo().getRequestId()) - .build()); + WorkflowOptions.newBuilder().setWorkflowId("mixed-" + input).build()); } @OperationImpl From 981cf958c98c10742903d3a55ca8a6081caf14b8 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Tue, 10 Nov 2026 14:23:10 -0800 Subject: [PATCH 08/20] refactor: remove explicit context from Temporal cancellation handlers --- .../nexus/TemporalOperationCancelContext.java | 88 ------------------- .../nexus/TemporalOperationHandler.java | 23 ++--- .../nexus/TemporalOperationHandlerTest.java | 35 ++++++++ .../CancelActivityAsyncOperationTest.java | 4 +- 4 files changed, 43 insertions(+), 107 deletions(-) delete mode 100644 temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationCancelContext.java create mode 100644 temporal-sdk/src/test/java/io/temporal/nexus/TemporalOperationHandlerTest.java diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationCancelContext.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationCancelContext.java deleted file mode 100644 index 963dc2981b..0000000000 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationCancelContext.java +++ /dev/null @@ -1,88 +0,0 @@ -package io.temporal.nexus; - -import io.nexusrpc.handler.OperationCancelDetails; -import io.nexusrpc.handler.OperationContext; -import io.nexusrpc.handler.OperationMethodCancellationListener; -import io.temporal.common.Experimental; -import java.time.Instant; -import java.util.Map; -import java.util.Objects; -import org.jspecify.annotations.Nullable; - -/** - * Context for a Nexus cancel operation request, passed to {@link - * TemporalOperationHandler#cancelWorkflowRun}. - */ -@Experimental -public final class TemporalOperationCancelContext { - - private final OperationContext operationContext; - private final OperationCancelDetails operationCancelDetails; - - TemporalOperationCancelContext( - OperationContext operationContext, OperationCancelDetails operationCancelDetails) { - this.operationContext = Objects.requireNonNull(operationContext); - this.operationCancelDetails = Objects.requireNonNull(operationCancelDetails); - } - - /** Returns the service name for this operation. */ - public String getService() { - return operationContext.getService(); - } - - /** Returns the operation name. */ - public String getOperation() { - return operationContext.getOperation(); - } - - /** Returns the headers for this cancel request. The returned map is case-insensitive. */ - public Map getHeaders() { - return operationContext.getHeaders(); - } - - /** - * Returns the deadline for the operation handler method. This is the time by which the method - * should complete. This is not the operation's deadline. - */ - public @Nullable Instant getDeadline() { - return operationContext.getDeadline(); - } - - /** Returns the operation token identifying the operation to cancel. */ - public String getOperationToken() { - return operationCancelDetails.getOperationToken(); - } - - /** - * True if the handler method has been cancelled. Note, this is method cancellation, unrelated to - * operation cancellation. - */ - public boolean isMethodCancelled() { - return operationContext.isMethodCancelled(); - } - - /** - * Reason the handler method was cancelled, or null if not cancelled. Note, this is method - * cancellation, unrelated to operation cancellation. - */ - public @Nullable String getMethodCancellationReason() { - return operationContext.getMethodCancellationReason(); - } - - /** - * Add a listener for method cancellation. The listener is invoked immediately before this method - * returns if the method is already cancelled. The listener must not block and must not be - * registered from within another cancellation listener. - */ - public void addMethodCancellationListener(OperationMethodCancellationListener listener) { - operationContext.addMethodCancellationListener(listener); - } - - /** - * Remove a method cancellation listener, if present. Must not be called from within another - * cancellation listener. - */ - public void removeMethodCancellationListener(OperationMethodCancellationListener listener) { - operationContext.removeMethodCancellationListener(listener); - } -} diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java index 4da3ae7aa4..179c15a323 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java @@ -21,7 +21,8 @@ * customize cancel behavior. Override {@link #cancelWorkflowRun}, {@link #cancelUpdateWorkflow}, or * {@link #cancelActivityExecution} to change how the corresponding cancellation is handled. The * {@link #start} and {@link #cancel} methods should not be overridden — they contain the core - * dispatch logic. + * dispatch logic. Operation information is available to both start and cancel handlers through + * {@link Nexus#getInfo()}. * *

Custom-cancel example: * @@ -38,8 +39,7 @@ * .setWorkflowId("transfer-" + input.getTransferId()) * .build())) { * @Override - * protected void cancelWorkflowRun( - * TemporalOperationCancelContext ctx, CancelWorkflowRunInput input) { + * protected void cancelWorkflowRun(CancelWorkflowRunInput input) { * // custom logic * } * }; @@ -116,20 +116,17 @@ public final void cancel(OperationContext ctx, OperationCancelDetails details) { HandlerException.ErrorType.BAD_REQUEST, "failed to parse operation token", e); } - TemporalOperationCancelContext cancelContext = new TemporalOperationCancelContext(ctx, details); switch (token.getType()) { case WORKFLOW_RUN: - cancelWorkflowRun(cancelContext, new CancelWorkflowRunInput(token.getWorkflowId())); + cancelWorkflowRun(new CancelWorkflowRunInput(token.getWorkflowId())); break; case WORKFLOW_UPDATE: cancelUpdateWorkflow( - cancelContext, new CancelUpdateWorkflowInput( token.getWorkflowId(), token.getRunId(), token.getUpdateId())); break; case ACTIVITY_EXECUTION: cancelActivityExecution( - cancelContext, new CancelActivityExecutionInput(token.getActivityId(), token.getRunId())); break; default: @@ -145,11 +142,9 @@ public final void cancel(OperationContext ctx, OperationCancelDetails details) { * *

Default behavior: cancels the underlying workflow. * - * @param context the cancel context * @param input describes the workflow run to cancel */ - protected void cancelWorkflowRun( - TemporalOperationCancelContext context, CancelWorkflowRunInput input) { + protected void cancelWorkflowRun(CancelWorkflowRunInput input) { WorkflowClient client = CurrentNexusOperationContext.get().getWorkflowClient(); client.newUntypedWorkflowStub(input.getWorkflowId()).cancel(); } @@ -161,11 +156,9 @@ protected void cancelWorkflowRun( *

Default behavior: not implemented. There is no server primitive to cancel an in-flight * workflow update. * - * @param context the cancel context * @param input describes the update to cancel */ - protected void cancelUpdateWorkflow( - TemporalOperationCancelContext context, CancelUpdateWorkflowInput input) { + protected void cancelUpdateWorkflow(CancelUpdateWorkflowInput input) { throw new HandlerException( HandlerException.ErrorType.NOT_IMPLEMENTED, new UnsupportedOperationException("cannot cancel an UpdateWorkflow operation")); @@ -177,11 +170,9 @@ protected void cancelUpdateWorkflow( * *

Default behavior: requests cancellation of the underlying standalone activity execution. * - * @param context the cancel context * @param input describes the activity execution to cancel */ - protected void cancelActivityExecution( - TemporalOperationCancelContext context, CancelActivityExecutionInput input) { + protected void cancelActivityExecution(CancelActivityExecutionInput input) { WorkflowClient wc = CurrentNexusOperationContext.get().getWorkflowClient(); ActivityClient ac = ActivityClient.newInstance( diff --git a/temporal-sdk/src/test/java/io/temporal/nexus/TemporalOperationHandlerTest.java b/temporal-sdk/src/test/java/io/temporal/nexus/TemporalOperationHandlerTest.java new file mode 100644 index 0000000000..bacb60ccee --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/nexus/TemporalOperationHandlerTest.java @@ -0,0 +1,35 @@ +package io.temporal.nexus; + +import io.nexusrpc.handler.OperationCancelDetails; +import io.nexusrpc.handler.OperationContext; +import io.temporal.internal.nexus.OperationTokenUtil; +import java.util.concurrent.atomic.AtomicReference; +import org.junit.Assert; +import org.junit.Test; + +public class TemporalOperationHandlerTest { + + @Test + public void cancelDispatchesTypedInputToContextFreeOverride() throws Exception { + AtomicReference receivedInput = new AtomicReference<>(); + TemporalOperationHandler handler = + new TemporalOperationHandler( + (client, input) -> TemporalOperationResult.sync(input)) { + @Override + protected void cancelActivityExecution(CancelActivityExecutionInput input) { + receivedInput.set(input); + } + }; + String operationToken = + OperationTokenUtil.generateActivityExecutionOperationToken( + "activity-id", "run-id", "namespace"); + + handler.cancel( + OperationContext.newBuilder().setService("service").setOperation("operation").build(), + OperationCancelDetails.newBuilder().setOperationToken(operationToken).build()); + + Assert.assertNotNull(receivedInput.get()); + Assert.assertEquals("activity-id", receivedInput.get().getActivityId()); + Assert.assertEquals("run-id", receivedInput.get().getRunId()); + } +} diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java index b1a652f3a9..a1d6bcd3f3 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java @@ -16,7 +16,6 @@ import io.temporal.failure.CanceledFailure; import io.temporal.failure.NexusOperationFailure; import io.temporal.nexus.CancelActivityExecutionInput; -import io.temporal.nexus.TemporalOperationCancelContext; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; @@ -255,8 +254,7 @@ public OperationHandler operation() { .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) .build())) { @Override - protected void cancelActivityExecution( - TemporalOperationCancelContext ctx, CancelActivityExecutionInput input) { + protected void cancelActivityExecution(CancelActivityExecutionInput input) { customCancelInvoked.set(true); // Intentionally do NOT invoke default cancel; the activity will self-terminate via // startToCloseTimeout. From 2334f015e95edabf64112c948d6a2b0a4ff4b795 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Tue, 10 Nov 2026 15:43:57 -0800 Subject: [PATCH 09/20] test: verify interceptor header access with context-free handler --- .../TemporalInterceptorMiddlewareTest.java | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java index 20453b8e01..407b2604de 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -1,11 +1,20 @@ package io.temporal.internal.nexus; +import static org.mockito.Mockito.mock; + +import io.nexusrpc.OperationException; import io.nexusrpc.handler.OperationContext; import io.nexusrpc.handler.OperationHandler; import io.nexusrpc.handler.OperationMethodCanceller; +import io.nexusrpc.handler.OperationStartDetails; +import io.temporal.client.WorkflowClient; import io.temporal.common.CancellationToken; +import io.temporal.common.interceptors.NexusOperationInboundCallsInterceptor; import io.temporal.common.interceptors.WorkerInterceptor; +import io.temporal.common.interceptors.WorkerInterceptorBase; import io.temporal.nexus.Nexus; +import io.temporal.nexus.TemporalOperationHandler; +import io.temporal.nexus.TemporalOperationResult; import java.time.Instant; import java.util.concurrent.CancellationException; import org.junit.After; @@ -14,6 +23,41 @@ public class TemporalInterceptorMiddlewareTest { + @Test + public void interceptorReadsHeadersBeforeContextFreeHandler() throws OperationException { + OperationContext operationContext = + OperationContext.newBuilder() + .setService("service") + .setOperation("operation") + .putHeader("test-header", "test-value") + .build(); + WorkerInterceptor interceptor = + new WorkerInterceptorBase() { + @Override + public NexusOperationInboundCallsInterceptor interceptNexusOperation( + OperationContext context, NexusOperationInboundCallsInterceptor next) { + Assert.assertEquals("test-value", context.getHeaders().get("test-header")); + return next; + } + }; + initializeTemporalContext(); + OperationHandler handler = + new TemporalInterceptorMiddleware(new WorkerInterceptor[] {interceptor}) + .intercept( + operationContext, + TemporalOperationHandler.create( + (client, input) -> TemporalOperationResult.sync(input))); + + Assert.assertEquals( + "input", + handler + .start( + operationContext, + OperationStartDetails.newBuilder().setRequestId("request-id").build(), + "input") + .getSyncResult()); + } + @After public void tearDown() { CurrentNexusOperationContext.unset(); @@ -76,14 +120,18 @@ public void deadlineReachesNexusOperationInfo() { } private static void installOperationContext(OperationContext operationContext) { - InternalNexusOperationContext temporalContext = - new InternalNexusOperationContext("namespace", "taskQueue", "endpoint", null, null); - temporalContext.setRequestId("request-id"); - CurrentNexusOperationContext.set(temporalContext); - + initializeTemporalContext(); TemporalInterceptorMiddleware middleware = new TemporalInterceptorMiddleware(new WorkerInterceptor[0]); middleware.intercept( operationContext, OperationHandler.sync((context, details, input) -> input)); } + + private static void initializeTemporalContext() { + InternalNexusOperationContext temporalContext = + new InternalNexusOperationContext( + "namespace", "taskQueue", "endpoint", null, mock(WorkflowClient.class)); + temporalContext.setRequestId("request-id"); + CurrentNexusOperationContext.set(temporalContext); + } } From 3dba9a5f29a84f19334f141e8a6682e6277dd213 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Tue, 10 Nov 2026 15:57:25 -0800 Subject: [PATCH 10/20] test: clarify interceptor header regression test name --- .../internal/nexus/TemporalInterceptorMiddlewareTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java index 407b2604de..0ab8a4d104 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -24,7 +24,8 @@ public class TemporalInterceptorMiddlewareTest { @Test - public void interceptorReadsHeadersBeforeContextFreeHandler() throws OperationException { + public void headersRemainAccessibleToInterceptorWithContextFreeHandler() + throws OperationException { OperationContext operationContext = OperationContext.newBuilder() .setService("service") From d193c9644f8d84ff3a442217256a0be8ba434e55 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Tue, 10 Nov 2026 16:15:14 -0800 Subject: [PATCH 11/20] test: migrate Kotlin Temporal operation to context-free signature --- .../kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt b/temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt index a009138538..fee95f2cf8 100644 --- a/temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt +++ b/temporal-kotlin/src/test/kotlin/io/temporal/workflow/KotlinTemporalOperationTest.kt @@ -12,7 +12,6 @@ import io.temporal.common.converter.KotlinObjectMapperFactory import io.temporal.nexus.TemporalNexusClient import io.temporal.nexus.TemporalOperation import io.temporal.nexus.TemporalOperationResult -import io.temporal.nexus.TemporalOperationStartContext import io.temporal.testing.internal.SDKTestWorkflowRule import org.junit.Assert.assertEquals import org.junit.Rule @@ -43,7 +42,6 @@ class KotlinTemporalOperationTest { class KotlinSugarServiceImpl { @TemporalOperation fun greet( - ctx: TemporalOperationStartContext, client: TemporalNexusClient, input: String ): TemporalOperationResult { From 922d10b5dd93fa603ce19b288db253e08006c19c Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Tue, 10 Nov 2026 16:37:59 -0800 Subject: [PATCH 12/20] docs: update context-free Temporal handler examples and API comments --- .../io/temporal/nexus/NexusOperationInfo.java | 23 ++++++++++++++++--- .../temporal/nexus/TemporalNexusClient.java | 7 +++--- .../io/temporal/nexus/TemporalOperation.java | 8 ++++--- .../nexus/TemporalOperationHandler.java | 2 -- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index 8105d2dd6e..e9623c33ba 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -26,15 +26,32 @@ public interface NexusOperationInfo { */ String getEndpoint(); - // :>) add comment + /** + * @return Service name for the current Nexus request. + */ String getService(); - // :>) add comment + /** + * @return Operation name for the current Nexus request. + */ String getOperation(); - // :>) add comment + /** + * Returns the deadline for the current handler invocation. This is the time by which the handler + * method should complete, not the deadline for the Nexus Operation itself. + * + * @return Handler invocation deadline, or null if no deadline was provided. + */ @Nullable Instant getDeadline(); + /** + * Returns a read-only token for observing cancellation of the current handler invocation. This is + * separate from a request to cancel the Nexus Operation itself. + * + *

When cancellation is requested, {@link CancellationToken#throwIfCancellationRequested()} + * throws a {@link CancellationException} whose message contains the invocation cancellation + * reason. + */ CancellationToken getCancellationToken(); } diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java index bd26be9309..6f3496f6bb 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java @@ -15,14 +15,15 @@ * Temporal from within a Nexus operation handler. * *

Passed to {@link TemporalOperation}-annotated methods (and {@link - * TemporalOperationHandler.StartHandler} implementations) alongside the start context and input. + * TemporalOperationHandler.StartHandler} implementations) alongside the operation input. Access + * information about the current invocation through {@link Nexus#getInfo()}. * *

Example usage to start a workflow from an operation: * *

{@code
  * @TemporalOperation
  * public TemporalOperationResult startTransfer(
- *     TemporalOperationStartContext ctx, TemporalNexusClient client, TransferInput input) {
+ *     TemporalNexusClient client, TransferInput input) {
  *   return client.startWorkflow(
  *       TransferWorkflow.class,
  *       TransferWorkflow::transfer,
@@ -39,7 +40,7 @@
  * 
{@code
  * @TemporalOperation
  * public TemporalOperationResult cancelOrder(
- *     TemporalOperationStartContext ctx, TemporalNexusClient client, CancelOrderInput input) {
+ *     TemporalNexusClient client, CancelOrderInput input) {
  *   client.getWorkflowClient()
  *       .newUntypedWorkflowStub("order-" + input.getOrderId())
  *       .signal("requestCancellation", input);
diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java
index a6fa9f5199..3f4c1dfcdd 100644
--- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java
+++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java
@@ -18,8 +18,7 @@
  *
  * 
    *
  • be {@code public}, - *
  • accept exactly three parameters: {@link TemporalOperationStartContext}, {@link - * TemporalNexusClient}, and the operation input type, + *
  • accept exactly two parameters: {@link TemporalNexusClient} and the operation input type, *
  • return {@link TemporalOperationResult}. *
* @@ -30,7 +29,7 @@ * public class TransferServiceImpl { * @TemporalOperation * public TemporalOperationResult transfer( - * TemporalOperationStartContext ctx, TemporalNexusClient client, TransferInput input) { + * TemporalNexusClient client, TransferInput input) { * return client.startWorkflow( * TransferWorkflow.class, * TransferWorkflow::transfer, @@ -42,6 +41,9 @@ * } * }
* + *

Access information about the current invocation through {@link Nexus#getInfo()}, including its + * deadline and cancellation token. + * *

For custom cancel, or any other handler composition, use {@link OperationImpl} with a {@link * TemporalOperationHandler} subclass that overrides {@link * TemporalOperationHandler#cancelWorkflowRun}. Both annotations can coexist on the same {@link diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java index 179c15a323..6cfdffaf90 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java @@ -91,8 +91,6 @@ public final OperationStartResult start( TemporalNexusClient client = new TemporalNexusClientImpl(nexusCtx.getWorkflowClient(), ctx, details); - // :<) remove - // TemporalOperationStartContext startContext = new TemporalOperationStartContext(ctx, details); TemporalOperationResult result = startHandler.apply(client, input); if (result.isSync()) { From 13e0b6f7f24e9f96fb15467ce05033873f981d12 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Thu, 12 Nov 2026 10:11:24 -0800 Subject: [PATCH 13/20] docs: add changelog entry for context-free Temporal handlers --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9153295fe1..32d4d7b5fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,12 @@ to docs, or any other relevant information. ## [1.41.0] - 2026-11-16 ### :boom: Breaking Changes +- **Experimental**: Temporal operation start handlers, including `@TemporalOperation` methods, now + accept only `(TemporalNexusClient, input)`. Cancellation overrides now accept only their typed + input, and `TemporalOperationCancelContext` has been removed. Use `Nexus.getInfo()` for service, + operation, handler deadline, and a read-only handler invocation cancellation token. Request headers + remain available to interceptors; request IDs, callbacks, and Nexus links are no longer exposed + through the Temporal handler context parameters. - `WorkflowOutboundCallsInterceptor` has new `sleep(Duration, TimerOptions)` and `await(Duration, TimerOptions, String, Supplier)` methods. Implementations that don't extend `WorkflowOutboundCallsInterceptorBase` need to add them, and Base subclasses override them to see calls with options. From b78bec7d375b09863d2fba502ec3b48c9ba05f93 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Thu, 12 Nov 2026 15:09:10 -0800 Subject: [PATCH 14/20] Remove deadline from Nexus operation info --- CHANGELOG.md | 2 +- .../io/temporal/internal/nexus/NexusInfoImpl.java | 10 ---------- .../nexus/TemporalInterceptorMiddleware.java | 1 - .../io/temporal/nexus/NexusOperationInfo.java | 11 ----------- .../java/io/temporal/nexus/TemporalOperation.java | 2 +- .../nexus/TemporalInterceptorMiddlewareTest.java | 15 --------------- 6 files changed, 2 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32d4d7b5fa..229549b0b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,7 +53,7 @@ to docs, or any other relevant information. - **Experimental**: Temporal operation start handlers, including `@TemporalOperation` methods, now accept only `(TemporalNexusClient, input)`. Cancellation overrides now accept only their typed input, and `TemporalOperationCancelContext` has been removed. Use `Nexus.getInfo()` for service, - operation, handler deadline, and a read-only handler invocation cancellation token. Request headers + operation, and a read-only handler invocation cancellation token. Request headers remain available to interceptors; request IDs, callbacks, and Nexus links are no longer exposed through the Temporal handler context parameters. - `WorkflowOutboundCallsInterceptor` has new `sleep(Duration, TimerOptions)` and diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java index 4e177e0ce2..a9648ab8b4 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java @@ -2,9 +2,7 @@ import io.temporal.common.CancellationToken; import io.temporal.nexus.NexusOperationInfo; -import java.time.Instant; import java.util.concurrent.CancellationException; -import javax.annotation.Nullable; class NexusInfoImpl implements NexusOperationInfo { private final String namespace; @@ -12,7 +10,6 @@ class NexusInfoImpl implements NexusOperationInfo { private final String endpoint; private final String service; private final String operation; - private final @Nullable Instant deadline; private final CancellationToken cancellationToken; NexusInfoImpl( @@ -21,14 +18,12 @@ class NexusInfoImpl implements NexusOperationInfo { String endpoint, String service, String operation, - @Nullable Instant deadline, CancellationToken cancellationToken) { this.namespace = namespace; this.taskQueue = taskQueue; this.endpoint = endpoint; this.service = service; this.operation = operation; - this.deadline = deadline; this.cancellationToken = cancellationToken; } @@ -57,11 +52,6 @@ public String getOperation() { return operation; } - @Override - public @Nullable Instant getDeadline() { - return deadline; - } - @Override public CancellationToken getCancellationToken() { return cancellationToken; diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java index 11e1b7e071..459a14f067 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java @@ -39,7 +39,6 @@ public OperationHandler intercept( temporalNexusContext.getEndpoint(), context.getService(), context.getOperation(), - context.getDeadline(), cancellationSource.token()))); return new OperationInterceptorConverter(inboundCallsInterceptor); } diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index e9623c33ba..b742726d1d 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -1,9 +1,7 @@ package io.temporal.nexus; import io.temporal.common.CancellationToken; -import java.time.Instant; import java.util.concurrent.CancellationException; -import javax.annotation.Nullable; /** * Temporal information about the Nexus Operation. Use {@link Nexus#getInfo()} from a Nexus @@ -36,15 +34,6 @@ public interface NexusOperationInfo { */ String getOperation(); - /** - * Returns the deadline for the current handler invocation. This is the time by which the handler - * method should complete, not the deadline for the Nexus Operation itself. - * - * @return Handler invocation deadline, or null if no deadline was provided. - */ - @Nullable - Instant getDeadline(); - /** * Returns a read-only token for observing cancellation of the current handler invocation. This is * separate from a request to cancel the Nexus Operation itself. diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java index 3f4c1dfcdd..78d4a91ddd 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java @@ -42,7 +42,7 @@ * }

* *

Access information about the current invocation through {@link Nexus#getInfo()}, including its - * deadline and cancellation token. + * cancellation token. * *

For custom cancel, or any other handler composition, use {@link OperationImpl} with a {@link * TemporalOperationHandler} subclass that overrides {@link diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java index 0ab8a4d104..39c1069d9c 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -15,7 +15,6 @@ import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; -import java.time.Instant; import java.util.concurrent.CancellationException; import org.junit.After; import org.junit.Assert; @@ -106,20 +105,6 @@ public void cancellationBeforeInterceptionReachesNexusOperationInfo() { Assert.assertEquals("early reason", exception.getMessage()); } - @Test - public void deadlineReachesNexusOperationInfo() { - Instant deadline = Instant.parse("2030-01-02T03:04:05Z"); - OperationContext operationContext = - OperationContext.newBuilder() - .setService("service") - .setOperation("operation") - .setDeadline(deadline) - .build(); - installOperationContext(operationContext); - - Assert.assertEquals(deadline, Nexus.getInfo().getDeadline()); - } - private static void installOperationContext(OperationContext operationContext) { initializeTemporalContext(); TemporalInterceptorMiddleware middleware = From 682d658a313a504f88e76fa66d96310067ee6053 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Fri, 13 Nov 2026 14:55:36 -0800 Subject: [PATCH 15/20] Remove obsolete start context and strengthen handler tests --- CHANGELOG.md | 6 +- .../nexus/TemporalOperationStartContext.java | 117 ------------------ .../TemporalInterceptorMiddlewareTest.java | 10 +- .../nexus/TemporalOperationProcessorTest.java | 3 +- .../nexus/AsyncActivityOperationTest.java | 7 +- .../CancelActivityAsyncOperationTest.java | 13 +- .../TemporalOperationAnnotationTest.java | 11 +- 7 files changed, 36 insertions(+), 131 deletions(-) delete mode 100644 temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationStartContext.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 229549b0b7..ca19f4bc04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,9 +52,9 @@ to docs, or any other relevant information. ### :boom: Breaking Changes - **Experimental**: Temporal operation start handlers, including `@TemporalOperation` methods, now accept only `(TemporalNexusClient, input)`. Cancellation overrides now accept only their typed - input, and `TemporalOperationCancelContext` has been removed. Use `Nexus.getInfo()` for service, - operation, and a read-only handler invocation cancellation token. Request headers - remain available to interceptors; request IDs, callbacks, and Nexus links are no longer exposed + input. `TemporalOperationStartContext` and `TemporalOperationCancelContext` have been removed. + Use `Nexus.getInfo()` for service, operation, and a read-only handler invocation cancellation token. + Request headers remain available to interceptors; request IDs, callbacks, and Nexus links are no longer exposed through the Temporal handler context parameters. - `WorkflowOutboundCallsInterceptor` has new `sleep(Duration, TimerOptions)` and `await(Duration, TimerOptions, String, Supplier)` methods. Implementations that don't extend diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationStartContext.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationStartContext.java deleted file mode 100644 index 493ccd5071..0000000000 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationStartContext.java +++ /dev/null @@ -1,117 +0,0 @@ -package io.temporal.nexus; - -import io.nexusrpc.Link; -import io.nexusrpc.handler.OperationContext; -import io.nexusrpc.handler.OperationMethodCancellationListener; -import io.nexusrpc.handler.OperationStartDetails; -import io.temporal.common.Experimental; -import java.time.Instant; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import org.jspecify.annotations.Nullable; - -/** - * Context for a Nexus start operation request, passed to {@link - * TemporalOperationHandler.StartHandler} alongside the {@link TemporalNexusClient} and input. - */ -@Experimental -public final class TemporalOperationStartContext { - - private final OperationContext operationContext; - private final OperationStartDetails operationStartDetails; - - TemporalOperationStartContext( - OperationContext operationContext, OperationStartDetails operationStartDetails) { - this.operationContext = Objects.requireNonNull(operationContext); - this.operationStartDetails = Objects.requireNonNull(operationStartDetails); - } - - /** Returns the service name for this operation. */ - public String getService() { - return operationContext.getService(); - } - - /** Returns the operation name. */ - public String getOperation() { - return operationContext.getOperation(); - } - - /** Returns the headers for this operation request. The returned map is case-insensitive. */ - public Map getHeaders() { - return operationContext.getHeaders(); - } - - /** - * Returns the deadline for the operation handler method. This is the time by which the method - * should complete. This is not the operation's deadline. - */ - public @Nullable Instant getDeadline() { - return operationContext.getDeadline(); - } - - /** Returns the request ID for this operation. */ - public String getRequestId() { - return operationStartDetails.getRequestId(); - } - - /** - * Optional callback URL for asynchronous operations to deliver results to. If present and the - * implementation is asynchronous, the implementation should ensure this callback is invoked with - * the result upon completion. - */ - public @Nullable String getCallbackUrl() { - return operationStartDetails.getCallbackUrl(); - } - - /** Headers to use on the callback if {@link #getCallbackUrl} is used. */ - public Map getCallbackHeaders() { - return operationStartDetails.getCallbackHeaders(); - } - - /** Links sent by the caller. Handlers may use these as metadata on associated resources. */ - public List getLinks() { - return operationStartDetails.getLinks(); - } - - /** - * True if the handler method has been cancelled. Note, this is method cancellation, unrelated to - * operation cancellation. - */ - public boolean isMethodCancelled() { - return operationContext.isMethodCancelled(); - } - - /** - * Reason the handler method was cancelled, or null if not cancelled. Note, this is method - * cancellation, unrelated to operation cancellation. - */ - public @Nullable String getMethodCancellationReason() { - return operationContext.getMethodCancellationReason(); - } - - /** - * Add a listener for method cancellation. The listener is invoked immediately before this method - * returns if the method is already cancelled. The listener must not block and must not be - * registered from within another cancellation listener. - */ - public void addMethodCancellationListener(OperationMethodCancellationListener listener) { - operationContext.addMethodCancellationListener(listener); - } - - /** - * Remove a method cancellation listener, if present. Must not be called from within another - * cancellation listener. - */ - public void removeMethodCancellationListener(OperationMethodCancellationListener listener) { - operationContext.removeMethodCancellationListener(listener); - } - - /** - * Associates links with the current operation to be propagated back to the caller. Links are only - * attached on successful responses. - */ - public void addResponseLinks(Link... links) { - operationContext.addLinks(links); - } -} diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java index 39c1069d9c..6b3e3d2557 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -22,6 +22,11 @@ public class TemporalInterceptorMiddlewareTest { + @After + public void tearDown() { + CurrentNexusOperationContext.unset(); + } + @Test public void headersRemainAccessibleToInterceptorWithContextFreeHandler() throws OperationException { @@ -58,11 +63,6 @@ public NexusOperationInboundCallsInterceptor interceptNexusOperation( .getSyncResult()); } - @After - public void tearDown() { - CurrentNexusOperationContext.unset(); - } - @Test public void methodCancellationReachesNexusOperationInfo() { OperationMethodCanceller canceller = new OperationMethodCanceller(); diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java index f41b862dad..e2eb07c84d 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalOperationProcessorTest.java @@ -133,7 +133,8 @@ public void rejects_badReturnType() { public void rejects_badArity() { IllegalArgumentException e = assertInvalid(() -> TemporalOperationProcessor.process(new BadArity())); - Assert.assertTrue(e.getMessage(), e.getMessage().contains("TemporalNexusClient, I)")); + Assert.assertTrue( + e.getMessage(), e.getMessage().contains("must accept (TemporalNexusClient, I)")); } @Test diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java index 92a7530bb6..a41be7e44b 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java @@ -11,6 +11,7 @@ import io.temporal.internal.nexus.OperationToken; import io.temporal.internal.nexus.OperationTokenType; import io.temporal.internal.nexus.OperationTokenUtil; +import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; @@ -99,7 +100,11 @@ public OperationHandler operation() { TestActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + input) + .setId( + "act-" + + Nexus.getInfo().getOperation() + + "-" + + Nexus.getInfo().getEndpoint()) .setStartToCloseTimeout(Duration.ofSeconds(10)) .build())); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java index a1d6bcd3f3..b9551295ed 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java @@ -16,6 +16,7 @@ import io.temporal.failure.CanceledFailure; import io.temporal.failure.NexusOperationFailure; import io.temporal.nexus.CancelActivityExecutionInput; +import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; @@ -194,7 +195,11 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + input) + .setId( + "act-" + + Nexus.getInfo().getOperation() + + "-" + + Nexus.getInfo().getEndpoint()) .setStartToCloseTimeout(Duration.ofSeconds(60)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) @@ -248,7 +253,11 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-override-" + input) + .setId( + "act-override-" + + Nexus.getInfo().getOperation() + + "-" + + Nexus.getInfo().getEndpoint()) .setStartToCloseTimeout(Duration.ofSeconds(5)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java index 0451eb87ce..d240ecc239 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java @@ -6,6 +6,7 @@ import io.nexusrpc.handler.OperationImpl; import io.nexusrpc.handler.ServiceImpl; import io.temporal.client.WorkflowOptions; +import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalNexusClient; import io.temporal.nexus.TemporalOperation; import io.temporal.nexus.TemporalOperationHandler; @@ -144,7 +145,10 @@ public TemporalOperationResult start(TemporalNexusClient client, String SugarTargetWorkflow.class, SugarTargetWorkflow::run, input, - WorkflowOptions.newBuilder().setWorkflowId("sugar-" + input).build()); + WorkflowOptions.newBuilder() + .setWorkflowId( + "sugar-" + Nexus.getInfo().getOperation() + "-" + Nexus.getInfo().getEndpoint()) + .build()); } } @@ -164,7 +168,10 @@ public TemporalOperationResult workflowOp(TemporalNexusClient client, St SugarTargetWorkflow.class, SugarTargetWorkflow::run, input, - WorkflowOptions.newBuilder().setWorkflowId("mixed-" + input).build()); + WorkflowOptions.newBuilder() + .setWorkflowId( + "mixed-" + Nexus.getInfo().getOperation() + "-" + Nexus.getInfo().getEndpoint()) + .build()); } @OperationImpl From 118f3c4ab6eefa0951ddbb60d9228c3c729d2f90 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Fri, 13 Nov 2026 16:05:24 -0800 Subject: [PATCH 16/20] Test request timeout cancellation in a context-free handler --- .../nexus/NexusTaskHandlerImplTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java index 141e213fb1..b4e41118d6 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java @@ -17,15 +17,24 @@ import io.temporal.api.nexus.v1.StartOperationResponse; import io.temporal.api.workflowservice.v1.PollNexusTaskQueueResponse; import io.temporal.client.WorkflowClient; +import io.temporal.common.CancellationToken; import io.temporal.common.converter.DataConverter; import io.temporal.common.converter.DefaultDataConverter; import io.temporal.common.interceptors.WorkerInterceptor; import io.temporal.common.reporter.TestStatsReporter; import io.temporal.internal.worker.NexusTask; import io.temporal.internal.worker.NexusTaskHandler; +import io.temporal.nexus.Nexus; +import io.temporal.nexus.TemporalNexusClient; +import io.temporal.nexus.TemporalOperation; +import io.temporal.nexus.TemporalOperationResult; import io.temporal.workflow.shared.TestNexusServices; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import javax.annotation.Nullable; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -43,6 +52,13 @@ public void setUp() { metricsScope = new RootScopeBuilder().reporter(reporter).reportEvery(Duration.ofMillis(10)); } + @After + public void tearDown() { + // Timed-out tasks currently skip context cleanup. Keep the timeout tests isolated until that + // separate production issue is fixed. + CurrentNexusOperationContext.unset(); + } + @Test public void nexusTaskHandlerImplStartNoService() { WorkflowClient client = mock(WorkflowClient.class); @@ -159,6 +175,36 @@ public void syncTimeoutTask() { () -> nexusTaskHandlerImpl.handle(new NexusTask(task, null, null), metricsScope)); } + @Test + public void requestTimeoutReachesContextFreeHandlerCancellationToken() { + ContextFreeCancellationService service = new ContextFreeCancellationService(); + NexusTaskHandlerImpl handler = + new NexusTaskHandlerImpl( + mock(WorkflowClient.class), + NAMESPACE, + TASK_QUEUE, + dataConverter, + new WorkerInterceptor[] {}); + handler.registerNexusServiceImplementations(new Object[] {service}); + handler.start(); + PollNexusTaskQueueResponse.Builder task = + PollNexusTaskQueueResponse.newBuilder() + .setRequest( + Request.newBuilder() + .putHeader(Header.REQUEST_TIMEOUT, "100ms") + .setStartOperation( + StartOperationRequest.newBuilder() + .setService("TestNexusService1") + .setOperation("operation") + .setPayload(dataConverter.toPayload("input").get()))); + + Assert.assertThrows( + TimeoutException.class, + () -> handler.handle(new NexusTask(task, null, null), metricsScope)); + Assert.assertNotNull("handler should observe the request timeout", service.cancellation); + Assert.assertEquals("timeout", service.cancellation.getMessage()); + } + @Test public void startTaskWithUndeserializableInput() throws TimeoutException { WorkflowClient client = mock(WorkflowClient.class); @@ -343,6 +389,31 @@ public void failureResponseDropsCapturedResponseLinks() throws TimeoutException response.hasSyncSuccess() || response.hasAsyncSuccess()); } + @ServiceImpl(service = TestNexusServices.TestNexusService1.class) + public static class ContextFreeCancellationService { + private CancellationException cancellation; + + @TemporalOperation + public TemporalOperationResult operation(TemporalNexusClient client, String input) { + CancellationToken token = Nexus.getInfo().getCancellationToken(); + CountDownLatch cancelled = new CountDownLatch(1); + try (CancellationToken.Registration ignored = token.onCancel(cancelled::countDown)) { + if (!cancelled.await(5, TimeUnit.SECONDS)) { + throw new IllegalStateException("Handler did not observe invocation cancellation"); + } + try { + token.throwIfCancellationRequested(); + } catch (CancellationException e) { + cancellation = e; + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw Nexus.wrap(e); + } + return TemporalOperationResult.sync(input); + } + } + /** * Handler that simulates what a real Nexus operation would do after issuing a signal: stash a * response link on the operation context, then return an async result. Lets us exercise the From bb10b281246a4d212233561471a53b629b7621aa Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Mon, 16 Nov 2026 16:25:26 -0800 Subject: [PATCH 17/20] Move context refactor changelog entry to Unreleased --- CHANGELOG.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca19f4bc04..4415a2630f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,12 @@ to docs, or any other relevant information. `upsertTypedSearchAttributes(UpsertSearchAttributesOptions, ...)`, and `upsertMemo(Map, UpsertMemoOptions)` methods. Implementations that don't extend `WorkflowOutboundCallsInterceptorBase` need to add them, and Base subclasses override them to see calls with options. +- **Experimental**: Temporal operation start handlers, including `@TemporalOperation` methods, now + accept only `(TemporalNexusClient, input)`. Cancellation overrides now accept only their typed + input. `TemporalOperationStartContext` and `TemporalOperationCancelContext` have been removed. + Use `Nexus.getInfo()` for service, operation, and a read-only handler invocation cancellation token. + Request headers remain available to interceptors; request IDs, callbacks, and Nexus links are no longer exposed + through the Temporal handler context parameters. ### Added - Experimental Event Groups associate related workflow commands so their history events can be @@ -50,12 +56,6 @@ to docs, or any other relevant information. ## [1.41.0] - 2026-11-16 ### :boom: Breaking Changes -- **Experimental**: Temporal operation start handlers, including `@TemporalOperation` methods, now - accept only `(TemporalNexusClient, input)`. Cancellation overrides now accept only their typed - input. `TemporalOperationStartContext` and `TemporalOperationCancelContext` have been removed. - Use `Nexus.getInfo()` for service, operation, and a read-only handler invocation cancellation token. - Request headers remain available to interceptors; request IDs, callbacks, and Nexus links are no longer exposed - through the Temporal handler context parameters. - `WorkflowOutboundCallsInterceptor` has new `sleep(Duration, TimerOptions)` and `await(Duration, TimerOptions, String, Supplier)` methods. Implementations that don't extend `WorkflowOutboundCallsInterceptorBase` need to add them, and Base subclasses override them to see calls with options. From c03bf5fd1aa02c314ab283e378054913d39fe61e Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Wed, 18 Nov 2026 12:17:50 -0800 Subject: [PATCH 18/20] Expose Nexus start request IDs through operation info Give Temporal operation handlers the incoming request ID through Nexus.getInfo().getRequestId() so they can deduplicate side effects on retry. Return null for cancel handlers and starts without an ID. Test that start handlers receive the exact ID for each invocation, retries reuse it, and cancel handlers receive null. Restore Workflow and Activity test fixture IDs derived from the start request ID. Assert the captured header after the handler call so a skipped interceptor fails the test. Share setup for the tests that cancel before and after interception. --- CHANGELOG.md | 7 ++- .../internal/nexus/NexusInfoImpl.java | 9 +++ .../nexus/TemporalInterceptorMiddleware.java | 2 + .../io/temporal/nexus/NexusOperationInfo.java | 12 ++++ .../io/temporal/nexus/TemporalOperation.java | 2 +- .../nexus/NexusTaskHandlerImplTest.java | 45 ++++++++++++++ .../TemporalInterceptorMiddlewareTest.java | 60 +++++++++---------- .../nexus/AsyncActivityOperationTest.java | 6 +- .../CancelActivityAsyncOperationTest.java | 24 ++++---- ...GenericHandlerSignalDeduplicationTest.java | 20 ++++--- .../TemporalOperationAnnotationTest.java | 6 +- 11 files changed, 125 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4415a2630f..6e448bf733 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,9 +27,10 @@ to docs, or any other relevant information. - **Experimental**: Temporal operation start handlers, including `@TemporalOperation` methods, now accept only `(TemporalNexusClient, input)`. Cancellation overrides now accept only their typed input. `TemporalOperationStartContext` and `TemporalOperationCancelContext` have been removed. - Use `Nexus.getInfo()` for service, operation, and a read-only handler invocation cancellation token. - Request headers remain available to interceptors; request IDs, callbacks, and Nexus links are no longer exposed - through the Temporal handler context parameters. + Use `Nexus.getInfo()` for service, operation, a read-only handler invocation cancellation token, + and the start request ID (`getRequestId()`, which returns `null` in cancel handlers). + Request headers remain available to interceptors. Callbacks and Nexus links are no longer exposed + to Temporal operation handlers. ### Added - Experimental Event Groups associate related workflow commands so their history events can be diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java index a9648ab8b4..6f853c4213 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInfoImpl.java @@ -3,6 +3,7 @@ import io.temporal.common.CancellationToken; import io.temporal.nexus.NexusOperationInfo; import java.util.concurrent.CancellationException; +import javax.annotation.Nullable; class NexusInfoImpl implements NexusOperationInfo { private final String namespace; @@ -10,6 +11,7 @@ class NexusInfoImpl implements NexusOperationInfo { private final String endpoint; private final String service; private final String operation; + private final @Nullable String requestId; private final CancellationToken cancellationToken; NexusInfoImpl( @@ -18,12 +20,14 @@ class NexusInfoImpl implements NexusOperationInfo { String endpoint, String service, String operation, + @Nullable String requestId, CancellationToken cancellationToken) { this.namespace = namespace; this.taskQueue = taskQueue; this.endpoint = endpoint; this.service = service; this.operation = operation; + this.requestId = requestId; this.cancellationToken = cancellationToken; } @@ -52,6 +56,11 @@ public String getOperation() { return operation; } + @Override + public @Nullable String getRequestId() { + return requestId; + } + @Override public CancellationToken getCancellationToken() { return cancellationToken; diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java index 459a14f067..055d713d32 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java @@ -1,5 +1,6 @@ package io.temporal.internal.nexus; +import com.google.common.base.Strings; import io.nexusrpc.OperationException; import io.nexusrpc.handler.*; import io.temporal.common.interceptors.NexusOperationInboundCallsInterceptor; @@ -39,6 +40,7 @@ public OperationHandler intercept( temporalNexusContext.getEndpoint(), context.getService(), context.getOperation(), + Strings.emptyToNull(temporalNexusContext.getRequestId()), cancellationSource.token()))); return new OperationInterceptorConverter(inboundCallsInterceptor); } diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index b742726d1d..7be94ea354 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -2,6 +2,7 @@ import io.temporal.common.CancellationToken; import java.util.concurrent.CancellationException; +import javax.annotation.Nullable; /** * Temporal information about the Nexus Operation. Use {@link Nexus#getInfo()} from a Nexus @@ -34,6 +35,17 @@ public interface NexusOperationInfo { */ String getOperation(); + /** + * Returns the ID of the current Nexus start request, or {@code null} when no ID is available (for + * example, in a cancel handler). + * + *

Retries of the same start request carry the same ID. Treat the ID as an opaque string. + */ + @Nullable + default String getRequestId() { + return null; + } + /** * Returns a read-only token for observing cancellation of the current handler invocation. This is * separate from a request to cancel the Nexus Operation itself. diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java index 78d4a91ddd..d103ec9c3d 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java @@ -42,7 +42,7 @@ * } * *

Access information about the current invocation through {@link Nexus#getInfo()}, including its - * cancellation token. + * cancellation token and {@linkplain NexusOperationInfo#getRequestId() start request ID}. * *

For custom cancel, or any other handler composition, use {@link OperationImpl} with a {@link * TemporalOperationHandler} subclass that overrides {@link diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java index b4e41118d6..44925e104a 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java @@ -121,6 +121,51 @@ public void startSyncTask() throws TimeoutException { String.class)); } + @Test + public void startRequestIdReachesContextFreeHandler() throws TimeoutException { + NexusTaskHandlerImpl handler = + new NexusTaskHandlerImpl( + mock(WorkflowClient.class), + NAMESPACE, + TASK_QUEUE, + dataConverter, + new WorkerInterceptor[] {}); + handler.registerNexusServiceImplementations(new Object[] {new RequestIdService()}); + handler.start(); + + Assert.assertEquals("opaque/id-1", requestIdSeenByHandler(handler, "opaque/id-1")); + Assert.assertEquals("opaque/id-2", requestIdSeenByHandler(handler, "opaque/id-2")); + Assert.assertNull(requestIdSeenByHandler(handler, "")); + } + + private String requestIdSeenByHandler(NexusTaskHandlerImpl handler, String requestId) + throws TimeoutException { + PollNexusTaskQueueResponse.Builder task = + PollNexusTaskQueueResponse.newBuilder() + .setRequest( + Request.newBuilder() + .setStartOperation( + StartOperationRequest.newBuilder() + .setService("TestNexusService1") + .setOperation("operation") + .setRequestId(requestId) + .setPayload(dataConverter.toPayload("input").get()))); + NexusTaskHandler.Result result = handler.handle(new NexusTask(task, null, null), metricsScope); + Assert.assertNull(result.getHandlerException()); + StartOperationResponse response = result.getResponse().getStartOperation(); + Assert.assertTrue("expected a successful start response", response.hasSyncSuccess()); + return dataConverter.fromPayload( + response.getSyncSuccess().getPayload(), String.class, String.class); + } + + @ServiceImpl(service = TestNexusServices.TestNexusService1.class) + public static class RequestIdService { + @TemporalOperation + public TemporalOperationResult operation(TemporalNexusClient client, String input) { + return TemporalOperationResult.sync(Nexus.getInfo().getRequestId()); + } + } + @Test public void nexusOperationContextExposesWorkerWorkflowClient() throws TimeoutException { WorkflowClient client = mock(WorkflowClient.class); diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java index 6b3e3d2557..2ea40871fb 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -16,12 +16,21 @@ import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; import java.util.concurrent.CancellationException; +import java.util.concurrent.atomic.AtomicReference; import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; public class TemporalInterceptorMiddlewareTest { + @Before + public void setUp() { + CurrentNexusOperationContext.set( + new InternalNexusOperationContext( + "namespace", "taskQueue", "endpoint", null, mock(WorkflowClient.class))); + } + @After public void tearDown() { CurrentNexusOperationContext.unset(); @@ -36,16 +45,16 @@ public void headersRemainAccessibleToInterceptorWithContextFreeHandler() .setOperation("operation") .putHeader("test-header", "test-value") .build(); + AtomicReference interceptedHeader = new AtomicReference<>(); WorkerInterceptor interceptor = new WorkerInterceptorBase() { @Override public NexusOperationInboundCallsInterceptor interceptNexusOperation( OperationContext context, NexusOperationInboundCallsInterceptor next) { - Assert.assertEquals("test-value", context.getHeaders().get("test-header")); + interceptedHeader.set(context.getHeaders().get("test-header")); return next; } }; - initializeTemporalContext(); OperationHandler handler = new TemporalInterceptorMiddleware(new WorkerInterceptor[] {interceptor}) .intercept( @@ -53,28 +62,22 @@ public NexusOperationInboundCallsInterceptor interceptNexusOperation( TemporalOperationHandler.create( (client, input) -> TemporalOperationResult.sync(input))); - Assert.assertEquals( - "input", + Object result = handler .start( operationContext, OperationStartDetails.newBuilder().setRequestId("request-id").build(), "input") - .getSyncResult()); + .getSyncResult(); + Assert.assertEquals("input", result); + Assert.assertEquals( + "worker interceptor should see the request header", "test-value", interceptedHeader.get()); } @Test public void methodCancellationReachesNexusOperationInfo() { OperationMethodCanceller canceller = new OperationMethodCanceller(); - OperationContext operationContext = - OperationContext.newBuilder() - .setService("service") - .setOperation("operation") - .setMethodCanceller(canceller) - .build(); - installOperationContext(operationContext); - - CancellationToken token = Nexus.getInfo().getCancellationToken(); + CancellationToken token = interceptAndGetCancellationToken(canceller); Assert.assertFalse(token.isCancellationRequested()); canceller.cancel("test reason"); @@ -89,15 +92,7 @@ public void methodCancellationReachesNexusOperationInfo() { public void cancellationBeforeInterceptionReachesNexusOperationInfo() { OperationMethodCanceller canceller = new OperationMethodCanceller(); canceller.cancel("early reason"); - OperationContext operationContext = - OperationContext.newBuilder() - .setService("service") - .setOperation("operation") - .setMethodCanceller(canceller) - .build(); - installOperationContext(operationContext); - - CancellationToken token = Nexus.getInfo().getCancellationToken(); + CancellationToken token = interceptAndGetCancellationToken(canceller); Assert.assertTrue(token.isCancellationRequested()); CancellationException exception = @@ -105,19 +100,18 @@ public void cancellationBeforeInterceptionReachesNexusOperationInfo() { Assert.assertEquals("early reason", exception.getMessage()); } - private static void installOperationContext(OperationContext operationContext) { - initializeTemporalContext(); + private static CancellationToken interceptAndGetCancellationToken( + OperationMethodCanceller canceller) { + OperationContext operationContext = + OperationContext.newBuilder() + .setService("service") + .setOperation("operation") + .setMethodCanceller(canceller) + .build(); TemporalInterceptorMiddleware middleware = new TemporalInterceptorMiddleware(new WorkerInterceptor[0]); middleware.intercept( operationContext, OperationHandler.sync((context, details, input) -> input)); - } - - private static void initializeTemporalContext() { - InternalNexusOperationContext temporalContext = - new InternalNexusOperationContext( - "namespace", "taskQueue", "endpoint", null, mock(WorkflowClient.class)); - temporalContext.setRequestId("request-id"); - CurrentNexusOperationContext.set(temporalContext); + return Nexus.getInfo().getCancellationToken(); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java index a41be7e44b..d1e1faeb4e 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java @@ -100,11 +100,7 @@ public OperationHandler operation() { TestActivity::process, input, StartActivityOptions.newBuilder() - .setId( - "act-" - + Nexus.getInfo().getOperation() - + "-" - + Nexus.getInfo().getEndpoint()) + .setId("act-" + Nexus.getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(10)) .build())); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java index b9551295ed..8a0586c18a 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java @@ -17,6 +17,7 @@ import io.temporal.failure.NexusOperationFailure; import io.temporal.nexus.CancelActivityExecutionInput; import io.temporal.nexus.Nexus; +import io.temporal.nexus.NexusOperationInfo; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.testing.CloudTestExclusion.RequiresCloudProvisioning; import io.temporal.testing.CloudTestExclusionNote; @@ -24,6 +25,7 @@ import io.temporal.workflow.*; import java.time.Duration; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -35,7 +37,7 @@ public class CancelActivityAsyncOperationTest extends BaseNexusTest { static final AtomicBoolean cancelled = new AtomicBoolean(false); - static final AtomicBoolean customCancelInvoked = new AtomicBoolean(false); + static final AtomicReference customCancelInfo = new AtomicReference<>(); @Rule public SDKTestWorkflowRule testWorkflowRule = @@ -54,7 +56,7 @@ protected SDKTestWorkflowRule getTestWorkflowRule() { @Before public void resetState() { cancelled.set(false); - customCancelInvoked.set(false); + customCancelInfo.set(null); } @Test(timeout = 60_000) @@ -92,7 +94,9 @@ public void testOverriddenActivityCancel() { // Acceptable. } // The overriding handler should have been called. - Assert.assertTrue("override should have been invoked", customCancelInvoked.get()); + NexusOperationInfo info = customCancelInfo.get(); + Assert.assertNotNull("override should have been invoked", info); + Assert.assertNull("cancel handler should have no start request ID", info.getRequestId()); // Default cancel was not invoked: activity ran until startToCloseTimeout fired // (no ActivityCompletionException raised on the worker). Assert.assertFalse("default cancel should not have been invoked", cancelled.get()); @@ -195,11 +199,7 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId( - "act-" - + Nexus.getInfo().getOperation() - + "-" - + Nexus.getInfo().getEndpoint()) + .setId("act-" + Nexus.getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(60)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) @@ -253,18 +253,14 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId( - "act-override-" - + Nexus.getInfo().getOperation() - + "-" - + Nexus.getInfo().getEndpoint()) + .setId("act-override-" + Nexus.getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(5)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) .build())) { @Override protected void cancelActivityExecution(CancelActivityExecutionInput input) { - customCancelInvoked.set(true); + customCancelInfo.set(Nexus.getInfo()); // Intentionally do NOT invoke default cancel; the activity will self-terminate via // startToCloseTimeout. } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java index 2c3399901e..6059ae8d92 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java @@ -18,8 +18,9 @@ import io.temporal.testing.internal.SDKTestWorkflowRule; import io.temporal.workflow.*; import java.time.Duration; +import java.util.List; import java.util.UUID; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.CopyOnWriteArrayList; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -35,13 +36,13 @@ public class GenericHandlerSignalDeduplicationTest { private static final String ONE_SIGNAL_MESSAGE = "Only one signal can be sent per Nexus operation invocation."; - private final AtomicInteger retryAttempts = new AtomicInteger(); + private final List retryRequestIds = new CopyOnWriteArrayList<>(); @Rule public SDKTestWorkflowRule testWorkflowRule = SDKTestWorkflowRule.newBuilder() .setWorkflowTypes(CallerWorkflowImpl.class, CountSignalsWorkflowImpl.class) - .setNexusServiceImplementation(new TestNexusServiceImpl(retryAttempts)) + .setNexusServiceImplementation(new TestNexusServiceImpl(retryRequestIds)) .build(); @Test @@ -73,7 +74,9 @@ public void signalDedupedAcrossHandlerRetry() { Assert.assertEquals("signaled-" + targetId, newCaller().execute("signalRetry", targetId)); Assert.assertEquals( - "handler should fail once and then be redelivered once", 2, retryAttempts.get()); + "handler should fail once and then be redelivered once", 2, retryRequestIds.size()); + Assert.assertNotNull("the handler should receive a request ID", retryRequestIds.get(0)); + Assert.assertEquals(retryRequestIds.get(0), retryRequestIds.get(1)); CountSignalsWorkflow target = testWorkflowRule.getWorkflowClient().newWorkflowStub(CountSignalsWorkflow.class, targetId); @@ -162,10 +165,10 @@ public interface TestNexusService { @ServiceImpl(service = TestNexusService.class) public static class TestNexusServiceImpl { - private final AtomicInteger retryAttempts; + private final List retryRequestIds; - public TestNexusServiceImpl(AtomicInteger retryAttempts) { - this.retryAttempts = retryAttempts; + public TestNexusServiceImpl(List retryRequestIds) { + this.retryRequestIds = retryRequestIds; } @TemporalOperation @@ -202,7 +205,8 @@ public TemporalOperationResult signalRetry( .build()) .signalWithStart("mySignal", new Object[] {targetId}, new Object[] {}); - if (retryAttempts.incrementAndGet() == 1) { + retryRequestIds.add(Nexus.getInfo().getRequestId()); + if (retryRequestIds.size() == 1) { throw new HandlerException( HandlerException.ErrorType.INTERNAL, "force handler redelivery after signalling", diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java index d240ecc239..4dfd11c85d 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java @@ -146,8 +146,7 @@ public TemporalOperationResult start(TemporalNexusClient client, String SugarTargetWorkflow::run, input, WorkflowOptions.newBuilder() - .setWorkflowId( - "sugar-" + Nexus.getInfo().getOperation() + "-" + Nexus.getInfo().getEndpoint()) + .setWorkflowId("sugar-" + Nexus.getInfo().getRequestId()) .build()); } } @@ -169,8 +168,7 @@ public TemporalOperationResult workflowOp(TemporalNexusClient client, St SugarTargetWorkflow::run, input, WorkflowOptions.newBuilder() - .setWorkflowId( - "mixed-" + Nexus.getInfo().getOperation() + "-" + Nexus.getInfo().getEndpoint()) + .setWorkflowId("mixed-" + Nexus.getInfo().getRequestId()) .build()); } From acb17d0626e081faad691cd4681cf881e26d5870 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Wed, 18 Nov 2026 14:45:08 -0800 Subject: [PATCH 19/20] Remove the Nexus operation info shortcut Use the existing Nexus operation context accessor for operation info. Update Javadocs, release notes, and tests to use that access path. --- CHANGELOG.md | 5 +++-- .../src/main/java/io/temporal/nexus/Nexus.java | 10 ---------- .../java/io/temporal/nexus/NexusOperationInfo.java | 4 ++-- .../java/io/temporal/nexus/TemporalNexusClient.java | 3 ++- .../main/java/io/temporal/nexus/TemporalOperation.java | 5 +++-- .../io/temporal/nexus/TemporalOperationHandler.java | 2 +- .../client/nexus/StandaloneNexusSignalLinkingTest.java | 2 +- .../internal/nexus/NexusTaskHandlerImplTest.java | 5 +++-- .../nexus/TemporalInterceptorMiddlewareTest.java | 2 +- .../workflow/nexus/ActivityOperationLinkingTest.java | 4 ++-- .../workflow/nexus/AsyncActivityOperationTest.java | 2 +- .../nexus/CancelActivityAsyncOperationTest.java | 6 +++--- .../workflow/nexus/GenericHandlerCancelTest.java | 2 +- .../workflow/nexus/GenericHandlerDoubleStartTest.java | 2 +- .../nexus/GenericHandlerSignalDeduplicationTest.java | 4 ++-- .../workflow/nexus/GenericHandlerTypedProcTest.java | 2 +- .../nexus/GenericHandlerTypedStartWorkflowTest.java | 2 +- .../nexus/GenericHandlerUntypedStartWorkflowTest.java | 2 +- .../workflow/nexus/NexusOperationInfoTest.java | 2 +- .../NexusSerializationContextPropagationTest.java | 2 +- .../nexus/TemporalOperationAnnotationTest.java | 4 ++-- 21 files changed, 33 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e448bf733..b1a84aaf47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,8 +27,9 @@ to docs, or any other relevant information. - **Experimental**: Temporal operation start handlers, including `@TemporalOperation` methods, now accept only `(TemporalNexusClient, input)`. Cancellation overrides now accept only their typed input. `TemporalOperationStartContext` and `TemporalOperationCancelContext` have been removed. - Use `Nexus.getInfo()` for service, operation, a read-only handler invocation cancellation token, - and the start request ID (`getRequestId()`, which returns `null` in cancel handlers). + Use `Nexus.getOperationContext().getInfo()` for service, operation, a read-only handler + invocation cancellation token, and the start request ID (`getRequestId()`, which returns `null` + in cancel handlers). Request headers remain available to interceptors. Callbacks and Nexus links are no longer exposed to Temporal operation handlers. diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/Nexus.java b/temporal-sdk/src/main/java/io/temporal/nexus/Nexus.java index 349d69a12c..1d4899b31e 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/Nexus.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/Nexus.java @@ -5,16 +5,6 @@ /** This class contains methods exposing Temporal APIs for Nexus Operations */ public final class Nexus { - /** - * Get information about the current Nexus Operation. - * - *

This static method relies on a thread-local variable and works only in the original Nexus - * thread. - */ - public static NexusOperationInfo getInfo() { - return getOperationContext().getInfo(); - } - /** * Can be used to get information about a Nexus Operation. This static method relies on a * thread-local variable and works only in the original Nexus thread. diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index 7be94ea354..c8824903d9 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -5,8 +5,8 @@ import javax.annotation.Nullable; /** - * Temporal information about the Nexus Operation. Use {@link Nexus#getInfo()} from a Nexus - * Operation implementation to access. + * Temporal information about the Nexus Operation. Use {@link NexusOperationContext#getInfo() + * Nexus.getOperationContext().getInfo()} from a Nexus Operation implementation to access. */ public interface NexusOperationInfo { /** diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java index 6f3496f6bb..37d7bf7c53 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalNexusClient.java @@ -16,7 +16,8 @@ * *

Passed to {@link TemporalOperation}-annotated methods (and {@link * TemporalOperationHandler.StartHandler} implementations) alongside the operation input. Access - * information about the current invocation through {@link Nexus#getInfo()}. + * information about the current invocation through {@link NexusOperationContext#getInfo() + * Nexus.getOperationContext().getInfo()}. * *

Example usage to start a workflow from an operation: * diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java index d103ec9c3d..70ffa34571 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperation.java @@ -41,8 +41,9 @@ * } * } * - *

Access information about the current invocation through {@link Nexus#getInfo()}, including its - * cancellation token and {@linkplain NexusOperationInfo#getRequestId() start request ID}. + *

Access information about the current invocation through {@link NexusOperationContext#getInfo() + * Nexus.getOperationContext().getInfo()}, including its cancellation token and {@linkplain + * NexusOperationInfo#getRequestId() start request ID}. * *

For custom cancel, or any other handler composition, use {@link OperationImpl} with a {@link * TemporalOperationHandler} subclass that overrides {@link diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java index 6cfdffaf90..4f99468216 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/TemporalOperationHandler.java @@ -22,7 +22,7 @@ * {@link #cancelActivityExecution} to change how the corresponding cancellation is handled. The * {@link #start} and {@link #cancel} methods should not be overridden — they contain the core * dispatch logic. Operation information is available to both start and cancel handlers through - * {@link Nexus#getInfo()}. + * {@link NexusOperationContext#getInfo() Nexus.getOperationContext().getInfo()}. * *

Custom-cancel example: * diff --git a/temporal-sdk/src/test/java/io/temporal/client/nexus/StandaloneNexusSignalLinkingTest.java b/temporal-sdk/src/test/java/io/temporal/client/nexus/StandaloneNexusSignalLinkingTest.java index e0837d77ad..00587b802e 100644 --- a/temporal-sdk/src/test/java/io/temporal/client/nexus/StandaloneNexusSignalLinkingTest.java +++ b/temporal-sdk/src/test/java/io/temporal/client/nexus/StandaloneNexusSignalLinkingTest.java @@ -176,7 +176,7 @@ public OperationHandler operation() { io.nexusrpc.handler.OperationStartDetails details, String input) -> { WorkflowClient client = Nexus.getOperationContext().getWorkflowClient(); - String tq = Nexus.getInfo().getTaskQueue(); + String tq = Nexus.getOperationContext().getInfo().getTaskQueue(); SanoSignalCalleeWorkflow startStub = client.newWorkflowStub( SanoSignalCalleeWorkflow.class, diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java index 44925e104a..c026f35fbb 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/NexusTaskHandlerImplTest.java @@ -162,7 +162,7 @@ private String requestIdSeenByHandler(NexusTaskHandlerImpl handler, String reque public static class RequestIdService { @TemporalOperation public TemporalOperationResult operation(TemporalNexusClient client, String input) { - return TemporalOperationResult.sync(Nexus.getInfo().getRequestId()); + return TemporalOperationResult.sync(Nexus.getOperationContext().getInfo().getRequestId()); } } @@ -440,7 +440,8 @@ public static class ContextFreeCancellationService { @TemporalOperation public TemporalOperationResult operation(TemporalNexusClient client, String input) { - CancellationToken token = Nexus.getInfo().getCancellationToken(); + CancellationToken token = + Nexus.getOperationContext().getInfo().getCancellationToken(); CountDownLatch cancelled = new CountDownLatch(1); try (CancellationToken.Registration ignored = token.onCancel(cancelled::countDown)) { if (!cancelled.await(5, TimeUnit.SECONDS)) { diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java index 2ea40871fb..82032d46c8 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -112,6 +112,6 @@ private static CancellationToken interceptAndGetCancellat new TemporalInterceptorMiddleware(new WorkerInterceptor[0]); middleware.intercept( operationContext, OperationHandler.sync((context, details, input) -> input)); - return Nexus.getInfo().getCancellationToken(); + return Nexus.getOperationContext().getInfo().getCancellationToken(); } } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityOperationLinkingTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityOperationLinkingTest.java index 49381b729a..9b2fbe123f 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityOperationLinkingTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/ActivityOperationLinkingTest.java @@ -165,9 +165,9 @@ public OperationHandler operation() { ActivityClient.newInstance( Nexus.getOperationContext().getWorkflowClient().getWorkflowServiceStubs(), ActivityClientOptions.newBuilder() - .setNamespace(Nexus.getInfo().getNamespace()) + .setNamespace(Nexus.getOperationContext().getInfo().getNamespace()) .build()); - String taskQueue = Nexus.getInfo().getTaskQueue(); + String taskQueue = Nexus.getOperationContext().getInfo().getTaskQueue(); ActivityHandle first = activityClient.start( diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java index d1e1faeb4e..932b3d0a38 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/AsyncActivityOperationTest.java @@ -100,7 +100,7 @@ public OperationHandler operation() { TestActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + Nexus.getInfo().getRequestId()) + .setId("act-" + Nexus.getOperationContext().getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(10)) .build())); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java index 8a0586c18a..12bdb813bc 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/CancelActivityAsyncOperationTest.java @@ -199,7 +199,7 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-" + Nexus.getInfo().getRequestId()) + .setId("act-" + Nexus.getOperationContext().getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(60)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) @@ -253,14 +253,14 @@ public OperationHandler operation() { HeartbeatingActivity::process, input, StartActivityOptions.newBuilder() - .setId("act-override-" + Nexus.getInfo().getRequestId()) + .setId("act-override-" + Nexus.getOperationContext().getInfo().getRequestId()) .setStartToCloseTimeout(Duration.ofSeconds(5)) .setHeartbeatTimeout(Duration.ofSeconds(2)) .setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build()) .build())) { @Override protected void cancelActivityExecution(CancelActivityExecutionInput input) { - customCancelInfo.set(Nexus.getInfo()); + customCancelInfo.set(Nexus.getOperationContext().getInfo()); // Intentionally do NOT invoke default cancel; the activity will self-terminate via // startToCloseTimeout. } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java index d2f7a18ace..fc039b6abc 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerCancelTest.java @@ -130,7 +130,7 @@ public interface TestNexusCancelService { public class TestNexusServiceImpl { @TemporalOperation public TemporalOperationResult operation(TemporalNexusClient client, String input) { - NexusOperationInfo info = Nexus.getInfo(); + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); return client.startWorkflow( WaitForCancelWorkflowInterface.class, WaitForCancelWorkflowInterface::execute, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java index 88eccd0794..390e32b738 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerDoubleStartTest.java @@ -104,7 +104,7 @@ public class TestNexusServiceImpl { @TemporalOperation public TemporalOperationResult operation(TemporalNexusClient client, String input) { // First start should succeed but the workflow blocks indefinitely - NexusOperationInfo info = Nexus.getInfo(); + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); client.startWorkflow( BlockingWorkflow.class, BlockingWorkflow::execute, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java index 6059ae8d92..b49386fb71 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerSignalDeduplicationTest.java @@ -201,11 +201,11 @@ public TemporalOperationResult signalRetry( "CountSignalsWorkflow", WorkflowOptions.newBuilder() .setWorkflowId(targetId) - .setTaskQueue(Nexus.getInfo().getTaskQueue()) + .setTaskQueue(Nexus.getOperationContext().getInfo().getTaskQueue()) .build()) .signalWithStart("mySignal", new Object[] {targetId}, new Object[] {}); - retryRequestIds.add(Nexus.getInfo().getRequestId()); + retryRequestIds.add(Nexus.getOperationContext().getInfo().getRequestId()); if (retryRequestIds.size() == 1) { throw new HandlerException( HandlerException.ErrorType.INTERNAL, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java index 93ec457e30..9061f5825d 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedProcTest.java @@ -70,7 +70,7 @@ public class TestNexusServiceImpl { @TemporalOperation public TemporalOperationResult operation(TemporalNexusClient client, Integer input) { - NexusOperationInfo info = Nexus.getInfo(); + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); String prefix = "generic-handler-test-proc" + input + "-"; String workflowId = prefix + info.getService() + "-" + info.getOperation(); WorkflowOptions options = WorkflowOptions.newBuilder().setWorkflowId(workflowId).build(); diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java index 1cd05240fc..69defd154f 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerTypedStartWorkflowTest.java @@ -71,7 +71,7 @@ public class TestNexusServiceImpl { @TemporalOperation public TemporalOperationResult operation(TemporalNexusClient client, Integer input) { String prefix = "generic-handler-test-func" + input + "-"; - NexusOperationInfo info = Nexus.getInfo(); + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); String workflowId = prefix + info.getService() + "-" + info.getOperation(); WorkflowOptions options = WorkflowOptions.newBuilder().setWorkflowId(workflowId).build(); diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java index 359a568708..aeab9133e9 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/GenericHandlerUntypedStartWorkflowTest.java @@ -66,7 +66,7 @@ public interface TestNexusServiceUntyped { public class TestNexusServiceImpl { @TemporalOperation public TemporalOperationResult operation(TemporalNexusClient client, String input) { - NexusOperationInfo info = Nexus.getInfo(); + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); return client.startWorkflow( "func1", String.class, diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusOperationInfoTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusOperationInfoTest.java index 8276cbcf0e..aeb639c9f1 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusOperationInfoTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusOperationInfoTest.java @@ -52,7 +52,7 @@ public static class TestNexusServiceImpl { public OperationHandler operation() { return OperationHandler.sync( (context, details, input) -> { - NexusOperationInfo info = Nexus.getInfo(); + NexusOperationInfo info = Nexus.getOperationContext().getInfo(); return info.getNamespace() + ":" + info.getTaskQueue() + ":" + info.getEndpoint(); }); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java index 064ba9e210..24ef842c1e 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/NexusSerializationContextPropagationTest.java @@ -464,7 +464,7 @@ public OperationHandler startWorkflow() { .setMemo(Collections.singletonMap("started", input)) .setStaticSummary("summary-" + input) .setStaticDetails("details-" + input) - .setTaskQueue(Nexus.getInfo().getTaskQueue()) + .setTaskQueue(Nexus.getOperationContext().getInfo().getTaskQueue()) .build()) ::execute); } diff --git a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java index 4dfd11c85d..6aa9700bae 100644 --- a/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java +++ b/temporal-sdk/src/test/java/io/temporal/workflow/nexus/TemporalOperationAnnotationTest.java @@ -146,7 +146,7 @@ public TemporalOperationResult start(TemporalNexusClient client, String SugarTargetWorkflow::run, input, WorkflowOptions.newBuilder() - .setWorkflowId("sugar-" + Nexus.getInfo().getRequestId()) + .setWorkflowId("sugar-" + Nexus.getOperationContext().getInfo().getRequestId()) .build()); } } @@ -168,7 +168,7 @@ public TemporalOperationResult workflowOp(TemporalNexusClient client, St SugarTargetWorkflow::run, input, WorkflowOptions.newBuilder() - .setWorkflowId("mixed-" + Nexus.getInfo().getRequestId()) + .setWorkflowId("mixed-" + Nexus.getOperationContext().getInfo().getRequestId()) .build()); } From 29102bb96c1dbd4becf00a96df6c001351f09e0a Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Wed, 18 Nov 2026 16:35:28 -0800 Subject: [PATCH 20/20] Document Nexus cancellation callback threading --- .../src/main/java/io/temporal/nexus/NexusOperationInfo.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java index c8824903d9..34e3a5a098 100644 --- a/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java +++ b/temporal-sdk/src/main/java/io/temporal/nexus/NexusOperationInfo.java @@ -53,6 +53,8 @@ default String getRequestId() { *

When cancellation is requested, {@link CancellationToken#throwIfCancellationRequested()} * throws a {@link CancellationException} whose message contains the invocation cancellation * reason. + * + *

Cancellation callbacks may run on the Nexus timeout thread and must return promptly. */ CancellationToken getCancellationToken(); }