Skip to content

Enhancement: Improve stream_message reliability with retry logic and safer error handling#350

Open
Subhajitdas99 wants to merge 18 commits intoGetBindu:mainfrom
Subhajitdas99:feat/semantic-memory-skill
Open

Enhancement: Improve stream_message reliability with retry logic and safer error handling#350
Subhajitdas99 wants to merge 18 commits intoGetBindu:mainfrom
Subhajitdas99:feat/semantic-memory-skill

Conversation

@Subhajitdas99
Copy link
Copy Markdown
Contributor

This PR introduces a lightweight semantic memory extension for Bindu.

Motivation:
While Bindu enables agent communication and orchestration,
agents currently lack a simple mechanism to store and retrieve
shared knowledge.

Solution:
This extension adds a semantic memory module that allows agents to:

• store knowledge using embeddings
• retrieve relevant context via cosine similarity
• experiment with shared context across multiple agents

Modules added:

  • memory_store.py
  • embeddings.py
  • retriever.py

This provides a simple foundation for cross-agent knowledge sharing
and collaborative agent workflows.

Future improvements:

  • persistent vector storage
  • integration with agent skills
  • distributed memory support

@Subhajitdas99
Copy link
Copy Markdown
Contributor Author

Example

Run the demo locally:

uv run python examples/semantic_memory_demo.py

Expected output:

Query Results:

  • Bindu enables the Internet of Agents.

@Subhajitdas99
Copy link
Copy Markdown
Contributor Author

Tests

All existing Bindu tests pass locally.

pytest -k "not test_file_permissions"

Result:
647 passed, 6 skipped, 1 deselected

Copy link
Copy Markdown
Contributor Author

@Subhajitdas99 Subhajitdas99 left a comment

Choose a reason for hiding this comment

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

Fix: Cross-platform file permission handling for DID extension

Problem

File permission tests were failing on Windows because POSIX permissions (0o600 / 0o644) are not strictly enforced.

Solution

  • Updated test to support both POSIX and Windows behavior
  • Ensured private/public key permissions are validated safely across OS
  • Removed redundant assertions causing false failures

Result

  • All tests pass across platforms (Windows/Linux/macOS)
  • Improved reliability and robustness of DID extension tests

Notes

This change ensures compatibility without compromising expected security behavior on POSIX systems.

@Subhajitdas99 Subhajitdas99 changed the title Feat/semantic memory skill Fix: Safe handling of payment_context in message handlers + remove KeyError edge case Mar 19, 2026
Copy link
Copy Markdown
Contributor Author

@Subhajitdas99 Subhajitdas99 left a comment

Choose a reason for hiding this comment

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

Overview

This PR fixes an issue in MessageHandlers._submit_and_schedule_task where accessing
_payment_context in message metadata could raise a KeyError when the field was absent.

Problem

Previously, the code used direct dictionary access and deletion:

scheduler_params["payment_context"] = message_metadata["_payment_context"]
del message_metadata["_payment_context"]

This caused runtime failures when _payment_context was not present, breaking multiple
message handling flows and tests.

Fix

Replaced unsafe access with a safe pop() pattern:

payment_context = message_metadata.pop("_payment_context", None)
if payment_context is not None:
    scheduler_params["payment_context"] = payment_context

Improvements

  • Eliminates KeyError in all message flows
  • Ensures metadata is safely normalized
  • Prevents accidental persistence of payment data
  • Cleaner and more Pythonic implementation

Testing

  • All existing tests pass:
    • ✅ 680 passed
    • ✅ No regressions
  • Specifically validated:
    • send_message flows
    • stream_message flows
    • metadata edge cases
    • payment context injection/removal

Impact

  • Improves stability of message handling pipeline
  • Ensures safe handling of optional metadata fields
  • Aligns with production-safe dictionary handling practices

breaking multiple message handling flows (send_message, stream_message) and causing test failures.
This change improves robustness of the message pipeline under optional/partial metadata scenarios.

@Subhajitdas99
Copy link
Copy Markdown
Contributor Author

@raahulrahl
This PR is ready for review.
All tests are passing and edge cases have been handled safely.
Happy to make any changes if needed.

Copy link
Copy Markdown
Contributor Author

@Subhajitdas99 Subhajitdas99 left a comment

Choose a reason for hiding this comment

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

Overview

This PR improves the reliability and robustness of the stream_message flow by introducing retry logic and safer handling of missing task states.

Problem

Previously, stream_message relied on a single call to storage.load_task().
If the task was temporarily unavailable (due to async timing or storage delay), the stream could fail prematurely.

Changes

  • Extracted retry logic into _retry_load_task
  • Replaced inline retry loop with reusable helper
  • Added safe fallback when task is missing after retries
  • Added guard for missing task fields
  • Simplified stream loop and removed duplicate logic

Improvements

  • More resilient streaming under intermittent failures
  • Cleaner and more maintainable code
  • Better separation of concerns

Testing

  • ✅ 680 tests passed
  • ✅ No regressions

Impact

  • Improves streaming stability
  • Prevents premature task failure in async environments
  • No breaking changes

@Subhajitdas99 Subhajitdas99 changed the title Fix: Safe handling of payment_context in message handlers + remove KeyError edge case Enhancement: Improve stream_message reliability with retry logic and safer error handling Mar 19, 2026
@Subhajitdas99
Copy link
Copy Markdown
Contributor Author

@raahulrahl
This PR improves stream reliability and removes duplicate retry logic.
All tests are passing (680), and no regressions observed.
Happy to refine based on feedback.

@chandan-1427
Copy link
Copy Markdown
Contributor

Hi @Subhajitdas99, thanks for your hard work on this! It looks like a few different initiatives got mixed up in this single branch.

To get this merged safely, we need to split it into distinct, focused PRs:

Bug Fixes: Please open a separate PR just for the stream_message, payment_context, and DID file permission fixes. We'd love to merge those.

Semantic Memory: For the memory extension, please open a separate PR. Note that we cannot use a global MEMORY_STORE = [] list as it won't persist across async web workers, and the [0.0] dummy fallback in embeddings.py could cause silent failures in production. We also need openai added as an optional dependency rather than a core one.

AgentMesh: It looks like the agentmesh/ directory and Docker files were committed accidentally. Please remove those from your branches.

Let us know when you have the bug-fix PR separated out and we will review it right away!

@Subhajitdas99
Copy link
Copy Markdown
Contributor Author

Thanks @chandan-1427 for the detailed review! Completely agree — I'll split this into focused PRs. Working on it now:

Bug fix PR for stream_message, payment_context, and DID fixes
Separate semantic memory PR with the issues you flagged fixed
Will remove agentmesh/ from all branches

Will have the bug fix PR ready shortly!

@Subhajitdas99
Copy link
Copy Markdown
Contributor Author

Hey @chandan-1427 — split done! The bug fixes are now in a focused PR:
PR #418: fix: safe payment_context handling and cross-platform DID file permissions
#418
Working on the semantic memory PR next with your feedback applied (fixing global MEMORY_STORE, removing dummy fallback, making openai an optional dependency). Will share shortly!

@Subhajitdas99
Copy link
Copy Markdown
Contributor Author

Closing this PR as requested — splitting into focused PRs. Bug fixes: #418. Semantic memory: coming shortly.

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.

2 participants