mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
fix(coding-agent): prevent duplicate messages on startup session switch (#7110)
This commit is contained in:
parent
61da9e2f37
commit
c2275d6703
2 changed files with 90 additions and 3 deletions
|
|
@ -1730,17 +1730,27 @@ export class InteractiveMode {
|
|||
}
|
||||
|
||||
private async rebindCurrentSession(options: { renderBeforeBind?: boolean } = {}): Promise<void> {
|
||||
const session = this.session;
|
||||
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = undefined;
|
||||
this.applyRuntimeSettings();
|
||||
|
||||
if (options.renderBeforeBind) {
|
||||
this.renderCurrentSessionState();
|
||||
this.subscribeToAgent();
|
||||
await this.bindCurrentSessionExtensions();
|
||||
} else {
|
||||
await this.bindCurrentSessionExtensions();
|
||||
}
|
||||
|
||||
await this.bindCurrentSessionExtensions();
|
||||
|
||||
if (this.session !== session) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!options.renderBeforeBind) {
|
||||
this.subscribeToAgent();
|
||||
}
|
||||
|
||||
await this.updateAvailableProviderCount();
|
||||
this.updateEditorBorderColor();
|
||||
this.updateTerminalTitle();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
import { describe, expect, it, vi } from "vitest";
|
||||
import { InteractiveMode } from "../../../src/modes/interactive/interactive-mode.ts";
|
||||
|
||||
type RebindContext = {
|
||||
session: object;
|
||||
unsubscribe?: () => void;
|
||||
applyRuntimeSettings: () => void;
|
||||
renderCurrentSessionState: () => void;
|
||||
bindCurrentSessionExtensions: () => Promise<void>;
|
||||
subscribeToAgent: () => void;
|
||||
updateAvailableProviderCount: () => Promise<void>;
|
||||
updateEditorBorderColor: () => void;
|
||||
updateTerminalTitle: () => void;
|
||||
};
|
||||
|
||||
type InteractiveModePrototype = {
|
||||
rebindCurrentSession(this: RebindContext, options?: { renderBeforeBind?: boolean }): Promise<void>;
|
||||
};
|
||||
|
||||
const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrototype;
|
||||
|
||||
describe("overlapping startup and replacement session rebinds", () => {
|
||||
it("does not subscribe from the stale startup rebind", async () => {
|
||||
const startupSession = {};
|
||||
const replacementSession = {};
|
||||
let resolveStartupBind!: () => void;
|
||||
let resolveReplacementBind!: () => void;
|
||||
|
||||
const startupBind = new Promise<void>((resolve) => {
|
||||
resolveStartupBind = resolve;
|
||||
});
|
||||
const replacementBind = new Promise<void>((resolve) => {
|
||||
resolveReplacementBind = resolve;
|
||||
});
|
||||
|
||||
const subscribeToAgent = vi.fn();
|
||||
const updateTerminalTitle = vi.fn();
|
||||
let bindCount = 0;
|
||||
|
||||
const context: RebindContext = {
|
||||
session: startupSession,
|
||||
applyRuntimeSettings: () => {},
|
||||
renderCurrentSessionState: () => {},
|
||||
bindCurrentSessionExtensions: () => {
|
||||
bindCount += 1;
|
||||
return bindCount === 1 ? startupBind : replacementBind;
|
||||
},
|
||||
subscribeToAgent,
|
||||
updateAvailableProviderCount: async () => {},
|
||||
updateEditorBorderColor: () => {},
|
||||
updateTerminalTitle,
|
||||
};
|
||||
|
||||
const startupRebind = interactiveModePrototype.rebindCurrentSession.call(context);
|
||||
expect(bindCount).toBe(1);
|
||||
|
||||
context.session = replacementSession;
|
||||
const replacementRebind = interactiveModePrototype.rebindCurrentSession.call(context, {
|
||||
renderBeforeBind: true,
|
||||
});
|
||||
|
||||
expect(bindCount).toBe(2);
|
||||
expect(subscribeToAgent).toHaveBeenCalledTimes(1);
|
||||
|
||||
resolveStartupBind();
|
||||
await startupRebind;
|
||||
|
||||
expect(subscribeToAgent).toHaveBeenCalledTimes(1);
|
||||
expect(updateTerminalTitle).not.toHaveBeenCalled();
|
||||
|
||||
resolveReplacementBind();
|
||||
await replacementRebind;
|
||||
|
||||
expect(subscribeToAgent).toHaveBeenCalledTimes(1);
|
||||
expect(updateTerminalTitle).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue