Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

41.2 KB
Raw Blame
import type { SandboxMode } from '@lobechat/builtin-tool-cloud-sandbox';
import { LocalSystemApiName, LocalSystemIdentifier } from '@lobechat/builtin-tool-local-system';
import {
  type CommandResult,
  type ExecScriptActivatedSkill,
  SkillsIdentifier,
} from '@lobechat/builtin-tool-skills';
import {
  type ExportFileResult,
  getDirname,
  type SkillRuntimeService,
  SkillsExecutionRuntime,
} from '@lobechat/builtin-tool-skills/executionRuntime';
import { BRANDING_NAME } from '@lobechat/business-const';
import { type SkillItem, type SkillListItem, type SkillResourceContent } from '@lobechat/types';
import { toRecord } from '@lobechat/utils/object';
import debug from 'debug';

import { AgentSkillModel } from '@/database/models/agentSkill';
import { FileModel } from '@/database/models/file';
import { UserModel } from '@/database/models/user';
import type { LobeChatDatabase } from '@/database/type';
import { deviceGateway } from '@/server/services/deviceGateway';
import { executeAuthorizedDeviceToolCall } from '@/server/services/deviceGateway/authorizedToolCall';
import { FileService } from '@/server/services/file';
import type { MarketService } from '@/server/services/market';
import {
  createSandboxService,
  normalizeSandboxCommandResult,
  resolveSandboxSessionConfig,
  type SandboxSessionSpecification,
} from '@/server/services/sandbox';
import type { SandboxInitialization } from '@/server/services/sandbox/environmentInit';
import { SkillResourceService } from '@/server/services/skill/resource';
import { getToolAccessDeniedError } from '@/server/services/toolExecution/errorClassification';
import {
  buildDeviceLhEnv,
  isLhCommand,
  preprocessLhCommand,
} from '@/server/services/toolExecution/preprocessLhCommand';
import { canonicalUserSkillIdentifier } from '@/server/services/userSkill';

import { readSkillUserSettings } from './librarySkillGate';
import { pageSandboxWrapper, type SandboxWrapper } from './pageSandboxSync';
import { resolveContentWorkspaceId } from './resolveWorkspaceScope';
import { createSandboxMarketService } from './sandboxMarket';
import { resolveSkillActivationResources } from './skillActivation';
import { type ServerRuntimeRegistration } from './types';

const log = debug('lobe-server:skills-runtime');

/**
 * Shell runs and file exports have side effects, so a failure must never be
 * replayed: a gateway timeout or dropped response says nothing about whether
 * the sandbox already ran the command, and a non-zero exit proves it did.
 * Without an explicit kind the tool error classifier matches words like
 * "timeout" in the message and the transport re-executes the call β€” a
 * background launch then ran three times. Mirrors ComputerRuntime, where only
 * read-only operations may use the classifier's retry.
 */
const withoutReplay = <T extends { error?: unknown; success: boolean }>(result: T): T =>
  result.success ? result : { ...result, error: { ...toRecord(result.error), kind: 'stop' } };

/**
 * A prepare the gateway gave up on: its `{"error":"TIMEOUT"}` body, the
 * transport's `DEVICE_RESPONSE_TIMEOUT` code (an empty-bodied 504), or our own
 * HTTP deadline when the gateway never answered. Deliberately narrow: a device
 * whose archive download itself failed (e.g. `504 Gateway Timeout` from the
 * CDN) has finished, and must not be told the work is still continuing.
 */
const isPrepareTimeout = (error?: string) =>
  !!error &&
  (/"error"\s*:\s*"TIMEOUT"/.test(error) ||
    error.startsWith('DEVICE_RESPONSE_TIMEOUT') ||
    /aborted due to timeout/i.test(error));

/**
 * Device-execution wiring for the exec APIs, present only when the run's
 * execution plan routed a device (`plan.kind === 'device'` β€” the aiAgent sets
 * `context.activeDeviceId` from exactly that condition). When present,
 * `execScript` runs ON the device instead of the cloud sandbox: skill archives
 * are prepared device-side via the `prepareSkillDirectory` RPC and the command
 * executes through the local-system tool over the device gateway.
 */
interface SkillDeviceExecution {
  deviceId: string;
  executionTimeoutMs?: number;
  /**
   * The run's resolved Local Sandbox decision (`context.localSandbox`), the
   * same value the local-system device-proxy applies to `runCommand`.
   */
  localSandbox?: boolean;
  /** `context.localSandboxNetwork`; only meaningful with {@link localSandbox}. */
  localSandboxNetwork?: boolean;
  operationId?: string;
  /**
   * Filesystem skills already living on the device (project/device SKILL.md).
   * execScript resolves their SKILL.md directory as cwd, mirroring the
   * prepared-archive skills.
   */
  projectSkills?: { location: string; name: string }[];
  /** Lazily resolved workspace principal β€” see `resolveRunWorkspaceId`. */
  resolveWorkspaceId: () => Promise<string | undefined>;
  /** cwd fallback when no activated skill resolves to a directory. */
  workingDirectory?: string;
}

