amiril 0.0.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- amiril/__init__.py +51 -0
- amiril/_adapters.py +399 -0
- amiril/_engine/__init__.py +1 -0
- amiril/_engine/engine.py +113 -0
- amiril/_engine/expr.py +653 -0
- amiril/_engine/numbers.py +111 -0
- amiril/_engine/regex.py +262 -0
- amiril/_engine/rules.py +192 -0
- amiril/_engine/sentence.py +251 -0
- amiril/_engine/timeparse.py +72 -0
- amiril/_engine/validate.py +863 -0
- amiril/_engine/values.py +273 -0
- amiril/_kernel/__init__.py +1 -0
- amiril/_kernel/bundle.py +245 -0
- amiril/_kernel/coerce.py +103 -0
- amiril/_kernel/events.py +87 -0
- amiril/_kernel/guard.py +1196 -0
- amiril/_kernel/ids.py +66 -0
- amiril/_kernel/messages.py +30 -0
- amiril/_kernel/normalise.py +165 -0
- amiril/_kernel/records.py +131 -0
- amiril/_kernel/store.py +661 -0
- amiril/_runtime.py +617 -0
- amiril/adk.py +577 -0
- amiril/control/__init__.py +25 -0
- amiril/control/_delivery.py +567 -0
- amiril/control/_disk.py +418 -0
- amiril/control/_holds.py +252 -0
- amiril/control/_jsonschema.py +265 -0
- amiril/control/_pathc.py +85 -0
- amiril/control/_records.py +293 -0
- amiril/control/_redact.py +79 -0
- amiril/control/_transport.py +184 -0
- amiril/control/_wire.py +73 -0
- amiril/control/client.py +1313 -0
- amiril/control/config.py +204 -0
- amiril/control/schemas/bundle.response.schema.json +22 -0
- amiril/control/schemas/common.schema.json +593 -0
- amiril/control/schemas/decisions.request.schema.json +558 -0
- amiril/control/schemas/decisions.response.schema.json +23 -0
- amiril/control/schemas/error.response.schema.json +54 -0
- amiril/control/schemas/heartbeat.request.schema.json +305 -0
- amiril/control/schemas/heartbeat.response.schema.json +34 -0
- amiril/control/schemas/hold-create.request.schema.json +138 -0
- amiril/control/schemas/hold.response.schema.json +226 -0
- amiril/control/schemas/installation-register.request.schema.json +492 -0
- amiril/control/schemas/installation-register.response.schema.json +85 -0
- amiril/core.py +89 -0
- amiril/docs/README.md +14 -0
- amiril/docs/durability/langgraph.md +292 -0
- amiril/docs/frameworks/adk.md +141 -0
- amiril/docs/frameworks/langgraph.md +154 -0
- amiril/docs/frameworks/no-framework.md +238 -0
- amiril/docs/frameworks/write-an-adapter.md +203 -0
- amiril/durable.py +342 -0
- amiril/langgraph.py +746 -0
- amiril/plain.py +454 -0
- amiril/py.typed +0 -0
- amiril/seams.py +357 -0
- amiril/testing.py +21 -0
- amiril-0.0.0.dev0.dist-info/METADATA +114 -0
- amiril-0.0.0.dev0.dist-info/RECORD +64 -0
- amiril-0.0.0.dev0.dist-info/WHEEL +4 -0
- amiril-0.0.0.dev0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# Durability on LangGraph: the restart spike
|
|
2
|
+
|
|
3
|
+
Status: findings, 2026-10-18, from the build plan's item 4 (spike one).
|
|
4
|
+
The question: when an agent process dies between a decision and the
|
|
5
|
+
tool's completion and LangGraph resumes the run from its checkpoint,
|
|
6
|
+
does the SDK ever silently retake a block, or an unapproved hold, as an
|
|
7
|
+
allow? The harness is `python/tests/durability/langgraph_restart.py`;
|
|
8
|
+
the CI test built on it is `test_langgraph_restart.py` beside it.
|
|
9
|
+
|
|
10
|
+
## How the spike was run
|
|
11
|
+
|
|
12
|
+
A child process runs a LangGraph agent under `amiril.init()` with
|
|
13
|
+
LangGraph's own `SqliteSaver` checkpointer on a file, a scripted model,
|
|
14
|
+
and three enforce rules: `pay-cap` blocks `pay` when `amount > 100`
|
|
15
|
+
(reads the call only), `pay-once` blocks a second `pay` in the run
|
|
16
|
+
(reads run history: `exists calls pay status ok`), and `transfer-hold`
|
|
17
|
+
holds every `transfer`. At one chosen point the child writes a marker
|
|
18
|
+
and waits; the parent sends it SIGKILL (every first process in the
|
|
19
|
+
matrix exited on signal 9), then starts a second child on the same
|
|
20
|
+
checkpoint file and thread with `graph.invoke(None, config)` and a
|
|
21
|
+
fresh `amiril.init()`, so the kernel's in-memory history is empty.
|
|
22
|
+
Both children record every kernel call with its run context, every
|
|
23
|
+
decision line, every tool execution, every checkpointer `put` and
|
|
24
|
+
`put_writes`, every question to the resolver, and the final messages.
|
|
25
|
+
|
|
26
|
+
Both installs were run, `create_agent` with `amiril.langgraph.middleware()`
|
|
27
|
+
and a raw `StateGraph` with `amiril.langgraph.ToolNode`, on `invoke`
|
|
28
|
+
(the asynchronous entry persists through the same hooks and the same
|
|
29
|
+
checkpointer calls; `SqliteSaver` has no asynchronous methods, so it
|
|
30
|
+
was not run). langgraph 1.2.16, langgraph-prebuilt 1.1.1,
|
|
31
|
+
langgraph-checkpoint 4.3.0, langgraph-checkpoint-sqlite 3.1.2,
|
|
32
|
+
langchain 1.4.6, langchain-core 1.6.12, Python 3.14. The whole matrix
|
|
33
|
+
runs in about 15 seconds.
|
|
34
|
+
|
|
35
|
+
Scripts: `allow` is one turn, `pay(50)` as `p1`. `block` is `pay(50)`
|
|
36
|
+
as `p1` (runs), then one turn with three calls: `pay(60)` as `p2`
|
|
37
|
+
(blocked by `pay-once`, the history rule), `pay(500)` as `p3` (blocked
|
|
38
|
+
by `pay-cap`) and `lookup(k)` as `l2`. `hold` is `transfer(10)` as
|
|
39
|
+
`t1`.
|
|
40
|
+
|
|
41
|
+
## The matrix
|
|
42
|
+
|
|
43
|
+
"Re-fired" is a call id the resumed process's hook saw again. "Kernel
|
|
44
|
+
saw" is what the fresh kernel made of it. Same root run id was true in
|
|
45
|
+
every cell.
|
|
46
|
+
|
|
47
|
+
| Kill point (what is true at the kill) | create_agent | StateGraph ToolNode |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| `before_tool`: `p1` decided allow and recorded, tool not run | `p1` re-fired as a fresh call (attempt 1, seq 1, no history); allowed again; tool ran once | same |
|
|
50
|
+
| `before_post`: `p1` ran, `after_call` not called | `p1` re-fired as a fresh call; allowed; **tool ran twice** (once per process); the kernel's history holds only the second run | same |
|
|
51
|
+
| `before_checkpoint`: tools node finished, its writes saved, the step checkpoint not written | nothing re-fired: LangGraph applies the saved writes and continues; the model saw `paid 50.0` | same |
|
|
52
|
+
| `after_block`: `p2` and `p3` answered blocked, sibling `l2` still running | only `l2` re-fired (each tool call is its own task; the two blocked tasks' writes were saved); the model saw both block sentences | **`p2`, `p3` and `l2` all re-fired** (one task for the batch): `p3` blocked again by `pay-cap`; **`p2` allowed by the empty history and `pay` ran (`paid 60.0`)**; `l2` ran twice |
|
|
53
|
+
| `block_decided`: `p2` blocked by `pay-once` and recorded, answer not yet returned to the framework | **`p2` re-fired as a fresh call and allowed; `pay` ran (`paid 60.0`)** where before the kill the model was to be told it was not performed | **same, plus `p3` blocked again and `l2` run twice** |
|
|
54
|
+
| `during_hold`: `t1` held, resolver asked, no answer | nothing recorded before the kill (lines are written when the hold settles); `t1` re-fired as a fresh call, held again, **resolver asked again**; it answered block, the model saw the refusal, the tool never ran | same |
|
|
55
|
+
|
|
56
|
+
Two framework facts behind the rows:
|
|
57
|
+
|
|
58
|
+
- `create_agent` runs every tool call of a turn as its own task (the
|
|
59
|
+
model node emits one `Send` per call) and LangGraph saves a task's
|
|
60
|
+
writes when the task completes (`put_writes`), before the step's
|
|
61
|
+
checkpoint. A finished call, allowed and run or blocked and answered,
|
|
62
|
+
is therefore not re-fired after a kill; only the calls in flight are.
|
|
63
|
+
The raw `ToolNode` is one task for the batch, so a kill during any
|
|
64
|
+
call re-fires the whole batch: siblings already blocked are decided
|
|
65
|
+
again and siblings already executed run again.
|
|
66
|
+
- `put_writes` is submitted to a background executor: the loop does not
|
|
67
|
+
wait for it. A probe that hung inside `put_writes` saw the run
|
|
68
|
+
finish anyway, so there is a window after a task completes in which
|
|
69
|
+
the process can die with the task's writes not yet durable. Nothing
|
|
70
|
+
the SDK does can close it; "finished calls are not re-fired" is the
|
|
71
|
+
framework's best effort, not a guarantee.
|
|
72
|
+
|
|
73
|
+
## What the kernel sees after a restart
|
|
74
|
+
|
|
75
|
+
1. **Identity survives.** The adapter derives the run id from the thread
|
|
76
|
+
id and the latest `HumanMessage` id. `add_messages` gives the input
|
|
77
|
+
message its id in the first step and the checkpoint carries it, so
|
|
78
|
+
the resumed process derived the same run id, byte for byte, in every
|
|
79
|
+
cell; tool call ids and the batch id (the `AIMessage` id) come from
|
|
80
|
+
the checkpointed model turn and are the same too. Root-run-scoped
|
|
81
|
+
call identity (kernel.md 5) is therefore sound across a resume: a
|
|
82
|
+
re-fired call would be recognised as the same call if the kernel
|
|
83
|
+
still had its record. (A caller who pins `config["run_id"]` must
|
|
84
|
+
pass the same value on resume; the derived id needs nothing.)
|
|
85
|
+
2. **The record does not survive.** The history store is in memory. The
|
|
86
|
+
fresh kernel has no record for the root run, the session or the
|
|
87
|
+
call, so every re-fired call is a fresh call: attempt 1, `seq`
|
|
88
|
+
restarting at 1, empty history, evaluated from scratch, new lines.
|
|
89
|
+
Nothing distinguishes a re-fire after a restart from a first firing.
|
|
90
|
+
3. **Argument-only decisions come out the same.** `pay-cap` blocked `p3`
|
|
91
|
+
again in every cell where it re-fired. A rule that reads only the
|
|
92
|
+
call (`arg`, `correlation`, the context) is deterministic in the
|
|
93
|
+
call, so re-deciding it after a restart gives the block again. (A
|
|
94
|
+
rule on `now` or `hour_utc` can legitimately differ.)
|
|
95
|
+
4. **History-dependent decisions are retaken as allow.** `pay-once`
|
|
96
|
+
blocked `p2` before the kill, on both installs; after the resume it
|
|
97
|
+
saw no earlier payment and allowed it, and the payment was made a
|
|
98
|
+
second time in the run. The block decision taken before the crash
|
|
99
|
+
became an allow, with no fault line, no retry marker and no sign to
|
|
100
|
+
the model or the product that anything was retaken. This is the
|
|
101
|
+
failure the plan's minimum forbids, and it is not specific to the
|
|
102
|
+
re-fired call: every rule in `run`, `session` or `correlation` scope
|
|
103
|
+
decides on an empty history for the rest of the resumed run, new
|
|
104
|
+
calls included.
|
|
105
|
+
5. **Holds are re-asked, never auto-allowed.** With no record of the
|
|
106
|
+
first hold, the resumed kernel opens a new one and the resolver
|
|
107
|
+
decides. Against the control plane that means a second hold for the
|
|
108
|
+
same call while the first stays pending until it expires; an approval
|
|
109
|
+
given to the first hold between the crash and the resume is not
|
|
110
|
+
found, and the approver is asked twice. Not an allow, but not the
|
|
111
|
+
behaviour kernel.md 7 describes for a durable store ("the hold id
|
|
112
|
+
... is what a durable store would persist to find the hold again
|
|
113
|
+
after a restart"). The kernel sees the token only when the resolver
|
|
114
|
+
returns, so today nothing could persist it before the crash.
|
|
115
|
+
6. **Unknown outcome.** In `before_post` the tool ran and its record was
|
|
116
|
+
`pending` in a process that died. LangGraph re-runs the task (it
|
|
117
|
+
would with or without Amiril), the kernel allows a fresh call, the
|
|
118
|
+
tool runs again, and the first execution's effect is invisible to
|
|
119
|
+
every rule from then on. The store's existing assumption for a stale
|
|
120
|
+
pending record ("its `after_call` is assumed lost", kernel.md 4.3)
|
|
121
|
+
is the right reading of this state.
|
|
122
|
+
|
|
123
|
+
## Which store tier must hold the write-ahead record
|
|
124
|
+
|
|
125
|
+
The record the kernel needs after a restart is: this call id, in this
|
|
126
|
+
root run, was decided `blocked` (or `held`, or `pending`) with these
|
|
127
|
+
decisions, before the crash; and the run's and session's earlier
|
|
128
|
+
records, so history rules decide on the same history.
|
|
129
|
+
|
|
130
|
+
- **The framework's state (the checkpoint) cannot hold it.** A node's
|
|
131
|
+
writes reach the checkpointer only when the task completes, and
|
|
132
|
+
asynchronously at that; nothing inside the hook can reach the
|
|
133
|
+
checkpoint before the tool runs, and the middleware keeps nothing in
|
|
134
|
+
the graph's state by ruling (build log, M2). The checkpoint does hold
|
|
135
|
+
the model's view of a finished call (the `ToolMessage`), which is why
|
|
136
|
+
finished calls are not re-fired, but it is the record of what the
|
|
137
|
+
model saw, never of what the kernel decided and why. LangGraph's
|
|
138
|
+
`BaseStore` (`runtime.store`) is written immediately and would do,
|
|
139
|
+
but it is optional, in-memory by default, and a second configuration
|
|
140
|
+
the customer must get right; it could be a backend later, not the
|
|
141
|
+
tier.
|
|
142
|
+
- **Local SQLite is the tier for v1.** One file the SDK owns, written
|
|
143
|
+
synchronously inside `before_call` before the outcome is returned to
|
|
144
|
+
the adapter and inside `after_call`. Survives process restarts on one
|
|
145
|
+
box with a persistent disk, which is exactly the restart LangGraph's
|
|
146
|
+
own `SqliteSaver` survives in this scenario; a replaced container
|
|
147
|
+
loses both. Nothing to configure beyond a path.
|
|
148
|
+
- **The control plane** already receives every decision record,
|
|
149
|
+
asynchronously and batched, so it is not write-ahead; a read-back
|
|
150
|
+
(records of a root run or session) would make it the tier for
|
|
151
|
+
ephemeral containers and for session and correlation scope across
|
|
152
|
+
processes. A protocol addition; later.
|
|
153
|
+
|
|
154
|
+
## What the SDK must persist before the tool runs
|
|
155
|
+
|
|
156
|
+
Before `before_call` returns an outcome that proceeds (and when a hold
|
|
157
|
+
starts, and when a block is decided): the call record as kernel.md 2.1
|
|
158
|
+
has it (`call_id`, `run_id`, `root_run_id`, `session_id`, `agent_id`,
|
|
159
|
+
`root_agent_id`, `tool_id`, `tool_name_raw`, `args`, `correlation`,
|
|
160
|
+
`status`, `seq`, `batch_id`, `parent_call_id`, `attempt`, `started_at`,
|
|
161
|
+
`meta` including `resolutions`), the decisions (rule id and version,
|
|
162
|
+
action, `rule_mode`, `mode`, outcome, sentence) and the message for the
|
|
163
|
+
model, so a re-execution can be answered without re-evaluating; plus
|
|
164
|
+
the run's bookkeeping that makes a resumed run's snapshots right: the
|
|
165
|
+
`seq` counter and the batch first-arrival marks (kernel.md 4.1, 4.2).
|
|
166
|
+
After the tool: status `ok` or `error`, `result`, `error`, `ended_at`.
|
|
167
|
+
For a hold to be re-found rather than re-asked, the resolver's token
|
|
168
|
+
at hold creation, which the resolver seam cannot report today.
|
|
169
|
+
|
|
170
|
+
With that persisted, kernel.md 5 already says what a re-fire of a
|
|
171
|
+
`blocked` record is: a retry (attempt 2, evaluated from scratch on the
|
|
172
|
+
same history, so a history rule blocks again and the earlier attempt
|
|
173
|
+
stays visible). The two states the spec was silent on are ruled in
|
|
174
|
+
kernel.md 4.1a (SDK commit 4590285):
|
|
175
|
+
|
|
176
|
+
- **A `pending` record whose outcome is unknown** (`before_post`) is
|
|
177
|
+
settled as `error` with the text "the process ended before the result
|
|
178
|
+
was recorded", and its re-fire is a retry evaluated on the restored
|
|
179
|
+
history, so a rule that counts attempts in any status can see that
|
|
180
|
+
something may have happened. The tool still runs again, as LangGraph
|
|
181
|
+
would re-run it with or without Amiril.
|
|
182
|
+
- **A `held` record with no resolution** (`during_hold`) is settled as
|
|
183
|
+
`blocked` with resolution `timeout`, and its re-fire is a retry that
|
|
184
|
+
asks the resolver again. Re-finding the original hold through its
|
|
185
|
+
token is later: it needs the resolver to report the token at hold
|
|
186
|
+
creation.
|
|
187
|
+
|
|
188
|
+
## What was built (kernel.md 4.1a)
|
|
189
|
+
|
|
190
|
+
`amiril.seams.DurableStore` is the seam; `amiril.durable.SqliteStore`
|
|
191
|
+
the local implementation on the standard library's `sqlite3`;
|
|
192
|
+
`amiril.init(durable=path)` or `AMIRIL_DURABLE_PATH` turns it on. The
|
|
193
|
+
history store mirrors every write to it synchronously (the record with
|
|
194
|
+
its decisions and message before `before_call` returns, the `held`
|
|
195
|
+
status before the resolver is asked, every later status change, the
|
|
196
|
+
completion at `after_call`), deletes what eviction, `end_run` and
|
|
197
|
+
`end_session` remove, and drops a run's `seq` counter when the run
|
|
198
|
+
closes. The first `before_call` of a root run or session not in memory
|
|
199
|
+
loads that unit back in its stored order, settles what the dead process
|
|
200
|
+
left unfinished as above, and remembers stored outcomes so a completed
|
|
201
|
+
call re-fired after the restart is a re-execution. A durable store that
|
|
202
|
+
fails writes a fault line and the in-memory store carries on; the
|
|
203
|
+
decision is never lost to its storage. The file is in WAL mode with
|
|
204
|
+
`synchronous=NORMAL`: a committed write survives the process dying,
|
|
205
|
+
which is the restart this spike is about; an operating-system crash or
|
|
206
|
+
power cut can lose the last commits, and `SqliteStore(path,
|
|
207
|
+
synchronous="FULL")` fsyncs each commit for hosts that need that.
|
|
208
|
+
|
|
209
|
+
With the store configured, the matrix's three failing cells pass: `p2`
|
|
210
|
+
re-fires as attempt 2, the restored history shows `p1` paid, `pay-once`
|
|
211
|
+
blocks again, the tool does not run and the model sees the block
|
|
212
|
+
sentence. `test_langgraph_restart.py` runs the whole matrix with the
|
|
213
|
+
store and asserts exactly that.
|
|
214
|
+
|
|
215
|
+
An independent review (same day) tried every real kill again and found
|
|
216
|
+
three paths that still silently allowed a prior block: a file written
|
|
217
|
+
by another SDK version or with one unreadable row failed the whole load
|
|
218
|
+
and the call was evaluated on an empty history; a resume later than
|
|
219
|
+
`run_idle_ms` evicted the restored history before evaluating; and a
|
|
220
|
+
`_remember` after `end_run` left an orphan row. All three are fixed
|
|
221
|
+
under the amended 4.1a (Bounds, Integrity, What it cannot do): the file
|
|
222
|
+
carries a schema version and is probe-written at open, so an
|
|
223
|
+
incompatible or read-only file fails in `init()`; rows are decoded
|
|
224
|
+
tolerantly and a bad one is skipped with a fault line naming the call;
|
|
225
|
+
a unit that cannot be loaded at all is untrusted until a load succeeds:
|
|
226
|
+
the load is retried on every call of that unit, each such call is a
|
|
227
|
+
fault before any decision (the per-rule fail-closed check of kernel.md
|
|
228
|
+
10 decides it, a fault line names it) and nothing of the unit is
|
|
229
|
+
appended or written through, so its rows are never overwritten and
|
|
230
|
+
attempts come from the loaded history only;
|
|
231
|
+
restored units count idle time from the restore; `end_run` deletes the
|
|
232
|
+
root's session-less rows and counter directly; `durable.retention_ms`
|
|
233
|
+
(default 7 days) sweeps the file when the kernel opens it and on the
|
|
234
|
+
periodic sweep, and counter rows with no records go with it; and a
|
|
235
|
+
record whose unit ended while its call was in flight is never written.
|
|
236
|
+
Durable fault lines carry the call's identity where it is known.
|
|
237
|
+
|
|
238
|
+
## Which frameworks pass, for the product
|
|
239
|
+
|
|
240
|
+
LangGraph (Python), both installs, passes the restart test: a block
|
|
241
|
+
decided before a crash is decided again, identically, when LangGraph
|
|
242
|
+
resumes the run, and a held call is held again and answered by the
|
|
243
|
+
approver, never allowed by default. Rules that read only the call
|
|
244
|
+
(arguments, correlation values, the run context) pass with no
|
|
245
|
+
configuration. Rules that read history (`run`, `session` or
|
|
246
|
+
`correlation` scope: `calls`, `prior`, `agg`, `prev`) pass when the
|
|
247
|
+
installation configures the durable store (`amiril.init(durable=...)`
|
|
248
|
+
or `AMIRIL_DURABLE_PATH`) on a disk that survives the restart, which is
|
|
249
|
+
the same condition LangGraph's own `SqliteSaver` needs; without it their
|
|
250
|
+
decision is retaken on an empty history and a block can become an allow,
|
|
251
|
+
so the gate should require the store, or the stated caveat, for history
|
|
252
|
+
rules. Temporal is the next spike; ADK has not been tested (its sessions
|
|
253
|
+
have no checkpoint-and-resume of a tool call).
|
|
254
|
+
|
|
255
|
+
LangGraph (TypeScript), `createAgent` with the middleware (the only
|
|
256
|
+
install the TypeScript adapter has), passes the same test at the same six
|
|
257
|
+
kill points with `init({ durable })` from `@amiril/sdk/node`
|
|
258
|
+
(`typescript/packages/langgraph/test/restart.test.ts`). The matrix matched
|
|
259
|
+
the Python `create_agent` column: only calls in flight were re-fired, and
|
|
260
|
+
`block_decided` re-fired `p2` as attempt 2, blocked again. Without the
|
|
261
|
+
store the same harness retakes that block as allow.
|
|
262
|
+
|
|
263
|
+
What the durable store cannot do, plainly (kernel.md 4.1a):
|
|
264
|
+
|
|
265
|
+
- **A tool whose completion was not persisted can run twice.** When the
|
|
266
|
+
process dies after the tool ran but before `after_call` reached the
|
|
267
|
+
file, the record is settled as `error` and LangGraph re-fires the call
|
|
268
|
+
in the same batch; the batch cut (4.2) hides the settled attempt from
|
|
269
|
+
that retry, so no rule can stop the second execution. Later batches do
|
|
270
|
+
see the `error` record when they ask for that status. This is the same
|
|
271
|
+
double execution LangGraph performs with or without Amiril; the store
|
|
272
|
+
makes it visible afterwards, not preventable.
|
|
273
|
+
- **The history is bounded, not complete.** The file holds what memory
|
|
274
|
+
would hold: `history.max_records` and the idle limits apply on load,
|
|
275
|
+
and `durable.retention_ms` (default 7 days) deletes units no process
|
|
276
|
+
has touched for that long. A run resumed after the retention window
|
|
277
|
+
starts with no history.
|
|
278
|
+
- **One file serves one live process at a time.** Two processes writing
|
|
279
|
+
the same file see each other's rows only when they load a unit, and
|
|
280
|
+
neither sees the other's in-memory state.
|
|
281
|
+
- **A hold settled after a restart is not closed on the control plane.**
|
|
282
|
+
The original hold stays open there until it expires, and the re-fire
|
|
283
|
+
opens a second one, so an approver may see the same call twice.
|
|
284
|
+
|
|
285
|
+
## Do later
|
|
286
|
+
|
|
287
|
+
- The resolver seam reporting the hold token at hold creation, so a
|
|
288
|
+
restart re-finds the hold instead of settling it and opening a second
|
|
289
|
+
one, and closes the original hold on the control plane (kernel.md
|
|
290
|
+
4.1a, 7).
|
|
291
|
+
- The control plane as a durable tier for ephemeral containers (a
|
|
292
|
+
read-back of a root run's or session's records).
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Get started with Amiril on Google ADK
|
|
2
|
+
|
|
3
|
+
Amiril checks each tool call your agent makes against rules, inside your
|
|
4
|
+
process, before the tool runs. A rule can record a call, hold it for a
|
|
5
|
+
person to approve, or block it; each decision carries one sentence naming
|
|
6
|
+
the values it used. On ADK, Amiril is a plugin that runs first.
|
|
7
|
+
|
|
8
|
+
This page is backed by `python/examples/adk_quickstart.py`, which runs
|
|
9
|
+
offline with a scripted model; a test runs it and checks that the code and
|
|
10
|
+
output below match.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install "amiril[adk]"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
The extra needs google-adk 2.11 or later. The adapter is tested on 2.11
|
|
19
|
+
and 2.12 (`amiril.adk.TESTED`). On a newer release it still checks every
|
|
20
|
+
call, with one cost: arguments are checked as the model sent them, without
|
|
21
|
+
coercion to the tool's declared types, and tools are reported without
|
|
22
|
+
schemas. The coverage report says so (`private_seam_off`,
|
|
23
|
+
`untested_framework_version`).
|
|
24
|
+
|
|
25
|
+
## Write a rule
|
|
26
|
+
|
|
27
|
+
Save this as `rules.json`. It blocks any payment above 10000.
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
{
|
|
31
|
+
"schema": "amiril.rules/1",
|
|
32
|
+
"ruleset_version": "example-1",
|
|
33
|
+
"rules": [
|
|
34
|
+
{
|
|
35
|
+
"id": "payment-cap",
|
|
36
|
+
"version": "1",
|
|
37
|
+
"mode": "enforce",
|
|
38
|
+
"description": "A payment amount must be above 0 and at most 10000",
|
|
39
|
+
"target": {"tools": ["issue_payment"]},
|
|
40
|
+
"assert": {"and": [{">": [{"arg": "amount"}, 0]}, {"<=": [{"arg": "amount"}, 10000]}]},
|
|
41
|
+
"action": "block"
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Add Amiril to your app
|
|
48
|
+
|
|
49
|
+
ADK has no process-wide plugin slot, so after `amiril.init()` you hand
|
|
50
|
+
your `App` to `amiril.instrument()` before you build the `Runner`, or hand
|
|
51
|
+
it the `Runner`. It puts Amiril's plugin first in the plugin list and
|
|
52
|
+
registers what it covers. A `Runner` copies the app's plugin list when it
|
|
53
|
+
is built; if `instrument(app)` finds runners already built on the app, it
|
|
54
|
+
adds the plugin to them too and logs a warning.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
handle = amiril.init(rules_file=RULES) # local-only: no API key, no network requests
|
|
58
|
+
|
|
59
|
+
agent = LlmAgent(name="payments", model=ScriptedModel(), tools=[issue_payment])
|
|
60
|
+
app = App(name="quickstart", root_agent=agent)
|
|
61
|
+
amiril.instrument(app) # puts Amiril's plugin first in app.plugins
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
You can also list the plugin yourself: `App(plugins=[amiril.adk.plugin(),
|
|
65
|
+
...])`. It must be first. ADK lets the first plugin that answers a call
|
|
66
|
+
hide it from the others, so Amiril raises `amiril.HookOrderError` when
|
|
67
|
+
another plugin comes before it: from `instrument()` directly, and at the
|
|
68
|
+
start of every run, where ADK re-raises it as a `RuntimeError` whose
|
|
69
|
+
`__cause__` is the `HookOrderError`.
|
|
70
|
+
|
|
71
|
+
`amiril.init()` raises when the rules file cannot be read or does not load,
|
|
72
|
+
so a broken configuration fails at start; with an API key the rules file is
|
|
73
|
+
only a fallback, and a broken one is logged instead.
|
|
74
|
+
`handle.report()["frameworks"]["adk"]["instrumented"]` turns true once the
|
|
75
|
+
plugin has seen a run start, and each registration in
|
|
76
|
+
`report()["registrations"]` says it for its own app.
|
|
77
|
+
|
|
78
|
+
On the main thread, if the process still has the default SIGTERM handler,
|
|
79
|
+
`amiril.init()` installs one that calls `shutdown()` (so queued decision
|
|
80
|
+
records are sent or spooled) and then ends the process by SIGTERM as
|
|
81
|
+
before (as PID 1 in a container, where Linux ignores that, with exit
|
|
82
|
+
status 143); `amiril.init(handle_sigterm=False)` leaves SIGTERM alone.
|
|
83
|
+
|
|
84
|
+
## Run it
|
|
85
|
+
|
|
86
|
+
The scripted model asks for two payments in one turn, 500 and 20000. The
|
|
87
|
+
first runs; the second never reaches the tool, and ADK hands the model
|
|
88
|
+
Amiril's answer as the tool result:
|
|
89
|
+
|
|
90
|
+
<!-- output: python/examples/adk_quickstart.py -->
|
|
91
|
+
```text
|
|
92
|
+
c1 {"amount": 500, "payment_id": "PAY-CLM-1"}
|
|
93
|
+
c2 {"blocked_by": "amiril", "message": "The call to issue_payment was not performed. Rule payment-cap blocked it: A payment amount must be above 0 and at most 10000 (amount 20000). Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed.", "status": "blocked"}
|
|
94
|
+
tools: ['issue_payment']
|
|
95
|
+
not covered: ['run_end_best_effort']
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The last two lines come from the coverage report, which lists every agent
|
|
99
|
+
and tool the plugin guards and what it cannot:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
coverage = handle.report()["registrations"][0]["coverage"]
|
|
103
|
+
print("tools:", [t["name"] for t in coverage["tools"]])
|
|
104
|
+
print("not covered:", [u["reason"] for u in coverage["uncovered"]])
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
ADK still opens its `execute_tool` span for a blocked call. On the tested
|
|
108
|
+
releases the span carries Amiril's answer as the tool response, so your
|
|
109
|
+
traces show the refusal; from google-adk 2.13, ADK records a tool's
|
|
110
|
+
response on its span only when OpenTelemetry's GenAI content capture is on.
|
|
111
|
+
|
|
112
|
+
## Shadow mode and holds
|
|
113
|
+
|
|
114
|
+
`amiril.init(mode="shadow")` records every decision and blocks nothing; a
|
|
115
|
+
rule's own `"mode": "shadow"` does the same for that rule alone. A rule
|
|
116
|
+
whose action is `hold` waits inside the plugin for the resolver you pass as
|
|
117
|
+
`amiril.init(resolver=...)`, while the other calls of the same model turn
|
|
118
|
+
keep running. With no resolver a hold is refused, since no approver means
|
|
119
|
+
not approved.
|
|
120
|
+
|
|
121
|
+
## Not covered
|
|
122
|
+
|
|
123
|
+
- An `AgentTool` built with `include_plugins=False` runs its inner agent
|
|
124
|
+
without any plugin, so its tool calls are not checked; the coverage
|
|
125
|
+
report lists it.
|
|
126
|
+
- Tools a toolset or an MCP server lists at run time are checked under
|
|
127
|
+
their raw names, without the argument types from a declaration.
|
|
128
|
+
- Built-in tools the model provider runs itself (such as `google_search`)
|
|
129
|
+
never reach the plugin.
|
|
130
|
+
- A cancelled run gets no run end, so rules that check something before
|
|
131
|
+
the run ends do not fire for it; an idle run is closed after a while.
|
|
132
|
+
- Argument types come from ADK's tool declaration, read through an
|
|
133
|
+
underscore-named ADK method, and only on google-adk 2.11 and 2.12. On
|
|
134
|
+
other releases rules see the model's raw arguments and the coverage
|
|
135
|
+
report says so (`private_seam_off`, `untested_framework_version`).
|
|
136
|
+
- Finding a `Runner` built before `instrument(app)` relies on Python's
|
|
137
|
+
garbage-collector referrers, which do not see objects frozen by
|
|
138
|
+
`gc.freeze()` (common in pre-fork servers such as gunicorn with
|
|
139
|
+
preloading). There, instrument the app before building its `Runner`, or
|
|
140
|
+
pass the `Runner` to `instrument()`.
|
|
141
|
+
- Live (streaming) mode has not been tested.
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# Get started with Amiril on LangGraph
|
|
2
|
+
|
|
3
|
+
Amiril checks each tool call your agent makes against rules, inside your
|
|
4
|
+
process, before the tool runs. A rule can record a call, hold it for a
|
|
5
|
+
person to approve, or block it; each decision carries one sentence naming
|
|
6
|
+
the values it used. On LangGraph, Amiril is a `create_agent` middleware or,
|
|
7
|
+
in a graph you build yourself, a `ToolNode`.
|
|
8
|
+
|
|
9
|
+
This page is backed by `python/examples/langgraph_quickstart.py`, which
|
|
10
|
+
runs offline with a scripted model; a test runs it and checks that the code
|
|
11
|
+
and output below match.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install "amiril[langgraph]"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The extra needs langgraph 1.2, langchain 1.3 and langchain-core 1.5 or
|
|
20
|
+
later. The adapter is tested on langgraph 1.2 and langchain 1.3 and 1.4
|
|
21
|
+
(`amiril.langgraph.TESTED`). On a newer release it works the same way: its
|
|
22
|
+
hooks are LangGraph's documented ones, used as documented but not verified
|
|
23
|
+
there. A newer langgraph or langchain is named in the coverage report
|
|
24
|
+
(`untested_framework_version`).
|
|
25
|
+
|
|
26
|
+
## Write a rule
|
|
27
|
+
|
|
28
|
+
Save this as `rules.json`. It blocks any payment above 10000.
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"schema": "amiril.rules/1",
|
|
33
|
+
"ruleset_version": "example-1",
|
|
34
|
+
"rules": [
|
|
35
|
+
{
|
|
36
|
+
"id": "payment-cap",
|
|
37
|
+
"version": "1",
|
|
38
|
+
"mode": "enforce",
|
|
39
|
+
"description": "A payment amount must be above 0 and at most 10000",
|
|
40
|
+
"target": {"tools": ["issue_payment"]},
|
|
41
|
+
"assert": {"and": [{">": [{"arg": "amount"}, 0]}, {"<=": [{"arg": "amount"}, 10000]}]},
|
|
42
|
+
"action": "block"
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Add Amiril to your agent
|
|
49
|
+
|
|
50
|
+
Start Amiril once per process:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
handle = amiril.init(rules_file=RULES) # local-only: no API key, no network requests
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
LangGraph has no process-wide middleware slot and no public way to add a
|
|
57
|
+
hook to a compiled graph, so the hook goes in when you build the agent, and
|
|
58
|
+
`amiril.instrument()` checks it and registers what it covers.
|
|
59
|
+
|
|
60
|
+
With `create_agent`, put Amiril's middleware first: the first middleware is
|
|
61
|
+
the outermost, so nothing else sees a call before Amiril does.
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
agent = create_agent(model, [issue_payment], middleware=[amiril.langgraph.middleware()], name="payments")
|
|
65
|
+
amiril.instrument(agent) # checks Amiril's middleware is in place and first
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
In a `StateGraph` you build yourself, use Amiril's `ToolNode` in place of
|
|
69
|
+
LangGraph's. It takes the same arguments, including your own
|
|
70
|
+
`handle_tool_errors`, `wrap_tool_call` and `awrap_tool_call`.
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
builder = StateGraph(State)
|
|
74
|
+
builder.add_node("model", call_model)
|
|
75
|
+
builder.add_node("tools", amiril.langgraph.ToolNode([issue_payment], agent="payments"))
|
|
76
|
+
builder.add_edge(START, "model")
|
|
77
|
+
builder.add_conditional_edges("model", tools_condition)
|
|
78
|
+
builder.add_edge("tools", "model")
|
|
79
|
+
graph = builder.compile()
|
|
80
|
+
amiril.instrument(graph) # checks every ToolNode carries Amiril's hook
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`amiril.instrument()` checks the graph and every subgraph (a supervisor
|
|
84
|
+
graph with agents as nodes, say). It raises when a `ToolNode` has no Amiril
|
|
85
|
+
hook, and `amiril.HookOrderError` when a middleware that adds graph nodes
|
|
86
|
+
(any `before_agent`, `before_model`, `after_model` or `after_agent` hook)
|
|
87
|
+
is listed before Amiril's.
|
|
88
|
+
|
|
89
|
+
`amiril.init()` raises when the rules file cannot be read or does not load,
|
|
90
|
+
so a broken configuration fails at start; with an API key the rules file is
|
|
91
|
+
only a fallback, and a broken one is logged instead.
|
|
92
|
+
|
|
93
|
+
On the main thread, if the process still has the default SIGTERM handler,
|
|
94
|
+
`amiril.init()` installs one that calls `shutdown()` (so queued decision
|
|
95
|
+
records are sent or spooled) and then ends the process by SIGTERM as
|
|
96
|
+
before (as PID 1 in a container, where Linux ignores that, with exit
|
|
97
|
+
status 143); `amiril.init(handle_sigterm=False)` leaves SIGTERM alone.
|
|
98
|
+
|
|
99
|
+
## Run it
|
|
100
|
+
|
|
101
|
+
The scripted model asks for two payments in one turn, 500 and 20000. The
|
|
102
|
+
first runs; the second never reaches the tool, and the model gets an error
|
|
103
|
+
`ToolMessage` carrying the rule's sentence:
|
|
104
|
+
|
|
105
|
+
<!-- output: python/examples/langgraph_quickstart.py -->
|
|
106
|
+
```text
|
|
107
|
+
create_agent c1 success {"payment_id": "PAY-CLM-1", "amount": 500.0}
|
|
108
|
+
create_agent c2 error The call to issue_payment was not performed. Rule payment-cap blocked it: A payment amount must be above 0 and at most 10000 (amount 20000). Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed.
|
|
109
|
+
StateGraph c1 success {"payment_id": "PAY-CLM-1", "amount": 500.0}
|
|
110
|
+
StateGraph c2 error The call to issue_payment was not performed. Rule payment-cap blocked it: A payment amount must be above 0 and at most 10000 (amount 20000). Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed.
|
|
111
|
+
not covered: ['run_end_best_effort']
|
|
112
|
+
not covered: ['no_run_end']
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
A blocked call never reaches the tool, so LangChain records no tool run
|
|
116
|
+
for it: in your traces the refusal shows only as the error `ToolMessage` in
|
|
117
|
+
the next model input. A tool that raises on an allowed call behaves exactly
|
|
118
|
+
as it would without Amiril.
|
|
119
|
+
|
|
120
|
+
The run id is the thread id and the latest user message's id, or yours:
|
|
121
|
+
`graph.invoke(inputs, {"configurable": {"thread_id": "t1",
|
|
122
|
+
**amiril.langgraph.run_config(run_id="r1")}})`. The thread id is the session.
|
|
123
|
+
The middleware keeps nothing in the graph's state.
|
|
124
|
+
|
|
125
|
+
## Shadow mode and holds
|
|
126
|
+
|
|
127
|
+
`amiril.init(mode="shadow")` records every decision and blocks nothing; a
|
|
128
|
+
rule's own `"mode": "shadow"` does the same for that rule alone. A rule
|
|
129
|
+
whose action is `hold` waits inside the hook for the resolver you pass as
|
|
130
|
+
`amiril.init(resolver=...)` (awaited under `ainvoke`, blocking the tool's
|
|
131
|
+
worker thread under `invoke`), while the other calls of the same model turn
|
|
132
|
+
keep running. With no resolver a hold is refused.
|
|
133
|
+
|
|
134
|
+
## Not covered
|
|
135
|
+
|
|
136
|
+
- A raw `StateGraph` has no run-end signal, so rules that check something
|
|
137
|
+
before the run ends never fire there. With `create_agent`, run end comes
|
|
138
|
+
from `after_agent`, which does not fire when the run raises or is
|
|
139
|
+
interrupted.
|
|
140
|
+
- The middleware adds one graph node, `AmirilMiddleware.after_agent`: it
|
|
141
|
+
shows once per run in `stream_mode="updates"` and takes one more step of
|
|
142
|
+
`recursion_limit`.
|
|
143
|
+
- An agent invoked inside a tool is checked only if it has Amiril's hook
|
|
144
|
+
too; the outer hook never sees its calls.
|
|
145
|
+
- A node that calls `tool.invoke` itself, and provider built-in tools
|
|
146
|
+
passed to `create_agent` as dicts, bypass `ToolNode`.
|
|
147
|
+
- Middleware that only wraps tool calls adds no graph node, so its place
|
|
148
|
+
relative to Amiril's cannot be checked; it is the order of your list.
|
|
149
|
+
- Exact tool-error behaviour in Amiril's `ToolNode` uses LangGraph's own
|
|
150
|
+
underscore-named error functions, on langgraph-prebuilt 1.1 only. On
|
|
151
|
+
other releases your `handle_tool_errors` goes to LangGraph unchanged, an
|
|
152
|
+
exception type, tuple or typed handler then also catches errors it
|
|
153
|
+
would not, and the coverage report says so.
|
|
154
|
+
- History is kept in memory: a restart forgets earlier calls.
|