Apply canonical AI outputs through scoped identity-checked changes
Foundry Go checks / check (push) Successful in 2m56s
Foundry Go checks / check (pull_request) Successful in 2m47s

This commit is contained in:
2026-11-08 00:23:00 +08:00
parent 0d7fc4dbe5
commit fa76773efc
12 changed files with 771 additions and 66 deletions
+15 -10
View File
@@ -111,6 +111,14 @@ func (s *service) applyCanvasAIOutput(w http.ResponseWriter, r *http.Request, pr
if err = json.Unmarshal(frozenRaw, &frozen); err != nil {
return err
}
identityOK, err := canvasAIIdentityMatches(r.Context(), tx, project, workspace, frozen)
if err != nil {
return err
}
if !identityOK {
reject(w, 409, "target_changed")
return nil
}
patch := response{}
switch output.Kind {
case "text":
@@ -158,29 +166,26 @@ func (s *service) applyCanvasAIOutput(w http.ResponseWriter, r *http.Request, pr
reject(w, 409, "ownership_conflict")
return nil
}
for key, value := range patch {
target[key] = value
}
encoded, _ := json.Marshal(doc)
normalized, ok := normalizeCanvas(encoded)
if !ok {
change, err := prepareCanvasAIPatch(r, tx, project, workspace, snapshot, node, patch)
if err != nil {
reject(w, 413, "canvas_too_large")
return nil
}
snapshot.Document = normalized
snapshot.Version++
if err = s.saveCanvas(r, tx, project, workspace, snapshot); err != nil {
snapshot = change.Snapshot
if err = s.saveCanvasAIChange(r, tx, project, workspace, account, change); err != nil {
return err
}
_, err = tx.ExecContext(r.Context(), `UPDATE canvas_ai_steps SET applied_version=$5 WHERE project=$1 AND workspace_id=$2 AND run_id=$3 AND id=$4`, project, workspace, id, stepID, snapshot.Version)
if err != nil {
return err
}
result := response{"run_id": id, "step_id": stepID, "board_id": targetBoard, "node_id": node, "output_id": input.OutputID, "canvas_version": snapshot.Version, "patch": patch}
change.addCanonical(result)
if err = appendAIEvent(r.Context(), tx, project, workspace, id, "step.applied", result); err != nil {
return err
}
encoded, _ = json.Marshal(result)
encoded, _ := json.Marshal(result)
_, err = tx.ExecContext(r.Context(), `INSERT INTO canvas_ai_mutations(project,workspace_id,run_id,account_id,action,idempotency_key,payload_hash,response_body) VALUES($1,$2,$3,$4,'apply',$5,$6,$7)`, project, workspace, id, account, key, hash, encoded)
if err != nil {
return err