Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Context Compiler Directive Drafter

Turn natural-language requests into candidate Context Compiler directives.

context-compiler-directive-drafter helps hosts translate user requests like:

Please use Docker for container examples.

into candidate directives, such as:

use docker

This package drafts suggestions for the Context Compiler. Only context-compiler applies directives and updates state.

The drafter suggests candidate directives. context-compiler decides what to do with them.

The drafter owns the human-facing acquisition step between messy user input and canonical directive text. That includes deciding when a message is close enough to propose a canonical directive, when the message is not a directive at all, and when the message is too unclear or malformed to safely interpret without more help. It does not become an authority over state, permissions, or application.


When To Use It

Use this package when you want to:

  • Translate user requests into safe, canonical directives.
  • Handle near-canonical input, alternate phrasing, and malformed-but-recoverable directive attempts before compiler handoff.
  • Distinguish "no directive" from "unknown or failed interpretation" in a stable host-facing contract.
  • Avoid accidental or unsafe state changes from ambiguous input.
  • Add a conservative natural-language-to-directive step before applying changes.

This package owns the human-facing acquisition boundary, including when to propose a canonical directive, when to abstain, and when to ask for clarification or interpretation confirmation before compiler handoff.

The normative acquisition contract lives in docs/DrafterAcquisitionSpec.md. This package does not own:

  • authoritative compiler state
  • the decision about whether a canonical directive is allowed in the current context
  • directive application or state mutation
  • invention of new directive semantics beyond the compiler-owned contract

Installation

Install in your host environment:

pip install "context-compiler-directive-drafter"

For local development:

uv sync --group dev

Basic Usage

Draft a candidate directive:

from context_compiler_directive_drafter import DirectiveDrafter
from context_compiler_directive_drafter import NoDirective, UnknownDirective

drafter = DirectiveDrafter()

result = drafter.draft_directive(
    "Please use Docker for container examples.",
)

if hasattr(result.result, "text"):
    print("Candidate directive:", result.result.text)
elif isinstance(result.result, NoDirective):
    print("No canonical directive drafted:", result.result.reason)
elif isinstance(result.result, UnknownDirective):
    print("Need clarification before drafting:", result.result.reason)

The host validates drafted output before passing it to engine.step(...).

For small runnable examples, see examples/basic_usage.py and examples/prompt_rendering.py.

Public API

Public interface:

  • DirectiveDrafter(): Synchronous orchestration over heuristic preprocessing, optional fallback acquisition, and output validation.
  • DraftResult: Structured non-authoritative result returned by DirectiveDrafter.draft_directive(...).
  • NoDirective and UnknownDirective: Non-canonical drafting result variants with preserved reasons.
  • preprocess_heuristic(message): Heuristically draft a candidate directive.
  • parse_preprocessor_output(raw_output): Validate and parse drafting output.
  • validate_preprocessor_output(raw_output): Classify raw output as directive, no_directive, or unknown.
  • render_prompt(path, premise, policies): Load and fill prompt templates.
  • Constants and sentinels exported from the package.

Output Contract

The intended drafting boundary is:

  • input: user text
  • output: DraftResult(source=<final producer>, result=<drafting-layer variant>)

Every drafting path should end in one of three host-visible result variants:

  • CanonicalDirective: a proposed canonical directive that is ready for compiler review and independent policy checks
  • NoDirective(reason=...): the input is not asking for a directive
  • UnknownDirective(reason=...): the input appears directive-related or interpretation failed, but the drafter should not guess

The source field records only the final producer of the returned drafting result, such as heuristic or a host-provided fallback acquisition callback. It does not track fallback history.

A returned CanonicalDirective means "this is a proposed canonical directive," not "this directive is permitted" and not "this directive has been applied."

Recommended Host Flow

  1. Run DirectiveDrafter().draft_directive(message) as the high-level drafting API. It always tries heuristic drafting first and may optionally call a non-authoritative fallback acquisition callback when the heuristic result is not directly returnable.
  2. If the result yields a CanonicalDirective, pass that canonical directive to context-compiler for authoritative review and application.
  3. If the result yields NoDirective, continue the host flow without a directive handoff.
  4. If the result yields UnknownDirective, preserve the boundary: ask for clarification, show resubmission guidance, or retry drafting in a safer workflow.

The public helpers remain available unchanged for hosts that prefer to orchestrate preprocessing and validation themselves.

Safety Guidance:

  • Always validate drafting output before compiler handoff.
  • Never pass raw model output directly to the compiler.
  • Bypass drafting when clarification is pending.
  • Do not drive authoritative transitions from package-owned drafting code.
  • Do not read or mutate engine.state directly from package-owned drafting code.
  • Prefer abstaining over unsafe guesses.
  • Output validation checks the canonical directive contract, not whether the directive is allowed in context.
  • A structurally valid drafted directive may still be the wrong interpretation of the user's meaning.
  • Reviewed semantic drafting belongs in a separate higher-level workflow such as preview, approval, or engine application.

Hosts may use UnknownDirective to trigger clarification, confirmation, or resubmission guidance. That interaction is part of the human-input drafting boundary, but any eventual canonical directive must still be revalidated before compiler handoff.

Do not pass raw model output to the compiler.

Prompt Resources

The package includes prompt templates for integrations that use model-based drafting when heuristic drafting does not produce a result.

  • prompts/default.txt: recommended default prompt
  • prompts/llama.txt: stricter prompt for Llama-family models

Use render_prompt(path, premise, policies) to load a template and fill it with the current prompt-ready premise and policies.

The rendered prompt can be sent to an LLM to attempt directive drafting when heuristic drafting does not produce a result.

Any model output should still be validated with parse_preprocessor_output(...) or validate_preprocessor_output(...) before it is shown or used.

Current Limits

This package is intentionally conservative. It abstains or returns unknown when input is:

  • Ambiguous, mixed-intent, or quoted.
  • Embedded in prose, markdown, or code.
  • Not safely interpretable as one canonical directive.

Boundary rules:

  • Process the full message, not fragments.
  • Emit at most one canonical directive.
  • Abstain when one message contains multiple directive-shaped instructions.
  • Do not mine surrounding prose for commands.
  • Do not split one message into multiple drafted directives.
  • Do not invent new directive semantics.
  • Avoid broad semantic rewrites that effectively create new policy meaning.
  • Prefer false negatives over false positives.

context-compiler-directive-drafter only proposes at most one candidate directive. context-compiler remains responsible for independently enforcing the single-directive invariant before any authoritative application.

The drafter should consume the compiler-owned grammar contract once that extracted contract is available. This package should not duplicate or become the normative owner of grammar rules in its own documentation or prompt resources.

Hosts that want broader proposal behavior should implement it explicitly.

CLI

The CLI command is directive-drafter. The CLI currently supports a limited set of behaviors:

uv run directive-drafter "please make replies concise"

It returns a non-zero exit status because the public high-level drafting API requires a host-provided engine context.

Development

Run local checks:

uv run pre-commit run --all-files
uv run pytest

License

Apache-2.0

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

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file context_compiler_directive_drafter-0.2.0.dev0.tar.gz.

File metadata

File hashes

Hashes for context_compiler_directive_drafter-0.2.0.dev0.tar.gz
Algorithm Hash digest
SHA256 14bb1096f34b0c2cc48ba3c23504dd33a305fc3dd203213b142d7b914f01649e
MD5 24c457d1d64711a9aa7d6f482966651c
BLAKE2b-256 ed4a4f594ecab9c89e771b566cbccc854c81fba89081d9dd736925d73be31fa7

See more details on using hashes here.

Provenance

The following attestation bundles were made for context_compiler_directive_drafter-0.2.0.dev0.tar.gz:

Publisher: publish-pypi.yml on rlippmann/context-compiler-directive-drafter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file context_compiler_directive_drafter-0.2.0.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for context_compiler_directive_drafter-0.2.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 505f41356fedd5757ee7df1801f1e35436e96afd5bc0920f93a814e9598c6433
MD5 99c316c3d2c63b40ca5af2c00464d227
BLAKE2b-256 b7e5b69ddec62e5206c580e4b9f3dba95da925f366181065f3d9d9c360a47685

See more details on using hashes here.

Provenance

The following attestation bundles were made for context_compiler_directive_drafter-0.2.0.dev0-py3-none-any.whl:

Publisher: publish-pypi.yml on rlippmann/context-compiler-directive-drafter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.0.dev0 This release

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page