Files
pipelines/internal/server/session_export_status_test.go
alan 22801a9b06 fix(stage): align session export render cap with artifact bound
Six computed Flutter runs stopped at the export handoff gate because the
rendered session.html cap was 2 MiB while their complete sessions (well inside
the 8 MiB source read bound) rendered between 2.25 and 4.28 MiB once html
escaping and the readable projection duplicated native text.

Raise SessionExportFileLimit to 8 MiB, the server's existing per-artifact file
bound, so a complete export is still listed and served. Normal export
verification, versioned repair and the handoff gate keep using the single
shared constant. The source read bound is unchanged, and an over-cap render is
still visibly truncated and recorded partial.

Add focused regressions for a complete export above the old cap, an over-new-cap
file rejected as partial, repair above the old cap leaving original evidence
untouched, the gate accepting the larger repair, and the constant staying inside
the artifact file bound. Update the artifact, handoff-gate and operations docs
and record the diagnosis in docs/notes/session-export-render-cap.md.
2026-10-21 15:26:29 +01:00

120 lines
4.4 KiB
Go

package server
import (
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
func TestSessionExportCompleteness(t *testing.T) {
for _, tc := range []struct {
evidence protocol.SessionExport
want string
}{
{protocol.SessionExport{Source: "native_export", HTML: "session.html", HTMLBytes: 100}, "empty"},
{protocol.SessionExport{Source: "native_export", Diagnostic: "export failed"}, "failed"},
{protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 100, Truncated: true}, "partial"},
{protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 100}, "complete"},
{protocol.SessionExport{Source: "unavailable"}, "unavailable"},
} {
classifySessionExport(&tc.evidence)
if tc.evidence.Status != tc.want {
t.Fatalf("%+v want %s", tc.evidence, tc.want)
}
if tc.want == "empty" && tc.evidence.Diagnostic == "" {
t.Fatal("empty export has no warning")
}
}
}
func TestRuntimeSessionExportFileVerificationDoesNotClaimMissingEvidence(t *testing.T) {
for _, kind := range []string{"missing", "empty", "symlink", "normal"} {
t.Run(kind, func(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "session.html")
switch kind {
case "empty":
if err := os.WriteFile(path, nil, 0600); err != nil {
t.Fatal(err)
}
case "normal":
if err := os.WriteFile(path, []byte("html"), 0600); err != nil {
t.Fatal(err)
}
case "symlink":
if err := os.Symlink(filepath.Join(t.TempDir(), "outside"), path); err != nil {
t.Fatal(err)
}
}
evidence := &protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 4}
verifySessionExportFile(evidence, dir)
classifySessionExport(evidence)
if (evidence.Status == "complete") != (kind == "normal") {
t.Fatalf("%s: %+v", kind, evidence)
}
})
}
}
func sizedSessionFile(t *testing.T, size int64) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "session.html")
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
if err := f.Truncate(size); err != nil {
f.Close()
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
return dir
}
// TestRuntimeSessionExportVerificationAcceptsAboveTheOldRenderCap pins the
// ordinary-session case the previous 2 MiB rendered bound cut. The file is
// larger than that old cap and inside the artifact-aligned bound, so normal
// export verification must keep it and classify the evidence complete.
func TestRuntimeSessionExportVerificationAcceptsAboveTheOldRenderCap(t *testing.T) {
size := int64(2<<20) + 4096
dir := sizedSessionFile(t, size)
evidence := &protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: size}
verifySessionExportFile(evidence, dir)
classifySessionExport(evidence)
if evidence.Status != "complete" || evidence.HTML != "session.html" || evidence.HTMLBytes != size || evidence.Diagnostic != "" {
t.Fatalf("complete export above the old cap was rejected: %+v", evidence)
}
}
// TestRuntimeSessionExportVerificationRejectsAboveTheNewCap confirms the larger
// bound is still a hard bound: an oversized file is not presented as evidence.
func TestRuntimeSessionExportVerificationRejectsAboveTheNewCap(t *testing.T) {
size := int64(stage.SessionExportFileLimit) + 1
dir := sizedSessionFile(t, size)
evidence := &protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: size}
verifySessionExportFile(evidence, dir)
classifySessionExport(evidence)
if evidence.Status != "partial" || evidence.HTML != "" || evidence.HTMLBytes != 0 || !strings.Contains(evidence.Diagnostic, "bounded verification") {
t.Fatalf("oversized export was not rejected as partial: %+v", evidence)
}
}
// TestSessionExportFileLimitStaysInsideArtifactBounds keeps the render cap
// principled: larger than the cap that cut ordinary sessions, and no larger
// than the ordinary per-artifact file bound that serves the file.
func TestSessionExportFileLimitStaysInsideArtifactBounds(t *testing.T) {
if stage.SessionExportFileLimit <= 2<<20 {
t.Fatalf("render cap %d did not grow beyond the old 2 MiB cutoff", stage.SessionExportFileLimit)
}
if stage.SessionExportFileLimit > maxArtifactFileBytes {
t.Fatalf("render cap %d exceeds the artifact file bound %d", stage.SessionExportFileLimit, maxArtifactFileBytes)
}
}