Files
DeepSeek-Reasonix/internal/agent/goal_display.go
T
YHH 4fbbca6a99 refactor(go): adopt modern stdlib idioms and gate them (#7831)
* refactor(go): adopt modern stdlib idioms across kernel and desktop

Mechanical sweep with golangci-lint's modernize/intrange/copyloopvar/
usestdlibvars fixers plus hand-fixes for what they could not rewrite:
slices.Contains, min/max, slices.Backward, WaitGroup.Go, reflect.TypeFor,
maps.Copy, strings.Cut/CutPrefix/SplitSeq, strings.Builder, range-over-int.
Net -549 lines with no behavior change.

Three loops in session.go stay index-based: slices.Backward yields element
copies, so the fixer's rewrite silently dropped writes to DecisionReceipts
and ToolCalls (caught by TestDelivery* and TestDependentSameBatch*). All 49
slices.Backward sites were audited for that shape; only these three write
through the index, and they carry a nolint with the reason.

CaptureAfter's scan loop had an empty body and never ran any logic, so the
fixer could not express it. Drop it and the accessor it was the only caller
of. omitzero is deliberately untouched: turning omitempty into omitzero
changes the JSON written for nested structs, which is a wire-format change
to session files and the extension protocol.

* build(lint): gate the modern-idiom linters

Nothing stopped the codebase from drifting back to pre-1.22 idioms. Both
modules are now clean for modernize, intrange, copyloopvar, and
usestdlibvars, so enable them rather than leaving the sweep as a one-off.

omitzero stays disabled with the reason inline; usetesting, perfsprint, and
errorlint still have residue and land separately.
2026-08-07 15:30:33 +08:00

74 lines
2.5 KiB
Go

package agent
import (
"fmt"
"strings"
)
// StripGoalMarkers removes goal status markers like [goal:complete],
// [goal:continue], and [goal:blocked:...] from display text so users see
// natural language instead of protocol markers. Exported for use by frontends.
// The markers are still kept in the session history for controller parsing.
func StripGoalMarkers(text string) string {
text = strings.TrimSpace(text)
lines := strings.Split(text, "\n")
cleaned := make([]string, 0, len(lines))
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed == "[goal:complete]" || trimmed == "[goal:continue]" {
continue
}
// Planner conclusion markers are coordinator protocol, not prose: the
// relay path trusts them (isNoOpPlan / plan approval) but users should
// see the planner's words, not the contract.
if lower := strings.ToLower(trimmed); lower == "[no_changes]" || lower == "[planner_requires_approval]" {
continue
}
if strings.HasPrefix(trimmed, "[goal:blocked:") && strings.HasSuffix(trimmed, "]") {
reason := strings.TrimPrefix(trimmed, "[goal:blocked:")
reason = strings.TrimSuffix(reason, "]")
if reason != "" {
cleaned = append(cleaned, fmt.Sprintf("\u26a0\ufe0f Blocked: %s", reason))
}
continue
}
cleaned = append(cleaned, line)
}
return strings.TrimSpace(strings.Join(cleaned, "\n"))
}
const (
autoResearchEvidenceOpen = "<autoresearch-evidence>"
autoResearchEvidenceClose = "</autoresearch-evidence>"
)
// StripAutoResearchEvidenceBlocks removes <autoresearch-evidence> protocol
// blocks the model emits for the controller's evidence recorder. Like goal
// markers, they stay in the session history for parsing (#6665).
func StripAutoResearchEvidenceBlocks(text string) string {
var b strings.Builder
rest := text
for {
start := strings.Index(rest, autoResearchEvidenceOpen)
if start < 0 {
b.WriteString(rest)
return strings.TrimSpace(b.String())
}
b.WriteString(rest[:start])
afterOpen := rest[start+len(autoResearchEvidenceOpen):]
_, after, ok := strings.Cut(afterOpen, autoResearchEvidenceClose)
if !ok {
return strings.TrimSpace(b.String())
}
rest = after
}
}
// DisplayAssistantText is the single display filter for assistant answer text:
// it removes every protocol artifact ([goal:*] markers, autoresearch evidence
// blocks) before the text reaches a sink. Session history keeps the raw text —
// apply this at emission or render boundaries only.
func DisplayAssistantText(text string) string {
return StripGoalMarkers(StripAutoResearchEvidenceBlocks(text))
}