Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

docs(embed): document compacting a thread while idle

Steven Enamakelad563be9dd4c
diff --git a/crates/openhuman-embed/src/agent/README.md b/crates/openhuman-embed/src/agent/README.md
index 2df6a740f2..99faa3354a 100644
--- a/crates/openhuman-embed/src/agent/README.md
+++ b/crates/openhuman-embed/src/agent/README.md
@@ -71,6 +71,7 @@ directories; transcripts and memory persist with the workspace.
| [`definition.rs`](definition.rs) | `AgentDefinitionSpec`, `ToolScopeSpec` and `SandboxModeSpec`: a small re-spelling of the core's `AgentDefinition`. |
| [`layout.rs`](layout.rs) | `AgentLayout`: where one agent's home, skills, transcripts and `action_dir` live. |
| [`build.rs`](build.rs) | `instantiate`, plus `check_domains_narrow` and `check_tool_groups_narrow`. |
+| [`compact.rs`](compact.rs) | `Agent::compact_thread`, `CompactOptions`, `CompactOutcome`: compact a stored conversation with no model turn. |
| [`attachments.rs`](attachments.rs) | `Agent::attach_tools`, `runtime_id`, `same_agent`, `ToolAttachmentError`, and the policy that composes attached sources with the agent's own. |
## Key types
@@ -109,6 +110,33 @@ directories; transcripts and memory persist with the workspace.
`Workspace` (an I/O failure laying out directories), `Invalid`, and `Call`
for a failed turn.
+## Compact a thread while its user is idle
+
+`Agent::compact_thread(thread_id, CompactOptions { min_tokens, keep_recent_tokens })`
+compacts a stored conversation with no model turn, so the thread's next turn
+resends a summary instead of the full history. It resolves the thread as
+`Turn::session` does and reads the head transcript generation:
+
+- no conversation: `CompactOutcome::Empty`;
+- below `min_tokens` (the compression middleware's estimate over the head's
+ messages, system prompt included): `Skipped { tokens }`, nothing written;
+- otherwise the agent's own compaction runs (`[context] compaction_strategy`,
+ `summarizer_model`, `summarizer_tool_rendering`,
+ `summarizer_max_output_tokens`). The head generation is sealed unchanged
+ and the next one opens with the system prompt, the summary as a user-role
+ message, and the kept tail (`keep_recent_tokens`, at most half of
+ `min_tokens`): `Compacted { tokens_before, tokens_after }`. Nothing older
+ than the tail to fold is `Skipped` too.
+
+The system prompt and the recorded tool declarations carry over byte for
+byte, so the provider's prefix cache survives. The one exception is a
+`memory_block`, which is rendered afresh for the new generation, so the next
+turn does not seal yet another generation to refresh it. A compaction holds
+the session's turn lock from its read to its write, so a turn sent on the
+thread meanwhile waits, then resumes from the compacted head. Logs:
+`[compact_thread]` with the thread id, the outcome and token counts, never
+content.
+
## Tool factories
`AgentSpec::tools` supplies a host's own in-process tools when an agent is
@@ -192,8 +220,9 @@ See the runnable [host_tools example](../../examples/host_tools.rs), region `hos
[`build_tests.rs`](build_tests.rs), [`definition_tests.rs`](definition_tests.rs), [`layout_tests.rs`](layout_tests.rs), [`spec_tests.rs`](spec_tests.rs)
and [`attachments_tests.rs`](attachments_tests.rs) sit beside their modules. The end-to-end proof of
-agent isolation is [`tests/runtime_agents.rs`](../../tests/runtime_agents.rs), and attachments are covered by
-[`tests/attached_tools.rs`](../../tests/attached_tools.rs).
+agent isolation is [`tests/runtime_agents.rs`](../../tests/runtime_agents.rs), attachments are covered by
+[`tests/attached_tools.rs`](../../tests/attached_tools.rs), and `compact_thread` by
+[`tests/compact_thread.rs`](../../tests/compact_thread.rs).
```bash
cargo test -p openhuman-embed --features inference,mcp,skills agent::
diff --git a/docs/gitbooks/en/developing/embed/concepts/turns-sessions.md b/docs/gitbooks/en/developing/embed/concepts/turns-sessions.md
index 4702571955..d284aaaeb8 100644
--- a/docs/gitbooks/en/developing/embed/concepts/turns-sessions.md
+++ b/docs/gitbooks/en/developing/embed/concepts/turns-sessions.md
@@ -68,4 +68,6 @@ No local path is read, so attachments are accepted whatever the turn's origin, i
The stream example checks that deltas precede the final result and that collected and streaming replies agree. A cancellation token passed to a turn is preserved by streaming; cancellation unblocks forwarding even when the consumer has stopped draining a full buffer.
-The harness derives transcript paths from the thread and agent IDs without a timestamp. Resumed sessions reuse their recorded system prompt and tool declarations; edits to prompts, skills or integrations apply to new sessions. An agent built with `AgentDefinitionSpec::refresh_prompt_on_resume(true)` sends a resumed thread its current prompt instead: when the prompt changed, the thread's transcript generation is sealed and its conversation carries into the next, at the cost of one uncached prompt per change, and that turn's message opens with a one-time note that the instructions changed, so the model does not hold to what it said under the old ones. Compaction seals a generation and opens a child instead of erasing history. See [observability](observability.md) for session IDs versus per-call turn IDs, and [API reference](../api-reference.md) for the session-store port.
+The harness derives transcript paths from the thread and agent IDs without a timestamp. Resumed sessions reuse their recorded system prompt and tool declarations; edits to prompts, skills or integrations apply to new sessions. An agent built with `AgentDefinitionSpec::refresh_prompt_on_resume(true)` sends a resumed thread its current prompt instead: when the prompt changed, the thread's transcript generation is sealed and its conversation carries into the next, at the cost of one uncached prompt per change, and that turn's message opens with a one-time note that the instructions changed, so the model does not hold to what it said under the old ones. Compaction seals a generation and opens a child instead of erasing history.
+
+A host can also compact a thread while its user is idle, so the next turn resends a summary instead of the whole history: `agent.compact_thread(&thread_id, CompactOptions { min_tokens, keep_recent_tokens }).await` returns `CompactOutcome::Compacted { tokens_before, tokens_after }`, `Skipped { tokens }` (below `min_tokens`, or nothing to fold; nothing written) or `Empty`. It runs the agent's own compaction (`[context] compaction_strategy` and the `summarizer_*` settings), places the summary as a user message after the system prompt, keeps the system prompt and tool declarations byte-identical (a memory block is rendered afresh for the new generation), and holds the session's turn lock, so a turn sent on the same thread meanwhile waits for it. See [observability](observability.md) for session IDs versus per-call turn IDs, and [API reference](../api-reference.md) for the session-store port.