Skip to content

Commit e406993

Browse files
author
Ismael Marchi
committed
docs: improve docstrings, inline comments, and add source references to SECURITY.md
- core.py: clarify TQ scoring logic and in-memory store comments - sanitizer.py: rename 'Production-Grade' to 'Semantic Privacy Guard™', expand docstrings for validate_sanitization() and batch_sanitize() - privacy.py: expand docstrings for batch_apply() and get_config() - validator.py: remove 'Production-grade' label, expand _score_content() docstring with normalization details - handover.py: expand docstrings for _sign_token(), _verify_token(), _fail_handover(), and _generate_summary() with implementation details - SECURITY.md: add Source References section linking all main modules Only readability and documentation improved — no functionality altered.
1 parent 2728e2b commit e406993

6 files changed

Lines changed: 116 additions & 19 deletions

File tree

SECURITY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,22 @@ If you discover a security vulnerability:
404404

405405
---
406406

407+
## Source References
408+
409+
The security architecture described in this document is implemented across the following modules in the public SDK:
410+
411+
| Module | Component | Path |
412+
|--------|-----------|------|
413+
| **Memory Core** | Zero-Knowledge Memory Layer (orchestration) | [`synapse_memory/core.py`](synapse_memory/core.py) |
414+
| **Semantic Privacy Guard™** | PII detection, removal & forensic hashing | [`synapse_memory/sanitizer.py`](synapse_memory/sanitizer.py) |
415+
| **Differential Privacy** | Gaussian noise injection on embeddings | [`synapse_memory/privacy.py`](synapse_memory/privacy.py) |
416+
| **Intelligent Intent Validation™** | Two-step classification + self-healing | [`synapse_memory/engine/validator.py`](synapse_memory/engine/validator.py) |
417+
| **Neural Handover™** | JWT-signed vault-first cross-agent transfer | [`synapse_memory/engine/handover.py`](synapse_memory/engine/handover.py) |
418+
419+
Each module includes inline tests that can be executed individually (e.g., `python -m synapse_memory.core`).
420+
421+
---
422+
407423
## Security Contact
408424

409425
**Email:** security@synapselayer.org

synapse_memory/core.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ def __init__(
135135
validate=True,
136136
)
137137

138-
# In-memory store (production uses pgvector + AES-256-GCM)
138+
# In-memory store — SDK demo only.
139+
# Production deployments persist to pgvector + AES-256-GCM.
139140
self._memories: List[Dict[str, Any]] = []
140141

