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
53 changes: 47 additions & 6 deletions src/lib/actions/sandbox/policy-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,13 +681,12 @@ async function planSandboxChannelAdd(
hydrateAddChannelEnvFromSession(sandboxName, channelId);

try {
const plan = await planner.buildPlan({
const plan = await planner.buildChannelAddPlanFromSandboxEntry({
sandboxName,
agent: toMessagingAgentId(agent),
workflow: "add-channel",
isInteractive: !isNonInteractive(),
configuredChannels: [channelId],
disabledChannels: [],
channelId,
sandboxEntry: registry.getSandbox(sandboxName),
supportedChannelIds,
credentialAvailability: buildCredentialAvailability([channelId]),
});
Expand All @@ -700,6 +699,46 @@ async function planSandboxChannelAdd(
}
}

async function persistManifestChannelDisabledPlan(
sandboxName: string,
channelId: string,
disabled: boolean,
): Promise<void> {
const entry = registry.getSandbox(sandboxName);
if (!entry) return;
const agent = resolveAgentForSandbox(sandboxName);
const planner = new MessagingWorkflowPlanner(messagingManifestRegistry);
const context = {
sandboxName,
agent: toMessagingAgentId(agent),
channelId,
sandboxEntry: entry,
supportedChannelIds: availableManifestChannelsForAgent(agent).map((manifest) => manifest.id),
};
const plan = disabled
? await planner.buildChannelStopPlanFromSandboxEntry(context)
: await planner.buildChannelStartPlanFromSandboxEntry(context);
if (plan) MessagingHostStateApplier.applyPlanToRegistry(sandboxName, plan);
}

async function persistManifestChannelRemovePlan(
sandboxName: string,
channelId: string,
): Promise<void> {
const entry = registry.getSandbox(sandboxName);
if (!entry) return;
const agent = resolveAgentForSandbox(sandboxName);
const planner = new MessagingWorkflowPlanner(messagingManifestRegistry);
const plan = await planner.buildChannelRemovePlanFromSandboxEntry({
sandboxName,
agent: toMessagingAgentId(agent),
channelId,
sandboxEntry: entry,
supportedChannelIds: availableManifestChannelsForAgent(agent).map((manifest) => manifest.id),
});
if (plan) MessagingHostStateApplier.applyPlanToRegistry(sandboxName, plan);
}

function buildCredentialAvailability(channelIds: readonly string[]): Record<string, boolean> {
const availability: Record<string, boolean> = {};
for (const channelId of channelIds) {
Expand Down Expand Up @@ -923,7 +962,7 @@ export async function addSandboxChannel(
}
await applyChannelAddToGatewayAndRegistry(sandboxName, canonical, {});
persistManifestAddState(sandboxName, manifest);
MessagingHostStateApplier.applyPlanToRegistry(sandboxName, plan, { mode: "merge" });
MessagingHostStateApplier.applyPlanToRegistry(sandboxName, plan);
console.log("");
const help = manifest.enrollmentHelp ?? manifest.inputs[0]?.prompt?.help;
if (help) console.log(` ${help}`);
Expand Down Expand Up @@ -982,7 +1021,7 @@ export async function addSandboxChannel(
}

persistManifestAddState(sandboxName, manifest);
MessagingHostStateApplier.applyPlanToRegistry(sandboxName, plan, { mode: "merge" });
MessagingHostStateApplier.applyPlanToRegistry(sandboxName, plan);

const rebuilt = await promptAndRebuild(sandboxName, `add '${canonical}'`);
if (rebuilt) verifyChannelBridgeAfterRebuild(sandboxName, canonical);
Expand Down Expand Up @@ -1290,6 +1329,7 @@ export async function removeSandboxChannel(
}

removeChannelPresetIfPresent(sandboxName, canonical);
await persistManifestChannelRemovePlan(sandboxName, canonical);

// Token-based channels: best-effort tidy of any leftover dir. Token
// revocation already prevents the bot from authenticating, so a
Expand Down Expand Up @@ -1345,6 +1385,7 @@ async function sandboxChannelsSetEnabled(
console.error(` Sandbox '${sandboxName}' not found in the registry.`);
process.exit(1);
}
await persistManifestChannelDisabledPlan(sandboxName, normalized, disabled);
const state = disabled ? "disabled" : "enabled";
console.log(` ${G}✓${R} Marked ${normalized} ${state} for '${sandboxName}'.`);
await promptAndRebuild(sandboxName, `${verb} '${normalized}'`);
Expand Down
48 changes: 48 additions & 0 deletions src/lib/actions/sandbox/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ 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 {
createBuiltInChannelManifestRegistry,
MessagingSetupApplier,
MessagingWorkflowPlanner,
toMessagingAgentId,
} from "../../messaging";
import { pruneDisabledMessagingPolicyPresets } from "../../onboard/messaging-policy-presets";
import {
captureSandboxListWithGatewayRecovery,
Expand Down Expand Up @@ -162,6 +168,33 @@ function preflightHermesProviderCredentials(
return false;
}

async function stageMessagingManifestPlanForRebuild(
sandboxName: string,
sandboxEntry: registry.SandboxEntry,
rebuildAgent: string | null,
log: (msg: string) => void,
): Promise<void> {
const agent = loadAgent(rebuildAgent || "openclaw");
const planner = new MessagingWorkflowPlanner(createBuiltInChannelManifestRegistry());
const plan = await planner.buildRebuildPlanFromSandboxEntry({
sandboxName,
agent: toMessagingAgentId(agent),
sandboxEntry,
supportedChannelIds: agent.messagingPlatforms,
});
if (!plan || plan.channels.length === 0) {
MessagingSetupApplier.clearPlanEnv();
log("Messaging manifest rebuild plan: no configured channels");
return;
}
MessagingSetupApplier.writePlanToEnv(plan);
log(
`Messaging manifest rebuild plan staged: ${plan.channels
.map((channel) => channel.channelId)
.join(",")}`,
);
}

/**
* Rebuild a live sandbox while preserving registered agent state and policies.
*
Expand Down Expand Up @@ -392,6 +425,21 @@ export async function rebuildSandbox(
);
}

try {
await stageMessagingManifestPlanForRebuild(sandboxName, sb, rebuildAgent, log);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.error("");
console.error(
` ${_RD}Rebuild preflight failed:${R} messaging manifest plan could not be staged.`,
);
console.error(` ${message}`);
console.error("");
console.error(" Sandbox is untouched — no data was lost.");
bail(message);
return;
}

// Step 1: Ensure sandbox is live for backup
log("Checking sandbox liveness: openshell sandbox list");
const liveRecovery = await captureSandboxListWithGatewayRecovery();
Expand Down
Loading
Loading