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
51 changes: 49 additions & 2 deletions packages/core/src/scheduler/policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ describe('policy.ts', () => {
});

describe('updatePolicy', () => {
it('should set AUTO_EDIT mode for auto-edit transition tools', async () => {
it('should set AUTO_EDIT mode for auto-edit transition tools and publish policy update', async () => {
const mockConfig = {
getApprovalMode: vi.fn().mockReturnValue(ApprovalMode.DEFAULT),
setApprovalMode: vi.fn(),
Expand All @@ -266,7 +266,54 @@ describe('policy.ts', () => {
expect(mockConfig.setApprovalMode).toHaveBeenCalledWith(
ApprovalMode.AUTO_EDIT,
);
expect(mockMessageBus.publish).not.toHaveBeenCalled();
expect(mockMessageBus.publish).toHaveBeenCalledWith(
expect.objectContaining({
type: MessageBusType.UPDATE_POLICY,
toolName: 'replace',
persist: false,
}),
);
});

it('should preserve the original mode set when a session allow triggers AUTO_EDIT', async () => {
let currentMode = ApprovalMode.DEFAULT;
const mockConfig = {
getApprovalMode: vi.fn(() => currentMode),
setApprovalMode: vi.fn((mode: ApprovalMode) => {
currentMode = mode;
}),
getSessionId: vi.fn().mockReturnValue('test-session-id'),
} as unknown as Mocked<Config>;
(mockConfig as unknown as { config: Config }).config =
mockConfig as Config;
const mockMessageBus = {
publish: vi.fn(),
} as unknown as Mocked<MessageBus>;
const tool = { name: 'replace' } as AnyDeclarativeTool;

await updatePolicy(
tool,
ToolConfirmationOutcome.ProceedAlways,
undefined,
mockConfig,
mockMessageBus,
);

expect(mockConfig.setApprovalMode).toHaveBeenCalledWith(
ApprovalMode.AUTO_EDIT,
);
expect(mockMessageBus.publish).toHaveBeenCalledWith(
expect.objectContaining({
type: MessageBusType.UPDATE_POLICY,
toolName: 'replace',
persist: false,
modes: [
ApprovalMode.DEFAULT,
ApprovalMode.AUTO_EDIT,
ApprovalMode.YOLO,
],
}),
);
});

it('should handle standard policy updates (persist=false)', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/scheduler/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ export async function updatePolicy(
messageBus: MessageBus,
toolInvocation?: AnyToolInvocation,
): Promise<void> {
const currentMode = context.config.getApprovalMode();

// Mode Transitions (AUTO_EDIT)
if (isAutoEditTransition(tool, outcome)) {
context.config.setApprovalMode(ApprovalMode.AUTO_EDIT);
return;
}
Comment thread
ahsanfarooq210 marked this conversation as resolved.

// Determine persist scope if we are persisting.
let persistScope: 'workspace' | 'user' | undefined;
let modes: ApprovalMode[] | undefined;
const currentMode = context.config.getApprovalMode();

// If this is an 'Always Allow' selection, we restrict it to the current mode
// and more permissive modes.
Expand Down
Loading