Archived
fix(sessions): correlate startup progress by token instead of workspace
Startup progress could still be shown on the wrong session's row. Routing by known session id first closed the case where the browser knew the other session, but left open the case where it does not -- which the browser is designed to produce. While a create is pending for a workspace, applyCreatedSession deliberately withholds a session.created event for that workspace and stashes it, to avoid a duplicate row. So during exactly the window this feature exists for, a session created by an agent's spawn or by another tab is intentionally absent from the session list. Its startup events carried an unrecognised id and a matching cwd, and were routed onto the user's pending create row, showing a phase and a label belonging to another session. Workspace path was never evidence of identity; it was the only key both sides happened to share. Give them a real one. The browser already invents a temporary row id for a pending create, so it now sends that id with the create request as an opaque startupToken; the daemon carries it through construction, echoes it on the startup events it publishes for that construction, and the browser matches it exactly. The token is a throwaway label the daemon never interprets. It never becomes the session id: activity.sessionId still carries Pi's SessionManager id, which remains how an open of an already-known session is routed. With exact identity available, the guessing is deleted rather than gated. startupProgressPendingStart goes entirely, and with it the selected-machine comparison, the cwd filter, and the single-match ambiguity rule: a second concurrent create carries a different token, and a foreign workspace or non-selected machine carries no token this browser is waiting on, so those cases stop existing rather than needing detection. One Map lookup replaces a filtered scan. cwd comes off the event, since it existed only as the routing key and nothing else read it. No compatibility path is needed. session.startup is unreleased -- checked against the published tarball, not only git tags -- so no deployed daemon emits these events and no deployed browser parses them. An older daemon ignores the extra request field; a newer daemon talking to an older browser degrades to the pre-existing generic wording, as does any unmatched token. One silent behaviour change to state plainly: startupProgress guarded on `sessionId === "" || cwd === ""`. Removing cwd from the event removes the meaningful half of that guard, and that half had no test. The session-id half is kept, which is the half that actually protects honest reporting. The replaced ambiguity test is rewritten rather than dropped, so the same three scenarios still pin the user-visible guarantee -- no match means the generic wording stays -- now including the reproduced foreign-session case, which fails against the previous code. Session creation ordering, semantics, and queueing are unchanged; the token is a passthrough label read only to build an event.
This commit is contained in:
@@ -199,6 +199,11 @@ type SessionCreationProvenance = "tracked-subsession";
|
||||
interface StartSessionOptions {
|
||||
parentSession?: string;
|
||||
initialModel?: AgentModel;
|
||||
/**
|
||||
* Opaque label, echoed on this construction's startup progress so a browser
|
||||
* row with no session id yet can recognise its own.
|
||||
*/
|
||||
startupToken?: string;
|
||||
}
|
||||
|
||||
interface InternalStartSessionOptions extends StartSessionOptions {
|
||||
@@ -390,7 +395,7 @@ interface PendingSessionOpen {
|
||||
promise: Promise<ActiveSession<PiSessionRuntime>>;
|
||||
}
|
||||
|
||||
interface CreateSessionRuntimeOptions extends Pick<InternalStartSessionOptions, "initialModel" | "creationProvenance"> {
|
||||
interface CreateSessionRuntimeOptions extends Pick<InternalStartSessionOptions, "initialModel" | "creationProvenance" | "startupToken"> {
|
||||
notificationGeneration?: SessionNotificationGeneration;
|
||||
notifications?: "enabled" | "disabled";
|
||||
/**
|
||||
@@ -992,6 +997,7 @@ export class PiSessionService implements SessionRouteService {
|
||||
cwd,
|
||||
{
|
||||
startupIntent: "create",
|
||||
...(options.startupToken === undefined ? {} : { startupToken: options.startupToken }),
|
||||
...(options.initialModel === undefined ? {} : { initialModel: options.initialModel }),
|
||||
...(options.creationProvenance === undefined ? {} : { creationProvenance: options.creationProvenance }),
|
||||
},
|
||||
@@ -2353,7 +2359,7 @@ export class PiSessionService implements SessionRouteService {
|
||||
cwd: string,
|
||||
options: CreateSessionRuntimeOptions = {},
|
||||
): Promise<ActiveSession<PiSessionRuntime>> {
|
||||
const startup = this.startupProgress(sessionManager, cwd, options.startupIntent ?? "open");
|
||||
const startup = this.startupProgress(sessionManager, options.startupIntent ?? "open", options.startupToken);
|
||||
try {
|
||||
return await this.createSessionRuntime(sessionManager, cwd, options, startup);
|
||||
} finally {
|
||||
@@ -2999,23 +3005,23 @@ export class PiSessionService implements SessionRouteService {
|
||||
/**
|
||||
* Build the reporter for one session construction.
|
||||
*
|
||||
* The session id and cwd are both known before any await — a `SessionManager`
|
||||
* has its id from construction — so the daemon can name what it is starting
|
||||
* even though the `PiAgentSession` that {@link publishActivity} needs does not
|
||||
* exist yet. When either is missing there is nothing honest to route on, so
|
||||
* the reporter stays silent and the browser keeps its own generic wording.
|
||||
* The session id is known before any await — a `SessionManager` has its id
|
||||
* from construction — so the daemon can name what it is starting even though
|
||||
* the `PiAgentSession` that {@link publishActivity} needs does not exist yet.
|
||||
* Without an id there is nothing to report against, so the reporter stays
|
||||
* silent and the browser keeps its own generic wording.
|
||||
*/
|
||||
private startupProgress(sessionManager: PiSessionManager, cwd: string, intent: "create" | "open"): SessionStartupProgressReporter {
|
||||
private startupProgress(sessionManager: PiSessionManager, intent: "create" | "open", startupToken: string | undefined): SessionStartupProgressReporter {
|
||||
const sessionId = sessionManager.getSessionId();
|
||||
if (sessionId === "" || cwd === "") return { report: noop, end: noop };
|
||||
if (sessionId === "") return { report: noop, end: noop };
|
||||
const label = intent === "create" ? "Creating session" : "Opening session";
|
||||
return {
|
||||
report: (phase) => { this.publishStartupProgress(sessionId, cwd, label, "active", this.startupDetail(phase)); },
|
||||
report: (phase) => { this.publishStartupProgress(sessionId, startupToken, label, "active", this.startupDetail(phase)); },
|
||||
end: () => {
|
||||
// A real activity published during the window (an extension error, say)
|
||||
// is the truth about this session and must survive the clear.
|
||||
if (this.activities.has(sessionId)) return;
|
||||
this.publishStartupProgress(sessionId, cwd, "idle", "idle", undefined);
|
||||
this.publishStartupProgress(sessionId, startupToken, "idle", "idle", undefined);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -3027,17 +3033,17 @@ export class PiSessionService implements SessionRouteService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Report startup progress on the global channel only, keyed by `cwd` so a
|
||||
* browser row that has no session id yet can find it.
|
||||
* Report startup progress on the global channel only, echoing the caller's
|
||||
* correlation token so a waiting browser row recognises its own construction.
|
||||
*
|
||||
* Unlike {@link publishActivity} this deliberately records nothing: no
|
||||
* `activities` entry, no workspace activity, no unread observation. There is
|
||||
* no session to own that state, and a failed creation would leave it stranded.
|
||||
*/
|
||||
private publishStartupProgress(sessionId: string, cwd: string, label: string, phase: "active" | "idle", detail: string | undefined): void {
|
||||
private publishStartupProgress(sessionId: string, startupToken: string | undefined, label: string, phase: "active" | "idle", detail: string | undefined): void {
|
||||
const at = new Date().toISOString();
|
||||
const activity = detail === undefined ? { sessionId, phase, label, at } : { sessionId, phase, label, detail, at };
|
||||
this.events.publishGlobal({ type: "session.startup", cwd, activity });
|
||||
this.events.publishGlobal(startupToken === undefined ? { type: "session.startup", activity } : { type: "session.startup", startupToken, activity });
|
||||
}
|
||||
|
||||
private publishActivity(session: PiAgentSession, label: string, phase: "active" | "idle" | "error", detail?: string): void {
|
||||
|
||||
Reference in New Issue
Block a user