interface ActivatedSkillArchive {
  name: string;
  url: string;
  zipHash: string;
}

/**
 * Sentinel returned by `execScriptOnDevice` when the routed device runs a
 * client build that predates the `prepareSkillDirectory` RPC (shipped with
 * this feature). The device dispatcher replies deterministically with an
 * unknown-method error, so this is a reliable capability probe β€” distinct
 * from network failures/timeouts, which must NOT trigger a fallback.
 */
const LEGACY_DEVICE_CLIENT = Symbol('legacy-device-client');

/**
 * Appended to the sandbox result on a legacy-client fallback so the model
 * discloses the degradation β€” the manifest already told it the command would
 * run on the user's device.
 */
const LEGACY_FALLBACK_NOTE = `Note: the user's device client is outdated and does not support on-device skill execution, so this command ran in the cloud sandbox instead. Tell the user to update their ${BRANDING_NAME} app to run skills on their device.`;

class SkillServerRuntimeService implements SkillRuntimeService {
  private agentId?: string;
  private resourceService: SkillResourceService;
  private skillModel: AgentSkillModel;
  private marketService: MarketService;
  private fileService: FileService;
  private fileModel: FileModel;
  private serverDB: LobeChatDatabase;
  private topicId?: string;
  /**
   * The run this runtime belongs to. Handed to `preprocessLhCommand` so a
   * sandbox `lh agent run` carries `LOBEHUB_OPERATION_ID`, exactly as the device
   * and heterogeneous runtimes do.
   */
  private operationId?: string;
  private userId: string;
  private workspaceId?: string;
  private sandboxCwd?: string;
  private sandboxInitialization?: SandboxInitialization;
  private sandboxWorkingDir?: string;
  private sandboxInstanceId?: string;
  private sandboxMode?: SandboxMode;
  private sandboxSpecification?: SandboxSessionSpecification;
  private device?: SkillDeviceExecution;
  private disabledSkillIds: Set<string>;
  private isLibrarySkillEnabled?: (identifier: string) => boolean;
  private resolveUserSkillArchive?: (
    name: string,
  ) => Promise<{ fileHash: string; url: string } | undefined>;
  private isSkillGranted?: (identifier: string) => boolean;
  private shareVisitorBlocked: boolean;
  private wrapSandbox?: SandboxWrapper;

  constructor(options: {
    agentId?: string;
    device?: SkillDeviceExecution;
    /**
     * Identifiers the agent has explicitly disabled (`agents.plugins` tri-state)
     * β€” findById/findByName resolve to `undefined` for these, so a disabled
     * DB/market skill can't be activated even by a model that already knows
     * its name, independent of whatever's listed in `<available_skills>`.
     */
    disabledSkillIds?: Set<string>;
    fileModel: FileModel;
    fileService: FileService;
    /** Library skills are opt-in per agent; see `resolveAgentSkillGates`. */
    isLibrarySkillEnabled?: (identifier: string) => boolean;
    /**
     * Agent Share only: answers "may this visitor reach the skill with this
     * identifier". Absent on a creator's own run, where the creator reaches
     * their whole catalog by definition.
     */
    isSkillGranted?: (identifier: string) => boolean;
    marketService: MarketService;
    operationId?: string;
    resourceService: SkillResourceService;
    /** The archive of a user skill (`user-skills:<name>`) this run may load. */
    resolveUserSkillArchive?: (
      name: string,
    ) => Promise<{ fileHash: string; url: string } | undefined>;
    /**
     * Persistence for this run, resolved once by the factory. Every sandbox
     * this service creates MUST carry the same values the cloud-sandbox runtime
     * uses: a topic has one sandbox, and a call that disagrees about the mode
     * routes to the other runtime and tears the live one down, taking installed
     * CLIs, injected credentials and anything outside the workspace with it.
     */
    sandboxCwd?: string;
    sandboxInitialization?: SandboxInitialization;
    sandboxInstanceId?: string;
    sandboxMode?: SandboxMode;
    sandboxSpecification?: SandboxSessionSpecification;
    sandboxWorkingDir?: string;
    serverDB: LobeChatDatabase;
    /** Agent Share only: `lh` must not mint a creator-scoped token for a visitor. */
    shareVisitorBlocked?: boolean;
    skillModel: AgentSkillModel;
    topicId?: string;
    userId: string;
    workspaceId?: string;
    wrapSandbox?: SandboxWrapper;
  }) {
    this.agentId = options.agentId;
    this.skillModel = options.skillModel;
    this.resourceService = options.resourceService;
    this.resolveUserSkillArchive = options.resolveUserSkillArchive;
    this.marketService = options.marketService;
    this.fileService = options.fileService;
    this.fileModel = options.fileModel;
    this.serverDB = options.serverDB;
    this.topicId = options.topicId;
    this.operationId = options.operationId;
    this.userId = options.userId;
    this.workspaceId = options.workspaceId;
    this.sandboxCwd = options.sandboxCwd;
    this.sandboxInitialization = options.sandboxInitialization;
    this.sandboxWorkingDir = options.sandboxWorkingDir;
    this.sandboxInstanceId = options.sandboxInstanceId;
    this.sandboxMode = options.sandboxMode;
    this.sandboxSpecification = options.sandboxSpecification;
    this.device = options.device;
    this.disabledSkillIds = options.disabledSkillIds ?? new Set();
    this.isLibrarySkillEnabled = options.isLibrarySkillEnabled;
    this.isSkillGranted = options.isSkillGranted;
    this.shareVisitorBlocked = options.shareVisitorBlocked ?? false;
    this.wrapSandbox = options.wrapSandbox;
  }

