This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 339ab95ab fix(ai): stop auditing a dry-run tool call as an applied 
change (#5940)
339ab95ab is described below

commit 339ab95ab414b0d7127ee4f85a641f1fbf368450
Author: 烤化の初雪 <[email protected]>
AuthorDate: Fri Nov 27 12:17:31 2026 +0800

    fix(ai): stop auditing a dry-run tool call as an applied change (#5940)
---
 .../studio/ops/ai/tool/filter/ToolAuditFilter.java |  11 +-
 .../ops/ai/tool/filter/ToolDryRunAuditTest.java    | 118 +++++++++++++++++++++
 2 files changed, 128 insertions(+), 1 deletion(-)

diff --git 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/tool/filter/ToolAuditFilter.java
 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/tool/filter/ToolAuditFilter.java
index 52ba8fe49..e82318d2e 100644
--- 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/tool/filter/ToolAuditFilter.java
+++ 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/tool/filter/ToolAuditFilter.java
@@ -18,6 +18,7 @@ package org.apache.rocketmq.studio.ops.ai.tool.filter;
 
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.studio.ops.ai.tool.contract.common.MutationOutput;
 import org.apache.rocketmq.studio.ops.ai.tool.core.ToolExecutionContext;
 import org.apache.rocketmq.studio.ops.ai.tool.core.ToolInvocation;
 import org.apache.rocketmq.studio.ops.audit.AuditService;
@@ -35,6 +36,9 @@ public class ToolAuditFilter implements ToolExecutionFilter {
         return Type.AUDIT;
     }
 
+    /** Detail marker for a preview, so the row cannot be read as an applied 
change. */
+    private static final String DRY_RUN_DETAIL = "dry_run=true (preview only, 
nothing applied)";
+
     @Override
     public Object filter(ToolInvocation invocation, Chain chain) {
         ToolExecutionContext context = invocation.context();
@@ -45,7 +49,12 @@ public class ToolAuditFilter implements ToolExecutionFilter {
             this.record(context, "FAILED", exception.getMessage());
             throw exception;
         }
-        this.record(context, "SUCCESS", null);
+        // A dry run stops at the mutation filter's plan, so it performs none 
of the change the row's
+        // operation name implies; without the marker an auditor cannot tell 
"who deleted topic X"
+        // from "who previewed deleting it".
+        boolean planned = output instanceof MutationOutput<?> mutation
+                && mutation.status() == MutationOutput.Status.PLANNED;
+        this.record(context, "SUCCESS", planned ? DRY_RUN_DETAIL : null);
         return output;
     }
 
diff --git 
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/tool/filter/ToolDryRunAuditTest.java
 
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/tool/filter/ToolDryRunAuditTest.java
new file mode 100644
index 000000000..b674e8e5c
--- /dev/null
+++ 
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/tool/filter/ToolDryRunAuditTest.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.studio.ops.ai.tool.filter;
+
+import org.apache.rocketmq.studio.ops.ai.tool.contract.common.MutationOutput;
+import org.apache.rocketmq.studio.ops.ai.tool.contract.plan.ToolPlan;
+import org.apache.rocketmq.studio.ops.ai.tool.core.ToolDefinition;
+import org.apache.rocketmq.studio.ops.ai.tool.core.ToolExecutionContext;
+import org.apache.rocketmq.studio.ops.ai.tool.core.ToolInvocation;
+import org.apache.rocketmq.studio.ops.ai.tool.core.ToolRiskLevel;
+import org.apache.rocketmq.studio.ops.ai.tool.handler.MutationToolHandler;
+import org.apache.rocketmq.studio.ops.ai.tool.service.ToolTokenService;
+import org.apache.rocketmq.studio.ops.audit.AuditService;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The audit row for a tool call has to say whether the change was applied. A 
dry run returns the
+ * mutation filter's plan without touching anything, so recording it exactly 
like an execution made
+ * "who deleted topic X" indistinguishable from "who previewed deleting it".
+ */
+class ToolDryRunAuditTest {
+
+    private final AuditService audit = mock(AuditService.class);
+    private final ToolTokenService tokens = mock(ToolTokenService.class);
+    private final ToolAuditFilter auditFilter = new ToolAuditFilter(audit);
+    private final ToolMutationFilter mutationFilter = new 
ToolMutationFilter(tokens, true);
+    private final ToolFilterChain chain = new 
ToolFilterChain(List.of(mutationFilter, auditFilter));
+    private final TestHandler handler = new TestHandler();
+
+    @Test
+    void aPreviewIsAuditedAsSuchInsteadOfAsAnAppliedChangeTest() {
+        ToolExecutionContext context = context(Map.of("topic", "orders", 
"dry_run", true));
+        when(tokens.issue(context)).thenReturn("signed-request");
+
+        MutationOutput<?> output = (MutationOutput<?>) chain.execute(new 
ToolInvocation(context, handler));
+
+        assertThat(output.status()).isEqualTo(MutationOutput.Status.PLANNED);
+        assertThat(handler.executions).isZero();
+        verify(audit).record(any(), any(), any(), any(), any(),
+                org.mockito.ArgumentMatchers.eq("SUCCESS"));
+        org.mockito.ArgumentCaptor<String> detail = 
org.mockito.ArgumentCaptor.forClass(String.class);
+        verify(audit).record(any(), any(), any(), any(), detail.capture(), 
any());
+        assertThat(detail.getValue()).contains("dry_run=true");
+    }
+
+    @Test
+    void anAppliedChangeIsAuditedWithoutThePreviewMarkerTest() {
+        ToolExecutionContext context = context(Map.of("topic", "orders"));
+        org.mockito.Mockito.doNothing().when(tokens).verify(context);
+
+        MutationOutput<?> output = (MutationOutput<?>) chain.execute(new 
ToolInvocation(context, handler));
+
+        assertThat(output.status()).isEqualTo(MutationOutput.Status.EXECUTED);
+        assertThat(handler.executions).isEqualTo(1);
+        verify(audit).record(any(), any(), any(), any(), 
org.mockito.ArgumentMatchers.isNull(), any());
+    }
+
+    private static ToolExecutionContext context(Map<String, Object> input) {
+        ToolDefinition definition = new ToolDefinition("rmq.topic.update",
+                new ToolDefinition.Cli("topic", "update"), "Update topic", 
ToolRiskLevel.L2,
+                "topic:write", List.of(), Map.of(), Map.of(), null, false, 
null);
+        return ToolExecutionContext.of("instance-a", definition, input, 
"alice");
+    }
+
+    private record Input(String topic) {
+    }
+
+    private static class TestHandler extends MutationToolHandler<Input, 
Object> {
+        private int executions;
+
+        private TestHandler() {
+            super(Input.class);
+        }
+
+        @Override
+        public String name() {
+            return "rmq.topic.update";
+        }
+
+        @Override
+        public ToolPlan preview(Input input, ToolExecutionContext context) {
+            return ToolPlan.builder("Update topic")
+                    .before(Map.of("topic", input.topic()))
+                    .after(Map.of("topic", input.topic()))
+                    .impact("Updates the topic configuration.")
+                    .build();
+        }
+
+        @Override
+        public Object execute(Input input, ToolExecutionContext context) {
+            executions++;
+            return Map.of("topic", input.topic());
+        }
+    }
+}

Reply via email to