Keep the Traces Hikers Rated

Your count in Collect Feedback on Every Answer left most ratings pointing to traces that were never written. Course 2’s shouldKeep() keeps every error and flagged answer, plus one in ten of the rest at SAMPLE_RATE 0.1. It decides the moment a request ends, before any hiker has clicked.

In this lesson, you’ll hold each trace for ten minutes so hikers can react, keep traces that receive signals, and record why you kept each one. Then you’ll count the week again. Start with the moment of the decision.

Decide After Hikers Have Had Their Say

A rating can only keep a trace that still exists, so the decision has to wait. Ten minutes matches the window noteAsk() uses for repeats. A longer hold catches more late ratings but stores more traces in memory and loses more on a restart. In trace.js, add a map of held traces and a set of trace IDs that got a signal:

Language: JavaScript Filename: trace.js
const HOLD_MS = 10 * 60_000;
const held = new Map();
const signals = new Set();

export function keepTrace(traceId) {
  if (!held.has(traceId) && !pending.has(traceId)) return false;
  signals.add(traceId);
  return true;
}

keepTrace() also accepts a trace still in pending, so a click on Talk to a warden mid-answer counts. It returns false once the hold ends or the server restarts.

Write traces for errors and flagged answers at once, whatever hikers do, and hold only the rest. At the end of finishTrace(), replace the shouldKeep() check and the write. mustKeep(), from the next section, returns "error", "flagged", or null. finishTrace() still returns spans for course 3’s charge():

Language: JavaScript Filename: trace.js
  if (spans.length === 0) {
    signals.delete(traceId);
    return spans;
  }
  const safe = spans.map(clean);
  const now = mustKeep(safe);
  if (now) await write(safe, now);
  held.set(traceId, now ? null : safe);
  setTimeout(() => decide(traceId), HOLD_MS).unref();
  return spans;
}

async function decide(traceId) {
  const spans = held.get(traceId);
  held.delete(traceId);
  const signal = signals.delete(traceId);
  if (!spans) return;
  const kept = shouldKeep(spans, signal);
  if (kept) await write(spans, kept);
}

async function write(spans, kept) {
  const { traceId } = spans[0];
  const day = spans[0].start.slice(0, 10);
  const lines = spans.map((s) =>
    JSON.stringify({ ...s, kept }) + "\n");
  // mkdir() and appendFile() with the catch, as before
}

clean() runs before the hold, so memory holds only redacted spans. A written trace stays in held as null, so a rating on it still counts as in time. Set.prototype.delete() returns true if the ID was there, telling shouldKeep() whether a signal arrived.

In server.js, add keepTrace to the import from ./trace.js. Then call keepTrace(again.target) inside the if (again) block in /ask. In /feedback, move the const line declaration below const busy = tooMany(hiker); and add an untraced field to the object. Both record() calls store that line, the emergency one included:

Language: JavaScript Filename: server.js
const busy = tooMany(hiker);
const untraced = !busy && kind !== "left"
  && !keepTrace(traceId);
const line = { ...parsed.data, version, known, untraced };

A signal over the limit isn’t stored, so it keeps nothing either.

A thumbs-up or thumbs-down rating, a handoff request, or a repeat keeps a trace. Leaving the page doesn’t, or nearly every trace would be kept. So once decide() runs, every answer rated in time has a trace on disk for 30 days, whatever the draw. The hold has a cost too, since a restart drops up to ten minutes of unflagged traces still waiting.

Record Why Each Trace Was Kept

Later lessons estimate rates from kept traces, and a sampled trace stands for about ten answers while a rated one stands for itself. So shouldKeep() now returns the reason it kept a trace, or null. When several reasons apply, it returns the first, and mustKeep() checks the first two:

Language: JavaScript Filename: trace.js
function mustKeep(spans) {
  if (spans.some((s) => s.error)) return "error";
  const flagged = spans.some((s) => s.failed?.length > 0
    || s.name === "handoff");
  return flagged ? "flagged" : null;
}

function shouldKeep(spans, signal) {
  const must = mustKeep(spans);
  if (must) return must;
  if (signal) return "signal";
  return Math.random() < SAMPLE_RATE ? "sample" : null;
}

Course 4’s handoff spans count as flagged. An answer kept because of a rating is recorded as signal before the draw, so it counts once even if it would have won. Only sample traces are weighted by ten (1 / SAMPLE_RATE), as in Price Each Request From Its Usage.

To check the estimate, add this to the end of scripts/count-feedback.js, below a new import { SAMPLE_RATE } from "../trace.js"; line:

