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.
This commit is contained in:
2026-10-21 15:26:29 +01:00
parent 0a00526692
commit 22801a9b06
9 changed files with 261 additions and 12 deletions
@@ -249,9 +249,10 @@ landed.
presented. A run recorded before the HTML-only change still lists `session.md`
among its stored artifacts (dated evidence kept on disk, not deleted); every
listing — app, TUI, web spike — hides it rather than showing it beside the html.
- The source read is capped at 8 MiB and the rendered file at 2 MiB; a cut source or
a cut rendered file carries a visible truncation banner at both ends and
`session_export.truncated: true`, and a truncated export is never called whole.
- The source read is capped at 8 MiB and the rendered file at 8 MiB, matching the
ordinary per-artifact file bound; a cut source or a cut rendered file carries a
visible truncation banner at both ends and `session_export.truncated: true`, and
a truncated export is never called whole.
- The step records `session_export` (`session_id`, `backend`, `source`, the html
file name, `html_bytes`, `messages`, `truncated`, `diagnostic`) in
`inspect RUN_ID --json`. A record written before the change still carried
@@ -136,6 +136,56 @@ func TestSessionExportGateRetryOnlyAndExactlyOnce(t *testing.T) {
}
}
// TestSessionExportGateAcceptsRepairAboveOldRenderCap keeps the handoff gate's
// versioned-repair verification on the same larger bound as normal export.
func TestSessionExportGateAcceptsRepairAboveOldRenderCap(t *testing.T) {
h, s, id, first, second := gateRunner(t, func(context.Context, stage.SessionExportRequest) (stage.SessionExportResult, error) {
return stage.SessionExportResult{}, errors.New("export offline")
}, false)
h.runner.wg.Wait()
ctx := context.Background()
if err := s.ConfirmRecoveredSession(ctx, pipeline.SessionOwner{RunID: id, ID: "ses_exact"}); err != nil {
t.Fatal(err)
}
size := int64(2<<20) + 4096
h.runner.exportSession = func(_ context.Context, req stage.SessionExportRequest) (stage.SessionExportResult, error) {
if err := os.MkdirAll(req.ArtifactsDir, 0700); err != nil {
return stage.SessionExportResult{}, err
}
f, err := os.Create(filepath.Join(req.ArtifactsDir, req.FileName))
if err != nil {
return stage.SessionExportResult{}, err
}
if err := f.Truncate(size); err != nil {
f.Close()
return stage.SessionExportResult{}, err
}
if err := f.Close(); err != nil {
return stage.SessionExportResult{}, err
}
return stage.SessionExportResult{Source: "native_export", HTML: req.FileName, HTMLBytes: size, Messages: 2}, nil
}
if _, err := h.runner.repairSessionExport(ctx, id, 1); err != nil {
t.Fatal(err)
}
// The worker promotes a persisted complete repair before claiming a lease.
job, err := s.ClaimSessionExportJob(ctx, time.Now().Add(time.Second), time.Minute)
if err != nil {
t.Fatal(err)
}
if job != nil {
t.Fatalf("complete repair still claimed as a recovery job: %+v", job)
}
if err := h.runner.resumeExportGates(ctx); err != nil {
t.Fatal(err)
}
h.runner.wg.Wait()
d, _ := s.GetRun(ctx, id)
if first.Load() != 1 || second.Load() != 1 || d.Status != "succeeded" || len(d.Steps) != 2 {
t.Fatalf("large repair did not advance the gate: %+v (%d/%d)", d, first.Load(), second.Load())
}
}
func TestSessionExportGateCleanupUnconfirmedDoesNotExportOrAdvance(t *testing.T) {
var exports atomic.Int32
h, s, id, _, second := gateRunner(t, func(ctx context.Context, req stage.SessionExportRequest) (stage.SessionExportResult, error) {
@@ -111,6 +111,57 @@ func TestRuntimeSessionExportRepairPreservesOriginalAndUsesRecordedOwner(t *test
}
}
// TestRuntimeSessionExportRepairAcceptsAboveTheOldRenderCap proves a versioned
// repair uses the same larger bound as a normal export while leaving the
// original recorded evidence untouched.
func TestRuntimeSessionExportRepairAcceptsAboveTheOldRenderCap(t *testing.T) {
h, s, id := sessionExportRunner(t, "v2", func(stage.SessionExportRequest) (stage.SessionExportResult, error) {
return stage.SessionExportResult{}, errors.New("initial export unavailable")
})
if err := s.ConfirmRecoveredSession(context.Background(), pipeline.SessionOwner{RunID: id, ID: "ses_run"}); err != nil {
t.Fatal(err)
}
original, err := s.GetRun(context.Background(), id)
if err != nil {
t.Fatal(err)
}
before, _ := json.Marshal(original.Steps[0].SessionExport)
size := int64(2<<20) + 4096
h.runner.exportSession = func(_ context.Context, req stage.SessionExportRequest) (stage.SessionExportResult, error) {
if err := os.MkdirAll(req.ArtifactsDir, 0700); err != nil {
return stage.SessionExportResult{}, err
}
f, err := os.Create(filepath.Join(req.ArtifactsDir, req.FileName))
if err != nil {
return stage.SessionExportResult{}, err
}
if err := f.Truncate(size); err != nil {
f.Close()
return stage.SessionExportResult{}, err
}
if err := f.Close(); err != nil {
return stage.SessionExportResult{}, err
}
return stage.SessionExportResult{Source: "native_export", HTML: req.FileName, HTMLBytes: size, Messages: 2}, nil
}
repaired, err := h.runner.repairSessionExport(context.Background(), id, 1)
if err != nil {
t.Fatal(err)
}
latest := repaired.Steps[0].SessionExportRepairs[0]
if latest.Status != "complete" || latest.HTMLBytes != size || latest.HTML == "" {
t.Fatalf("repair above the old cap was not complete: %+v", latest)
}
after, _ := json.Marshal(repaired.Steps[0].SessionExport)
if string(before) != string(after) {
t.Fatal("repair above the old cap overwrote original evidence")
}
info, err := os.Stat(filepath.Join(h.runner.artifactsRoot, "1", "stages", "01-"+repaired.Steps[0].Stage, latest.HTML))
if err != nil || info.Size() != size {
t.Fatalf("repair file missing or resized: %v %+v", err, info)
}
}
func TestRuntimeRepairRejectsUnrecordedAndActiveVisits(t *testing.T) {
h, _, id := sessionExportRunner(t, "v2", func(stage.SessionExportRequest) (stage.SessionExportResult, error) {
return stage.SessionExportResult{}, errors.New("unavailable")
+62 -1
View File
@@ -1,10 +1,13 @@
package server
import (
"git.a-wan.net/alan/pipelines/internal/protocol"
"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) {
@@ -56,3 +59,61 @@ func TestRuntimeSessionExportFileVerificationDoesNotClaimMissingEvidence(t *test
})
}
}
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)
}
}
+10 -3
View File
@@ -20,7 +20,7 @@ import (
// artifacts directory as one html file so the existing artifact listing and
// serving paths find it with no special casing. The source read is capped at
// SessionExportSourceLimit and the rendered file at SessionExportFileLimit, so
// the export adds at most ~2 MiB per step, inside the artifact scan limits.
// the export stays inside the server's per-artifact file bound.
const (
// SessionHTMLFile is the one session export a stage writes.
SessionHTMLFile = "session.html"
@@ -32,8 +32,15 @@ const (
// SessionExportSourceLimit is the native session read cap; it is the one
// opencode.MaxSessionExport value, not a second copy.
SessionExportSourceLimit = opencode.MaxSessionExport
SessionExportFileLimit = 2 << 20
SessionExportTimeout = 10 * time.Second
// SessionExportFileLimit is the rendered file cap. Ordinary native sessions
// commonly render a little larger than their raw source because html escaping
// and the readable projection duplicate native text; the previous 2 MiB cap
// cut complete sessions that were well inside the source bound. This cap
// matches the server's per-artifact file bound (maxArtifactFileBytes in
// internal/server/artifacts.go), so a complete export remains listed and
// served while a genuinely oversized one is still visibly truncated.
SessionExportFileLimit = 8 << 20
SessionExportTimeout = 10 * time.Second
)
// SessionExportRequest names the stage session to export and the evidence the
+32
View File
@@ -222,6 +222,38 @@ func TestExportSessionLargeHTTPExportWritesBoundedRealHTML(t *testing.T) {
}
}
// TestExportSessionCompletesAboveTheOldRenderedCap covers the ordinary case the
// previous 2 MiB render cap cut: a complete native session inside the 8 MiB
// source bound whose escaped html is larger than 2 MiB.
func TestExportSessionCompletesAboveTheOldRenderedCap(t *testing.T) {
const oldRenderCap = 2 << 20
dir := t.TempDir()
line := "complete ordinary session line "
text := strings.Repeat(line, oldRenderCap/len(line)+1000)
body := `{"info":{"id":"ses_ok"},"messages":[{"info":{"id":"m1","role":"user"},"text":"` + text + `"}]}`
sessionCLI(t, "ses_ok", body)
result, err := ExportSession(context.Background(), sessionRequest(dir))
if err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join(dir, result.HTML))
if err != nil {
t.Fatal(err)
}
if result.Truncated || result.Messages != 1 || result.HTMLBytes != int64(len(data)) {
t.Fatalf("complete export above the old cap was not recorded: %+v", result)
}
if len(data) <= oldRenderCap || len(data) > SessionExportFileLimit {
t.Fatalf("rendered size %d is not above the old cap and inside the new one", len(data))
}
if strings.Contains(string(data), "TRUNCATED") {
t.Fatal("a complete export carried a truncation banner")
}
if !strings.Contains(string(data), "complete ordinary session line") {
t.Fatal("complete export lost its session text")
}
}
func TestExportSessionSourceTruncationIsLabelledNotWhole(t *testing.T) {
session := opencode.ExportedSession{
ID: "ses_ok",