Skip to content

refactor(typeddict): align pkg/common/typeddict with KHI coding rules - #1325

Open
kyasbal wants to merge 1 commit into
mainfrom
push-ymuqpnwlzrpw
Open

kyasbal wants to merge 1 commit into
mainfrom
push-ymuqpnwlzrpw

Conversation

@kyasbal

@kyasbal kyasbal commented Dec 1, 2026

Copy link
Copy Markdown
Member

Summary

Aligns pkg/common/typeddict with KHI Go coding and testing conventions without changing exported APIs, while fixing two runtime bugs:

  • Replaces the unbounded lockers sync.Map per-key mutex table with a reference-counted keyLocks map guarded by lockMu (lazily initialized so zero-value &TypedDict[T]{} remains ready to use), reclaiming idle per-key locks when refs drops to 0 without holding lockMu across genFunc().
  • Handles stored untyped nil interface values in Get (returning (*new(T), true) before the value.(T) assertion) so storing nil into TypedDict[error] / TypedDict[any] no longer panics while preserving typed-nil interface values.
  • Cleans up the defensive type-mismatch panic text and refines GoDoc and local identifier names across pkg/common/typeddict/typeddict.go.
  • Strengthens TestTypedDict_Concurrent (adding schedule-independent assertions and testing.Short() scaling without skipping) and adds targeted tests for zero-value readiness, interface nil / typed-nil handling, per-key lock reclamation and panic cleanup, distinct-key concurrency, and boundary operations.

Verification

  • make pre-push — PASS (0 mustfix, 0 advisory, 0 skipped)
  • make qa-all-advisory CODEQUALITY_SIMILARITY=true — PASS (0 mustfix, 0 similarity advisories; 15/15 diff mutants killed)
  • go test -race -count=1 -cover ./pkg/common/typeddict/... ./pkg/common/patternfinder/... — PASS (pkg/common/typeddict: 97.9% statement coverage)

Retained Advisories

  • typeddict_test.go:550 (test_redundancy on TestTypedDict_KeyLockLifecycle/allows_distinct_keys_to_proceed_concurrently_without_blocking): intentionally retained because it is the sole deterministic causal-barrier test proving distinct keys do not block each other during GetOrSetFunc (single-goroutine mutation reachability does not capture cross-key lock serialization).
  • typeddict.go:94-96 (defensive internal type-mismatch panic guard): intentionally retained unexercised via exported APIs.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors TypedDict to serialize write operations per key using reference-counted mutexes, allowing its zero value to be ready for use without calling NewTypedDict. It also updates Get to handle nil values and adds comprehensive unit tests. The reviewer suggested using var zero T instead of *new(T) as a more idiomatic way to obtain the zero value of a generic type in Go.

Comment on lines 85 to 98
func Get[T any](dict *TypedDict[T], key string) (T, bool) {
value, ok := dict.container.Load(key)
if ok {
typed, ok := value.(T)
if !ok {
panic(fmt.Sprintf("expected dict value at %s is convertible to %T, but got %T.\nThis error rarely happens unless users forcibly casting the key types or a bug in KHI.\n Please report a bug. https://github.com/GoogleCloudPlatform/khi/issues", key, *new(T), value))
}
return typed, true
if !ok {
return *new(T), false
}
if value == nil {
return *new(T), true
}
typed, ok := value.(T)
if !ok {
panic(fmt.Sprintf("expected dict value at %s to be convertible to %T, but got %T.\nThis error indicates an internal type mismatch in KHI.\nPlease report a bug: https://github.com/GoogleCloudPlatform/khi/issues", key, *new(T), value))
}
return *new(T), false
return typed, true
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using var zero T is the standard and more idiomatic way to obtain the zero value of a generic type in Go, rather than allocating/dereferencing with *new(T). This also simplifies the code and improves readability.

Suggested change
func Get[T any](dict *TypedDict[T], key string) (T, bool) {
value, ok := dict.container.Load(key)
if ok {
typed, ok := value.(T)
if !ok {
panic(fmt.Sprintf("expected dict value at %s is convertible to %T, but got %T.\nThis error rarely happens unless users forcibly casting the key types or a bug in KHI.\n Please report a bug. https://github.com/GoogleCloudPlatform/khi/issues", key, *new(T), value))
}
return typed, true
if !ok {
return *new(T), false
}
if value == nil {
return *new(T), true
}
typed, ok := value.(T)
if !ok {
panic(fmt.Sprintf("expected dict value at %s to be convertible to %T, but got %T.\nThis error indicates an internal type mismatch in KHI.\nPlease report a bug: https://github.com/GoogleCloudPlatform/khi/issues", key, *new(T), value))
}
return *new(T), false
return typed, true
}
func Get[T any](dict *TypedDict[T], key string) (T, bool) {
var zero T
value, ok := dict.container.Load(key)
if !ok {
return zero, false
}
if value == nil {
return zero, true
}
typed, ok := value.(T)
if !ok {
panic(fmt.Sprintf("expected dict value at %s to be convertible to %T, but got %T.\nThis error indicates an internal type mismatch in KHI.\nPlease report a bug: https://github.com/GoogleCloudPlatform/khi/issues", key, zero, value))
}
return typed, true
}
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Go Quality Report (origin/main) — ✅ PASS (0 blocking, 1 advisory finding(s))

Metric Result
Changed Scope 1 prod file(s), 1 test file(s), 20 unit(s)
Patch Coverage 97.3% (36/37 lines)
Mutation Score 100.0% (15/15 killed, 0 survived)

Quality Rule Violations

# Rule Status Value (Limit) Location Subject
[Q1] test_redundancy ADVISORY 1 (<= 0) pkg/common/typeddict/typeddict_test.go:550 TestTypedDict_KeyLockLifecycle
[Q1] test_redundancy — pkg/common/typeddict/typeddict_test.go:550 (TestTypedDict_KeyLockLifecycle)
  • Why: Subtest exercises an identical branch-polarity path with no unique mutant kills, or checks only err != nil on an already-covered path.
  • Fix: Remove duplicate equivalence-class cases, or assert concrete output values so the case kills a unique mutant.
  • Detail: 1 redundant subtest(s) subsumed by stronger tests
  • [SAME PATH] TestTypedDict_KeyLockLifecycle/allows_distinct_keys_to_proceed_concurrently_without_blocking (L550) dominated by TestTypedDict_KeyLockLifecycle/acquires_and_reclaims_keyLocks_across_mutations_and_missing-key_deletes (pkg/common/typeddict/typeddict_test.go:493)

Commit: 5869dc84bdd3 · Cloud Build Logs

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

Labels

None yet

1 participant