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.

feat(map): a saved map can carry its diffs

Eren24990f875f1b
diff --git a/packages/cli/src/view/model.ts b/packages/cli/src/view/model.ts
index 4e0353f..91d300f 100644
--- a/packages/cli/src/view/model.ts
+++ b/packages/cli/src/view/model.ts
@@ -326,6 +326,12 @@ export interface LineageViewModel {
* read from git at build time. Absent on pages built before 8.15.
*/
files?: ViewFile[];
+ /**
+ * Patches embedded at build time, by commit and then by file ("" for the
+ * whole commit), so a saved map can show a diff without the local server.
+ * A page built without them keeps saying the server is needed.
+ */
+ diffs?: Record<string, Record<string, string>>;
/**
* Settings embedded at build time (Story 8.13). The settings modal shows them
* read-only over `file://`; over the local server it reads the live values
diff --git a/packages/template/src/DiffView.tsx b/packages/template/src/DiffView.tsx
index ccdb527..af50bf5 100644
--- a/packages/template/src/DiffView.tsx
+++ b/packages/template/src/DiffView.tsx
@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
-import { loadDiff, pageToken } from "./data.js";
+import { embeddedDiff, loadDiff, pageToken } from "./data.js";
import { type DiffFile, type DiffHunk, parsePatch, sideBySide } from "./diffView.js";
/** The viewer's choice of diff layout; kept locally, never sent anywhere. */
@@ -117,7 +117,11 @@ export function DiffView({
setError(null);
const token = pageToken();
if (token === null) {
- setError("Diffs need the local server: open the map with `cogit open`.");
+ // A saved page answers from the patches it was built with; when it has
+ // none, the row says why instead of failing.
+ const carried = embeddedDiff(commit, file);
+ if (carried !== undefined) setText(carried);
+ else setError("Diffs need the local server: open the map with `cogit open`.");
return () => {
mounted = false;
};
diff --git a/packages/template/src/SidePanel.tsx b/packages/template/src/SidePanel.tsx
index 1bb27c2..7f55810 100644
--- a/packages/template/src/SidePanel.tsx
+++ b/packages/template/src/SidePanel.tsx
@@ -11,7 +11,7 @@ import type {
import { type CopyState, copyText } from "./clipboard.js";
import { parseSubject, splitPath, typeColour } from "./codeRefs.js";
import { DiffView } from "./DiffView.js";
-import { isLivePage, pageToken } from "./data.js";
+import { hasEmbeddedDiff, isLivePage, pageToken } from "./data.js";
import { formatTime, kindLabel } from "./format.js";
import { layoutGitGraph } from "./gitLayout.js";
import {
@@ -493,7 +493,7 @@ function NodeDetails({
subject={tag?.subject}
missing={missing}
root={model.project.path}
- expandable={live && !missing}
+ expandable={!missing && (live || hasEmbeddedDiff(commit))}
/>
);
})}
@@ -518,7 +518,7 @@ function NodeDetails({
file={file}
root={model.project.path}
commit={owner?.hash}
- expandable={live && owner !== undefined}
+ expandable={owner !== undefined && (live || hasEmbeddedDiff(owner.hash, file))}
onHistory={onOpenFileHistory}
/>
);
@@ -526,7 +526,9 @@ function NodeDetails({
</ul>
</section>
) : null}
- {!live && (node.commits?.length || node.files?.length) ? (
+ {!live &&
+ (node.commits?.length || node.files?.length) &&
+ !(node.commits ?? []).some((hash) => hasEmbeddedDiff(hash)) ? (
<p className="muted">Open with `cogit open` to see diffs.</p>
) : null}
{node.kind === "message" ? <Conversation session={session} id={node.nodeId} /> : null}
diff --git a/packages/template/src/data.ts b/packages/template/src/data.ts
index a049436..e03d4c5 100644
--- a/packages/template/src/data.ts
+++ b/packages/template/src/data.ts
@@ -180,6 +180,28 @@ export async function saveCaptureState(
}
}
+/**
+ * The patches the page itself carries, so a saved map (or the demo page) can
+ * show a diff with no server behind it. The reader keeps the latest model's
+ * copy; a page built without diffs simply has none.
+ */
+let embeddedDiffs: Record<string, Record<string, string>> | undefined;
+
+export function rememberEmbeddedDiffs(model: PageModel): void {
+ embeddedDiffs = model.kind === "lineage" ? model.diffs : undefined;
+}
+
+/** The patch for a commit (`""` for all of it) or one of its files, if the page carries it. */
+export function embeddedDiff(commit: string, file?: string): string | undefined {
+ const byFile = embeddedDiffs?.[commit];
+ return byFile === undefined ? undefined : byFile[file ?? ""];
+}
+
+/** Whether the page carries a patch for this commit (or file), so a row can open. */
+export function hasEmbeddedDiff(commit: string, file?: string): boolean {
+ return embeddedDiff(commit, file) !== undefined;
+}
+
/**
* One commit's patch for the message details (Story 10.5). Only the local
* server answers this; over `file://` the diff rows do not expand.
diff --git a/packages/template/src/main.tsx b/packages/template/src/main.tsx
index a54de62..1aea050 100644
--- a/packages/template/src/main.tsx
+++ b/packages/template/src/main.tsx
@@ -2,7 +2,7 @@ import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import { DATA_ELEMENT_ID, type PageModel } from "../../cli/src/view/model.js";
import { App } from "./App.js";
-import { isLivePage, loadViewModel, parseViewModel } from "./data.js";
+import { isLivePage, loadViewModel, parseViewModel, rememberEmbeddedDiffs } from "./data.js";
import { Panel } from "./Panel.js";
import "./styles.css";
import { initTheme } from "./theme.js";
@@ -29,6 +29,9 @@ async function fetchLatestModel(): Promise<PageModel | null> {
*/
function Page({ initial }: { initial: PageModel }) {
const [model, setModel] = useState(initial);
+ // A saved page may carry its diffs; keep the newest model's copy for the
+ // rows that open without a server behind them.
+ rememberEmbeddedDiffs(model);
useEffect(() => {
if (!isLivePage()) return;
diff --git a/packages/template/test/embedded-diffs.test.ts b/packages/template/test/embedded-diffs.test.ts
new file mode 100644
index 0000000..0d74c75
--- /dev/null
+++ b/packages/template/test/embedded-diffs.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from "vitest";
+import type { PageModel } from "../../cli/src/view/model.js";
+import { embeddedDiff, hasEmbeddedDiff, rememberEmbeddedDiffs } from "../src/data.js";
+
+function page(over: Record<string, unknown>): PageModel {
+ return { kind: "lineage", ...over } as unknown as PageModel;
+}
+
+describe("embedded diffs", () => {
+ it("answers a saved page's rows from its own patches", () => {
+ rememberEmbeddedDiffs(
+ page({ diffs: { abc1234: { "": "the whole commit", "a.ts": "one file" } } }),
+ );
+ expect(embeddedDiff("abc1234")).toBe("the whole commit");
+ expect(embeddedDiff("abc1234", "a.ts")).toBe("one file");
+ });
+
+ it("says nothing for a commit or file the page does not carry", () => {
+ rememberEmbeddedDiffs(page({ diffs: { abc1234: { "": "the whole commit" } } }));
+ expect(embeddedDiff("abc1234", "b.ts")).toBeUndefined();
+ expect(hasEmbeddedDiff("def5678")).toBe(false);
+ });
+
+ it("keeps the rows closed on a page built without diffs", () => {
+ rememberEmbeddedDiffs(page({}));
+ expect(hasEmbeddedDiff("abc1234")).toBe(false);
+ });
+});