Skip to content

armgit / cogit

Public Imported from GitHub
0

Git for the intent behind agent-written code. Records what your coding agent decided and why, and links every commit to it.

fix(store): keep the agent's full answer in the conversation store

Erenbfae514d882d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4035201..70d95b3 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
+- The conversation store keeps the agent's full answer. A turn stored while the agent was still answering stayed empty or partial for good, so the map showed those messages without their answer; the next build fills in what the transcript still holds.
## [0.31.0] - 2026-11-01
### Added
diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts
index c80c7ca..42d44c0 100644
--- a/packages/cli/src/commands/build.ts
+++ b/packages/cli/src/commands/build.ts
@@ -153,8 +153,7 @@ async function readTranscriptViews(
const file = transcripts.get(session.sessionId);
const recordsMessages = session.nodes.some((node) => node.kind === "message");
const roots = [session.worktree?.path, root].filter((r): r is string => Boolean(r));
- const stored = await readConversationStore(root, session.sessionId);
- const storedByUuid = new Map(stored.map((turn) => [turn.uuid, turn]));
+ let stored = await readConversationStore(root, session.sessionId);
let transcript: ChatTurn[] | null = null;
if (file) {
try {
@@ -169,21 +168,19 @@ async function readTranscriptViews(
const fromTranscript =
transcript && transcript.length > 0 ? hashTurns(session.sessionId, transcript) : null;
if (fromTranscript) {
- const fresh = fromTranscript.filter(
- (turn) => turn.uuid !== undefined && !storedByUuid.has(turn.uuid),
- );
const hashes = new Map(
fromTranscript.flatMap((turn) =>
turn.uuid === undefined ? [] : [[turn.uuid, turn.hash] as [string, string]],
),
);
try {
- if (fresh.length > 0)
- await appendConversationTurns(
- root,
- session.sessionId,
- fresh.map((turn) => agent.displayTurn(turn, roots)),
- );
+ const appended = await appendConversationTurns(
+ root,
+ session.sessionId,
+ fromTranscript.map((turn) => agent.displayTurn(turn, roots)),
+ );
+ // The page reads these turns below, so it shows the answer this build just stored.
+ if (appended > 0) stored = await readConversationStore(root, session.sessionId);
// Turns stored before ids were stable get theirs now, before Claude Code
// deletes the transcript this hash comes from.
await appendConversationHashes(root, session.sessionId, hashes);
@@ -212,6 +209,7 @@ async function readTranscriptViews(
};
prepared.push(effective);
+ const storedByUuid = new Map(stored.map((turn) => [turn.uuid, turn]));
let turns: ViewChatTurn[];
if (fromTranscript) {
turns = fromTranscript.map((turn) => {
diff --git a/packages/cli/src/store/conversations.ts b/packages/cli/src/store/conversations.ts
index 1fdc6d0..d62e5ef 100644
--- a/packages/cli/src/store/conversations.ts
+++ b/packages/cli/src/store/conversations.ts
@@ -8,7 +8,9 @@ import { projectPaths } from "./paths.js";
/**
* The conversation as the drill-down shows it, kept in the project so it
* survives Claude Code deleting the transcript. One JSON line per turn, each
- * append one more gzip member: the file is append-only and never rewritten.
+ * append one more gzip member: the file is append-only and never rewritten. A
+ * turn first stored while the agent was still answering gets a later, fuller
+ * line for the same uuid.
*/
export interface StoredTurn {
/** The transcript's uuid for the user line; the key that makes appends idempotent. */
@@ -39,6 +41,13 @@ function isStoredTurn(value: unknown): value is StoredTurn {
);
}
+/** How much of the answer a turn holds, to tell a finished turn from a snapshot of it. */
+function answerSize(turn: Pick<StoredTurn, "replies" | "tools">): number {
+ return (
+ turn.replies.reduce((sum, reply) => sum + reply.length, 0) + JSON.stringify(turn.tools).length
+ );
+}
+
/**
* Every turn stored for the session, in the order they were appended. A missing
* file is an empty store; a corrupted member ends the read at what parsed, so a
@@ -58,7 +67,7 @@ export async function readConversationStore(
} catch {
return [];
}
- /** Later records for one uuid add a stable id without rewriting the turn. */
+ /** Later records for one uuid add a stable id or a fuller answer without rewriting the turn. */
const byUuid = new Map<string, StoredTurn>();
const order: string[] = [];
for (const line of text.split("\n")) {
@@ -76,27 +85,41 @@ export async function readConversationStore(
order.push(value.uuid);
continue;
}
- if (prev.hash === undefined && value.hash !== undefined) byUuid.set(value.uuid, value);
+ const fuller = answerSize(value) > answerSize(prev) ? value : prev;
+ const hash = prev.hash ?? value.hash;
+ byUuid.set(value.uuid, {
+ ...prev,
+ ...(hash === undefined ? {} : { hash }),
+ replies: fuller.replies,
+ tools: fuller.tools,
+ });
}
return order.map((uuid) => byUuid.get(uuid) as StoredTurn);
}
/**
- * Appends the turns the store does not hold yet, redacted, and returns how many
- * were written. Turns without a transcript uuid cannot be recognised again, so
- * they are left to the transcript; a build that has a transcript keeps showing
- * them. Never rewrites an existing turn.
+ * Appends the turns the store does not hold yet, and the ones whose answer has
+ * grown since they were stored, redacted, and returns how many were written. A
+ * build runs while the agent is still answering, so the first copy of a turn is
+ * often a snapshot with no replies. Turns without a transcript uuid cannot be
+ * recognised again, so they are left to the transcript; a build that has a
+ * transcript keeps showing them. Never rewrites an existing line.
*/
export async function appendConversationTurns(
root: string,
sessionId: string,
turns: ChatTurn[],
): Promise<number> {
- const known = new Set((await readConversationStore(root, sessionId)).map((turn) => turn.uuid));
+ const stored = new Map(
+ (await readConversationStore(root, sessionId)).map((turn) => [turn.uuid, turn]),
+ );
+ const seen = new Set<string>();
const fresh: StoredTurn[] = [];
for (const turn of turns) {
- if (turn.uuid === undefined || known.has(turn.uuid)) continue;
- known.add(turn.uuid);
+ if (turn.uuid === undefined || seen.has(turn.uuid)) continue;
+ seen.add(turn.uuid);
+ const held = stored.get(turn.uuid);
+ if (held && answerSize(turn) <= answerSize(held)) continue;
fresh.push({
uuid: turn.uuid,
...(turn.hash === undefined ? {} : { hash: turn.hash }),
@@ -108,13 +131,19 @@ export async function appendConversationTurns(
}
if (fresh.length === 0) return 0;
// The store outlives the transcript and may be committed by mistake, so it
- // gets the same secret net as the page, not the raw text.
- const { value } = redactDeep(fresh);
+ // gets the same secret net as the page, not the raw text. Compared again after
+ // it: a turn with a secret is longer raw than stored, and must not append on
+ // every build.
+ const value = redactDeep(fresh).value.filter((turn) => {
+ const held = stored.get(turn.uuid);
+ return !held || answerSize(turn) > answerSize(held);
+ });
+ if (value.length === 0) return 0;
const chunk = `${value.map((turn) => JSON.stringify(turn)).join("\n")}\n`;
const file = projectPaths(root).conversation(sessionId);
await mkdir(dirname(file), { recursive: true });
await appendFile(file, gzipSync(Buffer.from(chunk, "utf8")));
- return fresh.length;
+ return value.length;
}
/**
diff --git a/packages/cli/test/build.test.ts b/packages/cli/test/build.test.ts
index 15990e7..43b90ab 100644
--- a/packages/cli/test/build.test.ts
+++ b/packages/cli/test/build.test.ts
@@ -685,6 +685,35 @@ describe("cogit build", () => {
]);
});
+ it("shows and stores the answer of a turn first stored before the agent answered", async () => {
+ const folder = join(dir, "claude", "projects", "-proj");
+ await mkdir(folder, { recursive: true });
+ const user = {
+ type: "user",
+ uuid: "u1",
+ parentUuid: null,
+ timestamp: T0,
+ message: { content: "First" },
+ };
+ const answer = {
+ type: "assistant",
+ uuid: "a1",
+ parentUuid: "u1",
+ message: { content: [{ type: "text", text: "The answer" }] },
+ };
+ const transcript = join(folder, "s1.jsonl");
+ await writeFile(transcript, JSON.stringify(user));
+ await saveSession("s1", session({ sessionId: "s1", nodes: [node("m1", "message", "First")] }));
+
+ const early = await buildProject({ dir: root, io, templateHtml: TEMPLATE, quiet: true });
+ expect(early.sessions[0]?.chat?.[0]?.replies).toEqual([]);
+
+ await writeFile(transcript, [user, answer].map((line) => JSON.stringify(line)).join("\n"));
+ const late = await buildProject({ dir: root, io, templateHtml: TEMPLATE, quiet: true });
+ expect(late.sessions[0]?.chat?.[0]?.replies).toEqual(["The answer"]);
+ expect((await readConversationStore(root, "s1"))[0]?.replies).toEqual(["The answer"]);
+ });
+
it("keeps the conversation in the project and shows it after the transcript is gone", async () => {
const folder = join(dir, "claude", "projects", "-proj");
await mkdir(folder, { recursive: true });
diff --git a/packages/cli/test/conversations.test.ts b/packages/cli/test/conversations.test.ts
index ecf1092..e43c0b7 100644
--- a/packages/cli/test/conversations.test.ts
+++ b/packages/cli/test/conversations.test.ts
@@ -17,6 +17,33 @@ function turn(uuid: string | undefined, text: string, replies: string[] = []): C
}
describe("conversation store", () => {
+ it("takes a fuller answer for a turn first stored mid-answer, once", async () => {
+ const root = await tempDir();
+ await appendConversationTurns(root, "s1", [{ ...turn("u1", "one"), hash: "h1" }]);
+ expect(await appendConversationTurns(root, "s1", [turn("u1", "one", ["the answer"])])).toBe(1);
+ expect(await appendConversationTurns(root, "s1", [turn("u1", "one", ["the answer"])])).toBe(0);
+ const stored = await readConversationStore(root, "s1");
+ expect(stored).toHaveLength(1);
+ expect(stored[0]).toMatchObject({ hash: "h1", text: "one", replies: ["the answer"] });
+ });
+
+ it("keeps the fuller answer when a later turn holds less", async () => {
+ const root = await tempDir();
+ await appendConversationTurns(root, "s1", [turn("u1", "one", ["the whole answer"])]);
+ expect(await appendConversationTurns(root, "s1", [turn("u1", "one", ["the"])])).toBe(0);
+ expect((await readConversationStore(root, "s1"))[0]?.replies).toEqual(["the whole answer"]);
+ });
+
+ it("does not append again for a reply that only looks longer before redaction", async () => {
+ const root = await tempDir();
+ const secret = `token: ghp_${"a".repeat(36)}`;
+ await appendConversationTurns(root, "s1", [turn("u1", "one", [secret])]);
+ const held = (await readConversationStore(root, "s1"))[0]?.replies[0] ?? "";
+ expect(held).not.toContain("ghp_");
+ expect(held.length).toBeLessThan(secret.length);
+ expect(await appendConversationTurns(root, "s1", [turn("u1", "one", [secret])])).toBe(0);
+ });
+
it("is empty when the session was never stored", async () => {
expect(await readConversationStore(await tempDir(), "s1")).toEqual([]);
});