[Core SDK] _chat_impl and _achat_impl duplicate identical BEFORE_AGENT + prompt-update fragments (already drifting)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
- #5396 di @praisonai-triage-agent — chiusa senza merge
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
Start in praisonaiagents/agent/chat_mixin.py: compare the BEFORE_AGENT input construction and the hook prompt-update loop in _chat_impl (~line 3232) and _achat_impl (~line 4013). Decide which tools_available source is correct, then extract shared non-awaiting helpers, keeping each method's has_hooks guard and its own execute call. Validate with the tests in tests/unit/agent/ and the BEFORE_AGENT hook tests. Done means both paths share one helper and the existing tests pass unchanged.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
In praisonaiagents/agent/chat_mixin.py, the two longest methods in the package — _chat_impl (sync, ~line 3232 onwards) and _achat_impl (async, ~line 4013 onwards) — carry copy-pasted, non-awaiting fragments (the BEFORE_AGENT hook-input construction and the hook prompt-update loop, plus response-template application). The copies have already drifted, which is exactly the maintenance risk duplication creates. This is a duplicate-logic consolidation: extract the identical fragments into shared helpers while keeping the sync/async method split intact.
Current behaviour
The BEFORE_AGENT block appears in both methods. The prompt-update loop is byte-for-byte identical:
# _chat_impl (~3285) and _achat_impl (~4058) — IDENTICAL
for res in hook_results:
if res.output and res.output.modified_input and "prompt" in res.output.modified_input:
prompt = res.output.modified_input["prompt"]
llm_prompt = self._build_multimodal_prompt(prompt, attachments) if attachments else prompt
The BeforeAgentInput(...) construction is duplicated too — and has already diverged:
_chat_impl(~3273):prompt=prompt_str,tools_available=[... for t in self.tools]_achat_impl(~4047):prompt=prompt if isinstance(prompt, str) else str(prompt),tools_available=[... for t in (tools or self.tools)]
So the sync path reports self.tools to BEFORE_AGENT hooks while the async path reports tools or self.tools — an unintended inconsistency that slipped in precisely because the block is maintained in two places. Response-template application (_chat_impl ~3243–3257) is similarly mirrored.
Why it matters
- Maintenance duplication in the two longest methods in the SDK. Any change to hook-input fields or prompt-rewrite semantics must be made twice; the
tools_availabledivergence shows this has already gone wrong once. - Consolidating the identical, non-awaiting fragments removes the drift surface without touching the async control flow.
Category
Duplicate
Capability preserved
- Both
chat/achatentry points, the sync and async execution paths, and theBEFORE_AGENThook contract remain exactly as they are. - The sync/async split, cancellation-token handling, turn-tracking, and
execute_syncvsawait executedispatch are all retained — only the shared, non-awaiting body is factored out. - Hook blocking (
is_blocked→ returnNone) and prompt-modification behaviour are unchanged.
Proposed approach
Merge duplicate logic (not the methods). Extract small helpers used by both paths, e.g.:
_build_before_agent_input(prompt_str, tools)→ returns theBeforeAgentInput(this also lets thetools_availablesource be defined once, reconciling the drift as a deliberate choice rather than an accident)._apply_hook_prompt_updates(hook_results, prompt, attachments)→ returns the possibly-updated(prompt, llm_prompt).
Each method keeps its own has_hooks guard and its own dispatch line (execute_sync vs await execute); only the identical fragments move into the helpers. Do not merge the two methods' control flow.
Resolution sketch
# chat_mixin.py — shared helper (new)
def _apply_hook_prompt_updates(self, hook_results, prompt, attachments):
for res in hook_results:
if res.output and res.output.modified_input and "prompt" in res.output.modified_input:
prompt = res.output.modified_input["prompt"]
llm_prompt = self._build_multimodal_prompt(prompt, attachments) if attachments else prompt
return prompt, llm_prompt
# _chat_impl (sync) # _achat_impl (async)
hook_results = self._hook_runner.execute_sync(...) hook_results = await self._hook_runner.execute(...)
if self._hook_runner.is_blocked(hook_results): if self._hook_runner.is_blocked(hook_results):
return None return None
prompt, llm_prompt = self._apply_hook_prompt_updates(hook_results, prompt, attachments)
Severity
Medium (structural maintenance cost in the two most-edited methods; verified drift already present)
Validation
- Duplication confirmed by reading both regions; the prompt-update loop is identical and the
BeforeAgentInputconstruction differs only in the two fields noted. - Fix is capability-preserving: helpers contain no
await; the async method keepsawait self._hook_runner.execute(...). - Must be validated against the existing chat/hook tests (
tests/unit/agent/, hook/middleware suites) and theBEFORE_AGENThook tests before/after, since it touches the execution hot path.
Keep unchanged
- The separate
_chat_impl/_achat_implmethods and the sync/async boundary. - Async-only concerns: cancellation token,
_clear_turn_tracking, coroutine handling. - The
has_hooksfast-path guard (no input is built when noBEFORE_AGENThook is registered). - All public
chat/achatsignatures and semantics.
- Lingua principale
- Python
- Stelle
- 9.3k
- Fork
- 1.5k
- Merge medio
- 1g 5h
- PR unite (30g)
- 520
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di MervinPraison/PraisonAI
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
MervinPraison/PraisonAI#6710 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
claude
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
MervinPraison/PraisonAI#6707 ·
I maintainer di solito rispondono entro 1 giorno
-
bug claude
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
MervinPraison/PraisonAI#6671 ·
I maintainer di solito rispondono entro 1 giorno
-
Compaction budget regression fixture remains below budget with accurate tokenizationForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
MervinPraison/PraisonAI#5418 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
bug claude documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
MervinPraison/PraisonAI#5341 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di MervinPraison/PraisonAI
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 2 giorni
-
[Bug]: LANGSMITH_SAMPLING_RATE ignores fractional values (isdigit check), traces 100% of requestsAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
platform/backend tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Significant-Gravitas/AutoGPT#17100 ·
I maintainer di solito rispondono entro 1 giorno
-
bug good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 61/100
scverse/infercnvpy#238 ·
I maintainer di solito rispondono entro 1 giorno