Bootstrap vm run tooling before attach
Speed up first use of repo backed VMs by bootstrapping obvious tools before the best effort LLM harness runs. Add a host side tooling plan for pinned Go, Node, Python, and Rust versions, summarize that plan in the uploaded prompt, and run repo mise install plus guest global mise use -g --pin steps before the bounded opencode inspection. Keep the harness non fatal, prefer host opencode attach when the client supports it, fall back to guest opencode over SSH for older clients, and cover the new flow with CLI plus planner tests. Validation: - go test ./internal/cli ./internal/toolingplan - GOCACHE=/tmp/banger-gocache go test ./... - make build
This commit is contained in:
parent
1e967140c3
commit
4813e844e2
10 changed files with 1126 additions and 13 deletions
|
|
@ -30,6 +30,7 @@ import (
|
|||
"banger/internal/paths"
|
||||
"banger/internal/rpc"
|
||||
"banger/internal/system"
|
||||
"banger/internal/toolingplan"
|
||||
"banger/internal/vmdns"
|
||||
"banger/internal/vsockagent"
|
||||
|
||||
|
|
@ -56,7 +57,8 @@ var (
|
|||
opencodeCmd.Stdin = stdin
|
||||
return opencodeCmd.Run()
|
||||
}
|
||||
hostCommandOutputFunc = func(ctx context.Context, name string, args ...string) ([]byte, error) {
|
||||
hostOpencodeAttachSupportedFunc = hostOpencodeAttachSupported
|
||||
hostCommandOutputFunc = func(ctx context.Context, name string, args ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, name, args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
|
|
@ -94,12 +96,14 @@ var (
|
|||
guestDialFunc = func(ctx context.Context, address, privateKeyPath string) (vmRunGuestClient, error) {
|
||||
return guest.Dial(ctx, address, privateKeyPath)
|
||||
}
|
||||
prepareVMRunRepoCopyFunc = prepareVMRunRepoCopy
|
||||
cwdFunc = os.Getwd
|
||||
prepareVMRunRepoCopyFunc = prepareVMRunRepoCopy
|
||||
buildVMRunToolingPlanFunc = toolingplan.Build
|
||||
cwdFunc = os.Getwd
|
||||
)
|
||||
|
||||
type vmRunGuestClient interface {
|
||||
Close() error
|
||||
UploadFile(ctx context.Context, remotePath string, mode os.FileMode, data []byte, logWriter io.Writer) error
|
||||
RunScript(ctx context.Context, script string, logWriter io.Writer) error
|
||||
StreamTar(ctx context.Context, sourceDir, remoteCommand string, logWriter io.Writer) error
|
||||
StreamTarEntries(ctx context.Context, sourceDir string, entries []string, remoteCommand string, logWriter io.Writer) error
|
||||
|
|
@ -121,6 +125,22 @@ type vmRunRepoSpec struct {
|
|||
|
||||
const vmRunShallowFetchDepth = 10
|
||||
|
||||
const vmRunToolingHarnessModel = "opencode/mimo-v2-pro-free"
|
||||
const vmRunToolingHarnessTimeoutSeconds = 45
|
||||
const vmRunToolingInstallTimeoutSeconds = 120
|
||||
|
||||
const vmRunToolingHarnessPrompt = `You are preparing a development VM for this repository.
|
||||
|
||||
Inspect the repository for developer tools and binaries that are clearly needed to work on it. Look at files like .mise.toml, .tool-versions, README/setup docs, CI config, task runners, scripts, and build manifests.
|
||||
|
||||
Rules:
|
||||
- Use mise only for installs.
|
||||
- Do not edit repository files.
|
||||
- Prefer repo-declared versions first.
|
||||
- If a tool is clearly required but not pinned, you may install a conservative guest-global tool with mise.
|
||||
- Skip ambiguous installs instead of guessing.
|
||||
- End with a short summary of what you installed and what you skipped.`
|
||||
|
||||
func NewBangerCommand() *cobra.Command {
|
||||
root := &cobra.Command{
|
||||
Use: "banger",
|
||||
|
|
@ -1613,8 +1633,10 @@ func runVMRun(ctx context.Context, socketPath string, cfg model.DaemonConfig, st
|
|||
if err := importVMRunRepoToGuest(ctx, client, spec, progress); err != nil {
|
||||
return fmt.Errorf("vm %q is running but repo import failed: %w", vmRef, err)
|
||||
}
|
||||
progress.render("attaching opencode")
|
||||
if err := runVMRunAttach(ctx, stdin, stdout, stderr, vm.Runtime.GuestIP, vmRunGuestDir(spec.RepoName)); err != nil {
|
||||
if err := startVMRunToolingHarness(ctx, client, spec, progress); err != nil {
|
||||
printVMRunWarning(stderr, fmt.Sprintf("tooling harness start failed: %v", err))
|
||||
}
|
||||
if err := runVMRunAttach(ctx, socketPath, vmRef, cfg, stdin, stdout, stderr, vm.Runtime.GuestIP, vmRunGuestDir(spec.RepoName), progress); err != nil {
|
||||
return fmt.Errorf("vm %q is running but opencode attach failed: %w", vmRef, err)
|
||||
}
|
||||
return nil
|
||||
|
|
@ -1736,16 +1758,196 @@ func vmRunGuestDir(repoName string) string {
|
|||
return filepath.ToSlash(filepath.Join("/root", repoName))
|
||||
}
|
||||
|
||||
func runVMRunAttach(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, guestIP, guestDir string) error {
|
||||
func vmRunToolingHarnessPath(repoName string) string {
|
||||
return filepath.ToSlash(filepath.Join("/tmp", "banger-vm-run-tooling-"+repoName+".sh"))
|
||||
}
|
||||
|
||||
func vmRunToolingHarnessPromptPath(repoName string) string {
|
||||
return filepath.ToSlash(filepath.Join("/tmp", "banger-vm-run-tooling-"+repoName+".prompt.txt"))
|
||||
}
|
||||
|
||||
func vmRunToolingHarnessLogPath(repoName string) string {
|
||||
return filepath.ToSlash(filepath.Join("/root/.cache/banger", "vm-run-tooling-"+repoName+".log"))
|
||||
}
|
||||
|
||||
func startVMRunToolingHarness(ctx context.Context, client vmRunGuestClient, spec vmRunRepoSpec, progress *vmRunProgressRenderer) error {
|
||||
if progress != nil {
|
||||
progress.render("starting tooling harness")
|
||||
}
|
||||
plan := buildVMRunToolingPlanFunc(ctx, spec.RepoRoot)
|
||||
var uploadLog bytes.Buffer
|
||||
if err := client.UploadFile(ctx, vmRunToolingHarnessPromptPath(spec.RepoName), 0o644, []byte(vmRunToolingHarnessPromptData(plan)), &uploadLog); err != nil {
|
||||
return formatVMRunStepError("upload tooling harness prompt", err, uploadLog.String())
|
||||
}
|
||||
uploadLog.Reset()
|
||||
if err := client.UploadFile(ctx, vmRunToolingHarnessPath(spec.RepoName), 0o755, []byte(vmRunToolingHarnessScript(spec, plan)), &uploadLog); err != nil {
|
||||
return formatVMRunStepError("upload tooling harness", err, uploadLog.String())
|
||||
}
|
||||
var launchLog bytes.Buffer
|
||||
if err := client.RunScript(ctx, vmRunToolingHarnessLaunchScript(spec), &launchLog); err != nil {
|
||||
return formatVMRunStepError("launch tooling harness", err, launchLog.String())
|
||||
}
|
||||
if progress != nil {
|
||||
progress.render("tooling harness log: " + vmRunToolingHarnessLogPath(spec.RepoName))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func vmRunToolingHarnessPromptData(plan toolingplan.Plan) string {
|
||||
var prompt strings.Builder
|
||||
prompt.WriteString(vmRunToolingHarnessPrompt)
|
||||
lines := make([]string, 0, len(plan.RepoManagedTools)+len(plan.Steps)+len(plan.Skips))
|
||||
for _, tool := range plan.RepoManagedTools {
|
||||
lines = append(lines, fmt.Sprintf("- Repo already declares %s through mise", tool))
|
||||
}
|
||||
for _, step := range plan.Steps {
|
||||
lines = append(lines, fmt.Sprintf("- Planned deterministic install: %s@%s from %s", step.Tool, step.Version, step.Source))
|
||||
}
|
||||
for _, skip := range plan.Skips {
|
||||
lines = append(lines, fmt.Sprintf("- Deterministic skip: %s (%s)", skip.Target, skip.Reason))
|
||||
}
|
||||
if len(lines) == 0 {
|
||||
lines = append(lines, "- No deterministic prepass actions were planned")
|
||||
}
|
||||
prompt.WriteString("\n\nDeterministic prepass summary:\n")
|
||||
prompt.WriteString(strings.Join(lines, "\n"))
|
||||
prompt.WriteString("\n\nDo not repeat the deterministic prepass work unless it clearly failed. Focus on the remaining gaps.\n")
|
||||
return prompt.String()
|
||||
}
|
||||
|
||||
func vmRunToolingHarnessScript(spec vmRunRepoSpec, plan toolingplan.Plan) string {
|
||||
var script strings.Builder
|
||||
script.WriteString("set -uo pipefail\n")
|
||||
fmt.Fprintf(&script, "DIR=%s\n", shellQuote(vmRunGuestDir(spec.RepoName)))
|
||||
script.WriteString("export PATH=/usr/local/bin:/root/.local/share/mise/shims:$PATH\n")
|
||||
script.WriteString("if [ -f /etc/profile.d/mise.sh ]; then . /etc/profile.d/mise.sh || true; fi\n")
|
||||
script.WriteString("log() { printf '%s\\n' \"$*\"; }\n")
|
||||
script.WriteString("run_best_effort() {\n")
|
||||
script.WriteString(" \"$@\"\n")
|
||||
script.WriteString(" rc=$?\n")
|
||||
script.WriteString(" if [ \"$rc\" -ne 0 ]; then\n")
|
||||
script.WriteString(" log \"command failed ($rc): $*\"\n")
|
||||
script.WriteString(" fi\n")
|
||||
script.WriteString(" return 0\n")
|
||||
script.WriteString("}\n")
|
||||
script.WriteString("run_bounded_best_effort() {\n")
|
||||
script.WriteString(" timeout_secs=\"$1\"\n")
|
||||
script.WriteString(" shift\n")
|
||||
script.WriteString(" timeout_marker=\"$(mktemp)\"\n")
|
||||
script.WriteString(" rm -f \"$timeout_marker\"\n")
|
||||
script.WriteString(" \"$@\" &\n")
|
||||
script.WriteString(" cmd_pid=$!\n")
|
||||
script.WriteString(" (\n")
|
||||
script.WriteString(" sleep \"$timeout_secs\"\n")
|
||||
script.WriteString(" if kill -0 \"$cmd_pid\" 2>/dev/null; then\n")
|
||||
script.WriteString(" : >\"$timeout_marker\"\n")
|
||||
script.WriteString(" log \"command timed out after ${timeout_secs}s: $*\"\n")
|
||||
script.WriteString(" kill -TERM \"$cmd_pid\" 2>/dev/null || true\n")
|
||||
script.WriteString(" if command -v pkill >/dev/null 2>&1; then pkill -TERM -P \"$cmd_pid\" 2>/dev/null || true; fi\n")
|
||||
script.WriteString(" sleep 2\n")
|
||||
script.WriteString(" kill -KILL \"$cmd_pid\" 2>/dev/null || true\n")
|
||||
script.WriteString(" if command -v pkill >/dev/null 2>&1; then pkill -KILL -P \"$cmd_pid\" 2>/dev/null || true; fi\n")
|
||||
script.WriteString(" fi\n")
|
||||
script.WriteString(" ) &\n")
|
||||
script.WriteString(" watchdog_pid=$!\n")
|
||||
script.WriteString(" wait \"$cmd_pid\"\n")
|
||||
script.WriteString(" rc=$?\n")
|
||||
script.WriteString(" kill \"$watchdog_pid\" 2>/dev/null || true\n")
|
||||
script.WriteString(" wait \"$watchdog_pid\" 2>/dev/null || true\n")
|
||||
script.WriteString(" if [ -f \"$timeout_marker\" ]; then\n")
|
||||
script.WriteString(" rm -f \"$timeout_marker\"\n")
|
||||
script.WriteString(" return 0\n")
|
||||
script.WriteString(" fi\n")
|
||||
script.WriteString(" rm -f \"$timeout_marker\"\n")
|
||||
script.WriteString(" if [ \"$rc\" -ne 0 ]; then\n")
|
||||
script.WriteString(" log \"command failed ($rc): $*\"\n")
|
||||
script.WriteString(" fi\n")
|
||||
script.WriteString(" return 0\n")
|
||||
script.WriteString("}\n")
|
||||
script.WriteString("cd \"$DIR\" || { log \"missing repo directory: $DIR\"; exit 0; }\n")
|
||||
script.WriteString("MISE_BIN=\"$(command -v mise || true)\"\n")
|
||||
script.WriteString("OPENCODE_BIN=\"$(command -v opencode || true)\"\n")
|
||||
script.WriteString("if [ -z \"$MISE_BIN\" ]; then log \"mise not found; skipping tooling harness\"; exit 0; fi\n")
|
||||
script.WriteString("if [ -z \"$OPENCODE_BIN\" ]; then log \"opencode not found; skipping tooling harness\"; exit 0; fi\n")
|
||||
fmt.Fprintf(&script, "PROMPT_FILE=%s\n", shellQuote(vmRunToolingHarnessPromptPath(spec.RepoName)))
|
||||
script.WriteString("if [ ! -f \"$PROMPT_FILE\" ]; then log \"tooling prompt file missing: $PROMPT_FILE\"; exit 0; fi\n")
|
||||
script.WriteString("log \"starting tooling harness in $DIR\"\n")
|
||||
script.WriteString("if [ -f .mise.toml ] || [ -f .tool-versions ]; then\n")
|
||||
script.WriteString(" log \"running mise install from repo declarations\"\n")
|
||||
script.WriteString(" run_best_effort \"$MISE_BIN\" install\n")
|
||||
script.WriteString("fi\n")
|
||||
fmt.Fprintf(&script, "INSTALL_TIMEOUT_SECS=%d\n", vmRunToolingInstallTimeoutSeconds)
|
||||
for _, step := range plan.Steps {
|
||||
stepLabel := fmt.Sprintf("deterministic install: %s@%s (%s)", step.Tool, step.Version, step.Source)
|
||||
fmt.Fprintf(&script, "log %s\n", shellQuote(stepLabel))
|
||||
fmt.Fprintf(&script, "run_bounded_best_effort \"$INSTALL_TIMEOUT_SECS\" \"$MISE_BIN\" use -g --pin %s\n", shellQuote(step.Tool+"@"+step.Version))
|
||||
}
|
||||
for _, skip := range plan.Skips {
|
||||
skipLabel := fmt.Sprintf("deterministic skip: %s (%s)", skip.Target, skip.Reason)
|
||||
fmt.Fprintf(&script, "log %s\n", shellQuote(skipLabel))
|
||||
}
|
||||
if len(plan.Steps) > 0 {
|
||||
script.WriteString("run_best_effort \"$MISE_BIN\" reshim\n")
|
||||
}
|
||||
fmt.Fprintf(&script, "MODEL=%s\n", shellQuote(vmRunToolingHarnessModel))
|
||||
fmt.Fprintf(&script, "TIMEOUT_SECS=%d\n", vmRunToolingHarnessTimeoutSeconds)
|
||||
script.WriteString("log \"running bounded opencode repo tooling inspection with $MODEL for up to ${TIMEOUT_SECS}s\"\n")
|
||||
script.WriteString("run_bounded_best_effort \"$TIMEOUT_SECS\" bash -lc 'exec \"$1\" run --format json -m \"$2\" \"$(cat \"$3\")\"' _ \"$OPENCODE_BIN\" \"$MODEL\" \"$PROMPT_FILE\"\n")
|
||||
script.WriteString("log \"tooling harness finished\"\n")
|
||||
return script.String()
|
||||
}
|
||||
|
||||
func vmRunToolingHarnessLaunchScript(spec vmRunRepoSpec) string {
|
||||
var script strings.Builder
|
||||
script.WriteString("set -euo pipefail\n")
|
||||
fmt.Fprintf(&script, "HELPER=%s\n", shellQuote(vmRunToolingHarnessPath(spec.RepoName)))
|
||||
fmt.Fprintf(&script, "LOG=%s\n", shellQuote(vmRunToolingHarnessLogPath(spec.RepoName)))
|
||||
script.WriteString("mkdir -p \"$(dirname \"$LOG\")\"\n")
|
||||
script.WriteString("nohup bash \"$HELPER\" >\"$LOG\" 2>&1 </dev/null &\n")
|
||||
script.WriteString("disown || true\n")
|
||||
return script.String()
|
||||
}
|
||||
|
||||
func runVMRunAttach(ctx context.Context, socketPath, vmRef string, cfg model.DaemonConfig, stdin io.Reader, stdout, stderr io.Writer, guestIP, guestDir string, progress *vmRunProgressRenderer) error {
|
||||
guestIP = strings.TrimSpace(guestIP)
|
||||
if guestIP == "" {
|
||||
return errors.New("vm has no guest IP")
|
||||
}
|
||||
return opencodeExecFunc(ctx, stdin, stdout, stderr, []string{
|
||||
"attach",
|
||||
"--dir", guestDir,
|
||||
"http://" + net.JoinHostPort(guestIP, "4096"),
|
||||
})
|
||||
supportsAttach, err := hostOpencodeAttachSupportedFunc(ctx)
|
||||
if err != nil {
|
||||
printVMRunWarning(stderr, fmt.Sprintf("could not detect host opencode attach support: %v", err))
|
||||
}
|
||||
if supportsAttach {
|
||||
if progress != nil {
|
||||
progress.render("attaching opencode")
|
||||
}
|
||||
return opencodeExecFunc(ctx, stdin, stdout, stderr, []string{
|
||||
"attach",
|
||||
"--dir", guestDir,
|
||||
"http://" + net.JoinHostPort(guestIP, "4096"),
|
||||
})
|
||||
}
|
||||
if progress != nil {
|
||||
progress.render("host opencode has no attach support; starting guest opencode over ssh")
|
||||
}
|
||||
sshArgs, err := sshCommandArgs(cfg, guestIP, []string{"bash", "-lc", fmt.Sprintf("cd %s && exec opencode .", shellQuote(guestDir))})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return runSSHSession(ctx, socketPath, vmRef, stdin, stdout, stderr, sshArgs)
|
||||
}
|
||||
|
||||
func hostOpencodeAttachSupported(ctx context.Context) (bool, error) {
|
||||
output, err := hostCommandOutputFunc(ctx, "opencode", "attach", "--help")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return opencodeAttachHelpOutputSupported(output), nil
|
||||
}
|
||||
|
||||
func opencodeAttachHelpOutputSupported(output []byte) bool {
|
||||
text := strings.ToLower(string(output))
|
||||
return strings.Contains(text, "opencode attach")
|
||||
}
|
||||
|
||||
func formatVMRunStepError(action string, err error, log string) error {
|
||||
|
|
@ -1789,6 +1991,14 @@ func formatVMRunProgress(detail string) string {
|
|||
return "[vm run] " + detail
|
||||
}
|
||||
|
||||
func printVMRunWarning(out io.Writer, detail string) {
|
||||
detail = strings.TrimSpace(detail)
|
||||
if out == nil || detail == "" {
|
||||
return
|
||||
}
|
||||
_, _ = fmt.Fprintln(out, "[vm run] warning: "+detail)
|
||||
}
|
||||
|
||||
func shellQuote(value string) string {
|
||||
return "'" + strings.ReplaceAll(value, "'", `'"'"'`) + "'"
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue