Elffuss BraidHub

kikocis/continue public

add telemetry for match result
Changeset778571452873fbeb807253b3bd2732ded6463fe0760f93cb286a954abd235a8e
AuthorNate <sestinj@gmail.com> · 2025-07-04 20:04 -07:00
CommitterNate <sestinj@gmail.com> · 2025-07-04 20:04 -07:00
Parents02a83b8d1340

Browse the files at this changeset

4 files changed

modified core/edit/searchAndReplace/findSearchMatch.test.ts +27 −0
@@ -14,6 +14,7 @@
1414 expect(result).toEqual({
1515 startIndex: 0,
1616 endIndex: fileContent.length,
17+ strategyName: "exactMatch",
1718 });
1819 });
1920
@@ -32,6 +33,7 @@
3233 expect(result).toEqual({
3334 startIndex: 13, // After "const a = 1;\n"
3435 endIndex: 13 + searchContent.length,
36+ strategyName: "exactMatch",
3537 });
3638 });
3739
@@ -49,6 +51,7 @@
4951 expect(result).toEqual({
5052 startIndex: 13, // After "const a = 1;\n"
5153 endIndex: fileContent.length,
54+ strategyName: "exactMatch",
5255 });
5356 });
5457
@@ -63,6 +66,7 @@
6366 expect(result).toEqual({
6467 startIndex: 13, // After "const a = 1;\n"
6568 endIndex: 13 + searchContent.length,
69+ strategyName: "exactMatch",
6670 });
6771 });
6872
@@ -80,6 +84,7 @@
8084 expect(result).toEqual({
8185 startIndex: 18,
8286 endIndex: 18 + searchContent.length,
87+ strategyName: "exactMatch",
8388 });
8489 });
8590 });
@@ -98,6 +103,7 @@
98103 expect(result).toEqual({
99104 startIndex: 0,
100105 endIndex: fileContent.length, // Uses trimmed length
106+ strategyName: "trimmedMatch",
101107 });
102108 });
103109
@@ -117,6 +123,7 @@
117123 expect(result).toEqual({
118124 startIndex: 12,
119125 endIndex: 12 + searchContent.length,
126+ strategyName: "exactMatch",
120127 });
121128 });
122129
@@ -134,6 +141,7 @@
134141 expect(result).toEqual({
135142 startIndex: expectedStart,
136143 endIndex: expectedStart + trimmedSearchContent.length,
144+ strategyName: "trimmedMatch",
137145 });
138146 });
139147 });
@@ -150,6 +158,7 @@
150158 expect(result).toEqual({
151159 startIndex: 0,
152160 endIndex: 0,
161+ strategyName: "emptySearch",
153162 });
154163 });
155164
@@ -164,6 +173,7 @@
164173 expect(result).toEqual({
165174 startIndex: 0,
166175 endIndex: 0,
176+ strategyName: "emptySearch",
167177 });
168178 });
169179
@@ -176,6 +186,7 @@
176186 expect(result).toEqual({
177187 startIndex: 0,
178188 endIndex: 0,
189+ strategyName: "emptySearch",
179190 });
180191 });
181192 });
@@ -256,6 +267,7 @@
256267 expect(result).toEqual({
257268 startIndex: expectedStart,
258269 endIndex: expectedStart + searchContent.length,
270+ strategyName: "exactMatch",
259271 });
260272 });
261273
@@ -271,6 +283,7 @@
271283 expect(result).toEqual({
272284 startIndex: expectedStart,
273285 endIndex: expectedStart + searchContent.length,
286+ strategyName: "exactMatch",
274287 });
275288 });
276289
@@ -292,6 +305,7 @@
292305 expect(result).toEqual({
293306 startIndex: 0,
294307 endIndex: searchContent.length,
308+ strategyName: "exactMatch",
295309 });
296310 });
297311
@@ -306,6 +320,7 @@
306320 expect(result).toEqual({
307321 startIndex: 0,
308322 endIndex: searchContent.length,
323+ strategyName: "exactMatch",
309324 });
310325 });
311326 });
@@ -326,6 +341,7 @@
326341 expect(result).not.toBeNull();
327342 expect(result!.startIndex).toBe(20); // After "function example() {\n"
328343 expect(result!.endIndex).toBe(72); // End of " return x + y;"
344+ expect(result!.strategyName).toBe("whitespaceIgnoredMatch");
329345
330346 // Verify the matched content contains the original whitespace
331347 const matchedContent = fileContent.substring(
@@ -349,6 +365,7 @@
349365 expect(result).not.toBeNull();
350366 expect(result!.startIndex).toBe(12); // After "const a = 1;"
351367 expect(result!.endIndex).toBe(32); // End of "const b = 2;"
368+ expect(result!.strategyName).toBe("whitespaceIgnoredMatch");
352369
353370 // Verify the matched content preserves original spacing
354371 const matchedContent = fileContent.substring(
@@ -374,6 +391,7 @@
374391 expect(result).not.toBeNull();
375392 expect(result!.startIndex).toBe(17); // After "function test() {"
376393 expect(result!.endIndex).toBe(53); // End of "\t}"
394+ expect(result!.strategyName).toBe("whitespaceIgnoredMatch");
377395
378396 // Verify the matched content preserves original tabs
379397 const matchedContent = fileContent.substring(
@@ -401,6 +419,7 @@
401419 expect(result).not.toBeNull();
402420 expect(result!.startIndex).toBe(13); // After "const obj = {"
403421 expect(result!.endIndex).toBe(83); // End of " key3 : 'value3'"
422+ expect(result!.strategyName).toBe("whitespaceIgnoredMatch");
404423
405424 // Verify the matched content preserves original formatting
406425 const matchedContent = fileContent.substring(
@@ -431,6 +450,7 @@
431450 expect(result).not.toBeNull();
432451 expect(result!.startIndex).toBe(17); // After "function calc() {\n\n"
433452 expect(result!.endIndex).toBe(78); // End of " return result;"
453+ expect(result!.strategyName).toBe("whitespaceIgnoredMatch");
434454
435455 // Verify the matched content includes all original whitespace
436456 const matchedContent = fileContent.substring(
@@ -459,6 +479,7 @@
459479 expect(result).toEqual({
460480 startIndex: 0,
461481 endIndex: 10,
482+ strategyName: "exactMatch",
462483 });
463484 });
464485
@@ -474,6 +495,7 @@
474495 expect(result).toEqual({
475496 startIndex: 0,
476497 endIndex: 0,
498+ strategyName: "emptySearch",
477499 });
478500 });
479501
@@ -498,6 +520,7 @@
498520 expect(result).not.toBeNull();
499521 expect(result!.startIndex).toBe(15); // After "class Example {"
500522 expect(result!.endIndex).toBe(95); // End of " }"
523+ expect(result!.strategyName).toBe("whitespaceIgnoredMatch");
501524
502525 // Verify the matched content preserves original formatting
503526 const matchedContent = fileContent.substring(
@@ -536,6 +559,7 @@
536559 expect(result).toEqual({
537560 startIndex: 0,
538561 endIndex: 10,
562+ strategyName: "exactMatch",
539563 });
540564 });
541565 });
@@ -568,6 +592,7 @@
568592 expect(result).toEqual({
569593 startIndex: expectedStart,
570594 endIndex: expectedStart + searchContent.length,
595+ strategyName: "exactMatch",
571596 });
572597 });
573598
@@ -587,6 +612,7 @@
587612 expect(result).toEqual({
588613 startIndex: expectedStart,
589614 endIndex: expectedStart + searchContent.length,
615+ strategyName: "exactMatch",
590616 });
591617 });
592618
@@ -609,6 +635,7 @@
609635 expect(result).toEqual({
610636 startIndex: 0,
611637 endIndex: searchContent.length,
638+ strategyName: "exactMatch",
612639 });
613640 });
614641 });
modified core/edit/searchAndReplace/findSearchMatch.ts +22 −14
@@ -1,20 +1,28 @@
11 /**
2- * Represents a match result with start and end character positions
2+ * Represents a basic match result with start and end character positions
33 */
4-export interface SearchMatchResult {
4+interface BasicMatchResult {
55 /** The starting character index of the match in the file content */
66 startIndex: number;
77 /** The ending character index of the match in the file content */
88 endIndex: number;
99 }
1010
11+/**
12+ * Represents a match result with start and end character positions
13+ */
14+export interface SearchMatchResult extends BasicMatchResult {
15+ /** The name of the strategy that successfully matched */
16+ strategyName: string;
17+}
18+
1119 /**
1220 * Strategy function type for finding matches
1321 */
1422 type MatchStrategy = (
1523 fileContent: string,
1624 searchContent: string,
17-) => SearchMatchResult | null;
25+) => BasicMatchResult | null;
1826
1927 /**
2028 * Exact string matching strategy
@@ -22,7 +30,7 @@
2230 function exactMatch(
2331 fileContent: string,
2432 searchContent: string,
25-): SearchMatchResult | null {
33+): BasicMatchResult | null {
2634 const exactIndex = fileContent.indexOf(searchContent);
2735 if (exactIndex !== -1) {
2836 return {
@@ -39,7 +47,7 @@
3947 function trimmedMatch(
4048 fileContent: string,
4149 searchContent: string,
42-): SearchMatchResult | null {
50+): BasicMatchResult | null {
4351 const trimmedSearchContent = searchContent.trim();
4452 const trimmedIndex = fileContent.indexOf(trimmedSearchContent);
4553 if (trimmedIndex !== -1) {
@@ -58,7 +66,7 @@
5866 function whitespaceIgnoredMatch(
5967 fileContent: string,
6068 searchContent: string,
61-): SearchMatchResult | null {
69+): BasicMatchResult | null {
6270 // Remove all whitespace (spaces, tabs, newlines, etc.)
6371 const strippedFileContent = fileContent.replace(/\s/g, "");
6472 const strippedSearchContent = searchContent.replace(/\s/g, "");
@@ -109,12 +117,12 @@
109117 }
110118
111119 /**
112- * Ordered list of matching strategies to try
120+ * Ordered list of matching strategies to try with their names
113121 */
114-const matchingStrategies: MatchStrategy[] = [
115- exactMatch,
116- trimmedMatch,
117- whitespaceIgnoredMatch,
122+const matchingStrategies: Array<{ strategy: MatchStrategy; name: string }> = [
123+ { strategy: exactMatch, name: "exactMatch" },
124+ { strategy: trimmedMatch, name: "trimmedMatch" },
125+ { strategy: whitespaceIgnoredMatch, name: "whitespaceIgnoredMatch" },
118126 ];
119127
120128 /**
@@ -137,14 +145,14 @@
137145
138146 if (trimmedSearchContent === "") {
139147 // Empty search content matches the beginning of the file
140- return { startIndex: 0, endIndex: 0 };
148+ return { startIndex: 0, endIndex: 0, strategyName: "emptySearch" };
141149 }
142150
143151 // Try each matching strategy in order
144- for (const strategy of matchingStrategies) {
152+ for (const { strategy, name } of matchingStrategies) {
145153 const result = strategy(fileContent, searchContent);
146154 if (result !== null) {
147- return result;
155+ return { ...result, strategyName: name };
148156 }
149157 }
150158
modified gui/src/util/clientTools/searchReplaceImpl.test.ts +10 −0
@@ -99,6 +99,7 @@
9999 mockFindSearchMatch.mockReturnValue({
100100 startIndex,
101101 endIndex,
102+ strategyName: "exactMatch",
102103 });
103104 mockIdeMessenger.request.mockResolvedValue({ status: "success" });
104105
@@ -173,10 +174,12 @@
173174 .mockReturnValueOnce({
174175 startIndex: firstStartIndex,
175176 endIndex: firstEndIndex,
177+ strategyName: "exactMatch",
176178 })
177179 .mockReturnValueOnce({
178180 startIndex: secondStartIndex,
179181 endIndex: secondEndIndex,
182+ strategyName: "exactMatch",
180183 });
181184
182185 mockIdeMessenger.request.mockResolvedValue({ status: "success" });
@@ -239,6 +242,7 @@
239242 mockFindSearchMatch.mockReturnValue({
240243 startIndex: 10, // Position of "remove this line"
241244 endIndex: 26, // End of "remove this line"
245+ strategyName: "exactMatch",
242246 });
243247 mockIdeMessenger.request.mockResolvedValue({ status: "success" });
244248
@@ -278,6 +282,7 @@
278282 mockFindSearchMatch.mockReturnValue({
279283 startIndex: 6,
280284 endIndex: 11,
285+ strategyName: "exactMatch",
281286 });
282287 mockIdeMessenger.request.mockResolvedValue({ status: "success" });
283288
@@ -344,6 +349,7 @@
344349 .mockReturnValueOnce({
345350 startIndex: 0,
346351 endIndex: 13,
352+ strategyName: "exactMatch",
347353 })
348354 .mockReturnValueOnce(null); // Second search fails
349355
@@ -393,6 +399,7 @@
393399 mockFindSearchMatch.mockReturnValue({
394400 startIndex: 0,
395401 endIndex: 7,
402+ strategyName: "exactMatch",
396403 });
397404 mockIdeMessenger.request.mockRejectedValue(new Error("Apply failed"));
398405
@@ -423,6 +430,7 @@
423430 mockFindSearchMatch.mockReturnValue({
424431 startIndex: 0, // Empty search matches at beginning
425432 endIndex: 0,
433+ strategyName: "exactMatch",
426434 });
427435 mockIdeMessenger.request.mockResolvedValue({ status: "success" });
428436
@@ -457,6 +465,7 @@
457465 mockFindSearchMatch.mockReturnValue({
458466 startIndex: 0,
459467 endIndex: originalContent.length,
468+ strategyName: "exactMatch",
460469 });
461470 mockIdeMessenger.request.mockResolvedValue({ status: "success" });
462471
@@ -492,6 +501,7 @@
492501 mockFindSearchMatch.mockReturnValue({
493502 startIndex: 0,
494503 endIndex: 4,
504+ strategyName: "exactMatch",
495505 });
496506 mockIdeMessenger.request.mockResolvedValue({ status: "success" });
497507
modified gui/src/util/clientTools/searchReplaceImpl.ts +14 −0
@@ -1,6 +1,7 @@
11 import { findSearchMatch } from "core/edit/searchAndReplace/findSearchMatch";
22 import { parseAllSearchReplaceBlocks } from "core/edit/searchAndReplace/parseSearchReplaceBlock";
33 import { resolveRelativePathInDir } from "core/util/ideUtils";
4+import posthog from "posthog-js";
45 import { v4 as uuid } from "uuid";
56 import { ClientToolImpl } from "./callClientTool";
67
@@ -11,6 +12,9 @@
1112 ) => {
1213 const { filepath, diff } = args;
1314
15+ const state = extras.getState();
16+ const allowAnonymousTelemetry = state.config.config.allowAnonymousTelemetry;
17+
1418 const streamId = uuid();
1519
1620 // Resolve the file path
@@ -42,6 +46,16 @@
4246
4347 // Find the search content in the current state of the file
4448 const match = findSearchMatch(currentContent, searchContent || "");
49+
50+ // Because we don't have access to use hooks, we check `allowAnonymousTelemetry`
51+ // directly rather than using `CustomPostHogProvider`
52+ if (allowAnonymousTelemetry) {
53+ // Capture telemetry for tool calls
54+ posthog.capture("find_replace_match_result", {
55+ matchStrategy: match?.strategyName ?? "noMatch",
56+ });
57+ }
58+
4559 if (!match) {
4660 throw new Error(
4761 `Search content not found in block ${i + 1}:\n${searchContent}`,