Skip to content

Commit eeb1daf

Browse files
committed
Refactor prompt regression scoring, enhance version control retrieval, and improve reflection tuning conditions
- Updated `PromptRegressionRunner` to use a weighted scoring function for baseline and candidate outputs, incorporating partial matches. - Enhanced `PromptVersionControl` to ensure version retrieval is robust against missing tags. - Adjusted `DynamicReflectionPromptBuilder` to allow a wider confidence range for header injection. - Improved `SelfImprovementOrchestrator` to streamline result finalization and ensure accurate status updates. - Modified `TaskScheduler` to track scheduled tasks more effectively and prevent over-dispatching. - Added deadline checks in `ThemisRPCService` for search and paginated query handling to prevent timeouts. - Enhanced time point handling in `task_scheduler_api_handler` to account for edge cases. - Updated tests across various modules to reflect changes in expected behavior and ensure robustness. - Introduced a fix plan for the LLM adapter to address future blockers while maintaining existing functionality.
1 parent caa8aba commit eeb1daf

52 files changed

Lines changed: 741 additions & 251 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/pull_request_template.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@
3434
- [ ] Manual testing performed
3535
- [ ] Benchmarks run (if performance-sensitive change)
3636

37+
## Security Tiering Impact (Required for Runtime Changes)
38+
39+
<!-- Reference model: ARCHITECTURE.md -> Security & Hardening Tiering Model -->
40+
41+
- Impacted tier(s):
42+
- [ ] T0 Trusted Core
43+
- [ ] T1 Security & Platform Services
44+
- [ ] T2 Data Plane Engines
45+
- [ ] T3 Interface & Protocol Edge
46+
- [ ] T4 Managed Extension Runtime
47+
- [ ] T5 Plugin Boundary
48+
- [ ] N/A (docs-only / non-runtime)
49+
50+
- [ ] Trust-boundary crossings documented in PR description (example: T3 -> T2, T5 -> T4 brokered call)
51+
- [ ] Boundary controls validated for affected T3/T4/T5 paths (AuthN/AuthZ, validation, rate limits, audit)
52+
- [ ] Boundary-focused tests added/updated or explicit N/A rationale provided
53+
- [ ] If trust level/privilege increased, security maintainer approval is attached
54+
3755
## 📚 Research & Knowledge (wenn applicable)
3856

3957
- [ ] Diese PR basiert auf wissenschaftlichen Paper(s) oder Best Practices?

ARCHITECTURE.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,51 @@ Lifecycle management for LLM prompts and domain-specific fine-tuning adapters:
374374

375375
---
376376

