Public API reference for context_compiler.
This page documents the exported package surface and typical usage patterns. It does not redefine behavioral semantics.
Authoritative behavior documents:
For behavioral semantics, use the authoritative documents above. This page documents the public checkpoint APIs and their contract surface without redefining directive or continuation behavior.
Create a new engine instance.
state=None: start from empty authoritative statestate=<State>: initialize from a validated authoritative state snapshot
Typical use:
from context_compiler import create_engine
engine = create_engine()Parse one user turn and return a deterministic Decision.
Typical use:
decision = engine.step("set premise current project uses uv")Behavior for directive handling, clarification, and confirmation flows is defined by the Directive Grammar Specification.
Important grammar contract:
- one input may contain at most one canonical directive
- if a later canonical directive start appears in the same input,
engine.step(...)returns the normalclarifydecision contract - compound directives do not mutate authoritative state and do not create pending clarification or replacement state
- quote characters do not create protected literal regions inside recognized directive payloads
Read the current authoritative in-memory state snapshot.
The internal structure is intentionally opaque for normal host use. Prefer
get_premise_value(state) and get_policy_items(state, ...) over direct key
traversal unless you are working at an explicit serialization boundary.
Return whether a confirmation-required clarification is currently pending.
Typical use:
if engine.has_pending_clarification():
show_pending_ui()Each user message produces a Decision.
class Decision(TypedDict):
kind: Literal["passthrough", "update", "clarify"]
state: dict | None
prompt_to_user: str | NoneDecision kinds:
| kind | Intended host use |
|---|---|
passthrough |
forward the user input to the model/runtime |
update |
authoritative state changed; host may apply downstream behavior using updated state |
clarify |
show prompt_to_user; do not continue normal downstream processing yet |
Helper functions:
is_passthrough(decision)is_update(decision)is_clarify(decision)get_clarify_prompt(decision)get_decision_state(decision)
Typical use:
from context_compiler import get_clarify_prompt, is_clarify, is_update
decision = engine.step(user_input)
if is_clarify(decision):
show_to_user(get_clarify_prompt(decision))
elif is_update(decision):
apply_runtime_rules()Use the exported helpers for normal reads from a State snapshot.
get_premise_value(state)returns the current premise value orNone
get_policy_items(state)returns all policy itemsget_policy_items(state, "use")returnsuseitemsget_policy_items(state, "prohibit")returnsprohibititems
Typical use:
from context_compiler import POLICY_PROHIBIT, get_policy_items
blocked_tools = get_policy_items(state, POLICY_PROHIBIT)See the README’s State Model section for conceptual guidance on premise vs policy usage.
Export authoritative state as canonical JSON text.
Validate and restore authoritative state from exported JSON text.
Use these APIs for authoritative-state transport or persistence only.
Conceptual boundary:
export_json()/import_json()transport authoritative state only- checkpoint APIs transport authoritative state plus resumable continuation state
Export a resumable checkpoint object.
Validate and restore a checkpoint object.
Export a checkpoint as canonical JSON text.
Validate and restore a checkpoint from JSON text.
Use checkpoint APIs when you need both:
- authoritative state
- pending confirmation/continuation state
Checkpoint object shape:
{
"checkpoint_version": 1,
"authoritative_state": {
"premise": "concise replies",
"policies": {
"docker": "use"
},
"version": 2
},
"pending": {
"kind": "replacement",
"replacement": {
"kind": "use_only",
"new_item": "kubectl",
"old_item": null
},
"prompt_to_user": "..."
}
}At this boundary, direct key access is expected.
API-level contract notes:
pendingisnullwhen no continuation is waiting for confirmationpendingcaptures confirmation-required operations such as replacement flowsold_itemmay benullfor"use_only"when confirming “use X instead?” without an existing exact policy to replace- imported policy keys are normalized during
import_json(...)and checkpoint authoritative-state restore - if a policy key normalizes to
"", the payload is invalid and is rejected - checkpoint restore is full and deterministic: authoritative state and pending continuation are restored together
- checkpoint validation is all-or-nothing; invalid payloads raise and no partial restore occurs
checkpoint_versionis independent of authoritative stateversionand must be bumped when checkpoint contract shape changes, especiallypending
Typical use cases:
- stateless host or integration boundaries where engine instances are short-lived
- resume after interruption without losing pending clarification flow
- preserve pending confirmation flow state across process or request boundaries
These controller APIs are public package exports and can be used directly in host code, not only through the REPL.
Run one turn through an engine and return a StepResult.
StepResult contains:
output_versionmodedecisionstate
Run a deterministic dry-run preview and return a PreviewResult.
PreviewResult contains:
output_versionmodedecisionstate_beforestate_afterdiffwould_mutate
preview(...) restores live engine state after the dry run.
Return a StructuralDiff describing premise and policy changes between two
state snapshots.
Typical use:
from context_compiler import (
create_engine,
diff_has_changes,
get_preview_state_after,
preview,
state_diff,
)
engine = create_engine()
before = engine.state
dry_run = preview(engine, "prohibit peanuts")
diff = state_diff(before, get_preview_state_after(dry_run))
if diff_has_changes(diff):
show_preview(diff)Controller helper functions:
get_step_decision(step_result)get_step_state(step_result)get_preview_decision(preview_result)get_preview_state_after(preview_result)preview_would_mutate(preview_result)diff_has_changes(diff)
For controller result-envelope details, see the controller conformance fixture documentation in tests/fixtures/README.md.
Decision-kind constants:
DECISION_PASSTHROUGHDECISION_UPDATEDECISION_CLARIFY
Policy-value constants:
POLICY_USEPOLICY_PROHIBIT
Use these when you want explicit string comparisons without hard-coding literals in host code.
Public result and data object names exported at package root include:
DecisionStateCheckpointStepResultPreviewResultStructuralDiffEngine
These names are part of the public package surface. For the exact portable API export contract used by tests and ports, see tests/fixtures/conformance/api/public-api-v1.json.