fix(sessions): preserve notification inbox authority

This commit is contained in:
Federico Jaramillo Martinez
2026-07-19 02:17:59 +02:00
parent 503c2c743d
commit 71fd091e0e
7 changed files with 84 additions and 28 deletions
@@ -1,6 +1,6 @@
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { join, resolve, sep } from "node:path";
import { describe, expect, it, vi } from "vitest";
import { PiSessionService, type PiAgentSession, type PiSessionRuntime } from "./piSessionService.js";
import { SessionNotificationStore } from "./sessionNotificationStore.js";
@@ -404,8 +404,10 @@ describe("PiSessionService lifecycle, listing, and reload", () => {
const hub = new CapturingSessionEventHub();
const store = notificationStore();
const branch = [{ type: "message", message: { role: "user", content: "existing" } }];
const canonicalCwd = resolve(tmpdir(), "pi-web-notification-workspace");
const rawEquivalentCwd = `${canonicalCwd}${sep}nested${sep}..`;
const fake = fakeRuntime("notification-session", {
sessionManager: fakeSessionManager("/workspace", {
sessionManager: fakeSessionManager(rawEquivalentCwd, {
getSessionId: () => "notification-session",
getBranch: () => branch,
}),
@@ -419,12 +421,13 @@ describe("PiSessionService lifecycle, listing, and reload", () => {
heartbeatIntervalMs: 60_000,
});
await service.start("/workspace");
await service.start(canonicalCwd);
const notify = boundNotify(fake);
notify("duplicate", "warning");
notify("duplicate", "error");
const snapshot = service.notificationInbox(sessionRef("notification-session"));
const snapshot = service.notificationInbox({ id: "notification-session", cwd: canonicalCwd });
expect(snapshot.summary.cwd).toBe(canonicalCwd);
expect(snapshot.notifications).toMatchObject([
{ id: "daemon-lifecycle-test:2", message: "duplicate", severity: "error" },
{ id: "daemon-lifecycle-test:1", message: "duplicate", severity: "warning" },
+15 -14
View File
@@ -1429,10 +1429,7 @@ export class PiSessionService implements SessionRouteService {
try {
await session.reload(priorGeneration === undefined ? undefined : {
beforeSessionStart: () => {
candidateGeneration = this.notificationStore.beginReplacement(priorGeneration, {
sessionId: session.sessionId,
cwd: session.sessionManager.getCwd(),
});
candidateGeneration = this.notificationStore.beginReplacement(priorGeneration, notificationIdentityForSession(session));
this.notificationGenerationBySession.set(session, candidateGeneration);
this.replaceSessionNotificationContext(session, candidateGeneration);
},
@@ -1629,8 +1626,7 @@ export class PiSessionService implements SessionRouteService {
if (this.hasActiveWork(session)) throw new Error("Stop current session activity before reloading");
const priorGeneration = this.notificationGenerationBySession.get(session);
const sessionId = session.sessionId;
const cwd = session.sessionManager.getCwd();
const { sessionId, cwd } = notificationIdentityForSession(session);
let candidateGeneration: SessionNotificationGeneration | undefined;
try {
await this.closeActive(
@@ -2016,17 +2012,18 @@ export class PiSessionService implements SessionRouteService {
: "external";
if (notificationOwnership === "registered") {
const notificationIdentity = notificationIdentityForSession(runtime.session);
const existingCandidate = this.notificationStore.beginReplacementForSession(
runtime.session.sessionId,
runtime.session.sessionManager.getCwd(),
notificationIdentity.sessionId,
notificationIdentity.cwd,
);
if (existingCandidate !== undefined) {
notificationGeneration = existingCandidate;
notificationOwnership = "replacement";
} else {
const registration = this.notificationStore.registerSession(
runtime.session.sessionId,
runtime.session.sessionManager.getCwd(),
notificationIdentity.sessionId,
notificationIdentity.cwd,
);
notificationGeneration = registration.generation;
this.publishNotificationMutations(registration.mutations);
@@ -2042,10 +2039,7 @@ export class PiSessionService implements SessionRouteService {
let candidateGeneration: SessionNotificationGeneration | undefined;
try {
if (priorGeneration !== undefined) {
candidateGeneration = this.notificationStore.beginReplacement(priorGeneration, {
sessionId: session.sessionId,
cwd: session.sessionManager.getCwd(),
});
candidateGeneration = this.notificationStore.beginReplacement(priorGeneration, notificationIdentityForSession(session));
this.notificationGenerationBySession.set(session, candidateGeneration);
}
this.bindRuntime(active, session);
@@ -2512,6 +2506,13 @@ function modelToClientModel(model: PiAgentSession["model"]): ClientSessionModel
};
}
function notificationIdentityForSession(session: PiAgentSession): { sessionId: string; cwd: string } {
return {
sessionId: session.sessionId,
cwd: canonicalizeStoredCwd(session.sessionManager.getCwd()),
};
}
function clientSessionFromListEntry(session: PiSessionListEntry): ClientSession {
return {
id: session.id,