Open source CapCut alternative.
TypeScript
docs: add context-compaction guide; rename thread-record to conversation-record
David Led02e6f72e247
diff --git a/docs/README.md b/docs/README.mdindex 4954e8b1..7f7e5c9a 100644--- a/docs/README.md+++ b/docs/README.md@@ -18,7 +18,8 @@ Supported product and engineering guides live in `docs/guides/`. This list is th- [Agent Harness](guides/agent-harness.md) — the runtime loop that turns a request into completed work: plan, act, verify, recover, and the safety invariants.- [Harness Deep Dive](guides/harness-deep-dive.md) — subsystem mechanics, one level below the architecture loop.- [Decision System](guides/decision-system.md) — how a turn becomes a conversation, a question, a permission request, or guarded action, and what counts as done.-- [Thread Record](guides/thread-record.md) — the per-task `thread.md` conversation and trace: what gets written, the entry kinds, and the one-writer append-only rules.+- [Conversation Record](guides/conversation-record.md) — the per-conversation `conversation.md` record and trace: what gets written, the entry kinds, and the one-writer append-only rules.+- [Context Compaction](guides/context-compaction.md) — how a long conversation is kept whole on disk while each step gets a bounded rolling slice (recent events plus a rolling summary), and how the end-of-run summary is written.- [Donkey Vision](guides/donkey-vision.md) — the observation layer that turns on-screen windows into structured UI the agent can reason about.- [Background Input](guides/background-input.md) — driving an app without taking over the cursor or raising it: the foreground/background decision and the delivery lanes.diff --git a/docs/architecture.md b/docs/architecture.mdindex c7f763e0..c670f72e 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -73,9 +73,9 @@ in `guides/agent-harness.md` — it is not repeated here.| Store | Holds ||---|---|-| Thread markdown (`App Support/Donkey/Threads/<id>/thread.md`) | conversation contents and full reasoning/tool trace ([how to write it](guides/thread-record.md)) |-| Core Data | durable task metadata, events, and assets for per-run coordination |-| Task and compaction snapshots | execution state, and exactly what context each model decision saw |+| Conversation markdown (`App Support/Donkey/Conversations/<id>/conversation.md`) | conversation contents and full reasoning/tool trace ([how to write it](guides/conversation-record.md)) |+| Core Data | durable conversation metadata, events, and assets for per-run coordination |+| Agent and compaction snapshots | execution state, and exactly what context each model decision saw || Agent memory (SQLite, FTS5 + local vectors) | local-item records, negative lookups, runtime task definitions, bounded harness hints || Learned skill packs (app-support skills directory) | promoted playbooks and validated scripts |diff --git a/docs/guides/context-compaction.md b/docs/guides/context-compaction.mdnew file mode 100644index 00000000..8015fbf8--- /dev/null+++ b/docs/guides/context-compaction.md@@ -0,0 +1,53 @@+# Context Compaction++A conversation can run for many turns and steps, and every one appends to the conversation record. A model has a bounded context window, so the harness can't feed the whole history back each step. Compaction is how Donkey keeps a complete record on disk while handing the model only a small, high-priority slice of it — the same approach Claude Code takes: summarize older turns forward, carry recent ones verbatim. The word covers two jobs: building the rolling context the planner reads each step, and writing the end-of-run summary that sits beside the record.++**The one rule:** the model never sees the raw `conversation.md`. Each step it sees a bounded, priority-ranked view — recent conversation events plus a rolling summary of older ones — and when that view outgrows its budget the older turns are summarized into a fresh summary event the next steps read instead. If a step needs an old detail to stay coherent, pin the event or rely on the rolling summary; don't widen the window, because the compactor clips and drops to fit its budget regardless.++## How It Works++The conversation record (`conversation.md`) is the full, append-only conversation and trace. Beside it the harness keeps the same events as structured data. The two compaction paths read from these and serve different consumers:++```text+conversation events (structured) ──> rolling compaction ──> bounded step context ──> planner+ │+ └─ over threshold ──> summarize older turns ──> new summary event+conversation.md (full markdown) ──> end-of-run summary ──> summary.md ──> conversation view / next session+```++The division of labor: **rolling compaction decides what the model reads next; the end-of-run summary decides what a human (or a later session) reads as the digest.** Rolling compaction runs before every planner call; the end-of-run summary runs once when the run finishes. Both leave the full `conversation.md` untouched — see [Conversation Record](conversation-record.md) for the append-only writing rules.++## Rolling Context++Before each step the compaction driver loads the conversation's events, assets, and active agents, then selects a bounded subset. The strategy keeps four classes of events and merges them, so an important early event still survives even after dozens of later ones:++| Class | What it keeps | Why |+|---|---|---|+| Pinned | the most recent pinned events | events explicitly marked must-survive |+| Summary | the last two summary events | the rolling digest of older history |+| Tool | the most recent tool-result events | what the agent just observed and did |+| Recent | the last several events of any kind | immediate continuity |++Each selected event's text is clipped to a per-event character cap. Assets are capped newest-first. Active agents are kept; finished ones drop out. The selection is rendered to one context string, clipped to a whole-prompt budget, and handed to the planner, which renders it under a `CONVERSATION SO FAR` block — distinct from the current run's tool-history block, which is "what I did this run" rather than "what this conversation is about."++When that rendered context grows past the summary trigger, the driver summarizes the turns about to age out into a new summary event and writes it back into the conversation; the next step's selection picks it up automatically (the summary class above), so older raw turns fall away while their gist stays. A debounce keeps it from re-summarizing every step once near the trigger. The defaults are deliberately small — roughly a dozen recent events, a handful each of pinned and tool events, an eight-thousand-character prompt ceiling, and a six-thousand-character summary trigger below it — and live in one policy struct so the budget is tuned in one place.++Each turn also records a compaction snapshot: which event, asset, and agent ids it included, the resulting prompt size, and a per-class record of how many items were original, included, dropped, and truncated. The snapshot is the audit trail for "why didn't the model remember X" — if an event isn't in the snapshot's included ids, it wasn't in that step's context.++## End-Of-Run Summary++When a run finishes, the harness writes a structured brief to `summary.md` with fixed sections: goal, progress, key decisions, next steps, and critical context. It lands in two stages. A deterministic summary, built from the run's steps and outcome, is written immediately so the digest is never missing. Then, in the background, a model rewrites it from the full `conversation.md` text and replaces the file. The background rewrite never blocks the run's result; if it fails or returns nothing, the deterministic version stays.++The summary is the compact carry-over for the next time the conversation is opened, and it is what the in-app conversation view shows as the conversation's digest.++## Rules++1. **The record is whole; the context is a slice.** Compaction only chooses what to forward to the model and what to digest for a reader. It never edits, truncates, or rewrites `conversation.md`. The rolling summary lives as an event in the structured store, not in the markdown record.+2. **Summarize forward, don't widen.** When the conversation outgrows the budget, the older turns become one rolling summary event; pinning is the only way to force a specific old event to stay verbatim.+3. **One budget, one place.** The caps — events, pinned, tool events, assets, per-event characters, whole-prompt characters, summary trigger, summary debounce — come from a single policy. Tune the budget there; don't special-case it per caller.+4. **Every selection is auditable.** Each turn records a compaction snapshot of what it included and dropped. Reach for the snapshot before assuming the model "ignored" something.+5. **The end-of-run summary degrades safely.** The deterministic summary is the floor; the model-written one is an upgrade applied in the background. A run's outcome never waits on the summary call.++## Where It Lives++The compactor, its policy, and the per-class selection live in the generic harness's conversation store. The rolling-summary driver runs the compactor each step, writes summary events back, and feeds the bounded context to the planner; it is wired into the run loop by the user-query command handler. The end-of-run summary — deterministic build and background rewrite — also lives in the command handler, and the files it writes are owned by the conversation transcript. Start with the compactor in the conversation store.diff --git a/docs/guides/thread-record.md b/docs/guides/conversation-record.mdsimilarity index 63%rename from docs/guides/thread-record.mdrename to docs/guides/conversation-record.mdindex 0ef9e6a5..dd66d1e9 100644--- a/docs/guides/thread-record.md+++ b/docs/guides/conversation-record.md@@ -1,12 +1,12 @@-# Thread Record+# Conversation Record-Every session writes a running record of itself to `thread.md` — the real conversation plus the full reasoning and tool trace, one folder per task under Application Support (`Threads/<id>/thread.md`). It is human-readable markdown, appended live, so you can `tail -f` a run as it unfolds, and it is what the in-app conversation view renders back to the user. A compacted `summary.md` sits beside it.+Every session writes a running record of itself to `conversation.md` — the real conversation plus the full reasoning and tool trace, one folder per conversation under Application Support (`Conversations/<id>/conversation.md`). It is human-readable markdown, appended live, so you can `tail -f` a run as it unfolds, and it is what the in-app conversation view renders back to the user. A compacted `summary.md` sits beside it; how that summary is built and how each model turn gets a bounded slice of this record is covered in [Context Compaction](context-compaction.md).-**The one rule:** every session event a user (or a maintainer reading the run) would want to see goes into the thread, through the one transcript writer — never a bare `print`, never a log line that lives only in the console, never state that exists only in memory. If an event matters enough to happen, it matters enough to be in the record.+**The one rule:** every session event a user (or a maintainer reading the run) would want to see goes into the record, through the one transcript writer — never a bare `print`, never a log line that lives only in the console, never state that exists only in memory. If an event matters enough to happen, it matters enough to be in the record.## How It Works-The thread file is opened once per task and only ever appended to:+The conversation file is opened once per conversation and only ever appended to:```textbegin (writes the header once; no-op if the file exists)@@ -17,7 +17,7 @@ begin (writes the header once; no-op if the file exis-> summary (regenerates the compacted summary.md)```-The run loop records itself: the turn-trace manager is the single sink every model call and every executed step report to, so steps and model calls reach the thread without callers thinking about it. Events that happen *outside* a run — pausing, resuming, approving, or denying a task from the notch — are recorded by the overlay model's lifecycle announcer, which writes the same line to the in-app event store and to the thread. **The run loop records the work; the lifecycle announcer records the user's controls.**+The run loop records itself: the turn-trace manager is the single sink every model call and every executed step report to, so steps and model calls reach the record without callers thinking about it. Events that happen *outside* a run — pausing, resuming, approving, or denying from the notch — are recorded by the overlay model's lifecycle announcer, which writes the same line to the in-app event store and to the conversation file. **The run loop records the work; the lifecycle announcer records the user's controls.**## Entry Kinds@@ -30,17 +30,17 @@ Pick the kind that matches what happened; each renders with its own heading and| step | one executed step as a single block — the decision (thought, reason, action and its input), then that action's output || model call | one call to the model: clipped prompt and reply, finish reason, attempt, duration || response | the assistant's answer that closes a turn |-| system event | a lifecycle event — understanding parsed, run finished, task paused, permission granted |+| system event | a lifecycle event — understanding parsed, run finished, agent paused, permission granted || error | something hit outside a tool result — a planner reply that couldn't be used, a run that aborted before starting |The step is the only grouped block; everything else is a flat entry. Don't re-narrate a step as a separate system event — the step block already carries its decision and output.## Rules-1. **One writer, append-only.** Open the thread once and append. The header is written a single time; later turns and events only add to the end. Never truncate or rewrite the file.-2. **Keyed by task id.** Each task owns its thread folder, addressed by task id, so events from one task never bleed into another.+1. **One writer, append-only.** Open the record once and append. The header is written a single time; later turns and events only add to the end. Never truncate or rewrite the file.+2. **Keyed by conversation id.** Each conversation owns its folder, addressed by conversation id, so events from one conversation never bleed into another. Events carry the id of the agent that produced them, so a conversation's main agent and any subagents stay distinguishable within the one record.3. **Typed activity, not raw strings.** Lifecycle lines come from the centralized activity vocabulary — one icon and label per kind — so the record stays consistent and the conversation view can render it. Don't invent a one-off status string like "Approved — continuing."-4. **Plain text in the store, markdown in the thread.** The in-app event store keeps plain text — structured data the conversation view reads. The thread keeps the icon-prefixed markdown line. Presentation stays out of the data store.+4. **Plain text in the store, markdown in the record.** The in-app event store keeps plain text — structured data the conversation view reads. The record keeps the icon-prefixed markdown line. Presentation stays out of the data store.5. **Bounded and off the main thread.** Clip long prompts, replies, and outputs so a verbose run can't bloat the file, and write off the main thread — recording must never block the UI.## Where It Livesdiff --git a/docs/guides/user-query-overlay.md b/docs/guides/user-query-overlay.mdindex d90d746f..6c0aa2e6 100644--- a/docs/guides/user-query-overlay.md+++ b/docs/guides/user-query-overlay.md@@ -103,9 +103,9 @@ decision is made.- Scriptable app tasks prefer guarded AppleScript before Accessibility, AIvisual targets, or keyboard input. Completion is verified through thegeneric verification policy, not visible result text for every app.-- Thread contents persist as markdown under Application Support- (`Threads/<id>/thread.md`); Core Data stores durable task metadata, events,- and assets for per-run runtime coordination.+- Conversation contents persist as markdown under Application Support+ (`Conversations/<id>/conversation.md`); Core Data stores durable conversation+ metadata, events, and assets for per-run runtime coordination.- Command handling emits actionable `com.donkey.app` route/result logs:submitted commands, routing decisions, action traces (backend, input mode,whether an element click happened, the control or bounds target, and that