Skip to content

The agent that grows with you

fix(desktop): grapheme-safe truncation for cron error summaries and job text

Brooklyn Nicholson78315ed0b26a
diff --git a/apps/desktop/src/app/cron/cron-job-model.test.ts b/apps/desktop/src/app/cron/cron-job-model.test.ts
index b6db7f82bf3..0b8fa350182 100644
--- a/apps/desktop/src/app/cron/cron-job-model.test.ts
+++ b/apps/desktop/src/app/cron/cron-job-model.test.ts
@@ -106,6 +106,17 @@ describe('lastErrorSummary', () => {
expect(summary.endsWith('…')).toBe(true)
})
+ it('caps astral error text without splitting a surrogate pair (review follow-up)', () => {
+ // last_error is raw Python exception text; astral characters reach the cap. slice()'s
+ // UTF-16 unit limit used to leave a lone high surrogate that rendered as U+FFFD.
+ // 250 emoji = 500 units > ERROR_SUMMARY_MAX; the old slice(0, 199) cut inside the
+ // pair at unit 198, emitting '\uD83D…'.
+ const summary = lastErrorSummary(`ValueError: ${'😀'.repeat(250)}`)
+
+ expect(summary.endsWith('😀…')).toBe(true)
+ expect(summary.slice(0, -1).endsWith('\uD83D')).toBe(false)
+ })
+
it('returns an empty string for missing input', () => {
expect(lastErrorSummary(null)).toBe('')
expect(lastErrorSummary(undefined)).toBe('')
diff --git a/apps/desktop/src/app/cron/cron-job-model.ts b/apps/desktop/src/app/cron/cron-job-model.ts
index 4a8f832dae5..b74a9dc7092 100644
--- a/apps/desktop/src/app/cron/cron-job-model.ts
+++ b/apps/desktop/src/app/cron/cron-job-model.ts
@@ -1,5 +1,7 @@
import type { CronJob, CronJobUpdates } from '@/types/hermes'
+import { truncateText } from './job-state'
+
const asText = (value: unknown): string => (typeof value === 'string' ? value : '')
/** Script-only cron jobs run a shell script on schedule with no LLM prompt. */
@@ -123,7 +125,10 @@ export function lastErrorSummary(lastError: string | null | undefined): string {
const sentenceEnd = text.search(/\. |\n/)
const sentence = (sentenceEnd === -1 ? text : text.slice(0, sentenceEnd + 1)).trim()
- return sentence.length > ERROR_SUMMARY_MAX ? `${sentence.slice(0, ERROR_SUMMARY_MAX - 1).trimEnd()}…` : sentence
+ // Grapheme-aware cap (review follow-up): slice()'s UTF-16 unit limit could split a surrogate
+ // pair in astral error text. truncateText appends the ellipsis itself, so the old form's
+ // reserved slot (`max - 1`) is expressed by the max passed here.
+ return sentence.length > ERROR_SUMMARY_MAX ? truncateText(sentence, ERROR_SUMMARY_MAX - 1) : sentence
}
/** Build the API update payload, preserving an empty prompt on script-only jobs. */
diff --git a/apps/desktop/src/app/cron/job-state.test.ts b/apps/desktop/src/app/cron/job-state.test.ts
index eb0bc64826d..d7980fb8766 100644
--- a/apps/desktop/src/app/cron/job-state.test.ts
+++ b/apps/desktop/src/app/cron/job-state.test.ts
@@ -13,4 +13,22 @@ describe('truncateText', () => {
it('does not add an ellipsis at the limit', () => {
expect(truncateText('😀😀😀', 3)).toBe('😀😀😀')
})
+
+ // Grapheme clusters, not code points: the same shapes profile-glyph.test.tsx pins for the
+ // profile rail. A job name is raw user input, so these are plausible values, not exotica.
+ it('never cuts a grapheme cluster mid-sequence', () => {
+ const cases: Array<[string, number, string]> = [
+ ['🇨🇳xy', 1, '🇨🇳…'],
+ ['🇨🇳xy', 3, '🇨🇳xy'],
+ ['👍🏽x', 1, '👍🏽…'],
+ ['e\u0301x', 1, 'e\u0301…'],
+ ['👨‍👩‍👧‍👦xyz', 1, '👨‍👩‍👧‍👦…'],
+ ['👨‍👩‍👧‍👦xyz', 3, '👨‍👩‍👧‍👦xy…'],
+ ['1️⃣ 每日简报', 1, '1️⃣…']
+ ]
+
+ for (const [value, max, expected] of cases) {
+ expect(truncateText(value, max)).toBe(expected)
+ }
+ })
})
diff --git a/apps/desktop/src/app/cron/job-state.ts b/apps/desktop/src/app/cron/job-state.ts
index a6f1f7a40de..206e392f2a9 100644
--- a/apps/desktop/src/app/cron/job-state.ts
+++ b/apps/desktop/src/app/cron/job-state.ts
@@ -19,10 +19,15 @@ export function jobState(job: CronJob): string {
return state || (job.enabled === false ? 'disabled' : 'scheduled')
}
+// Grapheme segmentation, the same idiom as profile-short-label.ts: code points alone still
+// cut ZWJ sequences, flags, skin tones, and combining marks mid-cluster. A job name is raw
+// user input, so "🇨🇳 每日简报" is a plausible value to keep intact.
+const truncateGraphemes = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
+
export function truncateText(value: string, max: number): string {
- const codePoints = Array.from(value)
+ const graphemes = Array.from(truncateGraphemes.segment(value), ({ segment }) => segment)
- return codePoints.length > max ? `${codePoints.slice(0, max).join('')}…` : value
+ return graphemes.length > max ? `${graphemes.slice(0, max).join('')}…` : value
}
// Human label for a job: name → first 60 of prompt → first 60 of script → id.