Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126//go:build unix
// A Blue creation acknowledgement must recover only the exact frozen intent,// not a Standard session at the same UUID. Both paths use real daemon receipts.package e2e
import ( "context" "encoding/json" "errors" "io" "net/http" "strings" "testing"
"albedo/cli/internal/app" "albedo/cli/internal/daemon")
func TestDaybreakCreationReceipts(t *testing.T) { f := newBlueFixture(t) t.Run("lost-blue-acknowledgement", func(t *testing.T) { proxy := mutateConnectionAcknowledgement(t, f.connection, "/sessions", dropAcknowledgement, nil, nil) service := app.Service{Connect: func(context.Context) (*daemon.Connection, error) { return proxy.connection, nil }} opened, err := service.PrepareOpen(t.Context(), app.OpenOptions{Workspace: t.TempDir(), Fresh: true, CyberAccess: new("daybreak_blue")}) if err != nil { t.Fatal(err) } var admitted struct { ID string `json:"id"` } if decodeErr := json.Unmarshal(proxy.acceptedResponse(t), &admitted); decodeErr != nil { t.Fatal(decodeErr) } if opened.Selected == nil || opened.Selected.ID != admitted.ID { t.Fatal("lost Blue ACK recovered another ID") } requireBlue(t, *opened.Selected, "daybreak_blue") sessions, err := daemon.ListSessions(t.Context(), f.connection) if err != nil { t.Fatal(err) } count := 0 for _, session := range sessions { if session.Workspace == opened.Selected.Workspace { count++ } } if count != 1 { t.Fatalf("lost ACK allocated %d sessions", count) } }) t.Run("wrong-intent-refuses-recovery", func(t *testing.T) { handle, err := daemon.NewCreation(daemon.CreateSessionRequest{Workspace: t.TempDir(), Provider: "blue", CyberAccess: "daybreak_blue"}) if err != nil { t.Fatal(err) } guard := &conflictingBlueCreation{next: f.connection.HTTPClient().Transport} if guard.next == nil { guard.next = http.DefaultTransport } f.connection.HTTPClient().Transport = guard defer func() { f.connection.HTTPClient().Transport = guard.next }() _, err = daemon.CreateSessionOperation(t.Context(), f.connection, handle) if api, ok := errors.AsType[*daemon.APIError](err); !ok || api.Code != "creation_conflict" { t.Fatalf("wrong intent recovered: %v", err) } if guard.puts != 1 || guard.id != "/sessions/"+handle.ID() || guard.condition != "*" { t.Fatalf("changed stable creation identity: %+v", guard) } requireBlue(t, f.session(t, handle.ID()), "") }) t.Run("unsupported-and-unknown-creation-fail-closed", func(t *testing.T) { for _, profile := range []string{"ordinary", "unknown"} { workspace := t.TempDir() _, err := daemon.CreateSession(t.Context(), f.connection, daemon.CreateSessionRequest{Workspace: workspace, Provider: profile, CyberAccess: "daybreak_blue"}) if err == nil { t.Fatalf("%s Blue creation admitted", profile) } sessions, listErr := daemon.ListSessions(t.Context(), f.connection) if listErr != nil { t.Fatal(listErr) } for _, session := range sessions { if session.Workspace == workspace { t.Fatalf("%s rejection created a session", profile) } } } })}
type conflictingBlueCreation struct { next http.RoundTripper id, condition string puts int}
func (g *conflictingBlueCreation) RoundTrip(request *http.Request) (*http.Response, error) { if request.Method != http.MethodPut || !strings.HasPrefix(request.URL.Path, "/sessions/") { return g.next.RoundTrip(request) } g.puts++ g.id = request.URL.Path g.condition = request.Header.Get("If-None-Match") var body map[string]any if err := json.NewDecoder(request.Body).Decode(&body); err != nil { return nil, err } // Another intent won this identity; the caller still holds its Blue handle. delete(body, "cyber_access") encoded, err := json.Marshal(body) if err != nil { return nil, err } request.Body = io.NopCloser(strings.NewReader(string(encoded))) request.ContentLength = int64(len(encoded)) response, err := g.next.RoundTrip(request) if err != nil { return nil, err } defer response.Body.Close() _, _ = io.Copy(io.Discard, response.Body) return nil, io.ErrUnexpectedEOF}