UNPKG

@sellable/install

Version:

One-command installer for Sellable MCP in Claude Code, Codex, and Hermes

4,827 lines • 204 kB
import { createHash, createHmac, timingSafeEqual } from "node:crypto";
import {
  closeSync,
  constants,
  existsSync,
  fchmodSync,
  fchownSync,
  fsyncSync,
  lstatSync,
  openSync,
  readFileSync,
  realpathSync,
  renameSync,
  unlinkSync,
  writeFileSync,
} from "node:fs";
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";

export const HERMES_AGENT_BRIDGE_ID = "sellable-agent-hermes-bridge/v1";
export const HERMES_AGENT_BRIDGE_MARKER = "SELLABLE_AGENT_HERMES_BRIDGE_V1";
export const HERMES_DEVELOPER_SLACK_METADATA_CONTRACT =
  "hermes-developer-native-slack-metadata/v1";

const HERMES_DEVELOPER_NATIVE_SOURCE = "hermes-developer-node-socket-mode/v1";
const HERMES_DEVELOPER_EVENT_TYPES = new Set([
  "app_mention",
  "message",
  "interactive",
]);
const HERMES_DEVELOPER_EVENT_SUBTYPES = new Set([
  "",
  "message_changed",
  "message_deleted",
  "unsupported",
]);
const HERMES_DEVELOPER_SLACK_ID = /^[A-Z][A-Z0-9]{8,31}$/;
const HERMES_DEVELOPER_SLACK_TS = /^\d{1,16}\.\d{6}$/;
const HERMES_DEVELOPER_EVENT_ID = /^[A-Za-z0-9_.:-]{1,128}$/;

const sha256 = (value) => createHash("sha256").update(value).digest("hex");
const isSha256 = (value) =>
  typeof value === "string" && /^[a-f0-9]{64}$/.test(value);

function canonicalHermesDeveloperMetadata(value) {
  if (Array.isArray(value)) return value.map(canonicalHermesDeveloperMetadata);
  if (value && typeof value === "object") {
    return Object.fromEntries(
      Object.keys(value)
        .sort()
        .map((key) => [key, canonicalHermesDeveloperMetadata(value[key])])
    );
  }
  return value;
}

function exactHermesDeveloperMetadataKeys(value, expected) {
  return (
    value &&
    typeof value === "object" &&
    !Array.isArray(value) &&
    Object.keys(value).sort().join("\0") === [...expected].sort().join("\0")
  );
}

/**
 * Seal identity copied by the repo-owned Socket Mode receiver before any
 * planner or worker execution. This deliberately has no text or tool
 * arguments: downstream code cannot select a Slack principal, route, event or
 * acknowledgement contract.
 */
export function createHermesDeveloperTrustedSlackMetadata(
  input,
  authenticationKey
) {
  if (!Buffer.isBuffer(authenticationKey) || authenticationKey.length < 32) {
    throw new HermesAgentBridgeError(
      "HERMES_DEVELOPER_SLACK_METADATA_KEY_REQUIRED",
      "Hermes developer Slack metadata requires a gateway-private authentication key"
    );
  }
  const keys = [
    "source",
    "teamId",
    "channelId",
    "actorId",
    "eventId",
    "eventType",
    "eventSubtype",
    "messageTs",
    "threadTs",
    "botId",
    "ackCapability",
    "adapterGeneration",
  ];
  if (
    !exactHermesDeveloperMetadataKeys(input, keys) ||
    input.source !== HERMES_DEVELOPER_NATIVE_SOURCE ||
    !HERMES_DEVELOPER_SLACK_ID.test(input.teamId ?? "") ||
    !HERMES_DEVELOPER_SLACK_ID.test(input.channelId ?? "") ||
    !HERMES_DEVELOPER_SLACK_ID.test(input.actorId ?? "") ||
    !HERMES_DEVELOPER_EVENT_ID.test(input.eventId ?? "") ||
    !HERMES_DEVELOPER_EVENT_TYPES.has(input.eventType) ||
    !HERMES_DEVELOPER_EVENT_SUBTYPES.has(input.eventSubtype ?? "") ||
    !HERMES_DEVELOPER_SLACK_TS.test(input.messageTs ?? "") ||
    (input.threadTs !== null &&
      !HERMES_DEVELOPER_SLACK_TS.test(input.threadTs ?? "")) ||
    (input.botId !== null &&
      !HERMES_DEVELOPER_SLACK_ID.test(input.botId ?? "")) ||
    input.ackCapability !== "defer_until_durable_intake" ||
    !Number.isSafeInteger(input.adapterGeneration) ||
    input.adapterGeneration < 1
  ) {
    throw new HermesAgentBridgeError(
      "HERMES_DEVELOPER_SLACK_METADATA_REJECTED",
      "Hermes developer Slack metadata must come from the deferred-ack repo-owned receiver"
    );
  }
  const body = canonicalHermesDeveloperMetadata({
    contract: HERMES_DEVELOPER_SLACK_METADATA_CONTRACT,
    ...input,
  });
  const metadataDigest = sha256(JSON.stringify(body));
  return Object.freeze({
    ...body,
    metadataDigest,
    authenticationTag: createHmac("sha256", authenticationKey)
      .update(metadataDigest)
      .digest("hex"),
  });
}

export function verifyHermesDeveloperTrustedSlackMetadata(
  value,
  authenticationKey
) {
  try {
    if (!Buffer.isBuffer(authenticationKey) || authenticationKey.length < 32) {
      return false;
    }
    if (
      !exactHermesDeveloperMetadataKeys(value, [
        "contract",
        "source",
        "teamId",
        "channelId",
        "actorId",
        "eventId",
        "eventType",
        "eventSubtype",
        "messageTs",
        "threadTs",
        "botId",
        "ackCapability",
        "adapterGeneration",
        "metadataDigest",
        "authenticationTag",
      ]) ||
      value.contract !== HERMES_DEVELOPER_SLACK_METADATA_CONTRACT
    ) {
      return false;
    }
    const {
      contract: _contract,
      metadataDigest,
      authenticationTag,
      ...input
    } = value;
    const rebuilt = createHermesDeveloperTrustedSlackMetadata(
      input,
      authenticationKey
    );
    const supplied = Buffer.from(authenticationTag ?? "", "hex");
    const expected = Buffer.from(rebuilt.authenticationTag, "hex");
    return (
      rebuilt.metadataDigest === metadataDigest &&
      supplied.length === expected.length &&
      timingSafeEqual(supplied, expected)
    );
  } catch {
    return false;
  }
}

export class HermesAgentBridgeError extends Error {
  constructor(code, message, details = {}) {
    super(message);
    this.name = "HermesAgentBridgeError";
    this.code = code;
    this.details = details;
  }
}

function replaceExactlyOnce(source, needle, replacement, label) {
  const first = source.indexOf(needle);
  const last = source.lastIndexOf(needle);
  if (first < 0 || first !== last) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_PATCH_ANCHOR_MISMATCH",
      `Expected exactly one ${label} patch anchor`,
      { label }
    );
  }
  return `${source.slice(0, first)}${replacement}${source.slice(first + needle.length)}`;
}

const SECRET_FD_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: inherited Slack secret reader.
_SELLABLE_AGENT_SECRET_CACHE = {}

def _sellable_agent_read_slack_secret(fd: int, env_name: str, prefix: str):
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        return os.getenv(env_name)
    if env_name in os.environ:
        raise RuntimeError(f"{env_name} must not be present in the Sellable runtime environment")
    cache_key = (fd, env_name, prefix)
    if cache_key in _SELLABLE_AGENT_SECRET_CACHE:
        return _SELLABLE_AGENT_SECRET_CACHE[cache_key]
    try:
        duplicate = os.dup(fd)
    except OSError as exc:
        raise RuntimeError(f"required inherited descriptor {fd} is unavailable") from exc
    try:
        chunks = []
        size = 0
        while True:
            chunk = os.read(duplicate, min(1024, 4097 - size))
            if not chunk:
                break
            chunks.append(chunk)
            size += len(chunk)
            if size > 4096:
                raise RuntimeError(f"secret on descriptor {fd} exceeds the size limit")
    finally:
        os.close(duplicate)
    try:
        value = b"".join(chunks).decode("utf-8").strip()
    except UnicodeDecodeError as exc:
        raise RuntimeError(f"secret on descriptor {fd} is not valid UTF-8") from exc
    if not value or "\\x00" in value or "\\n" in value or "\\r" in value:
        raise RuntimeError(f"secret on descriptor {fd} is malformed")
    if not value.startswith(prefix):
        raise RuntimeError(f"secret on descriptor {fd} has the wrong credential class")
    _SELLABLE_AGENT_SECRET_CACHE[cache_key] = value
    return value
`;

const SLACK_TEST_API_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: bounded whole-image Slack API seam.
def _sellable_agent_slack_api_kwargs():
    if (
        os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1"
        or os.getenv("SELLABLE_AGENT_ALLOW_TEST_OIDC", "") != "1"
    ):
        return {}
    origin = os.getenv("SELLABLE_AGENT_SLACK_API_ORIGIN", "")
    from urllib.parse import urlparse
    parsed = urlparse(origin)
    if (
        parsed.scheme != "http"
        or parsed.hostname not in {"127.0.0.1", "host.docker.internal"}
        or parsed.username
        or parsed.password
        or parsed.path not in {"", "/"}
        or parsed.params
        or parsed.query
        or parsed.fragment
    ):
        raise RuntimeError("Sellable whole-image Slack API origin is invalid")
    return {"base_url": origin.rstrip("/") + "/api/"}
`;

const SLACK_DEDICATED_TEST_API_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: bounded disposable Fly Slack API seam.
def _sellable_agent_slack_api_kwargs():
    if (
        os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1"
        or (
            os.getenv("SELLABLE_AGENT_ALLOW_TEST_PROVIDERS", "") != "1"
            and os.getenv("SELLABLE_AGENT_ALLOW_TEST_OIDC", "") != "1"
        )
    ):
        return {}
    origin = os.getenv("SELLABLE_AGENT_SLACK_API_ORIGIN", "")
    from urllib.parse import urlparse
    parsed = urlparse(origin)
    local_http = (
        parsed.scheme == "http"
        and parsed.hostname in {"127.0.0.1", "host.docker.internal"}
    )
    disposable_https = parsed.scheme == "https" and bool(parsed.hostname)
    if (
        not (local_http or disposable_https)
        or parsed.username
        or parsed.password
        or parsed.path not in {"", "/"}
        or parsed.params
        or parsed.query
        or parsed.fragment
    ):
        raise RuntimeError("Sellable disposable Slack API origin is invalid")
    return {"base_url": origin.rstrip("/") + "/api/"}
`;

// Shared-app runtimes reach Slack through the machine-local stand-in
// (fly-customer-image/slack-stand-in.mjs). Both keys are required, and the
// origin must be the stand-in's own loopback listener.
const SLACK_SHARED_LOOPBACK_API_HELPER = replaceExactlyOnce(
  SLACK_DEDICATED_TEST_API_HELPER,
  "def _sellable_agent_slack_api_kwargs():\n",
  `def _sellable_agent_slack_api_kwargs():
    if (
        os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1"
        and os.getenv("SELLABLE_AGENT_SLACK_TRANSPORT", "") == "shared-local"
    ):
        origin = os.getenv("SELLABLE_AGENT_SLACK_API_ORIGIN", "")
        from urllib.parse import urlparse
        parsed = urlparse(origin)
        if (
            parsed.scheme != "http"
            or parsed.hostname != "127.0.0.1"
            or not parsed.port
            or parsed.username
            or parsed.password
            or parsed.path not in {"", "/"}
            or parsed.params
            or parsed.query
            or parsed.fragment
        ):
            raise RuntimeError("Sellable shared Slack stand-in origin is invalid")
        return {"base_url": origin.rstrip("/") + "/api/"}
`,
  "shared Slack stand-in API seam"
);

// A link preview (unfurl) reaches the adapter as a second \`message_changed\`
// event for the customer's own message. Hermes drops it once the original
// message's ts is claimed, but the claim comes after the channel checks, and
// the preview of a LinkedIn profile URL arrived inside that gap in both
// first-campaign threads of DhsD | Studio (2026-11-23, 2026-11-24): the same
// message ran as a second turn after the plan was posted. A person's edit
// carries Slack's \`edited\` stamp; a preview does not, so it is not someone
// speaking again.
const SLACK_PREVIEW_IS_NOT_A_MESSAGE_NEEDLE = `        updated_message = event.get("message")
        if not isinstance(updated_message, dict):
            return None
        original_message_ts = str(updated_message.get("ts") or "")
`;
const SLACK_PREVIEW_IS_NOT_A_MESSAGE = `        updated_message = event.get("message")
        if not isinstance(updated_message, dict):
            return None
        # ${HERMES_AGENT_BRIDGE_MARKER}: a link preview is not a new message. Only a person's edit
        # (Slack's "edited" stamp) can start a turn from message_changed.
        if not isinstance(updated_message.get("edited"), dict):
            return None
        original_message_ts = str(updated_message.get("ts") or "")
`;

const SLACK_AGENT_SESSIONS_STATUS_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: Slack Agent Sessions status normalization and dedupe.
_SELLABLE_AGENT_SESSION_STATUS_VALUES = frozenset({"processing", "active", "suspended", "closed"})
# Hermes asks every 2 seconds; Slack gets one renewal in 20. At 45 minutes a
# status that anything turned off stayed off for the rest of a run (demo
# workspace, 2026-12-02), so each renewal puts it back while work goes on.
_SELLABLE_AGENT_PROCESSING_DEDUPE_SECS = 20
_SELLABLE_AGENT_PROCESSING_DEDUPE_MAX = 512

def _sellable_agent_normalize_session_status(status: str) -> str:
    text = str(status or "").strip()
    if text in _SELLABLE_AGENT_SESSION_STATUS_VALUES:
        return text
    return "processing" if text else "active"

def _sellable_agent_should_skip_session_status(adapter, key, normalized: str) -> bool:
    cache = getattr(adapter, "_sellable_agent_session_status_cache", None)
    if cache is None:
        cache = {}
        setattr(adapter, "_sellable_agent_session_status_cache", cache)
    now = time.monotonic()
    if normalized != "processing":
        cache.pop(key, None)
        return False
    prev = cache.get(key)
    if prev is not None and (now - prev) < _SELLABLE_AGENT_PROCESSING_DEDUPE_SECS:
        return True
    cache[key] = now
    if len(cache) > _SELLABLE_AGENT_PROCESSING_DEDUPE_MAX:
        oldest = min(cache, key=cache.get)
        cache.pop(oldest, None)
    return False
`;

const SLACK_MESSAGE_UNFURL_FILE_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: recover files from bounded allowed-channel Slack message unfurls.
async def _sellable_agent_recover_slack_message_unfurl_files(
    adapter,
    attachments,
    current_channel_id,
    team_id,
    existing_files=None,
):
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        return []
    if not current_channel_id or not team_id:
        return []

    message_unfurls = [
        attachment
        for attachment in list(attachments or [])
        if isinstance(attachment, dict) and attachment.get("is_msg_unfurl") is True
    ][:3]
    if not message_unfurls:
        return []

    client = adapter._team_clients.get(team_id)
    if client is None:
        logger.warning(
            "[Slack] Refusing message-unfurl file recovery: exact workspace client is unavailable"
        )
        return []

    allowed_channel_ids = {
        channel_id.strip().upper()
        for channel_id in os.getenv("SLACK_ALLOWED_CHANNELS", "").split(",")
        if channel_id.strip()
    }
    if not allowed_channel_ids:
        logger.warning(
            "[Slack] Refusing message-unfurl file recovery: allowed channels are unavailable"
        )
        return []

    seen_file_ids = {
        str(file_obj.get("id"))
        for file_obj in (existing_files or [])
        if isinstance(file_obj, dict) and file_obj.get("id")
    }
    recovered = []
    for attachment in message_unfurls:
        target_channel_id = str(attachment.get("channel_id") or "")
        target_ts = str(attachment.get("ts") or "")
        if (
            target_channel_id.upper() not in allowed_channel_ids
            or re.fullmatch(r"\\d+\\.\\d{6}", target_ts) is None
        ):
            continue

        thread_ts = str(attachment.get("thread_ts") or "")
        if thread_ts and re.fullmatch(r"\\d+\\.\\d{6}", thread_ts) is None:
            thread_ts = ""

        permalink = str(
            attachment.get("from_url") or attachment.get("title_link") or ""
        )
        if permalink:
            try:
                from html import unescape
                from urllib.parse import parse_qs, urlsplit

                parsed = urlsplit(unescape(permalink))
                path_match = re.fullmatch(
                    r"/archives/([CGD][A-Z0-9]{8,})/p(\\d{16})",
                    parsed.path,
                    re.IGNORECASE,
                )
                if (
                    parsed.scheme != "https"
                    or not parsed.hostname
                    or not parsed.hostname.lower().endswith(".slack.com")
                    or parsed.username
                    or parsed.password
                    or parsed.port is not None
                    or path_match is None
                    or path_match.group(1).upper() != target_channel_id
                ):
                    continue
                packed_ts = path_match.group(2)
                permalink_ts = f"{packed_ts[:-6]}.{packed_ts[-6:]}"
                if permalink_ts != target_ts:
                    continue
                permalink_thread_ts = (parse_qs(parsed.query).get("thread_ts") or [""])[0]
                if re.fullmatch(r"\\d+\\.\\d{6}", permalink_thread_ts or ""):
                    thread_ts = permalink_thread_ts
            except (TypeError, ValueError):
                continue

        if attachment.get("is_reply_unfurl") and not thread_ts:
            logger.warning(
                "[Slack] Refusing reply-unfurl file recovery without an exact thread root"
            )
            continue

        try:
            if thread_ts and thread_ts != target_ts:
                response = await client.conversations_replies(
                    channel=target_channel_id,
                    ts=thread_ts,
                    oldest=target_ts,
                    latest=target_ts,
                    inclusive=True,
                    limit=1,
                )
            else:
                response = await client.conversations_history(
                    channel=target_channel_id,
                    oldest=target_ts,
                    latest=target_ts,
                    inclusive=True,
                    limit=1,
                )
        except Exception as exc:
            logger.warning(
                "[Slack] Message-unfurl file recovery failed for %s: %s",
                target_ts,
                type(exc).__name__,
            )
            continue

        linked_message = next(
            (
                message
                for message in ((response or {}).get("messages") or [])
                if isinstance(message, dict) and str(message.get("ts") or "") == target_ts
            ),
            None,
        )
        if linked_message is None:
            continue
        for file_obj in linked_message.get("files") or []:
            if not isinstance(file_obj, dict):
                continue
            file_id = str(file_obj.get("id") or "")
            if re.fullmatch(r"F[A-Z0-9]{8,}", file_id) is None or file_id in seen_file_ids:
                continue
            seen_file_ids.add(file_id)
            recovered.append(file_obj)
            if len(recovered) >= 10:
                return recovered
    return recovered
`;

const MCP_META_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: request-scoped Sellable MCP metadata.
def _sellable_agent_call_meta():
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        return None
    from gateway.session_context import get_session_env, session_context_engaged
    if (
        not session_context_engaged()
        or get_session_env("HERMES_SESSION_PLATFORM", "").strip().lower() != "slack"
    ):
        # Non-Slack runtime sessions (console/TUI, cron) carry no Slack actor
        # identity. Send no actor meta; the serving API enforces its own actor
        # gates, as with installs outside the Sellable runtime.
        return None
    from gateway.session_context import get_session_env, session_context_engaged
    if not session_context_engaged():
        raise RuntimeError("Sellable MCP calls require an active Hermes session context")
    legacy_names = (
        "HERMES_SESSION_PLATFORM",
        "HERMES_SESSION_CHAT_ID",
        "HERMES_SESSION_USER_ID",
        "HERMES_SESSION_MESSAGE_ID",
    )
    if any(os.getenv(name) for name in legacy_names):
        raise RuntimeError("Sellable MCP identity must not come from process-global session variables")
    platform = get_session_env("HERMES_SESSION_PLATFORM", "").strip().lower()
    channel_id = get_session_env("HERMES_SESSION_CHAT_ID", "").strip()
    requester_id = get_session_env("HERMES_SESSION_USER_ID", "").strip()
    message_id = get_session_env("HERMES_SESSION_MESSAGE_ID", "").strip()
    session_id = get_session_env("HERMES_SESSION_ID", "").strip()
    if platform != "slack" or not channel_id or not requester_id or not message_id or not session_id:
        raise RuntimeError("Sellable MCP calls require a bound Slack requester, channel, message, and session")
    return {
        "sellableAgent": {
            "requesterId": requester_id,
            "channelId": channel_id,
            "providerRequestId": message_id,
            "sessionId": session_id,
        }
    }
`;

const MCP_META_HELPER_OPTIONAL_SESSION = MCP_META_HELPER.replace(
  '    if platform != "slack" or not channel_id or not requester_id or not message_id or not session_id:\n        raise RuntimeError("Sellable MCP calls require a bound Slack requester, channel, message, and session")\n',
  '    if platform != "slack" or not channel_id or not requester_id or not message_id:\n        raise RuntimeError("Sellable MCP calls require a bound Slack requester, channel, and message")\n'
).replace(
  '    return {\n        "sellableAgent": {\n            "requesterId": requester_id,\n            "channelId": channel_id,\n            "providerRequestId": message_id,\n            "sessionId": session_id,\n        }\n    }\n',
  '    sellable_agent = {\n        "requesterId": requester_id,\n        "channelId": channel_id,\n        "providerRequestId": message_id,\n    }\n    if session_id:\n        sellable_agent["sessionId"] = session_id\n    return {"sellableAgent": sellable_agent}\n'
);

const V020_SOUL_TURN_PROLOGUE_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: apply only the proven live SOUL at a turn boundary.
_SELLABLE_AGENT_SOUL_SESSION_HASHES = {}

