mirror of
https://github.com/Twigpine/zero.git
synced 2026-11-12 19:13:28 -10:00
* Improve harness context efficiency
* Address harness efficiency review findings
* Make recaps idle and cancellable
Delay recap generation until the session has been inactive, orient it around the active goal and next task, cancel stale work on activity, and render the result ephemerally above the composer.
Also strengthen the formatter safety regression to exercise formatter-modified edit output.
* Track every replace-all edit span
Translate each replacement into its post-edit line range, including cumulative shifts from earlier replacements, so follow-up edits remain authorized for every model-supplied change.
* Make review regressions cross-platform
Verify replace-all tracking through the canonical edit tool path instead of a platform-sensitive temp path, and cover cancellation when recaps are disabled during generation.
* fix(tools): derive SeenWhole from the recorded ranges
SeenWhole was a flag set only by a SINGLE read covering the file, while
SeenRange judged coverage from the accumulated ranges. The two then
disagreed about identical reads: after reading lines 1-50 and 51-100 of a
100-line file, SeenRange(1,100) was true and SeenWhole was false.
That matters because write_file gates overwrites on SeenWhole. A file past
the read byte budget cannot be read whole in one call — the read is
truncated and RecordSeenRange is correctly skipped, since the model did not
see those lines — so no sequence of reads could ever set the flag, and the
overwrite was refused permanently with:
the intended change depends on content that has not been read exactly
in this session. Read the affected lines with read_file, then retry.
Advice that could not change the answer. edit_file was unaffected because it
gates on SeenRange, so the escape was to edit rather than rewrite, but a
model following the error text would loop.
SeenWhole is now computed from the ranges against the file's recorded line
count, so scoped reads that together cover a file make it writable and the
error text becomes actionable. The observation carries that line count, and
a read reporting a different one clears the accumulated ranges: they
describe a shape the file no longer has.
Coverage is judged by merging sorted ranges rather than walking every line
against every range, which the previous SeenRange did — same answer, and it
no longer scans line-by-line over a large file.
Co-Authored-By: Claude Opus 5 <[email protected]>
* test(tools): key the large-file test on the resolved path
read_file resolves the workspace root through EvalSymlinks before recording
anything, so the tracker is keyed on the resolved path. The test asserted
against the raw t.TempDir() path, which is the same string on Windows and a
different one on macOS, where the temp dir sits under /var — a symlink to
/private/var. The SeenWhole assertions would have looked up a key that never
existed: passing here, failing on macOS CI.
Also switches the line generator to fmt.Fprintf (staticcheck QF1012).
Both from CodeRabbit on #842.
Co-Authored-By: Claude Opus 5 <[email protected]>
* test(tools): actually switch the line generator to fmt.Fprintf
The previous commit's message claimed this was done. It was not: the edit
silently did not apply and I reported it fixed without re-reading the file,
so builder.WriteString(fmt.Sprintf(...)) survived at the head — caught by
@anandh8x, not by me.
Verified this time by running the linter rather than by reading the diff:
golangci-lint --enable-only unused,ineffassign,staticcheck ./internal/tools/
reports 0 issues.
* fix: keep exact-read workflows recoverable
* test: cover permission normalization wiring
* fix: commit file observations after output budgeting
* fix: defer file observations across retries
* test: normalize patch line endings
---------
Co-authored-by: Vasanthdev2004 <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>
272 lines
8.9 KiB
Go
272 lines
8.9 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
tea "charm.land/bubbletea/v2"
|
|
|
|
"github.com/Gitlawb/zero/internal/config"
|
|
"github.com/Gitlawb/zero/internal/zeroruntime"
|
|
)
|
|
|
|
const (
|
|
// recapIdleDelay keeps recap generation off the critical path of ordinary
|
|
// turns. A recap is useful when returning to a quiet session, not immediately
|
|
// after reading the answer that it would otherwise repeat.
|
|
recapIdleDelay = 4 * time.Minute
|
|
// recapTimeout bounds a single recap generation so a hung provider can never
|
|
// wedge the background command.
|
|
recapTimeout = 30 * time.Second
|
|
// recapMaxContextChars bounds the recent conversation excerpt sent to the
|
|
// provider. Goal and plan hints are added separately.
|
|
recapMaxContextChars = 4800
|
|
recapMaxRows = 8
|
|
)
|
|
|
|
const recapSystemPrompt = "The user is returning to an idle coding session. In under 40 words and 1-2 plain sentences, state the overall goal or current task, then the next actionable step. Reply with only the recap: no markdown, preamble, or trailing question."
|
|
|
|
// recapIdleMsg marks the end of an inactivity window. seq makes abandoned
|
|
// timers cheap: any subsequent user activity or run invalidates the message.
|
|
type recapIdleMsg struct {
|
|
runID int
|
|
seq int
|
|
}
|
|
|
|
// recapGeneratedMsg carries the outcome of a background recap generation back to
|
|
// the Update loop.
|
|
type recapGeneratedMsg struct {
|
|
runID int
|
|
seq int
|
|
recap string
|
|
err error
|
|
}
|
|
|
|
// cleanGeneratedRecap normalizes a raw model response into a single recap line:
|
|
// the first non-empty line, surrounding markup stripped.
|
|
func cleanGeneratedRecap(raw string) string {
|
|
for _, line := range strings.Split(strings.TrimSpace(raw), "\n") {
|
|
if trimmed := strings.Trim(line, " \t\r\n\"'`*#"); trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// generateRecap asks the provider for a compact orientation note using bounded
|
|
// session context. It is an isolated side request with no tools.
|
|
func generateRecap(ctx context.Context, provider zeroruntime.Provider, sessionContext string) (string, error) {
|
|
if provider == nil {
|
|
return "", errors.New("no provider configured")
|
|
}
|
|
sessionContext = strings.TrimSpace(sessionContext)
|
|
if sessionContext == "" {
|
|
return "", errors.New("no session context to recap")
|
|
}
|
|
request := zeroruntime.CompletionRequest{
|
|
Messages: []zeroruntime.Message{
|
|
{Role: zeroruntime.MessageRoleSystem, Content: recapSystemPrompt},
|
|
{Role: zeroruntime.MessageRoleUser, Content: sessionContext},
|
|
},
|
|
}
|
|
stream, err := provider.StreamCompletion(ctx, request)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
collected := zeroruntime.CollectStreamWithOptions(ctx, stream, zeroruntime.CollectOptions{})
|
|
if collected.Error != "" {
|
|
return "", errors.New(collected.Error)
|
|
}
|
|
recap := cleanGeneratedRecap(collected.Text)
|
|
if recap == "" {
|
|
return "", errors.New("model returned no usable recap")
|
|
}
|
|
return recap, nil
|
|
}
|
|
|
|
// recapContext builds the bounded orientation material used by the idle recap.
|
|
// Goal and plan state are authoritative; recent user/final-assistant rows fill
|
|
// in enough conversational context when neither exists.
|
|
func (m model) recapContext() string {
|
|
var sections []string
|
|
if goal := m.activeSession.Goal; goal != nil && strings.TrimSpace(goal.Objective) != "" {
|
|
sections = append(sections, "Overall goal: "+strings.TrimSpace(goal.Objective)+"\nGoal status: "+string(goal.Status))
|
|
}
|
|
if current := strings.TrimSpace(currentStepContent(m.plan.steps)); current != "" {
|
|
sections = append(sections, "Current task: "+current)
|
|
}
|
|
|
|
rows := make([]string, 0, recapMaxRows)
|
|
used := 0
|
|
for i := len(m.transcript) - 1; i >= 0 && len(rows) < recapMaxRows && used < recapMaxContextChars; i-- {
|
|
row := m.transcript[i]
|
|
role := ""
|
|
switch {
|
|
case row.kind == rowUser:
|
|
role = "User"
|
|
case row.kind == rowAssistant && row.final:
|
|
role = "Assistant"
|
|
default:
|
|
continue
|
|
}
|
|
content := strings.TrimSpace(row.text)
|
|
if content == "" {
|
|
continue
|
|
}
|
|
content = cutRunes(content, recapMaxContextChars-used)
|
|
rows = append(rows, role+": "+content)
|
|
used += len([]rune(content))
|
|
}
|
|
for left, right := 0, len(rows)-1; left < right; left, right = left+1, right-1 {
|
|
rows[left], rows[right] = rows[right], rows[left]
|
|
}
|
|
if len(rows) > 0 {
|
|
sections = append(sections, "Recent conversation:\n"+strings.Join(rows, "\n"))
|
|
}
|
|
return strings.Join(sections, "\n\n")
|
|
}
|
|
|
|
// generateRecapCmd builds the cancellable background command that generates an
|
|
// idle recap off the Update goroutine.
|
|
func (m model) generateRecapCmd(ctx context.Context, cancel context.CancelFunc, runID, seq int) tea.Cmd {
|
|
provider := m.provider
|
|
sessionContext := m.recapContext()
|
|
return func() tea.Msg {
|
|
defer cancel()
|
|
recap, err := generateRecap(ctx, provider, sessionContext)
|
|
return recapGeneratedMsg{runID: runID, seq: seq, recap: recap, err: err}
|
|
}
|
|
}
|
|
|
|
// maybeScheduleIdleRecap arms a one-shot inactivity window after a successful
|
|
// turn. It performs no provider work until that window actually elapses.
|
|
func (m model) maybeScheduleIdleRecap(runID int, answer string) (model, tea.Cmd) {
|
|
if !m.recapsEnabled || m.provider == nil || strings.TrimSpace(answer) == "" {
|
|
return m, nil
|
|
}
|
|
m.recapIdleArmed = true
|
|
m.recapIdleRunID = runID
|
|
return m.scheduleIdleRecap(runID)
|
|
}
|
|
|
|
func (m model) scheduleIdleRecap(runID int) (model, tea.Cmd) {
|
|
m.recapSeq++
|
|
seq := m.recapSeq
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
m.recapTimerCancel = cancel
|
|
return m, func() tea.Msg {
|
|
timer := time.NewTimer(recapIdleDelay)
|
|
defer timer.Stop()
|
|
select {
|
|
case <-timer.C:
|
|
return recapIdleMsg{runID: runID, seq: seq}
|
|
case <-ctx.Done():
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// cancelIdleRecap invalidates pending timers, aborts provider work, and clears
|
|
// the ephemeral orientation note as soon as the user resumes activity.
|
|
func (m model) cancelIdleRecap() model {
|
|
m.recapIdleArmed = false
|
|
m.recapIdleRunID = 0
|
|
return m.clearIdleRecapWork()
|
|
}
|
|
|
|
func (m model) clearIdleRecapWork() model {
|
|
m.recapSeq++
|
|
if m.recapTimerCancel != nil {
|
|
m.recapTimerCancel()
|
|
m.recapTimerCancel = nil
|
|
}
|
|
if m.recapCancel != nil {
|
|
m.recapCancel()
|
|
m.recapCancel = nil
|
|
}
|
|
m.recapRunning = false
|
|
m.idleRecap = ""
|
|
return m
|
|
}
|
|
|
|
// resetIdleRecapAfterActivity restarts the inactivity window for a completed
|
|
// turn. Activity still cancels in-flight generation and clears the ephemeral
|
|
// note, but scrolling or reading no longer disables recaps until another turn.
|
|
func (m model) resetIdleRecapAfterActivity() (model, tea.Cmd) {
|
|
armed := m.recapIdleArmed
|
|
runID := m.recapIdleRunID
|
|
m = m.clearIdleRecapWork()
|
|
if !armed || !m.recapsEnabled || m.provider == nil || runID != m.runID {
|
|
return m, nil
|
|
}
|
|
return m.scheduleIdleRecap(runID)
|
|
}
|
|
|
|
// handleRecapIdle starts generation only if the same run is still genuinely
|
|
// idle. Busy UI, a draft, a modal, or newer activity silently abandons it.
|
|
func (m model) handleRecapIdle(msg recapIdleMsg) (model, tea.Cmd) {
|
|
if msg.seq != m.recapSeq || msg.runID != m.runID || !m.recapsEnabled ||
|
|
m.provider == nil || m.pending || m.compactInFlight || m.recapRunning ||
|
|
strings.TrimSpace(m.composerValue()) != "" || !m.noBlockingModal() {
|
|
return m, nil
|
|
}
|
|
if strings.TrimSpace(m.recapContext()) == "" {
|
|
return m, nil
|
|
}
|
|
m.recapTimerCancel = nil
|
|
ctx, cancel := context.WithTimeout(context.Background(), recapTimeout)
|
|
m.recapCancel = cancel
|
|
m.recapRunning = true
|
|
return m, m.generateRecapCmd(ctx, cancel, msg.runID, msg.seq)
|
|
}
|
|
|
|
// handleRecapGenerated publishes an ephemeral orientation note above the
|
|
// composer. Stale, cancelled, failed, or now-busy results are silent.
|
|
func (m model) handleRecapGenerated(msg recapGeneratedMsg) (model, tea.Cmd) {
|
|
if msg.seq != m.recapSeq || msg.runID != m.runID {
|
|
return m, nil
|
|
}
|
|
m.recapCancel = nil
|
|
m.recapRunning = false
|
|
m.recapIdleArmed = false
|
|
m.recapIdleRunID = 0
|
|
recap := strings.TrimSpace(msg.recap)
|
|
if msg.err != nil || recap == "" || m.pending || m.compactInFlight ||
|
|
strings.TrimSpace(m.composerValue()) != "" || !m.noBlockingModal() {
|
|
return m, nil
|
|
}
|
|
m.idleRecap = recap
|
|
return m, nil
|
|
}
|
|
|
|
// handleConfigCommand applies a "/config <setting> <value>" toggle and returns a
|
|
// status string. Currently only "recaps on|off". A failed persist surfaces an
|
|
// error instead of falsely reporting success.
|
|
func (m model) handleConfigCommand(arg string) (model, string) {
|
|
switch arg {
|
|
case "recaps on", "recaps off":
|
|
m.recapsEnabled = arg == "recaps on"
|
|
if !m.recapsEnabled {
|
|
m = m.cancelIdleRecap()
|
|
}
|
|
if err := m.persistRecapsEnabled(); err != nil {
|
|
return m, "Config\nFailed to save recaps preference: " + err.Error()
|
|
}
|
|
return m, "Config\nrecaps: " + onOff(m.recapsEnabled)
|
|
default:
|
|
return m, "Config\nUnknown setting: " + arg + " (use: recaps on|off)"
|
|
}
|
|
}
|
|
|
|
// persistRecapsEnabled writes the recap preference to the user config, mirroring
|
|
// persistFavoriteModels. A no-op when there is no user config path.
|
|
func (m model) persistRecapsEnabled() error {
|
|
if strings.TrimSpace(m.userConfigPath) == "" {
|
|
return nil
|
|
}
|
|
_, err := config.SetRecapsEnabled(m.userConfigPath, m.recapsEnabled)
|
|
return err
|
|
}
|