Skip to content

Feat: Implement Trajectory Thought Pruning in adk-js#451

Merged
AmaadMartin merged 2 commits into
google:mainfrom
AmaadMartin:feat/trajectory-thought-pruning
Jun 30, 2026
Merged

Feat: Implement Trajectory Thought Pruning in adk-js#451
AmaadMartin merged 2 commits into
google:mainfrom
AmaadMartin:feat/trajectory-thought-pruning

Conversation

@AmaadMartin

Copy link
Copy Markdown
Collaborator

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

  1. Link to an existing issue (if applicable):
    Closes: #none

  2. Or, if no issue exists, describe the change:
    This PR implements Trajectory Thought Pruning (ReAct Step Distillation) in adk-js to reduce token consumption in long-running agent sessions.

Problem:
Long-running agent sessions consume a large number of tokens because they keep the full history of internal LLM thinking/thought parts from older turns.

Solution:

  1. Introduced TrajectoryThoughtPruningCompactor that implements BaseContextCompactor. It prunes parts with thought: true from events older than eventRetentionSize.
  2. Updated BaseSessionService and DatabaseSessionService to support upsert in appendEvent (updating existing events if the ID matches). This allows persisting pruned events without creating duplicates or causing primary key violations.

Testing Plan
Unit and integration tests have been implemented and pass cleanly.

Unit Tests:

  • Added core/test/context/trajectory_thought_pruning_compactor_test.ts covering constructor validation, shouldCompact, and compact logic (including edge cases).
  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Integration Tests:

  • Added tests/integration/context_compaction/trajectory_thought_pruning_test.ts verifying that thoughts are pruned correctly over a multi-turn conversation and that the changes are persisted correctly via SessionService.
  • Run with: npx vitest run tests/integration/context_compaction/trajectory_thought_pruning_test.ts

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

@AmaadMartin AmaadMartin reopened this Jun 26, 2026
Comment on lines +66 to +82
private hasThoughts(event: Event): boolean {
return !!event.content?.parts?.some((part) => part.thought === true);
}

private pruneThoughts(event: Event): Event {
const prunedParts = event.content!.parts!.filter(
(part) => part.thought !== true,
);

return {
...event,
content: {
...event.content!,
parts: prunedParts,
},
};
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

util functions

@AmaadMartin AmaadMartin merged commit 584ce87 into google:main Jun 30, 2026
12 checks passed
@kalenkevich kalenkevich mentioned this pull request Jun 30, 2026
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.

2 participants