  /**
   * The one place that decides whether a resolved DB skill may be opened at
   * all. Both rules are opt-outs from the creator's own catalog, applied
   * together so no lookup path can honor one and miss the other:
   *
   * - `disabledSkillIds` β€” the agent's own tri-state (`agents.plugins`);
   * - `isLibrarySkillEnabled` β€” a library skill the agent never turned on is off;
   * - `isSkillGranted` β€” an Agent Share visitor's per-skill allowlist
   *   (`shareConfig.skillGrants`). Absent for a creator's own run.
   *
   * This runs on RESOLVED rows, not on the caller's argument, because
   * `activateSkill` / `readReference` take a model-supplied NAME: only the row
   * carries the identifier the grant is written against.
   */
  private isSkillReachable = (identifier: string): boolean =>
    !this.disabledSkillIds.has(identifier) &&
    (this.isLibrarySkillEnabled?.(identifier) ?? true) &&
    (this.isSkillGranted?.(identifier) ?? true);

  findAll = async (): Promise<{ data: SkillListItem[]; total: number }> => {
    const result = await this.skillModel.findAll();
    if (!this.isSkillGranted && !this.isLibrarySkillEnabled && this.disabledSkillIds.size === 0) {
      return result;
    }

    // A share visitor must not learn the creator's catalog, and no run should be
    // offered a skill its agent never turned on or explicitly disabled. This list
    // is not just an internal lookup: `activateSkill` echoes it back in its
    // not-found message ("Available skills: ..."), handing every name and
    // description to the model.
    const data = result.data.filter((skill) => this.isSkillReachable(skill.identifier));
    return { data, total: data.length };
  };

  findById = async (id: string): Promise<SkillItem | undefined> => {
    const skill = await this.skillModel.findById(id);
    return skill && this.isSkillReachable(skill.identifier) ? skill : undefined;
  };

  findByName = async (name: string): Promise<SkillItem | undefined> => {
    const skill = await this.skillModel.findByName(name);
    return skill && this.isSkillReachable(skill.identifier) ? skill : undefined;
  };

  private resolveWorkspaceId = async (): Promise<string | undefined> => {
    return resolveContentWorkspaceId({
      agentId: this.agentId,
      serverDB: this.serverDB,
      workspaceId: this.workspaceId,
    });
  };

  /**
   * Rewrite an `lh` command for sandbox execution: prepend the auth +
   * workspace-scope prelude so the CLI runs as this user, against this run's
   * workspace. Shared by `runCommand` and `execScript` β€” the model picks
   * between them by manifest wording alone (`execScript` is the one described
   * as "run the CLI commands a skill's instructions tell you to"), so a hole in
   * either one is a hole in the whole `lh` surface.
   */
  private preprocessSandboxCommand = async (
    command: string,
  ): Promise<{ command: string; error?: string }> => {
    const workspaceId =
      this.workspaceId ?? (isLhCommand(command) ? await this.resolveWorkspaceId() : undefined);
    // `lobe-skills` IS reachable in an Agent Share visitor's run now (only its
    // two load APIs are), so this runtime does get constructed for one and the
    // guard is no longer redundant: `this.userId` is the CREATOR, and minting
    // an `lh` token from it inside a shell a visitor influenced would hand over
    // the creator's whole CLI surface. Nothing routes here today β€” the gate
    // strips `runCommand` / `execScript` before dispatch β€” so this is the
    // backstop for the day Agent Share grows an approval step and they open.
    const result = await preprocessLhCommand(
      command,
      this.userId,
      workspaceId,
      this.shareVisitorBlocked,
      this.operationId,
    );

    return { command: result.command, error: result.error };
  };

  readResource = async (id: string, path: string): Promise<SkillResourceContent> => {
    // Goes through `findById`, not `skillModel.findById`: this is a second
    // entry into skill CONTENT, reached with an id the caller already holds, so
    // resolving it raw would let an out-of-scope skill's resources be read even
    // though its activation was refused. Same not-found error either way β€” a
    // distinct "not allowed" message would confirm the skill exists.
    const skill = await this.findById(id);
    if (!skill) throw new Error(`Skill not found: ${id}`);
    if (!skill.resources) throw new Error(`Skill has no resources: ${id}`);
    return this.resourceService.readResource(skill.resources, path);
  };

  runCommand = async (options: { command: string }): Promise<CommandResult> =>
    withoutReplay(await this.runCommandInSandbox(options));

  private runCommandInSandbox = async (options: { command: string }): Promise<CommandResult> => {
    // The device manifest hides this sandbox API (`DEVICE_HIDDEN_API_NAMES` in
    // `resolveManifest`), but the builtin executor dispatches any method that
    // exists on this runtime regardless of the manifest β€” enforce the same
    // decision at execution time so a prompt-following or hallucinated call
    // can't silently run in the sandbox while the user expects their device.
    if (this.device) {
      return {
        exitCode: 1,
        output: '',
        stderr:
          'runCommand targets the cloud sandbox and is unavailable while a local device is routed. Use execScript for skill scripts, or lobe-local-system runCommand for other shell commands on the device.',
        success: false,
      };
    }

    if (!this.topicId) {
      throw new Error('topicId is required for runCommand');
    }

    // Preprocess lh commands: resolve `lh` to the CLI + inject auth/workspace env
    const lhResult = await this.preprocessSandboxCommand(options.command);
    if (lhResult.error) {
      return {
        executionEnv: 'sandbox',
        exitCode: 1,
        output: '',
        stderr: lhResult.error,
        success: false,
      };
    }

    try {
      const sandboxService = createSandboxService({
        fileService: this.fileService,
        marketService: this.marketService,
        sandboxCwd: this.sandboxCwd,
        sandboxInitialization: this.sandboxInitialization,
        sandboxInstanceId: this.sandboxInstanceId,
        sandboxMode: this.sandboxMode,
        sandboxSpecification: this.sandboxSpecification,
        sandboxWorkingDir: this.sandboxWorkingDir,
        serverDB: this.serverDB,
        topicId: this.topicId,
        userId: this.userId,
      });
      const response = await (this.wrapSandbox?.(sandboxService) ?? sandboxService).callTool(
        'runCommand',
        { command: lhResult.command },
      );

      log('runCommand response: %O', response);

      if (!response.success) {
        return {
          error: response.error,
          executionEnv: 'sandbox',
          exitCode: 1,
          output: '',
          stderr: response.error?.message || 'Command execution failed',
          success: false,
        };
      }

      return { ...normalizeSandboxCommandResult(response), executionEnv: 'sandbox' };
    } catch (error) {
      log('Error running command: %O', error);
      return {
        error: getToolAccessDeniedError(error, 'Command execution failed') ?? {
          message: (error as Error).message,
        },
        executionEnv: 'sandbox',
        exitCode: 1,
        output: '',
        stderr: (error as Error).message || 'Command execution failed',
        success: false,
      };
    }
  };

  /**
   * Resolve the presigned zip URLs (+ content hashes) of the activated skills
   * that have a persisted archive, preserving activation order. Shared by the
   * sandbox path (needs name β†’ url) and the device path (needs the zipHash as
   * the device-cache idempotency key).
   */
  private resolveActivatedSkillArchives = async (
    activatedSkills?: ExecScriptActivatedSkill[],
  ): Promise<ActivatedSkillArchive[]> => {
    const archives: ActivatedSkillArchive[] = [];
    if (!activatedSkills?.length) return archives;

    for (const activatedSkill of activatedSkills) {
      if (!activatedSkill.name) continue;

      // A user skill's package is its latest published version.
      if (canonicalUserSkillIdentifier(activatedSkill.name)) {
        const archive = await this.resolveUserSkillArchive?.(activatedSkill.name);
        const url = archive && (await this.fileService.getFullFileUrl(archive.url));
        if (archive && url) {
          archives.push({ name: activatedSkill.name, url, zipHash: archive.fileHash });
          log('Resolved zipUrl for user skill %s', activatedSkill.name);
        }
        continue;
      }

      // `findByName`, not `skillModel.findByName`: `activatedSkills` is
      // model-suppliable (see `ExecutionRuntime`'s `args.activatedSkills ??
      // this.activatedSkills`), so this resolves an arbitrary name into a skill
      // ARCHIVE β€” a third door into skill content, next to `activateSkill` and
      // `readReference`. Resolving it raw skipped both the agent's disabled set
      // and an Agent Share visitor's grant.
      const skill = await this.findByName(activatedSkill.name);

      if (!skill) {
        log('No persisted skill bundle found for activated skill: %s', activatedSkill.name);
        continue;
      }

      if (!skill.zipFileHash) continue;

      const fileInfo = await this.fileModel.checkHash(skill.zipFileHash);
      if (!fileInfo.isExist || !fileInfo.url) continue;

      const fullUrl = await this.fileService.getFullFileUrl(fileInfo.url);
      if (fullUrl) {
        archives.push({ name: skill.name, url: fullUrl, zipHash: skill.zipFileHash });
        log('Resolved zipUrl for skill %s', skill.name);
      }
    }

    return archives;
  };