141142
logger.info(
@@ -213,11 +214,13 @@ async def store(
213214
).hexdigest()
214215
memory_id = content_hash[:32]
215216

216-
# Trust Quotient = merged_confidence * validation_score
217+
# Trust Quotient (TQ) = merged_confidence × validation_score.
218+
# TQ is the primary ranking signal for recall operations.
217219
trust_quotient = round(
218220
validation.confidence * validation.validation_score, 4
219221
)
220-
# Apply confidence_boost for CRITICAL memories
222+
# Apply additive confidence boost for CRITICAL memories so they
223+
# consistently surface at the top of recall results.
221224
if validation.confidence_boost > 0:
222225
trust_quotient = min(
223226
trust_quotient + validation.confidence_boost * 0.1, 1.0
@@ -307,7 +310,8 @@ async def recall(
307310
"""
308311
query_lower = query.lower()
309312

310-
# Simple relevance scoring for SDK demo
313+
# Simple relevance scoring (SDK demo — substring matching).
314+
# Production: pgvector cosine similarity with ANN index.
311315
scored: List[tuple] = []
312316
for mem in self._memories:
313317
content_lower = mem['content'].lower()

synapse_memory/engine/handover.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,24 @@ def _sign_token(
621621
issued_at: float,
622622
expires_at: float,
623623
) -> HandoverToken:
624-
"""Create and sign a JWT-style handover token."""
624+
"""Create and sign a JWT-style handover token (SHT format).
625+
626+
Constructs a three-part ``header.payload.signature`` string
627+
using HMAC-SHA256 with the instance signing key. The token
628+
is base64url-encoded (no padding) for safe URL transport.
629+
630+
Args:
631+
token_id: Unique identifier for this handover.
632+
origin_agent: Source agent ID.
633+
target_agent: Destination agent ID.
634+
user_id: Owning user ID.
635+
scope: Access scope (e.g., "full", "read_only").
636+
issued_at: Unix timestamp of issuance.
637+
expires_at: Unix timestamp of expiration.
638+
639+
Returns:
640+
Fully populated ``HandoverToken`` with encoded JWT.
641+
"""
625642
header = {
626643
'alg': 'HS256',
627644
'typ': 'SHT', # Synapse Handover Token
@@ -665,7 +682,14 @@ def _sign_token(
665682
)
666683

667684
def _verify_token(self, token: HandoverToken) -> bool:
668-
"""Verify HMAC-SHA256 signature of a handover token."""
685+
"""Verify HMAC-SHA256 signature of a handover token.
686+
687+
Uses constant-time comparison (``hmac.compare_digest``) to
688+
prevent timing-based side-channel attacks.
689+
690+
Returns:
691+
True if signature is valid, False otherwise.
692+
"""
669693
try:
670694
parts = token.encoded_token.split('.')
671695
if len(parts) != 3:
@@ -685,7 +709,7 @@ def _verify_token(self, token: HandoverToken) -> bool:
685709
return False
686710

687711
def _get_package(self, handover_id: str) -> HandoverPackage:
688-
"""Retrieve a package or raise KeyError."""
712+
"""Retrieve a package from the Status Ledger or raise KeyError."""
689713
package = self._ledger.get(handover_id)
690714
if package is None:
691715
raise KeyError(f"Handover '{handover_id}' not found in ledger.")
@@ -696,7 +720,11 @@ def _fail_handover(
696720
package: HandoverPackage,
697721
reason: str,
698722
) -> HandoverPackage:
699-
"""Transition to FAILED and create emergency checkpoint."""
723+
"""Transition to FAILED and create an Emergency Checkpoint.
724+
725+
The checkpoint preserves a full snapshot of ``context_data`` so
726+
that recovery (manual or automated) is always possible.
727+
"""
700728
now = time.time()
701729
package.status = HandoverStatus.FAILED
702730
package.failed_at = now
@@ -725,10 +753,12 @@ def _fail_handover(
725753

726754
@staticmethod
727755
def _generate_summary(package: HandoverPackage) -> str:
728-
"""Generate a compact summary from expired handover context.
756+
"""Generate a compact summary from an expired handover context.
729757
730-
Used during grace period to return useful information
731-
without exposing raw memory data.
758+
Called during the grace period to provide the target agent with
759+
useful high-level information without exposing raw memory data.
760+
Includes handover metadata, intent distribution, and truncated
761+
content snippets (first 80 chars of up to 5 memories).
732762
"""
733763
parts: List[str] = [
734764
f"Handover Summary (expired): {package.handover_id}",

synapse_memory/engine/validator.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
SynapseValidator — Intelligent Intent Validation™ with Self-Healing
33
4-
Production-grade cognitive security layer that classifies, validates,
5-
and auto-corrects memory intent in a two-step pipeline:
4+
Cognitive security layer that classifies, validates, and auto-corrects
5+
memory intent in a two-step pipeline:
66
77
Step 1 (Agent Suggestion): Keyword heuristics + scoring → proposed intent
88
Step 2 (Synapse Validation): Confidence gate, critical promotion, self-healing
@@ -488,8 +488,18 @@ def batch_validate(
488488
def _score_content(self, content: str) -> Dict[str, float]:
489489
"""Score content against all keyword dictionaries.
490490
491+
Used by ``heal_conflicts()`` to re-evaluate evidence strength
492+
for both memories involved in a category conflict.
493+
494+
Normalization: ``min(hits / 3.0, 1.0)`` — three keyword
495+
matches saturate confidence for a given category.
496+
497+
Args:
498+
content: Text to score (will be lowercased internally).
499+
491500
Returns:
492-
Dict mapping category value strings to normalized scores.
501+
Dict mapping category value strings to normalized scores
502+
in the range [0.0, 1.0].
493503
"""
494504
content_lower = content.lower()
495505
scores: Dict[str, float] = {}

synapse_memory/privacy.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,27 @@ def apply(self, embedding: List[float]) -> PrivacyResult:
186186
def batch_apply(
187187
self, embeddings: List[List[float]]
188188
) -> List[PrivacyResult]:
189-
"""Apply differential privacy to a batch of embeddings."""
189+
"""Apply differential privacy to a batch of embeddings.
190+
191+
Each embedding receives independent noise calibrated to the
192+
same (ε, δ) budget. Noise instances are non-correlated.
193+
194+
Args:
195+
embeddings: List of dense float vectors (all same dimension).
196+
197+
Returns:
198+
List of ``PrivacyResult`` in the same order as input.
199+
"""
190200
return [self.apply(emb) for emb in embeddings]
191201

192202
def get_config(self) -> Dict[str, Any]:
193-
"""Return current privacy configuration for audit logging."""
203+
"""Return current privacy configuration for audit logging.
204+
205+
Useful for compliance reports and automated privacy audits.
206+
207+
Returns:
208+
Dict with ``epsilon``, ``delta``, ``normalize``, and ``mechanism``.
209+
"""
194210
return {
195211
'epsilon': self.epsilon,
196212
'delta': self.delta,

synapse_memory/sanitizer.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
SynapseSanitizer — Production-Grade Content Sanitization Engine
2+
SynapseSanitizer — Semantic Privacy Guard™ Content Sanitization Engine
33
44
High-performance PII detection, removal, and content hardening pipeline.
55
Supports standard and aggressive modes for maximum semantic privacy.
@@ -42,7 +42,7 @@ class SanitizationResult:
4242

4343
class SynapseSanitizer:
4444
"""
45-
Production-grade content sanitizer with:
45+
Semantic Privacy Guard™ content sanitizer with:
4646
- 12 precompiled regex patterns for PII/sensitive data detection
4747
- Aggressive mode: removes proper nouns (capitalized words) for
4848
maximum semantic privacy against embedding-based inference attacks
@@ -289,6 +289,17 @@ def validate_sanitization(
289289
) -> Dict[str, Any]:
290290
"""
291291
Compute effectiveness metrics comparing original vs. sanitized content.
292+
293+
Measures character-level reduction to quantify how much PII was
294+
removed. Useful for compliance dashboards and audit reporting.
295+
296+
Args:
297+
original: The raw, unsanitized text.
298+
sanitized: The output of ``sanitize_content()``.
299+
300+
Returns:
301+
Dict with ``original_length``, ``sanitized_length``,
302+
``reduction_pct``, and ``effectiveness`` ("high" or "low").
292303
"""
293304
orig_len = len(original)
294305
san_len = len(sanitized)
@@ -305,7 +316,17 @@ def validate_sanitization(
305316
def batch_sanitize(
306317
self, contents: List[str]
307318
) -> List[SanitizationResult]:
308-
"""Sanitize a list of texts in batch."""
319+
"""Sanitize a list of texts in batch.
320+
321+
Convenience wrapper that applies ``sanitize_content()`` to each
322+
element in *contents* and returns results in the same order.
323+
324+
Args:
325+
contents: List of raw text strings to sanitize.
326+
327+
Returns:
328+
List of ``SanitizationResult`` in input order.
329+
"""
309330
return [self.sanitize_content(c) for c in contents]
310331

311332

0 commit comments

Comments
 (0)