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(view): size map cards to their text instead of cutting it off

Eren322259a1cf57
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4e6eba7..7e138a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `codelineage session write --consume`: deletes the `--file` input once the record is saved and keeps it when the write fails. `gitPolicy=commit` now also ignores `.codelineage/inbox/`.
### Changed
+- Map cards size themselves to their text: titles and summaries wrap onto as many lines as they need instead of being cut off with "…", and the cards below move down to make room.
- Map opens like layers of an onion: sessions run down the page in time order, and inside the latest session only its top-level topics show, side by side as columns with an "N items inside" count; an opened topic lists its items down its column, nested ones indented, and any node with children expands and collapses on click (selecting a nested item in the outline opens every layer above it). Phases are labelled "topic", and collapsed session boxes are wide enough for their titles.
- `/codelineage:lineage` skill: organizes nodes by topic instead of by time (topics and sub-topics at any depth, time order kept inside each), reuses existing topics and moves misplaced nodes by resending them with the right `parent`.
- The npm package and the plugin manifest now take their version from `VERSION` (the one `./release` bumps): `npm run build` and `npm pack` copy it through `scripts/sync-version.mjs`, and a test fails while they differ. Both move from 0.1.0 to 0.4.0, so the first release continues at 0.5.0.
diff --git a/packages/template/src/Graph.tsx b/packages/template/src/Graph.tsx
index 8aa567a..f32c094 100644
--- a/packages/template/src/Graph.tsx
+++ b/packages/template/src/Graph.tsx
@@ -23,7 +23,7 @@ import {
useState,
} from "react";
import type { LineageViewModel } from "../../cli/src/view/model.js";
-import { formatDate, formatTime, kindLabel } from "./format.js";
+import { formatDate, formatTime, itemsInside, kindLabel } from "./format.js";
import { useFullscreen } from "./fullscreen.js";
import {
defaultExpanded,
@@ -144,9 +144,7 @@ function LineageBox({ id, data }: NodeProps<LineageFlowNode>) {
<strong title={data.title}>{data.title}</strong>
</div>
{data.hasChildren && !data.expanded ? (
- <p className="lineage-count">
- {data.count} item{data.count === 1 ? "" : "s"} inside
- </p>
+ <p className="lineage-count">{itemsInside(data.count)}</p>
) : data.at ? (
<time dateTime={data.at}>{formatTime(data.at)}</time>
) : data.summary ? (
diff --git a/packages/template/src/cardHeight.ts b/packages/template/src/cardHeight.ts
new file mode 100644
index 0000000..49a089c
--- /dev/null
+++ b/packages/template/src/cardHeight.ts
@@ -0,0 +1,98 @@
+/**
+ * Card heights for the map, computed from the text so every card shows its whole
+ * title and summary. The layout never measures the DOM; it wraps words against
+ * canvas text metrics (or a character estimate where no canvas exists).
+ */
+
+export type Measure = (text: string, font: string) => number;
+
+const FONT_FAMILY = 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif';
+const TITLE_FONT = `700 13px ${FONT_FAMILY}`;
+const BODY_FONT = `400 11px ${FONT_FAMILY}`;
+const KIND_FONT = `400 10px ${FONT_FAMILY}`;
+const TITLE_LINE = 17;
+const BODY_LINE = 15;
+/** Padding, borders and the gap above the body line. */
+const CHROME = 18;
+const BODY_GAP = 3;
+const HEAD_MIN = 20;
+/**
+ * Horizontal room the card's padding and borders take, plus a margin: the browser
+ * wraps a little earlier than the measured widths suggest.
+ */
+const INSET = 34;
+const TOGGLE = 26;
+const KIND_PADDING = 18;
+/** Wrapping is measured, not rendered; a little slack keeps the last line clear. */
+const SLACK = 4;
+
+let canvasMeasure: Measure | null | undefined;
+
+function defaultMeasure(): Measure {
+ if (canvasMeasure === undefined) {
+ canvasMeasure = null;
+ try {
+ const context = document.createElement("canvas").getContext("2d");
+ if (context) {
+ canvasMeasure = (text, font) => {
+ context.font = font;
+ return context.measureText(text).width;
+ };
+ }
+ } catch {
+ // no DOM (tests, server): fall back to the estimate
+ }
+ }
+ return canvasMeasure ?? estimate;
+}
+
+/** Average glyph width for the UI fonts, used when there is no canvas. */
+function estimate(text: string, font: string): number {
+ const size = Number.parseFloat(font.split(" ")[1] ?? "12");
+ return text.length * size * 0.56;
+}
+
+/** Number of lines `text` takes when wrapped at word boundaries to `width`. */
+export function countLines(text: string, width: number, font: string, measure: Measure): number {
+ const words = text.split(/\s+/).filter(Boolean);
+ if (words.length === 0) return 0;
+ let lines = 1;
+ let line = "";
+ for (const word of words) {
+ const next = line ? `${line} ${word}` : word;
+ if (measure(next, font) <= width || !line) {
+ // a single word wider than the card breaks inside itself
+ if (!line && measure(word, font) > width) {
+ lines += Math.ceil(measure(word, font) / width) - 1;
+ }
+ line = next;
+ } else {
+ lines += 1;
+ line = word;
+ if (measure(word, font) > width) lines += Math.ceil(measure(word, font) / width) - 1;
+ }
+ }
+ return lines;
+}
+
+export interface CardText {
+ kindLabel: string;
+ title: string;
+ /** The line under the title: a summary, a time or an "N items inside" count. */
+ body?: string;
+ hasToggle: boolean;
+}
+
+export function cardHeight(
+ card: CardText,
+ width: number,
+ measure: Measure = defaultMeasure(),
+): number {
+ const kindWidth = measure(card.kindLabel.toUpperCase(), KIND_FONT) * 1.05 + KIND_PADDING;
+ const titleWidth = width - INSET - kindWidth - (card.hasToggle ? TOGGLE : 0);
+ const titleLines = Math.max(1, countLines(card.title, titleWidth, TITLE_FONT, measure));
+ const head = Math.max(HEAD_MIN, titleLines * TITLE_LINE);
+ const bodyLines = card.body ? countLines(card.body, width - INSET, BODY_FONT, measure) : 0;
+ const body = bodyLines > 0 ? BODY_GAP + bodyLines * BODY_LINE : 0;
+ return Math.ceil(CHROME + head + body + SLACK);
+}
diff --git a/packages/template/src/format.ts b/packages/template/src/format.ts
index 5df0639..b1afdcf 100644
--- a/packages/template/src/format.ts
+++ b/packages/template/src/format.ts
@@ -18,3 +18,8 @@ export function formatDate(iso: string | null): string {
export function kindLabel(kind: string): string {
return kind === "phase" ? "topic" : kind;
}
+
+/** The line a collapsed topic shows in place of its summary. */
+export function itemsInside(count: number): string {
+ return `${count} item${count === 1 ? "" : "s"} inside`;
+}
diff --git a/packages/template/src/graph.ts b/packages/template/src/graph.ts
index d3b193e..e87c0e5 100644
--- a/packages/template/src/graph.ts
+++ b/packages/template/src/graph.ts
@@ -5,6 +5,8 @@ import ELK, {
type ElkNode,
} from "elkjs/lib/elk.bundled.js";
import type { LineageViewModel, ViewNode, ViewSession } from "../../cli/src/view/model.js";
+import { cardHeight } from "./cardHeight.js";
+import { formatTime, itemsInside, kindLabel } from "./format.js";
/**
* Builds the nested React Flow graph (epoch → session → node) and lays it out
@@ -14,8 +16,9 @@ import type { LineageViewModel, ViewNode, ViewSession } from "../../cli/src/view
*/
/** Fixed card sizes keep ELK fast: it never needs to measure DOM text. */
-const NODE_WIDTH = 220;
-const NODE_HEIGHT = 64;
+const NODE_WIDTH = 240;
+/** Cards grow with their text (see cardHeight); this is the floor. */
+const NODE_HEIGHT = 48;
const GROUP_HEIGHT = 56;
const GROUP_WIDTH = 260;
const GROUP_PADDING = 24;
@@ -168,6 +171,29 @@ interface Placement {
height: number;
}
+/** Tall enough for the card's whole title and the line below it, as the card renders them. */
+function nodeHeight(node: ViewNode, open: boolean): number {
+ const hasChildren = node.children.length > 0;
+ const body =
+ hasChildren && !open
+ ? itemsInside(countNodes(node.children))
+ : node.at
+ ? formatTime(node.at)
+ : node.summary;
+ return Math.max(
+ NODE_HEIGHT,
+ cardHeight(
+ {
+ kindLabel: kindLabel(node.kind),
+ title: node.title,
+ hasToggle: hasChildren,
+ ...(body ? { body } : {}),
+ },
+ NODE_WIDTH,
+ ),
+ );
+}
+
function treeEdge(source: string, target: string, targetHandle: string): FlowEdge {
return {
id: `edge:${source}->${target}`,
@@ -194,32 +220,29 @@ export function placeSessionNodes(
const edges: FlowEdge[] = [];
const top = GROUP_HEADER + 8;
let columnX = GROUP_PADDING;
- let maxRows = 0;
+ let bottom = top;
for (const root of nodes) {
- let row = 0;
+ let y = top;
let columnWidth = NODE_WIDTH;
const visit = (node: ViewNode, parent: string | null, depth: number): void => {
const id = nodeId(sessionGroup, node.id);
- boxes.set(id, {
- x: columnX + depth * INDENT,
- y: top + row * (NODE_HEIGHT + ROW_GAP),
- width: NODE_WIDTH,
- height: NODE_HEIGHT,
- });
- row += 1;
+ const open = isOpen(id);
+ const height = nodeHeight(node, open);
+ boxes.set(id, { x: columnX + depth * INDENT, y, width: NODE_WIDTH, height });
+ y += height + ROW_GAP;
columnWidth = Math.max(columnWidth, depth * INDENT + NODE_WIDTH);
if (parent !== null) edges.push(treeEdge(parent, id, TREE_LEFT));
- if (isOpen(id)) for (const child of node.children) visit(child, id, depth + 1);
+ if (open) for (const child of node.children) visit(child, id, depth + 1);
};
visit(root, null, 0);
- maxRows = Math.max(maxRows, row);
+ bottom = Math.max(bottom, y - ROW_GAP);
columnX += columnWidth + COLUMN_GAP;
}
return {
boxes,
edges,
width: Math.max(columnX - COLUMN_GAP, GROUP_PADDING) + GROUP_PADDING,
- height: top + maxRows * (NODE_HEIGHT + ROW_GAP) - ROW_GAP + GROUP_PADDING,
+ height: bottom + GROUP_PADDING,
};
}
diff --git a/packages/template/src/styles.css b/packages/template/src/styles.css
index a49e664..71e4876 100644
--- a/packages/template/src/styles.css
+++ b/packages/template/src/styles.css
@@ -434,25 +434,25 @@ body:has(.map-shell.is-expanded) {
.lineage-head {
display: flex;
- align-items: center;
+ align-items: flex-start;
gap: 6px;
min-width: 0;
}
+/* Text wraps; the layout sizes each card to fit it (cardHeight.ts mirrors these metrics). */
.lineage-head strong {
- overflow: hidden;
+ min-width: 0;
font-size: 13px;
- white-space: nowrap;
- text-overflow: ellipsis;
+ line-height: 17px;
+ overflow-wrap: anywhere;
}
.lineage-box p {
margin: 3px 0 0;
- overflow: hidden;
color: var(--muted);
font-size: 11px;
- white-space: nowrap;
- text-overflow: ellipsis;
+ line-height: 15px;
+ overflow-wrap: anywhere;
}
.lineage-box p.lineage-count {
@@ -461,7 +461,9 @@ body:has(.map-shell.is-expanded) {
.lineage-box time {
display: block;
- margin-top: 1px;
+ margin-top: 3px;
+ font-size: 11px;
+ line-height: 15px;
}
.kind {
diff --git a/packages/template/test/cardHeight.test.ts b/packages/template/test/cardHeight.test.ts
new file mode 100644
index 0000000..8e4e0fa
--- /dev/null
+++ b/packages/template/test/cardHeight.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it } from "vitest";
+import { cardHeight, countLines, type Measure } from "../src/cardHeight.js";
+
+/** Every character is 10px wide, so wrapping is easy to reason about. */
+const fixed: Measure = (text) => text.length * 10;
+
+describe("countLines", () => {
+ it("wraps at word boundaries", () => {
+ expect(countLines("", 100, "f", fixed)).toBe(0);
+ expect(countLines("aaaa bbbb", 100, "f", fixed)).toBe(1);
+ expect(countLines("aaaa bbbb cccc", 100, "f", fixed)).toBe(2);
+ });
+
+ it("breaks a word longer than the line", () => {
+ expect(countLines("a".repeat(25), 100, "f", fixed)).toBe(3);
+ expect(countLines(`ab ${"c".repeat(25)}`, 100, "f", fixed)).toBe(4);
+ });
+});
+
+describe("cardHeight", () => {
+ const card = { kindLabel: "step", title: "Short", hasToggle: false };
+
+ it("grows with a longer title and with a body line", () => {
+ const short = cardHeight(card, 240, fixed);
+ const long = cardHeight({ ...card, title: "word ".repeat(30) }, 240, fixed);
+ const withBody = cardHeight({ ...card, body: "summary ".repeat(20) }, 240, fixed);
+ expect(long).toBeGreaterThan(short);
+ expect(withBody).toBeGreaterThan(short);
+ });
+
+ it("leaves less room for the title when the card has a toggle", () => {
+ const title = "word ".repeat(9);
+ expect(cardHeight({ ...card, title, hasToggle: true }, 240, fixed)).toBeGreaterThanOrEqual(
+ cardHeight({ ...card, title }, 240, fixed),
+ );
+ });
+
+ it("falls back to an estimate without a canvas", () => {
+ expect(cardHeight(card, 240)).toBeGreaterThan(30);
+ });
+});