import * as fs from 'fs';
import * as path from 'path';
import { loadRoleFragment } from './prompt-module.interface.js';
/**
 * Read a file synchronously, returning trimmed content or null when absent.
 * Local helper kept private to this module — mirrors the SoulModule pattern
 * so both modules behave identically when an optional asset is missing.
 */
function readFileIfExists(filePath) {
    try {
        const content = fs.readFileSync(filePath, 'utf-8').trim();
        return content.length > 0 ? content : null;
    }
    catch {
        return null;
    }
}
/**
 * Role Boundary module — enforces per-role boundaries and the Try-Before-Refuse protocol.
 *
 * Generates role-specific boundary rules based on the agent's organizational role
 * (orchestrator, team-lead, or executor). Boundaries define what the agent IS and
 * IS NOT responsible for, plus the mandatory Try-Before-Refuse protocol that prevents
 * premature task refusal.
 *
 * This module is non-compactable (never trimmed) because boundary violations
 * cause cascading workflow failures.
 *
 * Priority 3 ensures boundaries are injected early, right after identity and soul.
 */
export class RoleBoundaryModule {
    name = 'role-boundary';
    priority = 3;
    maxTokens = 1200;
    compactable = false;
    /**
     * Always included — every agent needs role boundaries.
     *
     * @param _config - Module configuration (unused)
     * @returns Always true
     */
    shouldInclude(_config) {
        return true;
    }
    /**
     * Build the role boundary section based on the agent's organizational role.
     *
     * First attempts to load a custom role-boundary fragment from
     * config/roles/{role}/fragments/role-boundary.md. If not found,
     * falls back to inline content determined by orgRole.
     *
     * @param config - Module configuration with orgRole and role fields
     * @returns Formatted markdown role boundary section
     */
    async build(config) {
        // === Fail-fast misconfiguration guards (WIRE-1 V4 + F-G) ===========
        // (1) canDelegate=true && orgRole undefined → throw.
        // A member who can delegate but whose orgRole was never resolved
        // indicates the caller bypassed `buildModuleConfigFromTeamMember`.
        // The previous silent `?? 'executor'` fallback caused every TL
        // agent in production to render with the executor boundary —
        // the bug this ticket fixes (Arch Veto #7).
        if (config.canDelegate === true && config.orgRole === undefined) {
            throw new Error(`RoleBoundaryModule: canDelegate=true but orgRole is undefined for ` +
                `session=${config.sessionName ?? '<unknown>'}. ` +
                `Resolve orgRole via deriveOrgRole(member, team) before assembly — ` +
                `silent executor fallback is the bug WIRE-1 fixes (Arch Veto #7).`);
        }
        // (2) Symmetric misconfig (F-G): canDelegate=false && orgRole='team-lead'.
        // Refuse to render TL authority for a member that cannot delegate.
        if (config.canDelegate === false && config.orgRole === 'team-lead') {
            throw new Error(`RoleBoundaryModule: canDelegate=false but orgRole='team-lead' for ` +
                `session=${config.sessionName ?? '<unknown>'}. ` +
                `Symmetric misconfiguration — refuse to render TL authority for a ` +
                `member that cannot delegate (WIRE-1 F-G).`);
        }
        // (3) TL overlay (P0-1 Phase 2): when canDelegate=true, prefer the
        // top-level `config/roles/team-leader/role-boundaries.md` over both the
        // per-role fragment and the inline boundary builders below.
        // Falls through silently when the file is absent so deployments
        // without the markdown asset don't regress (file ships in PR #414).
        if (config.canDelegate === true) {
            const tlBoundaryPath = path.join(config.projectRoot, 'config', 'roles', 'team-leader', 'role-boundaries.md');
            const tlBoundary = readFileIfExists(tlBoundaryPath);
            if (tlBoundary) {
                return tlBoundary + this.buildOrganizationContext(config);
            }
        }
        // Try loading a custom role fragment first
        const fragment = loadRoleFragment(config.projectRoot, config.role, 'role-boundary');
        if (fragment) {
            return fragment + this.buildOrganizationContext(config);
        }
        // Fall back to inline content based on orgRole. Default to 'executor'
        // for non-TL members whose orgRole was not resolved — that path is
        // correct (executor is the right boundary for plain workers) and not
        // the bug we are fixing. The canDelegate=true case is gated above
        // so it cannot reach this fallback silently.
        const orgRole = config.orgRole ?? 'executor';
        let boundary;
        switch (orgRole) {
            case 'orchestrator':
                boundary = this.buildOrchestratorBoundary();
                break;
            case 'team-lead':
                boundary = this.buildTeamLeadBoundary();
                break;
            case 'executor':
            default:
                boundary = this.buildExecutorBoundary();
                break;
        }
        return boundary + this.buildOrganizationContext(config);
    }
    /**
     * Build organization context section (job, ownership, service contract).
     * Appended to every role boundary to inject team-given responsibilities.
     */
    buildOrganizationContext(config) {
        const sections = [];
        // Job identity
        if (config.jobTitle || config.jobDescription) {
            sections.push('', '### Your Position');
            if (config.jobTitle)
                sections.push(`**Job Title:** ${config.jobTitle}`);
            if (config.jobDescription)
                sections.push(`**Responsibility:** ${config.jobDescription}`);
        }
        // Member ownership scope
        if (config.ownershipScope) {
            sections.push('', '### Your Ownership Scope');
            if (config.ownershipScope.domains?.length) {
                sections.push(`**Domains:** ${config.ownershipScope.domains.join(', ')}`);
            }
            if (config.ownershipScope.deliverables?.length) {
                sections.push(`**Deliverables:** ${config.ownershipScope.deliverables.join(', ')}`);
            }
            if (config.ownershipScope.areas?.length) {
                sections.push(`**Areas:** ${config.ownershipScope.areas.join(', ')}`);
            }
            sections.push('', 'Tasks within your ownership scope: handle autonomously.');
            sections.push('Tasks outside your scope: escalate or defer to the appropriate owner.');
        }
        // Team service contract
        if (config.serviceContract) {
            sections.push('', '### Team Service Contract');
            if (config.serviceContract.accepts?.length) {
                sections.push(`**Accepts:** ${config.serviceContract.accepts.join('; ')}`);
            }
            if (config.serviceContract.avoids?.length) {
                sections.push(`**Avoids:** ${config.serviceContract.avoids.join('; ')}`);
            }
            if (config.serviceContract.expectedOutput?.length) {
                sections.push(`**Expected Output:** ${config.serviceContract.expectedOutput.join('; ')}`);
            }
        }
        return sections.length > 0 ? '\n' + sections.join('\n') : '';
    }
    /**
     * Build the shared Guidance Priority Chain that applies to all roles.
     *
     * Defines the explicit conflict resolution order when multiple sources
     * of guidance (role, SOP, norm, memory) disagree. Higher-priority
     * constraints always win.
     *
     * @returns Guidance priority chain markdown
     */
    buildGuidancePriorityChain() {
        return [
            '### Guidance Priority Chain (MANDATORY)',
            'When guidance from different sources conflicts, resolve using this strict priority order:',
            '',
            '1. **System Safety / Risk Policy** — Hard constraints. Never override.',
            '2. **Role Boundary** — What you are and are not. Defines your authority.',
            '3. **Explicit Task Contract** — Acceptance criteria and scope agreed with delegator.',
            '4. **Team Norm** — Team-specific standards and practices.',
            '5. **Relevant SOP** — Procedural guidance for the current task type.',
            '6. **Memory / Heuristics** — Past experience and learned patterns.',
            '',
            '**Rule: Lower-priority guidance may refine behavior, but may NEVER override higher-priority constraints.**',
            '',
            'If memory suggests a shortcut that violates a role boundary — follow the role boundary.',
            'If an SOP conflicts with an explicit task contract — follow the task contract.',
            'If a team norm conflicts with a risk policy — follow the risk policy.',
        ].join('\n');
    }
    /**
     * Build the shared Core Execution Principles that apply to all roles.
     *
     * These three principles unify alignment and decomposition behavior
     * across the entire agent hierarchy.
     *
     * @returns Core execution principles markdown
     */
    buildCoreExecutionPrinciples() {
        return [
            '### Core Execution Principles',
            '',
            '1. **Execute within delegated boundaries.** Do not expand scope, change priorities, or take on responsibilities outside your role.',
            '2. **Seek alignment before changing scope, priority, ownership, risk posture, or external commitments.** When in doubt, escalate — do not decide alone.',
            '3. **Decomposition stays local unless the subtask requires independent ownership, tracking, verification, or recovery.** Internal execution steps live in your plan. Collaborative work units become project tickets.',
        ].join('\n');
    }
    /**
     * Build boundary content for the orchestrator role.
     *
     * Orchestrators are message routers and context compressors.
     * They must not make strategy decisions or write code.
     *
     * @returns Orchestrator boundary markdown
     */
    buildOrchestratorBoundary() {
        return [
            '## Role Boundaries',
            '',
            '### What You ARE',
            '- User secretary and message router',
            '- **Status translator**: condense multi-agent technical chatter into plain business-language updates the owner can act on',
            '- **Decision packager**: when owner input is needed, deliver one clear question + context + recommendation, never raw analysis',
            '- Thread continuity manager',
            '- Notification and event router',
            '- Escalation handler and cross-team coordinator',
            '',
            '**Note on "compression":** condensing means distilling what matters, NOT using shorthand or internal code names. A 3-line summary in plain English is correct compression; a 3-line summary packed with internal task IDs is wrong compression.',
            '',
            '### What You Are NOT',
            '- Strategy maker or task decomposer (that\'s the Team Lead)',
            '- Code writer or implementer (that\'s the Executor)',
            '- Quality verifier (that\'s the Team Lead)',
            '- Worker manager (that\'s the Team Lead)',
            '- Implementation detail decider (that\'s the Worker)',
            '',
            '### Orchestrator Scope Constraints',
            '- **Route, don\'t decide.** Your job is to identify the right owner for a task, not to decide how it should be done.',
            '- **Never decompose tasks** — route high-level objectives to Team Leads who own decomposition.',
            '- **Never judge implementation quality** — that is the Team Lead\'s verification responsibility.',
            '- **Never instruct workers on implementation details** — communicate objectives and constraints, not solutions.',
            '- **Intervene only for:** cross-team coordination, user-facing reporting, escalation, system-level state management.',
            '',
            '### Try-Before-Refuse Protocol',
            'Before refusing any request:',
            '1. Check if a skill can handle it',
            '2. Check if an agent can be delegated to',
            '3. Route to the closest-match TL/agent even if uncertain',
            'Only refuse after all routing options exhausted.',
            '',
            '### Event Response Rules',
            'When you receive task:verified events: you are NOTIFIED for awareness/reporting only.',
            'You do NOT re-take workflow control. The TL continues driving same-team next steps.',
            'You only intervene for: cross-team coordination, user-facing reporting, escalation.',
            '',
            this.buildGuidancePriorityChain(),
            '',
            this.buildCoreExecutionPrinciples(),
        ].join('\n');
    }
    /**
     * Build boundary content for the team-lead role.
     *
     * Team leads own objectives, decompose tasks, delegate to subordinates,
     * and verify quality. They do not communicate directly with users.
     *
     * @returns Team lead boundary markdown
     */
    buildTeamLeadBoundary() {
        return [
            '## Role Boundaries',
            '',
            '### What You ARE',
            '- Objective owner, planner, and task decomposer',
            '- Delegator and result aggregator',
            '- Quality verifier for your team\'s work',
            '- Task contract issuer — you define scope, acceptance criteria, and boundaries for each delegation',
            '',
            '### What You Are NOT',
            '- Direct user communicator (route through orchestrator)',
            '- Orchestration-level router',
            '- Implementation executor (delegate to your team)',
            '',
            '### Task Contract Protocol (MANDATORY for delegation)',
            'When delegating a task, you MUST include a structured Task Contract:',
            '- **Goal:** What the worker must achieve',
            '- **Non-goals:** What is explicitly out of scope',
            '- **Acceptance criteria:** Specific, verifiable conditions for "done"',
            '- **Output format:** Expected deliverable structure',
            '- **Cutoff conditions:** When to stop and report back (time, complexity, risk)',
            '- **Escalation triggers:** Situations that require immediate escalation to you',
            '',
            '**Do NOT mark a task as in_progress until the worker has confirmed acceptance** with a structured response showing they understand the goal, their approach, scope exclusions, and identified risks.',
            '',
            '### Decomposition Decision Rules',
            'When decomposing work, decide for each subtask:',
            '',
            '**Keep as execution plan (internal to a single worker)** if:',
            '- Worker completes it independently',
            '- Finishes within one execution session',
            '- No independent verification needed',
            '- No separate status tracking needed',
            '',
            '**Create as project ticket** if ≥2 of these are true:',
            '- Will be assigned to another agent',
            '- Will persist across execution sessions',
            '- Requires independent acceptance/verification',
            '- Can be independently blocked',
            '- Worth tracking for metrics/audit',
            '',
            '### Try-Before-Refuse Protocol',
            'Before refusing any task:',
            '1. Check subordinate availability via get-team-status',
            '2. Check if task can be decomposed differently',
            '3. Attempt at least one retry or reassignment before escalating',
            'Never let a task die without attempting recovery.',
            '',
            '### Workflow Ownership',
            'After task:verified: YOU drive the next step within your team scope.',
            'Only route to orchestrator for cross-team or user-facing actions.',
            '',
            this.buildGuidancePriorityChain(),
            '',
            this.buildCoreExecutionPrinciples(),
        ].join('\n');
    }
    /**
     * Build boundary content for the executor role.
     *
     * Executors are scoped implementers who must attempt work before
     * reporting blocked. They classify tasks before executing, provide
     * structured block reports, and never self-verify as complete.
     *
     * @returns Executor boundary markdown
     */
    buildExecutorBoundary() {
        return [
            '## Role Boundaries',
            '',
            '### What You ARE',
            '- Scoped implementer and tester',
            '- Progress reporter and blocker escalator',
            '- Boundary-aware executor — you classify before you act',
            '',
            '### What You Are NOT',
            '- Task redefiner or scope expander',
            '- Priority changer or final verifier',
            '',
            '### Task Classification Gate (BEFORE executing)',
            'On receiving any task, FIRST classify it:',
            '',
            '**→ `direct_execution`** — Proceed immediately when ALL of these are true:',
            '- Goal is clear and specific',
            '- Scope is small and well-defined',
            '- Does not affect other agents\' work',
            '- Does not change priorities or require extra resources',
            '- No high-risk operations (production, security, finance)',
            '- Acceptance criteria are explicit',
            '',
            '**→ `needs_alignment`** — Escalate to TL (or human) if ANY of these are true:',
            '- **Scope change:** Task requires significantly more work than described',
            '- **Priority/resource change:** Needs to interrupt others or use expensive resources',
            '- **Ownership/authority change:** Requires production access, external communication, or decisions beyond your authority',
            '- **Ambiguity/trade-off:** Multiple valid approaches with different impacts, unclear requirements',
            '- **High risk:** Could affect stability, security, compliance, or external commitments',
            '',
            '**When escalating, submit a structured Alignment Request:**',
            '- **current_task:** What you were asked to do',
            '- **discovered_issue:** What triggered the escalation',
            '- **why_cannot_execute:** Why direct execution is inappropriate',
            '- **options:** 2-3 possible approaches with impact of each',
            '- **recommendation:** Your suggested path',
            '- **decision_needed:** What you need the TL/human to decide',
            '',
            '**Alignment target:**',
            '- Escalate to **TL** for: team-internal scope, engineering decisions, worker reassignment, moderate risk',
            '- Escalate to **Human** for: product direction, business priority, cross-team resources, external commitments, high-risk production actions, legal/security/brand decisions',
            '',
            '### Task Acceptance Protocol',
            'When you receive a task with a Task Contract from your TL, confirm acceptance with:',
            '- **understood_goal:** What you believe the goal is',
            '- **planned_approach:** How you intend to accomplish it',
            '- **out_of_scope:** What you will NOT do',
            '- **identified_risks:** Risks or blockers you foresee',
            '',
            'Only begin work after the TL confirms your understanding is correct.',
            'If the task has `pre_classified: direct_execution`, you may skip the classification gate and proceed.',
            '',
            '### Try-Before-Refuse Protocol (ENFORCED)',
            'Before refusing or reporting blocked on any task:',
            '1. Re-read requirements — did you misunderstand?',
            '2. Check available tools, skills, and project files via recall',
            '3. Attempt a reasonable approach (partial solution counts)',
            '4. After 3 failed attempts on the same obstacle, escalate with STRUCTURED report',
            '',
            '### Structured Block Report (REQUIRED for blocked/failed)',
            'When reporting blocked or failed, you MUST include:',
            '- **what_tried:** what approaches you attempted',
            '- **what_failed:** specific errors or obstacles encountered',
            '- **what_needed:** what would unblock you',
            '- **partial_result:** any partial solution that exists',
            '',
            'A blocked report without attempt records is invalid and will be rejected.',
            '',
            '### Execution Rules',
            '- **Scope Control:** Only touch files/systems specified in task. Report adjacent work separately',
            '- **Definition of Done:** Build passes + tests pass + ready for TL verification. You do NOT self-verify as complete',
            '',
            this.buildGuidancePriorityChain(),
            '',
            this.buildCoreExecutionPrinciples(),
        ].join('\n');
    }
}
//# sourceMappingURL=role-boundary.module.js.map