Git for the intent behind agent-written code. Records what your coding agent decided and why, and links every commit to it.
JavaScript ai-codingclaudeclaude-codeclaude-code-pluginclicoding-agentsdeveloper-toolsgitllmtypescript
import {
cogitIgnored,
ensureCogitGitignore,
ensureGitignore,
findGitRoot,
type GitPolicy,
} from "../git.js";
import { installGitHooks } from "../git-hooks.js";
import { CliError, type Io } from "../io.js";
import { addRecordRefspec } from "../record-sync.js";
import { Config, type RegistryEntry } from "../schema/config.js";
import { isDirectory } from "../store/fs.js";
import { resolveProjectRoot } from "../store/root.js";
import {
loadConfig,
loadProject,
loadRegistry,
updateConfig,
updateRegistry,
} from "../store/stores.js";
/** Settings a user may read and set; `migrated` is cogit's own bookkeeping. */
export const CONFIG_KEYS = Object.keys(Config.shape).filter(
(k) => k !== "schema" && k !== "migrated",
) as Exclude<keyof Config, "schema" | "migrated">[];
/** Keys `cogit config` lists; `catchUpModel` is only an accepted old alias (12.1). */
export const LISTED_CONFIG_KEYS = CONFIG_KEYS.filter((k) => k !== "catchUpModel");
/** `catchUpModel` was renamed to `sideModel` (Story 12.1); both names keep working. */
function canonicalKey(key: string): string {
return key === "catchUpModel" ? "sideModel" : key;
}
function assertKey(key: string): asserts key is (typeof CONFIG_KEYS)[number] {
if (!(CONFIG_KEYS as string[]).includes(key)) {
throw new CliError(`unknown key "${key}"; one of: ${CONFIG_KEYS.join(", ")}`);
}
}
/** Command-line text to a value: JSON when it parses, comma lists for arrays, else a string. */
function parseValue(key: string, raw: string): unknown {
if (key === "agents" && !raw.trim().startsWith("[")) {
return raw
.split(",")
.map((s) => s.trim())
.filter(Boolean);
}
try {
return JSON.parse(raw);
} catch {
return raw;
}
}
export async function configGet(key: string | undefined, io: Io): Promise<void> {
const config = await loadConfig(io.env);
if (key === undefined) {
for (const k of LISTED_CONFIG_KEYS) io.out(`${k} = ${JSON.stringify(config[k])}`);
return;
}
assertKey(key);
const name = canonicalKey(key);
io.out(JSON.stringify(config[name as keyof Config]));
}
export async function configSet(key: string, raw: string, io: Io): Promise<void> {
assertKey(key);
const value = parseValue(key, raw);
const next = Config.safeParse({ ...(await loadConfig(io.env)), [key]: value });
if (!next.success) {
throw new CliError(`invalid value for ${key}: ${next.error.issues[0]?.message ?? "rejected"}`);
}
const name = canonicalKey(key);
await updateConfig((c) => ({ ...c, [name]: next.data[key] }), io.env);
io.out(`${name} = ${JSON.stringify(next.data[key])}`);
}
/** `cogit config git` without a policy: what this project does now, read from its `.gitignore`. */
export async function configGitShow(dir: string, io: Io): Promise<void> {
const { root } = await resolveProjectRoot(dir);
const project = await loadProject(root);
if (!project.found) throw new CliError(`${root} is not initialized; run \`cogit init\` first`);
if (!project.ok) throw new CliError(project.error);
const policy: GitPolicy = (await cogitIgnored(root)) ? "ignore" : "share";
io.out(`Git policy for ${project.data.name}: ${policy}`);
}
/**
* `cogit config git share|ignore`: switches an existing project. Both ignore files
* are edited and no record file is ever deleted. Switching to `ignore` stops
* sending; what the remote already holds stays there until `cogit prune`.
*/
export async function configGit(dir: string, policy: GitPolicy, io: Io): Promise<void> {
const { root } = await resolveProjectRoot(dir);
const project = await loadProject(root);
if (!project.found) throw new CliError(`${root} is not initialized; run \`cogit init\` first`);
if (!project.ok) throw new CliError(project.error);
if ((await findGitRoot(root)) === undefined)
throw new CliError(`${root} is not in a git repository`);
const added = await ensureGitignore(root, policy);
await ensureCogitGitignore(root, policy);
await installGitHooks(root);
if (policy === "share") await addRecordRefspec(root, io.env);
io.out(`Git policy for ${project.data.name}: ${policy}`);
if (added.length > 0) io.out(`Added to .gitignore: ${added.join(", ")}`);
}
/**
* Stops the SessionStart setup hint in a project ("don't ask again"). The list is
* keyed by the main checkout's root, so a linked worktree is silenced with it.
*/
export async function configHintOff(dir: string, io: Io): Promise<void> {
const { root } = await resolveProjectRoot(dir);
let added = false;
await updateConfig((c) => {
if (c.setupHintOff.includes(root)) return c;
added = true;
return { ...c, setupHintOff: [...c.setupHintOff, root] };
}, io.env);
io.out(added ? `Setup hint off in ${root}` : `Setup hint was already off in ${root}`);
}
/** Turns the setup hint back on for one project, or for every project with `--all`. */
export async function configHintOn(dir: string, opts: { all?: boolean }, io: Io): Promise<void> {
if (opts.all === true) {
await updateConfig((c) => ({ ...c, setupHintOff: [] }), io.env);
io.out("Setup hint on in every project");
return;
}
const { root } = await resolveProjectRoot(dir);
let removed = false;
await updateConfig((c) => {
if (!c.setupHintOff.includes(root)) return c;
removed = true;
return { ...c, setupHintOff: c.setupHintOff.filter((path) => path !== root) };
}, io.env);
io.out(removed ? `Setup hint on in ${root}` : `Setup hint was not off in ${root}`);
}
export interface PruneOptions {
/** Lists the entries but leaves `projects.json` untouched. */
dryRun?: boolean;
}
/**
* Drops registry entries whose directory is gone. A corrupt registry is an error
* rather than an empty list, so a typo never wipes every project.
*/
export async function configPrune(opts: PruneOptions, io: Io): Promise<void> {
const loaded = await loadRegistry(io.env);
if (loaded.found && !loaded.ok) throw new CliError(`registry: ${loaded.error}`);
const entries = loaded.found && loaded.ok ? loaded.data.projects : [];
const missing: RegistryEntry[] = [];
for (const entry of entries) {
if (!(await isDirectory(entry.path))) missing.push(entry);
}
if (missing.length === 0) {
io.out("No missing projects");
return;
}
for (const entry of missing) {
io.out(`${opts.dryRun === true ? "Would remove" : "Removed"} ${entry.name} (${entry.path})`);
}
if (opts.dryRun === true) return;
await updateRegistry(
(r) => ({ ...r, projects: r.projects.filter((p) => !missing.some((m) => m.path === p.path)) }),
io.env,
);
}