diff --git a/changelog.d/fixed-sandbox-job-full-log.md b/changelog.d/fixed-sandbox-job-full-log.md new file mode 100644 index 0000000000..93e7064075 --- /dev/null +++ b/changelog.d/fixed-sandbox-job-full-log.md @@ -0,0 +1 @@ +- Sandboxed agents (`sandbox.enabled: true`, Podman or `runtime: job`) no longer 404 on the card's `πŸ“„ full log` / `πŸ“‹ log text` buttons: hive archives the container's captured stdout/stderr as the kick log (`//-sandbox.log`, `source: sandbox-job`/`sandbox-podman`) when the run ends, serves the newest archive from `GET /api/agents/{name}/log`, lists every run in the πŸ•˜ kick history, and names the archive in the last-run record (`logId`); before its first run the button explains that no archived run exists yet. The output used to live only in the Job pod (deleted on completion) and the sandbox workspace (swept after two hours). Reported-by: @joerunde (a1-vllm-spyre spoke). diff --git a/changelog.d/fixed-sandbox-pr-gate.md b/changelog.d/fixed-sandbox-pr-gate.md new file mode 100644 index 0000000000..540b4cf311 --- /dev/null +++ b/changelog.d/fixed-sandbox-pr-gate.md @@ -0,0 +1 @@ +- Sandboxed kicks no longer push a branch and open a PR just because commits exist: `.hive/agent-report.json` gained an optional `status` (`success`, `failed`, `blocked`), and when the report says `failed`/`blocked` or the sandbox exited non-zero hive keeps the commits local, records the run as failed with the reason (`prWithheld`, shown on the card as "no PR opened: …"), and writes a `sandbox_pr_withheld` audit line instead of a PR that can only fail CI. Reports without a `status` publish as before. Reported-by: @joerunde (a1-vllm-spyre spoke). diff --git a/src/crux/agent/kick_logs.go b/src/crux/agent/kick_logs.go index 14585cc31a..698fda062f 100644 --- a/src/crux/agent/kick_logs.go +++ b/src/crux/agent/kick_logs.go @@ -274,7 +274,11 @@ func (m *Manager) FullLogHeader(name string) string { m.mu.RLock() defer m.mu.RUnlock() agent, ok := m.agents[name] - if !ok || agent.tmuxSession == "" || !m.tmuxSessionExistsForAgent(agent) { + if !ok || agent.tmuxSession == "" { + return "" + } + // A sandboxed agent's full log is an archive that carries its own header. + if m.agentSandboxEnabledLocked(agent) || !m.tmuxSessionExistsForAgent(agent) { return "" } return m.kickLogHeaderLocked(agent, "captured", time.Now(), "live", m.kickLogSourceLocked(agent)) diff --git a/src/crux/agent/manager_sandbox.go b/src/crux/agent/manager_sandbox.go index 80a392f5d8..3c2ff299c8 100644 --- a/src/crux/agent/manager_sandbox.go +++ b/src/crux/agent/manager_sandbox.go @@ -2,6 +2,7 @@ package agent import ( "context" + "errors" "fmt" "path/filepath" "strings" @@ -207,8 +208,14 @@ func (m *Manager) runSandboxKick(ctx context.Context, name, runtime string, spec // LIFO), so a slow volume never stalls every status reader. var pending *SandboxRunHistoryEntry var persist func(string, SandboxRunHistoryEntry) error + var logArchive *sandboxKickLogArchive workDir := "" defer func() { + // The log first: it is what an operator opens to understand the run + // record, and the record names it by id. + if perr := m.writeSandboxKickLog(name, logArchive); perr != nil { + m.logger.Warn("sandbox kick log not archived", "name", name, "error", perr) + } if pending == nil { return } @@ -229,6 +236,10 @@ func (m *Manager) runSandboxKick(ctx context.Context, name, runtime string, spec // turn yesterday's successful run into "never ran". if err == nil || !agent.sandboxResumeAfterCancel { run := buildSandboxRun(runtime, started, ended, res, err) + logArchive = m.prepareSandboxKickLogLocked(agent, runtime, started, res, err) + if logArchive != nil { + run.LogID = logArchive.id + } agent.SandboxLastRun = &run agent.KickOutcome = KickOutcome{Kind: KickOutcomeEnded, At: ended, KickAt: started} if agent.LastKick != nil { @@ -262,6 +273,9 @@ func (m *Manager) runSandboxKick(ctx context.Context, name, runtime string, spec if agent.OutputBuffer != nil { agent.OutputBuffer.Write("sandbox kick failed: " + err.Error()) } + if errors.Is(err, ErrSandboxRunReportedFailure) { + m.auditSandbox(name, "sandbox_pr_withheld", fmt.Sprintf("workspace=%s commits=%d branch=%s reason=%s", res.Workspace, res.CommitCount, res.Branch, res.PRWithheld)) + } m.auditSandbox(name, "sandbox_failed", err.Error()) if res.Broker != nil && res.Broker.Error != "" { m.auditSandbox(name, "sandbox_broker_rejected", res.Broker.Error) diff --git a/src/crux/agent/manager_tmux.go b/src/crux/agent/manager_tmux.go index 5ba8bdc893..0a10a27586 100644 --- a/src/crux/agent/manager_tmux.go +++ b/src/crux/agent/manager_tmux.go @@ -544,13 +544,23 @@ func (m *Manager) CaptureFullLog(name string) (string, error) { m.mu.RLock() agent, ok := m.agents[name] var target kickTranscriptTarget + var sandboxed bool + var logDir string if ok { target = m.kickTranscriptTargetLocked(agent) + sandboxed = m.agentSandboxEnabledLocked(agent) + logDir = m.agentKickLogDir(name) } m.mu.RUnlock() if !ok { return "", fmt.Errorf("agent %s not found", name) } + // A sandboxed agent carries a session NAME like every agent but never a + // session: capturing it fails with "exit status 1", which the dashboard + // turned into a 404 on "full log" for every runtime: job agent. + if sandboxed { + return m.sandboxFullLog(name, logDir) + } if agent.tmuxSession == "" { return "", fmt.Errorf("agent %s has no active session", name) } @@ -560,6 +570,21 @@ func (m *Manager) CaptureFullLog(name string) (string, error) { return content, err } +// sandboxFullLog is CaptureFullLog for a sandboxed agent: there is no pane, +// so the "full log" is the newest archived sandbox kick log β€” the container +// or Job output hive collected when the last run ended. A sandboxed agent +// with no run yet gets a message that says so rather than a pane error. +func (m *Manager) sandboxFullLog(name, logDir string) (string, error) { + id, content, err := latestKickLog(logDir) + if err != nil { + return "", fmt.Errorf("agent %s: reading archived sandbox run log: %w", name, err) + } + if id == "" { + return "", fmt.Errorf("agent %s runs sandboxed and has no archived run yet; the log appears here when its first kick ends", name) + } + return content, nil +} + // captureVisiblePaneForAgent captures only the visible pane (no scrollback). func (m *Manager) captureVisiblePaneForAgent(agent *AgentProcess) string { return m.terminalSession().CaptureVisiblePane(agent) diff --git a/src/crux/agent/sandbox_executor.go b/src/crux/agent/sandbox_executor.go index b088bc76fb..baac6017b0 100644 --- a/src/crux/agent/sandbox_executor.go +++ b/src/crux/agent/sandbox_executor.go @@ -31,6 +31,9 @@ const ( sandboxPRBodyRelPath = ".hive/pr-body.md" sandboxPRTitleMaxLen = 200 sandboxPRBodyMaxLen = 60000 + // sandboxWithheldReasonMax bounds the report summary quoted in a + // publish-gate refusal, which lands in the run record and the audit log. + sandboxWithheldReasonMax = 300 ) type sandboxCommandRunner interface { @@ -75,13 +78,21 @@ type configSnapshot struct { } type SandboxKickResult struct { - Workspace string - Branch string - BaseSHA string - HeadSHA string - CommitCount int - ReportPath string - Report *outputschema.AgentReport + Workspace string + Branch string + BaseSHA string + HeadSHA string + CommitCount int + ReportPath string + Report *outputschema.AgentReport + // ReportStatus is the agent's own verdict from the report's status field, + // read leniently so a report that fails schema validation can still say + // "failed" and keep its commits from being published. + ReportStatus outputschema.RunStatus + // PRWithheld names why commits the run left were NOT pushed or turned + // into a PR (the agent reported failure, or the sandbox exited non-zero). + // Empty when nothing was withheld. + PRWithheld string TranscriptPath string Sandbox sandbox.Result Broker *pushbroker.Result @@ -175,7 +186,7 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo } res.TranscriptPath = filepath.Join(workspace, sandboxTranscriptRelPath) _ = writeTranscript(res.TranscriptPath, sres) - res.ReportPath, res.Report = collectAgentReport(workspace) + res.ReportPath, res.Report, res.ReportStatus = collectAgentReport(workspace) headSHA, count, commitErr := e.commitsSince(ctx, workspace, baseSHA) res.HeadSHA, res.CommitCount = headSHA, count if runErr != nil { @@ -186,6 +197,20 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo res.Error = commitErr.Error() return res, commitErr } + // Publish gate: commits alone do not earn a PR. The agent's own verdict + // (report status failed/blocked) or a non-zero sandbox exit means the + // work was not proven, and a PR that only fails CI is worse than none. + if reason := sandboxPublishWithheld(res); reason != "" { + if count > 0 { + res.PRWithheld = reason + err := fmt.Errorf("%w: %s; %d commit(s) not pushed, no PR opened", ErrSandboxRunReportedFailure, reason, count) + res.Error = err.Error() + return res, err + } + err := fmt.Errorf("%w: %s", ErrSandboxRunReportedFailure, reason) + res.Error = err.Error() + return res, err + } if count == 0 { return res, nil } @@ -532,7 +557,34 @@ func writeTranscript(path string, res sandbox.Result) error { return os.WriteFile(path, []byte(b.String()), 0o660) } -func collectAgentReport(workspace string) (string, *outputschema.AgentReport) { +// ErrSandboxRunReportedFailure wraps every publish-gate refusal so callers +// can tell "the agent said its run failed" from an infrastructure error. +var ErrSandboxRunReportedFailure = errors.New("sandbox run reported failure") + +// sandboxPublishWithheld returns why the run's commits must not be published, +// or "" when the gate is open. The order matters for the message an operator +// reads: the agent's own words first, the exit code second. +func sandboxPublishWithheld(res SandboxKickResult) string { + if res.ReportStatus.PublishBlocked() { + reason := "agent report status=" + string(res.ReportStatus) + if res.Report != nil && strings.TrimSpace(res.Report.Summary) != "" { + reason += ": " + truncateRunes(strings.TrimSpace(res.Report.Summary), sandboxWithheldReasonMax) + } + return reason + } + if res.Sandbox.ExitCode != 0 { + return fmt.Sprintf("sandbox exited %d", res.Sandbox.ExitCode) + } + return "" +} + +// collectAgentReport finds the agent's report in the workspace. The third +// value is the report's status: from the validated report when it parses, +// else read straight from the JSON so an agent that wrote +// {"status":"failed"} with an otherwise malformed report still stops the +// publish gate β€” the one field that must survive a sloppy report is the one +// that says "do not ship this". +func collectAgentReport(workspace string) (string, *outputschema.AgentReport, outputschema.RunStatus) { candidates := []string{ filepath.Join(workspace, ".hive", "agent-report.json"), filepath.Join(workspace, "agent-report.json"), @@ -555,14 +607,16 @@ func collectAgentReport(workspace string) (string, *outputschema.AgentReport) { } report, err := outputschema.Validate(data) if err == nil { - return path, report + return path, report, report.Status + } + var raw struct { + Status string `json:"status"` } - var raw map[string]any if json.Unmarshal(data, &raw) == nil { - return path, nil + return path, nil, outputschema.RunStatus(strings.ToLower(strings.TrimSpace(raw.Status))) } } - return "", nil + return "", nil, "" } func (e *SandboxExecutor) launcher() sandbox.Launcher { diff --git a/src/crux/agent/sandbox_executor_paths_test.go b/src/crux/agent/sandbox_executor_paths_test.go index 0c9045fe4a..9018eb1acf 100644 --- a/src/crux/agent/sandbox_executor_paths_test.go +++ b/src/crux/agent/sandbox_executor_paths_test.go @@ -344,7 +344,7 @@ func TestCollectAgentReport_FindsPrefixedReportFile(t *testing.T) { if err := os.WriteFile(path, []byte(valid), 0o660); err != nil { t.Fatal(err) } - gotPath, report := collectAgentReport(ws) + gotPath, report, _ := collectAgentReport(ws) if gotPath != path { t.Errorf("report path = %q, want %q", gotPath, path) } @@ -364,7 +364,7 @@ func TestCollectAgentReport_InvalidSchemaStillReturnsPath(t *testing.T) { if err := os.WriteFile(filepath.Join(dir, "agent-report.json"), []byte(`{"unexpected":"shape"}`), 0o660); err != nil { t.Fatal(err) } - gotPath, report := collectAgentReport(ws) + gotPath, report, _ := collectAgentReport(ws) if gotPath == "" { t.Error("path must be surfaced for a JSON report that fails schema validation") } @@ -374,7 +374,7 @@ func TestCollectAgentReport_InvalidSchemaStillReturnsPath(t *testing.T) { } func TestCollectAgentReport_NothingFound(t *testing.T) { - gotPath, report := collectAgentReport(t.TempDir()) + gotPath, report, _ := collectAgentReport(t.TempDir()) if gotPath != "" || report != nil { t.Errorf("empty workspace: got (%q, %+v), want (\"\", nil)", gotPath, report) } diff --git a/src/crux/agent/sandbox_executor_test.go b/src/crux/agent/sandbox_executor_test.go index aa271f4496..00e18c5547 100644 --- a/src/crux/agent/sandbox_executor_test.go +++ b/src/crux/agent/sandbox_executor_test.go @@ -3,6 +3,7 @@ package agent import ( "context" "errors" + "fmt" "log/slog" "os" "path/filepath" @@ -13,6 +14,7 @@ import ( "github.com/hivecommons/hive/crux" "github.com/hivecommons/hive/crux/config" + "github.com/hivecommons/hive/crux/outputschema" "github.com/hivecommons/hive/crux/pushbroker" "github.com/hivecommons/hive/crux/sandbox" ) @@ -456,3 +458,101 @@ func quietTestLogger() *slog.Logger { } var _ pushbroker.TokenMinter = sandboxFakeMinter{} + +// sandboxReportLauncher lets a test pick the agent-report.json the sandbox +// leaves behind and the exit code the launcher reports, so the publish gate +// can be exercised on every input it reads. +type sandboxReportLauncher struct { + report string + exitCode int +} + +func (l sandboxReportLauncher) Run(_ context.Context, spec sandbox.LaunchSpec) (sandbox.Result, error) { + if l.report != "" { + dir := filepath.Join(spec.Workspace, ".hive") + if err := os.MkdirAll(dir, 0o770); err != nil { + return sandbox.Result{}, err + } + if err := os.WriteFile(filepath.Join(dir, "agent-report.json"), []byte(l.report), 0o660); err != nil { + return sandbox.Result{}, err + } + } + return sandbox.Result{Stdout: "done\n", ExitCode: l.exitCode}, nil +} + +// A commit is not a success signal. When the agent's report says the run +// failed (or the sandbox exited non-zero) hive must keep the branch local +// and open no PR β€” the a1 deps run that opened a PR after its RPM install +// 401'd is the case this guards against. +func TestSandboxExecutorPublishGate(t *testing.T) { + const okReport = `{"lane":"deps","kind":"fix","findings":[],"prs_opened":[],"beads_filed":[],"summary":"bumped"%s}` + tests := []struct { + name string + report string + exitCode int + commits int + wantPR bool + wantReason string + wantStatus outputschema.RunStatus + }{ + {name: "success status publishes", report: fmt.Sprintf(okReport, `,"status":"success"`), commits: 1, wantPR: true, wantStatus: outputschema.StatusSuccess}, + {name: "unspecified status publishes", report: fmt.Sprintf(okReport, ""), commits: 1, wantPR: true}, + {name: "no report publishes", commits: 1, wantPR: true}, + {name: "failed status withholds", report: fmt.Sprintf(okReport, `,"status":"failed"`), commits: 1, wantReason: "agent report status=failed: bumped", wantStatus: outputschema.StatusFailed}, + {name: "blocked status withholds", report: fmt.Sprintf(okReport, `,"status":"blocked"`), commits: 1, wantReason: "agent report status=blocked", wantStatus: outputschema.StatusBlocked}, + {name: "malformed report still carries status", report: `{"status":"failed","summary":"rpm install 401"}`, commits: 1, wantReason: "agent report status=failed", wantStatus: outputschema.StatusFailed}, + {name: "non-zero exit withholds", report: fmt.Sprintf(okReport, ""), exitCode: 3, commits: 1, wantReason: "sandbox exited 3"}, + {name: "failed status without commits reports failure", report: fmt.Sprintf(okReport, `,"status":"failed"`), wantReason: "agent report status=failed", wantStatus: outputschema.StatusFailed}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + runner := &sandboxFakeRunner{commits: tt.commits} + pr := &sandboxFakePR{} + exec := &SandboxExecutor{ + Runner: runner, + Launcher: sandboxReportLauncher{report: tt.report, exitCode: tt.exitCode}, + PRClient: pr, + PushEnabled: true, + Minter: sandboxFakeMinter{}, + } + res, err := exec.Run(context.Background(), SandboxKickSpec{ + Agent: "deps", AgentConfig: configSnapshot{Backend: "claude"}, Message: "bump", + Org: "kubestellar", Repo: "hive", WorkspaceDir: t.TempDir(), Image: "agent-image", + }) + if res.ReportStatus != tt.wantStatus { + t.Errorf("ReportStatus = %q, want %q", res.ReportStatus, tt.wantStatus) + } + if tt.wantReason == "" { + if err != nil { + t.Fatalf("Run: %v", err) + } + if !runner.pushed || !pr.called { + t.Fatalf("pushed=%v pr.called=%v, want both true", runner.pushed, pr.called) + } + if res.PRWithheld != "" { + t.Fatalf("PRWithheld = %q, want empty", res.PRWithheld) + } + return + } + if !errors.Is(err, ErrSandboxRunReportedFailure) { + t.Fatalf("Run error = %v, want ErrSandboxRunReportedFailure", err) + } + if !strings.Contains(err.Error(), tt.wantReason) { + t.Errorf("error %q does not name reason %q", err, tt.wantReason) + } + if runner.pushed || pr.called { + t.Fatalf("pushed=%v pr.called=%v after a reported failure", runner.pushed, pr.called) + } + if tt.commits > 0 { + if !strings.Contains(res.PRWithheld, tt.wantReason) { + t.Errorf("PRWithheld = %q, want it to contain %q", res.PRWithheld, tt.wantReason) + } + if !strings.Contains(err.Error(), "1 commit(s) not pushed, no PR opened") { + t.Errorf("error %q should say the commits were not pushed", err) + } + } else if res.PRWithheld != "" { + t.Errorf("PRWithheld = %q with no commits, want empty", res.PRWithheld) + } + }) + } +} diff --git a/src/crux/agent/sandbox_kick_log.go b/src/crux/agent/sandbox_kick_log.go new file mode 100644 index 0000000000..08926f279b --- /dev/null +++ b/src/crux/agent/sandbox_kick_log.go @@ -0,0 +1,128 @@ +package agent + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "time" +) + +const ( + // kickLogSourceSandbox prefixes the source: header line of a sandboxed + // kick's archive β€” "sandbox-job" or "sandbox-podman" β€” so the kick + // history page shows what the body is: the container's stdout/stderr + // hive collected when the run ended, not a tmux capture. + kickLogSourceSandbox = "sandbox" + // kickLogReasonSandbox is the component of a sandboxed kick's + // archive filename. + kickLogReasonSandbox = "sandbox" +) + +// sandboxKickLogBody renders a sandboxed run's captured output as the body of +// a kick log: a short outcome block, then stdout and stderr verbatim. The +// transcript is already token-scrubbed by the executor; this adds no new +// text from the agent. +func sandboxKickLogBody(runtime string, res SandboxKickResult, runErr error) string { + var b strings.Builder + fmt.Fprintf(&b, "runtime: %s\nexit_code: %d\ncommits: %d\n", runtime, res.Sandbox.ExitCode, res.CommitCount) + if res.Branch != "" { + fmt.Fprintf(&b, "branch: %s\n", res.Branch) + } + if res.Workspace != "" { + fmt.Fprintf(&b, "workspace: %s\n", res.Workspace) + } + if res.PR != nil && res.PR.URL != "" { + fmt.Fprintf(&b, "pr: %s\n", res.PR.URL) + } + if res.PRWithheld != "" { + fmt.Fprintf(&b, "pr_withheld: %s\n", res.PRWithheld) + } + if res.ReportPath != "" { + fmt.Fprintf(&b, "agent_report: %s\n", res.ReportPath) + } + if res.ReportStatus != "" { + fmt.Fprintf(&b, "agent_report_status: %s\n", res.ReportStatus) + } + if res.TimedOut { + b.WriteString("timed_out: true\n") + } + if runErr != nil { + fmt.Fprintf(&b, "error: %s\n", runErr.Error()) + } + b.WriteString("\n---- stdout ----\n") + b.WriteString(res.Sandbox.Stdout) + if !strings.HasSuffix(res.Sandbox.Stdout, "\n") { + b.WriteString("\n") + } + b.WriteString("\n---- stderr ----\n") + b.WriteString(res.Sandbox.Stderr) + if !strings.HasSuffix(res.Sandbox.Stderr, "\n") { + b.WriteString("\n") + } + return b.String() +} + +// sandboxKickLogArchive is a sandboxed kick's log, built under m.mu (the +// header reads agent state) and written after the lock is released. +type sandboxKickLogArchive struct { + dir string + id string + content string +} + +// prepareSandboxKickLogLocked builds the archive for a sandboxed run so the +// caller can write it off-lock. The filename is keyed on the kick's own start +// time like every other archive, so the kick history page orders sandboxed +// and tmux kicks the same way. Returns nil when archiving is off. +// +// This is the only durable copy of a Job's output: the Job (and its pod log) +// is deleted on success or GC'd at its TTL, and the workspace transcript +// (.hive/sandbox-transcript.log) lives under the sandbox root the workspace +// sweep removes after two hours. Before this archive existed, "full log" on +// a job-runtime agent was a 404 as soon as either of those had happened. +func (m *Manager) prepareSandboxKickLogLocked(agent *AgentProcess, runtime string, started time.Time, res SandboxKickResult, runErr error) *sandboxKickLogArchive { + if m.kickLogRetention <= 0 || m.kickLogDir == "" { + return nil + } + body := sandboxKickLogBody(runtime, res, runErr) + source := kickLogSourceSandbox + if runtime != "" { + source += "-" + runtime + } + header := m.kickLogHeaderLocked(agent, "archived", time.Now(), kickLogReasonSandbox, source) + id := started.UTC().Format(kickLogTimestampFormat) + "-" + kickLogReasonSandbox + kickLogSuffix + return &sandboxKickLogArchive{dir: m.agentKickLogDir(agent.Name), id: id, content: header + body} +} + +// writeSandboxKickLog persists a prepared archive and prunes the directory. +// Callers must NOT hold m.mu; pruning reads the directory. +func (m *Manager) writeSandboxKickLog(name string, a *sandboxKickLogArchive) error { + if a == nil { + return nil + } + if err := os.MkdirAll(a.dir, kickLogDirMode); err != nil { + return err + } + if err := os.WriteFile(filepath.Join(a.dir, a.id), []byte(a.content), kickLogFileMode); err != nil { + return err + } + m.logger.Info("archived sandbox kick log", "name", name, "file", a.id, "bytes", len(a.content)) + m.pruneKickLogs(a.dir) + m.notifyKickObserver(name, KickObserverEventArchived, kickLogReasonSandbox) + return nil +} + +// latestKickLog returns the newest archive in dir and its id, or ("", "", +// nil) when the directory has none. +func latestKickLog(dir string) (id, content string, err error) { + infos, err := readKickLogDir(dir) + if err != nil || len(infos) == 0 { + return "", "", err + } + data, err := os.ReadFile(filepath.Join(dir, infos[0].ID)) + if err != nil { + return "", "", err + } + return infos[0].ID, string(data), nil +} diff --git a/src/crux/agent/sandbox_kick_log_test.go b/src/crux/agent/sandbox_kick_log_test.go new file mode 100644 index 0000000000..da308ac8a7 --- /dev/null +++ b/src/crux/agent/sandbox_kick_log_test.go @@ -0,0 +1,148 @@ +package agent + +import ( + "errors" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/crux/config" + "github.com/hivecommons/hive/crux/outputschema" + "github.com/hivecommons/hive/crux/sandbox" +) + +func TestSandboxKickLogBody(t *testing.T) { + res := SandboxKickResult{ + Branch: "hive/deps/20261027195251", + CommitCount: 1, + Workspace: "/data/agents/sandbox/deps-1", + ReportPath: "/data/agents/sandbox/deps-1/.hive/agent-report.json", + ReportStatus: outputschema.StatusFailed, + PRWithheld: "agent report status=failed: Artifactory 401", + Sandbox: sandbox.Result{Stdout: "installing rpms", Stderr: "401 Unauthorized\n", ExitCode: 0}, + } + body := sandboxKickLogBody("job", res, errors.New("sandbox run reported failure")) + for _, want := range []string{ + "runtime: job\n", "exit_code: 0\n", "commits: 1\n", + "branch: hive/deps/20261027195251\n", + "pr_withheld: agent report status=failed: Artifactory 401\n", + "agent_report_status: failed\n", + "error: sandbox run reported failure\n", + "---- stdout ----\ninstalling rpms\n", + "---- stderr ----\n401 Unauthorized\n", + } { + if !strings.Contains(body, want) { + t.Errorf("body missing %q:\n%s", want, body) + } + } + if strings.Contains(body, "pr:") && !strings.Contains(body, "pr_withheld") { + t.Errorf("no PR URL must be rendered when none was opened:\n%s", body) + } + if strings.Contains(sandboxKickLogBody("podman", SandboxKickResult{}, nil), "error:") { + t.Error("a clean run must not render an error line") + } +} + +// A job-runtime agent has no tmux pane, so "full log" must be the captured +// container output hive archived when the kick ended β€” the only durable copy +// once the Job is GC'd and the workspace swept. +func TestSandboxedKickArchivesLogAndServesFullLog(t *testing.T) { + yes := true + m, _, _ := jobRuntimeManager(t, + &config.AgentSandboxOverride{Enabled: &yes, Runtime: config.SandboxRuntimeJob}, + config.AgentSandboxConfig{Enabled: true, Image: "dev-image", + Job: &config.SandboxJobConfig{WorkspaceClaim: "hive-data"}}) + logDir := t.TempDir() + m.SetKickLogDir(logDir) + m.mu.Lock() + m.kickLogRetention = 3 + m.mu.Unlock() + + if _, err := m.CaptureFullLog("deps"); err == nil || !strings.Contains(err.Error(), "no archived run yet") { + t.Fatalf("CaptureFullLog before any kick: err = %v, want the no-archived-run explanation", err) + } + if hdr := m.FullLogHeader("deps"); hdr != "" { + t.Errorf("FullLogHeader for a sandboxed agent = %q, want empty (archive carries its own)", hdr) + } + + if err := m.SendKick("deps", "bump torch"); err != nil { + t.Fatalf("SendKick: %v", err) + } + waitIdle(t, m, "deps") + + // The archive is written after m.mu is released, so poll (ticker, not a + // fixed sleep: the sleep ratchet forbids new time.Sleep calls in tests). + var run *crux.SandboxRun + waitFor(t, "sandbox kick log archive", func() bool { + run = m.AllStatuses()["deps"].SandboxLastRun + if run == nil || run.LogID == "" { + return false + } + _, err := os.Stat(filepath.Join(logDir, "deps", run.LogID)) + return err == nil + }) + if !strings.HasSuffix(run.LogID, "-sandbox.log") { + t.Errorf("LogID = %q, want the -sandbox.log suffix", run.LogID) + } + + got, err := m.CaptureFullLog("deps") + if err != nil { + t.Fatalf("CaptureFullLog after kick: %v", err) + } + for _, want := range []string{"==== hive kick log ====", "agent: deps", "source: sandbox-job", "reason: sandbox", "---- stdout ----\nok\n"} { + if !strings.Contains(got, want) { + t.Errorf("full log missing %q:\n%s", want, got) + } + } + + infos, err := m.ListKickLogs("deps") + if err != nil || len(infos) != 1 || infos[0].ID != run.LogID { + t.Fatalf("ListKickLogs = %+v, %v; want exactly the archived run %q", infos, err, run.LogID) + } + if content, err := m.ReadKickLog("deps", run.LogID); err != nil || content != got { + t.Errorf("ReadKickLog must serve the same archive as the full log (err=%v, same=%v)", err, content == got) + } +} + +func TestSandboxedKickSkipsArchiveWhenRetentionOff(t *testing.T) { + yes := true + m, _, _ := jobRuntimeManager(t, + &config.AgentSandboxOverride{Enabled: &yes, Runtime: config.SandboxRuntimeJob}, + config.AgentSandboxConfig{Enabled: true, Image: "dev-image", + Job: &config.SandboxJobConfig{WorkspaceClaim: "hive-data"}}) + logDir := t.TempDir() + m.SetKickLogDir(logDir) + m.mu.Lock() + m.kickLogRetention = 0 + m.mu.Unlock() + if err := m.SendKick("deps", "bump"); err != nil { + t.Fatalf("SendKick: %v", err) + } + waitIdle(t, m, "deps") + if run := m.AllStatuses()["deps"].SandboxLastRun; run == nil || run.LogID != "" { + t.Fatalf("retention 0 must record the run without an archive id, got %+v", run) + } + if entries, _ := os.ReadDir(filepath.Join(logDir, "deps")); len(entries) != 0 { + t.Errorf("retention 0 must write nothing, found %d files", len(entries)) + } +} + +func TestBuildSandboxRunCarriesPRWithheld(t *testing.T) { + started := time.Date(2026, 10, 27, 19, 52, 51, 0, time.UTC) + res := SandboxKickResult{ + CommitCount: 1, + Branch: "hive/deps/20261027195251", + PRWithheld: "agent report status=failed: " + strings.Repeat("x", sandboxRunSummaryMax*2), + Report: &outputschema.AgentReport{Summary: "rpm install failed", Status: outputschema.StatusFailed}, + } + run := buildSandboxRun("job", started, started.Add(time.Minute), res, errors.New("sandbox run reported failure")) + if run.Succeeded || run.PRURL != "" { + t.Fatalf("withheld run must not look successful: %+v", run) + } + if !strings.HasPrefix(run.PRWithheld, "agent report status=failed: ") || len([]rune(run.PRWithheld)) > sandboxRunSummaryMax { + t.Errorf("PRWithheld = %q (len %d), want the bounded reason", run.PRWithheld, len([]rune(run.PRWithheld))) + } +} diff --git a/src/crux/agent/sandbox_run.go b/src/crux/agent/sandbox_run.go index 369371841a..a3d4db9892 100644 --- a/src/crux/agent/sandbox_run.go +++ b/src/crux/agent/sandbox_run.go @@ -64,6 +64,7 @@ func buildSandboxRun(runtime string, started, ended time.Time, res SandboxKickRe if res.PR != nil { run.PRURL = res.PR.URL } + run.PRWithheld = truncateRunes(res.PRWithheld, sandboxRunSummaryMax) if res.Report != nil && strings.TrimSpace(res.Report.Summary) != "" { run.Summary = truncateRunes(strings.TrimSpace(res.Report.Summary), sandboxRunSummaryMax) } else { diff --git a/src/crux/outputschema/outputschema.go b/src/crux/outputschema/outputschema.go index afdaaf666b..d7c04e0a9d 100644 --- a/src/crux/outputschema/outputschema.go +++ b/src/crux/outputschema/outputschema.go @@ -78,9 +78,29 @@ const ( BeadTypeFeature BeadType = "feature" ) +// RunStatus is the agent's own verdict on its sandboxed run. A sandboxed +// agent cannot push or open a PR; hive does that from whatever commits the +// run left behind. Status is how the agent tells hive that those commits are +// NOT fit to publish β€” the build it was asked to prove did not run, a smoke +// test failed β€” so hive withholds the push instead of opening a PR that only +// fails CI (hivecommons/hive: deps on a1-vllm-spyre, Artifactory 401). +type RunStatus string + +const ( + // StatusSuccess: the work is done and verified; commits may be published. + StatusSuccess RunStatus = "success" + // StatusFailed: the work could not be completed or verified; hive must + // not push or open a PR from this run. + StatusFailed RunStatus = "failed" + // StatusBlocked: the agent stopped on an external blocker (credentials, + // hardware, missing access); treated like failed for publishing. + StatusBlocked RunStatus = "blocked" +) + type AgentReport struct { Lane string `json:"lane"` Kind ReportKind `json:"kind"` + Status RunStatus `json:"status,omitempty"` Findings []Finding `json:"findings"` PRsOpened []PROpened `json:"prs_opened"` BeadsFiled []BeadFiled `json:"beads_filed"` @@ -89,6 +109,13 @@ type AgentReport struct { Receipt *StageReceipt `json:"stage_receipt,omitempty"` } +// PublishBlocked reports whether the status forbids publishing the run's +// commits. An empty status is "unspecified" and does not block, so reports +// written before the field existed keep their behaviour. +func (s RunStatus) PublishBlocked() bool { + return s == StatusFailed || s == StatusBlocked +} + // Artifact records a repository file produced or materially updated by an agent. // It lets artifact-producing lanes report their primary output independently of // the pull request that happens to carry it. @@ -204,6 +231,9 @@ func validateReport(report AgentReport) []Violation { if report.Kind != "" && !validKind(report.Kind) { violations = append(violations, Violation{Field: "kind", Message: "must be one of findings, fix, review, advisory, summary, instrument, stage_receipt"}) } + if report.Status != "" && !validStatus(report.Status) { + violations = append(violations, Violation{Field: "status", Message: "must be one of success, failed, blocked"}) + } if report.Findings == nil { violations = append(violations, Violation{Field: "findings", Message: "is required; use [] when there are no findings"}) } else if len(report.Findings) > MaxFindings { @@ -421,6 +451,15 @@ func validKind(kind ReportKind) bool { } } +func validStatus(status RunStatus) bool { + switch status { + case StatusSuccess, StatusFailed, StatusBlocked: + return true + default: + return false + } +} + func validSeverity(severity Severity) bool { switch severity { case SeverityInfo, SeverityLow, SeverityMedium, SeverityHigh, SeverityCritical: diff --git a/src/crux/outputschema/outputschema_test.go b/src/crux/outputschema/outputschema_test.go index 743fda571c..6feb3246e6 100644 --- a/src/crux/outputschema/outputschema_test.go +++ b/src/crux/outputschema/outputschema_test.go @@ -3,6 +3,7 @@ package outputschema import ( "encoding/json" "errors" + "fmt" "os" "strings" "testing" @@ -363,3 +364,38 @@ func countViolations(err *ValidationError, field string) int { } return count } + +// status is the agent's own verdict on a sandboxed run; hive's publish gate +// reads it, so the accepted set must be exact and the empty value must stay +// "unspecified" for reports written before the field existed. +func TestValidateRunStatus(t *testing.T) { + base := `{"lane":"deps","kind":"fix","findings":[],"prs_opened":[],"beads_filed":[],"summary":"s"%s}` + for _, tt := range []struct { + name string + status string + wantErr bool + wantGate bool + }{ + {name: "absent is unspecified", status: ""}, + {name: "success", status: `,"status":"success"`}, + {name: "failed blocks publish", status: `,"status":"failed"`, wantGate: true}, + {name: "blocked blocks publish", status: `,"status":"blocked"`, wantGate: true}, + {name: "unknown value rejected", status: `,"status":"meh"`, wantErr: true}, + } { + t.Run(tt.name, func(t *testing.T) { + report, err := Validate([]byte(fmt.Sprintf(base, tt.status))) + if tt.wantErr { + if err == nil || !strings.Contains(err.Error(), "status: must be one of success, failed, blocked") { + t.Fatalf("Validate() error = %v, want status violation", err) + } + return + } + if err != nil { + t.Fatalf("Validate() error = %v", err) + } + if got := report.Status.PublishBlocked(); got != tt.wantGate { + t.Errorf("PublishBlocked() = %v, want %v for %q", got, tt.wantGate, report.Status) + } + }) + } +} diff --git a/src/crux/process_types.go b/src/crux/process_types.go index b169df3f56..db4d8229e2 100644 --- a/src/crux/process_types.go +++ b/src/crux/process_types.go @@ -149,6 +149,14 @@ type SandboxRun struct { CommitCount int `json:"commitCount,omitempty"` Branch string `json:"branch,omitempty"` PRURL string `json:"prURL,omitempty"` + // PRWithheld is why the run's commits were NOT pushed or opened as a PR: + // the agent's report said failed/blocked, or the sandbox exited non-zero. + // Empty when nothing was withheld. + PRWithheld string `json:"prWithheld,omitempty"` + // LogID is the per-kick log archive (kick-log id under the agent's + // archive directory) holding the run's captured stdout/stderr β€” for a + // Job runtime, the pod log hive collected before the Job was deleted. + LogID string `json:"logId,omitempty"` // Summary is the agent's own report summary when it left one, else the // scrubbed tail of the transcript. Bounded; see sandboxRunSummaryMax. Summary string `json:"summary,omitempty"` diff --git a/src/docs/agent-logging.md b/src/docs/agent-logging.md index a1b748ee1e..9bb572e92e 100644 --- a/src/docs/agent-logging.md +++ b/src/docs/agent-logging.md @@ -279,8 +279,20 @@ read the logs of the last several runs β€” not just the latest β€” `GET /api/agents/{name}/kicks/{id}` (`text/plain`; `?download=1` for an attachment). Same auth rule as the full-log endpoint (any authenticated role), and archived content passes through the same token redaction. -- **Limitations.** Sandbox-executor kicks have no tmux session and are not - archived here. The archive holds at most the retained scrollback +- **Sandboxed agents.** A sandboxed kick (`sandbox.enabled: true`, Podman or + `runtime: job`) has no tmux session. When the run ends hive archives the + container's captured stdout/stderr here instead, as + `-sandbox.log` with `source: sandbox-podman` or + `source: sandbox-job` and an outcome block (exit code, commits, branch, + PR or `pr_withheld` reason, agent-report path and status, error) ahead of + the output. For these agents the `πŸ“„ full log` / `πŸ“‹ log text` controls and + `GET /api/agents/{name}/log` serve the newest such archive β€” the only + durable copy, since a completed Job is deleted at once and the sandbox + workspace is swept after two hours β€” and an agent that has not run yet + gets a plain "no archived run yet" message rather than a pane error. + `HIVE_KICK_LOG_RETENTION=0` disables these archives too, and the card + then has no log to show. +- **Limitations.** The archive holds at most the retained scrollback (`history-limit`, 50000 lines by default), so an extremely long run self-truncates from the top. A hard kill (SIGKILL, node loss) skips the shutdown snapshot; the previous kicks' archives on `/data` are unaffected. diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 0adbbe3048..7ae4480db8 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -626,6 +626,10 @@ More about the line: **What it is good for.** Use it to pause a worker, restart a stuck one, or change its settings. Fix any card that says it needs a login. Read the **Now:** lines to see what each worker is on without opening its log. +**The log buttons.** `πŸ“„ full log` opens the worker's whole log and `πŸ“‹ log text` copies it; `πŸ•˜ past kicks` lists the logs of earlier rounds. For a normal worker this is the terminal it is typing in. For a worker that runs each round in a sandbox or a Kubernetes Job (its card says so), there is no terminal: the buttons show the output Hive captured from the sandbox when the last round ended, and `past kicks` holds the earlier rounds. Before such a worker's first round has ended the button says there is no archived run yet. + +**Sandboxed worker's last run.** A sandboxed worker's card also carries a **last run** line: when it ran, how long it took, whether it succeeded, and the pull request it opened. If the worker's own report said the run failed, or the sandbox exited with an error, Hive keeps any commits the run made off GitHub and the line reads `no PR opened: ` β€” for example `no PR opened: agent report status=failed: RPM install returned 401`. The full reason is in the line's tooltip and in the full log's outcome block. + **Example.** A card says "needs login". You click **Login** and sign in again. Another card says `Now: working β€” no issue or pull request yet Β· started 50 min ago`. That worker has changed nothing through the hive for 50 minutes, so you open its log to see what it is doing. **When it appears.** Always. diff --git a/src/docs/sandbox-isolation.md b/src/docs/sandbox-isolation.md index 384cb5ebe2..5e8083d26a 100644 --- a/src/docs/sandbox-isolation.md +++ b/src/docs/sandbox-isolation.md @@ -127,7 +127,8 @@ For sandboxed agents, a kick now follows this path: 1. Hive prepares a per-kick host workspace under the sandbox workspace root by cloning/fetching the primary repo with hive-owned credentials before the sandbox starts. The clone URL and sandbox environment are sanitized so GitHub/token variables are not carried into the workspace or container. 2. Hive writes the kick prompt to `.hive/kick-prompt.txt` in the workspace and launches `pkg/sandbox.Launcher` with a rootless Podman `LaunchSpec`, workspace mount, explicit env allowlist, and configured network mode. 3. The manager marks the agent busy while the sandbox runs and returns it to idle or failed when the timeout/completion path finishes. Because the agent sits in `idle` between kicks, dashboard health for sandboxed agents comes from the last-run record rather than the session check; see [Health and metrics](#health-and-metrics-for-sandboxed-agents). -4. Hive collects the transcript at `.hive/sandbox-transcript.log` and any `agent-report*.json` artifact following `pkg/outputschema` conventions. +4. Hive collects the transcript at `.hive/sandbox-transcript.log` and any `agent-report*.json` artifact following `pkg/outputschema` conventions. The container's captured stdout/stderr is also archived as the kick's log under the kick-log directory (`//-sandbox.log`, same retention as tmux kick logs β€” see [Agent logging](agent-logging.md#per-kick-log-history)). That archive, not the workspace transcript, is what the card's "full log" / "log text" buttons and the πŸ•˜ kick history serve for a sandboxed agent, because the workspace is swept after two hours and a Job's pod is gone as soon as it completes. + **Publish gate.** Commits alone do not earn a PR. If the agent's report carries `"status": "failed"` or `"status": "blocked"`, or the sandbox exited non-zero, hive leaves the branch local, opens no PR, records the run as failed with the reason in `prWithheld` (shown on the card as "no PR opened: …"), and writes a `sandbox_pr_withheld` audit line naming the workspace, commit count and branch. The `status` field is optional; a report without one, or no report at all, does not block publishing, so agents that predate the field behave as before. Kick templates for sandboxed agents whose work must build or pass a smoke test before it is worth a PR should tell the agent to write `"status": "failed"` (with the failure in `summary`) instead of committing when that step fails. 5. If the sandbox produced commits, `pkg/pushbroker.Broker` scans the committed diff for token-like secrets and protected-path edits, mints a short-lived scoped GitHub App token outside the sandbox, pushes the branch, and opens a PR through the existing App-authored GitHub client. Broker rejection records audit detail and nothing is pushed. The PR title and body come from `.hive/pr-title.txt` (first line) and `.hive/pr-body.md` if the agent wrote them in the workspace, otherwise from the HEAD commit's subject and body; a generic `[] sandbox changes` placeholder is used only when neither exists. Kick templates for sandboxed agents should ask the agent to write those two files. @@ -166,7 +167,7 @@ agents: **The credential boundary.** The Job receives exactly the Secrets named in `env_from_secrets`, resolved by Kubernetes from the pod template; hive never reads them. Environment variables from the allowlist are copied in with the same credential-name filter Podman applies. An allowlisted `ANTHROPIC_BASE_URL` that points at a pod-local proxy (such as `127.0.0.1:18444`) is not reachable from a Job pod, so operators must supply a routable endpoint or model credentials via `env_from_secrets`. Hive's brokered GitHub token **never enters the Job**, so a Job cannot push or open a PR itself; hive does both from the returned workspace, with the same scan and scoped-token minting as the Podman path. If a workload needs its own registry or model credentials, they go in a Secret named here, and they are that workload's to protect. -**What the operator sees.** The kick shows on the agent card like any sandboxed kick, with the runtime named in the output buffer and the audit line. The pod log is the transcript, the container exit code is the sandbox exit code, and the kick timeout deletes a Job that is still running. A Job that **failed** is left in place until `ttl_seconds` (default one hour) so `kubectl describe job` and `kubectl logs` still work; a completed Job is deleted at once. Jobs carry the label `app.kubernetes.io/managed-by=hive-kubejob`. +**What the operator sees.** The kick shows on the agent card like any sandboxed kick, with the runtime named in the output buffer and the audit line. The pod log is the transcript β€” hive collects it before the Job is deleted and archives it as the kick log, so "full log" on the card and the kick history keep working after the Job is gone β€” the container exit code is the sandbox exit code, and the kick timeout deletes a Job that is still running. A Job that **failed** is left in place until `ttl_seconds` (default one hour) so `kubectl describe job` and `kubectl logs` still work; a completed Job is deleted at once. Jobs carry the label `app.kubernetes.io/managed-by=hive-kubejob`. **RBAC.** The spoke's ServiceAccount needs a Role for `batch/jobs` (create, get, list, delete) and `pods` plus `pods/log` (get, list). The standalone kustomize base ships it as `sandbox-job-rbac.yaml`, and the hub provisioner grants it to every hosted spoke as `hive-sandbox-jobs`. @@ -176,7 +177,7 @@ Hive warns at boot and on reload (`config.AgentSandboxGateWarnings`) when a job- A sandboxed agent (Podman or Job runtime) has no tmux session, no in-pod process and no `pluk` log between kicks: the manager parks it in the `idle` state and each kick runs to completion in a container or Job. The dashboard therefore must not read "not running" as "session down" for these agents, and the token collector cannot find their CLI session files because the CLI's home directory lives in the sandbox, not in the hive pod. -**Last-run record.** When a sandboxed kick ends, `crux/agent` builds a `SandboxRun` (runtime, start/end, succeeded, timed out, error, commit count, branch, PR URL, summary, usage) and keeps it on the agent process as `sandboxLastRun`. It is also written to `/sandbox-runs/.json` (restored on restart, so a pod roll does not blank the card) and appended to `/sandbox-runs/runs.jsonl`, which is compacted to its newest 1000 records once it passes 2 MiB (`work_dir` is `HIVE_WORK_DIR`, default `/data/agents`). The summary is the agent's `.hive/agent-report.json` `summary` when it left one, otherwise the scrubbed tail of the transcript. +**Last-run record.** When a sandboxed kick ends, `crux/agent` builds a `SandboxRun` (runtime, start/end, succeeded, timed out, error, commit count, branch, PR URL, `prWithheld` when the publish gate kept commits local, `logId` of the archived kick log, summary, usage) and keeps it on the agent process as `sandboxLastRun`. It is also written to `/sandbox-runs/.json` (restored on restart, so a pod roll does not blank the card) and appended to `/sandbox-runs/runs.jsonl`, which is compacted to its newest 1000 records once it passes 2 MiB (`work_dir` is `HIVE_WORK_DIR`, default `/data/agents`). The summary is the agent's `.hive/agent-report.json` `summary` when it left one, otherwise the scrubbed tail of the transcript. **Health.** `/api/status` carries a `sandboxRun` object on every sandboxed agent with a `health` verdict the SPA uses instead of the session check: diff --git a/src/pkg/dashboard/sandbox_health.go b/src/pkg/dashboard/sandbox_health.go index f51cd1c629..b49fd23285 100644 --- a/src/pkg/dashboard/sandbox_health.go +++ b/src/pkg/dashboard/sandbox_health.go @@ -55,11 +55,17 @@ type FrontendSandboxRun struct { CommitCount int `json:"commitCount,omitempty"` Branch string `json:"branch,omitempty"` PRURL string `json:"prURL,omitempty"` + // PRWithheld is why commits from the last run were not published (agent + // reported failure / non-zero exit); empty when nothing was withheld. + PRWithheld string `json:"prWithheld,omitempty"` + // LogID is the kick-log archive id of the last run's captured output + // (/api/agents/{name}/kick-logs/{id}); empty when none was archived. + LogID string `json:"logId,omitempty"` // Summary is the agent's report summary or the transcript tail. Summary string `json:"summary,omitempty"` // Tokens is the usage the run's transcript reported; 0 when it carried // none (the collector then has no session for this run either). - Tokens int64 `json:"tokens,omitempty"` + Tokens int64 `json:"tokens,omitempty"` Model string `json:"model,omitempty"` // DueAt is the scheduled occurrence that has been missed when Health is // overdue; empty otherwise. @@ -111,6 +117,8 @@ func sandboxRunHealth(proc *crux.Process, cadence config.Cadence, onDemand bool, out.CommitCount = run.CommitCount out.Branch = run.Branch out.PRURL = run.PRURL + out.PRWithheld = redactTokens(run.PRWithheld) + out.LogID = run.LogID out.Summary = redactTokens(run.Summary) out.Tokens = run.Usage.Total() out.Model = run.Usage.Model diff --git a/src/pkg/dashboard/sandbox_health_test.go b/src/pkg/dashboard/sandbox_health_test.go index 5c7ed90bcc..757cf55df6 100644 --- a/src/pkg/dashboard/sandbox_health_test.go +++ b/src/pkg/dashboard/sandbox_health_test.go @@ -56,6 +56,16 @@ func TestSandboxRunHealth(t *testing.T) { if got.Runtime != "job" || !got.Succeeded || got.DurationS != 29*60 || got.PRURL != ok.PRURL || got.Tokens != 15 || got.Model != "m" { t.Errorf("run fields not carried over: %+v", got) } + + withheld := &crux.SandboxRun{Runtime: "job", StartedAt: now.Add(-40 * time.Minute), EndedAt: now.Add(-11 * time.Minute), + Error: "sandbox run reported failure", PRWithheld: "agent report status=failed: rpm install 401 token ghp_abcdefghijklmnopqrstuvwxyz0123456789", LogID: "20261027-195251.000-sandbox.log"} + got = sandboxRunHealth(&crux.Process{State: crux.StateIdle, LastKick: kick(40 * time.Minute), SandboxLastRun: withheld}, "1h", false, now) + if got.Health != SandboxHealthFailed || got.LogID != withheld.LogID { + t.Errorf("withheld run must be failed and name its archive: %+v", got) + } + if !strings.HasPrefix(got.PRWithheld, "agent report status=failed: rpm install 401") || strings.Contains(got.PRWithheld, "ghp_abcdefghijklmnopqrstuvwxyz0123456789") { + t.Errorf("prWithheld must be carried over with tokens redacted, got %q", got.PRWithheld) + } } func TestSandboxOverdueGraceClamped(t *testing.T) { diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 1b8bea59cb..2e9e9bda96 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -10892,8 +10892,8 @@

Where to get help

: ``} ${agentPowerSwitchHtml(a)} ${sessionChip(a, 'β–Ά terminal', `β–Ά terminal`)} - ${sessionChip(a, 'πŸ“„ full log', `πŸ“„ full log`)} - ${sessionChip(a, 'πŸ“‹ log text', `πŸ“‹ log text`)} + ${sessionChip(a, 'πŸ“„ full log', `πŸ“„ full log`)} + ${sessionChip(a, 'πŸ“‹ log text', `πŸ“‹ log text`)} πŸ•˜ past kicks @@ -17842,6 +17842,16 @@

All labels

// explicit API opt-in for external clients. Dashboard navigations never // carry the shared token in the URL; token-secured spokes use // authenticated fetches for API calls instead. + /* A sandboxed agent (Podman or runtime: job) has no tmux pane: its full + log is the container/Job stdout+stderr hive archived when the last run + ended, so the tooltip says what the reader will get. */ + function fullLogTitle(a, paneTitle) { + if (!a || !a.sandboxed) return paneTitle; + const rt = (a.sandboxRun && a.sandboxRun.runtime === 'job') ? 'Job' : 'sandbox container'; + const when = (a.sandboxRun && a.sandboxRun.lastRunAt) ? ' (last run ' + a.sandboxRun.lastRunAt + ')' : ''; + return 'Captured stdout/stderr of the last sandboxed run β€” the ' + rt + ' output hive archived when the run ended' + when + '. Earlier runs: πŸ•˜ kick history.'; + } + function fullLogUrl(agentName, download) { const proto = window.location.protocol; const host = window.location.host; @@ -18016,7 +18026,11 @@

All labels

if (!text) return ''; let html = esc(text); if (sr.prURL) html += ' \u00b7 PR'; - if (sr.error && !sr.succeeded) html += ' \u2014 ' + esc(sr.error.length > 160 ? sr.error.slice(0, 160) + '\u2026' : sr.error); + if (sr.prWithheld) { + html += ' \u00b7 no PR opened: ' + esc(sr.prWithheld.length > 120 ? sr.prWithheld.slice(0, 120) + '\u2026' : sr.prWithheld) + ''; + } else if (sr.error && !sr.succeeded) { + html += ' \u2014 ' + esc(sr.error.length > 160 ? sr.error.slice(0, 160) + '\u2026' : sr.error); + } return html; } function agentBlockerLineHtml(a) { @@ -18181,7 +18195,7 @@

All labels

const agentSpanHandle = ``; const disabledEnableBtn = isDisabled ? `` : ''; const historyLink = `πŸ•˜ past kicks`; - const liveSessionActions = `${sessionChip(a, 'β–Ά terminal', `β–Ά terminal`)} ${sessionChip(a, 'πŸ“„ full log', `πŸ“„ full log`)} ${historyLink}`; + const liveSessionActions = `${sessionChip(a, 'β–Ά terminal', `β–Ά terminal`)} ${sessionChip(a, 'πŸ“„ full log', `πŸ“„ full log`)} ${historyLink}`; const sessionActions = isPoweredOff ? `${historyLink}` : isDisabled