fix(utils): share clamp helpers

Co-authored-by: Gustavo Madeira Santana <gumadeiras@gmail.com>
This commit is contained in:
Gustavo Madeira Santana
2026-01-18 06:18:05 -05:00
committed by Peter Steinberger
parent 810394f43b
commit 7252938339
3 changed files with 11 additions and 5 deletions

View File

@@ -26,6 +26,7 @@ Docs: https://docs.clawd.bot
||||||| parent of 903e9be49 (feat: surface node core/ui versions in macOS)
### Fixes
- Tools: clamp bash foreground duration using shared helpers.
- Auth profiles: keep auto-pinned preference while allowing rotation on failover; user pins stay locked. (#1138) — thanks @cheeeee.
- macOS: avoid touching launchd in Remote over SSH so quitting the app no longer disables the remote gateway. (#1105)
- Memory: index atomically so failed reindex preserves the previous memory database. (#1151)

View File

@@ -5,6 +5,7 @@ import { resolveSandboxRuntimeStatus } from "../../agents/sandbox.js";
import { killProcessTree } from "../../agents/shell-utils.js";
import type { ClawdbotConfig } from "../../config/config.js";
import { logVerbose } from "../../globals.js";
import { clampInt } from "../../utils.js";
import type { MsgContext } from "../templating.js";
import type { ReplyPayload } from "../types.js";
import { stripMentions, stripStructuralPrefixes } from "./mentions.js";
@@ -31,14 +32,10 @@ type ActiveBashJob =
let activeJob: ActiveBashJob | null = null;
function clampNumber(value: number, min: number, max: number) {
return Math.min(Math.max(value, min), max);
}
function resolveForegroundMs(cfg: ClawdbotConfig): number {
const raw = cfg.commands?.bashForegroundMs;
if (typeof raw !== "number" || Number.isNaN(raw)) return DEFAULT_FOREGROUND_MS;
return clampNumber(Math.floor(raw), 0, MAX_FOREGROUND_MS);
return clampInt(raw, 0, MAX_FOREGROUND_MS);
}
function formatSessionSnippet(sessionId: string) {

View File

@@ -8,6 +8,14 @@ export async function ensureDir(dir: string) {
await fs.promises.mkdir(dir, { recursive: true });
}
export function clampNumber(value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, value));
}
export function clampInt(value: number, min: number, max: number): number {
return clampNumber(Math.floor(value), min, max);
}
export type WebChannel = "web";
export function assertWebChannel(input: string): asserts input is WebChannel {