src/q_ai/cxp/ and implements a research harness for context file poisoning. It assembles poisoned instruction files from clean base templates and researcher-selected rules, generates test repositories, and provides an evidence pipeline for tracking results across assistants and models.
Module Structure
Data Flow
Rule Catalog
Source:catalog.py, rules/
Rules are YAML files defining insecure coding patterns. Each rule has content variants for markdown and plaintext syntax, a target section in the base template, and suggested trigger prompts.
Two sources:
- Built-in rules — 8 rules shipped with the package in
rules/, loaded viaimportlib.resources - User rules —
~/.qai/cxp/rules/*.yaml, loaded at runtime, override built-in rules with the same ID
Base Templates
Source:bases/, base_loader.py
One base template per assistant format — clean, legitimate project configuration files with invisible section markers. The builder inserts rule content at these markers, then strips all markers from the final output.
Section markers:
- Markdown formats:
<!-- cxp:section:dependencies -->,<!-- cxp:section:error-handling -->,<!-- cxp:section:api-routes --> - Plaintext formats:
# cxp:section:dependencies,# cxp:section:error-handling,# cxp:section:api-routes
Builder
Source:builder.py
The build() function assembles a test repository:
- Load the base template for the selected format
- Resolve each rule’s content for the format’s syntax type (markdown or plaintext)
- Insert rules at their target section markers
- Strip all section markers from the assembled content
- Write the context file to the repo directory
- Copy the project skeleton
- Generate the prompt reference companion file
- Write the manifest with build metadata
BuildResult with paths and metadata.
TUI
Source:tui/
Textual-based interactive interface. Five screens:
| Screen | Purpose |
|---|---|
| FormatScreen | Select target assistant format |
| RulesScreen | Browse catalog, toggle rules, add freestyle |
| PreviewScreen | View assembled context file with rule highlights |
| GenerateScreen | Run builder, display paths and prompt suggestions |
| RecordScreen | Record test results to evidence store |
CXPApp enables cross-screen data sharing. Navigation uses push_screen()/pop_screen() with Backspace for back navigation.
Evidence Store
Source:evidence.py
SQLite database at ~/.qai/cxp.db. Schema version tracked via PRAGMA user_version.
| Table | Purpose |
|---|---|
campaigns | Test campaigns with ID, name, description, timestamps |
test_results | Captured outputs with technique, assistant, model, rules_inserted, format_id, validation status |
Validator
Source:validator.py
Applies detection rules organized by category (backdoor, exfil, depconfusion, permescalation, cmdexec) against captured assistant output. Each category defines regex patterns that match indicators of compliance with the poisoned instructions.
Validation outcomes: hit (high-severity match), partial (medium/low match), miss (no match).
Reporter
Source:reporter.py
Two output modes:
- Comparison matrix — Pass/fail table across rules and assistants (Markdown or JSON)
- PoC package — ZIP archive with poisoned file, trigger prompt, captured output, and validation results for responsible disclosure