Fix canonical archive writes and retain scoped lifecycle evidence
Foundry Go checks / check (push) Successful in 2m41s
Foundry Go checks / check (pull_request) Successful in 2m40s

This commit is contained in:
2026-11-07 22:34:42 +08:00
parent b85e8bd2ca
commit 12458e10dc
5 changed files with 118 additions and 22 deletions
+49 -8
View File
@@ -24,11 +24,13 @@ type modelCanvas struct {
Parent *modelParent `json:"parent"`
}
type canvasModel struct {
Root string
Canvases map[string]modelCanvas
Elements map[string]map[string]any
Tombstones map[string]int64
Versions map[string]int64
Root string
Canvases map[string]modelCanvas
Elements map[string]map[string]any
Tombstones map[string]int64
TombstoneOwners map[string]string
DeadRecords map[string]map[string]any
Versions map[string]int64
}
type modelView struct {
Format int `json:"documentFormatVersion"`
@@ -66,7 +68,7 @@ type modelBatch struct {
}
func emptyModel(root string) canvasModel {
return canvasModel{Root: root, Canvases: map[string]modelCanvas{}, Elements: map[string]map[string]any{}, Tombstones: map[string]int64{}, Versions: map[string]int64{}}
return canvasModel{Root: root, Canvases: map[string]modelCanvas{}, Elements: map[string]map[string]any{}, Tombstones: map[string]int64{}, TombstoneOwners: map[string]string{}, DeadRecords: map[string]map[string]any{}, Versions: map[string]int64{}}
}
func modelString(value any) string { text, _ := value.(string); return text }
func modelInt(value any) int64 {
@@ -76,6 +78,32 @@ func modelInt(value any) int64 {
}
return int64(number)
}
func modelJSONSafe(value any, depth int) bool {
if depth > 64 {
return false
}
switch value := value.(type) {
case nil, bool, string:
return true
case float64:
return !math.IsNaN(value) && !math.IsInf(value, 0)
case []any:
for _, item := range value {
if !modelJSONSafe(item, depth+1) {
return false
}
}
return true
case map[string]any:
for key, item := range value {
if canvasChoice(key, "__proto__", "constructor", "prototype") || !modelJSONSafe(item, depth+1) {
return false
}
}
return true
}
return false
}
func modelEqual(left, right any) bool {
// Round-trip numeric Go structs and JSON records to the same JSON value domain.
a, ea := json.Marshal(left)
@@ -106,8 +134,16 @@ func (model canvasModel) view(participants map[string]int64) modelView {
}
view.Elements = append(view.Elements, element)
}
// Tombstones are small identity facts, not payloads; include them for safe incarnation checks.
for id, incarnation := range model.Tombstones {
owner := model.TombstoneOwners[id]
if live := model.Elements[id]; live != nil {
owner = modelString(live["ownerCanvasId"])
}
if participants != nil {
if _, exists := participants[owner]; !exists {
continue
}
}
view.Tombstones[id] = incarnation
}
for _, id := range sortedKeys(model.Versions) {
@@ -207,6 +243,9 @@ func validateCanvasModel(model canvasModel) error {
}
}
for id, element := range model.Elements {
if !modelJSONSafe(element, 0) {
return errCanvasModel
}
owner, component := modelString(element["ownerCanvasId"]), modelString(element["componentId"])
_, canvasExists := model.Canvases[owner]
incarnation, version := modelInt(element["incarnation"]), modelInt(element["dataVersion"])
@@ -333,6 +372,8 @@ func applyModelOperations(initial canvasModel, operations []modelOperation) (can
if after == nil {
if prior := model.Elements[change.ID]; prior != nil {
model.Tombstones[change.ID] = modelInt(prior["incarnation"])
model.TombstoneOwners[change.ID] = modelString(prior["ownerCanvasId"])
model.DeadRecords[change.ID] = prior
}
delete(model.Elements, change.ID)
} else {
@@ -340,7 +381,7 @@ func applyModelOperations(initial canvasModel, operations []modelOperation) (can
if !ok || record["id"] != change.ID {
return model, nil, errCanvasModel
}
if before == nil && modelInt(record["incarnation"]) != model.Tombstones[change.ID]+1 {
if before == nil && modelInt(record["incarnation"]) <= model.Tombstones[change.ID] {
return model, nil, errModelConflict
}
if prior := model.Elements[change.ID]; prior != nil && modelInt(record["incarnation"]) != modelInt(prior["incarnation"]) {