// Force strict mode and setup for ESM
"use strict";
import {
  redactLogCredentials
} from "./chunk-M5GB774H.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/serve/workspace-remember-errors.ts
init_esbuild_shims();
var MAX_REMEMBER_ERROR_DETAILS_CHARS = 1e3;
var MAX_REMEMBER_ERROR_CAUSE_DEPTH = 50;
var REMEMBER_ERROR_FORMAT_RE = new RegExp("[\\p{Cf}]|\\p{Variation_Selector}", "gu");
var REMEMBER_ERROR_SPACE_SEPARATOR_RE = /[\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]/gu;
var REMEMBER_ERROR_CREDENTIAL_SEPARATOR_RE = new RegExp("[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector}", "gu");
var REMEMBER_ERROR_AUTH_SCHEME_INVISIBLE_RE = new RegExp("\\b(Bearer|QQBot)(?:[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector})+(?=[A-Za-z0-9._~+/=-])", "giu");
var REMEMBER_ERROR_AUTH_TOKEN_WITH_SEPARATORS_RE = new RegExp("\\b(Bearer|QQBot)(?:\\s|[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector})+((?:[A-Za-z0-9._~+/=-]+(?:[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector})+)*[A-Za-z0-9._~+/=-]+)", "giu");
var REMEMBER_ERROR_BARE_TOKEN_WITH_SEPARATORS_RE = new RegExp("\\b(?:sk-|ghp_|gho_|ghs_|ghu_|github_pat_|glpat-|xox[b]-|xox[p]-)(?:[A-Za-z0-9_-]+(?:[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector})+)*[A-Za-z0-9_-]+", "gu");
var REMEMBER_ERROR_AWS_KEY_WITH_SEPARATORS_RE = new RegExp("\\b(?:AKIA|ASIA)(?:[A-Z0-9]+(?:[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector})+)*[A-Z0-9]+", "gu");
var REMEMBER_ERROR_SECRET_ASSIGNMENT_WITH_SEPARATORS_RE = new RegExp("((?:api[_-]?key|token|secret|password|pwd)[_-]?[=:]\\s*)((?:\\S+(?:[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector})+)*\\S+)", "giu");
var REMEMBER_ERROR_ENV_SECRET_ASSIGNMENT_WITH_SEPARATORS_RE = new RegExp("([A-Z][A-Z0-9]{0,50}(?:_[A-Z0-9]{1,50}){0,10}_(?:KEY|TOKEN|SECRET|PASSWORD)\\s*[=:]\\s*)((?:\\S+(?:[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector})+)*\\S+)", "gu");
var REMEMBER_ERROR_URL_CREDENTIALS_WITH_SEPARATORS_RE = new RegExp("(\\b[a-z][a-z0-9+.-]{0,31}:\\/\\/)([^/@\\s]*(?:[\\x00-\\x1f\\x7f-\\x9f\\p{Cf}\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]|\\p{Variation_Selector})+[^/@\\s]*)@", "giu");
var REMEMBER_ERROR_CONTROL_RE = /[\x00-\x1f\x7f-\x9f]/g;
var DETAIL_SUPPRESSED_REMEMBER_ERROR_CODES = /* @__PURE__ */ new Set([
  "managed_memory_unavailable"
]);
function createWorkspaceMemoryExtractionErrorLogger(logger) {
  return (target, err) => {
    logger.warn(`Failed to extract workspace memory error ${target}:`, {
      extractionError: err instanceof Error ? err.message : String(err)
    });
  };
}
__name(createWorkspaceMemoryExtractionErrorLogger, "createWorkspaceMemoryExtractionErrorLogger");
function reportRememberErrorExtractionFailure(onExtractionError, target, err) {
  try {
    onExtractionError?.(target, err);
  } catch {
  }
}
__name(reportRememberErrorExtractionFailure, "reportRememberErrorExtractionFailure");
function errorCodeFromRecord(record) {
  if (typeof record["code"] === "string") return record["code"];
  const data = record["data"];
  if (data && typeof data === "object") {
    const dataRecord = data;
    if (typeof dataRecord["errorKind"] === "string") {
      return dataRecord["errorKind"];
    }
    if (typeof dataRecord["code"] === "string") return dataRecord["code"];
  }
  return void 0;
}
__name(errorCodeFromRecord, "errorCodeFromRecord");
function rawRememberErrorCode(err, seen, depth) {
  if (depth > MAX_REMEMBER_ERROR_CAUSE_DEPTH) return void 0;
  if (!err || typeof err !== "object") return void 0;
  if (seen.has(err)) return void 0;
  seen.add(err);
  const record = err;
  const direct = errorCodeFromRecord(record);
  if (direct) return direct;
  const cause = record["cause"];
  if (cause != null) {
    return rawRememberErrorCode(cause, seen, depth + 1);
  }
  return void 0;
}
__name(rawRememberErrorCode, "rawRememberErrorCode");
function extractRememberErrorCode(err, fallback = "remember_failed") {
  return rawRememberErrorCode(err, /* @__PURE__ */ new WeakSet(), 0) ?? fallback;
}
__name(extractRememberErrorCode, "extractRememberErrorCode");
function workspaceMemoryFailureCode(err, fallback = "remember_failed", onExtractionError) {
  try {
    return extractRememberErrorCode(err, fallback);
  } catch (extractionErr) {
    reportRememberErrorExtractionFailure(
      onExtractionError,
      "code",
      extractionErr
    );
    return fallback;
  }
}
__name(workspaceMemoryFailureCode, "workspaceMemoryFailureCode");
function detailFromRecord(record, seen, depth) {
  const data = record["data"];
  if (data && typeof data === "object") {
    const dataRecord = data;
    const details = dataRecord["details"];
    if (typeof details === "string" && details.length > 0) return details;
    const message2 = dataRecord["message"];
    if (typeof message2 === "string" && message2.length > 0) return message2;
  }
  const message = record["message"];
  if (typeof message === "string" && message.length > 0) return message;
  if (typeof data === "string" && data.length > 0) return data;
  const cause = record["cause"];
  if (cause != null) {
    return rawRememberErrorDetails(cause, seen, depth + 1);
  }
  return void 0;
}
__name(detailFromRecord, "detailFromRecord");
function rawRememberErrorDetails(err, seen, depth) {
  if (depth > MAX_REMEMBER_ERROR_CAUSE_DEPTH) return void 0;
  if (typeof err === "string" && err.length > 0) return err;
  if (!err || typeof err !== "object") return void 0;
  if (seen.has(err)) return void 0;
  seen.add(err);
  return detailFromRecord(err, seen, depth);
}
__name(rawRememberErrorDetails, "rawRememberErrorDetails");
function replaceControlChars(details) {
  return details.replace(REMEMBER_ERROR_AUTH_SCHEME_INVISIBLE_RE, "$1 ").replace(REMEMBER_ERROR_FORMAT_RE, "").replace(REMEMBER_ERROR_SPACE_SEPARATOR_RE, " ").replace(REMEMBER_ERROR_CONTROL_RE, " ");
}
__name(replaceControlChars, "replaceControlChars");
function replaceStackControlChars(stack) {
  return stack.replace(REMEMBER_ERROR_AUTH_SCHEME_INVISIBLE_RE, "$1 ").replace(REMEMBER_ERROR_FORMAT_RE, "").replace(REMEMBER_ERROR_SPACE_SEPARATOR_RE, " ").replace(
    REMEMBER_ERROR_CONTROL_RE,
    (char) => char === "\n" || char === "\r" || char === "	" ? char : " "
  );
}
__name(replaceStackControlChars, "replaceStackControlChars");
function collapseCredentialSeparators(text) {
  return text.replace(
    REMEMBER_ERROR_URL_CREDENTIALS_WITH_SEPARATORS_RE,
    (_match, scheme, userinfo) => `${scheme}${userinfo.replace(REMEMBER_ERROR_CREDENTIAL_SEPARATOR_RE, "")}@`
  ).replace(
    REMEMBER_ERROR_AUTH_TOKEN_WITH_SEPARATORS_RE,
    (_match, scheme, token) => `${scheme} ${token.replace(REMEMBER_ERROR_CREDENTIAL_SEPARATOR_RE, "")}`
  ).replace(
    REMEMBER_ERROR_BARE_TOKEN_WITH_SEPARATORS_RE,
    (token) => token.replace(REMEMBER_ERROR_CREDENTIAL_SEPARATOR_RE, "")
  ).replace(
    REMEMBER_ERROR_AWS_KEY_WITH_SEPARATORS_RE,
    (token) => token.replace(REMEMBER_ERROR_CREDENTIAL_SEPARATOR_RE, "")
  ).replace(
    REMEMBER_ERROR_SECRET_ASSIGNMENT_WITH_SEPARATORS_RE,
    (_match, prefix, value) => `${prefix}${value.replace(REMEMBER_ERROR_CREDENTIAL_SEPARATOR_RE, "")}`
  ).replace(
    REMEMBER_ERROR_ENV_SECRET_ASSIGNMENT_WITH_SEPARATORS_RE,
    (_match, prefix, value) => `${prefix}${value.replace(REMEMBER_ERROR_CREDENTIAL_SEPARATOR_RE, "")}`
  );
}
__name(collapseCredentialSeparators, "collapseCredentialSeparators");
function collapseCredentialSeparatorsByStackLine(stack) {
  return stack.split(/(\r\n|\n|\r)/).map(
    (part) => part === "\r\n" || part === "\n" || part === "\r" ? part : collapseCredentialSeparators(part)
  ).join("");
}
__name(collapseCredentialSeparatorsByStackLine, "collapseCredentialSeparatorsByStackLine");
function isHighSurrogate(codeUnit) {
  return codeUnit >= 55296 && codeUnit <= 56319;
}
__name(isHighSurrogate, "isHighSurrogate");
function isLowSurrogate(codeUnit) {
  return codeUnit >= 56320 && codeUnit <= 57343;
}
__name(isLowSurrogate, "isLowSurrogate");
function truncateBeforeDanglingSurrogate(details, cutPoint) {
  const beforeCut = details.charCodeAt(cutPoint - 1);
  const atCut = details.charCodeAt(cutPoint);
  if (isHighSurrogate(beforeCut) && isLowSurrogate(atCut)) {
    return cutPoint - 1;
  }
  return cutPoint;
}
__name(truncateBeforeDanglingSurrogate, "truncateBeforeDanglingSurrogate");
function capRememberErrorText(redacted) {
  if (redacted.length <= MAX_REMEMBER_ERROR_DETAILS_CHARS) return redacted;
  const truncationSuffix = "... [truncated]";
  const cutPoint = truncateBeforeDanglingSurrogate(
    redacted,
    MAX_REMEMBER_ERROR_DETAILS_CHARS - truncationSuffix.length
  );
  return `${redacted.slice(0, cutPoint)}${truncationSuffix}`;
}
__name(capRememberErrorText, "capRememberErrorText");
function redactAndCapRememberErrorText(normalized) {
  const redacted = redactLogCredentials(normalized).trim();
  if (!redacted) return void 0;
  return capRememberErrorText(redacted);
}
__name(redactAndCapRememberErrorText, "redactAndCapRememberErrorText");
function sanitizeRememberErrorDetails(details) {
  return redactAndCapRememberErrorText(
    replaceControlChars(collapseCredentialSeparators(details))
  );
}
__name(sanitizeRememberErrorDetails, "sanitizeRememberErrorDetails");
function sanitizeRememberErrorStack(stack) {
  return redactAndCapRememberErrorText(
    replaceStackControlChars(collapseCredentialSeparatorsByStackLine(stack))
  );
}
__name(sanitizeRememberErrorStack, "sanitizeRememberErrorStack");
function extractRememberErrorDetails(err) {
  const raw = rawRememberErrorDetails(err, /* @__PURE__ */ new WeakSet(), 0);
  if (!raw) return void 0;
  return sanitizeRememberErrorDetails(raw);
}
__name(extractRememberErrorDetails, "extractRememberErrorDetails");
function extractRememberErrorStack(err) {
  if (!(err instanceof Error) || !err.stack) return void 0;
  return sanitizeRememberErrorStack(err.stack);
}
__name(extractRememberErrorStack, "extractRememberErrorStack");
function shouldSuppressRememberErrorDetails(code) {
  return DETAIL_SUPPRESSED_REMEMBER_ERROR_CODES.has(code);
}
__name(shouldSuppressRememberErrorDetails, "shouldSuppressRememberErrorDetails");
function workspaceMemoryFailureDiagnostics(err, onExtractionError) {
  let details;
  let stack;
  try {
    details = extractRememberErrorDetails(err);
  } catch (extractionErr) {
    reportRememberErrorExtractionFailure(
      onExtractionError,
      "details",
      extractionErr
    );
    details = void 0;
  }
  try {
    stack = extractRememberErrorStack(err);
  } catch (extractionErr) {
    reportRememberErrorExtractionFailure(
      onExtractionError,
      "stack",
      extractionErr
    );
    stack = void 0;
  }
  return {
    ...details ? { details } : {},
    debugDetails: details ?? "<details unavailable>",
    ...stack ? { stack } : {}
  };
}
__name(workspaceMemoryFailureDiagnostics, "workspaceMemoryFailureDiagnostics");

