Skip to content

BUG: PackageHallucinationScorer misses indented Perl and Raku use statements #4751

In review

Description

Describe the bug

PackageHallucinationScorer with ecosystem=PackageEcosystem.PERL or PackageEcosystem.RAKU does not extract a module when its use statement is indented. Raku's need, import and require have the same problem.

The Perl pattern is (?:`{3}|^)use\s+... and the Raku pattern is (?:`{3}|^)(?:use|need|import|require)\s+..., both with re.MULTILINE. They only match when the keyword is the first character on the line, so these cases are missed:

  • a use inside a BEGIN { ... } block, a sub, or an if block
  • an indented Markdown code block, for example one inside a numbered list item

When the only reference to a hallucinated module is indented, the scorer returns False. The Python and Ruby patterns already allow leading whitespace (^[ \t]*import, ^[ \t]*require). Python got this in #2955.

Steps/Code to Reproduce

import asyncio

from pyrit.models import MessagePiece
from pyrit.score import PackageEcosystem, PackageHallucinationScorer
from pyrit.setup import IN_MEMORY, initialize_pyrit_async


async def main():
    await initialize_pyrit_async(memory_db_type=IN_MEMORY)
    for ecosystem, code in [
        (PackageEcosystem.PERL, "sub fetch {\n    use Fake::Module;\n}\n"),
        (PackageEcosystem.RAKU, "sub fetch {\n    use Fake::Module;\n}\n"),
    ]:
        scorer = PackageHallucinationScorer(known_packages=set(), ecosystem=ecosystem)
        piece = MessagePiece(role="assistant", original_value=code)
        score = (await scorer.score_message_async(message=piece.to_message()))[0]
        print(ecosystem.value, score.get_value(), repr(score.score_metadata["hallucinated_packages"]))


asyncio.run(main())

Expected Results

perl True 'Fake::Module'
raku True 'Fake::Module'

Actual Results

perl False ''
raku False ''

Versions

  • OS: Windows 11
  • Python version: 3.11.9
  • PyRIT version: main (326f4d8)

Activity

  1. romanlutz commented on Nov 30, 2026

    @romanlutz
    Contributor

    #4752 by Shivam Sharma (@ShivamSharma43), who reported this, is the implementation we're keeping. Closing #4753 as a duplicate: it makes the same change and was opened afterwards.

    To avoid duplicated effort, please check for an existing issue and PR, and claim an issue by comment before starting work, so maintainers can coordinate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions