Skip to content

feat(core): add global context map to Agent interface#1207

Closed
KFCFans wants to merge 4 commits into
agentscope-ai:mainfrom
KFCFans:feat/agent-context-map
Closed

feat(core): add global context map to Agent interface#1207
KFCFans wants to merge 4 commits into
agentscope-ai:mainfrom
KFCFans:feat/agent-context-map

Conversation

@KFCFans

@KFCFans KFCFans commented Apr 13, 2026

Copy link
Copy Markdown

AgentScope-Java Version

1.0.11

Description

Background

When building complex agent applications with hooks, there is currently no built-in mechanism for hooks and other components to share custom data throughout the agent execution lifecycle. For example, a PreReasoningEvent hook might need to pass contextual information (e.g., request metadata, tracing context, feature flags) to a PostActingEvent hook, but there is no standard way to do so without resorting to external state management like ThreadLocal or static maps, which are fragile in reactive/async environments.

Changes

This PR adds a getContext() method to the Agent interface, providing a mutable Map<String, Object> that is scoped to each agent instance. This allows hooks and other components to share custom data across the entire agent execution lifecycle in a thread-safe manner.

Specific changes:

  • Agent.java: Added Map<String, Object> getContext() method to the interface with Javadoc.
  • AgentBase.java: Added a ConcurrentHashMap-backed context field, initialized in the constructor. The getContext() method is declared final to ensure consistent behavior across all agent implementations.

Usage Example

// In a PreReasoning hook — store data
agent.getContext().put("request_id", requestId);
agent.getContext().put("start_time", System.currentTimeMillis());

// In a PostActing hook — retrieve data
String requestId = (String) agent.getContext().get("request_id");
long elapsed = System.currentTimeMillis() - (long) agent.getContext().get("start_time");

Design Decisions

  • ConcurrentHashMap: Chosen for thread safety since hooks may execute on different reactor threads.
  • final method: Prevents subclasses from overriding the context behavior, ensuring a single consistent implementation.
  • Instance-scoped: Each agent has its own context map, avoiding cross-agent interference.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

Provide a mutable Map<String, Object> context on the Agent interface,
allowing hooks and other components to share custom data throughout
the agent execution lifecycle.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@KFCFans KFCFans requested a review from a team April 13, 2026 04:13
@CLAassistant

CLAassistant commented Apr 13, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Add the missing getContext() implementation to the TestAgent stub
in JsonlTraceExporterTest, required after the new method was added
to the Agent interface.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented Apr 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Verify that the context map is non-null, initially empty, supports
put/get, and is isolated per agent instance.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@LearningGp

Copy link
Copy Markdown
Member
image

@KFCFans

KFCFans commented Apr 13, 2026

Copy link
Copy Markdown
Author
image

DONE,已经签了。
不过 commit 的账号是公司账号 @taobao.com,逻辑上好像天然也不存在问题?hhh

image

@LearningGp

Copy link
Copy Markdown
Member

PTAL @chickenlj

@chickenlj

Copy link
Copy Markdown
Collaborator

Please try RuntimeContext introduced in 1.1.0-RC1 release.

@chickenlj chickenlj closed this May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants