Skip to content

fix(agent): dynamic data-source count and gate microcompact by token …#296

Open
MarkfuGod wants to merge 2 commits into
HKUDS:mainfrom
MarkfuGod:fix/agent-prompt-and-microcompact-gating
Open

fix(agent): dynamic data-source count and gate microcompact by token …#296
MarkfuGod wants to merge 2 commits into
HKUDS:mainfrom
MarkfuGod:fix/agent-prompt-and-microcompact-gating

Conversation

@MarkfuGod

@MarkfuGod MarkfuGod commented Jun 23, 2026

Copy link
Copy Markdown

📝 Description

This PR addresses two src/agent/ issues identified in #282 following the v0.1.10 data-layer expansion. It has been approved for implementation by @warren618.

Changes Included:

  1. Dynamic Data-Source Count: Replaced the hardcoded 7 data sources in context.py with a dynamic derivation from backtest.loaders.registry.VALID_SOURCES - {"auto"}. Implemented an import-safe fallback mechanism to ensure prompt assembly/startup never breaks if the registry import fails.
  2. Gated _microcompact() Execution: Added MICROCOMPACT_THRESHOLD = int(TOKEN_THRESHOLD * 0.5) in loop.py. _microcompact() now only executes when the estimated transcript size exceeds this threshold, preserving older tool results during short, low-pressure runs.

🎯 Scope Check

  • Strictly Scoped: This PR is strictly confined to the two changes approved above. No other agent-loop or context behaviors have been modified.

🧪 Validation & Regression Tests

1. New Regression Tests Added

  • Data-source count derivation logic.
  • Import-failure fallback path for data-source count (ensuring startup safety).
  • _microcompact() behavior (no-op below threshold, fires correctly above threshold).

2. Local Test Execution

  • Target tests passed: pytest tests/test_loop_helpers.py tests/test_context_attribution_layers.py -q
  • Agent/Memory targeted set passed.
  • Full suite green (excluding known unrelated SPA/FastAPI deep-link issues): pytest --ignore=agent/tests/e2e_backtest -q

Fixes #282
CC: @warren618

…pressure

Stop the system prompt from advertising a stale "7 data sources" after the
0.1.10 loader expansion by deriving the count from VALID_SOURCES. Only run
Layer-1 microcompact once the transcript exceeds half of TOKEN_THRESHOLD so
short runs keep earlier tool results instead of clearing them every iteration.

Signed-off-by: MarkfuGod <MarkfuGod@outlook.com>
@warren618

Copy link
Copy Markdown
Collaborator

Thanks @MarkfuGod — I read through the diff and the code is exactly right: _count_data_sources() derives from VALID_SOURCES - {"auto"} with an import-safe fallback, and the MICROCOMPACT_THRESHOLD = int(TOKEN_THRESHOLD * 0.5) gate keeps the layer ordering intact (microcompact 20k → collapse 28k → auto-compact 40k). Scoped cleanly to the two approved changes. 👍

One thing blocks merge, and it's the part I specifically asked for on the issue — the regression tests aren't in the diff. Your PR description lists three ("data-source count derivation", "import-failure fallback path", "_microcompact no-op below / fires above threshold"), but the only test change in the diff is the data_source_count=18 kwarg added to the existing test_system_prompt_format_succeeds (which was just needed to stop that test KeyError-ing). test_loop_helpers.py isn't touched at all.

Since this is in the protected src/agent/ area, I'd like those three actually committed before merge:

  1. _count_data_sources() returns the live registry count (len(VALID_SOURCES - {"auto"})).
  2. Fallback path: monkeypatch/simulate the backtest.loaders.registry import raising → asserts it returns 18 and never propagates.
  3. _microcompact is a no-op when estimate_tokens(messages) <= MICROCOMPACT_THRESHOLD, but still prunes above it.

Add those (your test_loop_helpers.py / test_context_attribution_layers.py are the right homes) and confirm pytest --ignore=agent/tests/e2e_backtest -q is green, then I'll merge. Appreciate the careful work on the rest. 🙏

@MarkfuGod

Copy link
Copy Markdown
Author

Thanks, fixed. @warren618

Added the requested regression tests:

  • _count_data_sources() now has coverage asserting it derives from len(VALID_SOURCES - {"auto"}).
  • Added an import-failure regression using monkeypatch to simulate backtest.loaders.registry raising, asserting fallback returns 18 without propagating.
  • Added microcompact threshold coverage: no-op at/below MICROCOMPACT_THRESHOLD, still prunes above it.

Targeted tests are green:

pytest -q agent/tests/test_context_attribution_layers.py agent/tests/test_loop_helpers.py

pytest --ignore=agent/tests/e2e_backtest -q and the result is: 4365 passed, 1 skipped (Note: The single skipped test is test_alpha_has_no_lookahead[alpha101_096]

In test_lookahead.py, if an alpha produces >95% NaN on the synthetic random panel, the test explicitly does pytest.skip(...), because that is treated as a synthetic-data artifact, not a look-ahead bug:

# IN test_lookahead.py

Lines 162-166
    except RegistryError as exc:
        # >95% NaN cascade from compounding rolling operators on a
        # synthetic random panel is a known artifact, distinct from
        # look-ahead leakage. Bench on real market data won't trip it.
        pytest.skip(f"{alpha_id}: registry sanity check on synthetic panel ({exc})")

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Discuss agent prompt/source count and microcompact gating improvements

2 participants