Skip to content

Commit 5639df4

Browse files
authored
Feature/add hint message (#198)
* Validate approved SQL joins before execution * Wire AgentScope pre-execution risk checks
1 parent 1e8c366 commit 5639df4

9 files changed

Lines changed: 404 additions & 6 deletions

File tree

‎data-agent-backend/src/main/java/io/github/malonetalk/agent/AgentService.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import io.github.malonetalk.service.MetricClarificationService;
5656
import io.github.malonetalk.service.PermissionService;
5757
import io.github.malonetalk.service.SessionDatasourceService;
58+
import io.github.malonetalk.service.ToolRiskService;
5859
import io.github.malonetalk.utils.DateTimeUtils;
5960
import io.opentelemetry.context.Context;
6061
import io.opentelemetry.instrumentation.reactor.v3_1.ContextPropagationOperator;
@@ -93,6 +94,7 @@ public class AgentService {
9394
private final DataPermissionService dataPermissions;
9495
private final MetricClarificationService metricClarification;
9596
private final ChatExecutionService chatExecutions;
97+
private final ToolRiskService toolRiskService;
9698
private Toolkit toolkit;
9799
private SkillBox skillBox;
98100

@@ -338,6 +340,7 @@ private ReActAgent createAgent(ToolCallContext toolCallContext, Hook eventStream
338340
.toolkit(toolkit)
339341
.toolExecutionContext(context)
340342
.hook(new MetricClarificationHook(toolCallContext, metricClarification))
343+
.hook(new DangerousOperationHook(toolCallContext, toolRiskService))
341344
.hook(eventStream)
342345
.skillBox(skillBox)
343346
.memory(memory)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2026 github.com/MaloneTalk
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
* limitations under the License.
17+
*/
18+
package io.github.malonetalk.agent;
19+
20+
import io.agentscope.core.hook.Hook;
21+
import io.agentscope.core.hook.HookEvent;
22+
import io.agentscope.core.hook.PreActingEvent;
23+
import io.github.malonetalk.service.ToolRiskService;
24+
import lombok.RequiredArgsConstructor;
25+
import reactor.core.publisher.Mono;
26+
27+
/** Checks tool calls using the current chat's trusted execution context. */
28+
@RequiredArgsConstructor
29+
final class DangerousOperationHook implements Hook {
30+
private final ToolCallContext context;
31+
private final ToolRiskService riskService;
32+
33+
@Override
34+
public int priority() {
35+
// Validate after parameter-preprocessing hooks.
36+
return 1000;
37+
}
38+
39+
@Override
40+
public <T extends HookEvent> Mono<T> onEvent(T event) {
41+
return Mono.defer(
42+
() -> {
43+
if (event instanceof PreActingEvent acting) {
44+
var call = acting.getToolUse();
45+
riskService.requireAllowed(context, call.getName(), call.getInput());
46+
}
47+
return Mono.just(event);
48+
});
49+
}
50+
}

‎data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource/SqlExecutor.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.github.malonetalk.service.ChatExecutionService;
2828
import io.github.malonetalk.service.ChatExecutionService.Execution;
2929
import io.github.malonetalk.service.ChatExecutionService.QueryTask;
30+
import io.github.malonetalk.service.SqlJoinValidationService;
3031
import io.github.malonetalk.service.SqlPermissionService;
3132
import java.io.IOException;
3233
import java.sql.Connection;
@@ -65,6 +66,7 @@ public class SqlExecutor {
6566

6667
private final DynamicDataSourceManager dynamicDataSourceManager;
6768
private final SqlPermissionService sqlPermissions;
69+
private final SqlJoinValidationService joinValidation;
6870

6971
public SqlExecution executeDetailed(DatasourcePo datasource, String sql, ToolCallContext ctx) {
7072
var authorized = authorizeSelect(datasource, sql, ctx.userId());
@@ -98,16 +100,22 @@ static String truncate(String message) {
98100
}
99101

100102
public AuthorizedSqlBo authorizeSelect(DatasourcePo datasource, String sql, Integer userId) {
101-
return sqlPermissions.authorize(userId, datasource, parseAllowedSelect(sql));
103+
var authorized = sqlPermissions.authorize(userId, datasource, parseAllowedSelect(sql));
104+
// Authorization rewrites the AST; validate the original query only after access is checked.
105+
joinValidation.validate(datasource.getId(), parseAllowedSelect(sql));
106+
return authorized;
102107
}
103108

104109
public AuthorizedSqlBo authorizeSelect(
105110
DatasourcePo datasource,
106111
String sql,
107112
Integer userId,
108113
Map<String, Set<String>> previousColumns) {
109-
return sqlPermissions.authorize(
110-
userId, datasource, parseAllowedSelect(sql), previousColumns);
114+
var authorized =
115+
sqlPermissions.authorize(
116+
userId, datasource, parseAllowedSelect(sql), previousColumns);
117+
joinValidation.validate(datasource.getId(), parseAllowedSelect(sql));
118+
return authorized;
111119
}
112120

113121
public String validateSelectSql(String sql) {

‎data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/AgentToolInvoker.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.github.malonetalk.exception.ErrorCode;
2828
import io.github.malonetalk.model.bo.AgentToolDefinitionBo;
2929
import io.github.malonetalk.model.bo.ToolResultBo;
30+
import io.github.malonetalk.service.ToolRiskService;
3031
import jakarta.annotation.PostConstruct;
3132
import java.lang.reflect.InvocationTargetException;
3233
import java.lang.reflect.Method;
@@ -49,6 +50,7 @@ public class AgentToolInvoker {
4950

5051
private final List<DataAgentTool> tools;
5152
private final ObjectMapper objectMapper;
53+
private final ToolRiskService toolRiskService;
5254
private final Map<String, ExposedTool> exposed = new LinkedHashMap<>();
5355
private volatile List<AgentToolDefinitionBo> definitions = List.of();
5456

@@ -94,6 +96,7 @@ public ToolResultBo invoke(String name, Map<String, Object> input, ToolCallConte
9496
arguments[parameter.index()] = objectMapper.convertValue(value, javaType);
9597
}
9698

99+
toolRiskService.requireAllowed(context, name, input == null ? Map.of() : input);
97100
try {
98101
Object result = tool.method().invoke(tool.bean(), arguments);
99102
if (result instanceof ToolResultBo toolResult) {

0 commit comments

Comments
 (0)