def _sellable_agent_restore_or_build_proven_soul(
    agent,
    system_message,
    conversation_history,
    restore_or_build_system_prompt,
):
    import os
    import re
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        if agent._cached_system_prompt is None:
            restore_or_build_system_prompt(agent, system_message, conversation_history)
        return

    import fcntl
    import hashlib
    import json
    import stat
    from pathlib import Path
    from hermes_constants import get_hermes_home

    profile_id = os.getenv("SELLABLE_HERMES_PROFILE_ID", "")
    if re.fullmatch(r"[a-z0-9][a-z0-9_-]{0,127}", profile_id or "") is None:
        raise RuntimeError("Sellable SOUL profile identity is unavailable or invalid")

    data_root = Path("/opt/data")
    expected_home = data_root / "profiles" / profile_id
    try:
        observed_home = Path(get_hermes_home())
        observed_resolved = observed_home.resolve(strict=True)
        expected_resolved = expected_home.resolve(strict=True)
        if observed_resolved != expected_resolved:
            # HERMES_HOME may legitimately be the data root (the directory
            # that CONTAINS profiles/<id>) when no per-session override is
            # active yet -- e.g. brand-new sessions in hermes serve. Accept
            # it only when root/profiles/<profile_id> strictly resolves to
            # the canonical profile directory.
            if (
                observed_home / "profiles" / profile_id
            ).resolve(strict=True) != expected_resolved:
                raise RuntimeError("Hermes resolved a different SOUL profile")
    except OSError as exc:
        raise RuntimeError("Hermes SOUL profile cannot be resolved") from exc

    lock_path = data_root / ".sellable-agent" / "soul-locks" / f"{profile_id}.lock"
    marker_path = data_root / ".sellable-agent" / "soul-proofs" / f"{profile_id}.json"
    state_root = data_root / ".sellable-agent"
    state_directory_stats = []
    for state_directory in (state_root, lock_path.parent, marker_path.parent):
        try:
            state_directory_stat = os.lstat(state_directory)
        except OSError as exc:
            raise RuntimeError("Sellable SOUL state directory is unavailable") from exc
        if (
            not stat.S_ISDIR(state_directory_stat.st_mode)
            or state_directory_stat.st_uid != 0
            or stat.S_IMODE(state_directory_stat.st_mode) != 0o2770
        ):
            raise RuntimeError("Sellable SOUL state directory has unsafe metadata")
        state_directory_stats.append(state_directory_stat)
    state_gid = state_directory_stats[0].st_gid
    if any(directory_stat.st_gid != state_gid for directory_stat in state_directory_stats[1:]):
        raise RuntimeError("Sellable SOUL state directories have divergent groups")
    allowed_owner_uids = {0, os.getuid()}

    open_flags = os.O_RDWR | getattr(os, "O_CLOEXEC", 0) | getattr(os, "O_NOFOLLOW", 0)
    try:
        lock_fd = os.open(lock_path, open_flags)
    except OSError as exc:
        raise RuntimeError("Sellable SOUL lock is unavailable") from exc

    try:
        lock_stat = os.fstat(lock_fd)
        if (
            not stat.S_ISREG(lock_stat.st_mode)
            or lock_stat.st_uid not in allowed_owner_uids
            or lock_stat.st_gid != state_gid
            or stat.S_IMODE(lock_stat.st_mode) != 0o660
            or lock_stat.st_nlink != 1
        ):
            raise RuntimeError("Sellable SOUL lock has unsafe metadata")
        fcntl.flock(lock_fd, fcntl.LOCK_EX)

        try:
            marker_stat = os.lstat(marker_path)
            if (
                not stat.S_ISREG(marker_stat.st_mode)
                or marker_stat.st_uid not in allowed_owner_uids
                or marker_stat.st_gid != state_gid
                or stat.S_IMODE(marker_stat.st_mode) != 0o660
                or marker_stat.st_nlink != 1
                or marker_stat.st_size <= 0
                or marker_stat.st_size > 4096
            ):
                raise RuntimeError("Sellable SOUL proof marker has unsafe metadata")
            marker_flags = os.O_RDONLY | getattr(os, "O_CLOEXEC", 0) | getattr(os, "O_NOFOLLOW", 0)
            marker_fd = os.open(marker_path, marker_flags)
            try:
                opened_marker_stat = os.fstat(marker_fd)
                if (
                    opened_marker_stat.st_dev != marker_stat.st_dev
                    or opened_marker_stat.st_ino != marker_stat.st_ino
                ):
                    raise RuntimeError("Sellable SOUL proof marker changed while opening")
                marker_chunks = []
                marker_size = 0
                while marker_size <= 4096:
                    marker_chunk = os.read(marker_fd, min(1024, 4097 - marker_size))
                    if not marker_chunk:
                        break
                    marker_chunks.append(marker_chunk)
                    marker_size += len(marker_chunk)
                marker_bytes = b"".join(marker_chunks)
            finally:
                os.close(marker_fd)
            if len(marker_bytes) > 4096 or len(marker_bytes) != marker_stat.st_size:
                raise RuntimeError("Sellable SOUL proof marker exceeds its size limit")
            marker = json.loads(marker_bytes.decode("utf-8"))
        except (OSError, UnicodeDecodeError, ValueError) as exc:
            raise RuntimeError("Sellable SOUL proof marker is unavailable or malformed") from exc

        required_keys = {
            "schemaVersion",
            "state",
            "profileId",
            "rawFileSha256",
            "effectiveSha256",
            "generation",
        }
        if (
            not isinstance(marker, dict)
            or set(marker) != required_keys
            or marker.get("schemaVersion") != "sellable-agent-soul-proof/v1"
            or marker.get("state") != "PROVEN"
            or marker.get("profileId") != profile_id
            or re.fullmatch(r"[a-f0-9]{64}", marker.get("rawFileSha256") or "") is None
            or re.fullmatch(r"[a-f0-9]{64}", marker.get("effectiveSha256") or "") is None
            or not isinstance(marker.get("generation"), str)
            or len(marker["generation"]) < 8
            or len(marker["generation"]) > 128
        ):
            marker_state = marker.get("state") if isinstance(marker, dict) else "invalid"
            raise RuntimeError(
                f"Sellable SOUL proof marker is not proven: path={marker_path} state={marker_state}"
            )

        soul_path = expected_home / "SOUL.md"
        try:
            soul_stat = os.lstat(soul_path)
            if (
                not stat.S_ISREG(soul_stat.st_mode)
                or soul_stat.st_uid not in allowed_owner_uids
                or stat.S_IMODE(soul_stat.st_mode) not in {0o600, 0o640}
                or soul_stat.st_nlink != 1
            ):
                raise RuntimeError("Hermes SOUL file has unsafe metadata")
            soul_fd = os.open(
                soul_path,
                os.O_RDONLY | getattr(os, "O_CLOEXEC", 0) | getattr(os, "O_NOFOLLOW", 0),
            )
            try:
                opened_soul_stat = os.fstat(soul_fd)
                if (
                    opened_soul_stat.st_dev != soul_stat.st_dev
                    or opened_soul_stat.st_ino != soul_stat.st_ino
                ):
                    raise RuntimeError("Hermes SOUL file changed while opening")
                soul_bytes = b""
                while len(soul_bytes) <= 65536:
                    chunk = os.read(soul_fd, min(8192, 65537 - len(soul_bytes)))
                    if not chunk:
                        break
                    soul_bytes += chunk
            finally:
                os.close(soul_fd)
        except OSError as exc:
            raise RuntimeError("Hermes SOUL file cannot be read safely") from exc
        if len(soul_bytes) > 65536:
            raise RuntimeError("Hermes SOUL file exceeds its size limit")
        raw_sha256 = hashlib.sha256(soul_bytes).hexdigest()
        if raw_sha256 != marker["rawFileSha256"]:
            raise RuntimeError("Hermes SOUL bytes are not the proven generation")

        session_id = str(agent.session_id or "")
        if not session_id:
            raise RuntimeError("Hermes session identity is unavailable for SOUL refresh")
        session_key = (profile_id, session_id)
        if (
            session_key not in _SELLABLE_AGENT_SOUL_SESSION_HASHES
            and len(_SELLABLE_AGENT_SOUL_SESSION_HASHES) >= 4096
        ):
            # A bounded cache miss only causes a safe one-time rebuild.
            _SELLABLE_AGENT_SOUL_SESSION_HASHES.clear()
        applied_sha256 = getattr(agent, "_sellable_soul_effective_sha256", None)
        if applied_sha256 is None:
            applied_sha256 = _SELLABLE_AGENT_SOUL_SESSION_HASHES.get(session_key)
        soul_stale = applied_sha256 != marker["effectiveSha256"]

        if soul_stale:
            agent._cached_system_prompt = None
            agent._cached_system_prompt_static = None
            session_db = getattr(agent, "_session_db", None)
            if session_db is None:
                raise RuntimeError("Hermes session database is unavailable for SOUL refresh")
            session_db.update_system_prompt(agent.session_id, None)

        if agent._cached_system_prompt is None:
            restore_or_build_system_prompt(agent, system_message, conversation_history)

        if soul_stale:

            from agent.prompt_builder import load_soul_md
            effective_soul = load_soul_md()
            effective_sha256 = hashlib.sha256(effective_soul.encode("utf-8")).hexdigest()
            if effective_sha256 != marker["effectiveSha256"]:
                agent._cached_system_prompt = None
                agent._cached_system_prompt_static = None
                raise RuntimeError("Hermes loader did not reproduce the proven SOUL")
            if conversation_history:
                # The upstream builder logs and continues on a DB write failure;
                # a continuing Sellable session must fail closed instead.
                session_db.update_system_prompt(
                    agent.session_id,
                    agent._cached_system_prompt,
                )
            applied_sha256 = effective_sha256
        agent._sellable_soul_effective_sha256 = applied_sha256
        _SELLABLE_AGENT_SOUL_SESSION_HASHES[session_key] = applied_sha256
    finally:
        try:
            fcntl.flock(lock_fd, fcntl.LOCK_UN)
        finally:
            os.close(lock_fd)
`;

const MCP_ENDPOINT_META_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: request-scoped Sellable MCP metadata.
def _sellable_agent_call_meta():
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        return None
    from gateway.session_context import get_session_env, session_context_engaged
    if not session_context_engaged():
        return None
    legacy_names = (
        "HERMES_SESSION_PLATFORM",
        "HERMES_SESSION_CHAT_ID",
        "HERMES_SESSION_USER_ID",
        "HERMES_SESSION_MESSAGE_ID",
        "HERMES_CRON_SESSION",
    )
    if any(os.getenv(name) for name in legacy_names):
        raise RuntimeError("Sellable MCP identity must not come from process-global session variables")
    platform = get_session_env("HERMES_SESSION_PLATFORM", "").strip().lower()
    session_id = get_session_env("HERMES_SESSION_ID", "").strip()
    if platform == "api_server":
        if not session_id:
            raise RuntimeError("Sellable endpoint MCP calls require a bound Hermes session")
        return {"sellableEndpoint": {"sessionId": session_id}}
    if not platform and get_session_env("HERMES_CRON_SESSION", "").strip() == "1":
        # Native _CronRunScope clears human routing and sets this task-local flag
        # (Phase207 ADMIN766 smoke). The API still enforces service/workspace gates.
        return None
    if platform != "slack":
        descriptor_path = os.getenv("SELLABLE_AGENT_TURN_DESCRIPTOR_PATH", "")
        if descriptor_path.startswith("/"):
            # The downstream proxy validates and atomically consumes the signed
            # endpoint principal, then retains that verified principal only for
            # this stdio transport. The descriptor file is intentionally absent
            # after the first tool call, so the configured absolute path rather
            # than continued file existence is the bridge handoff contract.
            # Hermes contributes no caller/model authority.
            return None
        raise RuntimeError("Sellable non-Slack MCP calls require a signed endpoint descriptor")
    channel_id = get_session_env("HERMES_SESSION_CHAT_ID", "").strip()
    requester_id = get_session_env("HERMES_SESSION_USER_ID", "").strip()
    message_id = get_session_env("HERMES_SESSION_MESSAGE_ID", "").strip()
    if not channel_id or not requester_id or not message_id or not session_id:
        raise RuntimeError("Sellable MCP calls require a bound Slack requester, channel, message, and session")
    return {
        "sellableAgent": {
            "requesterId": requester_id,
            "channelId": channel_id,
            "providerRequestId": message_id,
            "sessionId": session_id,
        }
    }
`;

const MCP_ENDPOINT_META_HELPER_OPTIONAL_SESSION =
  MCP_ENDPOINT_META_HELPER.replace(
    '    if not channel_id or not requester_id or not message_id or not session_id:\n        raise RuntimeError("Sellable MCP calls require a bound Slack requester, channel, message, and session")\n',
    '    if not channel_id or not requester_id or not message_id:\n        raise RuntimeError("Sellable MCP calls require a bound Slack requester, channel, and message")\n'
  ).replace(
    '    return {\n        "sellableAgent": {\n            "requesterId": requester_id,\n            "channelId": channel_id,\n            "providerRequestId": message_id,\n            "sessionId": session_id,\n        }\n    }\n',
    '    sellable_agent = {\n        "requesterId": requester_id,\n        "channelId": channel_id,\n        "providerRequestId": message_id,\n    }\n    if session_id:\n        sellable_agent["sessionId"] = session_id\n    return {"sellableAgent": sellable_agent}\n'
  );

// Hermes 0.23.5 gives a turn it starts itself (a delegate_task worker's result,
// a background job's notice) no message id, on purpose and in two places:
// gateway/run_notifications.py clears it on the event, and
// gateway/session_automation.py drops it from the admitted envelope ("Never
// anchor the wake to the stale human message"). Up to 0.21.5 that turn carried
// one. Without it every Sellable MCP call of the turn was refused, so a run
// could not save what its Message Drafting worker wrote (internal test agent,
// 2026-12-01, three runs).
//
// The rule: every Sellable MCP call acts for the last customer message of its
// conversation. A call in a turn a person started records that message for the
// conversation (Hermes's session key and the channel). A call in a turn with
// no message continues the recorded one, and is refused when there is none.
// The requester stays whoever Hermes bound for the turn; it is not part of the
// key, because the turn Hermes starts is bound to the person who opened the
// conversation, who is not always the person who wrote last.
//
// Why the customer's LAST message and not the thread's first message, the
// thread id or Hermes's stored session origin: an approval is confirmed only
// from a Slack event other than the one that asked for it
// (src/lib/sellable-agent/integration-approval-pause.ts,
// verifyDistinctSlackApprovalEvent, which compares ids and has no "later"
// test). An older id on a turn nobody typed would pass as a new human event.
// The last customer message is the asking event itself or a real later one.
// A turn with no customer message also never sends an approvalId at all.
//
// Only public names of gateway.session_context are read (get_session_env,
// session_context_engaged, bound_turn_origin and the HERMES_SESSION_* names);
// nothing comes from Hermes's session store or event metadata, which 0.23.5
// does not carry to the turn. The record lives in the gateway process: after a
// restart a turn with no message is refused until the customer writes again.
const MCP_ENDPOINT_META_CONVERSATION_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: the customer message a conversation's Sellable calls act for.
import threading as _sellable_agent_threading
from collections import OrderedDict as _SellableAgentOrderedDict

_SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE = _SellableAgentOrderedDict()
_SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE_LOCK = _sellable_agent_threading.Lock()
_SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE_LIMIT = 4096

def _sellable_agent_turn_origin():
    # What Hermes says started this turn ("delegation_result", "process_notification"),
    # for the log line only: Hermes clears it when a person's message joins the turn.
    try:
        from gateway.session_context import bound_turn_origin
        return str(bound_turn_origin() or "")
    except Exception:
        return ""

def _sellable_agent_conversation_message(session_key, channel_id, message_id):
    conversation = (session_key, channel_id) if session_key and channel_id else None
    continued = ""
    reason = "no session key or channel is bound in this turn"
    if conversation is not None:
        with _SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE_LOCK:
            if message_id:
                _SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE[conversation] = message_id
                _SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE.move_to_end(conversation)
                while len(_SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE) > _SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE_LIMIT:
                    _SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE.popitem(last=False)
            else:
                continued = _SELLABLE_AGENT_LAST_CUSTOMER_MESSAGE.get(conversation, "")
                reason = "no customer message is recorded for this conversation"
    if message_id:
        return message_id
    if continued:
        logger.info(
            "Sellable MCP call in a turn with no customer message (turn origin %s): continues the conversation's last customer message",
            _sellable_agent_turn_origin() or "none",
        )
    else:
        logger.warning(
            "Sellable MCP call in a turn with no customer message (turn origin %s) is refused: %s",
            _sellable_agent_turn_origin() or "none",
            reason,
        )
    return continued

def _sellable_agent_refuse_approval_without_customer_message(arguments):
    # An approval is confirmed by a customer message other than the one that asked
    # for it. A turn with no customer message of its own confirms nothing.
    if not isinstance(arguments, dict) or not str(arguments.get("approvalId") or "").strip():
        return
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        return
    from gateway.session_context import get_session_env, session_context_engaged
    if not session_context_engaged():
        return
    if get_session_env("HERMES_SESSION_PLATFORM", "").strip().lower() != "slack":
        return
    if get_session_env("HERMES_SESSION_MESSAGE_ID", "").strip():
        return
    raise RuntimeError(
        "Sellable approvals are confirmed in a turn the customer starts: ask them to reply in the thread"
    )
`;

const MCP_ENDPOINT_META_HELPER_V0235 =
  MCP_ENDPOINT_META_CONVERSATION_HELPER +
  replaceExactlyOnce(
    replaceExactlyOnce(
      MCP_ENDPOINT_META_HELPER_OPTIONAL_SESSION,
      '    message_id = get_session_env("HERMES_SESSION_MESSAGE_ID", "").strip()\n',
      [
        "    message_id = _sellable_agent_conversation_message(",
        '        get_session_env("HERMES_SESSION_KEY", "").strip(),',
        "        channel_id,",
        '        get_session_env("HERMES_SESSION_MESSAGE_ID", "").strip(),',
        "    )",
        "",
      ].join("\n"),
      "v0.23.5 MCP metadata conversation message"
    ),
    '    if session_id:\n        sellable_agent["sessionId"] = session_id\n    return {"sellableAgent": sellable_agent}\n',
    [
      "    if session_id:",
      '        sellable_agent["sessionId"] = session_id',
      '    thread_id = get_session_env("HERMES_SESSION_THREAD_ID", "").strip()',
      '    if thread_id and len(thread_id) <= 160 and "." in thread_id and thread_id.replace(".", "", 1).isdigit():',
      '        sellable_agent["threadTs"] = thread_id',
      '    return {"sellableAgent": sellable_agent}',
      "",
    ].join("\n"),
    "v0.23.5 MCP metadata Slack thread ts"
  );

const CRON_MCP_ISOLATION_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: cron sessions may use Sellable MCP toolsets.
import json as _sellable_agent_json

def _sellable_agent_strip_cron_mcp(toolsets, cfg):
    # Pass-through, kept for injection-site stability: cron sessions carry no
    # Slack actor meta and the serving API enforces its own actor gates.
    return list(toolsets or [])

_SELLABLE_MANAGED_CUSTOMER_PROGRAM_JOBS = frozenset({
    "sellable-default-campaign-daily-review",
    "sellable-default-campaign-daily-results",
    "sellable-default-campaign-weekly-review",
})

def _sellable_agent_validate_managed_wake_gate(job, script_path, prerun_script):
    if str(job.get("name") or "") not in _SELLABLE_MANAGED_CUSTOMER_PROGRAM_JOBS:
        return None, None
    if not script_path:
        return None, "managed wake gate script is missing"
    if not isinstance(prerun_script, tuple) or len(prerun_script) != 2:
        return None, "managed wake gate did not return a result"
    ran_ok, output = prerun_script
    if not ran_ok:
        return None, "managed wake gate script failed"
    lines = [line.strip() for line in str(output or "").splitlines() if line.strip()]
    if not lines:
        return None, "managed wake gate output is empty"
    try:
        value = _sellable_agent_json.loads(lines[-1])
    except Exception:
        return None, "managed wake gate output is malformed"
    if not isinstance(value, dict) or not isinstance(value.get("wakeAgent"), bool):
        return None, "managed wake gate output is malformed"
    return value["wakeAgent"], None

def _sellable_agent_managed_gate_block(job_id, job_name, reason):
    error = f"Sellable managed wake gate blocked before agent construction: {reason}"
    doc = (
        f"# Cron Job: {job_name}\\n\\n"
        f"**Job ID:** {job_id}\\n"
        "**Status:** BLOCKED\\n\\n"
        f"{error}\\n"
    )
    return (False, doc, "", error), None
`;

const V019_ROOT_TOKEN_FILE_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: root-owned Slack OAuth token-map gate.
_SELLABLE_AGENT_SLACK_TOKEN_FILE_SENTINEL = "__SELLABLE_ROOT_SLACK_TOKEN_FILE__"

def _sellable_agent_slack_token_source(env_value):
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        return env_value
    if env_value:
        raise RuntimeError("SLACK_BOT_TOKEN must not be present in the Sellable runtime environment")
    import stat
    from hermes_constants import get_hermes_home
    token_path = get_hermes_home() / "slack_tokens.json"
    try:
        token_stat = os.lstat(token_path)
    except OSError as exc:
        raise RuntimeError("root-owned Slack token map is unavailable") from exc
    if (
        not stat.S_ISREG(token_stat.st_mode)
        or token_stat.st_uid != 0
        or stat.S_IMODE(token_stat.st_mode) != 0o600
    ):
        raise RuntimeError("root-owned Slack token map has unsafe ownership or mode")
    return _SELLABLE_AGENT_SLACK_TOKEN_FILE_SENTINEL
`;

const V019_SLACK_MULTIPLEX_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: exact Sellable Slack workspace ownership.
_SELLABLE_AGENT_SLACK_TOKEN_FILE_SENTINEL = "__SELLABLE_ROOT_SLACK_TOKEN_FILE__"
_SELLABLE_AGENT_INGRESS_CALLBACK_SHAPE = "sellable.slack.authorize-ingress/v1"

def _sellable_agent_load_slack_tokens(raw_token):
    from hermes_constants import get_hermes_home
    tokens_file = get_hermes_home() / "slack_tokens.json"
    sellable_runtime = os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1"
    if not sellable_runtime:
        bot_tokens = [t.strip() for t in raw_token.split(",") if t.strip()]
        if tokens_file.exists():
            try:
                saved = json.loads(tokens_file.read_text(encoding="utf-8"))
                for team_id, entry in saved.items():
                    tok = entry.get("token", "") if isinstance(entry, dict) else ""
                    if tok and tok not in bot_tokens:
                        bot_tokens.append(tok)
                        team_label = entry.get("team_name", team_id) if isinstance(entry, dict) else team_id
                        logger.info("[Slack] Loaded saved token for workspace %s", team_label)
            except Exception as exc:
                logger.warning("[Slack] Failed to read %s: %s", tokens_file, exc)
        return bot_tokens, {}
    if raw_token != _SELLABLE_AGENT_SLACK_TOKEN_FILE_SENTINEL:
        raise RuntimeError("Sellable Slack bot credentials must come from the root-owned token map")
    import stat
    token_stat = os.lstat(tokens_file)
    if (
        not stat.S_ISREG(token_stat.st_mode)
        or token_stat.st_uid != 0
        or stat.S_IMODE(token_stat.st_mode) != 0o600
    ):
        raise RuntimeError("root-owned Slack token map has unsafe ownership or mode")
    saved = json.loads(tokens_file.read_text(encoding="utf-8"))
    if not isinstance(saved, dict) or not saved:
        raise RuntimeError("root-owned Slack token map is empty or malformed")
    bot_tokens = []
    token_owners = {}
    for team_id, entry in sorted(saved.items()):
        installation_id = entry.get("installationId", "") if isinstance(entry, dict) else ""
        token = entry.get("token", "") if isinstance(entry, dict) else ""
        if not team_id or not installation_id or not isinstance(token, str) or not token.startswith("xoxb-"):
            raise RuntimeError("root-owned Slack token map contains an invalid installation")
        prior = token_owners.get(token)
        owner = {"teamId": str(team_id), "installationId": str(installation_id)}
        if prior is not None and prior != owner:
            raise RuntimeError("one Slack credential is claimed by divergent installations")
        token_owners[token] = owner
        if token not in bot_tokens:
            bot_tokens.append(token)
    return bot_tokens, token_owners

async def _sellable_agent_authorize_slack_route(adapter, team_id, channel_id, thread_id, actor_id):
    callback = getattr(adapter, "_sellable_authorize_ingress", None)
    callback_shape = getattr(adapter, "_sellable_authorize_ingress_shape", None)
    installation_id = getattr(adapter, "_sellable_team_installation_ids", {}).get(team_id)
    if callback_shape != _SELLABLE_AGENT_INGRESS_CALLBACK_SHAPE or not callable(callback):
        raise RuntimeError("Sellable Slack ingress callback is unavailable or unsupported")
    if not team_id or not installation_id or not channel_id or not actor_id:
        raise RuntimeError("Sellable Slack ingress identity is incomplete")
    principal = callback({
        "teamId": str(team_id),
        "installationId": str(installation_id),
        "channelId": str(channel_id),
        "threadId": str(thread_id) if thread_id else None,
        "actorId": str(actor_id),
    })
    import inspect
    if inspect.isawaitable(principal):
        principal = await principal
    required = ("teamId", "installationId", "channelId", "profileId", "revisionId")
    if not isinstance(principal, dict) or any(not principal.get(key) for key in required):
        raise RuntimeError("Sellable Slack ingress callback denied or returned an invalid principal")
    if (
        str(principal["teamId"]) != str(team_id)
        or str(principal["installationId"]) != str(installation_id)
        or str(principal["channelId"]) != str(channel_id)
        or (principal.get("threadId") or None) != (str(thread_id) if thread_id else None)
    ):
        raise RuntimeError("Sellable Slack ingress callback returned a mismatched principal")
    from gateway.profile_routing import bind_sellable_slack_route_principal
    return principal, bind_sellable_slack_route_principal(principal)
`;

const V019_PROFILE_ROUTING_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: task-local exact Sellable route principal.
import contextvars as _sellable_contextvars
import os as _sellable_os

_sellable_slack_route_principal = _sellable_contextvars.ContextVar(
    "sellable_slack_route_principal",
    default=None,
)

def bind_sellable_slack_route_principal(principal):
    if _sellable_os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        raise RuntimeError("Sellable route binding is disabled outside the Sellable runtime")
    return _sellable_slack_route_principal.set(principal)

def reset_sellable_slack_route_principal(token):
    _sellable_slack_route_principal.reset(token)
`;

const V020_PROVIDER_PREFLIGHT_HELPER = `
# ${HERMES_AGENT_BRIDGE_MARKER}: bounded provider health before a gateway model turn.
_SELLABLE_AGENT_PROVIDER_PREFLIGHT_CACHE = {}
_SELLABLE_AGENT_PROVIDER_PREFLIGHT_LOCKS = {
    "openai-codex": threading.Lock(),
    "openrouter": threading.Lock(),
}

