import { LAB_REPRESENTATIVE_AGENT } from "../../agents/identity.js";
import { resolveSeedRuntimeLanguage } from "../language/index.js";
import { renderDynamicMemberLaunchPrompt } from "./prompt-bundle.js";
const nativeReadTools = ["read", "grep", "glob", "ls", "find", "rg"];
const nativeWriteTools = ["apply_patch", "edit", "multi_edit", "notebook_edit", "patch", "str_replace", "write"];
const nativeShellTools = ["bash", "run", "shell", "sh", "terminal"];
const nativeToolOrder = [...nativeReadTools, ...nativeWriteTools, ...nativeShellTools];
export function buildDynamicMemberContract(input) {
    const opencodeAgent = resolveVirtualMemberOpenCodeAgent(input.config);
    const executionSurface = resolveExecutionSurface(input.member);
    const contractWithoutPrompt = {
        schema_version: 1,
        run_id: input.runId,
        team_id: input.spec.team_id,
        team_name: input.spec.team_name,
        role_id: input.member.role_id,
        role_label: input.member.role_label,
        capability_profile: input.member.capability_profile,
        opencode_agent: opencodeAgent,
        mission_prompt: input.member.mission_prompt,
        model_route: input.member.model_route,
        launch_policy: input.member.launch_policy,
        tool_policy: input.member.tool_policy,
        mcp_policy: input.member.mcp_policy,
        skill_policy: input.member.skill_policy,
        spawn_policy: input.member.spawn_policy,
        read_paths: input.member.read_paths,
        write_paths: input.member.write_paths,
        lease_requirements: executionSurface.write_surface === "native_hook_enforced" ? input.member.write_paths : [],
        authoritative_inputs: input.member.authoritative_inputs,
        evidence_inputs: input.member.evidence_inputs,
        evidence_contract: input.member.evidence_contract,
        expected_outputs: input.member.expected_outputs,
        stop_condition: input.member.stop_condition,
        coverage: input.member.coverage,
        subplan_ids: input.subplanIds ?? [],
        ...(input.handoffMessageId ? { handoff_message_id: input.handoffMessageId } : {}),
        context_refs: input.contextRefs ?? [],
        professor_facing_language: resolveSeedRuntimeLanguage(input.config ?? defaultLanguageConfig()),
        execution_surface: executionSurface,
    };
    return {
        ...contractWithoutPrompt,
        prompt: renderDynamicMemberLaunchPrompt(contractWithoutPrompt),
    };
}
function defaultLanguageConfig() {
    return {
        reporting: {
            default_language: "ko",
            english_enabled: true,
            korean_enabled: true,
            live_progress_polish: false,
        },
    };
}
export function resolveVirtualMemberOpenCodeAgent(config) {
    if (config?.disabled_agents.includes(LAB_REPRESENTATIVE_AGENT)) {
        throw new Error(`resolved SEED agent is disabled: ${LAB_REPRESENTATIVE_AGENT}`);
    }
    return LAB_REPRESENTATIVE_AGENT;
}
export function resolveExecutionSurface(member) {
    const allowed = normalizedToolSet(member.tool_policy.allowed_tools);
    const denied = normalizedToolSet(member.tool_policy.denied_tools);
    const nativeTools = nativeToolOrder.filter((tool) => isToolAvailable(tool, allowed, denied, member.write_paths.length > 0));
    const hasNativeWriteTool = nativeWriteTools.some((tool) => nativeTools.includes(tool));
    const hasShellTool = nativeShellTools.some((tool) => nativeTools.includes(tool));
    const hasNativeWriteSurface = member.write_paths.length > 0 && (hasNativeWriteTool || hasShellTool);
    return {
        write_surface: hasNativeWriteSurface ? "native_hook_enforced" : "read_only_or_coordination_only",
        native_tools: nativeTools,
    };
}
function isToolAvailable(tool, allowed, denied, hasWritePaths) {
    if (denied.has(tool))
        return false;
    if (allowed.size > 0)
        return allowed.has(tool);
    if (nativeWriteTools.includes(tool))
        return hasWritePaths;
    return true;
}
function normalizedToolSet(values) {
    const result = new Set();
    for (const value of values) {
        const normalized = value.trim().toLowerCase();
        if (!normalized)
            continue;
        result.add(normalized);
        if (normalized.startsWith("opencode:"))
            result.add(normalized.slice("opencode:".length));
    }
    return result;
}
//# sourceMappingURL=member-runtime.js.map