Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,9 @@ async function ensureEventSubscription(directory: string): Promise<void> {
}

const currentSession = getCurrentSession();
if (!currentSession || currentSession.id !== request.sessionID) {
const isCurrent = currentSession?.id === request.sessionID;
const isSubagent = summaryAggregator.isSubagentSession(request.sessionID);
if (!currentSession || (!isCurrent && !isSubagent)) {
return;
}

Expand All @@ -766,7 +768,7 @@ async function ensureEventSubscription(directory: string): Promise<void> {
]);

logger.info(
`[Bot] Received permission request from agent: type=${request.permission}, requestID=${request.id}`,
`[Bot] Received permission request from agent: type=${request.permission}, requestID=${request.id}, subagent=${isSubagent}`,
);
await showPermissionRequest(botInstance.api, chatIdInstance, request);
});
Expand Down
14 changes: 12 additions & 2 deletions src/summary/aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@ class SummaryAggregator {
return this.trackedSessionParents.has(sessionId) && sessionId !== this.currentSessionId;
}

/**
* Public check: is this session a tracked subagent (child) of the current root session?
*/
isSubagentSession(sessionId: string): boolean {
return this.isTrackedChildSession(sessionId);
}

private getQueue(map: Map<string, string[]>, parentSessionId: string): string[] {
const existing = map.get(parentSessionId);
if (existing) {
Expand Down Expand Up @@ -1833,15 +1840,18 @@ class SummaryAggregator {
): void {
const request = event.properties;

if (request.sessionID !== this.currentSessionId) {
const isCurrent = request.sessionID === this.currentSessionId;
const isTrackedChild = this.isTrackedChildSession(request.sessionID);

if (!isCurrent && !isTrackedChild) {
logger.debug(
`[Aggregator] Ignoring permission.asked for different session: ${request.sessionID} (current: ${this.currentSessionId})`,
);
return;
}

logger.info(
`[Aggregator] Permission asked: requestID=${request.id}, type=${request.permission}, patterns=${request.patterns.length}`,
`[Aggregator] Permission asked: requestID=${request.id}, type=${request.permission}, patterns=${request.patterns.length}, subagent=${isTrackedChild}`,
);

if (this.onPermissionCallback) {
Expand Down
86 changes: 86 additions & 0 deletions tests/summary/aggregator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1520,4 +1520,90 @@ describe("summary/aggregator", () => {

expect(onTokens).not.toHaveBeenCalled();
});

it("forwards permission.asked events from tracked subagent (child) sessions", async () => {
const onPermission = vi.fn();
summaryAggregator.setOnPermission(onPermission);
summaryAggregator.setSession("root-session");

// Register a tracked child session via the task tool flow.
summaryAggregator.processEvent({
type: "message.part.updated",
properties: {
part: {
id: "subtask-1",
sessionID: "root-session",
messageID: "root-message",
type: "subtask",
prompt: "Edit file",
description: "Edit file",
agent: "general",
},
},
} as unknown as Event);

summaryAggregator.processEvent({
type: "session.created",
properties: {
info: {
id: "child-session-1",
parentID: "root-session",
title: "Edit file (@general subagent)",
slug: "child",
directory: "D:/repo",
projectID: "p1",
version: "1",
time: { created: Date.now(), updated: Date.now() },
},
},
} as unknown as Event);

// permission.asked from the child (subagent) session must reach the callback.
summaryAggregator.processEvent({
type: "permission.asked",
properties: {
id: "req-child-1",
sessionID: "child-session-1",
permission: "write",
patterns: ["src/foo.ts"],
metadata: {},
always: [],
},
} as unknown as Event);

await new Promise((resolve) => setImmediate(resolve));

expect(onPermission).toHaveBeenCalledTimes(1);
expect(onPermission.mock.calls[0][0]).toEqual(
expect.objectContaining({
id: "req-child-1",
sessionID: "child-session-1",
permission: "write",
}),
);
expect(summaryAggregator.isSubagentSession("child-session-1")).toBe(true);
});

it("ignores permission.asked events from unrelated sessions", async () => {
const onPermission = vi.fn();
summaryAggregator.setOnPermission(onPermission);
summaryAggregator.setSession("root-session");

summaryAggregator.processEvent({
type: "permission.asked",
properties: {
id: "req-other",
sessionID: "some-other-session",
permission: "write",
patterns: ["src/foo.ts"],
metadata: {},
always: [],
},
} as unknown as Event);

await new Promise((resolve) => setImmediate(resolve));

expect(onPermission).not.toHaveBeenCalled();
expect(summaryAggregator.isSubagentSession("some-other-session")).toBe(false);
});
});
Loading