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 changed file opens its history

Erendb7476b505d0
diff --git a/packages/template/src/FilesView.tsx b/packages/template/src/FilesView.tsx
index fc70618..132a2cc 100644
--- a/packages/template/src/FilesView.tsx
+++ b/packages/template/src/FilesView.tsx
@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
-import type { ViewFile } from "../../cli/src/view/model.js";
+import type { ViewFile, ViewSession } from "../../cli/src/view/model.js";
import { formatStamp } from "./format.js";
import { shortHash } from "./graph.js";
@@ -15,20 +15,60 @@ export interface FilesTarget {
messageId: string;
}
+export interface FilesFocus {
+ /** The file whose history the card asked for. */
+ path: string;
+ /** Counts the asks, so the same file opens again after the user folded it. */
+ key: number;
+}
+
+/**
+ * The reason behind one hit, as `cogit why` prints it: the message's own why,
+ * or the first option it rejected when there is no why yet.
+ */
+function reasonOf(
+ sessions: readonly ViewSession[] | undefined,
+ hit: { sessionId: string; messageId: string },
+): string | undefined {
+ const node = sessions
+ ?.find((session) => session.sessionId === hit.sessionId)
+ ?.messages.find((message) => message.id === hit.messageId);
+ if (node?.why !== undefined && node.why !== "") return node.why;
+ const rejected = node?.alternatives?.[0];
+ return rejected === undefined ? undefined : `not: ${rejected.option}`;
+}
+
export function FilesView({
files,
+ sessions,
active,
+ focus = null,
onSelect,
onClose,
}: {
files: readonly ViewFile[] | undefined;
+ /** The project's sessions, for each hit's reason. */
+ sessions?: readonly ViewSession[] | undefined;
/** The message the map has selected, if any. */
active: FilesTarget | null;
+ /** A file a message's card opened, shown with its history. */
+ focus?: FilesFocus | null;
onSelect: (target: FilesTarget) => void;
onClose: () => void;
}) {
const all = files ?? [];
- const [openPath, setOpenPath] = useState<string | null>(null);
+ const [openPath, setOpenPath] = useState<string | null>(focus?.path ?? null);
+
+ // The card asks for a file's history (its messages, the way `cogit why`
+ // answers for a path): open that file and bring its row into view.
+ useEffect(() => {
+ if (focus === null) return;
+ setOpenPath(focus.path);
+ const row = [...document.querySelectorAll<HTMLElement>("[data-file-path]")].find(
+ (element) => element.dataset.filePath === focus.path,
+ );
+ row?.scrollIntoView({ block: "nearest" });
+ }, [focus]);
// A map click (or a search hit) may land on a message whose file is not open;
// reveal the file that holds it so the row and its history are visible.
@@ -63,7 +103,7 @@ export function FilesView({
{all.map((file) => {
const open = openPath === file.path;
return (
- <li key={file.path}>
+ <li key={file.path} data-file-path={file.path}>
<div className="outline-row">
<button
type="button"
@@ -89,6 +129,7 @@ export function FilesView({
{file.hits.map((hit) => {
const selected =
active?.sessionId === hit.sessionId && active.messageId === hit.messageId;
+ const reason = reasonOf(sessions, hit);
return (
<li key={`${hit.sessionId}:${hit.messageId}`}>
<button
@@ -115,6 +156,11 @@ export function FilesView({
</span>
)}
</span>
+ {reason === undefined ? null : (
+ <span className="file-hit-reason" title={reason}>
+ {reason}
+ </span>
+ )}
</button>
</li>
);
diff --git a/packages/template/src/Graph.tsx b/packages/template/src/Graph.tsx
index 48048e4..14d0b13 100644
--- a/packages/template/src/Graph.tsx
+++ b/packages/template/src/Graph.tsx
@@ -21,7 +21,7 @@ import type { LineageViewModel, ViewSession } from "../../cli/src/view/model.js"
import { hitMessageIds, hitSessionIds } from "../../cli/src/view/search.js";
import { CapturesView, captureCounts, capturesButtonVisible } from "./CapturesView.js";
import { loadMapView, pageToken, saveOpenSessions, saveRowWidth } from "./data.js";
-import { type FilesTarget, FilesView } from "./FilesView.js";
+import { type FilesFocus, type FilesTarget, FilesView } from "./FilesView.js";
import { useFullscreen } from "./fullscreen.js";
import { GitGraph } from "./GitGraph.js";
import { type GitLayout, layoutGitGraph, ROW_WIDTH } from "./gitLayout.js";
@@ -338,6 +338,8 @@ function Flow({ model, search }: { model: LineageViewModel; search: MapSearchSta
const [showOutline, setShowOutline] = useState(true);
// The files and outline views share the left column; only one shows.
const [showFiles, setShowFiles] = useState(false);
+ // The file a message's card asked the history of; the Files view opens on it.
+ const [filesFocus, setFilesFocus] = useState<FilesFocus | null>(null);
const [showCaptures, setShowCaptures] = useState(false);
const [person, setPerson] = useState<PersonFilter>(null);
const people = useMemo(() => peopleOf(model), [model]);
@@ -645,6 +647,14 @@ function Flow({ model, search }: { model: LineageViewModel; search: MapSearchSta
[ensureOpen],
);
+ // A changed file in a message's card opens that file's history in the Files
+ // view: every message that touched it, the map's side of `cogit why <path>`.
+ const openFileHistory = useCallback((path: string) => {
+ setShowFiles(true);
+ setShowOutline(false);
+ setShowCaptures(false);
+ setFilesFocus((current) => ({ path, key: (current?.key ?? 0) + 1 }));
+ }, []);
// A file row (8.15) opens its message exactly like a dot click, and the view
// moves to it once the session's layout is in place.
const [fileJumps, setFileJumps] = useState(0);
@@ -861,11 +871,13 @@ function Flow({ model, search }: { model: LineageViewModel; search: MapSearchSta
) : showFiles ? (
<FilesView
files={model.files}
+ sessions={model.sessions}
active={
selection?.type === "node"
? { sessionId: selection.sessionId, messageId: selection.nodeId }
: null
}
+ focus={filesFocus}
onSelect={selectFromFiles}
onClose={() => setShowFiles(false)}
/>
@@ -904,6 +916,7 @@ function Flow({ model, search }: { model: LineageViewModel; search: MapSearchSta
setRevealCaptures(0);
}}
onNavigate={selectFromOutline}
+ onOpenFileHistory={openFileHistory}
bigChangeLines={bigChangeLines}
/>
</div>
diff --git a/packages/template/src/SidePanel.tsx b/packages/template/src/SidePanel.tsx
index e6903ef..1bb27c2 100644
--- a/packages/template/src/SidePanel.tsx
+++ b/packages/template/src/SidePanel.tsx
@@ -375,11 +375,13 @@ function NodeDetails({
model,
node,
onNavigate,
+ onOpenFileHistory,
bigChangeLines,
}: {
model: LineageViewModel;
node: NodeDetail;
onNavigate: ((target: OutlineTarget) => void) | undefined;
+ onOpenFileHistory: ((file: string) => void) | undefined;
/** A change at or above this many lines is marked as big. */
bigChangeLines: number;
}) {
@@ -517,6 +519,7 @@ function NodeDetails({
root={model.project.path}
commit={owner?.hash}
expandable={live && owner !== undefined}
+ onHistory={onOpenFileHistory}
/>
);
})}
@@ -626,11 +629,14 @@ function FileItem({
root,
commit,
expandable,
+ onHistory,
}: {
file: string;
root: string;
commit: string | undefined;
expandable: boolean;
+ /** Opens the file's history in the Files view; absent on a map without one. */
+ onHistory: ((file: string) => void) | undefined;
}) {
const [open, setOpen] = useState(false);
const { dir, name } = splitPath(file);
@@ -643,18 +649,30 @@ function FileItem({
);
return (
<li title={file}>
- {expandable && commit !== undefined ? (
- <button
- type="button"
- className="file-row"
- aria-expanded={open}
- onClick={() => setOpen((value) => !value)}
- >
- {content}
- </button>
- ) : (
- <div className="file-row">{content}</div>
- )}
+ <div className="file-item">
+ {expandable && commit !== undefined ? (
+ <button
+ type="button"
+ className="file-row"
+ aria-expanded={open}
+ onClick={() => setOpen((value) => !value)}
+ >
+ {content}
+ </button>
+ ) : (
+ <div className="file-row">{content}</div>
+ )}
+ {onHistory === undefined ? null : (
+ <button
+ type="button"
+ className="file-history"
+ title={`Every message that touched ${file}`}
+ onClick={() => onHistory(file)}
+ >
+ history
+ </button>
+ )}
+ </div>
{open && commit !== undefined ? <DiffView dir={root} commit={commit} file={file} /> : null}
</li>
);
@@ -731,6 +749,7 @@ export function DrillDown({
captureReveal = 0,
onClose,
onNavigate,
+ onOpenFileHistory,
bigChangeLines = 300,
}: {
model: LineageViewModel;
@@ -740,6 +759,8 @@ export function DrillDown({
onClose: () => void;
/** Moves the selection to another node and reveals it on the map. */
onNavigate?: (target: OutlineTarget) => void;
+ /** Opens a changed file's history in the Files view, like `cogit why <path>`. */
+ onOpenFileHistory?: ((file: string) => void) | undefined;
bigChangeLines?: number;
}) {
useEffect(() => {
@@ -798,6 +819,7 @@ export function DrillDown({
model={model}
node={node}
onNavigate={onNavigate}
+ onOpenFileHistory={onOpenFileHistory}
bigChangeLines={bigChangeLines}
/>
</div>
diff --git a/packages/template/src/styles.css b/packages/template/src/styles.css
index f858df4..2cf7142 100644
--- a/packages/template/src/styles.css
+++ b/packages/template/src/styles.css
@@ -1160,6 +1160,17 @@ body:has(.map-shell.is-expanded) {
text-overflow: ellipsis;
}
+/* The message's reason under its row, the way `cogit why` prints a decision. */
+.file-hit-reason {
+ display: block;
+ overflow: hidden;
+ color: var(--text);
+ font-size: 11px;
+ font-style: italic;
+ white-space: nowrap;
+ text-overflow: ellipsis;
+}
+
.file-hit-branch,
.file-hit-commit {
font-family: var(--mono, ui-monospace, SFMono-Regular, Menlo, monospace);
@@ -1693,6 +1704,36 @@ button.file-row:hover {
background: var(--hover);
}
+/* A changed file opens its history: every message that touched it, the map's
+ side of `cogit why <path>`. */
+.file-item {
+ display: flex;
+ align-items: center;
+}
+
+.file-item .file-row {
+ flex: 1 1 auto;
+ min-width: 0;
+}
+
+.file-history {
+ flex: none;
+ margin-right: 8px;
+ padding: 1px 8px;
+ color: var(--muted);
+ font: inherit;
+ font-size: 11px;
+ background: none;
+ border: 1px solid var(--line);
+ border-radius: 999px;
+ cursor: pointer;
+}
+
+.file-history:hover {
+ color: var(--text);
+ background: var(--hover);
+}
+
.file-name {
flex: none;
color: var(--text);
diff --git a/packages/template/test/files-view.test.tsx b/packages/template/test/files-view.test.tsx
new file mode 100644
index 0000000..3159d82
--- /dev/null
+++ b/packages/template/test/files-view.test.tsx
@@ -0,0 +1,84 @@
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { describe, expect, it } from "vitest";
+import type { ViewFile, ViewSession } from "../../cli/src/view/model.js";
+import { FilesView } from "../src/FilesView.js";
+
+const T0 = "2026-10-24T09:41:00.000Z";
+
+const file: ViewFile = {
+ path: "packages/template/src/fixture.ts",
+ count: 2,
+ lastAt: T0,
+ hits: [
+ {
+ sessionId: "s1",
+ sessionTitle: "Template package and single-file output",
+ messageId: "m4",
+ label: "m4",
+ at: T0,
+ title: "Build the dev fixture",
+ branch: "Template scaffold",
+ commits: ["1a2b3c4"],
+ },
+ {
+ sessionId: "s1",
+ sessionTitle: "Template package and single-file output",
+ messageId: "m5",
+ label: "m5",
+ at: T0,
+ title: "Check the page in the browser",
+ commits: [],
+ },
+ ],
+};
+
+const sessions = [
+ {
+ sessionId: "s1",
+ title: "Template package",
+ messages: [
+ { id: "m4", label: "m4", title: "Build the dev fixture", why: "The map needs data." },
+ {
+ id: "m5",
+ label: "m5",
+ title: "Check the page in the browser",
+ alternatives: [{ option: "trust the build", why: "layout breaks" }],
+ },
+ ],
+ },
+] as unknown as ViewSession[];
+
+function render(over: { focus?: { path: string; key: number } | null } = {}): string {
+ return renderToStaticMarkup(
+ createElement(FilesView, {
+ files: [file],
+ sessions,
+ active: null,
+ focus: over.focus ?? null,
+ onSelect: () => {},
+ onClose: () => {},
+ }),
+ );
+}
+
+describe("FilesView", () => {
+ it("keeps a file folded until the card asks for its history", () => {
+ const closed = render();
+ expect(closed).toContain("fixture.ts");
+ expect(closed).not.toContain("Build the dev fixture");
+ });
+
+ it("opens the file a message's card asked about, with its messages", () => {
+ const open = render({ focus: { path: file.path, key: 1 } });
+ expect(open).toContain("Build the dev fixture");
+ expect(open).toContain("Check the page in the browser");
+ });
+
+ it("prints each message's reason the way cogit why does", () => {
+ const open = render({ focus: { path: file.path, key: 1 } });
+ expect(open).toContain("The map needs data.");
+ // No why yet: the first rejected option stands in for it.
+ expect(open).toContain("not: trust the build");
+ });
+});