Skip to content

Commit 1a82e26

Browse files
committed
fix(ui): reconcile every null tombstone and align session sort with the gateway
The sessions.changed merge deleted null-tombstoned fields from a hand-kept eleven-field list while the gateway's tombstone policy (session-event-payload.ts) had grown past it: toolOverrides, observerDigest, controlOwnerSessionKey, restartRecoveryStatus, and goal leaked literal null into rows typed optional-not-null, and every new tombstoned field would silently repeat the drift. One loop over the event's null-valued keys now owns the rule; updatedAt/activeLeafEntryId (the schema's only nullable row fields) keep their explicit handling. Net −24 lines. Riders in the same owner neighborhood: - compareSessionRowsByUpdatedAt gains the gateway's ascending-key tie-break (session-list-order.ts: "Stable key ties keep offset paging deterministic") so tied rows — including all updatedAt:null rows — stop visibly swapping when the canonical refresh replaces an event-driven reconcile. - Deleted the unreachable kind === "cron" wire guards (gateway folds cron to direct before projection, #115299; both sites needed an `as string` cast because the protocol type has no such member). - The stale-active-snapshot path returns the original result identity when no defaults were passed, so the caller's result === state.result publish gate skips a spurious sidebar re-render on every stale event.
1 parent 26ff3c9 commit 1a82e26

4 files changed

Lines changed: 88 additions & 38 deletions

File tree

‎ui/src/lib/sessions/navigation.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { describe, expect, it } from "vitest";
33
import type { GatewaySessionRow, SessionsListResult } from "../../api/types.ts";
44
import {
5+
compareSessionRowsByUpdatedAt,
56
isSystemCreatedSessionRow,
67
resolveSessionNavigation,
78
visibleSessionMatches,
@@ -437,3 +438,18 @@ describe("isSystemCreatedSessionRow", () => {
437438
expect(isSystemCreatedSessionRow({ ...base, ...fields } as GatewaySessionRow)).toBe(expected);
438439
});
439440
});
441+
442+
describe("compareSessionRowsByUpdatedAt", () => {
443+
it("breaks updatedAt ties by key, matching the gateway list order", () => {
444+
// Gateway compareSessionEntryPairs ends with an ascending key tie-break
445+
// ("Stable key ties keep offset paging deterministic"); a UI order that
446+
// differs makes tied rows visibly swap on the canonical refresh.
447+
const rows = [
448+
{ key: "agent:main:zeta", kind: "direct", updatedAt: null },
449+
{ key: "agent:main:alpha", kind: "direct", updatedAt: null },
450+
{ key: "agent:main:mid", kind: "direct", updatedAt: null },
451+
] as GatewaySessionRow[];
452+
const sorted = rows.toSorted(compareSessionRowsByUpdatedAt).map((row) => row.key);
453+
expect(sorted).toEqual(["agent:main:alpha", "agent:main:mid", "agent:main:zeta"]);
454+
});
455+
});

‎ui/src/lib/sessions/navigation.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ type VisibleSessionRowOptions = {
248248
export function isSystemCreatedSessionRow(row: GatewaySessionRow): boolean {
249249
// Cron rows are owned by the automation toggle; cron creation stamps a
250250
// system actor, so classifying them here would demand both toggles at once.
251-
if ((row.kind as string) === "cron" || isCronSessionKey(row.key)) {
251+
if (isCronSessionKey(row.key)) {
252252
return false;
253253
}
254254
if (row.createdActor?.type === "system") {
@@ -289,8 +289,7 @@ export function filterVisibleSessionRows(
289289
sessionMatchesArchivedFilter(row, options.archivedFilter) &&
290290
row.kind !== "global" &&
291291
row.kind !== "unknown" &&
292-
(options.showCron === true ||
293-
((row.kind as string) !== "cron" && !isCronSessionKey(row.key))) &&
292+
(options.showCron === true || !isCronSessionKey(row.key)) &&
294293
(options.showSystem === true || !isSystemCreatedSessionRow(row)) &&
295294
!isSubagentSessionKey(row.key) &&
296295
!row.spawnedBy &&
@@ -313,7 +312,13 @@ export function compareSessionRowsByUpdatedAt(a: GatewaySessionRow, b: GatewaySe
313312
return pinnedStateDiff;
314313
}
315314
const pinnedDiff = (b.pinnedAt ?? 0) - (a.pinnedAt ?? 0);
316-
return pinnedDiff !== 0 ? pinnedDiff : (b.updatedAt ?? 0) - (a.updatedAt ?? 0);
315+
if (pinnedDiff !== 0) {
316+
return pinnedDiff;
317+
}
318+
const updatedDiff = (b.updatedAt ?? 0) - (a.updatedAt ?? 0);
319+
// Stable key tie-break mirrors the gateway comparator (session-list-order.ts)
320+
// so tied rows don't swap when the canonical refresh replaces an event merge.
321+
return updatedDiff !== 0 ? updatedDiff : a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
317322
}
318323

319324
export function resolveSessionNavigation(input: SessionNavigationInput): SessionNavigation {

‎ui/src/lib/sessions/reconcile.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,53 @@ test("sessions.changed removes a label when the event carries null", () => {
8989
expect(reconciled.result?.sessions[0]?.displayName).toBeUndefined();
9090
});
9191

92+
test("sessions.changed deletes every null-tombstoned field, not a hand-kept list", () => {
93+
// The gateway tombstones more fields than the old per-field cascade knew
94+
// about; these five leaked literal null into rows typed optional-not-null.
95+
const result: SessionsListResult = {
96+
ts: 1,
97+
path: "",
98+
count: 1,
99+
defaults: { modelProvider: null, model: null, contextTokens: null },
100+
sessions: [
101+
{
102+
key: "agent:main:main",
103+
kind: "direct",
104+
updatedAt: 1,
105+
toolOverrides: { profile: "coding" },
106+
controlOwnerSessionKey: "agent:main:owner",
107+
restartRecoveryStatus: "pending",
108+
goal: "ship it",
109+
} as never,
110+
],
111+
};
112+
113+
const reconciled = reconcileSessionChanged(result, {
114+
sessionKey: "agent:main:main",
115+
reason: "patch",
116+
updatedAt: 2,
117+
toolOverrides: null,
118+
observerDigest: null,
119+
controlOwnerSessionKey: null,
120+
restartRecoveryStatus: null,
121+
goal: null,
122+
} as never);
123+
124+
expect(reconciled.applied).toBe(true);
125+
const row = reconciled.result?.sessions[0] as Record<string, unknown> | undefined;
126+
for (const field of [
127+
"toolOverrides",
128+
"observerDigest",
129+
"controlOwnerSessionKey",
130+
"restartRecoveryStatus",
131+
"goal",
132+
]) {
133+
expect(row?.[field], field).toBeUndefined();
134+
}
135+
// updatedAt stays legitimately nullable and must not be deleted by the loop.
136+
expect(row?.updatedAt).toBe(2);
137+
});
138+
92139
test("sessions.changed invalidates the complete creator facet until canonical refresh", () => {
93140
const key = "agent:main:main";
94141
const result = buildResult([

‎ui/src/lib/sessions/reconcile.ts‎

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,9 @@ export function reconcileSessionChanged(
402402
ts: _ts,
403403
...rowFields
404404
} = source;
405+
// The gateway wire folds cron/spawn-child into "direct" before projection
406+
// (session-utils-row.ts, #115299); cron detection is isCronSessionKey.
405407
const kind =
406-
rowFields.kind === "cron" ||
407408
rowFields.kind === "direct" ||
408409
rowFields.kind === "group" ||
409410
rowFields.kind === "global" ||
@@ -434,38 +435,17 @@ export function reconcileSessionChanged(
434435
updatedAt: updatedAt ?? null,
435436
...(sessionId ? { sessionId } : {}),
436437
} as GatewaySessionRow;
437-
if (rowFields.archivedAt === null) {
438-
delete row.archivedAt;
439-
}
440-
if (rowFields.archivedBy === null) {
441-
delete row.archivedBy;
442-
}
443-
if (rowFields.pinnedAt === null) {
444-
delete row.pinnedAt;
445-
}
446-
if (rowFields.label === null) {
447-
delete row.label;
448-
}
449-
if (rowFields.icon === null) {
450-
delete row.icon;
451-
}
452-
if (rowFields.category === null) {
453-
delete row.category;
454-
}
455-
if (rowFields.displayName === null) {
456-
delete row.displayName;
457-
}
458-
if (rowFields.createdActor === null) {
459-
delete row.createdActor;
460-
}
461-
if (rowFields.thinkingLevel === null) {
462-
delete row.thinkingLevel;
463-
}
464-
if (rowFields.lastRunError === null) {
465-
delete row.lastRunError;
466-
}
467-
if (rowFields.agentStatus === null) {
468-
delete row.agentStatus;
438+
// The gateway emits explicit null tombstones so subscribed clients clear
439+
// fields during merge-reconcile (session-event-payload.ts). Row fields are
440+
// typed optional-not-null, so every null tombstone deletes — a hand-kept
441+
// field list here drifts as new tombstoned fields ship (it already had:
442+
// toolOverrides/observerDigest/controlOwnerSessionKey/restartRecoveryStatus/
443+
// goal leaked null). updatedAt/activeLeafEntryId are the schema's only
444+
// legitimately nullable row fields and keep their explicit handling.
445+
for (const [field, value] of Object.entries(rowFields)) {
446+
if (value === null && field !== "updatedAt" && field !== "activeLeafEntryId") {
447+
delete row[field as keyof GatewaySessionRow];
448+
}
469449
}
470450
const next = reconcileSessionHistory(result, row, undefined, {
471451
...options,
@@ -567,7 +547,9 @@ export function reconcileSessionHistory(
567547
existing,
568548
);
569549
if (isStaleForActiveSession(visibleSession, existing)) {
570-
return { ...result, defaults: nextDefaults };
550+
// Keep result identity when nothing changed so the caller's
551+
// result === state.result publish gate can skip a spurious re-render.
552+
return defaults ? { ...result, defaults: nextDefaults } : result;
571553
}
572554
const sessions = sessionMatchesArchivedFilter(visibleSession, archivedFilter)
573555
? [

0 commit comments

Comments
 (0)