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(map): read the stored row widths on load, not only the embedded ones

Eren35219de5e24d
diff --git a/packages/cli/src/server.ts b/packages/cli/src/server.ts
index fa88ee0..c69b3c8 100644
--- a/packages/cli/src/server.ts
+++ b/packages/cli/src/server.ts
@@ -21,7 +21,7 @@ import { dismissInboxSession } from "./store/inbox-state.js";
import { LockBusyError } from "./store/lock.js";
import { globalPaths, projectPaths } from "./store/paths.js";
import { setMessageRead } from "./store/read-state.js";
-import { setRowWidth } from "./store/row-widths.js";
+import { readRowWidths, setRowWidth } from "./store/row-widths.js";
import { loadConfig, loadRegistry, updateConfig } from "./store/stores.js";
import { isPanelEditableKey, LIVE_META_NAME, TOKEN_META_NAME } from "./view/model.js";
import { buildPanel, panelSettings } from "./view/panel.js";
@@ -402,6 +402,42 @@ export async function startMapServer(opts: MapServerOptions): Promise<MapServer>
res.end(JSON.stringify({ ok: true, settings: editableSettings(panelSettings(config)) }));
}
+ /**
+ * `GET /api/row-width?dir`: the stored row widths as they are now. The page
+ * embeds them at build time, but a reload can come before the rebuild that a
+ * drag's write starts, so the page asks here too. Same Origin and token checks
+ * as the settings read, and a registered project only.
+ */
+ async function readRowWidthsRequest(
+ req: IncomingMessage,
+ res: ServerResponse,
+ port: number,
+ url: URL,
+ ): Promise<void> {
+ if (
+ !readOriginAllowed(req.headers.origin, port) ||
+ !tokenMatches(req.headers[CONFIG_TOKEN_HEADER])
+ ) {
+ respond(res, 403, "forbidden");
+ return;
+ }
+ const dir = url.searchParams.get("dir");
+ if (!dir) {
+ respond(res, 400, "dir is required");
+ return;
+ }
+ const root = await registeredRoot(env, dir);
+ if (root === null) {
+ respond(res, 403, "not a registered project");
+ return;
+ }
+ res.writeHead(200, {
+ "content-type": "application/json",
+ "cache-control": "no-store",
+ });
+ res.end(JSON.stringify({ ok: true, widths: await readRowWidths(root) }));
+ }
+
/**
* `GET /api/diff?dir&commit[&file]` (Story 10.5): one commit's patch for the
* message details. Read-only, a registered project only, and the same Origin
@@ -793,6 +829,14 @@ export async function startMapServer(opts: MapServerOptions): Promise<MapServer>
await readDiff(req, res, port, url);
return;
}
+ if (url.pathname === "/api/row-width") {
+ if (req.method !== "GET") {
+ respond(res, 405, "method not allowed");
+ return;
+ }
+ await readRowWidthsRequest(req, res, port, url);
+ return;
+ }
if (url.pathname === "/api/config") {
if (req.method !== "GET") {
respond(res, 405, "method not allowed");
diff --git a/packages/cli/test/server.test.ts b/packages/cli/test/server.test.ts
index fbea739..c1571b0 100644
--- a/packages/cli/test/server.test.ts
+++ b/packages/cli/test/server.test.ts
@@ -945,6 +945,28 @@ describe("row width endpoint", () => {
expect((await widthWrite(server, token, { ...good, pad: "x".repeat(5000) })).status).toBe(413);
});
+ it("reads the stored widths back for a reload that beats the rebuild", async () => {
+ const server = await start();
+ const token = await panelToken(server);
+ await widthWrite(server, token, { dir: root, sessionId: SESSION, width: 840 });
+ const headers = { origin: `http://127.0.0.1:${server.port}`, [CONFIG_TOKEN_HEADER]: token };
+ const url = `${server.url}/api/row-width?dir=${encodeURIComponent(root)}`;
+ const reply = await request(url, headers);
+ expect(reply.status).toBe(200);
+ expect(JSON.parse(reply.body)).toEqual({ ok: true, widths: { [SESSION]: 840 } });
+ expect((await request(url, { ...headers, [CONFIG_TOKEN_HEADER]: "nope" })).status).toBe(403);
+ expect((await request(`${server.url}/api/row-width`, headers)).status).toBe(400);
+ expect(
+ (
+ await request(
+ `${server.url}/api/row-width?dir=${encodeURIComponent(join(dir, "elsewhere"))}`,
+ headers,
+ )
+ ).status,
+ ).toBe(403);
+ expect((await request(url, headers, "HEAD")).status).toBe(405);
+ });
+
it("answers 500 instead of crashing when the state cannot be written", async () => {
const server = await start();
const token = await panelToken(server);
diff --git a/packages/template/src/Graph.tsx b/packages/template/src/Graph.tsx
index 39b77ef..acbfff0 100644
--- a/packages/template/src/Graph.tsx
+++ b/packages/template/src/Graph.tsx
@@ -14,7 +14,7 @@ import { createContext, useCallback, useContext, useEffect, useMemo, useState }
import { unreadViewMessages } from "../../cli/src/view/change.js";
import type { LineageViewModel, ViewSession } from "../../cli/src/view/model.js";
import { hitMessageIds, hitSessionIds } from "../../cli/src/view/search.js";
-import { pageToken, saveRead, saveRowWidth } from "./data.js";
+import { loadRowWidths, pageToken, saveRead, saveRowWidth } from "./data.js";
import { type FilesTarget, FilesView } from "./FilesView.js";
import { useFullscreen } from "./fullscreen.js";
import { GitGraph } from "./GitGraph.js";
@@ -250,6 +250,17 @@ function Flow({ model, search }: { model: LineageViewModel; search: MapSearchSta
const [unfolded, setUnfolded] = useState<Set<string>>(() => new Set());
/** Each session's message row width, dragged at the rows' right edge. */
const [rowWidths, setRowWidths] = useState<RowWidths>(() => rowWidthsOf(model.rowWidths));
+ // The embedded widths are as of the last build; a reload right after a drag
+ // can beat the rebuild, so a served page asks for the stored ones once.
+ useEffect(() => {
+ const token = pageToken();
+ if (token === null) return;
+ void loadRowWidths(model.project.path, token)
+ .then((widths) => setRowWidths(rowWidthsOf(widths)))
+ .catch(() => {
+ // The embedded widths stay; they are at most one drag behind.
+ });
+ }, [model.project.path]);
const resizeRows = useCallback((sessionId: string, width: number) => {
setRowWidths((previous) => {
// Back at the default (double-click): forget the session.
diff --git a/packages/template/src/data.ts b/packages/template/src/data.ts
index 6109ee6..9dee868 100644
--- a/packages/template/src/data.ts
+++ b/packages/template/src/data.ts
@@ -98,6 +98,23 @@ export async function saveRead(
}
}
+/**
+ * The stored row widths as they are now. The page embeds them at build time,
+ * but a reload right after a drag can beat the rebuild, so the page asks too.
+ */
+export async function loadRowWidths(dir: string, token: string): Promise<unknown> {
+ const response = await fetch(`/api/row-width?dir=${encodeURIComponent(dir)}`, {
+ headers: { "x-cogit-token": token },
+ cache: "no-store",
+ });
+ if (!response.ok) {
+ const text = (await response.text()).trim();
+ throw new Error(text || `could not read the row widths (HTTP ${response.status})`);
+ }
+ const body = (await response.json()) as { widths?: unknown };
+ return body.widths;
+}
+
/**
* Stores how wide one session's message rows are, or forgets it (`null`). The
* state lives in the project's `.cogit/state/`, so this needs the local server;