Skip to content

Agentic tool loop resends full conversation history after every tool call #20

Description

@lyrishark

Summary

EntityTurn.process() builds the model message array once before entering the
agentic loop. After each tool call, it appends the assistant call and tool
result to that same array, then sends the entire array to the provider again.

For long-running conversations, every follow-up shell or other tool step can
therefore resend tens of thousands of unrelated historical tokens. The tool
itself is not reading chat history; the repeated model call surrounding each
tool invocation is.

Impact

  • Multi-step troubleshooting becomes progressively slower and more expensive.
  • Whole-turn watchdogs can stop an otherwise healthy tool chain before the
    entity finishes.
  • Each tool result increases an already-large prompt.
  • A captured report showed consecutive requests of approximately 88,684 and
    90,384 prompt tokens around a single tool round trip.

Local patch

  • Added packages/psycheros/src/entity/tool-loop-context.ts.
  • The first model pass remains unchanged and receives the normal conversation
    context.
  • After the first tool result, follow-up model calls retain:
    • the full system/core context,
    • the complete active user turn,
    • all tool calls and results from that active turn,
    • as many recent complete conversation turns as fit in a 16,000-token
      history budget.
  • Older turns are retained or removed as complete user-anchored units so an
    assistant tool call is never separated from its tool result.
  • The shell tool description now encourages batching independent read-only
    inspections into one command when practical.

The compaction only changes the transient provider payload during an active
tool loop. It does not modify stored conversations, identity, or memories.

Verification

  • deno check src/main.ts passed in the repository, packaged overlay, and live
    AppData source.
  • New compaction tests cover small contexts, long histories, complete recent
    turns, active tool-call/result pairing, and oversized active turns.
  • Focused package suite: 29 passed, 0 failed.
  • The double-click AppData fixer was tested against an isolated fake install;
    all files matched and all replaced files received backups.
  • The live Windows daemon restarted successfully and returned HTTP 200 from
    /health.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions