Skip to content

Commit d89e4a4

Browse files
dimitarproynovilayaperumalg
authored andcommitted
Use full length SHA256 hash for contextHash
Prevents collisions at sizes of 70000 prompts or more Closes #6746 Signed-off-by: Dimitar Proynov <[email protected]>
1 parent 3fc390f commit d89e4a4

3 files changed

Lines changed: 46 additions & 24 deletions

File tree

‎vector-stores/spring-ai-redis-semantic-cache/src/main/java/org/springframework/ai/chat/cache/semantic/SemanticCacheAdvisor.java‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,40 +214,28 @@ private String extractUserText(ChatClientRequest request) {
214214
*/
215215
private @Nullable String extractContextHash(ChatClientRequest request) {
216216
var systemMessage = request.prompt().getSystemMessage();
217-
if (systemMessage != null && systemMessage.getText() != null && !systemMessage.getText().isEmpty()) {
217+
if (systemMessage.getText() != null && !systemMessage.getText().isEmpty()) {
218218
return computeHash(systemMessage.getText());
219219
}
220220
return null;
221221
}
222222

223223
/**
224-
* Computes a deterministic hash for the given string. Uses the first 8 characters of
225-
* the SHA-256 hash to create a compact but unique identifier.
224+
* Computes a deterministic hash for the given string. Uses a SHA-256 hash to create a
225+
* unique identifier.
226226
* @param text the text to hash
227-
* @return an 8-character hash string
227+
* @return a 64-character hash string
228228
*/
229229
private String computeHash(String text) {
230230
try {
231231
java.security.MessageDigest digest = java.security.MessageDigest.getInstance("SHA-256");
232232
byte[] hash = digest.digest(text.getBytes(java.nio.charset.StandardCharsets.UTF_8));
233-
StringBuilder hexString = new StringBuilder();
234-
// Take first 4 bytes of SHA-256 hash → 8 hex characters
235-
// (4 billion possible values - sufficient for context differentiation)
236-
for (int i = 0; i < 4; i++) {
237-
// 0xff mask converts signed byte (-128..127) to unsigned (0..255)
238-
// ensuring correct hex representation (e.g., byte -1 → "ff", not
239-
// "ffffffff")
240-
String hex = Integer.toHexString(0xff & hash[i]);
241-
if (hex.length() == 1) {
242-
hexString.append('0');
243-
}
244-
hexString.append(hex);
245-
}
246-
return hexString.toString();
233+
return java.util.HexFormat.of().formatHex(hash);
247234
}
248235
catch (java.security.NoSuchAlgorithmException e) {
249-
// SHA-256 is always available in Java, but fallback to hashCode if needed
250-
return Integer.toHexString(text.hashCode());
236+
// possible only on a broken/tampered with java.security provider
237+
// configuration
238+
throw new IllegalStateException(e);
251239
}
252240
}
253241

‎vector-stores/spring-ai-redis-semantic-cache/src/test/java/org/springframework/ai/chat/cache/semantic/SemanticCacheAdvisorIT.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,8 @@ void testContextHashStoredInMetadata() {
840840
// Verify context_hash is stored in metadata
841841
assertThat(cachedDoc.getMetadata()).containsKey("context_hash");
842842
String storedHash = (String) cachedDoc.getMetadata().get("context_hash");
843-
assertThat(storedHash).isNotNull().hasSize(8); // 8 hex chars from SHA-256
843+
assertThat(storedHash).isNotNull().hasSize(64); // 64 hex chars (32 bytes) from
844+
// SHA-256
844845
}
845846

846847
@Test

‎vector-stores/spring-ai-redis-semantic-cache/src/test/java/org/springframework/ai/chat/cache/semantic/SemanticCacheAdvisorTests.java‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package org.springframework.ai.chat.cache.semantic;
1818

19+
import java.nio.charset.StandardCharsets;
20+
import java.security.MessageDigest;
21+
import java.security.NoSuchAlgorithmException;
22+
import java.util.HexFormat;
1923
import java.util.List;
2024
import java.util.Optional;
2125

@@ -89,11 +93,12 @@ void adviseCallWithSystemPromptUsesContextHash() {
8993
verify(this.mockCache).get(queryCaptor.capture(), hashCaptor.capture());
9094

9195
assertThat(queryCaptor.getValue()).isEqualTo(userText);
92-
assertThat(hashCaptor.getValue()).isNotNull().hasSize(8); // 8 hex chars
96+
assertThat(hashCaptor.getValue()).isNotNull().hasSize(64); // 64 hex chars (32
97+
// bytes)
9398

9499
// Assert - verify cache.set was called with same parameters
95100
verify(this.mockCache).set(eq(userText), eq(chatResponse), hashCaptor.capture());
96-
assertThat(hashCaptor.getValue()).isNotNull().hasSize(8);
101+
assertThat(hashCaptor.getValue()).isNotNull().hasSize(64);
97102
}
98103

99104
@Test
@@ -224,10 +229,38 @@ void emptySystemPromptTreatedAsNoSystemPrompt() {
224229
this.advisor.adviseCall(request, this.mockChain);
225230

226231
// Assert - empty system prompt should result in null context hash
227-
verify(this.mockCache).get(eq(userText), (String) isNull());
232+
verify(this.mockCache).get(eq(userText), isNull());
228233
verify(this.mockCache).set(eq(userText), eq(chatResponse), (String) isNull());
229234
}
230235

236+
@Test
237+
void contextHashMatchesFullSha256Digest() throws NoSuchAlgorithmException {
238+
String systemPromptText = "You are a helpful assistant.";
239+
String userText = "What is AI?";
240+
241+
Prompt prompt = new Prompt(List.of(new SystemMessage(systemPromptText), new UserMessage(userText)));
242+
243+
ChatClientRequest request = ChatClientRequest.builder().prompt(prompt).build();
244+
245+
ChatResponse chatResponse = createMockChatResponse("AI is artificial intelligence.");
246+
ChatClientResponse clientResponse = ChatClientResponse.builder().chatResponse(chatResponse).build();
247+
248+
when(this.mockCache.get(eq(userText), any(String.class))).thenReturn(Optional.empty());
249+
when(this.mockChain.nextCall(request)).thenReturn(clientResponse);
250+
251+
this.advisor.adviseCall(request, this.mockChain);
252+
253+
ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class);
254+
verify(this.mockCache).get(eq(userText), hashCaptor.capture());
255+
256+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
257+
byte[] expectedHash = digest.digest(systemPromptText.getBytes(StandardCharsets.UTF_8));
258+
String expectedHex = HexFormat.of().formatHex(expectedHash);
259+
260+
assertThat(hashCaptor.getValue()).isEqualTo(expectedHex);
261+
assertThat(hashCaptor.getValue()).matches("^[0-9a-f]{64}$");
262+
}
263+
231264
private ChatResponse createMockChatResponse(String text) {
232265
return ChatResponse.builder().generations(List.of(new Generation(new AssistantMessage(text)))).build();
233266
}

0 commit comments

Comments
 (0)