Skip to content

xref_context: Add test xref in project context #1051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion data_prep/introspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
INTROSPECTOR_ORACLE_ALL_PUBLIC_CANDIDATES = ''
INTROSPECTOR_ORACLE_OPTIMAL = ''
INTROSPECTOR_ORACLE_ALL_TESTS = ''
INTROSPECTOR_ORACLE_ALL_TESTS_XREF = ''
INTROSPECTOR_FUNCTION_SOURCE = ''
INTROSPECTOR_PROJECT_SOURCE = ''
INTROSPECTOR_XREF = ''
Expand Down Expand Up @@ -108,7 +109,8 @@ def set_introspector_endpoints(endpoint):
INTROSPECTOR_FUNCTION_WITH_MATCHING_RETURN_TYPE, \
INTROSPECTOR_ORACLE_ALL_TESTS, INTROSPECTOR_JVM_PROPERTIES, \
INTROSPECTOR_TEST_SOURCE, INTROSPECTOR_HARNESS_SOURCE_AND_EXEC, \
INTROSPECTOR_JVM_PUBLIC_CLASSES, INTROSPECTOR_LANGUAGE_STATS
INTROSPECTOR_JVM_PUBLIC_CLASSES, INTROSPECTOR_LANGUAGE_STATS, \
INTROSPECTOR_ORACLE_ALL_TESTS_XREF

INTROSPECTOR_ENDPOINT = endpoint

Expand Down Expand Up @@ -141,6 +143,8 @@ def set_introspector_endpoints(endpoint):
INTROSPECTOR_FUNCTION_WITH_MATCHING_RETURN_TYPE = (
f'{INTROSPECTOR_ENDPOINT}/function-with-matching-return-type')
INTROSPECTOR_ORACLE_ALL_TESTS = f'{INTROSPECTOR_ENDPOINT}/project-tests'
INTROSPECTOR_ORACLE_ALL_TESTS_XREF = (
f'{INTROSPECTOR_ENDPOINT}/project-tests-for-functions')
INTROSPECTOR_JVM_PROPERTIES = f'{INTROSPECTOR_ENDPOINT}/jvm-method-properties'
INTROSPECTOR_HARNESS_SOURCE_AND_EXEC = (
f'{INTROSPECTOR_ENDPOINT}/harness-source-and-executable')
Expand Down Expand Up @@ -232,6 +236,30 @@ def query_introspector_for_tests(project: str) -> list[str]:
return _get_data(resp, 'test-file-list', [])


def query_introspector_for_tests_xref(
project: str, functions: Optional[list[str]]) -> list[str]:
"""Gets the list of functions and xref test files in the target project."""
data = {'project': project}
if functions:
data['functions'] = ','.join(functions)

resp = _query_introspector(INTROSPECTOR_ORACLE_ALL_TESTS_XREF, data)

test_files = _get_data(resp, 'test-files', {})

handled = set()
result_list = []
for test_paths in test_files.values():
for test_path in test_paths:
if test_path in handled:
continue

handled.add(test_path)
result_list.append(query_introspector_test_source(project, test_path))

return result_list


def query_introspector_for_harness_intrinsics(
project: str) -> list[dict[str, str]]:
"""Gets the list of test files in the target project."""
Expand Down
15 changes: 15 additions & 0 deletions data_prep/project_context/context_introspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,23 @@ def _get_xrefs_to_function(self) -> list[str]:
'function_signature: %s', project, func_sig)
return xrefs

def _get_test_xrefs_to_function(self) -> list[str]:
"""Queries FI for test source calling the function being fuzzed."""
project = self._benchmark.project
func_name = self._benchmark.function_name
xrefs = introspector.query_introspector_for_tests_xref(project, [func_name])

if not xrefs:
logging.warning(
'Could not retrieve tests xrefs for project: %s '
'function_signature: %s', project, func_name)

return xrefs

def get_context_info(self) -> dict:
"""Retrieves contextual information and stores them in a dictionary."""
xrefs = self._get_xrefs_to_function()
test_xrefs = self._get_test_xrefs_to_function()
func_source = self._get_function_implementation()
files = self._get_files_to_include()
decl = self._get_embeddable_declaration()
Expand All @@ -188,6 +202,7 @@ def get_context_info(self) -> dict:
'files': files,
'decl': decl,
'header': header,
'test_xrefs': test_xrefs,
}

logging.info('Context: %s', context_info)
Expand Down
1 change: 1 addition & 0 deletions llm_toolkit/prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def format_context(self, context_info: dict) -> str:
func_source=context_info['func_source'],
xrefs='\n'.join(context_info['xrefs']),
include_statement=context_info['header'],
tests_xrefs='\n'.join(context_info['tests_xrefs']),
)

def _select_examples(self, examples: list[list],
Expand Down
5 changes: 5 additions & 0 deletions prompts/template_xml/context.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ Here is the source code for functions which reference the function being tested:
<code>
{{ xrefs }}
</code>

Here is the source code for the tests/examples that reference the function being tested:
<code>
{{ tests_xrefs }}
</code>
{% endif %}