Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Once a directive is accepted, it becomes authoritative for the remainder of the
User sets a constraint once:

```text
User: don't use docker
User: don't use peanuts
```

State becomes:
Expand All @@ -30,7 +30,7 @@ State becomes:
"focus.primary": null
},
"policies": {
"prohibit": ["docker"]
"prohibit": ["peanuts"]
},
"version": 1
}
Expand All @@ -39,7 +39,7 @@ State becomes:
Later in the conversation:

```text
User: how should I deploy my service?
User: how should I make this curry?
```

The host supplies the authoritative state to the model so the constraint persists across turns.
Expand Down Expand Up @@ -201,47 +201,47 @@ The same input sequence always produces the same state.
Hard negative directive:

```text
User: don't use docker
User: don't use peanuts
```

Result:

```json
{
"policies": {
"prohibit": ["docker"]
"prohibit": ["peanuts"]
}
}
```

Fact configuration:

```text
User: I'm using MacBook M3
User: use vegetarian curry
```

State update:

```text
facts.focus.primary = "MacBook M3"
facts.focus.primary = "vegetarian curry"
```

Correction:

```text
User: actually MacBook M2
User: actually vegan curry
```

Result:

```text
facts.focus.primary = "MacBook M2"
facts.focus.primary = "vegan curry"
```

Ambiguous mutation:

```text
User: no use docker
User: no use peanuts
```

Compiler response:
Expand All @@ -265,8 +265,8 @@ Before:

```json
{
"facts": {"focus.primary": "oracle"},
"policies": {"prohibit": ["stored procedures"]},
"facts": {"focus.primary": "vegetarian curry"},
"policies": {"prohibit": ["peanuts"]},
"version": 1
}
```
Expand All @@ -275,7 +275,7 @@ After `reset policies`:

