2026-01-30 09:19:20 +00:00
|
|
|
package agentic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
2026-01-30 10:18:54 +00:00
|
|
|
"strings"
|
2026-01-30 09:19:20 +00:00
|
|
|
|
|
|
|
|
"github.com/host-uk/core/pkg/framework"
|
Log all errors at handling point with contextual information (#321)
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
- Fixed formatting in `pkg/mcp/mcp.go` and `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/cli/runtime.go`.
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
- Fixed formatting in `pkg/mcp/mcp.go` and `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/cli/runtime.go`.
- Fixed CI failure in `auto-merge` workflow by providing explicit
repository context to the GitHub CLI.
* feat(log): address PR feedback and improve error context extraction
Addressed feedback from PR review:
- Improved `Fatalf` and other fatal functions in `pkg/cli/errors.go` to
use structured logging for the formatted message.
- Added direct unit tests for `StackTrace` and `FormatStackTrace` in
`pkg/log/errors_test.go`, covering edge cases like plain errors,
nil errors, and mixed error chains.
- Optimized the automatic context extraction loop in `pkg/log/log.go`
by capturing the original length of keyvals.
- Fixed a bug in `StackTrace` where operations were duplicated when
the error chain included non-`*log.Err` errors.
- Fixed formatting and unused imports from previous commits.
* fix: address code review comments
- Simplify Fatalf logging by removing redundant format parameter
(the formatted message is already logged as "msg")
- Tests for StackTrace/FormatStackTrace edge cases already exist
- Loop optimization in pkg/log/log.go already implemented
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude <developers@lethean.io>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 07:52:25 +00:00
|
|
|
"github.com/host-uk/core/pkg/log"
|
2026-01-30 09:19:20 +00:00
|
|
|
)
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
// Tasks for AI service
|
2026-01-30 09:19:20 +00:00
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
// TaskCommit requests Claude to create a commit.
|
|
|
|
|
type TaskCommit struct {
|
2026-01-30 09:19:20 +00:00
|
|
|
Path string
|
|
|
|
|
Name string
|
|
|
|
|
CanEdit bool // allow Write/Edit tools
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
// TaskPrompt sends a custom prompt to Claude.
|
|
|
|
|
type TaskPrompt struct {
|
2026-01-30 09:19:20 +00:00
|
|
|
Prompt string
|
|
|
|
|
WorkDir string
|
|
|
|
|
AllowedTools []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ServiceOptions for configuring the AI service.
|
|
|
|
|
type ServiceOptions struct {
|
|
|
|
|
DefaultTools []string
|
2026-01-30 10:18:54 +00:00
|
|
|
AllowEdit bool // global permission for Write/Edit tools
|
2026-01-30 09:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DefaultServiceOptions returns sensible defaults.
|
|
|
|
|
func DefaultServiceOptions() ServiceOptions {
|
|
|
|
|
return ServiceOptions{
|
|
|
|
|
DefaultTools: []string{"Bash", "Read", "Glob", "Grep"},
|
2026-01-30 10:18:54 +00:00
|
|
|
AllowEdit: false,
|
2026-01-30 09:19:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Service provides AI/Claude operations as a Core service.
|
|
|
|
|
type Service struct {
|
|
|
|
|
*framework.ServiceRuntime[ServiceOptions]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewService creates an AI service factory.
|
|
|
|
|
func NewService(opts ServiceOptions) func(*framework.Core) (any, error) {
|
|
|
|
|
return func(c *framework.Core) (any, error) {
|
|
|
|
|
return &Service{
|
|
|
|
|
ServiceRuntime: framework.NewServiceRuntime(c, opts),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
// OnStartup registers task handlers.
|
2026-01-30 09:19:20 +00:00
|
|
|
func (s *Service) OnStartup(ctx context.Context) error {
|
2026-01-30 10:18:54 +00:00
|
|
|
s.Core().RegisterTask(s.handleTask)
|
2026-01-30 09:19:20 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
func (s *Service) handleTask(c *framework.Core, t framework.Task) (any, bool, error) {
|
|
|
|
|
switch m := t.(type) {
|
|
|
|
|
case TaskCommit:
|
|
|
|
|
err := s.doCommit(m)
|
Log all errors at handling point with contextual information (#321)
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
- Fixed formatting in `pkg/mcp/mcp.go` and `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/cli/runtime.go`.
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
- Fixed formatting in `pkg/mcp/mcp.go` and `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/cli/runtime.go`.
- Fixed CI failure in `auto-merge` workflow by providing explicit
repository context to the GitHub CLI.
* feat(log): address PR feedback and improve error context extraction
Addressed feedback from PR review:
- Improved `Fatalf` and other fatal functions in `pkg/cli/errors.go` to
use structured logging for the formatted message.
- Added direct unit tests for `StackTrace` and `FormatStackTrace` in
`pkg/log/errors_test.go`, covering edge cases like plain errors,
nil errors, and mixed error chains.
- Optimized the automatic context extraction loop in `pkg/log/log.go`
by capturing the original length of keyvals.
- Fixed a bug in `StackTrace` where operations were duplicated when
the error chain included non-`*log.Err` errors.
- Fixed formatting and unused imports from previous commits.
* fix: address code review comments
- Simplify Fatalf logging by removing redundant format parameter
(the formatted message is already logged as "msg")
- Tests for StackTrace/FormatStackTrace edge cases already exist
- Loop optimization in pkg/log/log.go already implemented
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude <developers@lethean.io>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 07:52:25 +00:00
|
|
|
if err != nil {
|
|
|
|
|
log.Error("agentic: commit task failed", "err", err, "path", m.Path)
|
|
|
|
|
}
|
2026-01-30 10:18:54 +00:00
|
|
|
return nil, true, err
|
|
|
|
|
|
|
|
|
|
case TaskPrompt:
|
|
|
|
|
err := s.doPrompt(m)
|
Log all errors at handling point with contextual information (#321)
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
- Fixed formatting in `pkg/mcp/mcp.go` and `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/cli/runtime.go`.
* feat(log): log all errors at handling point with context
This change ensures all errors are logged at the point where they are
handled, including contextual information such as operations and
logical stack traces.
Key changes:
- Added `StackTrace` and `FormatStackTrace` to `pkg/log/errors.go`.
- Enhanced `Logger.log` in `pkg/log/log.go` to automatically extract
and log `op` and `stack` keys when an error is passed in keyvals.
- Updated CLI logging and output helpers to support structured logging.
- Updated CLI fatal error handlers to log errors before exiting.
- Audited and updated error logging in MCP service (tool handlers and
TCP transport), CLI background services (signal and health), and
Agentic task handlers.
- Fixed formatting in `pkg/mcp/mcp.go` and `pkg/io/local/client.go`.
- Removed unused `fmt` import in `pkg/cli/runtime.go`.
- Fixed CI failure in `auto-merge` workflow by providing explicit
repository context to the GitHub CLI.
* feat(log): address PR feedback and improve error context extraction
Addressed feedback from PR review:
- Improved `Fatalf` and other fatal functions in `pkg/cli/errors.go` to
use structured logging for the formatted message.
- Added direct unit tests for `StackTrace` and `FormatStackTrace` in
`pkg/log/errors_test.go`, covering edge cases like plain errors,
nil errors, and mixed error chains.
- Optimized the automatic context extraction loop in `pkg/log/log.go`
by capturing the original length of keyvals.
- Fixed a bug in `StackTrace` where operations were duplicated when
the error chain included non-`*log.Err` errors.
- Fixed formatting and unused imports from previous commits.
* fix: address code review comments
- Simplify Fatalf logging by removing redundant format parameter
(the formatted message is already logged as "msg")
- Tests for StackTrace/FormatStackTrace edge cases already exist
- Loop optimization in pkg/log/log.go already implemented
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude <developers@lethean.io>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 07:52:25 +00:00
|
|
|
if err != nil {
|
|
|
|
|
log.Error("agentic: prompt task failed", "err", err)
|
|
|
|
|
}
|
2026-01-30 10:18:54 +00:00
|
|
|
return nil, true, err
|
2026-01-30 09:19:20 +00:00
|
|
|
}
|
2026-01-30 10:18:54 +00:00
|
|
|
return nil, false, nil
|
2026-01-30 09:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
func (s *Service) doCommit(task TaskCommit) error {
|
2026-01-30 09:19:20 +00:00
|
|
|
prompt := Prompt("commit")
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
tools := []string{"Bash", "Read", "Glob", "Grep"}
|
|
|
|
|
if task.CanEdit {
|
|
|
|
|
tools = []string{"Bash", "Read", "Write", "Edit", "Glob", "Grep"}
|
2026-01-30 09:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
cmd := exec.CommandContext(context.Background(), "claude", "-p", prompt, "--allowedTools", strings.Join(tools, ","))
|
|
|
|
|
cmd.Dir = task.Path
|
2026-01-30 09:19:20 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
func (s *Service) doPrompt(task TaskPrompt) error {
|
|
|
|
|
opts := s.Opts()
|
|
|
|
|
tools := opts.DefaultTools
|
|
|
|
|
if len(tools) == 0 {
|
|
|
|
|
tools = []string{"Bash", "Read", "Glob", "Grep"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(task.AllowedTools) > 0 {
|
|
|
|
|
tools = task.AllowedTools
|
2026-01-30 09:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 10:18:54 +00:00
|
|
|
cmd := exec.CommandContext(context.Background(), "claude", "-p", task.Prompt, "--allowedTools", strings.Join(tools, ","))
|
|
|
|
|
if task.WorkDir != "" {
|
|
|
|
|
cmd.Dir = task.WorkDir
|
2026-01-30 09:19:20 +00:00
|
|
|
}
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
|
}
|