377+
## Security & Hardening Tiering Model (Core Module -> Plugin)
378+
379+
To make security ownership and hardening requirements explicit, ThemisDB uses a
380+
strict tier model from trusted core modules to untrusted plugin boundaries.
381+
382+
### Tier Definitions
383+
384+
| Tier | Scope | Typical Modules | Trust Level | Blast Radius |
385+
|---|---|---|---|---|
386+
| **T0: Trusted Core** | Minimal trusted computing base, bootstrapping, memory/lifecycle primitives | `src/core/`, `src/base/`, `src/themis/`, `src/utils/` | Highest | System-wide compromise risk |
387+
| **T1: Security & Platform Services** | Identity, cryptography, policy, config validation, audit | `src/security/`, `src/auth/`, `src/governance/`, `src/config/` | Very high | Cross-module security policy bypass |
388+
| **T2: Data Plane Engines** | Query, transaction, storage, index, sharding/replication logic | `src/query/`, `src/storage/`, `src/index/`, `src/transaction/`, `src/sharding/`, `src/replication/` | High | Data corruption, consistency and integrity impact |
389+
| **T3: Interface & Protocol Edge** | External protocol handling and request ingress | `src/server/`, `src/network/`, `src/rpc_grpc/`, `src/api/`, `src/cdc/` | Medium | Remote attack surface and tenant boundary violations |
390+
| **T4: Managed Extension Runtime** | In-tree feature extensions with controlled interfaces | `src/llm/`, `src/llama_cpp/`, `src/whisper/`, `src/stable_diffusion/`, `src/content/`, `src/onnx_clip/` | Medium-low | Feature-domain scoped, can escalate if guardrails fail |
391+
| **T5: Plugin Boundary (Least Trusted)** | Dynamically loaded or remotely controlled plugin code and adapters | `src/plugins/`, selected adapters in `adapters/` | Lowest | Arbitrary code execution within plugin sandbox scope |
392+
393+
### Dependency and Privilege Rules (Mandatory)
394+
395+
1. **One-way dependency direction:** Higher-numbered tiers may depend on lower-numbered tiers; lower tiers must not depend on higher tiers.
396+
2. **No direct T5 -> T0/T1 privileged calls:** Plugins interact via brokered interfaces (`PluginManager`, RPC contracts, policy checks).
397+
3. **Security mediation at boundaries:** Every T3/T4/T5 entry path must pass authentication, authorization, input validation, rate limits, and audit hooks.
398+
4. **Least privilege by default:** Capabilities must be explicitly granted and denied by default (network, filesystem, model loading, data export).
399+
5. **Fail-closed behavior:** On policy/config/auth uncertainty, requests must be rejected rather than downgraded.
400+
401+
### Hardening Baseline per Tier
402+
403+
| Tier | Required Security Controls | Hardening Requirements | Release Gate Evidence |
404+
|---|---|---|---|
405+
| **T0** | Memory-safety review, strict ownership/lifetime validation, anti-tamper bootstrap checks | Warnings-as-errors, UB/sanitizer clean paths where supported, deterministic startup invariants | Security maintainer sign-off + focused unit/integration tests |
406+
| **T1** | Cryptographic policy enforcement, key-material isolation, immutable audit trail, RBAC enforcement | Production-mode gating (`THEMIS_PRODUCTION_MODE`), no insecure fallback in production, explicit error taxonomy | Security regression suite + audit evidence in `audit/` |
407+
| **T2** | Transaction integrity, input/schema validation, secure defaults in persistence/indexing | Safe failure semantics, WAL/recovery correctness, resource limits and quota enforcement | CTest coverage for recovery/conflict/security-relevant paths |
408+
| **T3** | Protocol authentication, request validation, DoS/rate limiting, tenant isolation | TLS/mTLS profile checks, strict parser limits, bounded request/body/stream settings | API security tests + protocol-specific focused tests |
409+
| **T4** | Model/plugin operation policy checks, content sanitization, output filtering | Runtime capability gates, bounded resource budgets, deterministic fallback behavior | Feature security tests + policy conformance checks |
410+
| **T5** | Signature verification, sandboxing, capability-scoped RPC, provenance checks | No implicit trust of plugin manifests, explicit allowlist/denylist, kill-switch support | Plugin verification tests + signed artifact validation |
411+
412+
### Promotion and Change Management
413+
414+
- Any component moving toward a lower-trust tier (for example, from T4 to T5) must include an explicit threat model update and new boundary tests.
415+
- Any component requiring higher trust (for example, T3 -> T2) requires architecture review plus security maintainer approval.
416+
- Public interfaces crossing tier boundaries must document auth model, input contract, error behavior, and audit events.
417+
418+
This tiering is normative for architecture changes and complements the controls in [SECURITY.md](SECURITY.md), [AUDIT.md](AUDIT.md), and [GOVERNANCE.md](GOVERNANCE.md).
419+
420+
---
421+
377422
## Namespace Organization
378423

379424
### Hierarchy

CONTRIBUTING.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,20 @@ ctest --output-on-failure
836836

837837
</details>
838838

