#5630: refactor: extract unified withTimeout utility
channel: slack
## Summary
Extracts a unified `withTimeout` utility function to reduce code duplication across the codebase.
## Changes
- Add `src/utils/with-timeout.ts` with configurable timeout and custom error message
- Add comprehensive unit tests for `withTimeout`
- Add **consistency tests** to verify behavior matches original implementations
- Refactor `slack/probe.ts` to use unified `withTimeout`
- Refactor `line/probe.ts` to use unified `withTimeout`
## Consistency Verification
Created dedicated consistency tests that compare the new implementation against the original inline implementations:
| Test Case | Original | New | Status |
|-----------|----------|-----|--------|
| Normal completion | ✅ | ✅ | Match |
| Timeout error message | `"timeout"` | `"timeout"` | Match |
| timeoutMs=0 | Returns directly | Returns directly | Match |
| timeoutMs=-1 | Returns directly | Returns directly | Match |
| Error propagation | ✅ | ✅ | Match |
## Benefits
- Reduces code duplication (7+ similar implementations in codebase)
- Consistent behavior across all usages
- Easier to maintain and test
- Other files can be refactored incrementally
## Testing
- 7 unit tests for `withTimeout`
- 5 consistency tests comparing with original implementation
- 128 existing tests in `slack/` and `line/` modules pass
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR introduces a shared `src/utils/with-timeout.ts` helper and updates the Slack and LINE probe flows to use it (passing `"timeout"` to preserve the old error string). It also adds unit tests plus “consistency” tests that compare the new helper against the previous inline implementations.
Overall the refactor looks low-risk and the new helper matches the prior behavior for the call sites touched. The main items to watch are (1) `withTimeout`’s `!timeoutMs` check, which can silently treat `NaN` the same as `0` and skip timeouts, and (2) one timing-based unit test that’s likely to be flaky in CI.
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with minor follow-up tweaks.
- The change is a contained refactor with good test coverage and call-site preservation of the legacy timeout error message. Remaining concerns are limited to edge-case input handling (`NaN`/falsy timeout values) and a potentially flaky, timing-based test; neither suggests a broad behavioral regression for typical usage.
- src/utils/with-timeout.ts; src/utils/with-timeout.test.ts
<!-- greptile_other_comments_section -->
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#11874: fix: handle fetch rejections in provider usage withTimeout
by Zjianru · 2026-02-08
76.9%
#6463: fix(telegram): improve timeout handling and prevent channel exits
by ai-fanatic · 2026-02-01
76.2%
#4086: Test/add backoff tests
by TechWizard9999 · 2026-01-29
73.3%
#8332: fix: add per-tool-call timeout to prevent agent hangs (v2 - fixes m...
by vishaltandale00 · 2026-02-03
73.2%
#9114: Fix: Reduce NO_TIMEOUT_MS to 24 days to avoid Node.js overflow warning
by vishaltandale00 · 2026-02-04
73.0%
#17770: refactor(cli): reuse shared option builders
by iyoda · 2026-02-16
72.4%
#6268: fix: add timeout to compaction retry to prevent session lockout
by batumilove · 2026-02-01
72.3%
#20496: test(utils): add comprehensive unit tests for utility functions
by masifislamm · 2026-02-19
71.9%
#6302: fix: Add timeouts to prevent indefinite hangs (issues #4954, #4956,...
by batumilove · 2026-02-01
71.8%
#7085: test: skip flaky workspace-paths & safe-bins tests on non-Linux/CI ...
by ThinkIbrokeIt · 2026-02-02
71.8%