Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(swarm): universal data-citation discipline in worker prompt (#115)

Haozhe Wu8f49fe49024d
diff --git a/agent/src/swarm/worker.py b/agent/src/swarm/worker.py
index 8b38f8fd0..a2477ca7d 100644
--- a/agent/src/swarm/worker.py
+++ b/agent/src/swarm/worker.py
@@ -192,6 +192,36 @@ def build_worker_prompt(
# instruction to prefer these prices over training data.
prompt_parts.append(grounding_block)
+ # Universal anti-fabrication rule. The grounding_block carries a similar
+ # instruction but only renders when user_vars supplies explicit symbols.
+ # Free-form prompts ("look at A-share short-term sentiment") otherwise
+ # leave the worker with no guardrail and it cheerfully cites training-data
+ # prices and sector weights. This block applies the rule unconditionally
+ # — including to aggregator / synthesis agents that have no data tools
+ # and previously had no instruction against inventing numbers.
+ prompt_parts.append(
+ "## Data Citation Discipline (HARD RULE)\n\n"
+ "Every specific number you cite in your output — prices, percentages, "
+ "volumes, fund flows, market-cap rankings, sector weights, ETF codes, "
+ "ticker recommendations — MUST be traceable to one of:\n"
+ " (a) a tool call result obtained in THIS run,\n"
+ " (b) the Ground Truth block above (if present),\n"
+ " (c) the Upstream Context above (if present and the upstream agent "
+ "itself sourced it from (a) or (b)).\n\n"
+ "You may NOT cite numbers from memory or training data. Markets have "
+ "moved since your cutoff; any specific price/percentage you recall is "
+ "wrong by default.\n\n"
+ "If you cannot back a number with (a), (b), or (c), you have two "
+ "choices:\n"
+ " - call a data tool to fetch it (preferred), or\n"
+ " - omit the number and qualify the statement (e.g. \"directional "
+ "only — not verified against live data\").\n\n"
+ "This rule applies equally to synthesis / aggregator / editor roles "
+ "that lack data tools. If upstream did not provide a specific number, "
+ "do NOT introduce one from training data — say the upstream omitted "
+ "it and proceed without."
+ )
+
prompt_parts.append(
"## Execution Rules\n\n"
"You have a HARD LIMIT of 20 tool calls. After that you will be cut off. Work efficiently.\n\n"
diff --git a/agent/tests/test_swarm_grounding.py b/agent/tests/test_swarm_grounding.py
index b8ac6eba2..9f49275e5 100644
--- a/agent/tests/test_swarm_grounding.py
+++ b/agent/tests/test_swarm_grounding.py
@@ -205,7 +205,45 @@ def test_worker_prompt_includes_grounding_block_when_provided() -> None:
def test_worker_prompt_omits_grounding_section_when_block_empty() -> None:
prompt = build_worker_prompt(_spec(), {}, "(no matching skills)")
- assert "Ground Truth" not in prompt
+ # No rendered Ground Truth section. The Data Citation Discipline below
+ # may reference "Ground Truth block above (if present)" as a referent;
+ # that's fine. What must not appear is the actual section header.
+ assert "## Ground Truth" not in prompt
+
+
+def test_worker_prompt_always_includes_data_citation_discipline() -> None:
+ """Universal anti-fabrication rule: must appear regardless of whether
+ grounding_block or upstream_summaries were provided. Issue #106 reported
+ swarm reports citing prices the agent never actually fetched; the
+ grounding-block rule only covered runs with explicit symbols in user_vars."""
+ bare = build_worker_prompt(_spec(), {}, "(no matching skills)")
+ assert "Data Citation Discipline" in bare
+ assert "HARD RULE" in bare
+ assert "may NOT cite numbers from memory or training data" in bare
+
+ with_grounding = build_worker_prompt(
+ _spec(), {}, "(no matching skills)",
+ grounding_block="## Ground Truth — Recent Market Data\n\nNVDA.US ...",
+ )
+ assert with_grounding.count("Data Citation Discipline") == 1
+
+
+def test_worker_prompt_data_citation_discipline_precedes_execution_rules() -> None:
+ """The discipline must be in scope before Phase 1/2/3 execution rules
+ so the worker sees it while planning the first tool call."""
+ prompt = build_worker_prompt(_spec(), {}, "(no matching skills)")
+ assert prompt.index("Data Citation Discipline") < prompt.index("## Execution Rules")
+
+
+def test_worker_prompt_data_citation_rule_targets_aggregator_roles() -> None:
+ """The rule must explicitly address synthesis / aggregator agents
+ that lack data tools, since those were the worst-case path in #106
+ (equity_research_team aggregator has [bash, read_file, write_file] only)."""
+ prompt = build_worker_prompt(_spec(), {}, "(no matching skills)")
+ # Aggregators must be told not to invent numbers upstream omitted.
+ lowered = prompt.lower()
+ assert "synthesis" in lowered or "aggregator" in lowered
+ assert "upstream did not provide" in prompt
def test_runtime_threads_grounding_block_into_layer_workers(tmp_path, monkeypatch) -> None: