feat(tarko-agent): restore events on agent session restore#1548
Merged
Conversation
✅ Deploy Preview for agent-tars-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
… context restore Resolves Agent Session context loss when server restarts or model switches by: - Adding restoreEventsFromStorage method to AgentSession - Automatically restoring stored events when creating new agent instances - Preventing duplicate event storage with sessionStartTime tracking - Ensuring context continuity across agent instance recreations Tested with comprehensive unit tests covering: - Event restoration from storage - No event duplication on model config updates - Graceful handling of empty storage
…or elegant context restore Improves the previous implementation by: - Adding restoreEvents() method to AgentEventStream.Processor interface - Implementing batch event restoration without triggering subscribers - Simplifying AgentSession event handler (removing timestamp logic) - Providing cleaner separation of concerns between event restoration and real-time events This approach is more elegant than the previous sendEvent loop as it: - Avoids triggering event subscribers during restoration - Prevents potential side effects from restored events - Provides better performance for large event histories - Maintains cleaner code architecture
… elegant context restore Implements the more elegant solution by: - Adding initialEvents option to AgentOptions interface - Modifying Agent constructor to accept and store initial events - Overriding Agent.initialize() to restore events during initialization - Updating AgentSession to pass events directly to Agent constructor - Removing the separate restoreEventsFromStorage method - Updating MockAgent to support initialEvents for testing This approach provides: - Better encapsulation: Agent manages its own initialization completely - Simpler API: External code only needs to pass initialEvents to constructor - Safer timing: Events are restored at the correct moment during initialization - Better separation of concerns: AgentSession focuses on coordination, Agent handles its own state - More extensible: Can easily add more initialization options in the future All 60 tests pass, confirming backward compatibility.
…nts - the most elegant solution You were absolutely right! The simplest solution is to pass initialEvents directly to the EventStream constructor. Changes: - Add initialEvents option to AgentEventStream.ProcessorOptions interface - Modify AgentEventStreamProcessor constructor to accept and restore initial events - Update Agent to pass initialEvents through eventStreamOptions to EventStream - Remove unnecessary Agent.initialize() override and initialEvents property - Update MockEventStream to support initialEvents in constructor - Simplify MockAgent to use new EventStream constructor pattern This approach is the most elegant because: - ✅ Minimal code changes - just add one option to existing interface - ✅ Single responsibility - EventStream handles its own initialization - ✅ No complex Agent logic - just pass options through - ✅ Natural flow - events are restored when EventStream is created - ✅ Perfect encapsulation - EventStream manages its own state All 60 tests pass. This is indeed the simplest and most elegant solution!
…uctor pattern makes it obsolete With the new EventStream constructor initialEvents pattern, the restoreEvents() method is no longer needed: - ❌ Removed restoreEvents() from AgentEventStream.Processor interface - ❌ Removed restoreEvents() implementation from AgentEventStreamProcessor - ❌ Removed restoreEvents() from MockEventStream Reason for removal: - No external usage found in codebase (agent-snapshot, gui-agent, etc.) - Constructor pattern with initialEvents covers all use cases - Reduces API surface area and complexity - Single responsibility: EventStream manages its own initialization All 60 tests pass, confirming no functionality is broken.
Removed unused convenience methods that were added for interface completeness but not needed by tests: - subscribeToTypes() - not used in any test - subscribeToStreamingEvents() - not used in any test - getLatestToolResults() - not used in any test Keeps MockEventStream minimal and focused on actual test requirements. All tests still pass, confirming these methods were indeed unnecessary.
Deleted a massive block of empty method implementations that were never used by any tests: - getLLMClient, getCurrentModel, onLLMRequest, onLLMResponse, etc. - onEachAgentLoopStart, onAgentLoopEnd, requestLoopTermination, etc. - callLLM, onPrepareRequest, and many other lifecycle methods MockAgent now only implements methods actually needed by tests. All tests still pass, confirming these 38 lines were pure bloat.
6507da7 to
5f86bef
Compare
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes Agent Session context loss when server restarts or model switches by automatically restoring stored events to new agent instances.
Before
After
Checklist