Distinguish active stage deadlines from harness transport failures

Identify configured attempt expiry during V2 active-session idle waits and retain bounded phase, timeout and session diagnostics without claiming a lost response. Preserve genuine operation failures, error identities, redaction, cleanup and admitted-work replay protections. Add stage regressions and scratch-server CLI/websocket retention coverage.

Checked packaged templates in examples and the pipeline template generator: none needs updating because timeout configuration and contracts are unchanged. Checked packaged authoring skill references: debugging.md needs a STAGE DEADLINE playbook and clarified transport guidance. Recorded that stale shared documentation in docs/notes/stage-timeout-diagnostics.md for docs-fold; shared documentation was deliberately not edited.

Verification: go build ./...; go vet ./...; go test ./...; go test -count=1 ./internal/server ./internal/stage ./internal/pipeline ./internal/protocol; go test -count=1 -run TestStageDeadlineDiagnosticThroughCLIAndWebsocket -v ./internal/server; gofmt -l .; git diff --check. All passed. Live-provider behavior not tested; no deploy or deployed-service restart.
This commit is contained in:
2026-10-18 11:17:01 +01:00
parent b93eee234c
commit 31d8f4a6d5
8 changed files with 363 additions and 8 deletions
+138
View File
@@ -0,0 +1,138 @@
package server
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/cli"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Exercise the real V2 CLI/poll path, runner, SQLite retention and websocket CLI
// against an isolated loopback server/database, never the configured deployment.
func TestStageDeadlineDiagnosticThroughCLIAndWebsocket(t *testing.T) {
ntfy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"id":"scratch-receipt","event":"message"}`))
}))
defer ntfy.Close()
t.Setenv("PIPELINES_NTFY_URL", ntfy.URL+"/scratch")
t.Setenv("PIPELINES_NTFY_TOKEN", "")
t.Setenv("PIPELINES_NTFY_TOKEN_FILE", "")
for _, mode := range []string{"active", "http", "connection"} {
t.Run(mode, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dir := t.TempDir()
executable := filepath.Join(dir, "opencode")
script := `#!/bin/sh
if [ "$1" = "--help" ]; then echo ' api'; exit 0; fi
printf '%s %s\n' "$2" "$3" >> "@STATE@/calls"
case "$3" in
/api/session) echo '{"data":{"id":"ses_deadline"}}' ;;
*/prompt) echo 'edited once' >> "@STATE@/edited"; echo '{}' ;;
/api/session/active)
if [ -f "@STATE@/interrupted" ]; then echo '{"data":{}}'; exit 0; fi
case "@MODE@" in
active) echo '{"data":{"ses_deadline":{"type":"running"}}}' ;;
http) echo 'HTTP 503 token=private-token' >&2; exit 1 ;;
connection) echo 'connection refused token=private-token' >&2; exit 1 ;;
esac ;;
*/interrupt) touch "@STATE@/interrupted"; echo '{}' ;;
*/message*) echo '{"data":[{"content":[{"type":"text","text":"still working"}]}]}' ;;
/api/session/ses_deadline) echo '{"data":{"id":"ses_deadline"}}' ;;
*) echo '{}' ;;
esac
`
script = strings.ReplaceAll(strings.ReplaceAll(script, "@STATE@", dir), "@MODE@", mode)
if err := os.WriteFile(executable, []byte(script), 0700); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", executable)
store, err := pipeline.Open(filepath.Join(dir, "runs.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
source := pipeline.Template([]string{"worker"}, true)
source = strings.Replace(source, "```tools\n```", "```tools\nedit\n```", 1)
source = strings.Replace(source, "timeout=1h", "timeout=500ms", 1)
if _, err := store.Submit(ctx, "deadline", source); err != nil {
t.Fatal(err)
}
handler := Handler(ctx, store)
defer func() { cancel(); WaitRuns(handler) }()
srv := httptest.NewServer(handler)
defer srv.Close()
url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect"
var out, stderr bytes.Buffer
app := cli.App{Out: &out, Err: &stderr, In: strings.NewReader("")}
if err := app.Run(ctx, []string{"--url", url, "run", "deadline", "task", "--yes", "--json", "--wait", "--timeout", "10s", "--workspace", dir}); err == nil || !strings.Contains(err.Error(), "run ended failed") {
t.Fatalf("run not failed: %v, %s, %s", err, out.String(), stderr.String())
}
var response protocol.Message
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
t.Fatal(err, out.String())
}
detail := response.RunDetail
if detail == nil || detail.Status != "failed" || len(detail.Steps) != 1 {
t.Fatal("missing failed run/step", out.String())
}
step := detail.Steps[0]
if step.SessionID != "ses_deadline" || step.AgentActive || step.Output["error"] != step.Diagnostic || step.Status != "failed" || detail.Diagnostic != step.Diagnostic {
t.Fatalf("incorrect retained failure: %+v", detail)
}
wants := []string{"HARNESS TRANSPORT", "get /api/session/active", "CLI exit code 1"}
if mode == "active" {
wants = []string{"STAGE DEADLINE", "timeout 500ms", "waiting for session idle", "ses_deadline", "stage had not finished"}
if strings.Contains(detail.Diagnostic, "HARNESS TRANSPORT") || strings.Contains(detail.Diagnostic, "response lost") {
t.Fatal("active stage overrun reported as transport", detail.Diagnostic)
}
if strings.Contains(detail.Diagnostic, "\n") {
t.Fatal("duplicate context error appended to bounded stage diagnostic", detail.Diagnostic)
}
for _, want := range []string{"opencode_failures", "wait for session idle", "timeout 500ms", "retained_session", "still working"} {
if !strings.Contains(step.Transcript, want) {
t.Fatalf("missing retained evidence %q: %s", want, step.Transcript)
}
}
} else {
if !strings.Contains(step.Transcript, "[REDACTED]") {
t.Fatal("request failure evidence missing", step.Transcript)
}
if mode == "http" {
wants = append(wants, "HTTP 503")
} else if !strings.Contains(step.Transcript, "connection refused") {
t.Fatal("underlying connection evidence missing", step.Transcript)
}
}
for _, want := range wants {
if !strings.Contains(detail.Diagnostic, want) {
t.Fatalf("missing diagnostic %q: %s", want, detail.Diagnostic)
}
}
if strings.Contains(detail.Diagnostic+step.Transcript, "private-token") {
t.Fatal("credential leaked in retained diagnostic/evidence")
}
edited, err := os.ReadFile(filepath.Join(dir, "edited"))
if err != nil || string(edited) != "edited once\n" {
t.Fatalf("admitted tool work replayed or lost: %q, %v", edited, err)
}
if _, err := os.Stat(filepath.Join(dir, "interrupted")); err != nil {
t.Fatal("cleanup did not interrupt", err)
}
stored, err := store.GetRun(ctx, detail.ID)
if err != nil || stored.Diagnostic != detail.Diagnostic || stored.Steps[0].Transcript != step.Transcript {
t.Fatal("diagnostic/evidence not persisted in scratch database", err)
}
t.Logf("scratch run %s: %s", mode, stored.Diagnostic)
})
}
}