def _sellable_agent_preflight_float(env_name, default, minimum, maximum):
    try:
        value = float(os.getenv(env_name, str(default)))
    except (TypeError, ValueError):
        value = default
    return max(minimum, min(maximum, value))

def _sellable_agent_preflight_failure(provider, entry):
    import hashlib
    import httpx

    token = str(getattr(entry, "runtime_api_key", "") or "").strip()
    if not token:
        return 401, {
            "reason": "sellable_provider_preflight_missing_credential",
            "message": "Provider preflight found an empty runtime credential.",
        }

    timeout_seconds = _sellable_agent_preflight_float(
        "SELLABLE_PROVIDER_PREFLIGHT_TIMEOUT_SECONDS", 1.5, 0.5, 5.0
    )
    cache_seconds = _sellable_agent_preflight_float(
        "SELLABLE_PROVIDER_PREFLIGHT_CACHE_SECONDS", 30.0, 5.0, 300.0
    )
    fingerprint = hashlib.sha256(token.encode("utf-8")).hexdigest()
    cache_key = (provider, str(getattr(entry, "id", "") or ""), fingerprint)
    now = time.monotonic()
    if _SELLABLE_AGENT_PROVIDER_PREFLIGHT_CACHE.get(cache_key, 0.0) > now:
        return None

    base_url = str(getattr(entry, "runtime_base_url", "") or "").strip().rstrip("/")
    headers = {
        "Authorization": "Bearer " + token,
        "Accept": "application/json",
    }
    if provider == "openai-codex":
        from hermes_cli.auth import _codex_usage_probe_url, _decode_jwt_claims

        request_urls = {"usage": _codex_usage_probe_url(base_url)}
        headers["User-Agent"] = "codex-cli"
        try:
            claims = _decode_jwt_claims(token)
            auth_claims = claims.get("https://api.openai.com/auth") or {}
            account_id = auth_claims.get("chatgpt_account_id")
            if isinstance(account_id, str) and account_id.strip():
                headers["ChatGPT-Account-Id"] = account_id.strip()
        except Exception:
            pass
    else:
        from hermes_constants import OPENROUTER_BASE_URL

        openrouter_base_url = (base_url or OPENROUTER_BASE_URL).rstrip("/")
        request_urls = {
            "credits": openrouter_base_url + "/credits",
            "key": openrouter_base_url + "/key",
        }

    try:
        with httpx.Client(timeout=timeout_seconds) as client:
            responses = {
                name: client.get(url, headers=headers)
                for name, url in request_urls.items()
            }
    except Exception as exc:
        is_timeout = isinstance(exc, TimeoutError) or "timeout" in type(exc).__name__.lower()
        return (408 if is_timeout else 503), {
            "reason": (
                "sellable_provider_preflight_timeout"
                if is_timeout
                else "sellable_provider_preflight_unreachable"
            ),
            "message": "Provider preflight did not complete inside the bounded health window.",
            "reset_at": time.time() + 30.0,
        }

    payloads = {}
    for name, response in responses.items():
        status_code = int(getattr(response, "status_code", 0) or 0)
        if status_code < 200 or status_code >= 300:
            transient = status_code == 0 or status_code >= 500
            normalized_status = 503 if transient else status_code
            context = {
                "reason": "sellable_provider_preflight_http_rejected",
                "message": "Provider preflight rejected the configured credential.",
            }
            retry_after = str(getattr(response, "headers", {}).get("Retry-After", "") or "")
            try:
                context["reset_at"] = time.time() + float(retry_after)
            except (TypeError, ValueError):
                if transient:
                    context["reset_at"] = time.time() + 30.0
            return normalized_status, context
        try:
            payloads[name] = response.json() or {}
        except Exception:
            payloads[name] = {}

    if provider == "openai-codex":
        payload = payloads.get("usage") or {}
        rate_limit = payload.get("rate_limit") or {}
        exhausted_reset_times = []
        exhausted = (
            rate_limit.get("allowed") is False
            or rate_limit.get("limit_reached") is True
        )
        for window_name in ("primary_window", "secondary_window"):
            window = rate_limit.get(window_name) or {}
            used_percent = window.get("used_percent")
            if isinstance(used_percent, (int, float)) and float(used_percent) >= 100.0:
                exhausted = True
                if window.get("reset_at"):
                    exhausted_reset_times.append(window.get("reset_at"))
        if exhausted:
            context = {
                "reason": "sellable_provider_preflight_quota_exhausted",
                "message": "Codex usage health reports an exhausted account.",
            }
            if exhausted_reset_times:
                # Every exhausted window must recover before the account is usable.
                context["reset_at"] = max(exhausted_reset_times)
            return 429, context
    else:
        credits_data = (payloads.get("credits") or {}).get("data") or {}
        total_credits = credits_data.get("total_credits")
        total_usage = credits_data.get("total_usage")
        if (
            isinstance(total_credits, (int, float))
            and isinstance(total_usage, (int, float))
            and float(total_credits) - float(total_usage) <= 0.0
        ):
            return 402, {
                "reason": "sellable_provider_preflight_credits_exhausted",
                "message": "OpenRouter account health reports no remaining credits.",
            }

        key_data = (payloads.get("key") or {}).get("data") or {}
        remaining = key_data.get("limit_remaining")
        if isinstance(remaining, (int, float)) and float(remaining) <= 0.0:
            context = {
                "reason": "sellable_provider_preflight_quota_exhausted",
                "message": "OpenRouter key health reports no remaining quota.",
            }
            if key_data.get("limit_reset"):
                context["reset_at"] = key_data.get("limit_reset")
            return 402, context

    if len(_SELLABLE_AGENT_PROVIDER_PREFLIGHT_CACHE) >= 128:
        _SELLABLE_AGENT_PROVIDER_PREFLIGHT_CACHE.clear()
    _SELLABLE_AGENT_PROVIDER_PREFLIGHT_CACHE[cache_key] = time.monotonic() + cache_seconds
    return None

def _sellable_agent_preflight_provider(provider):
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        return
    try:
        from agent.credential_pool import load_pool

        pool = load_pool(provider)
        entry_count = len(pool.entries())
    except Exception:
        logger.warning("Sellable provider preflight could not load provider=%s", provider)
        return
    if entry_count == 0:
        return

    lock = _SELLABLE_AGENT_PROVIDER_PREFLIGHT_LOCKS[provider]
    with lock:
        attempted = set()
        while len(attempted) < entry_count:
            entry = pool.select()
            if entry is None or entry.id in attempted:
                break
            attempted.add(entry.id)
            failure = _sellable_agent_preflight_failure(provider, entry)
            if failure is None:
                return
            status_code, error_context = failure
            logger.warning(
                "Sellable provider preflight skipped an unhealthy %s credential (%s)",
                provider,
                error_context.get("reason", "unknown"),
            )
            try:
                pool.mark_exhausted_and_rotate(
                    status_code=status_code,
                    error_context=error_context,
                    credential_id=entry.id,
                )
            except Exception:
                logger.warning(
                    "Sellable provider preflight could not persist provider=%s cooldown",
                    provider,
                )
                return

def _sellable_agent_preflight_model_providers():
    if os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":
        return
    with concurrent.futures.ThreadPoolExecutor(
        max_workers=2,
        thread_name_prefix="sellable-provider-preflight",
    ) as executor:
        futures = [
            executor.submit(_sellable_agent_preflight_provider, provider)
            for provider in ("openai-codex", "openrouter")
        ]
        for future in futures:
            try:
                future.result()
            except Exception:
                logger.warning("Sellable provider preflight worker failed")
`;

// Round 11 of the first-campaign onboarding (2026-11-20): the Sellable
// render tool posts the customer's message itself (`post: true`), because on
// GPT-6.2 the agent rendered the exact sample and then posted its own version
// of it. The turn's final text right after that post is the model's copy or
// summary of a message the customer already has, so the gateway does not
// deliver it, and the silence marker (or an empty reply) after a post is
// accepted on a user turn instead of the "returned only a silence marker"
// warning. Only this turn's own render-tool results with the receipt count.
const POSTED_REPLY_HELPER_NEEDLE = `_UNEXPECTED_SILENCE_REPLY = (
    "⚠️ The model returned only a silence marker for a message that needed a reply. "
    "Try again or rephrase."
)
`;
const POSTED_REPLY_HELPER = String.raw`${POSTED_REPLY_HELPER_NEEDLE}
# ${HERMES_AGENT_BRIDGE_MARKER}: a customer never sees this runtime notice. A silence marker on
# any turn stays silent (empty text is never posted; gateway/run_startup.py reads this too).
_UNEXPECTED_SILENCE_REPLY = ""

# ${HERMES_AGENT_BRIDGE_MARKER}: the Sellable render tool posted this turn's reply itself.
import re as _sellable_agent_re
_SELLABLE_AGENT_POSTED_RECEIPT = _sellable_agent_re.compile(r"\bSELLABLE_POSTED kind=[a-z_]+ ts=\d")


def _sellable_agent_own_nudge(message):
    """A user-role row Hermes wrote itself inside a turn: its "[System: ...]" re-prompts
    (reasoning-only reply, acknowledgment, fragment) and its flagged scaffolding. It is not a
    customer message, so it does not start a new turn."""
    content = message.get("content")
    return (isinstance(content, str) and content.startswith("[System: ")) or any(
        value is True and str(key).endswith(("_synthetic", "_nudge")) for key, value in message.items()
    )


def _sellable_agent_turn_posted(agent_result):
    """Whether the Sellable render tool posted a message itself in this turn.

    "last": its post is the turn's last tool result, or only customer_program bookkeeping
    followed it, so a final text is the model's own copy or summary of a message the customer
    already has. "earlier": a post happened before other tool work. None: no post, or a
    failed turn. Only this turn counts (after its last customer message; a re-prompt Hermes
    adds mid-turn does not start a new one), and only the render tool's own results, never
    text another tool returned.
    """
    try:
        if not isinstance(agent_result, dict) or agent_result.get("failed"):
            return None
        messages = agent_result.get("messages") or []
        start = 0
        for index in range(len(messages) - 1, -1, -1):
            message = messages[index]
            if (
                isinstance(message, dict)
                and message.get("role") == "user"
                and not _sellable_agent_own_nudge(message)
            ):
                start = index + 1
                break
        posted = False
        after_post = []
        for message in messages[start:]:
            if not isinstance(message, dict) or message.get("role") != "tool":
                continue
            name = str(message.get("name") or message.get("tool_name") or "")
            content = message.get("content")
            text = content if isinstance(content, str) else json.dumps(content, default=str)
            if (
                name.startswith("mcp_")
                and "sellable" in name
                and name.endswith("render_first_campaign_message")
                and _SELLABLE_AGENT_POSTED_RECEIPT.search(text or "")
            ):
                posted = True
                after_post = []
            elif posted:
                after_post.append(name)
        if not posted:
            return None
        if all(name.endswith("customer_program") for name in after_post):
            return "last"
        return "earlier"
    except Exception:
        return None
`;
const POSTED_REPLY_SHAPE_NEEDLE = `        _intentional_silence = self._is_intentional_silence(agent_result, response)
        # A queued (/queue) chain's TERMINAL turn owns the silence verdict, not the event that
        # opened the chain: an internal follow-up may go silent, a human one must not.
        _silence_kind = agent_result.get("queued_terminal_display_kind", persist_user_display_kind)
        if _intentional_silence and not is_machinery_display_kind(_silence_kind):
`;
const POSTED_REPLY_SHAPE = `        _intentional_silence = self._is_intentional_silence(agent_result, response)
        # ${HERMES_AGENT_BRIDGE_MARKER}: when the Sellable render tool posted this turn's reply
        # itself, a final text written right after it is never delivered (it would double the
        # message), and the silence marker or an empty reply is accepted on a user turn.
        _sellable_posted = _sellable_agent_turn_posted(agent_result)
        if _sellable_posted and not _intentional_silence and (
            _sellable_posted == "last"
            or not str(response or "").strip()
            or response == "(empty)"
        ):
            logger.info(
                "Sellable posted the reply itself: not delivering the %d-char final text of session %s",
                len(str(response or "")), session_entry.session_id,
            )
            _intentional_silence = True
        # A queued (/queue) chain's TERMINAL turn owns the silence verdict, not the event that
        # opened the chain: an internal follow-up may go silent, a human one must not.
        _silence_kind = agent_result.get("queued_terminal_display_kind", persist_user_display_kind)
        # ${HERMES_AGENT_BRIDGE_MARKER}: silence on a user turn stays silent. The "returned only a
        # silence marker" notice told a customer to rephrase after the agent had already answered
        # (a link-preview copy of their own message, 2026-11-24). Logged, never posted.
        if _intentional_silence and not is_machinery_display_kind(_silence_kind) and not _sellable_posted:
            logger.warning(
                "Sellable: silence marker on a user turn stays silent: session %s",
                session_entry.session_id,
            )
        if False:
`;

// The same rule on the queued-follow-up path: a customer message that arrives
// while the turn writes its final reply (confirmation pass, 2026-11-20: "yes"
// 3 seconds before the plan turn ended) makes Hermes deliver that turn's
// reply before the queued message runs, with its own silence check, which
// turned the [SILENT] after a posted plan into "The model returned only a
// silence marker".
const POSTED_REPLY_QUEUED_NEEDLE = `        # Same silence predicate as the normal path, else this branch leaks the literal marker.
        if self._is_intentional_silence(_delivery_result, first_response):
            if is_machinery_display_kind(turn_ctx.persist_user_display_kind):
`;
const POSTED_REPLY_QUEUED = `        # Same silence predicate as the normal path, else this branch leaks the literal marker.
        # ${HERMES_AGENT_BRIDGE_MARKER}: the same Sellable posted-reply rule as the normal path.
        _sellable_posted = _sellable_agent_turn_posted(
            result if isinstance(result, dict) and result.get("messages") else _delivery_result
        )
        if _sellable_posted and not self._is_intentional_silence(_delivery_result, first_response) and (
            _sellable_posted == "last" or not str(first_response or "").strip() or first_response == "(empty)"
        ):
            logger.info(
                "Queued follow-up for session %s: Sellable posted the reply itself; not delivering the %d-char final text.",
                session_key or "?", len(str(first_response or "")),
            )
            first_response = ""
        elif self._is_intentional_silence(_delivery_result, first_response):
            # ${HERMES_AGENT_BRIDGE_MARKER}: silence on any turn stays silent, never the runtime notice.
            if True:
`;

// Codex models also deliver a commentary message live, while the response
// still streams (agent/stream_delivery.py _fire_streamed_codex_commentary),
// before the turn-round check above runs (confirmation pass, 2026-11-20: the
// fallback gpt-6.2-sol posted "I'm checking the LinkedIn post list. I'll check
// its status again in about a minute." after the On it post). The live path
// applies the same rule from the turn's messages so far.
const POSTED_LIVE_COMMENTARY_NEEDLE = `        if getattr(self, "interim_assistant_callback", None) is None or not isinstance(text, str):
            return
        visible = self._visible_commentary(text)
        if not visible or visible == "(empty)" or self._interim_text_was_delivered(visible):
            return
        self._deliver_interim(visible, already_streamed=False, record=[visible])
`;
const POSTED_LIVE_COMMENTARY = `        if getattr(self, "interim_assistant_callback", None) is None or not isinstance(text, str):
            return
        # ${HERMES_AGENT_BRIDGE_MARKER}: no live note after a Sellable post in this customer turn.
        try:
            from agent.turn_tool_round import _sellable_agent_quiet_commentary

            if _sellable_agent_quiet_commentary({}, getattr(self, "_session_messages", None)):
                logger.info(
                    "Sellable posts this turn's messages itself: not showing %d chars of live commentary",
                    len(text.strip()),
                )
                return
        except Exception:
            logger.debug("Sellable live-commentary check failed", exc_info=True)
        visible = self._visible_commentary(text)
        if not visible or visible == "(empty)" or self._interim_text_was_delivered(visible):
            return
        self._deliver_interim(visible, already_streamed=False, record=[visible])
`;

// The same round: a note the model writes next to a tool call is posted to
// Slack as its own message (interim commentary). GPT-6.2 wrote each
// first-campaign message as such a note (round 10, Nov 19), so next to a post
// by the Sellable render tool, or after one in the same customer turn, the
// note would double the message or add one the customer never needed. A
// customer message during the run (a steer) does not open a new turn here.
const POSTED_COMMENTARY_HELPER_NEEDLE = `logger = logging.getLogger("agent.conversation_loop")
`;
const POSTED_COMMENTARY_HELPER = String.raw`${POSTED_COMMENTARY_HELPER_NEEDLE}

# ${HERMES_AGENT_BRIDGE_MARKER}: the Sellable render tool posts the customer's messages itself.
import json as _sellable_agent_json
import re as _sellable_agent_re
_SELLABLE_AGENT_RENDER_TOOL = "render_first_campaign_message"
_SELLABLE_AGENT_POSTED_RECEIPT = _sellable_agent_re.compile(r"\bSELLABLE_POSTED kind=[a-z_]+ ts=\d")


def _sellable_agent_arguments(value):
    if isinstance(value, str):
        try:
            value = _sellable_agent_json.loads(value)
        except Exception:
            return {}
    return value if isinstance(value, dict) else {}


def _sellable_agent_posting_call(assistant_msg):
    """True when this assistant message asks the Sellable render tool to post (post: true),
    directly or through the deferred-tool tool_call wrapper."""
    for call in (assistant_msg or {}).get("tool_calls") or []:
        function = call.get("function") if isinstance(call, dict) else getattr(call, "function", None)
        if isinstance(function, dict):
            name, raw = str(function.get("name") or ""), function.get("arguments")
        else:
            name, raw = str(getattr(function, "name", "") or ""), getattr(function, "arguments", None)
        arguments = _sellable_agent_arguments(raw)
        inner = [arguments] if name.endswith(_SELLABLE_AGENT_RENDER_TOOL) else []
        if name == "tool_call":
            inner = [
                _sellable_agent_arguments(item.get("arguments"))
                for item in (arguments.get("calls") or [])
                if isinstance(item, dict) and str(item.get("name") or "").endswith(_SELLABLE_AGENT_RENDER_TOOL)
            ]
        if any(item.get("post") is True for item in inner):
            return True
    return False


def _sellable_agent_own_nudge(message):
    """A user-role row Hermes wrote itself inside a turn: its "[System: ...]" re-prompts
    (reasoning-only reply, acknowledgment, fragment) and its flagged scaffolding. It is not a
    customer message, so it does not start a new turn."""
    content = message.get("content")
    return (isinstance(content, str) and content.startswith("[System: ")) or any(
        value is True and str(key).endswith(("_synthetic", "_nudge")) for key, value in message.items()
    )


def _sellable_agent_quiet_commentary(assistant_msg, messages):
    """Commentary next to a Sellable post, or after one in this customer turn, is not shown."""
    try:
        if _sellable_agent_posting_call(assistant_msg):
            return True
        for message in reversed(messages or []):
            if not isinstance(message, dict):
                continue
            if (
                message.get("role") == "user"
                and message.get("display_kind") != "steer"
                and not _sellable_agent_own_nudge(message)
            ):
                return False
            if message.get("role") != "tool":
                continue
            name = str(message.get("name") or message.get("tool_name") or "")
            content = message.get("content")
            text = content if isinstance(content, str) else _sellable_agent_json.dumps(content, default=str)
            if name.endswith(_SELLABLE_AGENT_RENDER_TOOL) and _SELLABLE_AGENT_POSTED_RECEIPT.search(text or ""):
                return True
        return False
    except Exception:
        return False
`;
const POSTED_COMMENTARY_EMIT_NEEDLE = `    if not duplicate_previous_interim:
        agent._emit_interim_assistant_message(assistant_msg)
`;
const POSTED_COMMENTARY_EMIT = `    # ${HERMES_AGENT_BRIDGE_MARKER}: no note next to or after a Sellable post in this turn.
    if not duplicate_previous_interim and _sellable_agent_quiet_commentary(assistant_msg, messages):
        with suppress(Exception):
            _sellable_quiet_chars = sum(
                len(str(part).strip())
                for part in agent._extract_codex_interim_visible_parts(assistant_msg)
                or [assistant_msg.get("content") or ""]
            )
            if _sellable_quiet_chars:
                logger.info(
                    "Sellable posts this turn's messages itself: not showing %d chars of commentary",
                    _sellable_quiet_chars,
                )
    elif not duplicate_previous_interim:
        agent._emit_interim_assistant_message(assistant_msg)
`;

const POSTED_REPLY_HELPER_V0235_NEEDLE = `def _unexpected_silence_reply() -> str:
    """Reply when the model returned only a silence marker for a message that needed an answer."""
    return t("gateway.errors.unexpected_silence")
`;
const POSTED_REPLY_HELPER_V0235 = String.raw`def _unexpected_silence_reply() -> str:
    """Reply when the model returned only a silence marker for a message that needed an answer."""
    # ${HERMES_AGENT_BRIDGE_MARKER}: a customer never sees this runtime notice. A silence marker on
    # any turn stays silent (empty text is never posted; gateway/run_startup.py reads this too).
    return ""


# ${HERMES_AGENT_BRIDGE_MARKER}: the Sellable render tool posted this turn's reply itself.
import re as _sellable_agent_re
_SELLABLE_AGENT_POSTED_RECEIPT = _sellable_agent_re.compile(r"\bSELLABLE_POSTED kind=[a-z_]+ ts=\d")


def _sellable_agent_own_nudge(message):
    """A user-role row Hermes wrote itself inside a turn: its "[System: ...]" re-prompts
    (reasoning-only reply, acknowledgment, fragment) and its flagged scaffolding. It is not a
    customer message, so it does not start a new turn."""
    content = message.get("content")
    return (isinstance(content, str) and content.startswith("[System: ")) or any(
        value is True and str(key).endswith(("_synthetic", "_nudge")) for key, value in message.items()
    )


def _sellable_agent_turn_posted(agent_result):
    """Whether the Sellable render tool posted a message itself in this turn.

    "last": its post is the turn's last tool result, or only customer_program bookkeeping
    followed it, so a final text is the model's own copy or summary of a message the customer
    already has. "earlier": a post happened before other tool work. None: no post, or a
    failed turn. Only this turn counts (after its last customer message; a re-prompt Hermes
    adds mid-turn does not start a new one), and only the render tool's own results, never
    text another tool returned.
    """
    try:
        if not isinstance(agent_result, dict) or agent_result.get("failed"):
            return None
        messages = agent_result.get("messages") or []
        start = 0
        for index in range(len(messages) - 1, -1, -1):
            message = messages[index]
            if (
                isinstance(message, dict)
                and message.get("role") == "user"
                and not _sellable_agent_own_nudge(message)
            ):
                start = index + 1
                break
        posted = False
        after_post = []
        for message in messages[start:]:
            if not isinstance(message, dict) or message.get("role") != "tool":
                continue
            name = str(message.get("name") or message.get("tool_name") or "")
            content = message.get("content")
            text = content if isinstance(content, str) else json.dumps(content, default=str)
            if (
                name.startswith("mcp_")
                and "sellable" in name
                and name.endswith("render_first_campaign_message")
                and _SELLABLE_AGENT_POSTED_RECEIPT.search(text or "")
            ):
                posted = True
                after_post = []
            elif posted:
                after_post.append(name)
        if not posted:
            return None
        if all(name.endswith("customer_program") for name in after_post):
            return "last"
        return "earlier"
    except Exception:
        return None
`;
const POSTED_REPLY_QUEUED_V0235_NEEDLE = `        # Same silence predicate as the normal path, else this branch leaks the literal marker.
        _silent = False
        if self._is_intentional_silence(_delivery_result, first_response):
            _silent = self._silence_allowed_for_source(
                turn_ctx.source, turn_ctx.persist_user_display_kind, turn_ctx.reply_expected, _delivery_result)
            if _silent:
`;
const POSTED_REPLY_QUEUED_V0235 = `        # Same silence predicate as the normal path, else this branch leaks the literal marker.
        # ${HERMES_AGENT_BRIDGE_MARKER}: the same Sellable posted-reply rule as the normal path.
        _silent = False
        _sellable_posted = _sellable_agent_turn_posted(
            result if isinstance(result, dict) and result.get("messages") else _delivery_result
        )
        if _sellable_posted and not self._is_intentional_silence(_delivery_result, first_response) and (
            _sellable_posted == "last" or not str(first_response or "").strip() or first_response == "(empty)"
        ):
            logger.info(
                "Queued follow-up for session %s: Sellable posted the reply itself; not delivering the %d-char final text.",
                session_key or "?", len(str(first_response or "")),
            )
            first_response = ""
            _silent = True
        elif self._is_intentional_silence(_delivery_result, first_response):
            # ${HERMES_AGENT_BRIDGE_MARKER}: silence on any turn stays silent, never the runtime notice.
            _silent = True
            if _silent:
