crp.advanced¶
Auto-generated reference for the crp.advanced subpackage.
advanced¶
crp.advanced ¶
Advanced features - hierarchical, parallel, auto-ingest, CQS, meta-learning.
IngestFact dataclass ¶
Lightweight fact from per-chunk extraction.
IngestResult dataclass ¶
Summary returned by auto_ingest().
ContextHungerSignal dataclass ¶
Single context hunger signal detected from LLM output.
Attributes:
| Name | Type | Description |
|---|---|---|
signal_type | str | Signal category ("hedging", "reference_miss", or "repetition"). |
strength | float | Normalised signal strength in the range [0.0, 1.0]. |
topic | str | Short text snippet identifying the affected topic. |
window_id | str | Identifier of the window that produced the generation. |
token_offset | int | Token offset where the signal was observed. |
details | dict[str, Any] | Additional structured details about the signal. |
CQSDetector ¶
Detect implicit context hunger from LLM generation output.
detect_context_hunger(generation_text, window_id='', tokens_generated=None) ¶
Scan generation output for context hunger signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generation_text | str | Raw text produced by the LLM. | required |
window_id | str | Identifier of the originating window. | '' |
tokens_generated | int | None | Number of tokens generated, if known. | None |
Returns:
| Type | Description |
|---|---|
list[ContextHungerSignal] | List of detected signals (may be empty). |
respond_to_context_hunger(signals, tokens_generated=0) ¶
Determine action based on detected signals.
§12.4: If max(strength) >= 0.8 AND tokens < 500 → abandon + redispatch. Otherwise → enrich next window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signals | list[ContextHungerSignal] | Signals returned by :meth: | required |
tokens_generated | int | Number of tokens already generated for the current response. | 0 |
Returns:
| Type | Description |
|---|---|
CQSResponse | A |
CQSResponse | enrichment topics. |
CQSResponse dataclass ¶
Response after CQS processing.
Attributes:
| Name | Type | Description |
|---|---|---|
action | str | Recommended action ("abandon_and_redispatch", "enrich_next", or "none"). |
signals | list[ContextHungerSignal] | Detected hunger signals that led to the action. |
enrichment_budget | int | Suggested token budget for the next enrichment pass. |
enrichment_topics | list[str] | Topics to target during enrichment. |
ConsistencyIssue dataclass ¶
Single consistency issue found during validation.
Attributes:
| Name | Type | Description |
|---|---|---|
issue_type | str | Category of issue, such as |
description | str | Human-readable explanation of the issue. |
severity | str | Severity label ("low", "medium", or "high"). |
windows | list[int] | None | Window numbers involved in the issue, if known. |
confirmed | bool | Whether the issue has been confirmed by a higher tier. |
facts | list[Any] | None | Indices or identifiers of facts involved in the issue. |
CrossWindowValidator ¶
Three-tier cross-window consistency validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dispatch_fn | Callable[[str, str], tuple[str, Any]] | None | Callable accepting | None |
embedding_fn | Callable[[str], list[float]] | None | Callable that returns an embedding vector for a string; used for semantic contradiction detection. May be None. | None |
config | ReviewCycleConfig | None |
| None |
extraction_based_validation(facts, window_outputs=None, planned_sections=None) ¶
Zero-cost structural validation. Always runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
facts | list[dict[str, Any]] | List of fact dictionaries to validate. | required |
window_outputs | list[str] | None | Optional aggregated window outputs, used for structural completeness checks. | None |
planned_sections | list[str] | None | Optional list of planned sections to verify. | None |
Returns:
| Type | Description |
|---|---|
ValidationResult | A Tier 1 |
targeted_llm_validation(tier1_issues) ¶
Targeted LLM validation for Tier 1 issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tier1_issues | list[ConsistencyIssue] | Issues produced by Tier 1 validation. | required |
Returns:
| Type | Description |
|---|---|
ValidationResult | A Tier 2 |
full_llm_review(accumulated_output, task_intent, top_facts=None) ¶
Dedicated review window with top facts + document map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accumulated_output | str | Full generated output to review. | required |
task_intent | str | Original task description. | required |
top_facts | list[dict[str, Any]] | None | Optional top facts to ground the review. | None |
Returns:
| Type | Description |
|---|---|
ValidationResult | A Tier 3 |
assess_review_capability() ¶
Probe model to determine max validation tier (1, 2, or 3).
Returns:
| Type | Description |
|---|---|
int | Highest validation tier the configured model can support. |
apply_corrections(issues, task_intent='', blockers=None) ¶
Apply corrections based on config mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
issues | list[ConsistencyIssue] | Issues to process. | required |
task_intent | str | Original task description (unused by current logic). | '' |
blockers | list[str] | None | Optional list to populate with flagged issue summaries when | None |
Returns:
| Type | Description |
|---|---|
list[str] | List of correction actions taken. |
should_run_tier(window_index, tier) ¶
Check if a validation tier should run at this window index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_index | int | Current window number. | required |
tier | int | Tier to evaluate (1, 2, or 3). | required |
Returns:
| Type | Description |
|---|---|
bool | True when the tier is enabled and the window index aligns with its |
bool | configured interval. |
ValidationResult dataclass ¶
Output of a validation tier.
Attributes:
| Name | Type | Description |
|---|---|---|
tier | int | Tier number (1, 2, or 3). |
issues | list[ConsistencyIssue] | Issues produced by this tier. |
timestamp | float | Unix timestamp of the validation run. |
window_range | tuple[int, int] | Inclusive window range that was validated. |
CurationConfig dataclass ¶
Configuration for LLM curation.
LLMContextCurator ¶
LLM-driven context curation with progressive understanding.
current_synthesis property ¶
Return the current synthesis.
evolution_count property ¶
Return the current evolution count.
should_curate(window_index) ¶
Check if curation should run at this window.
curate(window_index, top_facts, recent_output_summary='') ¶
Run curation (initial or progressive).
Returns new synthesis or None if dispatch unavailable.
format_for_envelope() ¶
Format current synthesis for envelope injection (Section 1.5).
to_dict() ¶
Serialize the curator state, history and configuration to a dict.
LLMSynthesis dataclass ¶
Curated synthesis from LLM review of accumulated facts.
to_dict() ¶
Serialize this synthesis to a JSON-ready dict.
from_dict(data) classmethod ¶
Create a new instance from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | The data value. | required |
Returns:
| Type | Description |
|---|---|
LLMSynthesis |
|
FeedbackEntry dataclass ¶
Single human feedback action.
FeedbackLoop ¶
Human-in-the-loop corrections for facts in warm state.
entry_count property ¶
Return the current entry count.
override_fact(fact_id, corrected_text, reason='') ¶
Replace fact text with human-provided correction.
boost_confidence(fact_id, delta=0.1, reason='') ¶
Increase fact confidence based on human validation.
penalize_confidence(fact_id, delta=-0.2, reason='') ¶
Decrease fact confidence based on human rejection.
reject_fact(fact_id, reason='') ¶
Mark fact as rejected (confidence → 0).
get_adjusted_confidence(fact_id, base_confidence) ¶
Get confidence after applying all feedback adjustments.
get_entries_for_fact(fact_id) ¶
Return all feedback entries associated with fact_id.
to_dict() ¶
Serialize all feedback entries and cumulative adjustments to a dict.
HierarchicalPlan dataclass ¶
Plan for hierarchical processing.
HierarchicalProcessor ¶
Map-reduce-validate pattern for oversized inputs.
plan(total_tokens, config=None) ¶
Create a hierarchical processing plan.
map_phase(segments, task_intent) ¶
MAP: Process each segment independently.
reduce_phase(syntheses, task_intent, fan_in=DEFAULT_FAN_IN) ¶
REDUCE: Iteratively merge syntheses until ≤ fan_in remain.
hierarchical_dispatch(task_intent, large_input, config=None) ¶
Full map-reduce-validate pipeline for oversized input.
Returns (final_syntheses, plan).
MetaLearningEngine ¶
ORC + ICML + RTL meta-learning capabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dispatch_fn | Callable[[str, str], tuple[str, Any]] | None | Callable accepting | None |
model_capability | int | Integer capability level of the active model. | 1 |
config | MetaLearningConfig | None |
| None |
trace_count property ¶
Return the number of traces currently stored in the RTL.
should_use_orc(task_complexity=3, resource_pressure='NONE', probe_quality=0.0) ¶
Gate check for ORC activation.
Gate 1: resource_pressure >= HIGH → False Gate 2: model_capability >= task_complexity → False Gate 3: probe_quality >= 0.7 → False (ORC unnecessary)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_complexity | int | Estimated complexity of the task on a 1–5 scale. | 3 |
resource_pressure | str | Resource pressure label ("NONE", "LOW", "MODERATE", "HIGH", "CRITICAL"). | 'NONE' |
probe_quality | float | Quality score from a zero-shot probe; high values indicate ORC is unnecessary. | 0.0 |
Returns:
| Type | Description |
|---|---|
bool | True when ORC should be used to break the task into steps. |
orchestrated_reasoning(task_intent, task_complexity=3, resource_pressure='NONE') ¶
Decompose and execute an orchestrated reasoning chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_intent | str | Natural-language description of the task to solve. | required |
task_complexity | int | Estimated complexity of the task. | 3 |
resource_pressure | str | Resource pressure label; reduces the number of allowed steps under higher pressure. | 'NONE' |
Returns:
| Type | Description |
|---|---|
ORCResult | An |
ORCResult | answer. |
build_reasoning_scaffold(task_intent) ¶
Build a reasoning scaffold adapted to model capability.
Capability ≤ 1 (0.5B-1B): Full step-by-step template Capability ≤ 2 (2B-7B): Light approach Capability > 2: No scaffolding
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_intent | str | Task description to scaffold. | required |
Returns:
| Type | Description |
|---|---|
str | Scaffold string to prepend to the prompt, or an empty string when |
str | scaffolding is disabled. |
build_metacognitive_envelope(task_intent, base_envelope='', few_shot_traces=None) ¶
Build an envelope with reasoning scaffold + few-shot examples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_intent | str | Task description. | required |
base_envelope | str | Existing envelope text to preserve, if any. | '' |
few_shot_traces | list[ReasoningTrace] | None | Optional explicit traces to include as examples. When omitted, the RTL is queried for matching traces. | None |
Returns:
| Type | Description |
|---|---|
str | Combined envelope string containing the base envelope, scaffold, |
str | and any few-shot examples. |
store_trace(trace) ¶
Store a reasoning trace if quality meets the configured threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace | ReasoningTrace |
| required |
Returns:
| Type | Description |
|---|---|
bool | True when the trace was stored, False when RTL is disabled or the |
bool | trace quality is too low. |
to_dict() ¶
Serialize the engine and its trace library.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with the trace library and active configuration. |
ORCResult dataclass ¶
Result of orchestrated reasoning chain.
Attributes:
| Name | Type | Description |
|---|---|---|
steps_completed | int | Number of steps that produced output. |
steps_total | int | Total number of steps in the chain. |
final_output | str | Synthesised final response. |
step_outputs | list[str] | Raw output from each executed step. |
quality_score | float | Estimated quality score for the chain. |
trace | ReasoningTrace | None | Optional |
ReasoningTrace dataclass ¶
Complete reasoning trace for RTL storage.
Attributes:
| Name | Type | Description |
|---|---|---|
trace_id | str | Unique identifier for the trace. |
task_type | str | Category of task this trace addresses. |
task_summary | str | Short summary of the original task. |
steps | list[ReasoningStep] | Ordered reasoning steps that produced the result. |
model_class | str | Model capability class (e.g., "0.5B-1B", "2B-7B", "7B+"). |
quality_score | float | Quality score assigned to the trace. |
created_at | float | Unix timestamp when the trace was created. |
usage_count | int | Number of times the trace has been retrieved. |
to_dict() ¶
Serialize the trace to a JSON-friendly dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary representation of the trace, including nested steps. |
from_dict(data) classmethod ¶
Restore a ReasoningTrace from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | Serialized trace data produced by :meth: | required |
Returns:
| Type | Description |
|---|---|
ReasoningTrace | Reconstructed |
FanOutResult dataclass ¶
Result of one parallel dispatch.
FanOutTask dataclass ¶
One independent task for parallel dispatch.
ParallelFanOut ¶
Dispatch N independent windows and merge results.
Algorithm (§4.4): 1. Identify N independent tasks 2. Construct independent envelopes from warm_state 3. Dispatch all N windows (sequential fallback if no async) 4. Collect all N outputs 5. Extract facts from all N outputs 6. Merge facts into warm_state 7. Update DAG with fan-out edges 8. Continue with next dependent task
AssessmentResult dataclass ¶
Output from post-generation self-assessment.
ReviewCycleManager ¶
Active LLM review cycles - planning, checkpoint, assessment.
pre_generation_plan(task_intent, predicted_chain_length=0) ¶
Generate document plan when chain > 5 windows.
Returns None if chain is short or no dispatch_fn.
checkpoint_review(window_index, review_interval=20, task_intent='', top_facts=None, gap_summary='') ¶
Periodic review at checkpoint windows.
Gate: model_capability < 3 → None Gate: window_index not at interval → None
post_generation_assessment(accumulated_output, task_intent) ¶
Score output quality and flag issues.
Weak model → basic heuristic scoring. Strong model → full LLM self-assessment.
targeted_regeneration(issues, task_intent) ¶
Re-generate targeted fixes for each issue (capped at max_corrections).
ReviewGuidance dataclass ¶
Output from a checkpoint review.
QualityTier ¶
Bases: IntEnum
Quality tiers - S (single window) through D (>1000 windows at 128K ctx).
ScaleModeSelector ¶
Auto-configure session based on quality tier and model capability.
configure_session(estimated_tokens, model_capability=1) ¶
Auto-configure session based on input size and model capability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimated_tokens | int | Total estimated input tokens. | required |
model_capability | int | Assessed model capability (1, 2, or 3). | 1 |
Returns:
| Type | Description |
|---|---|
SessionConfig | SessionConfig with all parameters set. |
SessionConfig dataclass ¶
Auto-configured session parameters.
SourceGroundingEngine ¶
Store and retrieve verbatim source passages for high-confidence facts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count_tokens | Callable[[str], int] | None | Callable that returns the token count for a piece of text. Defaults to a rough character-based estimate when None. | None |
passage_count property ¶
Return the number of stored passages.
store_passage(passage, fact_confidence=0.0) ¶
Store a passage if its linked fact has confidence ≥ threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passage | SourcePassage | Passage to store. | required |
fact_confidence | float | Confidence score of the fact linked to the passage. | 0.0 |
Returns:
| Type | Description |
|---|---|
bool | True if the passage was stored, False if it was below the threshold. |
get_passages_for_fact(fact_id) ¶
Retrieve all source passages linked to a fact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fact_id | str | Identifier of the fact. | required |
Returns:
| Type | Description |
|---|---|
list[SourcePassage] | List of passages linked to the fact that remain in storage. |
build_source_grounded_envelope(scored_facts, budget_tokens, quality_tier='B') ¶
Build an envelope with source passages allocated by tier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scored_facts | list[dict[str, Any]] | Sorted list of fact dictionaries such as | required |
budget_tokens | int | Total envelope budget in tokens. | required |
quality_tier | str | Quality tier (S/A/B/C/D) controlling the fact/source budget split. | 'B' |
Returns:
| Type | Description |
|---|---|
tuple[list[dict[str, Any]], list[SourcePassage]] | A tuple of |
format_envelope_section(fact, passages) ¶
Format a fact with its source passages for envelope inclusion.
Format::
- {fact text} - Window N
↳ [SOURCE: Window N, tokens X-Y]
"{verbatim original text}"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fact | dict[str, Any] | Fact dictionary containing at least | required |
passages | list[SourcePassage] | Passages linked to the fact. | required |
Returns:
| Type | Description |
|---|---|
str | Formatted envelope section string. |
to_dict() ¶
Serialize the engine for persistence.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with all passages and fact-to-passage mappings. |
from_dict(data, count_tokens=None) classmethod ¶
Restore the engine from serialized state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | Serialized state produced by :meth: | required |
count_tokens | Callable[[str], int] | None | Optional token-counting callable; defaults to the engine's fallback estimator if None. | None |
Returns:
| Type | Description |
|---|---|
SourceGroundingEngine | Reconstructed |
SourcePassage dataclass ¶
Verbatim passage from the original input linked to facts.
Attributes:
| Name | Type | Description |
|---|---|---|
passage_id | str | Unique identifier for the passage. |
text | str | Verbatim source text. |
source_window | int | Window number from which the passage was extracted. |
token_offset_start | int | Start token offset within the source window. |
token_offset_end | int | End token offset within the source window. |
linked_fact_ids | list[str] | Identifiers of facts this passage grounds. |
token_count | int | Token count of |
relevance_score | float | Relevance score for retrieval ranking. |
to_dict() ¶
Serialize the passage to a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with all passage fields. |
from_dict(data) classmethod ¶
Restore a passage from a serialized dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | Serialized passage produced by :meth: | required |
Returns:
| Type | Description |
|---|---|
SourcePassage | Reconstructed |
advanced.auto_ingest¶
crp.advanced.auto_ingest ¶
Auto-ingest - oversized input handling with structure-aware chunking (§4.6).
Triggers when system_tokens + task_tokens > context_window - gen_reserve. Zero LLM cost by default: uses graduated extraction (stages 1-5) per chunk, then reconciles boundary duplicates/complements via embedding similarity.
ProtectedSpan dataclass ¶
Region that must not be split mid-structure.
Chunk dataclass ¶
One chunk of the oversized input.
IngestResult dataclass ¶
Summary returned by auto_ingest().
IngestFact dataclass ¶
Lightweight fact from per-chunk extraction.
detect_protected_structures(text) ¶
Find code blocks, tables, JSON blocks, numbered lists.
merge_overlapping_spans(spans) ¶
Merge overlapping/adjacent protected spans.
split_at_boundaries(text, chunk_size_chars, overlap_chars, protected_spans) ¶
Split text into chunks respecting protected structures.
reconcile_chunk_boundaries(per_chunk_facts, embedding_fn=None) ¶
Deduplicate/merge facts at chunk boundaries.
- cosine_similarity > 0.95 → duplicate → skip
- cosine_similarity > 0.75 AND token_overlap > 0.3 → complement → merge
- Otherwise → new fact → keep
merge_fact_texts(a, b) ¶
Merge two complementary fact texts, keeping unique content.
auto_ingest(system_prompt, task_input, task_intent_text, context_window, count_tokens, extract_fn=None, embedding_fn=None, store_raw_fn=None, session_id='') ¶
Handle oversized inputs with structure-aware chunking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_prompt | str | The system prompt (not modified). | required |
task_input | str | Raw oversized input text. | required |
task_intent_text | str | Short description of task intent. | required |
context_window | int | Total context window in tokens. | required |
count_tokens | Callable[[str], int] | Token counting function. | required |
extract_fn | Callable[[str, str], list[IngestFact]] | None | Per-chunk fact extractor (stages 1-5). If None, returns dummy facts. | None |
embedding_fn | Callable[[str], list[float]] | None | Optional embedding function for reconciliation. | None |
store_raw_fn | Callable[[str, str], None] | None | Optional function to store raw input in cold storage. | None |
session_id | str | Current session ID. | '' |
Returns:
| Type | Description |
|---|---|
tuple[list[IngestFact], IngestResult] | (reconciled_facts, ingest_result) |
advanced.cqs¶
crp.advanced.cqs ¶
CQS - Context Quality Signaling, detect LLM context hunger (§12; CRP-SPEC-019).
Three signal types: hedging, reference_miss, repetition. Preserves Model Ignorance (Axiom 4): signals are detected structurally from generation output, never by injecting meta-protocol into the LLM.
Relevant specifications
- CRP specification §12: Context Quality Signaling
- CRP-SPEC-019: Cognitive Quality Recognition (CQR)
ContextHungerSignal dataclass ¶
Single context hunger signal detected from LLM output.
Attributes:
| Name | Type | Description |
|---|---|---|
signal_type | str | Signal category ("hedging", "reference_miss", or "repetition"). |
strength | float | Normalised signal strength in the range [0.0, 1.0]. |
topic | str | Short text snippet identifying the affected topic. |
window_id | str | Identifier of the window that produced the generation. |
token_offset | int | Token offset where the signal was observed. |
details | dict[str, Any] | Additional structured details about the signal. |
CQSResponse dataclass ¶
Response after CQS processing.
Attributes:
| Name | Type | Description |
|---|---|---|
action | str | Recommended action ("abandon_and_redispatch", "enrich_next", or "none"). |
signals | list[ContextHungerSignal] | Detected hunger signals that led to the action. |
enrichment_budget | int | Suggested token budget for the next enrichment pass. |
enrichment_topics | list[str] | Topics to target during enrichment. |
CQSDetector ¶
Detect implicit context hunger from LLM generation output.
detect_context_hunger(generation_text, window_id='', tokens_generated=None) ¶
Scan generation output for context hunger signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generation_text | str | Raw text produced by the LLM. | required |
window_id | str | Identifier of the originating window. | '' |
tokens_generated | int | None | Number of tokens generated, if known. | None |
Returns:
| Type | Description |
|---|---|
list[ContextHungerSignal] | List of detected signals (may be empty). |
respond_to_context_hunger(signals, tokens_generated=0) ¶
Determine action based on detected signals.
§12.4: If max(strength) >= 0.8 AND tokens < 500 → abandon + redispatch. Otherwise → enrich next window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signals | list[ContextHungerSignal] | Signals returned by :meth: | required |
tokens_generated | int | Number of tokens already generated for the current response. | 0 |
Returns:
| Type | Description |
|---|---|
CQSResponse | A |
CQSResponse | enrichment topics. |
advanced.cross_window¶
crp.advanced.cross_window ¶
Cross-window validation - 3-tier consistency checks (§13; CRP-SPEC-019).
Tier 1: Extraction-based (always, zero LLM cost) Tier 2: LLM-targeted (2B+ models) Tier 3: Full LLM review (7B+ models)
Relevant specifications
- CRP specification §13: Cross-window validation
- CRP-SPEC-019: Cognitive Quality Recognition (CQR)
ConsistencyIssue dataclass ¶
Single consistency issue found during validation.
Attributes:
| Name | Type | Description |
|---|---|---|
issue_type | str | Category of issue, such as |
description | str | Human-readable explanation of the issue. |
severity | str | Severity label ("low", "medium", or "high"). |
windows | list[int] | None | Window numbers involved in the issue, if known. |
confirmed | bool | Whether the issue has been confirmed by a higher tier. |
facts | list[Any] | None | Indices or identifiers of facts involved in the issue. |
ValidationResult dataclass ¶
Output of a validation tier.
Attributes:
| Name | Type | Description |
|---|---|---|
tier | int | Tier number (1, 2, or 3). |
issues | list[ConsistencyIssue] | Issues produced by this tier. |
timestamp | float | Unix timestamp of the validation run. |
window_range | tuple[int, int] | Inclusive window range that was validated. |
ReviewCycleConfig dataclass ¶
Configuration for review cycles.
Attributes:
| Name | Type | Description |
|---|---|---|
enabled | bool | Master switch for review cycles. |
tier_1_interval | int | Window interval between Tier 1 validations. |
tier_2_enabled | bool | Enable Tier 2 LLM-targeted validation. |
tier_2_interval | int | Window interval between Tier 2 validations. |
tier_3_enabled | bool | Enable Tier 3 full LLM review. |
tier_3_interval | int | Window interval between Tier 3 validations. |
tier_3_min_model_capability | int | Minimum model capability for Tier 3. |
correction_mode | str | Correction behaviour ("flag" or "correct"). |
max_correction_windows | int | Maximum windows to attempt correcting. |
CrossWindowValidator ¶
Three-tier cross-window consistency validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dispatch_fn | Callable[[str, str], tuple[str, Any]] | None | Callable accepting | None |
embedding_fn | Callable[[str], list[float]] | None | Callable that returns an embedding vector for a string; used for semantic contradiction detection. May be None. | None |
config | ReviewCycleConfig | None |
| None |
extraction_based_validation(facts, window_outputs=None, planned_sections=None) ¶
Zero-cost structural validation. Always runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
facts | list[dict[str, Any]] | List of fact dictionaries to validate. | required |
window_outputs | list[str] | None | Optional aggregated window outputs, used for structural completeness checks. | None |
planned_sections | list[str] | None | Optional list of planned sections to verify. | None |
Returns:
| Type | Description |
|---|---|
ValidationResult | A Tier 1 |
targeted_llm_validation(tier1_issues) ¶
Targeted LLM validation for Tier 1 issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tier1_issues | list[ConsistencyIssue] | Issues produced by Tier 1 validation. | required |
Returns:
| Type | Description |
|---|---|
ValidationResult | A Tier 2 |
full_llm_review(accumulated_output, task_intent, top_facts=None) ¶
Dedicated review window with top facts + document map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accumulated_output | str | Full generated output to review. | required |
task_intent | str | Original task description. | required |
top_facts | list[dict[str, Any]] | None | Optional top facts to ground the review. | None |
Returns:
| Type | Description |
|---|---|
ValidationResult | A Tier 3 |
assess_review_capability() ¶
Probe model to determine max validation tier (1, 2, or 3).
Returns:
| Type | Description |
|---|---|
int | Highest validation tier the configured model can support. |
apply_corrections(issues, task_intent='', blockers=None) ¶
Apply corrections based on config mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
issues | list[ConsistencyIssue] | Issues to process. | required |
task_intent | str | Original task description (unused by current logic). | '' |
blockers | list[str] | None | Optional list to populate with flagged issue summaries when | None |
Returns:
| Type | Description |
|---|---|
list[str] | List of correction actions taken. |
should_run_tier(window_index, tier) ¶
Check if a validation tier should run at this window index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_index | int | Current window number. | required |
tier | int | Tier to evaluate (1, 2, or 3). | required |
Returns:
| Type | Description |
|---|---|
bool | True when the tier is enabled and the window index aligns with its |
bool | configured interval. |
advanced.curator¶
crp.advanced.curator ¶
LLM context curation - progressive understanding synthesis (§18).
Periodically dispatches curation windows to build an evolving synthesis of findings, relationships, and gaps. Injected into envelopes as Section 1.5 between CRITICAL STATE and DISCOVERIES.
LLMSynthesis dataclass ¶
Curated synthesis from LLM review of accumulated facts.
to_dict() ¶
Serialize this synthesis to a JSON-ready dict.
from_dict(data) classmethod ¶
Create a new instance from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | The data value. | required |
Returns:
| Type | Description |
|---|---|
LLMSynthesis |
|
CurationConfig dataclass ¶
Configuration for LLM curation.
LLMContextCurator ¶
LLM-driven context curation with progressive understanding.
current_synthesis property ¶
Return the current synthesis.
evolution_count property ¶
Return the current evolution count.
should_curate(window_index) ¶
Check if curation should run at this window.
curate(window_index, top_facts, recent_output_summary='') ¶
Run curation (initial or progressive).
Returns new synthesis or None if dispatch unavailable.
format_for_envelope() ¶
Format current synthesis for envelope injection (Section 1.5).
to_dict() ¶
Serialize the curator state, history and configuration to a dict.
advanced.feedback¶
crp.advanced.feedback ¶
Human-in-the-loop feedback - fact override, confidence adjustment (§18, MAY).
FeedbackEntry dataclass ¶
Single human feedback action.
FeedbackLoop ¶
Human-in-the-loop corrections for facts in warm state.
entry_count property ¶
Return the current entry count.
override_fact(fact_id, corrected_text, reason='') ¶
Replace fact text with human-provided correction.
boost_confidence(fact_id, delta=0.1, reason='') ¶
Increase fact confidence based on human validation.
penalize_confidence(fact_id, delta=-0.2, reason='') ¶
Decrease fact confidence based on human rejection.
reject_fact(fact_id, reason='') ¶
Mark fact as rejected (confidence → 0).
get_adjusted_confidence(fact_id, base_confidence) ¶
Get confidence after applying all feedback adjustments.
get_entries_for_fact(fact_id) ¶
Return all feedback entries associated with fact_id.
to_dict() ¶
Serialize all feedback entries and cumulative adjustments to a dict.
advanced.hierarchical¶
crp.advanced.hierarchical ¶
Hierarchical processing - map-reduce-validate for Tier C/D inputs (§4.5, §11).
Splits massive inputs into segments, processes each independently, reduces iteratively, and validates cross-window consistency.
HierarchicalPlan dataclass ¶
Plan for hierarchical processing.
HierarchicalConfig dataclass ¶
Configuration for hierarchical processing.
SegmentResult dataclass ¶
Output of processing one segment.
HierarchicalProcessor ¶
Map-reduce-validate pattern for oversized inputs.
plan(total_tokens, config=None) ¶
Create a hierarchical processing plan.
map_phase(segments, task_intent) ¶
MAP: Process each segment independently.
reduce_phase(syntheses, task_intent, fan_in=DEFAULT_FAN_IN) ¶
REDUCE: Iteratively merge syntheses until ≤ fan_in remain.
hierarchical_dispatch(task_intent, large_input, config=None) ¶
Full map-reduce-validate pipeline for oversized input.
Returns (final_syntheses, plan).
chain_degradation(levels, per_level=0.03) ¶
Compute effective degradation after N hierarchy levels.
d_chain(L) = 1 - (1 - per_level)^L
effective_context(context_window, levels, per_level=0.03) ¶
Effective context capacity after hierarchical degradation.
EffCtx_hier(N) = C × (1 - d_chain(⌈log_k(N)⌉))
advanced.meta_learning¶
crp.advanced.meta_learning ¶
Meta-learning - ORC, ICML, Reasoning Template Library (§19; CRP-SPEC-019).
Three mechanisms
- Orchestrated Reasoning Chains (ORC): decompose complex reasoning into micro-steps
- In-Context Meta-Learning (ICML): reasoning scaffolds + few-shot examples
- Reasoning Template Library (RTL): store/retrieve successful reasoning traces
Relevant specifications
- CRP-SPEC-019: Cognitive Quality Recognition (CQR)
- CRP specification §19: Meta-learning / amplified reasoning
ReasoningStep dataclass ¶
Single step in a reasoning chain.
Attributes:
| Name | Type | Description |
|---|---|---|
step_description | str | Human-readable description of what this step does. |
system_prompt_template | str | Prompt template used to execute the step. |
expected_output_format | str | Description of the output shape expected from the step. |
scaffold_level | int | Amount of scaffolding to apply (0–3). |
ReasoningTrace dataclass ¶
Complete reasoning trace for RTL storage.
Attributes:
| Name | Type | Description |
|---|---|---|
trace_id | str | Unique identifier for the trace. |
task_type | str | Category of task this trace addresses. |
task_summary | str | Short summary of the original task. |
steps | list[ReasoningStep] | Ordered reasoning steps that produced the result. |
model_class | str | Model capability class (e.g., "0.5B-1B", "2B-7B", "7B+"). |
quality_score | float | Quality score assigned to the trace. |
created_at | float | Unix timestamp when the trace was created. |
usage_count | int | Number of times the trace has been retrieved. |
to_dict() ¶
Serialize the trace to a JSON-friendly dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary representation of the trace, including nested steps. |
from_dict(data) classmethod ¶
Restore a ReasoningTrace from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | Serialized trace data produced by :meth: | required |
Returns:
| Type | Description |
|---|---|
ReasoningTrace | Reconstructed |
ORCResult dataclass ¶
Result of orchestrated reasoning chain.
Attributes:
| Name | Type | Description |
|---|---|---|
steps_completed | int | Number of steps that produced output. |
steps_total | int | Total number of steps in the chain. |
final_output | str | Synthesised final response. |
step_outputs | list[str] | Raw output from each executed step. |
quality_score | float | Estimated quality score for the chain. |
trace | ReasoningTrace | None | Optional |
MetaLearningConfig dataclass ¶
Configuration for meta-learning features.
Attributes:
| Name | Type | Description |
|---|---|---|
enabled | bool | Master switch for meta-learning. |
orc_enabled | bool | Enable Orchestrated Reasoning Chains. |
orc_max_steps | int | Maximum number of reasoning steps allowed in ORC. |
orc_min_model_capability | int | Minimum model capability required for ORC. |
icml_enabled | bool | Enable In-Context Meta-Learning. |
icml_max_examples | int | Maximum few-shot examples to inject. |
rtl_enabled | bool | Enable Reasoning Template Library storage/retrieval. |
rtl_min_quality_for_storage | float | Minimum quality score for storing a trace. |
scaffold_level | str | Default scaffolding level ("auto", "none", "light", "heavy"). |
curation_interval | int | Number of windows between RTL curation passes. |
MetaLearningEngine ¶
ORC + ICML + RTL meta-learning capabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dispatch_fn | Callable[[str, str], tuple[str, Any]] | None | Callable accepting | None |
model_capability | int | Integer capability level of the active model. | 1 |
config | MetaLearningConfig | None |
| None |
trace_count property ¶
Return the number of traces currently stored in the RTL.
should_use_orc(task_complexity=3, resource_pressure='NONE', probe_quality=0.0) ¶
Gate check for ORC activation.
Gate 1: resource_pressure >= HIGH → False Gate 2: model_capability >= task_complexity → False Gate 3: probe_quality >= 0.7 → False (ORC unnecessary)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_complexity | int | Estimated complexity of the task on a 1–5 scale. | 3 |
resource_pressure | str | Resource pressure label ("NONE", "LOW", "MODERATE", "HIGH", "CRITICAL"). | 'NONE' |
probe_quality | float | Quality score from a zero-shot probe; high values indicate ORC is unnecessary. | 0.0 |
Returns:
| Type | Description |
|---|---|
bool | True when ORC should be used to break the task into steps. |
orchestrated_reasoning(task_intent, task_complexity=3, resource_pressure='NONE') ¶
Decompose and execute an orchestrated reasoning chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_intent | str | Natural-language description of the task to solve. | required |
task_complexity | int | Estimated complexity of the task. | 3 |
resource_pressure | str | Resource pressure label; reduces the number of allowed steps under higher pressure. | 'NONE' |
Returns:
| Type | Description |
|---|---|
ORCResult | An |
ORCResult | answer. |
build_reasoning_scaffold(task_intent) ¶
Build a reasoning scaffold adapted to model capability.
Capability ≤ 1 (0.5B-1B): Full step-by-step template Capability ≤ 2 (2B-7B): Light approach Capability > 2: No scaffolding
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_intent | str | Task description to scaffold. | required |
Returns:
| Type | Description |
|---|---|
str | Scaffold string to prepend to the prompt, or an empty string when |
str | scaffolding is disabled. |
build_metacognitive_envelope(task_intent, base_envelope='', few_shot_traces=None) ¶
Build an envelope with reasoning scaffold + few-shot examples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_intent | str | Task description. | required |
base_envelope | str | Existing envelope text to preserve, if any. | '' |
few_shot_traces | list[ReasoningTrace] | None | Optional explicit traces to include as examples. When omitted, the RTL is queried for matching traces. | None |
Returns:
| Type | Description |
|---|---|
str | Combined envelope string containing the base envelope, scaffold, |
str | and any few-shot examples. |
store_trace(trace) ¶
Store a reasoning trace if quality meets the configured threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace | ReasoningTrace |
| required |
Returns:
| Type | Description |
|---|---|
bool | True when the trace was stored, False when RTL is disabled or the |
bool | trace quality is too low. |
to_dict() ¶
Serialize the engine and its trace library.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with the trace library and active configuration. |
advanced.parallel¶
crp.advanced.parallel ¶
Parallel fan-out - N independent windows dispatched concurrently (§4.4).
FanOutTask dataclass ¶
One independent task for parallel dispatch.
FanOutResult dataclass ¶
Result of one parallel dispatch.
ParallelFanOut ¶
Dispatch N independent windows and merge results.
Algorithm (§4.4): 1. Identify N independent tasks 2. Construct independent envelopes from warm_state 3. Dispatch all N windows (sequential fallback if no async) 4. Collect all N outputs 5. Extract facts from all N outputs 6. Merge facts into warm_state 7. Update DAG with fan-out edges 8. Continue with next dependent task
advanced.review_cycle¶
crp.advanced.review_cycle ¶
Review cycle management - active LLM review patterns (§14).
Three interaction patterns
- Pre-generation planning (predict chain > 5 windows)
- Checkpoint review (periodic, Tier 3 models only)
- Post-generation self-assessment (quality scoring + targeted re-gen)
ReviewGuidance dataclass ¶
Output from a checkpoint review.
AssessmentResult dataclass ¶
Output from post-generation self-assessment.
PlannedSection dataclass ¶
One section in the generation plan.
DocumentPlan dataclass ¶
Full generation plan from pre-generation planning.
ReviewCycleManager ¶
Active LLM review cycles - planning, checkpoint, assessment.
pre_generation_plan(task_intent, predicted_chain_length=0) ¶
Generate document plan when chain > 5 windows.
Returns None if chain is short or no dispatch_fn.
checkpoint_review(window_index, review_interval=20, task_intent='', top_facts=None, gap_summary='') ¶
Periodic review at checkpoint windows.
Gate: model_capability < 3 → None Gate: window_index not at interval → None
post_generation_assessment(accumulated_output, task_intent) ¶
Score output quality and flag issues.
Weak model → basic heuristic scoring. Strong model → full LLM self-assessment.
targeted_regeneration(issues, task_intent) ¶
Re-generate targeted fixes for each issue (capped at max_corrections).
advanced.scale_mode¶
crp.advanced.scale_mode ¶
Scale-mode selector - auto-configure session by quality tier (§8.3, §15).
Classifies input into quality tiers S/A/B/C/D based on token ratio, then configures processing mode, validation tiers, review cycles, etc.
QualityTier ¶
Bases: IntEnum
Quality tiers - S (single window) through D (>1000 windows at 128K ctx).
SessionConfig dataclass ¶
Auto-configured session parameters.
ScaleModeSelector ¶
Auto-configure session based on quality tier and model capability.
configure_session(estimated_tokens, model_capability=1) ¶
Auto-configure session based on input size and model capability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimated_tokens | int | Total estimated input tokens. | required |
model_capability | int | Assessed model capability (1, 2, or 3). | 1 |
Returns:
| Type | Description |
|---|---|
SessionConfig | SessionConfig with all parameters set. |
classify_quality_tier(estimated_tokens, context_window) ¶
Classify input into quality tier based on token-to-context ratio.
select_processing_mode(estimated_tokens, context_window) ¶
Select processing mode based on windows needed.
advanced.source_grounding¶
crp.advanced.source_grounding ¶
Source grounding - store/retrieve verbatim source passages (§17).
Stores passages for facts with confidence ≥ 0.8. Integrates passages into envelopes with tier-based budget allocation.
Relevant specifications
- CRP specification §17: Source grounding
- CRP-SPEC-024: Coverage Differential Retrieval (CDR)
- CRP-SPEC-025: Context Differential Graph Retrieval (CDGR)
SourcePassage dataclass ¶
Verbatim passage from the original input linked to facts.
Attributes:
| Name | Type | Description |
|---|---|---|
passage_id | str | Unique identifier for the passage. |
text | str | Verbatim source text. |
source_window | int | Window number from which the passage was extracted. |
token_offset_start | int | Start token offset within the source window. |
token_offset_end | int | End token offset within the source window. |
linked_fact_ids | list[str] | Identifiers of facts this passage grounds. |
token_count | int | Token count of |
relevance_score | float | Relevance score for retrieval ranking. |
to_dict() ¶
Serialize the passage to a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with all passage fields. |
from_dict(data) classmethod ¶
Restore a passage from a serialized dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | Serialized passage produced by :meth: | required |
Returns:
| Type | Description |
|---|---|
SourcePassage | Reconstructed |
SourceGroundingEngine ¶
Store and retrieve verbatim source passages for high-confidence facts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count_tokens | Callable[[str], int] | None | Callable that returns the token count for a piece of text. Defaults to a rough character-based estimate when None. | None |
passage_count property ¶
Return the number of stored passages.
store_passage(passage, fact_confidence=0.0) ¶
Store a passage if its linked fact has confidence ≥ threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passage | SourcePassage | Passage to store. | required |
fact_confidence | float | Confidence score of the fact linked to the passage. | 0.0 |
Returns:
| Type | Description |
|---|---|
bool | True if the passage was stored, False if it was below the threshold. |
get_passages_for_fact(fact_id) ¶
Retrieve all source passages linked to a fact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fact_id | str | Identifier of the fact. | required |
Returns:
| Type | Description |
|---|---|
list[SourcePassage] | List of passages linked to the fact that remain in storage. |
build_source_grounded_envelope(scored_facts, budget_tokens, quality_tier='B') ¶
Build an envelope with source passages allocated by tier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scored_facts | list[dict[str, Any]] | Sorted list of fact dictionaries such as | required |
budget_tokens | int | Total envelope budget in tokens. | required |
quality_tier | str | Quality tier (S/A/B/C/D) controlling the fact/source budget split. | 'B' |
Returns:
| Type | Description |
|---|---|
tuple[list[dict[str, Any]], list[SourcePassage]] | A tuple of |
format_envelope_section(fact, passages) ¶
Format a fact with its source passages for envelope inclusion.
Format::
- {fact text} - Window N
↳ [SOURCE: Window N, tokens X-Y]
"{verbatim original text}"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fact | dict[str, Any] | Fact dictionary containing at least | required |
passages | list[SourcePassage] | Passages linked to the fact. | required |
Returns:
| Type | Description |
|---|---|
str | Formatted envelope section string. |
to_dict() ¶
Serialize the engine for persistence.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with all passages and fact-to-passage mappings. |
from_dict(data, count_tokens=None) classmethod ¶
Restore the engine from serialized state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | Serialized state produced by :meth: | required |
count_tokens | Callable[[str], int] | None | Optional token-counting callable; defaults to the engine's fallback estimator if None. | None |
Returns:
| Type | Description |
|---|---|
SourceGroundingEngine | Reconstructed |