// packages/cli/src/serve/workspace-memory-remember-constants.ts
init_esbuild_shims();
var MAX_REMEMBER_CONTENT_BYTES = 64 * 1024;

// packages/cli/src/serve/workspace-memory-summaries.ts
init_esbuild_shims();
function formatWorkspaceMemoryForgetSummary(removedEntryCount) {
  return removedEntryCount > 0 ? `Forgot ${removedEntryCount} memory entr${removedEntryCount === 1 ? "y" : "ies"}.` : "No managed auto-memory entries matched.";
}
__name(formatWorkspaceMemoryForgetSummary, "formatWorkspaceMemoryForgetSummary");
function formatWorkspaceMemoryDreamSummary(touchedTopicCount) {
  return touchedTopicCount > 0 ? "Managed auto-memory dream completed." : "No managed auto-memory topics changed.";
}
__name(formatWorkspaceMemoryDreamSummary, "formatWorkspaceMemoryDreamSummary");

// packages/cli/src/serve/validate-server-name.ts
init_esbuild_shims();
var VALID_SERVER_NAME = /^[A-Za-z0-9_-]+$/;
var RESERVED_SERVER_NAMES = /* @__PURE__ */ new Set([
  "__proto__",
  "constructor",
  "prototype"
]);
function isValidServerName(name) {
  return typeof name === "string" && name.length > 0 && name.length <= 256 && VALID_SERVER_NAME.test(name) && !RESERVED_SERVER_NAMES.has(name);
}
__name(isValidServerName, "isValidServerName");

export {
  createWorkspaceMemoryExtractionErrorLogger,
  extractRememberErrorCode,
  workspaceMemoryFailureCode,
  shouldSuppressRememberErrorDetails,
  workspaceMemoryFailureDiagnostics,
  MAX_REMEMBER_CONTENT_BYTES,
  formatWorkspaceMemoryForgetSummary,
  formatWorkspaceMemoryDreamSummary,
  isValidServerName
};
/**
 * @license
 * Copyright 2026 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 */