`;
const POSTED_REPLY_SHAPE_V0235_NEEDLE = `        _intentional_silence = self._is_intentional_silence(agent_result, response)
        # A queued (/queue) chain's TERMINAL turn owns the silence verdict, not the event that
        # opened the chain: an internal follow-up, or a message not addressed to the bot, may go
        # silent; any other human one must not.
        from gateway.response_filters import is_machinery_display_kind, strip_trailing_silence_marker
        from gateway.run_turn import _unexpected_silence_reply
        _silence_kind = agent_result.get("queued_terminal_display_kind", persist_user_display_kind)
        _silence_reply_expected = agent_result.get("queued_terminal_reply_expected", reply_expected)
        _silence_source = agent_result.get("queued_terminal_source", source)
        # A bot peer expects no reply even when it addressed us (a DM, a mention): its message may end
        # the exchange in silence, and the visible fallback would be runtime text it answers (#137027).
        if not automatic_notices_allowed(_silence_source):
            _silence_reply_expected = False
        if _intentional_silence and not self._silence_allowed_for_source(
            _silence_source, _silence_kind, _silence_reply_expected, agent_result,
        ):
`;
const POSTED_REPLY_SHAPE_V0235 = `        _intentional_silence = self._is_intentional_silence(agent_result, response)
        # A queued (/queue) chain's TERMINAL turn owns the silence verdict, not the event that
        # opened the chain: an internal follow-up, or a message not addressed to the bot, may go
        # silent; any other human one must not.
        from gateway.response_filters import is_machinery_display_kind, strip_trailing_silence_marker
        from gateway.run_turn import _sellable_agent_turn_posted, _unexpected_silence_reply
        # ${HERMES_AGENT_BRIDGE_MARKER}: when the Sellable render tool posted this turn's reply
        # itself, a final text written right after it is never delivered (it would double the
        # message), and the silence marker or an empty reply is accepted on a user turn.
        _sellable_posted = _sellable_agent_turn_posted(agent_result)
        if _sellable_posted and not _intentional_silence and (
            _sellable_posted == "last"
            or not str(response or "").strip()
            or response == "(empty)"
        ):
            logger.info(
                "Sellable posted the reply itself: not delivering the %d-char final text of session %s",
                len(str(response or "")), session_entry.session_id,
            )
            _intentional_silence = True
        _silence_kind = agent_result.get("queued_terminal_display_kind", persist_user_display_kind)
        _silence_reply_expected = agent_result.get("queued_terminal_reply_expected", reply_expected)
        _silence_source = agent_result.get("queued_terminal_source", source)
        # A bot peer expects no reply even when it addressed us (a DM, a mention): its message may end
        # the exchange in silence, and the visible fallback would be runtime text it answers (#137027).
        if not automatic_notices_allowed(_silence_source):
            _silence_reply_expected = False
        # ${HERMES_AGENT_BRIDGE_MARKER}: silence on a user turn stays silent. Logged, never posted.
        if (
            _intentional_silence
            and not self._silence_allowed_for_source(
                _silence_source, _silence_kind, _silence_reply_expected, agent_result,
            )
            and not _sellable_posted
        ):
            logger.warning(
                "Sellable: silence marker on a user turn stays silent: session %s",
                session_entry.session_id,
            )
        if False:
`;

// Customers talk to "Sellable Agent"; Hermes' own chat text names Hermes and
// points at terminal commands they cannot run. Each kind applies its base
// patch (if the file already has one) and then these exact replacements.
const SELLABLE_AGENT_COPY_PATCHES = {
  "gateway-posted-reply-v0235": {
    base: null,
    replacements: [
      [POSTED_REPLY_HELPER_V0235_NEEDLE, POSTED_REPLY_HELPER_V0235],
      [POSTED_REPLY_QUEUED_V0235_NEEDLE, POSTED_REPLY_QUEUED_V0235],
    ],
  },
  "gateway-posted-reply-persistence-v0235": {
    base: null,
    replacements: [[POSTED_REPLY_SHAPE_V0235_NEEDLE, POSTED_REPLY_SHAPE_V0235]],
  },
  "turn-failure-copy-v0235": {
    base: "turn-failure-copy-v0215",
    replacements: [
      ["so Hermes didn't run it.", "so Sellable Agent didn't run it."],
    ],
  },
  "stream-delivery-posted-commentary-v0215": {
    replacements: [[POSTED_LIVE_COMMENTARY_NEEDLE, POSTED_LIVE_COMMENTARY]],
  },
  "tool-round-posted-commentary-v0215": {
    replacements: [
      [POSTED_COMMENTARY_HELPER_NEEDLE, POSTED_COMMENTARY_HELPER],
      [POSTED_COMMENTARY_EMIT_NEEDLE, POSTED_COMMENTARY_EMIT],
    ],
  },
  "gateway-posted-reply-v0215": {
    base: "gateway-first-contact-v0213",
    replacements: [
      [POSTED_REPLY_HELPER_NEEDLE, POSTED_REPLY_HELPER],
      [POSTED_REPLY_SHAPE_NEEDLE, POSTED_REPLY_SHAPE],
      [POSTED_REPLY_QUEUED_NEEDLE, POSTED_REPLY_QUEUED],
    ],
  },
  "gateway-session-copy-v0215": {
    base: "gateway-session-v021",
    replacements: [
      [
        'return lead + " If this keeps happening, run `hermes doctor` on the host."',
        'return lead + " If this keeps happening, let the Sellable team know."',
      ],
      [
        '"or ask whoever runs this bot to run `hermes doctor` on the host."),',
        '"or let the Sellable team know."),',
      ],
      [
        'if it keeps happening, run "\n                                         "`hermes doctor` on the host."),',
        'if it keeps happening, let the "\n                                         "Sellable team know."),',
      ],
      [
        '"if it persists, run `hermes doctor` on the host."),',
        '"if it persists, let the Sellable team know."),',
      ],
      ['"⚠️ Hermes could not reach', '"⚠️ Sellable Agent could not reach'],
      [
        'if it persists, run `hermes doctor` on the host."))',
        'if it persists, let the Sellable team know."))',
      ],
    ],
  },
  "gateway-provider-preflight-copy-v0215": {
    base: "gateway-provider-preflight-v021",
    replacements: [
      [
        'return "Hermes is working\\n"',
        'return "Sellable Agent is working\\n"',
      ],
      ['title="Hermes is working",', 'title="Sellable Agent is working",'],
      [
        '"failing, run `hermes doctor` on the host."),',
        '"failing, let the Sellable team know."),',
      ],
    ],
  },
  "turn-failure-copy-v0215": {
    base: null,
    replacements: [
      [
        'run `hermes doctor` and share the error details."',
        'let the Sellable team know."',
      ],
      [
        '"(or `hermes model` in a terminal).{prefix_hint}"',
        '"(or ask the Sellable team).{prefix_hint}"',
      ],
      [
        'if it keeps happening, run `hermes doctor`."',
        'if it keeps happening, let the Sellable team know."',
      ],
      ["\"Hermes couldn't verify", "\"Sellable Agent couldn't verify"],
      [
        'or run `hermes setup` in a terminal."',
        'or ask the Sellable team to update it."',
      ],
      [
        "and Hermes couldn't shrink \"",
        "and Sellable Agent couldn't shrink \"",
      ],
      [
        "\"Hermes didn't run the incomplete action.",
        "\"Sellable Agent didn't run the incomplete action.",
      ],
      ['"Hermes hit repeated errors', '"Sellable Agent hit repeated errors'],
      [
        '"Hermes was shutting down and stopped this turn.',
        '"Sellable Agent was shutting down and stopped this turn.',
      ],
      [
        "\"Hermes couldn't shrink it enough automatically.",
        "\"Sellable Agent couldn't shrink it enough automatically.",
      ],
      ['window Hermes knows for "', 'window Sellable Agent knows for "'],
      [
        "smaller window than Hermes assumes.",
        "smaller window than Sellable Agent assumes.",
      ],
      [
        '"Hermes hit an internal error',
        '"Sellable Agent hit an internal error',
      ],
      [
        '"a backup provider with `hermes fallback add`."',
        '"a backup model through the Sellable team."',
      ],
      [
        'f"add a backup provider with `hermes fallback add`.\\n\\nProvider said: {summary}"',
        'f"ask the Sellable team to add a backup model.\\n\\nProvider said: {summary}"',
      ],
    ],
  },
  "gateway-busy-copy-v0215": {
    base: null,
    replacements: [
      [
        'return "Hermes wasn\'t paused."',
        'return "Sellable Agent wasn\'t paused."',
      ],
      [
        'f"⏸️ Hermes is already paused{suffix}.',
        'f"⏸️ Sellable Agent is already paused{suffix}.',
      ],
    ],
  },
  "slash-commands-copy-v0215": {
    base: null,
    replacements: [
      [
        '"Messages will be queued for the next turn while Hermes is busy."',
        '"Messages will be queued for the next turn while Sellable Agent is busy."',
      ],
      [
        '"Messages will interrupt the current run while Hermes is busy."',
        '"Messages will interrupt the current run while Sellable Agent is busy."',
      ],
    ],
  },
};

function applySellableAgentCopy(patchKind, { base, replacements }, source) {
  let patched = base
    ? patchHermesSourceFile(base, source)
    : `# ${HERMES_AGENT_BRIDGE_MARKER}: Sellable Agent customer-facing copy.\n${source}`;
  for (const [needle, replacement] of replacements) {
    patched = replaceExactlyOnce(
      patched,
      needle,
      replacement,
      `${patchKind} copy`
    );
  }
  return patched;
}

export function patchHermesSourceFile(patchKind, source) {
  if (typeof source !== "string") {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_INVALID_SOURCE",
      "Hermes source must be UTF-8 text"
    );
  }
  if (source.includes(HERMES_AGENT_BRIDGE_MARKER)) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_ALREADY_PATCHED_SOURCE",
      "Refusing to transform source that already contains the bridge marker",
      { patchKind }
    );
  }
  if (Object.hasOwn(SELLABLE_AGENT_COPY_PATCHES, patchKind)) {
    return applySellableAgentCopy(
      patchKind,
      SELLABLE_AGENT_COPY_PATCHES[patchKind],
      source
    );
  }

  if (patchKind === "api-opener-stage-v021") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n
