agent/pkg/agentic/commands_workspace.go
Snider 537226bd4d feat: AX v0.8.0 upgrade — Core features + quality gates
AX Quality Gates (RFC-025):
- Eliminate os/exec from all test + production code (12+ files)
- Eliminate encoding/json from all test files (15 files, 66 occurrences)
- Eliminate os from all test files except TestMain (Go runtime contract)
- Eliminate path/filepath, net/url from all files
- String concat: 39 violations replaced with core.Concat()
- Test naming AX-7: 264 test functions renamed across all 6 packages
- Example test 1:1 coverage complete

Core Features Adopted:
- Task Composition: agent.completion pipeline (QA → PR → Verify → Ingest → Poke)
- PerformAsync: completion pipeline runs with WaitGroup + progress tracking
- Config: agents.yaml loaded once, feature flags (auto-qa/pr/merge/ingest)
- Named Locks: c.Lock("drain") for queue serialisation
- Registry: workspace state with cross-package QUERY access
- QUERY: c.QUERY(WorkspaceQuery{Status: "running"}) for cross-service queries
- Action descriptions: 25+ Actions self-documenting
- Data mounts: prompts/tasks/flows/personas/workspaces via c.Data()
- Content Actions: agentic.prompt/task/flow/persona callable via IPC
- Drive endpoints: forge + brain registered with tokens
- Drive REST helpers: DriveGet/DrivePost/DriveDo for Drive-aware HTTP
- HandleIPCEvents: auto-discovered by WithService (no manual wiring)
- Entitlement: frozen-queue gate on write Actions
- CLI dispatch: workspace dispatch wired to real dispatch method
- CLI: --quiet/-q and --debug/-d global flags
- CLI: banner, version, check (with service/action/command counts), env
- main.go: minimal — 5 services + c.Run(), no os import
- cmd tests: 84.2% coverage (was 0%)

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-26 06:38:02 +00:00

160 lines
4.4 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
// Workspace CLI commands registered by the agentic service during OnStartup.
package agentic
import (
"context"
core "dappco.re/go/core"
)
// registerWorkspaceCommands adds workspace management commands.
func (s *PrepSubsystem) registerWorkspaceCommands() {
c := s.Core()
c.Command("workspace/list", core.Command{Description: "List all agent workspaces with status", Action: s.cmdWorkspaceList})
c.Command("workspace/clean", core.Command{Description: "Remove completed/failed/blocked workspaces", Action: s.cmdWorkspaceClean})
c.Command("workspace/dispatch", core.Command{Description: "Dispatch an agent to work on a repo task", Action: s.cmdWorkspaceDispatch})
}
func (s *PrepSubsystem) cmdWorkspaceList(opts core.Options) core.Result {
wsRoot := WorkspaceRoot()
fsys := s.Core().Fs()
statusFiles := core.PathGlob(core.JoinPath(wsRoot, "*", "status.json"))
count := 0
for _, sf := range statusFiles {
wsName := core.PathBase(core.PathDir(sf))
if sr := fsys.Read(sf); sr.OK {
content := sr.Value.(string)
status := extractField(content, "status")
repo := extractField(content, "repo")
agent := extractField(content, "agent")
core.Print(nil, " %-8s %-8s %-10s %s", status, agent, repo, wsName)
count++
}
}
if count == 0 {
core.Print(nil, " no workspaces")
}
return core.Result{OK: true}
}
func (s *PrepSubsystem) cmdWorkspaceClean(opts core.Options) core.Result {
wsRoot := WorkspaceRoot()
fsys := s.Core().Fs()
filter := opts.String("_arg")
if filter == "" {
filter = "all"
}
statusFiles := core.PathGlob(core.JoinPath(wsRoot, "*", "status.json"))
var toRemove []string
for _, sf := range statusFiles {
wsName := core.PathBase(core.PathDir(sf))
sr := fsys.Read(sf)
if !sr.OK {
continue
}
status := extractField(sr.Value.(string), "status")
switch filter {
case "all":
if status == "completed" || status == "failed" || status == "blocked" || status == "merged" || status == "ready-for-review" {
toRemove = append(toRemove, wsName)
}
case "completed":
if status == "completed" || status == "merged" || status == "ready-for-review" {
toRemove = append(toRemove, wsName)
}
case "failed":
if status == "failed" {
toRemove = append(toRemove, wsName)
}
case "blocked":
if status == "blocked" {
toRemove = append(toRemove, wsName)
}
}
}
if len(toRemove) == 0 {
core.Print(nil, "nothing to clean")
return core.Result{OK: true}
}
for _, name := range toRemove {
path := core.JoinPath(wsRoot, name)
fsys.DeleteAll(path)
core.Print(nil, " removed %s", name)
}
core.Print(nil, "\n %d workspaces removed", len(toRemove))
return core.Result{OK: true}
}
func (s *PrepSubsystem) cmdWorkspaceDispatch(opts core.Options) core.Result {
repo := opts.String("_arg")
if repo == "" {
core.Print(nil, "usage: core-agent workspace dispatch <repo> --task=\"...\" --issue=N|--pr=N|--branch=X [--agent=codex]")
return core.Result{OK: false}
}
// Call dispatch directly — CLI is an explicit user action,
// not gated by the frozen-queue entitlement.
input := DispatchInput{
Repo: repo,
Task: opts.String("task"),
Agent: opts.String("agent"),
Org: opts.String("org"),
Template: opts.String("template"),
Branch: opts.String("branch"),
Issue: parseIntStr(opts.String("issue")),
PR: parseIntStr(opts.String("pr")),
}
_, out, err := s.dispatch(context.Background(), nil, input)
if err != nil {
core.Print(nil, "dispatch failed: %s", err.Error())
return core.Result{Value: err, OK: false}
}
agent := out.Agent
if agent == "" {
agent = "codex"
}
core.Print(nil, "dispatched %s to %s", agent, repo)
if out.WorkspaceDir != "" {
core.Print(nil, " workspace: %s", out.WorkspaceDir)
}
if out.PID > 0 {
core.Print(nil, " pid: %d", out.PID)
}
return core.Result{OK: true}
}
// extractField does a quick JSON field extraction without full unmarshal.
func extractField(jsonStr, field string) string {
needle := core.Concat("\"", field, "\"")
idx := -1
for i := 0; i <= len(jsonStr)-len(needle); i++ {
if jsonStr[i:i+len(needle)] == needle {
idx = i + len(needle)
break
}
}
if idx < 0 {
return ""
}
for idx < len(jsonStr) && (jsonStr[idx] == ':' || jsonStr[idx] == ' ' || jsonStr[idx] == '\t') {
idx++
}
if idx >= len(jsonStr) || jsonStr[idx] != '"' {
return ""
}
idx++
end := idx
for end < len(jsonStr) && jsonStr[end] != '"' {
end++
}
return jsonStr[idx:end]
}