fix: Phase 4 structural refactor — reduce lint findings from 28 to 5

cyclop (5→0): extract subcommand/case bodies in codesearch,
subprocess, Telegram, indexing
funlen (5→3): extract memory action handlers, tool provider startup
gocognit (7→2): extract config finalize/loadDirInto helpers, release
announcer, capture tool validation, Telegram gateway (200→108),
archie-agent main loop (35→0)
nestif (8→0): extract identity validation, container validation,
conf.d scanning, email multipart parsing, Telegram model/update
parsing, build-no-changes closure
interfacebloat (3→0): split Forge/IssueForge/PRForge/RepoForge,
SessionStore/SessionLifecycle/MessageHistory,
TaskStore/TaskLifecycle/TaskQueries/TaskEvents

Remaining: daemon process, workflow Feasibility/TDD/AgentStage,
archied main.go run (108→30 remaining). These require deeper
refactoring best done per-function in follow-up beads.

Updated contract tests and archied wiring tests to check both
main.go and the new telegram_setup.go.
This commit is contained in:
sam committed 2026-07-29 10:15:22 +10:00
1 parent 676ad3fe48
commit 712388dc6c
24 files changed
+1090 -886

No files matched your search

+49 -32
View File
@@ -161,42 +161,59 @@ func captureToolSet(specs []CaptureTool, captures map[string][]json.RawMessage)
tools := make(core.ToolSet, len(specs))
for _, spec := range specs {
tools[spec.Name] = core.NewTool(spec.Name, spec.Description, spec.Parameters,
func(_ context.Context, input string) (string, error) {
if spec.MaxCalls > 0 && len(captures[spec.Name]) >= spec.MaxCalls {
return fmt.Sprintf("%s rejected: maximum call count is %d", spec.Name, spec.MaxCalls), nil
}
value := json.RawMessage(input)
if !json.Valid(value) {
return spec.Name + " rejected: arguments are not valid JSON", nil
}
var object map[string]json.RawMessage
if err := json.Unmarshal(value, &object); err != nil {
return spec.Name + " rejected: arguments must be a JSON object", nil //nolint:nilerr // the agent loop must see malformed tool arguments as feedback it can correct, not as a failed tool call
}
for _, field := range spec.RequiredFields {
if _, ok := object[field]; !ok {
return fmt.Sprintf("%s rejected: %s is required", spec.Name, field), nil
}
}
for _, field := range spec.NonEmptyStrings {
var text string
if raw, ok := object[field]; !ok || json.Unmarshal(raw, &text) != nil || strings.TrimSpace(text) == "" {
return fmt.Sprintf("%s rejected: %s must be a non-empty string", spec.Name, field), nil //nolint:nilerr // same: a rejection message lets the model retry with a valid argument
}
}
for _, field := range spec.BooleanFields {
var value bool
if raw, ok := object[field]; !ok || json.Unmarshal(raw, &value) != nil {
return fmt.Sprintf("%s rejected: %s must be a boolean", spec.Name, field), nil //nolint:nilerr // same: a rejection message lets the model retry with a valid argument
}
}
captures[spec.Name] = append(captures[spec.Name], append(json.RawMessage(nil), value...))
return spec.Name + " recorded", nil
})
makeCaptureHandler(spec, captures))
}
return tools
}
// makeCaptureHandler builds a tool handler that validates and records
// capture-tool invocations.
func makeCaptureHandler(spec CaptureTool, captures map[string][]json.RawMessage) func(context.Context, string) (string, error) {
return func(_ context.Context, input string) (string, error) {
if spec.MaxCalls > 0 && len(captures[spec.Name]) >= spec.MaxCalls {
return fmt.Sprintf("%s rejected: maximum call count is %d", spec.Name, spec.MaxCalls), nil
}
value := json.RawMessage(input)
if !json.Valid(value) {
return spec.Name + " rejected: arguments are not valid JSON", nil
}
rejection, ok := validateCaptureArgs(spec, value)
if !ok {
return rejection, nil
}
captures[spec.Name] = append(captures[spec.Name], append(json.RawMessage(nil), value...))
return spec.Name + " recorded", nil
}
}
// validateCaptureArgs checks that the capture tool arguments satisfy all
// constraints. Returns a rejection message and false on failure, or "" and
// true on success.
func validateCaptureArgs(spec CaptureTool, value json.RawMessage) (string, bool) {
var object map[string]json.RawMessage
if err := json.Unmarshal(value, &object); err != nil {
return spec.Name + " rejected: arguments must be a JSON object", false //nolint:nilerr // the agent loop must see malformed tool arguments as feedback it can correct, not as a failed tool call
}
for _, field := range spec.RequiredFields {
if _, ok := object[field]; !ok {
return fmt.Sprintf("%s rejected: %s is required", spec.Name, field), false
}
}
for _, field := range spec.NonEmptyStrings {
var text string
if raw, ok := object[field]; !ok || json.Unmarshal(raw, &text) != nil || strings.TrimSpace(text) == "" {
return fmt.Sprintf("%s rejected: %s must be a non-empty string", spec.Name, field), false //nolint:nilerr // same: a rejection message lets the model retry with a valid argument
}
}
for _, field := range spec.BooleanFields {
var val bool
if raw, ok := object[field]; !ok || json.Unmarshal(raw, &val) != nil {
return fmt.Sprintf("%s rejected: %s must be a boolean", spec.Name, field), false //nolint:nilerr // same: a rejection message lets the model retry with a valid argument
}
}
return "", true
}
// scriptToolSet exposes run_go_script: interpreting a Yaegi Go script
// bundled with a skill (.agents/skills/<skill>/scripts/*.go) or living
// anywhere else in the workspace, and returning what it printed. This is
+8 -2
View File
@@ -72,9 +72,15 @@ func (r *SubprocessRunner) Run(ctx context.Context, workspace string, req Reques
if stdout.truncated {
return Result{}, fmt.Errorf("archie-agent response exceeds %d bytes", maxProtocolBytes)
}
return decodeSubprocessResponse(bytes.NewReader(stdout.Bytes()), stderr.String(), req)
}
// decodeSubprocessResponse reads one Response from r and validates it
// against req, returning the Result or an error.
func decodeSubprocessResponse(r io.Reader, stderrText string, req Request) (Result, error) {
var response Response
if err := decodeOne(bytes.NewReader(stdout.Bytes()), &response); err != nil {
return Result{}, fmt.Errorf("decode archie-agent response: %w: %s", err, strings.TrimSpace(stderr.String()))
if err := decodeOne(r, &response); err != nil {
return Result{}, fmt.Errorf("decode archie-agent response: %w: %s", err, strings.TrimSpace(stderrText))
}
if response.Version != ProtocolVersion {
return Result{}, fmt.Errorf("archie-agent response protocol version %d is unsupported", response.Version)