Merge pull request 'handleSummary ignores errors from WorkflowStats/StageStats/TokensByDay (archie)' (#78) from fix/53-handlesummary-ignores-errors-from-workflowstatsstagestatstok into main
Reviewed-on: sam/archie-core#78
This commit is contained in:
commit
2facb2be1b
5 files changed
+148
-16
No files matched your search
@@ -115,7 +115,17 @@ func TestTaskPayloadWrittenAsVolumeFile(t *testing.T) {
|
||||
// ── Regression tests for error paths ────────────────────────────────
|
||||
|
||||
func TestWriteTaskJSONNonexistentDir(t *testing.T) {
|
||||
err := WriteTaskJSON("/nonexistent/path/that/does/not/exist", TaskPayload{ID: 1})
|
||||
// Create a regular file and then try to use a path where that file
|
||||
// would need to be a directory. Even root cannot mkdir through a
|
||||
// regular file, making this test reliable regardless of privileges.
|
||||
f, err := os.CreateTemp("", "archie-test-blocker")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
err = WriteTaskJSON(filepath.Join(f.Name(), "subdir"), TaskPayload{ID: 1})
|
||||
if err == nil {
|
||||
t.Error("expected error writing to nonexistent directory")
|
||||
}
|
||||
|
||||
@@ -206,14 +206,20 @@ func TestTurnBudgetSpillFileNaming(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTurnBudgetSpillDirPermissions(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// Make dir read-only.
|
||||
if err := os.Chmod(dir, 0o444); err != nil {
|
||||
t.Fatalf("chmod: %v", err)
|
||||
// Create a regular file and use a sub-path as the spill directory.
|
||||
// Even root cannot write a file when a path component is a regular
|
||||
// file, making this test reliable regardless of privileges.
|
||||
f, err := os.CreateTemp("", "archie-test-spill-blocker")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = os.Chmod(dir, 0o755) }()
|
||||
f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
b := NewTurnBudget(100_000, dir)
|
||||
// spillDir is a sub-path of the regular file — WriteFile must fail.
|
||||
spillDir := filepath.Join(f.Name(), "sub")
|
||||
|
||||
b := NewTurnBudget(100_000, spillDir)
|
||||
ref := b.Spill("tool", []byte("data"))
|
||||
|
||||
// Spill should handle write failure gracefully.
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -583,14 +584,19 @@ func TestDispatchPerToolLimitSpillFailureFallback(t *testing.T) {
|
||||
// Before fix: the "spill failed" path had no fallback; the per-tool
|
||||
// limit was silently bypassed, leaving the full 500-char preview.
|
||||
|
||||
dir := t.TempDir()
|
||||
// Make spill directory read-only to force write failure.
|
||||
if err := os.Chmod(dir, 0o444); err != nil {
|
||||
t.Fatalf("chmod: %v", err)
|
||||
// Create a regular file and use a sub-path as the spill directory.
|
||||
// Even root cannot write a file when a path component is a regular
|
||||
// file, making this test reliable regardless of privileges.
|
||||
f, err := os.CreateTemp("", "archie-test-spill-blocker")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = os.Chmod(dir, 0o755) }()
|
||||
f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
budget := NewTurnBudget(100_000, dir)
|
||||
spillDir := filepath.Join(f.Name(), "sub")
|
||||
|
||||
budget := NewTurnBudget(100_000, spillDir)
|
||||
e := ToolEntry{
|
||||
Name: "big-output",
|
||||
Handler: echoHandler,
|
||||
|
||||
+15
-3
@@ -84,9 +84,21 @@ func (s *Server) handleSummary(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
workflows, _ := s.Store.WorkflowStats(ctx)
|
||||
stages, _ := s.Store.StageStats(ctx)
|
||||
days, _ := s.Store.TokensByDay(ctx, 14)
|
||||
workflows, err := s.Store.WorkflowStats(ctx)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
stages, err := s.Store.StageStats(ctx)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
days, err := s.Store.TokensByDay(ctx, 14)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{
|
||||
"statuses": statuses, "workflows": workflows,
|
||||
"stages": stages, "tokens_by_day": days,
|
||||
|
||||
@@ -2,7 +2,9 @@ package webui
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -27,6 +29,37 @@ func newTestServer(t *testing.T) *Server {
|
||||
return &Server{Store: s, Log: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
||||
}
|
||||
|
||||
// stubStore wraps a real TaskStore and overrides selected methods to
|
||||
// inject errors. Methods that are not overridden delegate to the inner
|
||||
// store via the embedded interface.
|
||||
type stubStore struct {
|
||||
store.TaskStore
|
||||
workflowStatsErr error
|
||||
stageStatsErr error
|
||||
tokensByDayErr error
|
||||
}
|
||||
|
||||
func (s *stubStore) WorkflowStats(ctx context.Context) ([]store.WorkflowStat, error) {
|
||||
if s.workflowStatsErr != nil {
|
||||
return nil, s.workflowStatsErr
|
||||
}
|
||||
return s.TaskStore.WorkflowStats(ctx)
|
||||
}
|
||||
|
||||
func (s *stubStore) StageStats(ctx context.Context) ([]store.StageStat, error) {
|
||||
if s.stageStatsErr != nil {
|
||||
return nil, s.stageStatsErr
|
||||
}
|
||||
return s.TaskStore.StageStats(ctx)
|
||||
}
|
||||
|
||||
func (s *stubStore) TokensByDay(ctx context.Context, days int) ([]store.DayTokens, error) {
|
||||
if s.tokensByDayErr != nil {
|
||||
return nil, s.tokensByDayErr
|
||||
}
|
||||
return s.TaskStore.TokensByDay(ctx, days)
|
||||
}
|
||||
|
||||
func TestHandleSummary(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
ctx := t.Context()
|
||||
@@ -52,6 +85,71 @@ func TestHandleSummary(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleSummaryWorkflowStatsError proves that handleSummary does not
|
||||
// silently discard an error from WorkflowStats. Today the handler uses
|
||||
// blank-identifier assignment (workflows, _ := ...), so this test FAILS
|
||||
// (wants 500, gets 200 with nil workflows).
|
||||
func TestHandleSummaryWorkflowStatsError(t *testing.T) {
|
||||
real := newTestServer(t)
|
||||
srv := &Server{
|
||||
Store: &stubStore{
|
||||
TaskStore: real.Store,
|
||||
workflowStatsErr: fmt.Errorf("db locked"),
|
||||
},
|
||||
Log: real.Log,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/summary", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("status = %d, want 500 — WorkflowStats error was silently discarded", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleSummaryStageStatsError proves that handleSummary does not
|
||||
// silently discard an error from StageStats.
|
||||
func TestHandleSummaryStageStatsError(t *testing.T) {
|
||||
real := newTestServer(t)
|
||||
srv := &Server{
|
||||
Store: &stubStore{
|
||||
TaskStore: real.Store,
|
||||
stageStatsErr: fmt.Errorf("db locked"),
|
||||
},
|
||||
Log: real.Log,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/summary", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("status = %d, want 500 — StageStats error was silently discarded", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleSummaryTokensByDayError proves that handleSummary does not
|
||||
// silently discard an error from TokensByDay.
|
||||
func TestHandleSummaryTokensByDayError(t *testing.T) {
|
||||
real := newTestServer(t)
|
||||
srv := &Server{
|
||||
Store: &stubStore{
|
||||
TaskStore: real.Store,
|
||||
tokensByDayErr: fmt.Errorf("db locked"),
|
||||
},
|
||||
Log: real.Log,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/summary", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("status = %d, want 500 — TokensByDay error was silently discarded", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleTasks(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
ctx := t.Context()
|
||||
|
||||
Reference in new issue
Block a user