# ${HERMES_AGENT_BRIDGE_MARKER}: reserved, narrowing-only opener writer/editor sessions.
def _sellable_opener_output_format(model_options):
    import hashlib
    import json
    options = model_options if isinstance(model_options, dict) else {}
    if "sellable_opener_output_contract" not in options:
        return None
    contract = options["sellable_opener_output_contract"]
    def reject():
        raise RuntimeError("sellable_opener_output_contract_rejected")
    if not isinstance(contract, dict) or set(contract) != {"schemaVersion", "schema", "nativeProposalSchemaDigest"}:
        reject()
    schema = contract["schema"]
    if (
        contract["schemaVersion"] != "sellable-opener-output-contract/v1"
        or not isinstance(schema, dict)
        or set(schema) != {"type", "additionalProperties", "required", "properties"}
        or schema.get("type") != "object" or schema.get("additionalProperties") is not False
        or schema.get("required") != ["proposal"]
        or not isinstance(schema.get("properties"), dict) or set(schema["properties"]) != {"proposal"}
    ):
        reject()
    encoded = json.dumps(schema, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
    proposal = json.dumps(schema["properties"]["proposal"], sort_keys=True, separators=(",", ":"), ensure_ascii=False)
    if len(encoded.encode()) > 32768 or hashlib.sha256(proposal.encode()).hexdigest() != contract["nativeProposalSchemaDigest"]:
        reject()
    return {"type": "json_schema", "name": "sellable_opener_proposal", "strict": True, "schema": schema}

def _sellable_opener_json_schema(output_format):
    # Chat-completions spelling of the same strict format (OpenRouter response_format).
    return {"type": "json_schema", "json_schema": {"name": output_format["name"], "strict": True, "schema": output_format["schema"]}}

def _sellable_opener_stage_kwargs(session_id, agent_kwargs, model_options=None):
    import os
    import re
    runtime = os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1"
    stage = runtime and re.fullmatch(r"sellable_opener_stage_[a-f0-9]{32}", str(session_id or "")) is not None
    endpoint = runtime and re.fullmatch(r"sellable_endpoint_[a-f0-9]{32}", str(session_id or "")) is not None
    if stage:
        agent_kwargs.update(
            enabled_toolsets=[], skip_memory=True, skip_background_review=True,
            skip_context_files=True, load_soul_identity=False, checkpoints_enabled=False,
        )
        output_format = _sellable_opener_output_format(model_options)
        if output_format is not None:
            overrides = dict(agent_kwargs.get("request_overrides") or {})
            extra_body = dict(overrides.get("extra_body") or {})
            if "text" in overrides or "text" in extra_body or "response_format" in overrides or "response_format" in extra_body:
                raise RuntimeError("sellable_opener_output_format_conflict")
            extra_body["text"] = {"format": output_format}
            overrides["extra_body"] = extra_body
            agent_kwargs.update(request_overrides=overrides, fallback_model=None)
    elif endpoint and _sellable_opener_output_format(model_options) is not None:
        # A per-run model's endpoint session keeps its tools; only its output
        # is held to the strict schema, routed to providers that enforce it.
        output_format = _sellable_opener_output_format(model_options)
        overrides = dict(agent_kwargs.get("request_overrides") or {})
        extra_body = dict(overrides.get("extra_body") or {})
        if "text" in overrides or "text" in extra_body or "response_format" in overrides or "response_format" in extra_body:
            raise RuntimeError("sellable_opener_output_format_conflict")
        extra_body["response_format"] = _sellable_opener_json_schema(output_format)
        overrides["extra_body"] = extra_body
        agent_kwargs.update(request_overrides=overrides, fallback_model=None, provider_require_parameters=True)
    elif isinstance(model_options, dict) and "sellable_opener_output_contract" in model_options:
        raise RuntimeError("sellable_opener_output_contract_session_rejected")
    return stage

def _sellable_assert_opener_stage_tools(stage, agent, model_options=None):
    if stage and (agent.tools or agent.valid_tool_names):
        raise RuntimeError("sellable_opener_stage_tools_not_empty")
    output_format = _sellable_opener_output_format(model_options)
    if output_format is None:
        return
    import copy
    if stage:
        if agent.api_mode != "codex_responses" or agent.provider != "openai-codex" or agent._fallback_chain:
            raise RuntimeError("sellable_opener_output_format_unsupported")
        expected_key, expected_value = "text", {"format": copy.deepcopy(output_format)}
    else:
        if agent.api_mode != "chat_completions" or agent.provider != "openrouter" or agent._fallback_chain:
            raise RuntimeError("sellable_opener_output_format_unsupported")
        expected_key, expected_value = "response_format", _sellable_opener_json_schema(copy.deepcopy(output_format))
    selected = (agent.model, agent.provider, agent.api_mode)
    build_api_kwargs = agent._build_api_kwargs
    def strict_build_api_kwargs(*args, **kwargs):
        built = build_api_kwargs(*args, **kwargs)
        extra_body = built.get("extra_body") or {}
        if (
            (agent.model, agent.provider, agent.api_mode) != selected or agent._fallback_chain
            or extra_body.get(expected_key) != expected_value
            or (not stage and (extra_body.get("provider") or {}).get("require_parameters") is not True)
        ):
            raise RuntimeError("sellable_opener_output_format_not_enforced")
        return built
    agent._build_api_kwargs = strict_build_api_kwargs
`,
      "v0.21 isolated opener stage helpers"
    );
    patched = replaceExactlyOnce(
      patched,
      "        agent = AIAgent(**agent_kwargs)\n",
      "        _sellable_opener_stage = _sellable_opener_stage_kwargs(session_id, agent_kwargs, model_options)\n        agent = AIAgent(**agent_kwargs)\n        _sellable_assert_opener_stage_tools(_sellable_opener_stage, agent, model_options)\n",
      "v0.21 isolated opener agent construction"
    );
    return patched;
  }

  if (patchKind === "keyless-exa-full-page-v0235") {
    // The free Exa reader answers with about 3,000 characters of a page unless
    // it is asked for more, and v0.23.5 never asks, so web_extract's own
    // char_limit has nothing left to keep. Ask for the whole page: web_extract
    // then cuts it to char_limit and stores the full text, as it does for
    // every other reader.
    return replaceExactlyOnce(
      source,
      '            text = mcp_call(EXA_MCP_URL, "web_fetch_exa", {"urls": [url]})\n',
      [
        `            # ${HERMES_AGENT_BRIDGE_MARKER}: read the whole page.`,
        '            text = mcp_call(EXA_MCP_URL, "web_fetch_exa", {"urls": [url], "maxCharacters": 100000})',
        "",
      ].join("\n"),
      "keyless Exa full page"
    );
  }

  if (patchKind === "terminal-foreground-modifiers-v0235") {
    // v0.23.5 upstream already normalizes foreground heartbeat=60 to 0, but
    // still validates heartbeat type before checking background and refuses
    // notify/watch_patterns/notify_on_complete on foreground calls. Normalize
    // all background-only modifiers away on foreground calls while keeping
    // pty, persist_on_release, and background heartbeat validation intact.
    return replaceExactlyOnce(
      source,
      [
        "    if not isinstance(heartbeat, int) or isinstance(heartbeat, bool) or heartbeat < 0:",
        '        return tool_error("heartbeat must be a whole number of seconds (0 disables; positive values are clamped to min 60).")',
        '    if not args.get("background", False):',
        "        if notify or watch_patterns or notify_on_complete:",
        "            return tool_error(",
        '                "notify only applies to background commands (foreground "',
        '                "results return directly). Either drop it, or run as "',
        '                "terminal(command=..., background=true, notify=...)."',
        "            )",
        "        heartbeat = 0",
        '        if args.get("pty", False):',
        "            return tool_error(",
        '                "pty requires background=true (a PTY session is interacted "',
        "                \"with via process_manage(action='write'/'submit'), which needs a \"",
        '                "tracked background process). Retry as terminal(command=..., "',
        '                "background=true, pty=true)."',
        "            )",
        "        if persist_on_release:",
        "            return tool_error(",
        '                "persist_on_release only applies to background commands (a foreground "',
        '                "process is awaited inline and has nothing to persist). Retry as "',
        '                "terminal(command=..., background=true, persist_on_release=true)."',
        "            )",
        "",
      ].join("\n"),
      [
        '    if not args.get("background", False):',
        `        # ${HERMES_AGENT_BRIDGE_MARKER}: a foreground call ignores`,
        "        # background-only modifiers instead of refusing them.",
        "        notify, notify_on_complete, watch_patterns, heartbeat = None, False, None, 0",
        '        if args.get("pty", False):',
        "            return tool_error(",
        '                "pty requires background=true (a PTY session is interacted "',
        "                \"with via process_manage(action='write'/'submit'), which needs a \"",
        '                "tracked background process). Retry as terminal(command=..., "',
        '                "background=true, pty=true)."',
        "            )",
        "        if persist_on_release:",
        "            return tool_error(",
        '                "persist_on_release only applies to background commands (a foreground "',
        '                "process is awaited inline and has nothing to persist). Retry as "',
        '                "terminal(command=..., background=true, persist_on_release=true)."',
        "            )",
        "    elif not isinstance(heartbeat, int) or isinstance(heartbeat, bool) or heartbeat < 0:",
        '        return tool_error("heartbeat must be a whole number of seconds (0 disables; positive values are clamped to min 60).")',
        "",
      ].join("\n"),
      "v0.23.5 foreground terminal modifiers"
    );
  }

  if (patchKind === "gateway-first-contact-v0235") {
    return replaceExactlyOnce(
      source,
      `        if human_platform and source.chat_type == "dm" and not await self.async_session_store.has_any_sessions():
            # Same branch logic as the TUI (profile-build offer once when "ask", else plain intro);
            # first_contact_turn_note already falls back to the plain intro on error.
            from agent.onboarding import first_contact_turn_note
            note = first_contact_turn_note(
                _load_gateway_config(), _gateway_config_home() / "config.yaml",
                session_history_empty=True, install_has_prior_sessions=False,
            )
            if note:
                turn_sidecar_notes.append(note)`,
      `        # ${HERMES_AGENT_BRIDGE_MARKER}: Sellable owns the hosted welcome and
        # must receive the customer's first request unchanged.
        _gateway_config = {}
        with suppress(Exception):
            _loaded_gateway_config = _load_gateway_config()
            if isinstance(_loaded_gateway_config, dict):
                _gateway_config = _loaded_gateway_config
        _onboarding_config = _gateway_config.get("onboarding")
        _sellable_managed_welcome = (
            isinstance(_onboarding_config, dict)
            and _onboarding_config.get("sellable_managed_welcome") is True
        )
        if (
            not _sellable_managed_welcome
            and human_platform
            and source.chat_type == "dm"
            and not await self.async_session_store.has_any_sessions()
        ):
            # Same branch logic as the TUI (profile-build offer once when "ask", else plain intro);
            # first_contact_turn_note already falls back to the plain intro on error.
            from agent.onboarding import first_contact_turn_note
            note = first_contact_turn_note(
                _gateway_config, _gateway_config_home() / "config.yaml",
                session_history_empty=True, install_has_prior_sessions=False,
            )
            if note:
                turn_sidecar_notes.append(note)`,
      "v0.23.5 Sellable-managed first contact"
    );
  }

  if (patchKind === "slack-adapter-shared-v0235") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${SECRET_FD_HELPER}${SLACK_SHARED_LOOPBACK_API_HELPER}${SLACK_AGENT_SESSIONS_STATUS_HELPER}${SLACK_MESSAGE_UNFURL_FILE_HELPER}`,
      "v0.23.5 dedicated Slack helpers"
    );
    patched = replaceExactlyOnce(
      patched,
      '        app_token = _get_scoped_secret("SLACK_APP_TOKEN")',
      '        app_token = (_sellable_agent_read_slack_secret(8, "SLACK_APP_TOKEN", "xapp-")\n                     if os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1"\n                     else _get_scoped_secret("SLACK_APP_TOKEN"))',
      "v0.23.5 dedicated app credential"
    );
    patched = replaceExactlyOnce(
      patched,
      '        client = AsyncWebClient(token=token, user_agent_prefix=hermes_user_agent("HermesAgent"))',
      '        client = AsyncWebClient(token=token, user_agent_prefix=hermes_user_agent("HermesAgent"),\n                                **_sellable_agent_slack_api_kwargs())',
      "v0.23.5 Slack API client"
    );
    patched = replaceExactlyOnce(
      patched,
      "        present = tokens_file.exists()",
      '        present = tokens_file.exists() and os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1"',
      "v0.23.5 OAuth file isolation"
    );
    patched = replaceExactlyOnce(
      patched,
      "            scope_id=str(team_id) if team_id else None,\n            message_id=ts,\n            # Workflow/app posts",
      "            scope_id=str(team_id) if team_id else None,\n            message_id=ts,\n            # Workflow/app posts",
      "v0.23.5 Slack source identity"
    );
    patched = replaceExactlyOnce(
      patched,
      '        for f in files_with_forwarded(event.get("files"), event.get("attachments")):',
      [
        '        files = list(files_with_forwarded(event.get("files"), event.get("attachments")))',
        "        files.extend(await _sellable_agent_recover_slack_message_unfurl_files(",
        '            self, event.get("attachments") or [], channel_id, team_id, files))',
        "        for f in files:",
      ].join("\n"),
      "v0.23.5 unfurled file recovery"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "        try:",
        "            _set_status = _session_status_method(self._get_client(chat_id, team_id=team_id))",
        "            await _set_status(channel_id=chat_id, thread_ts=thread_ts, status=status)",
        "        except Exception as e:",
        '            logger.debug("[Slack] assistant.threads.setStatus %s: %s", fail_label, e)',
      ].join("\n"),
      [
        "        try:",
        "            client = self._get_client(chat_id, team_id=team_id)",
        "            _set_status = _session_status_method(client)",
        '            if getattr(_set_status, "__name__", "") == "agents_sessions_setStatus":',
        "                normalized = _sellable_agent_normalize_session_status(status)",
        '                dedupe_key = (str(team_id or ""), str(chat_id), str(thread_ts))',
        "                if _sellable_agent_should_skip_session_status(self, dedupe_key, normalized):",
        "                    return",
        "                try:",
        "                    await _set_status(channel_id=chat_id, thread_ts=thread_ts, status=normalized)",
        "                except Exception as agent_err:",
        '                    logger.debug("[Slack] agents.sessions.setStatus %s (falling back): %s", fail_label, agent_err)',
        "                    await client.assistant_threads_setStatus(channel_id=chat_id, thread_ts=thread_ts, status=status)",
        "            else:",
        "                await _set_status(channel_id=chat_id, thread_ts=thread_ts, status=status)",
        "        except Exception as e:",
        '            logger.debug("[Slack] assistant.threads.setStatus %s: %s", fail_label, e)',
      ].join("\n"),
      "v0.23.5 Slack Agent Sessions status normalization"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "        try:",
        "            _set_title = _session_title_method(self._get_client(channel_id, team_id=team_id))",
        "            await _set_title(channel_id=channel_id, thread_ts=thread_ts, title=title)",
        "        except Exception as e:",
        '            logger.debug("[Slack] session title set failed: %s", e)',
      ].join("\n"),
      [
        "        try:",
        "            client = self._get_client(channel_id, team_id=team_id)",
        "            _set_title = _session_title_method(client)",
        "            try:",
        "                await _set_title(channel_id=channel_id, thread_ts=thread_ts, title=title)",
        "            except Exception as rename_err:",
        '                if getattr(_set_title, "__name__", "") == "agents_sessions_rename":',
        '                    logger.debug("[Slack] agents.sessions.rename failed (falling back): %s", rename_err)',
        "                    await client.assistant_threads_setTitle(channel_id=channel_id, thread_ts=thread_ts, title=title)",
        "                else:",
        "                    raise",
        "        except Exception as e:",
        '            logger.debug("[Slack] session title set failed: %s", e)',
      ].join("\n"),
      "v0.23.5 Slack Agent Sessions title fallback"
    );
    return replaceExactlyOnce(
      patched,
      SLACK_PREVIEW_IS_NOT_A_MESSAGE_NEEDLE,
      SLACK_PREVIEW_IS_NOT_A_MESSAGE,
      "v0.23.5 link preview is not a new message"
    );
  }

  if (patchKind === "mcp-tool-endpoint-v0235") {
    let patched = replaceExactlyOnce(
      source,
      'logger = logging.getLogger("tools.mcp_tool")\n',
      `logger = logging.getLogger("tools.mcp_tool")\nimport os\n${MCP_ENDPOINT_META_HELPER_V0235}`,
      "v0.23.5 MCP metadata helper"
    );
    patched = replaceExactlyOnce(
      patched,
      "    _call_coro = server.session.call_tool(tool_name, arguments=args, **extra)",
      [
        "    _sellable_agent_refuse_approval_without_customer_message(args)",
        "    sellable_meta = _sellable_agent_call_meta()",
        '    call_kwargs = {"arguments": args, **extra}',
        "    if sellable_meta is not None:",
        '        call_kwargs["meta"] = sellable_meta',
        "    _call_coro = server.session.call_tool(tool_name, **call_kwargs)",
      ].join("\n"),
      "v0.23.5 MCP call metadata"
    );
    // Allow concurrent JSON-RPC 2.0 calls over the sellable stdio proxy when
    // supports_parallel_tool_calls is enabled: sellable-mcp is an async Node
    // event-loop server with strict JSON-RPC id correlation and no sampling or
    // elicitation, and mcp-context-proxy pipelines Slack calls non-blockingly.
    return replaceExactlyOnce(
      patched,
      "        shared = (parallel and server._is_http() is True",
      '        shared = (parallel and (server._is_http() is True or getattr(server, "name", None) == "sellable")',
      "v0.23.5 MCP parallel stdio for sellable server"
    );
  }

  if (patchKind === "tool-search-eager-v0235") {
    let patched = replaceExactlyOnce(
      source,
      [
        "def _core_tool_names() -> frozenset[str]:",
        '    """Names that never defer by default (lazy: ``toolsets`` imports ``tools.registry``)."""',
        "    try:",
        "        from toolsets import _HERMES_CORE_TOOLS",
        "        return frozenset(_HERMES_CORE_TOOLS)",
        "    except Exception:",
        "        return frozenset()",
      ].join("\n"),
      [
        `# ${HERMES_AGENT_BRIDGE_MARKER}: honor configured eager MCP tools so hot-path tools never burn tool_describe turns.`,
        "def _sellable_eager_tool_names() -> frozenset[str]:",
        "    try:",
        "        import hermes_cli.config as _cfg_mod",
        '        tools_cfg = (_cfg_mod.load_config_readonly() or {}).get("tools")',
        '        ts_cfg = tools_cfg.get("tool_search") if isinstance(tools_cfg, dict) else None',
        '        eager_raw = ts_cfg.get("eager") if isinstance(ts_cfg, dict) else None',
        "        if isinstance(eager_raw, (list, tuple, set)):",
        "            return frozenset(str(n).strip() for n in eager_raw if str(n).strip())",
        "    except Exception:",
        "        pass",
        "    return frozenset()",
        "",
        "",
        "def _core_tool_names() -> frozenset[str]:",
        '    """Names that never defer by default (lazy: ``toolsets`` imports ``tools.registry``)."""',
        "    names: set[str] = set()",
        "    try:",
        "        from toolsets import _HERMES_CORE_TOOLS",
        "        names.update(_HERMES_CORE_TOOLS)",
        "    except Exception:",
        "        pass",
        "    names.update(_sellable_eager_tool_names())",
        "    return frozenset(names)",
      ].join("\n"),
      "v0.23.5 tool_search eager core tools"
    );
    return replaceExactlyOnce(
      patched,
      [
        "    if not is_deferrable_tool_name(name, load_config_readonly().effective_defer_tools):",
        "        return None, {}, not_deferrable_error(name, session_tool_names)",
      ].join("\n"),
      [
        "    if not is_deferrable_tool_name(name, load_config_readonly().effective_defer_tools):",
        "        if name in _sellable_eager_tool_names():",
        "            return name, raw_args, None",
        "        return None, {}, not_deferrable_error(name, session_tool_names)",
      ].join("\n"),
      "v0.23.5 tool_search eager tool_call compatibility"
    );
  }

  if (patchKind === "turn-context-v0235") {
    let openerIsolatedSoulHelper = V020_SOUL_TURN_PROLOGUE_HELPER.replace(
      "    import fcntl\n",
      `    # Opener stages carry their complete frozen policy/facts in the request.
    # Do not read profile SOUL/memory while constructing this isolated turn.
    if re.fullmatch(r"sellable_opener_stage_[a-f0-9]{32}", str(getattr(agent, "session_id", ""))) is not None:
        if (
            getattr(agent, "skip_context_files", False) is not True
            or getattr(agent, "load_soul_identity", True) is not False
            or getattr(agent, "tools", None) != []
            or getattr(agent, "valid_tool_names", None) != set()
        ):
            raise RuntimeError("sellable_opener_stage_context_not_isolated")
        if agent._cached_system_prompt is None:
            restore_or_build_system_prompt(agent, system_message, conversation_history)
        return

    import fcntl\n`
    );
    // v0.23.5 Persistence handle exposes session CRUD via session_db.sessions.
    openerIsolatedSoulHelper = replaceExactlyOnce(
      openerIsolatedSoulHelper,
      `            session_db = getattr(agent, "_session_db", None)
            if session_db is None:
                raise RuntimeError("Hermes session database is unavailable for SOUL refresh")
            session_db.update_system_prompt(agent.session_id, None)`,
      `            session_db = getattr(agent, "_session_db", None)
            if session_db is None:
                raise RuntimeError("Hermes session database is unavailable for SOUL refresh")
            session_store = getattr(session_db, "sessions", None) or session_db
            session_store.update_system_prompt(agent.session_id, None)`,
      "v0.23.5 SOUL clear system prompt"
    );
    openerIsolatedSoulHelper = replaceExactlyOnce(
      openerIsolatedSoulHelper,
      `                session_db.update_system_prompt(
                    agent.session_id,
                    agent._cached_system_prompt,
                )`,
      `                session_store.update_system_prompt(
                    agent.session_id,
                    agent._cached_system_prompt,
                )`,
      "v0.23.5 SOUL persist system prompt"
    );
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${openerIsolatedSoulHelper}`,
      "v0.23.5 SOUL helper"
    );
    return replaceExactlyOnce(
      patched,
      [
        "    refresh_warm_prompt(agent, system_message, conversation_history)",
        "    if agent._cached_system_prompt is None:",
        "        restore_or_build_system_prompt(agent, system_message, conversation_history)",
      ].join("\n"),
      [
        "    refresh_warm_prompt(agent, system_message, conversation_history)",
        "    _sellable_agent_restore_or_build_proven_soul(",
        "        agent, system_message, conversation_history, restore_or_build_system_prompt,",
        "    )",
      ].join("\n"),
      "v0.23.5 SOUL turn boundary"
    );
  }

  if (patchKind === "skills-cache-freshness-v0235") {
    let patched = replaceExactlyOnce(
      source,
      [
        "    names, is_skill = [], False",
        "    for entry in entries:",
        "        with suppress(OSError):",
        "            if entry.is_dir():",
        "                names.append(entry.name)",
        '            elif entry.name == "SKILL.md":',
        "                is_skill = True",
        "    names.sort()",
        "    sig.append((str(root), mtime, tuple(names)))",
      ].join("\n"),
      [
        `    # ${HERMES_AGENT_BRIDGE_MARKER}: include SKILL.md stat so in-place edits bust the cache immediately.`,
        "    names, is_skill, skill_md_sig = [], False, None",
        "    for entry in entries:",
        "        with suppress(OSError):",
        "            if entry.is_dir():",
        "                names.append(entry.name)",
        '            elif entry.name == "SKILL.md":',
        "                is_skill = True",
        "                skill_stat = entry.stat(follow_symlinks=False)",
        "                skill_md_sig = (skill_stat.st_mtime_ns, skill_stat.st_size)",
        "    names.sort()",
        "    sig.append((str(root), mtime, tuple(names), skill_md_sig))",
      ].join("\n"),
      "v0.23.5 live SKILL.md signature"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "    cached = _SKILLS_CACHE.get(cache_key)",
        "    if cached is not None and cached[0] == signature and (now - cached[1]) < _SKILLS_CACHE_TTL_SECONDS:",
      ].join("\n"),
      [
        "    cached = _SKILLS_CACHE.get(cache_key)",
        "    if cached is not None and cached[0] != signature:",
        "        _SKILLS_CACHE.clear()",
        "        cached = None",
        "    if cached is not None and cached[0] == signature and (now - cached[1]) < _SKILLS_CACHE_TTL_SECONDS:",
      ].join("\n"),
      "v0.23.5 both-variant cache invalidation"
    );
    return patched;
  }

  if (patchKind === "terminal-foreground-modifiers-v0215") {
    // Models fill schema defaults (heartbeat: 60) into every terminal call.
    // Upstream refuses those background-only modifiers on a foreground call,
    // the model resends the identical call, and Hermes halts the whole turn.
    // A foreground call now ignores them; background calls are unchanged.
    return replaceExactlyOnce(
      source,
      [
        "    # `notify` wins. Background-only modifiers on a foreground call fail",
        "    # with the corrected call instead of being silently ignored.",
        '    notify = args.get("notify")',
        '    notify_on_complete = args.get("notify_on_complete", False)',
        '    watch_patterns = args.get("watch_patterns")',
        '    heartbeat = args.get("heartbeat") or 0',
        "    if not isinstance(heartbeat, int) or isinstance(heartbeat, bool) or heartbeat < 0:",
        '        return tool_error("heartbeat must be a whole number of seconds (min 60).")',
        '    if not args.get("background", False):',
        "        if notify or watch_patterns or notify_on_complete or heartbeat:",
        "            return tool_error(",
        '                "notify/heartbeat only apply to background commands (foreground "',
        '                "results return directly). Either drop them, or run as "',
        '                "terminal(command=..., background=true, notify=...)."',
        "            )",
        '        if args.get("pty", False):',
        "",
      ].join("\n"),
      [
        "    # `notify` wins.",
        '    notify = args.get("notify")',
        '    notify_on_complete = args.get("notify_on_complete", False)',
        '    watch_patterns = args.get("watch_patterns")',
        '    heartbeat = args.get("heartbeat") or 0',
        '    if not args.get("background", False):',
        `        # ${HERMES_AGENT_BRIDGE_MARKER}: a foreground call ignores the`,
        "        # background-only modifiers instead of refusing them. Models fill",
        "        # schema defaults (heartbeat=60) into every call, so the refusal made",
        "        # them resend the identical call until identical_call_streak_halt",
        "        # ended the turn. The foreground result returns directly anyway.",
        "        notify, notify_on_complete, watch_patterns, heartbeat = None, False, None, 0",
        "    if not isinstance(heartbeat, int) or isinstance(heartbeat, bool) or heartbeat < 0:",
        '        return tool_error("heartbeat must be a whole number of seconds (min 60).")',
        '    if not args.get("background", False):',
        '        if args.get("pty", False):',
        "",
      ].join("\n"),
      "v0.21.5 foreground terminal modifiers"
    );
  }

  if (patchKind === "run-agent-halt-copy-v0215") {
    // Oct 9-14: the halt reply named the tool and pointed at "the last result
    // above", which customers never see. Say what happened in plain words.
    return replaceExactlyOnce(
      source,
      [
        "    def _toolguard_controlled_halt_response(self, decision: ToolGuardrailDecision) -> str:",
        '        # Shown to the user as the reply, so no decision codes; the code stays in result["guardrail"].',
        "        return (",
        "            f\"I stopped retrying because I kept running {decision.tool_name or 'the same tool'} \"",
        '            f"{decision.count} times without making progress. The last result above shows what "',
        "            \"blocked it. Tell me how you'd like to proceed, or send `continue` and I'll try a \"",
        '            "different approach."',
        "        )",
        "",
      ].join("\n"),
      [
        "    def _toolguard_controlled_halt_response(self, decision: ToolGuardrailDecision) -> str:",
        `        # ${HERMES_AGENT_BRIDGE_MARKER}: customers never see tool names or`,
        "        # tool results, so the reply says what happened in plain words. The",
        '        # code stays in result["guardrail"].',
        '        count = getattr(decision, "count", None)',
        '        tries = f"{count} times" if isinstance(count, int) and count > 1 else "several times"',
        "        return (",
        '            f"I tried the same step {tries} and it kept failing, so I stopped instead of "',
        '            "repeating it. Reply continue and I\'ll try a different way, or tell me "',
        '            "what you\'d like instead."',
        "        )",
        "",
      ].join("\n"),
      "v0.21.5 tool guardrail halt reply"
    );
  }

  if (patchKind === "cron-scheduler-v0215") {
    // A halt's reply asks for a chat reply; in a scheduled job it reached the
    // customer's channel as the job's report. The run stays ok so this plain
    // notice is delivered instead.
    return replaceExactlyOnce(
      patchHermesSourceFile("cron-scheduler-v020", source),
      [
        '            "delivering the response instead of failing the cron run",',
        "            job_name)",
        "",
        '    final_response = result.get("final_response", "") or ""',
        "",
      ].join("\n"),
      [
        '            "delivering the response instead of failing the cron run",',
        "            job_name)",
        "",
        `    # ${HERMES_AGENT_BRIDGE_MARKER}: a tool-guardrail halt's reply is`,
        "    # written for a chat; a scheduled run delivers this notice instead.",
        '    if turn_exit_reason == "guardrail_halt":',
        '        _guardrail = result.get("guardrail")',
        "        logger.warning(",
        "            \"Job '%s': tool guardrail halted the run (%s); delivering the scheduled-run notice\",",
        '            job_id, (_guardrail.get("code") if isinstance(_guardrail, dict) else None) or "unknown")',
        "        return (",
        '            "This scheduled run stopped early because of a problem on our side. "',
        '            "It will run again at its next scheduled time. Nothing needed from you."',
        "        )",
        "",
        '    final_response = result.get("final_response", "") or ""',
        "",
      ].join("\n"),
      "v0.21.5 cron guardrail halt notice"
    );
  }

  // Hermes 0.21 split configuration, MCP dispatch, and turn execution into
  // separate modules. Keep the historical contracts intact for older images.
  if (patchKind === "gateway-first-contact-v0213") {
    return replaceExactlyOnce(
      source,
      `        if not await self.async_session_store.has_any_sessions():
            _intro_note = (
                "[System note: This is the user's very first message ever. "
                "Briefly introduce yourself and mention that /help shows available commands. "
                "Keep the introduction concise -- one or two sentences max.]"
            )
            # onboarding.profile_build == "ask" (default) and not yet offered: swap the plain intro for
            # a consent-gated profile-build directive. Fires at most once.
            try:
                from agent.onboarding import (
                    PROFILE_BUILD_FLAG, is_seen, mark_seen, profile_build_directive,
                    profile_build_mode,
                )
                _onb_cfg = _load_gateway_config()
                if profile_build_mode(_onb_cfg) == "ask" and not is_seen(_onb_cfg, PROFILE_BUILD_FLAG):
                    turn_sidecar_notes.append(profile_build_directive().strip())
                    mark_seen(_hermes_home / "config.yaml", PROFILE_BUILD_FLAG)
                else:
                    turn_sidecar_notes.append(_intro_note)
            except Exception as _pb_err:
                logger.debug("Profile-build onboarding directive failed, using plain intro: %s", _pb_err)
                turn_sidecar_notes.append(_intro_note)`,
      `        # ${HERMES_AGENT_BRIDGE_MARKER}: Sellable owns the hosted welcome and
        # must receive the customer's first request unchanged.
        _gateway_config = {}
        with suppress(Exception):
            _loaded_gateway_config = _load_gateway_config()
            if isinstance(_loaded_gateway_config, dict):
                _gateway_config = _loaded_gateway_config
        _onboarding_config = _gateway_config.get("onboarding")
        _sellable_managed_welcome = (
            isinstance(_onboarding_config, dict)
            and _onboarding_config.get("sellable_managed_welcome") is True
        )
        if (
            not _sellable_managed_welcome
            and not await self.async_session_store.has_any_sessions()
        ):
            _intro_note = (
                "[System note: This is the user's very first message ever. "
                "Briefly introduce yourself and mention that /help shows available commands. "
                "Keep the introduction concise -- one or two sentences max.]"
            )
            # onboarding.profile_build == "ask" (default) and not yet offered: swap the plain intro for
            # a consent-gated profile-build directive. Fires at most once.
            try:
                from agent.onboarding import (
                    PROFILE_BUILD_FLAG, is_seen, mark_seen, profile_build_directive,
                    profile_build_mode,
                )
                _onb_cfg = _gateway_config
                if profile_build_mode(_onb_cfg) == "ask" and not is_seen(_onb_cfg, PROFILE_BUILD_FLAG):
                    turn_sidecar_notes.append(profile_build_directive().strip())
                    mark_seen(_hermes_home / "config.yaml", PROFILE_BUILD_FLAG)
                else:
                    turn_sidecar_notes.append(_intro_note)
            except Exception as _pb_err:
                logger.debug("Profile-build onboarding directive failed, using plain intro: %s", _pb_err)
                turn_sidecar_notes.append(_intro_note)`,
      "v0.21.3 Sellable-managed first contact"
    );
  }

  if (patchKind === "gateway-config-dedicated-v021") {
    return replaceExactlyOnce(
      source,
      "getenv = _getenv_str\n",
      `${SECRET_FD_HELPER}
def getenv(name, default=""):
    if name == "SLACK_BOT_TOKEN" and os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1":
        return _sellable_agent_read_slack_secret(9, "SLACK_BOT_TOKEN", "xoxb-")
    return _getenv_str(name, default)
`,
      "v0.21 dedicated config credential reader"
    );
  }

  if (patchKind === "platform-env-dedicated-v0235") {
    // v0.23.5 moved Slack's SLACK_BOT_TOKEN env override out of gateway/config_env.py
    // into plugins/platforms/slack/gateway_env.py -> gateway/platform_env.py::EnvCred.
    return replaceExactlyOnce(
      source,
      "getenv = _getenv_str\n",
      `import os
${SECRET_FD_HELPER}
def getenv(name, default=""):
    if name == "SLACK_BOT_TOKEN" and os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1":
        return _sellable_agent_read_slack_secret(9, "SLACK_BOT_TOKEN", "xoxb-")
    return _getenv_str(name, default)
`,
      "v0.23.5 dedicated platform env credential reader"
    );
  }

  if (
    patchKind === "slack-adapter-dedicated-v021" ||
    patchKind === "slack-adapter-dedicated-v0213" ||
    patchKind === "slack-adapter-shared-v0215"
  ) {
    // v0.21.3 moved credential lookup into a profile-scoped helper. Preserve
    // that upstream isolation outside the dedicated Sellable runtime.
    const scopedCredential = patchKind !== "slack-adapter-dedicated-v021";
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${SECRET_FD_HELPER}${
        patchKind === "slack-adapter-shared-v0215"
          ? SLACK_SHARED_LOOPBACK_API_HELPER
          : SLACK_DEDICATED_TEST_API_HELPER
      }${SLACK_MESSAGE_UNFURL_FILE_HELPER}`,
      "v0.21 dedicated Slack helpers"
    );
    patched = replaceExactlyOnce(
      patched,
      scopedCredential
        ? '        app_token = _get_scoped_secret("SLACK_APP_TOKEN")'
        : '            app_token = get_secret("SLACK_APP_TOKEN")',
      scopedCredential
        ? '        app_token = (_sellable_agent_read_slack_secret(8, "SLACK_APP_TOKEN", "xapp-")\n                     if os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1"\n                     else _get_scoped_secret("SLACK_APP_TOKEN"))'
        : '            app_token = (_sellable_agent_read_slack_secret(8, "SLACK_APP_TOKEN", "xapp-")\n                         if os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1"\n                         else get_secret("SLACK_APP_TOKEN"))',
      "v0.21 dedicated app credential"
    );
    patched = replaceExactlyOnce(
      patched,
      "        client = AsyncWebClient(token=token, user_agent_prefix=_HERMES_SLACK_USER_AGENT_PREFIX)",
      "        client = AsyncWebClient(token=token, user_agent_prefix=_HERMES_SLACK_USER_AGENT_PREFIX,\n                                **_sellable_agent_slack_api_kwargs())",
      "v0.21 Slack API client"
    );
    patched = replaceExactlyOnce(
      patched,
      "        present = tokens_file.exists()",
      '        present = tokens_file.exists() and os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1"',
      "v0.21 OAuth file isolation"
    );
    patched = replaceExactlyOnce(
      patched,
      scopedCredential
        ? "            scope_id=str(team_id) if team_id else None,\n            message_id=ts,\n            # Workflow/app posts"
        : "            scope_id=str(team_id) if team_id else None,\n            # Workflow/app posts",
      "            scope_id=str(team_id) if team_id else None,\n            message_id=ts,\n            # Workflow/app posts",
      "v0.21 Slack source identity"
    );
    patched = replaceExactlyOnce(
      patched,
      '        for f in event.get("files", []):',
      [
        '        files = list(event.get("files") or [])',
        "        files.extend(await _sellable_agent_recover_slack_message_unfurl_files(",
        '            self, event.get("attachments") or [], channel_id, team_id, files))',
        "        for f in files:",
      ].join("\n"),
      "v0.21 unfurled file recovery"
    );
    if (patchKind !== "slack-adapter-shared-v0215") return patched;
    return replaceExactlyOnce(
      patched,
      SLACK_PREVIEW_IS_NOT_A_MESSAGE_NEEDLE,
      SLACK_PREVIEW_IS_NOT_A_MESSAGE,
      "v0.21.5 link preview is not a new message"
    );
  }

  if (patchKind === "mcp-tool-endpoint-v021") {
    let patched = replaceExactlyOnce(
      source,
      'logger = logging.getLogger("tools.mcp_tool")\n',
      `logger = logging.getLogger("tools.mcp_tool")\nimport os\n${MCP_ENDPOINT_META_HELPER_OPTIONAL_SESSION}`,
      "v0.21 MCP metadata helper"
    );
    return replaceExactlyOnce(
      patched,
      "    _call_coro = server.session.call_tool(tool_name, arguments=args)",
      [
        "    sellable_meta = _sellable_agent_call_meta()",
        '    call_kwargs = {"arguments": args}',
        "    if sellable_meta is not None:",
        '        call_kwargs["meta"] = sellable_meta',
        "    _call_coro = server.session.call_tool(tool_name, **call_kwargs)",
      ].join("\n"),
      "v0.21 MCP call metadata"
    );
  }

  if (patchKind === "gateway-session-v021") {
    return replaceExactlyOnce(
      source,
      "            session_key=context.session_key,\n",
      `            # ${HERMES_AGENT_BRIDGE_MARKER}: durable conversation identity.
            session_key=context.session_key,
            session_id=context.session_id,
`,
      "v0.21 durable session identity"
    );
  }

  if (patchKind === "gateway-provider-preflight-v021") {
    let patched = replaceExactlyOnce(
      source,
      'logger = logging.getLogger("gateway.run")\n',
      `logger = logging.getLogger("gateway.run")\nimport os\nimport concurrent.futures\n${V020_PROVIDER_PREFLIGHT_HELPER}`,
      "v0.21 provider helper"
    );
    return replaceExactlyOnce(
      patched,
      "        max_iterations = _current_max_iterations()\n",
      "        max_iterations = _current_max_iterations()\n        _sellable_agent_preflight_model_providers()\n",
      "v0.21 provider preflight"
    );
  }

  if (patchKind === "turn-context-v021") {
    const openerIsolatedSoulHelper = V020_SOUL_TURN_PROLOGUE_HELPER.replace(
      "    import fcntl\n",
      `    # Opener stages carry their complete frozen policy/facts in the request.
    # Do not read profile SOUL/memory while constructing this isolated turn.
    if re.fullmatch(r"sellable_opener_stage_[a-f0-9]{32}", str(getattr(agent, "session_id", ""))) is not None:
        if (
            getattr(agent, "skip_context_files", False) is not True
            or getattr(agent, "load_soul_identity", True) is not False
            or getattr(agent, "tools", None) != []
            or getattr(agent, "valid_tool_names", None) != set()
        ):
            raise RuntimeError("sellable_opener_stage_context_not_isolated")
        if agent._cached_system_prompt is None:
            restore_or_build_system_prompt(agent, system_message, conversation_history)
        return

    import fcntl\n`
    );
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${openerIsolatedSoulHelper}`,
      "v0.21 SOUL helper"
    );
    return replaceExactlyOnce(
      patched,
      "    # System prompt is cached per session for prefix caching.\n    if agent._cached_system_prompt is None:\n        restore_or_build_system_prompt(agent, system_message, conversation_history)",
      "    # System prompt is cached per session for prefix caching.\n    _sellable_agent_restore_or_build_proven_soul(\n        agent, system_message, conversation_history, restore_or_build_system_prompt,\n    )",
      "v0.21 SOUL turn boundary"
    );
  }

  if (patchKind === "gateway-config") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${SECRET_FD_HELPER}${SLACK_TEST_API_HELPER}`,
      "gateway helper"
    );
    patched = replaceExactlyOnce(
      patched,
      '    slack_token = os.getenv("SLACK_BOT_TOKEN")',
      '    slack_token = _sellable_agent_read_slack_secret(9, "SLACK_BOT_TOKEN", "xoxb-")',
      "Slack bot token"
    );
    return patched;
  }

  if (patchKind === "gateway-config-v019") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${V019_ROOT_TOKEN_FILE_HELPER}`,
      "v0.19 gateway token-map helper"
    );
    patched = replaceExactlyOnce(
      patched,
      '    slack_token = getenv("SLACK_BOT_TOKEN")',
      '    slack_token = _sellable_agent_slack_token_source(getenv("SLACK_BOT_TOKEN"))',
      "v0.19 Slack bot token source"
    );
    return patched;
  }

  if (patchKind === "gateway-config-dedicated-v019") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${SECRET_FD_HELPER}`,
      "v0.19 dedicated gateway helper"
    );
    patched = replaceExactlyOnce(
      patched,
      '    slack_token = getenv("SLACK_BOT_TOKEN")',
      '    slack_token = _sellable_agent_read_slack_secret(9, "SLACK_BOT_TOKEN", "xoxb-")',
      "v0.19 dedicated Slack bot token"
    );
    return patched;
  }

  if (patchKind === "slack-adapter") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${SECRET_FD_HELPER}`,
      "Slack adapter helper"
    );
    patched = replaceExactlyOnce(
      patched,
      '        app_token = os.getenv("SLACK_APP_TOKEN")',
      '        app_token = _sellable_agent_read_slack_secret(8, "SLACK_APP_TOKEN", "xapp-")',
      "Slack app token"
    );
    patched = replaceExactlyOnce(
      patched,
      '        tokens_file = get_hermes_home() / "slack_tokens.json"\n        if tokens_file.exists():',
      '        tokens_file = get_hermes_home() / "slack_tokens.json"\n        if tokens_file.exists() and os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":',
      "legacy Slack token file"
    );
    return patched;
  }

  if (patchKind === "slack-adapter-v019") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${SECRET_FD_HELPER}${V019_SLACK_MULTIPLEX_HELPER}`,
      "v0.19 Slack multiplex helper"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "        except UnscopedSecretError:",
        '            app_token = os.getenv("SLACK_APP_TOKEN")',
      ].join("\n"),
      [
        "        except UnscopedSecretError:",
        '            app_token = _sellable_agent_read_slack_secret(8, "SLACK_APP_TOKEN", "xapp-")',
      ].join("\n"),
      "v0.19 Slack app token"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "        # Support comma-separated bot tokens for multi-workspace",
        '        bot_tokens = [t.strip() for t in raw_token.split(",") if t.strip()]',
        "",
        "        # Also load tokens from OAuth token file",
        "        from hermes_constants import get_hermes_home",
        "",
        '        tokens_file = get_hermes_home() / "slack_tokens.json"',
        "        if tokens_file.exists():",
        "            try:",
        '                saved = json.loads(tokens_file.read_text(encoding="utf-8"))',
        "                for team_id, entry in saved.items():",
        '                    tok = entry.get("token", "") if isinstance(entry, dict) else ""',
        "                    if tok and tok not in bot_tokens:",
        "                        bot_tokens.append(tok)",
        "                        team_label = (",
        '                            entry.get("team_name", team_id)',
        "                            if isinstance(entry, dict)",
        "                            else team_id",
        "                        )",
        "                        logger.info(",
        '                            "[Slack] Loaded saved token for workspace %s", team_label',
        "                        )",
        "            except Exception as e:",
        '                logger.warning("[Slack] Failed to read %s: %s", tokens_file, e)',
      ].join("\n"),
      "        bot_tokens, _sellable_token_owners = _sellable_agent_load_slack_tokens(raw_token)",
      "v0.19 Slack OAuth token inputs"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "            self._team_clients = {}",
        "            self._team_bot_user_ids = {}",
      ].join("\n"),
      [
        "            self._team_clients = {}",
        "            self._team_bot_user_ids = {}",
        "            self._sellable_team_installation_ids = {}",
      ].join("\n"),
      "v0.19 Slack installation map reset"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        '                team_name = auth_response.get("team", "unknown")',
        "",
        "                self._team_clients[team_id] = client",
      ].join("\n"),
      [
        '                team_name = auth_response.get("team", "unknown")',
        "",
        '                if os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1":',
        "                    owner = _sellable_token_owners.get(token)",
        '                    bot_id = auth_response.get("bot_id", "")',
        '                    if not owner or owner["teamId"] != team_id or not bot_user_id or not bot_id:',
        '                        raise RuntimeError("Slack OAuth token authenticated to an unexpected team or principal")',
        "                    prior_installation = self._sellable_team_installation_ids.get(team_id)",
        '                    if prior_installation and prior_installation != owner["installationId"]:',
        '                        raise RuntimeError("Slack team has divergent installation ownership")',
        '                    self._sellable_team_installation_ids[team_id] = owner["installationId"]',
        "",
        "                self._team_clients[team_id] = client",
      ].join("\n"),
      "v0.19 Slack authenticated installation binding"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "    def _get_client(self, chat_id: str, team_id: Optional[str] = None) -> Any:",
        '        """Return the workspace-specific WebClient for a channel."""',
        "        if team_id and team_id in self._team_clients:",
        "            return self._team_clients[team_id]",
        "        team_id = self._channel_team.get(chat_id)",
        "        if team_id and team_id in self._team_clients:",
        "            return self._team_clients[team_id]",
        "        return self._app.client  # fallback to primary",
      ].join("\n"),
      [
        "    def _get_client(self, chat_id: str, team_id: Optional[str] = None) -> Any:",
        '        """Return the workspace-specific WebClient for a channel."""',
        '        if os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1":',
        '            installation_id = getattr(self, "_sellable_team_installation_ids", {}).get(team_id)',
        "            client = self._team_clients.get(team_id) if team_id else None",
        "            if not team_id or not installation_id or client is None:",
        '                raise RuntimeError("exact Slack workspace client is unavailable")',
        "            return client",
        "        if team_id and team_id in self._team_clients:",
        "            return self._team_clients[team_id]",
        "        team_id = self._channel_team.get(chat_id)",
        "        if team_id and team_id in self._team_clients:",
        "            return self._team_clients[team_id]",
        "        return self._app.client  # fallback to primary",
      ].join("\n"),
      "v0.19 exact Slack egress client"
    );
    patched = replaceExactlyOnce(
      patched,
      "        # Build source\n        source = self.build_source(",
      [
        "        # Sellable admission runs before native profile resolution and any Agent/session work.",
        "        _sellable_principal = None",
        "        _sellable_route_token = None",
        '        if os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1":',
        "            _sellable_principal, _sellable_route_token = await _sellable_agent_authorize_slack_route(",
        "                self, team_id, channel_id, thread_ts, user_id",
        "            )",
        "        try:",
        "            source = self.build_source(",
      ].join("\n"),
      "v0.19 pre-resolution Slack admission"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        '            is_bot=bool(event.get("bot_id")) or event.get("subtype") == "bot_message",',
        "        )",
        "",
        "        # Per-channel ephemeral prompt",
      ].join("\n"),
      [
        '                is_bot=bool(event.get("bot_id")) or event.get("subtype") == "bot_message",',
        "            )",
        "        finally:",
        "            if _sellable_route_token is not None:",
        "                from gateway.profile_routing import reset_sellable_slack_route_principal",
        "                reset_sellable_slack_route_principal(_sellable_route_token)",
        '        if _sellable_principal is not None and source.profile != _sellable_principal["profileId"]:',
        '            raise RuntimeError("Sellable Slack profile resolution did not preserve the exact principal")',
        "",
        "        # Per-channel ephemeral prompt",
      ].join("\n"),
      "v0.19 post-resolution Slack principal check"
    );
    return patched;
  }

  if (patchKind === "slack-adapter-dedicated-v019") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${SECRET_FD_HELPER}${SLACK_DEDICATED_TEST_API_HELPER}${SLACK_MESSAGE_UNFURL_FILE_HELPER}`,
      "v0.19 dedicated Slack helper"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "        try:",
        '            app_token = get_secret("SLACK_APP_TOKEN")',
        "        except UnscopedSecretError:",
        '            app_token = os.getenv("SLACK_APP_TOKEN")',
      ].join("\n"),
      [
        '        if os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1":',
        '            app_token = _sellable_agent_read_slack_secret(8, "SLACK_APP_TOKEN", "xapp-")',
        "        else:",
        "            try:",
        '                app_token = get_secret("SLACK_APP_TOKEN")',
        "            except UnscopedSecretError:",
        '                app_token = os.getenv("SLACK_APP_TOKEN")',
      ].join("\n"),
      "v0.19 dedicated Slack app token"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "            primary_client = AsyncWebClient(",
        "                token=primary_token,",
        "                user_agent_prefix=_HERMES_SLACK_USER_AGENT_PREFIX,",
        "            )",
      ].join("\n"),
      [
        "            primary_client = AsyncWebClient(",
        "                token=primary_token,",
        "                user_agent_prefix=_HERMES_SLACK_USER_AGENT_PREFIX,",
        "                **_sellable_agent_slack_api_kwargs(),",
        "            )",
      ].join("\n"),
      "v0.19 dedicated Slack app API client"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "                client = AsyncWebClient(",
        "                    token=token,",
        "                    user_agent_prefix=_HERMES_SLACK_USER_AGENT_PREFIX,",
        "                )",
      ].join("\n"),
      [
        "                client = AsyncWebClient(",
        "                    token=token,",
        "                    user_agent_prefix=_HERMES_SLACK_USER_AGENT_PREFIX,",
        "                    **_sellable_agent_slack_api_kwargs(),",
        "                )",
      ].join("\n"),
      "v0.19 dedicated Slack workspace API client"
    );
    patched = replaceExactlyOnce(
      patched,
      "        if tokens_file.exists():",
      '        if tokens_file.exists() and os.getenv("SELLABLE_AGENT_RUNTIME", "") != "1":',
      "v0.19 dedicated Slack OAuth token file"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "            scope_id=str(team_id) if team_id else None,",
        "            # Slack Workflow Builder / app posts arrive as",
      ].join("\n"),
      [
        "            scope_id=str(team_id) if team_id else None,",
        "            # Preserve Hermes' native Slack message identity in task-local context.",
        "            message_id=ts,",
        "            # Slack Workflow Builder / app posts arrive as",
      ].join("\n"),
      "v0.19 dedicated Slack request identity"
    );
    patched = replaceExactlyOnce(
      patched,
      '        files = event.get("files", [])',
      [
        '        files = list(event.get("files") or [])',
        "        files.extend(",
        "            await _sellable_agent_recover_slack_message_unfurl_files(",
        "                self, slack_attachments, channel_id, team_id, files",
        "            )",
        "        )",
      ].join("\n"),
      "v0.19 dedicated Slack message-unfurl files"
    );
    return patched;
  }

  if (patchKind === "profile-routing-v019") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${V019_PROFILE_ROUTING_HELPER}`,
      "v0.19 profile route principal helper"
    );
    patched = replaceExactlyOnce(
      patched,
      '    """Return the best-matching route, or None for no match."""\n    for route in routes:',
      [
        '    """Return the best-matching route, or None for no match."""',
        '    if _sellable_os.getenv("SELLABLE_AGENT_RUNTIME", "") == "1":',
        "        principal = _sellable_slack_route_principal.get()",
        '        if not isinstance(principal, dict) or not principal.get("profileId"):',
        '            raise RuntimeError("exact Sellable Slack route principal is unavailable")',
        "        return ProfileRoute(",
        '            name="sellable-exact-route",',
        "            platform=str(platform),",
        '            profile=str(principal["profileId"]),',
        "            chat_id=str(chat_id) if chat_id else None,",
        "            thread_id=str(thread_id) if thread_id else None,",
        "        )",
        "    for route in routes:",
      ].join("\n"),
      "v0.19 exact profile route selection"
    );
    return patched;
  }

  if (
    patchKind === "mcp-tool" ||
    patchKind === "mcp-tool-v020" ||
    patchKind === "mcp-tool-endpoint-v020"
  ) {
    const metaHelper =
      patchKind === "mcp-tool-endpoint-v020"
        ? MCP_ENDPOINT_META_HELPER_OPTIONAL_SESSION
        : patchKind === "mcp-tool-v020"
          ? MCP_META_HELPER_OPTIONAL_SESSION
          : MCP_META_HELPER;
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${metaHelper}`,
      "MCP metadata helper"
    );
    patched = replaceExactlyOnce(
      patched,
      "                    result = await server.session.call_tool(tool_name, arguments=args)",
      [
        "                    sellable_meta = _sellable_agent_call_meta()",
        '                    call_kwargs = {"arguments": args}',
        "                    if sellable_meta is not None:",
        '                        call_kwargs["meta"] = sellable_meta',
        "                    result = await server.session.call_tool(tool_name, **call_kwargs)",
      ].join("\n"),
      "MCP call metadata"
    );
    return patched;
  }

  if (patchKind === "gateway-session-v020") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${V020_PROVIDER_PREFLIGHT_HELPER}`,
      "v0.20 provider preflight helper"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "            session_key=context.session_key,",
        '            message_id=str(context.source.message_id) if context.source.message_id else "",',
      ].join("\n"),
      [
        "            session_key=context.session_key,",
        "            # Bind the durable Hermes conversation ID alongside the Slack actor.",
        "            # Sellable MCP SOUL calls require this task-local identity, and omitting",
        "            # it must not make otherwise valid Slack MCP calls fail closed.",
        "            session_id=context.session_id,",
        '            message_id=str(context.source.message_id) if context.source.message_id else "",',
      ].join("\n"),
      "v0.20 gateway durable session identity"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "        max_iterations = _current_max_iterations()",
        "",
        "        try:",
        "            model, runtime_kwargs = self._runner._resolve_session_agent_runtime(",
      ].join("\n"),
      [
        "        max_iterations = _current_max_iterations()",
        "",
        "        _sellable_agent_preflight_model_providers()",
        "",
        "        try:",
        "            model, runtime_kwargs = self._runner._resolve_session_agent_runtime(",
      ].join("\n"),
      "v0.20 gateway provider preflight invocation"
    );
    return patched;
  }

  if (patchKind === "clarify-session-key-v020") {
    return replaceExactlyOnce(
      source,
      [
        "        session_key = build_session_key(",
        "            event.source,",
        '            group_sessions_per_user=self.config.extra.get("group_sessions_per_user", True),',
        '            thread_sessions_per_user=self.config.extra.get("thread_sessions_per_user", False),',
        "        )",
      ].join("\n"),
      [
        '        _sk_store = getattr(self, "_session_store", None)',
        "        session_key = build_session_key(",
        "            event.source,",
        '            group_sessions_per_user=self.config.extra.get("group_sessions_per_user", True),',
        '            thread_sessions_per_user=self.config.extra.get("thread_sessions_per_user", False),',
        "            profile=_sk_store._resolve_profile_for_key(event.source) if _sk_store else None,",
        "        )",
      ].join("\n"),
      "v0.20 profile-scoped clarify session key"
    );
  }

  if (patchKind === "turn-context-v020") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${V020_SOUL_TURN_PROLOGUE_HELPER}`,
      "v0.20 SOUL turn-prologue helper"
    );
    patched = replaceExactlyOnce(
      patched,
      [
        "    # ── System prompt (cached per session for prefix caching) ──",
        "    if agent._cached_system_prompt is None:",
        "        restore_or_build_system_prompt(agent, system_message, conversation_history)",
      ].join("\n"),
      [
        "    # ── System prompt (cached per session for prefix caching) ──",
        "    _sellable_agent_restore_or_build_proven_soul(",
        "        agent,",
        "        system_message,",
        "        conversation_history,",
        "        restore_or_build_system_prompt,",
        "    )",
      ].join("\n"),
      "v0.20 SOUL turn-prologue call"
    );
    return patched;
  }

  if (patchKind === "cron-scheduler-v020") {
    let patched = replaceExactlyOnce(
      source,
      "logger = logging.getLogger(__name__)\n",
      `logger = logging.getLogger(__name__)\n${CRON_MCP_ISOLATION_HELPER}`,
      "v0.20 cron MCP isolation helper"
    );
    patched = replaceExactlyOnce(
      patched,
      "        return _merge_mcp_into_per_job_toolsets(list(per_job), cfg or {})",
      "        return _sellable_agent_strip_cron_mcp(\n            _merge_mcp_into_per_job_toolsets(list(per_job), cfg or {}), cfg or {}\n        )",
      "v0.20 per-job cron MCP isolation"
    );
    patched = replaceExactlyOnce(
      patched,
      '        return sorted(_get_platform_tools(cfg or {}, "cron"))',
      '        return _sellable_agent_strip_cron_mcp(\n            sorted(_get_platform_tools(cfg or {}, "cron")), cfg or {}\n        )',
      "v0.20 platform cron MCP isolation"
    );
    if (patched.includes("def _prepare_job_prompt(")) {
      patched = replaceExactlyOnce(
        patched,
        [
          "    prerun_script = None",
          '    script_path = job.get("script")',
          "    if script_path:",
        ].join("\n"),
        [
          "    prerun_script = None",
          '    script_path = job.get("script")',
          "    if (",
          '        str(job.get("name") or "") in _SELLABLE_MANAGED_CUSTOMER_PROGRAM_JOBS',
          "        and not script_path",
          "    ):",
          "        return _sellable_agent_managed_gate_block(",
          '            job_id, job_name, "managed wake gate script is missing"',
          "        )",
          "    if script_path:",
        ].join("\n"),
        "v0.20 managed cron missing wake gate"
      );
      if (patched.includes("workdir=_resolve_job_workdir(job, job_id)")) {
        patched = replaceExactlyOnce(
          patched,
          [
            "    if script_path:",
            "        prerun_script = _run_job_script_with_claim_heartbeat(",
            "            job,",
            "            script_path,",
            "            workdir=_resolve_job_workdir(job, job_id),",
            "            cancel_event=cancel_event,",
            "        )",
          ].join("\n"),
          [
            "    if script_path:",
            "        try:",
            "            prerun_script = _run_job_script_with_claim_heartbeat(",
            "                job,",
            "                script_path,",
            "                workdir=_resolve_job_workdir(job, job_id),",
            "                cancel_event=cancel_event,",
            "            )",
            "        except Exception as _managed_launch_exc:",
            '            if str(job.get("name") or "") in _SELLABLE_MANAGED_CUSTOMER_PROGRAM_JOBS:',
            "                return _sellable_agent_managed_gate_block(",
            '                    job_id, job_name, f"managed wake gate could not start: {_managed_launch_exc}"',
            "                )",
            "            raise",
          ].join("\n"),
          "v0.21.3 managed cron launch failure"
        );
      } else {
        patched = replaceExactlyOnce(
          patched,
          "    if script_path:\n        prerun_script = _run_job_script_with_claim_heartbeat(job, script_path, cancel_event=cancel_event)",
          [
            "    if script_path:",
            "        try:",
            "            prerun_script = _run_job_script_with_claim_heartbeat(job, script_path, cancel_event=cancel_event)",
            "        except Exception as _managed_launch_exc:",
            '            if str(job.get("name") or "") in _SELLABLE_MANAGED_CUSTOMER_PROGRAM_JOBS:',
            "                return _sellable_agent_managed_gate_block(",
            '                    job_id, job_name, f"managed wake gate could not start: {_managed_launch_exc}"',
            "                )",
            "            raise",
          ].join("\n"),
          "v0.21.1 managed cron launch failure"
        );
      }
      patched = replaceExactlyOnce(
        patched,
        [
          "        _ran_ok, _script_output = prerun_script",
          "        if _ran_ok and not _parse_wake_gate(_script_output):",
        ].join("\n"),
        [
          "        _ran_ok, _script_output = prerun_script",
          "        _managed_wake, _managed_error = _sellable_agent_validate_managed_wake_gate(",
          "            job, script_path, prerun_script",
          "        )",
          "        if _managed_error:",
          "            return _sellable_agent_managed_gate_block(job_id, job_name, _managed_error)",
          "        if _ran_ok and not _parse_wake_gate(_script_output):",
        ].join("\n"),
        "v0.20 managed cron wake gate result"
      );
    }
    return patched;
  }

  if (
    patchKind === "skills-cache-freshness-v020" ||
    patchKind === "skills-cache-freshness-v021"
  ) {
    let patched = replaceExactlyOnce(
      source,
      "        sig.append((str(d), m))",
      [
        "        skill_files = []",
        "        for root, dirs, files in os.walk(d, followlinks=False):",
        "            dirs[:] = sorted(",
        "                name for name in dirs",
        "                if not os.path.islink(os.path.join(root, name))",
        "            )",
        '            if "SKILL.md" not in files:',
        "                continue",
        '            skill_path = os.path.join(root, "SKILL.md")',
        "            try:",
        "                skill_stat = os.stat(skill_path, follow_symlinks=False)",
        "                skill_files.append(",
        "                    (skill_path, skill_stat.st_mtime_ns, skill_stat.st_size)",
        "                )",
        "            except OSError:",
        "                continue",
        "        sig.append((str(d), m, tuple(skill_files)))",
      ].join("\n"),
      "v0.20 live SKILL.md signature"
    );
    patched = replaceExactlyOnce(
      patched,
      "    disabled = set() if skip_disabled else _get_disabled_skill_names()",
      [
        "    disabled = _get_disabled_skill_names()",
        "    filtered_disabled = set() if skip_disabled else disabled",
      ].join("\n"),
      "v0.20 shared disabled signature"
    );
    patched = replaceExactlyOnce(
      patched,
      patchKind === "skills-cache-freshness-v021"
        ? "                if name in seen_names or name in disabled:\n                    continue"
        : "                if name in disabled:\n                    continue",
      patchKind === "skills-cache-freshness-v021"
        ? "                if name in seen_names or name in filtered_disabled:\n                    continue"
        : "                if name in filtered_disabled:\n                    continue",
      "v0.20 filtered disabled behavior"
    );
    patched = replaceExactlyOnce(
      patched,
      "    skills = []\n    seen_names: set = set()",
      [
        "    # A signature change invalidates both filtered and unfiltered",
        "    # variants together; neither waits for the 30-second TTL.",
        "    if cached is not None and cached[0] != signature:",
        "        _SKILLS_CACHE.clear()",
        "",
        "    skills = []",
        "    seen_names: set = set()",
      ].join("\n"),
      "v0.20 both-variant cache invalidation"
    );
    return patched;
  }

  if (
    patchKind === "skills-prompt-freshness-v020" ||
    patchKind === "skills-prompt-freshness-v021" ||
    patchKind === "skills-prompt-freshness-v0215"
  ) {
    let patched = replaceExactlyOnce(
      source,
      "_SKILLS_PROMPT_CACHE_LOCK = threading.Lock()",
      [
        "_SKILLS_PROMPT_CACHE_LOCK = threading.Lock()",
        "_SELLABLE_AGENT_SKILLS_PROMPT_SIGNATURE = None",
      ].join("\n"),
      "v0.20 prompt signature state"
    );
    const hasProjectSkillDirs = patched.includes(
      "    project_dirs = project_dirs or []\n    cache_key = ("
    );
    const cacheKeyAnchor = hasProjectSkillDirs
      ? [
          "    disabled = get_disabled_skill_names(_platform_hint or None)",
          "    project_dirs = project_dirs or []",
          "    cache_key = (",
        ].join("\n")
      : "    disabled = get_disabled_skill_names(_platform_hint or None)\n    cache_key = (";
    patched = replaceExactlyOnce(
      patched,
      cacheKeyAnchor,
      [
        "    disabled = get_disabled_skill_names(_platform_hint or None)",
        ...(hasProjectSkillDirs
          ? ["    project_dirs = project_dirs or []"]
          : []),
        `    signature_roots = [skills_dir, *external_dirs${
          hasProjectSkillDirs ? ", *project_dirs" : ""
        }]`,
        "    live_signature = tuple(",
        "        (",
        "            str(root),",
        "            tuple(",
        "                (path, tuple(values))",
        "                for path, values in sorted(_build_skills_manifest(root).items())",
        "            ),",
        "        )",
        "        for root in signature_roots",
        "        if root.exists()",
        "    )",
        "    cache_key = (",
      ].join("\n"),
      "v0.20 prompt live signature"
    );
    if (patchKind === "skills-prompt-freshness-v0215") {
      patched = replaceExactlyOnce(
        patched,
        "        _oneshot_prompt_variant(),\n    )\n    snapshot = _load_skills_snapshot(skills_dir)",
        "        _oneshot_prompt_variant(),\n        live_signature,\n    )\n    snapshot = _load_skills_snapshot(skills_dir)",
        "v0.21.5 prompt signature cache key"
      );
      // Keep upstream oneshot partitions and app-gated cache bypass intact.
      return replaceExactlyOnce(
        patched,
        "    with _SKILLS_PROMPT_CACHE_LOCK:\n        cached = _SKILLS_PROMPT_CACHE.get(cache_key)",
        [
          "    global _SELLABLE_AGENT_SKILLS_PROMPT_SIGNATURE",
          "    with _SKILLS_PROMPT_CACHE_LOCK:",
          "        if _SELLABLE_AGENT_SKILLS_PROMPT_SIGNATURE != live_signature:",
          "            _SKILLS_PROMPT_CACHE.clear()",
          "            _SELLABLE_AGENT_SKILLS_PROMPT_SIGNATURE = live_signature",
          "        cached = _SKILLS_PROMPT_CACHE.get(cache_key)",
        ].join("\n"),
        "v0.21.5 prompt cache invalidation"
      );
    }
    patched = replaceExactlyOnce(
      patched,
      (patchKind === "skills-prompt-freshness-v021"
        ? "        _platform_hint, tuple(sorted(disabled)), tuple(sorted(compact_categories or ())),"
        : "        tuple(sorted(compact_categories or ())),") +
        "\n    )\n    with _SKILLS_PROMPT_CACHE_LOCK:\n        cached = _SKILLS_PROMPT_CACHE.get(cache_key)",
      [
        patchKind === "skills-prompt-freshness-v021"
          ? "        _platform_hint, tuple(sorted(disabled)), tuple(sorted(compact_categories or ())),"
          : "        tuple(sorted(compact_categories or ())),",
        "        live_signature,",
        "    )",
        "    global _SELLABLE_AGENT_SKILLS_PROMPT_SIGNATURE",
        "    with _SKILLS_PROMPT_CACHE_LOCK:",
        "        if _SELLABLE_AGENT_SKILLS_PROMPT_SIGNATURE != live_signature:",
        "            _SKILLS_PROMPT_CACHE.clear()",
        "            _SELLABLE_AGENT_SKILLS_PROMPT_SIGNATURE = live_signature",
        "        cached = _SKILLS_PROMPT_CACHE.get(cache_key)",
      ].join("\n"),
      "v0.20 prompt cache invalidation"
    );
    return patched;
  }

  throw new HermesAgentBridgeError(
    "HERMES_BRIDGE_UNKNOWN_PATCH_KIND",
    `Unknown Hermes bridge patch kind: ${patchKind}`,
    { patchKind }
  );
}

