Skip to content

trading_agent.doctor.checks.injection_check

Scans the files that become the desks' instructions for text that tries to steer an AI.

It reads the shared instruction files, each desk's Persona.md and Memory.md, and every installed skill's SKILL.md. Memory.md is included because a desk writes it itself, sometimes after reading web pages.

Typical usage example:

result = InjectionCheck(layout, ['main']).run()

InjectionCheck

InjectionCheck(layout, desk_names)

Bases: DoctorCheck

Checks the instruction files and installed skills for prompt injection.

Creates the check.

Parameters:

Name Type Description Default
layout WorkspaceLayout

Where the files are.

required
desk_names Sequence[str]

The desks whose files to scan.

required
Source code in src/trading_agent/doctor/checks/injection_check.py
def __init__(
    self,
    layout: workspace_layout.WorkspaceLayout,
    desk_names: Sequence[str],
):
    """Creates the check.

    Args:
        layout (workspace_layout.WorkspaceLayout): Where the files are.
        desk_names (Sequence[str]): The desks whose files to scan.
    """
    self._layout = layout
    self._desk_names = list(desk_names)

run

run()

Scans every file.

Returns:

Type Description
CheckResult

OK with the number of files scanned, or a warning listing each suspicious passage.

Source code in src/trading_agent/doctor/checks/injection_check.py
def run(self) -> doctor_check.CheckResult:
    """Scans every file.

    Returns:
        doctor_check.CheckResult: OK with the number of files scanned, or a warning listing each suspicious passage.
    """
    scanner = injection_scanner.InjectionScanner()
    paths = self._paths()
    problems = []
    for path in paths:
        text = path.read_text(encoding='utf-8', errors='replace')
        for finding in scanner.scan(text):
            problems.append(
                f'{path}, line {finding.line_number}: {finding.rule} ("{finding.excerpt}")'
            )
    if problems:
        return doctor_check.CheckResult(
            doctor_check.CheckStatus.WARNING,
            'Read these passages; they look like they try to steer an AI: '
            + '; '.join(problems),
        )
    return doctor_check.CheckResult(
        doctor_check.CheckStatus.OK,
        f'Nothing suspicious in {len(paths)} files.',
    )