Files
Bedrock/internal/platform/canvas_ai_model_integration_test.go
T
codex-bot f12c4ad8bd
Foundry Go checks / check (push) Successful in 2m51s
Foundry Go checks / check (pull_request) Successful in 3m6s
Persist queryable AI action outcomes and zero-effect rejection receipts
2026-11-08 00:53:11 +08:00

415 lines
18 KiB
Go

package platform
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/base64"
"encoding/hex"
"encoding/json"
"image"
"image/png"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"testing"
"gitea.entroia.com/Zayden-Jung/foundry-core/internal/codex"
)
func TestCanonicalAIAdmissionAndLifecycle(t *testing.T) {
dsn := os.Getenv("PLATFORM_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("requires an isolated integration database")
}
db, err := sql.Open("pgx", dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
ctx := context.Background()
var entropy [32]byte
_, _ = rand.Read(entropy[:])
project := "canonical-ai-" + hex.EncodeToString(entropy[:8])
token := base64.RawURLEncoding.EncodeToString(entropy[:])
digest := sha256.Sum256([]byte(token))
var account, workspace, root string
if err = db.QueryRowContext(ctx, `INSERT INTO project_accounts(project,id) VALUES($1,gen_random_uuid()) RETURNING id::text`, project).Scan(&account); err != nil {
t.Fatal(err)
}
defer func() {
for _, table := range []string{"canvas_ai_runs", "canvas_model_operations", "canvas_model_identity_map", "canvas_model_requests", "canvas_model_projects", "canvas_asset_bindings", "canvas_assets", "canvas_subgraphs", "workspaces", "project_accounts"} {
_, _ = db.ExecContext(ctx, "DELETE FROM "+table+" WHERE project=$1", project)
}
}()
if _, err = db.ExecContext(ctx, `INSERT INTO project_sessions(token_hash,project,account_id,expires_at) VALUES($1,$2,$3,now()+interval '1 hour')`, base64.RawURLEncoding.EncodeToString(digest[:]), project, account); err != nil {
t.Fatal(err)
}
if err = db.QueryRowContext(ctx, `INSERT INTO workspaces(project,name,owner_id) VALUES($1,'Canonical AI',$2) RETURNING id::text`, project, account).Scan(&workspace); err != nil {
t.Fatal(err)
}
if _, err = db.ExecContext(ctx, `INSERT INTO workspace_members(project,workspace_id,account_id,role) VALUES($1,$2,$3,'admin')`, project, workspace, account); err != nil {
t.Fatal(err)
}
if err = db.QueryRowContext(ctx, `INSERT INTO materials(project,workspace_id,kind,title,created_by,canvas_document) VALUES($1,$2,'board','Canonical AI',$3,$4) RETURNING id::text`, project, workspace, account, json.RawMessage(canvasFixture)).Scan(&root); err != nil {
t.Fatal(err)
}
temp, err := os.MkdirTemp("", "fc-canonical-ai-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(temp)
socket := filepath.Join(temp, "worker.sock")
listener, err := net.Listen("unix", socket)
if err != nil {
t.Fatal(err)
}
var pngBytes bytes.Buffer
if err = png.Encode(&pngBytes, image.NewRGBA(image.Rect(0, 0, 3, 2))); err != nil {
t.Fatal(err)
}
hooks := make(chan func(), 1)
var executions atomic.Int32
worker := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/canvas/models" {
reply(w, 200, response{"models": []any{codex.CanvasModel{ID: "text", Available: true, InputModalities: []string{"text", "image"}, OutputModalities: []string{"text"}, MaxInputBytes: 1 << 20, MaxExecutionMS: 90000}, codex.CanvasModel{ID: "image", Available: true, InputModalities: []string{"text", "image"}, OutputModalities: []string{"image"}, MaxInputBytes: 1 << 20, MaxExecutionMS: 180000}}})
return
}
if r.URL.Path != "/canvas/exec" {
reject(w, 404, "not_found")
return
}
var task codex.CanvasTask
if json.NewDecoder(r.Body).Decode(&task) != nil {
reject(w, 400, "invalid_request")
return
}
executions.Add(1)
select {
case hook := <-hooks:
hook()
default:
}
if task.OutputModality == "image" {
reply(w, 200, response{"output": codex.CanvasOutput{Kind: "image", Image: &codex.CanvasImage{MIME: "image/png", Data: pngBytes.Bytes()}}})
} else {
reply(w, 200, response{"result": "generated result"})
}
})}
go worker.Serve(listener)
defer worker.Close()
s := &service{db: db, origin: "https://api.example.com", canvasAI: codex.NewCanvasClient(socket)}
callbacks := map[string]string{project: "https://app.example.com/auth/callback"}
mux := http.NewServeMux()
prefix := "/projects/{project}/workspaces/{workspace}/materials/{material}/canvas"
mux.HandleFunc(prefix+"/model", s.protected(callbacks, s.getCanvasModel))
mux.HandleFunc(prefix+"/model/migrate", s.protected(callbacks, s.migrateCanonicalCanvas))
mux.HandleFunc(prefix+"/ai/runs", s.protected(callbacks, s.createCanvasAIRun))
mux.HandleFunc(prefix+"/ai/runs/{run}", s.protected(callbacks, s.getCanvasAIRun))
mux.HandleFunc(prefix+"/ai/runs/{run}/apply", s.protected(callbacks, s.applyCanvasAIOutput))
mux.HandleFunc(prefix+"/ai/runs/{run}/cancel", s.protected(callbacks, s.cancelCanvasAIRun))
mux.HandleFunc(prefix+"/ai/operations/{request}", s.protected(callbacks, s.getCanvasAIAdmission))
mux.HandleFunc("/projects/{project}/workspaces/{workspace}/assets/{asset}", s.protected(callbacks, s.getAsset))
base := func(board string) string {
return "/projects/" + project + "/workspaces/" + workspace + "/materials/" + board + "/canvas"
}
call := func(method, path string, input any, status int, version int64, key string) response {
t.Helper()
raw, _ := json.Marshal(input)
r := httptest.NewRequest(method, path, bytes.NewReader(raw))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Accept", "application/vnd.foundry.canvas+json;version=2")
if version > 0 {
r.Header.Set("If-Match", `"`+strconv.FormatInt(version, 10)+`"`)
}
if key != "" {
r.Header.Set("Idempotency-Key", key)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
if w.Code != status {
t.Fatalf("%s %s got %d %s want %d", method, path, w.Code, w.Body.String(), status)
}
result := response{}
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
return result
}
read := func() canvasModel {
t.Helper()
tx, e := db.BeginTx(ctx, nil)
if e != nil {
t.Fatal(e)
}
defer tx.Rollback()
model, e := loadCanvasModel(ctx, tx, project, workspace, root)
if e != nil {
t.Fatal(e)
}
return model
}
mutate := func(edit func(*canvasModel)) {
t.Helper()
tx, e := db.BeginTx(ctx, nil)
if e != nil {
t.Fatal(e)
}
defer tx.Rollback()
if _, _, e = lockMaterial(ctx, tx, project, workspace, root, account); e != nil {
t.Fatal(e)
}
before, e := loadCanvasModel(ctx, tx, project, workspace, root)
if e != nil {
t.Fatal(e)
}
after := cloneModel(before)
edit(&after)
participants := map[string]bool{}
for id := range after.Canvases {
after.Versions[id] = before.Versions[id] + 1
participants[id] = true
}
if e = validateCanvasModel(after); e != nil {
t.Fatal(e)
}
if e = s.saveCanvasModel(queueRequest(ctx), tx, project, workspace, account, before, after, participants); e != nil {
t.Fatal(e)
}
if e = tx.Commit(); e != nil {
t.Fatal(e)
}
}
migrationKey := modelTestID(6000)
migration := call("POST", base(root)+"/model/migrate", response{"protocol_version": 2, "request_id": migrationKey, "expected_versions": map[string]int64{root: 1}}, 200, 0, migrationKey)
node := ""
for _, entry := range migration["identity_map"].([]any) {
mapping := entry.(map[string]any)
if mapping["sourceId"] == "a" {
node = mapping["elementId"].(string)
}
}
if node == "" {
t.Fatal("missing migrated target")
}
mutate(func(model *canvasModel) {
model.Elements[node]["geometry"].(map[string]any)["width"] = float64(9000)
model.Elements[node]["opaque"] = map[string]any{"kept": true}
model.Elements[node]["data"].(map[string]any)["extension"] = map[string]any{"constructor": map[string]any{"kept": true}}
model.Elements["future"] = modelTestNode("future", root)
model.Elements["future"]["componentId"] = "future-component"
model.Elements["future"]["dataVersion"] = float64(12)
})
next := 6100
command := func(board, target, model string, incarnation int64) (response, string) {
next += 2
key := modelTestID(next)
return response{"protocol_version": 2, "request_id": key, "operation_id": modelTestID(next + 1), "target_incarnation": incarnation, "expected_versions": read().Versions, "node_id": target, "model_id": model, "prompt": "Generate", "parameters": response{}, "propagate": false}, key
}
start := func(board, target, model string, incarnation int64) response {
input, key := command(board, target, model, incarnation)
return call("POST", base(board)+"/ai/runs", input, 202, read().Versions[board], key)
}
execute := func(id string) {
t.Helper()
run, e := s.claimCanvasRun(ctx)
if e != nil || run == nil || run.ID != id {
t.Fatalf("claim %v %v", run, e)
}
s.executeCanvasRun(ctx, *run)
}
input, key := command(root, node, "text", 1)
sourceVersion := read().Versions[root]
admitted := call("POST", base(root)+"/ai/runs", input, 202, sourceVersion, key)
if admitted["admission"] != "accepted" || admitted["target_incarnation"] != float64(1) || admitted["model"] == nil {
t.Fatal("canonical admission lost identity/base")
}
retry := call("POST", base(root)+"/ai/runs", input, 202, sourceVersion, key)
if !modelEqual(admitted, retry) {
t.Fatal("admission retry changed result")
}
call("POST", base(root)+"/ai/runs", input, 409, sourceVersion+1, key)
queried := call("GET", base(root)+"/ai/operations/"+key, nil, 200, 0, "")
if !modelEqual(admitted, queried) {
t.Fatal("lost admission response not recoverable")
}
unknown := call("GET", base(root)+"/ai/operations/"+modelTestID(6099), nil, 404, 0, "")
if unknown["status"] != "unknown" || unknown["terminal"] == true {
t.Fatal("404 pretended zero execution")
}
duplicate := response{}
for k, v := range input {
duplicate[k] = v
}
duplicateKey := modelTestID(6098)
duplicate["request_id"] = duplicateKey
call("POST", base(root)+"/ai/runs", duplicate, 409, read().Versions[root], duplicateKey)
mutate(func(model *canvasModel) { model.Elements[node]["geometry"].(map[string]any)["x"] = float64(33) })
execute(admitted["run_id"].(string))
saved := read()
data := saved.Elements[node]["data"].(map[string]any)
if data["text"] != "generated result" || saved.Elements[node]["geometry"].(map[string]any)["x"] != float64(33) || saved.Elements[node]["geometry"].(map[string]any)["width"] != float64(9000) || saved.Elements["future"] == nil || saved.Elements[node]["opaque"] == nil || data["extension"] == nil {
t.Fatal("AI lost canonical fields, geometry or future records")
}
// A content conflict retains its output; explicit application is still a narrow canonical write.
conflictRun := start(root, node, "text", 1)
hooks <- func() {
mutate(func(model *canvasModel) { model.Elements[node]["data"].(map[string]any)["text"] = "user edit" })
}
execute(conflictRun["run_id"].(string))
conflictPath := base(root) + "/ai/runs/" + conflictRun["run_id"].(string)
conflict := call("GET", conflictPath, nil, 200, 0, "")
if conflict["status"] != "conflict" {
t.Fatal("changed content was overwritten")
}
outputID := conflict["outputs"].([]any)[0].(map[string]any)["output"].(map[string]any)["output_id"].(string)
applyKey := modelTestID(6200)
expected := read().Versions[root]
applyInput := response{"protocol_version": 2, "request_id": applyKey, "operation_id": modelTestID(6199), "target_incarnation": 1, "target_canvas_id": root, "node_id": node, "output_id": outputID}
applied := call("POST", conflictPath+"/apply", applyInput, 200, expected, applyKey)
again := call("POST", conflictPath+"/apply", applyInput, 200, expected, applyKey)
if !modelEqual(applied, again) || applied["model"] == nil || read().Elements[node]["geometry"].(map[string]any)["width"] != float64(9000) {
t.Fatal("explicit canonical application changed geometry or receipt")
}
applyReceipt := call("GET", base(root)+"/ai/operations/"+applyKey, nil, 200, 0, "")
if !modelEqual(applied, applyReceipt) || applied["action"] != "run.apply" || applied["status"] != "applied" {
t.Fatal("explicit apply receipt is not recoverable")
}
call("POST", conflictPath+"/apply", applyInput, 409, expected+1, applyKey)
// Keep one unapplied old output, then replace the target incarnation atomically.
stale := start(root, node, "text", 1)
hooks <- func() {
mutate(func(model *canvasModel) { model.Elements[node]["data"].(map[string]any)["text"] = "keep this" })
}
execute(stale["run_id"].(string))
stalePath := base(root) + "/ai/runs/" + stale["run_id"].(string)
staleResult := call("GET", stalePath, nil, 200, 0, "")
staleOutput := staleResult["outputs"].([]any)[0].(map[string]any)["output"].(map[string]any)["output_id"].(string)
old := start(root, node, "text", 1)
run, e := s.claimCanvasRun(ctx)
if e != nil || run == nil || run.ID != old["run_id"] {
t.Fatalf("claim stale %v", e)
}
if ok, e := s.startCanvasStep(ctx, *run, run.Plan.Steps[0]); e != nil || !ok {
t.Fatalf("start stale %v", e)
}
mutate(func(model *canvasModel) {
model.Elements[node]["incarnation"] = float64(2)
model.Tombstones[node] = 1
model.TombstoneOwners[node] = root
})
state, e := s.finishCanvasStep(ctx, *run, run.Plan.Steps[0], "late output", "", true)
if e != nil || state != "cancelled" {
t.Fatalf("restored target accepted old callback %s %v", state, e)
}
if e = s.completeCanvasRun(ctx, *run, nil); e != nil {
t.Fatal(e)
}
if read().Elements[node]["data"].(map[string]any)["text"] != "keep this" {
t.Fatal("restore was overwritten")
}
call("POST", stalePath+"/apply", response{"node_id": node, "output_id": staleOutput}, 409, read().Versions[root], modelTestID(6201))
bad, badKey := command(root, node, "text", 1)
call("POST", base(root)+"/ai/runs", bad, 409, read().Versions[root], badKey)
restored := start(root, node, "text", 2)
cancelPath := base(root) + "/ai/runs/" + restored["run_id"].(string) + "/cancel"
cancelKey := modelTestID(6202)
cancelInput := response{"protocol_version": 2, "request_id": cancelKey, "operation_id": modelTestID(6203)}
cancelled := call("POST", cancelPath, cancelInput, 200, 0, cancelKey)
cancelAgain := call("POST", cancelPath, cancelInput, 200, 0, cancelKey)
cancelReceipt := call("GET", base(root)+"/ai/operations/"+cancelKey, nil, 200, 0, "")
if cancelled["status"] != "applied" || cancelled["run_status"] != "cancelled" || !modelEqual(cancelled, cancelAgain) || !modelEqual(cancelled, cancelReceipt) {
t.Fatal("cancel receipt changed on retry/query")
}
duplicateCancelKey := modelTestID(6204)
call("POST", cancelPath, response{"protocol_version": 2, "request_id": duplicateCancelKey, "operation_id": modelTestID(6203)}, 409, 0, duplicateCancelKey)
if executions.Load() != 3 {
t.Fatal("duplicate/cancelled/old execution called the model")
}
// Image output resolves a managed resource while retaining the element envelope.
imageID := modelTestID(6300)
mutate(func(model *canvasModel) {
record := modelTestNode(imageID, root)
record["componentId"] = "plain-image"
record["data"].(map[string]any)["src"] = "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes.Bytes())
record["geometry"].(map[string]any)["width"] = float64(10000)
model.Elements[imageID] = record
})
imageRun := start(root, imageID, "image", 1)
execute(imageRun["run_id"].(string))
imageRecord := read().Elements[imageID]
imageData := imageRecord["data"].(map[string]any)
if imageRecord["geometry"].(map[string]any)["width"] != float64(10000) || imageData["assetId"] == nil || !strings.HasPrefix(modelString(imageData["src"]), s.assetURL(project, workspace, "")) {
t.Fatal("image result lost geometry or resource identity")
}
request := httptest.NewRequest("GET", modelString(imageData["src"]), nil)
request.Header.Set("Authorization", "Bearer "+token)
download := httptest.NewRecorder()
mux.ServeHTTP(download, request)
if download.Code != 200 || !bytes.Equal(download.Body.Bytes(), pngBytes.Bytes()) {
t.Fatal("canonical generated asset is unreadable")
}
moved := start(root, imageID, "image", 1)
moving, e := s.claimCanvasRun(ctx)
if e != nil || moving == nil || moving.ID != moved["run_id"] {
t.Fatalf("claim move %v", e)
}
if ok, e := s.startCanvasStep(ctx, *moving, moving.Plan.Steps[0]); e != nil || !ok {
t.Fatalf("start move %v", e)
}
child, frame := modelTestID(6301), modelTestID(6302)
mutate(func(model *canvasModel) {
entry := modelTestNode(frame, root)
entry["componentId"] = "frame"
model.Elements[frame] = entry
model.Canvases[child] = modelCanvas{ID: child, Parent: &modelParent{CanvasID: root, EntryElementID: frame}}
model.Elements[imageID]["ownerCanvasId"] = child
})
state, e = s.finishCanvasStep(ctx, *moving, moving.Plan.Steps[0], "wrong owner", "", true)
if e != nil || state != "cancelled" {
t.Fatalf("moved target accepted old output %s %v", state, e)
}
if e = s.completeCanvasRun(ctx, *moving, nil); e != nil {
t.Fatal(e)
}
staleInput, staleKey := command(child, imageID, "image", 1)
staleVersions := staleInput["expected_versions"].(map[string]int64)
staleVersions[root]-- // The target version is current; its ancestor context is not.
beforeRejected := read()
denied := call("POST", base(child)+"/ai/runs", staleInput, 412, read().Versions[child], staleKey)
if denied["status"] != "not-applied" || denied["terminal"] != true || denied["action"] != "run.start" || !modelEqual(beforeRejected, read()) {
t.Fatal("rejection was not a zero-effect receipt")
}
if queried := call("GET", base(child)+"/ai/operations/"+staleKey, nil, 200, 0, ""); !modelEqual(denied, queried) {
t.Fatal("negative admission receipt lost")
}
// A later context change cannot turn an old rejected key into a paid execution.
mutate(func(model *canvasModel) { model.Elements[imageID]["geometry"].(map[string]any)["x"] = float64(123) })
if retried := call("POST", base(child)+"/ai/runs", staleInput, 412, beforeRejected.Versions[child], staleKey); !modelEqual(denied, retried) {
t.Fatal("rejected request was reevaluated")
}
missingRunKey := modelTestID(6400)
missingCancel := call("POST", base(child)+"/ai/runs/"+modelTestID(6401)+"/cancel", response{"protocol_version": 2, "request_id": missingRunKey, "operation_id": modelTestID(6402)}, 404, 0, missingRunKey)
if missingCancel["status"] != "not-applied" || missingCancel["terminal"] != true {
t.Fatal("missing run cancel has no stable rejection")
}
if queried := call("GET", base(child)+"/ai/operations/"+missingRunKey, nil, 200, 0, ""); !modelEqual(missingCancel, queried) {
t.Fatal("negative cancel receipt lost")
}
missingInput, missingKey := command(child, imageID, "image", 1)
delete(missingInput, "expected_versions")
call("POST", base(child)+"/ai/runs", missingInput, 400, read().Versions[child], missingKey)
childRun := start(child, imageID, "image", 1)
execute(childRun["run_id"].(string))
if read().Elements[imageID]["ownerCanvasId"] != child {
t.Fatal("child result moved its target back")
}
}