diff --git a/v3/@claude-flow/cli/__tests__/update-ci-detection.test.ts b/v3/@claude-flow/cli/__tests__/update-ci-detection.test.ts new file mode 100644 index 0000000000..1a754b0b6e --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/update-ci-detection.test.ts @@ -0,0 +1,36 @@ +/** + * The startup update check must skip CI runs however CI is spelled. It matched only + * `CI=true`, so a `CI=1` run checked, found a newer published CLI and ran + * `npm install @claude-flow/cli@ --save-exact` in the working directory during `ruflo init`. + */ +import { afterEach, describe, expect, it } from 'vitest'; +import { isCI, shouldCheckForUpdates } from '../src/update/rate-limiter.js'; + +const saved = { CI: process.env.CI, CONTINUOUS_INTEGRATION: process.env.CONTINUOUS_INTEGRATION }; +afterEach(() => { + for (const [k, v] of Object.entries(saved)) { + if (v === undefined) delete process.env[k]; + else process.env[k] = v; + } +}); + +describe('update check: CI detection', () => { + it.each(['true', '1', 'TRUE', 'yes', 'woodpecker'])('CI=%s is CI', (value) => { + expect(isCI({ CI: value })).toBe(true); + }); + + it.each(['', '0', 'false', 'False'])('CI=%j is not CI', (value) => { + expect(isCI({ CI: value })).toBe(false); + }); + + it('CONTINUOUS_INTEGRATION counts too; no variable is not CI', () => { + expect(isCI({ CONTINUOUS_INTEGRATION: '1' })).toBe(true); + expect(isCI({})).toBe(false); + }); + + it('shouldCheckForUpdates skips a CI=1 run', () => { + delete process.env.CONTINUOUS_INTEGRATION; + process.env.CI = '1'; + expect(shouldCheckForUpdates()).toEqual({ allowed: false, reason: 'CI environment detected' }); + }); +}); diff --git a/v3/@claude-flow/cli/src/update/rate-limiter.ts b/v3/@claude-flow/cli/src/update/rate-limiter.ts index eb8676128c..c977aa331d 100644 --- a/v3/@claude-flow/cli/src/update/rate-limiter.ts +++ b/v3/@claude-flow/cli/src/update/rate-limiter.ts @@ -60,11 +60,23 @@ export function saveState(state: RateLimitState): void { fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2)); } +/** + * Whether this is a CI run. CI systems set `CI` to `true`, `1` or other values, so any value + * except empty, `0` or `false` counts (GitHub Actions sets CI=true). Matching only `true` let CI=1 runs check for updates and + * install one (`npm install @claude-flow/cli@`) into the working directory mid-run. + */ +export function isCI(env: NodeJS.ProcessEnv = process.env): boolean { + return ['CI', 'CONTINUOUS_INTEGRATION'].some((name) => { + const value = env[name]; + return value !== undefined && value.trim() !== '' && !/^(0|false)$/i.test(value.trim()); + }); +} + export function shouldCheckForUpdates( intervalHours: number = DEFAULT_INTERVAL_HOURS ): { allowed: boolean; reason?: string } { // Skip in CI environments - if (process.env.CI === 'true' || process.env.CONTINUOUS_INTEGRATION === 'true') { + if (isCI()) { return { allowed: false, reason: 'CI environment detected' }; }