  /**
   * Run execScript ON the routed device: prepare every activated skill archive
   * device-side (idempotent by zipHash), then execute the command through the
   * local-system tool over the device gateway with cwd = the extracted skill
   * directory.
   *
   * Failures return an explicit error and NEVER fall back to the sandbox β€” a
   * silent sandbox run against a user who chose their device is exactly the
   * regression this path fixes. Single exception: an older client build that
   * doesn't know the `prepareSkillDirectory` RPC yet (version-skew window)
   * returns the `LEGACY_DEVICE_CLIENT` sentinel, and the caller runs the
   * sandbox path with an explicit disclosure note instead.
   */
  private execScriptOnDevice = async (
    command: string,
    activatedSkills?: ExecScriptActivatedSkill[],
  ): Promise<CommandResult | typeof LEGACY_DEVICE_CLIENT> => {
    const device = this.device!;
    const fail = (stderr: string): CommandResult => ({
      executionEnv: 'device',
      exitCode: 1,
      output: '',
      stderr,
      success: false,
    });

    try {
      const archives = await this.resolveActivatedSkillArchives(activatedSkills);
      const archiveByName = new Map(archives.map((a) => [a.name.toLowerCase(), a]));
      const workspaceId = await device.resolveWorkspaceId();

      // Resolve each activated skill to a device directory in activation
      // order; the LAST resolvable one wins as cwd β€” mirrors the sandbox
      // provider's resolveExecScriptSkillName. Filesystem (project/device)
      // skills already live on the device, so their SKILL.md directory is the
      // cwd directly; archive-backed skills are prepared device-side first
      // (idempotent by zipHash).
      //
      // Prepares fire concurrently (deduped by zipHash β€” concurrent extraction
      // of the same archive would also race device-side): each call is a full
      // device-gateway round-trip and activatedSkills accumulates over the
      // conversation, so awaiting one by one scales exec latency linearly with
      // skill count. The walk below consumes the settled results in activation
      // order, preserving the sequential semantics: last resolvable wins, and
      // the FIRST failure in activation order is the one reported (including
      // the legacy-client sentinel).
      const isProjectSkill = (lowerName: string) =>
        device.projectSkills?.some((s) => s.name.toLowerCase() === lowerName);
      const prepareByHash = new Map<
        string,
        ReturnType<typeof deviceGateway.prepareSkillDirectory>
      >();
      for (const activated of activatedSkills ?? []) {
        const lowerName = activated.name?.toLowerCase();
        if (!lowerName || isProjectSkill(lowerName)) continue;
        const archive = archiveByName.get(lowerName);
        if (!archive || prepareByHash.has(archive.zipHash)) continue;
        prepareByHash.set(
          archive.zipHash,
          deviceGateway.prepareSkillDirectory({
            deviceId: device.deviceId,
            url: archive.url,
            userId: this.userId,
            workspaceId,
            zipHash: archive.zipHash,
          }),
        );
      }
      // Settle everything up front so the early return on a first failure
      // below can't leave a later rejection unhandled (prepareSkillDirectory
      // reports failures as `success: false` rather than throwing, so this is
      // belt-and-braces; re-awaiting a settled entry in the walk is free).
      await Promise.allSettled(prepareByHash.values());

      let runDir: string | undefined;
      for (const activated of activatedSkills ?? []) {
        if (!activated.name) continue;
        const lowerName = activated.name.toLowerCase();

        // Filesystem skills take precedence on name collision, matching
        // `activateSkill` in the ExecutionRuntime.
        const projectSkill = device.projectSkills?.find((s) => s.name.toLowerCase() === lowerName);
        if (projectSkill) {
          runDir = getDirname(projectSkill.location) || runDir;
          continue;
        }

        const archive = archiveByName.get(lowerName);
        if (!archive) continue;

        const prepared = await prepareByHash.get(archive.zipHash)!;

        if (!prepared.success || !prepared.extractedDir) {
          // The device dispatcher's deterministic reply for a method it does
          // not know β€” the client predates this RPC, hand back to the caller
          // for the sandbox fallback.
          if (prepared.error?.includes('Unknown device RPC method')) {
            log('Device %s predates prepareSkillDirectory, falling back', device.deviceId);
            return LEGACY_DEVICE_CLIENT;
          }

          // The gateway stopped waiting, not the device: it keeps downloading and
          // unpacking (a multi-MB skill on a slow link outlasts the deadline), and
          // the next call joins or reuses that work. "Your app may need an
          // update" sent the model to the user instead of simply trying again.
          if (isPrepareTimeout(prepared.error)) {
            return fail(
              `Preparing skill "${archive.name}" on the user's device did not finish in time. This is usually the device still downloading and unpacking the skill package (a large skill or a slow network); that continues in the background and the finished copy is reused. Wait about a minute, then run the same execScript again. If it keeps timing out, tell the user the device's network looks slow, or that the device may have gone to sleep.`,
            );
          }

          return fail(
            `Failed to prepare skill "${archive.name}" on the user's device: ${prepared.error ?? 'unknown error'}. ` +
              `Do not retry elsewhere β€” report this to the user (their ${BRANDING_NAME} app may need an update).`,
          );
        }
        runDir = prepared.extractedDir;
      }

      const cwd = runDir ?? device.workingDirectory;
      // A fenced run writes only to the agent's working directory: `runDir` is
      // the device's shared skill cache, which must stay out of the fence so a
      // fenced run cannot plant code a later unfenced run executes.
      if (device.localSandbox && !device.workingDirectory) {
        return fail(
          'Local Sandbox requires a working directory. Set one for this agent (or topic) and run the command again.',
        );
      }
      // Content scope, NOT the gateway-addressing scope resolved above: a
      // workspace agent routed to the caller's own machine is still editing
      // workspace content.
      const deviceLhEnv = buildDeviceLhEnv({
        operationId: device.operationId,
        workspaceId: await this.resolveWorkspaceId(),
      });
      const response = await executeAuthorizedDeviceToolCall(
        this.serverDB,
        {
          deviceId: device.deviceId,
          operationId: device.operationId,
          userId: this.userId,
          workspaceId,
        },
        {
          apiName: LocalSystemApiName.runCommand,
          // `timeout` is the device-side shell observation window (default
          // 30s, clamped at the device's MAX_OBSERVATION_TIMEOUT_MS) β€” without
          // it a long script returns early while still running.
          arguments: JSON.stringify({
            command,
            ...(cwd && { cwd }),
            // Keep `lh` on the device in this run's workspace instead of the
            // device credentials' personal scope, and tell it which run it acts for.
            ...(deviceLhEnv && { env: deviceLhEnv }),
            ...(device.executionTimeoutMs && { timeout: device.executionTimeoutMs }),
            // The fence is the run owner's decision, applied exactly as the
            // local-system device-proxy does for `runCommand`; neither it nor
            // its root comes from the model. A client predating `sandboxRoot`
            // still fences, just to `cwd`.
            ...(device.localSandbox !== undefined && {
              sandbox: device.localSandbox,
              ...(device.localSandbox && {
                sandboxNetwork: device.localSandboxNetwork === true,
                sandboxRoot: device.workingDirectory,
              }),
            }),
          }),
          identifier: LocalSystemIdentifier,
        },
        device.executionTimeoutMs,
      );

      log('execScript device response: %O', response);

      const state = (response.state ?? {}) as {
        commandId?: string;
        error?: string;
        exitCode?: number;
        outputFiles?: CommandResult['outputFiles'];
        stderr?: string;
        stdout?: string;
        success?: boolean;
      };

      // `response.success` is the delivery envelope. Device-side service
      // failures (spawn error, shell lost, missing params) now come back with
      // `success: false`, but desktop builds predating that fix report them as
      // `success: true` with `state.success: false` and no exitCode β€” and a
      // device runs whatever version the user has installed. Keep testing both
      // or those runs fall through to the still-running branch below and read
      // as a successful run.
      if (!response.success || state.success === false) {
        return fail(
          state.stderr ||
            state.error ||
            response.error ||
            response.content ||
            'Command execution failed on the device',
        );
      }

      // The device shell reports success for any delivered observation, even
      // when the command exited non-zero (the exit status only lives in
      // exitCode) β€” derive success from the exit status instead. An undefined
      // exitCode means the command is still running past the observation
      // window: pass it through with the shell handle so the formatter
      // reports a running command (pollable via local-system.getCommandOutput)
      // instead of pretending completion.
      const exitCode = state.exitCode;
      return {
        executionEnv: 'device',
        exitCode,
        output: state.stdout ?? response.content ?? '',
        // Large streams are truncated to a preview device-side with the full
        // output saved to disk β€” the paths are the only retrieval handle.
        outputFiles: state.outputFiles,
        shellId: state.commandId,
        stderr: state.stderr,
        success: exitCode === undefined || exitCode === 0,
      };
    } catch (error) {
      log('Error executing script on device: %O', error);
      return fail((error as Error).message || 'Command execution failed on the device');
    }
  };

