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): put "left off here" at the newest message, not the newest record

Erenfdce10efaee6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9466510..6c85c6f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
+### Fixed
+- "Left off here" (the map's HEAD, the jump on load and the project switcher's left-off line) follows the newest message the user sent instead of the most recently updated session record, so fixing an old session's record no longer moves it there.
+
### Changed
- Map: the status card at the top of the page is gone; the map comes first. What it showed is in the side panel when you click a session (goal, done, pending with "needs you", next), and the agent gets it at session start.
diff --git a/packages/cli/src/view/panel.ts b/packages/cli/src/view/panel.ts
index a81939a..634aa17 100644
--- a/packages/cli/src/view/panel.ts
+++ b/packages/cli/src/view/panel.ts
@@ -40,15 +40,28 @@ function lastMessageTitle(session: Session): string {
return messages[messages.length - 1]?.title ?? "";
}
+/** When the session's newest message was sent; its `updatedAt` when no message has a time. */
+function lastActive(session: Session): number {
+ let last = Number.NEGATIVE_INFINITY;
+ for (const node of session.nodes) {
+ if (node.kind !== "message" || node.at === undefined) continue;
+ const at = Date.parse(node.at);
+ if (!Number.isNaN(at) && at > last) last = at;
+ }
+ if (last !== Number.NEGATIVE_INFINITY) return last;
+ const updated = Date.parse(session.updatedAt);
+ return Number.isNaN(updated) ? Number.NEGATIVE_INFINITY : updated;
+}
+
/**
- * Where the newest session left off: its own `next` step, or the title of its
- * last message when no next step was recorded.
+ * Where the work left off: the `next` step of the session with the newest
+ * message (not the most recently updated record — fixing an old record must
+ * not move it), or the title of its last message when no next step was recorded.
*/
export function leftOffLine(sessions: Session[]): string {
- const newest = [...sessions].sort((a, b) => {
- const byTime = Date.parse(b.updatedAt) - Date.parse(a.updatedAt);
- return Number.isNaN(byTime) ? a.sessionId.localeCompare(b.sessionId) : byTime;
- })[0];
+ const newest = [...sessions].sort(
+ (a, b) => lastActive(b) - lastActive(a) || a.sessionId.localeCompare(b.sessionId),
+ )[0];
if (!newest) return "";
return newest.status.next.trim() || lastMessageTitle(newest);
}
diff --git a/packages/cli/test/switcher.test.ts b/packages/cli/test/switcher.test.ts
index 2109b1c..069e59f 100644
--- a/packages/cli/test/switcher.test.ts
+++ b/packages/cli/test/switcher.test.ts
@@ -51,6 +51,7 @@ function session(input: {
updatedAt: string;
next?: string;
messages?: string[];
+ messageAt?: string;
}) {
return Session.parse({
schema: 1,
@@ -63,7 +64,7 @@ function session(input: {
kind: "message",
title,
parent: index === 0 ? null : `m${index}`,
- at: "2026-10-20T00:00:00.000Z",
+ at: input.messageAt ?? "2026-10-20T00:00:00.000Z",
})),
});
}
@@ -123,6 +124,26 @@ describe("leftOffLine", () => {
expect(leftOffLine(sessions)).toBe("Ship it.");
});
+ it("follows the newest message, not a record fixed later", () => {
+ const sessions = [
+ session({
+ sessionId: "old-but-touched",
+ updatedAt: "2026-10-25T10:00:00.000Z",
+ next: "Old step.",
+ messages: ["Old message"],
+ messageAt: "2026-10-20T10:00:00.000Z",
+ }),
+ session({
+ sessionId: "current",
+ updatedAt: "2026-10-24T10:00:00.000Z",
+ next: "Current step.",
+ messages: ["Newest message"],
+ messageAt: "2026-10-24T09:00:00.000Z",
+ }),
+ ];
+ expect(leftOffLine(sessions)).toBe("Current step.");
+ });
+
it("falls back to the newest session's last message title, then to empty", () => {
const sessions = [
session({
diff --git a/packages/template/src/graph.ts b/packages/template/src/graph.ts
index 96e454a..21d77dd 100644
--- a/packages/template/src/graph.ts
+++ b/packages/template/src/graph.ts
@@ -54,7 +54,23 @@ function latestSession(model: LineageViewModel): ViewSession | undefined {
)[0];
}
+/**
+ * "Left off here": the newest message the user sent, in whichever session it
+ * is — not the most recently updated record, since fixing an old session's
+ * record must not move HEAD there. Sessions whose messages carry no time fall
+ * back to the most recently updated session and its last message.
+ */
export function headOf(model: LineageViewModel): Head | null {
+ let best: { sessionId: string; messageId: string; at: number } | null = null;
+ for (const session of [...model.epochs.flatMap((epoch) => epoch.sessions), ...model.unassigned]) {
+ for (const message of session.messages) {
+ const at = time(message.at ?? "");
+ if (at === Number.NEGATIVE_INFINITY) continue;
+ if (best === null || at > best.at)
+ best = { sessionId: session.sessionId, messageId: message.id, at };
+ }
+ }
+ if (best !== null) return { sessionId: best.sessionId, messageId: best.messageId };
const latest = latestSession(model);
if (!latest) return null;
return {
diff --git a/packages/template/test/graph.test.ts b/packages/template/test/graph.test.ts
index c126220..d20a26e 100644
--- a/packages/template/test/graph.test.ts
+++ b/packages/template/test/graph.test.ts
@@ -37,7 +37,7 @@ const model: LineageViewModel = {
};
describe("headOf", () => {
- it("marks the newest message of the most recently updated session", () => {
+ it("marks the newest message", () => {
expect(headOf(model)).toEqual({ sessionId: "s1", messageId: "m2" });
});
@@ -55,19 +55,47 @@ describe("headOf", () => {
expect(headOf({ ...model, epochs: [], unassigned: [] })).toBeNull();
});
- it("picks the latest updatedAt across epochs and unassigned sessions", () => {
- const withUnassigned: LineageViewModel = {
+ it("follows the newest message, not the most recently updated record", () => {
+ const fixedLater: LineageViewModel = {
...model,
unassigned: [
{
...model.epochs[0]!.sessions[0]!,
sessionId: "u1",
+ // Its record was touched last (say, an old pending item was fixed),
+ // but its message is older than s1's newest one.
updatedAt: "2026-10-23T00:00:00.000Z",
messages: [message("m9")],
},
],
};
- expect(headOf(withUnassigned)).toEqual({ sessionId: "u1", messageId: "m9" });
+ expect(headOf(fixedLater)).toEqual({ sessionId: "s1", messageId: "m2" });
+
+ const newerMessage: LineageViewModel = {
+ ...fixedLater,
+ unassigned: [
+ { ...fixedLater.unassigned[0]!, messages: [message("m9", "2026-10-22T00:00:00.000Z")] },
+ ],
+ };
+ expect(headOf(newerMessage)).toEqual({ sessionId: "u1", messageId: "m9" });
+ });
+
+ it("falls back to the most recently updated session when messages carry no time", () => {
+ const untimed: LineageViewModel = {
+ ...model,
+ epochs: [
+ {
+ ...model.epochs[0]!,
+ sessions: [
+ {
+ ...model.epochs[0]!.sessions[0]!,
+ messages: [{ id: "m1", kind: "message", title: "m1", parent: null }],
+ },
+ ],
+ },
+ ],
+ };
+ expect(headOf(untimed)).toEqual({ sessionId: "s1", messageId: "m1" });
});
});