Skip to content

Commit 568a539

Browse files
waleedlatif1claude
andauthored
fix(workflow-renderer): remove quadratic backtracking in reference highlighting (#7672)
The env-var branch of the display-text scanner used `\{\{[^}]+\}\}`. Because the character class admits `{`, a run of unmatched braces restarts a full backtracking scan at every offset, making the scan quadratic — 100k braces took 8.3s. Excluding `{` from the class makes it linear (~1ms) and matches the `[^{}]+` form the executor's placeholder resolvers already use, so highlighting no longer marks references the executor would never resolve. Claude-Session: https://claude.ai/code/session_0139x4ngJzcoYrjwsS9B2ZG4 Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 575bc6b commit 568a539

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

packages/workflow-renderer/src/formatted-text.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,21 @@ describe('shared reference formatting', () => {
104104
expect((html.match(/class=/g) ?? []).length).toBe(2)
105105
})
106106
})
107+
108+
describe('brace scanning', () => {
109+
it('highlights the innermost environment reference inside repeated braces', () => {
110+
const html = renderToStaticMarkup(
111+
<>{formatDisplayText('{{{{KEY}}', { availableEnvVars: new Set(['KEY']) })}</>
112+
)
113+
114+
expect(html).toContain('<span>{{</span>')
115+
expect(html).toContain('text-[var(--brand-secondary)]')
116+
expect(html).toContain('{{KEY}}')
117+
})
118+
119+
it('scans unterminated brace runs in linear time', () => {
120+
const started = performance.now()
121+
formatDisplayText('{'.repeat(100_000))
122+
expect(performance.now() - started).toBeLessThan(1_000)
123+
})
124+
})

packages/workflow-renderer/src/formatted-text.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function formatDisplayTextInternal(
107107
}
108108

109109
const nodes: ReactNode[] = []
110-
const regex = /<[^<>]+>|\{\{[^}]+\}\}/g
110+
const regex = /<[^<>]+>|\{\{[^{}]+\}\}/g
111111
let lastIndex = 0
112112
let key = 0
113113

0 commit comments

Comments
 (0)