  execScript = async (
    command: string,
    options: {
      activatedSkills?: ExecScriptActivatedSkill[];
      description: string;
    },
  ): Promise<CommandResult> => {
    // Execution target follows the run's plan: a routed device wins over the
    // sandbox (restores the pre-gateway desktop behavior).
    if (this.device) {
      const deviceResult = await this.execScriptOnDevice(command, options.activatedSkills);
      if (deviceResult !== LEGACY_DEVICE_CLIENT) return withoutReplay(deviceResult);

      // Version-skew fallback: the client predates the RPC. Run the sandbox
      // path but disclose the degradation in stderr so the model relays it.
      const sandboxResult = await this.execScriptInSandbox(command, options);
      return withoutReplay({
        ...sandboxResult,
        stderr: [sandboxResult.stderr, LEGACY_FALLBACK_NOTE].filter(Boolean).join('\n'),
      });
    }

    return withoutReplay(await this.execScriptInSandbox(command, options));
  };

  private execScriptInSandbox = async (
    command: string,
    options: {
      activatedSkills?: ExecScriptActivatedSkill[];
      description: string;
    },
  ): Promise<CommandResult> => {
    const { activatedSkills, description } = options;

    if (!this.topicId) {
      throw new Error('topicId is required for execScript');
    }

    // Same `lh` handling as runCommand β€” the client-side executor
    // (`routers/tools/market.ts`) has always preprocessed both tools, and
    // gateway runs must not behave differently.
    const lhResult = await this.preprocessSandboxCommand(command);
    if (lhResult.error) {
      return {
        executionEnv: 'sandbox',
        exitCode: 1,
        output: '',
        stderr: lhResult.error,
        success: false,
      };
    }

    try {
      const enhancedParams: Record<string, unknown> = {
        activatedSkills,
        command: lhResult.command,
        description,
      };

      const archives = await this.resolveActivatedSkillArchives(activatedSkills);
      if (archives.length > 0) {
        enhancedParams.skillZipUrls = Object.fromEntries(archives.map((a) => [a.name, a.url]));
        log(
          'Added skillZipUrls to execScript params: %O',
          archives.map((a) => a.name),
        );
      }

      const sandboxService = createSandboxService({
        fileService: this.fileService,
        marketService: this.marketService,
        sandboxCwd: this.sandboxCwd,
        sandboxInitialization: this.sandboxInitialization,
        sandboxInstanceId: this.sandboxInstanceId,
        sandboxMode: this.sandboxMode,
        sandboxSpecification: this.sandboxSpecification,
        sandboxWorkingDir: this.sandboxWorkingDir,
        serverDB: this.serverDB,
        topicId: this.topicId,
        userId: this.userId,
      });
      const response = await (this.wrapSandbox?.(sandboxService) ?? sandboxService).callTool(
        'execScript',
        enhancedParams,
      );

      log('execScript response: %O', response);

      if (!response.success) {
        return {
          error: response.error,
          executionEnv: 'sandbox',
          exitCode: 1,
          output: '',
          stderr: response.error?.message || 'Command execution failed',
          success: false,
        };
      }

      return { ...normalizeSandboxCommandResult(response), executionEnv: 'sandbox' };
    } catch (error) {
      log('Error executing script: %O', error);
      return {
        error: getToolAccessDeniedError(error, 'Command execution failed') ?? {
          message: (error as Error).message,
        },
        executionEnv: 'sandbox',
        exitCode: 1,
        output: '',
        stderr: (error as Error).message || 'Command execution failed',
        success: false,
      };
    }
  };

