Skip to content

fix(utils): preserve explicit empty slices in deep copies - #504

Open
ickeep wants to merge 7 commits into
a2aproject:mainfrom
HuashiCloud:codex/preserve-empty-slices-upstream
Open

ickeep wants to merge 7 commits into
a2aproject:mainfrom
HuashiCloud:codex/preserve-empty-slices-upstream

Conversation

@ickeep

@ickeep ickeep commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

DeepCopy's gob round trip converts explicitly empty slices to nil. Task metadata and DataPart values consequently change JSON [] to null, losing a distinction callers rely on.

Based on official v2.9.0 (c746bfd5e4db3806d423d0cea15dbf0919f9bbfa), this change restores originally non-nil empty slices after successful gob decoding while preserving nil values, non-JSON types, copy isolation, and existing encoding errors. Custom GobEncoder and BinaryMarshaler representations remain authoritative. TextMarshaler-only types continue using gob's structural encoding.

The restoration pass skips per-element traversal of byte slices after handling their empty/nil shape. This addresses the large Raw-part performance regression raised in review without changing serialization. A 4 MiB Raw Task benchmark on the same host measured approximately 138–140 ms/op before this adjustment and 1.14–1.21 ms/op afterward (3 iterations, two runs per variant); this is a focused comparison, not a general latency guarantee.

Tests remain table-driven in internal/utils/utils_test.go and cover actual Task/DataPart values, nested containers, explicit null, empty bytes and Raw values, custom codecs, and isolation of a 4 MiB Raw part. Raw comparisons preserve the nil/empty distinction.

Validation of the tree in a370a56522a1c397686266891d49f2d89f62a19e:

  • Root SDK go test -race -mod=readonly -count=1 -shuffle=on ./...: 38 test-bearing packages pass; 17 other packages have no tests.
  • go vet -mod=readonly ./... passes.
  • The same empty-slice regression fails on unmodified official v2.9.0 and passes with this fix.
  • After simplifying the large-Raw test comparison and adding nil/empty Raw cases, the affected utils package passes race testing again.
  • Downstream qiyun History/replay/cancellation/request regressions and full shared-ai application compilation pass against the candidate. The HistoryLength response behavior added in v2.9.0 does not replace stored history.

The nested examples/clustermode module is outside the root-module ./... run. Cancellation is already fixed upstream; this PR contains no cancellation changes.

@ickeep ickeep changed the title fix(utils): 深复制保留显式空切片 fix(utils): preserve explicit empty slices in deep copies Nov 5, 2026
Comment thread internal/utils/utils.go Outdated
}

// DeepCopy uses gob encode-decode pass to create a deep-copy of the referenced data.
// Preserve explicit empty slices so metadata arrays do not become null.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's drop, the comment in the body already covers it

Comment thread internal/utils/utils.go
}

// gob decodes non-nil empty slices as nil; restore their shape without changing nil values.
restoreEmptySlices(reflect.ValueOf(task).Elem(), reflect.ValueOf(&copy).Elem())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably replace gob with a reflection-based deep copy instead of copying and then walking the result again. one pass and we stop depending on gob quirks. not blocking though

Comment thread internal/utils/utils.go Outdated
}

// MustPrettyPrint is a debugging utility that prints indented JSON represenation of a value.
// Print structured diagnostic values, failing explicitly if encoding fails.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change, let's revert

Comment thread internal/utils/utils.go Outdated
fmt.Println(string(raw))
}

// restoreEmptySlices restores empty slices lost by gob while preserving nil values, types, and deep-copy semantics.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need to document unexported helpers, here and on test helpers and test funcs

Comment thread internal/utils/utils.go Outdated
Comment on lines +116 to +117
encoder := reflect.TypeFor[gob.GobEncoder]()
if source.Type().Implements(encoder) || (source.Kind() != reflect.Pointer && reflect.PointerTo(source.Type()).Implements(encoder)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gob also uses encoding.BinaryMarshaler and encoding.TextMarshaler (docs), let's skip those too

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correction: gob doesn't use TextMarshaler (see the NOTE in encoding/gob/type.go and golang.org/issue/6760), current handling is correct

Comment thread internal/utils/empty_slices_test.go Outdated
)

// TestDeepCopyPreservesEmptyCollections verifies empty arrays, explicit null, nested metadata, and write isolation.
func TestDeepCopyPreservesEmptyCollections(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's please move these to utils_test.go and make them table-driven. also let's add a case with a2a.Task that has []any{} in metadata and in a DataPart, that's the behavior we care about

Comment thread internal/utils/empty_slices_test.go Outdated
source := map[string]any{"empty": []any{}, "null": nil, "nested": map[string]any{"items": []any{}, "body": []any{"正文"}}, "metadata": []any{map[string]any{"empty": []any{}}}}
copied, err := DeepCopy(&source)
if err != nil {
t.Fatal(err)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.Fatalf("DeepCopy() error = %v, want nil", err) here and in other places

Comment thread internal/utils/empty_slices_test.go Outdated
Nil []byte
Nested [1][]int
Pointer *[]string
Integer uint64

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to the fix, let's drop

@yarolegovich

Copy link
Copy Markdown
Member

I think it's probably time to hand roll clones like in #462 instead of adding a reflection pass on top of gob, but not in the current release.

Comment thread internal/utils/utils.go
Comment on lines +182 to +184
for i := 0; i < min(source.Len(), target.Len()); i++ {
restoreEmptySlices(source.Index(i), target.Index(i))
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this walks every element, for a2a.Raw that's a reflection call per byte. DeepCopy of a task with a 4MB raw part goes from ~8ms to ~365ms, and we copy on every task store read/write and every event. let's skip the loop when the element type can't contain slices (at least []byte) and add a case with a large Raw part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants