Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): reject an invalid --watch --interval instead of looping at ~1 ms (#5931)

garrytanc60caebb82f0
diff --git a/scripts/test-weights.json b/scripts/test-weights.json
index e7e1f0e9ed..3697c21a45 100644
--- a/scripts/test-weights.json
+++ b/scripts/test-weights.json
@@ -1921,6 +1921,7 @@
"test/sync-trigger-cli.test.ts": 419,
"test/sync-walker-submodule.test.ts": 2,
"test/sync-walker-symlink.test.ts": 24,
+ "test/sync-watch-interval.test.ts": 1000,
"test/sync-working-tree-cli.test.ts": 712,
"test/sync-working-tree.test.ts": 1451,
"test/sync.test.ts": 7676,
diff --git a/scripts/ubicloud/weights.json b/scripts/ubicloud/weights.json
index f53d3a8d7a..a842f557cd 100644
--- a/scripts/ubicloud/weights.json
+++ b/scripts/ubicloud/weights.json
@@ -2433,6 +2433,7 @@
"unit:test/sync-trigger-cli.test.ts": 1438,
"unit:test/sync-walker-submodule.test.ts": 479,
"unit:test/sync-walker-symlink.test.ts": 263,
+ "unit:test/sync-watch-interval.test.ts": 1000,
"unit:test/sync-working-tree-cli.test.ts": 3734,
"unit:test/sync-working-tree.test.ts": 3829,
"unit:test/sync.test.ts": 8892,
diff --git a/src/commands/sync/args.ts b/src/commands/sync/args.ts
index ea0cecf059..f26201f3a3 100644
--- a/src/commands/sync/args.ts
+++ b/src/commands/sync/args.ts
@@ -9,6 +9,7 @@ import { parseDurationSeconds, parseWorkers } from '../../core/sync-concurrency.
import { resolveNoEmbed } from '../../core/sync-git.ts';
import type { SyncOpts } from '../sync.ts';
import { parseMissingPathMode } from './missing-path.ts';
+import { intFlagValue } from '../../cli/flag-values.ts';
import type { MissingPathMode } from './missing-path.ts';
export function printSyncHelp(): void {
@@ -115,11 +116,16 @@ See also:
}
/** Flags read before the `--break-lock` branch. */
+/** setTimeout treats delays above 2^31-1 ms as ~1 ms; --interval stays within that bound. */
+const MAX_WATCH_INTERVAL_SECONDS = Math.floor(2_147_483_647 / 1000);
+
export function parseSyncFlags(args: string[]) {
const repoPath = args.find((a, i) => args[i - 1] === '--repo') || undefined;
const watch = args.includes('--watch');
- const intervalStr = args.find((a, i) => args[i - 1] === '--interval');
- const interval = intervalStr ? parseInt(intervalStr, 10) : 60;
+ // #5931 (D4): timers coerce delays above 2^31-1 ms (and NaN/0) to ~1 ms, so a bad
+ // --interval used to spin the watch loop; it is a usage error (exit 2) instead.
+ const intervalAt = args.indexOf('--interval');
+ const interval = intervalAt === -1 ? 60 : intFlagValue(args[intervalAt + 1], '--interval', { min: 1, max: MAX_WATCH_INTERVAL_SECONDS, example: 60 });
const dryRun = args.includes('--dry-run');
const full = args.includes('--full');
const noPull = args.includes('--no-pull');
diff --git a/test/sync-watch-interval.test.ts b/test/sync-watch-interval.test.ts
new file mode 100644
index 0000000000..2f6e6e8a12
--- /dev/null
+++ b/test/sync-watch-interval.test.ts
@@ -0,0 +1,28 @@
+/**
+ * #5931 (absorbed in agent contract v1 D4): `sync --watch --interval` rejects
+ * values that Node/Bun timers coerce to an approximately immediate delay, as
+ * an invalid_params usage error (exit 2 via renderCliError). Protects the CLI
+ * boundary without starting a sync loop or touching a brain.
+ */
+import { describe, expect, test } from 'bun:test';
+import { parseSyncFlags } from '../src/commands/sync/args.ts';
+
+describe('sync watch interval', () => {
+ test('keeps the default and accepts a positive integer number of seconds', () => {
+ expect(parseSyncFlags([]).interval).toBe(60);
+ expect(parseSyncFlags(['--watch', '--interval', '15']).interval).toBe(15);
+ expect(parseSyncFlags(['--watch', '--interval', '2147483']).interval).toBe(2147483);
+ });
+
+ test.each(['oops', '0', '-1', '1.5', '3abc', '2147484', '9007199254740992'])('%s is rejected before watch starts', (value) => {
+ expect(() => parseSyncFlags(['--watch', '--interval', value]))
+ .toThrow(expect.objectContaining({ code: 'invalid_params', message: expect.stringContaining('--interval must be an integer from 1 to 2147483') }));
+ });
+
+ test.each(['missing', 'flag-shaped'])('a %s interval value is rejected', (caseName) => {
+ const args = ['--watch', '--interval'];
+ if (caseName === 'flag-shaped') args.push('--watch');
+ expect(() => parseSyncFlags(args))
+ .toThrow(expect.objectContaining({ code: 'invalid_params', message: expect.stringContaining('--interval requires a value') }));
+ });
+});