Skip to content

Commit 35dd7a5

Browse files
author
Vaibhaav
committed
fix: preserve cleared preview state
1 parent 95c9647 commit 35dd7a5

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

backend/internal/preview/entry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/aoagents/agent-orchestrator/backend/internal/domain"
1212
)
1313

14-
var entryCandidates = []string{"index.html", "dist/index.html", "build/index.html", "public/index.html"}
14+
var entryCandidates = []string{"index.html", "public/index.html", "dist/index.html", "build/index.html"}
1515

1616
// Entry is a workspace-local static frontend entrypoint.
1717
type Entry struct {

backend/internal/preview/poller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/aoagents/agent-orchestrator/backend/internal/domain"
1313
)
1414

15+
// DefaultPollInterval is the preview poller's scan interval when none is configured.
1516
const DefaultPollInterval = 250 * time.Millisecond
1617

1718
type sessionPreviewSource interface {
@@ -22,6 +23,7 @@ type previewSetter interface {
2223
SetPreview(ctx context.Context, id domain.SessionID, previewURL string) (domain.Session, error)
2324
}
2425

26+
// PollerConfig configures preview poller timing and logging.
2527
type PollerConfig struct {
2628
Interval time.Duration
2729
Logger *slog.Logger
@@ -44,6 +46,7 @@ type entryState struct {
4446
size int64
4547
}
4648

49+
// NewPoller constructs a preview poller over the supplied session source and setter.
4750
func NewPoller(source sessionPreviewSource, setter previewSetter, baseURL string, cfg PollerConfig) *Poller {
4851
p := &Poller{
4952
source: source,
@@ -137,7 +140,7 @@ func (p *Poller) Poll(ctx context.Context) error {
137140
func (p *Poller) shouldRefresh(sess domain.SessionRecord, target string, seenBefore bool) bool {
138141
current := strings.TrimSpace(sess.Metadata.PreviewURL)
139142
if current == "" {
140-
return !seenBefore
143+
return !seenBefore && sess.Metadata.PreviewRevision == 0
141144
}
142145
if current == target || isWorkspacePreviewURL(current, sess.ID) {
143146
return true

backend/internal/preview/poller_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ func TestPollerUsesFirstExistingEntrypoint(t *testing.T) {
7070
})
7171
}
7272

73+
func TestPollerPreservesEntrypointPriority(t *testing.T) {
74+
workspace := t.TempDir()
75+
writeFile(t, filepath.Join(workspace, "public", "index.html"), "<main>public</main>")
76+
writeFile(t, filepath.Join(workspace, "dist", "index.html"), "<main>dist</main>")
77+
svc := &fakePreviewSessions{sessions: []domain.SessionRecord{workerSession("ao-1", workspace, "")}}
78+
poller := NewPoller(svc, svc, "http://127.0.0.1:3001", PollerConfig{Logger: discardLogger()})
79+
80+
if err := poller.Poll(context.Background()); err != nil {
81+
t.Fatalf("Poll: %v", err)
82+
}
83+
84+
assertSets(t, svc.sets, previewSet{
85+
id: "ao-1",
86+
url: "http://127.0.0.1:3001/api/v1/sessions/ao-1/preview/files/public/index.html",
87+
})
88+
}
89+
7390
func TestPollerRefreshesOnlyWhenEntrypointChanges(t *testing.T) {
7491
workspace := t.TempDir()
7592
entry := filepath.Join(workspace, "index.html")
@@ -101,6 +118,23 @@ func TestPollerRefreshesOnlyWhenEntrypointChanges(t *testing.T) {
101118
}
102119
}
103120

121+
func TestPollerDoesNotRestoreClearedPreviewAfterRestart(t *testing.T) {
122+
workspace := t.TempDir()
123+
writeFile(t, filepath.Join(workspace, "index.html"), "<main>hello</main>")
124+
sess := workerSession("ao-1", workspace, "")
125+
sess.Metadata.PreviewRevision = 2
126+
svc := &fakePreviewSessions{sessions: []domain.SessionRecord{sess}}
127+
poller := NewPoller(svc, svc, "http://127.0.0.1:3001", PollerConfig{Logger: discardLogger()})
128+
129+
if err := poller.Poll(context.Background()); err != nil {
130+
t.Fatalf("Poll: %v", err)
131+
}
132+
133+
if len(svc.sets) != 0 {
134+
t.Fatalf("sets = %#v, want cleared preview to remain empty after restart", svc.sets)
135+
}
136+
}
137+
104138
func TestPollerDoesNotOverrideExplicitPreviewTarget(t *testing.T) {
105139
workspace := t.TempDir()
106140
writeFile(t, filepath.Join(workspace, "index.html"), "<main>hello</main>")

0 commit comments

Comments
 (0)