/** * End-to-end: memory, curation and knowledge are MACHINE-WIDE features (#378, * D-FEATURES-NARROW-ONLY). `devflow init --[no-]` and `devflow * --enable/--disable` write the one switch — `features.` in * ~/.devflow/manifest.json — and every runtime gate reads that switch; a * repository's `features` can only narrow it, never widen it. * * The reported bug: `devflow init --no-curation` printed "Curation: disabled" * while every OTHER repo — one holding a stale `learning: true` an earlier init * wrote, one with no config, a non-git cwd — kept capturing and spawning the * Curate agent, because the runtime gates read the per-repo config. Those * per-repo keys are now retired: a stale value decides nothing either way, and * init's next config write drops it. * * These drive the REAL compiled CLI and the INSTALLED hook scripts (not the * source tree), so the assertion is about what a user's machine actually runs. * * Every spawn gets its env from the shared `sandboxEnv(tmpHome)` — an * allowlist with HOME pinned to a temp dir, so no inherited DEVFLOW_DIR or * CLAUDE_* redirect reaches the child — and the helper asserts that HOME is not a * real home before a run can start. Requires a build (`npm run build`). */ import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { spawnSync, execFileSync } from 'child_process'; import { promises as fs, existsSync, readFileSync } from 'fs'; import * as os from 'os'; import * as path from 'path'; import { requireBuiltCli, sandboxEnv } from './helpers.js'; import { systemManagedSettingsAtRisk } from './helpers/managed-settings.js'; import { useWarmFakeExecutables } from './helpers/warm-fake-executable.js'; import { countMemoryHooks, hasMemoryHooks } from '../src/cli/commands/memory.js'; import { hasAmbientHook } from '../src/cli/commands/ambient.js'; import { hasHudStatusLine } from '../src/cli/commands/hud.js'; import { addProxyHooks, applyProxyEnv, hasProxyHooks } from '../src/cli/commands/proxy.js'; import { writeProxyState, buildProxyState, DEFAULT_PROXY_PORT } from '../src/core/proxy-state.js'; import { DEVFLOW_HISTORICAL_DENY } from '../src/targets/claude-code/post-install.js'; import type { Settings } from '../src/targets/claude-code/hooks.js'; const CLI = requireBuiltCli(); const writeFakeExecutable = useWarmFakeExecutables(); /** One CLI spawn under load runs well past vitest's 5 s default. */ const SUBPROCESS_TIMEOUT_MS = 60_000; /** Tests that chain several init runs get a budget of several spawns. */ const MULTI_RUN_TIMEOUT_MS = 4 * SUBPROCESS_TIMEOUT_MS; let tmpHome: string; let repoA: string; let repoB: string; let nonGit: string; const devflowDir = (): string => path.join(tmpHome, '.devflow'); const claudeDir = (): string => path.join(tmpHome, '.claude'); const installedHook = (name: string): string => path.join(devflowDir(), 'scripts', 'hooks', name); const fakeBin = (): string => path.join(tmpHome, 'bin'); const childEnv = (): NodeJS.ProcessEnv => sandboxEnv(tmpHome, { PATH: `${fakeBin()}${path.delimiter}${process.env.PATH ?? ''}` }); function runCli(cwd: string, ...args: string[]): { status: number | null; out: string } { const result = spawnSync('node', [CLI, ...args], { encoding: 'utf-8', timeout: SUBPROCESS_TIMEOUT_MS, cwd, env: childEnv(), }); if (result.error) throw result.error; return { status: result.status, out: `${result.stdout ?? ''}${result.stderr ?? ''}` }; } function runInit(cwd: string, ...args: string[]): string { const { status, out } = runCli(cwd, 'init', ...args); expect(status, `init ${args.join(' ')} failed:\n${out}`).toBe(0); return out; } /** Run an INSTALLED hook with JSON stdin; the manifest resolves via $HOME. */ function runInstalledHook(name: string, input: object): { stdout: string; exitCode: number } { const hook = installedHook(name); expect(existsSync(hook), `init did not install ${name}`).toBe(true); try { const stdout = execFileSync('bash', [hook], { input: JSON.stringify(input), env: childEnv(), stdio: ['pipe', 'pipe', 'pipe'], }).toString(); return { stdout, exitCode: 0 }; } catch (e: unknown) { const err = e as { stdout?: Buffer; status?: number }; return { stdout: err.stdout?.toString() ?? '', exitCode: err.status ?? 1 }; } } async function readManifestRaw(): Promise { return fs.readFile(path.join(devflowDir(), 'manifest.json'), 'utf-8'); } async function readManifestFeatures(): Promise> { return (JSON.parse(await readManifestRaw()) as { features: Record }).features; } async function readRepoConfig(repo: string): Promise> { return JSON.parse(await fs.readFile(path.join(repo, '.devflow', 'config.json'), 'utf-8')) as Record; } async function writeRepoConfig(repo: string, body: Record): Promise { await fs.mkdir(path.join(repo, '.devflow'), { recursive: true }); await fs.writeFile(path.join(repo, '.devflow', 'config.json'), JSON.stringify(body), 'utf-8'); } const curationQueue = (dir: string): string => path.join(dir, '.devflow', 'curation', '.pending-turns.jsonl'); const curationProcessing = (dir: string): string => path.join(dir, '.devflow', 'curation', '.pending-turns.processing'); async function seedCurationQueue(dir: string): Promise { await fs.mkdir(path.dirname(curationQueue(dir)), { recursive: true }); await fs.writeFile(curationQueue(dir), '{"role":"user","content":"we chose X over Y","ts":1}\n', 'utf-8'); } const memoryQueue = (dir: string): string => path.join(dir, '.devflow', 'memory', '.pending-turns.jsonl'); const linesOf = (file: string): number => existsSync(file) ? readFileSync(file, 'utf-8').split('\n').filter(Boolean).length : 0; const queueLines = (dir: string): number => linesOf(curationQueue(dir)); /** Drive one prompt + one turn through the installed capture hooks in `dir`. */ function captureTurn(dir: string): void { runInstalledHook('capture-prompt', { cwd: dir, prompt: 'another turn' }); runInstalledHook('capture-turn', { cwd: dir, session_id: 's', last_assistant_message: 'a reply' }); } /** The retired per-repo switches, as a pre-#378 init or toggle left them. */ const STALE_OFF = { memory: false, learning: false, knowledge: false, reviewPublication: 'auto' } as const; const STALE_ON = { memory: true, learning: true, knowledge: true, reviewPublication: 'auto' } as const; /** The additionalContext session-start-context injects for `cwd` ('' when none). */ function sessionContext(cwd: string): string { const { stdout, exitCode } = runInstalledHook('session-start-context', { cwd, source: 'startup' }); expect(exitCode).toBe(0); if (stdout.trim() === '') return ''; return (JSON.parse(stdout) as { hookSpecificOutput: { additionalContext: string } }).hookSpecificOutput.additionalContext; } async function readSettings(): Promise { return fs.readFile(path.join(claudeDir(), 'settings.json'), 'utf-8'); } beforeEach(async () => { tmpHome = await fs.mkdtemp(path.join(os.tmpdir(), 'df-switch-home-')); repoA = await fs.mkdtemp(path.join(os.tmpdir(), 'df-switch-repoA-')); repoB = await fs.mkdtemp(path.join(os.tmpdir(), 'df-switch-repoB-')); nonGit = await fs.mkdtemp(path.join(os.tmpdir(), 'df-switch-nongit-')); // Init needs a Claude Code config directory. The stub permits no model run. await fs.mkdir(claudeDir(), { recursive: true }); await fs.mkdir(fakeBin(), { recursive: true }); writeFakeExecutable(path.join(fakeBin(), 'claude'), '#!/bin/bash\nif [ "$1" = --version ]; then printf "2.1.0 (Claude Code)\\n"; else exit 1; fi\n'); execFileSync('git', ['init', '-q'], { cwd: repoA, env: childEnv() }); execFileSync('git', ['init', '-q'], { cwd: repoB, env: childEnv() }); }); afterEach(async () => { for (const dir of [tmpHome, repoA, repoB, nonGit]) { await fs.rm(dir, { recursive: true, force: true }); } }); // ───────────────────────────────────────────────────────────────────────────── // init is the machine-wide switch // ───────────────────────────────────────────────────────────────────────────── describe('init --no- switches the feature off in every project', () => { it('curation: silences a second repo and a non-git cwd, drains the cwd queue, and init --curation restores every repo', async () => { // Repo B carries the stale `true` an earlier init wrote — the reported case. await writeRepoConfig(repoB, STALE_ON); await seedCurationQueue(repoA); await fs.writeFile(curationProcessing(repoA), '{"role":"user","content":"claimed","ts":1}\n', 'utf-8'); await seedCurationQueue(repoB); await seedCurationQueue(nonGit); const out = runInit(repoA, '--recommended', '--no-curation', '--no-knowledge'); expect(out).toMatch(/Curation:\s+disabled/); expect(await readManifestFeatures()).toMatchObject({ curation: false, learning: false, knowledge: false }); // The cwd repo's pending curation queue (and a claimed batch) is drained. expect(existsSync(curationQueue(repoA))).toBe(false); expect(existsSync(curationProcessing(repoA))).toBe(false); // No Curation directive in the other repo or a non-git cwd… expect(sessionContext(repoB)).not.toContain('CURATION MAINTENANCE'); expect(sessionContext(nonGit)).not.toContain('CURATION MAINTENANCE'); // …and the capture hooks append nothing to their curation queues. for (const dir of [repoB, nonGit]) { const before = queueLines(dir); captureTurn(dir); expect(queueLines(dir), `curation captured in ${dir} after a machine-wide disable`).toBe(before); } // The status of a repo whose config still says true reports the truth. expect(runCli(repoB, 'curation', '--status').out).toContain('Curation: disabled'); expect(runCli(repoB, 'knowledge', '--status').out).toContain('Status: disabled'); // Turning it back on from ANOTHER directory restores every repo. runInit(nonGit, '--recommended', '--curation', '--knowledge'); expect(await readManifestFeatures()).toMatchObject({ curation: true, learning: true, knowledge: true }); await seedCurationQueue(repoA); expect(sessionContext(repoA)).toContain('--- CURATION MAINTENANCE ---'); expect(sessionContext(repoB)).toContain('--- CURATION MAINTENANCE ---'); expect(runCli(repoB, 'knowledge', '--status').out).toContain('Status: enabled'); }, MULTI_RUN_TIMEOUT_MS); it('memory: a second repo and a non-git cwd stop appending to the memory queue', async () => { await writeRepoConfig(repoB, STALE_ON); runInit(repoA, '--recommended', '--no-memory'); expect(await readManifestFeatures()).toMatchObject({ memory: false }); expect(hasMemoryHooks(await readSettings())).toBe(false); captureTurn(repoB); expect(linesOf(memoryQueue(repoB)), 'memory captured in a second repo after init --no-memory').toBe(0); // Non-vacuity: the hooks ran and curation (still on) captured the turn. expect(queueLines(repoB), 'the capture hooks did not run in the second repo').toBe(2); // A non-git cwd captures nothing at all, whatever the switches say (D-HOOKS-GIT-ONLY). captureTurn(nonGit); expect(linesOf(memoryQueue(nonGit))).toBe(0); expect(queueLines(nonGit)).toBe(0); expect(runCli(repoB, 'memory', '--status').out).toContain('Working memory: disabled'); // init --memory from another directory restores it everywhere. runInit(nonGit, '--recommended', '--memory'); expect(hasMemoryHooks(await readSettings())).toBe(true); captureTurn(repoB); expect(linesOf(memoryQueue(repoB))).toBe(2); }, MULTI_RUN_TIMEOUT_MS); it('from a linked worktree: drains the main checkout\'s curation queue and this checkout\'s memory queue (D-LEDGER-MAIN-WORKTREE)', async () => { // The hooks queue curation turns into the main checkout's ledger (DF_LEDGER_ROOT) // but keep working memory per checkout, so init drains each where it is written. execFileSync('git', ['-c', 'user.email=t@t', '-c', 'user.name=t', 'commit', '-q', '--allow-empty', '-m', 'init'], { cwd: repoA }); const wtParent = await fs.mkdtemp(path.join(os.tmpdir(), 'df-switch-wt-')); const wt = path.join(wtParent, 'wt'); try { execFileSync('git', ['worktree', 'add', '-q', wt, '-b', 'feat'], { cwd: repoA }); await seedCurationQueue(repoA); await fs.mkdir(path.dirname(memoryQueue(repoA)), { recursive: true }); await fs.writeFile(memoryQueue(repoA), '{"role":"user"}\n', 'utf-8'); await fs.mkdir(path.dirname(memoryQueue(wt)), { recursive: true }); await fs.writeFile(memoryQueue(wt), '{"role":"user"}\n', 'utf-8'); runInit(wt, '--recommended', '--no-curation', '--no-memory'); expect(existsSync(curationQueue(repoA)), 'the main checkout\'s curation queue survived init --no-curation').toBe(false); expect(existsSync(memoryQueue(wt)), 'this checkout\'s memory queue survived init --no-memory').toBe(false); // Memory is per checkout: the main checkout's own queue is not this run's to drain. expect(existsSync(memoryQueue(repoA))).toBe(true); } finally { await fs.rm(wtParent, { recursive: true, force: true }); } }, MULTI_RUN_TIMEOUT_MS); it('a re-init keeps the machine-wide choice even from a repo whose config says true', async () => { runInit(repoA, '--recommended', '--no-curation', '--no-knowledge', '--no-memory'); await writeRepoConfig(repoB, STALE_ON); // A plain re-init (an upgrade, say) from repo B must not flip the switch back. runInit(repoB, '--recommended'); expect(await readManifestFeatures()).toMatchObject({ learning: false, knowledge: false, memory: false }); expect(hasMemoryHooks(await readSettings())).toBe(false); }, MULTI_RUN_TIMEOUT_MS); it('a stale repo config saying false switches nothing off, and init drops it', async () => { runInit(repoA, '--recommended', '--memory'); await writeRepoConfig(repoB, { ...STALE_OFF, tracker: 'jira' }); // Every feature is on for repo B: the stale per-repo false decides nothing. captureTurn(repoB); expect(linesOf(memoryQueue(repoB))).toBe(2); expect(queueLines(repoB)).toBe(2); expect(sessionContext(repoB)).toContain('--- CURATION MAINTENANCE ---'); expect(runCli(repoB, 'curation', '--status').out).toContain('Curation: enabled'); expect(runCli(repoB, 'knowledge', '--status').out).toContain('Status: enabled'); // A re-init from repo B does not turn anything off machine-wide, and its // managed write drops the retired keys while keeping the repo's own facts. runInit(repoB, '--recommended'); expect(await readManifestFeatures()).toMatchObject({ memory: true, learning: true, knowledge: true }); expect(await readRepoConfig(repoB)).toEqual({ reviewPublication: 'auto', tracker: 'jira' }); }, MULTI_RUN_TIMEOUT_MS); }); // ───────────────────────────────────────────────────────────────────────────── // The feature commands are the same machine-wide switch // ───────────────────────────────────────────────────────────────────────────── describe('devflow memory|curation|knowledge --enable/--disable are machine-wide', () => { it('curation --disable from one repo silences every repo and drains that repo; --enable from a non-git cwd restores', async () => { runInit(repoA, '--recommended'); await seedCurationQueue(repoA); await seedCurationQueue(repoB); expect(runCli(repoB, 'curation', '--disable').status).toBe(0); expect((await readManifestFeatures()).learning).toBe(false); expect(existsSync(curationQueue(repoB)), 'the current repo queue is drained').toBe(false); expect(sessionContext(repoA)).not.toContain('CURATION MAINTENANCE'); expect(runCli(repoA, 'curation', '--status').out).toContain('Curation: disabled'); // Never requires a git root: the switch is not a per-project setting. const enabled = runCli(nonGit, 'curation', '--enable'); expect(enabled.status, enabled.out).toBe(0); expect((await readManifestFeatures()).learning).toBe(true); expect(sessionContext(repoA)).toContain('--- CURATION MAINTENANCE ---'); }, MULTI_RUN_TIMEOUT_MS); it('memory --disable converges settings.json exactly as init --no-memory does, and drains the repo queue', async () => { runInit(repoA, '--recommended', '--memory'); await fs.mkdir(path.dirname(memoryQueue(repoA)), { recursive: true }); await fs.writeFile(memoryQueue(repoA), '{"role":"user","content":"x","ts":1}\n'); expect(runCli(repoA, 'memory', '--disable').status).toBe(0); const viaCommand = await readSettings(); expect(hasMemoryHooks(viaCommand)).toBe(false); expect((await readManifestFeatures()).memory).toBe(false); expect(existsSync(memoryQueue(repoA))).toBe(false); captureTurn(repoB); expect(linesOf(memoryQueue(repoB))).toBe(0); // The same settings init itself produces for the same choice. Parsed, not // byte-compared: a re-init re-serialises its own top-level keys (flags, env) // in its own order; every hook array — order included — must match. runInit(repoA, '--recommended', '--no-memory'); expect(JSON.parse(await readSettings())).toEqual(JSON.parse(viaCommand)); }, MULTI_RUN_TIMEOUT_MS); it('a fresh recommended install records memory off and registers no memory hooks', async () => { const out = runInit(repoA, '--recommended'); expect(out).toMatch(/Working memory:\s+disabled \(turn on: devflow memory --enable\)/); expect(await readManifestFeatures()).toMatchObject({ memory: false }); expect(countMemoryHooks(await readSettings())).toBe(0); }, MULTI_RUN_TIMEOUT_MS); it('memory --enable after a fresh install registers the worker after capture-turn and starts capture', async () => { runInit(repoA, '--recommended'); const enabled = runCli(nonGit, 'memory', '--enable'); expect(enabled.status, enabled.out).toBe(0); const settings = await readSettings(); expect(hasMemoryHooks(settings)).toBe(true); expect((await readManifestFeatures()).memory).toBe(true); const stop = (JSON.parse(settings) as Settings).hooks?.Stop ?? []; const commands = stop.flatMap((m) => m.hooks.map((h) => h.command)); const captureAt = commands.findIndex((c) => c.includes('capture-turn')); const workerAt = commands.findIndex((c) => c.includes('memory-worker')); expect(captureAt).toBeGreaterThanOrEqual(0); expect(workerAt, 'memory-worker must follow capture-turn in the Stop array').toBeGreaterThan(captureAt); expect(runCli(repoB, 'memory', '--status').out).toContain('Working memory: enabled'); captureTurn(repoB); expect(linesOf(memoryQueue(repoB))).toBe(2); }, MULTI_RUN_TIMEOUT_MS); it('curation --disable, memory --disable and init --no-curation --no-memory delete nothing through a linked queue folder, and say so (D-CLI-NO-SYMLINK)', async () => { runInit(repoA, '--recommended'); const outside = await fs.mkdtemp(path.join(os.tmpdir(), 'df-switch-outside-')); try { // git reports the toplevel with every symlink resolved, so the CLI names the // physical path (macOS /var → /private/var). const repoReal = await fs.realpath(repoA); const kept = path.join(outside, '.pending-turns.jsonl'); await fs.writeFile(kept, 'a file outside the project\n', 'utf-8'); for (const folder of ['memory', 'curation']) { await fs.rm(path.join(repoA, '.devflow', folder), { recursive: true, force: true }); await fs.symlink(outside, path.join(repoA, '.devflow', folder)); } for (const [feature, folder] of [['curation', 'curation'], ['memory', 'memory']]) { const run = runCli(repoA, feature, '--disable'); expect(run.status, run.out).toBe(0); expect(run.out).toContain(`${path.join(repoReal, '.devflow', folder)} is a symbolic link`); } const reinit = runInit(repoA, '--recommended', '--no-curation', '--no-memory'); expect(reinit).toContain(`${path.join(repoReal, '.devflow', 'memory')} is a symbolic link`); expect(reinit).toContain(`${path.join(repoReal, '.devflow', 'curation')} is a symbolic link`); expect(await fs.readFile(kept, 'utf-8'), 'nothing is deleted through either link').toBe('a file outside the project\n'); expect(await readManifestFeatures()).toMatchObject({ learning: false, memory: false }); } finally { await fs.rm(outside, { recursive: true, force: true }); } }, MULTI_RUN_TIMEOUT_MS); it('knowledge --disable / --enable flip the machine-wide switch from any repo', async () => { runInit(repoA, '--recommended'); expect(runCli(repoB, 'knowledge', '--disable').status).toBe(0); expect((await readManifestFeatures()).knowledge).toBe(false); expect(runCli(repoA, 'knowledge', '--status').out).toContain('Status: disabled'); expect(runCli(repoA, 'knowledge', '--enable').status).toBe(0); expect((await readManifestFeatures()).knowledge).toBe(true); expect(runCli(repoB, 'knowledge', '--status').out).toContain('Status: enabled'); }, MULTI_RUN_TIMEOUT_MS); it('the commands never write a repo config', async () => { runInit(repoA, '--recommended'); const body = JSON.stringify({ ...STALE_ON, tracker: 'linear' }); await fs.mkdir(path.join(repoB, '.devflow'), { recursive: true }); await fs.writeFile(path.join(repoB, '.devflow', 'config.json'), body, 'utf-8'); for (const args of [['curation', '--disable'], ['memory', '--disable'], ['knowledge', '--disable'], ['curation', '--enable']]) { expect(runCli(repoB, ...args).status).toBe(0); } expect(await fs.readFile(path.join(repoB, '.devflow', 'config.json'), 'utf-8')).toBe(body); }, MULTI_RUN_TIMEOUT_MS); }); // ───────────────────────────────────────────────────────────────────────────── // Older manifests: absent keys and the legacy `decisions` key // ───────────────────────────────────────────────────────────────────────────── /** Rewrite the installed manifest's features: drop `remove`, then merge `set`. */ async function editManifestFeatures(remove: string[], set: Record = {}): Promise { const manifest = JSON.parse(await readManifestRaw()) as { features: Record }; const features = Object.fromEntries(Object.entries(manifest.features).filter(([k]) => !remove.includes(k))); await fs.writeFile( path.join(devflowDir(), 'manifest.json'), JSON.stringify({ ...manifest, features: { ...features, ...set } }, null, 2), 'utf-8', ); } async function editManifestVersion(version: string): Promise { const manifest = JSON.parse(await readManifestRaw()) as Record; await fs.writeFile(path.join(devflowDir(), 'manifest.json'), JSON.stringify({ ...manifest, version }, null, 2), 'utf-8'); } describe('older manifests agree with the runtime gates', () => { it('re-init records an absent memory switch as on and retains other switches', async () => { runInit(repoA, '--recommended', '--no-knowledge'); await editManifestFeatures(['memory']); captureTurn(repoB); expect(linesOf(memoryQueue(repoB))).toBe(2); runInit(repoA, '--recommended'); expect(await readManifestFeatures()).toMatchObject({ memory: true, knowledge: false }); expect(hasMemoryHooks(await readSettings())).toBe(true); }, MULTI_RUN_TIMEOUT_MS); it('a manifest without the curation key, the legacy learning key and the knowledge key: a plain re-init keeps both enabled (D-FEATURES-ABSENT-ON)', async () => { runInit(repoA, '--recommended'); await editManifestFeatures(['curation', 'learning', 'knowledge']); // The gates already read the absent keys as on… expect(runCli(repoA, 'curation', '--status').out).toContain('Curation: enabled'); expect(runCli(repoA, 'knowledge', '--status').out).toContain('Status: enabled'); // …so a plain re-init (an upgrade) must record them on, not seed them off. runInit(repoA, '--recommended'); expect(await readManifestFeatures()).toMatchObject({ curation: true, learning: true, knowledge: true }); await seedCurationQueue(repoA); expect(sessionContext(repoA)).toContain('--- CURATION MAINTENANCE ---'); }, MULTI_RUN_TIMEOUT_MS); it('a manifest holding only the legacy decisions:false: curation is off in the hooks and the CLI status (D-LEARNING-LEGACY-DECISIONS)', async () => { runInit(repoA, '--recommended', '--memory'); await editManifestFeatures(['curation', 'learning'], { decisions: false }); await seedCurationQueue(repoB); expect(sessionContext(repoB)).not.toContain('CURATION MAINTENANCE'); const before = queueLines(repoB); captureTurn(repoB); expect(queueLines(repoB), 'curation captured under a legacy decisions:false').toBe(before); // Non-vacuity: the capture hooks ran — memory (still on) captured the turn. expect(linesOf(memoryQueue(repoB))).toBe(2); expect(runCli(repoA, 'curation', '--status').out).toContain('Curation: disabled'); // An explicit value for the current key and its pre-rename copy wins over the legacy decisions key. await editManifestFeatures([], { curation: true, learning: true }); expect(sessionContext(repoB)).toContain('--- CURATION MAINTENANCE ---'); expect(runCli(repoA, 'curation', '--status').out).toContain('Curation: enabled'); }, MULTI_RUN_TIMEOUT_MS); it('a manifest holding only the legacy kb:false: knowledge --status reports disabled (D-KNOWLEDGE-LEGACY-KB)', async () => { runInit(repoA, '--recommended'); await editManifestFeatures(['knowledge'], { kb: false }); expect(runCli(repoB, 'knowledge', '--status').out).toContain('Status: disabled'); // An explicit knowledge value wins over the legacy key. await editManifestFeatures([], { knowledge: true }); expect(runCli(repoB, 'knowledge', '--status').out).toContain('Status: enabled'); }, MULTI_RUN_TIMEOUT_MS); }); // ───────────────────────────────────────────────────────────────────────────── // TP-7 — --hud-only over a full install // ───────────────────────────────────────────────────────────────────────────── describe('init --hud-only over a full install (D-HUD-ONLY-PRESERVE)', () => { it('keeps every prior manifest value and only turns the HUD on', async () => { runInit(repoA, '--recommended', '--no-hud'); const prior = JSON.parse(await readManifestRaw()) as Record & { features: Record }; expect(prior.features.hud).toBe(false); expect(prior.features.learning).toBe(true); runInit(repoA, '--hud-only'); const after = JSON.parse(await readManifestRaw()) as Record & { features: Record }; expect(after.features).toEqual({ ...prior.features, hud: true }); expect(after.plugins).toEqual(prior.plugins); expect(after.version).toBe(prior.version); expect(after.installedAt).toBe(prior.installedAt); // With curation a machine-wide switch, a HUD install must not disable it. await seedCurationQueue(repoB); expect(sessionContext(repoB)).toContain('--- CURATION MAINTENANCE ---'); }, MULTI_RUN_TIMEOUT_MS); it('prints one skew line when the recorded version differs from the running one, in either direction', async () => { const running = (JSON.parse(readFileSync(path.join(import.meta.dirname, '..', 'package.json'), 'utf-8')) as { version: string }).version; runInit(repoA, '--recommended'); for (const recorded of ['0.0.1', '999.0.0']) { await editManifestVersion(recorded); const out = runInit(repoA, '--hud-only'); const lines = out.split('\n').filter((line) => line.includes('--hud-only refreshed only the scripts')); expect(lines, `expected one skew line for recorded ${recorded}`).toHaveLength(1); expect(lines[0]).toContain(`devflow ${recorded}`); expect(lines[0]).toContain(`CLI is ${running}`); expect(lines[0]).toContain('run `devflow init` to update the prompts.'); // --hud-only reinstalls no plugin, so the recorded version stays as it was. expect((JSON.parse(await readManifestRaw()) as { version: string }).version).toBe(recorded); } }, MULTI_RUN_TIMEOUT_MS); it('prints no skew line when the recorded version matches the running one', async () => { runInit(repoA, '--recommended'); expect(runInit(repoA, '--hud-only')).not.toContain('--hud-only refreshed only the scripts'); }, MULTI_RUN_TIMEOUT_MS); it('prints no skew line over a fresh machine', () => { expect(runInit(repoA, '--hud-only')).not.toContain('--hud-only refreshed only the scripts'); }, MULTI_RUN_TIMEOUT_MS); it('keeps a stored gate level, null included', async () => { runInit(repoA, '--recommended'); for (const value of ['lean', 'Lean', null]) { await editManifestFeatures([], { gateLevel: value }); runInit(repoA, '--hud-only'); const features = await readManifestFeatures(); expect(Object.hasOwn(features, 'gateLevel'), `expected ${JSON.stringify(value)} to be kept`).toBe(true); expect(features.gateLevel).toBe(value); } }, MULTI_RUN_TIMEOUT_MS); it('a fresh --hud-only install still records only the HUD', async () => { runInit(repoA, '--hud-only'); const features = await readManifestFeatures(); expect(features).toMatchObject({ hud: true, ambient: false, memory: false, rules: false, proxy: false }); expect(JSON.parse(await readManifestRaw()).plugins).toEqual([]); }, MULTI_RUN_TIMEOUT_MS); }); describe('init preserves the HUD forecast choice', () => { it.each([ ['--recommended', true], ['--recommended', false], ['--hud-only', true], ['--hud-only', false], ] as const)('%s keeps forecast: %s', async (mode, forecast) => { runInit(repoA, '--recommended', '--hud'); const configPath = path.join(devflowDir(), 'hud.json'); await fs.writeFile(configPath, JSON.stringify({ enabled: false, detail: true, forecast })); runInit(repoA, mode); const config: unknown = JSON.parse(await fs.readFile(configPath, 'utf-8')); expect(config).toEqual({ enabled: true, detail: true, forecast }); }, MULTI_RUN_TIMEOUT_MS); }); // ───────────────────────────────────────────────────────────────────────────── // TP-8/TP-9 — on→off re-init transitions converge every artifact // ───────────────────────────────────────────────────────────────────────────── describe('init on→off re-init transitions', () => { it('memory, ambient, HUD, rules, curation and knowledge all turn off on re-init', async () => { runInit(repoA, '--recommended', '--ambient', '--memory', '--hud', '--rules', '--curation', '--knowledge'); const on = await readSettings(); expect(hasMemoryHooks(on)).toBe(true); expect(hasAmbientHook(on)).toBe(true); expect(hasHudStatusLine(on)).toBe(true); expect(existsSync(path.join(claudeDir(), 'rules', 'devflow'))).toBe(true); expect(await readManifestFeatures()).toMatchObject({ learning: true, knowledge: true }); // Pending memory and curation queues that the off re-init must drain. await fs.mkdir(path.dirname(memoryQueue(repoA)), { recursive: true }); await fs.writeFile(memoryQueue(repoA), '{"role":"user","content":"x","ts":1}\n'); await seedCurationQueue(repoA); // Non-vacuity: curation is really on for the seeded queue before the re-init. expect(sessionContext(repoA)).toContain('--- CURATION MAINTENANCE ---'); runInit(repoA, '--recommended', '--no-ambient', '--no-memory', '--no-hud', '--no-rules', '--no-curation', '--no-knowledge'); const off = await readSettings(); expect(hasMemoryHooks(off)).toBe(false); expect(hasAmbientHook(off)).toBe(false); expect(hasHudStatusLine(off)).toBe(false); expect(existsSync(path.join(claudeDir(), 'rules', 'devflow'))).toBe(false); expect(await readManifestFeatures()).toMatchObject({ ambient: false, memory: false, hud: false, rules: false, learning: false, knowledge: false, }); // init never writes a feature switch into the per-repo config (D-FEATURES-NARROW-ONLY). expect(await readRepoConfig(repoA)).toEqual({ reviewPublication: 'auto' }); expect(existsSync(memoryQueue(repoA))).toBe(false); expect(existsSync(curationQueue(repoA)), 'the curation-off re-init drains the repo curation queue').toBe(false); expect(sessionContext(repoA)).not.toContain('CURATION MAINTENANCE'); expect(runCli(repoA, 'knowledge', '--status').out).toContain('Status: disabled'); expect(JSON.parse(await fs.readFile(path.join(devflowDir(), 'hud.json'), 'utf-8')).enabled).toBe(false); }, MULTI_RUN_TIMEOUT_MS); // No env seam exists for the managed-settings path (deliberately — sudo writes // run against it), so a `none` re-init here reaches the REAL system file when it // holds Devflow entries: the direct attempt rewrites that file or, when nothing // else is left in it, unlinks it. That attempt fails EACCES only while neither // the file nor its directory is writable by this process, and with no TTY no // sudo is tried — so the test runs only then, and never as root // (D-TESTS-NO-SYSTEM-MANAGED, tests/helpers/managed-settings.ts). The managed // arm is pinned structurally in init-machine-switch.test.ts and by the // injected-path unit tests. it.skipIf(systemManagedSettingsAtRisk())('security: a `--security none` re-init over a user-mode install strips every Devflow deny entry', async () => { runInit(repoA, '--recommended', '--security', 'user'); const devflowDeny = (json: string): string[] => ((JSON.parse(json) as { permissions?: { deny?: string[] } }).permissions?.deny ?? []) .filter((e) => DEVFLOW_HISTORICAL_DENY.has(e)); // Non-vacuity: the user-mode install really wrote the deny list. expect(devflowDeny(await readSettings()).length).toBeGreaterThan(0); expect((await readManifestFeatures()).security).toBe('user'); runInit(repoA, '--recommended', '--security', 'none'); expect(devflowDeny(await readSettings())).toEqual([]); expect((await readManifestFeatures()).security).toBe('none'); }, MULTI_RUN_TIMEOUT_MS); it('a plain `init --no-proxy` over a proxy-on install removes the hooks, the env and the manifest flag', async () => { runInit(repoA, '--recommended'); // Seed the proxy-on state init would have written (the preflight cannot pass // in a sandbox): hooks + ANTHROPIC_BASE_URL + proxy.json + manifest flag. const settings = JSON.parse(await readSettings()) as Settings; addProxyHooks(settings, devflowDir()); await fs.writeFile( path.join(claudeDir(), 'settings.json'), applyProxyEnv(JSON.stringify(settings), DEFAULT_PROXY_PORT), 'utf-8', ); const written = await writeProxyState(devflowDir(), buildProxyState({ enabled: true, port: DEFAULT_PROXY_PORT, binPath: null, configPath: null, devflowVersion: null, })); expect(written.ok).toBe(true); const manifest = JSON.parse(await readManifestRaw()) as { features: Record }; manifest.features.proxy = true; await fs.writeFile(path.join(devflowDir(), 'manifest.json'), JSON.stringify(manifest, null, 2), 'utf-8'); // Non-vacuous: the on state is really there. expect(hasProxyHooks(await readSettings())).toBe(true); expect(await readSettings()).toContain('ANTHROPIC_BASE_URL'); runInit(repoA, '--no-proxy'); const off = await readSettings(); expect(hasProxyHooks(off)).toBe(false); expect(off).not.toContain('ANTHROPIC_BASE_URL'); expect((await readManifestFeatures()).proxy).toBe(false); }, MULTI_RUN_TIMEOUT_MS); });