Skip to content

Commit afb5010

Browse files
authored
polish v2: smoothness & delight (#484)
* feat(tui): actionable hints on provider/model errors Classify a failed turn's provider error into auth / rate-limit / connectivity / model-not-found / context-overflow and render a one-line next step below the red error row, instead of dumping an identical raw blob for every failure mode. The classifier lives in a new internal/errhint package so both the TUI error row and the CLI exec path can share it (TUIHint references slash commands, CLIHint references zero subcommands). Classification is a conservative string heuristic keyed off providerio.ClassifiedError's prefixes plus lower-level DNS/TLS/timeout/context-length signatures, since the numeric HTTP status is gone by the time the error reaches a UI surface. * feat(tools): budget bash output with head+tail truncation bash was the one tool that returned command output with no byte cap — 'cat large.log' or a verbose test run could dump megabytes into context and force compaction, while every other read/search tool already applies a budget. Cap stdout and stderr at 96KB each, keeping the head and tail of an oversized stream (build/test failures usually land at the tail) and dropping the middle behind a marker that points at redirect-to-file + read_file. Record raw_bytes/emitted_bytes/estimated_tokens/truncated in Meta like the other tools. Sandbox-denial and shell-issue detection still run on the full raw output before budgeting. * fix(gemini): retry 401 with refreshed OAuth token The Gemini provider used providerio.SendWithRetry (429/503/529 only), so an expired OAuth token surfaced as a terminal 'API key not valid' auth error while OpenAI and Anthropic silently recovered via SendWithAuthRetry's force-refresh-and-replay on 401. Plumb OAuthResolver through gemini.Options/Provider and the factory (exactly as the other two providers already do) and switch the stream send to SendWithAuthRetry. With a nil resolver (API-key users) behavior is unchanged. * fix(errhint): gate classification on provider-origin marker Address CodeRabbit review on #1: agentResponseMsg.err can also carry local failures (a tool's 'permission denied', a 'file does not exist', a config error), and broad substrings would have attached a bogus /provider or /model hint to those. Classify now returns Unknown unless the message carries a provider marker the provider layer always attaches (auth error: / rate limit error: / provider error: / provider request error: / provider stream error:). Local errors never draw a provider hint; provider errors sub-classify as before. Added local-failure test cases proving they stay Unknown. * feat(cli): actionable hint on text-mode exec provider errors zero exec printed provider failures as a bare '[zero] <raw error>' with no next step. Append a one-line hint (reusing the shared errhint classifier) for recognized provider failures — 'run `zero auth`', 'run `zero doctor`', etc. The provider-origin gate means non-provider codes (sandbox_error, mcp_error) and JSON/stream-json output are untouched. * fix(tools): set Result.Truncated for budgeted bash output Address CodeRabbit review on #3: budgetBashOutput recorded truncation in meta["truncated"] but never set the Result.Truncated struct field, so consumers reading the struct (like glob/web_fetch/read_minified do) would miss truncated bash results. Return the bool from budgetBashOutput and set Result.Truncated on every return path. * perf(tui): reuse one LSP manager across prompts runAgentWithOptions built a fresh lsp.NewManager per run and shut it down when the run returned, so gopls (and every other language server) cold-started on the first edit of every turn — 200ms-2s of latency the manager's own design (long-lived, reused servers) exists to avoid. Build one manager per session in newModel (cheap: servers start lazily on first Check) and reuse it across runs; the SelfCorrector still gets a fresh checker wrapping the shared manager each run. Torn down with a short deadline in quit(). Runs fall back to a per-run manager when the session manager is absent (cwd unknown, or a directly-built test model). * perf(tui): accumulate streamingText as []byte (O(1) append) m.streamingText += msg.delta was O(len) per delta -> O(n²) across a long generation (a 10k-token code gen allocates hundreds of MB of intermediate strings and the UI gets progressively laggier). Accumulate into a []byte with append (O(1) amortized) and read via streamingTextString(). A []byte rather than strings.Builder because the TUI model is copied by value on every Update, which would trip strings.Builder's copy check; []byte is nil-safe and copies cleanly like the other slice fields. * perf(tui): coalesce streamed text deltas to one frame Every OnText delta was its own tea.Msg, so a fast provider (100+ tok/s) drove 100+ full Update->View cycles per second, each re-parsing the growing markdown — visible stutter over SSH and wasted CPU. Batch agentTextMsg deltas at the runtime sink over a ~16ms frame and forward them as a single message, decoupling render rate from token rate. Any non-text message flushes pending text first so ordering with tool-call / reasoning / row messages is preserved; the final agentResponseMsg (a tea.Cmd return, not a sink message) is safe because the model already drops deltas for an inactive runID. * fix(agent): raise tool-failure stop threshold 4->6 guardrails.go halted a run after 4 consecutive same-error tool failures. A corrective hint fires at 2, and a model iterating on a genuinely tricky edit can legitimately fail a couple more times after the hint while converging — stopping at 4 cut those runs short. Raise to 6; the streak still resets the instant the tool succeeds or hits a different error, so only true same-error loops are affected. * fix(agent): more reconnect retries with jitter; narrow to transport errors #19: bump maxStreamReconnects 2->4 and add up-to-50% jitter on the exponential backoff (capped at 8s), so a multi-second network blip is ridden out instead of killing the run on a 2s hiccup, and concurrent runs (swarms, cron fleets) don't reconnect in lockstep. #34: drop the '502'/'503' substring matches from shouldReconnect. 503 already exhausted providerio.SendWithRetry (retrying here is a redundant double-retry) and 502 is non-idempotent by providerio's rule (the POST may have been processed). Only genuine transport failures — where no response was received — reconnect now. * fix(agent): reduce stall retries 2->1 to bound stuck-session time A no-output stream stall is detected only after the full stream idle timeout (~5min) elapses, and each retry can idle again — so 2 retries left an interactive session frozen for ~15min. Drop to 1 retry: keeps the common single-hiccup recovery while bounding the worst case to ~2x the idle timeout. * fix(tui): serialize coalescer forwarding to preserve message order Address CodeRabbit review on #7: flush() and the run-switch path drained the text buffer under c.mu but called forward() after releasing it, so a timer-fired text flush could race a concurrent non-text send() and land after the tool-call/reasoning message it should precede. Hold c.mu across drain AND forward (drainAndForwardLocked), so whoever holds the lock delivers atomically and the other caller blocks until it is done — text can never overtake a following non-text message. Added a concurrency stress test (timer racing an inline send) that passes under -race. * feat(tui): accept /compact now Bare /compact already triggers compaction, but /compact now — what users reach for when the context gauge climbs — hit the usage-error branch. Accept the 'now' keyword and advertise it in the command usage. * feat(tui): add /retry, /edit, /copy, /export commands /retry resends the last prompt; /edit recalls it into the composer to tweak and resend — both read a new lastPrompt field captured verbatim (pre-expansion) in launchPrompt. /copy puts the last answer on the clipboard via the existing copy machinery; /export writes a plain role-prefixed transcript to a file (timestamped default, or a given path). Real gaps for SSH users with no mouse select. * feat(tools): separator-insensitive tool_search matching tool_search ranked deferred tools by exact substring, so a model that typed 'webfetch' (dropping the underscore) matched nothing and looped. Add a separator-squashing fallback ('web_fetch' -> 'webfetch') scored below an exact substring match, so precise queries still rank first. * feat(tools): signal ask_user dismissal distinctly from a blank field Empty answers all rendered as '(no answer provided)', so the model couldn't tell a wholesale dismissal (user closed the prompt without answering anything) from one field left blank amid real answers — and might invent a default. FormatAskUserAnswers now flags a full dismissal up front as a skip and marks individual empties '(left blank)' vs '(skipped)'. * feat(tui): fuzzy ranking in model/session pickers The pickers filtered by flat strings.Contains with no ranking, so finding 'sonnet 4.5' among 50+ models meant an exact prefix or scrolling. Rank matches (exact < prefix < contains < subsequence, reusing fuzzySubsequenceGap) so the closest match lands on top, and 'snt45' now matches 'Sonnet 4.5'. Groups stay contiguous — ordered by their best match, never split — so the grouped model picker still renders one header per provider. Covers both /model and /resume (shared commandPicker). * perf(agent): cache per-tool schema render across turns partitionTools re-ran the recursive schema->map conversion (schemaToRuntimeMap) for every tool on every turn — redundant, since a tool's advertised name/description/schema is stable for the run. Add a per-run definition cache keyed by tool name (nil-safe; the plain partitionTools entrypoint and tests pass nil for fresh renders). The partitioning itself (visibility, deferral, ordering) still recomputes every turn — it must, because a tool's deferred state can flip mid-run (swarm tools un-defer once a swarm is active). Only the expensive schema render is memoized. tool_search is excluded by its callers (dynamic description) so it never poisons the cache. * perf(agent): calibrate compaction estimate against real prompt tokens The byte/4 heuristic (ApproxTextTokens) over-counts code-heavy content ~15-20%, so with triggerRatio=0.7 compaction fired at ~60% of true capacity — premature summarizer calls that degrade quality. Each turn now folds (rawEstimate, provider InputTokens) into an EMA calibration ratio, clamped per-sample to a sane band; maybeCompact and the reactive path scale their estimates by it. Turn 1 uses the raw estimate (no data yet); later turns compact near real capacity. * style: gofmt the new agent test files The CI gofmt check flagged the #22/#25 test files (struct-field and switch alignment). No behavior change. * feat(agent): turn-based plan staleness reminder The stale-plan nudge only fired after 10 tool calls since the last update_plan, so a plan that drifts stale across many low-tool-call turns (one call per turn) took many turns to catch — the 'plan set turn 1 stays pending forever' case. Add a turn-count complement: after stalePlanTurnThreshold (8) turns without an update AND with items still pending, fire the same one-shot reminder. Gated on pending items so a fully-completed plan is never nagged; the existing tool-call trigger is unchanged. (The roadmap's fuzzy 'tool calls unrelated to plan items' relevance match is deliberately omitted — too false-positive-prone.) * feat(agent): preserve recent-edits summary across compaction Compaction preserved the plan, loaded skills, tool schemas, and project instructions, but not file edits — so after the editing turns were elided the model knew it had changed a file only vaguely, and would re-read to rediscover its own footprint. Extract write_file/edit_file targets from the elided middle with a one-line note from each tool result, merge them across repeated compactions (newer note per path wins), and carry them in the preserved-state JSON (capped at 20 paths / 160 bytes per note). * feat(tui): workspace-scoped /resume + fewer event reads /resume listed sessions from every project globally, and read the full event file for each of them to test for resumable content. Filter the picker (and /resume latest) by the current workspace Cwd, checked BEFORE the per-session read so a large global history no longer pays N full file reads to build one workspace's list; also skip zero-event sessions via metadata without any read. Sessions with no recorded Cwd (older runs) and an unknown current workspace stay visible so nothing is hidden that can't be confidently placed. Explicit /resume <id> still resolves any session. * feat(tui): warn on model switch that drops staged images Staged images were silently dropped at submit when the active model had no vision support — the user only learned after sending. Chips already render above the composer (renderAttachmentChips); add an immediate amber warning line to the /model switch status (both same-provider and cross-provider paths) when images are staged and the newly selected model can't accept them, so the drop is surfaced at switch time. * feat(tui): inline recovery affordance on setup errors A first-run setup error rendered as a bare red 'error: <text>' with no pointer to the fix. Append a faint, stage-tailored recovery line (edit the endpoint/name/key above then Enter, or pick another model) that names only keys the stage's footer confirms, so the guidance is always accurate. Empty for stages whose footer already makes recovery obvious. * fix: address CodeRabbit review (5 findings) - loop.go: recompute the compaction calibration estimate at calibration time, not request-build time, so a reactive-compaction reissue that shrinks the request feeds the EMA the sample that actually completed. - errhint: match bare HTTP status codes (401/403/429/529) only at digit boundaries, so an incidental number ('4290ms', 'id 14015') isn't mis-bucketed as auth/rate-limit. - bash: drop the stray unmatched ']' in the output-truncation marker. - tui /retry: apply the same exiting/compactInFlight guards commandPrompt has, so a retry can't race compactResultMsg's rewrite of session state. - tui /export: write the transcript 0o600 (it may contain echoed secrets), not world/group-readable 0o644. * test(tui): skip export file-mode assertion on Windows os.WriteFile ignores Unix permission bits on Windows and Stat reports 0666, so the 0o600 assertion failed there. The production 0o600 is honored on Linux/macOS (where it matters); guard the check with runtime.GOOS so Windows smoke passes. * fix(tui): align /resume latest filters + case-insensitive workspace match on Windows Address CodeRabbit review on #21: - latestResumableInWorkspace now applies the SAME filters as the picker (skip zero-event metadata and empty/failed runs), so /resume latest can't land on a session the picker intentionally hides. - sessionMatchesWorkspace compares paths case-insensitively on Windows (its filesystem is), so the same workspace spelled with different casing no longer hides resumable sessions; other platforms stay case-sensitive. * fix(tui): /retry resends the last prompt's image/PDF attachments /retry relaunched only lastPrompt's text, but launchPrompt clears the pending image/document queues once a turn is sent. A vision- or PDF-backed prompt that failed would silently retry as text-only and answer a different task. Snapshot the consumed attachments at launch and re-stage them on /retry so launchPrompt rebuilds an identical request (document preamble, images, and the submit-time vision re-check). startNewSession drops the snapshot so a post-/new /retry cannot leak the previous session's attachments. Also correct the OAuthResolver factory comment, which omitted the Gemini provider it is now passed to. * fix: address jatmn review (reconnect 5xx, /edit attachments, edit-cap, coalescer test) - reconnect: exclude HTTP 5xx (500/502/503/504) before the transport substring match so "504 Gateway Timeout" no longer slips through on the generic "timeout" needle. A gateway timeout is non-idempotent — the completion POST may already have reached the model — so replaying the connect risked duplicate billable work. Reuses a digit-boundary status matcher exported from errhint (HasStatusCode). Test asserts 504/500 stay non-reconnectable. - tui /edit: re-stage the remembered image/PDF snapshot alongside the recalled prompt text, mirroring the /retry fix — editing a vision- or document-backed prompt no longer silently resends a text-only version. - compaction recent-edits: order edits by LAST edit, not first, and move a re-touched path to the newest position on merge, so the maxRecentEdits tail cap keeps the file the model most recently edited instead of dropping it. Adds mergeRecentEdits (edit-specific) and a capped-list regression. - coalescer test: inject the frame timer via afterFunc so TestCoalescerBatchesDeltas proves buffering deterministically instead of racing the real 16ms wall-clock timer. * fix(tools): bound bash output capture in memory, not just model-visible text Previously stdout/stderr streamed into unbounded bytes.Buffers and were only truncated to the 96 KiB budget AFTER the command finished, so a runaway command (cat huge.log, yes) could grow Zero's memory until it stalled or OOMed before budgetBashOutput ever ran. Replace the capture with boundedBuffer, an io.Writer that keeps only the head+tail each stream will surface and discards the middle as it arrives, while counting the true total for the truncation marker. Memory is now bounded to ~head+2×tail per stream regardless of output size. truncateHeadTail is refactored onto a total-aware core so the string path (and its tests) are unchanged, and budgetBashCapture reports the true raw_bytes even though only a bounded slice was held. Adds boundedBuffer/capture unit tests and an end-to-end test that streams ~5× the budget and asserts bounded emission with an accurate raw_bytes.
1 parent 30dd7c3 commit afb5010

