Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

collapse clampToCharLimit onto trimToClause so textUtils has one sentence-cut algorithm (#6455)

[._.]/ Adam Eivyc3ba2e3c0906
diff --git a/server/lib/textUtils.js b/server/lib/textUtils.js
index 677b2d7eb7..b939d700e7 100644
--- a/server/lib/textUtils.js
+++ b/server/lib/textUtils.js
@@ -115,16 +115,10 @@ export function trimToClause(v, max) {
/**
* Bound a string to a hard character cap, cutting on a natural boundary.
*
- * `trimTo` above slices blindly, which is right for a log field and wrong for
- * text a human or a renderer reads: a mid-word cut reads as corruption, and in
- * a render prompt it can change what the final phrase asks for. Cut at the last
- * sentence end when one sits deep enough in the allowance (60%+, so an
- * abbreviation or a decimal near the start can't throw the prompt away), else
- * at the last word boundary, else at the cap.
- *
- * Returns `{ text, truncated }` so the caller can TELL the user the text was
- * cut — a silent trim reads as the model losing detail on its own. A
- * non-positive/non-finite `max` means "no cap" and passes the text through.
+ * Delegates to `trimToClause` for sentence-aware boundary detection. Returns
+ * `{ text, truncated }` so the caller can TELL the user the text was cut — a
+ * silent trim reads as the model losing detail on its own. A non-positive/
+ * non-finite `max` means "no cap" and passes the text through.
*
* Distinct from the two capping helpers that append a marker — `clampText`
* (`promptFencing.js`, `… [truncated]`) and `truncateForTelegram`
@@ -139,13 +133,7 @@ export function trimToClause(v, max) {
export function clampToCharLimit(text, max) {
const value = typeof text === 'string' ? text : '';
if (!Number.isFinite(max) || max <= 0 || value.length <= max) return { text: value, truncated: false };
- const cut = value.slice(0, max);
- const sentenceEnd = Math.max(cut.lastIndexOf('.'), cut.lastIndexOf('!'), cut.lastIndexOf('?'));
- const wordEnd = cut.lastIndexOf(' ');
- const at = sentenceEnd >= max * 0.6 ? sentenceEnd + 1
- : wordEnd > 0 ? wordEnd
- : max;
- return { text: cut.slice(0, at).trim(), truncated: true };
+ return { text: trimToClause(value, max), truncated: true };
}
/**
diff --git a/server/lib/textUtils.test.js b/server/lib/textUtils.test.js
index 1270787654..077f3ac7a4 100644
--- a/server/lib/textUtils.test.js
+++ b/server/lib/textUtils.test.js
@@ -55,6 +55,18 @@ describe('clampToCharLimit', () => {
// change what the tail asks the renderer for.
expect(clampToCharLimit('alpha bravo charlie delta', 14)).toEqual({ text: 'alpha bravo', truncated: true });
});
+
+ it('does not cut mid-abbreviation when an abbreviation sits within the budget', () => {
+ // When an abbreviation like "Dr." appears early in the text but near enough
+ // to the cap, the abbreviation guard should prevent treating it as a sentence
+ // end. This test verifies the abbreviation protection works end-to-end through
+ // clampToCharLimit by ensuring we don't clip mid-abbreviation.
+ const input = 'The patient called Dr. Smith, a specialist, to discuss the diagnosis';
+ const { text, truncated } = clampToCharLimit(input, 50);
+ expect(truncated).toBe(true);
+ // Should not break in the middle of "Dr." or leave an incomplete abbreviation
+ expect(text).not.toMatch(/\bDr$/);
+ });
});
describe('escapeRegExp', () => {