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-14746-repeat-failure-copilot-omp-agy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **The repeated-failure check now sees commands run by copilot, omp and headless agy agents** ([#14746](https://github.com/hivecommons/hive/issues/14746), [#14747](https://github.com/hivecommons/hive/issues/14747), [#14750](https://github.com/hivecommons/hive/issues/14750), part of [#14550](https://github.com/hivecommons/hive/issues/14550)). These backends run shell commands where the neutral `BASH_ENV` route never reaches them, so an agent retrying the same failing command went unnoticed. With `repeat_failure.enabled` on, Hive now reads Copilot's `events.jsonl` session log (one record per shell `tool.execution_complete`, with the real exit code), loads a Hive-owned omp extension via its own `--extension` flag (one record per bash `tool_result`, fail-open), and has `hive agy-turn` record each finished `run_command` step. Nothing is written into the backends' own settings; with the check off Hive passes no flag and removes the omp extension at the next launch. copilot and omp are marked covered in the "Repeated-failure check" table of `docs/backend-support-tiers.md` with committed, re-runnable harness evidence; agy stays "not covered" there until a real-run harness can log in.
6 changes: 3 additions & 3 deletions src/crux/agent/agy_launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestAgyLaunchCommandLine_DerivesEffortFromModelSuffix(t *testing.T) {
}

func TestAgyHeadlessTurnCommandRunsTurnRunner(t *testing.T) {
cmd := agyHeadlessTurnShellCommand("/usr/local/bin/hive", "agy", "gemini-pro", "high", "/data/agents/a/.hive-agy-prompt-a.txt", false)
cmd := agyHeadlessTurnShellCommand("/usr/local/bin/hive", "agy", "gemini-pro", "high", "/data/agents/a/.hive-agy-prompt-a.txt", false, "")

if strings.Contains(cmd, "do the task") || strings.Contains(cmd, "$(cat") {
t.Fatalf("headless turn command must not expand the prompt into argv (E2BIG past 128 KiB); cmd: %q", cmd)
Expand All @@ -148,10 +148,10 @@ func TestAgyHeadlessTurnCommandRunsTurnRunner(t *testing.T) {
if strings.Contains(cmd, "--new-conversation") {
t.Errorf("a normal kick must continue the agent's conversation; cmd: %q", cmd)
}
if cmd := agyHeadlessTurnShellCommand("hive", "agy", "", "", "/p", true); !strings.Contains(cmd, "--new-conversation") {
if cmd := agyHeadlessTurnShellCommand("hive", "agy", "", "", "/p", true, ""); !strings.Contains(cmd, "--new-conversation") {
t.Errorf("ClearOnKick must start a new conversation; cmd: %q", cmd)
}
if cmd := agyHeadlessTurnShellCommand("hive", "agy", "", "high", "/p", false); strings.Contains(cmd, "--model") || strings.Contains(cmd, "--effort") {
if cmd := agyHeadlessTurnShellCommand("hive", "agy", "", "high", "/p", false, ""); strings.Contains(cmd, "--model") || strings.Contains(cmd, "--effort") {
t.Errorf("no model configured must pass neither --model nor --effort; cmd: %q", cmd)
}
}
Expand Down
46 changes: 46 additions & 0 deletions src/crux/agent/agy_turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ type agyTurnOptions struct {
promptFile string
conversationFile string
newConversation bool
// recordFile is the repeat-failure raw record file; empty while the
// check is off (repeat_failure_agy.go).
recordFile string
}

// RunAgyTurn implements `hive agy-turn`. It returns the process exit code.
Expand All @@ -149,6 +152,7 @@ func parseAgyTurnArgs(args []string, stderr io.Writer) (agyTurnOptions, error) {
fs.StringVar(&opts.promptFile, "prompt-file", "", "file holding the prompt for this turn (required)")
fs.StringVar(&opts.conversationFile, "conversation-file", "", "file recording the conversation id to resume; empty disables continuity")
fs.BoolVar(&opts.newConversation, "new-conversation", false, "ignore the recorded conversation and start a new one")
fs.StringVar(&opts.recordFile, agyRepeatFailureRecordFlag, "", "repeat-failure record file to append one record per run_command to; empty records nothing")
if err := fs.Parse(args); err != nil {
return opts, err
}
Expand Down Expand Up @@ -205,6 +209,7 @@ func runAgyTurn(opts agyTurnOptions, stdout, stderr io.Writer) int {
}

r := newAgyTurnRenderer(stdout, time.Now())
r.recordFile = opts.recordFile
r.status()
if err := cmd.Start(); err != nil {
r.finish()
Expand Down Expand Up @@ -366,6 +371,13 @@ type agyTurnRenderer struct {
pending map[int]string // step index → assistant text not yet ended by a newline
lastTool string
result *agyTurnResult
// recordFile receives one repeat-failure record per run_command step that
// ends; empty records nothing. commands holds each run_command step's
// CommandLine from its ACTIVE update; recorded holds the steps already
// written.
recordFile string
commands map[int]string
recorded map[int]bool
}

func newAgyTurnRenderer(w io.Writer, started time.Time) *agyTurnRenderer {
Expand Down Expand Up @@ -503,6 +515,9 @@ func (r *agyTurnRenderer) handleStep(s *agyStepUpdate) {
if name == "" && s.ToolInfo != nil {
name = s.ToolInfo.Name
}
if name == "run_command" {
r.recordRunCommand(s)
}
switch s.State {
case "ACTIVE":
r.mu.Lock()
Expand All @@ -525,6 +540,37 @@ func (r *agyTurnRenderer) handleStep(s *agyStepUpdate) {
}
}

// recordRunCommand appends the repeat-failure record of a run_command step
// once it ends, at most once per step. Fails open: an unwritable record file
// means no record and nothing else.
func (r *agyTurnRenderer) recordRunCommand(s *agyStepUpdate) {
if r.recordFile == "" {
return
}
r.mu.Lock()
if r.commands == nil {
r.commands, r.recorded = map[int]string{}, map[int]bool{}
}
command := agyRunCommandLine(s.ToolInfo)
if command == "" {
command = r.commands[s.StepIndex]
} else {
r.commands[s.StepIndex] = command
}
rec, ok := agyRunCommandRecord(s, command)
if ok && !r.recorded[s.StepIndex] {
r.recorded[s.StepIndex] = true
delete(r.commands, s.StepIndex)
} else {
ok = false
}
r.mu.Unlock()
if ok {
rec.Time = r.now()
_ = AppendRepeatFailureRecord(r.recordFile, rec)
}
}

// summarizeAgyToolParams renders tool parameters as one short line, with
// CommandLine (run_command) first since it is the one operators read most.
func summarizeAgyToolParams(info *agyToolInfo) string {
Expand Down
2 changes: 1 addition & 1 deletion src/crux/agent/agy_turn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestAgyHeadlessLaunchShim_ExportsEnvAndConversationFile(t *testing.T) {
launch := agyHeadlessFullLaunchCmd(
shellEnvVar("HIVE_AGENT_MODE", "issues-only")+" "+shellEnvVar("HTTPS_PROXY", "http://127.0.0.1:18443")+" ",
agyHeadlessLaunchCmd())
kick := agyHeadlessTurnShellCommand(runner, "agy", "", "", "/tmp/p", false)
kick := agyHeadlessTurnShellCommand(runner, "agy", "", "", "/tmp/p", false, "")
line := launch + "\n" + kick + "\n" + kick + "\n" + launch + "\n" + kick + "\n"

out, err := exec.Command(bash, "--noprofile", "--norc", "-c", line).CombinedOutput()
Expand Down
3 changes: 2 additions & 1 deletion src/crux/agent/busy_visibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func newestCopilotTranscriptActivityCached(home string, cache *transcriptSession
}
}
var mod time.Time
if info, err := os.Stat(filepath.Join(root, name, "events.jsonl")); err == nil {
// Lstat: a link planted in the agent's HOME is never followed.
if info, err := os.Lstat(filepath.Join(root, name, "events.jsonl")); err == nil && info.Mode().IsRegular() {
mod = info.ModTime()
}
if cache != nil {
Expand Down
8 changes: 7 additions & 1 deletion src/crux/agent/kick_transcript.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package agent

import (
Expand All @@ -11,6 +11,7 @@
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -153,7 +154,12 @@
if !t.since.IsZero() && h.LastActive.Before(t.since) {
return "", fmt.Errorf("newest %s transcript %s predates the kick", t.backend, h.Transcript)
}
f, err := os.Open(h.Transcript)
// The transcript sits in the agent's HOME: never follow a link planted
// there (the same discipline as the repeat-failure Copilot reader).
if info, err := os.Lstat(h.Transcript); err != nil || !info.Mode().IsRegular() {
return "", fmt.Errorf("%s transcript %s is not a regular file", t.backend, h.Transcript)
}
f, err := os.OpenFile(h.Transcript, os.O_RDONLY|syscall.O_NOFOLLOW, 0)
if err != nil {
return "", fmt.Errorf("opening %s transcript: %w", t.backend, err)
}
Expand Down
5 changes: 5 additions & 0 deletions src/crux/agent/kick_transcript_copilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type copilotTranscriptEvent struct {
Success *bool `json:"success"`
Result json.RawMessage `json:"result"`
Error json.RawMessage `json:"error"`
// ShellExecution ({"exitCode":N}) and ToolTelemetry are read by the
// repeat-failure reader (repeat_failure_copilot.go); raw so a shape
// change never breaks the kick log.
ShellExecution json.RawMessage `json:"shellExecution"`
ToolTelemetry json.RawMessage `json:"toolTelemetry"`
} `json:"data"`
}

Expand Down
4 changes: 3 additions & 1 deletion src/crux/agent/manager_kick.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package agent

import (
Expand Down Expand Up @@ -428,7 +428,9 @@
}
model = normalizeModelNameForBackend(model, "agy", false)
// ClearOnKick maps to a new agy conversation: there is no TUI to /clear.
cmd := agyHeadlessTurnShellCommand(agyTurnRunnerBinary(), "agy", model, agent.Config.ReasoningEffort, promptFile, agent.Config.ClearOnKick)
// The repeat-failure switch is read per kick, so turning it off stops the
// next turn's records without a relaunch.
cmd := agyHeadlessTurnShellCommand(agyTurnRunnerBinary(), "agy", model, agent.Config.ReasoningEffort, promptFile, agent.Config.ClearOnKick, m.repeatFailureAgyRecordFile(agent))
if err := m.tmuxSendLiteralForAgent(agent, cmd); err != nil {
return m.kickDeliveryFailedLocked(agent, "send text", err)
}
Expand Down
12 changes: 10 additions & 2 deletions src/crux/agent/manager_launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ func (m *Manager) launchInTmux(ctx context.Context, agent *AgentProcess) error {
if advisorFlag := m.advisorLaunchFlag(agent, backend, isInference); advisorFlag != "" {
launchCmd += advisorFlag
}
// Repeat-failure check (#14550): omp loads Hive's record extension with
// its own --extension, whether or not the advisor is on.
launchCmd += m.repeatFailureOmpLaunchFlag(agent, backend)

if bootstrapPrompt == "" && isInference {
bootstrapPrompt = "You are an AI agent. Await further instructions."
Expand Down Expand Up @@ -301,6 +304,7 @@ func (m *Manager) launchInTmux(ctx context.Context, agent *AgentProcess) error {
// Repeat-failure check (#14550): write the BASH_ENV route the env prefix
// below points at, or remove it when the check is now off.
m.provisionRepeatFailureBashEnv(agent, backend)
m.provisionRepeatFailureAgyRecordDir(agent, backend)

// NOTE on Copilot's "Confirm folder trust" modal (≥1.0.78): #4563 tried to
// pre-seed config.json trusted_folders here so it never appears. That does
Expand Down Expand Up @@ -1181,8 +1185,9 @@ var agyTurnRunnerBinary = func() string {

// agyHeadlessTurnShellCommand is the line a kick types into a headless agy
// pane: one `hive agy-turn` run, then the ready marker. The prompt stays in a
// file and reaches agy on stdin (see agy_turn.go for why not argv).
func agyHeadlessTurnShellCommand(hiveBinary, agyBinary, model, effort, promptFile string, newConversation bool) string {
// file and reaches agy on stdin (see agy_turn.go for why not argv). A
// non-empty recordFile turns on the repeat-failure route for this turn.
func agyHeadlessTurnShellCommand(hiveBinary, agyBinary, model, effort, promptFile string, newConversation bool, recordFile string) string {
args := []string{shellQuote(hiveBinary), agyTurnSubcommand, "--agy", shellQuote(agyBinary)}
if model != "" {
args = append(args, "--model", shellQuote(model), "--effort", shellQuote(agyLaunchEffort(model, effort)))
Expand All @@ -1192,6 +1197,9 @@ func agyHeadlessTurnShellCommand(hiveBinary, agyBinary, model, effort, promptFil
if newConversation {
args = append(args, "--new-conversation")
}
if recordFile != "" {
args = append(args, "--"+agyRepeatFailureRecordFlag, shellQuote(recordFile))
}
return fmt.Sprintf("printf '\\n%s\\n'; %s; echo %s",
agyHeadlessRunningMarker, strings.Join(args, " "), agyHeadlessReadyMarker)
}
Expand Down
98 changes: 98 additions & 0 deletions src/crux/agent/repeat_failure_agy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package agent

import (
"os"
"regexp"
"strconv"
"strings"
)

// agy route for the repeat-failure check (hivecommons/hive#14550, C1, C3,
// C14; issue #14750).
//
// agy is a closed binary whose commands never reach the neutral BASH_ENV
// route. In the headless launch every kick runs one `hive agy-turn`, which
// already parses agy's stream-json steps: a `tool` step for `run_command`
// carries the command in Parameters["CommandLine"]. While the check is on the
// kick passes `--repeat-failure-record <file>` and the runner appends one
// record per run_command step that ends (DONE, or a failed state) to the
// agent's raw record file. agy reports no exit status field; when the tool
// output states one ("exit code: 2") it is recorded, otherwise only the step
// state decides failed/succeeded. The interactive TUI launch bypasses the
// runner and has no route. Nothing is written into agy's own files; with the
// check off the kick passes no flag and the runner writes nothing (C14).

// RepeatFailureRouteAgyTurn names the agy-turn runner in a record's Route.
const RepeatFailureRouteAgyTurn = "agy_turn"

// agyRepeatFailureRecordFlag is the agy-turn flag naming the raw record file.
const agyRepeatFailureRecordFlag = "repeat-failure-record"

// agyRunCommandExitCode matches an exit status stated in run_command output.
var agyRunCommandExitCode = regexp.MustCompile(`(?i)\bexit(?:ed with)?[ _]?(?:code|status)\b[\s:=]*(-?\d+)`)

// agyRunCommandRecord builds the record of a run_command step that ended, or
// reports false for any other step. command is the step's CommandLine, taken
// from its earlier ACTIVE update when the final one omits it.
func agyRunCommandRecord(s *agyStepUpdate, command string) (RepeatFailureRecord, bool) {
rec := RepeatFailureRecord{Agent: os.Getenv("HIVE_AGENT"), Backend: "agy", RawCommand: command, Route: RepeatFailureRouteAgyTurn}
switch strings.ToUpper(strings.TrimSpace(s.State)) {
case "DONE":
case "ERROR", "FAILED", "CANCELED", "CANCELLED":
rec.Failed = true
default:
return rec, false
}
if strings.TrimSpace(command) == "" {
return rec, false
}
if s.ToolInfo != nil {
if m := agyRunCommandExitCode.FindAllStringSubmatch(s.ToolInfo.Output, -1); len(m) > 0 {
if code, err := strconv.Atoi(m[len(m)-1][1]); err == nil {
rec.ExitCode = &code
rec.Failed = code != 0
}
}
}
return rec, true
}

// agyRunCommandLine returns a run_command step's CommandLine, or "".
func agyRunCommandLine(info *agyToolInfo) string {
if info == nil {
return ""
}
cmd, _ := info.Parameters["CommandLine"].(string)
return cmd
}

// repeatFailureAgyRecordFile is the record file a headless agy kick passes to
// the runner, or "" when the check is off (C14). Lock-free.
func (m *Manager) repeatFailureAgyRecordFile(agent *AgentProcess) string {
if agent == nil || !m.repeatFailureEnabled() {
return ""
}
home := AgentHome(agent.Name, agent.UID, "agy")
if home == "" {
return ""
}
return RepeatFailureRecordPath(home)
}

// provisionRepeatFailureAgyRecordDir creates the agent's 0700 record
// directory at a headless agy launch while the check is on, so the runner,
// which runs as the agent, can append to the record file. Best-effort: a
// failure is logged and the runner's appends then fail open.
func (m *Manager) provisionRepeatFailureAgyRecordDir(agent *AgentProcess, backend string) {
if !strings.EqualFold(strings.TrimSpace(backend), "agy") || !agyHeadlessEnabled() {
return
}
recordPath := m.repeatFailureAgyRecordFile(agent)
if recordPath == "" {
return
}
if err := ensureRepeatFailureRecordDirAs(m.agentExecUserSpec(agent), recordPath); err != nil {
m.logger.Warn("repeat-failure: creating the agy record dir failed; agent launches without the route",
"agent", agent.Name, "path", recordPath, "error", err)
}
}
Loading
Loading