```json
{
"facts": {"focus.primary": "oracle"},
"facts": {"focus.primary": "vegetarian curry"},
"policies": {"prohibit": []},
"version": 1
}
Expand Down
10 changes: 5 additions & 5 deletions examples/01_persistent_guardrails.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ def build_prompt(state: State, user_input: str) -> str:
def main() -> None:
engine = create_engine()

print("User: don't use docker")
decision1 = engine.step("don't use docker")
print("User: don't use peanuts")
decision1 = engine.step("don't use peanuts")
print("Decision:")
print_json(decision1)
print("State after turn 1:")
print_json(engine.state)
print()

print("User: how should I deploy my service?")
decision2 = engine.step("how should I deploy my service?")
print("User: how should I make this curry?")
decision2 = engine.step("how should I make this curry?")
print("Decision:")
print_json(decision2)
print("State after turn 2:")
print_json(engine.state)
print()

print("Host prompt construction with persisted policy:")
prompt = build_prompt(engine.state, "how should I deploy my service?")
prompt = build_prompt(engine.state, "how should I make this curry?")
print(prompt)


Expand Down
8 changes: 4 additions & 4 deletions examples/02_configuration_and_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
def main() -> None:
engine = create_engine()

print("User: I'm using MacBook M3")
decision1 = engine.step("I'm using MacBook M3")
print("User: use vegetarian curry")
decision1 = engine.step("use vegetarian curry")
print("Decision:")
print_json(decision1)
print("State:")
print_json(engine.state)
print()

print("User: actually MacBook M2")
decision2 = engine.step("actually MacBook M2")
print("User: actually vegan curry")
decision2 = engine.step("actually vegan curry")
print("Decision:")
print_json(decision2)
print("State (last write wins):")
Expand Down
6 changes: 3 additions & 3 deletions examples/03_ambiguity_with_clarification.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def fake_llm(user_input: str) -> str:
def main() -> None:
engine = create_engine()

print("User: no use docker")
decision1 = engine.step("no use docker")
print("User: no use peanuts")
decision1 = engine.step("no use peanuts")
print("Decision:")
print_json(decision1)
print()
Expand All @@ -23,7 +23,7 @@ def main() -> None:
print("Host behavior: clarification pending, do NOT call LLM.")
print(f"Prompt to user: {decision1['prompt_to_user']}")
else:
fake_llm("no use docker")
fake_llm("no use peanuts")
print()

print("User: yes")
Expand Down
4 changes: 2 additions & 2 deletions examples/05_llm_integration_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def main() -> None:
engine = create_engine()

handle_turn("hello there", engine)
handle_turn("don't use docker", engine)
handle_turn("no use kubernetes", engine)
handle_turn("don't use peanuts", engine)
handle_turn("no use shellfish", engine)
handle_turn("yes", engine)


Expand Down
56 changes: 28 additions & 28 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def _canonical_json(obj: object) -> str:
def test_persistent_guardrails_example_output() -> None:
output = _run_example("01_persistent_guardrails.py")

assert "User: don't use docker" in output
assert "User: don't use peanuts" in output
assert (
_canonical_json(
{
"kind": "update",
"prompt_to_user": None,
"state": {
"facts": {"focus.primary": None},
"policies": {"prohibit": ["docker"]},
"policies": {"prohibit": ["peanuts"]},
"version": 1,
},
}
Expand All @@ -46,28 +46,28 @@ def test_persistent_guardrails_example_output() -> None:
_canonical_json(
{
"facts": {"focus.primary": None},
"policies": {"prohibit": ["docker"]},
"policies": {"prohibit": ["peanuts"]},
"version": 1,
}
)
in output
)
assert "Compiled context:" in output
assert "- policies.prohibit: docker" in output
assert "User: how should I deploy my service?" in output
assert "- policies.prohibit: peanuts" in output
assert "User: how should I make this curry?" in output


def test_configuration_and_correction_example_output() -> None:
output = _run_example("02_configuration_and_correction.py")

assert "User: I'm using MacBook M3" in output
assert "User: use vegetarian curry" in output
assert (
_canonical_json(
{
"kind": "update",
"prompt_to_user": None,
"state": {
"facts": {"focus.primary": "MacBook M3"},
"facts": {"focus.primary": "vegetarian curry"},
"policies": {"prohibit": []},
"version": 1,
},
Expand All @@ -78,21 +78,21 @@ def test_configuration_and_correction_example_output() -> None:
assert (
_canonical_json(
{
"facts": {"focus.primary": "MacBook M3"},
"facts": {"focus.primary": "vegetarian curry"},
"policies": {"prohibit": []},
"version": 1,
}
)
in output
)
assert "User: actually MacBook M2" in output
assert "User: actually vegan curry" in output
assert (
_canonical_json(
{
"kind": "update",
"prompt_to_user": None,
"state": {
"facts": {"focus.primary": "MacBook M2"},
"facts": {"focus.primary": "vegan curry"},
"policies": {"prohibit": []},
"version": 1,
},
Expand All @@ -103,7 +103,7 @@ def test_configuration_and_correction_example_output() -> None:
assert (
_canonical_json(
{
"facts": {"focus.primary": "MacBook M2"},
"facts": {"focus.primary": "vegan curry"},
"policies": {"prohibit": []},
"version": 1,
}
Expand All @@ -116,19 +116,19 @@ def test_configuration_and_correction_example_output() -> None:
def test_ambiguity_with_clarification_example_output() -> None:
output = _run_example("03_ambiguity_with_clarification.py")

assert "User: no use docker" in output
assert "User: no use peanuts" in output
assert (
_canonical_json(
{
"kind": "clarify",
"prompt_to_user": "Did you mean to prohibit 'docker'?",
"prompt_to_user": "Did you mean to prohibit 'peanuts'?",
"state": None,
}
)
in output
)
assert "do NOT call LLM" in output
assert "Prompt to user: Did you mean to prohibit 'docker'?" in output
assert "Prompt to user: Did you mean to prohibit 'peanuts'?" in output
assert "User: yes" in output
assert (
_canonical_json(
Expand All @@ -137,7 +137,7 @@ def test_ambiguity_with_clarification_example_output() -> None:
"prompt_to_user": None,
"state": {
"facts": {"focus.primary": None},
"policies": {"prohibit": ["docker"]},
"policies": {"prohibit": ["peanuts"]},
"version": 1,
},
}
Expand All @@ -148,7 +148,7 @@ def test_ambiguity_with_clarification_example_output() -> None:
_canonical_json(
{
"facts": {"focus.primary": None},
"policies": {"prohibit": ["docker"]},
"policies": {"prohibit": ["peanuts"]},
"version": 1,
}
)
Expand Down Expand Up @@ -193,56 +193,56 @@ def test_tool_governance_denylist_example_output() -> None:

def test_llm_integration_pattern_example_output() -> None:
output = _run_example("05_llm_integration_pattern.py")
docker_state = {
peanuts_state = {
"facts": {"focus.primary": None},
"policies": {"prohibit": ["docker"]},
"policies": {"prohibit": ["peanuts"]},
"version": 1,
}
docker_kubernetes_state = {
peanuts_shellfish_state = {
"facts": {"focus.primary": None},
"policies": {"prohibit": ["docker", "kubernetes"]},
"policies": {"prohibit": ["peanuts", "shellfish"]},
"version": 1,
}

assert "User: hello there" in output
assert _canonical_json({"kind": "passthrough", "prompt_to_user": None, "state": None}) in output
assert "Host action: passthrough -> call fake_llm() without state" in output
assert "state: null" in output
assert "User: don't use docker" in output
assert "User: don't use peanuts" in output
assert (
_canonical_json(
{
"kind": "update",
"prompt_to_user": None,
"state": docker_state,
"state": peanuts_state,
}
)
in output
)
assert "Host action: update -> call fake_llm() with compiled state" in output
assert f"state: {_canonical_json(docker_state)}" in output
assert "User: no use kubernetes" in output
assert f"state: {_canonical_json(peanuts_state)}" in output
assert "User: no use shellfish" in output
assert (
_canonical_json(
{
"kind": "clarify",
"prompt_to_user": "Did you mean to prohibit 'kubernetes'?",
"prompt_to_user": "Did you mean to prohibit 'shellfish'?",
"state": None,
}
)
in output
)
assert "Host action: clarify -> show prompt, DO NOT call LLM" in output
assert "User: yes" in output
assert "prompt_to_user: Did you mean to prohibit 'kubernetes'?" in output
assert "prompt_to_user: Did you mean to prohibit 'shellfish'?" in output
assert (
_canonical_json(
{
"kind": "update",
"prompt_to_user": None,
"state": docker_kubernetes_state,
"state": peanuts_shellfish_state,
}
)
in output
)
assert f"state: {_canonical_json(docker_kubernetes_state)}" in output
assert f"state: {_canonical_json(peanuts_shellfish_state)}" in output
Loading