  exportFile = async (path: string, filename: string): Promise<ExportFileResult> =>
    withoutReplay(await this.exportFileFromSandbox(path, filename));

  private exportFileFromSandbox = async (
    path: string,
    filename: string,
  ): Promise<ExportFileResult> => {
    // Same manifest-hidden guard as `runCommand`: the message reaches the
    // model through the ExecutionRuntime catch ("Failed to export file: ...").
    if (this.device) {
      throw new Error(
        "exportFile pulls artifacts out of the cloud sandbox and is unavailable while a local device is routed β€” files created on the device are already on the user's machine.",
      );
    }

    if (!this.topicId) {
      throw new Error('topicId is required for exportFile');
    }

    try {
      const sandboxService = createSandboxService({
        fileService: this.fileService,
        marketService: this.marketService,
        sandboxCwd: this.sandboxCwd,
        sandboxInitialization: this.sandboxInitialization,
        sandboxInstanceId: this.sandboxInstanceId,
        sandboxMode: this.sandboxMode,
        sandboxSpecification: this.sandboxSpecification,
        sandboxWorkingDir: this.sandboxWorkingDir,
        topicId: this.topicId,
        userId: this.userId,
      });
      const result = await sandboxService.exportAndUploadFile(path, filename);

      return {
        error: result.error,
        fileId: result.fileId,
        filename: result.filename,
        mimeType: result.mimeType,
        size: result.size,
        success: result.success,
        url: result.url,
      };
    } catch (error) {
      log('Error exporting file: %O', error);
      return {
        error: getToolAccessDeniedError(error, 'File export failed') ?? {
          message: (error as Error).message,
        },
        filename,
        success: false,
      };
    }
  };
}

/**
 * Skills Server Runtime
 * Per-request runtime (needs serverDB, userId, topicId)
 */
