import { validateAgentWorkerResult } from './agent-worker-pipeline.js';
export async function runFakeAgent(agent, slice, opts = {}) {
    if (Number(slice?.delay_ms || 0) > 0)
        await delay(Number(slice.delay_ms));
    const patchEnvelopes = buildFixturePatchEnvelopes(agent, slice, opts);
    return validateAgentWorkerResult({
        mission_id: opts.missionId || opts.mission_id || '',
        agent_id: agent.id,
        session_id: agent.session_id,
        persona_id: agent.persona_id || agent.id,
        task_slice_id: slice?.id || '',
        status: 'done',
        backend: 'fake',
        summary: 'Fixture agent ' + agent.id + ' completed ' + (slice?.id || 'slice') + ' for ' + (opts.route || 'agent-run') + '.',
        findings: ['fixture agent completed assigned slice'],
        proposed_changes: [],
        changed_files: [],
        lease_compliance: { ok: true, violations: [] },
        artifacts: ['agent-events.jsonl', 'agent-task-board.json'],
        blockers: [],
        confidence: 'fixture',
        handoff_notes: 'No handoff required for fake backend fixture.',
        unverified: ['fake backend does not prove real parallel execution'],
        writes: [],
        ...(patchEnvelopes.length ? { patch_envelopes: patchEnvelopes } : {}),
        source_intelligence_refs: agent.source_intelligence_refs || null,
        goal_mode_ref: agent.goal_mode_ref || null,
        ...(opts.emitFollowUpWorkItems ? { follow_up_work_items: [buildFixtureFollowUp(agent, slice)] } : {}),
        verification: { status: 'fixture', checks: ['schema-normalized-result'] },
        recursion_guard: { ok: true, violations: [] }
    });
}
function delay(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
function buildFixtureFollowUp(agent, slice) {
    return {
        id: `${String(slice?.id || agent.id)}-follow-up-001`,
        title: 'Fixture follow-up work item',
        description: 'Schema-valid follow-up generated by the fake backend fixture.',
        required_persona_category: 'verifier',
        priority: 100,
        dependencies: [],
        lease_requirements: [],
        max_attempts: 1,
        reason: 'exercise follow_up_work_items scheduler enqueue path',
        source_agent_session_id: agent.session_id
    };
}
export function buildFixturePatchEnvelopes(agent, slice, opts = {}) {
    const writePaths = Array.isArray(slice?.write_paths) ? slice.write_paths.map(String).filter(Boolean) : [];
    if (!writePaths.length)
        return [];
    return writePaths.map((file, index) => {
        const leaseId = `write:${String(agent.id)}:${file}`;
        const nodeId = String(slice?.micro_win_id || slice?.id || `patch-${index + 1}`);
        const verificationNodeId = String(slice?.verification_node_id || `verify:${nodeId}`);
        const rollbackNodeId = String(slice?.rollback_node_id || `rollback:${nodeId}`);
        return {
            schema: 'sks.agent-patch-envelope.v1',
            source: 'fixture',
            mission_id: String(opts.missionId || opts.mission_id || ''),
            route: String(opts.route || '$Agent'),
            agent_id: String(agent.id),
            session_id: String(agent.session_id || ''),
            slot_id: String(agent.slot_id || agent.worker_slot_id || agent.id),
            generation_index: Number(agent.generation_index || 1),
            native_cli_worker_session_id: opts.nativeCliWorkerSessionId || agent.native_cli_worker_session_id || agent.session_id,
            native_cli_process_id: Number(opts.nativeCliProcessId || agent.native_cli_process_id || process.pid),
            worker_process_id: Number(opts.workerProcessId || opts.nativeCliProcessId || agent.native_cli_process_id || process.pid),
            fast_mode: opts.fastMode !== false,
            service_tier: opts.serviceTier === 'standard' ? 'standard' : 'fast',
            task_slice_id: String(slice?.id || ''),
            lease_id: leaseId,
            allowed_paths: [file],
            strategy_task_id: nodeId,
            ...(slice?.micro_win_id ? { micro_win_id: String(slice.micro_win_id) } : {}),
            verification_node_id: verificationNodeId,
            rollback_node_id: rollbackNodeId,
            lease_proof: {
                lease_id: leaseId,
                owner_agent: String(agent.id),
                owner_persona: String(agent.persona_id || agent.role || 'fixture'),
                allowed_paths: [file],
                strategy_task_id: nodeId,
                micro_win_id: slice?.micro_win_id ? String(slice.micro_win_id) : undefined,
                protected_path_check: 'passed',
                conflict_prediction_id: `conflict:${nodeId}`,
                verification_node_id: verificationNodeId,
                rollback_node_id: rollbackNodeId
            },
            operations: [{
                    op: 'write',
                    path: file,
                    content: `patched by ${String(agent.id)} for ${String(slice?.id || 'fixture')}\n`
                }],
            rationale: 'Fixture patch envelope emitted by fake backend for a leased write task.',
            verification_hint: { node_id: verificationNodeId, expected_status: 'applied_hashes_recorded' },
            rollback_hint: { node_id: rollbackNodeId, strategy: 'restore content_before or delete newly created file' }
        };
    });
}
//# sourceMappingURL=agent-runner-fake.js.map