-
-
Notifications
You must be signed in to change notification settings - Fork 316
Amp #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary of ChangesHello @hkfires, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the configuration management for the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
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 introduces changes to support hot-reloading of Amp-related configurations, specifically the upstream API key and model mappings. The changes are logical, but I've identified a few areas for improvement. There's a redundant function call, an opportunity to use more idiomatic Go for mutex handling, and a significant issue with the hashing logic for model mappings that could lead to collisions. My review includes suggestions to address these points.
| func summarizeAmpModelMappings(mappings []config.AmpModelMapping) ampModelMappingsSummary { | ||
| if len(mappings) == 0 { | ||
| return ampModelMappingsSummary{} | ||
| } | ||
| entries := make([]string, 0, len(mappings)) | ||
| for _, mapping := range mappings { | ||
| from := strings.TrimSpace(mapping.From) | ||
| to := strings.TrimSpace(mapping.To) | ||
| if from == "" && to == "" { | ||
| continue | ||
| } | ||
| entries = append(entries, from+"->"+to) | ||
| } | ||
| if len(entries) == 0 { | ||
| return ampModelMappingsSummary{} | ||
| } | ||
| sort.Strings(entries) | ||
| sum := sha256.Sum256([]byte(strings.Join(entries, "|"))) | ||
| return ampModelMappingsSummary{ | ||
| hash: hex.EncodeToString(sum[:]), | ||
| count: len(entries), | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current method of creating a hash for model mappings by concatenating strings with -> and | is brittle and can lead to hash collisions if model names contain these characters. For example, the list of mappings [{from: "a", to: "b|c"}, {from: "d", to: "e"}] would produce the same hash as [{from: "a", to: "b"}, {from: "c|d", to: "e"}]. A more robust approach, consistent with other hashing functions in this file (e.g., computeExcludedModelsHash), is to marshal the structured data into JSON before hashing. This ensures an unambiguous representation.
func summarizeAmpModelMappings(mappings []config.AmpModelMapping) ampModelMappingsSummary {
if len(mappings) == 0 {
return ampModelMappingsSummary{}
}
type mappingForHash struct {
From string `json:"from"`
To string `json:"to"`
}
canonical := make([]mappingForHash, 0, len(mappings))
for _, mapping := range mappings {
from := strings.TrimSpace(mapping.From)
to := strings.TrimSpace(mapping.To)
if from == "" && to == "" {
continue
}
canonical = append(canonical, mappingForHash{From: from, To: to})
}
if len(canonical) == 0 {
return ampModelMappingsSummary{}
}
sort.Slice(canonical, func(i, j int) bool {
if canonical[i].From != canonical[j].From {
return canonical[i].From < canonical[j].From
}
return canonical[i].To < canonical[j].To
})
data, err := json.Marshal(canonical)
if err != nil {
return ampModelMappingsSummary{count: len(canonical)}
}
sum := sha256.Sum256(data)
return ampModelMappingsSummary{
hash: hex.EncodeToString(sum[:]),
count: len(canonical),
}
}| if m.secretSource != nil { | ||
| if ms, ok := m.secretSource.(*MultiSourceSecret); ok { | ||
| ms.UpdateExplicitKey(settings.UpstreamAPIKey) | ||
| ms.InvalidateCache() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| s.mu.Lock() | ||
| s.explicitKey = strings.TrimSpace(key) | ||
| s.cache = nil | ||
| s.mu.Unlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For safer concurrency and to follow idiomatic Go, it's best to use defer to unlock the mutex immediately after locking it. This guarantees that the mutex will be unlocked even if a panic occurs within the function.
| s.mu.Lock() | |
| s.explicitKey = strings.TrimSpace(key) | |
| s.cache = nil | |
| s.mu.Unlock() | |
| s.mu.Lock() | |
| defer s.mu.Unlock() | |
| s.explicitKey = strings.TrimSpace(key) | |
| s.cache = nil |
No description provided.