function normalizeContract(input) {
  if (!input || typeof input !== "object") {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_INVALID_CONTRACT",
      "Bridge contract is required"
    );
  }
  const versionFile = input.versionFile;
  if (
    !input.hermesVersion ||
    !versionFile ||
    typeof versionFile.path !== "string" ||
    !isSha256(versionFile.sha256) ||
    !Array.isArray(input.files) ||
    input.files.length === 0
  ) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_INVALID_CONTRACT",
      "Bridge contract is malformed"
    );
  }
  const seen = new Set([versionFile.path]);
  const files = input.files.map((file) => {
    if (
      !file ||
      typeof file.path !== "string" ||
      typeof file.patchKind !== "string" ||
      !isSha256(file.sourceSha256) ||
      !isSha256(file.patchedSha256) ||
      seen.has(file.path)
    ) {
      throw new HermesAgentBridgeError(
        "HERMES_BRIDGE_INVALID_CONTRACT",
        "Bridge file contract is malformed"
      );
    }
    seen.add(file.path);
    return Object.freeze({ ...file });
  });
  const normalized = {
    bridgeId: HERMES_AGENT_BRIDGE_ID,
    hermesVersion: String(input.hermesVersion),
    versionFile: Object.freeze({ ...versionFile }),
    files: Object.freeze(files),
  };
  return Object.freeze({
    ...normalized,
    contractDigest: sha256(JSON.stringify(normalized)),
  });
}

