docs: cut the comments that were not earning their place
I wrote 731 comment lines on this branch against 4,530 lines of code — 14%, where the rest of the repo runs at 1.8%. CLAUDE.md asks for code that reads like its surroundings, and this did not. Removed by genre rather than by taste: - restating the code, e.g. "JS getUTCDay() numbering: Sunday = 0" above the map that literally shows it, and a docblock on startOfWeek explaining that it returns the start of the week; - narrating history — "this used to rebuild the whole map", "left the bar recording forever" — which the commit message and git blame already carry; - saying the same thing in several places: the "cannot record is not a denied microphone" reason appeared three times in one file, and the "aborting stops a per-minute metered call" reason across three files. Each now lives once, where the behaviour it explains lives; - defending decisions nobody would question, like why toLatinDigits is its own module; - over-explaining defensive branches, three separate comments to distinguish null from missing-kind from unrecognised-kind. What stays is what the code cannot say: the patient-right convention in toFdi, whose failure mode is a valid code for the wrong tooth; the "this"-vs-"next" week anchoring; StrictMode re-arming mountedRef; Safari accepting no mimeType hint; and the invariants whose violation already cost a bug — the body parser's middleware ordering and the dispatch panel's auto-fill rules. Comments only. The diff contains no non-comment line. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,6 @@ import type { DueIntent, UnresolvedItem, Weekday } from './voice.types';
|
||||
* to reason about instants.
|
||||
*/
|
||||
|
||||
/** JS `getUTCDay()` numbering: Sunday = 0. */
|
||||
const WEEKDAY_TO_JS: Record<Weekday, number> = {
|
||||
saturday: 6,
|
||||
sunday: 0,
|
||||
@@ -26,7 +25,6 @@ const WEEKDAY_TO_JS: Record<Weekday, number> = {
|
||||
friday: 5,
|
||||
};
|
||||
|
||||
/** Refuse absurd deadlines however they were arrived at. */
|
||||
const MAX_DAYS_AHEAD = 365 * 5;
|
||||
|
||||
export type DueResolution = {
|
||||
@@ -78,12 +76,9 @@ function unresolved(spoken: string): DueResolution {
|
||||
}
|
||||
|
||||
/**
|
||||
* What to quote back when a deadline could not be resolved.
|
||||
*
|
||||
* Every field here is nullable on the wire and `toVoiceIntent` casts rather than checks,
|
||||
* so a half-classified deadline arrives with nulls in it. The review sheet renders this
|
||||
* verbatim — `"null null" — not a usable date` in front of a clinician is worse than the
|
||||
* reason on its own, which the sheet already handles for a blank string.
|
||||
* What to quote back when a deadline could not be resolved. Every field is nullable on the
|
||||
* wire and `toVoiceIntent` casts rather than checks, and the sheet renders this verbatim —
|
||||
* so a half-classified deadline must fall back to '', not to `"null null"`.
|
||||
*/
|
||||
function describe(intent: DueIntent): string {
|
||||
const usable = (value: unknown): value is number =>
|
||||
@@ -108,8 +103,7 @@ function describe(intent: DueIntent): string {
|
||||
? `${intent.y}-${intent.m}-${intent.d}`
|
||||
: '';
|
||||
default: {
|
||||
// Reaching here means an unrecognised `kind`, which resolveDueDate has already
|
||||
// established is a string — echo it so the review row names what was heard.
|
||||
// An unrecognised `kind`, already established as a string — echo what was heard.
|
||||
const kind = (intent as { kind?: unknown })?.kind;
|
||||
return typeof kind === 'string' ? kind : '';
|
||||
}
|
||||
@@ -117,11 +111,9 @@ function describe(intent: DueIntent): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Which weekday starts the week, per locale.
|
||||
*
|
||||
* "Next Thursday" is week-relative, so this changes the answer: the Iranian week starts
|
||||
* Saturday, the Dutch and (European) English week starts Monday. Hardcoding Saturday
|
||||
* would put an en/nl clinician's deadline a week out.
|
||||
* Saturday, the Dutch and English week Monday. Hardcoding either puts the other locale's
|
||||
* deadline a week out.
|
||||
*/
|
||||
const WEEK_START_BY_LOCALE: Record<string, number> = {
|
||||
fa: WEEKDAY_TO_JS.saturday,
|
||||
@@ -135,22 +127,18 @@ export function weekStartForLocale(locale: string): number {
|
||||
return WEEK_START_BY_LOCALE[locale] ?? DEFAULT_WEEK_START;
|
||||
}
|
||||
|
||||
/** Most recent week-start day, counting today if today is that day. */
|
||||
function startOfWeek(iso: string, weekStartJs: number): string {
|
||||
const back = (civilDateJsWeekday(iso) - weekStartJs + 7) % 7;
|
||||
return addDays(iso, -back);
|
||||
}
|
||||
|
||||
/**
|
||||
* `'this'` is occurrence-anchored: the soonest occurrence strictly after today, so "by
|
||||
* Thursday" said on a Thursday means the next one — a deadline of today is almost never
|
||||
* what was meant, and this can never resolve into the past.
|
||||
* `'this'` is occurrence-anchored: the soonest occurrence strictly after today, so it can
|
||||
* never resolve into the past.
|
||||
*
|
||||
* `'next'` is *week*-anchored, not "this plus seven". "Thursday next week" means the
|
||||
* Thursday of the Saturday-start week after this one; adding a week to `'this'` would
|
||||
* overshoot by seven days whenever `'this'` had already rolled into next week. The two
|
||||
* can legitimately coincide — said on a Thursday, "the coming Saturday" and "Saturday
|
||||
* next week" are the same day.
|
||||
* `'next'` is *week*-anchored, not "this plus seven" — adding a week to `'this'` overshoots
|
||||
* by seven days whenever `'this'` has already rolled into next week. The two legitimately
|
||||
* coincide: said on a Thursday, "the coming Saturday" and "Saturday next week" are one day.
|
||||
*/
|
||||
function resolveWeekday(
|
||||
intent: Extract<DueIntent, { kind: 'weekday' }>,
|
||||
@@ -199,18 +187,15 @@ export function resolveDueDate(
|
||||
todayIso: string,
|
||||
weekStartJs: number = DEFAULT_WEEK_START,
|
||||
): DueResolution {
|
||||
// Absent is not an error — most utterances carry no deadline. Anything else that is not
|
||||
// an intent object is a deadline we failed to understand, and must be flagged rather
|
||||
// than silently dropped.
|
||||
// Absent is not an error — most utterances carry no deadline.
|
||||
if (intent === null || intent === undefined) {
|
||||
return { dueDate: null, unresolved: null };
|
||||
}
|
||||
if (typeof intent !== 'object') {
|
||||
return unresolved(String(intent).slice(0, 120));
|
||||
}
|
||||
// An object carrying no `kind` at all says nothing about a deadline; flagging it would
|
||||
// put a blank "heard but lost" row in front of a clinician who never mentioned one. An
|
||||
// object with an *unrecognised* kind did try to say something, and is flagged below.
|
||||
// No `kind` at all says nothing about a deadline, so it is not "heard but lost". An
|
||||
// *unrecognised* kind did try to say something, and is flagged below.
|
||||
if (typeof (intent as { kind?: unknown }).kind !== 'string') {
|
||||
return { dueDate: null, unresolved: null };
|
||||
}
|
||||
@@ -240,8 +225,7 @@ export function resolveDueDate(
|
||||
|
||||
if (!resolved) return unresolved(describe(intent));
|
||||
|
||||
// An absolute date the model invented can land anywhere; a deadline in the past or
|
||||
// decades away is not a deadline.
|
||||
// A date the model invented can land anywhere; past or decades away is not a deadline.
|
||||
const daysAhead = (utcMsOf(resolved) - utcMsOf(todayIso)) / 86_400_000;
|
||||
if (daysAhead < 0 || daysAhead > MAX_DAYS_AHEAD)
|
||||
return unresolved(describe(intent));
|
||||
|
||||
Reference in New Issue
Block a user