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(thought): skip the trailer for a chat working in another repository
Eren2b76458b7366
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8b9efae..434b1d9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Git older than 2.29 can download the team's records again: cogit no longer passes it `--no-write-fetch-head`, and a failed download names git's error instead of a line of its usage text.- A Shape diagram in the message panel keeps legible text: it shrinks to the panel's width but not below half size, and a tall or wide one scrolls inside its box instead of shrinking until the labels cannot be read.- A message is no longer lost from the record when the side model gets one of its fields wrong, or (in mixed mode) writes a field that belongs to the agent: only that field is left out, and the message keeps its title, summary and the rest.+- A chat that commits into another repository no longer stamps that commit with a `Thought:` trailer the other repository can never resolve: the trailer is added only when the chat runs in the project being committed (or in that working tree).## [1.0.0] - 2026-11-04### Changeddiff --git a/docs/format.md b/docs/format.mdindex 89fa64c..746da04 100644--- a/docs/format.md+++ b/docs/format.md@@ -269,7 +269,11 @@ naming the newest user message of the session that made the commit:`--continue` whose id moved, a session of another project), no trailer isadded. cogit never falls back to `.cogit/HEAD` here: it may name the othersession.-3. When no session is named (a human commit in a terminal), `.cogit/HEAD` is+3. When the named session runs in another directory (its transcript's `cwd` is+ neither in this project nor in the working tree being committed), no trailer+ is added: a session that commits into a neighbouring repository keeps its+ record in its own project, so the link could never be followed from here.+4. When no session is named (a human commit in a terminal), `.cogit/HEAD` isused when it is fresh (within `trailerMaxAgeHours`, default 6) and itstranscript is readable.diff --git a/packages/cli/src/agents/claude/index.ts b/packages/cli/src/agents/claude/index.tsindex b88e289..82ed99c 100644--- a/packages/cli/src/agents/claude/index.ts+++ b/packages/cli/src/agents/claude/index.ts@@ -28,6 +28,7 @@ import {readConversation,readForkOrigin,readLastTurn,+ readSessionCwd,readsHandoff,} from "./transcripts.js";@@ -53,6 +54,7 @@ export const claude: AgentAdapter = {collectTranscriptIds,readConversation,readForkOrigin,+ readSessionCwd,readsHandoff,readLastTurn,countUserLines,diff --git a/packages/cli/src/agents/claude/transcripts.ts b/packages/cli/src/agents/claude/transcripts.tsindex 4f042c3..b03c08a 100644--- a/packages/cli/src/agents/claude/transcripts.ts+++ b/packages/cli/src/agents/claude/transcripts.ts@@ -261,6 +261,31 @@ export async function isHeadlessTranscript(file: string): Promise<boolean> {}/** Where a forked transcript came from; null for a session of its own. */+/**+ * The directory the session runs in: the `cwd` of its first line that has one.+ * Undefined when the transcript has none in its first lines or cannot be read.+ */+export async function readSessionCwd(file: string): Promise<string | undefined> {+ let stream: ReturnType<typeof createReadStream> | undefined;+ try {+ stream = createReadStream(file, { encoding: "utf8" });+ const lines = createInterface({ input: stream, crlfDelay: Number.POSITIVE_INFINITY });+ let read = 0;+ for await (const text of lines) {+ if (++read > 50) break;+ try {+ const cwd = (JSON.parse(text) as { cwd?: unknown }).cwd;+ if (typeof cwd === "string" && cwd !== "") return cwd;+ } catch {}+ }+ return undefined;+ } catch {+ return undefined;+ } finally {+ stream?.destroy();+ }+}+export async function readForkOrigin(file: string): Promise<ForkOrigin | null> {return forkOrigin(file, await readAllTurns(file), readTranscriptTurns, isMessageLine);}diff --git a/packages/cli/src/agents/types.ts b/packages/cli/src/agents/types.tsindex e23d5a4..1196fac 100644--- a/packages/cli/src/agents/types.ts+++ b/packages/cli/src/agents/types.ts@@ -144,6 +144,8 @@ export interface AgentAdapter {readConversation(file: string): Promise<ChatTurn[]>;/** Where a forked session came from; null for a session of its own. */readForkOrigin(file: string): Promise<ForkOrigin | null>;+ /** The directory the session runs in, from its transcript; undefined when unknown. */+ readSessionCwd(file: string): Promise<string | undefined>;/** Whether the transcript shows the agent reading a handoff file (Story 8.6). */readsHandoff(file: string): Promise<boolean>;/**diff --git a/packages/cli/src/thought.ts b/packages/cli/src/thought.tsindex 5eda7b4..ebef9c0 100644--- a/packages/cli/src/thought.ts+++ b/packages/cli/src/thought.ts@@ -1,6 +1,6 @@import { execFile } from "node:child_process";-import { access } from "node:fs/promises";-import { isAbsolute, resolve } from "node:path";+import { access, realpath } from "node:fs/promises";+import { isAbsolute, relative, resolve } from "node:path";import { promisify } from "node:util";import { type AgentAdapter, defaultAgent } from "./agents/index.js";import { findGitRoot } from "./git.js";@@ -71,6 +71,32 @@ function messagePath(file: string): string {}/** The newest user message of a transcript, as the trailer's value. */+/**+ * True when the session's directory is in this project or in the working tree+ * the commit is made in: git runs the hook at that tree's top, which for a+ * linked worktree outside the project is not under the project's root.+ * Unknown counts as here, as before.+ */+async function sessionRunsIn(+ agent: AgentAdapter,+ transcript: string,+ dir: string,+): Promise<boolean> {+ const cwd = await agent.readSessionCwd(transcript);+ if (cwd === undefined) return true;+ const session = await realOrSame(cwd);+ for (const place of [dir, process.cwd()]) {+ const root = await realOrSame((await findGitRoot(place)) ?? place);+ const inside = relative(root, session);+ if (inside === "" || (!inside.startsWith("..") && !isAbsolute(inside))) return true;+ }+ return false;+}++async function realOrSame(path: string): Promise<string> {+ return realpath(path).catch(() => resolve(path));+}+async function newestThought(agent: AgentAdapter,transcript: string,@@ -136,6 +162,10 @@ export async function addThoughtTrailer(opts: TrailerOptions): Promise<boolean>if (named !== "") {const transcript = await agent.findTranscript(named, env);if (transcript === null) return false;+ // A session working in another repository (a library next to its project)+ // must not stamp its message here: that record lives in the other project,+ // so the link could never be followed from this one.+ if (!(await sessionRunsIn(agent, transcript, opts.dir))) return false;const value = await newestThought(agent, transcript, named);if (value === undefined) return false;await writeTrailer(opts.messageFile, value);diff --git a/packages/cli/test/thought.test.ts b/packages/cli/test/thought.test.tsindex ebcb9df..51ea446 100644--- a/packages/cli/test/thought.test.ts+++ b/packages/cli/test/thought.test.ts@@ -4,6 +4,7 @@ import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";import { dirname, join, relative } from "node:path";import { promisify } from "node:util";import { beforeEach, describe, expect, it } from "vitest";+import { readSessionCwd } from "../src/agents/claude/transcripts.js";import { blame } from "../src/commands/blame.js";import { buildProject } from "../src/commands/build.js";import { configSet } from "../src/commands/config.js";@@ -93,12 +94,13 @@ describe("the thought behind a commit (Story 9.4)", () => {await writeFile(paths.session("s1"), JSON.stringify(session(nodes)));};- const writeTranscript = async (id = "s1", text = "second"): Promise<string> => {+ const writeTranscript = async (id = "s1", text = "second", cwd?: string): Promise<string> => {const file = join(dir, "claude", "projects", "proj", `${id}.jsonl`);await mkdir(dirname(file), { recursive: true });await writeFile(file,[+ ...(cwd === undefined ? [] : [{ type: "system", cwd }]),// Uuids are unique per session; only a fork shares them (see fork.test.ts).{ type: "user", uuid: `${id}-u1`, timestamp: T0, message: { content: "first" } },{ type: "user", uuid: `${id}-u2`, timestamp: NEWEST_AT, message: { content: text } },@@ -363,6 +365,39 @@ describe("the thought behind a commit (Story 9.4)", () => {expect(text).not.toContain(thoughtOf());});+ it("adds nothing for a session that runs in another repository", async () => {+ const commit = async (cwd: string): Promise<boolean> =>+ addThoughtTrailer({+ dir: root,+ messageFile: await messageFile(),+ source: "message",+ env: { ...env, CLAUDE_CODE_SESSION_ID: "s4" },+ now: NOW,+ });+ // The session's own project, or a folder inside it: the link resolves here.+ await writeTranscript("s4", "here", join(root, "src"));+ expect(await commit(join(root, "src"))).toBe(true);+ // A session next door that commits into this repository: its record is elsewhere.+ const elsewhere = join(dir, "other-project");+ await mkdir(elsewhere, { recursive: true });+ await writeTranscript("s4", "there", elsewhere);+ expect(await commit(elsewhere)).toBe(false);+ // The working tree git runs the hook in counts too (a worktree outside the project).+ await writeTranscript("s4", "tree", process.cwd());+ expect(await commit(process.cwd())).toBe(true);+ });++ it("reads a session's directory from its transcript, or nothing", async () => {+ expect(await readSessionCwd(await writeTranscript("s5", "x", "/somewhere"))).toBe(+ "/somewhere",+ );+ expect(await readSessionCwd(await writeTranscript("s6", "x"))).toBeUndefined();+ expect(await readSessionCwd(join(dir, "missing.jsonl"))).toBeUndefined();+ const junk = join(dir, "junk.jsonl");+ await writeFile(junk, `not json\n${'{"a":1}\n'.repeat(60)}{"cwd":"/late"}\n`);+ expect(await readSessionCwd(junk)).toBeUndefined();+ });+it("works with no .cogit/HEAD at all (a headless claude -p run)", async () => {await writeTranscript("s3", "headless");const file = await messageFile();