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
fix(init): replace an outdated missing-plugin guard
Eren35588f89151d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 894dcbf..9135554 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).## [Unreleased]+### Fixed+- A project set up by an earlier `cogit init --project-settings` kept its old missing-plugin guard, which named the install command from before the move to erensrtky/cogit. Running `cogit init` again now replaces an outdated guard and marketplace entry, touching nothing else; the team setup also writes one current guard over an old one.## [0.18.1] - 2026-10-28### Changeddiff --git a/packages/cli/src/agents/claude/index.ts b/packages/cli/src/agents/claude/index.tsindex 7eae1e7..c51d6f8 100644--- a/packages/cli/src/agents/claude/index.ts+++ b/packages/cli/src/agents/claude/index.ts@@ -7,6 +7,7 @@ import {readProjectSetup,readRetention,readSkillAllowed,+ refreshProjectSetup,SKILL_RULE,setRetention,writeProjectSetup,@@ -59,5 +60,6 @@ export const claude: AgentAdapter = {allowSkill,readProjectSetup,writeProjectSetup,+ refreshProjectSetup,markLive,};diff --git a/packages/cli/src/agents/claude/settings.ts b/packages/cli/src/agents/claude/settings.tsindex 83804cf..0d82927 100644--- a/packages/cli/src/agents/claude/settings.ts+++ b/packages/cli/src/agents/claude/settings.ts@@ -163,12 +163,60 @@ function hasGuard(settings: Record<string, unknown>): boolean {return JSON.stringify(hooks ?? []).includes(GUARD_TAG);}-/** Whether the project's shared settings already carry the team setup; undefined if unreadable. */+/** Whether the guard there is this version's: an older one may name a moved install command. */+function hasCurrentGuard(settings: Record<string, unknown>): boolean {+ const hooks = isObject(settings.hooks) ? settings.hooks.UserPromptSubmit : undefined;+ return JSON.stringify(hooks ?? []).includes(JSON.stringify(GUARD_COMMAND));+}++/** The UserPromptSubmit entries without ours, and the current guard appended. */+function withCurrentGuard(submit: unknown[]): unknown[] {+ return [+ ...submit.filter((entry) => !JSON.stringify(entry).includes(GUARD_TAG)),+ { hooks: [{ type: "command", command: GUARD_COMMAND, timeout: 5 }] },+ ];+}++/** Whether the project's shared settings already carry the current team setup; undefined if unreadable. */export async function readProjectSetup(root: string): Promise<boolean | undefined> {const settings = await readSettings(projectSettingsPath(root));if (!settings) return undefined;const plugins = isObject(settings.enabledPlugins) ? settings.enabledPlugins : {};- return plugins[PLUGIN_ID] === true && hasGuard(settings);+ return plugins[PLUGIN_ID] === true && hasCurrentGuard(settings);+}++/**+ * Brings what an earlier `init` wrote up to date: an outdated guard is replaced+ * and our marketplace entry points at the current repository. Nothing is added+ * that the project did not have, and other keys and hooks stay as they are.+ * Returns the file when it changed.+ */+export async function refreshProjectSetup(root: string): Promise<string | undefined> {+ const file = projectSettingsPath(root);+ const settings = await readSettings(file);+ if (!settings || !isObject(settings.hooks)) return undefined;+ const submit = settings.hooks.UserPromptSubmit;+ const marketplaces = isObject(settings.extraKnownMarketplaces)+ ? settings.extraKnownMarketplaces+ : undefined;+ const ours = marketplaces?.[MARKETPLACE];+ const staleGuard = Array.isArray(submit) && hasGuard(settings) && !hasCurrentGuard(settings);+ const staleMarketplace =+ isObject(ours) &&+ isObject(ours.source) &&+ ours.source.source === "github" &&+ ours.source.repo !== MARKETPLACE_REPO;+ if (!staleGuard && !staleMarketplace) return undefined;+ const next: Record<string, unknown> = { ...settings };+ if (staleGuard)+ next.hooks = { ...settings.hooks, UserPromptSubmit: withCurrentGuard(submit as unknown[]) };+ if (staleMarketplace)+ next.extraKnownMarketplaces = {+ ...marketplaces,+ [MARKETPLACE]: { source: { source: "github", repo: MARKETPLACE_REPO } },+ };+ await atomicWriteJson(file, next);+ return file;}/**@@ -194,15 +242,9 @@ export async function writeProjectSetup(root: string): Promise<FileChange> {...(settings.extraKnownMarketplaces as object),[MARKETPLACE]: { source: { source: "github", repo: MARKETPLACE_REPO } },},- hooks: hasGuard(settings)+ hooks: hasCurrentGuard(settings)? hooks- : {- ...hooks,- UserPromptSubmit: [- ...submit,- { hooks: [{ type: "command", command: GUARD_COMMAND, timeout: 5 }] },- ],- },+ : { ...hooks, UserPromptSubmit: withCurrentGuard(submit) },};await mkdir(join(root, ".claude"), { recursive: true });await atomicWriteJson(file, next);diff --git a/packages/cli/src/agents/types.ts b/packages/cli/src/agents/types.tsindex 759abc0..22a441b 100644--- a/packages/cli/src/agents/types.ts+++ b/packages/cli/src/agents/types.ts@@ -147,6 +147,8 @@ export interface AgentAdapter {readProjectSetup(root: string): Promise<boolean | undefined>;/** Enables the plugin for the whole project and adds the guard against sessions without it. */writeProjectSetup(root: string): Promise<FileChange>;+ /** Updates an outdated guard or marketplace an earlier setup wrote; the file when it changed. */+ refreshProjectSetup(root: string): Promise<string | undefined>;/** Records that this session started with the plugin loaded; the guard looks for it. */markLive(sessionId: string): Promise<void>;}diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.tsindex 6e8a00d..48627df 100644--- a/packages/cli/src/commands/init.ts+++ b/packages/cli/src/commands/init.ts@@ -208,6 +208,10 @@ export async function init(dir: string, opts: InitOptions, io: Io): Promise<voidawait register(io, root, existing.data.name);if (opts.allowSkill !== undefined) await askSkillPermission(io, opts);if (opts.projectSettings !== undefined) await askProjectSettings(io, root, opts);+ await bestEffort(io, "could not refresh the project's Claude Code settings", async () => {+ const file = await agent.refreshProjectSetup(root);+ if (file) io.out(`Updated the cogit guard and marketplace in ${file}.`);+ });await refreshGitIntegration(io, root);io.out(`Already initialized: ${existing.data.name} (${root})`);return;diff --git a/packages/cli/test/project-setup.test.ts b/packages/cli/test/project-setup.test.tsindex b40a351..f7e1700 100644--- a/packages/cli/test/project-setup.test.ts+++ b/packages/cli/test/project-setup.test.ts@@ -10,10 +10,12 @@ import {PLUGIN_ID,projectSettingsPath,readProjectSetup,+ refreshProjectSetup,writeProjectSetup,} from "../src/agents/claude/settings.js";+import { init } from "../src/commands/init.js";import { runHook } from "../src/hooks/run.js";-import { envFor, T0, tempDir } from "./helpers.js";+import { envFor, fakeIo, T0, tempDir } from "./helpers.js";let root: string;let tmp: string;@@ -52,6 +54,84 @@ async function withTmp<T>(fn: () => Promise<T>): Promise<T> {}describe("project setup", () => {+ /** What an older cogit wrote: its own guard command and the old repository. */+ const oldGuard = "node -e 'old' # cogit:guard";+ const writeOld = async (extra: Record<string, unknown> = {}) => {+ await mkdir(join(root, ".claude"), { recursive: true });+ await writeFile(+ projectSettingsPath(root),+ JSON.stringify({+ enabledPlugins: { [PLUGIN_ID]: true },+ extraKnownMarketplaces: { cogit: { source: { source: "github", repo: "old/cogit" } } },+ hooks: {+ UserPromptSubmit: [+ { hooks: [{ type: "command", command: "echo mine" }] },+ { hooks: [{ type: "command", command: oldGuard, timeout: 5 }] },+ ],+ },+ ...extra,+ }),+ );+ };++ it("replaces an outdated guard and marketplace, keeping everything else", async () => {+ await writeOld({ model: "x" });+ expect(await readProjectSetup(root)).toBe(false);+ expect(await refreshProjectSetup(root)).toBe(projectSettingsPath(root));+ const settings = await readSettings();+ expect(settings.model).toBe("x");+ expect(settings.extraKnownMarketplaces.cogit.source.repo).toBe("erensrtky/cogit");+ const commands = settings.hooks.UserPromptSubmit.map(+ (entry: { hooks: { command: string }[] }) => entry.hooks[0]?.command,+ );+ expect(commands).toEqual(["echo mine", GUARD_COMMAND]);+ expect(await readProjectSetup(root)).toBe(true);+ // Up to date: nothing to write.+ expect(await refreshProjectSetup(root)).toBeUndefined();+ });++ it("refreshes only what an earlier setup wrote", async () => {+ // No settings at all, or settings without our guard or marketplace.+ expect(await refreshProjectSetup(root)).toBeUndefined();+ await mkdir(join(root, ".claude"));+ await writeFile(projectSettingsPath(root), JSON.stringify({ hooks: { Stop: [] } }));+ expect(await refreshProjectSetup(root)).toBeUndefined();+ // Only the marketplace is outdated: the guard is not added.+ await writeFile(+ projectSettingsPath(root),+ JSON.stringify({+ hooks: {},+ extraKnownMarketplaces: { cogit: { source: { source: "github", repo: "old/cogit" } } },+ }),+ );+ expect(await refreshProjectSetup(root)).toBe(projectSettingsPath(root));+ const settings = await readSettings();+ expect(settings.hooks).toEqual({});+ expect(settings.extraKnownMarketplaces.cogit.source.repo).toBe("erensrtky/cogit");+ });++ it("writes one current guard over an outdated one", async () => {+ await writeOld();+ await writeProjectSetup(root);+ const text = JSON.stringify((await readSettings()).hooks);+ expect(text).not.toContain("'old'");+ expect(text.split("# cogit:guard").length - 1).toBe(1);+ });++ it("refreshes an outdated guard when init runs again in a set-up project", async () => {+ const env = envFor(tmp);+ await mkdir(join(root, ".git"));+ const options = { name: "p", git: "ignore", retention: "keep", language: "auto" } as const;+ await init(root, { ...options, allowSkill: false, projectSettings: false }, fakeIo(env));+ await writeOld();+ const io = fakeIo(env, [], true);+ await init(root, {}, io);+ expect(io.lines).toContain(+ `Updated the cogit guard and marketplace in ${projectSettingsPath(root)}.`,+ );+ expect(await readProjectSetup(root)).toBe(true);+ });+it("enables the plugin and adds the guard, keeping what was there", async () => {await mkdir(join(root, ".claude"));const other = { hooks: [{ type: "command", command: "echo other" }] };