mirror of
https://github.com/Twigpine/zero.git
synced 2026-11-09 05:19:33 -10:00
* chore: remove code unreachable on every supported platform The Security and code health job's advisory deadcode step reports 152 unreachable functions on main. Running the same check under GOOS=linux, windows, and darwin shows 13 of those are false positives whose callers live in windows build-tagged files, so only functions unreachable on all three platforms are touched. Functions with no references anywhere are deleted, including the internal/reasoning package, which nothing imports. Functions that only tests reference move into per-package export_test.go files so tests keep their seams while the production binary sheds the code. Test call sites of deleted convenience wrappers are ported to the live APIs (scoped tool constructors, tools.DeferredSource, FinishReason checks). Kept despite the report: cron.Store.Runs (only reader of runs.jsonl, which production writes), and agenteval.TraceEventKeys and MissingTraceEvents (used across packages by the trace contract test). * docs(config): correct ValidateFile's errors.As claim Message is a flattened string (.Error() already called on the parse error), not a wrapped error, so callers can't recover the concrete json error type via errors.As. Fix the comment to match. Co-Authored-By: Claude Sonnet 5 <[email protected]> * chore(trace,skills): drop obsolete Sink adapters and fix stale doc references The dead-code pass removed the Emit methods but left Sink, NDJSONSink, and TextSink exported with a contract nothing implements; remove the types with their contract. Also remove the stray blank line at EOF of trace.go flagged by gofmt, and update skills package docs and HOW_ZERO_WORKS.md to stop directing callers to DiscoveryRoots/Duplicates, which are now test seams. Co-Authored-By: Claude Fable 5 <[email protected]> * chore: clean up remaining review nits on dead code removal Replace a pointless fmt.Errorf(...%w...).Error() with fmt.Sprintf using %v, since the wrapped error was discarded immediately anyway. Remove a comment block that was duplicated verbatim. * docs(skills): drop remaining references to test-only List/Get Load and MergedSkills comments still named List/Get as production entry points after they moved to export_test.go. Point callers at Load and ListFromRoots instead. * chore: remove now-unreferenced export_test.go seams execSessionManager.len() and likelySandboxDenied/sandboxDenialKind had no remaining callers after the sandbox unify-command-execution rebase; the production code they wrapped for tests no longer exists. --------- Co-authored-by: Claude Sonnet 5 <[email protected]>
193 lines
5.3 KiB
Go
193 lines
5.3 KiB
Go
package tui
|
|
|
|
import "strings"
|
|
|
|
// streamingDecoder incrementally decodes a tool call's streaming JSON arguments
|
|
// for the live "writing" preview. Each delta is processed exactly once, so the
|
|
// preview costs O(fragment) per delta instead of re-scanning the whole growing
|
|
// args buffer on every render (the previous O(n²) behavior — M14/L6/L7).
|
|
//
|
|
// It buffers raw args until the content value starts (extracting the file path
|
|
// from the head), then decodes the content's JSON-string escapes as fragments
|
|
// arrive, keeping only a bounded tail of lines plus a running line count.
|
|
type streamingDecoder struct {
|
|
pathKeys []string
|
|
contentKeys []string
|
|
tailCap int
|
|
|
|
rawLen int // total raw arg bytes seen (for the "receiving N KB" indicator)
|
|
head []byte // raw args before the content value; dropped once content starts
|
|
pathScan []byte // bounded post-content args used only to recover a late path
|
|
path string
|
|
pathDone bool
|
|
|
|
inContent bool
|
|
closed bool // the content value's closing quote was seen
|
|
|
|
tail []string // last tailCap completed content lines
|
|
cur []byte // the in-progress (incomplete) last line
|
|
lineCount int // completed content lines (decoded newlines)
|
|
pendingEsc bool // the previous byte was a lone backslash
|
|
uSkip int // remaining hex digits to skip after a \uXXXX escape
|
|
}
|
|
|
|
func newStreamingDecoder() *streamingDecoder {
|
|
return &streamingDecoder{
|
|
pathKeys: []string{"path", "file", "file_path", "filepath", "filename"},
|
|
contentKeys: []string{"content", "new_string", "new_str", "new_text", "new", "replacement", "patch", "input"},
|
|
tailCap: streamingTailLines,
|
|
}
|
|
}
|
|
|
|
// feed consumes one streamed argument fragment.
|
|
func (d *streamingDecoder) feed(fragment string) {
|
|
d.rawLen += len(fragment)
|
|
if d.closed {
|
|
d.scanPathFragment(fragment)
|
|
return
|
|
}
|
|
if d.inContent {
|
|
if remainder := d.consume(fragment); remainder != "" {
|
|
d.scanPathFragment(remainder)
|
|
}
|
|
return
|
|
}
|
|
d.head = append(d.head, fragment...)
|
|
if !d.pathDone {
|
|
// Best effort: the path completes quickly (it precedes the content), and a
|
|
// partial value just updates until it's whole.
|
|
d.setPathFrom(string(d.head))
|
|
}
|
|
start := d.contentValueStart()
|
|
if start < 0 {
|
|
return // content value hasn't begun yet
|
|
}
|
|
d.inContent = true
|
|
rest := string(d.head[start:])
|
|
d.head = nil
|
|
if remainder := d.consume(rest); remainder != "" {
|
|
d.scanPathFragment(remainder)
|
|
}
|
|
}
|
|
|
|
func (d *streamingDecoder) setPathFrom(args string) {
|
|
if d.pathDone {
|
|
return
|
|
}
|
|
if v := streamingFilePath(args); v != "" {
|
|
d.path = v
|
|
d.pathDone = true
|
|
}
|
|
}
|
|
|
|
const maxStreamingPathScanBytes = 8192
|
|
|
|
func (d *streamingDecoder) scanPathFragment(fragment string) {
|
|
if d.pathDone || fragment == "" {
|
|
return
|
|
}
|
|
d.pathScan = append(d.pathScan, fragment...)
|
|
if len(d.pathScan) > maxStreamingPathScanBytes {
|
|
d.pathScan = d.pathScan[len(d.pathScan)-maxStreamingPathScanBytes:]
|
|
}
|
|
d.setPathFrom(string(d.pathScan))
|
|
}
|
|
|
|
// contentValueStart returns the index in head just past the opening quote of the
|
|
// first content value, or -1 if no content value has started yet.
|
|
func (d *streamingDecoder) contentValueStart() int {
|
|
s := string(d.head)
|
|
best := -1
|
|
for _, key := range d.contentKeys {
|
|
if idx := jsonStringValueStart(s, key); idx >= 0 && (best < 0 || idx < best) {
|
|
best = idx
|
|
}
|
|
}
|
|
return best
|
|
}
|
|
|
|
// consume decodes content bytes, splitting on decoded newlines into tail lines,
|
|
// stopping at the value's closing quote, and returning any bytes after it.
|
|
func (d *streamingDecoder) consume(b string) string {
|
|
for i := 0; i < len(b); i++ {
|
|
c := b[i]
|
|
if d.uSkip > 0 {
|
|
d.uSkip-- // skip the 4 hex digits of a \uXXXX escape (not decoded for a preview)
|
|
continue
|
|
}
|
|
if d.pendingEsc {
|
|
d.pendingEsc = false
|
|
switch c {
|
|
case 'n':
|
|
d.newline()
|
|
case 't':
|
|
d.cur = append(d.cur, '\t')
|
|
case 'r':
|
|
// drop carriage returns from the preview
|
|
case 'u':
|
|
d.uSkip = 4
|
|
default: // " \ / and anything else: take the literal char
|
|
d.cur = append(d.cur, c)
|
|
}
|
|
continue
|
|
}
|
|
switch c {
|
|
case '\\':
|
|
d.pendingEsc = true
|
|
case '"':
|
|
d.closed = true // closing quote of the content value
|
|
return b[i+1:]
|
|
default:
|
|
d.cur = append(d.cur, c)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (d *streamingDecoder) newline() {
|
|
d.tail = append(d.tail, string(d.cur))
|
|
if len(d.tail) > d.tailCap {
|
|
d.tail = d.tail[len(d.tail)-d.tailCap:]
|
|
}
|
|
d.cur = d.cur[:0]
|
|
d.lineCount++
|
|
}
|
|
|
|
// hasContent reports whether the content value has begun streaming.
|
|
func (d *streamingDecoder) hasContent() bool { return d.inContent }
|
|
|
|
// lineTotal is the line count shown in the header (completed lines plus the
|
|
// in-progress one).
|
|
func (d *streamingDecoder) lineTotal() int {
|
|
if !d.inContent {
|
|
return 0
|
|
}
|
|
n := d.lineCount
|
|
if len(d.cur) > 0 || !d.closed {
|
|
n++ // the current (possibly empty, still-open) line
|
|
}
|
|
if n < 1 {
|
|
n = 1
|
|
}
|
|
return n
|
|
}
|
|
|
|
// jsonStringValueStart finds `"key"` then, tolerating whitespace around the colon,
|
|
// returns the index just past the opening quote of its string value, or -1.
|
|
func jsonStringValueStart(s, key string) int {
|
|
marker := `"` + key + `"`
|
|
idx := strings.Index(s, marker)
|
|
if idx < 0 {
|
|
return -1
|
|
}
|
|
i := skipJSONSpace(s, idx+len(marker))
|
|
if i >= len(s) || s[i] != ':' {
|
|
return -1
|
|
}
|
|
i = skipJSONSpace(s, i+1)
|
|
if i >= len(s) || s[i] != '"' {
|
|
return -1
|
|
}
|
|
return i + 1
|
|
}
|