export function createHermesBridgeContractFromSources({
  hermesVersion,
  versionFilePath = "hermes_cli/__init__.py",
  versionFileSource,
  files,
}) {
  if (typeof versionFileSource !== "string" || !Array.isArray(files)) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_INVALID_CONTRACT",
      "Source-backed contract is malformed"
    );
  }
  return normalizeContract({
    hermesVersion,
    versionFile: { path: versionFilePath, sha256: sha256(versionFileSource) },
    files: files.map((file) => ({
      path: file.path,
      patchKind: file.patchKind,
      sourceSha256: sha256(file.source),
      patchedSha256: sha256(patchHermesSourceFile(file.patchKind, file.source)),
    })),
  });
}

export const HERMES_AGENT_BRIDGE_CONTRACT = normalizeContract({
  hermesVersion: "0.18.0",
  versionFile: {
    path: "hermes_cli/__init__.py",
    sha256: "58b0e216b05a3db1ec84e6eff8087a611261b78f0d3004c31cd83d93c257bbc3",
  },
  files: [
    {
      path: "gateway/config.py",
      patchKind: "gateway-config",
      sourceSha256:
        "a08cf991bc6614539389936573b0d179fbe8169db05037df496123308f651b50",
      patchedSha256:
        "6c629d4a95597c97e115a9d92f85d6f4da6312be7e5ffec81a110d0651961586",
    },
    {
      path: "plugins/platforms/slack/adapter.py",
      patchKind: "slack-adapter",
      sourceSha256:
        "20b12d9bbff0f5b280e0be8e795c7327f4089c3fa8b04d7a6d346dabae591f2a",
      patchedSha256:
        "548ab70f515e22830cbcbf6b0691768272dd708fd0118f7c601e1e1232bb016b",
    },
    {
      path: "tools/mcp_tool.py",
      patchKind: "mcp-tool",
      sourceSha256:
        "1bcceccaeb8ec8894c83819d389a64258517f4b48db1c8fea5c486633e56fe8b",
      patchedSha256:
        "651b6e036c95e0b88b9e3bb8d9c636aaea9bf37a91192083dc0b1ffb91842c47",
    },
  ],
});

export const HERMES_AGENT_BRIDGE_V019_CONTRACT = normalizeContract({
  hermesVersion: "0.19.0",
  versionFile: {
    path: "hermes_cli/__init__.py",
    sha256: "d9805993b3f400e9891c9dc97acb2ff1554f54c78061d57dc48a80493e244157",
  },
  files: [
    {
      path: "gateway/config.py",
      patchKind: "gateway-config-v019",
      sourceSha256:
        "7221ee05798566ca7cf570035615a9b29034cf92ce5a6eaa5eec0693040c08aa",
      patchedSha256:
        "764ffffdb6fb5d612b6ac226aadcf10030c24d259bd741cb98b77d18d823e580",
    },
    {
      path: "gateway/profile_routing.py",
      patchKind: "profile-routing-v019",
      sourceSha256:
        "1948c862fe2cdd3b9dd211e8bff7be1269a3b413e6c370192dac7e1f47f44c44",
      patchedSha256:
        "9dcedc1a895a2a031bc117db68c2ebba5659aadada2e15294f502bbe39147fa7",
    },
    {
      path: "plugins/platforms/slack/adapter.py",
      patchKind: "slack-adapter-v019",
      sourceSha256:
        "b89cb173a98c4ec948198556da9f8dfce66d3b44754a0a24a38f9c5f30add817",
      patchedSha256:
        "3a2d59ab3f7637d1fca668a513db71e5d8b557e7eb44cf3c1ab3d56480461867",
    },
    {
      path: "tools/mcp_tool.py",
      patchKind: "mcp-tool",
      sourceSha256:
        "a7328f3f3762ae43f6a9426646b0c28c17ec8663aa391506f48d628035ad5460",
      patchedSha256:
        "1ed7388ca812d6676007b332120ad45bc949fdbdce8eeada88f433bec5f94673",
    },
  ],
});

export const HERMES_AGENT_BRIDGE_CONTRACT_V019 =
  HERMES_AGENT_BRIDGE_V019_CONTRACT;

export const HERMES_AGENT_BRIDGE_DEDICATED_V019_CONTRACT = normalizeContract({
  hermesVersion: "0.19.1",
  versionFile: {
    path: "hermes_cli/__init__.py",
    sha256: "653172511583adb549dc18fa71d6dde11dcbf2b85db8513ce896f8631e94fffd",
  },
  files: [
    {
      path: "gateway/config.py",
      patchKind: "gateway-config-dedicated-v019",
      sourceSha256:
        "c5138c0dce57ee148399b01fc4ec56f6cf1d8d0229b16c338b01254b59816e7f",
      patchedSha256:
        "6970e3fec4c0fbe3357f8f8e03950f129e3f1c98178308bfb7f2ab99dfbc020e",
    },
    {
      path: "plugins/platforms/slack/adapter.py",
      patchKind: "slack-adapter-dedicated-v019",
      sourceSha256:
        "a58a82ce2af5137bf2452c89a5aaed6d6af562eef6fdad5597be17e15a4227ce",
      patchedSha256:
        "5b43bcb637f6cd602b2bd8642310091b4633b0934d14149d87665a91b512bba8",
    },
    {
      path: "tools/mcp_tool.py",
      patchKind: "mcp-tool",
      sourceSha256:
        "562e48fa192562000c852103c4f9f2e939dd41398605731b709389e03fd4f22d",
      patchedSha256:
        "8ed9c8bde6f35a5f159a218840a37054531269141d0ab8dfcb789de1fa0799bb",
    },
  ],
});

export const HERMES_AGENT_BRIDGE_DEDICATED_V020_CONTRACT = normalizeContract({
  hermesVersion: "0.20.4",
  versionFile: {
    path: "hermes_cli/__init__.py",
    sha256: "ac4b7eb9d0d62a050e936d8d09168efe0cc72ccd3056a4d9340069f5c39a1305",
  },
  files: [
    {
      path: "gateway/config.py",
      patchKind: "gateway-config-dedicated-v019",
      sourceSha256:
        "abb44862cc7f5b503d5c087c99dc7f923e325ccf7cf1d5e3540bf1c40837536a",
      patchedSha256:
        "2718cb4a45057b6f84623437d62f18e00197c389071394b7607fe7f8e6bf0ca2",
    },
    {
      path: "plugins/platforms/slack/adapter.py",
      patchKind: "slack-adapter-dedicated-v019",
      sourceSha256:
        "d3db079d731adf1fd3e41682376651a0ad833526cca4bf45c0b56cd1657c51a7",
      patchedSha256:
        "3c7b560d6c27e0ad151c4310b0559dd8685f6ac3a6bfc994f82d4fff4766727f",
    },
    {
      path: "tools/mcp_tool.py",
      patchKind: "mcp-tool-endpoint-v020",
      sourceSha256:
        "8ce75e7f2d999edf6554a8f5a037289c885bc9427263da1cd6e3448e608fe20f",
      patchedSha256:
        "e4bf77633c8333d1f79310768c0824e8eaed87938ec7ffd2478050f12f5d36a6",
    },
    {
      path: "gateway/run.py",
      patchKind: "gateway-session-v020",
      sourceSha256:
        "97a3173d1a7ee0df98822d3ba173c72e997e52460bf64193b6f2c68ab20a48e3",
      patchedSha256:
        "af08998cb177636bc575ef524d94318911a99870b39bf293e3fa1adfaba96120",
    },
    {
      path: "agent/turn_context.py",
      patchKind: "turn-context-v020",
      sourceSha256:
        "4a65e543a0795c341fd7598ac42a40eb2d87da6637a3521ecc3a00096eb56896",
      patchedSha256:
        "f4768b3992a1a64832b82ac66981a014c1ef53bfd3f336f3d1890e348a107d7f",
    },
    {
      path: "cron/scheduler.py",
      patchKind: "cron-scheduler-v020",
      sourceSha256:
        "ac0d2f0edcfaf26ffa21aa4e7478b44bb78e6f43e3072697e2849f2b1b13b7e7",
      patchedSha256:
        "c0d3e30b9eeca1871b7133924ec0621d7a5265536379c26561e6e3062bd7c1cf",
    },
    {
      path: "tools/skills_tool.py",
      patchKind: "skills-cache-freshness-v020",
      sourceSha256:
        "c601bc4ed4dac3fbea22666239d6aa3085bbe2f4a4c0d2db1ab3d2eb5d947de9",
      patchedSha256:
        "af89fb92a4b0c8032328e7f26c70925e581e3d1208d24fedf5b0df2df4b5c209",
    },
    {
      path: "agent/prompt_builder.py",
      patchKind: "skills-prompt-freshness-v020",
      sourceSha256:
        "bfb9a774d0c6c2490bc26fcef727ed2d38d0be2fab62a4782f69230101c9e997",
      patchedSha256:
        "30d7d9a45e3cce31ed5805bb048914dec577e743d6e21be573dec0cfa579d0b7",
    },
  ],
});

export const HERMES_AGENT_BRIDGE_DEDICATED_V021_CONTRACT = normalizeContract({
  bridgeId: "sellable-agent-hermes-bridge/v1",
  hermesVersion: "0.21.1",
  versionFile: {
    path: "hermes_cli/__init__.py",
    sha256: "efae023ac37622277aac298ac15c646b855e870132c38970feeda9552f1ecc27",
  },
  files: [
    {
      path: "gateway/platforms/api_server.py",
      patchKind: "api-opener-stage-v021",
      sourceSha256:
        "221589ae8c45349736bb291b4679b86e9e12cb22f5a33220ae3d22929fea8297",
      patchedSha256:
        "b029a26050aa777a520ab136c203f3755b178e04af11ca6e9f810046a9eae6d4",
    },
    {
      path: "gateway/config_env.py",
      patchKind: "gateway-config-dedicated-v021",
      sourceSha256:
        "984ae7b68befecd52fc17b6d38e763f40c7040a2ba69ed4a2c39f1a88ae6734a",
      patchedSha256:
        "0180fbcb1df6e5a7651f172c492a8e317f7455c8ed0e955c7a4f0488fa23b6c2",
    },
    {
      path: "plugins/platforms/slack/adapter.py",
      patchKind: "slack-adapter-dedicated-v021",
      sourceSha256:
        "23048a107304993686250af044256199be2f73171bb5b4cca6a900ea272a6064",
      patchedSha256:
        "bc05fd5f227e42f733ee78c80ffab6c7c53840975fc5bc9755aa2ef8f2c8ff47",
    },
    {
      path: "tools/mcp_tool_handlers.py",
      patchKind: "mcp-tool-endpoint-v021",
      sourceSha256:
        "e6af250f0fbfeb12a936c340a46f40f8d40332ed8bbc4eb63fded66e37aaa7bf",
      patchedSha256:
        "6c4765aad3074b6f8ad6951299edafa36cf536c9a9f0b57a48bdd541e3209790",
    },
    {
      path: "gateway/run.py",
      patchKind: "gateway-session-v021",
      sourceSha256:
        "3b1f670243d4932ac1f2a2a68bee61a8c7f9bcdac7bd48d43289db90f17bab67",
      patchedSha256:
        "2e25d50add6c49748b2cdd4c2bd115039e72c0b4eeb9c882eb1cd07a2a25c304",
    },
    {
      path: "gateway/run_turn_runner.py",
      patchKind: "gateway-provider-preflight-v021",
      sourceSha256:
        "8ebd74318db2259ac52c35dbf9568b98dbb19c1d2e30289f4efc76a12ac8ceeb",
      patchedSha256:
        "26a13f167835036c37ff92da3c1d036267544cf8a6529f5d40c3ee0ae58fa1cb",
    },
    {
      path: "agent/turn_context.py",
      patchKind: "turn-context-v021",
      sourceSha256:
        "d1a84a24c5b50a7a866268fde453694c6b98790e31d7d4310d8756027149863a",
      patchedSha256:
        "13517e034866224c072a860dbd9fbb7eb25c9d3cb86a1f59ae3a2e5ec1cef7ad",
    },
    {
      path: "cron/scheduler.py",
      patchKind: "cron-scheduler-v020",
      sourceSha256:
        "f1891e94697ccf5f7b66ae7788345368b452af447fbc43356ae5376a3c753518",
      patchedSha256:
        "03d0848e584b7cbc5b126e0fae989dbd5d3d83ccee3ad989c08ffc8f8ef423b9",
    },
    {
      path: "tools/skills_tool.py",
      patchKind: "skills-cache-freshness-v021",
      sourceSha256:
        "0f205b8c2d9266d272d8c6cae3d189b86a5aebce98f03b61605d266b65f9497c",
      patchedSha256:
        "8696b37ef58c0cc570c437abac730ac0e670e44365ddeae452ad40a501f13a1b",
    },
    {
      path: "agent/prompt_builder.py",
      patchKind: "skills-prompt-freshness-v021",
      sourceSha256:
        "83160fab72772012bc308bc730a0085e7acb126f1bea49d62f990505f00ce677",
      patchedSha256:
        "6ab2aa30e06b124333d28ed73fc7b56394fbc71f040f3387f6fbc835d799f266",
    },
  ],
  contractDigest:
    "a8520b6decd64b43de1074005c8eda2d9be4a451f0f9a43b1777659acd5a86b6",
});

export const HERMES_AGENT_BRIDGE_DEDICATED_V0213_CONTRACT = normalizeContract({
  bridgeId: "sellable-agent-hermes-bridge/v1",
  hermesVersion: "0.21.3",
  versionFile: {
    path: "hermes_cli/__init__.py",
    sha256: "0d78a58a9f27f32adfdac959e89767cecde93a424bcfbd39d64fb95f6cf13e6c",
  },
  files: [
    {
      path: "gateway/config_env.py",
      patchKind: "gateway-config-dedicated-v021",
      sourceSha256:
        "40bb1029e482ebe423a5a6ddc9175ef96f821f89e9e040b5cf62cdd7ec903e4c",
      patchedSha256:
        "c189faaa2ab7c0ea603b44eb3e6b37dcf847a83b12f603ca185f37ba0ca582fc",
    },
    {
      path: "plugins/platforms/slack/adapter.py",
      patchKind: "slack-adapter-dedicated-v0213",
      sourceSha256:
        "12bb80dffde14eca299f6f0fa57907fbe1636519402c08dc07d96d1c144e5f83",
      patchedSha256:
        "da13c90b8a81c32b57909e5d82f9e3500d14b290cf9a38ffd6dd53e77785e038",
    },
    {
      path: "tools/mcp_tool_handlers.py",
      patchKind: "mcp-tool-endpoint-v021",
      sourceSha256:
        "c4713aa289c7ca17b53c0ae53ceb6a276321f6d36e259084974e8b5738b6a4dd",
      patchedSha256:
        "1bd801ebf1c6e17a0d81899b2ee0401f038ae4d1a493552f021a1b66fccf0e98",
    },
    {
      path: "gateway/run.py",
      patchKind: "gateway-session-v021",
      sourceSha256:
        "ab619aa0dc193c6d8741f3cabc9a028c2fb7459c22262c69597b0e872091bde5",
      patchedSha256:
        "50fc00c3fa236c201d4f4e0831ec728d87ee7ad16b0865c69242304a5de65a23",
    },
    {
      path: "gateway/run_turn.py",
      patchKind: "gateway-first-contact-v0213",
      sourceSha256:
        "56e6fb62b60e954ef0d258c7c44ed3fca99c4aa6d2c502d8f80a1c516569f537",
      patchedSha256:
        "d66e2aecd00ebb1c6f30a1ecc522b1f408d4a8ff1413b63516752e17ea0e55a1",
    },
    {
      path: "gateway/run_turn_runner.py",
      patchKind: "gateway-provider-preflight-v021",
      sourceSha256:
        "b87d6072f2cc13634398d380eb0857d550f75502a8ace137138cf16e00d9a763",
      patchedSha256:
        "0113b3df868e73c7f06bacf0c5cec916077e21973e3c1b03b6778c3b91e92b8b",
    },
    {
      path: "agent/turn_context.py",
      patchKind: "turn-context-v021",
      sourceSha256:
        "46ed030ee35a13f75994bf8e28cde3633db14969d1fb5c859fc0ff6964cbb0bd",
      patchedSha256:
        "facc6ff45a479766b0c0fe719d96fd6867ee25c40e638d5e82499cc97bd72e04",
    },
    {
      path: "cron/scheduler.py",
      patchKind: "cron-scheduler-v020",
      sourceSha256:
        "df7b82c50485cece408264f3cd44dc910b6b2a7d319b6d138bd42e2cf7ba2e86",
      patchedSha256:
        "ea8b4df9d12dbd82c59044d401de5e99ed466657ac873f018ac5d8d741893b2c",
    },
    {
      path: "tools/skills_tool.py",
      patchKind: "skills-cache-freshness-v021",
      sourceSha256:
        "1bec6a244ac61603b1adeb5599149a4097b68263458f63eafe55836415e5f3a0",
      patchedSha256:
        "168bad88d45af80328c38296be3f1c9b3509f680a7939735836feac94328843e",
    },
    {
      path: "agent/prompt_builder.py",
      patchKind: "skills-prompt-freshness-v021",
      sourceSha256:
        "586ea363fa1e70bb0fdd5426af40758976a16c54f07efeb7a1b4f3fe0ad99309",
      patchedSha256:
        "769a0662a396bcb0ffb82e14cacd03b2b864526e68c1cde601f68ccea61e2836",
    },
    {
      path: "gateway/platforms/api_server.py",
      patchKind: "api-opener-stage-v021",
      sourceSha256:
        "7ec405dc2a59789582ad9f61a189a41079e621f14642d3c9dc754e312616a868",
      patchedSha256:
        "1423ea98ec142e9b0dc5ba423dadfe9cf192c39c0565cf58cafc374805725052",
    },
  ],
  contractDigest:
    "75b5d16081dd2fdc5795d3b86d341f2ff341bf3c599e4c57de5ec14b3dd63ce0",
});

