Let $f(x)=\frac{\cos 2x-(a+2)\cos x+a+1}{\sin x}.$ For constant $a$ such that $\lim_{x\rightarrow 0} \frac{f(x)}{x}=\frac 12$, evaluate $\int_{\frac{\pi}{3}}^{\frac{\pi}{2}} \frac{1}{f(x)}dx.$
1. **Determine the value of \(a\) using the given limit condition:**

   Given:
   \[
   \lim_{x \rightarrow 0} \frac{f(x)}{x} = \frac{1}{2}
   \]
   where
   \[
   f(x) = \frac{\cos 2x - (a+2)\cos x + a + 1}{\sin x}
   \]

   To find the limit, we first need to simplify \(f(x)\) around \(x = 0\). Using the Taylor series expansions for \(\cos x\) and \(\sin x\) around \(x = 0\):
   \[
   \cos x \approx 1 - \frac{x^2}{2} + O(x^4)
   \]
   \[
   \cos 2x \approx 1 - 2x^2 + O(x^4)
   \]
   \[
   \sin x \approx x - \frac{x^3}{6} + O(x^5)
   \]

   Substitute these approximations into \(f(x)\):
   \[
   f(x) = \frac{(1 - 2x^2) - (a+2)(1 - \frac{x^2}{2}) + a + 1}{x - \frac{x^3}{6}}
   \]
   Simplify the numerator:
   \[
   \cos 2x - (a+2)\cos x + a + 1 \approx (1 - 2x^2) - (a+2)(1 - \frac{x^2}{2}) + a + 1
   \]
   \[
   = 1 - 2x^2 - (a+2) + \frac{(a+2)x^2}{2} + a + 1
   \]
   \[
   = 2 - (a+2) + \left(\frac{(a+2)}{2} - 2\right)x^2
   \]
   \[
   = 2 - a - 2 + \left(\frac{a}{2} + 1 - 2\right)x^2
   \]
   \[
   = -a + \left(\frac{a}{2} - 1\right)x^2
   \]

   Therefore,
   \[
   f(x) \approx \frac{-a + \left(\frac{a}{2} - 1\right)x^2}{x}
   \]
   \[
   = \frac{-a}{x} + \left(\frac{a}{2} - 1\right)x
   \]

   For the limit to be \(\frac{1}{2}\), the coefficient of \(x\) in the numerator must be zero:
   \[
   \frac{a}{2} - 1 = 0 \implies a = 2
   \]

2. **Evaluate the integral:**

   With \(a = 2\), the function \(f(x)\) becomes:
   \[
   f(x) = \frac{\cos 2x - 4\cos x + 3}{\sin x}
   \]

   We need to evaluate:
   \[
   \int_{\frac{\pi}{3}}^{\frac{\pi}{2}} \frac{1}{f(x)} \, dx
   \]

   Substitute \(a = 2\) into the integral:
   \[
   \int_{\frac{\pi}{3}}^{\frac{\pi}{2}} \frac{\sin x}{\cos 2x - 4\cos x + 3} \, dx
   \]

   Simplify the denominator:
   \[
   \cos 2x = 2\cos^2 x - 1
   \]
   \[
   \cos 2x - 4\cos x + 3 = 2\cos^2 x - 1 - 4\cos x + 3
   \]
   \[
   = 2\cos^2 x - 4\cos x + 2
   \]
   \[
   = 2(\cos^2 x - 2\cos x + 1)
   \]
   \[
   = 2(\cos x - 1)^2
   \]

   Therefore,
   \[
   f(x) = \frac{\sin x}{2(\cos x - 1)^2}
   \]

   The integral becomes:
   \[
   \int_{\frac{\pi}{3}}^{\frac{\pi}{2}} \frac{\sin x}{2(\cos x - 1)^2} \, dx
   \]

   Let \(u = \cos x - 1\), then \(du = -\sin x \, dx\). When \(x = \frac{\pi}{3}\), \(\cos x = \frac{1}{2}\), so \(u = \frac{1}{2} - 1 = -\frac{1}{2}\). When \(x = \frac{\pi}{2}\), \(\cos x = 0\), so \(u = -1\).

   The integral becomes:
   \[
   -\int_{-\frac{1}{2}}^{-1} \frac{1}{2u^2} \, du
   \]
   \[
   = -\frac{1}{2} \int_{-\frac{1}{2}}^{-1} u^{-2} \, du
   \]
   \[
   = -\frac{1}{2} \left[ -u^{-1} \right]_{-\frac{1}{2}}^{-1}
   \]
   \[
   = \frac{1}{2} \left[ \frac{1}{u} \right]_{-\frac{1}{2}}^{-1}
   \]
   \[
   = \frac{1}{2} \left( \frac{1}{-1} - \frac{1}{-\frac{1}{2}} \right)
   \]
   \[
   = \frac{1}{2} \left( -1 + 2 \right)
   \]
   \[
   = \frac{1}{2} \times 1 = \frac{1}{2}
   \]

The final answer is \(\boxed{\frac{1}{2}}\)