ct-toolkit 0.3.1__tar.gz → 0.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/PKG-INFO +11 -6
  2. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/README.md +10 -5
  3. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/core/wrapper.py +51 -29
  4. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/divergence/engine.py +18 -3
  5. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit.egg-info/PKG-INFO +11 -6
  6. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/pyproject.toml +1 -1
  7. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/LICENSE +0 -0
  8. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/__init__.py +0 -0
  9. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/core/__init__.py +0 -0
  10. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/core/compatibility.py +0 -0
  11. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/core/exceptions.py +0 -0
  12. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/core/integrity.py +0 -0
  13. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/core/kernel.py +0 -0
  14. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/divergence/__init__.py +0 -0
  15. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/divergence/analysis.py +0 -0
  16. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/divergence/l2_judge.py +0 -0
  17. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/divergence/l3_icm.py +0 -0
  18. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/divergence/loss.py +0 -0
  19. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/divergence/scheduler.py +0 -0
  20. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/endorsement/__init__.py +0 -0
  21. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/endorsement/probes/__init__.py +0 -0
  22. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/endorsement/reflective.py +0 -0
  23. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/identity/__init__.py +0 -0
  24. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/identity/embedding.py +0 -0
  25. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/identity/templates/__init__.py +0 -0
  26. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/identity/templates/defense.yaml +0 -0
  27. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/identity/templates/finance.yaml +0 -0
  28. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/identity/templates/general.yaml +0 -0
  29. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/identity/templates/medical.yaml +0 -0
  30. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/kernels/__init__.py +0 -0
  31. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/kernels/default.yaml +0 -0
  32. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/kernels/defense.yaml +0 -0
  33. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/middleware/__init__.py +0 -0
  34. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/middleware/autogen.py +0 -0
  35. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/middleware/crewai.py +0 -0
  36. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/middleware/deepagents.py +0 -0
  37. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/middleware/langchain.py +0 -0
  38. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/provenance/__init__.py +0 -0
  39. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/provenance/log.py +0 -0
  40. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/provenance/vault/__init__.py +0 -0
  41. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/utils/__init__.py +0 -0
  42. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit/utils/logger.py +0 -0
  43. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit.egg-info/SOURCES.txt +0 -0
  44. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit.egg-info/dependency_links.txt +0 -0
  45. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit.egg-info/requires.txt +0 -0
  46. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/ct_toolkit.egg-info/top_level.txt +0 -0
  47. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/setup.cfg +0 -0
  48. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/tests/test_analysis.py +0 -0
  49. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/tests/test_deep_agents_integration.py +0 -0
  50. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/tests/test_loss.py +0 -0
  51. {ct_toolkit-0.3.1 → ct_toolkit-0.3.2}/tests/test_summarization_drift.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ct-toolkit
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Computational Theseus Toolkit — Identity Continuity Guardrails for Agentic Systems
5
5
  Author: Hakan Damar
6
6
  License: Apache-2.0
@@ -51,6 +51,7 @@ CT Toolkit is an open-source security layer designed to preserve the **identity
51
51
  ---
52
52
 
53
53
  ## 📖 Official Documentation
54
+
54
55
  For full API reference, architecture details, examples, and integration guides, visit our documentation site:
