Skip to content
Merged
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
8 changes: 3 additions & 5 deletions src/aiida_agents/agents/execution/prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,16 @@ Before building inputs, check historical successful runs in the database to lear
```python
query_run_context(
query_type="past_successful_workflows",
filters={
"workflow_type": "aiida.workflows:PwRelaxWorkChain",
"structure_type": "metallic"
}
filters={"workflow_type": "aiida.workflows:PwRelaxWorkChain"}
)
```
`query_run_context` returns a `units` field naming the unit of each statistic. Quote values with
those units verbatim and never supply one yourself: a cutoff reported as `60.0` is 60 Ry, and
calling it 60 eV is a factor-of-twenty error in a number the user may run a calculation with. If a
statistic comes back `null`, the database holds no value for it β€” say that rather than estimating.

> **Note:** `structure_type` in filters (`"metallic"`, `"insulator"`, `"semiconductor"`) is metadata to guide parameter heuristics β€” it is not applied as a strict database query predicate.
`workflow_type` is required β€” statistics across every workflow type in the profile would not mean
anything, so omitting it is an error rather than a broad search.

### Step 4: Gather References & Build Inputs (`build`)
To submit a calculation, you need the user's specific atomic structure reference. If they haven't provided it yet, ask cleanly:
Expand Down
61 changes: 24 additions & 37 deletions src/aiida_agents/tools/run_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ class AvailableCodeInfo(t.TypedDict, total=False):
codes: list[dict[str, t.Any]]
"""List of available codes with versions"""

recommended_version: str | None
"""Which version is recommended"""

note: str
"""Any caveats or notes"""

Expand All @@ -196,9 +193,10 @@ def query_run_context(
dict[str, t.Any],
Field(
description=(
"Context-specific filters (e.g. structure_type, composition, workflow_type). "
"Note: 'structure_type' is not a database-level filter β€” AiiDA stores no "
"per-node material-class attribute. It is echoed back as metadata only."
"Filters for this query_type. 'past_successful_workflows' requires "
"{'workflow_type': <entry point>}; 'available_codes' takes an "
"optional {'code': <substring>}; 'failed_attempts' takes an optional "
"{'workflow_type': ...} and/or {'structure_pk': ...}."
)
),
],
Expand All @@ -217,7 +215,7 @@ def query_run_context(

Args:
query_type: The kind of information to retrieve.
filters: Context that narrows the query (workflow_type, structure_type, etc.).
filters: What to narrow the query to; see the field description.

Returns:
dict with context-specific data (past runs, codes, failures, etc.).
Expand Down Expand Up @@ -257,18 +255,26 @@ def query_run_context(


def _query_past_workflows(filters: dict[str, t.Any]) -> dict[str, t.Any]:
"""Query real AiiDA database for completed workflow statistics.
"""Summarise past runs of one workflow, from the active AiiDA database.

Note: ``structure_type`` in *filters* is not applied as a database predicate.
AiiDA stores no per-node material-class attribute, so all finished workflows
of the requested type are included regardless of ``structure_type``.
The value is echoed back in the return dict as metadata.
Reports only what the database holds. Where a value is absent it is
reported as ``None`` rather than estimated, and nothing here ranks or
recommends: an endorsement this function has no basis for would read to a
caller as one it does.
"""
from aiida import orm
from statistics import median

workflow_type = filters.get("workflow_type", "aiida.workflows:PwRelaxWorkChain")
structure_type = filters.get("structure_type", "metallic")
from aiida import orm

workflow_type = filters.get("workflow_type")
if not workflow_type:
msg = (
"past_successful_workflows needs filters={'workflow_type': ...} -- the "
"entry point of the workflow to summarise, as list_workflows() reports "
"it. Statistics across every workflow type in the profile would not "
"mean anything."
)
raise ValueError(msg)
process_label, resolved = _resolve_process_label(workflow_type)

try:
Expand All @@ -291,11 +297,6 @@ def _query_past_workflows(filters: dict[str, t.Any]) -> dict[str, t.Any]:
return {
"query_type": "past_successful_workflows",
"workflow_type": workflow_type,
"structure_type": structure_type,
"structure_type_filter_note": (
"structure_type is not filterable at the database level; "
"all finished workflows of this type are included."
),
"count": 0,
"success_rate": 0.0,
"median_ecutwfc": None,
Expand Down Expand Up @@ -375,11 +376,6 @@ def _query_past_workflows(filters: dict[str, t.Any]) -> dict[str, t.Any]:
return {
"query_type": "past_successful_workflows",
"workflow_type": workflow_type,
"structure_type": structure_type,
"structure_type_filter_note": (
"structure_type is not filterable at the database level; "
"all finished workflows of this type are included."
),
"count": len(successful_runs),
"success_rate": success_rate,
"median_ecutwfc": med_ecutwfc,
Expand Down Expand Up @@ -411,7 +407,6 @@ def _query_available_codes(filters: dict[str, t.Any]) -> dict[str, t.Any]:
filters.get("code") or filters.get("plugin") or filters.get("code_label")
)
codes_list: list[dict[str, str]] = []
recommended: str | None = None
for row in results:
label, plugin, desc = row[0], row[1], row[2]
if code_filter and (
Expand All @@ -426,22 +421,18 @@ def _query_available_codes(filters: dict[str, t.Any]) -> dict[str, t.Any]:
"description": str(desc) if desc else "",
}
)
if not recommended or "pw" in str(label).lower():
recommended = str(label)

if not codes_list and code_filter and "unknown" in str(code_filter).lower():
return {
"query_type": "available_codes",
"codes": [],
"recommended_version": None,
"note": f"No codes found matching {code_filter!r} in active AiiDA profile.",
}

if not codes_list:
return {
"query_type": "available_codes",
"codes": [],
"recommended_version": None,
"note": (
"No codes found in the active AiiDA profile. "
"Please set up a code first: verdi code setup"
Expand All @@ -451,11 +442,10 @@ def _query_available_codes(filters: dict[str, t.Any]) -> dict[str, t.Any]:
return {
"query_type": "available_codes",
"codes": codes_list,
"recommended_version": recommended,
"note": (
f"Found {len(codes_list)} available code(s) matching filter in active AiiDA profile."
if results
else "No matching codes found in active AiiDA profile. Providing schema recommendation."
f"Found {len(codes_list)} code(s) in the active AiiDA profile. Pick one "
"by its entry point with list_codes(entry_point=...); nothing here "
"ranks them."
),
}

Expand Down Expand Up @@ -570,8 +560,5 @@ def _query_available_pseudos(filters: dict[str, t.Any]) -> dict[str, t.Any]:
"query_type": "available_pseudos",
"installed_families": pseudo_families,
"upf_data_count": upf_count,
"recommended_family": pseudo_families[0]["label"]
if pseudo_families
else "SSSP/1.3/PBE/efficiency (needs installation)",
"note": note,
}
70 changes: 64 additions & 6 deletions tests/tools/test_run_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_query_past_workflows_returns_expected_structure(self) -> None:
"""Querying past workflows must return required structured fields."""
res = query_run_context(
query_type="past_successful_workflows",
filters={"workflow_type": "PwRelaxWorkChain", "structure_type": "metallic"},
filters={"workflow_type": "PwRelaxWorkChain"},
)
assert res["query_type"] == "past_successful_workflows"
assert res["workflow_type"] == "PwRelaxWorkChain"
Expand All @@ -25,10 +25,6 @@ def test_query_past_workflows_returns_expected_structure(self) -> None:
assert "common_parameters" in res
assert "common_failure_modes" in res
assert "example_structures" in res
assert "structure_type_filter_note" in res, (
"structure_type_filter_note must be present so the model knows "
"structure_type is not a DB-level filter"
)

def test_query_available_codes_returns_list(self, arithmetic_add_code: Any) -> None:
"""Querying available codes returns a list of codes with expected structure.
Expand Down Expand Up @@ -92,7 +88,6 @@ def test_query_past_workflows_empty_results(self) -> None:
assert res["median_ecutwfc"] is None
assert res["common_parameters"] == {}
assert res["note"]
assert "structure_type_filter_note" in res

def test_query_available_codes_empty_results(self) -> None:
"""Querying an unknown code should return an empty codes list."""
Expand Down Expand Up @@ -320,3 +315,66 @@ def test_units_are_present_even_when_there_are_no_runs(self) -> None:

assert res["count"] == 0
assert res["units"]["ecutwfc"] == "Ry"


class TestTheToolStatesNothingItCannotSupport:
"""No invented subject, no invented qualifier, no unfounded endorsement.

Audit finding. Every item below was a value the tool supplied itself and
the caller could not tell apart from a queried one -- which is exactly what
the agents' grounding rules forbid the *model* from doing, enforced
everywhere except in the tool feeding it.
"""

def test_missing_workflow_type_is_an_error_not_a_guess(self) -> None:
"""It used to default to PwRelaxWorkChain and report that as the subject.

On a profile with 35 entry points, a caller who omitted the filter got
confident statistics about a workflow they never named.
"""
with pytest.raises(ValueError, match="needs filters"):
query_run_context(query_type="past_successful_workflows", filters={})

def test_no_structure_type_is_echoed_back(
self, multiply_add_workchain: Any
) -> None:
"""It used to default to "metallic" and return it as if asked.

Nothing filters on it and nothing verifies it, so a model could report
"for metallic structures, the median is ..." on a qualifier that came
from the tool rather than from the user or the database.
"""
res = query_run_context(
query_type="past_successful_workflows",
filters={"workflow_type": "MultiplyAddWorkChain"},
)

assert "structure_type" not in res
assert "structure_type_filter_note" not in res

def test_codes_are_not_ranked(self, arithmetic_add_code: Any) -> None:
"""`recommended_version` was "the first code, or any whose label has 'pw'".

That is not a recommendation, and the field name asserted an
endorsement the tool has no basis for -- while also holding a label
rather than a version.
"""
res = query_run_context(query_type="available_codes", filters={})

assert res["codes"], "fixture code should be found"
assert "recommended_version" not in res

def test_no_pseudo_family_is_named_when_none_is_installed(self) -> None:
"""The worst of them: it returned "SSSP/1.3/PBE/efficiency (needs installation)".

A family label for something not in the profile, in a field a model
would reasonably pass on as a workflow input. The note still tells the
user how to install one -- guidance in prose, not a usable-looking
reference.
"""
res = query_run_context(query_type="available_pseudos", filters={})

assert "recommended_family" not in res
assert "installed_families" in res
if not res["installed_families"]:
assert "aiida-pseudo install" in res["note"]
Loading