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
4 changes: 2 additions & 2 deletions src/lib/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3837,12 +3837,12 @@ async function selectPolicyTier(): Promise<string> {
async function selectTierPresetsAndAccess(
tierName: string,
allPresets: Array<{ name: string; description?: string }>,
extraSelected: string[] = [],
initialSelected?: string[],
): Promise<Array<{ name: string; access: string }>> {
return getPolicySelectionPromptHelpers().selectTierPresetsAndAccess(
tierName,
allPresets,
extraSelected,
initialSelected,
);
}

Expand Down
13 changes: 13 additions & 0 deletions src/lib/onboard/policy-selection-prompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ describe("createPolicySelectionPromptHelpers", () => {
expect(stdin.listenerCount("data")).toBe(0);
});

it("selectTierPresetsAndAccess honors an exact initial selection", async () => {
const { helpers, stdin } = createHarness();
const result = helpers.selectTierPresetsAndAccess(
"balanced",
[{ name: "npm" }, { name: "pypi" }, { name: "github" }],
["npm"],
);

stdin.emit("data", "\r");

await expect(result).resolves.toEqual([{ name: "npm", access: "read" }]);
});

it("selectPolicyTier marks rollback and restores raw mode on SIGTERM", () => {
const { helpers, markCancelled, processEvents, stdin } = createHarness();

Expand Down
22 changes: 13 additions & 9 deletions src/lib/onboard/policy-selection-prompts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import type { SandboxCancelRollback } from "./cancel-rollback";
import type { TierDefinition } from "../policy/tiers";
import type { SandboxCancelRollback } from "./cancel-rollback";

type PresetWithDescription = { name: string; description?: string };
type PresetWithAccess = { name: string; access: string };
Expand Down Expand Up @@ -54,7 +54,7 @@ export function createPolicySelectionPromptHelpers(deps: PolicySelectionPromptDe
selectTierPresetsAndAccess(
tierName: string,
allPresets: PresetWithDescription[],
extraSelected?: string[],
initialSelected?: string[],
): Promise<PresetWithAccess[]>;
presetsCheckboxSelector(
allPresets: Array<{ name: string; description: string }>,
Expand Down Expand Up @@ -207,12 +207,13 @@ export function createPolicySelectionPromptHelpers(deps: PolicySelectionPromptDe
* per-preset access (read vs read-write).
*
* Tier presets are listed first (in tier order), then remaining presets
* alphabetically. Tier presets are pre-checked; others start unchecked.
* alphabetically. Callers can supply the exact initial checked set after
* filtering tier defaults for the selected agent and integrations.
*/
async function selectTierPresetsAndAccess(
tierName: string,
allPresets: PresetWithDescription[],
extraSelected: string[] = [],
initialSelected?: string[],
): Promise<PresetWithAccess[]> {
const tierDef = tiers.getTier(tierName);
const tierPresetMap: Record<string, string> = {};
Expand All @@ -232,11 +233,14 @@ export function createPolicySelectionPromptHelpers(deps: PolicySelectionPromptDe
...allPresets.filter((preset) => !tierSet.has(preset.name)),
];

// Initial inclusion: tier presets + any already-applied extras.
const included = new Set([
...tierNames,
...extraSelected.filter((name) => ordered.find((preset) => preset.name === name)),
]);
// When the caller has already reconciled tier defaults with the selected
// agent and integrations, preserve that exact initial choice. Direct
// callers that omit it retain the tier defaults.
const included = new Set(
(initialSelected ?? tierNames).filter((name) =>
ordered.some((preset) => preset.name === name),
),
);

// Access levels: tier defaults for tier presets, read-write default for others.
const accessModes: Record<string, string> = {};
Expand Down
6 changes: 3 additions & 3 deletions src/lib/onboard/policy-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export type SetupPolicySelectionDeps = {
selectTierPresetsAndAccess: (
tierName: string,
presets: Preset[],
extraSelected: string[],
initialSelected: string[],
) => Promise<Array<Preset & { access: string }>>;
parsePolicyPresetEnv: (raw: string) => string[];
env?: NodeJS.ProcessEnv;
Expand Down Expand Up @@ -460,14 +460,14 @@ async function setupPoliciesWithSelectionInner(
}

const knownNames = new Set(allPresets.map((preset) => preset.name));
const extraSelected = [
const initialSelected = [
...appliedForPreservation.filter((name) => knownNames.has(name)),
...suggestions.filter((name) => knownNames.has(name) && !applied.includes(name)),
];
const resolvedPresets = await deps.selectTierPresetsAndAccess(
tierName,
allPresets,
extraSelected,
initialSelected,
);
const interactiveChoice = pruneUnavailablePresets(
mergeRequiredSetupPolicyPresets(
Expand Down
67 changes: 56 additions & 11 deletions test/policy-tiers-onboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type SetupHarnessOptions = {
customPresets?: TestPreset[];
customOwnsObservability?: boolean;
recordedPolicyTier?: string | null;
nonInteractive?: boolean;
env?: NodeJS.ProcessEnv;
};

Expand All @@ -106,6 +107,7 @@ function createSetupHarness({
customPresets = [],
customOwnsObservability = false,
recordedPolicyTier = null,
nonInteractive = true,
env = {},
}: SetupHarnessOptions = {}) {
const notes: string[] = [];
Expand Down Expand Up @@ -142,7 +144,7 @@ function createSetupHarness({
localInferenceProviders: ["ollama-local", "vllm-local"],
step: () => undefined,
note: (message) => notes.push(message),
isNonInteractive: () => true,
isNonInteractive: () => nonInteractive,
waitForSandboxReady: () => true,
syncPresetSelection: (sandboxName, current, selected, accessByName) => {
syncCalls.push({
Expand All @@ -161,9 +163,13 @@ function createSetupHarness({
tierUpdates.push({ sandboxName, policyTier });
},
getRecordedPolicyTier: () => recordedPolicyTier,
selectTierPresetsAndAccess: async (selectedTier, presets, extraSelected) => {
selectTierPresetsAndAccess: async (selectedTier, presets, initialSelected) => {
const promptHarness = createPromptHarness();
return promptHarness.helpers.selectTierPresetsAndAccess(selectedTier, presets, extraSelected);
return promptHarness.helpers.selectTierPresetsAndAccess(
selectedTier,
presets,
initialSelected,
);
},
parsePolicyPresetEnv,
env: {
Expand Down Expand Up @@ -469,6 +475,46 @@ describe("policy tier setup", () => {
assert.ok(!result.appliedCalls.includes("brave"));
});

it.each([
["OpenClaw", "openclaw", "no web search", null, []],
[
"OpenClaw",
"openclaw",
"Brave Search",
{ fetchEnabled: true, provider: "brave" as const },
["brave"],
],
[
"OpenClaw",
"openclaw",
"Tavily Search",
{ fetchEnabled: true, provider: "tavily" as const },
["tavily"],
],
["Hermes", "hermes", "no web search", null, []],
[
"Hermes",
"hermes",
"Tavily Search",
{ fetchEnabled: true, provider: "tavily" as const },
["tavily"],
],
])("preselects only the matching web-search preset for fresh interactive %s onboarding with %s (#7125)", async (_agentLabel, agent, _searchLabel, webSearchConfig, expectedSearchPresets) => {
const result = await runPolicySetup(
{ tierName: "balanced", nonInteractive: false },
{
agent,
webSearchConfig,
webSearchSupported: true,
},
);

assert.deepEqual(
result.applied.filter((name) => name === "brave" || name === "tavily"),
expectedSearchPresets,
);
});

it("keeps explicitly requested built-in Brave when web search is supported", async () => {
const result = await runPolicySetup(
{
Expand Down Expand Up @@ -797,10 +843,10 @@ describe("policy tier setup", () => {
describe("selectTierPresetsAndAccess", () => {
async function resolve(
tierName: string,
extraSelected: string[] = [],
initialSelected?: string[],
): Promise<Array<{ name: string; access: string }>> {
const { helpers } = createPromptHarness();
return helpers.selectTierPresetsAndAccess(tierName, policy.listPresets(), extraSelected);
return helpers.selectTierPresetsAndAccess(tierName, policy.listPresets(), initialSelected);
}

it("returns tier presets with their default access levels", async () => {
Expand All @@ -819,20 +865,19 @@ describe("selectTierPresetsAndAccess", () => {
assert.deepEqual(await resolve("restricted"), []);
});

it("adds a non-tier preset to the initial checked set through extraSelected", async () => {
const names = (await resolve("balanced", ["slack"])).map((preset) => preset.name);
assert.ok(names.includes("slack"), "slack should be included via extraSelected");
assert.ok(names.includes("npm"), "npm (tier default) should still be included");
it("uses an explicit initial checked set when provided", async () => {
const names = (await resolve("balanced", ["npm", "slack"])).map((preset) => preset.name);
assert.deepEqual(names, ["npm", "slack"]);
});

it("silently filters an invalid extraSelected preset name", async () => {
it("silently filters an invalid initial preset name", async () => {
const names = (await resolve("balanced", ["nonexistent-preset"])).map((preset) => preset.name);
assert.ok(!names.includes("nonexistent-preset"), "invalid preset should be dropped");
});

it("returns tier presets before non-tier presets", async () => {
const names = (await resolve("balanced", ["slack"])).map((preset) => preset.name);
const tierNames = ["npm", "pypi", "huggingface", "brew", "brave"];
const names = (await resolve("balanced", [...tierNames, "slack"])).map((preset) => preset.name);
const lastTierIdx = Math.max(...tierNames.map((name) => names.indexOf(name)));
const slackIdx = names.indexOf("slack");
assert.ok(slackIdx > lastTierIdx, "non-tier preset (slack) should appear after tier presets");
Expand Down
Loading