fix(extension): keep user-opened tabs free & fix agent-tab leak on session stop (issue #57)

- session_stop: exclude tabs still tracked in agentCreatedTabs from the
  release decision so a tab that failed to close is no longer mistaken for
  a user tab (which previously kept the window open and leaked the tab).
- Distinguish user-created tabs from agent-created tabs in the Agent Window
  via a pending-tab counter consumed by chrome.tabs.onCreated, so tabs the
  user opens themselves (new-tab button / Cmd+T / open in new tab) are never
  taken over by the BrowserSkill control mask.
- background overlay: decide per-tab state BEFORE showing controls
  (overlayStateForTab + pushOverlayStateForTab) so a user tab receives hidden
  on its first overlay.ready ping instead of flashing control then hiding.
- AgentWindowApi.ensureActiveTab now resolves with the activated/created tab
  id instead of void, so SessionManager can record homeTabId. This fixes the
  tsc failure flagged in review (the interface previously returned
  Promise<void> while callers treated the result as number | null).
- Wire the production tabManagement.tabs / tabsQuery deps into
  tool.session_stop, both in the dispatcher and in the disconnect cleanup.
  They were never injected, which left the agent-tab cleanup and the
  window-release path as dead code outside tests (behaviour identical to
  main). Added a dispatcher-level integration test that fails without the
  wiring: a surviving user tab must release the window, not close it.
This commit is contained in:
Ljy-0827
2026-09-01 11:24:56 +08:00
committed by iAstro
parent f06a514bd2
commit a4fed136e3
23 changed files with 645 additions and 60 deletions
@@ -13,7 +13,7 @@ function fakeAgentWindow(): AgentWindowApi & {
return id;
});
const removeMock = vi.fn(async (_id: number) => {});
const ensureActiveTabMock = vi.fn(async (_windowId: number, _url: string) => {});
const ensureActiveTabMock = vi.fn(async (_windowId: number, _url: string) => 0);
return {
create: createMock,
remove: removeMock,
@@ -151,6 +151,55 @@ describe("SessionManager", () => {
expect(sm.list()).toEqual([]);
});
describe("classifyNewTab (user vs agent tab freedom)", () => {
it("classifies a tab born during window init as agent (home tab)", async () => {
const aw = fakeAgentWindow();
const sm = new SessionManager({ agentWindow: aw });
const ctx = await sm.start("aa11");
// Simulate the onCreated event for the home tab while still initializing.
// (start() already clears the flag, so re-add to emulate the race.)
(sm as unknown as { windowInitializing: Set<number> }).windowInitializing.add(
ctx.agentWindowId,
);
const kind = sm.classifyNewTab(0, ctx.agentWindowId);
expect(kind).toBe("initializing");
expect(ctx.agentCreatedTabs.has(0)).toBe(true);
expect(ctx.userTabs.has(0)).toBe(false);
});
it("classifies a pending tab_create tab as agent", async () => {
const sm = new SessionManager({ agentWindow: fakeAgentWindow() });
const ctx = await sm.start("aa11");
sm.markAgentTabPending(ctx.agentWindowId);
const kind = sm.classifyNewTab(11, ctx.agentWindowId);
expect(kind).toBe("agent");
expect(ctx.agentCreatedTabs.has(11)).toBe(true);
expect(ctx.userTabs.has(11)).toBe(false);
expect(ctx.pendingAgentTabCount).toBe(0);
});
it("classifies a user-opened tab (no pending) as user and keeps it free", async () => {
const sm = new SessionManager({ agentWindow: fakeAgentWindow() });
const ctx = await sm.start("aa11");
const kind = sm.classifyNewTab(99, ctx.agentWindowId);
expect(kind).toBe("user");
expect(ctx.userTabs.has(99)).toBe(true);
expect(ctx.agentCreatedTabs.has(99)).toBe(false);
});
it("matches multiple pending agent tabs to multiple onCreated events", async () => {
const sm = new SessionManager({ agentWindow: fakeAgentWindow() });
const ctx = await sm.start("aa11");
sm.markAgentTabPending(ctx.agentWindowId);
sm.markAgentTabPending(ctx.agentWindowId);
expect(sm.classifyNewTab(11, ctx.agentWindowId)).toBe("agent");
// Second agent tab still pending → agent; not mistaken for user.
expect(sm.classifyNewTab(12, ctx.agentWindowId)).toBe("agent");
// No more pending → a later user tab is user.
expect(sm.classifyNewTab(99, ctx.agentWindowId)).toBe("user");
});
});
describe("findBorrowingSession", () => {
it("returns null when no session has borrowed the tab", async () => {
const sm = new SessionManager({ agentWindow: fakeAgentWindow() });