Language: JavaScript Filename: scripts/count-feedback.js
const reasons = new Map(all("traces")
  .map((s) => [s.traceId, s.kept]));
let estimate = 0;
for (const why of reasons.values()) {
  estimate += why === "sample" ? 1 / SAMPLE_RATE : 1;
}
const asks = readFileSync("server.log", "utf8")
  .split("\n").filter((l) => l.includes('"route":"/ask"'))
  .length;
console.log(`${Math.round(estimate)} of ${asks} requests estimated`);

Then replace the original lost line and the two original console.log() lines from Collect Feedback on Every Answer. The new lost list identifies answers rated in time whose traces weren’t kept, and answers rated only too late get a count of their own:

Language: JavaScript Filename: scripts/count-feedback.js
const thumbs = all("feedback")
  .filter((f) => f.kind === "thumbs");
const timely = new Set(thumbs.filter((f) => !f.untraced)
  .map((f) => f.traceId));
const late = new Set(thumbs.map((f) => f.traceId)
  .filter((t) => !timely.has(t)));
const lost = [...timely].filter((t) => !kept.has(t));
console.log(`${latest.size} rated of ${answers} answers`);
console.log(`${lost.length} rated in time with no kept trace`);
console.log(`${late.size} rated only too late, marked untraced`);

Ratings aren’t the only signals. At the end of the script, check that every timely repeat and handoff has a kept trace, for any reason, and that leaving the page alone never keeps one as signal:

Language: JavaScript Filename: scripts/count-feedback.js
const signals = all("feedback").filter((f) => !f.untraced);
for (const kind of ["askedAgain", "handoff"]) {
  const ids = new Set(signals.filter((f) => f.kind === kind)
    .map((f) => f.traceId));
  const gone = [...ids].filter((t) => !kept.has(t)).length;
  console.log(`${gone} of ${ids.size} ${kind} with no kept trace`);
}
const clicked = new Set(signals.filter((f) => f.kind !== "left")
  .map((f) => f.traceId));
const leftOnly = [...reasons].filter(([t, why]) =>
  why === "signal" && !clicked.has(t)).length;
console.log(`${leftOnly} kept as signal with only a left`);

Older feedback has no untraced field, so clear the last run’s feedback and traces:

Language: Windows PowerShell
PS> Remove-Item -Recurse -Force -ErrorAction Ignore feedback, traces
Language: Shell
$ rm -rf feedback traces

Start the server, logging to a fresh server.log:

Language: Windows PowerShell
PS> npm start | Tee-Object server.log
Language: Shell
$ npm start | tee server.log

In a second terminal, run the simulated week:

Language: Shell
$ npm run traffic -- --month=september --feedback

When it returns, wait ten minutes so the server can decide which held traces to keep. Then stop the server with Ctrl+C and run node scripts/count-feedback.js.

Three simulated weeks of about 1,450 requests, run on December 3, 2026, printed 0 on the four check lines: rated in time with no kept trace, askedAgain, handoff, and kept as signal with only a left. Yours must too.

They had 15, 31, and 33 answers rated only too late, 9 to 20 percent of ratings. That share depends on a simulator setting, has nothing to do with answer quality, and swings this much between runs of the same code.

Real hikers who find the Valbrune ford closed rate hours later, so the most useful ratings may stay untraced. If the untraced share stays above about a fifth of ratings for several weeks, lengthen HOLD_MS, accepting the memory and restart cost named above.

The three runs estimated 1,463, 1,473, and 1,509 requests against 1,447, 1,457, and 1,458. Weighting signal traces by ten as well gave 3,272, 3,345, and 3,264. An unlucky draw can miss by a fifth, but a weighting bug lands near double.

The button below asks the mentor why only sample counts ten times. Add your estimate, request count, and one-sentence reason:

Check that your reason separates traces kept whatever the draw, which stand for themselves, from sample traces, which also stand for the answers the draw passed over.

Check Your Understanding

  1. A hiker clicks Not helpful twenty minutes after an answer that passed every check. Nobody rated it, asked again, or asked for a warden before that. What’s stored, and does the answer have a trace?
  2. A flagged answer also got a thumbs-down. What does its trace record as the reason, and how much does it count?
  1. The rating, with untraced: true. The server decided whether to keep the trace at ten minutes with no signal, so it exists only if it won the one-in-ten draw, recorded as sample.
  2. flagged, the first reason that applies, and it counts once. Every flagged trace is kept, so it stands for no other answers.

In the next lesson, See What Thumbs Up Misses, you’ll read a week of ratings and kept traces side by side.

Become a Member to join the conversation.