Make Pipelines Docker-only with OpenCode and Pi
Install current verified harness releases inside Docker, preserve persistent accounts and settings, and provide separate interactive and pipeline model defaults with terminal onboarding and fuzzy pickers. Add Docker-aware authoring transport, Pi stage execution, bounded workflow controls, reliable final-result readback and usage settlement, and finite short-lived CLI pipe draining. Keep dated installation evidence and packaged authoring guidance consistent. Verified with a real Docker installation, both harness stages and interactive authors, the built-in plan/implement/review starter, independent host race/vet checks, installer tests, and packaged skill tests. Generated release receipts stay ignored in clean checkouts.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ShortLivedWaitDelay is the single finite grace for draining inherited output
|
||||
// pipes of short-lived, context-controlled CLI calls (API calls, `--help`
|
||||
// detection and the native export fallback). After the child exits os/exec waits
|
||||
// at most this long for descendants that still hold the inherited stdout/stderr
|
||||
// pipes, then closes the pipes and reports exec.ErrWaitDelay. It bounds pipe
|
||||
// draining only: it does not terminate, signal or confirm cleanup of descendants.
|
||||
const ShortLivedWaitDelay = 250 * time.Millisecond
|
||||
|
||||
// CommandContext builds a short-lived CLI command with a finite WaitDelay so a
|
||||
// context cancellation or a descendant-held output pipe cannot block the caller
|
||||
// for the child's natural lifetime. Callers still set Dir, Stdout and Stderr.
|
||||
// It has no dependencies and no side effects.
|
||||
func CommandContext(ctx context.Context, executable string, args ...string) *exec.Cmd {
|
||||
cmd := exec.CommandContext(ctx, executable, args...)
|
||||
cmd.WaitDelay = ShortLivedWaitDelay
|
||||
return cmd
|
||||
}
|
||||
@@ -102,9 +102,15 @@ func Executable() string {
|
||||
|
||||
// Detect checks the advertised command interface, not an assumed release number.
|
||||
func Detect(ctx context.Context, executable string) (string, error) {
|
||||
cmd := exec.CommandContext(ctx, executable, "--help")
|
||||
cmd := CommandContext(ctx, executable, "--help")
|
||||
data, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
// Preserve the caller's cancellation/deadline identity; a parent that
|
||||
// exits 0 while a descendant still holds the combined-output pipe is
|
||||
// never parsed from its partial stdout.
|
||||
if ctx.Err() != nil {
|
||||
return "", ctx.Err()
|
||||
}
|
||||
return "", fmt.Errorf("cannot run the selected OpenCode CLI: %w", err)
|
||||
}
|
||||
if regexp.MustCompile(`(?m)^\s*(?:opencode\s+)?api\s`).Match(data) {
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -48,7 +48,7 @@ func (c *Connection) Messages(ctx context.Context, id string) ([]byte, error) {
|
||||
if executable == "" {
|
||||
executable = Executable()
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, executable, "export", id)
|
||||
cmd := CommandContext(ctx, executable, "export", id)
|
||||
cmd.Dir = c.Directory
|
||||
stdout, stderr := &boundedOutput{limit: MaxResponse}, &boundedOutput{limit: 8 << 10}
|
||||
cmd.Stdout = stdout
|
||||
@@ -57,7 +57,9 @@ func (c *Connection) Messages(ctx context.Context, id string) ([]byte, error) {
|
||||
if ctx.Err() != nil {
|
||||
return data, ctx.Err()
|
||||
}
|
||||
return data, fmt.Errorf("%w; read-only native export fallback failed", err)
|
||||
// Keep the original HTTP-400/OutputFormat evidence identity while also
|
||||
// preserving exec.ErrWaitDelay from a descendant-held output pipe.
|
||||
return data, errors.Join(err, fmt.Errorf("read-only native export fallback failed: %w", exportErr))
|
||||
}
|
||||
if stdout.overflow {
|
||||
return data, fmt.Errorf("%w; native export exceeds 1 MiB evidence limit", err)
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func writeFixture(t *testing.T, script string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "opencode")
|
||||
if err := os.WriteFile(path, []byte(script), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func waitForFile(t *testing.T, path string, watchdog time.Duration) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(watchdog)
|
||||
for {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("fixture marker never appeared")
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectReadsAdvertisedInterface(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"v2": "#!/bin/sh\necho 'opencode api --help'\n",
|
||||
"v1": "#!/bin/sh\necho 'opencode serve --help'\n",
|
||||
}
|
||||
for want, script := range cases {
|
||||
backend, err := Detect(context.Background(), writeFixture(t, script))
|
||||
if err != nil || backend != want {
|
||||
t.Fatalf("Detect = %q, %v; want %q", backend, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFailsWhenDescendantHoldsOutput(t *testing.T) {
|
||||
// A valid api line is printed, but the parent exits 0 while a descendant
|
||||
// still holds the combined-output pipe: Detect must fail rather than parse
|
||||
// the partial advertisement.
|
||||
script := "#!/bin/sh\necho 'opencode api --help'\n( sleep 4 ) &\nexit 0\n"
|
||||
backend, err := Detect(context.Background(), writeFixture(t, script))
|
||||
if err == nil || backend != "" {
|
||||
t.Fatalf("partial output after ErrWaitDelay must not be parsed, got %q, %v", backend, err)
|
||||
}
|
||||
if !errors.Is(err, exec.ErrWaitDelay) {
|
||||
t.Fatalf("expected ErrWaitDelay identity, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectReturnsContextCancellation(t *testing.T) {
|
||||
marker := filepath.Join(t.TempDir(), "detect-started")
|
||||
t.Setenv("PIPELINES_TEST_STARTED", marker)
|
||||
script := "#!/bin/sh\nprintf started > \"$PIPELINES_TEST_STARTED\"\nsleep 5\n"
|
||||
bin := writeFixture(t, script)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := Detect(ctx, bin)
|
||||
done <- err
|
||||
}()
|
||||
waitForFile(t, marker, 2*time.Second)
|
||||
start := time.Now()
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("wanted context cancellation, got %v", err)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > time.Second {
|
||||
t.Fatalf("Detect waited %s for the descendant output pipes", elapsed)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("Detect cancellation watchdog expired")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessagesExportWaitDelayIsNotSuccess(t *testing.T) {
|
||||
export := `{"info":{"id":"ses_old"},"messages":[{"info":{"role":"assistant"}}]}`
|
||||
bin := writeFixture(t, "#!/bin/sh\n[ \"$1\" = export ] || exit 8\necho '"+export+"'\n( sleep 4 ) &\nexit 0\n")
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte(`{"name":"BadRequest","data":{"message":"Expected OutputFormatJsonSchema at info.format"}}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
c := &Connection{Backend: "v1", URL: server.URL, Executable: bin}
|
||||
data, err := c.Messages(context.Background(), "ses_old")
|
||||
if err == nil || !errors.Is(err, exec.ErrWaitDelay) {
|
||||
t.Fatalf("descendant-held export pipes must fail with ErrWaitDelay, got %v", err)
|
||||
}
|
||||
if strings.Contains(string(data), "messages") {
|
||||
t.Fatalf("partial exported JSON must not be returned as success: %s", data)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user