```
export function lineTag(body: string): string {
  if (typeof body !== "string") {
    throw new Error("lineTag expects a string");
  }
  let sum = 0;
  for (const ch of body) {
    sum = (sum + ch.codePointAt(0)!) % 256;
  }
  return sum.toString(16).padStart(2, "0");
}

export function checkLine(line: string): string {
  if (typeof line !== "string" || line.length < 3) {
    throw new Error("checkLine expects a string of at least three characters");
  }

  const parts = line.split("~", 2);
  if (parts.length !== 2) {
    throw new Error("checkLine requires exactly one tilde in the third-from-last position");
  }

  const [body, tag] = parts;

  if (!/^[0-9a-fA-F]{2}$/.test(tag)) {
    throw new Error("tag must be a two-character hexadecimal string");
  }

  const computedTag = lineTag(body);

  if (tag !== computedTag) {
    throw new Error(`checkLine tag mismatch: expected ${tag}, got ${computedTag}`);
  }

  return body;
}
```