Skip to content

Conversation

@mingshl
Copy link
Collaborator

@mingshl mingshl commented Nov 3, 2025

Description

Add Complete Context Management Framework with Memory-Integrated Hook Events to ML Agents

This PR introduces a comprehensive context management system for ML agents, featuring memory-integrated hook
events, complete hook registry with fault-tolerant execution, enhanced context integration, and intelligent
resource management for enterprise-grade deployments.

Major Features:

  1. Complete Hook Registry System
    Fault-Tolerant Event Emission: emitEvent() with exception isolation and comprehensive logging
    Thread-Safe Operations: ConcurrentHashMap ensuring concurrent callback registration and execution
    Callback Management: Type-safe callback registration with monitoring capabilities
    Production-Grade Error Handling: Individual callback failures don't affect system operation

  2. Comprehensive Hook Event Architecture
    HookEvent Base Class: Abstract base with invocation state for all lifecycle events
    PreLLMEvent: pre llm event with context integration and retrieved re-act interactions, including tool used result/ completed steps
    EnhancedPostToolEvent: Tool event with context integration
    Event Hierarchy: Well-designed inheritance from HookEvent to specialized events

  3. Complete CRUD REST API Implementation
    Create Operations: MLCreateContextManagementTemplateAction, MLCreateContextManagementTemplateRequest,
    MLCreateContextManagementTemplateResponse
    Read Operations: MLGetContextManagementTemplateAction, MLGetContextManagementTemplateRequest,
    MLGetContextManagementTemplateResponse
    Delete Operations: MLDeleteContextManagementTemplateAction, MLDeleteContextManagementTemplateRequest,
    MLDeleteContextManagementTemplateResponse
    Full CRUD Lifecycle: Complete template management with create, read, and delete operations

  4. Context Management Integration
    ML Agent Integration: contextManagementName and contextManagement fields with validation
    Template System: ContextManagementTemplate with hooks, validation, and serialization
    Hook Provider Integration: ContextManagerHookProvider registers callbacks for all event types

  5. Rich Context State Management
    ContextManagerContext: Comprehensive context with prompts, history, tools, interactions, and parameters
    Utility Methods: getEstimatedTokenCount(), getMessageCount(), addParameter(), addToolInteraction()
    Memory Integration: Context managers can access both current context and retrieved memory
    Dynamic Modification: Runtime context updates with memory and invocation state tracking

  6. Event-Driven Execution Pipeline
    Tool Event Processing: handlePostTool() processes enhanced tool events with context
    Pre-LLM Processing: handlePreLLM() processes context before LLM execution
    Memory Event Processing: handlePostMemory() processes memory events with context and history
    Automatic Execution: All events processed automatically through hook registry

  7. Complete Activation Rules System
    TokensExceedRule: Context-aware activation using comprehensive token estimation
    MessageCountExceedRule: Conversation length management with memory integration
    CompositeActivationRule: Complex conditions considering memory and context state

  8. Token Counting Architecture
    TokenCounter Interface: Standardized interface with multiple truncation strategies
    Context Integration: Token counters integrated with activation rules and memory state
    Pluggable Design: Interface supports model-specific implementations

  9. Pluggable Context Manager Architecture
    ContextManagerConfig: Configuration with type, activation, and manager settings
    Template Management: Complete template system with memory-integrated hooks
    Registry Integration: Managers registered for memory, tool, and LLM events

Technical Highlights:
Comprehensive Event Model: Complete lifecycle coverage with memory, tool, and LLM events
Fault-Tolerant Processing: Exception isolation ensures system stability across all event types
Rich Context Access: Context managers have access to full context and memory

Architecture Benefits:
Complete Lifecycle Coverage: Events cover all major phases of ML agent execution
Integrated Memory Management: Memory retrieval and context management work together seamlessly
Enterprise Reliability: Fault-tolerant design with comprehensive error handling

Use Cases:
Comprehensive Resource Management: Manage tokens across current context and memory history

Impact:
Comprehensive Agent Lifecycle: Full coverage of memory, tool, and LLM execution phases
Advanced AI Orchestration: Sophisticated context management with memory integration

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@mingshl mingshl had a problem deploying to ml-commons-cicd-env November 3, 2025 19:21 — with GitHub Actions Failure
@mingshl mingshl had a problem deploying to ml-commons-cicd-env November 3, 2025 19:21 — with GitHub Actions Failure
@mingshl mingshl marked this pull request as ready for review November 3, 2025 20:17
@mingshl mingshl force-pushed the feature/context_manager_hooks_inline_create branch from caa6d57 to 4eaeb94 Compare November 4, 2025 23:35
@mingshl mingshl had a problem deploying to ml-commons-cicd-env November 4, 2025 23:37 — with GitHub Actions Failure
@mingshl mingshl had a problem deploying to ml-commons-cicd-env November 4, 2025 23:37 — with GitHub Actions Failure
@mingshl mingshl had a problem deploying to ml-commons-cicd-env November 4, 2025 23:41 — with GitHub Actions Failure
@mingshl mingshl had a problem deploying to ml-commons-cicd-env November 4, 2025 23:41 — with GitHub Actions Failure
@mingshl mingshl had a problem deploying to ml-commons-cicd-env November 18, 2025 00:23 — with GitHub Actions Failure
@mingshl mingshl had a problem deploying to ml-commons-cicd-env November 18, 2025 00:23 — with GitHub Actions Failure
* @param <T> The type of hook event
*/
public <T extends HookEvent> void addCallback(Class<T> eventType, HookCallback<T> callback) {
callbacks.computeIfAbsent(eventType, k -> new ArrayList<>()).add(callback);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callbacks.computeIfAbsent --> this is not thread-safe. Do you see any concern?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zhangxunmt can you explain your intention please?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any new Threads added in this PR. So iirc there's no multiple threads in agents registration which is the only trigger to add hooks. If this addCallback is never used in the agent execution we should be good. Please double check @mingshl .

Zhangxunmt and others added 13 commits November 18, 2025 11:54
Signed-off-by: Xun Zhang <xunzh@amazon.com>
…project#4345)

* initiate context management api with hook implementation

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

* apply spotless

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

---------

Signed-off-by: Mingshi Liu <mingshl@amazon.com>
* add pre_llm hook to per agent

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

change context management passing from query parameters to payload

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

pass hook registery into PER

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

apply spotless

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

initiate context management api with hook implementation

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

* add comment

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

* format

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

* add validation

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

---------

Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
…ing in index (opensearch-project#4403)

* allow inline create context management without storing in agent register

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

* make ML_COMMONS_MULTI_TENANCY_ENABLED default is false

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

---------

Signed-off-by: Mingshi Liu <mingshl@amazon.com>
…roject#4408)

* Fix POST_TOOL hook interaction updates and add tenant ID support
Signed-off-by: Mingshi Liu <mingshl@amazon.com>

- Fix POST_TOOL hook to return full ContextManagerContext like PRE_LLM hook
- Update MLChatAgentRunner to properly handle interaction updates from POST_TOOL hook
- Ensure interactions list and tmpParameters.INTERACTIONS stay synchronized
- Add tenant ID support to MLPredictionTaskRequest in ModelGuardrail and SummarizationManager

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

* fix error message escaping

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

* consolicate post_hook logic

Signed-off-by: Mingshi Liu <mingshl@amazon.com>

---------

Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
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.

3 participants