// Force strict mode and setup for ESM
"use strict";
import {
  getTeammateContext,
  isTeammate
} from "./chunk-S6LOFUVP.js";
import {
  isInForkExecution
} from "./chunk-SRY26X4H.js";
import {
  ToolNames
} from "./chunk-CFDR3GNL.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/core/src/config/approval-mode.ts
init_esbuild_shims();
var ApprovalMode = /* @__PURE__ */ ((ApprovalMode2) => {
  ApprovalMode2["PLAN"] = "plan";
  ApprovalMode2["DEFAULT"] = "default";
  ApprovalMode2["AUTO_EDIT"] = "auto-edit";
  ApprovalMode2["AUTO"] = "auto";
  ApprovalMode2["YOLO"] = "yolo";
  return ApprovalMode2;
})(ApprovalMode || {});
var APPROVAL_MODES = Object.values(ApprovalMode);

// packages/core/src/agents/runtime/agent-context.ts
init_esbuild_shims();
import { AsyncLocalStorage } from "node:async_hooks";
var storage = new AsyncLocalStorage();
function runWithAgentContext(agentId, fn, depthOverride) {
  const current = storage.getStore() ?? {};
  const depth = depthOverride ?? (current.depth ?? -1) + 1;
  return storage.run({ ...current, agentId, depth }, fn);
}
__name(runWithAgentContext, "runWithAgentContext");
function runWithRuntimeContentGenerator(view, fn) {
  const current = storage.getStore() ?? {};
  return storage.run({ ...current, runtimeView: view }, fn);
}
__name(runWithRuntimeContentGenerator, "runWithRuntimeContentGenerator");
function getCurrentAgentId() {
  return storage.getStore()?.agentId ?? null;
}
__name(getCurrentAgentId, "getCurrentAgentId");
function getCurrentAgentDepth() {
  return storage.getStore()?.depth ?? 0;
}
__name(getCurrentAgentDepth, "getCurrentAgentDepth");
function getRuntimeContentGenerator() {
  return storage.getStore()?.runtimeView;
}
__name(getRuntimeContentGenerator, "getRuntimeContentGenerator");
function runOutsideAgentContext(fn) {
  return storage.exit(fn);
}
__name(runOutsideAgentContext, "runOutsideAgentContext");
function isTopLevelSession() {
  return getCurrentAgentId() === null;
}
__name(isTopLevelSession, "isTopLevelSession");
function childLaunchDepth() {
  return getCurrentAgentId() !== null ? getCurrentAgentDepth() + 1 : 0;
}
__name(childLaunchDepth, "childLaunchDepth");
function canSpawnNestedAgent(maxDepth) {
  return childLaunchDepth() + 1 <= maxDepth;
}
__name(canSpawnNestedAgent, "canSpawnNestedAgent");
function spawnBlockReason(maxDepth) {
  if (!canSpawnNestedAgent(maxDepth)) return "depth";
  if (isTeammate()) return "teammate";
  if (isInForkExecution()) return "fork";
  return null;
}
__name(spawnBlockReason, "spawnBlockReason");

