Deterministic conversational state engine for LLM applications.
Project description
Context Compiler
Deterministic conversational state for LLM systems.
Modern language models are strong at reasoning but unreliable at maintaining consistent state across interactions. Corrections compete with earlier statements, constraints disappear, and long conversations accumulate contradictions.
The Context Compiler introduces a deterministic state layer that governs authoritative conversational state independently of the model.
The model performs reasoning and generation.
The compiler manages facts and constraints.
Once a directive is accepted, it becomes authoritative for the remainder of the session.
10-Second Example
User sets a constraint once:
User: don't use docker
State becomes:
{
"facts": {
"focus.device": null
},
"policies": {
"prohibit": ["docker"]
},
"version": 1
}
Later in the conversation:
User: how should I deploy my service?
The host supplies the compiled state to the model so the constraint persists across turns.
Why Not Just Prompt Engineering?
Prompt instructions are soft and easy to lose across long interactions.
The Context Compiler gives the host an authoritative state snapshot that is independent of transcript drift.
Only explicit user directives can modify state.
In other words:
- the model reasons
- the compiler decides whether state changes
- the host controls when the LLM runs
Architecture
User Input
↓
Context Compiler
↓
Decision (passthrough | update | clarify)
↓
Host Application
↓
LLM
The compiler governs conversational state. It never calls the LLM.
The LLM performs reasoning and generation but cannot modify authoritative state.
The host application decides whether the model runs based on the compiler’s Decision.
Compiler responsibilities
The compiler:
- Parses user input
- Detects explicit directives
- Ensures mutations are unambiguous
- Returns a deterministic
Decision
The compiler never calls the LLM.
Host responsibilities
The host:
- displays clarification prompts
- calls the LLM when allowed
- formats prompts using compiled state
Decision API
Each user message produces a Decision.
class Decision(TypedDict):
kind: Literal["passthrough", "update", "clarify"]
state: dict | None
prompt_to_user: str | None
Meaning:
| kind | host behavior |
|---|---|
| passthrough | forward user input to LLM |
| update | forward input with updated state |
| clarify | show prompt_to_user and do not call the LLM |
State Model
The compiler maintains an authoritative state snapshot:
{
"facts": {
"focus.device": null
},
"policies": {
"prohibit": []
},
"version": 1
}
Note
In M1, the fact schema contains a single exclusive slot: facts["focus.device"].
This slot exists to demonstrate deterministic fact replacement and correction semantics. Richer fact schemas may be introduced in future milestones.
State Properties
- Facts are exclusive (last write wins)
- Policies are additive
- No inference or semantic reasoning
- State is deterministic
The same input sequence always produces the same state.
Directive Examples
Hard negative directive:
User: don't use docker
Result:
{
"policies": {
"prohibit": ["docker"]
}
}
Fact configuration:
User: I'm using MacBook M3
State update:
facts.focus.device = "MacBook M3"
Correction:
User: actually MacBook M2
Result:
facts.focus.device = "MacBook M2"
Ambiguous mutation:
User: no use docker
Compiler response:
Decision.kind = "clarify"
No state mutation occurs until confirmation.
Examples
The examples/ directory contains small integration demonstrations.
-
Persistent guardrails
Demonstrates constraints persisting across turns. -
Configuration with correction
Shows deterministic fact replacement. -
Ambiguity detection with clarification
Demonstrates clarification before state mutation. -
Tool governance for agents
Shows how host applications can block tools using compiler policies. -
LLM integration pattern
Demonstrates the host control flow around theDecisionAPI.
Quickstart
Run the interactive REPL:
context-compiler
Run an example:
python examples/01_persistent_guardrails.py
Run tests:
pytest
Guarantees
The compiler enforces several invariants:
- State never changes without explicit directive or confirmation
- The same input sequence always produces the same state
- LLM output never affects state
- No mutation occurs during clarification
- Facts are exclusive
- Policies are additive
- Pending clarification blocks mutation
Current Milestone
The current implementation corresponds to:
M1 — Deterministic State Engine
Goal:
Ensure explicit user corrections and constraints persist reliably within a single conversation.
Future Milestones
Only M1 is currently implemented.
Possible future work:
M2 — Scope & Session Control (optional)
Introduce isolation boundaries so state applies only within the intended conversational context.
M3 — Cross-Session Recall
Allow explicit recall of prior authoritative state across sessions.
M4 — MCP Integration (optional)
Expose the state engine through a standardized context interface.
M5 — Branch Awareness (optional / experimental)
Provide tools for managing multiple conversation branches during exploratory discussions.
Design Philosophy
The Context Compiler deliberately avoids:
- semantic reasoning
- ontology inference
- machine-learning parsing
- transcript reconstruction
- passive memory
Authoritative state must originate only from explicit user directives.
Specification
The authoritative behavioral specification is:
M1Design.md
Future milestone documents describe potential extensions but are not normative for current behavior.
License
Apache-2.0.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_compiler-0.1.0.tar.gz.
File metadata
- Download URL: context_compiler-0.1.0.tar.gz
- Upload date:
- Size: 46.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54c81aac738d8f6536002567c91e4f9ec328bad11678ef04d9ba936923f3f10e
|
|
| MD5 |
e58cb7ff1823d9fbe6d4040917df6de6
|
|
| BLAKE2b-256 |
22c46286ca820adf3c81e716aedba1ffd7652ae931d463517223b1d0cbe8d212
|
File details
Details for the file context_compiler-0.1.0-py3-none-any.whl.
File metadata
- Download URL: context_compiler-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
769f8d6163e15fa9ac28f865b7fda03e8c63b6666414179d27be2aa631e414f4
|
|
| MD5 |
a0d54ca934a679868fa930f1c8184066
|
|
| BLAKE2b-256 |
bf3eebc77181646debd655dcb3c8864dd4dda816b8cd99534ff4ceef04737c50
|