Skip to content

Commit afac389

Browse files
authored
perf: reduce chat history metadata scanning (#133635)
1 parent d2948c7 commit afac389

2 files changed

Lines changed: 12 additions & 21 deletions

File tree

‎src/auto-reply/heartbeat.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ export function stripHeartbeatToken(
162162
if (!trimmed) {
163163
return { shouldSkip: true, text: "", didStrip: false };
164164
}
165+
// Markup cleanup inserts spaces or removes edge wrappers; it cannot create this token.
166+
if (!trimmed.includes(HEARTBEAT_TOKEN)) {
167+
return { shouldSkip: false, text: trimmed, didStrip: false };
168+
}
165169

166170
const mode: StripHeartbeatMode = opts.mode ?? "message";
167171
const maxAckCharsRaw = opts.maxAckChars;
@@ -187,10 +191,6 @@ export function stripHeartbeatToken(
187191
.replace(/[*`~_]+$/, "");
188192

189193
const trimmedNormalized = stripMarkup(trimmed);
190-
const hasToken = trimmed.includes(HEARTBEAT_TOKEN) || trimmedNormalized.includes(HEARTBEAT_TOKEN);
191-
if (!hasToken) {
192-
return { shouldSkip: false, text: trimmed, didStrip: false };
193-
}
194194

195195
const strippedOriginal = stripTokenAtEdges(trimmed);
196196
const strippedNormalized = stripTokenAtEdges(trimmedNormalized);

‎src/auto-reply/reply/strip-inbound-meta.ts‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,14 @@ function isInboundContextHeaderLine(line: string): boolean {
3838
return t.length > INBOUND_CONTEXT_MARKER.length && t.endsWith(INBOUND_CONTEXT_MARKER);
3939
}
4040

41-
// Pre-compiled fast-path regex — avoids line-by-line parse when no blocks present.
42-
// Active-memory's bare Context: sentinel is valid only as a complete line.
43-
const SENTINEL_SUBSTRING_ALTERNATIVES = [INBOUND_CONTEXT_MARKER, ...MESSAGE_TOOL_DELIVERY_HINTS]
44-
.map((sentinel) => sentinel.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
45-
.join("|");
46-
const ACTIVE_MEMORY_HEADER_ESCAPED = ACTIVE_MEMORY_CONTEXT_HEADER.replace(
47-
/[.*+?^${}()|[\]\\]/g,
48-
"\\$&",
49-
);
50-
const SENTINEL_FAST_RE = new RegExp(
51-
`${SENTINEL_SUBSTRING_ALTERNATIVES}|^[ \t]*${ACTIVE_MEMORY_HEADER_ESCAPED}[ \t]*$`,
52-
"m",
53-
);
54-
5541
/** Fast check for whether text contains any inbound metadata sentinel. */
5642
export function hasInboundMetadataSentinel(text: string): boolean {
57-
return Boolean(text && SENTINEL_FAST_RE.test(text));
43+
return (
44+
text.includes(INBOUND_CONTEXT_MARKER) ||
45+
MESSAGE_TOOL_DELIVERY_HINTS.some((hint) => text.includes(hint)) ||
46+
// Active-memory's bare Context: sentinel is valid only as a complete line.
47+
(text.includes(ACTIVE_MEMORY_CONTEXT_HEADER) && /^[ \t]*Context:[ \t]*$/m.test(text))
48+
);
5849
}
5950

6051
function isMessageToolDeliveryHintLine(line: string): boolean {
@@ -212,7 +203,7 @@ export function stripInboundMetadata(text: string): string {
212203
}
213204

214205
const withoutTimestamp = text.replace(LEADING_TIMESTAMP_PREFIX_RE, "");
215-
if (!SENTINEL_FAST_RE.test(withoutTimestamp)) {
206+
if (!hasInboundMetadataSentinel(withoutTimestamp)) {
216207
return withoutTimestamp;
217208
}
218209

@@ -282,7 +273,7 @@ export function stripInboundMetadata(text: string): string {
282273

283274
/** Strips only leading inbound metadata blocks while preserving later user text. */
284275
export function stripLeadingInboundMetadata(text: string): string {
285-
if (!text || !SENTINEL_FAST_RE.test(text)) {
276+
if (!hasInboundMetadataSentinel(text)) {
286277
return text;
287278
}
288279

0 commit comments

Comments
 (0)