55
56
  👉 [**https://hakandamar.github.io/ct-toolkit/**](https://hakandamar.github.io/ct-toolkit/)
56
57
 
@@ -63,6 +64,7 @@ For full API reference, architecture details, examples, and integration guides,
63
64
  In complex agentic workflows, LLMs tend to "drift" from their original instructions. CT Toolkit provides the mathematical and cryptographic guardrails to ensure your agents remain aligned with their core constitution, even across deep hierarchies.
64
65
 
65
66
  - **Constitutional Kernels**: Axiomatic identity anchors.
67
+ - **Autonomous Self-Correction**: Active L2->L1 feedback loop that retries and corrects divergent responses before they reach the user.
66
68
  - **Divergence Engine**: Multi-tiered drift analysis (L1/L2/L3).
67
69
  - **Hierarchical Propagation**: Mother-to-child constraint inheritance.
68
70
  - **Provenance Log**: Immutable HMAC-signed interaction history.
@@ -92,15 +94,16 @@ print(f"Divergence Score: {response.divergence_score}")
92
94
 
93
95
  ## 🚦 Project Health & Status
94
96
 
95
- | Metric | Status |
96
- | :--- | :--- |
97
- | **Tests** | ✅ 218/218 passing (93% coverage) |
98
- | **Last Phase** | ✅ Phase 4: OS Support, Deep Agents & Alerts (Complete) |
99
- | **Current Goal** | 🔶 Phase 5: Vault and Security Infrastructure |
97
+ | Metric | Status |
98
+ | :--------------- | :------------------------------------------------------ |
99
+ | **Tests** | ✅ 231/232 passing (90% coverage) |
100
+ | **Last Phase** | ✅ Phase 4: OS Support, Deep Agents & Alerts (Complete) |
101
+ | **Current Goal** | 🔶 Phase 5: Vault & Security (In Progress) |
100
102
 
101
103
  For a detailed breakdown of the 8-phase roadmap, see [**PROJECT_STATUS.md**](docs/PROJECT_STATUS.md).
102
104
 
103
105
  ### Framework & Model Support
106
+
104
107
  Seamlessly integrate with your favorite frameworks and local models:
105
108
 
106
109
  - **Local Models**: Support for LM Studio, Ollama, and local Qwen/Llama endpoints.
@@ -111,9 +114,11 @@ Seamlessly integrate with your favorite frameworks and local models:
111
114
  ---
112
115
 
113
116
  ## Theoretical Foundation
117
+
114
118
  Translating the framework proposed in [**The Computational Theseus (2025)**](https://hakandamar.com/the-computational-theseus-engineering-identity-continuity-as-a-guardrail-against-sequential-963918c1720d) into engineering practice.
115
119
 
116
120
  ---
117
121
 
118
122
  ## License
123
+
119
124
  Apache License 2.0.
@@ -12,6 +12,7 @@ CT Toolkit is an open-source security layer designed to preserve the **identity
12
12
  ---
13
13
 
14
14
  ## 📖 Official Documentation
15
+
15
16
  For full API reference, architecture details, examples, and integration guides, visit our documentation site:
16
17
  👉 [**https://hakandamar.github.io/ct-toolkit/**](https://hakandamar.github.io/ct-toolkit/)
17
18
 
@@ -24,6 +25,7 @@ For full API reference, architecture details, examples, and integration guides,
24
25
  In complex agentic workflows, LLMs tend to "drift" from their original instructions. CT Toolkit provides the mathematical and cryptographic guardrails to ensure your agents remain aligned with their core constitution, even across deep hierarchies.
25
26
 
26
27
  - **Constitutional Kernels**: Axiomatic identity anchors.
28
+ - **Autonomous Self-Correction**: Active L2->L1 feedback loop that retries and corrects divergent responses before they reach the user.
27
29
  - **Divergence Engine**: Multi-tiered drift analysis (L1/L2/L3).
28
30
  - **Hierarchical Propagation**: Mother-to-child constraint inheritance.
29
31
  - **Provenance Log**: Immutable HMAC-signed interaction history.
@@ -53,15 +55,16 @@ print(f"Divergence Score: {response.divergence_score}")
53
55
 
54
56
  ## 🚦 Project Health & Status
55
57
 
56
- | Metric | Status |
57
- | :--- | :--- |
58
- | **Tests** | ✅ 218/218 passing (93% coverage) |
59
- | **Last Phase** | ✅ Phase 4: OS Support, Deep Agents & Alerts (Complete) |
60
- | **Current Goal** | 🔶 Phase 5: Vault and Security Infrastructure |
58
+ | Metric | Status |
59
+ | :--------------- | :------------------------------------------------------ |
60
+ | **Tests** | ✅ 231/232 passing (90% coverage) |
61
+ | **Last Phase** | ✅ Phase 4: OS Support, Deep Agents & Alerts (Complete) |
62
+ | **Current Goal** | 🔶 Phase 5: Vault & Security (In Progress) |
61
63
 
62
64
  For a detailed breakdown of the 8-phase roadmap, see [**PROJECT_STATUS.md**](docs/PROJECT_STATUS.md).
63
65
 
64
66
  ### Framework & Model Support
67
+
65
68
  Seamlessly integrate with your favorite frameworks and local models:
66
69
 
67
70
  - **Local Models**: Support for LM Studio, Ollama, and local Qwen/Llama endpoints.
@@ -72,9 +75,11 @@ Seamlessly integrate with your favorite frameworks and local models:
72
75
  ---
73
76
 
74
77
  ## Theoretical Foundation
78
+
75
79
  Translating the framework proposed in [**The Computational Theseus (2025)**](https://hakandamar.com/the-computational-theseus-engineering-identity-continuity-as-a-guardrail-against-sequential-963918c1720d) into engineering practice.
76
80
 
77
81
  ---
78
82
 
79
83
  ## License
84
+
80
85
  Apache License 2.0.
@@ -59,6 +59,10 @@ class WrapperConfig:
59
59
  embedding_model: str = "text-embedding-3-small"
60
60
  enterprise_mode: bool = False # Run all tiers all the time
61
61
  parent_kernel: ConstitutionalKernel | None = None # Propagated from mother agent
62
+
63
+ # -- Auto-Correction Loop --
64
+ auto_correction: bool = False
65
+ max_correction_retries: int = 1
62
66
 
63
67
  # -- Dynamic Stability-Plasticity Scheduling --
64
68
  elasticity_max_thresholds: tuple[float, float, float] | None = None # (max_l1, max_l2, max_l3)
@@ -385,34 +389,52 @@ class TheseusWrapper:
385
389
  f"Please provide a client or set the appropriate environment variables."
386
390
  )
387
391
 
388
- start_time = time.monotonic()
389
-
390
- try:
391
- raw_response = self._call_provider(messages, model=model, **kwargs)
392
- except Exception as e:
393
- logger.error(f"Provider API error: {e}")
394
- raise
395
-
396
- elapsed = time.monotonic() - start_time
397
- content = self._extract_content(raw_response)
398
- model_used = self._extract_model(raw_response, model)
399
- self._last_model = model_used
400
-
401
- # Compute current interaction experience
402
- interaction_count = 0
403
- if self._config.log_requests:
404
- interaction_count = self._provenance_log.get_interaction_count(
405
- template=self._config.template,
406
- kernel_name=self._kernel.name,
407
- model=model_used
392
+ retries = 0
393
+ max_retries = self._config.max_correction_retries if self._config.auto_correction else 0
394
+
395
+ while retries <= max_retries:
396
+ start_time = time.monotonic()
397
+
398
+ try:
399
+ raw_response = self._call_provider(messages, model=model, **kwargs)
400
+ except Exception as e:
401
+ logger.error(f"Provider API error: {e}")
402
+ raise
403
+
404
+ elapsed = time.monotonic() - start_time
405
+ content = self._extract_content(raw_response)
406
+ model_used = self._extract_model(raw_response, model)
407
+ self._last_model = model_used
408
+
409
+ # Compute current interaction experience
410
+ interaction_count = 0
411
+ if self._config.log_requests:
412
+ interaction_count = self._provenance_log.get_interaction_count(
413
+ template=self._config.template,
414
+ kernel_name=self._kernel.name,
415
+ model=model_used
416
+ )
417
+
418
+ # Divergence Engine (L1 -> L2 -> L3)
419
+ skip_l3 = self._config.auto_correction and retries < max_retries
420
+ div_result = self._run_divergence_engine(
421
+ message=message,
422
+ response=content,
423
+ interaction_count=interaction_count,
424
+ skip_l3=skip_l3,
408
425
  )
409
-
410
- # Divergence Engine (L1 -> L2 -> L3)
411
- div_result = self._run_divergence_engine(
412
- message=message,
413
- response=content,
414
- interaction_count=interaction_count,
415
- )
426
+
427
+ from ct_toolkit.divergence.l2_judge import JudgeVerdict
428
+ if getattr(div_result, 'l2_verdict', None) == JudgeVerdict.MISALIGNED and retries < max_retries:
429
+ logger.warning(f"Auto-correction triggered. Reason: {div_result.l2_reason}")
430
+ messages.extend([
431
+ {"role": "assistant", "content": content},
432
+ {"role": "user", "content": f"Identity Drift Detected. Your previous response violated the core identity instructions. Reason: {div_result.l2_reason}. Please revise your response to strictly adhere to the system prompt."}
433
+ ])
434
+ retries += 1
435
+ continue
436
+
437
+ break
416
438
 
417
439
  # Provenance Log record
418
440
  provenance_id = None
@@ -582,10 +604,10 @@ class TheseusWrapper:
582
604
  return raw.model
583
605
  return fallback or "unknown"
584
606
 
585
- def _run_divergence_engine(self, message: str, response: str, interaction_count: int = 0) -> Any:
607
+ def _run_divergence_engine(self, message: str, response: str, interaction_count: int = 0, skip_l3: bool = False) -> Any:
586
608
  """Runs the staged divergence engine (L1 -> L2 -> L3)."""
587
609
  try:
588
- return self._divergence_engine.analyze(message, response, interaction_count)
610
+ return self._divergence_engine.analyze(message, response, interaction_count, skip_l3=skip_l3)
589
611
  except Exception as e:
590
612
  logger.error(f"Divergence Engine execution failed: {e}")
591
613
  from ct_toolkit.divergence.engine import DivergenceResult, DivergenceTier
@@ -154,6 +154,7 @@ class DivergenceEngine:
154
154
  request_text: str,
155
155
  response_text: str,
156
156
  interaction_count: int = 0,
157
+ skip_l3: bool = False,
157
158
  ) -> DivergenceResult:
158
159
  """
159
160
  Performs full divergence analysis.
@@ -166,8 +167,8 @@ class DivergenceEngine:
166
167
  )
167
168
 
168
169
  if self._enterprise:
169
- return self._enterprise_analyze(request_text, response_text)
170
- return self._standard_analyze(request_text, response_text)
170
+ return self._enterprise_analyze(request_text, response_text, skip_l3=skip_l3)
171
+ return self._standard_analyze(request_text, response_text, skip_l3=skip_l3)
171
172
 
172
173
  def get_drift_report(self, window_size: int = 50, model: str | None = None) -> DriftReport | None:
173
174
  """
@@ -189,6 +190,7 @@ class DivergenceEngine:
189
190
  self,
190
191
  request_text: str,
191
192
  response_text: str,
193
+ skip_l3: bool = False,
192
194
  ) -> DivergenceResult:
193
195
  """Progressive analysis: L1 → L2 if needed → L3 if needed."""
194
196
 
@@ -236,6 +238,18 @@ class DivergenceEngine:
236
238
  )
237
239
 
238
240
  # L3 — L2 problematic or L1 too high
241
+ if skip_l3:
242
+ return DivergenceResult(
243
+ tier=DivergenceTier.L2_JUDGE,
244
+ l1_score=l1_score,
245
+ l2_verdict=l2_result.verdict,
246
+ l2_confidence=l2_result.confidence,
247
+ l2_reason=l2_result.reason,
248
+ action_required=True,
249
+ cascade_blocked=True,
250
+ summary="L3 skipped due to auto-correction retry logic",
251
+ )
252
+
239
253
  if not self._l3_available:
240
254
  return DivergenceResult(
241
255
  tier=DivergenceTier.L2_JUDGE,
@@ -269,6 +283,7 @@ class DivergenceEngine:
269
283
  self,
270
284
  request_text: str,
271
285
  response_text: str,
286
+ skip_l3: bool = False,
272
287
  ) -> DivergenceResult:
273
288
  """Enterprise mode: L1 + L2 + L3 all run, decision based on total score."""
274
289
  l1_score = self._run_l1(response_text)
@@ -278,7 +293,7 @@ class DivergenceEngine:
278
293
  l2_result = self._run_l2(request_text, response_text)
279
294
 
280
295
  l3_report = None
281
- if self._l3_available:
296
+ if self._l3_available and not skip_l3:
282
297
  l3_report = self._run_l3(max_probes=5) # Fast subset in Enterprise
283
298
 
284
299
  # Calculate total risk score
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ct-toolkit
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Computational Theseus Toolkit — Identity Continuity Guardrails for Agentic Systems
5
5
  Author: Hakan Damar
6
6
  License: Apache-2.0
@@ -51,6 +51,7 @@ CT Toolkit is an open-source security layer designed to preserve the **identity
51
51
  ---
52
52
 
53
53
  ## 📖 Official Documentation
54
+
54
55
  For full API reference, architecture details, examples, and integration guides, visit our documentation site:
55
56
  👉 [**https://hakandamar.github.io/ct-toolkit/**](https://hakandamar.github.io/ct-toolkit/)
56
57
 
@@ -63,6 +64,7 @@ For full API reference, architecture details, examples, and integration guides,
63
64
  In complex agentic workflows, LLMs tend to "drift" from their original instructions. CT Toolkit provides the mathematical and cryptographic guardrails to ensure your agents remain aligned with their core constitution, even across deep hierarchies.
64
65
 
65
66
  - **Constitutional Kernels**: Axiomatic identity anchors.
67
+ - **Autonomous Self-Correction**: Active L2->L1 feedback loop that retries and corrects divergent responses before they reach the user.
66
68
  - **Divergence Engine**: Multi-tiered drift analysis (L1/L2/L3).
67
69
  - **Hierarchical Propagation**: Mother-to-child constraint inheritance.
68
70
  - **Provenance Log**: Immutable HMAC-signed interaction history.
@@ -92,15 +94,16 @@ print(f"Divergence Score: {response.divergence_score}")
92
94
 
93
95
  ## 🚦 Project Health & Status
94
96
 
95
- | Metric | Status |
96
- | :--- | :--- |
97
- | **Tests** | ✅ 218/218 passing (93% coverage) |
98
- | **Last Phase** | ✅ Phase 4: OS Support, Deep Agents & Alerts (Complete) |
99
- | **Current Goal** | 🔶 Phase 5: Vault and Security Infrastructure |
97
+ | Metric | Status |
98
+ | :--------------- | :------------------------------------------------------ |
99
+ | **Tests** | ✅ 231/232 passing (90% coverage) |
100
+ | **Last Phase** | ✅ Phase 4: OS Support, Deep Agents & Alerts (Complete) |
101
+ | **Current Goal** | 🔶 Phase 5: Vault & Security (In Progress) |
100
102
 
101
103
  For a detailed breakdown of the 8-phase roadmap, see [**PROJECT_STATUS.md**](docs/PROJECT_STATUS.md).
102
104
 
103
105
  ### Framework & Model Support
106
+
104
107
  Seamlessly integrate with your favorite frameworks and local models:
105
108
 
106
109
  - **Local Models**: Support for LM Studio, Ollama, and local Qwen/Llama endpoints.
@@ -111,9 +114,11 @@ Seamlessly integrate with your favorite frameworks and local models:
111
114
  ---
112
115
 
113
116
  ## Theoretical Foundation
117
+
114
118
  Translating the framework proposed in [**The Computational Theseus (2025)**](https://hakandamar.com/the-computational-theseus-engineering-identity-continuity-as-a-guardrail-against-sequential-963918c1720d) into engineering practice.
115
119
 
116
120
  ---
117
121
 
118
122
  ## License
123
+
119
124
  Apache License 2.0.
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ct-toolkit"
7
- version = "0.3.1"
7
+ version = "0.3.2"
8
8
  description = "Computational Theseus Toolkit — Identity Continuity Guardrails for Agentic Systems"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
File without changes
File without changes