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
1 change: 1 addition & 0 deletions changelog.d/added-14798-job-runtime-terminal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The dashboard's **▶ terminal** now works for agents with `sandbox.runtime: job` ([#14798](https://github.com/hivecommons/hive/issues/14798)). Those kicks run as a Kubernetes Job in another pod, so there was no tmux session to attach and the pane waited forever. For a job-runtime agent the link now opens a read-only viewer at `/terminal/job/<agent>` that streams the newest Job pod's log through the launcher's existing in-cluster client and `pods/log` RBAC: it follows the pod while the kick runs (waiting while it is `Pending`), prints the container exit code when it ends, and between kicks shows the last run's persisted sandbox transcript — which now ends with an `exit code: N` line — or a clear "no sandbox run yet" state. Podman-sandboxed and tmux agents are unchanged.
6 changes: 1 addition & 5 deletions src/crux/agent/manager_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -178,10 +177,7 @@ func (m *Manager) startSandboxKickLocked(agent *AgentProcess, message string) er
}

func (m *Manager) sandboxWorkspaceDirLocked() string {
if strings.TrimSpace(m.sandboxConfig.WorkspaceDir) != "" {
return m.sandboxConfig.WorkspaceDir
}
return filepath.Join(m.workDir, "sandbox")
return sandbox.WorkspaceRoot(m.sandboxConfig.WorkspaceDir, m.workDir)
}

func (m *Manager) tieredSandboxMinterLocked(tier string) pushbroker.TokenMinter {
Expand Down
33 changes: 9 additions & 24 deletions src/crux/agent/sandbox_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -24,7 +25,7 @@ const (
DefaultSandboxKickTimeout = 45 * time.Minute
defaultSandboxNetworkMode = sandbox.NetworkModeRestricted
sandboxPromptRelPath = ".hive/kick-prompt.txt"
sandboxTranscriptRelPath = ".hive/sandbox-transcript.log"
sandboxTranscriptRelPath = sandbox.TranscriptRelPath
// The sandboxed agent may leave the PR title and body it wants in the
// workspace; the executor (which alone can open the PR) picks them up.
sandboxPRTitleRelPath = ".hive/pr-title.txt"
Expand Down Expand Up @@ -197,7 +198,7 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo
defer cancel()
sres, runErr := e.launcher().Run(runCtx, sandbox.LaunchSpec{
Image: spec.Image,
Name: "hive-" + sanitizeBranchPart(spec.Agent) + "-sandbox",
Name: sandbox.LaunchName(spec.Agent),
Workspace: workspace,
WorkspaceMount: sandbox.DefaultWorkspaceMount,
WorkDir: sandbox.DefaultWorkspaceMount,
Expand Down Expand Up @@ -426,6 +427,11 @@ func writeTranscript(path string, res sandbox.Result) error {
b.WriteString(res.Stdout)
b.WriteString("\n\nstderr:\n")
b.WriteString(res.Stderr)
// Trailing status line, read back by the dashboard's job-runtime
// terminal pane once the Job pod is gone (hivecommons/hive#14798).
b.WriteString("\n\n" + sandbox.TranscriptExitPrefix)
b.WriteString(strconv.Itoa(res.ExitCode))
b.WriteString("\n")
return os.WriteFile(path, []byte(b.String()), 0o660)
}

Expand Down Expand Up @@ -636,28 +642,7 @@ func (e *SandboxExecutor) branchName(agentName string) string {
return "hive/" + sanitizeBranchPart(agentName) + "/" + e.now().Format("20060102150405")
}

func sanitizeBranchPart(s string) string {
s = strings.TrimSpace(strings.ToLower(s))
var b strings.Builder
lastDash := false
for _, r := range s {
ok := (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9')
if ok {
b.WriteRune(r)
lastDash = false
continue
}
if !lastDash {
b.WriteByte('-')
lastDash = true
}
}
out := strings.Trim(b.String(), "-")
if out == "" {
return "agent"
}
return out
}
func sanitizeBranchPart(s string) string { return sandbox.AgentSlug(s) }

func shellQuote(s string) string {
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
Expand Down
19 changes: 19 additions & 0 deletions src/crux/agent/sandbox_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ func TestSandboxExecutorWorkspacePrepSanitizesCredentials(t *testing.T) {
if res.Workspace == "" || res.Report == nil || res.TranscriptPath == "" {
t.Fatalf("result missing collected artifacts: %+v", res)
}
// The workspace and transcript follow the shared sandbox naming so the
// dashboard's job-runtime terminal pane can find them by agent name
// (hivecommons/hive#14798), and the transcript ends with the exit code.
if want := sandbox.WorkspaceDir(root, "scanner", time.Unix(1, 0).UnixNano()); res.Workspace != want {
t.Fatalf("workspace = %q, want %q", res.Workspace, want)
}
if latest, ok := sandbox.LatestTranscript(root, "scanner"); !ok || latest != res.TranscriptPath {
t.Fatalf("LatestTranscript = %q, %v; want %q", latest, ok, res.TranscriptPath)
}
transcript, err := os.ReadFile(res.TranscriptPath)
if err != nil {
t.Fatal(err)
}
if code, ok := sandbox.TranscriptExitCode(string(transcript)); !ok || code != 0 {
t.Fatalf("transcript exit code = %d, %v; transcript:\n%s", code, ok, transcript)
}
if !strings.HasPrefix(string(transcript), "stdout:\ndone\n") {
t.Fatalf("transcript body changed shape:\n%s", transcript)
}
for _, env := range runner.envs {
for _, pair := range env {
if strings.HasPrefix(pair, "GITHUB_TOKEN=") {
Expand Down
2 changes: 1 addition & 1 deletion src/crux/agent/sandbox_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (e *SandboxExecutor) prepareWorkspace(ctx context.Context, spec *SandboxKic
if strings.TrimSpace(spec.Stage) != "" {
return e.prepareRunStageWorkspace(ctx, spec)
}
workspace := filepath.Join(spec.WorkspaceDir, sanitizeBranchPart(spec.Agent)+"-"+fmt.Sprint(e.now().UnixNano()))
workspace := sandbox.WorkspaceDir(spec.WorkspaceDir, spec.Agent, e.now().UnixNano())
repoURL := fmt.Sprintf("https://github.com/%s/%s.git", spec.Org, spec.Repo)
authArgs, cleanupAuth, err := e.cloneAuthArgs(ctx, spec.Org+"/"+spec.Repo, spec.WorkspaceDir)
if err != nil {
Expand Down
Loading
Loading