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(record): keep a message when one of its fields is malformed

Erenc783b908a52d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c7051b4..8b9efae 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The map's outline follows the search: it marks and scrolls to the hit you are on, and scrolls to a message you click on the map.
- 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.
## [1.0.0] - 2026-11-04
### Changed
diff --git a/packages/cli/src/commands/backfill.ts b/packages/cli/src/commands/backfill.ts
index e2931d6..13a8e84 100644
--- a/packages/cli/src/commands/backfill.ts
+++ b/packages/cli/src/commands/backfill.ts
@@ -289,15 +289,44 @@ export function writableAnswer(
if (Array.isArray(out.nodes)) {
const kept: unknown[] = [];
for (const node of out.nodes) {
- const fixed = summaryAsText(node);
- if (ownsNode(fixed, soleOwner) && LineageNodeInput.safeParse(fixed).success) kept.push(fixed);
- else dropped += 1;
+ const fixed = ownedFields(summaryAsText(node), soleOwner);
+ dropped += fixed.removed;
+ const salvaged = withoutBadFields(fixed.node);
+ if (salvaged === undefined) {
+ dropped += 1;
+ continue;
+ }
+ kept.push(salvaged.node);
+ dropped += salvaged.removed;
}
out.nodes = kept;
}
return { payload: out, dropped };
}
+/** What a node cannot do without; a bad one here loses the node. */
+const REQUIRED_FIELDS = new Set(["id", "kind", "title"]);
+
+/**
+ * A node with its malformed optional fields taken out (a `work` item over the
+ * length limit, an alternative without an option): one bad field must not cost
+ * the message its whole record. Undefined when a required field is bad.
+ */
+function withoutBadFields(node: unknown): { node: unknown; removed: number } | undefined {
+ if (typeof node !== "object" || node === null) return undefined;
+ let current = { ...(node as Record<string, unknown>) };
+ let removed = 0;
+ for (let attempt = 0; attempt < 20; attempt++) {
+ const parsed = LineageNodeInput.safeParse(current);
+ if (parsed.success) return { node: current, removed };
+ const bad = new Set(parsed.error.issues.map((issue) => String(issue.path[0] ?? "")));
+ if ([...bad].some((key) => key === "" || REQUIRED_FIELDS.has(key))) return undefined;
+ current = Object.fromEntries(Object.entries(current).filter(([key]) => !bad.has(key)));
+ removed += bad.size;
+ }
+ return undefined;
+}
+
/** Drops every null, so the schema's defaults fill the gaps. */
function stripNulls(value: unknown): unknown {
if (Array.isArray(value)) return value.map(stripNulls);
@@ -323,11 +352,15 @@ function summaryAsText(node: unknown): unknown {
return typeof text === "string" ? { ...rest, summary: text } : rest;
}
-/** In mixed mode the side model may not write a branch summary or the agent's decisions. */
-function ownsNode(node: unknown, soleOwner: boolean): boolean {
- if (soleOwner || typeof node !== "object" || node === null) return true;
- const n = node as Record<string, unknown>;
- return n.branch === undefined && n.agentDecisions === undefined;
+/**
+ * In mixed mode the side model may not write a branch summary or the agent's
+ * decisions: those fields are left out, and the rest of the node is kept.
+ */
+function ownedFields(node: unknown, soleOwner: boolean): { node: unknown; removed: number } {
+ if (soleOwner || typeof node !== "object" || node === null) return { node, removed: 0 };
+ const { branch, agentDecisions, ...rest } = node as Record<string, unknown>;
+ const removed = (branch === undefined ? 0 : 1) + (agentDecisions === undefined ? 0 : 1);
+ return { node: removed === 0 ? node : rest, removed };
}
export async function backfillTail(
diff --git a/packages/cli/test/backfill.test.ts b/packages/cli/test/backfill.test.ts
index a71a29d..46eca6b 100644
--- a/packages/cli/test/backfill.test.ts
+++ b/packages/cli/test/backfill.test.ts
@@ -9,6 +9,7 @@ import {
headlessEnv,
resultText,
withoutNulls,
+ writableAnswer,
} from "../src/commands/backfill.js";
import { init } from "../src/commands/init.js";
import { sessionWrite } from "../src/commands/session.js";
@@ -333,3 +334,72 @@ describe("backfill --tail", () => {
await expect(backfillTail(root, "s1", fakeIo(env))).rejects.toThrow();
});
});
+
+describe("writableAnswer", () => {
+ it("keeps a node whose only fault is an optional field, without that field", () => {
+ const { payload, dropped } = writableAnswer(
+ {
+ nodes: [
+ {
+ id: "m1",
+ kind: "message",
+ title: "Add the CSV parser",
+ summary: "Plain JavaScript, no library.",
+ work: ["x".repeat(400)],
+ alternatives: [{ why: "no option named" }],
+ },
+ { id: "m2", kind: "message", title: "" },
+ ],
+ },
+ "side",
+ );
+ expect(payload.nodes).toEqual([
+ {
+ id: "m1",
+ kind: "message",
+ title: "Add the CSV parser",
+ summary: "Plain JavaScript, no library.",
+ },
+ ]);
+ // Two bad fields of m1, and m2 whose title is missing.
+ expect(dropped).toBe(3);
+ });
+
+ it("leaves a branch summary to the agent in mixed mode", () => {
+ const branch = { title: "T", asked: "A", outcome: "O" };
+ const { payload, dropped } = writableAnswer(
+ { nodes: [{ id: "m1", kind: "message", title: "Start", branch }] },
+ "mixed",
+ );
+ // The summary stays the agent's; the message itself is still recorded.
+ expect(payload.nodes).toEqual([{ id: "m1", kind: "message", title: "Start" }]);
+ expect(dropped).toBe(1);
+ });
+
+ it("in mixed mode leaves out the agent's fields, not the message", () => {
+ const { payload, dropped } = writableAnswer(
+ {
+ status: { goal: "kept only for a sole owner" },
+ nodes: [
+ {
+ id: "m1",
+ kind: "message",
+ title: "Start the parser",
+ branch: { title: "Parser", asked: "a", outcome: "b" },
+ agentDecisions: [{ title: "Plain JS" }],
+ },
+ ],
+ },
+ "mixed",
+ );
+ expect(payload).toEqual({ nodes: [{ id: "m1", kind: "message", title: "Start the parser" }] });
+ expect(dropped).toBe(2);
+ });
+
+ it("still drops a node it cannot read at all", () => {
+ expect(writableAnswer({ nodes: ["not a node", null] }, "side")).toEqual({
+ payload: { nodes: [] },
+ dropped: 2,
+ });
+ });
+});