Skip to content

feat(tarko-agent): restore events on agent session restore#1548

Merged
ulivz merged 8 commits intomainfrom
fix/agent-session-context-restore
Sep 17, 2025
Merged

feat(tarko-agent): restore events on agent session restore#1548
ulivz merged 8 commits intomainfrom
fix/agent-session-context-restore

Conversation

@ulivz
Copy link
Member

@ulivz ulivz commented Sep 17, 2025

Summary

Fixes Agent Session context loss when server restarts or model switches by automatically restoring stored events to new agent instances.

Before

image

After

image

Checklist

  • Added or updated necessary tests (Optional).
  • Updated documentation to align with changes (Optional).
  • Verified no breaking changes, or prepared solutions for any occurring breaking changes (Optional).
  • My change does not involve the above items.

@netlify
Copy link

netlify bot commented Sep 17, 2025

Deploy Preview for agent-tars-docs ready!

Name Link
🔨 Latest commit a056b71
🔍 Latest deploy log https://app.netlify.com/projects/agent-tars-docs/deploys/68ca79e6633fc90008e327f5
😎 Deploy Preview https://deploy-preview-1548--agent-tars-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@ulivz ulivz changed the title fix(tarko-agent-server): restore events from storage on agent session context restore feat(tarko-agent): restore events on agent session restore Sep 17, 2025
… 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.
@ulivz ulivz force-pushed the fix/agent-session-context-restore branch from 6507da7 to 5f86bef Compare September 17, 2025 08:48
@ulivz ulivz changed the title feat(tarko-agent): restore events on agent session restore fix(tarko-agent): restore events on agent session restore Sep 17, 2025
@ulivz ulivz changed the title fix(tarko-agent): restore events on agent session restore feat(tarko-agent): restore events on agent session restore Sep 17, 2025
@ulivz ulivz merged commit 1282b70 into main Sep 17, 2025
10 checks passed
@ulivz ulivz deleted the fix/agent-session-context-restore branch September 17, 2025 09:43
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.

1 participant

Comments