Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions cmd/devteam/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (
"flag"
"fmt"
"io/fs"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/MichielDean/devteam/internal/api"
"github.com/MichielDean/devteam/internal/chat"
Expand Down Expand Up @@ -105,8 +109,28 @@ func main() {
server.SetChatConfig(cfg, p.Dispatcher())
server.RestoreActiveProcesses()

fmt.Printf("Dev Team Web UI starting on %s\n", *httpAddr)
if err := server.Start(); err != nil {
// Graceful shutdown on SIGTERM/SIGINT (construction prerequisite, iac-designs
// §5 item 4 / infra-specs §6.2). Gives in-flight requests 30s to drain before
// the process exits; systemd's TimeoutStopSec=35 provides 5s margin.
shutdownErrCh := make(chan error, 1)
go func() {
fmt.Printf("Dev Team Web UI starting on %s\n", *httpAddr)
if err := server.Start(); err != nil && err != http.ErrServerClosed {
shutdownErrCh <- err
}
}()

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
select {
case <-sigCh:
fmt.Println("\nreceived shutdown signal, draining...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.Shutdown(ctx)
database.Close()
fmt.Println("shutdown complete")
case err := <-shutdownErrCh:
fmt.Fprintf(os.Stderr, "server error: %v\n", err)
os.Exit(1)
}
Expand Down
33 changes: 18 additions & 15 deletions internal/api/agent_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,14 @@ func (s *Server) dispatchHumanProxy(featureID, phase string) {
f.ID, f.Title, f.Scope, f.Depth, f.CurrentStage, stageName)
os.WriteFile(filepath.Join(contextDir, "CONTEXT.md"), []byte(contextContent), 0644)

// Write opencode config via the shared BuildOpencodeJSON (G3-2, NFR-MAINT-1).
// Both emit sites (this one and internal/role/tmux.go) call the same function
// so the output is structurally identical (SC5). Default-safe: no providers
// configured → default ollama provider (pre-feature behavior).
opencodeConfigBytes, err := role.BuildOpencodeJSON(role.OpencodeConfigInput{
Model: role.DefaultModel,
Providers: nil,
})
if err != nil {
log.Printf("dispatchHumanProxy: build opencode.json: %v", err)
return
}
os.WriteFile(filepath.Join(contextDir, "opencode.json"), opencodeConfigBytes, 0644)
// Write opencode config (same isolated config as stage agents). Uses the
// consolidated buildOpencodeJSON builder (0600 mode, provider-aware).
// The human-proxy currently inherits the legacy ollama fallback (no
// Provider set on the request) — provider resolution for the human-proxy
// is a fast-follow; MVP keeps it on the default provider.
proxyReq := role.DispatchRequest{FeatureID: featureID, Role: "human-proxy"}
opencodeConfig := role.BuildOpencodeJSONExport(proxyReq)
os.WriteFile(filepath.Join(contextDir, "opencode.json"), []byte(opencodeConfig), 0600)

// Write AGENTS.md
agentsMD := "# Human Proxy Agent\n\nYou are answering questions on behalf of the human in an autonomous pipeline. Use the devteam CLI to read and answer questions.\n"
Expand All @@ -173,10 +168,18 @@ func (s *Server) dispatchHumanProxy(featureID, phase string) {
os.WriteFile(filepath.Join(contextDir, ".bashrc"), []byte(fmt.Sprintf("export PATH=\"%s:$PATH\"\n", filepath.Dir(devteamPath))), 0644)
}

// Write agent role file
// Write agent role file. Uses the consolidated buildAgentMD so the model line
// follows the resolved provider (legacy ollama fallback for human-proxy MVP).
agentsDir := filepath.Join(contextDir, "agents")
os.MkdirAll(agentsDir, 0755)
agentMD := "---\ndescription: Answers questions on behalf of the human\nmode: primary\nmodel: ollama/glm-5.2:cloud\n---\n\nYou are the human-proxy agent. Read the pending questions and answer them.\n"
agentMD := role.BuildAgentMDExport(role.DispatchRequest{
FeatureID: featureID,
Phase: phase,
Role: "human-proxy",
})
// Append the human-proxy-specific instructions (buildAgentMD writes the
// generic Dev Team agent header; the human-proxy needs its own task line).
agentMD += "\nYou are the human-proxy agent. Read the pending questions and answer them.\n"
os.WriteFile(filepath.Join(agentsDir, "human-proxy.md"), []byte(agentMD), 0644)

// Build and run the opencode command
Expand Down
36 changes: 36 additions & 0 deletions internal/api/config_guard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package api

import (
"net/http"
"os"
)

// adminGuard wraps a PUT /api/config/* handler. The request is allowed if:
// - r.RemoteAddr is 127.0.0.1 / ::1 / localhost (localhost pass-through), OR
// - header X-Admin-Secret matches env var ADMIN_SECRET (shared secret).
// Otherwise 401 with {"error": "admin access required"}.
//
// GET handlers are NOT guarded (matches existing /api/features unguarded;
// NFR-SEC-02 guards writes only). Full RBAC is deferred (X6).
// Traces U-API-04, NFR-SEC-02.
func adminGuard(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if isLocalhost(r) || matchesAdminSecret(r) {
next(w, r)
return
}
writeError(w, http.StatusUnauthorized, "admin_access_required", "admin access required")
}
}

// matchesAdminSecret returns true if the X-Admin-Secret header matches the
// ADMIN_SECRET env var. If ADMIN_SECRET is unset, this always returns false
// (no shared secret configured → only localhost access allowed).
func matchesAdminSecret(r *http.Request) bool {
secret := os.Getenv("ADMIN_SECRET")
if secret == "" {
return false
}
provided := r.Header.Get("X-Admin-Secret")
return provided != "" && provided == secret
}
Loading
Loading