55 files changed

Lines changed: 2802 additions & 122 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎internal/agent/compaction.go‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,43 @@ type compactionState struct {
325325
// OnText is deliberately NOT forwarded (compaction stays invisible to the user),
326326
// but its token COST must still be counted so usage reports and budgets include it.
327327
onUsage func(Usage)
328+
329+
// calibrationRatio scales the raw byte/4 token estimate toward the provider's
330+
// real prompt-token count. ApproxTextTokens over-counts code-heavy content by
331+
// ~15-20%, which would trip compaction early (at ~60% of true capacity). It
332+
// starts at 1.0 and converges via an EMA as each turn reports actual usage, so
333+
// later turns compact nearer to real capacity. Zero is treated as 1.0.
334+
calibrationRatio float64
335+
}
336+
337+
// calibrate folds one turn's (rawEstimate, actualPromptTokens) sample into the
338+
// running calibration ratio. A single sample is clamped to a sane band so an
339+
// outlier (a huge cache-read turn, a provider-overhead spike) can't skew the
340+
// estimate enough to disable or thrash compaction.
341+
func (state *compactionState) calibrate(rawEstimate int, actualPromptTokens int) {
342+
if !state.enabled || rawEstimate <= 0 || actualPromptTokens <= 0 {
343+
return
344+
}
345+
sample := float64(actualPromptTokens) / float64(rawEstimate)
346+
if sample < 0.5 {
347+
sample = 0.5
348+
} else if sample > 2.0 {
349+
sample = 2.0
350+
}
351+
if state.calibrationRatio <= 0 {
352+
state.calibrationRatio = 1.0
353+
}
354+
const alpha = 0.3 // weight on the newest sample; smooths jitter across turns
355+
state.calibrationRatio = state.calibrationRatio*(1-alpha) + sample*alpha
356+
}
357+
358+
// calibratedTokens applies the learned ratio to a raw estimate. Before any sample
359+
// arrives (ratio unset) it returns the raw estimate unchanged.
360+
func (state *compactionState) calibratedTokens(raw int) int {
361+
if state.calibrationRatio <= 0 {
362+
return raw
363+
}
364+
return int(float64(raw) * state.calibrationRatio)
328365
}
329366

