2026-01-30 10:18:54 +00:00
|
|
|
package dev
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-02-16 00:30:41 +00:00
|
|
|
"forge.lthn.ai/core/cli/pkg/agentic"
|
|
|
|
|
"forge.lthn.ai/core/cli/pkg/cli"
|
|
|
|
|
"forge.lthn.ai/core/cli/pkg/framework"
|
|
|
|
|
"forge.lthn.ai/core/cli/pkg/git"
|
2026-01-30 10:18:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Tasks for dev service
|
|
|
|
|
|
|
|
|
|
// TaskWork runs the full dev workflow: status, commit, push.
|
|
|
|
|
type TaskWork struct {
|
|
|
|
|
RegistryPath string
|
|
|
|
|
StatusOnly bool
|
|
|
|
|
AutoCommit bool
|
Implement Background Goroutines for Long-Running Operations (#309)
* feat: implement background goroutines for long-running operations
Introduced `PerformAsync` in the Core framework to support non-blocking
execution of long-running tasks. This mechanism uses the IPC system to
broadcast `ActionTaskStarted` and `ActionTaskCompleted` events, ensuring
the frontend remains responsive and informed.
- Added `PerformAsync(Task) string` to `Core`.
- Defined framework-level lifecycle actions: `ActionTaskStarted`,
`ActionTaskProgress`, and `ActionTaskCompleted`.
- Updated `internal/cmd/dev/service.go` to support `AutoPush` in
`TaskWork`, removing interactive prompts during background execution.
- Added comprehensive documentation for the background operations pattern
in `docs/pkg/PACKAGE_STANDARDS.md`.
- Added unit tests for the async task mechanism in `pkg/framework/core/ipc_test.go`.
* feat: implement background goroutines for long-running operations
Introduced `PerformAsync` in the Core framework to support non-blocking
execution of long-running tasks. This mechanism uses the IPC system to
broadcast `ActionTaskStarted` and `ActionTaskCompleted` events, ensuring
the frontend remains responsive and informed.
- Added `PerformAsync(Task) string` to `Core`.
- Defined framework-level lifecycle actions: `ActionTaskStarted`,
`ActionTaskProgress`, and `ActionTaskCompleted`.
- Updated `internal/cmd/dev/service.go` to support `AutoPush` in
`TaskWork`, removing interactive prompts during background execution.
- Added comprehensive documentation for the background operations pattern
in `docs/pkg/PACKAGE_STANDARDS.md`.
- Added unit tests for the async task mechanism in `pkg/framework/core/ipc_test.go`.
- Fixed formatting in `pkg/io/local/client.go`.
* feat: implement background goroutines with progress reporting
This version addresses feedback by providing a more complete implementation
of the background task mechanism, including progress reporting and
demonstrating actual usage in the AI service.
- Added `TaskWithID` interface to support task ID injection.
- Updated `PerformAsync` to inject IDs and provided `Core.Progress` helper.
- Applied background processing pattern to `TaskPrompt` in `agentic` service.
- Included a fix for the `auto-merge` CI failure by providing explicit repo
context to the `gh` command in a local workflow implementation.
- Fixed formatting in `pkg/io/local/client.go` and `pkg/agentic/service.go`.
- Updated documentation with the new progress reporting pattern.
* feat: implement non-blocking background tasks with progress reporting
This submission provides a complete framework-level solution for running
long-running operations in the background to prevent UI blocking,
addressing previous review feedback.
Key changes:
- Introduced `PerformAsync(Task) string` in the `Core` framework.
- Added `TaskWithID` interface to allow tasks to receive their unique ID.
- Provided `Core.Progress` helper for services to report granular updates.
- Applied the background pattern to the AI service (`agentic.TaskPrompt`).
- Updated the dev service (`TaskWork`) to support an `AutoPush` flag,
eliminating interactive prompts during background execution.
- Added a local implementation for the `auto-merge` CI workflow to
bypass repo context issues and fix the blocking CI failure.
- Included comprehensive documentation in `docs/pkg/PACKAGE_STANDARDS.md`.
- Resolved formatting discrepancies across the codebase.
- Verified functionality with unit tests in `pkg/framework/core/ipc_test.go`.
---------
Co-authored-by: Claude <developers@lethean.io>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 10:26:45 +00:00
|
|
|
AutoPush bool
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TaskStatus displays git status for all repos.
|
|
|
|
|
type TaskStatus struct {
|
|
|
|
|
RegistryPath string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ServiceOptions for configuring the dev service.
|
|
|
|
|
type ServiceOptions struct {
|
|
|
|
|
RegistryPath string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Service provides dev workflow orchestration as a Core service.
|
|
|
|
|
type Service struct {
|
|
|
|
|
*framework.ServiceRuntime[ServiceOptions]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewService creates a dev 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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OnStartup registers task handlers.
|
|
|
|
|
func (s *Service) OnStartup(ctx context.Context) error {
|
|
|
|
|
s.Core().RegisterTask(s.handleTask)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) handleTask(c *framework.Core, t framework.Task) (any, bool, error) {
|
|
|
|
|
switch m := t.(type) {
|
|
|
|
|
case TaskWork:
|
|
|
|
|
err := s.runWork(m)
|
|
|
|
|
return nil, true, err
|
|
|
|
|
|
|
|
|
|
case TaskStatus:
|
|
|
|
|
err := s.runStatus(m)
|
|
|
|
|
return nil, true, err
|
|
|
|
|
}
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) runWork(task TaskWork) error {
|
|
|
|
|
// Load registry
|
|
|
|
|
paths, names, err := s.loadRegistry(task.RegistryPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(paths) == 0 {
|
2026-01-31 23:36:43 +00:00
|
|
|
cli.Println("No git repositories found")
|
2026-01-30 10:18:54 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QUERY git status
|
|
|
|
|
result, handled, err := s.Core().QUERY(git.QueryStatus{
|
|
|
|
|
Paths: paths,
|
|
|
|
|
Names: names,
|
|
|
|
|
})
|
|
|
|
|
if !handled {
|
2026-01-31 11:39:19 +00:00
|
|
|
return cli.Err("git service not available")
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
statuses := result.([]git.RepoStatus)
|
|
|
|
|
|
|
|
|
|
// Sort by name
|
|
|
|
|
sort.Slice(statuses, func(i, j int) bool {
|
|
|
|
|
return statuses[i].Name < statuses[j].Name
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Display status table
|
|
|
|
|
s.printStatusTable(statuses)
|
|
|
|
|
|
|
|
|
|
// Collect dirty and ahead repos
|
|
|
|
|
var dirtyRepos []git.RepoStatus
|
|
|
|
|
var aheadRepos []git.RepoStatus
|
|
|
|
|
|
|
|
|
|
for _, st := range statuses {
|
|
|
|
|
if st.Error != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if st.IsDirty() {
|
|
|
|
|
dirtyRepos = append(dirtyRepos, st)
|
|
|
|
|
}
|
|
|
|
|
if st.HasUnpushed() {
|
|
|
|
|
aheadRepos = append(aheadRepos, st)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Auto-commit dirty repos if requested
|
|
|
|
|
if task.AutoCommit && len(dirtyRepos) > 0 {
|
2026-01-31 23:36:43 +00:00
|
|
|
cli.Blank()
|
|
|
|
|
cli.Println("Committing changes...")
|
|
|
|
|
cli.Blank()
|
2026-01-30 10:18:54 +00:00
|
|
|
|
|
|
|
|
for _, repo := range dirtyRepos {
|
|
|
|
|
_, handled, err := s.Core().PERFORM(agentic.TaskCommit{
|
|
|
|
|
Path: repo.Path,
|
|
|
|
|
Name: repo.Name,
|
|
|
|
|
})
|
|
|
|
|
if !handled {
|
|
|
|
|
// Agentic service not available - skip silently
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print(" - %s: agentic service not available\n", repo.Name)
|
2026-01-30 10:18:54 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print(" x %s: %s\n", repo.Name, err)
|
2026-01-30 10:18:54 +00:00
|
|
|
} else {
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print(" v %s\n", repo.Name)
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Re-query status after commits
|
|
|
|
|
result, _, _ = s.Core().QUERY(git.QueryStatus{
|
|
|
|
|
Paths: paths,
|
|
|
|
|
Names: names,
|
|
|
|
|
})
|
|
|
|
|
statuses = result.([]git.RepoStatus)
|
|
|
|
|
|
|
|
|
|
// Rebuild ahead repos list
|
|
|
|
|
aheadRepos = nil
|
|
|
|
|
for _, st := range statuses {
|
|
|
|
|
if st.Error == nil && st.HasUnpushed() {
|
|
|
|
|
aheadRepos = append(aheadRepos, st)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If status only, we're done
|
|
|
|
|
if task.StatusOnly {
|
|
|
|
|
if len(dirtyRepos) > 0 && !task.AutoCommit {
|
2026-01-31 23:36:43 +00:00
|
|
|
cli.Blank()
|
|
|
|
|
cli.Println("Use --commit flag to auto-commit dirty repos")
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push repos with unpushed commits
|
|
|
|
|
if len(aheadRepos) == 0 {
|
2026-01-31 23:36:43 +00:00
|
|
|
cli.Blank()
|
|
|
|
|
cli.Println("All repositories are up to date")
|
2026-01-30 10:18:54 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-31 23:36:43 +00:00
|
|
|
cli.Blank()
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print("%d repos with unpushed commits:\n", len(aheadRepos))
|
2026-01-30 10:18:54 +00:00
|
|
|
for _, st := range aheadRepos {
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print(" %s: %d commits\n", st.Name, st.Ahead)
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
Implement Background Goroutines for Long-Running Operations (#309)
* feat: implement background goroutines for long-running operations
Introduced `PerformAsync` in the Core framework to support non-blocking
execution of long-running tasks. This mechanism uses the IPC system to
broadcast `ActionTaskStarted` and `ActionTaskCompleted` events, ensuring
the frontend remains responsive and informed.
- Added `PerformAsync(Task) string` to `Core`.
- Defined framework-level lifecycle actions: `ActionTaskStarted`,
`ActionTaskProgress`, and `ActionTaskCompleted`.
- Updated `internal/cmd/dev/service.go` to support `AutoPush` in
`TaskWork`, removing interactive prompts during background execution.
- Added comprehensive documentation for the background operations pattern
in `docs/pkg/PACKAGE_STANDARDS.md`.
- Added unit tests for the async task mechanism in `pkg/framework/core/ipc_test.go`.
* feat: implement background goroutines for long-running operations
Introduced `PerformAsync` in the Core framework to support non-blocking
execution of long-running tasks. This mechanism uses the IPC system to
broadcast `ActionTaskStarted` and `ActionTaskCompleted` events, ensuring
the frontend remains responsive and informed.
- Added `PerformAsync(Task) string` to `Core`.
- Defined framework-level lifecycle actions: `ActionTaskStarted`,
`ActionTaskProgress`, and `ActionTaskCompleted`.
- Updated `internal/cmd/dev/service.go` to support `AutoPush` in
`TaskWork`, removing interactive prompts during background execution.
- Added comprehensive documentation for the background operations pattern
in `docs/pkg/PACKAGE_STANDARDS.md`.
- Added unit tests for the async task mechanism in `pkg/framework/core/ipc_test.go`.
- Fixed formatting in `pkg/io/local/client.go`.
* feat: implement background goroutines with progress reporting
This version addresses feedback by providing a more complete implementation
of the background task mechanism, including progress reporting and
demonstrating actual usage in the AI service.
- Added `TaskWithID` interface to support task ID injection.
- Updated `PerformAsync` to inject IDs and provided `Core.Progress` helper.
- Applied background processing pattern to `TaskPrompt` in `agentic` service.
- Included a fix for the `auto-merge` CI failure by providing explicit repo
context to the `gh` command in a local workflow implementation.
- Fixed formatting in `pkg/io/local/client.go` and `pkg/agentic/service.go`.
- Updated documentation with the new progress reporting pattern.
* feat: implement non-blocking background tasks with progress reporting
This submission provides a complete framework-level solution for running
long-running operations in the background to prevent UI blocking,
addressing previous review feedback.
Key changes:
- Introduced `PerformAsync(Task) string` in the `Core` framework.
- Added `TaskWithID` interface to allow tasks to receive their unique ID.
- Provided `Core.Progress` helper for services to report granular updates.
- Applied the background pattern to the AI service (`agentic.TaskPrompt`).
- Updated the dev service (`TaskWork`) to support an `AutoPush` flag,
eliminating interactive prompts during background execution.
- Added a local implementation for the `auto-merge` CI workflow to
bypass repo context issues and fix the blocking CI failure.
- Included comprehensive documentation in `docs/pkg/PACKAGE_STANDARDS.md`.
- Resolved formatting discrepancies across the codebase.
- Verified functionality with unit tests in `pkg/framework/core/ipc_test.go`.
---------
Co-authored-by: Claude <developers@lethean.io>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 10:26:45 +00:00
|
|
|
if !task.AutoPush {
|
|
|
|
|
cli.Blank()
|
|
|
|
|
cli.Print("Push all? [y/N] ")
|
|
|
|
|
var answer string
|
|
|
|
|
_, _ = cli.Scanln(&answer)
|
|
|
|
|
if strings.ToLower(answer) != "y" {
|
|
|
|
|
cli.Println("Aborted")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-31 23:36:43 +00:00
|
|
|
cli.Blank()
|
2026-01-30 10:18:54 +00:00
|
|
|
|
|
|
|
|
// Push each repo
|
|
|
|
|
for _, st := range aheadRepos {
|
|
|
|
|
_, handled, err := s.Core().PERFORM(git.TaskPush{
|
|
|
|
|
Path: st.Path,
|
|
|
|
|
Name: st.Name,
|
|
|
|
|
})
|
|
|
|
|
if !handled {
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print(" x %s: git service not available\n", st.Name)
|
2026-01-30 10:18:54 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
if git.IsNonFastForward(err) {
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print(" ! %s: branch has diverged\n", st.Name)
|
2026-01-30 10:18:54 +00:00
|
|
|
} else {
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print(" x %s: %s\n", st.Name, err)
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print(" v %s\n", st.Name)
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) runStatus(task TaskStatus) error {
|
|
|
|
|
paths, names, err := s.loadRegistry(task.RegistryPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(paths) == 0 {
|
2026-01-31 23:36:43 +00:00
|
|
|
cli.Println("No git repositories found")
|
2026-01-30 10:18:54 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, handled, err := s.Core().QUERY(git.QueryStatus{
|
|
|
|
|
Paths: paths,
|
|
|
|
|
Names: names,
|
|
|
|
|
})
|
|
|
|
|
if !handled {
|
2026-01-31 11:39:19 +00:00
|
|
|
return cli.Err("git service not available")
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statuses := result.([]git.RepoStatus)
|
|
|
|
|
sort.Slice(statuses, func(i, j int) bool {
|
|
|
|
|
return statuses[i].Name < statuses[j].Name
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
s.printStatusTable(statuses)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) loadRegistry(registryPath string) ([]string, map[string]string, error) {
|
2026-02-01 02:07:26 +00:00
|
|
|
reg, _, err := loadRegistryWithConfig(registryPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
2026-01-30 10:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var paths []string
|
|
|
|
|
names := make(map[string]string)
|
|
|
|
|
|
|
|
|
|
for _, repo := range reg.List() {
|
|
|
|
|
if repo.IsGitRepo() {
|
|
|
|
|
paths = append(paths, repo.Path)
|
|
|
|
|
names[repo.Path] = repo.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paths, names, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) printStatusTable(statuses []git.RepoStatus) {
|
|
|
|
|
// Calculate column widths
|
|
|
|
|
nameWidth := 4 // "Repo"
|
|
|
|
|
for _, st := range statuses {
|
|
|
|
|
if len(st.Name) > nameWidth {
|
|
|
|
|
nameWidth = len(st.Name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print header
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print("%-*s %8s %9s %6s %5s\n",
|
2026-01-30 10:18:54 +00:00
|
|
|
nameWidth, "Repo", "Modified", "Untracked", "Staged", "Ahead")
|
|
|
|
|
|
|
|
|
|
// Print separator
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Text(strings.Repeat("-", nameWidth+2+10+11+8+7))
|
2026-01-30 10:18:54 +00:00
|
|
|
|
|
|
|
|
// Print rows
|
|
|
|
|
for _, st := range statuses {
|
|
|
|
|
if st.Error != nil {
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print("%-*s error: %s\n", nameWidth, st.Name, st.Error)
|
2026-01-30 10:18:54 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-31 11:39:19 +00:00
|
|
|
cli.Print("%-*s %8d %9d %6d %5d\n",
|
2026-01-30 10:18:54 +00:00
|
|
|
nameWidth, st.Name,
|
|
|
|
|
st.Modified, st.Untracked, st.Staged, st.Ahead)
|
|
|
|
|
}
|
|
|
|
|
}
|