export const skillsRuntime: ServerRuntimeRegistration = {
  factory: async (context) => {
    if (!context.serverDB) {
      throw new Error('serverDB is required for Skills execution');
    }
    if (!context.userId) {
      throw new Error('userId is required for Skills execution');
    }

    // Fetch market access token from user settings
    const userSettings = await readSkillUserSettings(
      new UserModel(context.serverDB, context.userId),
    );
    const marketAccessToken = userSettings?.market?.accessToken;

    const activation = await resolveSkillActivationResources(
      { ...context, serverDB: context.serverDB, userId: context.userId },
      { userSettings },
    );
    const {
      builtinSkills: resolvedBuiltinSkills,
      deviceFileAccess,
      disabledSkillIds,
      isLibrarySkillEnabled,
      isSkillGranted,
      listAvailableSkills,
      loadUserSkill,
      projectSkills: resolvedProjectSkills,
      resolveDeviceWorkspaceId,
      resolveSkill,
      workspaceId,
    } = activation;

    const skillModel = new AgentSkillModel(context.serverDB, context.userId, workspaceId);
    const resourceService = new SkillResourceService(context.serverDB, context.userId, workspaceId);
    /**
     * `workspaceId` decides which sandbox session this runtime reaches: the
     * session is keyed by the acting account, so a token without it acts as the
     * personal account while `lobe-creds` and `lobe-cloud-sandbox` β€” which do
     * pass it β€” act as the workspace. Omitting it split one workspace topic
     * across two sandboxes, leaving injected credentials invisible here.
     */
    // Same resolution, same inputs as the cloud-sandbox runtime β€” see the
    // `sandboxMode` note on the service options for what a disagreement costs.
    const sandbox = await resolveSandboxSessionConfig({
      isShareVisitorRun: Boolean(context.agentShareVisitor),
      serverDB: context.serverDB,
      topicId: context.topicId,
      userId: context.userId,
      workspaceId,
    });

    const marketService = createSandboxMarketService(
      context,
      marketAccessToken,
      workspaceId,
      sandbox.claim,
    );
    const fileService = new FileService(context.serverDB, context.userId, workspaceId);
    const fileModel = new FileModel(context.serverDB, context.userId, workspaceId);

    // `activeDeviceId` presence is the device-branch switch: execScript then
    // runs on the device instead of the cloud sandbox. The executors filter
    // the raw metadata id through `resolveRunActiveDeviceId` (plan/policy
    // gate) before it reaches this context, so a preset or stale id cannot
    // route execution onto a device the resolved plan didn't authorize;
    // `device-unrouted` runs keep the sandbox path (with the unrouted
    // disclosure in the manifest).
    const device: SkillDeviceExecution | undefined = context.activeDeviceId
      ? {
          deviceId: context.activeDeviceId,
          executionTimeoutMs: context.executionTimeoutMs,
          localSandbox: context.localSandbox,
          localSandboxNetwork: context.localSandboxNetwork,
          operationId: context.operationId,
          projectSkills: resolvedProjectSkills,
          // Same lazy workspace-principal recovery as the local-system runtime,
          // so workspace devices are addressed under the right gateway pool.
          resolveWorkspaceId: resolveDeviceWorkspaceId,
          workingDirectory: context.workingDirectory,
        }
      : undefined;

    const service = new SkillServerRuntimeService({
      wrapSandbox: pageSandboxWrapper(context, workspaceId),
      agentId: context.agentId,
      device,
      disabledSkillIds,
      fileModel,
      fileService,
      isLibrarySkillEnabled,
      isSkillGranted,
      marketService,
      // The run this runtime belongs to, so an `lh agent run` it preprocesses in
      // the cloud sandbox can be attributed to the agent that launched it β€” the
      // device branch below carries the same id for its own routing.
      operationId: context.operationId,
      resourceService,
      // A user skill's latest version is its archive, read through the same
      // load as `activateSkill` so the same visibility rules apply.
      resolveUserSkillArchive: async (name) => (await loadUserSkill(name))?.archive,
      sandboxCwd: sandbox.cwd,
      sandboxInitialization: sandbox.initialization,
      sandboxInstanceId: sandbox.environment,
      sandboxMode: sandbox.mode,
      sandboxSpecification: sandbox.specification,
      sandboxWorkingDir: sandbox.workingDir,
      serverDB: context.serverDB,
      shareVisitorBlocked: !!context.agentShareVisitor,
      skillModel,
      topicId: context.topicId,
      userId: context.userId,
      // The recovered id, so the `lh` prelude names the same workspace the
      // sandbox session was opened under rather than looking it up again.
      workspaceId,
    });

    return new SkillsExecutionRuntime({
      // Resolved by the runtime executors from the operation's message
      // history (the server-side stepContext equivalent); execScript falls
      // back to these because the raw LLM args never carry activatedSkills.
      activatedSkills: context.activatedSkills,
      builtinSkills: resolvedBuiltinSkills,
      deviceFileAccess,
      listAvailableSkills,
      projectSkills: resolvedProjectSkills,
      resolveSkill,
      service,
    });
  },
  identifier: SkillsIdentifier,
};