330367
func newCompactionState(options Options) *compactionState {
@@ -353,7 +390,7 @@ func (state *compactionState) maybeCompact(
353390
// the messages; both the threshold check and the shrink check below use the
354391
// same term so they stay consistent.
355392
toolTokens := estimateToolDefTokens(tools)
356-
size := estimateTokens(messages) + toolTokens
393+
size := state.calibratedTokens(estimateTokens(messages) + toolTokens)
357394
if size <= state.threshold {
358395
return messages
359396
}
@@ -370,7 +407,7 @@ func (state *compactionState) maybeCompact(
370407
// entirely and preserve recent turns verbatim.
371408
if pruned, reclaimed := pruneStaleToolOutput(messages, state.preserveLast); reclaimed > 0 {
372409
messages = pruned
373-
size = estimateTokens(messages) + toolTokens
410+
size = state.calibratedTokens(estimateTokens(messages) + toolTokens)
374411
if size <= state.threshold {
375412
state.lowWaterMark = size
376413
return messages
@@ -386,7 +423,7 @@ func (state *compactionState) maybeCompact(
386423
// later turn) can try again; we never drop messages on failure here.
387424
return messages
388425
}
389-
newSize := estimateTokens(compacted) + toolTokens
426+
newSize := state.calibratedTokens(estimateTokens(compacted) + toolTokens)
390427
if newSize >= size {
391428
// Compaction did not actually shrink anything (e.g. nothing to
392429
// summarize). Leave the history untouched and don't churn next turn.
@@ -444,7 +481,7 @@ func (state *compactionState) recover(
444481
// the SAME combined (messages + tool-defs) domain maybeCompact uses, so the
445482
// proactive shrink-guard compares like with like.
446483
state.reactiveAttempted = true
447-
state.lowWaterMark = estimateTokens(result) + estimateToolDefTokens(tools)
484+
state.lowWaterMark = state.calibratedTokens(estimateTokens(result) + estimateToolDefTokens(tools))
448485
return result, true, nil
449486
}
450487

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package agent
2+
3+
import "testing"
4+
5+
func TestCalibrationConvergesTowardActual(t *testing.T) {
6+
state := &compactionState{enabled: true}
7+
8+
// Before any sample, the estimate is unchanged.
9+
if got := state.calibratedTokens(1000); got != 1000 {
10+
t.Fatalf("uncalibrated tokens = %d, want 1000", got)
11+
}
12+
13+
// The estimator over-counts: our raw estimate is 1000 but the provider reports
14+
// 850 (a 0.85 ratio). Feeding that sample repeatedly should pull the ratio
15+
// toward 0.85, so a 1000-token raw estimate calibrates downward.
16+
for range 20 {
17+
state.calibrate(1000, 850)
18+
}
19+
got := state.calibratedTokens(1000)
20+
if got >= 1000 || got < 820 || got > 880 {
21+
t.Fatalf("calibrated tokens = %d, want ~850 after convergence", got)
22+
}
23+
}
24+
25+
func TestCalibrateIgnoresDegenerateSamples(t *testing.T) {
26+
state := &compactionState{enabled: true}
27+
state.calibrate(0, 500) // zero estimate
28+
state.calibrate(500, 0) // zero actual
29+
if state.calibrationRatio != 0 {
30+
t.Fatalf("degenerate samples must not move the ratio, got %v", state.calibrationRatio)
31+
}
32+
// Disabled state never calibrates.
33+
disabled := &compactionState{enabled: false}
34+
disabled.calibrate(1000, 850)
35+
if disabled.calibrationRatio != 0 {
36+
t.Fatalf("disabled compaction must not calibrate, got %v", disabled.calibrationRatio)
37+
}
38+
}
39+
40+
func TestCalibrateClampsOutliers(t *testing.T) {
41+
state := &compactionState{enabled: true}
42+
// A wild outlier (10x) is clamped to 2.0, so one bad sample can't blow up the
43+
// ratio and disable compaction.
44+
for range 50 {
45+
state.calibrate(1000, 10000)
46+
}
47+
if state.calibrationRatio > 2.0 {
48+
t.Fatalf("ratio should be clamped at 2.0, got %v", state.calibrationRatio)
49+
}
50+
}

‎internal/agent/compaction_preserve.go‎

Lines changed: 194 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ const (
2020
toolNameUpdatePlan = "update_plan"
2121
toolNameToolSearch = "tool_search"
2222
toolNameSkill = "skill"
23+
toolNameWriteFile = "write_file"
24+
toolNameEditFile = "edit_file"
25+
)
26+
27+
// maxRecentEdits caps how many edited files are carried across a compaction, and
28+
// maxEditNoteBytes caps each edit's one-line note, so a long editing run can't
29+
// bloat the preserved-state block.
30+
const (
31+
maxRecentEdits = 20
32+
maxEditNoteBytes = 160
2333
)
2434

2535
const (
@@ -100,12 +110,123 @@ func formatPlanArguments(arguments string) string {
100110
}
101111

102112
// skillEntry is a named preserved body. It began as loaded-skill state and is
103-
// reused for loaded tools and project instruction blocks.
113+
// reused for loaded tools, project instruction blocks, and recent file edits.
104114
type skillEntry struct {
105115
name string
106116
body string
107117
}
108118

119+
// recentEdits returns the files mutated by write_file/edit_file calls in messages
120+
// — latest note per path, in last-seen order — as skillEntry{name: path, body:
121+
// note}. After compaction elides the editing turns, this tells the model WHAT it
122+
// changed in each file (from the tool's result) so it need not re-read to
123+
// rediscover its own footprint. Capped at maxRecentEdits paths.
124+
//
125+
// Ordering is by LAST edit, not first: re-editing a file moves it to the newest
126+
// position so the tail cap below keeps the file the model most recently touched
127+
// rather than an earlier, now-stale entry.
128+
func recentEdits(messages []zeroruntime.Message) []skillEntry {
129+
pathByID := map[string]string{}
130+
sequence := make([]string, 0)
131+
for _, message := range messages {
132+
for _, call := range message.ToolCalls {
133+
if call.Name != toolNameWriteFile && call.Name != toolNameEditFile {
134+
continue
135+
}
136+
path := editPathFromArguments(call.Arguments)
137+
if path == "" {
138+
continue
139+
}
140+
if call.ID != "" {
141+
pathByID[call.ID] = path
142+
}
143+
sequence = append(sequence, path)
144+
}
145+
}
146+
order := lastSeenOrder(sequence)
147+
if len(order) == 0 {
148+
return nil
149+
}
150+
151+
noteByPath := map[string]string{}
152+
for _, message := range messages {
153+
if message.Role != zeroruntime.MessageRoleTool || message.ToolCallID == "" {
154+
continue
155+
}
156+
if path, ok := pathByID[message.ToolCallID]; ok {
157+
noteByPath[path] = editNote(message.Content)
158+
}
159+
}
160+
161+
// Keep the most recent maxRecentEdits paths (the tail of last-seen order).
162+
if len(order) > maxRecentEdits {
163+
order = order[len(order)-maxRecentEdits:]
164+
}
165+
entries := make([]skillEntry, 0, len(order))
166+
for _, path := range order {
167+
entries = append(entries, skillEntry{name: path, body: noteByPath[path]})
168+
}
169+
return entries
170+
}
171+
172+
// lastSeenOrder dedupes paths keeping each at its LAST occurrence, preserving
173+
// chronological order otherwise. A re-edited path therefore lands at the newest
174+
// position rather than staying pinned to where it first appeared.
175+
func lastSeenOrder(paths []string) []string {
176+
lastIdx := make(map[string]int, len(paths))
177+
for i, p := range paths {
178+
lastIdx[p] = i
179+
}
180+
order := make([]string, 0, len(lastIdx))
181+
for i, p := range paths {
182+
if lastIdx[p] == i {
183+
order = append(order, p)
184+
}
185+
}
186+
return order
187+
}
188+
189+
// editPathFromArguments pulls the target file path from a write_file/edit_file
190+
// call's JSON arguments (path/file/file_path/filename aliases). Returns "" on
191+
// malformed arguments or no path.
192+
func editPathFromArguments(arguments string) string {
193+
var parsed struct {
194+
Path string `json:"path"`
195+
File string `json:"file"`
196+
FilePath string `json:"file_path"`
197+
Filename string `json:"filename"`
198+
}
199+
if err := json.Unmarshal([]byte(strings.TrimSpace(arguments)), &parsed); err != nil {
200+
return ""
201+
}
202+
for _, candidate := range []string{parsed.Path, parsed.File, parsed.FilePath, parsed.Filename} {
203+
if s := strings.TrimSpace(candidate); s != "" {
204+
return s
205+
}
206+
}
207+
return ""
208+
}
209+
210+
// editNote reduces an edit tool's result to a single short line (its first
211+
// non-empty line, byte-capped on a rune boundary) for the preserved summary.
212+
func editNote(content string) string {
213+
for _, line := range strings.Split(content, "\n") {
214+
s := strings.TrimSpace(line)
215+
if s == "" {
216+
continue
217+
}
218+
if len(s) > maxEditNoteBytes {
219+
limit := maxEditNoteBytes
220+
for limit > 0 && !utf8.RuneStart(s[limit]) {
221+
limit--
222+
}
223+
s = s[:limit] + "…"
224+
}
225+
return s
226+
}
227+
return ""
228+
}
229+
109230
// loadedSkills returns the skills loaded via the skill tool in messages — the
110231
// latest body per name, in first-seen order — matching each skill tool call to
111232
// its tool result by id.
@@ -310,11 +431,17 @@ func capBody(body string) string {
310431
// preservedState is the JSON shape of the carried-across-compaction block.
311432
type preservedState struct {
312433
Plan string `json:"plan,omitempty"`
434+
RecentEdits []preservedEdit `json:"recent_edits,omitempty"`
313435
Tools []preservedTool `json:"tools,omitempty"`
314436
Skills []preservedSkill `json:"skills,omitempty"`
315437
ProjectInstructions []preservedInstruction `json:"project_instructions,omitempty"`
316438
}
317439

440+
type preservedEdit struct {
441+
Path string `json:"path"`
442+
Note string `json:"note,omitempty"`
443+
}
444+
318445
type preservedTool struct {
319446
Name string `json:"name"`
320447
Body string `json:"body"`
@@ -347,6 +474,12 @@ func appendPreservedState(summary string, middle []zeroruntime.Message) string {
347474
plan = priorState.Plan
348475
}
349476

477+
// Recent edits: merge edits preserved earlier with fresh write_file/edit_file
478+
// results in middle (newer note per path wins AND moves to the newest
479+
// position), so the tail cap keeps the files the model most recently touched
480+
// across repeated compactions rather than dropping a just-re-edited file.
481+
edits := capRecentEdits(mergeRecentEdits(preservedEditsToEntries(priorState.RecentEdits), recentEdits(middle)))
482+
350483
// Tools: preserve deferred tool_search schemas from the transcript. Fresh
351484
// loads override older carried copies by name.
352485
tools := mergeSkillEntries(preservedToolsToEntries(priorState.Tools), loadedToolSchemas(middle))
@@ -360,19 +493,76 @@ func appendPreservedState(summary string, middle []zeroruntime.Message) string {
360493
projectInstructionEntries(middle),
361494
)
362495

363-
if block := formatPreservedState(plan, tools, skills, instructions); block != "" {
496+
if block := formatPreservedState(plan, edits, tools, skills, instructions); block != "" {
364497
summary += "\n\n" + block
365498
}
366499
return summary
367500
}
368501

502+
// mergeRecentEdits overlays fresh edits onto edits preserved by an earlier
503+
// compaction. Unlike mergeSkillEntries (which keeps refreshed entries in their
504+
// original slot), a path touched again by a fresh edit MOVES to the newest
505+
// position and takes the fresh note, so capRecentEdits — which keeps only the
506+
// most-recent tail — never drops a file the model just re-edited. Paths present
507+
// only in the older set keep their relative order, ahead of the fresh ones.
508+
func mergeRecentEdits(older, newer []skillEntry) []skillEntry {
509+
if len(newer) == 0 {
510+
return older
511+
}
512+
freshBody := make(map[string]string, len(newer))
513+
freshOrder := make([]string, 0, len(newer))
514+
for _, e := range newer {
515+
if _, ok := freshBody[e.name]; !ok {
516+
freshOrder = append(freshOrder, e.name)
517+
}
518+
freshBody[e.name] = e.body
519+
}
520+
merged := make([]skillEntry, 0, len(older)+len(freshOrder))
521+
// Older entries that were NOT re-edited keep their position, oldest first.
522+
for _, e := range older {
523+
if _, refreshed := freshBody[e.name]; refreshed {
524+
continue // re-appended at its newest position below
525+
}
526+
merged = append(merged, e)
527+
}
528+
// Fresh edits follow, in last-seen order, each carrying the fresh note — so
529+
// the most recently touched files sit at the tail the cap preserves.
530+
for _, name := range freshOrder {
531+
merged = append(merged, skillEntry{name: name, body: freshBody[name]})
532+
}
533+
return merged
534+
}
535+
536+
// capRecentEdits bounds the preserved edit list to the most recent maxRecentEdits
537+
// entries after a merge, so repeated compactions can't grow it without limit.
538+
func capRecentEdits(entries []skillEntry) []skillEntry {
539+
if len(entries) > maxRecentEdits {
540+
return entries[len(entries)-maxRecentEdits:]
541+
}
542+
return entries
543+
}
544+
545+
func preservedEditsToEntries(edits []preservedEdit) []skillEntry {
546+
entries := make([]skillEntry, 0, len(edits))
547+
for _, e := range edits {
548+
if e.Path == "" {
549+
continue
550+
}
551+
entries = append(entries, skillEntry{name: e.Path, body: e.Note})
552+
}
553+
return entries
554+
}
555+
369556
// formatPreservedState renders state as the labelled, single-line
370557
// JSON block. Returns "" when there is nothing to preserve.
371-
func formatPreservedState(plan string, tools, skills, instructions []skillEntry) string {
372-
if plan == "" && len(tools) == 0 && len(skills) == 0 && len(instructions) == 0 {
558+
func formatPreservedState(plan string, edits, tools, skills, instructions []skillEntry) string {
559+
if plan == "" && len(edits) == 0 && len(tools) == 0 && len(skills) == 0 && len(instructions) == 0 {
373560
return ""
374561
}
375562
state := preservedState{Plan: plan}
563+
for _, e := range edits {
564+
state.RecentEdits = append(state.RecentEdits, preservedEdit{Path: e.name, Note: e.body})
565+
}
376566
for _, t := range tools {
377567
state.Tools = append(state.Tools, preservedTool{Name: t.name, Body: t.body})
378568
}

0 commit comments

Comments
 (0)