Skip to content

Commit 7f744d1

Browse files
authored
Fix: Modify agent_id field to agent_id_log for logging (#4762)
* fix: modify agent_id field to agent_id_log Signed-off-by: Pavan Yekbote <pybot@amazon.com> * fix: use the right variable for PER executor Signed-off-by: Pavan Yekbote <pybot@amazon.com> * fix: v2 agent id log & add test cases for agenttool Signed-off-by: Pavan Yekbote <pybot@amazon.com> * fix: use chat agent in IT Signed-off-by: Pavan Yekbote <pybot@amazon.com> * modify region for it Signed-off-by: Pavan Yekbote <pybot@amazon.com> --------- Signed-off-by: Pavan Yekbote <pybot@amazon.com>
1 parent 9fb971d commit 7f744d1

File tree

12 files changed

+408
-34
lines changed

12 files changed

+408
-34
lines changed

common/src/main/java/org/opensearch/ml/common/CommonValue.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public class CommonValue {
2323

2424
/** The field name containing the tenant id */
2525
public static final String TENANT_ID_FIELD = "tenant_id";
26-
public static final String AGENT_ID_FIELD = "agent_id";
26+
/**
27+
* Internal parameter key for propagating the executing agent's ID through the parameter map
28+
* for logging. Uses a distinct key to avoid colliding with the "agent_id" tool parameter
29+
* used by AgentTool to specify which sub-agent to invoke.
30+
*/
31+
public static final String AGENT_ID_LOG_FIELD = "agent_id_log";
2732

2833
public static final String MASTER_KEY = "master_key";
2934
public static final String CREATE_TIME_FIELD = "create_time";

common/src/test/java/org/opensearch/ml/common/utils/ToolUtilsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ public void testBuildToolParameters_WithNullToolSpecParameters() {
172172
assertEquals("test_tenant", result.get(TENANT_ID_FIELD));
173173
}
174174

175+
@Test
176+
public void testBuildToolParameters_AgentIdLogDoesNotOverrideAgentId() {
177+
Map<String, String> parameters = new HashMap<>();
178+
parameters.put("question", "test question");
179+
MLToolSpec toolSpec = MLToolSpec.builder().type("AgentTool").parameters(Map.of("agent_id", "configured-sub-agent-id")).build();
180+
Map<String, String> result = ToolUtils.buildToolParameters(parameters, toolSpec, "test_tenant");
181+
assertEquals("configured-sub-agent-id", result.get("agent_id"));
182+
}
183+
175184
@Test
176185
public void testFilterToolOutput_NoFiltering() {
177186
// Create a simple object

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/AbstractV2AgentRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package org.opensearch.ml.engine.algorithms.agent;
77

8-
import static org.opensearch.ml.common.CommonValue.AGENT_ID_FIELD;
8+
import static org.opensearch.ml.common.CommonValue.AGENT_ID_LOG_FIELD;
99

1010
import java.util.ArrayList;
1111
import java.util.HashMap;
@@ -550,7 +550,7 @@ public final void runV2(
550550
Memory memory,
551551
List<Message> fullConversation
552552
) {
553-
String agentId = params.get(AGENT_ID_FIELD);
553+
String agentId = params.get(AGENT_ID_LOG_FIELD);
554554
String tenantId = mlAgent.getTenantId();
555555
log.info("Starting Agent with {} messages. agentId={}, tenantId={}", fullConversation.size(), agentId, tenantId);
556556

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLAGUIAgentRunner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package org.opensearch.ml.engine.algorithms.agent;
77

8-
import static org.opensearch.ml.common.CommonValue.AGENT_ID_FIELD;
8+
import static org.opensearch.ml.common.CommonValue.AGENT_ID_LOG_FIELD;
99
import static org.opensearch.ml.common.CommonValue.ENDPOINT_FIELD;
1010
import static org.opensearch.ml.common.agui.AGUIConstants.AGUI_PARAM_LOAD_CHAT_HISTORY;
1111
import static org.opensearch.ml.engine.algorithms.agent.AgentUtils.createMemoryParams;
@@ -129,7 +129,7 @@ public void run(MLAgent mlAgent, Map<String, String> params, ActionListener<Obje
129129
log
130130
.error(
131131
"Error starting AG-UI agent execution. agentId={}, tenantId={}",
132-
params.get(AGENT_ID_FIELD),
132+
params.get(AGENT_ID_LOG_FIELD),
133133
mlAgent.getTenantId(),
134134
e
135135
);
@@ -177,7 +177,7 @@ private void handleHistoryLoad(MLAgent mlAgent, Map<String, String> params, Acti
177177
log
178178
.error(
179179
"Failed to load history for AGUI snapshot. agentId={}, tenantId={}",
180-
params.get(AGENT_ID_FIELD),
180+
params.get(AGENT_ID_LOG_FIELD),
181181
mlAgent.getTenantId(),
182182
e
183183
);

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutor.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import static org.opensearch.common.xcontent.json.JsonXContent.jsonXContent;
99
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
10-
import static org.opensearch.ml.common.CommonValue.AGENT_ID_FIELD;
10+
import static org.opensearch.ml.common.CommonValue.AGENT_ID_LOG_FIELD;
1111
import static org.opensearch.ml.common.CommonValue.ENDPOINT_FIELD;
1212
import static org.opensearch.ml.common.CommonValue.MCP_CONNECTORS_FIELD;
1313
import static org.opensearch.ml.common.CommonValue.ML_AGENT_INDEX;
@@ -266,7 +266,7 @@ public void execute(Input input, ActionListener<Output> listener, TransportChann
266266
Map<String, String> requestParameters = inputDataSet.getParameters();
267267

268268
// Add agentId to parameters for logging in agent runners
269-
inputDataSet.getParameters().put(AGENT_ID_FIELD, agentId);
269+
inputDataSet.getParameters().put(AGENT_ID_LOG_FIELD, agentId);
270270

271271
mlAgent = applyMemoryContainerOverride(mlAgent, inputDataSet, agentId);
272272
final MLAgent finalMlAgent = mlAgent;
@@ -674,7 +674,7 @@ private void createParentInteractionAndExecute(
674674
.error(
675675
"Failed to regenerate for interaction {}. agentId={}, tenantId={}",
676676
regenerateInteractionId,
677-
inputDataSet.getParameters().get(AGENT_ID_FIELD),
677+
inputDataSet.getParameters().get(AGENT_ID_LOG_FIELD),
678678
tenantId,
679679
e
680680
);
@@ -703,7 +703,7 @@ private void createParentInteractionAndExecute(
703703
log
704704
.error(
705705
"Failed to create parent interaction. agentId={}, tenantId={}",
706-
inputDataSet.getParameters().get(AGENT_ID_FIELD),
706+
inputDataSet.getParameters().get(AGENT_ID_LOG_FIELD),
707707
tenantId,
708708
ex
709709
);
@@ -882,18 +882,24 @@ void performInitialMemoryOperations(
882882
log
883883
.error(
884884
"Failed during memory post-processing. agentId={}, tenantId={}",
885-
params.get(AGENT_ID_FIELD),
885+
params.get(AGENT_ID_LOG_FIELD),
886886
mlAgent.getTenantId(),
887887
ex
888888
);
889889
listener.onFailure(ex);
890890
}
891891
}, e -> {
892-
log.error("Failed to save input messages. agentId={}, tenantId={}", params.get(AGENT_ID_FIELD), mlAgent.getTenantId(), e);
892+
log
893+
.error(
894+
"Failed to save input messages. agentId={}, tenantId={}",
895+
params.get(AGENT_ID_LOG_FIELD),
896+
mlAgent.getTenantId(),
897+
e
898+
);
893899
listener.onFailure(e);
894900
}));
895901
}, e -> {
896-
log.error("Failed to get history. agentId={}, tenantId={}", params.get(AGENT_ID_FIELD), mlAgent.getTenantId(), e);
902+
log.error("Failed to get history. agentId={}, tenantId={}", params.get(AGENT_ID_LOG_FIELD), mlAgent.getTenantId(), e);
897903
listener.onFailure(e);
898904
}));
899905
}
@@ -967,7 +973,7 @@ private void executeAgent(
967973
ActionListener<Object> agentActionListener = createAsyncTaskUpdater(
968974
mlTask,
969975
mlAgent.getType(),
970-
inputDataSet.getParameters().get(AGENT_ID_FIELD),
976+
inputDataSet.getParameters().get(AGENT_ID_LOG_FIELD),
971977
tenantId,
972978
outputs,
973979
modelTensors,
@@ -995,7 +1001,7 @@ private void executeAgent(
9951001
log
9961002
.error(
9971003
"Failed to run agent. agentId={}, tenantId={}, statusCode={}",
998-
inputDataSet.getParameters().get(AGENT_ID_FIELD),
1004+
inputDataSet.getParameters().get(AGENT_ID_LOG_FIELD),
9991005
tenantId,
10001006
extractStatusCode(e),
10011007
e
@@ -1006,7 +1012,7 @@ private void executeAgent(
10061012
log
10071013
.error(
10081014
"Failed to create task for agent async execution. agentId={}, tenantId={}",
1009-
inputDataSet.getParameters().get(AGENT_ID_FIELD),
1015+
inputDataSet.getParameters().get(AGENT_ID_LOG_FIELD),
10101016
tenantId,
10111017
e
10121018
);
@@ -1018,7 +1024,7 @@ private void executeAgent(
10181024
outputs,
10191025
modelTensors,
10201026
mlAgent.getType(),
1021-
inputDataSet.getParameters().get(AGENT_ID_FIELD),
1027+
inputDataSet.getParameters().get(AGENT_ID_LOG_FIELD),
10221028
tenantId,
10231029
parentInteractionId,
10241030
memory
@@ -1043,7 +1049,7 @@ private void executeAgent(
10431049
log
10441050
.error(
10451051
"Failed to run agent. agentId={}, tenantId={}, statusCode={}",
1046-
inputDataSet.getParameters().get(AGENT_ID_FIELD),
1052+
inputDataSet.getParameters().get(AGENT_ID_LOG_FIELD),
10471053
tenantId,
10481054
extractStatusCode(e),
10491055
e
@@ -1073,7 +1079,7 @@ private void executeV2Agent(
10731079
HookRegistry hookRegistry,
10741080
List<Message> inputMessages
10751081
) {
1076-
String agentId = inputDataSet.getParameters().get(AGENT_ID_FIELD);
1082+
String agentId = inputDataSet.getParameters().get(AGENT_ID_LOG_FIELD);
10771083
Map<String, String> params = new HashMap<>(inputDataSet.getParameters());
10781084

10791085
// Get runner (routes to MLChatAgentRunnerV2Refactored for CONVERSATIONAL_V2)

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLChatAgentRunner.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package org.opensearch.ml.engine.algorithms.agent;
77

8-
import static org.opensearch.ml.common.CommonValue.AGENT_ID_FIELD;
8+
import static org.opensearch.ml.common.CommonValue.AGENT_ID_LOG_FIELD;
99
import static org.opensearch.ml.common.CommonValue.ENDPOINT_FIELD;
1010
import static org.opensearch.ml.common.CommonValue.TENANT_ID_FIELD;
1111
import static org.opensearch.ml.common.agui.AGUIConstants.AGUI_PARAM_ASSISTANT_TOOL_CALL_MESSAGES;
@@ -351,7 +351,7 @@ private void runWithMemory(
351351

352352
runAgent(mlAgent, params, listener, memory, functionCalling);
353353
}, e -> {
354-
log.error("Failed to get chat history. agentId={}, tenantId={}", params.get(AGENT_ID_FIELD), mlAgent.getTenantId(), e);
354+
log.error("Failed to get chat history. agentId={}, tenantId={}", params.get(AGENT_ID_LOG_FIELD), mlAgent.getTenantId(), e);
355355
listener.onFailure(e);
356356
}));
357357
}, listener::onFailure));
@@ -444,7 +444,7 @@ private void resolveModelMetadataAndRunReAct(
444444
FunctionCalling functionCalling,
445445
Map<String, Tool> backendTools
446446
) {
447-
String agentId = parameters.get(AGENT_ID_FIELD);
447+
String agentId = parameters.get(AGENT_ID_LOG_FIELD);
448448
String tenantId = mlAgent.getTenantId();
449449
log.info("Starting chat agent execution. agentId={}, tenantId={}", agentId, tenantId);
450450

@@ -512,7 +512,7 @@ private void runReAct(
512512
boolean usesUnifiedInterface,
513513
ModelProvider modelProvider
514514
) {
515-
String agentId = parameters.get(AGENT_ID_FIELD);
515+
String agentId = parameters.get(AGENT_ID_LOG_FIELD);
516516
LLMSpec llm = mlAgent.getLlm();
517517
String sessionId = memory != null ? memory.getId() : null;
518518

@@ -981,7 +981,7 @@ private static void runTool(
981981
.info(
982982
"Tool invoked. toolName={}, agentId={}, tenantId={}",
983983
action,
984-
tmpParameters.get(AGENT_ID_FIELD),
984+
tmpParameters.get(AGENT_ID_LOG_FIELD),
985985
tmpParameters.get(TENANT_ID_FIELD)
986986
);
987987
if (tools.get(action) instanceof MLModelTool) {
@@ -1003,7 +1003,7 @@ private static void runTool(
10031003
.error(
10041004
"Failed to run tool {}. agentId={}, tenantId={}, statusCode={}",
10051005
action,
1006-
tmpParameters.get(AGENT_ID_FIELD),
1006+
tmpParameters.get(AGENT_ID_LOG_FIELD),
10071007
tmpParameters.get(TENANT_ID_FIELD),
10081008
extractStatusCode(e),
10091009
e
@@ -1580,7 +1580,7 @@ void generateLLMSummary(
15801580
.error(
15811581
"Failed to invoke model in agent. modelId={}, agentId={}, tenantId={}, statusCode={}",
15821582
llmSpec.getModelId(),
1583-
summaryParams.get(AGENT_ID_FIELD),
1583+
summaryParams.get(AGENT_ID_LOG_FIELD),
15841584
tenantId,
15851585
AgentUtils.extractStatusCode(e),
15861586
e

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLChatAgentRunnerV2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package org.opensearch.ml.engine.algorithms.agent;
77

8-
import static org.opensearch.ml.common.CommonValue.AGENT_ID_FIELD;
8+
import static org.opensearch.ml.common.CommonValue.AGENT_ID_LOG_FIELD;
99

1010
import java.util.ArrayList;
1111
import java.util.HashMap;
@@ -79,7 +79,7 @@ protected void executeAgentLogic(
7979
ModelProvider modelProvider,
8080
ActionListener<AgentLogicResult> listener
8181
) {
82-
String agentId = params.get(AGENT_ID_FIELD);
82+
String agentId = params.get(AGENT_ID_LOG_FIELD);
8383
int maxIterations = getMaxIterations(params);
8484
// Create mutable list for ReAct iterations (will append tool results)
8585
List<Message> messages = new ArrayList<>(conversationHistory);
@@ -164,7 +164,7 @@ private void executeLLMCall(
164164
List<String> toolInteractionJsonList,
165165
ActionListener<AgentLogicResult> listener
166166
) {
167-
String agentId = params.get(AGENT_ID_FIELD);
167+
String agentId = params.get(AGENT_ID_LOG_FIELD);
168168
String tenantId = mlAgent.getTenantId();
169169
int currentIteration = iteration.getAndIncrement();
170170

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLPlanExecuteAndReflectAgentRunner.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
package org.opensearch.ml.engine.algorithms.agent;
77

8+
import static org.opensearch.ml.common.CommonValue.AGENT_ID_LOG_FIELD;
89
import static org.opensearch.ml.common.CommonValue.ENDPOINT_FIELD;
910
import static org.opensearch.ml.common.MLTask.STATE_FIELD;
1011
import static org.opensearch.ml.common.MLTask.TASK_ID_FIELD;
1112
import static org.opensearch.ml.common.conversation.ConversationalIndexConstants.INTERACTIONS_ADDITIONAL_INFO_FIELD;
1213
import static org.opensearch.ml.common.conversation.ConversationalIndexConstants.INTERACTIONS_INPUT_FIELD;
1314
import static org.opensearch.ml.common.conversation.ConversationalIndexConstants.INTERACTIONS_RESPONSE_FIELD;
14-
import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.AGENT_ID_FIELD;
1515
import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.MEMORY_CONTAINER_ID_FIELD;
1616
import static org.opensearch.ml.common.utils.MLTaskUtils.updateMLTaskDirectly;
1717
import static org.opensearch.ml.common.utils.StringUtils.isJson;
@@ -393,7 +393,13 @@ private void setupMemoryAndRun(
393393

394394
setToolsAndRunAgent(mlAgent, allParams, completedSteps, memory, memory.getId(), listener, tokenTracker, functionCalling);
395395
}, e -> {
396-
log.error("Failed to get chat history. agentId={}, tenantId={}", allParams.get(AGENT_ID_FIELD), mlAgent.getTenantId(), e);
396+
log
397+
.error(
398+
"Failed to get chat history. agentId={}, tenantId={}",
399+
allParams.get(AGENT_ID_LOG_FIELD),
400+
mlAgent.getTenantId(),
401+
e
402+
);
397403
listener.onFailure(e);
398404
}));
399405
}, listener::onFailure));
@@ -714,7 +720,7 @@ private void executePlanningLoop(
714720
tokenTracker
715721
);
716722
}, e -> {
717-
String agentId = allParams.getOrDefault("agent_id", "unknown");
723+
String agentId = allParams.getOrDefault(AGENT_ID_LOG_FIELD, "unknown");
718724
String tenantIdLog = allParams.get(TENANT_ID_FIELD);
719725
log.error("Failed to execute ReAct agent. agentId={}, tenantId={}", agentId, tenantIdLog, e);
720726
finalListener.onFailure(e);
@@ -725,7 +731,7 @@ private void executePlanningLoop(
725731
.error(
726732
"Failed to invoke model in agent. modelId={}, agentId={}, tenantId={}, statusCode={}",
727733
llm.getModelId(),
728-
allParams.get(AGENT_ID_FIELD),
734+
allParams.get(AGENT_ID_LOG_FIELD),
729735
allParams.get(TENANT_ID_FIELD),
730736
extractStatusCode(e),
731737
e
@@ -1074,7 +1080,7 @@ private void generateSummary(
10741080
.error(
10751081
"Failed to invoke model in agent. modelId={}, agentId={}, tenantId={}, statusCode={}",
10761082
llmSpec.getModelId(),
1077-
summaryParams.get(AGENT_ID_FIELD),
1083+
summaryParams.get(AGENT_ID_LOG_FIELD),
10781084
summaryParams.get(TENANT_ID_FIELD),
10791085
extractStatusCode(e),
10801086
e

0 commit comments

Comments
 (0)