Fix the trust check for an AI-added npm package

Review the AI's codeEasy10 min

Your task

Building a date feature, your AI assistant added date-helpers-lite to package.json. Models invent package names, and attackers register near copies of popular ones, called typosquats, so that installing one runs their install script. Your team now vets every dependency an assistant adds before anyone installs it.

An assistant wrote vet_ai_added_dependency(name, exists_on_registry, weekly_downloads, days_since_first_publish, has_source_repo, popular_names). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • name: the package name the assistant added.
  • exists_on_registry: whether npm has a package with that name.
  • weekly_downloads, days_since_first_publish, has_source_repo: the package's facts on npm.
  • popular_names: the well-known packages your team recognizes.
  • Return "hallucinated", "trusted", "typosquat" or "unvetted".

Rules the tests check, in order. Return the first that applies. Names are compared exactly, case included.

  1. Not on the registry: "hallucinated".
  2. name is exactly one of popular_names: "trusted".
  3. name is within edit distance 2 of any name in popular_names: "typosquat". Edit distance is the fewest single-character insertions, deletions or substitutions that turn one name into the other. This check comes before any stats, because download counts are cheap to fake.
  4. At least 10000 weekly downloads, at least 180 days since first publish, and a source repo: "trusted".
  5. Anything else: "unvetted".

Examples

vet_ai_added_dependency(name: str, exists_on_registry: bool, weekly_downloads: int, days_since_first_publish: int, has_source_repo: bool, popular_names: list[str]) -> str
Example 1
Inputvet_ai_added_dependency("date-helpers-lite", False, 0, 0, False, ["date-fns", "dayjs", "luxon"])
Output"hallucinated"
Example 2
Inputvet_ai_added_dependency("date-fns", True, 21000000, 3400, True, ["date-fns", "dayjs", "luxon"])
Output"trusted"
Example 3
Inputvet_ai_added_dependency("date-fnss", True, 40, 3, False, ["date-fns", "dayjs", "luxon"])
Output"typosquat"