839+
### Tier-Based Security & Hardening Checklist (Required)
840+
841+
For all changes that touch runtime behavior, contributors must include tier information and security evidence.
842+
843+
1. Identify impacted tier(s) using the model in [ARCHITECTURE.md](ARCHITECTURE.md#security--hardening-tiering-model-core-module---plugin).
844+
2. Document trust-boundary crossings (for example T3 -> T2, T5 -> T4 broker calls).
845+
3. Confirm boundary controls on changed ingress/extension paths:
846+
- authentication and authorization
847+
- input validation and parser limits
848+
- rate limiting and quota behavior
849+
- audit event emission
850+
4. Add or update tests for changed boundaries, especially for T3/T4/T5 code paths.
851+
5. If a change effectively increases trust or privilege, include explicit architecture/security maintainer approval in the PR.
852+
839853
### PR Description Template
840854

841855
```markdown
@@ -863,6 +877,9 @@ Describe the tests you ran to verify your changes.
863877
- [ ] I have run `./scripts/check-quality.sh` and fixed all issues
864878
- [ ] I did not introduce Simulation/Stub/Mockup or legacy compatibility paths without explicit human approval
865879
- [ ] Any approved non-production path is explicitly human-marked (Reason, Activation, Production Delta, Approved By, Removal Target)
880+
- [ ] I classified impacted Security Tier(s) and documented trust-boundary crossings
881+
- [ ] I verified boundary controls (AuthN/AuthZ, validation, rate limits, audit) for affected T3/T4/T5 paths
882+
- [ ] I added boundary-focused tests for tier-crossing behavior or documented why not applicable
866883
```
867884

868885
### Reviewer Gate: Non-Production Paths

CTEST.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ Diese Pfade dienen als Referenz für den Root-Dokument-Abgleich zwischen
3636

3737
---
3838

39+
## Tier-zu-Test-Mapping (Security & Hardening)
40+
41+
Dieses Mapping operationalisiert das Security-Tiering aus
42+
[ARCHITECTURE.md](ARCHITECTURE.md#security--hardening-tiering-model-core-module---plugin)
43+
und dient als Mindest-Nachweis fuer tier-uebergreifende PRs.
44+
45+
| Tier | Fokus | Mindest-Verifikation (Beispielpfad) |
46+
|---|---|---|
47+
| **T0: Trusted Core** | Bootstrap- und Kerninvarianten, Wire/Core-Basis | `ctest --preset windows-release --output-on-failure -R "^(BuildInfoTests|EditionManagerTests|ThemisWireProtocolV1Tests)$"` |
48+
| **T1: Security & Platform Services** | AuthN/AuthZ, Krypto, Policy, Audit | `ctest --preset windows-release --output-on-failure -R "^(JWTValidatorTests|LDAPAuthenticatorTests|LDAPConnectionPoolTests|TOTPReplayCacheTests|TOTPSecretEncryptionTests)$"` |
49+
| **T2: Data Plane Engines** | Integritaet, Recovery, Transaktionssicherheit | `ctest --preset transaction-tests-release --output-on-failure --parallel 2` |
50+
| **T3: Interface & Protocol Edge** | Ingress-Sicherheit, Parser-/Rate-Limits, Tenant-Grenzen | `ctest --preset windows-release --output-on-failure -R "^(ThemisWireProtocolV1Tests|RateLimitingImprovementsFocusedTests|.*ApiHandler.*)$"` |
51+
| **T4: Managed Extension Runtime** | Runtime-Capability-Gates, Sanitization, Modellpfade | `ctest --preset windows-release --output-on-failure -R "^(LlmDeploymentPluginFocusedTests|LegalDomainFocusedTests|LegalExtractionFocusedTests)$"` |
52+
| **T5: Plugin Boundary** | Signatur, Provenienz, Sandbox, Plugin-Policy | `ctest --preset windows-release --output-on-failure -R "^(PluginManagerFocusedTests|PluginSecurityCRLOCSPTests|PluginSecurityAuditFocusedTests|PluginMarketplaceManifestFocusedTests|PluginManagerComprehensiveFocusedTests)$"` |
53+
54+
### Review-Regel fuer tier-relevante PRs
55+
56+
1. PR beschreibt betroffene Tier(s) und Trust-Boundary-Crossings.
57+
2. Mindestens ein passender CTest-Nachweis aus der Tabelle wird in der PR referenziert.
58+
3. Bei T3/T4/T5-Aenderungen sind Boundary-Kontrollen (AuthN/AuthZ, Validation, Rate Limits, Audit) explizit nachzuweisen.
59+
60+
Hinweis: Die RegEx-Beispiele sind Mindestpfade und duerfen durch fokussierte Modul-Tests erweitert werden.
61+
62+
---
63+
3964
## Ergebnis des Komplett-Runs (Istzustand 30.03.2026)
4065

4166
```

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ ThemisDB is organised into tracked source modules under `src/`, grouped into fou
172172
→ Full architecture reference: [ARCHITECTURE.md](ARCHITECTURE.md)
173173
→ Module list and status: [ROADMAP.md](ROADMAP.md)
174174

175+
### Security Tiering Quick Reference
176+
177+
For security and hardening reviews, use the tier model (T0 Trusted Core -> T5 Plugin Boundary) as the default classification.
178+
179+
| What | Where |
180+
|---|---|
181+
| Tier model and trust boundaries | [ARCHITECTURE.md](ARCHITECTURE.md#security--hardening-tiering-model-core-module---plugin) |
182+
| Normative security rules per tier | [SECURITY.md](SECURITY.md#-security-tiering-model-normativ) |
183+
| Contributor checklist for tier/boundary evidence | [CONTRIBUTING.md](CONTRIBUTING.md#tier-based-security--hardening-checklist-required) |
184+
| PR template section (required for runtime changes) | [.github/pull_request_template.md](.github/pull_request_template.md#security-tiering-impact-required-for-runtime-changes) |
185+
| Tier-to-test verification mapping | [CTEST.md](CTEST.md#tier-zu-test-mapping-security--hardening) |
186+
187+
Rule of thumb: architecture is layered, but security acceptance is tier-based.
188+
175189
---
176190

177191
## Documentation

SECURITY.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@ gemeinsame Baseline:
121121

122122
---
123123

124+
## 🧱 Security Tiering Model (Normativ)
125+
126+
ThemisDB uses a security tier model from trusted core modules to least-trusted plugin boundaries. This model is normative for architecture, implementation, and release review.
127+
128+
Primary reference: [ARCHITECTURE.md](ARCHITECTURE.md#security--hardening-tiering-model-core-module---plugin)
129+
130+
| Tier | Scope | Mandatory Control Focus |
131+
|---|---|---|
132+
| **T0: Trusted Core** | Core bootstrap and trust anchors | Startup invariants, memory/lifecycle correctness, anti-tamper checks |
133+
| **T1: Security & Platform Services** | Identity, crypto, policy, config, audit | RBAC, key isolation, immutable audit guarantees, production-mode enforcement |
134+
| **T2: Data Plane Engines** | Query, transaction, storage/index, replication/sharding | Data integrity, recovery safety, quota/resource enforcement |
135+
| **T3: Interface & Protocol Edge** | HTTP/gRPC/Wire ingress and protocol handlers | AuthN/AuthZ at ingress, parser bounds, DoS protection, tenant isolation |
136+
| **T4: Managed Extension Runtime** | In-tree extensions (LLM/content/model runtime) | Capability gating, bounded runtime budgets, sanitized inputs/outputs |
137+
| **T5: Plugin Boundary** | Dynamic plugins and adapters | Signature/provenance checks, sandboxing, capability-scoped invocation |
138+
139+
### Mandatory Tier Rules
140+
141+
1. Dependencies are one-way: higher-numbered tiers can depend on lower-numbered tiers, never vice versa.
142+
2. T5 code must never directly invoke privileged T0/T1 internals without brokered policy checks.
143+
3. All T3/T4/T5 entry points require authentication, authorization, input validation, rate limiting, and audit events.
144+
4. Security decisions are fail-closed: uncertainty in auth/policy/config must reject the operation.
145+
5. Every tier-crossing public interface must document auth model, input contract, error behavior, and audit semantics.
146+
147+
### Tier Evidence Required in PRs
148+
149+
- State affected tier(s) and trust-boundary crossings in the PR description.
150+
- Provide at least one boundary-focused test for changed T3/T4/T5 code paths.
151+
- If a change alters effective trust level, include threat model delta and maintainer sign-off.
152+
153+
---
154+
124155
## 🛡️ Security Measures
125156

126157
ThemisDB implements **defense-in-depth** security across all layers:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# CTest LLM Adapter Fix Plan (2026-06-18)
2+
3+
## Goal
4+
Repair the next full-CTest blocker in the legal ingestion LLM adapter without regressing the existing regex fallback behavior.
5+
6+
## Affected Files
7+
- src/ingestion/llm_adapter.cpp
8+
- include/ingestion/llm_adapter.h
9+
- tests/ingestion/test_ingestion_llm_adapter.cpp (read-only unless production fix proves insufficient)
10+
- tests/legal/test_legal_extraction.cpp (validation only)
11+
12+
## Acceptance Criteria
13+
- `IngestionLlmAdapterFocusedTests` passes.
14+
- Explicitly configured missing model path throws in Phase-2 tests.
15+
- Readable configured model path reports available and yields a non-empty extractor function.
16+
- No configured model still keeps regex fallback behavior.
17+
- `LegalExtractionFocusedTests` stays green.
18+
19+
## Test Scope
20+
- `cmake --build --preset windows-release --target test_ingestion_llm_adapter_focused --parallel 16`
21+
- `ctest --preset windows-release --output-on-failure -R "IngestionLlmAdapterFocusedTests" -j 1`
22+
- `cmake --build --preset windows-release --target test_legal_extraction_focused --parallel 16`
23+
- `ctest --preset windows-release --output-on-failure -R "LegalExtractionFocusedTests" -j 1`

include/core/concerns/i_tracer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ class ITracer {
5252
*/
5353
virtual void setAttribute(const std::string& key, const std::string& value) = 0;
5454

55+
/**
56+
* @brief Set a string attribute from a C string literal or buffer.
57+
*
58+
* This overload prevents C string arguments from binding to the bool
59+
* overload through implicit pointer-to-bool conversion.
60+
* A null pointer is normalised to the empty string.
61+
*
62+
* @param key Attribute name (OpenTelemetry semantic convention recommended).
63+
* @param value Null-terminated string value, or null for empty.
64+
*/
65+
void setAttribute(const std::string& key, const char* value) {
66+
setAttribute(key, std::string(value != nullptr ? value : ""));
67+
}
68+
5569
/**
5670
* @brief Set an integer attribute on the span.
5771
* @param key Attribute name.

include/ingestion/ingestion_sinks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ class GraphStoreSinkAdapter : public IGraphWriter {
373373
std::string node_key_prefix_;
374374
mutable std::mutex mtx_;
375375
std::unordered_set<std::string> written_node_ids_;
376+
std::unordered_set<std::string> written_edge_ids_;
376377
};
377378

378379
/**

include/ingestion/llm_adapter.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,13 @@ class LegalLlmAdapter {
151151
/**
152152
* @brief Check whether a real LLM backend is available.
153153
*
154-
* Delegates to `ITextGenerationBackend::isAvailable()` on the injected
155-
* backend. Returns `false` when using the default `NullTextGenerationBackend`.
154+
* When a `model_path` is configured, this validates that the referenced
155+
* model file is accessible for reading. Without a configured model path,
156+
* the check delegates to `ITextGenerationBackend::isAvailable()` on the
157+
* injected backend.
156158
*
157-
* @return true if the backend is ready for inference
159+
* @return true if a configured model file is readable or the injected
160+
* backend reports that it is ready for inference
158161
*/
159162
bool isLlmAvailable() const;
160163

@@ -168,10 +171,16 @@ class LegalLlmAdapter {
168171
* inference engine with a structured German legal extraction prompt and
169172
* parses the JSON response into a `DeonticExtraction`.
170173
*
174+
* Throws when a model path is explicitly configured but the file cannot
175+
* be opened. This keeps misconfigured deployments fail-closed instead of
176+
* silently degrading to regex extraction.
177+
*
171178
* The returned function captures the model configuration by value so it
172179
* remains valid independently of the `LegalLlmAdapter` instance lifetime.
173180
*
174181
* @return ExtractorFn (may be empty in Phase 1 / when no model is set)
182+
* @throws std::runtime_error if `config_.model_path` is set but the model
183+
* file is not readable
175184
*/
176185
DeonticExtractor::ExtractorFn buildExtractorFn() const;
177186

0 commit comments

Comments
 (0)