Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion cli/__tests__/install-search-code.test.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import fs, { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
import fs, {
existsSync,
mkdirSync,
readFileSync,
realpathSync,
rmSync,
writeFileSync,
} from 'node:fs';
import { syncBuiltinESMExports } from 'node:module';
import { join } from 'node:path';
import { DatabaseSync } from 'node:sqlite';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { codebaseRegisterCommand } from '../../gate-engine/judge/mcp/registration-remedy.mts';
import { DEFAULT_INDEX_COMMAND, installSearchCode } from '../lib/install/install-search-code.mts';
import { tmpRepos } from './_helpers.mts';

Expand Down Expand Up @@ -92,6 +101,43 @@ describe('search-code opt-in component', () => {
log.mockRestore();
});

it('prints the command that registers the codebase MCP server for this repo', () => {
const root = tmpRepo();
const log = vi.spyOn(console, 'log').mockImplementation(() => {});
installSearchCode(root, false);
expect(log.mock.calls.flat()).toContain(` ${codebaseRegisterCommand(realpathSync(root))}`);
log.mockRestore();
});

it.each([
['not opted in: it says to opt the primary in, and prints no command', false],
['opted in: it registers the primary checkout, which outlives the worktree', true],
])('run from a linked worktree whose primary is %s', (_name, primaryOptedIn) => {
const root = tmpRepo();
const worktree = `${root}-wt`;
const git = (...args: string[]) => execFileSync('git', args, { cwd: root });
git('init', '-q');
git('-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-q', '--allow-empty', '-m', 'base');
git('worktree', 'add', '-q', '--detach', worktree);
if (primaryOptedIn) writeFileSync(join(root, 'search-code.config.json'), '{}\n');
const log = vi.spyOn(console, 'log').mockImplementation(() => {});
try {
installSearchCode(worktree, false);
const out = log.mock.calls.flat().map(String);
const primary = realpathSync(root);
expect(out.find((l) => l.includes('claude mcp add'))).toBe(
primaryOptedIn ? ` ${codebaseRegisterCommand(primary)}` : undefined,
);
if (!primaryOptedIn)
expect(out).toContain(
` ℹ gate reviewers search the primary checkout: run this there too, in ${primary}`,
);
} finally {
log.mockRestore();
rmSync(worktree, { recursive: true, force: true });
}
});

it('is off by default (no --search-code → no config)', () => {
const root = tmpRepo();
devkit(root, 'init', '--stack', 'generic', '--yes');
Expand Down
23 changes: 22 additions & 1 deletion cli/lib/install/install-search-code.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import { appendFileSync, existsSync, readFileSync, realpathSync, rmSync, statSyn
import { join } from 'node:path';
import {
indexToInspect,
primaryCheckout,
usableChunksAt,
} from '../../../gate-engine/co-occurrence/index-refresh.mts';
import {
codebaseRegisterCommand,
SEARCH_CODE_CONFIG,
} from '../../../gate-engine/judge/mcp/registration-remedy.mts';
import { withGitignoreLock } from './gitignore-cache.mts';
import { writeFileAtomic } from '../atomic-write.mts';
import { type CheckResult, check } from '../doctor/check-result.mts';
import { readJson, writeIfAbsent } from '../fs-helpers.mts';

export const SEARCH_CODE_CONFIG = 'search-code.config.json';
export { SEARCH_CODE_CONFIG };
const INDEX_LINE = '.search-code/';
const INDEX_PATH = '.search-code/index.db';
/** What an overlay install git-excludes instead of writing the tracked `.gitignore`. */
Expand Down Expand Up @@ -159,6 +164,22 @@ export function installSearchCode(
console.log(' ℹ needs the search-code engine + Ollama. Install once, then index this repo:');
console.log(' bun add -g git+ssh://git@github.com/norvalbv/search-code.git');
console.log(' search-code index');
printCodebaseRegistration(cwd);
}

/** Gates register and search the primary checkout, so a linked worktree's opt-in alone is not enough. */
function printCodebaseRegistration(cwd: string): void {
const primary = realpathSync(primaryCheckout(cwd) ?? cwd);
if (primary !== realpathSync(cwd) && !existsSync(join(primary, SEARCH_CODE_CONFIG))) {
console.log(
` ℹ gate reviewers search the primary checkout: run this there too, in ${primary}`,
);
return;
}
console.log(
' ℹ gate reviewers search the index only once the codebase MCP server is registered (skip if it is):',
);
console.log(` ${codebaseRegisterCommand(primary)}`);
}

// clean reversal: remove the devkit-written opt-in config. The `.search-code/` gitignore line is
Expand Down
31 changes: 31 additions & 0 deletions dist/gate-engine/judge/mcp/registration-remedy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { existsSync, realpathSync } from 'node:fs';
import { homedir } from 'node:os';
import path from 'node:path';
import { shellWord } from '../../review/valve/shell-word.mjs';
/** search-code indexes a repo only when this file sits at its root (the engine's opt-in gate). */
export const SEARCH_CODE_CONFIG = 'search-code.config.json';
/** Registers the codebase server for `root` in ~/.claude.json, whatever CLAUDE_CONFIG_DIR says. The
* root is pinned: a gate worktree under $TMPDIR may hold no search-code config to walk up to. */
export function codebaseRegisterCommand(root) {
const at = shellWord(root);
return `cd ${at} && env -u CLAUDE_CONFIG_DIR claude mcp add -s local codebase -e SEMANTIC_SEARCH_ROOT=${at} -- search-code serve`;
}
function isClaudeRegistry(registryPath) {
const file = path.join(homedir(), '.claude.json');
try {
return realpathSync(file) === registryPath;
}
catch {
return file === registryPath;
}
}
/** How to restore codebase search for `root`. A repo search-code never indexed is told to opt in
* first, since a bare registration clears DEGRADED while reviewers still have nothing to search. */
export function codebaseRemedy(root, registryPath) {
const fix = isClaudeRegistry(registryPath)
? `run \`${codebaseRegisterCommand(root)}\`, and remove \`codebase\` from this project's \`disabledMcpServers\` in ${registryPath} if it is listed there`
: `add the \`codebase\` MCP server for this project in ${registryPath}, or remove it from that project's \`disabledMcpServers\``;
if (!existsSync(path.join(root, SEARCH_CODE_CONFIG)))
return `opt this repo into search-code first (\`devkit init --search-code\`, then \`search-code index\`), then ${fix}`;
return fix;
}
1 change: 1 addition & 0 deletions docs/decisions/judge-mcp-profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ created: 2026-08-15
- 2026-10-15 — sc-5134: codebase capability stays on the registry path; claude judges get no search-code Bash grant. A missing codebase server marks every PASS DEGRADED regardless of what the judge's shell retrieved (cascade/reviewer.mts sets mcpDegraded from the profile cause), so a shell grant could not clear it, and most observed fallback calls were piped or chained, which a prefix rule never approves. It would also add a PATH-resolved retrieval channel outside the trusted registry. Instead the brief tells a headless commit-guard to skip the shell fallback, the DEGRADED warn line names the remedy (add or re-enable the codebase server for the project in the registry, or set DEVKIT_JUDGE_MCP_CONFIG), and commit-guard gains only Bash(guard-dup-allowlist check:*), a read-only lookup; check-clone is skipped headless and stays ungranted.
- 2026-10-18 — sc-5156: codex gate judges no longer run in the owner's CODEX_HOME. codex injects $CODEX_HOME/AGENTS.override.md or AGENTS.md as user instructions on every turn (codex-rs codex-home/src/instructions/mod.rs, loaded unconditionally from core/src/agents_md_manager.rs); --ignore-user-config only blanks config.toml and no flag disables it (openai/codex#5983 NOT_PLANNED). Measured with the free codex debug prompt-input on codex-cli 0.162.1: a sentinel global AGENTS.md reached the prompt even with -c project_doc_max_bytes=0, and the owner's HOME-relative ~/.agents/skills catalog also reached it (33,919 bytes before, 11,191 after). Every codex judge (read-only and workspace-write) now gets CODEX_HOME=~/.devkit/codex-judge-home, prepared by gate-engine/judge/codex/home.mts, holding only an auth.json symlink to the owner's auth file, plus -c skills.include_instructions=false. Upstream names a separate CODEX_HOME as the isolation unit (openai/codex#15410). The symlink works because codex rewrites auth.json in place (login/src/auth/storage.rs), so refreshed tokens land in the owner's file. A regular auth.json in the judge home is left alone (a login landing between the lstat and the rename can still be replaced; the window is microseconds), and the home never links to itself. Rejected: copying auth.json (forks single-use refresh tokens, #15410); a per-spawn tmpdir home (writable from workspace-write judge shells, and it re-fetches about 1.7 MB of caches per spawn); overriding HOME (breaks git, gh, the login shell and the keychain). Codex bench baselines recorded before this change carried owner context. codex app-server quota probes (rate-limits.mts) still use the owner home on purpose.
- 2026-10-19 — sc-4645: the sc-4709 Revisit-when fired. Codex gate judges are now refused mechanically, not by instruction: judgeCliFor passes every codex judge a PreToolUse hook (-c hooks.PreToolUse) and trusts it per run through an inline -c hooks.state trusted_hash computed by devkit (codex reads hook trust from the session-flags layer), so --ignore-user-config and --ignore-rules stay and the owner's ~/.codex/hooks.json stays untrusted. --dangerously-bypass-hook-trust was rejected because it also runs those owner hooks. The hook (gate-engine/judge/codex/tool-gate-hook.mts) parses each Bash command with unbash and allows only a read-only list matching the Claude judge grant: shell reads, git read subcommands without -c/--output/--ext-diff, sed -n print ranges, and node on the granted checklist script; any parse error, expansion, assignment, redirect other than to /dev/null, compound block or other tool is denied (apply_patch included, which closes the .claude write residual; MCP tools stay allowed). Deny is exit 2 and the exit code starts at 2, because codex runs the call on any other hook outcome. Verified live on codex-cli 0.150.0, 0.159.3 and 0.162.1. Refusals are ledgered per codex session and reach judge_exec as permission_denials, so codex spawns are now measured (the 2026-10-13 review-gate-in-chain wording that codex keeps the bare reason no longer holds). A run whose executed commands outnumber the ledger's allows prints 'codex tool gate inactive' and emits gate_degraded without changing the verdict; that catches a future hook-schema change and managed-only hook configs. A checklist grant matches only its exact path, cd and git -C only into the confined repository root, and unquoted globs are denied (they expand to names the reviewed diff controls, such as a file named --output=x), so a lookalike script the reviewed diff adds is not reachable that way. Residuals: exec_command's model-chosen workdir and shell (such as pwsh) are not in the hook payload, so a relative grant could still resolve from another directory, and pwsh is mitigated only by denying unquoted braces. Rejected: execpolicy prefix rules (not hermetic, leaky) and app-server host approvals (replaces the spawn seam for one family).
- 2026-11-20 — sc-7321: the DEGRADED codebase remedy is now a runnable command, cd <primary> && env -u CLAUDE_CONFIG_DIR claude mcp add -s local codebase -e SEMANTIC_SEARCH_ROOT=<primary> -- search-code serve, also printed by init --search-code (gate-engine/judge/mcp/registration-remedy.mts). The registry gap behind about 300 gate_degraded rows a day was devkit, claude-usage-dashboard and sonar never being registered; the October rise is when sc-2837 started recording the cause, not an outage. The command cds to the primary checkout because local scope keys the row by the session's path, and projectCandidates reads the primary row from a linked worktree. It pins SEMANTIC_SEARCH_ROOT to the primary, the shape frink, qavis and frink-oss run with zero DEGRADED rows: the stdio server starts in the judge's cwd (probed under --strict-mcp-config), and a ship gate worktree under TMPDIR has no search-code config to walk up to when an overlay consumer git-excludes it. It runs claude through env -u CLAUDE_CONFIG_DIR, since a set CLAUDE_CONFIG_DIR (as in a Frink session) makes claude write a file this registry never reads; probed, claude mcp add keys local scope by the main worktree root and leaves disabledMcpServers untouched, so the remedy names that step and an explicit DEVKIT_JUDGE_MCP_CONFIG registry gets no claude command. A repo with no search-code.config.json is told to opt in first, because DEGRADED measures registration, not retrieval: a bare registration would clear it while reviewers have nothing to search. Rejected: one unpinned user-scope registration, which would clear DEGRADED for repos with no index; a devkit doctor row, a third copy of a remedy every ship already prints.
46 changes: 44 additions & 2 deletions gate-engine/judge/__tests__/mcp-profile.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import {
spawnDegradedCause,
withNamedAgentMcpTools,
} from '../mcp/profile.mts';
import { codebaseRegisterCommand, codebaseRemedy } from '../mcp/registration-remedy.mts';

const root = mkdtempSync(path.join(tmpdir(), 'judge-mcp-profile-'));
const repo = path.join(root, 'repo');
const registry = path.join(root, 'registry.json');
mkdirSync(repo);

const remedy = (registryPath: string) =>
` — to restore it, add or re-enable the \`codebase\` MCP server for this project in ${realpathSync(registryPath)}, or point DEVKIT_JUDGE_MCP_CONFIG at a registry that defines it`;
const remedy = (registryPath: string, at = repo) =>
` — to restore it, ${codebaseRemedy(realpathSync(at), realpathSync(registryPath))}, or point DEVKIT_JUDGE_MCP_CONFIG at a registry that defines it`;

interface RegistryFixtureOverrides {
projects?: {
Expand Down Expand Up @@ -326,6 +327,47 @@ describe('missing-server resolution and reporting (sc-2837)', () => {
]);
});

it('the remedy registers codebase at the primary checkout, not the linked worktree', () => {
spyStderr();
const home = mkdtempSync(path.join(root, 'home-'));
const registryPath = path.join(home, '.claude.json');
writeFileSync(registryPath, '{"mcpServers":{}}', { mode: 0o600 });
vi.stubEnv('HOME', home);
vi.stubEnv('DEVKIT_JUDGE_MCP_CONFIG', undefined);
const worktree = mkdtempSync(path.join(root, 'worktree-'));
writeFileSync(path.join(repo, 'search-code.config.json'), '{}\n');
try {
const resolution = judgeMcpMissingServers(namedAgentMcpProfile(), {
cwd: worktree,
projectRoots: [worktree, repo],
});
reportMissingMcpServers(resolution, ['correctness-reviewer']);
expect(stderr[0].endsWith(remedy(registryPath, repo))).toBe(true);
} finally {
rmSync(path.join(repo, 'search-code.config.json'));
vi.unstubAllEnvs();
}
expect(stderr[0]).toContain(`run \`${codebaseRegisterCommand(realpathSync(repo))}\``);
expect(stderr[0]).not.toContain(realpathSync(worktree));
});

it('a second checkout with the same registry state still prints its own command', () => {
spyStderr();
const registryPath = withServers('context7', 'autonomous_bugs');
const other = mkdtempSync(path.join(root, 'other-'));
for (const at of [repo, other])
reportMissingMcpServers(
judgeMcpMissingServers(namedAgentMcpProfile(), {
cwd: at,
registryPath,
projectRoots: [at],
}),
['correctness-reviewer'],
);
expect(stderr).toHaveLength(2);
expect(stderr[1].endsWith(remedy(registryPath, other))).toBe(true);
});

it('a registry edited mid-process to lose a DIFFERENT server warns again (new state, new key)', () => {
spyStderr();
const registryPath = withServers('codebase', 'context7');
Expand Down
77 changes: 77 additions & 0 deletions gate-engine/judge/__tests__/registration-remedy.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { spawnSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';
import {
codebaseRegisterCommand,
codebaseRemedy,
SEARCH_CODE_CONFIG,
} from '../mcp/registration-remedy.mts';

const base = mkdtempSync(path.join(tmpdir(), 'registration-remedy-'));
const home = path.join(base, 'home');
mkdirSync(home);
const claudeRegistry = path.join(realpathSync(home), '.claude.json');
writeFileSync(claudeRegistry, '{}');
afterAll(() => rmSync(base, { recursive: true, force: true }));
afterEach(() => vi.unstubAllEnvs());

function repoAt(name: string, optedIn: boolean): string {
const dir = path.join(base, name);
mkdirSync(dir);
if (optedIn) writeFileSync(path.join(dir, SEARCH_CODE_CONFIG), '{}\n');
return dir;
}

describe('codebaseRegisterCommand', () => {
it('pastes as-is: a root with spaces and a quote cds, pins, and drops CLAUDE_CONFIG_DIR', () => {
const dir = repoAt("Personal and learning's devkit", true);
const bin = path.join(base, 'bin');
mkdirSync(bin);
const stub = 'pwd -P; printf "%s\\n" "${CLAUDE_CONFIG_DIR-unset}" "$@"';
writeFileSync(path.join(bin, 'claude'), `#!/bin/sh\n${stub}\n`, { mode: 0o755 });
const out = spawnSync('/bin/sh', ['-c', codebaseRegisterCommand(dir)], {
encoding: 'utf8',
env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, CLAUDE_CONFIG_DIR: '/decoy' },
}).stdout;
expect(out.trimEnd().split('\n')).toEqual([
realpathSync(dir),
'unset',
...['mcp', 'add', '-s', 'local', 'codebase', '-e', `SEMANTIC_SEARCH_ROOT=${dir}`],
...['--', 'search-code', 'serve'],
]);
});

it('writes ~/.claude.json even under CLAUDE_CONFIG_DIR, with the name before -e', () => {
expect(codebaseRegisterCommand('/repo')).toBe(
'cd /repo && env -u CLAUDE_CONFIG_DIR claude mcp add -s local codebase -e SEMANTIC_SEARCH_ROOT=/repo -- search-code serve',
);
});
});

describe('codebaseRemedy', () => {
it('a repo search-code never indexed opts in before it registers', () => {
vi.stubEnv('HOME', home);
const dir = repoAt('plain', false);
expect(codebaseRemedy(dir, claudeRegistry)).toMatch(
/^opt this repo into search-code first \(`devkit init --search-code`, then `search-code index`\), then run `cd /,
);
});

it('an opted-in repo gets the command, plus the disabledMcpServers step it does not cover', () => {
vi.stubEnv('HOME', home);
const dir = repoAt('opted', true);
expect(codebaseRemedy(dir, claudeRegistry)).toBe(
`run \`${codebaseRegisterCommand(dir)}\`, and remove \`codebase\` from this project's \`disabledMcpServers\` in ${claudeRegistry} if it is listed there`,
);
});

it('an explicit registry gets no claude mcp command, which would write ~/.claude.json instead', () => {
vi.stubEnv('HOME', home);
const dir = repoAt('explicit', true);
expect(codebaseRemedy(dir, '/etc/judge-mcp.json')).toBe(
"add the `codebase` MCP server for this project in /etc/judge-mcp.json, or remove it from that project's `disabledMcpServers`",
);
});
});
Loading