export const HERMES_AGENT_BRIDGE_DEDICATED_V0215_CONTRACT = normalizeContract({
  bridgeId: "sellable-agent-hermes-bridge/v1",
  hermesVersion: "0.21.5",
  versionFile: {
    path: "hermes_cli/__init__.py",
    sha256: "d05c98cd44e22781497a20cd968af0d9b8962406b909f1889168c624545b229e",
  },
  files: [
    {
      path: "gateway/config_env.py",
      patchKind: "gateway-config-dedicated-v021",
      sourceSha256:
        "5661d9cfb7d798d6c995b03727bdd4018669ae12c96d1e7a8f3c99a3ea85acc7",
      patchedSha256:
        "cd8efa414e0313bca77d0d8093acc505d9f345b8fb48d734821e66027545bf57",
    },
    {
      path: "plugins/platforms/slack/adapter.py",
      patchKind: "slack-adapter-shared-v0215",
      sourceSha256:
        "3f2ec0cef97964bf4cb1e9791c0917a03cd1e73bb0bc3293768680cd6302f613",
      patchedSha256:
        "6b89898ec588fb8df5475179cd53966e4913c18d66d40b05650f5b80ff9736b6",
    },
    {
      path: "tools/mcp_tool_handlers.py",
      patchKind: "mcp-tool-endpoint-v021",
      sourceSha256:
        "d1b4463309a2de351aaf6c7cd8f7a2a1e83f5e26dbe058371c9eeb9d0c567903",
      patchedSha256:
        "61ceab8c24aef50e88d6c1dad1bbf80daed66f8950b05fb642525a62a0bb789c",
    },
    {
      path: "gateway/run.py",
      patchKind: "gateway-session-copy-v0215",
      sourceSha256:
        "324f7095223816b865dd68a75dc83bfd1c3d101ac8c1c38dba69026dab2b6af1",
      patchedSha256:
        "81eaea618888ecba1b651ec07279e67fa32061f51f7d1a2407fa989ede93806c",
    },
    {
      path: "gateway/run_turn.py",
      patchKind: "gateway-posted-reply-v0215",
      sourceSha256:
        "5bdee5c82c00e02f664516f9da0af99674e23493ec2a30eff06cb03afc6718a0",
      patchedSha256:
        "7d05cd81c51b7dd5ad1d3f1720fa4d7fa6127d07d0c3b513733a5d56ad667afd",
    },
    {
      path: "gateway/run_turn_runner.py",
      patchKind: "gateway-provider-preflight-copy-v0215",
      sourceSha256:
        "216bdca083b7d2bfae481d07bd0791a76834674b3614fd4ee0aafac5c58f30db",
      patchedSha256:
        "236bc879348c237983ceea3c86b568ded228fa9d725c495fcb88354cd1253956",
    },
    {
      path: "agent/turn_context.py",
      patchKind: "turn-context-v021",
      sourceSha256:
        "85f857dc3c317366d918516bc8b37a022f1020b34da0c3966a31bd289b2942e1",
      patchedSha256:
        "a6487c43bc2d21f40e3a86fe31a9a0c2e4e603fff813f7c8429bbf5c22591648",
    },
    {
      path: "cron/scheduler.py",
      patchKind: "cron-scheduler-v0215",
      sourceSha256:
        "dcf0e6794a220e478584a21f786b8451b915fc7397d38eda357af1363249fa2a",
      patchedSha256:
        "0476de14a517ac681f9ad06228f3af46b0dd3f730a2cb3216c35311706ef65e8",
    },
    {
      path: "tools/skills_tool.py",
      patchKind: "skills-cache-freshness-v021",
      sourceSha256:
        "0d47bda41ed9d1ec84856df3d336a42bb94161e89bd9a8b857e76ec76be9f7b7",
      patchedSha256:
        "1bdc17603f3bb9fb34c5d04099dee2ca7d631eedc58b523bcd74d6e946dc9eb7",
    },
    {
      path: "agent/prompt_builder.py",
      patchKind: "skills-prompt-freshness-v0215",
      sourceSha256:
        "64bc77a26641754b8c5ca76fc876687d3c9e81aad7816c2e76ce251a41121526",
      patchedSha256:
        "56edefd92c50f38a3fed8d56b6fc02352f8e5892b43553ab277e3113606d25fb",
    },
    {
      path: "gateway/platforms/api_server.py",
      patchKind: "api-opener-stage-v021",
      sourceSha256:
        "fa83a20bd4f9f3a90a3ac010db0a68f548259587946ea80b81b2f3253bafd5ec",
      patchedSha256:
        "dbe6e400c7c7a9d3b377b7f9f08e05f2e9f6b622f9019f873e4fdb0cd828b19f",
    },
    {
      path: "agent/turn_failure_copy.py",
      patchKind: "turn-failure-copy-v0215",
      sourceSha256:
        "64311f06bb7022749852f291d60b566592e45955f76b192d84fc227373f6afe3",
      patchedSha256:
        "d0a092640016cc31704ff76f0df2b5ef4b829d3ac4d4bbc99ae21ebadbb2be01",
    },
    {
      path: "gateway/run_busy.py",
      patchKind: "gateway-busy-copy-v0215",
      sourceSha256:
        "494cb6c868f4653c88fc2fe8af5d4b5d434cb9570164c2956cf890fbe6f5433a",
      patchedSha256:
        "fbda52798ae7e5372e9fb74876183e135bb63bf1fbb9f5176b89c511dd61d7b0",
    },
    {
      path: "gateway/slash_commands.py",
      patchKind: "slash-commands-copy-v0215",
      sourceSha256:
        "d50f7b34aef17ba9c9b80da39e51060f1d140dd829a1b526b539496473b3ea7e",
      patchedSha256:
        "74501e3f55091f27d089fae022a46888e34edb1cd348958565f086fa1c491fcd",
    },
    {
      path: "tools/terminal_tool.py",
      patchKind: "terminal-foreground-modifiers-v0215",
      sourceSha256:
        "6eb6696b527c5bfc209195206db05331468cca061a30f5edbe00dbf9ba5b263d",
      patchedSha256:
        "bcfb14a60cd4cf7a0b958de1fed997abf752cffe6b32051f04ddd4ab29da7ca7",
    },
    {
      path: "agent/stream_delivery.py",
      patchKind: "stream-delivery-posted-commentary-v0215",
      sourceSha256:
        "e1d9d72b2f86fe24b650c8cd352ccee0156ed0af8a891136225cbd21501b3aad",
      patchedSha256:
        "ef7efd2c7b5cf6b8576ae0b2f473d237b9a27c55de1d680b8f93375f20f74350",
    },
    {
      path: "agent/turn_tool_round.py",
      patchKind: "tool-round-posted-commentary-v0215",
      sourceSha256:
        "45d55ae294290398f48ed2b333f67c50817ea5360f43a4ba2252ad7e758002fc",
      patchedSha256:
        "6190666042eb8f3eab174ffc39a63f2b5e9e76b14b4054d783cabd77907c2d5f",
    },
    {
      path: "run_agent.py",
      patchKind: "run-agent-halt-copy-v0215",
      sourceSha256:
        "244da863d3c21591a3b5326dc14c2962d4e31131dda52df628502cd9fcbfea33",
      patchedSha256:
        "0cf6f5b2299299612234301dfabc65fc35547550b6187a8dd20de27843a3d208",
    },
  ],
  contractDigest:
    "ae4340dce6f9563af1b9219b42d0962ee6c05358ac65a32cab6c11fb3d690370",
});

// Hermes 0.23.5 retires four bridge files relative to 0.21.5:
// - gateway/run.py (upstream binds context.session_id and moved chat copy to locales/en.yaml)
// - agent/prompt_builder.py (upstream hashes every SKILL.md/DESCRIPTION.md via always_digest=True)
// - gateway/run_busy.py and gateway/slash_commands.py (moved to per-profile locales/en.yaml overlay)
// Upstream also split gateway/run_turn.py into run_turn.py, run_turn_persistence.py, and run_turn_prepare.py.
export const HERMES_AGENT_BRIDGE_DEDICATED_V0235_CONTRACT = normalizeContract({
  bridgeId: "sellable-agent-hermes-bridge/v1",
  hermesVersion: "0.23.5",
  versionFile: {
    path: "install-stamp.json",
    sha256: "07ec97a7f47f9d50c2dd96232f9c68b3017bc7d0295fec5aa4813b0bed7b75c7",
  },
  files: [
    {
      path: "gateway/platform_env.py",
      patchKind: "platform-env-dedicated-v0235",
      sourceSha256:
        "9da1814a3e0ac93a412a8b31d78ca77a850429fa7d0a209cffd9aedfb688c40d",
      patchedSha256:
        "0446725b1524c5f62a23d7736cdab9a3486189cc434fd80e883b2944d45abb7c",
    },
    {
      path: "plugins/platforms/slack/adapter.py",
      patchKind: "slack-adapter-shared-v0235",
      sourceSha256:
        "b580b4d9217700a4576dbc700b1b19e64fae05ecbed31624b8f7ae98dfc6968c",
      patchedSha256:
        "70f05740de48c6836f37547c71953f8dace9cfda7288b108e58f52fbd1c6bfba",
    },
    {
      path: "tools/mcp_tool_handlers.py",
      patchKind: "mcp-tool-endpoint-v0235",
      sourceSha256:
        "38ea1f8d84b4bdae1a085476445f73432a5bc5e272a0b0e4cad042959c984116",
      patchedSha256:
        "8409de8c342e9f59085099378815e54ecc2bab4689c3a5a19d6619112a316750",
    },
    {
      path: "tools/tool_search.py",
      patchKind: "tool-search-eager-v0235",
      sourceSha256:
        "7597bc2239ebe55c81cd952f5d900da3490408989441b50d2ede166b95c113cf",
      patchedSha256:
        "6adbd2ebfa345ea5382629b47df277cfa32710c9aa30b8b5fe49059aa9c0ed80",
    },
    {
      path: "gateway/run_turn.py",
      patchKind: "gateway-posted-reply-v0235",
      sourceSha256:
        "aab16b3e3930d7a694a8af3b6ffc3bfb5fe176b809d38a8ed956d6026073d18a",
      patchedSha256:
        "b7db9c9b488bcb73eb336e1bc51728dab29e6a0dd2b122635ed15f49dffb387f",
    },
    {
      path: "gateway/run_turn_persistence.py",
      patchKind: "gateway-posted-reply-persistence-v0235",
      sourceSha256:
        "388ed2200bfc5bc983585e28e3755c4e14daa57ed55b79b5d6afc15fe7ed3f30",
      patchedSha256:
        "34de6d57cae82f8e38e3259cdae26f6657f4b67585117105cc755b4abeedfded",
    },
    {
      path: "gateway/run_turn_prepare.py",
      patchKind: "gateway-first-contact-v0235",
      sourceSha256:
        "5ff3ab81b38d019b8568126dcfb24424db0553c377fdbaeff9b19ce8b42f4848",
      patchedSha256:
        "014f2ac8d57b989915bb77e820bf6bbeb6c61ae2f8479a329fbd19895d58a7ad",
    },
    {
      path: "gateway/run_turn_runner.py",
      patchKind: "gateway-provider-preflight-v021",
      sourceSha256:
        "3843440af2cd02c925e6adebd093090d35dbec5df79d8bb627d4f60960da2c97",
      patchedSha256:
        "f45d340c69b51938386277f77c6a9ad1672b0cda91e42ed61a41b7ac05f5e5ae",
    },
    {
      path: "agent/turn_context.py",
      patchKind: "turn-context-v0235",
      sourceSha256:
        "9935a825c587d1c5ea9f96e64a0083a3c965e9d7a9d79834d41ca5fde6d0337d",
      patchedSha256:
        "5ba57bd5f60c650e42542c9b55eba545415eaff66299a377d5abfe5aabfdacb7",
    },
    {
      path: "cron/scheduler.py",
      patchKind: "cron-scheduler-v0215",
      sourceSha256:
        "3b62bb315efa2f9adb7f860aa16a2de097f0cb7464aee05ab14457082a9676e3",
      patchedSha256:
        "f7cfd696fcde6957dda1bcb280c073e4e24fa2d92b4dd45b6bc6d6801f1f891f",
    },
    {
      path: "tools/skills_tool.py",
      patchKind: "skills-cache-freshness-v0235",
      sourceSha256:
        "492d1cc49f0f3e982aed98c8538d455988c06b376fb4560f473f6c1f602c3154",
      patchedSha256:
        "04405a16356f37a5080e939870cbf23b0be8e462cac5d8331e0cf6655ac20529",
    },
    {
      path: "gateway/platforms/api_server.py",
      patchKind: "api-opener-stage-v021",
      sourceSha256:
        "90a6633ee54ab54795c0c93d0e79687a580ebc1738ff635f754e9b2ee5803df6",
      patchedSha256:
        "cdbf49cd9a231d2e19f10ce03282d40f8543ac2436d30db1c627832e471bea62",
    },
    {
      path: "agent/turn_failure_copy.py",
      patchKind: "turn-failure-copy-v0235",
      sourceSha256:
        "ca9769f93386067ab8a3309b1e9928af1333853065a4736017eeca0d5a96871b",
      patchedSha256:
        "514d2df642f1cdb772786ccb59e91a1b130a91b89dc957f2b31750bc836d5347",
    },
    {
      path: "tools/terminal_tool.py",
      patchKind: "terminal-foreground-modifiers-v0235",
      sourceSha256:
        "eaefd9ea7abff927274c7664a9d6b93c96dd34d7f1b95b42a3748064ff120ce7",
      patchedSha256:
        "0200197f984906a06db215541688d8c494b8d1e959d8d05bc4bcabeb730a0672",
    },
    {
      path: "plugins/web/keyless_mcp.py",
      patchKind: "keyless-exa-full-page-v0235",
      sourceSha256:
        "6d76940d1b4b2568715d3bab727f53061c44ce3e26ea6b9fca5d05fa5bb09d5f",
      patchedSha256:
        "17d88344449f11cba050c3fe2451088d7befce7a1e6dc90de0966400cd123145",
    },
    {
      path: "agent/stream_delivery.py",
      patchKind: "stream-delivery-posted-commentary-v0215",
      sourceSha256:
        "49434dd6fc40f2c6eb57ddc758637d6de79ee1bef39175ce44c9591265b6af5a",
      patchedSha256:
        "6ed5543b7c752eae6c72a6fbaa7979d5516eb9fd6f6590cf9ea7b59835252145",
    },
    {
      path: "agent/turn_tool_round.py",
      patchKind: "tool-round-posted-commentary-v0215",
      sourceSha256:
        "9cd3be661ea93acb51985c2907dafced48b6df6238060903bd9608a560c83b17",
      patchedSha256:
        "651d4d1ca7c3295ed20d57cec83be901cd518befc6eaffdc29d819e5479cf571",
    },
    {
      path: "run_agent.py",
      patchKind: "run-agent-halt-copy-v0215",
      sourceSha256:
        "aa83627f2afaf35a12b6b905a07de31c96e825dc0fa22cb8533b0300135dbd2d",
      patchedSha256:
        "fa0bddea3617542fc49bb1ec0b7a5f52f42a28f7a01260987641ffac9cd96835",
    },
  ],
  contractDigest:
    "5dd4a4f0992c706aa4c1b30119283865fb53b454a749a913662d1d93c3ba0cf1",
});

function assertRelativeContractPath(path) {
  if (
    !path ||
    isAbsolute(path) ||
    path.split(/[\\/]+/).some((part) => part === ".." || part === "")
  ) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_UNSAFE_PATH",
      "Bridge contract path is unsafe",
      { path }
    );
  }
}

function resolveConfined(root, path) {
  assertRelativeContractPath(path);
  const candidate = resolve(root, path);
  const rel = relative(root, candidate);
  if (!rel || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_UNSAFE_PATH",
      "Bridge path escapes source root",
      { path }
    );
  }
  return candidate;
}

function assertSafeRegularFile(root, path) {
  const candidate = resolveConfined(root, path);
  let cursor = root;
  for (const component of relative(root, candidate).split(sep)) {
    cursor = join(cursor, component);
    const info = lstatSync(cursor);
    if (info.isSymbolicLink()) {
      throw new HermesAgentBridgeError(
        "HERMES_BRIDGE_SYMLINK",
        "Hermes bridge paths may not contain symlinks",
        { path }
      );
    }
    if (
      cursor !== candidate &&
      (!info.isDirectory() || (info.mode & 0o022) !== 0)
    ) {
      throw new HermesAgentBridgeError(
        "HERMES_BRIDGE_UNSAFE_DIRECTORY",
        "Hermes bridge parent directories must be real and not group/world writable",
        { path }
      );
    }
  }
  const info = lstatSync(candidate);
  if (!info.isFile()) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_NOT_REGULAR",
      "Hermes bridge target is not a regular file",
      { path }
    );
  }
  if ((info.mode & 0o022) !== 0) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_WRITABLE_SOURCE",
      "Hermes bridge target is group/world writable",
      { path }
    );
  }
  return { candidate, info };
}

function validateSourceRoot(sourceRoot) {
  if (
    typeof sourceRoot !== "string" ||
    !isAbsolute(sourceRoot) ||
    resolve(sourceRoot) !== sourceRoot
  ) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_UNSAFE_ROOT",
      "Hermes source root must be an absolute normalized path"
    );
  }
  const rootInfo = lstatSync(sourceRoot);
  if (
    !rootInfo.isDirectory() ||
    rootInfo.isSymbolicLink() ||
    realpathSync(sourceRoot) !== sourceRoot
  ) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_UNSAFE_ROOT",
      "Hermes source root must be a real directory"
    );
  }
  if ((rootInfo.mode & 0o022) !== 0) {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_WRITABLE_ROOT",
      "Hermes source root is group/world writable"
    );
  }
  return sourceRoot;
}

function readVersion(source, path = "") {
  if (typeof path === "string" && path.endsWith(".json")) {
    try {
      const parsed = JSON.parse(source);
      const version =
        parsed?.baseVersion ?? parsed?.displayVersion ?? parsed?.version;
      return typeof version === "string" && version.trim()
        ? version.trim()
        : null;
    } catch {
      return null;
    }
  }
  const match = source.match(/^__version__\s*=\s*["']([^"']+)["']/m);
  return match?.[1] ?? null;
}

function publicFileReceipts(files) {
  return files.map(({ path, sha256: digest }) => ({ path, sha256: digest }));
}

export function inspectHermesAgentBridge({
  sourceRoot,
  contract = HERMES_AGENT_BRIDGE_CONTRACT,
}) {
  const pinned = normalizeContract(contract);
  const root = validateSourceRoot(sourceRoot);
  const versionTarget = assertSafeRegularFile(root, pinned.versionFile.path);
  const versionBytes = readFileSync(versionTarget.candidate);
  const versionSource = versionBytes.toString("utf8");
  const versionDigest = sha256(versionBytes);
  const version = readVersion(versionSource, pinned.versionFile.path);
  const reasons = [];
  if (versionDigest !== pinned.versionFile.sha256)
    reasons.push("VERSION_FILE_HASH_MISMATCH");
  if (version !== pinned.hermesVersion) reasons.push("HERMES_VERSION_MISMATCH");

  const files = pinned.files.map((file) => {
    const target = assertSafeRegularFile(root, file.path);
    const digest = sha256(readFileSync(target.candidate));
    const state =
      digest === file.sourceSha256
        ? "source"
        : digest === file.patchedSha256
          ? "patched"
          : "unknown";
    if (state === "unknown") reasons.push(`UNKNOWN_SOURCE:${file.path}`);
    return {
      ...file,
      absolutePath: target.candidate,
      mode: target.info.mode & 0o777,
      sha256: digest,
      state,
    };
  });
  const knownStates = new Set(files.map((file) => file.state));
  if (knownStates.has("source") && knownStates.has("patched"))
    reasons.push("MIXED_BRIDGE_STATE");

  let state = "blocked";
  if (
    reasons.length === 0 &&
    knownStates.size === 1 &&
    knownStates.has("source")
  )
    state = "needs_install";
  if (
    reasons.length === 0 &&
    knownStates.size === 1 &&
    knownStates.has("patched")
  )
    state = "installed";
  return Object.freeze({
    bridgeId: pinned.bridgeId,
    contractDigest: pinned.contractDigest,
    hermesVersion: version,
    state,
    reasons: Object.freeze(reasons),
    files: Object.freeze(publicFileReceipts(files)),
  });
}

function fsyncDirectory(path) {
  const fd = openSync(path, constants.O_RDONLY);
  try {
    fsyncSync(fd);
  } finally {
    closeSync(fd);
  }
}

function atomicReplace(candidate, bytes, { mode, uid, gid }) {
  const temp = join(
    dirname(candidate),
    `.${candidate.split(sep).at(-1)}.sellable-agent-${process.pid}-${Date.now()}.tmp`
  );
  let fd;
  try {
    fd = openSync(
      temp,
      constants.O_CREAT |
        constants.O_EXCL |
        constants.O_WRONLY |
        constants.O_NOFOLLOW,
      mode
    );
    writeFileSync(fd, bytes);
    fchmodSync(fd, mode);
    fchownSync(fd, uid, gid);
    fsyncSync(fd);
    closeSync(fd);
    fd = undefined;
    renameSync(temp, candidate);
    fsyncDirectory(dirname(candidate));
  } finally {
    if (fd !== undefined) closeSync(fd);
    if (existsSync(temp)) unlinkSync(temp);
  }
}

export function installHermesAgentBridge({
  sourceRoot,
  contract = HERMES_AGENT_BRIDGE_CONTRACT,
}) {
  const pinned = normalizeContract(contract);
  const before = inspectHermesAgentBridge({ sourceRoot, contract: pinned });
  if (before.state === "installed")
    return Object.freeze({ ...before, status: "already_current" });
  if (before.state !== "needs_install") {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_SOURCE_REJECTED",
      "Hermes source does not match the pinned bridge preimage",
      { state: before.state, reasons: before.reasons }
    );
  }

  const staged = pinned.files.map((file) => {
    const { candidate, info } = assertSafeRegularFile(sourceRoot, file.path);
    const original = readFileSync(candidate);
    const patched = Buffer.from(
      patchHermesSourceFile(file.patchKind, original.toString("utf8")),
      "utf8"
    );
    const digest = sha256(patched);
    if (digest !== file.patchedSha256) {
      throw new HermesAgentBridgeError(
        "HERMES_BRIDGE_PATCH_DIGEST_MISMATCH",
        "Generated Hermes patch does not match the pinned postimage",
        { path: file.path }
      );
    }
    return {
      ...file,
      candidate,
      ownership: { mode: info.mode & 0o777, uid: info.uid, gid: info.gid },
      original,
      patched,
    };
  });

  const replaced = [];
  try {
    for (const file of staged) {
      atomicReplace(file.candidate, file.patched, file.ownership);
      replaced.push(file);
    }
  } catch (error) {
    const rollbackErrors = [];
    for (const file of replaced.reverse()) {
      try {
        atomicReplace(file.candidate, file.original, file.ownership);
      } catch (rollbackError) {
        rollbackErrors.push({
          path: file.path,
          message:
            rollbackError instanceof Error
              ? rollbackError.message
              : String(rollbackError),
        });
      }
    }
    throw new HermesAgentBridgeError(
      rollbackErrors.length
        ? "HERMES_BRIDGE_ROLLBACK_FAILED"
        : "HERMES_BRIDGE_INSTALL_FAILED",
      rollbackErrors.length
        ? "Hermes bridge install and rollback failed"
        : "Hermes bridge install failed and was rolled back",
      {
        rollbackErrors,
        cause: error instanceof Error ? error.message : String(error),
      }
    );
  }

  const after = inspectHermesAgentBridge({ sourceRoot, contract: pinned });
  if (after.state !== "installed") {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_VERIFY_FAILED",
      "Hermes bridge post-install verification failed",
      {
        state: after.state,
        reasons: after.reasons,
      }
    );
  }
  return Object.freeze({ ...after, status: "installed" });
}

export function verifyHermesAgentBridge({
  sourceRoot,
  contract = HERMES_AGENT_BRIDGE_CONTRACT,
}) {
  const observation = inspectHermesAgentBridge({ sourceRoot, contract });
  if (observation.state !== "installed") {
    throw new HermesAgentBridgeError(
      "HERMES_BRIDGE_NOT_INSTALLED",
      "Pinned Hermes bridge is not installed",
      {
        state: observation.state,
        reasons: observation.reasons,
      }
    );
  }
  return Object.freeze({ ...observation, status: "verified" });
}