Agents: per-agent policy to replay a root agent's own tool calls from earlier turns

Aperta
#6,655 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
12/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
ai-infra-agents, sql, typescript

Direzione di ricerca

Start with BaseAgent.injectPriorTurnToolResults and PriorTurnToolResultCache, then readPromptStepStack, loadStoppedRunContinuation and RunAIAgentFromConversationDetail to see how history is built today. The proposal adds a per-agent replay policy with four modes, plus a lastRunId chain walk and a token budget. Done means the ToolCalls policy passes the three-turn, replaced-row, off-branch and byte-identical-prefix tests listed in the issue.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement

Summary

Add a per-agent policy for replaying a root agent's own tool calls from earlier turns into its history, so an orchestrator agent remembers what it did, not just what it said.

Today, between turns a root agent sees only the text of each conversation message. The only exceptions are a two-turn carry-forward of read-only tool results and a full stack rebuild for runs the user stopped. For simple chat agents that's a sensible default. For an orchestrator that delegates to sub-agents over a long conversation, it loses the information it most needs on the next turn:

  • what it delegated,
  • the brief it sent,
  • what came back,
  • which lookups it already did.

Current behavior

What MJ does today (on next):

  • History is text only. RunAIAgentFromConversationDetail builds history from ConversationDetail rows: the newest summary plus the tail, or the last N rows. Only each row's Message text is carried. No tool calls, action results, sub-agent result messages or reasoning from earlier turns.
  • Read-tool carry-forward. BaseAgent.injectPriorTurnToolResults with PriorTurnToolResultCache carries completed Tool step outputs from the previous settled root run of the same agent, limited to read-tool families (CarryForwardToolFamilies). They arrive as one user message (buildCarryForwardMessage) that expires after 2 turns. Only the direct predecessor is used, and only when the run has a conversationId.
  • Stopped-run resume. loadStoppedRunContinuation / tryResumeStoppedRunStack / readPromptStepStack already rebuild a run's full message stack from its Prompt steps' InputData.conversationMessages, but only for runs the user stopped (stopContinuationMode: 'full_stack').
  • lastRunId doesn't restore messages. It restores the payload (with autoPopulateLastRunPayload), bound action parameters and open sub-agent consults. The caller always supplies conversationMessages.
  • Thinking. It's replayed within a run on the native tool path (BuildAssistantToolCallTurn, with signatures), but never across turns.

So most of the machinery exists: rebuilding a stack from steps, carrying results forward, and the cache. It just isn't available as a general option for continuing a conversation turn to turn.

Why it matters

Agentic coding tools keep the main agent's own tool calls and results in the transcript across turns and compact them as the context fills. Sub-agent internals stay out: the parent sees only each sub-agent's final report. Anthropic's context-engineering guidance makes the same split. Keep the orchestrator's tool-call history, condense what sub-agents return, and clear old tool results (not the calls) once they're stale.

An MJ orchestrator built as a Loop agent with sub-agents can't do this today. On turn N+1 it doesn't know which sub-agent it called on turn N, what brief it gave, or what came back, unless that happened to be in its reply text. Follow-ups like "no, I meant renewals" or "why did you pick that table?" then depend on the model guessing.

Proposal

A per-agent replay policy

Add an agent field (or ExecuteAgentParams option, with the agent field as the default):

Policy Replays from earlier turns
None Nothing beyond the conversation text
ReadToolResults Today's carry-forward (the default, unchanged)
ToolCalls The agent's own tool calls / action invocations / sub-agent delegations with their briefs, plus results; results older than PriorTurnReplayKeepResults turns are replaced by a placeholder or their existing compacted form
Full The full rebuilt stack of each earlier turn, as stopped-run resume does today

What gets replayed:

  • Sub-agent results are replayed as the existing sub-agent-result message, which is already condensed. A sub-agent's internal steps are never replayed.
  • Reasoning and thinking blocks are excluded by default. They cost tokens on every turn, and they break when earlier history changes. If ever needed, they could be a separate opt-in.
Source: run steps, not a new store

Generalize readPromptStepStack so it can read any earlier root run, not just a stopped one. Each turn's record comes from that run's steps:

  • Native tool mode: the stored assistant tool-call turns plus tool results.
  • JSON envelope mode: the action-invocation and result annotations the loop already writes.
Interleaving with the conversation text

Each earlier root run is tied to the AI reply it produced; the cache already tracks the reply detail ID. Insert that run's turn record immediately before its reply message.

  • Runs whose reply row was replaced, or that sit off the current branch, are skipped.
  • Turns before a compaction boundary are covered by SummaryOfEarlierConversation and are not replayed.
Two ways to find earlier runs
  1. By conversationId, when MJ owns the conversation (MJ's own chat). This works like injectPriorTurnToolResults does today, but across more than one earlier turn.
  2. By the lastRunId chain, when the caller supplies conversationMessages itself because the conversation lives in another system or database. Walk LastRunID back up to PriorTurnReplayMaxTurns runs. External hosts can't use the conversationId path today, because the ConversationDetail rows aren't in the agent's database.
Limits and prompt caching
  • Limits: PriorTurnReplayMaxTurns, plus a token budget. When the budget is exceeded, the oldest replayed turns drop first, then compaction takes over.
  • Caching: replay is append-only from turn to turn, so the cached prefix stays stable. Clear old results in batches, so the prefix changes once per batch rather than every turn (the same idea as clear_at_least in Anthropic's context editing).

Out of scope

  • Replaying sub-agent internals.
  • Replaying thinking across turns.
  • Changing how ConversationDetail text history is windowed.

Tests

  • ToolCalls policy over a three-turn conversation: delegations and briefs from turns 1 and 2 are present on turn 3, and results older than the keep window are cleared.
  • A replaced reply row and an off-branch run are both excluded.
  • The lastRunId chain path works with caller-supplied conversationMessages and no conversationId.
  • With the default policy (ReadToolResults), behavior is unchanged.
  • The rendered prefix is byte-identical between two consecutive turns, apart from the appended turn.

Motivation and backport

This comes from Skip, an external host that runs its agents on its own MJ instance while the conversation lives in the client's MJ database. Skip is redesigning its top-level orchestrator as a Loop agent. It needs the lastRunId chain path and the ToolCalls policy. Skip is on the 6.2 LTS line, so a backport to lts/6.2 would be very helpful if the change is small enough to qualify.

🤖 Generated with Claude Code

Lingua principale
TSQL
Stelle
32
Fork
7
Merge medio
1g 16h
PR unite (30g)
504

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di MemberJunction/MJ

Tutte le issue di MemberJunction/MJ

Issue simili

Altre issue su AI Infra & Agents

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.