// packages/core/src/agents/runtime/subagent-plan-tool-policy.ts
init_esbuild_shims();
var SUBAGENT_PLAN_LIFECYCLE_TOOLS = /* @__PURE__ */ new Set([
  ToolNames.ENTER_PLAN_MODE,
  ToolNames.EXIT_PLAN_MODE
]);
var PLAN_REQUIRED_TEAMMATE_PRE_APPROVAL_TOOLS = /* @__PURE__ */ new Set([
  ToolNames.EXIT_PLAN_MODE,
  ToolNames.READ_FILE,
  ToolNames.GREP,
  ToolNames.GLOB,
  ToolNames.LS,
  ToolNames.LSP,
  ToolNames.TASK_LIST,
  ToolNames.TOOL_SEARCH,
  ToolNames.READ_MCP_RESOURCE
]);
var PRE_APPROVAL_TASK_CLAIM_KEYS = /* @__PURE__ */ new Set([
  "taskId",
  "status",
  "owner",
  "addBlocks",
  "addBlockedBy"
]);
function isSubagentLikeExecutionContext() {
  return getCurrentAgentId() !== null || isTeammate();
}
__name(isSubagentLikeExecutionContext, "isSubagentLikeExecutionContext");
function isPlanRequiredTeammateContext() {
  return getTeammateContext()?.planModeRequired === true;
}
__name(isPlanRequiredTeammateContext, "isPlanRequiredTeammateContext");
function isPlanRequiredTeammateAwaitingApproval(config) {
  return isPlanRequiredTeammateContext() && config.getApprovalMode() === "plan" /* PLAN */;
}
__name(isPlanRequiredTeammateAwaitingApproval, "isPlanRequiredTeammateAwaitingApproval");
function isPlanLifecycleToolUnavailableInSubagent(toolName) {
  if (!isSubagentLikeExecutionContext()) return false;
  if (toolName === ToolNames.ENTER_PLAN_MODE) return true;
  if (toolName === ToolNames.EXIT_PLAN_MODE) {
    return !isPlanRequiredTeammateContext();
  }
  return false;
}
__name(isPlanLifecycleToolUnavailableInSubagent, "isPlanLifecycleToolUnavailableInSubagent");
function shouldUsePlanOnlyReminderInSubagentContext() {
  return isSubagentLikeExecutionContext() && !isPlanRequiredTeammateContext();
}
__name(shouldUsePlanOnlyReminderInSubagentContext, "shouldUsePlanOnlyReminderInSubagentContext");
function isLeaderOnlyToolUnavailableInSubagent(toolName) {
  return isSubagentLikeExecutionContext() && toolName === ToolNames.TEAM_PLAN_APPROVAL;
}
__name(isLeaderOnlyToolUnavailableInSubagent, "isLeaderOnlyToolUnavailableInSubagent");
function getLeaderOnlyToolUnavailableMessage(toolName) {
  return `${toolName} is only available to the team leader. Subagents and teammates cannot approve teammate plans.`;
}
__name(getLeaderOnlyToolUnavailableMessage, "getLeaderOnlyToolUnavailableMessage");
function getPlanRequiredTeammatePreApprovalMessage(toolName) {
  return `${toolName} is not available while this plan-required teammate is waiting for leader approval. Finish investigation, call exit_plan_mode with the proposed plan, and wait for the leader to approve it before taking execution actions.`;
}
__name(getPlanRequiredTeammatePreApprovalMessage, "getPlanRequiredTeammatePreApprovalMessage");
function isPlanRequiredTeammatePreApprovalAllowedTool(toolName, params) {
  if (PLAN_REQUIRED_TEAMMATE_PRE_APPROVAL_TOOLS.has(toolName)) {
    return true;
  }
  if (toolName !== ToolNames.TASK_UPDATE) {
    return false;
  }
  return isPreApprovalClaimOnlyTaskUpdate(params);
}
__name(isPlanRequiredTeammatePreApprovalAllowedTool, "isPlanRequiredTeammatePreApprovalAllowedTool");
function isPreApprovalClaimOnlyTaskUpdate(params) {
  if (typeof params !== "object" || params === null || Array.isArray(params)) {
    return false;
  }
  const taskParams = params;
  for (const key of Object.keys(taskParams)) {
    if (!PRE_APPROVAL_TASK_CLAIM_KEYS.has(key)) {
      return false;
    }
  }
  const agentName = getTeammateContext()?.agentName;
  return typeof taskParams["taskId"] === "string" && taskParams["status"] === "in_progress" && (taskParams["owner"] === void 0 || taskParams["owner"] === agentName) && isAbsentOrEmptyArray(taskParams["addBlocks"]) && isAbsentOrEmptyArray(taskParams["addBlockedBy"]);
}
__name(isPreApprovalClaimOnlyTaskUpdate, "isPreApprovalClaimOnlyTaskUpdate");
function isAbsentOrEmptyArray(value) {
  return value === void 0 || Array.isArray(value) && value.length === 0;
}
__name(isAbsentOrEmptyArray, "isAbsentOrEmptyArray");
function getSubagentPlanToolUnavailableMessage(toolName) {
  return `${toolName} is not available inside subagents or team agents. Plan mode is owned by the caller/main session; return your plan, findings, or constraints to the caller in your normal response instead of entering or exiting plan mode.`;
}
__name(getSubagentPlanToolUnavailableMessage, "getSubagentPlanToolUnavailableMessage");
function buildSubagentPlanToolBlockedResult(toolName, logTag, logger) {
  const message = getSubagentPlanToolUnavailableMessage(toolName);
  logger.warn(
    `[${logTag}] Blocked plan lifecycle tool call from subagent: ${toolName}`
  );
  return {
    llmContent: message,
    returnDisplay: message,
    error: { message }
  };
}
__name(buildSubagentPlanToolBlockedResult, "buildSubagentPlanToolBlockedResult");

export {
  APPROVAL_MODES,
  runWithAgentContext,
  runWithRuntimeContentGenerator,
  getCurrentAgentId,
  getCurrentAgentDepth,
  getRuntimeContentGenerator,
  runOutsideAgentContext,
  isTopLevelSession,
  childLaunchDepth,
  spawnBlockReason,
  SUBAGENT_PLAN_LIFECYCLE_TOOLS,
  isSubagentLikeExecutionContext,
  isPlanRequiredTeammateContext,
  isPlanRequiredTeammateAwaitingApproval,
  isPlanLifecycleToolUnavailableInSubagent,
  shouldUsePlanOnlyReminderInSubagentContext,
  isLeaderOnlyToolUnavailableInSubagent,
  getLeaderOnlyToolUnavailableMessage,
  getPlanRequiredTeammatePreApprovalMessage,
  isPlanRequiredTeammatePreApprovalAllowedTool,
  getSubagentPlanToolUnavailableMessage,
  buildSubagentPlanToolBlockedResult
};
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 */
