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: 5 additions & 1 deletion src/lib/actions/sandbox/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import * as agentRuntime from "../../agent/runtime";
import { RD as _RD, B, D, G, R, YW } from "../../cli/terminal-style";
import { getSandboxDeleteOutcome } from "../../domain/sandbox/destroy";
import * as nim from "../../inference/nim";
import { pruneDisabledMessagingPolicyPresets } from "../../onboard/messaging-policy-presets";
import {
captureSandboxListWithGatewayRecovery,
printSandboxListFailureWithRecoveryContext,
Expand Down Expand Up @@ -760,7 +761,10 @@ export async function rebuildSandbox(
// Policy presets live in the gateway policy engine, not the sandbox filesystem.
// They are lost when the sandbox is destroyed and recreated. Re-apply any
// presets that were captured in the backup manifest.
const savedPresets = backupManifest.policyPresets || [];
const savedPresets = pruneDisabledMessagingPolicyPresets(
backupManifest.policyPresets || [],
rebuildDisabledChannels,
);
if (savedPresets.length > 0) {
console.log("");
console.log(" Restoring policy presets...");
Expand Down
74 changes: 34 additions & 40 deletions src/lib/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ import {
setupHermesToolGateways,
stringSetsEqual,
} from "./onboard/hermes-managed-tools";
import { mergePolicyMessagingChannels } from "./onboard/messaging-policy-presets";
import {
filterEnabledChannelsByAgent,
getAvailableMessagingChannelsForAgent,
Expand All @@ -408,7 +409,7 @@ import { decidePolicyCarryForward } from "./onboard/policy-carryforward";
import { getSuggestedPolicyPresets } from "./onboard/policy-presets";
import {
computeSetupPresetSuggestions as computeSetupPresetSuggestionsImpl,
isStaleBuiltinBravePolicyPreset,
preparePolicyPresetResumeSelection,
type SetupPolicySelectionOptions,
type SetupPresetSuggestionOptions,
setupPoliciesWithSelection as setupPoliciesWithSelectionImpl,
Expand Down Expand Up @@ -9566,7 +9567,14 @@ async function onboard(opts: OnboardOptions = {}): Promise<void> {
const recordedMessagingChannels = Array.isArray(latestSession?.messagingChannels)
? latestSession.messagingChannels
: [];
const activeMessagingChannels = registry.getSandbox(sandboxName)?.messagingChannels;
const activeSandbox = registry.getSandbox(sandboxName);
const activeMessagingChannels = activeSandbox?.messagingChannels;
const policyMessagingChannels = mergePolicyMessagingChannels(
selectedMessagingChannels,
recordedMessagingChannels,
activeMessagingChannels,
activeSandbox?.disabledChannels,
);
verifyCompatibleEndpointSandboxSmoke({
sandboxName,
provider,
Expand All @@ -9575,44 +9583,27 @@ async function onboard(opts: OnboardOptions = {}): Promise<void> {
redact,
endpointUrl,
credentialEnv,
messagingChannels: Array.isArray(activeMessagingChannels) ? activeMessagingChannels : [],
messagingChannels: policyMessagingChannels,
agent,
});
const policyPresetSupportOptions = { webSearchSupported };
const selectablePolicyPresetsForSupport = [
...policies.listSetupPolicyPresets(sandboxName, policyPresetSupportOptions),
...policies.getAppliedPresets(sandboxName).map((name) => ({ name })),
];
const customPolicyPresetNames = new Set(
policies.listCustomPresets(sandboxName).map((p: { name: string }) => p.name),
);
let recordedPolicyPresetsForSupport = policies.clampSetupPolicyPresetNames(
recordedPolicyPresets || [],
selectablePolicyPresetsForSupport,
policyPresetSupportOptions,
customPolicyPresetNames,
);
recordedPolicyPresetsForSupport = recordedPolicyPresetsForSupport.filter(
(name) =>
!isStaleBuiltinBravePolicyPreset(name, {
webSearchConfig,
customPresetNames: customPolicyPresetNames,
}),
);
if (recordedPolicyPresets) {
recordedPolicyPresetsForSupport = mergeRequiredHermesToolGatewayPolicyPresets(
recordedPolicyPresetsForSupport,
const policyResumeSelection = preparePolicyPresetResumeSelection(
{ policies },
sandboxName,
{
recordedPolicyPresets,
disabledChannels: activeSandbox?.disabledChannels,
enabledChannels: policyMessagingChannels,
hermesToolGateways,
selectablePolicyPresetsForSupport.map((p) => p.name),
);
}
const recordedPolicyPresetsHaveUnsupported =
Array.isArray(recordedPolicyPresets) &&
recordedPolicyPresetsForSupport.length !== recordedPolicyPresets.length;
webSearchConfig,
webSearchSupported,
},
);
const recordedPolicyPresetsForSupport = policyResumeSelection.policyPresets;
const resumePolicies =
resume &&
sandboxName &&
!recordedPolicyPresetsHaveUnsupported &&
!policyResumeSelection.recordedPolicyPresetsNeedReconcile &&
!policyResumeSelection.disabledMessagingPolicyPresetApplied &&
arePolicyPresetsApplied(sandboxName, recordedPolicyPresetsForSupport);
if (resumePolicies) {
skippedStepMessage("policies", recordedPolicyPresetsForSupport.join(", "));
Expand All @@ -9632,15 +9623,13 @@ async function onboard(opts: OnboardOptions = {}): Promise<void> {
model,
policyPresets: recordedPolicyPresetsForSupport,
});
const appliedPolicyPresets = await setupPoliciesWithSelection(sandboxName, {
const setupAppliedPolicyPresets = await setupPoliciesWithSelection(sandboxName, {
selectedPresets:
Array.isArray(recordedPolicyPresets)
? recordedPolicyPresetsForSupport
: null,
enabledChannels:
selectedMessagingChannels.length > 0
? selectedMessagingChannels
: recordedMessagingChannels,
enabledChannels: policyMessagingChannels,
disabledChannels: activeSandbox?.disabledChannels,
webSearchConfig,
provider,
webSearchSupported,
Expand All @@ -9654,7 +9643,12 @@ async function onboard(opts: OnboardOptions = {}): Promise<void> {
});
await recordStepComplete(
"policies",
toSessionUpdates({ sandboxName, provider, model, policyPresets: appliedPolicyPresets }),
toSessionUpdates({
sandboxName,
provider,
model,
policyPresets: setupAppliedPolicyPresets,
}),
);
}

Expand Down
9 changes: 2 additions & 7 deletions src/lib/onboard/initial-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import path from "node:path";
import YAML from "yaml";

import * as policies from "../policy";
import { requiredMessagingChannelPolicyPresets } from "./messaging-policy-presets";
import { cleanupTempDir, secureTempFile } from "./temp-files";

export type InitialSandboxPolicy = {
Expand All @@ -14,10 +15,6 @@ export type InitialSandboxPolicy = {
cleanup?: () => boolean;
};

const CREATE_TIME_POLICY_PRESETS_BY_CHANNEL: Record<string, string[]> = {
slack: ["slack"],
};

const HERMES_MESSAGING_POLICY_KEYS: Record<string, string[]> = {
discord: ["discord"],
slack: ["slack"],
Expand Down Expand Up @@ -229,9 +226,7 @@ export function prepareInitialSandboxCreatePolicy(
const requestedCreateTimePresets = [
...new Set(
[
...activeMessagingChannels.flatMap(
(channel) => CREATE_TIME_POLICY_PRESETS_BY_CHANNEL[channel] || [],
),
...requiredMessagingChannelPolicyPresets(activeMessagingChannels),
...(options.additionalPresets || []),
],
),
Expand Down
80 changes: 80 additions & 0 deletions src/lib/onboard/messaging-policy-presets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, expect, it } from "vitest";

import {
hasDisabledMessagingPolicyPreset,
mergeAppliedPolicyPresetsForDisabledMessagingCleanup,
mergePolicyMessagingChannels,
mergeRequiredMessagingChannelPolicyPresets,
pruneDisabledMessagingPolicyPresets,
requiredMessagingChannelPolicyPresets,
} from "./messaging-policy-presets";

describe("messaging policy presets", () => {
it("maps Slack messaging to the Slack network policy preset", () => {
expect(requiredMessagingChannelPolicyPresets(["slack"])).toEqual(["slack"]);
expect(requiredMessagingChannelPolicyPresets([" Slack "])).toEqual(["slack"]);
});

it("merges required messaging presets into an existing selection", () => {
expect(mergeRequiredMessagingChannelPolicyPresets(["npm", "pypi"], ["slack"])).toEqual([
"npm",
"pypi",
"slack",
]);
});

it("does not add a required preset that is not available to the sandbox", () => {
expect(
mergeRequiredMessagingChannelPolicyPresets(["npm"], ["slack"], new Set(["npm"])),
).toEqual(["npm"]);
});

it("merges policy channels while excluding disabled channels", () => {
expect(
mergePolicyMessagingChannels(
["slack", "telegram"],
[" Slack "],
["discord", "slack"],
["slack"],
),
).toEqual(["telegram", "discord"]);
});

it("removes policy presets for disabled messaging channels", () => {
expect(pruneDisabledMessagingPolicyPresets(["npm", "slack", "pypi"], [" Slack "])).toEqual([
"npm",
"pypi",
]);
});

it("preserves non-required policy presets when a same-named channel is disabled", () => {
expect(
pruneDisabledMessagingPolicyPresets(["telegram", "npm", "pypi"], ["telegram"]),
).toEqual(["telegram", "npm", "pypi"]);
});

it("detects applied policy presets for disabled messaging channels", () => {
expect(hasDisabledMessagingPolicyPreset(["npm", "slack", "pypi"], ["slack"])).toBe(true);
expect(hasDisabledMessagingPolicyPreset(["telegram", "npm"], ["telegram"])).toBe(false);
});

it("preserves unrelated applied presets when cleaning disabled messaging presets", () => {
expect(
mergeAppliedPolicyPresetsForDisabledMessagingCleanup(
["npm"],
["npm", "github", "slack"],
["slack"],
),
).toEqual(["npm", "github"]);
expect(
mergeAppliedPolicyPresetsForDisabledMessagingCleanup(
["npm"],
["npm", "github"],
["slack"],
),
).toEqual(["npm"]);
});
});
105 changes: 105 additions & 0 deletions src/lib/onboard/messaging-policy-presets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

const REQUIRED_POLICY_PRESETS_BY_MESSAGING_CHANNEL: Record<string, readonly string[]> = {
slack: ["slack"],
};

function normalizedNames(values: string[] | null | undefined): string[] {
if (!Array.isArray(values)) return [];
const names: string[] = [];
for (const value of values) {
if (typeof value !== "string") continue;
const name = value.trim().toLowerCase();
if (!name || names.includes(name)) continue;
names.push(name);
}
return names;
}

export function mergePolicyMessagingChannels(
selectedChannels: string[] | null | undefined,
recordedChannels: string[] | null | undefined,
activeChannels: string[] | null | undefined,
disabledChannels: string[] | null | undefined = null,
): string[] {
const disabled = new Set(normalizedNames(disabledChannels));
const merged: string[] = [];
for (const channels of [selectedChannels, recordedChannels, activeChannels]) {
for (const channel of normalizedNames(channels)) {
if (!channel || disabled.has(channel) || merged.includes(channel)) continue;
merged.push(channel);
}
}
return merged;
}

export function requiredMessagingChannelPolicyPresets(
channels: string[] | null | undefined,
): string[] {
const required: string[] = [];
for (const channel of normalizedNames(channels)) {
for (const preset of REQUIRED_POLICY_PRESETS_BY_MESSAGING_CHANNEL[channel] || []) {
if (!required.includes(preset)) required.push(preset);
}
}
return required;
}

export function mergeRequiredMessagingChannelPolicyPresets(
selectedPresets: string[],
channels: string[] | null | undefined,
knownPresetNames?: Iterable<string> | null,
): string[] {
const merged = [...selectedPresets];
const selected = new Set(merged);
const known = knownPresetNames ? new Set(knownPresetNames) : null;

for (const preset of requiredMessagingChannelPolicyPresets(channels)) {
if (known && !known.has(preset)) continue;
if (selected.has(preset)) continue;
merged.push(preset);
selected.add(preset);
}

return merged;
}

export function pruneDisabledMessagingPolicyPresets(
selectedPresets: string[],
disabledChannels: string[] | null | undefined,
): string[] {
const disabledRequiredPresets = new Set(
requiredMessagingChannelPolicyPresets(disabledChannels),
);
if (disabledRequiredPresets.size === 0) return selectedPresets;
return selectedPresets.filter(
(preset) => !disabledRequiredPresets.has(preset.trim().toLowerCase()),
);
}

export function hasDisabledMessagingPolicyPreset(
selectedPresets: string[],
disabledChannels: string[] | null | undefined,
): boolean {
return (
pruneDisabledMessagingPolicyPresets(selectedPresets, disabledChannels).length !==
selectedPresets.length
);
}

export function mergeAppliedPolicyPresetsForDisabledMessagingCleanup(
selectedPresets: string[],
appliedPresets: string[],
disabledChannels: string[] | null | undefined,
): string[] {
if (!hasDisabledMessagingPolicyPreset(appliedPresets, disabledChannels)) {
return selectedPresets;
}

const merged = [...selectedPresets];
for (const preset of pruneDisabledMessagingPolicyPresets(appliedPresets, disabledChannels)) {
if (!merged.includes(preset)) merged.push(preset);
}
return merged;
}
Loading
Loading