```ts
export function graceWindow(counts: number[], goal: number, grace: number): number {
  if (goal <= 0) throw new Error("Goal must be positive");
  if (grace < 0) throw new Error("Grace cannot be negative");

  let longestStretch = 0;
  let currentStretch = 0;
  let unkeptDays = 0;

  for (const count of counts) {
    if (count >= goal) {
      if (unkeptDays > grace) {
        currentStretch = 1;
        unkeptDays = 0;
      } else {
        currentStretch++;
        unkeptDays = 0;
      }
    } else {
      unkeptDays++;
      if (currentStretch > longestStretch) {
        longestStretch = currentStretch;
      }
      currentStretch = 0;
    }
  }

  return Math.max(longestStretch, currentStretch);
}
```