mistri 0.7.0 → 0.8.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +321 -2
- data/CONTRIBUTING.md +17 -3
- data/NOTICE +11 -0
- data/README.md +145 -284
- data/UPGRADING.md +810 -0
- data/assets/vendors/README.md +12 -0
- data/assets/vendors/claude-mark.svg +3 -0
- data/assets/vendors/gemini-mark.svg +10 -0
- data/assets/vendors/openai-mark-dark.svg +1 -0
- data/assets/vendors/openai-mark.svg +1 -0
- data/docs/README.md +2 -1
- data/docs/context-and-workspaces.md +84 -13
- data/docs/logging.md +90 -0
- data/docs/mcp.md +762 -21
- data/docs/reliability.md +210 -24
- data/docs/sessions.md +415 -36
- data/docs/sub-agents.md +85 -7
- data/docs/tool-contracts.md +528 -13
- data/examples/approval.rb +7 -7
- data/examples/browser.rb +1 -1
- data/examples/page_editor.rb +2 -2
- data/examples/quickstart.rb +1 -1
- data/lib/generators/mistri/install/templates/migration.rb.tt +4 -2
- data/lib/generators/mistri/mcp/mcp_generator.rb +2 -2
- data/lib/generators/mistri/mcp/templates/model.rb.tt +21 -8
- data/lib/mistri/agent.rb +815 -180
- data/lib/mistri/call_order.rb +137 -0
- data/lib/mistri/child.rb +69 -13
- data/lib/mistri/compaction.rb +200 -35
- data/lib/mistri/compactor/text.rb +91 -0
- data/lib/mistri/compactor.rb +474 -117
- data/lib/mistri/console.rb +44 -11
- data/lib/mistri/content.rb +70 -3
- data/lib/mistri/definition.rb +2 -2
- data/lib/mistri/edit.rb +13 -7
- data/lib/mistri/errors.rb +37 -0
- data/lib/mistri/event.rb +20 -9
- data/lib/mistri/locks/rails_cache.rb +16 -2
- data/lib/mistri/locks.rb +12 -3
- data/lib/mistri/mcp/client.rb +247 -61
- data/lib/mistri/mcp/conversation.rb +161 -0
- data/lib/mistri/mcp/egress.rb +5 -4
- data/lib/mistri/mcp/mirror.rb +90 -0
- data/lib/mistri/mcp/negotiation.rb +222 -0
- data/lib/mistri/mcp/oauth/client_identity.rb +53 -0
- data/lib/mistri/mcp/oauth/tolerance.rb +61 -0
- data/lib/mistri/mcp/oauth.rb +303 -82
- data/lib/mistri/mcp/results.rb +97 -0
- data/lib/mistri/mcp/sdk_client.rb +47 -0
- data/lib/mistri/mcp/wires.rb +325 -52
- data/lib/mistri/mcp.rb +304 -55
- data/lib/mistri/memory.rb +1 -1
- data/lib/mistri/message.rb +45 -8
- data/lib/mistri/models.rb +146 -24
- data/lib/mistri/partial_json.rb +5 -5
- data/lib/mistri/providers/anthropic/assembler.rb +40 -3
- data/lib/mistri/providers/anthropic/serializer.rb +154 -22
- data/lib/mistri/providers/anthropic.rb +77 -12
- data/lib/mistri/providers/fake.rb +4 -1
- data/lib/mistri/providers/gemini/assembler.rb +63 -3
- data/lib/mistri/providers/gemini/serializer.rb +27 -16
- data/lib/mistri/providers/gemini.rb +56 -11
- data/lib/mistri/providers/openai/assembler.rb +36 -5
- data/lib/mistri/providers/openai/serializer.rb +76 -22
- data/lib/mistri/providers/openai.rb +91 -14
- data/lib/mistri/reminder.rb +35 -3
- data/lib/mistri/result.rb +6 -4
- data/lib/mistri/result_limit.rb +164 -0
- data/lib/mistri/sandbox/quickjs.rb +345 -0
- data/lib/mistri/sandbox.rb +18 -0
- data/lib/mistri/schema.rb +26 -2
- data/lib/mistri/scratchpad.rb +107 -0
- data/lib/mistri/session/log.rb +137 -0
- data/lib/mistri/session.rb +405 -75
- data/lib/mistri/sinks/logger.rb +17 -11
- data/lib/mistri/skills.rb +10 -4
- data/lib/mistri/spawner.rb +43 -12
- data/lib/mistri/sse.rb +3 -2
- data/lib/mistri/stores/active_record.rb +30 -6
- data/lib/mistri/stores/jsonl.rb +26 -7
- data/lib/mistri/stores/memory.rb +6 -2
- data/lib/mistri/sub_agent/execution.rb +10 -2
- data/lib/mistri/sub_agent/runtime.rb +11 -1
- data/lib/mistri/sub_agent.rb +13 -4
- data/lib/mistri/tool.rb +71 -7
- data/lib/mistri/tool_arguments.rb +10 -11
- data/lib/mistri/tool_context.rb +15 -3
- data/lib/mistri/tool_executor.rb +49 -26
- data/lib/mistri/tool_result.rb +8 -2
- data/lib/mistri/tool_search/index.rb +193 -0
- data/lib/mistri/tool_search.rb +200 -0
- data/lib/mistri/tools/delete_file.rb +22 -0
- data/lib/mistri/tools/edit_file.rb +24 -14
- data/lib/mistri/tools/find_in_file.rb +1 -0
- data/lib/mistri/tools/grep.rb +212 -0
- data/lib/mistri/tools/list_files.rb +38 -5
- data/lib/mistri/tools/read_file.rb +83 -21
- data/lib/mistri/tools/read_memory.rb +2 -1
- data/lib/mistri/tools/run_code.rb +167 -0
- data/lib/mistri/tools/write_file.rb +66 -9
- data/lib/mistri/tools.rb +72 -1
- data/lib/mistri/transport/streamable_http.rb +140 -0
- data/lib/mistri/transport.rb +53 -63
- data/lib/mistri/usage.rb +33 -4
- data/lib/mistri/version.rb +1 -1
- data/lib/mistri/workspace/active_record.rb +22 -10
- data/lib/mistri/workspace/directory.rb +5 -1
- data/lib/mistri/workspace/memory.rb +4 -0
- data/lib/mistri/workspace/path_space.rb +56 -0
- data/lib/mistri/workspace/single.rb +6 -1
- data/lib/mistri.rb +10 -4
- metadata +29 -2
data/docs/sessions.md
CHANGED
|
@@ -11,7 +11,7 @@ chosen store.
|
|
|
11
11
|
Every Agent has a Session. Without one, Mistri creates an in-memory session:
|
|
12
12
|
|
|
13
13
|
```ruby
|
|
14
|
-
agent = Mistri.agent("claude-opus-
|
|
14
|
+
agent = Mistri.agent("claude-opus-5-5")
|
|
15
15
|
agent.session.id
|
|
16
16
|
```
|
|
17
17
|
|
|
@@ -21,13 +21,17 @@ Pass a persistent store when work must survive a process:
|
|
|
21
21
|
store = Mistri::Stores::JSONL.new("tmp/mistri-sessions")
|
|
22
22
|
session = Mistri::Session.new(store: store)
|
|
23
23
|
|
|
24
|
-
agent = Mistri.agent("claude-opus-
|
|
24
|
+
agent = Mistri.agent("claude-opus-5-5", session: session)
|
|
25
25
|
agent.run("Start the research plan.")
|
|
26
26
|
|
|
27
27
|
reloaded = Mistri::Session.new(store: store, id: session.id)
|
|
28
|
-
Mistri.agent("claude-opus-
|
|
28
|
+
Mistri.agent("claude-opus-5-5", session: reloaded).run("Continue the plan.")
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
A session's string id is fixed when the Session is built. Mistri keeps its
|
|
32
|
+
own copy of it, and `Session#id` returns a copy, so changing either string
|
|
33
|
+
afterwards does not move the session.
|
|
34
|
+
|
|
31
35
|
`Session#entries` returns the raw typed log. `Session#messages` returns the
|
|
32
36
|
provider-neutral conversation after replay and compaction. `Session#transcript`
|
|
33
37
|
returns a presentation-oriented entry view with inline image bytes removed. It
|
|
@@ -35,6 +39,12 @@ is not an authorization or redaction boundary: prompts, tool arguments and
|
|
|
35
39
|
results, UI payloads, errors, and provider metadata can remain. Authorize and
|
|
36
40
|
redact before exposing a transcript.
|
|
37
41
|
|
|
42
|
+
A system message with `reminder` set was written by Mistri for the model, such
|
|
43
|
+
as the list of deferred tools or a server's instructions. It is a message of
|
|
44
|
+
the conversation and holds the note's text alone. It is not the user's
|
|
45
|
+
message, so a host that shows the user and assistant messages to a person
|
|
46
|
+
does not show it.
|
|
47
|
+
|
|
38
48
|
Mistri does not encrypt JSONL or Active Record session payloads. The host owns
|
|
39
49
|
storage encryption, access control, retention, deletion, and redaction specific
|
|
40
50
|
to the application. Treat the complete session record as sensitive data.
|
|
@@ -55,16 +65,97 @@ fresh decoded copies or deeply immutable snapshots. Mistri does not rely on
|
|
|
55
65
|
Ruby object identity. `Session#append` normalizes entries through JSON, so store
|
|
56
66
|
implementations see string keys and JSON values.
|
|
57
67
|
|
|
68
|
+
### Reading only what is new
|
|
69
|
+
|
|
70
|
+
A session that loads its whole log on every turn costs more as it ages. A
|
|
71
|
+
store lets a session read each entry once by answering two more things:
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
load(session_id, from: 0) # the entries at position `from` and after
|
|
75
|
+
settled? # true when a load returns only committed entries
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
An entry's position is the number of entries appended before it, and it never
|
|
79
|
+
changes. While `settled?` is true, a `Session` object holds the entries it has
|
|
80
|
+
read, image data included, and asks only for the ones after them. When it is
|
|
81
|
+
false, or the store does not answer it, the session reads fresh on every read
|
|
82
|
+
and holds nothing.
|
|
83
|
+
|
|
84
|
+
Answer true only when the load that follows returns committed entries that
|
|
85
|
+
can no longer change or disappear. Entries must also become visible in order:
|
|
86
|
+
a load never shows an entry before one that was appended earlier. On a
|
|
87
|
+
database, answer false while a transaction is open on the connection that
|
|
88
|
+
reads: its own rows can be rolled back, and it expects to read its own
|
|
89
|
+
snapshot. The answer has to hold for the load that follows it and for the
|
|
90
|
+
place that load reads from, so a subclass or a wrapper that reads from
|
|
91
|
+
somewhere else must answer for itself and not inherit the answer. When in
|
|
92
|
+
doubt answer false. A session then reads fresh, which is always correct. The
|
|
93
|
+
built-in stores answer both.
|
|
94
|
+
|
|
95
|
+
### Starting at the newest checkpoint
|
|
96
|
+
|
|
97
|
+
A compaction written by Mistri is a checkpoint. Besides the summary and
|
|
98
|
+
`kept_from`, the position replay starts at, its entry carries what a later
|
|
99
|
+
turn needs from before that position: the links to sub-agents, which of them
|
|
100
|
+
have reported, the old-style `call_N` ids that already took part in an
|
|
101
|
+
approval, the names of the deferred tools that were loaded, and the versions
|
|
102
|
+
of the documents that code read or wrote. When the newest compaction is a
|
|
103
|
+
checkpoint, a session's window is the entries from its `kept_from` to the
|
|
104
|
+
end. Replay, the context size, the inbox, open approvals and the audit of
|
|
105
|
+
tool calls all work on the window, however old the session is.
|
|
106
|
+
`Session#children` adds the carried links, so it stays complete.
|
|
107
|
+
`Session#entries` and `Session#transcript` still return everything.
|
|
108
|
+
|
|
109
|
+
A store lets a new `Session` find its window without loading the rest by
|
|
110
|
+
answering two more things:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
size(session_id) # the position the next entry will get
|
|
114
|
+
load(session_id, from: 0, to: nil) # the entries from `from` up to, not including, `to`
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The session reads back from the end in growing chunks until one holds a
|
|
118
|
+
compaction. When that compaction is a checkpoint it reads from its
|
|
119
|
+
`kept_from`; when it is not, it reads the whole log. A store without `size` is
|
|
120
|
+
read whole and the same window is used in memory, so every store answers the
|
|
121
|
+
same. The memory and Active Record stores answer both. The JSONL store does
|
|
122
|
+
not, because a file is read from its start either way.
|
|
123
|
+
|
|
124
|
+
Three things follow at the edge. A tool call id has to be unique within the
|
|
125
|
+
window, not over the session's whole life. A decision repeated for a call that
|
|
126
|
+
was settled before the newest checkpoint raises "no open approval". An entry
|
|
127
|
+
older than the window is no longer replayed or audited as a message, though a
|
|
128
|
+
store may still decode it on the way to finding the window.
|
|
129
|
+
|
|
130
|
+
Because a call id can come back after a checkpoint, an approval decision
|
|
131
|
+
records the position of the request it answers and applies only to that
|
|
132
|
+
request. A decision that arrives late never decides a newer request that
|
|
133
|
+
happens to carry the same call id. If its own request has left the window,
|
|
134
|
+
its caller is told the approval has already been decided.
|
|
135
|
+
|
|
136
|
+
A compaction entry written by hand, or by a version before checkpoints,
|
|
137
|
+
carries nothing. While it is the newest compaction the session is read whole.
|
|
138
|
+
Write one with `Session#checkpoint(summary:, kept_from:)`. The start must be
|
|
139
|
+
a turn boundary: a user message, an assistant turn that calls tools, or the
|
|
140
|
+
end of the log. It raises `Mistri::ConfigurationError` for any other start,
|
|
141
|
+
and for one that would move back or pass an open approval or a steer or report
|
|
142
|
+
not folded in yet.
|
|
143
|
+
|
|
58
144
|
Built-in stores are:
|
|
59
145
|
|
|
60
146
|
- `Mistri::Stores::Memory` for one process and tests;
|
|
61
147
|
- `Mistri::Stores::JSONL` for one file per session;
|
|
62
148
|
- `Mistri::Stores::ActiveRecord` for a host model and database.
|
|
63
149
|
|
|
64
|
-
JSONL
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
150
|
+
JSONL names each file after the session id, so the id must match
|
|
151
|
+
`[A-Za-z0-9_-]{1,249}`; any other id raises `ArgumentError`. It creates each
|
|
152
|
+
file with mode 0600 and closes each append, so completed lines survive an
|
|
153
|
+
ordinary process restart. A partial last line left by a crash mid-write is
|
|
154
|
+
skipped on load and the next append starts on a fresh line. Load skips any
|
|
155
|
+
line it cannot parse, so it does not detect damage to a complete line. Every
|
|
156
|
+
load reads the whole file, whatever `from` is. It does not call `fsync`,
|
|
157
|
+
provide a database transaction, or promise power-loss durability. Use a
|
|
158
|
+
database or host store when either distinction matters.
|
|
68
159
|
|
|
69
160
|
### Active Record
|
|
70
161
|
|
|
@@ -84,7 +175,18 @@ $ bin/rails generate mistri:install AgentEntry
|
|
|
84
175
|
|
|
85
176
|
The table needs `session_id`, `position`, and `payload`, plus a unique index on
|
|
86
177
|
`[session_id, position]`. The unique index is the optimistic append boundary for
|
|
87
|
-
independent writers
|
|
178
|
+
independent writers, and it serves every read: a load from a position is one
|
|
179
|
+
range read on it. It also serves a read or a delete of a whole session, so the
|
|
180
|
+
store needs no other index.
|
|
181
|
+
|
|
182
|
+
Positions run 1, 2, 3 with no gap because rows are only ever appended. A read
|
|
183
|
+
whose rows are not at consecutive positions raises
|
|
184
|
+
`Mistri::ConfigurationError`, which is what deleting a row from the middle of
|
|
185
|
+
a session causes. Delete a session's rows together. The reading connection
|
|
186
|
+
needs READ COMMITTED or a stronger isolation level, which MySQL and PostgreSQL
|
|
187
|
+
have by default. The store knows about a transaction that Active Record
|
|
188
|
+
opened. One begun with raw SQL is invisible to it, so do not read a session
|
|
189
|
+
inside one.
|
|
88
190
|
|
|
89
191
|
On MySQL and Trilogy, `payload` must be LONGTEXT because one legal parallel
|
|
90
192
|
tool turn can exceed MEDIUMTEXT even while each call stays inside its own
|
|
@@ -109,43 +211,56 @@ Rebuild the Agent with the current tool definitions, authorization context,
|
|
|
109
211
|
hooks, provider configuration, and business policy before resuming. The Session
|
|
110
212
|
contains durable facts, not frozen application policy.
|
|
111
213
|
|
|
214
|
+
The Session does not store the system prompt or the tool definitions. On
|
|
215
|
+
current Claude models a change to either makes the API drop the session's
|
|
216
|
+
earlier thinking. The run continues. See
|
|
217
|
+
[Thinking is signed against the prefix](reliability.md#thinking-is-signed-against-the-prefix).
|
|
218
|
+
|
|
112
219
|
## Human approval
|
|
113
220
|
|
|
114
221
|
Declare a boolean or argument predicate on the tool:
|
|
115
222
|
|
|
116
223
|
```ruby
|
|
117
|
-
|
|
118
|
-
"
|
|
224
|
+
issue_refund = Mistri::Tool.define(
|
|
225
|
+
"issue_refund", "Refunds an order.",
|
|
119
226
|
schema: lambda {
|
|
120
|
-
string :
|
|
121
|
-
number :
|
|
227
|
+
string :order_id, "Order ID", required: true
|
|
228
|
+
number :amount_usd, "Refund amount", required: true
|
|
122
229
|
},
|
|
123
|
-
needs_approval: ->(args) { args.fetch("
|
|
230
|
+
needs_approval: ->(args) { args.fetch("amount_usd") > 100 },
|
|
124
231
|
) do |args|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
232
|
+
Refunds.issue!(
|
|
233
|
+
order_id: args.fetch("order_id"),
|
|
234
|
+
amount_usd: args.fetch("amount_usd"),
|
|
128
235
|
)
|
|
129
236
|
end
|
|
130
237
|
```
|
|
131
238
|
|
|
239
|
+
Mistri tells the model about the gate. The description a provider receives
|
|
240
|
+
ends with "A person reviews each call before it runs.", or "may review a call"
|
|
241
|
+
for a predicate. Without it, a careful model asks the user in text and the run
|
|
242
|
+
never reaches the approval.
|
|
243
|
+
|
|
132
244
|
When the model calls it:
|
|
133
245
|
|
|
134
246
|
```ruby
|
|
135
|
-
result = agent.run("
|
|
247
|
+
result = agent.run("Refund all 240 dollars of order 42")
|
|
136
248
|
result.awaiting_approval? # => true
|
|
137
249
|
|
|
138
250
|
call = result.pending.first
|
|
139
251
|
session_id = agent.session.id
|
|
140
252
|
```
|
|
141
253
|
|
|
142
|
-
The handler has not run
|
|
143
|
-
|
|
254
|
+
The handler has not run, and no call written after it in that turn has run.
|
|
255
|
+
Each of those answers with an error unless it needs approval and waits too, see
|
|
256
|
+
[The order of a turn's calls](tool-contracts.md#the-order-of-a-turns-calls).
|
|
257
|
+
Persist or route `session_id` and `call.id` in the host's own UI. A decision
|
|
258
|
+
needs only a store and Session:
|
|
144
259
|
|
|
145
260
|
```ruby
|
|
146
261
|
session = Mistri::Session.new(store: store, id: session_id)
|
|
147
262
|
session.approve(call.id, note: "Approved by finance")
|
|
148
|
-
# or: session.deny(call.id, note: "
|
|
263
|
+
# or: session.deny(call.id, note: "Refund the shipping fee only")
|
|
149
264
|
```
|
|
150
265
|
|
|
151
266
|
The decision may be appended from another process at any later time. Nothing
|
|
@@ -154,7 +269,7 @@ sleeps while waiting. To continue:
|
|
|
154
269
|
```ruby
|
|
155
270
|
session = Mistri::Session.new(store: store, id: session_id)
|
|
156
271
|
agent = Mistri.agent(
|
|
157
|
-
"claude-opus-
|
|
272
|
+
"claude-opus-5-5",
|
|
158
273
|
tools: current_tools,
|
|
159
274
|
session: session,
|
|
160
275
|
context: current_authorization_context,
|
|
@@ -198,55 +313,294 @@ Mistri::Session.new(store: store, id: session_id).steer(
|
|
|
198
313
|
|
|
199
314
|
The active Agent does not need to be shared with the writer. If a steer lands
|
|
200
315
|
while the model finishes cleanly, the run extends for another turn so the new
|
|
201
|
-
instruction is answered.
|
|
316
|
+
instruction is answered. `steer` raises `ArgumentError` for text that is empty
|
|
317
|
+
or only whitespace.
|
|
202
318
|
|
|
203
319
|
Background child reports use the same ordered inbox. A host that wakes idle
|
|
204
320
|
sessions should watch `session.pending_inbox`, not only
|
|
205
|
-
`session.pending_steers`, so worker reports receive the same treatment.
|
|
321
|
+
`session.pending_steers`, so worker reports receive the same treatment. An entry
|
|
322
|
+
can land as a run finishes. If waking raises `Mistri::SessionBusyError`, retry
|
|
323
|
+
shortly and check `pending_inbox` again.
|
|
206
324
|
|
|
207
325
|
Inbox consumption is itself an append: the folded message records the source
|
|
208
326
|
entry ID. A crash cannot consume a steer invisibly or require a repair write.
|
|
209
327
|
|
|
210
328
|
## Compaction
|
|
211
329
|
|
|
212
|
-
For a catalogued model, automatic compaction begins when
|
|
330
|
+
For a catalogued model, automatic compaction begins when the context level
|
|
213
331
|
crosses the published window minus safe output headroom. Disable it with
|
|
214
332
|
`compaction: false`, or pass policy explicitly:
|
|
215
333
|
|
|
216
334
|
```ruby
|
|
217
335
|
settings = Mistri::Compaction.new(
|
|
336
|
+
threshold: 0.6,
|
|
218
337
|
reserve: 64_000,
|
|
219
338
|
keep_recent: 24_000,
|
|
339
|
+
max_tokens: 8_192,
|
|
340
|
+
fallback: "claude-sonnet-5-5",
|
|
220
341
|
instructions: "Preserve decisions, identifiers, and unresolved risks.",
|
|
221
342
|
)
|
|
222
343
|
|
|
223
|
-
agent = Mistri.agent("claude-opus-
|
|
344
|
+
agent = Mistri.agent("claude-opus-5-5", compaction: settings)
|
|
224
345
|
```
|
|
225
346
|
|
|
347
|
+
A compaction starts in one of three ways. The agent starts one by itself,
|
|
348
|
+
between two requests of a run, when the level passes the threshold. The host
|
|
349
|
+
starts one with `agent.compact`. And the agent starts one when a provider
|
|
350
|
+
refuses a request as longer than the model's window. The model cannot start
|
|
351
|
+
one: no tool does it.
|
|
352
|
+
|
|
353
|
+
| Provider | Who writes the summary | What a host can read |
|
|
354
|
+
| --- | --- | --- |
|
|
355
|
+
| Anthropic | Anthropic, in its compaction on demand | The text of its block |
|
|
356
|
+
| OpenAI | OpenAI, on a trigger | Nothing: the item is sealed |
|
|
357
|
+
| Gemini | Mistri, with its own summary request | The summary |
|
|
358
|
+
|
|
359
|
+
With `native: false` Mistri writes the summary on every provider, and a host
|
|
360
|
+
can read it. The sections below say what each row means.
|
|
361
|
+
|
|
362
|
+
`threshold:` is the level where a compaction starts: an Integer for tokens,
|
|
363
|
+
or a Float for that share of the window, such as `0.6`. Without it a
|
|
364
|
+
compaction starts near the window. The rule near the window stays under a
|
|
365
|
+
threshold as the net, so the lower of the two applies. A threshold in tokens
|
|
366
|
+
also works for a model with no known window. A share means the same on every
|
|
367
|
+
model. A number of tokens does not, because each provider counts the same
|
|
368
|
+
conversation differently.
|
|
369
|
+
|
|
370
|
+
A compaction keeps `keep_recent:` tokens of recent turns, and never more than
|
|
371
|
+
half of what the threshold leaves above the fixed part of a request, its
|
|
372
|
+
system prompt and tools. So about half of that room is free for the turns
|
|
373
|
+
that follow. It keeps no turn that the compaction before it already kept, so
|
|
374
|
+
compactions that follow each other closely keep only the turns made since.
|
|
375
|
+
The part it summarizes then ends where a request ended, and its request reads
|
|
376
|
+
that part from the provider's cache. A threshold below the fixed part cannot
|
|
377
|
+
be met, and such an agent compacts on every turn.
|
|
378
|
+
|
|
226
379
|
An explicit `window:` enables compaction for an uncatalogued deployment. An
|
|
227
380
|
explicit `reserve:` is host policy; the automatic default otherwise protects a
|
|
228
|
-
catalogued model's shared output capacity plus framing slack. Gemini
|
|
229
|
-
an independent input limit, so its automatic reserve remains
|
|
381
|
+
catalogued model's shared output capacity plus framing slack. A Gemini model
|
|
382
|
+
before 4 publishes an independent input limit, so its automatic reserve remains
|
|
383
|
+
input-oriented. Gemini 4 shares its window with the output, as Anthropic and
|
|
384
|
+
OpenAI do.
|
|
385
|
+
|
|
386
|
+
On a Claude model that offers it, Anthropic writes the summary in its own
|
|
387
|
+
[compaction on demand](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand).
|
|
388
|
+
Mistri decides where to cut, sends the turns before the cut with the agent's
|
|
389
|
+
system prompt and tools, and gives its own summary prompt as the
|
|
390
|
+
instructions. The reply is one block, stored as it came and sent back first
|
|
391
|
+
on every later request, with the turns Mistri kept directly after it. Those
|
|
392
|
+
turns keep their thinking, which a summary Mistri wrote would cost them.
|
|
393
|
+
`event.content` and the entry's `"summary"` hold the block's text. A host can
|
|
394
|
+
show it and cannot change it. The request, and each request that carries the
|
|
395
|
+
block, sends the beta header `compact-2026-09-04`.
|
|
396
|
+
|
|
397
|
+
On a GPT model, OpenAI compacts on a
|
|
398
|
+
[trigger](https://developers.openai.com/api/docs/guides/compaction). Mistri
|
|
399
|
+
decides where to cut and ends the request with the trigger. The reply is one
|
|
400
|
+
sealed item that only OpenAI can read. It is stored as it came and sent back
|
|
401
|
+
first, and the kept turns after it keep their reasoning. Nobody can read this
|
|
402
|
+
summary: `event.content` and the entry's `"summary"` are nil, and
|
|
403
|
+
`instructions:` do not reach it. Only OpenAI can read the item. Its own
|
|
404
|
+
compaction, and a request that repeats the conversation, fold it into a later
|
|
405
|
+
compaction. A later summary that has to be asked with Mistri's text, as over
|
|
406
|
+
the window, from a fallback summarizer or outside an agent, first asks OpenAI
|
|
407
|
+
for the item's text: one small request that holds the item alone. The text
|
|
408
|
+
then updates what came back. When that request fails, the compaction fails
|
|
409
|
+
with a reason, and the session stays as it was.
|
|
410
|
+
|
|
411
|
+
`Compaction.new(native: false)` asks for Mistri's own summary instead. On
|
|
412
|
+
OpenAI that summary stands after the turns it leaves and not before them,
|
|
413
|
+
because the API removes the reasoning of a turn that follows a summary the
|
|
414
|
+
caller wrote. One case still costs the kept turns their reasoning there: a
|
|
415
|
+
tool that was loaded in the part the summary replaces. The stream reports it
|
|
416
|
+
with `:thinking_dropped`. OpenAI's own compaction keeps the reasoning in that
|
|
417
|
+
case too.
|
|
418
|
+
|
|
419
|
+
On Gemini, Mistri always writes the summary itself. On Anthropic and OpenAI it
|
|
420
|
+
also writes its own when the provider cannot: a model or an `origin:` without
|
|
421
|
+
the feature, a failed request, or a reply with no summary. A provider's
|
|
422
|
+
compaction that ends at the output limit is followed by the text described
|
|
423
|
+
below and not by the conversation: both of those requests run with the host's
|
|
424
|
+
thinking under the same limit, and thinking over a long history can take all
|
|
425
|
+
of it. The `:compaction`
|
|
426
|
+
event says which in `event.status`, `:native` or `:own`, and why the
|
|
427
|
+
provider's was not used in `event.error_message`. A session whose compaction
|
|
428
|
+
a provider wrote stays on that provider: another one refuses it.
|
|
429
|
+
|
|
430
|
+
For its own summary, an agent asks with the conversation itself. The request
|
|
431
|
+
repeats the system prompt, the tools and the messages up to the cut as the
|
|
432
|
+
turns sent them, and ends with the summary prompt. The prompt is a system
|
|
433
|
+
message where the provider takes one in a conversation, and a user message
|
|
434
|
+
otherwise. While the session's prompt cache is warm the request reads it, and
|
|
435
|
+
the summarizer reads every tool result whole. The request runs with the
|
|
436
|
+
host's thinking and reasoning settings, because the cached messages depend on
|
|
437
|
+
them. A reply that goes on with the conversation and calls a tool is not a
|
|
438
|
+
summary: the same provider is then asked once more, with the text described
|
|
439
|
+
below.
|
|
440
|
+
|
|
441
|
+
`max_tokens:` bounds the summary's output on every built-in provider (16,384
|
|
442
|
+
by default, never more than the model's published output limit; sent as
|
|
443
|
+
Anthropic `max_tokens`, OpenAI `max_output_tokens`, and Gemini
|
|
444
|
+
`maxOutputTokens`, each of which counts thinking or reasoning toward the
|
|
445
|
+
limit). A short limit a host set for its own replies never shapes the
|
|
446
|
+
summary.
|
|
447
|
+
|
|
448
|
+
A `fallback:` cannot read that cache, and `Mistri::Compactor.call` without an
|
|
449
|
+
agent has no request to repeat. Each sends the part before the cut as one
|
|
450
|
+
text with its own system prompt, with the API's default thinking and
|
|
451
|
+
reasoning, and with no cache write. The text holds every tool result whole
|
|
452
|
+
when it fits the summarizer's context window, less the reserve. When it does
|
|
453
|
+
not fit, the longest results are shortened first, to one length that makes it
|
|
454
|
+
fit, with both ends kept. For a model
|
|
455
|
+
whose window is not known, each result over 2,000 characters is shortened.
|
|
456
|
+
|
|
457
|
+
`fallback:` names a second summarizer, a provider or a model id, that gets one
|
|
458
|
+
try when the session's provider cannot write a usable summary for any reason,
|
|
459
|
+
a refusal included. The model that failed is never asked again: a fallback
|
|
460
|
+
naming the primary's own model is skipped, so a second deployment of the same
|
|
461
|
+
model is not a fallback. The host chooses it, so a refusal never changes
|
|
462
|
+
models silently. Natural pairs are a smaller model of the same family: Sonnet
|
|
463
|
+
5.5 behind Fable 5.1 or Opus 5.5, Sol behind Astra, Flash behind Gemini Pro. The
|
|
464
|
+
compaction entry records which model wrote the summary, and the `:compaction`
|
|
465
|
+
event carries that reply in `event.message` (so a serialized event repeats the
|
|
466
|
+
summary once). When both fail, the one `compaction_failed` entry names each
|
|
467
|
+
model with its reason, and the usage of both attempts is counted. An attempt
|
|
468
|
+
that reports no usage counts as unknown cost. Under a cost budget a reply with
|
|
469
|
+
no priced usage ends compaction before another request or the fallback can
|
|
470
|
+
spend, and the agent validates the fallback's pricing at construction, so give
|
|
471
|
+
a cost-budgeted agent an explicitly configured provider with a deterministic
|
|
472
|
+
service tier rather than a model id.
|
|
230
473
|
|
|
231
474
|
```ruby
|
|
232
475
|
agent.context_usage
|
|
233
|
-
# => { tokens: 141_000, window: 1_000_000, fraction: 0.141
|
|
476
|
+
# => { tokens: 141_000, window: 1_000_000, fraction: 0.141, estimated: 0,
|
|
477
|
+
# threshold: 600_000, remaining: 459_000 }
|
|
478
|
+
|
|
479
|
+
agent.context_usage(count: true)
|
|
234
480
|
|
|
235
481
|
agent.compact
|
|
482
|
+
agent.compact(instructions: "Keep every date and amount.")
|
|
236
483
|
```
|
|
237
484
|
|
|
485
|
+
`threshold` is the level in tokens where a compaction starts, and `remaining`
|
|
486
|
+
the tokens left until it. Both are nil when compaction is off or no level is
|
|
487
|
+
known. `compact(instructions:)` gives the summary a focus for that one time,
|
|
488
|
+
in place of the setting's own `instructions:`.
|
|
489
|
+
|
|
490
|
+
`tokens` is the provider's own count wherever it has one: the usage of the
|
|
491
|
+
last answer, and after a compaction the provider's count of what it left.
|
|
492
|
+
What arrived since is estimated by a rule measured on each provider's counts,
|
|
493
|
+
and `estimated` is how many of the tokens that is. Zero means the number is
|
|
494
|
+
the provider's own. The rule was measured on English text and code. Text
|
|
495
|
+
outside English can be estimated at 0.7 to 2.9 times the provider's count.
|
|
496
|
+
Near the threshold the count decides. `count: true` asks the provider to
|
|
497
|
+
count the next request. That is one round trip: about 0.1 to 0.5 seconds on
|
|
498
|
+
Anthropic and Google, and about 13 ms for each 1,000 tokens on OpenAI.
|
|
499
|
+
|
|
500
|
+
Mistri asks for that count itself in two places: once in each compaction, and
|
|
501
|
+
before an automatic compaction when the reported part of the level is under
|
|
502
|
+
the threshold and twice the estimated part would reach it. A provider on
|
|
503
|
+
another `origin:` is not asked, and a count that fails leaves the estimate.
|
|
504
|
+
A call that waits for an approval has no result yet, and Anthropic and OpenAI
|
|
505
|
+
refuse to count such a history. So the count answers each waiting call with an
|
|
506
|
+
empty result.
|
|
507
|
+
|
|
508
|
+
`keep_recent:` is measured from the prompts the turns reported, so a
|
|
509
|
+
compaction keeps about that many tokens as the provider counts them.
|
|
510
|
+
|
|
238
511
|
Manual `compact` returns nil when there is no useful cut. During a compaction,
|
|
239
|
-
`:compacting` announces that work began
|
|
240
|
-
the
|
|
512
|
+
`:compacting` announces that work began, with what started it in
|
|
513
|
+
`event.reason`, `:manual`, `:automatic` or `:overflow`, and the level in
|
|
514
|
+
`event.tokens_before`. The later `:compaction` event carries the visible
|
|
515
|
+
summary in `event.content`, the same reason, `tokens_before`, `tokens_after`
|
|
516
|
+
and the seconds it took in `event.duration`. The compaction entry stores them
|
|
517
|
+
as `"trigger"`, `"tokens_before"`, `"tokens_after"` and `"duration"`, with
|
|
518
|
+
the usage of the compaction's own requests as `"usage"`. An
|
|
519
|
+
automatic compaction does not start when nothing has been stored since the
|
|
520
|
+
last one.
|
|
521
|
+
|
|
522
|
+
Nothing streams while a compaction runs, and its request can take half a
|
|
523
|
+
minute. A host that shows a run's activity can show a state of its own from
|
|
524
|
+
`:compacting` until `:compaction` or `:compaction_failed`, with the reason
|
|
525
|
+
and the level from the first event and the summary from the second where
|
|
526
|
+
there is one. The run holds the session in that time, so a message sent then
|
|
527
|
+
is steering, as during any turn. The level is checked between requests. One
|
|
528
|
+
turn with large tool results can pass the threshold by the size of those
|
|
529
|
+
results before the next compaction.
|
|
530
|
+
|
|
531
|
+
A worker that runs inline and has no `compaction:` of its own starts
|
|
532
|
+
compacting where its parent does: it takes the parent's `threshold:`. Give a
|
|
533
|
+
worker its own setting to change that. A worker in the background runs with
|
|
534
|
+
the `compaction:` that the host's runtime factory returns for it, and with
|
|
535
|
+
the default when the factory returns none. A tool reads its agent's setting
|
|
536
|
+
from `context.compaction`.
|
|
537
|
+
|
|
538
|
+
The summary is a handoff for the model that resumes: seven fixed sections,
|
|
539
|
+
goal and the requests that shaped the work in the user's words, constraints,
|
|
540
|
+
exact facts and references, decisions, progress, open failures, and current
|
|
541
|
+
work with the next step. The summarizer records what each party said as
|
|
542
|
+
theirs, keeps current values only, and stays under a thousand words; later
|
|
543
|
+
checkpoints fold new messages into the previous summary under the same
|
|
544
|
+
headings. Replay introduces the result as a handoff written by another model
|
|
545
|
+
from the compacted messages, followed by the kept recent turns.
|
|
546
|
+
|
|
547
|
+
Only a normally completed summary with nonblank text and no tool calls can
|
|
548
|
+
become a checkpoint. A truncated or otherwise incomplete response leaves the
|
|
549
|
+
previous summary and retained context unchanged, emits `:compaction_failed`
|
|
550
|
+
with the reason in `event.error_message`, and appends a `compaction_failed`
|
|
551
|
+
entry carrying the reason and its trigger (`manual`, `automatic` or
|
|
552
|
+
`overflow`), so the
|
|
553
|
+
failure shows in `entries` and `transcript` and survives an agent rebuilt in
|
|
554
|
+
another process. Manual `compact` raises `Mistri::CompactionError` with the
|
|
555
|
+
attempt's usage in `error.usage`. Automatic compaction counts that usage
|
|
556
|
+
toward the run and its budget, then continues with the existing context if
|
|
557
|
+
the budget permits. It tries again on a later turn while the context stays
|
|
558
|
+
over the threshold, and stops after three failed automatic attempts in a row
|
|
559
|
+
until a compaction succeeds; manual attempts are recorded but do not count.
|
|
560
|
+
|
|
561
|
+
When a provider refuses a turn because its input is longer than the model's
|
|
562
|
+
context window, the errored message has the type `ContextTooLongError`. An
|
|
563
|
+
agent with compaction then compacts once, with the reason `:overflow`,
|
|
564
|
+
whatever the level says, and sends the turn again. The refused attempt is
|
|
565
|
+
recorded as a retry, so the subscriber receives `:compacting`, `:compaction`
|
|
566
|
+
and then `:retry`. A second refusal ends the run with the error, and after
|
|
567
|
+
three failed compactions of this kind in a row the agent stops trying. Under a
|
|
568
|
+
cost budget a refused request ends the run first, as any request does that
|
|
569
|
+
returns no priced usage.
|
|
570
|
+
|
|
571
|
+
A part to summarize can itself be longer than the window: after a move to a
|
|
572
|
+
model with a smaller window, or when compaction was off for a long time. The
|
|
573
|
+
provider then refuses its own compaction and the conversation with the summary
|
|
574
|
+
prompt, and the text is asked. When the text fits no single request, it is
|
|
575
|
+
read in parts. The first part gets the summary prompt, and each next part
|
|
576
|
+
updates the summary so far, so nothing of the history is dropped. After a
|
|
577
|
+
compaction that OpenAI sealed, the first part updates the text that OpenAI
|
|
578
|
+
gives for its item. A part that the provider still refuses as too long is
|
|
579
|
+
halved and asked again; a refused request reports no usage. A summary that went
|
|
580
|
+
through several parts was rewritten that many times, and a small model can
|
|
581
|
+
lose an early fact on the way.
|
|
241
582
|
|
|
242
583
|
The summary and kept-tail boundary append to the session. Provider replay then
|
|
243
|
-
uses the
|
|
244
|
-
remains available through `entries` and
|
|
584
|
+
uses the summary, or the provider's block or item, plus the retained tail,
|
|
585
|
+
while the full original log remains available through `entries` and
|
|
586
|
+
`transcript`. Compaction cannot hide an
|
|
245
587
|
open approval or split a completed tool call from its result.
|
|
246
588
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
589
|
+
Current Claude models bind thinking to the conversation prefix. After a
|
|
590
|
+
compaction whose summary Mistri wrote, replay omits their pre-compaction
|
|
591
|
+
thinking from the kept tail; new thinking and the durable transcript stay
|
|
592
|
+
intact. This applies to every Anthropic model except the older ones the
|
|
593
|
+
catalog names, so a model newer than the catalog is covered. After a
|
|
594
|
+
compaction that Anthropic wrote, the kept turns keep their thinking. Manual
|
|
595
|
+
compaction must use the same per-session serialization as `run` and `resume`,
|
|
596
|
+
between provider turns, not inside an event callback or alongside an in-flight
|
|
597
|
+
request. An answer generated against the old prefix cannot safely join the
|
|
598
|
+
new one.
|
|
599
|
+
|
|
600
|
+
Tool results are shortened only in the text sent to a summarizer, and only
|
|
601
|
+
when that text does not fit its window. The durable result and ordinary
|
|
602
|
+
replay remain unchanged until a successful compaction intentionally replaces
|
|
603
|
+
earlier context with the summary.
|
|
250
604
|
|
|
251
605
|
## Provider changes
|
|
252
606
|
|
|
@@ -254,6 +608,16 @@ The shared message model permits a session to continue with another built-in
|
|
|
254
608
|
provider. Opaque reasoning, signatures, and pairing metadata replay only to the
|
|
255
609
|
provider that created them.
|
|
256
610
|
|
|
611
|
+
One case is refused. A session whose latest compaction a provider wrote holds
|
|
612
|
+
that provider's block or item, and no other provider can read it. A run of
|
|
613
|
+
such a session on another provider ends with `Mistri::ConfigurationError`.
|
|
614
|
+
A host that plans to move sessions sets `Compaction.new(native: false)`.
|
|
615
|
+
|
|
616
|
+
A session can move to another model of the same provider. A Claude model that
|
|
617
|
+
Anthropic writes no compaction for, such as `claude-haiku-4-5`, refuses the
|
|
618
|
+
block, so Mistri sends that model the text of the block as a summary of its
|
|
619
|
+
own. Any model on another `origin:` is sent that text as well.
|
|
620
|
+
|
|
257
621
|
Completed foreign tool exchanges become a neutral representation when needed.
|
|
258
622
|
In particular, Mistri does not manufacture a signed Gemini function call from
|
|
259
623
|
another provider's metadata.
|
|
@@ -285,7 +649,22 @@ provider history remains pairable. That error explicitly says the tool may
|
|
|
285
649
|
have run; it does not make replay safe.
|
|
286
650
|
|
|
287
651
|
Hosts must serialize active runners and make side effects idempotent or
|
|
288
|
-
reconcilable.
|
|
652
|
+
reconcilable. With a [lock adapter](sub-agents.md#lock-adapters) configured,
|
|
653
|
+
`run`, `resume`, `task`, and `compact` hold a lease on the session, and a
|
|
654
|
+
second runner raises `Mistri::SessionBusyError` before it appends. The lease
|
|
655
|
+
catches a missed case; it does not replace host serialization. Without an
|
|
656
|
+
adapter nothing is checked. With one, a runner that stalls or cannot renew for
|
|
657
|
+
longer than the lease can still overlap the next. After a crash the lease
|
|
658
|
+
expires on its own within three minutes. When the lock backend is down, a
|
|
659
|
+
top-level run warns and goes ahead without its lease, and a sub-agent still
|
|
660
|
+
fails with a report. Read [Reliability](reliability.md) for the complete
|
|
661
|
+
contract.
|
|
662
|
+
|
|
663
|
+
A session has one lease, whoever runs it. A sub-agent's runner holds it for
|
|
664
|
+
the child it runs. Pass `lease: false` to `Agent.new` when your own code
|
|
665
|
+
already holds a lock for the session and the agent should take none. The lease
|
|
666
|
+
key is the session ID alone, so session IDs must be unique across every store
|
|
667
|
+
that shares one lock adapter.
|
|
289
668
|
|
|
290
669
|
## Related guides
|
|
291
670
|
|