Place Guardrails Around the Stream and the Cache

In Check Answers in Code, the checks span became a gate. But streamAnswer() sends each piece to the page as it arrives, so the hiker reads the answer before the gate runs, and the response cache can serve an answer no check has seen.

Here, you’ll place each check in the request path and prove the gate holds, starting with the check that comes first:

EMERGENCY replies before hikerQuota, input checks and the cache. Output checks hold the model's answer and fail closed to a checked latestReports(). Only passed answer-role text reaches remember().
Emergencies First and Output Checks Before the Page

The emergency check replies first, output checks hold model answers and reports before they reach the page, and remember() stores only checked text from the answer path.

Put the Emergency Check First

A hiker in an emergency must get the rescue number even when the quota is spent, the cache has a hit, or the model fails. But the EMERGENCY list from course 3 is English-only, and the check runs after hikerQuota. Move it to guards/emergency.js with German, Italian, and French stems:

Language: JavaScript Filename: guards/emergency.js
const WORDS = [
  "emergenc", "injur", "hurt", "accident", "lost", "stuck",
  "bleed", // English, from course 3
  "notfall", "verletz", "unfall", "blut", "verirrt", // German
  "emergenz", "ferit", "sangu", "bloccat", // Italian
  "urgence", "blesse", "saign", "bloqu", "perdu", // French
  "bone", "knochen", "osso", "l'os", // A bone sticking out
];
const EMERGENCY = new RegExp(`\\b(${WORDS.join("|")})`);

export function emergencyWord(question) {
  const plain = question.toLowerCase().normalize("NFD")
    .replace(/\p{M}/gu, "").replaceAll("’", "'");
  return plain.match(EMERGENCY)?.[1] ?? null;
}

Try it on questions as hikers type them:

Language: JavaScript Filename: try-emergency.js
import { emergencyWord } from "./guards/emergency.js";

for (const q of [
  "Mon ami est blesse et ne peut pas bouger.",
  "On voit l’os de sa jambe.",
  "I lost the path above hut 6. Which way?",
  "Is the Lauzet footbridge open?",
]) console.log(emergencyWord(q), "|", q);

Run it:

Language: Shell
$ node try-emergency.js
blesse | Mon ami est blesse et ne peut pas bouger.
l'os | On voit l’os de sa jambe.
lost | I lost the path above hut 6. Which way?
null | Is the Lauzet footbridge open?

Missing accents and curly apostrophes no longer matter. The lost hiker’s question matches too, so they get the rescue number instead of route reports. That’s the price of a broad list, and you keep it because a hiker lost at dusk can end up needing a rescue.

In model/fallback.js and route.js, replace EMERGENCY.test(q) with emergencyWord(q). Then add middleware on /ask, after promptVersion() and ahead of hikerQuota:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.