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(server): rebuild a map another cogit version wrote before serving it

Erenee6e3820a1d1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c8cdf40..cf188e1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
+- After an upgrade the map no longer shows the old version's page until the next message: every map is stamped with the cogit version that built it, and the server rebuilds one another version wrote before serving it.
- A session the closing pass left untitled no longer shows as a bare id on the map and in the inbox: it takes its first message's title, and the side model is now told to write a title when the record has none.
### Changed
diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts
index 7e73e6a..8fa929e 100644
--- a/packages/cli/src/commands/build.ts
+++ b/packages/cli/src/commands/build.ts
@@ -1,5 +1,6 @@
import { readdir, stat } from "node:fs/promises";
import { dirname, resolve } from "node:path";
+import pkg from "../../package.json" with { type: "json" };
import { type ChatTurn, defaultAgent } from "../agents/index.js";
import {
type CommitChange,
@@ -37,7 +38,7 @@ import { loadAllSessions, loadConfig, loadProject } from "../store/stores.js";
import { attachThoughtTrailers, earliestSessionStart, readThoughtTrailers } from "../thought.js";
import { attachChanges } from "../view/change.js";
import { buildFileHistory, modelSessions } from "../view/files.js";
-import { buildViewModel, embedViewModel } from "../view/lineage.js";
+import { buildViewModel, embedViewModel, markBuiltBy } from "../view/lineage.js";
import type { LineageViewModel, ViewChatTurn, ViewPendingMessage } from "../view/model.js";
import {
buildPanel,
@@ -383,7 +384,10 @@ export async function buildProject(opts: BuildOptions): Promise<LineageViewModel
io.warn(`redacted ${redacted.count} secret-looking value(s) in the map`);
const template = opts.templateHtml ?? TEMPLATE_HTML;
- await atomicWrite(paths.html, embedViewModel(template, redacted.value));
+ await atomicWrite(
+ paths.html,
+ markBuiltBy(embedViewModel(template, redacted.value), pkg.version),
+ );
if (!opts.quiet) io.out(`Wrote ${paths.html}`);
return redacted.value;
},
diff --git a/packages/cli/src/server.ts b/packages/cli/src/server.ts
index 21aae30..7951784 100644
--- a/packages/cli/src/server.ts
+++ b/packages/cli/src/server.ts
@@ -23,6 +23,7 @@ import { MAX_OPEN, readMapView, setOpenSessions, setRowWidth } from "./store/map
import { globalPaths, projectPaths } from "./store/paths.js";
import { setMessageRead } from "./store/read-state.js";
import { loadConfig, loadRegistry, updateConfig } from "./store/stores.js";
+import { builtBy } from "./view/lineage.js";
import { isPanelEditableKey, LIVE_META_NAME, TOKEN_META_NAME } from "./view/model.js";
import { buildPanel, panelSettings } from "./view/panel.js";
import { editableSettings } from "./view/settings.js";
@@ -939,6 +940,12 @@ export async function startMapServer(opts: MapServerOptions): Promise<MapServer>
let html: string;
try {
html = await readFile(target.file, "utf8");
+ // A map another cogit wrote (an older plugin's hook, a server before an
+ // upgrade) would show that version's page; build it again with this one.
+ if (target.root !== undefined && builtBy(html) !== VERSION) {
+ await buildProject({ dir: target.root, io: buildIo, quiet: true }).catch(() => undefined);
+ html = await readFile(target.file, "utf8");
+ }
} catch {
respond(res, 404, "not found");
return;
diff --git a/packages/cli/src/view/lineage.ts b/packages/cli/src/view/lineage.ts
index fc84c52..65448dd 100644
--- a/packages/cli/src/view/lineage.ts
+++ b/packages/cli/src/view/lineage.ts
@@ -370,6 +370,21 @@ export function escapeForScript(json: string): string {
}
/** Fills the template's empty data element with the serialized model. */
+/** The meta tag naming the cogit version that built a map page. */
+export const BUILT_BY_META_NAME = "cogit-built-by";
+
+/** Stamps the version that built the page, so a server of another version can rebuild it. */
+export function markBuiltBy(html: string, version: string): string {
+ const meta = `<meta name="${BUILT_BY_META_NAME}" content="${version}">`;
+ return html.replace(/<head(\s[^>]*)?>/i, (head) => `${head}${meta}`);
+}
+
+/** The version that built a page, or null for a page built before the stamp. */
+export function builtBy(html: string): string | null {
+ const match = html.match(/<meta name="cogit-built-by" content="([^"]*)">/);
+ return match?.[1] ?? null;
+}
+
export function embedViewModel(template: string, model: PageModel): string {
if (!template.includes(EMPTY_DATA_ELEMENT)) {
throw new Error(`the template has no empty <script id="${DATA_ELEMENT_ID}"> element`);
diff --git a/packages/cli/test/server.test.ts b/packages/cli/test/server.test.ts
index 336c0b1..1d6ea52 100644
--- a/packages/cli/test/server.test.ts
+++ b/packages/cli/test/server.test.ts
@@ -33,6 +33,7 @@ import { readMapView } from "../src/store/map-view.js";
import { globalPaths, projectPaths } from "../src/store/paths.js";
import { readReadState } from "../src/store/read-state.js";
import { loadConfig, updateConfig } from "../src/store/stores.js";
+import { builtBy } from "../src/view/lineage.js";
import { DATA_ELEMENT_ID, LIVE_META_NAME, TOKEN_META_NAME } from "../src/view/model.js";
import { envFor, fakeIo, noGitProject, tempDir } from "./helpers.js";
@@ -237,6 +238,23 @@ describe("map server", () => {
expect(reply.body).toContain("proj");
});
+ it("rebuilds a map another cogit version wrote before serving it", async () => {
+ const server = await start();
+ await writeFile(
+ projectPaths(root).html,
+ '<html><head><meta name="cogit-built-by" content="0.0.1"></head><body>old page</body></html>',
+ );
+ const reply = await request(mapPageUrl(server.url, root));
+ expect(reply.status).toBe(200);
+ expect(reply.body).not.toContain("old page");
+ expect(builtBy(reply.body)).toBe(pkg.version);
+ // Built by this version already: served as it is.
+ const again = await readFile(projectPaths(root).html, "utf8");
+ expect((await request(mapPageUrl(server.url, root))).body).toContain(
+ again.slice(again.indexOf("</head>")),
+ );
+ });
+
it("serves the panel at the root", async () => {
const server = await start();
const reply = await request(`${server.url}/`);
diff --git a/packages/cli/test/session-title.test.ts b/packages/cli/test/session-title.test.ts
index f73ca87..d2c2b66 100644
--- a/packages/cli/test/session-title.test.ts
+++ b/packages/cli/test/session-title.test.ts
@@ -20,3 +20,12 @@ describe("sessionTitle", () => {
expect(sessionTitle({ title: "", nodes: [] })).toBe("");
});
});
+
+describe("builtBy", () => {
+ it("reads back the version a page was stamped with, and null without one", async () => {
+ const { builtBy, markBuiltBy } = await import("../src/view/lineage.js");
+ const page = markBuiltBy("<html><head><title>x</title></head></html>", "1.2.3");
+ expect(builtBy(page)).toBe("1.2.3");
+ expect(builtBy("<html><head></head></html>")).toBeNull();
+ });
+});