test(audit): verify wrong-session rejection through isolated V2 API

This commit is contained in:
2026-10-20 21:22:37 +01:00
parent ec3eda9e05
commit 860615acb5
2 changed files with 26 additions and 10 deletions
+25 -9
View File
@@ -4,7 +4,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
@@ -12,6 +13,7 @@ import (
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/stage"
)
@@ -77,13 +79,28 @@ func TestAutomaticSessionExportRecoveryWrongSessionExhaustsNeverClean(t *testing
h, s, id := recoveryRunner(t)
ctx := context.Background()
dir := t.TempDir()
calls := filepath.Join(dir, "calls")
bin := filepath.Join(dir, "opencode")
script := fmt.Sprintf("#!/bin/sh\necho read >> %q\necho '{\"info\":{\"id\":\"ses_wrong\"},\"messages\":[{\"info\":{\"role\":\"assistant\"},\"parts\":[{\"type\":\"text\",\"text\":\"wrong\"}]}]}'\n", calls)
if err := os.WriteFile(bin, []byte(script), 0700); err != nil {
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, password, ok := r.BasicAuth()
if !ok || user != "opencode" || password != "fixture-password" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if r.Method != "GET" || r.URL.Path != "/api/experimental/session/ses_run/export" {
w.WriteHeader(http.StatusNotFound)
return
}
calls.Add(1)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"info":{"id":"ses_wrong"},"messages":[{"info":{"role":"assistant"},"parts":[{"type":"text","text":"wrong"}]}]}`))
}))
t.Cleanup(srv.Close)
passwordFile := filepath.Join(dir, "password")
if err := os.WriteFile(passwordFile, []byte("fixture-password"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_OPENCODE", bin)
t.Setenv(opencode.EndpointEnvKey, srv.URL)
t.Setenv(opencode.EndpointPasswordFileEnvKey, passwordFile)
h.runner.exportSession = stage.ExportSession
for range 3 {
job, _ := s.SessionExportJob(ctx, id, 1)
@@ -98,9 +115,8 @@ func TestAutomaticSessionExportRecoveryWrongSessionExhaustsNeverClean(t *testing
if worked, err := h.runner.recoverSessionExportJob(ctx, time.Now().Add(24*time.Hour)); err != nil || worked {
t.Fatalf("unbounded recovery: %v %v", worked, err)
}
body, _ := os.ReadFile(calls)
if strings.Count(string(body), "read") != 3 {
t.Fatalf("wrong-session read was retried internally: %s", body)
if calls.Load() != 3 {
t.Fatalf("wrong-session read was retried internally: %d", calls.Load())
}
detail, _ := s.GetRun(ctx, id)
if len(detail.Steps) != 1 || len(detail.Steps[0].SessionExportRepairs) != 3 {
+1 -1
View File
@@ -180,7 +180,7 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc
func (r *runner) sessionExportUpdatedDetail(ctx context.Context, id int64) (*protocol.RunDetail, error) {
detail, err := r.store.GetRun(ctx, id)
if err == nil {
r.emitRun(protocol.RunEventFrame{RunID: id, PipelineID: detail.PipelineID, Name: detail.Name, Title: detail.Title, Version: detail.Version, Workspace: detail.Workspace, Status: detail.Status, CurrentStage: detail.CurrentStage, CompletedSteps: detail.CompletedSteps, SessionExportUpdated: true}, true)
r.emitRun(protocol.RunEventFrame{RunID: id, PipelineID: detail.PipelineID, Name: detail.Name, Title: detail.Title, Version: detail.Version, Workspace: detail.Workspace, Status: detail.Status, ExportPendingSequence: detail.ExportPendingSequence, CurrentStage: detail.CurrentStage, CompletedSteps: detail.CompletedSteps, SessionExportUpdated: true}, true)
}
return detail, err
}