2026-03-16 11:10:33 +00:00
|
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
|
|
|
|
|
|
package agentic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-03-22 03:41:07 +00:00
|
|
|
core "dappco.re/go/core"
|
2026-03-16 11:10:33 +00:00
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Workspace status file convention:
|
|
|
|
|
//
|
|
|
|
|
// {workspace}/status.json — current state of the workspace
|
2026-03-29 20:15:58 +00:00
|
|
|
// {workspace}/repo/BLOCKED.md — question the agent needs answered (written by agent)
|
|
|
|
|
// {workspace}/repo/ANSWER.md — response from human (written by reviewer)
|
|
|
|
|
// {workspace}/.meta/agent-*.log — captured agent output
|
2026-03-16 11:10:33 +00:00
|
|
|
//
|
|
|
|
|
// Status lifecycle:
|
|
|
|
|
// running → completed (normal finish)
|
|
|
|
|
// running → blocked (agent wrote BLOCKED.md and exited)
|
|
|
|
|
// blocked → running (resume after ANSWER.md provided)
|
2026-03-17 17:45:04 +00:00
|
|
|
// completed → merged (PR verified and auto-merged)
|
2026-03-16 11:10:33 +00:00
|
|
|
// running → failed (agent crashed / non-zero exit)
|
|
|
|
|
|
|
|
|
|
// WorkspaceStatus represents the current state of an agent workspace.
|
2026-03-22 03:41:07 +00:00
|
|
|
//
|
2026-03-24 13:02:41 +00:00
|
|
|
// st, err := ReadStatus(wsDir)
|
2026-03-22 03:41:07 +00:00
|
|
|
// if err == nil && st.Status == "completed" { autoCreatePR(wsDir) }
|
2026-03-16 11:10:33 +00:00
|
|
|
type WorkspaceStatus struct {
|
2026-03-29 20:15:58 +00:00
|
|
|
Status string `json:"status"` // running, completed, blocked, failed
|
|
|
|
|
Agent string `json:"agent"` // gemini, claude, codex
|
|
|
|
|
Repo string `json:"repo"` // target repo
|
|
|
|
|
Org string `json:"org,omitempty"` // forge org (e.g. "core")
|
|
|
|
|
Task string `json:"task"` // task description
|
|
|
|
|
Branch string `json:"branch,omitempty"` // git branch name
|
2026-03-25 10:10:17 +00:00
|
|
|
Issue int `json:"issue,omitempty"` // forge issue number
|
|
|
|
|
PID int `json:"pid,omitempty"` // OS process ID (if running)
|
|
|
|
|
ProcessID string `json:"process_id,omitempty"` // go-process ID for managed lookup
|
|
|
|
|
StartedAt time.Time `json:"started_at"` // when dispatch started
|
2026-03-29 20:15:58 +00:00
|
|
|
UpdatedAt time.Time `json:"updated_at"` // last status change
|
|
|
|
|
Question string `json:"question,omitempty"` // from BLOCKED.md
|
|
|
|
|
Runs int `json:"runs"` // how many times dispatched/resumed
|
|
|
|
|
PRURL string `json:"pr_url,omitempty"` // pull request URL (after PR created)
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 06:38:02 +00:00
|
|
|
// WorkspaceQuery is the QUERY type for workspace state lookups.
|
|
|
|
|
// Returns the workspace Registry via c.QUERY(agentic.WorkspaceQuery{}).
|
|
|
|
|
//
|
|
|
|
|
// r := c.QUERY(agentic.WorkspaceQuery{})
|
|
|
|
|
// if r.OK { reg := r.Value.(*core.Registry[*WorkspaceStatus]) }
|
|
|
|
|
// r := c.QUERY(agentic.WorkspaceQuery{Name: "core/go-io/task-5"})
|
|
|
|
|
type WorkspaceQuery struct {
|
|
|
|
|
Name string // specific workspace (empty = all)
|
|
|
|
|
Status string // filter by status (empty = all)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 11:10:33 +00:00
|
|
|
func writeStatus(wsDir string, status *WorkspaceStatus) error {
|
2026-03-30 07:30:42 +00:00
|
|
|
r := writeStatusResult(wsDir, status)
|
|
|
|
|
if !r.OK {
|
|
|
|
|
err, _ := r.Value.(error)
|
|
|
|
|
if err == nil {
|
|
|
|
|
err = core.E("writeStatus", "failed to write status", nil)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// writeStatusResult writes status.json and returns core.Result.
|
|
|
|
|
//
|
|
|
|
|
// r := writeStatusResult("/srv/core/workspace/core/go-io/task-5", &WorkspaceStatus{Status: "running"})
|
|
|
|
|
// if r.OK { return }
|
|
|
|
|
func writeStatusResult(wsDir string, status *WorkspaceStatus) core.Result {
|
|
|
|
|
if status == nil {
|
|
|
|
|
return core.Result{Value: core.E("writeStatus", "status is required", nil), OK: false}
|
|
|
|
|
}
|
2026-03-16 11:10:33 +00:00
|
|
|
status.UpdatedAt = time.Now()
|
2026-03-29 21:11:46 +00:00
|
|
|
statusPath := WorkspaceStatusPath(wsDir)
|
feat(v0.8.0): full AX migration — ServiceRuntime, Actions, quality gates, transport
go-process:
- Register factory, Result lifecycle, 5 named Action handlers
- Start/Run/StartWithOptions/RunWithOptions all return core.Result
- core.ID() replaces fmt.Sprintf, core.As replaces errors.As
core/agent:
- PrepSubsystem + monitor.Subsystem + setup.Service embed ServiceRuntime[T]
- 22 named Actions + agent.completion Task pipeline in OnStartup
- ChannelNotifier removed — all IPC via c.ACTION(messages.X{})
- proc.go: all methods via s.Core().Process(), returns core.Result
- status.go: WriteAtomic + JSONMarshalString
- paths.go: Fs.NewUnrestricted() replaces unsafe.Pointer
- transport.go: ONE net/http file — HTTPGet/HTTPPost/HTTPDo/MCP transport
- All disallowed imports eliminated from source files (13 quality gates)
- String concat eliminated — core.Concat() throughout
- 1:1 _test.go + _example_test.go for every source file
- Reference docs synced from core/go v0.8.0
- RFC-025 updated with net/http, net/url, io/fs quality gates
- lib.go: io/fs eliminated via Data.ListNames, Array[T].Deduplicate
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-26 01:27:46 +00:00
|
|
|
if r := fs.WriteAtomic(statusPath, core.JSONMarshalString(status)); !r.OK {
|
2026-03-22 03:45:50 +00:00
|
|
|
err, _ := r.Value.(error)
|
2026-03-30 07:30:42 +00:00
|
|
|
if err == nil {
|
|
|
|
|
return core.Result{Value: core.E("writeStatus", "failed to write status", nil), OK: false}
|
|
|
|
|
}
|
|
|
|
|
return core.Result{Value: core.E("writeStatus", "failed to write status", err), OK: false}
|
2026-03-22 03:41:07 +00:00
|
|
|
}
|
2026-03-30 07:30:42 +00:00
|
|
|
return core.Result{OK: true}
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-24 13:02:41 +00:00
|
|
|
// ReadStatus parses the status.json in a workspace directory.
|
|
|
|
|
//
|
|
|
|
|
// st, err := agentic.ReadStatus("/path/to/workspace")
|
|
|
|
|
func ReadStatus(wsDir string) (*WorkspaceStatus, error) {
|
2026-03-30 07:30:42 +00:00
|
|
|
r := ReadStatusResult(wsDir)
|
|
|
|
|
if !r.OK {
|
|
|
|
|
err, _ := r.Value.(error)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return nil, core.E("ReadStatus", "failed to read status", nil)
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
st, ok := r.Value.(*WorkspaceStatus)
|
|
|
|
|
if !ok || st == nil {
|
|
|
|
|
return nil, core.E("ReadStatus", "invalid status payload", nil)
|
|
|
|
|
}
|
|
|
|
|
return st, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReadStatusResult parses status.json and returns a WorkspaceStatus pointer.
|
|
|
|
|
//
|
|
|
|
|
// r := ReadStatusResult("/path/to/workspace")
|
|
|
|
|
// if r.OK { st := r.Value.(*WorkspaceStatus) }
|
|
|
|
|
func ReadStatusResult(wsDir string) core.Result {
|
2026-03-29 21:11:46 +00:00
|
|
|
r := fs.Read(WorkspaceStatusPath(wsDir))
|
2026-03-22 03:41:07 +00:00
|
|
|
if !r.OK {
|
2026-03-30 07:30:42 +00:00
|
|
|
err, _ := r.Value.(error)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return core.Result{Value: core.E("ReadStatus", "status not found", nil), OK: false}
|
|
|
|
|
}
|
|
|
|
|
return core.Result{Value: core.E("ReadStatus", core.Concat("status not found for ", wsDir), err), OK: false}
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
var s WorkspaceStatus
|
feat(v0.8.0): full AX migration — ServiceRuntime, Actions, quality gates, transport
go-process:
- Register factory, Result lifecycle, 5 named Action handlers
- Start/Run/StartWithOptions/RunWithOptions all return core.Result
- core.ID() replaces fmt.Sprintf, core.As replaces errors.As
core/agent:
- PrepSubsystem + monitor.Subsystem + setup.Service embed ServiceRuntime[T]
- 22 named Actions + agent.completion Task pipeline in OnStartup
- ChannelNotifier removed — all IPC via c.ACTION(messages.X{})
- proc.go: all methods via s.Core().Process(), returns core.Result
- status.go: WriteAtomic + JSONMarshalString
- paths.go: Fs.NewUnrestricted() replaces unsafe.Pointer
- transport.go: ONE net/http file — HTTPGet/HTTPPost/HTTPDo/MCP transport
- All disallowed imports eliminated from source files (13 quality gates)
- String concat eliminated — core.Concat() throughout
- 1:1 _test.go + _example_test.go for every source file
- Reference docs synced from core/go v0.8.0
- RFC-025 updated with net/http, net/url, io/fs quality gates
- lib.go: io/fs eliminated via Data.ListNames, Array[T].Deduplicate
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-26 01:27:46 +00:00
|
|
|
if ur := core.JSONUnmarshalString(r.Value.(string), &s); !ur.OK {
|
|
|
|
|
err, _ := ur.Value.(error)
|
2026-03-30 07:30:42 +00:00
|
|
|
if err == nil {
|
|
|
|
|
return core.Result{Value: core.E("ReadStatus", "invalid status json", nil), OK: false}
|
|
|
|
|
}
|
|
|
|
|
return core.Result{Value: core.E("ReadStatus", "invalid status json", err), OK: false}
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
2026-03-30 07:30:42 +00:00
|
|
|
return core.Result{Value: &s, OK: true}
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- agentic_status tool ---
|
|
|
|
|
|
refactor: migrate core/agent to Core primitives — reference implementation
Phase 1: go-io/go-log → core.Fs{}, core.E(), core.Error/Info/Warn
Phase 2: strings/fmt → core.Contains, core.Sprintf, core.Split etc
Phase 3: embed.FS → core.Mount/core.Embed, core.Extract
Phase 4: cmd/main.go → core.Command(), c.Cli().Run(), no cli package
All packages migrated:
- pkg/lib (Codex): core.Mount, core.Extract, Result returns, AX comments
- pkg/setup (Codex): core.Fs, core.E, fixed missing lib helpers
- pkg/brain (Codex): Core primitives, AX comments
- pkg/monitor (Codex): Core string/logging primitives
- pkg/agentic (Codex): 20 files, Core primitives throughout
- cmd/main.go: pure Core CLI, no fmt/log/filepath/strings/cli
Remaining stdlib: path/filepath (Core doesn't wrap OS paths),
fmt.Sscanf/strings.Map (no Core equivalent).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 06:13:41 +00:00
|
|
|
// StatusInput is the input for agentic_status.
|
|
|
|
|
//
|
2026-03-29 21:56:45 +00:00
|
|
|
// input := agentic.StatusInput{Workspace: "core/go-io/task-42", Limit: 50}
|
2026-03-16 11:10:33 +00:00
|
|
|
type StatusInput struct {
|
|
|
|
|
Workspace string `json:"workspace,omitempty"` // specific workspace name, or empty for all
|
2026-03-23 12:53:33 +00:00
|
|
|
Limit int `json:"limit,omitempty"` // max results (default 100)
|
|
|
|
|
Status string `json:"status,omitempty"` // filter: running, completed, failed, blocked
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
refactor: migrate core/agent to Core primitives — reference implementation
Phase 1: go-io/go-log → core.Fs{}, core.E(), core.Error/Info/Warn
Phase 2: strings/fmt → core.Contains, core.Sprintf, core.Split etc
Phase 3: embed.FS → core.Mount/core.Embed, core.Extract
Phase 4: cmd/main.go → core.Command(), c.Cli().Run(), no cli package
All packages migrated:
- pkg/lib (Codex): core.Mount, core.Extract, Result returns, AX comments
- pkg/setup (Codex): core.Fs, core.E, fixed missing lib helpers
- pkg/brain (Codex): Core primitives, AX comments
- pkg/monitor (Codex): Core string/logging primitives
- pkg/agentic (Codex): 20 files, Core primitives throughout
- cmd/main.go: pure Core CLI, no fmt/log/filepath/strings/cli
Remaining stdlib: path/filepath (Core doesn't wrap OS paths),
fmt.Sscanf/strings.Map (no Core equivalent).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 06:13:41 +00:00
|
|
|
// StatusOutput is the output for agentic_status.
|
2026-03-23 12:53:33 +00:00
|
|
|
// Returns stats by default. Only blocked workspaces are listed (they need attention).
|
refactor: migrate core/agent to Core primitives — reference implementation
Phase 1: go-io/go-log → core.Fs{}, core.E(), core.Error/Info/Warn
Phase 2: strings/fmt → core.Contains, core.Sprintf, core.Split etc
Phase 3: embed.FS → core.Mount/core.Embed, core.Extract
Phase 4: cmd/main.go → core.Command(), c.Cli().Run(), no cli package
All packages migrated:
- pkg/lib (Codex): core.Mount, core.Extract, Result returns, AX comments
- pkg/setup (Codex): core.Fs, core.E, fixed missing lib helpers
- pkg/brain (Codex): Core primitives, AX comments
- pkg/monitor (Codex): Core string/logging primitives
- pkg/agentic (Codex): 20 files, Core primitives throughout
- cmd/main.go: pure Core CLI, no fmt/log/filepath/strings/cli
Remaining stdlib: path/filepath (Core doesn't wrap OS paths),
fmt.Sscanf/strings.Map (no Core equivalent).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 06:13:41 +00:00
|
|
|
//
|
2026-03-23 12:53:33 +00:00
|
|
|
// out := agentic.StatusOutput{Total: 42, Running: 3, Queued: 10, Completed: 25}
|
2026-03-16 11:10:33 +00:00
|
|
|
type StatusOutput struct {
|
2026-03-29 20:15:58 +00:00
|
|
|
Total int `json:"total"`
|
|
|
|
|
Running int `json:"running"`
|
|
|
|
|
Queued int `json:"queued"`
|
|
|
|
|
Completed int `json:"completed"`
|
|
|
|
|
Failed int `json:"failed"`
|
|
|
|
|
Blocked []BlockedInfo `json:"blocked,omitempty"`
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-23 12:53:33 +00:00
|
|
|
// BlockedInfo shows a workspace that needs human input.
|
refactor: migrate core/agent to Core primitives — reference implementation
Phase 1: go-io/go-log → core.Fs{}, core.E(), core.Error/Info/Warn
Phase 2: strings/fmt → core.Contains, core.Sprintf, core.Split etc
Phase 3: embed.FS → core.Mount/core.Embed, core.Extract
Phase 4: cmd/main.go → core.Command(), c.Cli().Run(), no cli package
All packages migrated:
- pkg/lib (Codex): core.Mount, core.Extract, Result returns, AX comments
- pkg/setup (Codex): core.Fs, core.E, fixed missing lib helpers
- pkg/brain (Codex): Core primitives, AX comments
- pkg/monitor (Codex): Core string/logging primitives
- pkg/agentic (Codex): 20 files, Core primitives throughout
- cmd/main.go: pure Core CLI, no fmt/log/filepath/strings/cli
Remaining stdlib: path/filepath (Core doesn't wrap OS paths),
fmt.Sscanf/strings.Map (no Core equivalent).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 06:13:41 +00:00
|
|
|
//
|
2026-03-29 21:56:45 +00:00
|
|
|
// info := agentic.BlockedInfo{Name: "core/go-io/task-4", Repo: "go-io", Question: "Which API version?"}
|
2026-03-23 12:53:33 +00:00
|
|
|
type BlockedInfo struct {
|
refactor: migrate core/agent to Core primitives — reference implementation
Phase 1: go-io/go-log → core.Fs{}, core.E(), core.Error/Info/Warn
Phase 2: strings/fmt → core.Contains, core.Sprintf, core.Split etc
Phase 3: embed.FS → core.Mount/core.Embed, core.Extract
Phase 4: cmd/main.go → core.Command(), c.Cli().Run(), no cli package
All packages migrated:
- pkg/lib (Codex): core.Mount, core.Extract, Result returns, AX comments
- pkg/setup (Codex): core.Fs, core.E, fixed missing lib helpers
- pkg/brain (Codex): Core primitives, AX comments
- pkg/monitor (Codex): Core string/logging primitives
- pkg/agentic (Codex): 20 files, Core primitives throughout
- cmd/main.go: pure Core CLI, no fmt/log/filepath/strings/cli
Remaining stdlib: path/filepath (Core doesn't wrap OS paths),
fmt.Sscanf/strings.Map (no Core equivalent).
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 06:13:41 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
Repo string `json:"repo"`
|
2026-03-23 12:53:33 +00:00
|
|
|
Agent string `json:"agent"`
|
|
|
|
|
Question string `json:"question"`
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *PrepSubsystem) registerStatusTool(server *mcp.Server) {
|
|
|
|
|
mcp.AddTool(server, &mcp.Tool{
|
|
|
|
|
Name: "agentic_status",
|
|
|
|
|
Description: "List agent workspaces and their status (running, completed, blocked, failed). Shows blocked agents with their questions.",
|
|
|
|
|
}, s.status)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *PrepSubsystem) status(ctx context.Context, _ *mcp.CallToolRequest, input StatusInput) (*mcp.CallToolResult, StatusOutput, error) {
|
2026-03-29 20:15:58 +00:00
|
|
|
statusFiles := WorkspaceStatusPaths()
|
2026-03-16 11:10:33 +00:00
|
|
|
|
2026-03-23 12:53:33 +00:00
|
|
|
var out StatusOutput
|
2026-03-16 11:10:33 +00:00
|
|
|
|
2026-03-22 16:03:11 +00:00
|
|
|
for _, statusPath := range statusFiles {
|
|
|
|
|
wsDir := core.PathDir(statusPath)
|
2026-03-29 21:19:37 +00:00
|
|
|
name := WorkspaceName(wsDir)
|
2026-03-16 11:10:33 +00:00
|
|
|
|
2026-03-24 13:02:41 +00:00
|
|
|
st, err := ReadStatus(wsDir)
|
2026-03-16 11:10:33 +00:00
|
|
|
if err != nil {
|
2026-03-23 12:53:33 +00:00
|
|
|
out.Total++
|
|
|
|
|
out.Failed++
|
2026-03-16 11:10:33 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If status is "running", check if PID is still alive
|
|
|
|
|
if st.Status == "running" && st.PID > 0 {
|
2026-03-30 00:28:11 +00:00
|
|
|
if !PIDAlive(st.PID) {
|
2026-03-29 20:15:58 +00:00
|
|
|
blockedPath := workspaceBlockedPath(wsDir)
|
2026-03-22 03:41:07 +00:00
|
|
|
if r := fs.Read(blockedPath); r.OK {
|
2026-03-16 11:10:33 +00:00
|
|
|
st.Status = "blocked"
|
2026-03-23 12:53:33 +00:00
|
|
|
st.Question = core.Trim(r.Value.(string))
|
2026-03-16 11:10:33 +00:00
|
|
|
} else {
|
2026-03-29 20:15:58 +00:00
|
|
|
if len(workspaceLogFiles(wsDir)) == 0 {
|
2026-03-21 17:10:43 +00:00
|
|
|
st.Status = "failed"
|
|
|
|
|
st.Question = "Agent process died (no output log)"
|
|
|
|
|
} else {
|
|
|
|
|
st.Status = "completed"
|
|
|
|
|
}
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
writeStatus(wsDir, st)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-23 12:53:33 +00:00
|
|
|
out.Total++
|
|
|
|
|
switch st.Status {
|
|
|
|
|
case "running":
|
|
|
|
|
out.Running++
|
|
|
|
|
case "queued":
|
|
|
|
|
out.Queued++
|
|
|
|
|
case "completed":
|
|
|
|
|
out.Completed++
|
|
|
|
|
case "failed":
|
|
|
|
|
out.Failed++
|
|
|
|
|
case "blocked":
|
|
|
|
|
out.Blocked = append(out.Blocked, BlockedInfo{
|
|
|
|
|
Name: name,
|
|
|
|
|
Repo: st.Repo,
|
|
|
|
|
Agent: st.Agent,
|
|
|
|
|
Question: st.Question,
|
|
|
|
|
})
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-23 12:53:33 +00:00
|
|
|
return nil, out, nil
|
2026-03-16 11:10:33 +00:00
|
|
|
}
|